PIPS
codegen.c File Reference
#include "local.h"
+ Include dependency graph for codegen.c:

Go to the source code of this file.

Macros

#define INTEGER_DECL   " INTEGER"
 We define a set of constant in order to a more generic function for the insert of the declarations of the new created variables. More...
 
#define REAL_DECL   " REAL"
 
#define COMPLEX_DECL   " COMPLEX"
 
#define LOGICAL_DECL   " LOGICAL"
 
#define DOUBLE_DECL   " DOUBLE PRECISION"
 
#define CHARACTER_DECL   " CHARACTER"
 
#define SPACE   " "
 
#define COMA   ","
 
#define NEWLINE   "\n *"
 
#define ENDLINE   "\nC\n"
 
#define LINE_LENGHT   68
 
#define MARGIN_LENGHT   7
 

Functions

static expression find_tmp_of_exp (expression exp)
 =========================================================================== More...
 
void put_stmt_in_Block (statement new_stmt, Block *cb)
 =========================================================================== More...
 
expression assign_tmp_to_exp (expression exp, Block *cb)
 =========================================================================== More...
 
void insert_one_type_declaration (entity mod_entity, list var_to_decl, string type_decl)
 =========================================================================== More...
 
void insert_new_declarations (char *mod_name)
 =========================================================================== More...
 
void store_expression (expression exp, Block *cb)
 =========================================================================== More...
 

Variables

hash_table MemToTmp
 – codegen.c More...
 

Macro Definition Documentation

◆ CHARACTER_DECL

#define CHARACTER_DECL   " CHARACTER"

Definition at line 215 of file codegen.c.

◆ COMA

#define COMA   ","

Definition at line 217 of file codegen.c.

◆ COMPLEX_DECL

#define COMPLEX_DECL   " COMPLEX"

Definition at line 212 of file codegen.c.

◆ DOUBLE_DECL

#define DOUBLE_DECL   " DOUBLE PRECISION"

Definition at line 214 of file codegen.c.

◆ ENDLINE

#define ENDLINE   "\nC\n"

Definition at line 219 of file codegen.c.

◆ INTEGER_DECL

#define INTEGER_DECL   " INTEGER"

We define a set of constant in order to a more generic function for the insert of the declarations of the new created variables.

Definition at line 210 of file codegen.c.

◆ LINE_LENGHT

#define LINE_LENGHT   68

Definition at line 220 of file codegen.c.

◆ LOGICAL_DECL

#define LOGICAL_DECL   " LOGICAL"

Definition at line 213 of file codegen.c.

◆ MARGIN_LENGHT

#define MARGIN_LENGHT   7

Definition at line 221 of file codegen.c.

◆ NEWLINE

#define NEWLINE   "\n *"

Definition at line 218 of file codegen.c.

◆ REAL_DECL

#define REAL_DECL   " REAL"

Definition at line 211 of file codegen.c.

◆ SPACE

#define SPACE   " "

Definition at line 216 of file codegen.c.

Function Documentation

◆ assign_tmp_to_exp()

expression assign_tmp_to_exp ( expression  exp,
Block cb 
)

===========================================================================

expression assign_tmp_to_exp(expression exp, Block *cb): Produces an assign statement of "exp" in a temporary (this is a load), puts it in "cb" and returns the expression of the temporary.

There are two cases:

  1. "exp" is found to be a scalar variable already loaded in a temporary, then the expression of the temporary is returned.
  2. otherwise, we do the following manipulations: _ Firstly, we compute a new basic of the same kind as "exp". _ Secondly, we create a new temporary with the basic found above. _ Thirdly, the new assign statement is created. _ Fourthly, this new statement is put in the current block ("cb"). _ Finaly, we return the expression of the new temporary.

The variable "cb" memorises the information about the block of statements that contains the expressions.

Called functions : _ entity_scalar_p() : ri-util/entity.c _ make_entity_expression() : ri-util/util.c _ make_assign_statement() : ri-util/statement.c

If this expression is a scalar variable already loaded in a temporary, then it is unnecessary to loaded it again. The only thing to do is to find the temporary that was used and return it.

We compute the basic of "exp".

We create a new temporary with the basic "tmp_basic".

