PIPS
spaghettify.c
Go to the documentation of this file.
1 /*
2 
3  $Id: spaghettify.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 /**
28  * The spaghettifier is used in context of PHRASE project while
29  * creating "Finite State Machine"-like code portions in order to synthetise
30  * them in reconfigurables units.
31  *
32  * This phase transforms structured code portions (eg. loops) in
33  * unstructured statements.
34  *
35  * To add flexibility, the behavior of \texttt{spaghettifier} is
36  * controlled by the properties
37  * - DESTRUCTURE_TESTS
38  * - DESTRUCTURE_LOOPS
39  * - DESTRUCTURE_WHILELOOPS
40  * - DESTRUCTURE_FORLOOPS
41  * to allow more or less destruction power !
42  *
43  * spaghettify > MODULE.code
44  * < PROGRAM.entities
45  * < MODULE.code
46  *
47  */
48 
49 #include <stdio.h>
50 #include <ctype.h>
51 
52 #include "genC.h"
53 #include "linear.h"
54 #include "ri.h"
55 #include "effects.h"
56 
57 #include "resources.h"
58 
59 #include "misc.h"
60 #include "ri-util.h"
61 #include "prettyprint.h"
62 #include "effects-util.h"
63 #include "pipsdbm.h"
64 
65 #include "text-util.h"
66 
67 #include "dg.h"
68 #include "properties.h"
69 
70 #include "control.h"
71 #include "callgraph.h"
72 #include "spaghettify.h"
73 #include "phrase_tools.h"
74 
75 /*
76  * This function is recursively called during spaghettization.
77  * It takes the statement stat as parameter and return a new
78  * spaghettized statement (or the same if nothing has been done).
79  * Spaguettization is done:
80  * - on Tests (if property DESTRUCTURE_TESTS set to true)
81  * - on Loops (if property DESTRUCTURE_LOOPS set to true)
82  * - on WhileLoops (if property DESTRUCTURE_WHILELOOPS set to true)
83  * - on ForLoops (if property DESTRUCTURE_FORLOOPS set to true)
84  */
86 {
87  // Defaut behaviour is to return parameter statement stat
88  statement returned_statement = stat;
90 
91  pips_debug(2,"\nSPAGHETTIFY: Module statement: =====================================\n");
92  ifdebug(2) {
93  print_statement(stat);
94  }
95  pips_debug(2,"domain number = %"PRIdPTR"\n", statement_domain_number(stat));
96  pips_debug(2,"entity = UNDEFINED\n");
97  pips_debug(2,"statement number = %"PRIdPTR"\n", statement_number(stat));
98  pips_debug(2,"statement ordering = %"PRIdPTR"\n", statement_ordering(stat));
100  pips_debug(2,"statement comments = EMPTY\n");
101  }
102  else {
103  pips_debug(2,"statement comments = %s\n", statement_comments(stat));
104  }
105  pips_debug(2,"statement instruction = %s\n", statement_type_as_string(stat));
106  switch (instruction_tag(i)) {
107  case is_instruction_test:
108  {
109  pips_debug(2, "TEST\n");
110  if (get_bool_property("DESTRUCTURE_TESTS")) {
111  returned_statement = spaghettify_test (stat, module_name);
112  }
113  break;
114  }
116  {
118  pips_debug(2, "SEQUENCE\n");
119  MAP(STATEMENT, current_stat,
120  {
121  statement new_stat = spaghettify_statement(current_stat, module_name);
122  if (new_stat != NULL) {
123  gen_list_patch (sequence_statements(seq), current_stat, new_stat);
124  }
125  }, sequence_statements(seq));
126  break;
127  }
128  case is_instruction_loop: {
129  pips_debug(2, "LOOP\n");
130  if (get_bool_property("DESTRUCTURE_LOOPS")) {
131  returned_statement = spaghettify_loop (stat, module_name);
132  }
133  break;
134  }
136  pips_debug(2, "WHILELOOP\n");
137  if (get_bool_property("DESTRUCTURE_WHILELOOPS")) {
138  returned_statement = spaghettify_whileloop (stat, module_name);
139  }
140  break;
141  }
142  case is_instruction_forloop: {
143  pips_debug(2, "FORLOOP\n");
144  if (get_bool_property("DESTRUCTURE_FORLOOPS")) {
145  returned_statement = spaghettify_forloop (stat, module_name);
146  }
147  break;
148  }
149  case is_instruction_call: {
150  pips_debug(2, "CALL\n");
151  break;
152  }
154  pips_debug(2, "UNSTRUCTURED\n");
155  break;
156  }
157  case is_instruction_goto: {
158  pips_debug(2, "GOTO\n");
159  break;
160  }
161  default:
162  pips_debug(2, "UNDEFINED\n");
163  break;
164  }
165 
166  return returned_statement;
167 }
168 
169 /*********************************************************
170  * Phase main
171  *********************************************************/
172 
173 bool spaghettify(const char* module_name)
174 {
175  /* get the resources */
176  statement stat = (statement) db_get_memory_resource(DBR_CODE,
177  module_name,
178  true);
179 
182 
183  debug_on("SPAGUETTIFY_DEBUG_LEVEL");
184 
185  /* Now do the job */
186  stat = spaghettify_statement(stat,module_name);
187 
188  pips_assert("Statement is consistent after SPAGUETTIFY",
189  statement_consistent_p(stat));
190 
191  /* Reorder the module, because new statements have been added */
192  module_reorder(stat);
193  DB_PUT_MEMORY_RESOURCE(DBR_CODE, module_name, stat);
194  DB_PUT_MEMORY_RESOURCE(DBR_CALLEES, module_name,
195  compute_callees(stat));
196 
197  /* update/release resources */
200 
201  debug_off();
202 
203  return true;
204 }
bool statement_consistent_p(statement p)
Definition: ri.c:2195
callees compute_callees(const statement stat)
Recompute the callees of a module statement.
Definition: callgraph.c:355
struct _newgen_struct_statement_ * statement
Definition: cloning.h:21
const char * module_name(const char *s)
Return the module part of an entity name.
Definition: entity_names.c:296
bool get_bool_property(const string)
FC 2015-07-20: yuk, moved out to prevent an include cycle dependency include "properties....
statement spaghettify_forloop(statement stat, const char *module_name)
This function takes the statement stat as parameter and return a new spaghettized statement,...
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_list_patch(list l, const void *x, const void *y)
Replace all the reference to x in list l by a reference to y:
Definition: list.c:985
#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
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_MEMORY_RESOURCE(res_name, own_name, res_val)
conform to old interface.
Definition: pipsdbm-local.h:66
bool statement_with_empty_comment_p(statement)
Return true if the statement has an empty statement:
Definition: statement.c:126
statement spaghettify_loop(statement stat, const char *module_name)
This function takes the statement stat as parameter and return a new spaghettized statement,...
#define debug_on(env)
Definition: misc-local.h:157
#define pips_debug
these macros use the GNU extensions that allow variadic macros, including with an empty list.
Definition: misc-local.h:145
#define pips_assert(what, predicate)
common macros, two flavors depending on NDEBUG
Definition: misc-local.h:172
#define debug_off()
Definition: misc-local.h:160
statement spaghettify_test(statement, const char *)
test_spaghettify.c
string statement_type_as_string(statement)
phrase_tools.c
Definition: phrase_tools.c:65
statement spaghettify_whileloop(statement, const char *)
whileloop_spaghettify.c
void print_statement(statement)
Print a statement on stderr.
Definition: statement.c:98
bool module_reorder(statement body)
Reorder a module and recompute order to statement if any.
Definition: reorder.c:244
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
#define statement_ordering(x)
Definition: ri.h:2454
@ 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 statement_domain_number(x)
Definition: ri.h:2448
#define sequence_statements(x)
Definition: ri.h:2360
#define instruction_sequence(x)
Definition: ri.h:1514
#define statement_instruction(x)
Definition: ri.h:2458
#define statement_comments(x)
Definition: ri.h:2456
#define statement_number(x)
Definition: ri.h:2452
#define STATEMENT(x)
STATEMENT.
Definition: ri.h:2413
#define ifdebug(n)
Definition: sg.c:47
bool spaghettify(const char *module_name)
Definition: spaghettify.c:173
statement spaghettify_statement(statement stat, const char *module_name)
The spaghettifier is used in context of PHRASE project while creating "Finite State Machine"-like cod...
Definition: spaghettify.c:85