PIPS
initial.c
Go to the documentation of this file.
1 /*
2 
3  $Id: initial.c 23412 2017-08-09 15:07:09Z irigoin $
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  * Computation of initial transformers that allow to collect
29  * global initializations of BLOCK DATA and so.
30  *
31  * This computation is not safe because pipsmake does not provide a real
32  * link-edit. The set of modules called $ALL may contain modules which
33  * are never called by the main.
34  *
35  * The initial precondition of each module must be filtered with respect
36  * to the effects of the MAIN in order to eliminate information about
37  * unused variables.
38  *
39  * The filtering cannot be performed at the module level because some
40  * modules such as BLOCKDATA do not have any proper effects and because
41  * a module can initialize a variable in a common without using it.
42  *
43  */
44 
45 #include <stdio.h>
46 #include <string.h>
47 
48 #include "genC.h"
49 
50 #include "linear.h"
51 #include "ri.h"
52 #include "effects.h"
53 #include "ri-util.h"
54 #include "effects-util.h"
55 
56 #include "database.h"
57 #include "pipsdbm.h"
58 #include "resources.h"
59 
60 #include "misc.h"
61 
62 #include "effects-generic.h"
63 #include "effects-simple.h"
64 
65 #include "prettyprint.h"
66 #include "transformer.h"
67 
68 #include "semantics.h"
69 
70 
71 /******************************************************** PIPSMAKE INTERFACE */
72 
73 /* Compute an initial transformer.
74  */
75 bool initial_precondition(string name)
76 {
78  transformer prec;
79 
80  debug_on("SEMANTICS_DEBUG_LEVEL");
81 
84  db_get_memory_resource(DBR_CODE, name, true));
87  db_get_memory_resource(DBR_CUMULATED_EFFECTS, name, true));
89  db_get_memory_resource(DBR_PROPER_EFFECTS, name, true));
91 
93 
94  ifdebug(1)
95  pips_assert("consistent initial precondition before filtering",
97 
98  /* Filter out local and unused variables from the local precondition?
99  * No, because it is not possible to guess what is used or unused at
100  * the program level. Filtering is postponed to program_precondition().
101  */
102 
103  DB_PUT_MEMORY_RESOURCE(DBR_INITIAL_PRECONDITION, strdup(name), (char*) prec);
104 
110 
112 
113  debug_off();
114  return true;
115 }
116 
117 /* returns t1 inter= t2;
118  */
119 static void
121  transformer t1,
122  transformer t2)
123 {
127 }
128 
129 #define pred_debug(level, msg, trans) \
130  ifdebug(level) { pips_debug(level, msg); dump_transformer(trans);}
131 
132 /* Compute the union of all initial preconditions.
133  */
134 bool program_precondition(const string name)
135 {
137 
138  /* A small function that would require pipsdbm and ri-util*/
139  const string mn = get_main_entity_name();
140  entity the_main = module_name_to_entity(mn);
141 
142  pips_assert("main was found", the_main!=entity_undefined);
143 
144  free(mn);
145 
146  int i, nmodules;
147  gen_array_t modules;
148  list e_inter = NIL;
149 
150  debug_on("SEMANTICS_DEBUG_LEVEL");
151  pips_debug(1, "considering program \"%s\" with main \"%s\"\n", name,
152  module_local_name(the_main));
153 
154  set_current_module_entity(the_main);
156  db_get_memory_resource(DBR_CODE,
157  module_local_name(the_main),
158  true));
161  db_get_memory_resource(DBR_CUMULATED_EFFECTS,
162  module_local_name(the_main),
163  true));
165  db_get_memory_resource(DBR_PROPER_EFFECTS,
166  module_local_name(the_main),
167  true));
168 
169  /* e_inter = effects_to_list(get_cumulated_rw_effects(get_current_module_statement())); */
170 
171  e_inter = effects_to_list( (effects)
172  db_get_memory_resource(DBR_SUMMARY_EFFECTS,
173  module_local_name(the_main),
174  true));
175 
176  module_to_value_mappings(the_main);
177 
178  /* Unavoidable pitfall: initializations in uncalled modules may be
179  * taken into account. It all depends on the "create" command.
180  */
181  modules = db_get_module_list();
182  nmodules = gen_array_nitems(modules);
183  pips_assert("some modules in the program", nmodules>0);
184 
185  for(i=0; i<nmodules; i++) {
186  transformer tm;
187  string mname = gen_array_item(modules, i);
188  pips_debug(1, "considering module %s\n", mname);
189 
190  tm = transformer_dup((transformer) /* no dup & false => core */
191  db_get_memory_resource(DBR_INITIAL_PRECONDITION,
192  mname, true));
193 
194  pred_debug(3, "current: t =\n", t);
195  pred_debug(2, "to be added: tm =\n", tm);
196  translate_global_values(the_main, tm); /* modifies tm! */
197  pred_debug(3, "to be added after translation:\n", tm);
198  tm = transformer_intra_to_inter(tm, e_inter);
199  /* tm = transformer_normalize(tm, 2); */
200  if(!transformer_consistency_p(tm)) {
201  (void) print_transformer(tm);
202  pips_internal_error("Non-consistent filtered initial precondition");
203  }
204  pred_debug(3, "to be added after filtering:\n", tm);
205 
206  intersect(t, tm);
207  free_transformer(tm);
208  }
209 
210  pred_debug(1, "resulting program precondition:\n", t);
211 
212  ifdebug(1)
213  pips_assert("consistent program precondition",
215 
216  DB_PUT_MEMORY_RESOURCE(DBR_PROGRAM_PRECONDITION, "", t);
217 
223 
225  gen_array_full_free(modules);
226 
227  debug_off();
228  return true;
229 }
230 
231 /* The program correctness postcondition cannot be infered. It should be
232  provided by the user. */
233 bool program_postcondition(const string name)
234 {
236 
237  /* A small function that would require pipsdbm and ri-util*/
238  const string mn = get_main_entity_name();
239  entity the_main = module_name_to_entity(mn);
240  free(mn);
241 
242  debug_on("SEMANTICS_DEBUG_LEVEL");
243  pips_debug(1, "considering program \"%s\" with main \"%s\"\n", name,
244  module_local_name(the_main));
245 
246  pred_debug(1, "assumed program postcondition:\n", post);
247 
248  ifdebug(1)
249  pips_assert("consistent program postcondition",
251 
252  DB_PUT_MEMORY_RESOURCE(DBR_PROGRAM_POSTCONDITION, "", post);
253 
254  debug_off();
255  return true;
256 }
257 
258 
259 /*********************************************************** PRETTY PRINTERS */
260 
261 bool print_initial_precondition(const string name)
262 {
263  bool ok;
265  transformer t = (transformer)
266  db_get_memory_resource(DBR_INITIAL_PRECONDITION, name, true);
267 
268  debug_on("SEMANTICS_DEBUG_LEVEL");
269 
272  db_get_memory_resource(DBR_CODE, name, true));
274  db_get_memory_resource(DBR_CUMULATED_EFFECTS,
275  name,
276  true));
278 
279  ok = make_text_resource_and_free(name, DBR_PRINTED_FILE, ".ipred",
280  text_transformer(t));
281 
285 
287 
288  debug_off();
289 
290  return ok;
291 }
292 
293 bool print_program_precondition(const string name)
294 {
295  bool ok;
296  transformer t = (transformer)
297  db_get_memory_resource(DBR_PROGRAM_PRECONDITION, "", true);
298 
299  /* A small function that would require pipsdbm and ri-util*/
300  const string mn = get_main_entity_name();
302  free(mn);
303 
304  debug_on("SEMANTICS_DEBUG_LEVEL");
305  pips_debug(1, "for \"%s\"\n", name);
306 
309  db_get_memory_resource(DBR_CODE,
311  true));
313  db_get_memory_resource(DBR_CUMULATED_EFFECTS,
315  true));
317 
318  ok = make_text_resource_and_free(name, DBR_PRINTED_FILE, ".pipred",
319  text_transformer(t));
320 
324 
326 
327  debug_off();
328 
329  return ok;
330 }
331 
void free_transformer(transformer p)
Definition: ri.c:2616
size_t gen_array_nitems(const gen_array_t a)
Definition: array.c:131
void gen_array_full_free(gen_array_t a)
Definition: array.c:77
void * gen_array_item(const gen_array_t a, size_t i)
Definition: array.c:143
transformer transformer_dup(transformer t_in)
transformer package - basic routines
Definition: basic.c:49
transformer transformer_identity()
Allocate an identity transformer.
Definition: basic.c:110
bool transformer_consistency_p(transformer t)
FI: I do not know if this procedure should always return or fail when an inconsistency is found.
Definition: basic.c:612
void reset_proper_rw_effects(void)
void set_proper_rw_effects(statement_effects)
void set_cumulated_rw_effects(statement_effects)
void reset_cumulated_rw_effects(void)
list effects_to_list(effects)
Definition: effects.c:209
void free(void *)
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
#define NIL
The empty list (nil in Lisp)
Definition: newgen_list.h:47
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
gen_array_t db_get_module_list(void)
Get an array of all the modules (functions, procedures and compilation units) of a workspace.
Definition: database.c:1266
#define DB_PUT_MEMORY_RESOURCE(res_name, own_name, res_val)
conform to old interface.
Definition: pipsdbm-local.h:66
bool program_precondition(const string name)
Compute the union of all initial preconditions.
Definition: initial.c:134
bool initial_precondition(string name)
Compute an initial transformer.
Definition: initial.c:75
#define pred_debug(level, msg, trans)
Definition: initial.c:129
bool program_postcondition(const string name)
The program correctness postcondition cannot be infered.
Definition: initial.c:233
static void intersect(transformer t1, transformer t2)
returns t1 inter= t2;
Definition: initial.c:120
bool print_initial_precondition(const string name)
Definition: initial.c:261
bool print_program_precondition(const string name)
Definition: initial.c:293
#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 pips_internal_error
Definition: misc-local.h:149
#define debug_off()
Definition: misc-local.h:160
static char * module
Definition: pips.c:74
string get_main_entity_name(void)
Return the local name of the main module if it is available, or the local name of any module by defau...
Definition: util.c:63
#define print_transformer(t)
Definition: print.c:357
bool make_text_resource_and_free(const char *, const char *, const char *, text)
Definition: print.c:82
entity module_name_to_entity(const char *mn)
This is an alias for local_name_to_top_level_entity.
Definition: entity.c:1479
const char * module_local_name(entity e)
Returns the module local user name.
Definition: entity.c:582
void free_statement_global_stack(void)
Definition: static.c:358
void make_statement_global_stack(void)
Definition: static.c:318
#define entity_undefined
Definition: ri.h:2761
#define transformer_relation(x)
Definition: ri.h:2873
struct _newgen_struct_transformer_ * transformer
Definition: ri.h:431
#define predicate_system(x)
Definition: ri.h:2069
transformer all_data_to_precondition(entity m)
any variable is included.
transformer transformer_intra_to_inter(transformer tf, list le)
Psysteme sc_append(Psysteme s1, Psysteme s2)
Psysteme sc_append(Psysteme s1, Psysteme s2): calcul de l'intersection des polyedres definis par s1 e...
char * strdup()
void translate_global_values(entity m, transformer tf)
void module_to_value_mappings(entity m)
void module_to_value_mappings(entity m): build hash tables between variables and values (old,...
Definition: mappings.c:624
text text_transformer(transformer tran)
text text_transformer(transformer tran) input : a transformer representing a transformer or a precond...
Definition: prettyprint.c:464
#define ifdebug(n)
Definition: sg.c:47
static bool ok
The structure used to build lists in NewGen.
Definition: newgen_list.h:41
void free_value_mappings(void)
Normal call to free the mappings.
Definition: value.c:1212