If "exp_ent" is a scalar variable, this temporary is placed in MemToTmp at the key "exp_ent".

We create a new expression with this temporary.

The new assign statement is created ...

... and put in the current Block.

The new temporary is returned.

Parameters
expxp
cbb

Definition at line 155 of file codegen.c.

158 {
159 expression tmp;
160 entity tmp_ent, exp_ent = entity_undefined;
161 basic tmp_basic;
162 statement new_stmt;
163 
164 /* If this expression is a scalar variable already loaded in a
165  * temporary, then it is unnecessary to loaded it again. The only
166  * thing to do is to find the temporary that was used and return it.
167  */
169  {
171  if(entity_scalar_p(exp_ent))
172  {
173  tmp = find_tmp_of_exp(exp);
174  if(tmp != expression_undefined)
175  return(tmp);
176  }
177  }
178 
179 /* We compute the basic of "exp". */
180 tmp_basic = basic_of_expression(exp);
181 
182 /* We create a new temporary with the basic "tmp_basic". */
183 tmp_ent = make_new_entity(tmp_basic, TMP_ENT);
184 
185 /* If "exp_ent" is a scalar variable, this temporary is placed in
186  * MemToTmp at the key "exp_ent".
187  */
189  if(entity_scalar_p(exp_ent))
190  hash_put(MemToTmp, (char *) exp_ent, (char *) tmp_ent);
191 
192 /* We create a new expression with this temporary. */
193 tmp = make_entity_expression(tmp_ent, NIL);
194 
195 /* The new assign statement is created ... */
196 new_stmt = make_assign_statement(tmp, exp);
197 
198 /* ... and put in the current Block. */
199 put_stmt_in_Block(new_stmt, cb);
200 
201 /* The new temporary is returned. */
202 return(tmp);
203 }
#define TMP_ENT
hash_table MemToTmp
– codegen.c
Definition: atomizer.c:95
void put_stmt_in_Block(statement new_stmt, Block *cb)
===========================================================================
Definition: codegen.c:89
static expression find_tmp_of_exp(expression exp)
===========================================================================
Definition: codegen.c:58
#define NIL
The empty list (nil in Lisp)
Definition: newgen_list.h:47
statement make_assign_statement(expression, expression)
Definition: statement.c:583
void hash_put(hash_table htp, const void *key, const void *val)
This functions stores a couple (key,val) in the hash table pointed to by htp.
Definition: hash.c:364
expression make_entity_expression(entity e, cons *inds)
Definition: expression.c:176
basic basic_of_expression(expression)
basic basic_of_expression(expression exp): Makes a basic of the same basic as the expression "exp".
Definition: type.c:1383
entity make_new_entity(basic, int)
Definition: variable.c:898
bool entity_scalar_p(entity)
The concrete type of e is a scalar type.
Definition: variable.c:1113
#define syntax_reference(x)
Definition: ri.h:2730
#define syntax_tag(x)
Definition: ri.h:2727
#define reference_variable(x)
Definition: ri.h:2326
@ is_syntax_reference
Definition: ri.h:2691
#define entity_undefined
Definition: ri.h:2761
#define expression_undefined
Definition: ri.h:1223
#define expression_syntax(x)
Definition: ri.h:1247
#define exp
Avoid some warnings from "gcc -Wshadow".
Definition: vasnprintf.c:207

References basic_of_expression(), entity_scalar_p(), entity_undefined, exp, expression_syntax, expression_undefined, find_tmp_of_exp(), hash_put(), is_syntax_reference, make_assign_statement(), make_entity_expression(), make_new_entity(), MemToTmp, NIL, put_stmt_in_Block(), reference_variable, syntax_reference, syntax_tag, and TMP_ENT.

Referenced by atomizer_of_expression().

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

◆ find_tmp_of_exp()

static expression find_tmp_of_exp ( expression  exp)
static

===========================================================================

expression find_tmp_of_exp(expression exp): Returns the expression of the temporary in which the variable of "exp" was loaded. If "exp" is not atomic, or, if "exp" was not yet loaded, this function returns "expression_undefined".

This function uses the hash table MemToTmp. For a given memory variable you may have an associated temporary. This association is built when such a variable is loaded for the first time.

