5 File name: ir/ana/pto_init.c
6 Purpose: Initialisation Functions
9 Created: Sat Nov 13 19:35:27 CET 2004
11 Copyright: (c) 1999-2004 Universität Karlsruhe
12 Licence: This file is protected by the GPL - GNU GENERAL PUBLIC LICENSE.
20 pto_init: Initialisation Functions
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"
33 # include "typewalk.h"
38 # define obstack_chunk_alloc xmalloc
39 # define obstack_chunk_free free
41 /* Local Data Types: */
42 typedef struct init_env_str
47 typedef struct reset_env_str
52 /* Local Variables: */
53 extern struct obstack *qset_obst; /* from pto_name */
55 static struct obstack *pto_obst = NULL; /* all pto_t's go onto this one */
57 /* Local Prototypes: */
59 /* ===================================================
61 =================================================== */
62 /* Allocate a new pto */
63 static pto_t *new_pto (ir_node *node)
65 pto_t *pto = obstack_alloc (pto_obst, sizeof (pto_t));
66 pto->values = qset_new (N_INITIAL_OJBS, qset_obst);
71 /* Allocate a new alloc_pto */
72 static alloc_pto_t *new_alloc_pto (ir_node *alloc, int n_ctxs)
75 alloc_pto_t *alloc_pto = obstack_alloc (pto_obst, sizeof (alloc_pto_t));
78 assert (op_Alloc == get_irn_op(alloc));
80 tp = get_Alloc_type (alloc);
82 alloc_pto->ptos = (pto_t**) obstack_alloc (pto_obst, n_ctxs * sizeof (pto_t*));
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);
93 /* Allocate a new pto for a symconst */
94 static pto_t* new_symconst_pto (ir_node *symconst)
100 assert (op_SymConst == get_irn_op(symconst));
102 pto = new_pto (symconst);
103 ent = get_SymConst_entity (symconst);
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);
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);
119 fprintf (stderr, "%s: not handled: %s[%li] (\"%s\")\n",
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");
127 qset_insert (pto->values, desc);
129 /* HERE3 ("end", own_name, ent_name); */
134 /* Helper to pto_init --- clear the link fields of class types */
135 static void clear_type_link (type_or_ent *thing, void *_unused)
137 if (is_type (thing)) {
138 type *tp = (type*) thing;
140 if (is_Class_type (tp)) {
141 DBGPRINT (1, (stdout, "%s (\"%s\")\n",
143 get_type_name (tp)));
145 set_type_link (tp, NULL);
147 } else if (is_entity (thing)) {
148 entity *ent = (entity*) thing;
150 DBGPRINT (1, (stdout, "%s (\"%s\")\n",
152 get_entity_name (ent)));
154 set_entity_link (ent, NULL);
158 /* Helper to pto_init_graph --- clear the links of the given node */
159 static void clear_node_link (ir_node *node, void *_unused)
161 set_irn_link (node, NULL);
164 /* Helper to pto_init_graph --- clear the links of all nodes */
165 static void clear_graph_links (ir_graph *graph)
167 irg_walk_graph (graph, clear_node_link, NULL, NULL);
170 /* Reset ALL the pto values for a new pass */
171 static void reset_node_pto (ir_node *node, void *env)
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);
177 /* HERE ("start"); */
182 case (iro_Block): /* END BLOCK only */
184 /* allocate 'empty' pto values */
185 pto_t *pto = new_pto (node);
186 set_node_pto (node, pto);
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];
194 DBGPRINT (1, (stdout, "%s: setting pto of \"%s[%li]\" for ctx %i\n",
200 assert (alloc_pto->curr_pto);
203 case (iro_SymConst): {
204 /* nothing, leave as-is */
208 /* basically, nothing */
209 DBGPRINT (2, (stdout, "%s: resetting pto of \"%s[%li]\"\n",
213 set_node_pto (node, NULL);
220 /* Initialise primary name sources */
221 static void init_pto (ir_node *node, void *env)
223 init_env_t *init_env = (init_env_t*) env;
224 int n_ctxs = init_env->n_ctxs;
226 opcode op = get_irn_opcode (node);
229 case (iro_SymConst): {
230 if (mode_is_reference (get_irn_mode (node))) {
231 entity *ent = get_SymConst_entity (node);
232 type *tp = get_entity_type (ent);
233 if (is_Class_type (tp) || is_Pointer_type (tp)) {
234 pto_t *symconst_pto = new_symconst_pto (node);
235 set_node_pto (node, symconst_pto);
238 DBGPRINT (1, (stdout, "%s: new name \"%s\" for \"%s[%li]\"\n",
240 get_entity_name (ent),
248 alloc_pto_t *alloc_pto = new_alloc_pto (node, n_ctxs);
251 set_alloc_pto (node, alloc_pto);
253 tp = get_Alloc_type (node); /* debugging only */
254 DBGPRINT (1, (stdout, "%s: %i names \"%s\" for \"%s[%li]\"\n",
263 tarval *tv = get_Const_tarval (node);
265 /* only need 'NULL' pointer constants */
266 if (mode_P == get_tarval_mode (tv)) {
267 if (get_tarval_null (mode_P) == tv) {
268 pto_t *pto = new_pto (node);
269 set_node_pto (node, pto);
276 /* nothing --- handled by reset_node_pto on each pass */
285 /* Initialise the given graph for a new pass run */
286 static void pto_init_graph_allocs (ir_graph *graph)
288 graph_info_t *ginfo = ecg_get_info (graph);
289 init_env_t *init_env;
291 init_env = xmalloc (sizeof (init_env_t));
292 init_env->n_ctxs = ginfo->n_ctxs;
294 /* HERE ("start"); */
296 irg_walk_graph (graph, init_pto, NULL, init_env);
299 memset (init_env, 0x00, sizeof (init_env_t));
303 /* ===================================================
304 Exported Implementation:
305 =================================================== */
306 /* "Fake" the arguments to the main method */
307 void fake_main_args (ir_graph *graph)
309 /* HERE ("start"); */
311 entity *ent = get_irg_entity (graph);
312 type *mtp = get_entity_type (ent);
313 ir_node **args = find_irg_args (graph);
314 type *ctp = get_method_param_type (mtp, 1); /* ctp == char[]*[]* */
318 /* 'main' has signature 'void(int, char[]*[]*)' */
319 assert (NULL == args [2]);
321 assert (is_Pointer_type (ctp));
323 ctp = get_pointer_points_to_type (ctp); /* ctp == char[]*[] */
325 assert (is_Array_type (ctp));
327 arg_desc = new_name (ctp, args [1], -1);
328 arg_pto = new_pto (args [1]);
329 /* todo: simulate 'store' to arg1[] ?!? */
330 qset_insert (arg_pto->values, arg_desc);
332 set_node_pto (args [1], arg_pto);
334 DBGPRINT (1, (stdout, "%s:%i (%s[%li])\n",
335 __FUNCTION__, __LINE__,
336 OPNAME (args [1]), OPNUM (args [1])));
338 # ifdef TEST_MAIN_TYPE
339 ctp = get_array_element_type (ctp); /* ctp == char[]* */
341 assert (is_Pointer_type (ctp));
343 ctp = get_pointer_points_to_type (ctp); /* ctp == char[] */
345 assert (is_Array_type (ctp));
347 ctp = get_array_element_type (ctp); /* ctp == char */
349 assert (is_primitive_type (ctp));
350 # endif /* defined TEST_MAIN_TYPE */
355 /* Initialise the Init module */
356 void pto_init_init ()
358 pto_obst = (struct obstack*) xmalloc (sizeof (struct obstack));
360 obstack_init (pto_obst);
363 /* Cleanup the Init module */
364 void pto_init_cleanup ()
366 obstack_free (pto_obst, NULL);
367 memset (pto_obst, 0x00, sizeof (struct obstack));
373 /* Initialise the Names of the Types/Entities */
374 void pto_init_type_names ()
376 /* HERE ("start"); */
377 type_walk (clear_type_link, NULL, NULL);
381 /* Initialise the given graph for a new pass run */
382 void pto_init_graph (ir_graph *graph)
385 graph_info_t *ginfo = ecg_get_info (graph);
386 const int n_ctxs = ginfo->n_ctxs;
388 /* only for debugging stuff: */
389 entity *ent = get_irg_entity (graph);
390 const char *ent_name = (char*) get_entity_name (ent);
391 const char *own_name = (char*) get_type_name (get_entity_owner (ent));
393 DBGPRINT (2, (stdout, "%s: init \"%s.%s\" for %i ctxs\n",
395 own_name, ent_name, n_ctxs));
397 /* HERE ("start"); */
399 clear_graph_links (graph);
400 pto_init_graph_allocs (graph);
404 assert (NULL == get_irg_proj_args (graph));
405 proj_args = find_irg_args (graph);
406 set_irg_proj_args (graph, proj_args);
407 assert (proj_args == get_irg_proj_args (graph));
410 /* Reset the given graph for a new pass run */
411 void pto_reset_graph_pto (ir_graph *graph, int ctx_idx)
413 reset_env_t *reset_env;
415 reset_env = (reset_env_t*) xmalloc (sizeof (reset_env_t));
416 reset_env->ctx_idx = ctx_idx;
418 /* HERE ("start"); */
420 irg_walk_graph (graph, reset_node_pto, NULL, reset_env);
423 memset (reset_env, 0x00, sizeof (reset_env_t));
430 Revision 1.16 2005/01/14 13:36:50 liekweg
431 don't put environments on the stack; handle consts
433 Revision 1.15 2005/01/10 17:26:34 liekweg
434 fixup printfs, don't put environments on the stack
436 Revision 1.14 2005/01/05 14:25:54 beck
437 renames all is_x*_type() functions to is_X*_type() to prevent name clash with EDG fronten
439 Revision 1.13 2004/12/21 15:07:55 beck
440 removed C99 contructs
441 removed unnecessary allocation
442 removed use of mode_P, use mode_is_reference() instead
443 removed handling of Const with pointer tarvals, these constructs are removed
445 Revision 1.12 2004/12/20 17:41:14 liekweg
448 Revision 1.11 2004/12/20 17:34:35 liekweg
449 fix recursion handling
451 Revision 1.10 2004/12/15 13:31:00 liekweg
452 store ctx idx in names
454 Revision 1.9 2004/12/15 09:18:18 liekweg
457 Revision 1.8 2004/12/02 16:17:51 beck
458 fixed config.h include
460 Revision 1.7 2004/11/30 14:47:54 liekweg
461 fix initialisation; do correct iteration
463 Revision 1.6 2004/11/26 16:00:41 liekweg
464 recognize class consts vs. ptr-to-class consts
466 Revision 1.5 2004/11/24 14:53:56 liekweg
469 Revision 1.4 2004/11/20 21:21:56 liekweg
470 Finalise initialisation
472 Revision 1.3 2004/11/18 16:37:07 liekweg