PIPS
cprettyprinter.c File Reference
#include <stdio.h>
#include <ctype.h>
#include "genC.h"
#include "linear.h"
#include "properties.h"
#include "misc.h"
#include "pipsdbm.h"
#include "ri.h"
#include "effects.h"
#include "ri-util.h"
#include "prettyprint.h"
#include "effects-util.h"
#include "text-util.h"
+ Include dependency graph for cprettyprinter.c:

Go to the source code of this file.

Data Structures

struct  s_ppt
 
struct  c_full_name
 

Macros

#define EMPTY   ""
 
#define NL   "\n"
 
#define SEMICOLON   ";" NL
 
#define SPACE   " "
 
#define OPENBRACKET   "["
 
#define CLOSEBRACKET   "]"
 
#define OPENPAREN   "("
 
#define CLOSEPAREN   ")"
 
#define OPENBRACE   "{"
 
#define CLOSEBRACE   "}"
 
#define SHARPDEF   "#define"
 
#define COMMENT   "//" SPACE
 
#define INDENT   "indent"
 
#define CROUGH   ".crough"
 
#define CPRETTY   ".c"
 
#define INTERFACE   "_interface.f08"
 
#define SCALAR_IN_SIG_EXT   "_p4a_copy"
 
#define MAX_FCT   "crough_max"
 
#define MIN_FCT   "crough_min"
 
#define MAX_DEF   "#define " MAX_FCT "(a,b) (((a)>(b))?(a):(b))\n"
 
#define MIN_DEF   "#define " MIN_FCT "(a,b) (((a)<(b))?(a):(b))\n"
 
#define POW_PRE   "crough_"
 
#define POW_DEF   "#define " POW_PRE "powi(a,b) ((a)^(b))\n"
 
#define CMPLX_FCT   "init_complex"
 
#define CMPLX_DEF   "#define " CMPLX_FCT "(a,b) (a + b*I)\n"
 
#define current_module_is_a_function()    (entity_function_p(get_current_module_entity()))
 
#define RESULT_NAME   "result"
 
#define MAIN_DECLARATION   "int main(int argc, char *argv[])" NL
 returns the head of the function/subroutine/program. More...
 
#define RET   "return"
 
#define CONT   "continue"
 

Typedefs

typedef string(* prettyprinter) (const char *, list)
 generate a basic c expression. More...
 

Functions

static string c_expression (expression, bool)
 forward declaration. More...
 
static bool convert_double_value (char **str)
 test if the string looks like a REAL*8 (double in C) declaration i.e something like 987987D54654 : a bunch of digit with a letter in the middle. More...
 
static void const_wrapper (char **s)
 
static char * c_entity_local_name (entity var)
 
static void build_written_list (list l)
 
static bool written_p (entity e)
 
static string scalar_prelude ()
 
static string scalar_postlude ()
 
static bool scalar_by_pointer (entity var)
 we want to decide if a scalar variable need to be passed by pointer or by value to a C function. More...
 
static string c_basic_string (basic b)
 
static string c_type_string (type t)
 
static string c_dim_string (list ldim, bool fct_sig)
 
static string c_qualifier_string (list l)
 
static string c_brace_expression_string (expression exp)
 
static string this_entity_cdeclaration (entity var, bool fct_sig)
 
static bool parameter_p (entity e)
 
static bool variable_p (entity e)
 
static bool parameter_or_variable_p (entity e)
 
static bool argument_p (entity e)
 
static string c_declarations (entity module, bool(*consider_this_entity)(entity), string separator, bool lastsep, bool fct_sig)
 
static string c_include (void)
 
static string c_macro (void)
 
static string c_head (entity module)
 
static bool expression_needs_parenthesis_p (expression)
 
static string ppt_binary (const char *in_c, list le)
 
static string ppt_unary (const char *in_c, list le)
 
static string ppt_unary_post (const char *in_c, list le)
 
static string ppt_call (const char *in_c, list le)
 SG: PBM spotted HERE. More...
 
static void get_c_full_name (string *base_in_c, basic b)
 fill the c_base_name to get the c full name accorgind to its basic More...
 
static string ppt_math (const char *in_c, list le)
 
static string ppt_min_max (const char *in_c, list le)
 
static string ppt_unknown (const char *in_f, list le)
 
static string ppt_must_error (const char *in_f, _UNUSED_ list le)
 
static struct s_pptget_ppt (entity f)
 return the prettyprinter structure for c. More...
 
static string c_call (call c, bool breakable)
 
static string c_reference (reference r)
 Attention with Fortran: the indexes are reversed. More...
 
static string c_statement (statement s, bool breakable)
 
static string c_unstructured (unstructured u, bool breakable)
 
static string c_test (test t, bool breakable)
 
static string c_sequence (sequence seq, bool breakable)
 
static string c_loop (loop l)
 
static string c_whileloop (whileloop w)
 
static string c_forloop (forloop f)
 
static string interface_type_string (type t, bool value)
 
static string interface_basic_string (basic b, bool value)
 Convert the fortran basic to its interface string value. More...
 
static string interface_argument_type_string (entity var)
 return a string representation of the type to be used for a variable decalaration in an interface module in order to ensure that the C function can be called from fotran codes More...
 
static string interface_argument_declaration (entity module, string separator, string indent)
 
static string interface_signature (entity module)
 return the interface signature for a module, i.e. More...
 
static string interface_code_string (entity module, _UNUSED_ statement stat)
 
static string c_code_string (entity module, statement stat)
 
bool print_interface (const char *module_name)
 cprettyprinter.c More...
 
bool print_crough (const char *module_name)
 
bool print_c_code (const char *module_name)
 C indentation thru indent. More...
 

Variables

static list l_type = NIL
 
static list l_name = NIL
 
static list l_rename = NIL
 
static list l_entity = NIL
 
static list l_written = NIL
 
static c_full_name c_base_name_to_c_full_name []
 
static struct s_ppt intrinsic_to_c []
 

Macro Definition Documentation

◆ CLOSEBRACE

#define CLOSEBRACE   "}"

Definition at line 74 of file cprettyprinter.c.

◆ CLOSEBRACKET

#define CLOSEBRACKET   "]"

Definition at line 68 of file cprettyprinter.c.

◆ CLOSEPAREN

#define CLOSEPAREN   ")"

Definition at line 71 of file cprettyprinter.c.

◆ CMPLX_DEF

#define CMPLX_DEF   "#define " CMPLX_FCT "(a,b) (a + b*I)\n"

Definition at line 96 of file cprettyprinter.c.

◆ CMPLX_FCT

#define CMPLX_FCT   "init_complex"

Definition at line 95 of file cprettyprinter.c.

◆ COMMENT

#define COMMENT   "//" SPACE

Definition at line 77 of file cprettyprinter.c.

◆ CONT

#define CONT   "continue"

Definition at line 1399 of file cprettyprinter.c.

◆ CPRETTY

#define CPRETTY   ".c"

Definition at line 82 of file cprettyprinter.c.

◆ CROUGH

#define CROUGH   ".crough"

Definition at line 81 of file cprettyprinter.c.

◆ current_module_is_a_function

#define current_module_is_a_function ( )     (entity_function_p(get_current_module_entity()))

Definition at line 112 of file cprettyprinter.c.

◆ EMPTY

#define EMPTY   ""

Definition at line 62 of file cprettyprinter.c.

◆ INDENT

#define INDENT   "indent"

Definition at line 80 of file cprettyprinter.c.

◆ INTERFACE

#define INTERFACE   "_interface.f08"

Definition at line 83 of file cprettyprinter.c.

◆ MAIN_DECLARATION

#define MAIN_DECLARATION   "int main(int argc, char *argv[])" NL

returns the head of the function/subroutine/program.

declarations look ANSI C.

Definition at line 903 of file cprettyprinter.c.

◆ MAX_DEF

#define MAX_DEF   "#define " MAX_FCT "(a,b) (((a)>(b))?(a):(b))\n"

Definition at line 91 of file cprettyprinter.c.

◆ MAX_FCT

#define MAX_FCT   "crough_max"

Definition at line 89 of file cprettyprinter.c.

◆ MIN_DEF

#define MIN_DEF   "#define " MIN_FCT "(a,b) (((a)<(b))?(a):(b))\n"

Definition at line 92 of file cprettyprinter.c.

◆ MIN_FCT

#define MIN_FCT   "crough_min"

Definition at line 90 of file cprettyprinter.c.

◆ NL

#define NL   "\n"

Definition at line 63 of file cprettyprinter.c.

◆ OPENBRACE

#define OPENBRACE   "{"

Definition at line 73 of file cprettyprinter.c.

◆ OPENBRACKET

#define OPENBRACKET   "["

Definition at line 67 of file cprettyprinter.c.

◆ OPENPAREN

#define OPENPAREN   "("

Definition at line 70 of file cprettyprinter.c.

◆ POW_DEF

#define POW_DEF   "#define " POW_PRE "powi(a,b) ((a)^(b))\n"

Definition at line 94 of file cprettyprinter.c.

◆ POW_PRE

#define POW_PRE   "crough_"

Definition at line 93 of file cprettyprinter.c.

◆ RESULT_NAME

#define RESULT_NAME   "result"

Definition at line 116 of file cprettyprinter.c.

◆ RET

#define RET   "return"

Definition at line 1398 of file cprettyprinter.c.

◆ SCALAR_IN_SIG_EXT

#define SCALAR_IN_SIG_EXT   "_p4a_copy"

Definition at line 86 of file cprettyprinter.c.

◆ SEMICOLON

#define SEMICOLON   ";" NL

Definition at line 64 of file cprettyprinter.c.

◆ SHARPDEF

#define SHARPDEF   "#define"

Definition at line 76 of file cprettyprinter.c.

◆ SPACE

#define SPACE   " "

Definition at line 65 of file cprettyprinter.c.

Typedef Documentation

◆ prettyprinter

typedef string(* prettyprinter) (const char *, list)

generate a basic c expression.

no operator priority is assumed...

Definition at line 967 of file cprettyprinter.c.

Function Documentation

◆ argument_p()

static bool argument_p ( entity  e)
static
Returns
true if the entity is an argument

Formal variables

Definition at line 792 of file cprettyprinter.c.

793 {
794  /* Formal variables */
795  return type_variable_p(entity_type(e)) &&
797 }
#define storage_formal_p(x)
Definition: ri.h:2522
#define entity_storage(x)
Definition: ri.h:2794
#define entity_type(x)
Definition: ri.h:2792
#define type_variable_p(x)
Definition: ri.h:2947

References entity_storage, entity_type, storage_formal_p, and type_variable_p.

Referenced by c_head(), interface_argument_declaration(), interface_argument_type_string(), and interface_signature().

+ Here is the caller graph for this function:

◆ build_written_list()

static void build_written_list ( list  l)
static

Definition at line 211 of file cprettyprinter.c.

211  {
212  list l_effects = l;
213  FOREACH (EFFECT, eff, l_effects) {
214  if (effect_write_p (eff)) {
215  entity e = effect_any_entity (eff);
217  pips_debug (5, "entity %s (%p) is written\n", entity_local_name (e), e);
218  } else {
219  entity e = effect_any_entity (eff);
220  pips_debug (5, "entity %s (%p) is not written\n", entity_local_name (e), e);
221  }
222  }
223 }
list gen_entity_cons(entity p, list l)
Definition: ri.c:2537
static list l_written
#define effect_write_p(eff)
#define effect_any_entity(e)
some useful SHORTHANDS for EFFECT:
#define EFFECT(x)
EFFECT.
Definition: effects.h:608
#define FOREACH(_fe_CASTER, _fe_item, _fe_list)
Apply/map an instruction block on all the elements of a list.
Definition: newgen_list.h:179
#define pips_debug
these macros use the GNU extensions that allow variadic macros, including with an empty list.
Definition: misc-local.h:145
const char * entity_local_name(entity e)
entity_local_name modified so that it does not core when used in vect_fprint, since someone thought t...
Definition: entity.c:453
The structure used to build lists in NewGen.
Definition: newgen_list.h:41

References EFFECT, effect_any_entity, effect_write_p, entity_local_name(), FOREACH, gen_entity_cons(), l_written, and pips_debug.

Referenced by print_crough().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ c_basic_string()

static string c_basic_string ( basic  b)
static

An expression indeed... To be fixed...

ignore if it is signed or unsigned

c99 style with include of complex.h

Definition at line 345 of file cprettyprinter.c.