Called functions : _ entity_scalar_p() : ri-util/entity.c _ make_entity_expression() : ri-util/util.c

Definition at line 58 of file codegen.c.

60 {
62 
64  {
65  entity exp_ent, tmp_ent;
67  if(entity_scalar_p(exp_ent))
68  {
69  tmp_ent = (entity) hash_get(MemToTmp, (char *) exp_ent);
70  if(tmp_ent != entity_undefined)
71  ret_exp = make_entity_expression(tmp_ent, NIL);
72  }
73  }
74 return(ret_exp);
75 }
struct _newgen_struct_entity_ * entity
Definition: abc_private.h:14
void * hash_get(const hash_table htp, const void *key)
this function retrieves in the hash table pointed to by htp the couple whose key is equal to key.
Definition: hash.c:449

References entity_scalar_p(), entity_undefined, exp, expression_syntax, expression_undefined, hash_get(), is_syntax_reference, make_entity_expression(), MemToTmp, NIL, reference_variable, syntax_reference, and syntax_tag.

Referenced by assign_tmp_to_exp().

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

◆ insert_new_declarations()

void insert_new_declarations ( char *  mod_name)

===========================================================================

void insert_new_declarations(char *mod_name): puts in the declaration text of the module "mod_name" the declarations for the temporary, auxiliary and NLC variables.

Called functions : _ local_name_to_top_level_entity() : ri-util/entity.c

We declare only variables that are not INTEGER or REAL simple precision.

Parameters
mod_nameod_name

Definition at line 285 of file codegen.c.

286 {
287  entity mod_entity;
288 
289  mod_entity = local_name_to_top_level_entity(mod_name);
290 
291  /* We declare only variables that are not INTEGER or
292  * REAL simple precision.
293  */
294 /*
295  integer_entities = gen_nreverse(integer_entities);
296  real_entities = gen_nreverse(real_entities);
297 */
302 
303 /*
304  insert_one_type_declaration(mod_entity, integer_entities, INTEGER_DECL);
305  insert_one_type_declaration(mod_entity, real_entities, REAL_DECL);
306 */
311 
312  // ??? dandling pointers to be cleaned up later
319 }
#define DOUBLE_DECL
Definition: codegen.c:214
#define CHARACTER_DECL
Definition: codegen.c:215
#define COMPLEX_DECL
Definition: codegen.c:212
#define LOGICAL_DECL
Definition: codegen.c:213
void insert_one_type_declaration(entity mod_entity, list var_to_decl, string type_decl)
===========================================================================
Definition: codegen.c:229
list gen_nreverse(list cp)
reverse a list in place
Definition: list.c:304
void gen_free_list(list l)
free the spine of the list
Definition: list.c:327
list double_entities
list real_entities
list complex_entities
list logical_entities
list char_entities
list integer_entities
Make a new variable entity which name is one letter prefix + one incrementing number.
Definition: ri-util.h:2787
entity local_name_to_top_level_entity(const char *n)
This function try to find a top-level entity from a local name.
Definition: entity.c:1450

References char_entities, CHARACTER_DECL, COMPLEX_DECL, complex_entities, DOUBLE_DECL, double_entities, gen_free_list(), gen_nreverse(), insert_one_type_declaration(), integer_entities, local_name_to_top_level_entity(), LOGICAL_DECL, logical_entities, and real_entities.

+ Here is the call graph for this function:

◆ insert_one_type_declaration()

void insert_one_type_declaration ( entity  mod_entity,
list  var_to_decl,
string  type_decl 
)

===========================================================================

void insert_one_type_declaration(entity mod_entity, list var_to_decl, string type_decl): Inserts in the module "mod_entity" the declarations of the entities in the list "var_to_decl" with the type "type_decl".

Type declaration.

Lenght of the declaration to insert.

If the line is to long ..., we begin a new line!!

We separate the variables between comas.

Parameters
mod_entityod_entity
var_to_declar_to_decl
type_declype_decl

Definition at line 229 of file codegen.c.

