renames all is_x*_type() functions to is_X*_type() to prevent name clash with EDG...
[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));
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, "new_symconst_pto(): not handled: %s[%li] (\"%s\")\n",
120              get_op_name (get_irn_op (symconst)),
121              get_irn_node_nr (symconst),
122              get_entity_name (ent));
123     assert (0 && "something not handled");
124   }
125
126   qset_insert (pto->values, desc);
127
128   /* HERE3 ("end", own_name, ent_name); */
129
130   return (pto);
131 }
132
133 /* Helper to pto_init --- clear the link fields of class types */
134 static void clear_type_link (type_or_ent *thing, void *_unused)
135 {
136   if (is_type (thing)) {
137     type *tp = (type*) thing;
138
139     if (is_Class_type (tp)) {
140       DBGPRINT (1, (stdout, "clear_type_link() (\"%s\")\n",
141                     get_type_name (tp)));
142
143       set_type_link (tp, NULL);
144     }
145   } else if (is_entity (thing)) {
146     entity *ent = (entity*) thing;
147
148     DBGPRINT (1, (stdout, "clear_type_link() (\"%s\")\n",
149                   get_entity_name (ent)));
150
151     set_entity_link (ent, NULL);
152   }
153 }
154
155 /* Helper to pto_init_graph --- clear the links of the given node */
156 static void clear_node_link (ir_node *node, void *_unused)
157 {
158   set_irn_link (node, NULL);
159 }
160
161 /* Helper to pto_init_graph --- clear the links of all nodes */
162 static void clear_graph_links (ir_graph *graph)
163 {
164   irg_walk_graph (graph, clear_node_link, NULL, NULL);
165 }
166
167 /* Reset ALL the pto values for a new pass */
168 static void reset_node_pto (ir_node *node, void *env)
169 {
170   reset_env_t *reset_env = (reset_env_t*) env;
171   int ctx_idx = reset_env->ctx_idx;
172   opcode op = get_irn_opcode (node);
173
174   /* HERE ("start"); */
175
176   switch (op) {
177   case (iro_Load):
178   case (iro_Call):
179   case (iro_Block):             /* END BLOCK only */
180   case (iro_Phi): {
181     /* allocate 'empty' pto values */
182     pto_t *pto = new_pto (node);
183     set_node_pto (node, pto);
184   } break;
185
186   case (iro_Alloc): {
187     /* set alloc to 'right' current pto */
188     alloc_pto_t *alloc_pto = (alloc_pto_t*) get_irn_link (node);
189     alloc_pto->curr_pto = alloc_pto->ptos [ctx_idx];
190
191     DBGPRINT (1, (stdout, "reset_node_pto(): setting pto of \"%s[%li]\" for ctx %i\n",
192                   OPNAME (node),
193                   OPNUM (node),
194                   ctx_idx));
195
196     assert (alloc_pto->curr_pto);
197   } break;
198   case (iro_SymConst): {
199       /* nothing, leave as-is */
200     } break;
201
202   default: {
203     /* basically, nothing */
204     DBGPRINT (2, (stdout, "reset_node_pto(): resetting pto of \"%s[%li]\"\n",
205                   OPNAME (node),
206                   OPNUM (node)));
207     set_node_pto (node, NULL);
208   } break;
209   }
210
211   /* HERE ("end"); */
212 }
213
214 /* Initialise primary name sources */
215 static void init_pto (ir_node *node, void *env)
216 {
217   init_env_t *init_env = (init_env_t*) env;
218   int n_ctxs = init_env->n_ctxs;
219
220   opcode op = get_irn_opcode (node);
221
222   switch (op) {
223   case (iro_SymConst): {
224     if (mode_is_reference(get_irn_mode (node))) {
225       entity *ent = get_SymConst_entity (node);
226       type   *tp = get_entity_type (ent);
227       if (is_Class_type (tp) || is_Pointer_type (tp)) {
228         pto_t *symconst_pto = new_symconst_pto (node);
229         set_node_pto (node, symconst_pto);
230
231         /* debugging only */
232         DBGPRINT (1, (stdout, "init_pto(): new name \"%s\" for \"%s[%li]\"\n",
233                       get_entity_name (ent),
234                       OPNAME (node),
235                       OPNUM (node)));
236       }
237     }
238   } break;
239
240   case (iro_Alloc): {
241     alloc_pto_t *alloc_pto = new_alloc_pto (node, n_ctxs);
242     type *tp;
243
244     set_alloc_pto (node, alloc_pto);
245
246     tp = get_Alloc_type (node); /* debugging only */
247     DBGPRINT (1, (stdout, "init_pto(): %i names \"%s\" for \"%s[%li]\"\n",
248                   n_ctxs,
249                   get_type_name (tp),
250                   OPNAME (node),
251                   OPNUM (node)));
252   } break;
253
254   case (iro_Load):
255   case (iro_Call):
256   case (iro_Phi):
257     /* nothing --- handled by reset_node_pto on each pass */
258     break;
259   default: {
260     /* nothing */
261   } break;
262   }
263 }
264
265
266 /* Initialise the given graph for a new pass run */
267 static void pto_init_graph_allocs (ir_graph *graph)
268 {
269   graph_info_t *ginfo = ecg_get_info (graph);
270   init_env_t init_env;
271
272   init_env.n_ctxs = ginfo->n_ctxs;
273
274   /* HERE ("start"); */
275
276   irg_walk_graph (graph, init_pto, NULL, &init_env);
277
278   /* HERE ("end"); */
279 }
280
281 /* ===================================================
282    Exported Implementation:
283    =================================================== */
284 /* "Fake" the arguments to the main method */
285 void fake_main_args (ir_graph *graph)
286 {
287   /* HERE ("start"); */
288
289   entity *ent = get_irg_entity (graph);
290   type *mtp = get_entity_type (ent);
291   ir_node **args = find_irg_args (graph);
292   type *ctp = get_method_param_type (mtp, 1); /* ctp == char[]*[]* */
293   desc_t *arg_desc;
294   pto_t *arg_pto;
295
296   /* 'main' has signature 'void(int, char[]*[]*)' */
297   assert (NULL == args [2]);
298
299   assert (is_Pointer_type (ctp));
300
301   ctp = get_pointer_points_to_type (ctp); /* ctp == char[]*[] */
302
303   assert (is_Array_type (ctp));
304
305   arg_desc = new_name (ctp, args [1], -1);
306   arg_pto = new_pto (args [1]);
307   /* todo: simulate 'store' to arg1[] ?!? */
308   qset_insert (arg_pto->values, arg_desc);
309
310   set_node_pto (args [1], arg_pto);
311
312   DBGPRINT (1, (stdout, "fake_main_args():%i (%s[%li])\n",
313                 __LINE__, OPNAME (args [1]), OPNUM (args [1])));
314
315 # ifdef TEST_MAIN_TYPE
316   ctp = get_array_element_type (ctp); /* ctp == char[]* */
317
318   assert (is_Pointer_type (ctp));
319
320   ctp = get_pointer_points_to_type (ctp); /* ctp == char[] */
321
322   assert (is_Array_type (ctp));
323
324   ctp = get_array_element_type (ctp); /* ctp == char */
325
326   assert (is_primitive_type (ctp));
327 # endif /* defined  TEST_MAIN_TYPE */
328
329   /* HERE ("end"); */
330 }
331
332 /* Initialise the Init module */
333 void pto_init_init (void)
334 {
335   pto_obst = xmalloc(sizeof(*pto_obst));
336
337   obstack_init (pto_obst);
338 }
339
340 /* Cleanup the Init module */
341 void pto_init_cleanup (void)
342 {
343   obstack_free (pto_obst, NULL);
344   memset (pto_obst, 0, sizeof (*pto_obst));
345   free (pto_obst);
346   pto_obst = NULL;
347 }
348
349
350 /* Initialise the Names of the Types/Entities */
351 void pto_init_type_names ()
352 {
353   /* HERE ("start"); */
354   type_walk (clear_type_link, NULL, NULL);
355   /* HERE ("end"); */
356 }
357
358 /* Initialise the given graph for a new pass run */
359 void pto_init_graph (ir_graph *graph)
360 {
361   ir_node **proj_args;
362   graph_info_t *ginfo = ecg_get_info (graph);
363   const int n_ctxs = ginfo->n_ctxs;
364
365   /* only for debugging stuff: */
366   entity *ent = get_irg_entity (graph);
367   const char *ent_name = (char*) get_entity_name (ent);
368   const char *own_name = (char*) get_type_name (get_entity_owner (ent));
369
370   DBGPRINT (2, (stdout, "pto_init_graph(): init \"%s.%s\" for %i ctxs\n",
371                 own_name, ent_name, n_ctxs));
372
373   /* HERE ("start"); */
374
375   clear_graph_links     (graph);
376   pto_init_graph_allocs (graph);
377
378   /* HERE ("end"); */
379
380   assert (NULL == get_irg_proj_args (graph));
381   proj_args = find_irg_args (graph);
382   set_irg_proj_args (graph, proj_args);
383   assert (proj_args == get_irg_proj_args (graph));
384 }
385
386 /* Reset the given graph for a new pass run */
387 void pto_reset_graph_pto (ir_graph *graph, int ctx_idx)
388 {
389   reset_env_t reset_env;
390   reset_env.ctx_idx = ctx_idx;
391
392   /* HERE ("start"); */
393
394   irg_walk_graph (graph, reset_node_pto, NULL, &reset_env);
395
396   /* HERE ("end"); */
397 }
398
399 \f
400 /*
401   $Log$
402   Revision 1.14  2005/01/05 14:25:54  beck
403   renames all is_x*_type() functions to is_X*_type() to prevent name clash with EDG fronten
404
405   Revision 1.13  2004/12/21 15:07:55  beck
406   removed C99 contructs
407   removed unnecessary allocation
408   removed use of mode_P, use mode_is_reference() instead
409   removed handling of Const with pointer tarvals, these constructs are removed
410
411   Revision 1.12  2004/12/20 17:41:14  liekweg
412   __unused -> _unused
413
414   Revision 1.11  2004/12/20 17:34:35  liekweg
415   fix recursion handling
416
417   Revision 1.10  2004/12/15 13:31:00  liekweg
418   store ctx idx in names
419
420   Revision 1.9  2004/12/15 09:18:18  liekweg
421   pto_name.c
422
423   Revision 1.8  2004/12/02 16:17:51  beck
424   fixed config.h include
425
426   Revision 1.7  2004/11/30 14:47:54  liekweg
427   fix initialisation; do correct iteration
428
429   Revision 1.6  2004/11/26 16:00:41  liekweg
430   recognize class consts vs. ptr-to-class consts
431
432   Revision 1.5  2004/11/24 14:53:56  liekweg
433   Bugfixes
434
435   Revision 1.4  2004/11/20 21:21:56  liekweg
436   Finalise initialisation
437
438   Revision 1.3  2004/11/18 16:37:07  liekweg
439   rewrite
440
441
442 */