recognize class consts vs. ptr-to-class consts
[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   switch (op) {
187   case (iro_Load):
188     /* case (iro_SymConst): */ /* WHY? */
189   case (iro_Const):
190   case (iro_Call):
191   case (iro_Block):             /* END BLOCK only */
192   case (iro_Phi): {
193     /* allocate 'empty' pto values */
194     pto_t *pto = new_pto (node);
195     set_node_pto (node, pto);
196     } break;
197
198   case (iro_Alloc): {
199     /* set alloc to 'right' current pto */
200     alloc_pto_t *alloc_pto = (alloc_pto_t*) get_irn_link (node);
201     alloc_pto->curr_pto = alloc_pto->ptos [ctx_idx];
202     } break;
203
204   default: {
205     /* nothing */
206   } break;
207   }
208 }
209
210 /* Temporary fix until we get 'real' ptos: Allocate some dummy for pto */
211 static void init_pto (ir_node *node, void *env)
212 {
213   init_env_t *init_env = (init_env_t*) env;
214   int n_ctxs = init_env->n_ctxs;
215
216   const opcode op = get_irn_opcode (node);
217
218   switch (op) {
219   case (iro_SymConst): {
220     if (mode_P == get_irn_mode (node)) {
221       /* debugging only */
222       entity *ent = get_SymConst_entity (node);
223       if (is_class_type (get_entity_type (ent)) ||
224           is_pointer_type (get_entity_type (ent))) {
225         DBGPRINT (0, (stdout, "%s: new name \"%s\" for \"%s[%li]\"\n",
226                       __FUNCTION__,
227                       get_entity_name (ent),
228                       OPNAME (node),
229                       OPNUM (node)));
230
231         pto_t *symconst_pto = new_symconst_pto (node);
232         set_node_pto (node, symconst_pto);
233       }
234     }
235   } break;
236   case (iro_Alloc): {
237     type *tp = get_Alloc_type (node); /* debugging only */
238     alloc_pto_t *alloc_pto = new_alloc_pto (node, n_ctxs);
239     set_alloc_pto (node, alloc_pto);
240
241     DBGPRINT (0, (stdout, "%s: %i names \"%s\" for \"%s[%li]\"\n",
242                   __FUNCTION__,
243                   n_ctxs,
244                   get_type_name (tp),
245                   OPNAME (node),
246                   OPNUM (node)));
247   } break;
248
249   case (iro_Const): {
250     if (mode_P == get_irn_mode (node)) {
251       pto_t *pto = new_const_pto (node);
252       set_node_pto (node, pto);
253     }
254   } break;
255
256   case (iro_Load):
257   case (iro_Call):
258   case (iro_Phi):
259     /* nothing --- handled by reset_node_pto on each pass */
260     break;
261   default: {
262     /* nothing */
263   } break;
264   }
265 }
266
267
268 /* Initialise the given graph for a new pass run */
269 static void pto_init_graph_allocs (ir_graph *graph)
270 {
271   graph_info_t *ginfo = ecg_get_info (graph);
272   int n_ctxs = ginfo->n_ctxs;
273
274   init_env_t *init_env = xmalloc (sizeof (init_env_t));
275   init_env->n_ctxs = n_ctxs;
276
277   HERE ("start");
278
279   irg_walk_graph (graph, init_pto, NULL, init_env);
280
281   memset (init_env, 0x00, sizeof (init_env_t));
282   free (init_env);
283
284   HERE ("end");
285 }
286
287 /* ===================================================
288    Exported Implementation:
289    =================================================== */
290 /* "Fake" the arguments to the main method */
291 void fake_main_args (ir_graph *graph)
292 {
293   HERE ("start");
294
295   entity *ent = get_irg_entity (graph);
296   type *mtp = get_entity_type (ent);
297   ir_node **args = find_irg_args (graph);
298   type *ctp = get_method_param_type (mtp, 1); /* ctp == char[]*[]* */
299
300   /* 'main' has signature 'void(int, char[]*[]*)' */
301   assert (NULL == args [2]);
302
303   assert (is_pointer_type (ctp));
304
305   ctp = get_pointer_points_to_type (ctp); /* ctp == char[]*[] */
306
307   assert (is_array_type (ctp));
308
309   desc_t *arg_desc = new_name (ctp, args [1]);
310   pto_t *arg_pto = new_pto (args [1]);
311   /* todo: simulate 'store' to arg1[] ?!? */
312   qset_insert (arg_pto->values, arg_desc);
313
314   set_node_pto (args [1], arg_pto);
315
316 # ifdef TEST_MAIN_TYPE
317   ctp = get_array_element_type (ctp); /* ctp == char[]* */
318
319   assert (is_pointer_type (ctp));
320
321   ctp = get_pointer_points_to_type (ctp); /* ctp == char[] */
322
323   assert (is_array_type (ctp));
324
325   ctp = get_array_element_type (ctp); /* ctp == char */
326
327   assert (is_primitive_type (ctp));
328 # endif /* defined  TEST_MAIN_TYPE */
329
330   HERE ("end");
331 }
332
333 /* Initialise the Init module */
334 void pto_init_init ()
335 {
336   pto_obst = (struct obstack*) xmalloc (sizeof (struct obstack));
337
338   obstack_init (pto_obst);
339 }
340
341 /* Cleanup the Init module */
342 void pto_init_cleanup ()
343 {
344   obstack_free (pto_obst, NULL);
345   memset (pto_obst, 0x00, sizeof (struct obstack));
346   free (pto_obst);
347   pto_obst = NULL;
348 }
349
350
351 /* Initialise the Names of the Types/Entities */
352 void pto_init_type_names ()
353 {
354   HERE ("start");
355   type_walk (clear_type_link, NULL, NULL);
356   HERE ("end");
357 }
358
359 /* Initialise the given graph for a new pass run */
360 void pto_init_graph (ir_graph *graph)
361 {
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 (0, (stdout, "%s: init \"%s.%s\" for %i ctxs\n", __FUNCTION__,
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
381 /* Reset the given graph for a new pass run */
382 void pto_reset_graph_pto (ir_graph *graph, int ctx_idx)
383 {
384   HERE ("start");
385
386   reset_env_t *reset_env = (reset_env_t*) xmalloc (sizeof (reset_env_t));
387   reset_env->ctx_idx = ctx_idx;
388
389   irg_walk_graph (graph, reset_node_pto, NULL, reset_env);
390
391   memset (reset_env, 0x00, sizeof (reset_env_t));
392   free (reset_env);
393   HERE ("end");
394 }
395
396 \f
397 /*
398   $Log$
399   Revision 1.6  2004/11/26 16:00:41  liekweg
400   recognize class consts vs. ptr-to-class consts
401
402   Revision 1.5  2004/11/24 14:53:56  liekweg
403   Bugfixes
404
405   Revision 1.4  2004/11/20 21:21:56  liekweg
406   Finalise initialisation
407
408   Revision 1.3  2004/11/18 16:37:07  liekweg
409   rewrite
410
411
412 */