fix initialisation; do correct iteration
[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   assert (iro_Alloc == get_irn_opcode (alloc));
75
76   int i;
77   alloc_pto_t *alloc_pto = obstack_alloc (pto_obst, sizeof (alloc_pto_t));
78   type *tp = get_Alloc_type (alloc);
79
80   alloc_pto->ptos = (pto_t**) obstack_alloc (pto_obst, n_ctxs * sizeof (pto_t*));
81
82   for (i = 0; i < n_ctxs; i ++) {
83     desc_t *desc = new_name (tp, alloc);
84     alloc_pto->ptos [i] = new_pto (alloc);
85     qset_insert (alloc_pto->ptos [i]->values, desc);
86   }
87
88   return (alloc_pto);
89 }
90
91 /* Allocate a new pto for a symconst */
92 static pto_t* new_symconst_pto (ir_node *symconst)
93 {
94   assert (iro_SymConst == get_irn_opcode (symconst));
95
96   pto_t *pto = new_pto (symconst);
97   entity *ent = get_SymConst_entity (symconst);
98   desc_t *desc = NULL;
99
100   /* ok, so if the symconst has a pointer-to-mumble, it's some address
101      calculation, but if it's the mumble itself, it's just the same,
102      except it's presumably a constant of mumble. In any case, we need to
103      branch on this.  "How's that for object fucking oriented? --jwz" */
104   if (is_pointer_type (get_entity_type (ent))) {
105     desc = new_ent_name (ent);
106   } else if (is_class_type (get_entity_type (ent))) {
107     desc = new_name (get_entity_type (ent), symconst);
108   } else {
109     fprintf (stderr, "%s: not handled: %s[%li] (\"%s\")\n",
110              __FUNCTION__,
111              get_op_name (get_irn_op (symconst)),
112              get_irn_node_nr (symconst),
113              get_entity_name (ent));
114     assert (0 && "something not handled");
115   }
116
117   qset_insert (pto->values, desc);
118
119   return (pto);
120 }
121
122 /* Allocate a new pto for a constant */
123 static pto_t *new_const_pto (ir_node *cnst)
124 {
125   assert (iro_Const == get_irn_opcode (cnst));
126   assert (mode_P == get_irn_mode (cnst));
127
128   static pto_t *cnst_pto = NULL;
129
130   /* since 'store's and 'load's via a NULL pointer are hardly ever
131      successful, we get away with an empty set */
132
133   tarval *tv = get_Const_tarval (cnst);
134
135   assert (tv == get_tarval_null (mode_P));
136
137   if (NULL == cnst_pto) {
138     cnst_pto = new_pto (cnst);
139   }
140
141   return (cnst_pto);
142 }
143
144
145 /* Helper to pto_init --- clear the link fields of class types */
146 static void clear_type_link (type_or_ent *thing, void *__unused)
147 {
148   if (is_type (thing)) {
149     type *tp = (type*) thing;
150
151     if (is_class_type (tp)) {
152       DBGPRINT (1, (stdout, "%s (\"%s\")\n",
153                     __FUNCTION__, get_type_name (tp)));
154
155       set_type_link (tp, NULL);
156     }
157   } else if (is_entity (thing)) {
158     entity *ent = (entity*) thing;
159
160     DBGPRINT (1, (stdout, "%s (\"%s\")\n",
161                   __FUNCTION__, get_entity_name (ent)));
162
163     set_entity_link (ent, NULL);
164   }
165 }
166
167 /* Helper to pto_init_graph --- clear the links of the given node */
168 static void clear_node_link (ir_node *node, void *__unused)
169 {
170   set_irn_link (node, NULL);
171 }
172
173 /* Helper to pto_init_graph --- clear the links of all nodes */
174 static void clear_graph_links (ir_graph *graph)
175 {
176   irg_walk_graph (graph, clear_node_link, NULL, NULL);
177 }
178
179 /* Reset ALL the pto values for a new pass */
180 static void reset_node_pto (ir_node *node, void *env)
181 {
182   reset_env_t *reset_env = (reset_env_t*) env;
183   int ctx_idx = reset_env->ctx_idx;
184   const opcode op = get_irn_opcode (node);
185
186   /* HERE ("start"); */
187
188   switch (op) {
189   case (iro_Load):
190     /* case (iro_Const): */
191   case (iro_Call):
192   case (iro_Block):             /* END BLOCK only */
193   case (iro_Phi): {
194     /* allocate 'empty' pto values */
195     pto_t *pto = new_pto (node);
196     set_node_pto (node, pto);
197   } break;
198
199   case (iro_Alloc): {
200     /* set alloc to 'right' current pto */
201
202     /* HERE ("alloc"); */
203
204     alloc_pto_t *alloc_pto = (alloc_pto_t*) get_irn_link (node);
205     alloc_pto->curr_pto = alloc_pto->ptos [ctx_idx];
206
207     DBGPRINT (1, (stdout, "%s: setting pto of \"%s[%li]\" for ctx %i\n",
208                   __FUNCTION__,
209                   OPNAME (node),
210                   OPNUM (node),
211                   ctx_idx));
212
213     assert (alloc_pto->curr_pto);
214   } break;
215   case (iro_Const):
216   case (iro_SymConst): {
217       /* nothing, leave as-is */
218     } break;
219
220   default: {
221     /* basically, nothing */
222     DBGPRINT (2, (stdout, "%s: resetting pto of \"%s[%li]\"\n",
223                   __FUNCTION__,
224                   OPNAME (node),
225                   OPNUM (node)));
226     set_node_pto (node, NULL);
227   } break;
228   }
229
230   /* HERE ("end"); */
231 }
232
233 /* Temporary fix until we get 'real' ptos: Allocate some dummy for pto */
234 static void init_pto (ir_node *node, void *env)
235 {
236   init_env_t *init_env = (init_env_t*) env;
237   int n_ctxs = init_env->n_ctxs;
238
239   const opcode op = get_irn_opcode (node);
240
241   switch (op) {
242   case (iro_SymConst): {
243     if (mode_P == get_irn_mode (node)) {
244       /* debugging only */
245       entity *ent = get_SymConst_entity (node);
246       if (is_class_type (get_entity_type (ent)) ||
247           is_pointer_type (get_entity_type (ent))) {
248         DBGPRINT (1, (stdout, "%s: new name \"%s\" for \"%s[%li]\"\n",
249                       __FUNCTION__,
250                       get_entity_name (ent),
251                       OPNAME (node),
252                       OPNUM (node)));
253
254         pto_t *symconst_pto = new_symconst_pto (node);
255         set_node_pto (node, symconst_pto);
256       }
257     }
258   } break;
259   case (iro_Alloc): {
260     type *tp = get_Alloc_type (node); /* debugging only */
261     alloc_pto_t *alloc_pto = new_alloc_pto (node, n_ctxs);
262     set_alloc_pto (node, alloc_pto);
263
264     DBGPRINT (1, (stdout, "%s: %i names \"%s\" for \"%s[%li]\"\n",
265                   __FUNCTION__,
266                   n_ctxs,
267                   get_type_name (tp),
268                   OPNAME (node),
269                   OPNUM (node)));
270   } break;
271
272   case (iro_Const): {
273     if (mode_P == get_irn_mode (node)) {
274       pto_t *pto = new_const_pto (node);
275       set_node_pto (node, pto);
276     }
277   } break;
278
279   case (iro_Load):
280   case (iro_Call):
281   case (iro_Phi):
282     /* nothing --- handled by reset_node_pto on each pass */
283     break;
284   default: {
285     /* nothing */
286   } break;
287   }
288 }
289
290
291 /* Initialise the given graph for a new pass run */
292 static void pto_init_graph_allocs (ir_graph *graph)
293 {
294   graph_info_t *ginfo = ecg_get_info (graph);
295   int n_ctxs = ginfo->n_ctxs;
296
297   init_env_t *init_env = xmalloc (sizeof (init_env_t));
298   init_env->n_ctxs = n_ctxs;
299
300   /* HERE ("start"); */
301
302   irg_walk_graph (graph, init_pto, NULL, init_env);
303
304   memset (init_env, 0x00, sizeof (init_env_t));
305   free (init_env);
306
307   /* HERE ("end"); */
308 }
309
310 /* ===================================================
311    Exported Implementation:
312    =================================================== */
313 /* "Fake" the arguments to the main method */
314 void fake_main_args (ir_graph *graph)
315 {
316   /* HERE ("start"); */
317
318   entity *ent = get_irg_entity (graph);
319   type *mtp = get_entity_type (ent);
320   ir_node **args = find_irg_args (graph);
321   type *ctp = get_method_param_type (mtp, 1); /* ctp == char[]*[]* */
322
323   /* 'main' has signature 'void(int, char[]*[]*)' */
324   assert (NULL == args [2]);
325
326   assert (is_pointer_type (ctp));
327
328   ctp = get_pointer_points_to_type (ctp); /* ctp == char[]*[] */
329
330   assert (is_array_type (ctp));
331
332   desc_t *arg_desc = new_name (ctp, args [1]);
333   pto_t *arg_pto = new_pto (args [1]);
334   /* todo: simulate 'store' to arg1[] ?!? */
335   qset_insert (arg_pto->values, arg_desc);
336
337   set_node_pto (args [1], arg_pto);
338
339   DBGPRINT (1, (stdout, "%s:%i (%s[%li])\n",
340                 __FUNCTION__, __LINE__, OPNAME (args [1]), OPNUM (args [1])));
341
342 # ifdef TEST_MAIN_TYPE
343   ctp = get_array_element_type (ctp); /* ctp == char[]* */
344
345   assert (is_pointer_type (ctp));
346
347   ctp = get_pointer_points_to_type (ctp); /* ctp == char[] */
348
349   assert (is_array_type (ctp));
350
351   ctp = get_array_element_type (ctp); /* ctp == char */
352
353   assert (is_primitive_type (ctp));
354 # endif /* defined  TEST_MAIN_TYPE */
355
356   /* HERE ("end"); */
357 }
358
359 /* Initialise the Init module */
360 void pto_init_init ()
361 {
362   pto_obst = (struct obstack*) xmalloc (sizeof (struct obstack));
363
364   obstack_init (pto_obst);
365 }
366
367 /* Cleanup the Init module */
368 void pto_init_cleanup ()
369 {
370   obstack_free (pto_obst, NULL);
371   memset (pto_obst, 0x00, sizeof (struct obstack));
372   free (pto_obst);
373   pto_obst = NULL;
374 }
375
376
377 /* Initialise the Names of the Types/Entities */
378 void pto_init_type_names ()
379 {
380   /* HERE ("start"); */
381   type_walk (clear_type_link, NULL, NULL);
382   /* HERE ("end"); */
383 }
384
385 /* Initialise the given graph for a new pass run */
386 void pto_init_graph (ir_graph *graph)
387 {
388   graph_info_t *ginfo = ecg_get_info (graph);
389   const int n_ctxs = ginfo->n_ctxs;
390
391   /* only for debugging stuff: */
392   entity *ent = get_irg_entity (graph);
393   const char *ent_name = (char*) get_entity_name (ent);
394   const char *own_name = (char*) get_type_name (get_entity_owner (ent));
395
396   DBGPRINT (1, (stdout, "%s: init \"%s.%s\" for %i ctxs\n", __FUNCTION__,
397                 own_name, ent_name, n_ctxs));
398
399   /* HERE ("start"); */
400
401   clear_graph_links     (graph);
402   pto_init_graph_allocs (graph);
403
404   /* HERE ("end"); */
405 }
406
407 /* Reset the given graph for a new pass run */
408 void pto_reset_graph_pto (ir_graph *graph, int ctx_idx)
409 {
410   /* HERE ("start"); */
411
412   reset_env_t *reset_env = (reset_env_t*) xmalloc (sizeof (reset_env_t));
413   reset_env->ctx_idx = ctx_idx;
414
415   irg_walk_graph (graph, reset_node_pto, NULL, reset_env);
416
417   memset (reset_env, 0x00, sizeof (reset_env_t));
418   free (reset_env);
419   /* HERE ("end"); */
420 }
421
422 \f
423 /*
424   $Log$
425   Revision 1.7  2004/11/30 14:47:54  liekweg
426   fix initialisation; do correct iteration
427
428   Revision 1.6  2004/11/26 16:00:41  liekweg
429   recognize class consts vs. ptr-to-class consts
430
431   Revision 1.5  2004/11/24 14:53:56  liekweg
432   Bugfixes
433
434   Revision 1.4  2004/11/20 21:21:56  liekweg
435   Finalise initialisation
436
437   Revision 1.3  2004/11/18 16:37:07  liekweg
438   rewrite
439
440
441 */