fixup printfs, don't put environments on the stack
[libfirm] / ir / ana2 / pto_init.c
1 /* -*- c -*- */
2
3 /*
4    Project:     libFIRM
5    File name:   ir/ana/pto_init.c
6    Purpose:     Initialisation Functions
7    Author:      Florian
8    Modified by:
9    Created:     Sat Nov 13 19:35:27 CET 2004
10    CVS-ID:      $Id$
11    Copyright:   (c) 1999-2004 Universität Karlsruhe
12    Licence:     This file is protected by the GPL -  GNU GENERAL PUBLIC LICENSE.
13 */
14
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
18
19 /*
20  pto_init: Initialisation Functions
21 */
22
23 # include <obstack.h>
24 # include <string.h>
25
26 # include "pto.h"
27 # include "pto_init.h"
28 # include "pto_debug.h"
29 # include "pto_comp.h"
30 # include "pto_name.h"
31 # include "pto_util.h"
32
33 # include "typewalk.h"
34 # include "irgwalk.h"
35 # include "xmalloc.h"
36
37 /* Local Defines: */
38 # define obstack_chunk_alloc xmalloc
39 # define obstack_chunk_free  free
40
41 /* Local Data Types: */
42 typedef struct init_env_str
43 {
44   int n_ctxs;
45 } init_env_t;
46
47 typedef struct reset_env_str
48 {
49   int ctx_idx;
50 } reset_env_t;
51
52 /* Local Variables: */
53 extern struct obstack *qset_obst; /* from pto_name */
54
55 static struct obstack *pto_obst = NULL; /* all pto_t's go onto this one */
56
57 /* Local Prototypes: */
58
59 /* ===================================================
60    Local Implementation:
61    =================================================== */
62 /* Allocate a new pto */
63 static pto_t *new_pto (ir_node *node)
64 {
65   pto_t *pto = obstack_alloc (pto_obst, sizeof (pto_t));
66   pto->values = qset_new (N_INITIAL_OJBS, qset_obst);
67
68   return (pto);
69 }
70
71 /* Allocate a new alloc_pto */
72 static alloc_pto_t *new_alloc_pto (ir_node *alloc, int n_ctxs)
73 {
74   int i;
75   alloc_pto_t *alloc_pto = obstack_alloc (pto_obst, sizeof (alloc_pto_t));
76   type *tp;
77
78   assert (op_Alloc == get_irn_op(alloc));
79
80   tp = get_Alloc_type (alloc);
81
82   alloc_pto->ptos = (pto_t**) obstack_alloc (pto_obst, n_ctxs * sizeof (pto_t*));
83
84   for (i = 0; i < n_ctxs; i ++) {
85     desc_t *desc = new_name (tp, alloc, i);
86     alloc_pto->ptos [i] = new_pto (alloc);
87     qset_insert (alloc_pto->ptos [i]->values, desc);
88   }
89
90   return (alloc_pto);
91 }
92
93 /* Allocate a new pto for a symconst */
94 static pto_t* new_symconst_pto (ir_node *symconst)
95 {
96   pto_t *pto;
97   entity *ent;
98   desc_t *desc = NULL;
99
100   assert (op_SymConst == get_irn_op(symconst));
101
102   pto = new_pto (symconst);
103   ent = get_SymConst_entity (symconst);
104
105   /*
106   const char *ent_name = (char*) get_entity_name (ent);
107   const char *own_name = (char*) get_type_name (get_entity_owner (ent));
108   HERE3 ("start", own_name, ent_name);
109   */
110   /* Ok, so if the symconst has a pointer-to-mumble, it's some address
111      calculation, but if it's the mumble itself, it's just the same,
112      except it's presumably a constant of mumble. In any case, we need to
113      branch on this.  "How's that for object fucking oriented? --jwz" */
114   if (is_Pointer_type (get_entity_type (ent))) {
115     desc = new_ent_name (ent);
116   } else if (is_Class_type (get_entity_type (ent))) {
117     desc = new_name (get_entity_type (ent), symconst, -1);
118   } else {
119     fprintf (stderr, "%s: not handled: %s[%li] (\"%s\")\n",
120              __FUNCTION__,
121              get_op_name (get_irn_op (symconst)),
122              get_irn_node_nr (symconst),
123              get_entity_name (ent));
124     assert (0 && "something not handled");
125   }
126
127   qset_insert (pto->values, desc);
128
129   /* HERE3 ("end", own_name, ent_name); */
130
131   return (pto);
132 }
133
134 /* Helper to pto_init --- clear the link fields of class types */
135 static void clear_type_link (type_or_ent *thing, void *_unused)
136 {
137   if (is_type (thing)) {
138     type *tp = (type*) thing;
139
140     if (is_Class_type (tp)) {
141       DBGPRINT (1, (stdout, "%s (\"%s\")\n",
142                     __FUNCTION__,
143                     get_type_name (tp)));
144
145       set_type_link (tp, NULL);
146     }
147   } else if (is_entity (thing)) {
148     entity *ent = (entity*) thing;
149
150     DBGPRINT (1, (stdout, "%s (\"%s\")\n",
151                   __FUNCTION__,
152                   get_entity_name (ent)));
153
154     set_entity_link (ent, NULL);
155   }
156 }
157
158 /* Helper to pto_init_graph --- clear the links of the given node */
159 static void clear_node_link (ir_node *node, void *_unused)
160 {
161   set_irn_link (node, NULL);
162 }
163
164 /* Helper to pto_init_graph --- clear the links of all nodes */
165 static void clear_graph_links (ir_graph *graph)
166 {
167   irg_walk_graph (graph, clear_node_link, NULL, NULL);
168 }
169
170 /* Reset ALL the pto values for a new pass */
171 static void reset_node_pto (ir_node *node, void *env)
172 {
173   reset_env_t *reset_env = (reset_env_t*) env;
174   int ctx_idx = reset_env->ctx_idx;
175   opcode op = get_irn_opcode (node);
176
177   /* HERE ("start"); */
178
179   switch (op) {
180   case (iro_Load):
181   case (iro_Call):
182   case (iro_Block):             /* END BLOCK only */
183   case (iro_Phi): {
184     /* allocate 'empty' pto values */
185     pto_t *pto = new_pto (node);
186     set_node_pto (node, pto);
187   } break;
188
189   case (iro_Alloc): {
190     /* set alloc to 'right' current pto */
191     alloc_pto_t *alloc_pto = (alloc_pto_t*) get_irn_link (node);
192     alloc_pto->curr_pto = alloc_pto->ptos [ctx_idx];
193
194     DBGPRINT (1, (stdout, "%s: setting pto of \"%s[%li]\" for ctx %i\n",
195                   __FUNCTION__,
196                   OPNAME (node),
197                   OPNUM (node),
198                   ctx_idx));
199
200     assert (alloc_pto->curr_pto);
201   } break;
202   case (iro_SymConst): {
203       /* nothing, leave as-is */
204     } break;
205
206   default: {
207     /* basically, nothing */
208     DBGPRINT (2, (stdout, "%s: resetting pto of \"%s[%li]\"\n",
209                   __FUNCTION__,
210                   OPNAME (node),
211                   OPNUM (node)));
212     set_node_pto (node, NULL);
213   } break;
214   }
215
216   /* HERE ("end"); */
217 }
218
219 /* Initialise primary name sources */
220 static void init_pto (ir_node *node, void *env)
221 {
222   init_env_t *init_env = (init_env_t*) env;
223   int n_ctxs = init_env->n_ctxs;
224
225   opcode op = get_irn_opcode (node);
226
227   switch (op) {
228   case (iro_SymConst): {
229     if (mode_is_reference(get_irn_mode (node))) {
230       entity *ent = get_SymConst_entity (node);
231       type   *tp = get_entity_type (ent);
232       if (is_Class_type (tp) || is_Pointer_type (tp)) {
233         pto_t *symconst_pto = new_symconst_pto (node);
234         set_node_pto (node, symconst_pto);
235
236         /* debugging only */
237         DBGPRINT (1, (stdout, "%s: new name \"%s\" for \"%s[%li]\"\n",
238                       __FUNCTION__,
239                       get_entity_name (ent),
240                       OPNAME (node),
241                       OPNUM (node)));
242       }
243     }
244   } break;
245
246   case (iro_Alloc): {
247     alloc_pto_t *alloc_pto = new_alloc_pto (node, n_ctxs);
248     type *tp;
249
250     set_alloc_pto (node, alloc_pto);
251
252     tp = get_Alloc_type (node); /* debugging only */
253     DBGPRINT (1, (stdout, "%s: %i names \"%s\" for \"%s[%li]\"\n",
254                   __FUNCTION__,
255                   n_ctxs,
256                   get_type_name (tp),
257                   OPNAME (node),
258                   OPNUM (node)));
259   } break;
260
261   case (iro_Load):
262   case (iro_Call):
263   case (iro_Phi):
264     /* nothing --- handled by reset_node_pto on each pass */
265     break;
266   default: {
267     /* nothing */
268   } break;
269   }
270 }
271
272
273 /* Initialise the given graph for a new pass run */
274 static void pto_init_graph_allocs (ir_graph *graph)
275 {
276   graph_info_t *ginfo = ecg_get_info (graph);
277   init_env_t *init_env;
278
279   init_env = xmalloc (sizeof (init_env_t));
280   init_env->n_ctxs = ginfo->n_ctxs;
281
282   /* HERE ("start"); */
283
284   irg_walk_graph (graph, init_pto, NULL, init_env);
285
286   /* HERE ("end"); */
287   memset (init_env, 0x00, sizeof (init_env_t));
288   free (init_env);
289 }
290
291 /* ===================================================
292    Exported Implementation:
293    =================================================== */
294 /* "Fake" the arguments to the main method */
295 void fake_main_args (ir_graph *graph)
296 {
297   /* HERE ("start"); */
298
299   entity *ent = get_irg_entity (graph);
300   type *mtp = get_entity_type (ent);
301   ir_node **args = find_irg_args (graph);
302   type *ctp = get_method_param_type (mtp, 1); /* ctp == char[]*[]* */
303   desc_t *arg_desc;
304   pto_t *arg_pto;
305
306   /* 'main' has signature 'void(int, char[]*[]*)' */
307   assert (NULL == args [2]);
308
309   assert (is_Pointer_type (ctp));
310
311   ctp = get_pointer_points_to_type (ctp); /* ctp == char[]*[] */
312
313   assert (is_Array_type (ctp));
314
315   arg_desc = new_name (ctp, args [1], -1);
316   arg_pto = new_pto (args [1]);
317   /* todo: simulate 'store' to arg1[] ?!? */
318   qset_insert (arg_pto->values, arg_desc);
319
320   set_node_pto (args [1], arg_pto);
321
322   DBGPRINT (1, (stdout, "%s:%i (%s[%li])\n",
323                 __FUNCTION__, __LINE__,
324                 OPNAME (args [1]), OPNUM (args [1])));
325
326 # ifdef TEST_MAIN_TYPE
327   ctp = get_array_element_type (ctp); /* ctp == char[]* */
328
329   assert (is_Pointer_type (ctp));
330
331   ctp = get_pointer_points_to_type (ctp); /* ctp == char[] */
332
333   assert (is_Array_type (ctp));
334
335   ctp = get_array_element_type (ctp); /* ctp == char */
336
337   assert (is_primitive_type (ctp));
338 # endif /* defined  TEST_MAIN_TYPE */
339
340   /* HERE ("end"); */
341 }
342
343 /* Initialise the Init module */
344 void pto_init_init ()
345 {
346   pto_obst = (struct obstack*) xmalloc (sizeof (struct obstack));
347
348   obstack_init (pto_obst);
349 }
350
351 /* Cleanup the Init module */
352 void pto_init_cleanup ()
353 {
354   obstack_free (pto_obst, NULL);
355   memset (pto_obst, 0x00, sizeof (struct obstack));
356   free (pto_obst);
357   pto_obst = NULL;
358 }
359
360
361 /* Initialise the Names of the Types/Entities */
362 void pto_init_type_names ()
363 {
364   /* HERE ("start"); */
365   type_walk (clear_type_link, NULL, NULL);
366   /* HERE ("end"); */
367 }
368
369 /* Initialise the given graph for a new pass run */
370 void pto_init_graph (ir_graph *graph)
371 {
372   ir_node **proj_args;
373   graph_info_t *ginfo = ecg_get_info (graph);
374   const int n_ctxs = ginfo->n_ctxs;
375
376   /* only for debugging stuff: */
377   entity *ent = get_irg_entity (graph);
378   const char *ent_name = (char*) get_entity_name (ent);
379   const char *own_name = (char*) get_type_name (get_entity_owner (ent));
380
381   DBGPRINT (2, (stdout, "%s: init \"%s.%s\" for %i ctxs\n",
382                 __FUNCTION__,
383                 own_name, ent_name, n_ctxs));
384
385   /* HERE ("start"); */
386
387   clear_graph_links     (graph);
388   pto_init_graph_allocs (graph);
389
390   /* HERE ("end"); */
391
392   assert (NULL == get_irg_proj_args (graph));
393   proj_args = find_irg_args (graph);
394   set_irg_proj_args (graph, proj_args);
395   assert (proj_args == get_irg_proj_args (graph));
396 }
397
398 /* Reset the given graph for a new pass run */
399 void pto_reset_graph_pto (ir_graph *graph, int ctx_idx)
400 {
401   reset_env_t *reset_env;
402
403   reset_env = (reset_env_t*) xmalloc (sizeof (reset_env_t));
404   reset_env->ctx_idx = ctx_idx;
405
406   /* HERE ("start"); */
407
408   irg_walk_graph (graph, reset_node_pto, NULL, &reset_env);
409
410   /* HERE ("end"); */
411   memset (reset_env, 0x00, sizeof (reset_env_t));
412   free (reset_env);
413 }
414
415 \f
416 /*
417   $Log$
418   Revision 1.15  2005/01/10 17:26:34  liekweg
419   fixup printfs, don't put environments on the stack
420
421   Revision 1.14  2005/01/05 14:25:54  beck
422   renames all is_x*_type() functions to is_X*_type() to prevent name clash with EDG fronten
423
424   Revision 1.13  2004/12/21 15:07:55  beck
425   removed C99 contructs
426   removed unnecessary allocation
427   removed use of mode_P, use mode_is_reference() instead
428   removed handling of Const with pointer tarvals, these constructs are removed
429
430   Revision 1.12  2004/12/20 17:41:14  liekweg
431   __unused -> _unused
432
433   Revision 1.11  2004/12/20 17:34:35  liekweg
434   fix recursion handling
435
436   Revision 1.10  2004/12/15 13:31:00  liekweg
437   store ctx idx in names
438
439   Revision 1.9  2004/12/15 09:18:18  liekweg
440   pto_name.c
441
442   Revision 1.8  2004/12/02 16:17:51  beck
443   fixed config.h include
444
445   Revision 1.7  2004/11/30 14:47:54  liekweg
446   fix initialisation; do correct iteration
447
448   Revision 1.6  2004/11/26 16:00:41  liekweg
449   recognize class consts vs. ptr-to-class consts
450
451   Revision 1.5  2004/11/24 14:53:56  liekweg
452   Bugfixes
453
454   Revision 1.4  2004/11/20 21:21:56  liekweg
455   Finalise initialisation
456
457   Revision 1.3  2004/11/18 16:37:07  liekweg
458   rewrite
459
460
461 */