346 {
347  const char* result = "UNKNOWN_BASIC" SPACE;
348  char * aresult=NULL;
349  bool user_type = get_bool_property ("CROUGH_USER_DEFINED_TYPE");
350  switch (basic_tag(b)) {
351  case is_basic_int: {
352  pips_debug(2,"Basic int\n");
353  if (user_type == false) {
354  switch (basic_int(b)) {
355  case 1: result = "char" SPACE;
356  break;
357  case 2: result = "short" SPACE;
358  break;
359  case 4: result = "int" SPACE;
360  break;
361  case 6: result = "long" SPACE;
362  break;
363  case 8: result = "long long" SPACE;
364  break;
365  case 11: result = "unsigned char" SPACE;
366  break;
367  case 12: result = "unsigned short" SPACE;
368  break;
369  case 14: result = "unsigned int" SPACE;
370  break;
371  case 16: result = "unsigned long" SPACE;
372  break;
373  case 18: result = "unsigned long long" SPACE;
374  break;
375  case 21: result = "signed char" SPACE;
376  break;
377  case 22: result = "signed short" SPACE;
378  break;
379  case 24: result = "signed int" SPACE;
380  break;
381  case 26: result = "signed long" SPACE;
382  break;
383  case 28: result = "signed long long" SPACE;
384  break;
385  }
386  } else {
387  result = get_string_property ("CROUGH_INTEGER_TYPE");
388  }
389  break;
390  }
391  case is_basic_float: {
392  if (user_type == false) {
393  switch (basic_float(b)){
394  case 4: result = "float" SPACE;
395  break;
396  case 8: result = "double" SPACE;
397  break;
398  }
399  } else {
400  result = get_string_property ("CROUGH_REAL_TYPE");
401  }
402  break;
403  }
404  case is_basic_logical:
405  result = "int" SPACE;
406  break;
407  case is_basic_string:
408  result = "char" SPACE;
409  break;
410  case is_basic_bit:
411  {
412  /* An expression indeed... To be fixed... */
413  _int i = (_int) basic_bit(b);
414  pips_debug(2,"Bit field basic: %td\n", i);
415  result = "int" SPACE; /* ignore if it is signed or unsigned */
416  break;
417  }
418  case is_basic_pointer:
419  {
420  type t = basic_pointer(b);
421  pips_debug(2,"Basic pointer\n");
422  result = concatenate(c_type_string(t),"* ",NULL);
423  break;
424  }
425  case is_basic_derived:
426  {
427  entity ent = basic_derived(b);
428  type t = entity_type(ent);
429  char* name = c_entity_local_name(ent);
430  result = concatenate(c_type_string(t),name,NULL);
431  free(name);
432  break;
433  }
434  case is_basic_typedef:
435  {
436  entity ent = basic_typedef(b);
437  aresult = c_entity_local_name(ent);
438  break;
439  }
440  case is_basic_complex:
441  result = "complex" SPACE; /* c99 style with include of complex.h*/
442  break;
443  default:
444  pips_internal_error("unhandled case");
445  }
446  return aresult ? aresult : strdup(result);
447 }
static string c_type_string(type t)
static char * c_entity_local_name(entity var)
#define SPACE
char * get_string_property(const char *)
bool get_bool_property(const string)
FC 2015-07-20: yuk, moved out to prevent an include cycle dependency include "properties....
void free(void *)
#define pips_internal_error
Definition: misc-local.h:149
string concatenate(const char *,...)
Return the concatenation of the given strings.
Definition: string.c:183
intptr_t _int
_INT
Definition: newgen_types.h:53
@ is_basic_derived
Definition: ri.h:579
@ is_basic_string
Definition: ri.h:576
@ is_basic_float
Definition: ri.h:572
@ is_basic_bit
Definition: ri.h:577
@ is_basic_pointer
Definition: ri.h:578
@ is_basic_int
Definition: ri.h:571
@ is_basic_logical
Definition: ri.h:573
@ is_basic_typedef
Definition: ri.h:580
@ is_basic_complex
Definition: ri.h:575
#define basic_pointer(x)
Definition: ri.h:637
#define basic_derived(x)
Definition: ri.h:640
#define basic_int(x)
Definition: ri.h:616
#define basic_tag(x)
Definition: ri.h:613
#define basic_typedef(x)
Definition: ri.h:643
#define basic_float(x)
Definition: ri.h:619
#define basic_bit(x)
Definition: ri.h:634
char * strdup()

References basic_bit, basic_derived, basic_float, basic_int, basic_pointer, basic_tag, basic_typedef, c_entity_local_name(), c_type_string(), concatenate(), entity_type, free(), get_bool_property(), get_string_property(), is_basic_bit, is_basic_complex, is_basic_derived, is_basic_float, is_basic_int, is_basic_logical, is_basic_pointer, is_basic_string, is_basic_typedef, pips_debug, pips_internal_error, SPACE, and strdup().

Referenced by c_head(), c_type_string(), and this_entity_cdeclaration().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ c_brace_expression_string()

static string c_brace_expression_string ( expression  exp)
static

Definition at line 556 of file cprettyprinter.c.

557 {
558  string result = "{";
560 
561  bool first = true;
562  FOREACH (EXPRESSION,e,args)
563  {
564  if (brace_expression_p(e))
565  result = strdup(concatenate(result,first?"":",",
566  c_brace_expression_string(e),NULL));
567  else
568  result = strdup(concatenate(result,first?"":",",
569  expression_to_string(e),NULL));
570  first = false;
571  }
572  result = strdup(concatenate(result,"}",NULL));
573  return result;
574 }
static string c_brace_expression_string(expression exp)
string expression_to_string(expression e)
Definition: expression.c:77
bool brace_expression_p(expression e)
Return bool indicating if expression e is a brace expression.
Definition: expression.c:3384
#define EXPRESSION(x)
EXPRESSION.
Definition: ri.h:1217
#define syntax_call(x)
Definition: ri.h:2736
#define call_arguments(x)
Definition: ri.h:711
#define expression_syntax(x)
Definition: ri.h:1247
#define exp
Avoid some warnings from "gcc -Wshadow".
Definition: vasnprintf.c:207

References brace_expression_p(), call_arguments, concatenate(), exp, EXPRESSION, expression_syntax, expression_to_string(), FOREACH, strdup(), and syntax_call.

Referenced by this_entity_cdeclaration().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ c_call()

static string c_call ( call  c,
bool  breakable 
)
static

special case...

Definition at line 1401 of file cprettyprinter.c.

1402 {
1403  entity called = call_function(c);
1404  struct s_ppt * ppt = get_ppt(called);
1405  char* local_name = strdup(entity_local_name(called));
1406  string result = NULL;
1407 
1408  /* special case... */
1409  if (same_string_p(local_name, "RETURN")) {
1410  string copy_out = scalar_postlude ();
1412  result = RET " 0";
1413  else if (current_module_is_a_function())
1414  result = RET SPACE RESULT_NAME;
1415  else
1416  result = RET;
1417  result = strdup(concatenate (copy_out, result, NULL));
1418  free (copy_out);
1419  }
1420  else if (same_string_p(local_name, "CONTINUE") )
1421  {
1422  result = breakable?strdup(CONT):strdup("");
1423  }
1424  else if (call_constant_p(c))
1425  {
1427  result = strlower(strdup(local_name),local_name);
1428  }
1429  else
1430  {
1431  result = ppt->ppt(ppt->c? ppt->c: local_name, call_arguments(c));
1432  string tmp = result;
1433  result=strlower(strdup(result),result);
1434  free(tmp);
1435  }
1436  //free(local_name);
1437 
1438  return result;
1439 }
static struct s_ppt * get_ppt(entity f)
return the prettyprinter structure for c.
static void const_wrapper(char **s)
#define CONT
static string scalar_postlude()
#define current_module_is_a_function()
#define RESULT_NAME
#define RET
const char * local_name(const char *s)
Does not take care of block scopes and returns a pointer.
Definition: entity_names.c:221
#define call_constant_p(C)
Definition: flint_check.c:51
entity get_current_module_entity(void)
Get the entity of the current module.
Definition: static.c:85
string strlower(string, const char *)
Definition: string.c:228
#define same_string_p(s1, s2)
bool entity_main_module_p(entity e)
Definition: entity.c:700
#define call_function(x)
Definition: ri.h:709
prettyprinter ppt

References s_ppt::c, call_arguments, call_constant_p, call_function, concatenate(), const_wrapper(), CONT, current_module_is_a_function, entity_local_name(), entity_main_module_p(), free(), get_current_module_entity(), get_ppt(), local_name(), s_ppt::ppt, RESULT_NAME, RET, same_string_p, scalar_postlude(), SPACE, strdup(), and strlower().

Referenced by c_expression(), and c_statement().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ c_code_string()

static string c_code_string ( entity  module,
statement  stat 
)
static

What about declarations that are external a module scope ? Consider a source file as a module entity, put all declarations in it (external static + TOP-LEVEL)

before_head only generates the constant declarations, such as #define

Definition at line 2108 of file cprettyprinter.c.

2109 {
2110  string head, decls, body, result, copy_in, include, macro;
2111 
2112  /* What about declarations that are external a module scope ?
2113  Consider a source file as a module entity, put all declarations in it
2114  (external static + TOP-LEVEL) */
2115 
2116  /* before_head only generates the constant declarations, such as #define*/
2117  ifdebug(2)
2118  {
2119  printf("Module statement: \n");
2120  print_statement(stat);
2121  printf("and declarations: \n");
2123  }
2124 
2125  // get the needed includes
2126  include = c_include ();
2127  // get the needed macro
2128  macro = c_macro ();
2129  // function declaration
2130  head = c_head(module);
2131  // What about declarations associated to statements
2133  false);
2134  body = c_statement(stat, false);
2135  copy_in = scalar_prelude ();
2136 
2137  // concatenate everything to get the code
2138  result = concatenate(include, macro, head, OPENBRACE, NL, decls,
2139  copy_in, NL, body, CLOSEBRACE, NL, NULL);
2140 
2141  free (include);
2142  free(head);
2143  free(decls);
2144  free(body);
2145  free(copy_in);
2146  return strdup(result);
2147 }
static string scalar_prelude()
#define OPENBRACE
static string c_declarations(entity module, bool(*consider_this_entity)(entity), string separator, bool lastsep, bool fct_sig)
static string c_statement(statement s, bool breakable)
static string c_include(void)
static string c_head(entity module)
#define NL
static string c_macro(void)
static bool parameter_or_variable_p(entity e)
#define CLOSEBRACE
#define SEMICOLON
static text include(const char *file)
if the common is declared similarly in all routines, generate "include 'COMMON.h'",...
Definition: declarations2.c:92
static char * module
Definition: pips.c:74
void print_statement(statement)
Print a statement on stderr.
Definition: statement.c:98
void print_entities(list l)
Definition: entity.c:167
#define statement_declarations(x)
Definition: ri.h:2460
int printf()
#define ifdebug(n)
Definition: sg.c:47

References c_declarations(), c_head(), c_include(), c_macro(), c_statement(), CLOSEBRACE, concatenate(), free(), ifdebug, include(), module, NL, OPENBRACE, parameter_or_variable_p(), print_entities(), print_statement(), printf(), scalar_prelude(), SEMICOLON, statement_declarations, and strdup().

Referenced by print_crough().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ c_declarations()

static string c_declarations ( entity  module,
bool(*)(entity consider_this_entity,
string  separator,
bool  lastsep,
bool  fct_sig 
)
static
Returns
the string representation of the given declarations.
Parameters
module,themodule to get the declaration.
consider_this_entity,thefunction test pointer.
separator,theseparatot to be used between vars.
lastsep,setto true if a final separator is needed.
fct_sig,setto true if in a function signature.

Definition at line 805 of file cprettyprinter.c.

812 {
813  string result = strdup("");
814  code c;
815  bool first = true;
816 
817  pips_assert("it is a code", value_code_p(entity_initial(module)));
818 
821  {
822  string tmp = NULL;
823  tmp = c_entity_local_name(var);
824  pips_debug(2, "Prettyprinter declaration for variable :%s\n",tmp);
825  free(tmp);
826  if (consider_this_entity(var))
827  {
828  string old = result;
829  string svar = this_entity_cdeclaration(var, fct_sig);
830  pips_debug(6, "svar = %s\n", svar);
831  result = strdup(concatenate(old, !first ? separator: "",
832  svar, NULL));
833  pips_debug(6, "result = %s\n", result);
834  free(svar);
835  free(old);
836  first = false;
837  }
838  }
839  // insert the last separtor if required and if at least one declaration
840  // has been inserted.
841  if (lastsep && !first)
842  result = strdup(concatenate(result, separator, NULL));
843  return result;
844 }
static string this_entity_cdeclaration(entity var, bool fct_sig)
#define pips_assert(what, predicate)
common macros, two flavors depending on NDEBUG
Definition: misc-local.h:172
#define value_code_p(x)
Definition: ri.h:3065
#define ENTITY(x)
ENTITY.
Definition: ri.h:2755
#define code_declarations(x)
Definition: ri.h:784
#define value_code(x)
Definition: ri.h:3067
#define entity_initial(x)
Definition: ri.h:2796

References c_entity_local_name(), code_declarations, concatenate(), ENTITY, entity_initial, FOREACH, free(), module, pips_assert, pips_debug, strdup(), this_entity_cdeclaration(), value_code, and value_code_p.

Referenced by c_code_string(), and c_head().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ c_dim_string()

static string c_dim_string ( list  ldim,
bool  fct_sig 
)
static
Returns
a newly allocated string of the dimensions in C
Parameters
ldimthe variable dimension
fct_sig,setto true if the variable is part of a function signature

In fact, the lower bound of array in C is always equal to 0, we only need to print (upper dimension + 1) but in order to handle Fortran code, we check all other possibilities and print (upper - lower + 1). Problem : the order of dimensions is reversed !!!!

to be refined here to make more beautiful expression

otherwise the list is empty, no dimension to declare

Definition at line 452 of file cprettyprinter.c.

453 {
454  string result = "";
455  if (ldim != NIL )
456  {
457  FOREACH(DIMENSION, dim,ldim)
458  {
459  expression elow = dimension_lower(dim);
460  expression eup = dimension_upper(dim);
461  intptr_t low;
462  intptr_t up;
463  string slow;
464  string sup;
465 
466  /* In fact, the lower bound of array in C is always equal to 0,
467  we only need to print (upper dimension + 1)
468  but in order to handle Fortran code, we check all other possibilities
469  and print (upper - lower + 1). Problem : the order of dimensions is reversed !!!! */
470 #if 1
471  if (expression_integer_value(elow, &low))
472  {
473  if (low == 0)
474  {
475  if (expression_integer_value(eup, &up))
476  result = strdup(concatenate(OPENBRACKET,int2a(up+1),CLOSEBRACKET,result,NULL));
477  else
478  /* to be refined here to make more beautiful expression */
479  result = strdup(concatenate(OPENBRACKET,
481  eup,int_to_expression(1))
482  ),
483  CLOSEBRACKET,result,NULL));
484  }
485  else
486  {
487  if (expression_integer_value(eup, &up)) {
488  result = strdup(concatenate(OPENBRACKET,int2a(up-low+1),CLOSEBRACKET,result,NULL));
489  } else {
490  sup = expression_to_string(eup);
491  if (fct_sig == true) {
492  string tmp = NULL;
493  if (get_bool_property ("CROUGH_FORTRAN_USES_INTERFACE") == false) {
494  tmp = strdup (concatenate ("(*",sup, SCALAR_IN_SIG_EXT, ")", NULL));
495  free (sup);
496  sup = tmp;
497  }
498  }
499  result = strdup(concatenate(OPENBRACKET,sup,"-",int2a(low-1),CLOSEBRACKET,result,NULL));
500  free(sup);
501  }
502  }
503  }
504  else
505 #endif
506  {
507  slow = c_expression(elow,false);
508  sup = c_expression(eup,false);
509  result = strdup(concatenate(OPENBRACKET,sup,"-",slow,"+ 1",CLOSEBRACKET,result,NULL));
510  free(slow);
511  free(sup);
512  }
513  }
514  }
515  /* otherwise the list is empty, no dimension to declare */
516  return strlower (strdup (result), result);
517 }
#define CLOSEBRACKET
static string c_expression(expression, bool)
forward declaration.
#define OPENBRACKET
#define SCALAR_IN_SIG_EXT
#define NIL
The empty list (nil in Lisp)
Definition: newgen_list.h:47
entity CreateIntrinsic(string name)
this function does not create an intrinsic function because they must all be created beforehand by th...
Definition: entity.c:1311
bool expression_integer_value(expression e, intptr_t *pval)
Definition: eval.c:792
expression MakeBinaryCall(entity f, expression eg, expression ed)
Creates a call expression to a function with 2 arguments.
Definition: expression.c:354
expression int_to_expression(_int i)
transform an int into an expression and generate the corresponding entity if necessary; it is not cle...
Definition: expression.c:1188
#define dimension_lower(x)
Definition: ri.h:980
#define dimension_upper(x)
Definition: ri.h:982
#define intptr_t
Definition: stdint.in.h:294
char * int2a(int)
util.c
Definition: util.c:42