233 {
234 string new_decls;
235 code mod_code;
236 list l;
237 int counter = strlen(type_decl);
238 
239 if(var_to_decl != NIL)
240  {
241  /* Type declaration. */
242  new_decls = strdup(concatenate(type_decl, (char *) NULL));
243  for(l = var_to_decl; l != NIL;)
244  {
245  entity e = ENTITY(CAR(l));
246 
247  /* Lenght of the declaration to insert. */
248  int decl_len = strlen(SPACE) + strlen(COMA) + strlen(entity_local_name(e));
249 
250  /* If the line is to long ..., we begin a new line!! */
251  if( (counter + decl_len) > LINE_LENGHT)
252  {
253  counter = MARGIN_LENGHT;
254  new_decls = strdup(concatenate(new_decls, NEWLINE, (char *) NULL));
255  }
256  new_decls = strdup(concatenate(new_decls, SPACE,
257  entity_local_name(e), (char *) NULL));
258  l = CDR(l);
259 
260  /* We separate the variables between comas. */
261  if(l != NIL)
262  new_decls = strdup(concatenate(new_decls, COMA, (char *) NULL));
263 
264  counter += decl_len;
265  }
266 
267  new_decls = strdup(concatenate(new_decls, ENDLINE, (char *) NULL));
268 
269  mod_code = entity_code(mod_entity);
270  code_decls_text(mod_code) = strdup(concatenate(code_decls_text(mod_code),
271  new_decls, (char *) NULL));
272  }
273 }
#define LINE_LENGHT
Definition: codegen.c:220
#define ENDLINE
Definition: codegen.c:219
#define MARGIN_LENGHT
Definition: codegen.c:221
#define SPACE
Definition: codegen.c:216
#define NEWLINE
Definition: codegen.c:218
#define COMA
Definition: codegen.c:217
#define CAR(pcons)
Get the value of the first element of a list.
Definition: newgen_list.h:92
#define CDR(pcons)
Get the list less its first element.
Definition: newgen_list.h:111
string concatenate(const char *,...)
Return the concatenation of the given strings.
Definition: string.c:183
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
code entity_code(entity e)
Definition: entity.c:1098
#define ENTITY(x)
ENTITY.
Definition: ri.h:2755
#define code_decls_text(x)
Definition: ri.h:786
char * strdup()
The structure used to build lists in NewGen.
Definition: newgen_list.h:41

References CAR, CDR, code_decls_text, COMA, concatenate(), ENDLINE, ENTITY, entity_code(), entity_local_name(), LINE_LENGHT, MARGIN_LENGHT, NEWLINE, NIL, SPACE, and strdup().

Referenced by insert_new_declarations().

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

◆ put_stmt_in_Block()

void put_stmt_in_Block ( statement  new_stmt,
Block cb 
)

===========================================================================

codegen.c

void put_stmt_in_Block(statement new_stmt, Block *cb): Puts "new_stmt" at the end of the list "cb->first".

First, if "new_stmt" is the first statement generated by the statement being translated (the current statement), then the caracteritics of "new_stmt" are taken from those of the current statement.

Second, "new_stmt" is added to the list of statements of "cb".

The current statement is the first element of the list "cb->last".

"stmt_generated" is false if the current statement has not produced any statement yet, in which case the new statement gets the "label", "number", "ordering" and "comments" (cf. RI) of the current statement.

The new statement gets the caracteristic of the current statement, while the latter gets the caracteristics of the new statement.

The current statement has, now, generated at least one statement.

The new statement is put just before the current statement in the block. Thus, it is put in last position in the list "cb->first".

Parameters
new_stmtew_stmt
cbb

Definition at line 89 of file codegen.c.

