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)
74 assert (iro_Alloc == get_irn_opcode (alloc));
77 alloc_pto_t *alloc_pto = obstack_alloc (pto_obst, sizeof (alloc_pto_t));
78 type *tp = get_Alloc_type (alloc);
80 alloc_pto->ptos = (pto_t**) obstack_alloc (pto_obst, n_ctxs * sizeof (pto_t*));
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);
91 /* Allocate a new pto for a symconst */
92 static pto_t* new_symconst_pto (ir_node *symconst)
94 assert (iro_SymConst == get_irn_opcode (symconst));
96 pto_t *pto = new_pto (symconst);
97 entity *ent = get_SymConst_entity (symconst);
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);
109 fprintf (stderr, "%s: not handled: %s[%li] (\"%s\")\n",
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");
117 qset_insert (pto->values, desc);
122 /* Allocate a new pto for a constant */
123 static pto_t *new_const_pto (ir_node *cnst)
125 assert (iro_Const == get_irn_opcode (cnst));
126 assert (mode_P == get_irn_mode (cnst));
128 static pto_t *cnst_pto = NULL;
130 /* since 'store's and 'load's via a NULL pointer are hardly ever
131 successful, we get away with an empty set */
133 tarval *tv = get_Const_tarval (cnst);
135 assert (tv == get_tarval_null (mode_P));
137 if (NULL == cnst_pto) {
138 cnst_pto = new_pto (cnst);
145 /* Helper to pto_init --- clear the link fields of class types */
146 static void clear_type_link (type_or_ent *thing, void *__unused)
148 if (is_type (thing)) {
149 type *tp = (type*) thing;
151 if (is_class_type (tp)) {
152 DBGPRINT (1, (stdout, "%s (\"%s\")\n",
153 __FUNCTION__, get_type_name (tp)));
155 set_type_link (tp, NULL);
157 } else if (is_entity (thing)) {
158 entity *ent = (entity*) thing;
160 DBGPRINT (1, (stdout, "%s (\"%s\")\n",
161 __FUNCTION__, get_entity_name (ent)));
163 set_entity_link (ent, NULL);
167 /* Helper to pto_init_graph --- clear the links of the given node */
168 static void clear_node_link (ir_node *node, void *__unused)
170 set_irn_link (node, NULL);
173 /* Helper to pto_init_graph --- clear the links of all nodes */
174 static void clear_graph_links (ir_graph *graph)
176 irg_walk_graph (graph, clear_node_link, NULL, NULL);
179 /* Reset ALL the pto values for a new pass */
180 static void reset_node_pto (ir_node *node, void *env)
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);
186 /* HERE ("start"); */
190 /* case (iro_Const): */
192 case (iro_Block): /* END BLOCK only */
194 /* allocate 'empty' pto values */
195 pto_t *pto = new_pto (node);
196 set_node_pto (node, pto);
200 /* set alloc to 'right' current pto */
202 /* HERE ("alloc"); */
204 alloc_pto_t *alloc_pto = (alloc_pto_t*) get_irn_link (node);
205 alloc_pto->curr_pto = alloc_pto->ptos [ctx_idx];
207 DBGPRINT (1, (stdout, "%s: setting pto of \"%s[%li]\" for ctx %i\n",
213 assert (alloc_pto->curr_pto);
216 case (iro_SymConst): {
217 /* nothing, leave as-is */
221 /* basically, nothing */
222 DBGPRINT (2, (stdout, "%s: resetting pto of \"%s[%li]\"\n",
226 set_node_pto (node, NULL);
233 /* Temporary fix until we get 'real' ptos: Allocate some dummy for pto */
234 static void init_pto (ir_node *node, void *env)
236 init_env_t *init_env = (init_env_t*) env;
237 int n_ctxs = init_env->n_ctxs;
239 const opcode op = get_irn_opcode (node);
242 case (iro_SymConst): {
243 if (mode_P == get_irn_mode (node)) {
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",
250 get_entity_name (ent),
254 pto_t *symconst_pto = new_symconst_pto (node);
255 set_node_pto (node, symconst_pto);
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);
264 DBGPRINT (1, (stdout, "%s: %i names \"%s\" for \"%s[%li]\"\n",
273 if (mode_P == get_irn_mode (node)) {
274 pto_t *pto = new_const_pto (node);
275 set_node_pto (node, pto);
282 /* nothing --- handled by reset_node_pto on each pass */
291 /* Initialise the given graph for a new pass run */
292 static void pto_init_graph_allocs (ir_graph *graph)
294 graph_info_t *ginfo = ecg_get_info (graph);
295 int n_ctxs = ginfo->n_ctxs;
297 init_env_t *init_env = xmalloc (sizeof (init_env_t));
298 init_env->n_ctxs = n_ctxs;
300 /* HERE ("start"); */
302 irg_walk_graph (graph, init_pto, NULL, init_env);
304 memset (init_env, 0x00, sizeof (init_env_t));
310 /* ===================================================
311 Exported Implementation:
312 =================================================== */
313 /* "Fake" the arguments to the main method */
314 void fake_main_args (ir_graph *graph)
316 /* HERE ("start"); */
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[]*[]* */
323 /* 'main' has signature 'void(int, char[]*[]*)' */
324 assert (NULL == args [2]);
326 assert (is_pointer_type (ctp));
328 ctp = get_pointer_points_to_type (ctp); /* ctp == char[]*[] */
330 assert (is_array_type (ctp));
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);
337 set_node_pto (args [1], arg_pto);
339 DBGPRINT (1, (stdout, "%s:%i (%s[%li])\n",
340 __FUNCTION__, __LINE__, OPNAME (args [1]), OPNUM (args [1])));
342 # ifdef TEST_MAIN_TYPE
343 ctp = get_array_element_type (ctp); /* ctp == char[]* */
345 assert (is_pointer_type (ctp));
347 ctp = get_pointer_points_to_type (ctp); /* ctp == char[] */
349 assert (is_array_type (ctp));
351 ctp = get_array_element_type (ctp); /* ctp == char */
353 assert (is_primitive_type (ctp));
354 # endif /* defined TEST_MAIN_TYPE */
359 /* Initialise the Init module */
360 void pto_init_init ()
362 pto_obst = (struct obstack*) xmalloc (sizeof (struct obstack));
364 obstack_init (pto_obst);
367 /* Cleanup the Init module */
368 void pto_init_cleanup ()
370 obstack_free (pto_obst, NULL);
371 memset (pto_obst, 0x00, sizeof (struct obstack));
377 /* Initialise the Names of the Types/Entities */
378 void pto_init_type_names ()
380 /* HERE ("start"); */
381 type_walk (clear_type_link, NULL, NULL);
385 /* Initialise the given graph for a new pass run */
386 void pto_init_graph (ir_graph *graph)
388 graph_info_t *ginfo = ecg_get_info (graph);
389 const int n_ctxs = ginfo->n_ctxs;
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));
396 DBGPRINT (1, (stdout, "%s: init \"%s.%s\" for %i ctxs\n", __FUNCTION__,
397 own_name, ent_name, n_ctxs));
399 /* HERE ("start"); */
401 clear_graph_links (graph);
402 pto_init_graph_allocs (graph);
407 /* Reset the given graph for a new pass run */
408 void pto_reset_graph_pto (ir_graph *graph, int ctx_idx)
410 /* HERE ("start"); */
412 reset_env_t *reset_env = (reset_env_t*) xmalloc (sizeof (reset_env_t));
413 reset_env->ctx_idx = ctx_idx;
415 irg_walk_graph (graph, reset_node_pto, NULL, reset_env);
417 memset (reset_env, 0x00, sizeof (reset_env_t));
425 Revision 1.7 2004/11/30 14:47:54 liekweg
426 fix initialisation; do correct iteration
428 Revision 1.6 2004/11/26 16:00:41 liekweg
429 recognize class consts vs. ptr-to-class consts
431 Revision 1.5 2004/11/24 14:53:56 liekweg
434 Revision 1.4 2004/11/20 21:21:56 liekweg
435 Finalise initialisation
437 Revision 1.3 2004/11/18 16:37:07 liekweg