References c_expression(), CLOSEBRACKET, concatenate(), CreateIntrinsic(), DIMENSION, dimension_lower, dimension_upper, expression_integer_value(), expression_to_string(), FOREACH, free(), get_bool_property(), int2a(), int_to_expression(), intptr_t, MakeBinaryCall(), NIL, OPENBRACKET, SCALAR_IN_SIG_EXT, strdup(), and strlower().

Referenced by this_entity_cdeclaration().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ c_entity_local_name()

static char* c_entity_local_name ( entity  var)
static

Delete all the prefixes

switch to lower cases...

Definition at line 176 of file cprettyprinter.c.

177 {
178  const char* name;
179 
181  var != get_current_module_entity() &&
183  )
184  name = RESULT_NAME;
185  else
186  {
187  name = entity_local_name(var);
188 
189  /* Delete all the prefixes */
190 
191  if (strstr(name,STRUCT_PREFIX) != NULL)
192  name = strstr(name,STRUCT_PREFIX) + 1;
193  if (strstr(name,UNION_PREFIX) != NULL)
194  name = strstr(name,UNION_PREFIX) + 1;
195  if (strstr(name,ENUM_PREFIX) != NULL)
196  name = strstr(name,ENUM_PREFIX) + 1;
197  if (strstr(name,TYPEDEF_PREFIX) != NULL)
198  name = strstr(name,TYPEDEF_PREFIX) + 1;
199  if (strstr(name,MEMBER_SEP_STRING) != NULL)
200  name = strstr(name,MEMBER_SEP_STRING) + 1;
201 
202  /* switch to lower cases... */
203 
204  }
205  char *rname=strlower(strdup(name),name);
206  pips_debug (5, "local name %s found\n", rname);
207  return rname;
208 }
#define TYPEDEF_PREFIX
Definition: naming-local.h:62
#define UNION_PREFIX
Definition: naming-local.h:58
#define ENUM_PREFIX
Definition: naming-local.h:60
#define MEMBER_SEP_STRING
Definition: naming-local.h:53
#define STRUCT_PREFIX
Definition: naming-local.h:56

References current_module_is_a_function, entity_local_name(), ENUM_PREFIX, get_current_module_entity(), MEMBER_SEP_STRING, pips_debug, RESULT_NAME, same_string_p, strdup(), strlower(), STRUCT_PREFIX, TYPEDEF_PREFIX, and UNION_PREFIX.

Referenced by c_basic_string(), c_declarations(), c_head(), c_loop(), c_reference(), c_statement(), interface_argument_declaration(), interface_code_string(), interface_signature(), and this_entity_cdeclaration().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ c_expression()

static string c_expression ( expression  e,
bool  breakable 
)
static

forward declaration.

add cast, sizeof here

Definition at line 1486 of file cprettyprinter.c.

1487 {
1488  string result = NULL;
1489  syntax s = expression_syntax(e);
1490  switch (syntax_tag(s))
1491  {
1492  case is_syntax_call:
1493  result = c_call(syntax_call(s),breakable);
1494  break;
1495  case is_syntax_range:
1496  result = strdup("range not implemented");
1497  break;
1498  case is_syntax_reference:
1499  result = c_reference(syntax_reference(s));
1500  break;
1501  /* add cast, sizeof here */
1502  default:
1503  pips_internal_error("unexpected syntax tag");
1504  }
1505  return result;
1506 }
static string c_call(call c, bool breakable)
static string c_reference(reference r)
Attention with Fortran: the indexes are reversed.
#define syntax_reference(x)
Definition: ri.h:2730
#define syntax_tag(x)
Definition: ri.h:2727
@ is_syntax_range
Definition: ri.h:2692
@ is_syntax_call
Definition: ri.h:2693
@ is_syntax_reference
Definition: ri.h:2691

References c_call(), c_reference(), expression_syntax, is_syntax_call, is_syntax_range, is_syntax_reference, pips_internal_error, strdup(), syntax_call, syntax_reference, and syntax_tag.

Referenced by c_dim_string(), c_forloop(), c_loop(), c_reference(), c_test(), c_unstructured(), c_whileloop(), ppt_binary(), ppt_call(), ppt_min_max(), ppt_unary(), and ppt_unary_post().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ c_forloop()

static string c_forloop ( forloop  f)
static

partial implementation...

Definition at line 1762 of file cprettyprinter.c.

1763 {
1764  /* partial implementation... */
1765  string result;
1766  string body = c_statement(forloop_body(f),true);
1767  string init = c_expression(forloop_initialization(f),true);
1768  string cond = c_expression(forloop_condition(f),true);
1769  string inc = c_expression(forloop_increment(f),true);
1770  result = strdup(concatenate("for (", init, ";",cond,";",inc,") {" NL,
1771  body, "}" NL, NULL));
1772 
1773  free(inc);
1774  free(cond);
1775  free(init);
1776  free(body);
1777  return result;
1778 }
int f(int off1, int off2, int n, float r[n], float a[n], float b[n])
Definition: offsets.c:15
static int init
Maximal value set for Fortran 77.
Definition: entity.c:320
#define forloop_initialization(x)
Definition: ri.h:1366
#define forloop_increment(x)
Definition: ri.h:1370
#define forloop_condition(x)
Definition: ri.h:1368
#define forloop_body(x)
Definition: ri.h:1372

References c_expression(), c_statement(), concatenate(), f(), forloop_body, forloop_condition, forloop_increment, forloop_initialization, free(), init, NL, and strdup().

Referenced by c_statement().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ c_head()

static string c_head ( entity  module)
static

another kind : "int main(void)" ?

define type head.

define args.

Definition at line 905 of file cprettyprinter.c.

906 {
907  string result;
908 
909  pips_assert("it is a function", type_functional_p(entity_type(module)));
910 
912  /* another kind : "int main(void)" ?*/
913  result = strdup(MAIN_DECLARATION);
914  }
915  else {
916  string head, args, svar;
918 
919  /* define type head. */
920  if (get_bool_property ("DO_RETURN_TYPE_AS_TYPEDEF") == true) {
921  head = strdup (get_string_property ("SET_RETURN_TYPE_AS_TYPEDEF_NEW_TYPE"));
922  }
923  else if (entity_subroutine_p(module)) {
924  head = strdup("void");
925  }
926  else {
927  variable v;
928  pips_assert("type of result is a variable",
931  head = c_basic_string(variable_basic(v));
932  }
933 
934  /* define args. */
936  {
937  args = c_declarations(module, argument_p, ", ", false, true);
938  }
939  else
940  {
941  args = strdup("void");
942  }
943 
944  svar = c_entity_local_name(module);
945  if (get_bool_property("PRETTYPRINT_C_FUNCTION_NAME_WITH_UNDERSCORE"))
946 
947  result = strdup(concatenate(head, SPACE, svar, "_",
948  OPENPAREN, args, CLOSEPAREN, NL, NULL));
949 
950  else
951  result = strdup(concatenate(head, SPACE, svar,
952  OPENPAREN, args, CLOSEPAREN, NL, NULL));
953 
954  free(svar);
955  free(head);
956  free(args);
957  }
958 
959  return result;
960 }
static bool argument_p(entity e)
#define CLOSEPAREN
#define OPENPAREN
static string c_basic_string(basic b)
#define MAIN_DECLARATION
returns the head of the function/subroutine/program.
bool entity_subroutine_p(entity e)
Definition: entity.c:737
#define type_functional_p(x)
Definition: ri.h:2950
#define functional_result(x)
Definition: ri.h:1444
#define type_functional(x)
Definition: ri.h:2952
#define type_variable(x)
Definition: ri.h:2949
#define functional_parameters(x)
Definition: ri.h:1442
#define variable_basic(x)
Definition: ri.h:3120

References argument_p(), c_basic_string(), c_declarations(), c_entity_local_name(), CLOSEPAREN, concatenate(), entity_main_module_p(), entity_subroutine_p(), entity_type, f(), free(), functional_parameters, functional_result, get_bool_property(), get_string_property(), MAIN_DECLARATION, module, NL, OPENPAREN, pips_assert, SPACE, strdup(), type_functional, type_functional_p, type_variable, type_variable_p, and variable_basic.

Referenced by c_code_string().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ c_include()

static string c_include ( void  )
static

Definition at line 846 of file cprettyprinter.c.

846  {
847  string result = NULL;
848 
849  // add some c include files in order to support fortran intrinsic
850  result = strdup (concatenate ("//needed include to compile the C output\n"
851  "#include \"math.h\"\n", // fabs
852  "#include \"stdlib.h\"\n", // abs
853  "#include \"complex.h\"\n", // abs
854  "\n",
855  NULL));
856 
857  // take care of include file required by the user
858  const char* user_req = get_string_property ("CROUGH_INCLUDE_FILE_LIST");
859  pips_debug (5, "including the user file list %s\n", user_req);
860  string match = NULL;
861  string tmp = strdup(user_req);
862  match = strtok (tmp, " ,");
863  while (match != NULL) {
864  string old = result;
865  pips_debug (7, "including the file %s\n", match);
866  result = strdup (concatenate (result, "#include \"", match, "\"\n", NULL));
867  match = strtok (NULL, " ,");
868  free (old);
869  }
870  free (match);free(tmp);
871 
872  // user might use its own type that are define in a specific file
873  bool user_type = get_bool_property ("CROUGH_USER_DEFINED_TYPE");
874  pips_debug (5, "includind the user define type file %s\n", user_req);
875  if (user_type == true) {
876  string old = result;
877  const char* f_name = get_string_property ("CROUGH_INCLUDE_FILE");
878  pips_debug (7, "including the file %s\n", f_name);
879  result = strdup (concatenate (result, "#include \"", f_name, "\"\n",
880  NULL));
881  free (old);
882  }
883  pips_debug (5, "include string : %s\n", result);
884  return result;
885 }

References concatenate(), free(), get_bool_property(), get_string_property(), pips_debug, and strdup().

Referenced by c_code_string().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ c_loop()

static string c_loop ( loop  l)
static

partial implementation... However, there is not this kind of loop in C

what about step

Definition at line 1711 of file cprettyprinter.c.

1712 {
1713  /* partial implementation...
1714  However, there is not this kind of loop in C */
1715  string result;
1716  string body = c_statement(loop_body(l),true);
1717  string index = c_entity_local_name(loop_index(l));
1718  range r = loop_range(l);
1719  string low = c_expression(range_lower(r),true);
1720  string up = c_expression(range_upper(r),true);
1721  string theincr = c_expression(range_increment(r),true);
1722  string incr = 0;
1723  if( strcmp(theincr,"1")==0 )
1724  incr = strdup("++");
1725  else
1726  incr = strdup(concatenate( "+=", theincr , NULL ));
1727  free(theincr);
1728  /* what about step*/
1729  result = strdup(concatenate("for (", index, "=", low, "; ",
1730  index, "<=", up, "; ",
1731  index, incr, ")", SPACE, OPENBRACE, NL,
1732  body, CLOSEBRACE, NL, NULL));
1733  free(body);
1734  free(index);
1735  free(incr);
1736  // TODO: There are some allocation bugs in c_expression()
1737  //free(low);
1738  //free(up);
1739  return result;
1740 }
#define loop_body(x)
Definition: ri.h:1644
#define range_upper(x)
Definition: ri.h:2290
#define range_increment(x)
Definition: ri.h:2292
#define range_lower(x)
Definition: ri.h:2288
#define loop_range(x)
Definition: ri.h:1642
#define loop_index(x)
Definition: ri.h:1640

References c_entity_local_name(), c_expression(), c_statement(), CLOSEBRACE, concatenate(), free(), loop_body, loop_index, loop_range, NL, OPENBRACE, range_increment, range_lower, range_upper, SPACE, and strdup().

Referenced by c_statement(), and loop_annotate().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ c_macro()

static string c_macro ( void  )
static

Definition at line 888 of file cprettyprinter.c.

888  {
889  string result = NULL;
890  // add some macro to support fortran intrinsics
891  result = strdup (concatenate ("// The macros to support some fortran intrinsics\n",
892  "// and complex declaration\n"
893  MAX_DEF, MIN_DEF, POW_DEF, CMPLX_DEF, "\n",
894  NULL));
895  return result;
896 }
#define MIN_DEF
#define POW_DEF
#define CMPLX_DEF
#define MAX_DEF

References CMPLX_DEF, concatenate(), MAX_DEF, MIN_DEF, POW_DEF, and strdup().

Referenced by c_code_string().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ c_qualifier_string()

static string c_qualifier_string ( list  l)
static

Definition at line 519 of file cprettyprinter.c.

