PIPS
craft.c
Go to the documentation of this file.
1 /*
2 
3  $Id: craft.c 23470 2018-06-02 13:39:47Z coelho $
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of PIPS.
8 
9  PIPS is free software: you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  any later version.
13 
14  PIPS is distributed in the hope that it will be useful, but WITHOUT ANY
15  WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  FITNESS FOR A PARTICULAR PURPOSE.
17 
18  See the GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with PIPS. If not, see <http://www.gnu.org/licenses/>.
22 
23 */
24 #ifdef HAVE_CONFIG_H
25  #include "pips_config.h"
26 #endif
27 /* Prettyprinter for CRAFT loops.
28 
29  There are memory leaks here since a new expression is constructed.
30 
31 */
32 
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 
38 #include "linear.h"
39 #include "genC.h"
40 
41 #include "misc.h"
42 #include "properties.h"
43 
44 #include "text-util.h"
45 #include "ri-util.h"
46 
47 #include "prettyprint.h"
48 
49 /*=======================================================================*/
50 /* void rewrite_modulo_expression(exp):
51  *
52  * filter on the call "ca". It removes all the call to the modulo
53  * function.
54  *
55  * AP 94/12/20 */
56 
58 {
59  syntax sy;
60  call ca;
61  entity func;
62  list args;
63  expression first_arg;
64 
65  sy = expression_syntax(exp);
66  if(syntax_tag(sy) == is_syntax_call) {
67  ca = syntax_call(sy);
68  func = call_function(ca);
69  if(strcmp(entity_local_name(func), MODULO_OPERATOR_NAME) == 0) {
70  args = call_arguments(ca);
71  if(gen_length(args) != 2)
72  user_error("rewrite_modulo_expression",
73  "\nA modulo not with exactly 2 arguments\n");
74  else {
75  first_arg = EXPRESSION(CAR(args));
77  }
78  }
79  }
80 }
81 
82 /*======================================================================*/
85 {
87 
88  return(exp);
89 }
90 
91 /*======================================================================*/
92 text text_loop_craft(module, label, margin, obj, n, lr, lidx)
93  entity module;
94  const char* label;
95  int margin;
96  loop obj;
97  int n;
98  list lr, lidx;
99 {
100  text result_text = make_text(NIL);
101  instruction i;
102  entity idx;
103  range r;
104 
106  idx = loop_index(obj);
107  r = loop_range(obj);
108 
109  lr = gen_nconc(lr, CONS(RANGE, r, NIL));
110  lidx = gen_nconc(lidx, CONS(ENTITY, idx, NIL));
111 
112  if(!instruction_assign_p(i)) {
113  if(instruction_loop_p(i)) {
114  result_text = text_loop_craft(module, label, margin,
115  instruction_loop(i), n, lr, lidx);
116  }
117  }
118  else {
119  list pc, lli, llr, args, new_lli = NIL;
120  unformatted u;
121  int c;
122  char *comment;
124  syntax lhs_sy;
125 
126  args = call_arguments(instruction_call(i));
127  if(!ENDP(args))
128  lhs_exp = copy_expression(EXPRESSION(CAR(args)));
129  else
130  user_error("text_loop_craft",
131  "Call to an assign with no argument\n");
132 
133  lhs_sy = expression_syntax(lhs_exp);
134  if(syntax_tag(lhs_sy) != is_syntax_reference)
135  user_error("text_loop_craft", "\n An lhs expression not a ref\n");
136  else {
137  lli = reference_indices(syntax_reference(lhs_sy));
138  for(; !ENDP(lli); POP(lli)) {
139  new_lli = gen_nconc(new_lli, CONS(EXPRESSION,
141  NIL));
142  }
143  reference_indices(syntax_reference(lhs_sy)) = new_lli;
144  }
145 
146  // BUG: missing overflow checks
147  comment = (char*) malloc(64);
148  char * x = comment;
149  x += sprintf(comment, "CDIR$ DOSHARED(");
150  for(lli = lidx; !ENDP(lli); POP(lli)) {
151  x += sprintf(x, "%s", entity_local_name(ENTITY(CAR(lli))));
152  if(CDR(lli) != NIL)
153  x += sprintf(x, ", ");
154  }
155  list pdl = NIL;
156  x += sprintf(x, ") ON %s\n", words_to_string(words_expression(lhs_exp, &pdl)));
157  gen_free_list(pdl);
159  comment));
160 
161  for(lli = lidx, llr = lr, c = 0; !ENDP(lli); POP(lli), POP(llr), c++) {
162  pc = CHAIN_SWORD(NIL, "DO " );
163  pc = CHAIN_SWORD(pc, entity_local_name(ENTITY(CAR(lli))));
164  pc = CHAIN_SWORD(pc, " = ");
165  pc = gen_nconc(pc, words_loop_range(RANGE(CAR(llr)), &pdl));
166  gen_free_list(pdl);
167  u = make_unformatted(strdup(label), n,
168  margin+c*INDENTATION, pc);
169  ADD_SENTENCE_TO_TEXT(result_text,
171  }
172  MERGE_TEXTS(result_text, text_statement(module, margin+c*INDENTATION,
173  loop_body(obj), &pdl));
174  gen_free_list(pdl);
175 
176  for(c = gen_length(lidx)-1; c > -1; c--) {
177  ADD_SENTENCE_TO_TEXT(result_text,
179  "ENDDO"));
180  }
181  }
182  return(result_text);
183 }
expression copy_expression(expression p)
EXPRESSION.
Definition: ri.c:850
unformatted make_unformatted(string a1, intptr_t a2, intptr_t a3, list a4)
Definition: text.c:149
sentence make_sentence(enum sentence_utype tag, void *val)
Definition: text.c:59
text make_text(list a)
Definition: text.c:107
text text_loop_craft(entity module, const char *label, int margin, loop obj, int n, list lr, list lidx)
=====================================================================
Definition: craft.c:92
expression remove_modulo(expression exp)
=====================================================================
Definition: craft.c:83
static void rewrite_modulo_expression(expression exp)
Prettyprinter for CRAFT loops.
Definition: craft.c:57
static void comment(string_buffer code, spoc_hardware_type hw, dagvtx v, int stage, int side, bool flip)
Definition: freia_spoc.c:52
#define gen_recurse(start, domain_number, flt, rwt)
Definition: genC.h:283
void * malloc(YYSIZE_T)
bool gen_true(__attribute__((unused)) gen_chunk *unused)
Return true and ignore the argument.
Definition: genClib.c:2780
bool instruction_assign_p(instruction i)
Test if an instruction is an assignment.
Definition: instruction.c:164
#define ENDP(l)
Test if a list is empty.
Definition: newgen_list.h:66
#define POP(l)
Modify a list pointer to point on the next element of the list.
Definition: newgen_list.h:59
#define NIL
The empty list (nil in Lisp)
Definition: newgen_list.h:47
size_t gen_length(const list l)
Definition: list.c:150
#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 CAR(pcons)
Get the value of the first element of a list.
Definition: newgen_list.h:92
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
#define user_error(fn,...)
Definition: misc-local.h:265
static char * module
Definition: pips.c:74
list words_expression(expression obj, list *ppdl)
This one is exported.
Definition: misc.c:2611
list words_loop_range(range obj, list *ppdl)
exported for craft
Definition: misc.c:434
text text_statement(entity, int, statement, list *)
#define INDENTATION
#define MODULO_OPERATOR_NAME
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
#define loop_body(x)
Definition: ri.h:1644
#define expression_domain
newgen_execution_domain_defined
Definition: ri.h:154
#define syntax_reference(x)
Definition: ri.h:2730
#define syntax_tag(x)
Definition: ri.h:2727
#define instruction_loop_p(x)
Definition: ri.h:1518
#define call_function(x)
Definition: ri.h:709
#define ENTITY(x)
ENTITY.
Definition: ri.h:2755
#define instruction_loop(x)
Definition: ri.h:1520
@ is_syntax_call
Definition: ri.h:2693
@ is_syntax_reference
Definition: ri.h:2691
#define EXPRESSION(x)
EXPRESSION.
Definition: ri.h:1217
#define expression_undefined
Definition: ri.h:1223
#define RANGE(x)
RANGE.
Definition: ri.h:2257
#define reference_indices(x)
Definition: ri.h:2328
#define syntax_call(x)
Definition: ri.h:2736
#define statement_instruction(x)
Definition: ri.h:2458
#define instruction_call(x)
Definition: ri.h:1529
#define loop_range(x)
Definition: ri.h:1642
#define call_arguments(x)
Definition: ri.h:711
#define expression_syntax(x)
Definition: ri.h:1247
#define loop_index(x)
Definition: ri.h:1640
char * strdup()
static char * x
Definition: split_file.c:159
The structure used to build lists in NewGen.
Definition: newgen_list.h:41
#define CHAIN_SWORD(l, s)
#define MERGE_TEXTS(r, t)
#define MAKE_ONE_WORD_SENTENCE(m, s)
#define ADD_SENTENCE_TO_TEXT(t, p)
string words_to_string(cons *lw)
Definition: print.c:211
@ is_sentence_formatted
Definition: text.h:57
@ is_sentence_unformatted
Definition: text.h:58
#define exp
Avoid some warnings from "gcc -Wshadow".
Definition: vasnprintf.c:207