92 {
94 
95 /* The current statement is the first element of the list "cb->last". */
97 
98 /* "stmt_generated" is false if the current statement has not produced
99  * any statement yet, in which case the new statement gets the "label",
100  * "number", "ordering" and "comments" (cf. RI) of the current statement.
101  */
102 if (! cb->stmt_generated)
103  {
104  entity new_label;
105  /* The new statement gets the caracteristic of the current statement,
106  * while the latter gets the caracteristics of the new statement.
107  */
108  new_label = statement_label(new_stmt);
113 
114  statement_label(current_stmt) = new_label;
118 
119  /* The current statement has, now, generated at least one statement. */
120  cb->stmt_generated = true;
121  }
122 
123 /* The new statement is put just before the current statement in the block.
124  * Thus, it is put in last position in the list "cb->first".
125  */
126 cb->first = gen_nconc(cb->first, CONS(STATEMENT, new_stmt, NIL));
127 }
static statement current_stmt
#define CONS(_t_, _i_, _l_)
List element cell constructor (insert an element at the beginning of a list)
Definition: newgen_list.h:150
list gen_nconc(list cp1, list cp2)
physically concatenates CP1 and CP2 but do not duplicates the elements
Definition: list.c:344
#define STATEMENT_ORDERING_UNDEFINED
mapping.h inclusion
Definition: newgen-local.h:35
#define string_undefined
Definition: newgen_types.h:40
#define STATEMENT_NUMBER_UNDEFINED
default values
#define statement_ordering(x)
Definition: ri.h:2454
#define statement_label(x)
Definition: ri.h:2450
#define statement_comments(x)
Definition: ri.h:2456
#define statement_number(x)
Definition: ri.h:2452
#define STATEMENT(x)
STATEMENT.
Definition: ri.h:2413
bool stmt_generated
list first
list last

References CAR, CONS, current_stmt, Block::first, gen_nconc(), Block::last, NIL, STATEMENT, statement_comments, statement_label, statement_number, STATEMENT_NUMBER_UNDEFINED, statement_ordering, STATEMENT_ORDERING_UNDEFINED, Block::stmt_generated, and string_undefined.

Referenced by assign_tmp_to_exp(), and atomizer_of_external().

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

◆ store_expression()

void store_expression ( expression  exp,
Block cb 
)

===========================================================================

void store_expression(expression exp, Block *cb): Removes the key of the hash table MemToTmp corresponding to the variable contained in "exp". It means that the value of this variable is no longer equal to the value of the temporary stored in MemToTmp.

Note: "exp" must have a "syntax_tag" equal to "reference".

Called functions : _ entity_scalar_p() : ri-util/entity.c _ make_entity_expression() : ri-util/util.c _ make_assign_statement() : ri-util/statement.c

entity tmp_ent;

Parameters
expxp
cbb

Definition at line 335 of file codegen.c.

336 {
337  entity ent;
338 
339  pips_assert("true", cb==cb);
340 
342  pips_user_error("Expression is not a reference");
343 
345  if(entity_scalar_p(ent))
346  {
347  /* entity tmp_ent; */
348 
349  (void) hash_del(MemToTmp, (char *) ent);
350 
351  /*
352  tmp_ent = (entity) hash_get(MemToTmp, (char *) ent);
353  if(tmp_ent != entity_undefined)
354  {
355  expression tmp;
356  tmp = make_entity_expression(tmp_ent, NIL);
357  put_stmt_in_Block(make_assign_statement(exp, tmp), cb);
358  (void) hash_del(MemToTmp, (char *) ent);
359  }
360  */
361  }
362 }
void * hash_del(hash_table htp, const void *key)
this function removes from the hash table pointed to by htp the couple whose key is equal to key.
Definition: hash.c:439
#define pips_assert(what, predicate)
common macros, two flavors depending on NDEBUG
Definition: misc-local.h:172
#define pips_user_error
Definition: misc-local.h:147

References entity_scalar_p(), exp, expression_syntax, hash_del(), is_syntax_reference, MemToTmp, pips_assert, pips_user_error, reference_variable, syntax_reference, and syntax_tag.

+ Here is the call graph for this function:

Variable Documentation

◆ MemToTmp

hash_table MemToTmp
extern

– codegen.c

package atomizer : Alexis Platonoff, juillet 91

Functions that generates the new instructions. A hash table to map temporary variables (entities) to memory variables (entities).

– codegen.c

They are used for the declarations of the temporaries and the auxiliaries. A hash table to map temporary variables (entities) to memory variables (entities).

Definition at line 95 of file atomizer.c.

Referenced by assign_tmp_to_exp(), atomizer_of_external(), atomizer_of_intrinsic(), atomizer_of_loop(), atomizer_of_unstructured(), find_tmp_of_exp(), initialize_global_variables(), reset_global_variables(), and store_expression().