520 {
521  string result="";
522  FOREACH(QUALIFIER,q,l)
523  {
524  switch (qualifier_tag(q)) {
526  result = concatenate(result,"register ",NULL);
527  break;
528  case is_qualifier_thread:
529  result = concatenate(result,"__thread ",NULL);
530  break;
531  case is_qualifier_const:
532  result = concatenate(result,"const ",NULL);
533  break;
535  result = concatenate(result,"restrict ",NULL);
536  break;
538  result = concatenate(result,"volatile ",NULL);
539  break;
540  case is_qualifier_auto:
541  result = concatenate(result,"auto ",NULL);
542  break;
543  case is_qualifier_asm:
544  result = concatenate(result,"__asm(",qualifier_asm(q),") ", NULL);
545  break;
547  result = concatenate(result,"static ",NULL);
548  break;
549  default:
550  pips_internal_error("Unknown qualifier tag %d.\n", qualifier_tag(q));
551  }
552  }
553  return strdup(result);
554 }
#define qualifier_tag(x)
Definition: ri.h:2175
#define QUALIFIER(x)
QUALIFIER.
Definition: ri.h:2106
#define qualifier_asm(x)
Definition: ri.h:2196
@ is_qualifier_volatile
Definition: ri.h:2129
@ is_qualifier_register
Definition: ri.h:2130
@ is_qualifier_restrict
Definition: ri.h:2128
@ is_qualifier_thread
Definition: ri.h:2132
@ is_qualifier_const
Definition: ri.h:2127
@ is_qualifier_static_dimension
Definition: ri.h:2134
@ is_qualifier_auto
Definition: ri.h:2131
@ is_qualifier_asm
Definition: ri.h:2133

References concatenate(), FOREACH, is_qualifier_asm, is_qualifier_auto, is_qualifier_const, is_qualifier_register, is_qualifier_restrict, is_qualifier_static_dimension, is_qualifier_thread, is_qualifier_volatile, pips_internal_error, QUALIFIER, qualifier_asm, qualifier_tag, and strdup().

Referenced by this_entity_cdeclaration().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ c_reference()

static string c_reference ( reference  r)
static

Attention with Fortran: the indexes are reversed.

And array dimensions in C always rank from 0. BC.

Definition at line 1444 of file cprettyprinter.c.

1445 {
1446  string result = strdup(EMPTY), old, svar;
1447 
1449 
1451  expression e_tmp;
1452  expression e_lower = dimension_lower(DIMENSION(CAR(l_dim)));
1453  string s;
1454  intptr_t itmp;
1455 
1456  if( !expression_equal_integer_p(e_lower, 0))
1457  e_tmp =
1459  copy_expression(e),
1460  copy_expression(e_lower));
1461  else
1462  e_tmp = copy_expression(e);
1463 
1464  if(expression_integer_value(e_tmp, &itmp))
1465  s = int2a(itmp);
1466  else
1467  s = c_expression( e_tmp,false);
1468 
1469  old = result;
1470  result = strdup(concatenate(OPENBRACKET, s, CLOSEBRACKET,old, NULL));
1471  //free(old);
1472  //free(s);
1473  free_expression(e_tmp);
1474  POP(l_dim);
1475  }
1476 
1477 
1478  old = result;
1480  result = strdup(concatenate(svar, old, NULL));
1481  free(old);
1482  free(svar);
1483  return result;
1484 }
expression copy_expression(expression p)
EXPRESSION.
Definition: ri.c:850
void free_expression(expression p)
Definition: ri.c:853
#define EMPTY
#define POP(l)
Modify a list pointer to point on the next element of the list.
Definition: newgen_list.h:59
#define CAR(pcons)
Get the value of the first element of a list.
Definition: newgen_list.h:92
#define MINUS_OPERATOR_NAME
entity entity_intrinsic(const char *name)
FI: I do not understand this function name (see next one!).
Definition: entity.c:1292
bool expression_equal_integer_p(expression exp, int i)
================================================================
Definition: expression.c:1977
type ultimate_type(type)
Definition: type.c:3466
#define reference_variable(x)
Definition: ri.h:2326
#define reference_indices(x)
Definition: ri.h:2328
#define variable_dimensions(x)
Definition: ri.h:3122

References c_entity_local_name(), c_expression(), CAR, CLOSEBRACKET, concatenate(), copy_expression(), DIMENSION, dimension_lower, EMPTY, entity_intrinsic(), entity_type, EXPRESSION, expression_equal_integer_p(), expression_integer_value(), FOREACH, free(), free_expression(), int2a(), intptr_t, MakeBinaryCall(), MINUS_OPERATOR_NAME, OPENBRACKET, POP, reference_indices, reference_variable, strdup(), type_variable, ultimate_type(), and variable_dimensions.

Referenced by c_expression().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ c_sequence()

static string c_sequence ( sequence  seq,
bool  breakable 
)
static

Definition at line 1697 of file cprettyprinter.c.

1698 {
1699  string result = strdup(EMPTY);
1701  {
1702  string oldresult = result;
1703  string current = c_statement(s,breakable);
1704  result = strdup(concatenate(oldresult, current, NULL));
1705  free(current);
1706  free(oldresult);
1707  }
1708  return result;
1709 }
#define sequence_statements(x)
Definition: ri.h:2360
#define STATEMENT(x)
STATEMENT.
Definition: ri.h:2413
static size_t current
Definition: string.c:115

References c_statement(), concatenate(), current, EMPTY, FOREACH, free(), sequence_statements, STATEMENT, and strdup().

Referenced by c_statement().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ c_statement()

static string c_statement ( statement  s,
bool  breakable 
)
static

rintf("\nCurrent statement : \n"); print_statement(s);

add switch, forloop break, continue, return instructions here

Definition at line 1781 of file cprettyprinter.c.

1782 {
1783  string result;
1786  /*printf("\nCurrent statement : \n");
1787  print_statement(s);*/
1788  switch (instruction_tag(i))
1789  {
1790  case is_instruction_test:
1791  {
1792  test t = instruction_test(i);
1793  result = c_test(t,breakable);
1794  break;
1795  }
1797  {
1798  sequence seq = instruction_sequence(i);
1799  result = c_sequence(seq,breakable);
1800  break;
1801  }
1802  case is_instruction_loop:
1803  {
1804  loop l = instruction_loop(i);
1805  result = c_loop(l);
1806  break;
1807  }
1809  {
1811  result = c_whileloop(w);
1812  break;
1813  }
1815  {
1817  result = c_forloop(f);
1818  break;
1819  }
1820  case is_instruction_call:
1821  {
1822  string scall = c_call(instruction_call(i),breakable);
1823  result = strdup(concatenate(scall, SEMICOLON, NULL));
1824  break;
1825  }
1827  {
1829  result = c_unstructured(u,breakable);
1830  break;
1831  }
1832  case is_instruction_goto:
1833  {
1834  statement g = instruction_goto(i);
1835  entity el = statement_label(g);
1836  const char* l = entity_local_name(el) + sizeof(LABEL_PREFIX) -1;
1837  result = strdup(concatenate("goto ",l, SEMICOLON, NULL));
1838  break;
1839  }
1840  /* add switch, forloop break, continue, return instructions here*/
1841  default:
1842  result = strdup(concatenate(COMMENT, " Instruction not implemented" NL, NULL));
1843  break;
1844  }
1845 
1846  if (!ENDP(l))
1847  {
1848  string decl = "";
1849  MAP(ENTITY, var,
1850  {
1851  string svar;
1852  string tmp = c_entity_local_name(var);
1853  debug(2, "\n In block declaration for variable :", tmp);
1854  free(tmp);
1855  svar = this_entity_cdeclaration(var, false);
1856  decl = strdup(concatenate(decl, svar, SEMICOLON, NULL));
1857  free(svar);
1858  },l);
1859  result = strdup(concatenate(decl,result,NULL));
1860  }
1861 
1862  return result;
1863 }
#define COMMENT
static string c_whileloop(whileloop w)
static string c_unstructured(unstructured u, bool breakable)
static string c_sequence(sequence seq, bool breakable)
static string c_loop(loop l)
static string c_test(test t, bool breakable)
static string c_forloop(forloop f)
#define ENDP(l)
Test if a list is empty.
Definition: newgen_list.h:66
#define MAP(_map_CASTER, _map_item, _map_code, _map_list)
Apply/map an instruction block on all the elements of a list (old fashioned)
Definition: newgen_list.h:226
void debug(const int the_expected_debug_level, const char *calling_function_name, const char *a_message_format,...)
ARARGS0.
Definition: debug.c:189
#define LABEL_PREFIX
Definition: naming-local.h:31
#define instruction_loop(x)
Definition: ri.h:1520
#define instruction_goto(x)
Definition: ri.h:1526
#define statement_label(x)
Definition: ri.h:2450
@ is_instruction_goto
Definition: ri.h:1473
@ is_instruction_unstructured
Definition: ri.h:1475
@ is_instruction_whileloop
Definition: ri.h:1472
@ is_instruction_test
Definition: ri.h:1470
@ is_instruction_call
Definition: ri.h:1474
@ is_instruction_sequence
Definition: ri.h:1469
@ is_instruction_forloop
Definition: ri.h:1477
@ is_instruction_loop
Definition: ri.h:1471
#define instruction_tag(x)
Definition: ri.h:1511
#define instruction_sequence(x)
Definition: ri.h:1514
#define instruction_forloop(x)
Definition: ri.h:1538
#define instruction_whileloop(x)
Definition: ri.h:1523
#define statement_instruction(x)
Definition: ri.h:2458
#define instruction_call(x)
Definition: ri.h:1529
#define instruction_test(x)
Definition: ri.h:1517
#define instruction_unstructured(x)
Definition: ri.h:1532

References c_call(), c_entity_local_name(), c_forloop(), c_loop(), c_sequence(), c_test(), c_unstructured(), c_whileloop(), COMMENT, concatenate(), debug(), ENDP, ENTITY, entity_local_name(), f(), free(), instruction_call, instruction_forloop, instruction_goto, instruction_loop, instruction_sequence, instruction_tag, instruction_test, instruction_unstructured, instruction_whileloop, is_instruction_call, is_instruction_forloop, is_instruction_goto, is_instruction_loop, is_instruction_sequence, is_instruction_test, is_instruction_unstructured, is_instruction_whileloop, LABEL_PREFIX, MAP, NL, SEMICOLON, statement_declarations, statement_instruction, statement_label, strdup(), and this_entity_cdeclaration().

Referenced by c_code_string(), c_forloop(), c_loop(), c_sequence(), c_test(), c_unstructured(), and c_whileloop().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ c_test()

static string c_test ( test  t,
bool  breakable 
)
static

Definition at line 1676 of file cprettyprinter.c.

1677 {
1678  string result;
1679  bool no_false;
1680  string cond, strue, sfalse;
1681  cond = c_expression(test_condition(t),breakable);
1682  strue = c_statement(test_true(t),breakable);
1683  no_false = empty_statement_p(test_false(t));
1684 
1685  sfalse = no_false? NULL: c_statement(test_false(t),false);
1686 
1687  result = strdup(concatenate("if (", cond, ") {" NL,
1688  strue,
1689  no_false? "}" NL: "} else {" NL,
1690  sfalse, "}" NL, NULL));
1691  free(cond);
1692  free(strue);
1693  if (sfalse) free(sfalse);
1694  return result;
1695 }
bool empty_statement_p(statement)
Test if a statement is empty.
Definition: statement.c:391
#define test_false(x)
Definition: ri.h:2837
#define test_true(x)
Definition: ri.h:2835
#define test_condition(x)
Definition: ri.h:2833

References c_expression(), c_statement(), concatenate(), empty_statement_p(), free(), NL, strdup(), test_condition, test_false, and test_true.

Referenced by c_statement(), controlize_forloop(), controlize_loop(), controlize_repeatloop(), controlize_whileloop(), and link_3_control_nodes().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ c_type_string()

static string c_type_string ( type  t)
static

Definition at line 307 of file cprettyprinter.c.

308 {
309  string result = "UNKNOWN_TYPE" SPACE;
310  switch (type_tag(t))
311  {
312  case is_type_variable:
313  {
315  result = c_basic_string(b);
316  break;
317  }
318  case is_type_void:
319  {
320  result = "void" SPACE;
321  break;
322  }
323  case is_type_struct:
324  {
325  result = "struct" SPACE;
326  break;
327  }
328  case is_type_union:
329  {
330  result = "union" SPACE;
331  break;
332  }
333  case is_type_enum:
334  {
335  result = "enum" SPACE;
336  break;
337  }
338  default:
339  pips_user_warning("case not handled yet \n");
340  }
341  return strdup(result);
342 }
#define pips_user_warning
Definition: misc-local.h:146
#define type_tag(x)
Definition: ri.h:2940
@ is_type_void
Definition: ri.h:2904
@ is_type_enum
Definition: ri.h:2907
@ is_type_variable
Definition: ri.h:2900
@ is_type_union
Definition: ri.h:2906
@ is_type_struct
Definition: ri.h:2905

References c_basic_string(), is_type_enum, is_type_struct, is_type_union, is_type_variable, is_type_void, pips_user_warning, SPACE, strdup(), type_tag, type_variable, and variable_basic.

Referenced by c_basic_string(), and this_entity_cdeclaration().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ c_unstructured()

static string c_unstructured ( unstructured  u,
bool  breakable 
)
static

build an arbitrary reverse trail of control nodes

Copy from text_trail ...

A GOTO must be generated to reach the control successor

Is there a textual successor?

This may happen after restructuring

succ2 must be reached by GOTO

succ1 must be reached by GOTO

Both successors must be labelled

Both successors must be textual predecessors

Definition at line 1510 of file cprettyprinter.c.

