Finalise initialisation
[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 "pto_init.h"
24 # include "pto_debug.h"
25 # include "pto_comp.h"
26
27 # include "typewalk.h"
28 # include "irgwalk.h"
29 # include "xmalloc.h"
30
31 /* Local Defines: */
32
33 /* Local Data Types: */
34 typedef struct init_env_str
35 {
36   int n_ctxs;
37 } init_env_t;
38
39 typedef struct reset_env_str
40 {
41   int ctx_idx;
42 } reset_env_t;
43
44 /* Local Variables: */
45
46 /* Local Prototypes: */
47
48 /* ===================================================
49    Local Implementation:
50    =================================================== */
51 /* Allocate a new pto */
52 static pto_t *new_pto (ir_node *node)
53 {
54   /* dummy implementation for fake_pto */
55   pto_t *pto = xmalloc (sizeof (pto_t));
56
57   return (pto);
58 }
59
60 /* Allocate a new alloc_pto */
61 static alloc_pto_t *new_alloc_pto (ir_node *node, int n_ctxs)
62 {
63   int i;
64   /* dummy implementation for testing */
65   alloc_pto_t *alloc_pto = xmalloc (sizeof (alloc_pto_t));
66
67   alloc_pto->ptos = (pto_t**) xmalloc (n_ctxs * sizeof (pto_t*));
68
69   for (i = 0; i < n_ctxs; i ++) {
70     alloc_pto->ptos [i] = new_pto (node);
71   }
72
73   return (alloc_pto);
74 }
75
76
77 /* Helper to pto_init --- clear the link fields of class types */
78 static void clear_type_link (type_or_ent *thing, void *__unused)
79 {
80   if (is_type (thing)) {
81     type *tp = (type*) thing;
82
83     if (is_class_type (tp)) {
84       DBGPRINT (1, (stdout, "%s (\"%s\")\n",
85                     __FUNCTION__, get_type_name (tp)));
86
87       set_type_link (tp, NULL);
88     }
89   } else if (is_entity (thing)) {
90     entity *ent = (entity*) thing;
91
92     DBGPRINT (1, (stdout, "%s (\"%s\")\n",
93                   __FUNCTION__, get_entity_name (ent)));
94
95     set_entity_link (ent, NULL);
96   }
97 }
98
99 /* Helper to pto_init_graph --- clear the links of the given node */
100 static void clear_node_link (ir_node *node, void *__unused)
101 {
102   set_irn_link (node, NULL);
103 }
104
105 /* Helper to pto_init_graph --- clear the links of all nodes */
106 static void clear_graph_links (ir_graph *graph)
107 {
108   irg_walk_graph (graph, clear_node_link, NULL, NULL);
109 }
110
111 /* Reset ALL the pto values for a new pass */
112 static void reset_node_pto (ir_node *node, void *env)
113 {
114   reset_env_t *reset_env = (reset_env_t*) env;
115   int ctx_idx = reset_env->ctx_idx;
116   const opcode op = get_irn_opcode (node);
117
118   switch (op) {
119   case (iro_Load):
120   case (iro_SymConst):
121   case (iro_Const):
122   case (iro_Call):
123   case (iro_Phi): {
124     /* todo: allocate 'empty' pto values */
125     pto_t *pto = new_pto (node);
126     set_node_pto (node, pto);
127     } break;
128
129   case (iro_Alloc): {
130     /* todo: set alloc to 'right' current pto */
131     alloc_pto_t *alloc_pto = (alloc_pto_t*) get_irn_link (node);
132     alloc_pto->curr_pto = alloc_pto->ptos [ctx_idx];
133     } break;
134
135   default: {
136     /* nothing */
137   } break;
138   }
139 }
140
141 /* Temporary fix until we get 'real' ptos: Allocate some dummy for pto */
142 static void init_alloc_pto (ir_node *node, void *env)
143 {
144   init_env_t *init_env = (init_env_t*) env;
145   int n_ctxs = init_env->n_ctxs;
146
147   const opcode op = get_irn_opcode (node);
148
149   switch (op) {
150   case (iro_Load):
151   case (iro_SymConst):
152   case (iro_Const):
153   case (iro_Call):
154   case (iro_Phi): {
155     /* nothing (handled by init_node_pto) */
156     } break;
157
158   case (iro_Alloc): {
159     /* todo: alloc 'right' ptos */
160     alloc_pto_t *alloc_pto = new_alloc_pto (node, n_ctxs);
161     set_alloc_pto (node, alloc_pto);
162
163     } break;
164   default: {
165     /* nothing */
166   } break;
167   }
168 }
169
170
171 /* Initialise the given graph for a new pass run */
172 static void pto_init_graph_allocs (ir_graph *graph)
173 {
174   graph_info_t *ginfo = ecg_get_info (graph);
175   int n_ctxs = ginfo->n_ctxs;
176
177   init_env_t *init_env = xmalloc (sizeof (init_env_t));
178   init_env->n_ctxs = n_ctxs;
179
180   HERE ("start");
181
182   irg_walk_graph (graph, init_alloc_pto, NULL, init_env);
183
184   HERE ("end");
185 }
186
187 /* ===================================================
188    Exported Implementation:
189    =================================================== */
190 /* Initialise the Names of the Types/Entities */
191 void pto_init_type_names ()
192 {
193   HERE ("start");
194   type_walk (clear_type_link, NULL, NULL);
195   HERE ("end");
196 }
197
198 /* Initialise the given graph for a new pass run */
199 void pto_init_graph (ir_graph *graph)
200 {
201   graph_info_t *ginfo = ecg_get_info (graph);
202   const int n_ctxs = ginfo->n_ctxs;
203
204   /* only for debugging stuff: */
205   entity *ent = get_irg_entity (graph);
206   const char *ent_name = (char*) get_entity_name (ent);
207   const char *own_name = (char*) get_type_name (get_entity_owner (ent));
208
209   DBGPRINT (0, (stdout, "%s: init \"%s.%s\" for %i ctxs\n", __FUNCTION__,
210                 own_name, ent_name, n_ctxs));
211
212   HERE ("start");
213
214   clear_graph_links     (graph);
215   pto_init_graph_allocs (graph);
216
217   HERE ("end");
218 }
219
220 /* Reset the given graph for a new pass run */
221 void pto_reset_graph_pto (ir_graph *graph, int ctx_idx)
222 {
223   HERE ("start");
224
225   reset_env_t *reset_env = (reset_env_t*) xmalloc (sizeof (reset_env_t));
226   reset_env->ctx_idx = ctx_idx;
227
228   irg_walk_graph (graph, reset_node_pto, NULL, reset_env);
229
230   HERE ("end");
231 }
232
233 \f
234 /*
235   $Log$
236   Revision 1.4  2004/11/20 21:21:56  liekweg
237   Finalise initialisation
238
239   Revision 1.3  2004/11/18 16:37:07  liekweg
240   rewrite
241
242
243 */