PIPS
prettyprint.c
Go to the documentation of this file.
1 /*
2 
3  $Id: prettyprint.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 /* package continuation : Be'atrice Creusillet, 1996
28  *
29  * This File contains the functions to prettyprint continuation conditions
30  * of a module (over- and under-approximations.
31  *
32  */
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 
38 #include "linear.h"
39 
40 #include "genC.h"
41 
42 #include "text.h"
43 #include "text-util.h"
44 
45 #include "top-level.h"
46 
47 #include "ri.h"
48 #include "effects.h"
49 #include "ri-util.h"
50 #include "effects-util.h"
51 
52 #include "database.h"
53 #include "pipsdbm.h"
54 #include "resources.h"
55 
56 #include "misc.h"
57 #include "properties.h"
58 
59 #include "prettyprint.h"
60 
61 #include "transformer.h"
62 #include "effects-generic.h"
63 
64 #include "semantics.h"
65 #include "continuation.h"
66 
67 #define PREC_FORESYS_PREFIX "C$PREC"
68 #define TRAN_FORESYS_PREFIX "C$TRAN"
69 
70 static bool is_user_view_p;
72 
73 /* The strange argument type is required by qsort(), deep down in the calls */
74 static int
75 is_inferior_pvarval(Pvecteur * pvarval1, Pvecteur * pvarval2)
76 {
77  /* The constant term is given the highest weight to push constant
78  terms at the end of the constraints and to make those easy
79  to compare. If not, constant 0 will be handled differently from
80  other constants. However, it would be nice to give constant terms
81  the lowest weight to print simple constraints first...
82 
83  Either I define two comparison functions, or I cheat somewhere else.
84  Let's cheat? */
85  int is_equal = 0;
86 
87  if (term_cst(*pvarval1) && !term_cst(*pvarval2))
88  is_equal = 1;
89  else if (term_cst(*pvarval1) && term_cst(*pvarval2))
90  is_equal = 0;
91  else if(term_cst(*pvarval2))
92  is_equal = -1;
93  else
94  is_equal =
95  strcmp(pips_user_value_name((entity) vecteur_var(*pvarval1)),
97 
98 
99  return is_equal;
100 }
101 
102 #define continuation get_comment_continuation()
103 #define append(s) add_to_current_line(crt_line, s, continuation, txt)
104 
105 /* text text_continuation(transformer tran)
106  * input : a transformer representing a transformer or a precondition
107  * output : a text containing commentaries representing the transformer
108  * modifies : nothing.
109  *
110  * Modification: AP, Nov 10th, 1995. Instead of building a (very long)
111  * string, I directly use the transformer to build the prettyprint in text
112  * format. This is to avoid the problem occuring when the buffer used in
113  * transformer[precondition]_to_string() is too small. I also use a static
114  * buffer to build each constraint; we are restricted to constraints of
115  * lengths smaller than the line length.
116  */
117 static text
118 text_continuation(transformer cont, bool is_must)
119 {
120  text txt = make_text(NIL);
121  char crt_line[MAX_LINE_LENGTH];
122 
123  crt_line[0] = '\0';
125  append(" ");
126 
127  if(cont != (transformer) HASH_UNDEFINED_VALUE )
128  {
129  if(cont==transformer_undefined)
130  {
131  append(" CONTINUATION: TRANSFORMER_UNDEFINED");
132  }
133  else
134  {
137 
138  append(is_must? "C-MUST-":"C-MAY-");
139  system_text_format(crt_line, continuation, txt, ps,
141  false);
142  }
143 
144  close_current_line(crt_line, txt,continuation);
145  }
146  /* else an empty text is returned.
147  */
148 
149  return txt;
150 }
151 
152 static text
154  transformer must_cont_t,
155  transformer may_cont_t)
156 {
157 
158  text cont_text = make_text(NIL);
159  bool loose_p = get_bool_property("PRETTYPRINT_LOOSE");
160 
161  if ((must_cont_t ==(transformer) HASH_UNDEFINED_VALUE) &&
162  (may_cont_t ==(transformer) HASH_UNDEFINED_VALUE) )
163  return(cont_text);
164 
165  if (loose_p)
166  {
167  ADD_SENTENCE_TO_TEXT(cont_text,
169  strdup("\n")));
170  }
171 
172  /* First: must continuation conditions */
173  if (must_cont_t !=(transformer) HASH_UNDEFINED_VALUE)
174  {
175  MERGE_TEXTS(cont_text, text_continuation(must_cont_t,true));
176  }
177 
178  /* Then: may continuation conditions */
179  if (may_cont_t !=(transformer) HASH_UNDEFINED_VALUE)
180  {
181  MERGE_TEXTS(cont_text, text_continuation(may_cont_t,false));
182  }
183  if (loose_p)
184  ADD_SENTENCE_TO_TEXT(cont_text,
186  strdup("\n")));
187 
188  return(cont_text);
189 }
190 
191 static text
193  entity module,
194  int margin,
195  statement stat)
196 {
197  pips_assert("true", module==module && margin==margin);
198 
199  transformer must_cont_t, may_cont_t;
200  statement s;
201 
202  s = is_user_view_p?
203  (statement) hash_get(nts, (char *) statement_number(stat)) :
204  stat;
205 
206  if (is_user_view_p)
207  {
208  s = (statement) hash_get(nts, (char *) statement_number(stat));
209  }
210 
211 
212  if (s != (statement) HASH_UNDEFINED_VALUE)
213  {
214  must_cont_t = load_statement_must_continuation(s);
215  may_cont_t = load_statement_may_continuation(s);
216  }
217  else
218  {
219  must_cont_t = (transformer) HASH_UNDEFINED_VALUE;
220  may_cont_t = (transformer) HASH_UNDEFINED_VALUE;
221  }
222 
223  return text_continuation_conditions(must_cont_t, may_cont_t);
224 }
225 
226 
227 static text
228 get_continuation_condition_text(const char* module_name, bool give_code_p)
229 {
230  entity module;
231  statement module_stat, user_stat = statement_undefined;
232  text txt = make_text(NIL);
234 
238  (DBR_CODE, module_name, true));
239  module_stat = get_current_module_statement();
240 
241  /* To set up the hash table to translate value into value names */
243  db_get_memory_resource(DBR_CUMULATED_EFFECTS, module_name, true));
245 
246 
247  if(is_user_view_p)
248  {
249  user_stat = (statement)
250  db_get_memory_resource(DBR_PARSED_CODE, module_name, true);
251 
253  nts = build_number_to_statement(nts, module_stat);
254 
255  ifdebug(1)
256  {
258  }
259  }
260 
261  debug_on("CONTINUATION_DEBUG_LEVEL");
262 
264  db_get_memory_resource(DBR_MUST_CONTINUATION, module_name, true) );
266  db_get_memory_resource(DBR_MAY_CONTINUATION, module_name, true) );
268  db_get_memory_resource(DBR_MUST_SUMMARY_CONTINUATION, module_name, true);
270  db_get_memory_resource(DBR_MAY_SUMMARY_CONTINUATION, module_name, true);
271 
272  /* prepare the prettyprinting */
274  /* summary information first */
277 
278  if (give_code_p)
279  /* then code with regions,
280  * using text_statement_continuation_conditions */
282  is_user_view_p? user_stat : module_stat));
283 
284  debug_off();
285 
286  if(is_user_view_p)
287  {
290  }
291 
293 
300 
301  return txt;
302 }
303 
304 static bool
306 {
307  char *file_name, *file_resource_name;
308  bool success = true;
309 
310  file_name = strdup(concatenate(".cont",
312  ("PRETTYPRINT_UNSTRUCTURED_AS_A_GRAPH") ?
313  GRAPH_FILE_EXT : "",
314  NULL));
315  file_resource_name =
316  get_bool_property("PRETTYPRINT_UNSTRUCTURED_AS_A_GRAPH") ?
317  DBR_GRAPH_PRINTED_FILE :
318  (is_user_view_p ? DBR_PARSED_PRINTED_FILE : DBR_PRINTED_FILE);
319 
320  success =
322  module_name,
323  file_resource_name,
324  file_name,
326 
327  free(file_name);
328  return(success);
329 }
330 
332 {
333  is_user_view_p = false;
335 }
336 
337 
339 {
340  is_user_view_p = true;
342 }
343 
sentence make_sentence(enum sentence_utype tag, void *val)
Definition: text.c:59
text make_text(list a)
Definition: text.c:107
struct _newgen_struct_statement_ * statement
Definition: cloning.h:21
void system_text_format(string line, string prefix, text txt, Psysteme ps, string(*variable_name)(Variable), bool a_la_fortran)
appends ps to line/txt with prefix continuations.
bool print_source_continuation_conditions(const string module_name)
Definition: prettyprint.c:338
static text text_statement_continuation_conditions(entity module, int margin, statement stat)
Definition: prettyprint.c:192
static text text_continuation(transformer cont, bool is_must)
text text_continuation(transformer tran) input : a transformer representing a transformer or a precon...
Definition: prettyprint.c:118
static text get_continuation_condition_text(const char *module_name, bool give_code_p)
Definition: prettyprint.c:228
static bool is_user_view_p
Definition: prettyprint.c:70
static int is_inferior_pvarval(Pvecteur *pvarval1, Pvecteur *pvarval2)
The strange argument type is required by qsort(), deep down in the calls.
Definition: prettyprint.c:75
#define continuation
Definition: prettyprint.c:102
static hash_table nts
Definition: prettyprint.c:71
static text text_continuation_conditions(transformer must_cont_t, transformer may_cont_t)
Definition: prettyprint.c:153
#define append(s)
Definition: prettyprint.c:103
bool print_code_continuation_conditions(const string module_name)
prettyprint.c
Definition: prettyprint.c:331
static bool print_continuation_conditions(const char *module_name)
Definition: prettyprint.c:305
static transformer may_sum_cont_t
Definition: continuation.c:68
static transformer must_sum_cont_t
package continuation : Be'atrice Creusillet, 1996
Definition: continuation.c:67
transformer load_statement_must_continuation(statement)
void set_may_continuation_map(statement_mapping)
transformer load_statement_may_continuation(statement)
void set_must_continuation_map(statement_mapping)
void reset_may_continuation_map(void)
void reset_must_continuation_map(void)
void set_cumulated_rw_effects(statement_effects)
void reset_cumulated_rw_effects(void)
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....
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 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
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_get(const hash_table htp, const void *key)
this function retrieves in the hash table pointed to by htp the couple whose key is equal to key.
Definition: hash.c:449
void hash_table_free(hash_table htp)
this function deletes a hash table that is no longer useful.
Definition: hash.c:327
#define debug_on(env)
Definition: misc-local.h:157
#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
static char * module
Definition: pips.c:74
string get_comment_sentinel()
Start a single line comment.
Definition: misc.c:154
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
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 transformer_undefined
Definition: ri.h:2847
#define transformer_relation(x)
Definition: ri.h:2873
struct _newgen_struct_transformer_ * transformer
Definition: ri.h:431
#define statement_number(x)
Definition: ri.h:2452
#define predicate_system(x)
Definition: ri.h:2069
#define statement_undefined
Definition: ri.h:2419
char * strdup()
void sc_lexicographic_sort(Psysteme sc, int(*compare)(Pvecteur *, Pvecteur *))
Minimize first the lexico-graphic weight of each constraint according to the comparison function "com...
Definition: sc_unaires.c:206
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
#define ifdebug(n)
Definition: sg.c:47
le type des coefficients dans les vecteurs: Value est defini dans le package arithmetique
Definition: vecteur-local.h:89
#define MERGE_TEXTS(r, t)
#define ADD_SENTENCE_TO_TEXT(t, p)
#define MAX_LINE_LENGTH
maximum length of a line when prettyprinting...
void close_current_line(string, text, string)
Definition: util.c:235
@ is_sentence_formatted
Definition: text.h:57
const char * pips_user_value_name(entity)
This function is called many times when the constraints and the system of constraints are sorted usin...
Definition: value.c:815
void free_value_mappings(void)
Normal call to free the mappings.
Definition: value.c:1212
static string file_name
#define vecteur_var(v)
char *(* get_variable_name_t)(Variable)
Definition: vecteur-local.h:62
#define term_cst(varval)