1511 {
1512  string result = "";
1513  /* build an arbitrary reverse trail of control nodes */
1514  list trail = unstructured_to_trail(u);
1515  list cc = NIL;
1516  trail = gen_nreverse(trail);
1517  ifdebug(3)
1518  {
1519  printf("Print trail: \n");
1520  dump_trail(trail);
1521  }
1522  /* Copy from text_trail ...*/
1523  for(cc=trail; !ENDP(cc); POP(cc))
1524  {
1525  control c = CONTROL(CAR(cc));
1526  const char* l;
1527  int nsucc = gen_length(control_successors(c));
1528  statement st = control_statement(c);
1529  ifdebug(3)
1530  {
1531  printf("Processing statement:\n");
1532  print_statement(st);
1533  }
1534  switch(nsucc)
1535  {
1536  case 0:
1537  {
1538  printf("nsucc = 0 \n");
1539  result = strdup(concatenate(result,c_statement(st,false),NULL));
1540  break;
1541  }
1542  case 1:
1543  {
1544  control succ = CONTROL(CAR(control_successors(c)));
1545  printf("nsucc = 1 \n");
1547  !get_bool_property("PRETTYPRINT_CHECK_IO_STATEMENTS"))
1548  {
1549  succ = CONTROL(CAR(CDR(control_successors(succ))));
1551  !get_bool_property("PRETTYPRINT_CHECK_IO_STATEMENTS"))
1552  {
1553 
1554  succ = CONTROL(CAR(CDR(control_successors(succ))));
1555  }
1556  pips_assert("The successor is not a check io statement",
1558  }
1559 
1560  result = strdup(concatenate(result,c_statement(st,false),NULL));
1561  if(statement_does_return(st))
1562  {
1563  if(!ENDP(CDR(cc)))
1564  {
1565  control tsucc = CONTROL(CAR(CDR(cc)));
1566  if(tsucc==succ)
1567  {
1568  break;
1569  }
1570  }
1571  /* A GOTO must be generated to reach the control successor */
1572 
1574  pips_assert("Must be labelled", l!= string_undefined);
1575  result = strdup(concatenate(result,"goto ",l,SEMICOLON,NULL));
1576  }
1577  break;
1578  }
1579  case 2:
1580  {
1581  control succ1 = CONTROL(CAR(control_successors(c)));
1582  control succ2 = CONTROL(CAR(CDR(control_successors(c))));
1584  test t = instruction_test(i);
1585  bool no_endif = false;
1586  string str = NULL;
1587  printf("nsucc = 2 \n");
1588  pips_assert("must be a test", instruction_test_p(i));
1589 
1590  result = strdup(concatenate(result,"if (",c_expression(test_condition(t),breakable), ") {", NL, NULL));
1591  printf("Result = %s\n",result);
1592 
1593  /* Is there a textual successor? */
1594  if(!ENDP(CDR(cc)))
1595  {
1596  control tsucc = CONTROL(CAR(CDR(cc)));
1597  if(tsucc==succ1)
1598  {
1599  if(tsucc==succ2)
1600  {
1601  /* This may happen after restructuring */
1602  printf("This may happen after restructuring\n");
1603  ;
1604  }
1605  else
1606  {
1607  /* succ2 must be reached by GOTO */
1608  printf("succ2 must be reached by GOTO\n");
1610  pips_assert("Must be labelled", l!= string_undefined);
1611  str = strdup(concatenate("}",NL, "else {",NL,"goto ", l, SEMICOLON,"}",NL,NULL));
1612  printf("str = %s\n",str);
1613  }
1614  }
1615  else
1616  {
1617  if(tsucc==succ2)
1618  {
1619  /* succ1 must be reached by GOTO */
1620  printf("succ1 must be reached by GOTO\n");
1622  pips_assert("Must be labelled", l!= string_undefined);
1623  no_endif = true;
1624  }
1625  else
1626  {
1627  /* Both successors must be labelled */
1628  printf("Both successors must be labelled\n");
1630  pips_assert("Must be labelled", l!= string_undefined);
1631  str = strdup(concatenate("goto ", l, SEMICOLON, "}", NL,"else {",NL,NULL));
1633  pips_assert("Must be labelled", l!= string_undefined);
1634  str = strdup(concatenate(str,"goto ", l, SEMICOLON, NULL));
1635  printf("str = %s\n",str);
1636  }
1637  }
1638  }
1639  else
1640  {
1641  /* Both successors must be textual predecessors */
1642  printf("Both successors must be textual predecessors \n");
1644  pips_assert("Must be labelled", l!= string_undefined);
1645  str = strdup(concatenate("goto ", l, SEMICOLON, "}",NL,"else {",NL,NULL));
1647  pips_assert("Must be labelled", l!= string_undefined);
1648  str = strdup(concatenate(str,"goto ", l, SEMICOLON, "}",NL, NULL));
1649  printf("str = %s\n",str);
1650  }
1651 
1652  if(no_endif)
1653  {
1654  printf("No endif\n");
1655  result = strdup(concatenate(result," goto ", l, SEMICOLON, "}",NL,NULL));
1656  printf("Result = %s\n",result);
1657  }
1658  printf("Result before = %s\n",result);
1659  if (str != NULL)
1660  {
1661  printf("str before = %s\n",str);
1662  result = strdup(concatenate(result,str,NULL));
1663  }
1664  printf("Result after = %s\n",result);
1665  break;
1666  }
1667  default:
1668  pips_internal_error("Too many successors for a control node");
1669  }
1670  }
1671 
1672  gen_free_list(trail);
1673  return result;
1674 }
list gen_nreverse(list cp)
reverse a list in place
Definition: list.c:304
size_t gen_length(const list l)
Definition: list.c:150
void gen_free_list(list l)
free the spine of the list
Definition: list.c:327
#define CDR(pcons)
Get the list less its first element.
Definition: newgen_list.h:111
bool check_io_statement_p(statement)
Definition: statement.c:528
bool statement_does_return(statement)
Returns false is no syntactic control path exits s (i.e.
Definition: statement.c:2195
#define string_undefined
Definition: newgen_types.h:40
void dump_trail(list)
Definition: unstructured.c:263
list unstructured_to_trail(unstructured)
Definition: unstructured.c:240
const char * label_local_name(entity e)
END_EOLE.
Definition: entity.c:604
#define CONTROL(x)
CONTROL.
Definition: ri.h:910
#define control_successors(x)
Definition: ri.h:945
#define instruction_test_p(x)
Definition: ri.h:1515
#define control_statement(x)
Definition: ri.h:941

References s_ppt::c, c_expression(), c_statement(), CAR, CDR, check_io_statement_p(), concatenate(), CONTROL, control_statement, control_successors, dump_trail(), ENDP, gen_free_list(), gen_length(), gen_nreverse(), get_bool_property(), ifdebug, instruction_test, instruction_test_p, label_local_name(), NIL, NL, pips_assert, pips_internal_error, POP, print_statement(), printf(), SEMICOLON, statement_does_return(), statement_instruction, statement_label, strdup(), string_undefined, test_condition, and unstructured_to_trail().

Referenced by c_statement().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ c_whileloop()

static string c_whileloop ( whileloop  w)
static

partial implementation...

o while and while do loops

Definition at line 1743 of file cprettyprinter.c.

1744 {
1745  /* partial implementation... */
1746  string result;
1747  string body = c_statement(whileloop_body(w),true);
1748  string cond = c_expression(whileloop_condition(w),true);
1750  /*do while and while do loops */
1752  result = strdup(concatenate("while (", cond, ") {" NL,
1753  body, "}" NL, NULL));
1754  else
1755  result = strdup(concatenate("do " NL, "{" NL,
1756  body, "}" NL,"while (", cond, ");" NL, NULL));
1757  free(cond);
1758  free(body);
1759  return result;
1760 }
static Value eval(Pvecteur pv, Value val, Variable var)
#define whileloop_evaluation(x)
Definition: ri.h:3166
#define whileloop_body(x)
Definition: ri.h:3162
#define whileloop_condition(x)
Definition: ri.h:3160
#define evaluation_before_p(x)
Definition: ri.h:1159

References c_expression(), c_statement(), concatenate(), eval(), evaluation_before_p, free(), NL, strdup(), whileloop_body, whileloop_condition, and whileloop_evaluation.

Referenced by c_statement().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ const_wrapper()

static void const_wrapper ( char **  s)
static

search fortran constant

Definition at line 149 of file cprettyprinter.c.

150 {
151  static char * const_to_c[][2] = { { ".true." , "1" } , { ".false." , "0" }};
152  static const int const_to_c_sz = sizeof(const_to_c)/sizeof(*const_to_c);
153  int i;
154  pips_debug (5, "constant to convert : %s\n", *s);
155  if (convert_double_value (s) == false) {
156  /* search fortran constant */
157  char *name = strlower(strdup(*s),*s);
158  for(i=0;i<const_to_c_sz;i++)
159  {
160  if(strcmp(name,const_to_c[i][0]) == 0 )
161  {
162  free(*s);
163  *s = strdup(const_to_c[i][1]);
164  break;
165  }
166  }
167  free(name);
168  }
169  pips_debug (5, "constant converted : %s\n", *s);
170 }
static bool convert_double_value(char **str)
test if the string looks like a REAL*8 (double in C) declaration i.e something like 987987D54654 : a ...

References convert_double_value(), free(), pips_debug, strdup(), and strlower().

Referenced by c_call().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ convert_double_value()

static bool convert_double_value ( char **  str)
static

test if the string looks like a REAL*8 (double in C) declaration i.e something like 987987D54654 : a bunch of digit with a letter in the middle.

if yes convert it to C (i.e replace D by E) and return true

Definition at line 123 of file cprettyprinter.c.

123  {
124  bool result = true;
125  int match = 0;
126  int i = 0;
127  pips_debug (5, "test if str : %s is a double value. %c 0 = \n", *str, '0');
128  for (i = 0; ((*str)[i] != '\0') && (result == true); i++) {
129  bool cond = ((*str)[i] == 'D') && (match == 0);
130  if (cond == true) {
131  match = i;
132  continue;
133  }
134  result &= (((*str)[i] >= '0') && ((*str)[i] <= '9')) || ((*str)[i] == '.') || (cond);
135  }
136  pips_debug (5, "end with i = %d, match = %d result = %s\n",
137  i, match, (result)?"true":"false");
138  result &= ((*str)[i] == '\0') && (match + 1 != i) && (match != 0);
139  if (result == true) {
140  *str = strdup (*str);
141  (*str)[match] = 'E';
142  }
143  return result;
144 }
struct _newgen_struct_match_ * match
Definition: sac_private.h:74

References pips_debug, and strdup().

Referenced by const_wrapper().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ expression_needs_parenthesis_p()

static bool expression_needs_parenthesis_p ( expression  e)
static

Definition at line 1381 of file cprettyprinter.c.

1382 {
1383  syntax s = expression_syntax(e);
1384  switch (syntax_tag(s))
1385  {
1386  case is_syntax_call:
1387  {
1388  struct s_ppt * p = get_ppt(call_function(syntax_call(s)));
1389  return p->ppt==ppt_binary;
1390  }
1391  case is_syntax_reference:
1392  case is_syntax_range:
1393  default:
1394  return false;
1395  }
1396 }
static string ppt_binary(const char *in_c, list le)

References call_function, expression_syntax, get_ppt(), is_syntax_call, is_syntax_range, is_syntax_reference, s_ppt::ppt, ppt_binary(), syntax_call, and syntax_tag.

Referenced by ppt_binary().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_c_full_name()

static void get_c_full_name ( string base_in_c,
basic  b 
)
static

fill the c_base_name to get the c full name accorgind to its basic

Definition at line 1095 of file cprettyprinter.c.

1095  {
1096  pips_debug (7, "find the C function for \"%s\" according to the basic\n",
1097  *base_in_c);
1098  pips_assert ("cant deal with basic undefined", b != basic_undefined);
1099  // initialize some varaibles
1101  enum basic_utype type = basic_tag (b);
1102  intptr_t size = basic_type_size (b);
1103 
1104  // find the correct row
1105  while ((table->c_base_name != NULL) &&
1106  !(same_string_p(*base_in_c, table->c_base_name) &&
1107  (table->type == type) &&
1108  (table->size == size)))
1109  table++;
1110  if (table->c_base_name == NULL) {
1111  pips_internal_error("can not determin the c function to call");
1112  }
1113  str_append (base_in_c, table->suffix);
1114  str_prepend (base_in_c, table->prefix);
1115  return;
1116 }
static c_full_name c_base_name_to_c_full_name[]
void str_append(string *, string)
Append the suffix to the string.
Definition: string.c:356
void str_prepend(string *, string)
Prepend the prefix to the string.
Definition: string.c:342
int basic_type_size(basic)
See also SizeOfElements()
Definition: type.c:1074
basic_utype
Definition: ri.h:570
#define basic_undefined
Definition: ri.h:556
char * c_base_name
enum basic_utype type
intptr_t size

References basic_tag, basic_type_size(), basic_undefined, c_full_name::c_base_name, c_base_name_to_c_full_name, intptr_t, pips_assert, pips_debug, pips_internal_error, c_full_name::prefix, same_string_p, c_full_name::size, str_append(), str_prepend(), c_full_name::suffix, and c_full_name::type.

Referenced by ppt_math().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_ppt()

static struct s_ppt* get_ppt ( entity  f)
static

return the prettyprinter structure for c.

Definition at line 1372 of file cprettyprinter.c.

1373 {
1374  const char* called = entity_local_name(f);
1375  struct s_ppt * table = intrinsic_to_c;
1376  while (table->intrinsic && !same_string_p(called, table->intrinsic))
1377  table++;
1378  return table;
1379 }
static struct s_ppt intrinsic_to_c[]
char * intrinsic

References entity_local_name(), f(), s_ppt::intrinsic, intrinsic_to_c, and same_string_p.

Referenced by c_call(), and expression_needs_parenthesis_p().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ interface_argument_declaration()

static string interface_argument_declaration ( entity  module,
string  separator,
string  indent 
)
static
Returns
the string representation of the arguments of the given modules to be used as a variable declaration in an interface.
Parameters
module,themodule to get the declaration.
separator,theseparatot to be used between vars.
lastsep,setto true if a final separator is needed.

Definition at line 2021 of file cprettyprinter.c.

2022  {
2023  code c;
2024  string tmp = NULL;
2025  string args = strdup ("");
2026  string result = NULL;
2027 
2028  pips_assert("it is a code", value_code_p(entity_initial(module)));
2029 
2031  FOREACH(ENTITY, var,code_declarations(c)) {
2032  if (argument_p(var) == true) {
2033  tmp = args;
2034  args = strdup (concatenate (args, indent,
2036  " :: ",
2037  c_entity_local_name (var),
2038  separator,
2039  NULL));
2040  free(tmp);
2041  }
2042  }
2043  result = strdup (args);
2044  free (args);
2045  return result;
2046 }
static string interface_argument_type_string(entity var)
return a string representation of the type to be used for a variable decalaration in an interface mod...

References argument_p(), s_ppt::c, c_entity_local_name(), code_declarations, concatenate(), ENTITY, entity_initial, FOREACH, free(), interface_argument_type_string(), module, pips_assert, strdup(), value_code, and value_code_p.

Referenced by interface_code_string().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ interface_argument_type_string()

static string interface_argument_type_string ( entity  var)
static

return a string representation of the type to be used for a variable decalaration in an interface module in order to ensure that the C function can be called from fotran codes

Definition at line 2003 of file cprettyprinter.c.

2003  {
2004  pips_assert("this function is deicated to arguments", argument_p(var));
2005  string result = NULL;
2006  type t = entity_type(var);
2007  variable v = type_variable(t);
2008  if (variable_dimensions (v) != NULL) {
2009  result = strdup ("type (c_ptr), value");
2010  } else {
2011  result = interface_type_string(t, true);
2012  }
2013  return result;
2014 }
static string interface_type_string(type t, bool value)

References argument_p(), entity_type, interface_type_string(), pips_assert, strdup(), type_variable, and variable_dimensions.

Referenced by interface_argument_declaration().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ interface_basic_string()

static string interface_basic_string ( basic  b,
bool  value 
)
static

Convert the fortran basic to its interface string value.

Parameters
b,thebasic to be converted to string
value,setto true if the var has to be passed by value

case 1: result = "char";

break;

case 2: result = "short";

break;

case 6: result = "long";

break;

case 11: result = "unsigned char";

break;

case 12: result = "unsigned short";

break;

case 14: result = "unsigned int";

break;

case 16: result = "unsigned long";

break;

case 18: result = "unsigned long long";

break;

case 21: result = "signed char";

break;

case 22: result = "signed short";

break;

case 24: result = "signed int";

break;

case 26: result = "signed long";

break;

case 28: result = "signed long long";

break;

Definition at line 1871 of file cprettyprinter.c.

1872 {
1873  const char* result = "UNKNOWN_BASIC" SPACE;
1874  char * aresult=NULL;
1875  bool user_type = get_bool_property ("CROUGH_USER_DEFINED_TYPE");
1876  switch (basic_tag(b)) {
1877  case is_basic_int: {
1878  pips_debug(2,"Basic int\n");
1879  if (user_type == false) {
1880  switch (basic_int(b)) {
1881  /* case 1: result = "char"; */
1882  /* break; */
1883  /* case 2: result = "short"; */
1884  /* break; */
1885  case 4:
1886  result = "integer (c_int)";
1887  break;
1888  /* case 6: result = "long"; */
1889  /* break; */
1890  case 8:
1891  result = "integer (c_size_t)";
1892  break;
1893  /* case 11: result = "unsigned char"; */
1894  /* break; */
1895  /* case 12: result = "unsigned short"; */
1896  /* break; */
1897  /* case 14: result = "unsigned int"; */
1898  /* break; */
1899  /* case 16: result = "unsigned long"; */
1900  /* break; */
1901  /* case 18: result = "unsigned long long"; */
1902  /* break; */
1903  /* case 21: result = "signed char"; */
1904  /* break; */
1905  /* case 22: result = "signed short"; */
1906  /* break; */
1907  /* case 24: result = "signed int"; */
1908  /* break; */
1909  /* case 26: result = "signed long"; */
1910  /* break; */
1911  /* case 28: result = "signed long long"; */
1912  /* break; */
1913  default:
1914  pips_assert ("not handle case", false);
1915  break;
1916  }
1917  } else {
1918  result = get_string_property ("CROUGH_INTEGER_TYPE");
1919  }
1920  break;
1921  }
1922  case is_basic_float: {
1923  if (user_type == false) {
1924  switch (basic_float(b)){
1925  case 4: result = "real (c_float)";
1926  break;
1927  case 8: result = "real (c_double)";
1928  break;
1929  }
1930  } else {
1931  result = get_string_property ("CROUGH_REAL_TYPE");
1932  }
1933  break;
1934  }
1935  case is_basic_logical:
1936  result = "integer (c_int)";
1937  break;
1938  case is_basic_string:
1939  result = "character (c_char)";
1940  break;
1941  case is_basic_bit:
1942  pips_internal_error("unhandled case");
1943  break;
1944  case is_basic_pointer:
1945  {
1946  if (value == true) {
1947  type t = basic_pointer(b);
1948  pips_debug(2,"Basic pointer\n");
1949  aresult = interface_type_string (t, false);
1950  if (!type_void_p (t))
1951  return aresult;
1952  }
1953  else {
1954  result = "type (c_ptr)";
1955  }
1956  break;
1957  }
1958  case is_basic_derived:
1959  pips_internal_error("unhandled case");
1960  break;
1961  case is_basic_typedef:
1962  pips_internal_error("unhandled case");
1963  default:
1964  pips_internal_error("unhandled case");
1965  }
1966  if (value == true) {
1967  if(!aresult)aresult=strdup(result);
1968  char * tmp =aresult;
1969  aresult = strdup(concatenate (aresult, ", value", NULL));
1970  free(tmp);
1971  return aresult;
1972  }
1973  return strdup(result) ;
1974 }
#define type_void_p(x)
Definition: ri.h:2959

References basic_float, basic_int, basic_pointer, basic_tag, concatenate(), free(), get_bool_property(), get_string_property(), interface_type_string(), is_basic_bit, is_basic_derived, is_basic_float, is_basic_int, is_basic_logical, is_basic_pointer, is_basic_string, is_basic_typedef, pips_assert, pips_debug, pips_internal_error, SPACE, strdup(), and type_void_p.

Referenced by interface_type_string().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ interface_code_string()

static string interface_code_string ( entity  module,
_UNUSED_ statement  stat 
)
static

Definition at line 2078 of file cprettyprinter.c.

2079 {
2080  string name = NULL;
2081  string decls = NULL;
2082  string result = NULL;
2083  string signature = NULL;
2084 
2085  pips_assert("only available for subroutines, to be implemented for functions",
2087 
2088  name = c_entity_local_name (module);
2089  signature = interface_signature (module);
2090  decls = interface_argument_declaration (module, "\n", "\t\t\t");
2091 
2092  result = strdup(concatenate ("module ", name, "_interface\n",
2093  "\tinterface\n",
2094  "\t\tsubroutine ", name, signature,
2095  " bind(C, name = \"", name, "\")\n",
2096  "\t\t\tuse iso_c_binding\n", decls,
2097  "\t\tend subroutine ", name,
2098  "\n\tend interface\n",
2099  "end module ", name, "_interface\n",
2100  NULL));
2101  free (name);
2102  free (decls);
2103  free (signature);
2104 
2105  return result;
2106 }
static string interface_signature(entity module)
return the interface signature for a module, i.e.
static string interface_argument_declaration(entity module, string separator, string indent)

References c_entity_local_name(), concatenate(), entity_subroutine_p(), free(), interface_argument_declaration(), interface_signature(), module, pips_assert, and strdup().

Referenced by print_interface().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ interface_signature()

static string interface_signature ( entity  module)
static

return the interface signature for a module, i.e.

the list of the variable names that are comma serparated.

Definition at line 2050 of file cprettyprinter.c.

2051 {
2052  code c = code_undefined;
2053  bool first = true;
2054  string tmp = NULL;
2055  string args = strdup ("(");
2056  string result = NULL;
2057 
2058  pips_assert("it is a function", type_functional_p(entity_type(module)));
2059  pips_assert("it is a code", value_code_p(entity_initial(module)));
2060 
2062 
2063  FOREACH(ENTITY, var,code_declarations(c)) {
2064  if (argument_p (var) == true) {
2065  tmp = args;
2066  args = strdup (concatenate (args, first == true ? "" : ", ",
2067  c_entity_local_name (var), NULL));
2068  free(tmp);
2069  first = false;
2070  }
2071  }
2072 
2073  result = strdup (concatenate (args, ")", NULL));
2074  free (args);
2075  return result;
2076 }
#define code_undefined
Definition: ri.h:757

References argument_p(), s_ppt::c, c_entity_local_name(), code_declarations, code_undefined, concatenate(), ENTITY, entity_initial, entity_type, FOREACH, free(), module, pips_assert, strdup(), type_functional_p, value_code, and value_code_p.

Referenced by interface_code_string().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ interface_type_string()

static string interface_type_string ( type  t,
bool  value 
)
static
Parameters
t,thetype to be converted to its string representation
value,setto true if the associated argument is passed by value (i.e. not by pointer)

Definition at line 1979 of file cprettyprinter.c.

1980 {
1981  string result ;
1982  switch (type_tag(t)) {
1983  case is_type_variable: {
1985  result = interface_basic_string(b, value);
1986  break;
1987  }
1988  case is_type_void: {
1989  result = strdup ("type (c_ptr)");
1990  break;
1991  }
1992  default:
1993  pips_user_error("case not handled yet \n");
1994  // dead code to avoid compiler warnings.
1995  result = NULL;
1996  }
1997  return result;
1998 }
static string interface_basic_string(basic b, bool value)
Convert the fortran basic to its interface string value.
#define pips_user_error
Definition: misc-local.h:147

References interface_basic_string(), is_type_variable, is_type_void, pips_user_error, strdup(), type_tag, type_variable, and variable_basic.

Referenced by interface_argument_type_string(), and interface_basic_string().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parameter_or_variable_p()

static bool parameter_or_variable_p ( entity  e)
static

Definition at line 786 of file cprettyprinter.c.

787 {
788  return parameter_p(e) || variable_p(e);
789 }
static bool variable_p(entity e)
static bool parameter_p(entity e)

References parameter_p(), and variable_p().

Referenced by c_code_string().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parameter_p()

static bool parameter_p ( entity  e)
static

Constant variables

Definition at line 771 of file cprettyprinter.c.

772 {
773  /* Constant variables */
774  return storage_rom_p(entity_storage(e)) &&
777 }
#define value_symbolic_p(x)
Definition: ri.h:3068
#define storage_rom_p(x)
Definition: ri.h:2525

References entity_initial, entity_storage, entity_type, storage_rom_p, type_functional_p, and value_symbolic_p.

Referenced by parameter_or_variable_p().

+ Here is the caller graph for this function:

◆ ppt_binary()

static string ppt_binary ( const char *  in_c,
list  le 
)
static

Definition at line 990 of file cprettyprinter.c.

991 {
992  string result;
993  expression e1, e2;
994  string s1, s2;
995  bool p1, p2;
996 
997  pips_assert("2 arguments to binary call", gen_length(le)==2);
998 
999  e1 = EXPRESSION(CAR(le));
1001  s1 = c_expression(e1,false);
1002 
1003  e2 = EXPRESSION(CAR(CDR(le)));
1005  s2 = c_expression(e2,false);
1006 
1007  result = strdup(concatenate(p1? OPENPAREN: EMPTY, s1, p1? CLOSEPAREN: EMPTY,
1008  SPACE, in_c, SPACE,
1009  p2? OPENPAREN: EMPTY, s2, p2? CLOSEPAREN: EMPTY,
1010  NULL));
1011 
1012  free(s1);
1013  //free(s2);
1014 
1015  return result;
1016 }
static bool expression_needs_parenthesis_p(expression)
s1
Definition: set.c:247

References c_expression(), CAR, CDR, CLOSEPAREN, concatenate(), EMPTY, EXPRESSION, expression_needs_parenthesis_p(), free(), gen_length(), OPENPAREN, pips_assert, s1, SPACE, and strdup().

Referenced by expression_needs_parenthesis_p().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ppt_call()

static string ppt_call ( const char *  in_c,
list  le 
)
static

SG: PBM spotted HERE.

Attention: not like this for io statements

Definition at line 1039 of file cprettyprinter.c.

1040 {
1041  string scall, old;
1042  bool pointer = !get_bool_property ("CROUGH_SCALAR_BY_VALUE_IN_FCT_CALL");
1043  if (le == NIL)
1044  {
1045  scall = strdup(concatenate(in_c, "()", NULL));
1046  }
1047  else
1048  {
1049  bool first = true;
1050  scall = strdup(concatenate(in_c, OPENPAREN, NULL));
1051 
1052  /* Attention: not like this for io statements*/
1053  FOREACH (EXPRESSION, e, le)
1054  {
1055  string arg = c_expression(e,false);
1056  old = scall;
1057  scall = strdup(concatenate(old, first? "" : ", ",
1058  expression_scalar_p(e) && pointer ? "&" : "",
1059  arg, NULL));
1060  free(arg);
1061  free(old);
1062  first = false;
1063  }
1064 
1065  old = scall;
1066  scall = strdup(concatenate(old, CLOSEPAREN, NULL));
1067  free(old);
1068  }
1069  return scall;
1070 }
#define expression_scalar_p(e)

References c_expression(), CLOSEPAREN, concatenate(), EXPRESSION, expression_scalar_p, FOREACH, free(), get_bool_property(), NIL, OPENPAREN, and strdup().

Referenced by ppt_math(), and ppt_unknown().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ppt_math()

static string ppt_math ( const char *  in_c,
list  le 
)
static

Definition at line 1121 of file cprettyprinter.c.

1122 {
1123  basic res_basic = basic_undefined;
1124  pips_assert ("need at least one argument", 0 != gen_length (le));
1125  FOREACH (EXPRESSION, exp, le) {
1126  pips_debug (7, "let's analyse the expression to find the involved types\n");
1127  type tmp = expression_to_type (exp);
1128  pips_assert ("type must be a variable", type_variable_p (tmp) == true);
1129  basic cur_b = variable_basic (type_variable (tmp));
1130  pips_assert ("expression_to_type returns a basic undefined",
1131  cur_b != basic_undefined);
1132  if (res_basic == basic_undefined) {
1133  res_basic = copy_basic (cur_b);
1134  }
1135  else {
1136  basic old = res_basic;
1137  res_basic = basic_maximum (old, cur_b);
1138  free_basic (old);
1139  pips_assert ("expression_to_type returns a basic undefined",
1140  !basic_overloaded_p (res_basic));
1141  }
1142  free_type (tmp);
1143  }
1144  string str_copy = strdup (in_c);
1145  get_c_full_name (&str_copy, res_basic);
1146  string result = ppt_call (str_copy, le);
1147  if (res_basic != basic_undefined)
1148  free_basic (res_basic);
1149  free (str_copy);
1150  return result;
1151 }
basic copy_basic(basic p)
BASIC.
Definition: ri.c:104
void free_type(type p)
Definition: ri.c:2658
void free_basic(basic p)
Definition: ri.c:107
static void get_c_full_name(string *base_in_c, basic b)
fill the c_base_name to get the c full name accorgind to its basic
static string ppt_call(const char *in_c, list le)
SG: PBM spotted HERE.
type expression_to_type(expression)
For an array declared as int a[10][20], the type returned for a[i] is int [20].
Definition: type.c:2486
basic basic_maximum(basic, basic)
Definition: type.c:1816
#define basic_overloaded_p(x)
Definition: ri.h:623

References basic_maximum(), basic_overloaded_p, basic_undefined, copy_basic(), exp, EXPRESSION, expression_to_type(), FOREACH, free(), free_basic(), free_type(), gen_length(), get_c_full_name(), pips_assert, pips_debug, ppt_call(), strdup(), type_variable, type_variable_p, and variable_basic.

+ Here is the call graph for this function:

◆ ppt_min_max()

static string ppt_min_max ( const char *  in_c,
list  le 
)
static

Definition at line 1156 of file cprettyprinter.c.

1157 {
1158  bool flag = false;
1159  bool pointer = !get_bool_property ("CROUGH_SCALAR_BY_VALUE_IN_FCT_CALL");
1160  expression exp = EXPRESSION (CAR (le));
1161  string arg = c_expression (exp, false);
1162  string result = strdup(concatenate ((expression_scalar_p(exp) &&
1163  pointer)? "&" : "", arg, NULL));
1164  POP (le);
1165  free (arg);
1166 
1167  FOREACH (EXPRESSION, e, le){
1168  arg = c_expression(e,false);
1169  string old = result;
1170  result = strdup(concatenate(in_c , OPENPAREN, old, ", ",
1171  expression_scalar_p(e) && pointer ? "&" : "",
1172  arg, CLOSEPAREN, NULL));
1173  free(arg);
1174  free(old);
1175  flag = true;
1176  }
1177 
1178  pips_assert ("min and max should have at least 2 arguments", flag == true);
1179  return result;
1180 }

References c_expression(), CAR, CLOSEPAREN, concatenate(), exp, EXPRESSION, expression_scalar_p, FOREACH, free(), get_bool_property(), OPENPAREN, pips_assert, POP, and strdup().

+ Here is the call graph for this function:

◆ ppt_must_error()

static string ppt_must_error ( const char *  in_f,
_UNUSED_ list  le 
)
static
Parameters
in_f,theinstrinsic in fortran

Definition at line 1196 of file cprettyprinter.c.

1197 {
1198  string result = strdup ("");
1199  pips_user_error("This intrinsic should not be found in a fortran code: %s\n",
1200  in_f);
1201  return result;
1202 }

◆ ppt_unary()

static string ppt_unary ( const char *  in_c,
list  le 
)
static

Definition at line 1018 of file cprettyprinter.c.

1019 {
1020  string e, result;
1021  pips_assert("one arg to unary call", gen_length(le)==1);
1022  e = c_expression(EXPRESSION(CAR(le)),false);
1023  result = strdup(concatenate(in_c, SPACE, e, NULL));
1024  free(e);
1025  return result;
1026 }

References c_expression(), CAR, concatenate(), EXPRESSION, free(), gen_length(), pips_assert, SPACE, and strdup().

+ Here is the call graph for this function:

◆ ppt_unary_post()

static string ppt_unary_post ( const char *  in_c,
list  le 
)
static

Definition at line 1028 of file cprettyprinter.c.

1029 {
1030  string e, result;
1031  pips_assert("one arg to unary call", gen_length(le)==1);
1032  e = c_expression(EXPRESSION(CAR(le)),false);
1033  result = strdup(concatenate(e, SPACE, in_c, NULL));
1034  free(e);
1035  return result;
1036 }

References c_expression(), CAR, concatenate(), EXPRESSION, free(), gen_length(), pips_assert, SPACE, and strdup().

+ Here is the call graph for this function:

◆ ppt_unknown()

static string ppt_unknown ( const char *  in_f,
list  le 
)
static
Parameters
in_f,theinstrinsic in fortran

Definition at line 1185 of file cprettyprinter.c.

1186 {
1187  if (get_bool_property ("CROUGH_PRINT_UNKNOWN_INTRINSIC") == false)
1188  pips_user_error ("This intrinsic can not be tranbslated in c: %s\n", in_f);
1189  string result = ppt_call (in_f, le);
1190  return result;
1191 }

References get_bool_property(), pips_user_error, and ppt_call().

+ Here is the call graph for this function:

◆ print_c_code()

bool print_c_code ( const char *  module_name)

C indentation thru indent.

Parameters
module_nameodule_name

Definition at line 2262 of file cprettyprinter.c.

2263 {
2264  string crough, cpretty, dir, cmd;
2265 
2266  crough = db_get_memory_resource(DBR_CROUGH, module_name, true);
2267  cpretty = db_build_file_resource_name(DBR_C_PRINTED_FILE, module_name, CPRETTY);
2269 
2270  cmd = strdup(concatenate(INDENT, " ",
2271  dir, "/", crough, " -st > ",
2272  dir, "/", cpretty, NULL));
2273 
2274  safe_system(cmd);
2275 
2276  DB_PUT_FILE_RESOURCE(DBR_C_PRINTED_FILE, module_name, cpretty);
2277  free(cmd);
2278  free(dir);
2279 
2280  return true;
2281 }
#define CPRETTY
#define INDENT
const char * module_name(const char *s)
Return the module part of an entity name.
Definition: entity_names.c:296
string db_get_memory_resource(const char *rname, const char *oname, bool pure)
Return the pointer to the resource, whatever it is.
Definition: database.c:755
#define DB_PUT_FILE_RESOURCE
Put a file resource into the current workspace database.
Definition: pipsdbm-local.h:85
string db_build_file_resource_name(const char *rname, const char *oname, const char *suffix)
returns an allocated file name for a file resource.
Definition: lowlevel.c:169
void safe_system(string)
system.c
Definition: system.c:38
string db_get_current_workspace_directory(void)
Definition: workspace.c:96

References concatenate(), CPRETTY, db_build_file_resource_name(), db_get_current_workspace_directory(), db_get_memory_resource(), DB_PUT_FILE_RESOURCE, free(), INDENT, module_name(), safe_system(), and strdup().

+ Here is the call graph for this function:

◆ print_crough()

bool print_crough ( const char *  module_name)

save to file

Parameters
module_nameodule_name

Definition at line 2194 of file cprettyprinter.c.

2195 {
2196  FILE * out;
2197  string ppt, crough, dir, filename;
2198  entity module;
2199  statement stat;
2200  list l_effect = NULL;
2201 
2202  // get what is needed from PIPS DBM
2203  crough = db_build_file_resource_name(DBR_CROUGH, module_name, CROUGH);
2206  filename = strdup(concatenate(dir, "/", crough, NULL));
2207  stat = (statement) db_get_memory_resource(DBR_CODE, module_name, true);
2208  l_effect = effects_to_list((effects)
2209  db_get_memory_resource(DBR_SUMMARY_EFFECTS,
2210  module_name, true));
2213 
2214  debug_on("CPRETTYPRINTER_DEBUG_LEVEL");
2215  pips_debug(1, "Begin C prettyprrinter for %s\n", entity_name(module));
2216 
2217  // init the list needed for the function pre and postlude
2218  l_type = NIL;
2219  l_name = NIL;
2220  l_rename = NIL;
2221  l_entity = NIL;
2222  l_written = NIL;
2223  // build the list of written entity
2224  build_written_list (l_effect);
2225 
2226  // get the c code as a string
2227  ppt = c_code_string(module, stat);
2228  pips_debug(1, "end\n");
2229  debug_off();
2230 
2231  /* save to file */
2232  out = safe_fopen(filename, "w");
2233  fprintf(out, "/* C pretty print for module %s. */\n", module_name);
2234  fprintf(out, "%s", ppt);
2235  safe_fclose(out, filename);
2236 
2237  // free and reset strin lists
2243  l_type = NIL;
2244  l_name = NIL;
2245  l_rename = NIL;
2246  l_entity = NIL;
2247  l_written = NIL;
2248  free(ppt);
2249  free(dir);
2250  free(filename);
2251 
2252  DB_PUT_FILE_RESOURCE(DBR_CROUGH, module_name, crough);
2253 
2256 
2257  return true;
2258 }
static FILE * out
Definition: alias_check.c:128
struct _newgen_struct_statement_ * statement
Definition: cloning.h:21
static void build_written_list(list l)
#define CROUGH
static list l_type
static string c_code_string(entity module, statement stat)
static list l_rename
static list l_entity
static list l_name
list effects_to_list(effects)
Definition: effects.c:209
FILE * safe_fopen(const char *filename, const char *what)
Definition: file.c:67
int safe_fclose(FILE *stream, const char *filename)
Definition: file.c:77
void reset_current_module_entity(void)
Reset the current module entity.
Definition: static.c:97
void reset_current_module_statement(void)
Reset the current module statement.
Definition: static.c:221
statement set_current_module_statement(statement)
Set the current module statement.
Definition: static.c:165
entity set_current_module_entity(entity)
static.c
Definition: static.c:66
void gen_free_string_list(list ls)
Definition: list.c:564
#define debug_on(env)
Definition: misc-local.h:157
#define debug_off()
Definition: misc-local.h:160
entity module_name_to_entity(const char *mn)
This is an alias for local_name_to_top_level_entity.
Definition: entity.c:1479
#define entity_name(x)
Definition: ri.h:2790
int fprintf()
test sc_min : ce test s'appelle par : programme fichier1.data fichier2.data ...

References build_written_list(), c_code_string(), concatenate(), CROUGH, db_build_file_resource_name(), db_get_current_workspace_directory(), db_get_memory_resource(), DB_PUT_FILE_RESOURCE, debug_off, debug_on, effects_to_list(), entity_name, fprintf(), free(), gen_free_list(), gen_free_string_list(), l_entity, l_name, l_rename, l_type, l_written, module, module_name(), module_name_to_entity(), NIL, out, pips_debug, reset_current_module_entity(), reset_current_module_statement(), safe_fclose(), safe_fopen(), set_current_module_entity(), set_current_module_statement(), and strdup().

+ Here is the call graph for this function:

◆ print_interface()

bool print_interface ( const char *  module_name)

cprettyprinter.c

save to file

Parameters
module_nameodule_name

Definition at line 2151 of file cprettyprinter.c.

2152 {
2153  FILE * out;
2154  string interface_code, interface, dir, filename;
2155  entity module;
2156  statement stat;
2157 
2158  // get what is needed from PIPS DBM
2159  interface = db_build_file_resource_name(DBR_INTERFACE, module_name, INTERFACE);
2162  filename = strdup(concatenate(dir, "/", interface, NULL));
2163  stat = (statement) db_get_memory_resource(DBR_CODE, module_name, true);
2164 
2167 
2168  debug_on("INTERFACE_DEBUG_LEVEL");
2169  pips_debug(1, "Begin print_interface for %s\n", entity_name(module));
2170 
2171  // get the inteface code as a string
2172  interface_code = interface_code_string(module, stat);
2173  pips_debug(1, "end\n");
2174  debug_off();
2175 
2176  /* save to file */
2177  out = safe_fopen(filename, "w");
2178  fprintf(out, "! Fortran interface module for %s. \n", module_name);
2179  fprintf(out, "%s", interface_code);
2180  safe_fclose(out, filename);
2181 
2182  DB_PUT_FILE_RESOURCE(DBR_INTERFACE, module_name, INTERFACE);
2183 
2186 
2187  free (interface_code);
2188  free (dir);
2189  free (filename);
2190 
2191  return true;
2192 }
static string interface_code_string(entity module, _UNUSED_ statement stat)
#define INTERFACE

References concatenate(), db_build_file_resource_name(), db_get_current_workspace_directory(), db_get_memory_resource(), DB_PUT_FILE_RESOURCE, debug_off, debug_on, entity_name, fprintf(), free(), INTERFACE, interface_code_string(), module, module_name(), module_name_to_entity(), out, pips_debug, reset_current_module_entity(), reset_current_module_statement(), safe_fclose(), safe_fopen(), set_current_module_entity(), set_current_module_statement(), and strdup().

+ Here is the call graph for this function:

◆ scalar_by_pointer()

static bool scalar_by_pointer ( entity  var)
static

we want to decide if a scalar variable need to be passed by pointer or by value to a C function.

Fortran77 assumes that all scalars are passed by pointer. Starting With Fotran95, the arguments can be passed by value by using interfaces.

Returns
true if the variable has to be passed by pointer
Parameters
var,thevariable to be test as an entity

Definition at line 280 of file cprettyprinter.c.

280  {
281  // init result to false
282  bool result = false;
283  if ((get_bool_property ("CROUGH_FORTRAN_USES_INTERFACE") == false)) {
284  // no interface, var is a scalar
285  result = true;
286  }
287  else if ((get_bool_property ("CROUGH_FORTRAN_USES_INTERFACE") == true) &&
288  (get_bool_property ("CROUGH_SCALAR_BY_VALUE_IN_FCT_DECL") == false) &&
289  (written_p (var) == true)) {
290  // interface exists but var is written (and user doesn't use the property
291  // to force the passing of scalar by value)
292  result = true;
293  }
294 
295  return result;
296 }
static bool written_p(entity e)

References get_bool_property(), and written_p().

Referenced by this_entity_cdeclaration().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ scalar_postlude()

static string scalar_postlude ( )
static

Definition at line 248 of file cprettyprinter.c.

248  {
249  string result = NULL;
250  string previous = NULL;
251  list n = l_name;
252  list r = l_rename;
253  list e = l_entity;
254 
255  for (; n != NIL && r != NIL && e != NIL; n = n->cdr, r = r->cdr, e = e->cdr) {
256  if (written_p (gen_car (e))) {
257  result = strdup (concatenate ("*", (string) gen_car (r), " = ",
258  (string) gen_car (n), ";\n", previous, NULL));
259  if (previous != NULL) free (previous);
260  previous = result;
261  pips_debug (5, "entity %s (%p) restored\n",
263  gen_car (e));
264  } else {
265  pips_debug (5, "entity %s (%p) not restored\n",
267  gen_car (e));
268  }
269  }
270  return (result == NULL) ? strdup("") : result;
271 }
void * gen_car(list l)
Definition: list.c:364
struct cons * cdr
The pointer to the next element.
Definition: newgen_list.h:43

References cons::cdr, concatenate(), entity_local_name(), free(), gen_car(), l_entity, l_name, l_rename, NIL, pips_debug, strdup(), and written_p().

Referenced by c_call().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ scalar_prelude()

static string scalar_prelude ( )
static

Definition at line 231 of file cprettyprinter.c.

231  {
232  string result = NULL;
233  string previous = NULL;
234  list t = l_type;
235  list n = l_name;
236  list r = l_rename;
237  for (; n != NIL && r!= NIL && t!= NIL; n = n->cdr, r = r->cdr, t = t->cdr) {
238  result = strdup (concatenate ((char*) gen_car (t), SPACE,
239  (string) gen_car (n), " = ", "*",
240  (string) gen_car (r), ";\n", previous,
241  NULL));
242  if (previous != NULL) free (previous);
243  previous = result;
244  }
245  return (result == NULL) ? strdup("") : result;
246 }

References cons::cdr, concatenate(), free(), gen_car(), l_name, l_rename, l_type, NIL, SPACE, and strdup().

Referenced by c_code_string().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ this_entity_cdeclaration()

static string this_entity_cdeclaration ( entity  var,
bool  fct_sig 
)
static
Parameters
var,thevariable to get the c declaration
fct_sig,setto true if the variable is part of a function signature

Many possible combinations

This is a typedef name, what about typedef int myint[5] ???

hat about real, double, string, ... ?

ram r = storage_ram(s); entity sec = ram_section(r); if ((sec == CurrentSourceFileStaticArea) || (sec == CurrentStaticArea)) result = "static ";

problems with order !

It is an expression...

Definition at line 578 of file cprettyprinter.c.

579 {
580  string result = NULL;
581  //string name = entity_local_name(var);
582  type t = entity_type(var);
583  storage s = entity_storage(var);
584  pips_debug(2,"Entity name : %s\n",entity_name(var));
585  /* Many possible combinations */
586 
587  /* This is a typedef name, what about typedef int myint[5] ??? */
588  if (typedef_entity_p(var))
589  {
590  string tmp = NULL;
591  tmp=c_entity_local_name(var);
592  result = strdup(concatenate("typedef ", c_type_string(t),SPACE,tmp,NULL));
593  free(tmp);
594  return result;
595  }
596 
597  switch (storage_tag(s)) {
598  case is_storage_rom:
599  {
600  value va = entity_initial(var);
601  if (!value_undefined_p(va))
602  {
603  constant c = NULL;
604  if (value_constant_p(va))
605  c = value_constant(va);
606  else if (value_symbolic_p(va))
608  if (c)
609  {
610  if (constant_int_p(c))
611  {
612  string sval = int2a(constant_int(c));
613  string svar = c_entity_local_name(var);
616  string sbasic = basic_to_string(entity_basic(var));
618  asprintf(&result,"static const %s %s = %s\n",sbasic,svar,sval);
619  free(sval);
620  free(svar);
621  free(sbasic);
622  return result;
623  }
624  /*What about real, double, string, ... ?*/
625  }
626  }
627  break;
628  }
629  case is_storage_ram:
630  {
631  /* ram r = storage_ram(s);
632  entity sec = ram_section(r);
633  if ((sec == CurrentSourceFileStaticArea) || (sec == CurrentStaticArea))
634  result = "static ";*/
635  break;
636  }
637  default:
638  break;
639  }
640 
641  switch (type_tag(t)) {
642  case is_type_variable:
643  {
644  variable v = type_variable(t);
645  string sptr, st, sd, svar, sq, ext;
646  value val = entity_initial(var);
648  sd = c_dim_string(variable_dimensions(v), fct_sig);
650  svar = c_entity_local_name(var);
651 
652  // In the case of a signature check if the scalars need to
653  // be passed by pointers. If the check return true
654  // a "*" must be added
655  if ((fct_sig == true) && (variable_dimensions(v) == NIL) &&
656  (scalar_by_pointer (var) == true)) {
657  ext = SCALAR_IN_SIG_EXT;
658  sptr = "*";
659  l_type = gen_string_cons(strdup(concatenate(sq, st, NULL)),
660  l_type);
661  l_name = gen_string_cons(strdup(concatenate(svar, NULL)),
662  l_name);
663  l_rename = gen_string_cons(strdup(concatenate(svar,ext,NULL)),
664  l_rename);
666  }
667  // In case of a signature check if the arrays need to
668  // be passed by pointers. If the check return true
669  // a "*" must be added and the dim must be remove
670  else if ((fct_sig == true) && (variable_dimensions(v) != NIL) &&
671  (get_bool_property("CROUGH_ARRAY_PARAMETER_AS_POINTER") == true)) {
672  ext = "";
673  sptr = "*";
674  free (sd);
675  sd = strdup ("");
676  }
677  else {
678  ext = "";
679  sptr = "";
680  }
681 
682 
683  /* problems with order !*/
684  result = strdup(concatenate(sq, st, sptr, SPACE, svar, ext,
685  sd, NULL));
686  free(svar);
687  if (!value_undefined_p(val))
688  {
689  if (value_expression_p(val))
690  {
692  if (brace_expression_p(exp))
693  result = strdup(concatenate(result,"=",
695  else
696  result = strdup(concatenate(result,"=",
698  NULL));
699  }
700  }
701  if (basic_bit_p(variable_basic(v)))
702  {
703  /* It is an expression... */
704  _int i = (_int) basic_bit(variable_basic(v));
705  pips_debug(2,"Basic bit %td",i);
706  result = strdup(concatenate(result,":",int2a(i),NULL));
707  user_error("this_entity_cdeclaration",
708  "Bitfield to be finished...");
709  }
710  free(st);
711  free(sd);
712  break;
713  }
714  case is_type_struct:
715  {
716  list l = type_struct(t);
717  string tmp =NULL;
718  tmp = c_entity_local_name(var);
719  result = strdup(concatenate("struct ",tmp, "{", NL,NULL));
720  free(tmp);
721  MAP(ENTITY,ent,
722  {
723  string s = this_entity_cdeclaration(ent, fct_sig);
724  result = strdup(concatenate(result, s, SEMICOLON, NULL));
725  free(s);
726  },l);
727  result = strdup(concatenate(result,"}", NULL));
728  break;
729  }
730  case is_type_union:
731  {
732  list l = type_union(t);
733  string tmp =NULL;
734  tmp = c_entity_local_name(var);
735  result = strdup(concatenate("union ",tmp, "{", NL,NULL));
736  free(tmp);
737  MAP(ENTITY,ent,
738  {
739  string s = this_entity_cdeclaration(ent, fct_sig);
740  result = strdup(concatenate(result, s, SEMICOLON, NULL));
741  free(s);
742  },l);
743  result = strdup(concatenate(result,"}", NULL));
744  break;
745  }
746  case is_type_enum:
747  {
748  list l = type_enum(t);
749  bool first = true;
750  string tmp = NULL;
751  tmp = c_entity_local_name(var);
752  result = strdup(concatenate("enum ", tmp, " {",NULL));
753  free(tmp);
754  MAP(ENTITY,ent,
755  {
756  tmp = c_entity_local_name(ent);
757  result = strdup(concatenate(result,first?"":",",tmp,NULL));
758  free(tmp);
759  first = false;
760  },l);
761  result = strdup(concatenate(result,"}", NULL));
762  break;
763  }
764  default:
765  break;
766  }
767 
768  return result? result: strdup("");
769 }
static string c_qualifier_string(list l)
static bool scalar_by_pointer(entity var)
we want to decide if a scalar variable need to be passed by pointer or by value to a C function.
static string c_dim_string(list ldim, bool fct_sig)
list gen_string_cons(string s, const list l)
Definition: list.c:919
enum language_utype get_prettyprint_language_tag()
Definition: language.c:67
void set_prettyprint_language_tag(enum language_utype lang)
set the prettyprint language from a language_utype argument
Definition: language.c:143
#define asprintf
Definition: misc-local.h:225
#define user_error(fn,...)
Definition: misc-local.h:265
string basic_to_string(basic)
Definition: type.c:87
bool typedef_entity_p(entity e)
Definition: entity.c:1902
basic entity_basic(entity e)
return the basic associated to entity e if it's a function/variable/constant basic_undefined otherwis...
Definition: entity.c:1380
#define value_undefined_p(x)
Definition: ri.h:3017
#define type_struct(x)
Definition: ri.h:2964
#define value_constant(x)
Definition: ri.h:3073
#define storage_tag(x)
Definition: ri.h:2515
#define symbolic_constant(x)
Definition: ri.h:2599
#define constant_int(x)
Definition: ri.h:850
#define value_constant_p(x)
Definition: ri.h:3071
#define value_symbolic(x)
Definition: ri.h:3070
@ is_storage_rom
Definition: ri.h:2494
@ is_storage_ram
Definition: ri.h:2492
#define type_enum(x)
Definition: ri.h:2970
#define constant_int_p(x)
Definition: ri.h:848
#define variable_qualifiers(x)
Definition: ri.h:3124
#define value_expression_p(x)
Definition: ri.h:3080
#define basic_bit_p(x)
Definition: ri.h:632
#define value_expression(x)
Definition: ri.h:3082
language_utype
Definition: ri.h:1565
@ is_language_c
Definition: ri.h:1567
#define type_union(x)
Definition: ri.h:2967

References asprintf, basic_bit, basic_bit_p, basic_to_string(), brace_expression_p(), c_basic_string(), c_brace_expression_string(), c_dim_string(), c_entity_local_name(), c_qualifier_string(), c_type_string(), concatenate(), constant_int, constant_int_p, ENTITY, entity_basic(), entity_initial, entity_name, entity_storage, entity_type, exp, expression_to_string(), free(), gen_entity_cons(), gen_string_cons(), get_bool_property(), get_prettyprint_language_tag(), int2a(), is_language_c, is_storage_ram, is_storage_rom, is_type_enum, is_type_struct, is_type_union, is_type_variable, l_entity, l_name, l_rename, l_type, MAP, NIL, NL, pips_debug, scalar_by_pointer(), SCALAR_IN_SIG_EXT, SEMICOLON, set_prettyprint_language_tag(), SPACE, storage_tag, strdup(), symbolic_constant, type_enum, type_struct, type_tag, type_union, type_variable, typedef_entity_p(), user_error, value_constant, value_constant_p, value_expression, value_expression_p, value_symbolic, value_symbolic_p, value_undefined_p, variable_basic, variable_dimensions, and variable_qualifiers.

Referenced by c_declarations(), and c_statement().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ variable_p()

static bool variable_p ( entity  e)
static

Definition at line 779 of file cprettyprinter.c.

780 {
781  storage s = entity_storage(e);
782  return type_variable_p(entity_type(e)) &&
783  (storage_ram_p(s) || storage_return_p(s));
784 }
#define storage_ram_p(x)
Definition: ri.h:2519
#define storage_return_p(x)
Definition: ri.h:2516

References entity_storage, entity_type, storage_ram_p, storage_return_p, and type_variable_p.

Referenced by parameter_or_variable_p().

+ Here is the caller graph for this function:

◆ written_p()

static bool written_p ( entity  e)
static

Definition at line 226 of file cprettyprinter.c.

226  {
227  return gen_in_list_p (e, l_written);
228 }
bool gen_in_list_p(const void *vo, const list lx)
tell whether vo belongs to lx
Definition: list.c:734

References gen_in_list_p(), and l_written.

Referenced by scalar_by_pointer(), and scalar_postlude().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ c_base_name_to_c_full_name

c_full_name c_base_name_to_c_full_name[]
static
Initial value:
= {
{"abs" , is_basic_int , 1 , "" , "" },
{"abs" , is_basic_int , 2 , "" , "" },
{"abs" , is_basic_int , 4 , "" , "" },
{"abs" , is_basic_int , 6 , "l" , "" },
{"abs" , is_basic_int , 8 , "ll" , "" },
{"abs" , is_basic_float , 4 , "f" , "f"},
{"abs" , is_basic_float , 8 , "f" , "" },
{"abs" , is_basic_complex , 8 , "c" , "f"},
{"abs" , is_basic_complex , 16 , "c" , "" },
{"pow" , is_basic_int , 1 , POW_PRE, "i"},
{"pow" , is_basic_int , 2 , POW_PRE, "i"},
{"pow" , is_basic_int , 4 , POW_PRE, "i"},
{"pow" , is_basic_int , 6 , POW_PRE, "i"},
{"pow" , is_basic_int , 8 , POW_PRE, "i"},
{"pow" , is_basic_float , 4 , "" , "f"},
{"pow" , is_basic_float , 8 , "" , "" },
{"pow" , is_basic_complex , 8 , "c" , "f"},
{"pow" , is_basic_complex , 16 , "c" , "" },
{NULL , is_basic_int , 0 , "" , "" }
}
#define POW_PRE

Definition at line 1072 of file cprettyprinter.c.

Referenced by get_c_full_name().

◆ intrinsic_to_c

struct s_ppt intrinsic_to_c[]
static

Definition at line 1196 of file cprettyprinter.c.

Referenced by get_ppt().

◆ l_entity

list l_entity = NIL
static

Definition at line 107 of file cprettyprinter.c.

Referenced by print_crough(), scalar_postlude(), and this_entity_cdeclaration().

◆ l_name

list l_name = NIL
static

◆ l_rename

list l_rename = NIL
static

◆ l_type

list l_type = NIL
static

Definition at line 104 of file cprettyprinter.c.

Referenced by print_crough(), scalar_prelude(), and this_entity_cdeclaration().

◆ l_written

list l_written = NIL
static

Definition at line 108 of file cprettyprinter.c.

Referenced by build_written_list(), print_crough(), and written_p().