PIPS
prettyprint.c
Go to the documentation of this file.
1 /*
2 
3  $Id: prettyprint.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 /* package generic effects : Beatrice Creusillet 5/97
28  *
29  * File: prettyprint.c
30  * ~~~~~~~~~~~~~~~~~~~
31  *
32  * This File contains the generic functions necessary for the prettyprint of
33  * all types of effects.
34  *
35  */
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdlib.h>
39 
40 #include "linear.h"
41 
42 #include "genC.h"
43 
44 #include "misc.h"
45 
46 #include "text.h"
47 #include "text-util.h"
48 
49 #include "ri.h"
50 #include "ri-util.h"
51 
52 #include "effects.h"
53 #include "effects-util.h"
54 
55 // ??? for GRAPH_FILE_EXT
56 #include "top-level.h"
57 
58 #include "resources.h"
59 #include "pipsdbm.h"
60 #include "properties.h"
61 
62 #include "prettyprint.h"
63 
64 // for free_value_mappings()?
65 // ??? could/should be removed?
66 #include "transformer.h"
67 // for module_to_value_mappings()?
68 // ??? could/should be removed?
69 #include "semantics.h"
70 
71 #include "effects-generic.h"
72 
73 /***************************************************** ACTION INTERPRETATION */
74 
75 static string
76  read_action_interpretation = "read",
77  write_action_interpretation = "write";
78 
79 void set_action_interpretation(string r, string w)
80 {
81  read_action_interpretation = r;
82  write_action_interpretation = w;
83 }
84 
86 {
87  read_action_interpretation = "read";
88  write_action_interpretation = "write";
89 }
90 
91 string action_interpretation(int tag)
92 {
93  return tag==is_action_read ?
94  read_action_interpretation : write_action_interpretation;
95 }
96 
97 /****************************************************************************/
98 
99 static bool is_user_view_p = false;
101 
102 void
104 {
106 }
107 
108 static bool prettyprint_with_attachments_p = false;
109 
110 void
111 set_prettyprint_with_attachments(bool attachments_p)
112 {
113  prettyprint_with_attachments_p = attachments_p;
114 }
115 
116 
117 /****************************************************** PRETTYPRINT STUFFS */
118 
119 typedef struct
120 {
121  string name;
122  bool is_a_summary;
124  generic_text_function get_text;
125  generic_prettyprint_function prettyprint;
127 }
128  prettyprint_stuff, *p_prettyprint_stuff;
129 
130 /* the required prettyprints are stored in the specified list items.
131  * there can be any number of them. They are stored.
132  */
133 static list /* of p_prettyprint_stuff */ lp = NIL;
134 
135 void
137 {
138  gen_map(free, lp);
139  gen_free_list(lp);
140  lp = NIL;
141 }
142 
143 void
145  string resource_name,
146  bool is_a_summary,
147  gen_chunk* res,
151 {
152  p_prettyprint_stuff pps =
153  (p_prettyprint_stuff) malloc(sizeof(prettyprint_stuff));
154 
155  pps->name = resource_name;
156  pps->is_a_summary = is_a_summary;
157  pps->resource = res;
158  pps->get_text = tf;
159  pps->prettyprint = tp;
160  pps->attach = ta;
161 
162  lp = CONS(VOID_STAR, pps, lp); /* hum... */
163 }
164 
165 void
167  string resource_name,
168  bool is_a_summary,
172 {
174  gen_chunk_undefined, tf, tp, ta);
175 }
176 
177 static void
178 load_resources(const char* module_name)
179 {
180  list l;
181  for (l=lp; l; POP(l))
182  {
183  p_prettyprint_stuff pps = (p_prettyprint_stuff) STRING(CAR(l));
184  pps->resource =
185  (gen_chunk*) db_get_memory_resource(pps->name, module_name, true);
186  }
187 }
188 
189 static list
190 load_list(statement_effects m, statement s)
191 {
193  list el = effects_effects(e);
194  pips_assert("Retrieved effects are consistent", effects_consistent_p(e));
195  return el;
196 }
197 
198 /********************************************************************* TEXT */
199 
200 /* returns the text associated to a specified prettyprint and statement
201  */
202 static text
203 resource_text(
204  entity module __attribute__ ((__unused__)),
205  int margin __attribute__ ((__unused__)),
206  statement stat,
207  p_prettyprint_stuff pps)
208 {
209  list l_eff = NIL;
210  text l_eff_text;
211 
212  pips_assert("must not be a summary", !pps->is_a_summary);
213 
214  if (is_user_view_p)
215  {
216  statement i;
217 
220  {
221  l_eff = load_list((statement_effects) pps->resource, i);
222  }
223  else
224  l_eff = (list) HASH_UNDEFINED_VALUE;
225  }
226  else
227  {
228  l_eff = load_list((statement_effects) pps->resource, stat);
229  ifdebug(1)
230  {
231  if (l_eff != (list) HASH_UNDEFINED_VALUE &&
232  l_eff != list_undefined)
233  {
234  pips_debug(1, "current effects:\n");
235  (*(pps->prettyprint))(l_eff);
236  }
237  }
238  }
239 
240  l_eff_text = (*(pps->get_text))(l_eff);
241 
242  /* (*attach_effects_decoration_to_text_func)(the_effect_text); */
243 
244  return l_eff_text;
245 }
246 
247 /* returns the text of all required summaries
248  */
249 static text
250 text_summary_any_effect_type(
251  entity module __attribute__ ((__unused__)))
252 {
253  text result = make_text(NIL);
254  list l;
255  for (l=lp; l; POP(l))
256  {
257  p_prettyprint_stuff pps = (p_prettyprint_stuff) STRING(CAR(l));
258  if (pps->is_a_summary) {
259  pips_debug(5, "considering resource %s\n", pps->name);
260  MERGE_TEXTS(result, (*(pps->get_text))
261  (effects_effects( (effects) pps->resource)));
262  }
263  }
264 
265  return result;
266 }
267 
268 /* returns the text of all required effects associated to statement stat
269  */
270 static text
271 text_statement_any_effect_type(
272  entity module,
273  int margin,
274  statement stat)
275 {
276  text result = make_text(NIL);
277  list l;
278  for (l=lp; l; POP(l))
279  {
280  p_prettyprint_stuff pps = (p_prettyprint_stuff) STRING(CAR(l));
281  if (!pps->is_a_summary) {
282  pips_debug(5, "considering resource %s\n", pps->name);
283  MERGE_TEXTS(result, resource_text(module, margin, stat, pps));
284  }
285  }
286 
287  return result;
288 }
289 
290 static text
291 get_any_effects_text(
292  const char* module_name,
293  bool give_code_p, bool use_values)
294 {
295  entity module;
296  statement module_stat, user_stat = statement_undefined;
297  text txt = make_text(NIL);
298  string pp;
299 
300  /* current entity
301  */
304 
305  /* current statement
306  */
308  (DBR_CODE, module_name, true));
309  module_stat = get_current_module_statement();
310 
311  /* resources to be prettyprinted...
312  */
313  load_resources(module_name);
314 
315  if (use_values)
316  {
317  /* To set up the hash table to translate value into value names */
320  (DBR_CUMULATED_EFFECTS, module_name, true));
323  (DBR_PROPER_EFFECTS, module_name, true));
325  }
326 
327  /* Since we want to prettyprint with a sequential syntax, save the
328  PRETTYPRINT_PARALLEL property that may select the parallel output
329  style before overriding it: */
330  pp = strdup(get_string_property("PRETTYPRINT_PARALLEL"));
331  /* Select the default prettyprint style for sequential prettyprint: */
332  set_string_property("PRETTYPRINT_PARALLEL",
333  get_string_property("PRETTYPRINT_SEQUENTIAL_STYLE"));
334 
335 
336 
337  debug_on("EFFECTS_DEBUG_LEVEL");
338 
339  if(is_user_view_p)
340  {
341  user_stat = (statement)
342  db_get_memory_resource(DBR_PARSED_CODE, module_name, true);
343 
345  nts = build_number_to_statement(nts, module_stat);
346 
347  ifdebug(1)
349  }
350 
351  /* prepare the prettyprinting */
352  init_prettyprint(text_statement_any_effect_type);
353 
354  /* summary regions first */
355  MERGE_TEXTS(txt, text_summary_any_effect_type(module));
356 
357  if (give_code_p)
358  /* then code with effects, using text_statement_any_effect_type */
360  is_user_view_p? user_stat : module_stat));
361 
363  {
366  }
367 
369 
370  debug_off();
371 
372 
373  /* Restore the previous PRETTYPRINT_PARALLEL property for the next
374  parallel code prettyprint: */
375  set_string_property("PRETTYPRINT_PARALLEL", pp);
376  free(pp);
377 
378  if (use_values)
379  {
383 
384  }
385 
388 
389  return txt;
390 }
391 
392 /* generic engine for prettyprinting effects.
393  */
394 bool
396  const char* module_name,
397  string file_suffix,
398  bool use_values)
399 {
400  char *file_name, *file_resource_name;
401  bool success = true;
403 
404  /* Set the prettyprint language */
405  value mv = entity_initial(e_module);
406  if(value_code_p(mv)) {
407  code c = value_code(mv);
409  } else {
410  /* Should never arise */
412  }
413 
414  file_name =
415  strdup(concatenate(file_suffix,
417  ("PRETTYPRINT_UNSTRUCTURED_AS_A_GRAPH") ?
418  GRAPH_FILE_EXT : "",
419 
420  /* To exploit the language sensitive prettyprint ability of the display */
421  c_module_p(e_module)? ".c" : ".f",
422  NULL));
423  file_resource_name =
424  get_bool_property("PRETTYPRINT_UNSTRUCTURED_AS_A_GRAPH") ?
425  DBR_GRAPH_PRINTED_FILE :
426  (is_user_view_p ? DBR_PARSED_PRINTED_FILE : DBR_PRINTED_FILE);
427 
428  if (prettyprint_with_attachments_p)
430 
432  (module_name,
433  file_resource_name,
434  file_name,
435  get_any_effects_text(module_name, true, use_values));
436 
437  if (prettyprint_with_attachments_p)
439 
440  return success;
441 }
442 
443 
444 
445 
446 
447 /************************************************************ INTERFACES */
448 
449 static void
450 push_prettyprints(
451  string resource_name,
452  string summary_resource_name)
453 {
454 
457  false,
461 
462  if (!string_undefined_p(summary_resource_name))
463  add_a_generic_prettyprint(summary_resource_name,
464  true,
468 }
469 
470 /* get the text
471  */
472 text
474  const char* module_name,
475  string resource_name,
476  string summary_resource_name,
477  bool give_code_p)
478 {
479  text txt;
480  push_prettyprints(resource_name, summary_resource_name);
481  txt = get_any_effects_text(module_name, give_code_p, false);
483  return txt;
484 }
485 
486 /* initial engine
487  */
488 bool
490  const char* module_name,
491  string resource_name,
492  string summary_resource_name,
493  string file_suffix,
494  bool use_values)
495 {
496  bool ok;
497  push_prettyprints(resource_name, summary_resource_name);
498  ok = print_source_or_code_effects_engine(module_name, file_suffix, use_values);
500  return ok;
501 }
502 
503 
504 void generic_print_effects( list pc)
505 {
506  // remove consistency check which creates a generic -> convex dependency
507  /*
508  // Well that should not be done this way BC
509  if (effect_consistent_p_func == region_consistent_p &&
510  effects_reference_sharing_p(pc, false)) {
511  pips_internal_error("A list of regions share some references");
512  }
513  */
514 
515  if (pc != NIL) {
516  FOREACH(EFFECT, e, pc) {
517  if(store_effect_p(e))
518  (*effect_consistent_p_func)(e);
519  (*effect_prettyprint_func)(e);
520  }
521  }
522  else
523  fprintf(stderr, "\t<NONE>\n");
524 }
525 
bool effects_consistent_p(effects p)
Definition: effects.c:541
effects apply_statement_effects(statement_effects f, statement k)
Definition: effects.c:1007
text make_text(list a)
Definition: text.c:107
void begin_attachment_prettyprint(void)
The translation functions between unique names and objects:
void end_attachment_prettyprint(void)
Clean the things related with the attachment of properties:
struct _newgen_struct_statement_ * statement
Definition: cloning.h:21
static bool is_user_view_p
Definition: prettyprint.c:62
static hash_table nts
Definition: prettyprint.c:63
#define resource_name(x)
Definition: database.h:108
struct _newgen_struct_resource_ * resource
Definition: database.h:23
void(* generic_prettyprint_function)(list)
text(* generic_text_function)(list)
prettyprint function types:
void(* generic_attachment_function)(text)
void set_is_user_view_p(bool)
string action_interpretation(int tag)
void reset_action_interpretation(void)
void add_a_generic_prettyprint(string, bool, generic_text_function, generic_prettyprint_function, generic_attachment_function)
void(* attach_effects_decoration_to_text_func)(text)
bool print_source_or_code_effects_engine(const char *, string, bool)
text get_any_effect_type_text(const char *, string, string, bool)
void reset_proper_rw_effects(void)
void set_proper_rw_effects(statement_effects)
text(* effects_to_text_func)(list)
void set_cumulated_rw_effects(statement_effects)
void set_action_interpretation(string, string)
prettyprint.c
void reset_generic_prettyprints(void)
void set_prettyprint_with_attachments(bool)
void generic_print_effects(list)
void(* effects_prettyprint_func)(list)
void set_a_generic_prettyprint(string, bool, gen_chunk *, generic_text_function, generic_prettyprint_function, generic_attachment_function)
bool print_source_or_code_with_any_effects_engine(const char *, string, string, string, bool)
void reset_cumulated_rw_effects(void)
bool store_effect_p(effect)
Definition: effects.c:1062
#define effects_effects(x)
Definition: effects.h:710
@ is_action_read
Definition: effects.h:292
#define EFFECT(x)
EFFECT.
Definition: effects.h:608
const char * module_name(const char *s)
Return the module part of an entity name.
Definition: entity_names.c:296
char * get_string_property(const char *)
bool get_bool_property(const string)
FC 2015-07-20: yuk, moved out to prevent an include cycle dependency include "properties....
#define STRING(x)
Definition: genC.h:87
#define gen_chunk_undefined
Definition: genC.h:74
#define VOID_STAR(x)
Definition: genC.h:95
if(!(yy_init))
Definition: genread_lex.c:1029
void * malloc(YYSIZE_T)
void free(void *)
bool success
Definition: gpips-local.h:59
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
statement get_current_module_statement(void)
Get the current module statement.
Definition: static.c:208
entity set_current_module_entity(entity)
static.c
Definition: static.c:66
entity get_current_module_entity(void)
Get the entity of the current module.
Definition: static.c:85
#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
void gen_map(gen_iter_func_t fp, const list l)
Definition: list.c:172
#define CONS(_t_, _i_, _l_)
List element cell constructor (insert an element at the beginning of a list)
Definition: newgen_list.h:150
#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 list_undefined
Undefined list definition :-)
Definition: newgen_list.h:69
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
statement apply_number_to_statement(hash_table, _int)
Definition: statement.c:1495
hash_table allocate_number_to_statement(void)
Definition: statement.c:1540
hash_table build_number_to_statement(hash_table, statement)
Definition: statement.c:1516
void hash_table_free(hash_table htp)
this function deletes a hash table that is no longer useful.
Definition: hash.c:327
void set_prettyprint_language_from_property(enum language_utype native)
set the prettyprint language according to the property PRETTYPRINT_LANGUAGE @description If the prope...
Definition: language.c:103
#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
string concatenate(const char *,...)
Return the concatenation of the given strings.
Definition: string.c:183
#define HASH_UNDEFINED_VALUE
value returned by hash_get() when the key is not found; could also be called HASH_KEY_NOT_FOUND,...
Definition: newgen_hash.h:56
#define hash_table_undefined
Value of an undefined hash_table.
Definition: newgen_hash.h:49
int tag
TAG.
Definition: newgen_types.h:92
#define string_undefined_p(s)
Definition: newgen_types.h:41
struct cons * list
Definition: newgen_types.h:106
static char * module
Definition: pips.c:74
void close_prettyprint()
because some prettyprint functions may be used for debug, so the last hook set by somebody may have s...
Definition: misc.c:242
void init_prettyprint(text(*hook)(entity, int, statement))
checks that the prettyprint hook was actually reset...
Definition: misc.c:231
text text_module(entity, statement)
bool make_text_resource_and_free(const char *, const char *, const char *, text)
Definition: print.c:82
void print_number_to_statement(hash_table)
Definition: statement.c:209
bool user_view_p(void)
print_code or print_source
Definition: print.c:95
void set_string_property(const char *, const char *)
bool c_module_p(entity m)
Test if a module "m" is written in C.
Definition: entity.c:2777
entity module_name_to_entity(const char *mn)
This is an alias for local_name_to_top_level_entity.
Definition: entity.c:1479
#define value_code_p(x)
Definition: ri.h:3065
#define value_code(x)
Definition: ri.h:3067
#define statement_undefined_p(x)
Definition: ri.h:2420
#define statement_number(x)
Definition: ri.h:2452
#define code_language(x)
Definition: ri.h:792
#define language_tag(x)
Definition: ri.h:1590
@ is_language_fortran
Definition: ri.h:1566
#define statement_undefined
Definition: ri.h:2419
#define entity_initial(x)
Definition: ri.h:2796
int fprintf()
test sc_min : ce test s'appelle par : programme fichier1.data fichier2.data ...
char * strdup()
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
static bool __attribute__((unused))
Definition: prettyprint.c:435
#define ifdebug(n)
Definition: sg.c:47
static bool ok
The structure used to build lists in NewGen.
Definition: newgen_list.h:41
#define MERGE_TEXTS(r, t)
void free_value_mappings(void)
Normal call to free the mappings.
Definition: value.c:1212
A gen_chunk is used to store every object.
Definition: genC.h:58
static string file_name