PIPS
instruction.c
Go to the documentation of this file.
1 /*
2 
3  $Id: instruction.c 23065 2016-03-02 09:05:50Z 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 /** @file
28 
29  Methods dealing with instructions.
30 */
31 
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include "linear.h"
35 #include "genC.h"
36 #include "misc.h"
37 #include "ri.h"
38 #include "ri-util.h"
39 
40 
41 /** @defgroup instruction_constructors Instruction constructors
42 
43  @{
44  */
45 
46 /* Build an instruction that call a function entity with an argument list.
47 
48  @param e is the function entity to call
49  @param l is the list of argument expressions given to the function to call
50  */
53 }
54 
55 
56 /* Creates a call instruction to a function with no argument.
57 
58  @param f is the function entity to call
59  */
61  return make_call_instruction(f, NIL);
62 }
63 
64 
65 /* Creates a call instruction to a function with one argument.
66 
67  @param f is the function entity to call
68  @param e is the argument expression given to the function to call
69  */
71  expression e) {
73 }
74 
75 
76 /* Creates a CONTINUE instruction, that is the FORTRAN nop, the ";" in C
77  or the "pass" in Python for example.
78 */
80  entity called_function;
81  called_function = entity_intrinsic(CONTINUE_FUNCTION_NAME);
82  return MakeNullaryCallInst(called_function);
83 }
84 
85 
88  expression r)
89 {
90  call c = call_undefined;
92 
93 /* SG: not true in C
94  * pips_assert("make_assign_statement",
95  syntax_reference_p(expression_syntax(l)));*/
97  CONS(EXPRESSION, l, CONS(EXPRESSION, r, NIL)));
99 
100  return i;
101 }
102 
103 
104 /** Build an instruction block from a list of statements
105  */
107  return make_instruction_sequence(make_sequence(statements));
108 }
109 /** @} */
110 
111 
112 /** @defgroup instructions_p Predicates on instructions
113 
114  @{
115 */
116 
117 
118 /* Test if a call is a native instruction of the language
119 
120  @param c is the call to investigate
121  @param s is the name of the native instruction to investigate
122  @return true if the instruction is a native instruction of the language
123  */
125  string op_name) {
126  bool call_s_p = false;
127 
128  // The called function
129  entity f = call_function(c);
130 
131  if (strcmp(entity_user_name(f), op_name) == 0)
132  call_s_p = true;
133 
134  return call_s_p;
135 }
136 
137 
138 /* Test if an instruction is a native instruction of the language
139 
140  @param i is the instruction to investigate
141  @param s is the name of the native instruction to investigate
142  @return true if the instruction is a native instruction of the language
143  */
145  string op_name)
146 {
147  bool call_s_p = false;
148 
149  // Call can be directly inside the instruction,
150  // or wrapped inside an expression
151  if (instruction_call_p(i)) {
152  call_s_p = native_call_p(instruction_call(i), op_name);
153  } else if(instruction_expression_p(i)) {
155  if(syntax_call_p(s)) {
156  call_s_p = native_call_p(syntax_call(s), op_name);
157  }
158  }
159 
160  return call_s_p;
161 }
162 
163 /* Test if an instruction is an assignment. */
165 {
167 }
168 
169 
170 /* Test if an instruction is a CONTINUE, that is the FORTRAN nop, the ";" in C
171  or the "pass" in Python... according to the language.
172 */
174 {
176 }
177 
178 
179 /* Test if an instruction is a C or Fortran "return"
180 
181  Note that this function is not named "instruction_return_p" since
182  it would mean return is a field of instruction ... which used to be
183  the case :)
184 */
186 {
189 }
190 
192 {
194 }
195 
197 {
199 }
200 
202 {
204 }
206 {
208 }
209 
210 
211 /* Test if an instruction is a Fortran STOP.
212 */
213 bool
216 }
217 
218 
219 /* Test if an instruction is a Fortran FORMAT.
220 */
221 bool
224 }
225 
226 
227 /* Checks if an instruction block is a list of assignments, possibly
228  followed by a continue.
229 */
231 {
232  /* FOREACH cannot be used in this case */
233  MAPL(cs,
234  {
235  statement s = STATEMENT(CAR(cs));
236 
237  if(!assignment_statement_p(s))
238  if(!(continue_statement_p(s) && ENDP(CDR(cs)) ))
239  return false;
240  },
241  instruction_block(i));
242  return true;
243 }
244 
245 
246 /** @} */
247 
248 
249 /* Flatten an instruction block if necessary.
250 
251  Detects sequences of sequences and reorder as one sequence.
252 
253  Some memory leaks. Should not be used. Use the functions from control
254  instead.
255 
256  This function cannot be used for C code as local declarations are
257  discarded without warnings.
258 */
260 {
261  if (instruction_block_p(i))
262  {
263  list ls = NIL;
266  if (instruction_block_p(ib))
267  ls = gen_nconc(ls, instruction_block(ib));
268  else
269  ls = gen_nconc(ls, CONS(STATEMENT, s, NIL));
270  }
272  instruction_block(i) = ls;
273  }
274 }
275 
276 
277 /* Return a constant string representing symbolically the instruction type.
278 
279  Does not work for undefined instructions: core dump.
280 
281  @return a constant string such as "WHILE LOOP" for a "while()" or "do
282  while()" loop and so on.
283 */
285 {
286  string instrstring = NULL;
287 
288  switch (instruction_tag(i))
289  {
290  case is_instruction_loop:
291  instrstring="DO LOOP";
292  break;
294  instrstring="WHILE LOOP";
295  break;
296  case is_instruction_test:
297  instrstring="TEST";
298  break;
299  case is_instruction_goto:
300  instrstring="GOTO";
301  break;
302  case is_instruction_call:
303  {if (instruction_continue_p(i))
304  instrstring="CONTINUE";
305  else if (return_instruction_p(i))
306  instrstring="RETURN";
307  else if (instruction_stop_p(i))
308  instrstring="STOP";
309  else if (instruction_format_p(i))
310  instrstring="FORMAT";
311  else if (instruction_assign_p(i))
312  instrstring="ASSIGN";
313  else {
314  instrstring="CALL";
315  }
316  break;
317  }
319  instrstring="BLOCK";
320  break;
322  instrstring="UNSTRUCTURED";
323  break;
325  instrstring="FOR LOOP";
326  break;
328  instrstring="EXPRESSION";
329  break;
330  default: pips_internal_error("ill. instruction tag %d",
331  instruction_tag(i));
332  }
333 
334  return instrstring;
335 }
336 
338 {
339  string instrstring = string_undefined;
341  instrstring = "UNDEFINED INSTRUCTION";
342  else
343  instrstring = instruction_identification(i);
344  return instrstring;
345 }
346 
347 
call make_call(entity a1, list a2)
Definition: ri.c:269
instruction make_instruction_sequence(sequence _field_)
Definition: ri.c:1169
instruction make_instruction(enum instruction_utype tag, void *val)
Definition: ri.c:1166
sequence make_sequence(list a)
Definition: ri.c:2125
instruction make_instruction_block(list statements)
Build an instruction block from a list of statements.
Definition: instruction.c:106
instruction make_continue_instruction()
Creates a CONTINUE instruction, that is the FORTRAN nop, the ";" in C or the "pass" in Python for exa...
Definition: instruction.c:79
instruction make_assign_instruction(expression l, expression r)
Definition: instruction.c:87
instruction make_call_instruction(entity e, list l)
Build an instruction that call a function entity with an argument list.
Definition: instruction.c:51
instruction MakeUnaryCallInst(entity f, expression e)
Creates a call instruction to a function with one argument.
Definition: instruction.c:70
instruction MakeNullaryCallInst(entity f)
Creates a call instruction to a function with no argument.
Definition: instruction.c:60
bool instruction_assign_p(instruction i)
Test if an instruction is an assignment.
Definition: instruction.c:164
bool instruction_stop_p(instruction i)
Test if an instruction is a Fortran STOP.
Definition: instruction.c:214
bool assignment_block_p(instruction i)
Checks if an instruction block is a list of assignments, possibly followed by a continue.
Definition: instruction.c:230
bool exit_instruction_p(instruction i)
Definition: instruction.c:201
bool abort_instruction_p(instruction i)
Definition: instruction.c:205
bool instruction_format_p(instruction i)
Test if an instruction is a Fortran FORMAT.
Definition: instruction.c:222
bool native_instruction_p(instruction i, string op_name)
Test if an instruction is a native instruction of the language.
Definition: instruction.c:144
bool instruction_continue_p(instruction i)
Test if an instruction is a CONTINUE, that is the FORTRAN nop, the ";" in C or the "pass" in Python....
Definition: instruction.c:173
bool return_instruction_p(instruction i)
Test if an instruction is a C or Fortran "return".
Definition: instruction.c:185
bool C_return_instruction_p(instruction i)
Definition: instruction.c:196
bool native_call_p(call c, string op_name)
Test if a call is a native instruction of the language.
Definition: instruction.c:124
bool fortran_return_instruction_p(instruction i)
Definition: instruction.c:191
#define ENDP(l)
Test if a list is empty.
Definition: newgen_list.h:66
#define NIL
The empty list (nil in Lisp)
Definition: newgen_list.h:47
#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 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 CDR(pcons)
Get the list less its first element.
Definition: newgen_list.h:111
#define MAPL(_map_list_cp, _code, _l)
Apply some code on the addresses of all the elements of a list.
Definition: newgen_list.h:203
bool continue_statement_p(statement)
Test if a statement is a CONTINUE, that is the FORTRAN nop, the ";" in C or the "pass" in Python....
Definition: statement.c:203
bool assignment_statement_p(statement)
Test if a statement is an assignment.
Definition: statement.c:135
string safe_instruction_identification(instruction i)
Definition: instruction.c:337
void flatten_block_if_necessary(instruction i)
Flatten an instruction block if necessary.
Definition: instruction.c:259
string instruction_identification(instruction i)
Return a constant string representing symbolically the instruction type.
Definition: instruction.c:284
#define pips_internal_error
Definition: misc-local.h:149
#define string_undefined
Definition: newgen_types.h:40
int f(int off1, int off2, int n, float r[n], float a[n], float b[n])
Definition: offsets.c:15
#define instruction_block_p(i)
#define RETURN_FUNCTION_NAME
#define ABORT_FUNCTION_NAME
#define C_RETURN_FUNCTION_NAME
#define is_instruction_block
soft block->sequence transition
#define CONTINUE_FUNCTION_NAME
#define STOP_FUNCTION_NAME
#define EXIT_FUNCTION_NAME
#define instruction_block(i)
#define FORMAT_FUNCTION_NAME
#define ASSIGN_OPERATOR_NAME
Definition: ri-util-local.h:95
const char * entity_user_name(entity e)
Since entity_local_name may contain PIPS special characters such as prefixes (label,...
Definition: entity.c:487
entity entity_intrinsic(const char *name)
FI: I do not understand this function name (see next one!).
Definition: entity.c:1292
#define call_function(x)
Definition: ri.h:709
#define syntax_call_p(x)
Definition: ri.h:2734
#define instruction_undefined_p(x)
Definition: ri.h:1455
#define EXPRESSION(x)
EXPRESSION.
Definition: ri.h:1217
#define instruction_undefined
Definition: ri.h:1454
@ is_instruction_goto
Definition: ri.h:1473
@ is_instruction_unstructured
Definition: ri.h:1475
@ is_instruction_whileloop
Definition: ri.h:1472
@ is_instruction_expression
Definition: ri.h:1478
@ is_instruction_test
Definition: ri.h:1470
@ is_instruction_call
Definition: ri.h:1474
@ is_instruction_forloop
Definition: ri.h:1477
@ is_instruction_loop
Definition: ri.h:1471
#define instruction_tag(x)
Definition: ri.h:1511
#define syntax_call(x)
Definition: ri.h:2736
#define instruction_call_p(x)
Definition: ri.h:1527
#define instruction_expression(x)
Definition: ri.h:1541
#define statement_instruction(x)
Definition: ri.h:2458
#define instruction_call(x)
Definition: ri.h:1529
#define call_undefined
Definition: ri.h:685
#define expression_syntax(x)
Definition: ri.h:1247
#define instruction_expression_p(x)
Definition: ri.h:1539
#define STATEMENT(x)
STATEMENT.
Definition: ri.h:2413
The structure used to build lists in NewGen.
Definition: newgen_list.h:41