PIPS
externals.c
Go to the documentation of this file.
1 /*
2 
3  $Id: externals.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  * Some functions to manage special (non newgen) resources.
29  */
30 
31 #include "private.h"
32 #include "paf_ri.h" // static control stuff
33 #include "ri-util.h" // statement ordering stuff
34 
35 /********************************************************************* UTILS */
36 
37 /* reads an int while sharing file and buffers with newgen...
38  */
39 static int
40 lire_int(FILE * fd)
41 {
42  int c, i = 0, sign = 1;
43  genread_in = fd;
44 
45  while (isspace(c = genread_input())) ; /* trim */
46 
47  if (c == '-') sign = -1;
48  else if (isdigit(c))
49  i = c-'0';
50  else if(c==EOF)
51  pips_internal_error("Unexpected EOF, corrupted resource file");
52  else
53  pips_internal_error("digit or '-' expected : %c %x", c, c);
54 
55  while (isdigit(c = genread_input()))
56  i = 10*i + (c-'0');
57 
58  return sign*i;
59 }
60 
61 /*********************************************************** ENTITY WRAPPERS */
62 
63 char *
65 {
66  int read = gen_read_tabulated(fd, false);
67  pips_assert("entities were read", read==entity_domain);
68  return (char *) entity_domain;
69 }
70 
71 void
73 {
74  pips_assert("argument not used", p==p);
76 }
77 
78 /**************************************************** OLD STATEMENT MAPPING */
79 
80 /* The old statement_mapping is an hash_table managed directly.
81  * this table associates statement to any newgen type.
82  * Now explicit newgen functions ("->") should be prefered.
83  * the storing of the mapping is based on the statement ordering for
84  * later reconstruction with the CODE.
85  * tabulating statements would help, but is that desirable?
86  *
87  * MY opinion is that all newgen objects should have a unique id
88  * associated to it, as an addditional hidden field, to support
89  * persistence more easily. Well, there would be some problemes, too.
90  *
91  * FC.
92  */
93 static int
95 {
96  int n = 0;
99  n++,
100  h);
101  return n;
102 }
103 
104 /** Write a statement mapping.
105 
106  This function is quite too low level... It mixes raw printf in a FILE
107  with gen_write. To survive other NewGen backend (XML), fprintf could
108  be replaced with a gen_fprintf() that could encapsulate the output in
109  a CDATA for example in the case of XML.
110 
111  But in this case it should be a call to something like
112  gen_write_int(fd, order) instead to do even simpler.
113 */
114 void
116  FILE * fd, /**< file to write to */
117  statement_mapping h /**< hash table to dump */)
118 {
119  fprintf(fd, "%d\n", statement_mapping_count(h));
121  {
122  statement key = (statement) s;
123  gen_chunkp val = (gen_chunkp) v;
124  int order = statement_ordering(key);
125  if (order!=STATEMENT_ORDERING_UNDEFINED) { /* save it! */
126  fprintf(fd, "%d\n", order);
127  gen_write(fd, (gen_chunkp) val);
128  }
129  else pips_user_warning("statement with illegal ordering\n");
130  },
131  h);
132 }
133 
134 
135 /** Read a statement mapping.
136 
137  This function is quite too low level... It mixes raw getc() from a
138  FILE with gen_read. To survive other NewGen backend (XML), fprintf
139  could be replaced with a gen_getc() that could peek in a CDATA for
140  example in the case of XML.
141 
142  But in this case it should be a call to something like so =
143  gen_read_int(fd) instead to do even simpler and read an int value (in
144  textual form or in <int>...</int> in the case of XML.
145 */
148 {
149  statement stat;
151  int n;
152 
153  pips_assert("some current module name", dbll_current_module);
154  pips_debug(3, "statement -> ??? for %s\n", dbll_current_module);
155  stat = (statement)
157 
159 
160  /* get meta data.
161  */
162  n = lire_int(fd);
163 
164  while (n-->0) {
165  int so = lire_int(fd);
166  pips_assert("valid ordering", so!=STATEMENT_ORDERING_UNDEFINED);
167  hash_put(result,(void*)ordering_to_statement(so),(void*)gen_read(fd));
168  }
169 
171 
172  return result;
173 }
174 
175 /* a little bit partial, because domain are not checked.
176  */
177 bool
179 {
180  STATEMENT_MAPPING_MAP(k, v, {
181  pips_assert("consistent statement",
183  pips_assert("consistent val", gen_consistent_p((void*) v));
184  },
185  h);
186  return true;
187 }
188 
189 void
191 {
192  STATEMENT_MAPPING_MAP(k, v, gen_free((void*) v), h);
194 }
195 
196 /**************************************** STATEMENT FUNCTIONS: STATEMENT -> */
197 
198 /* this piece of code makes low level assumptions about newgen internals.
199  * see also the comments about statement mappings above.
200  */
201 
202 #define STATEMENT_FUNCTION_MAP(k, v, code, _map_hash_h) \
203  { void * _map_hash_p = NULL; \
204  void * _map_k; void * _map_v; \
205  while ((_map_hash_p = \
206  hash_table_scan(_map_hash_h,_map_hash_p,&_map_k,&_map_v))) { \
207  statement k = (statement) ((gen_chunkp)_map_k)->p ; \
208  gen_chunkp v = ((gen_chunkp)_map_v)->p; \
209  code ; }}
210 
211 /* count the number of statements with a valid ordering. */
212 static int
214 {
215  int n = 0;
217  {
218  pips_assert("variable not used", x==x);
220  },
221  h);
222  return n;
223 }
224 
225 bool
227 {
228  hash_table h = (map+1)->h;
230  {
231  if (gen_type((void*)s)!=statement_domain) return false;
232  if (!gen_consistent_p((void*)x)) return false;
233  },
234  h);
235  return true;
236 }
237 
238 /* the stored stuff need be based on the ordering... because newgen won't
239  regenerate pointers...
240 
241  Should use a higher level pipsdbm_write_statement_mapping() to survive
242  to XML
243 */
244 void
246  FILE * fd, /**< file to write to */
247  gen_chunkp map /**< statement function */)
248 {
249  hash_table h = (map+1)->h;
250  fprintf(fd, "%td\n%d\n", map->i, number_of_ordered_statements(h));
252  {
253  int order = statement_ordering(s);
254  if (order!=STATEMENT_ORDERING_UNDEFINED) { /* save it! */
255  fprintf(fd, "%d\n", order);
256  gen_write(fd, x);
257  }
258  else {
259  pips_user_warning("Statement with illegal ordering, lost data: %s\n",
261  }
262  },
263  h);
264 }
265 
266 
267 /* Should use a higher level pipsdbm_read_statement_mapping() to survive
268  to XML
269 */
271 pipsdbm_read_statement_function(FILE * fd /**< file to read from */)
272 {
273  statement stat;
274  int domain, n;
275  gen_chunkp result;
276  hash_table h;
277 
278  pips_assert("some current module name", dbll_current_module);
279  pips_debug(3, "statement -> ??? for %s\n", dbll_current_module);
280  stat = (statement)
282 
284 
285  /* get meta data.
286  */
287  domain = lire_int(fd);
288  n = lire_int(fd);
289 
290  /* allocate the statement function.
291  */
292  result = gen_alloc(2*sizeof(gen_chunk), GEN_CHECK_ALLOC, domain);
293  h = (result+1)->h;
294 
295  /* now reads each couple and rebuild the function.
296  */
297  while(n-->0) {
298  int so = lire_int(fd);
299  pips_assert("valid ordering", so!=STATEMENT_ORDERING_UNDEFINED);
300  HASH_EXTEND(p, p, h, ordering_to_statement(so), gen_read(fd));
301  }
302 
304 
305  return result;
306 }
307 
308 /********************************************************************** MISC */
309 
310 /* Modification Dec 11 1995: ne pas utiliser free_static_control
311  * car il libere des champs qui appartiennent a d'autres structures
312  * que celles controlees par static_controlize...(champs d'origine)
313  * Les liberation de ces champs par un autre transformer (use_def_elim)
314  * entrainait alors un core dump au niveau de cette procedure.
315  * On fait a la place des gen_free_list en detail --DB ]
316  */
317  void
319 {
320  STATEMENT_MAPPING_MAP(s, val, {
326  gen_free( (void*) val );
327  }, map);
328 
330 }
331 
332 /* Functions to read and write declarations resource, which is a hash table
333  whose key and value are string (keyword/typedef and TK_keyword/TK_typedef)*/
334 
336 {
337  HASH_MAP(k,v,
338  {
339  fprintf(f, "%s\n", (char *) k);
340  fprintf(f, "%td\n", (_int) v);
341  },h);
342 }
343 
344 
346 {
348  int c;
349  while ((c = getc(f)) && c != EOF)
350  {
351  ungetc(c,f);
352  char* key = safe_readline(f);
353  _int value = atoi(safe_readline(f));
354 
355  hash_put(result,key,(void*)value);
356  }
357  return result;
358 }
359 
bool statement_consistent_p(statement p)
Definition: ri.c:2195
struct _newgen_struct_statement_ * statement
Definition: cloning.h:21
#define STATEMENT_FUNCTION_MAP(k, v, code, _map_hash_h)
this piece of code makes low level assumptions about newgen internals.
Definition: externals.c:202
bool pipsdbm_consistent_statement_function(gen_chunkp map)
Definition: externals.c:226
void declarations_write(FILE *f, hash_table h)
Functions to read and write declarations resource, which is a hash table whose key and value are stri...
Definition: externals.c:335
void free_static_control_mapping(statement_mapping map)
Modification Dec 11 1995: ne pas utiliser free_static_control car il libere des champs qui appartienn...
Definition: externals.c:318
static int number_of_ordered_statements(hash_table h)
count the number of statements with a valid ordering.
Definition: externals.c:213
void pipsdbm_free_statement_mapping(statement_mapping h)
Definition: externals.c:190
hash_table declarations_read(FILE *f)
Definition: externals.c:345
static int statement_mapping_count(statement_mapping h)
The old statement_mapping is an hash_table managed directly.
Definition: externals.c:94
void pipsdbm_write_statement_mapping(FILE *fd, statement_mapping h)
Write a statement mapping.
Definition: externals.c:115
gen_chunkp pipsdbm_read_statement_function(FILE *fd)
Should use a higher level pipsdbm_read_statement_mapping() to survive to XML.
Definition: externals.c:271
void pipsdbm_write_statement_function(FILE *fd, gen_chunkp map)
the stored stuff need be based on the ordering...
Definition: externals.c:245
char * pipsdbm_read_entities(FILE *fd)
externals.c
Definition: externals.c:64
bool pipsdbm_check_statement_mapping(statement_mapping h)
a little bit partial, because domain are not checked.
Definition: externals.c:178
static int lire_int(FILE *fd)
reads an int while sharing file and buffers with newgen...
Definition: externals.c:40
hash_table pipsdbm_read_statement_mapping(FILE *fd)
Read a statement mapping.
Definition: externals.c:147
void pipsdbm_free_entities(char *p)
Definition: externals.c:72
char * safe_readline(FILE *file)
returns the allocated line read, whatever its length.
Definition: file.c:497
#define GEN_CHECK_ALLOC
Definition: genC.h:307
union gen_chunk * gen_chunkp
void gen_free(gen_chunk *obj)
version without shared_pointers.
Definition: genClib.c:992
gen_chunk * gen_alloc(int size, int gen_check_p, int dom,...)
allocates something in newgen.
Definition: genClib.c:298
int gen_read_tabulated(FILE *file, int create_p)
GEN_READ_TABULATED reads FILE to update the Gen_tabulated_ table.
Definition: genClib.c:2334
int gen_consistent_p(gen_chunk *obj)
GEN_CONSISTENT_P dynamically checks the type correctness of OBJ.
Definition: genClib.c:2398
int gen_free_tabulated(int domain)
free tabulated elements of this domain.
Definition: genClib.c:1461
int gen_type(gen_chunk *obj)
GEN_TYPE returns the domain number for the object in argument.
Definition: genClib.c:82
gen_chunk * gen_read(FILE *file)
GEN_READ reads any object from the FILE stream.
Definition: genClib.c:2323
void gen_write(FILE *fd, gen_chunk *obj)
GEN_WRITE writes the OBJect on the stream FD.
Definition: genClib.c:1745
FILE * genread_in
int genread_input(void)
void gen_free_list(list l)
free the spine of the list
Definition: list.c:327
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
string statement_identification(statement)
Like external_statement_identification(), but with internal information, the hexadecimal address of t...
Definition: statement.c:1700
hash_table hash_table_make(hash_key_type key_type, size_t size)
Definition: hash.c:294
void hash_put(hash_table htp, const void *key, const void *val)
This functions stores a couple (key,val) in the hash table pointed to by htp.
Definition: hash.c:364
const char * dbll_current_module
the current module is expected by some load/save functions...
Definition: lowlevel.c:41
#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_user_warning
Definition: misc-local.h:146
#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 FREE_STATEMENT_MAPPING(map)
Definition: newgen-local.h:45
#define STATEMENT_ORDERING_UNDEFINED
mapping.h inclusion
Definition: newgen-local.h:35
#define STATEMENT_MAPPING_MAP(s, v, code, h)
Definition: newgen-local.h:53
#define HASH_MAP(k, v, code, ht)
Definition: newgen_hash.h:60
@ hash_string
Definition: newgen_hash.h:32
@ hash_pointer
Definition: newgen_hash.h:32
#define HASH_EXTEND(start, image, h, k, v)
Definition: newgen_map.h:36
intptr_t _int
_INT
Definition: newgen_types.h:53
int f(int off1, int off2, int n, float r[n], float a[n], float b[n])
Definition: offsets.c:15
hash_table set_ordering_to_statement(statement s)
To be used instead of initialize_ordering_to_statement() to make sure that the hash table ots is in s...
Definition: ordering.c:172
statement ordering_to_statement(int o)
Get the statement associated to a given ordering.
Definition: ordering.c:111
void reset_ordering_to_statement(void)
Reset the mapping from ordering to statement.
Definition: ordering.c:185
#define static_control_loops(x)
Definition: paf_ri.h:757
#define static_control_params(x)
Definition: paf_ri.h:755
#define static_control_tests(x)
Definition: paf_ri.h:759
#define statement_ordering(x)
Definition: ri.h:2454
#define statement_domain
newgen_sizeofexpression_domain_defined
Definition: ri.h:362
#define entity_domain
newgen_syntax_domain_defined
Definition: ri.h:410
int fprintf()
test sc_min : ce test s'appelle par : programme fichier1.data fichier2.data ...
static char * x
Definition: split_file.c:159
A DOMAIN union describes the structure of a user type.
A gen_chunk is used to store every object.
Definition: genC.h:58
_int i
Definition: genC.h:62