hook_replace() added to exchange
[libfirm] / ir / ir / irprog.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/irprog.c
4  * Purpose:     Entry point to the representation of a whole program.
5  * Author:      Goetz Lindenmaier
6  * Modified by:
7  * Created:     2000
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2000-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16
17 #ifdef HAVE_STRING_H
18 # include <string.h>
19 #endif
20
21 # include "irprog_t.h"
22 # include "irgraph_t.h"
23 # include "pseudo_irg.h"
24 # include "array.h"
25 # include "obst.h"
26 # include "typegmod.h"
27
28 #define GLOBAL_TYPE_NAME "GlobalType"
29 #define INITAL_PROG_NAME "no_name_set"
30
31 /* A variable from where everything in the ir can be accessed. */
32 ir_prog *irp;
33 ir_prog *get_irp(void) { return irp; }
34
35 /**
36  *  Create a new incomplete ir_prog.
37  */
38 static ir_prog *new_incomplete_ir_prog (void) {
39   ir_prog *res;
40
41   res = xmalloc (sizeof(*res));
42   memset(res, 0, sizeof(*res));
43   irp = res;
44
45   res->kind          = k_ir_prog;
46   res->graphs        = NEW_ARR_F(ir_graph *, 0);
47   res->pseudo_graphs = NEW_ARR_F(ir_graph *, 0);
48   res->types         = NEW_ARR_F(type *, 0);
49   res->modes         = NEW_ARR_F(ir_mode *, 0);
50
51 #ifdef DEBUG_libfirm
52   res->max_node_nr = 0;
53 #endif
54
55   return res;
56 }
57
58 /** Completes an incomplete irprog. */
59 static ir_prog *complete_ir_prog(ir_prog *irp) {
60
61   irp->name      = new_id_from_str(INITAL_PROG_NAME);
62
63   irp->glob_type = new_type_class(new_id_from_str (GLOBAL_TYPE_NAME));
64   /* Remove type from type list.  Must be treated differently than
65      other types. */
66   remove_irp_type(irp->glob_type);
67
68   irp->const_code_irg   = new_const_code_irg();
69
70   irp->outs_state       = outs_none;
71   irp->ip_outedges      = NULL;
72   irp->trouts_state     = outs_none;
73   irp->class_cast_state = ir_class_casts_transitive;
74
75   return irp;
76 }
77
78 /* initializes ir_prog. Constructs only the basic lists */
79 void init_irprog_1(void) {
80   new_incomplete_ir_prog();
81 }
82
83 /* Completes ir_prog. */
84 void init_irprog_2(void) {
85   complete_ir_prog(irp);
86 }
87
88 /* Create a new ir prog. Automatically called by init_firm through
89    init_irprog. */
90 ir_prog *new_ir_prog (void) {
91   return complete_ir_prog(new_incomplete_ir_prog());
92 }
93
94 /* frees all memory used by irp.  Types in type list, irgs in irg
95     list and entities in global type must be freed by hand before. */
96 void     free_ir_prog(void) {
97   if (irp->glob_type)
98     free_type(irp->glob_type);
99
100   /* @@@ * free_ir_graph(irp->const_code_irg); * ?? End has no in?? */
101   DEL_ARR_F(irp->graphs);
102   DEL_ARR_F(irp->pseudo_graphs);
103   DEL_ARR_F(irp->types);
104   DEL_ARR_F(irp->modes);
105
106   irp->name           = NULL;
107   irp->const_code_irg = NULL;
108   irp->kind           = k_BAD;
109 }
110
111 /*- Functions to access the fields of ir_prog -*/
112
113
114 /* Access the main routine of the compiled program. */
115 ir_graph *get_irp_main_irg(void) {
116   assert (irp);
117   return irp->main_irg;
118 }
119
120 void set_irp_main_irg(ir_graph *main_irg) {
121   assert (irp);
122   irp->main_irg = main_irg;
123 }
124
125 type *(get_glob_type)(void) {
126   return _get_glob_type();
127 }
128
129 /* Adds irg to the list of ir graphs in irp. */
130 void add_irp_irg(ir_graph *irg) {
131   assert (irg != NULL);
132   assert(irp && irp->graphs);
133   ARR_APP1 (ir_graph *, irp->graphs, irg);
134 }
135
136 /* Removes irg from the list or irgs, shrinks the list by one. */
137 void remove_irp_irg_from_list(ir_graph *irg){
138   int i, found = false;
139   assert(irg);
140   for (i = 0; i < (ARR_LEN (irp->graphs)); i++) {
141     if (irp->graphs[i] == irg) {
142       found = true;
143       for(; i < (ARR_LEN (irp->graphs)) - 1; i++) {
144         irp->graphs[i] = irp->graphs[i+1];
145       }
146       ARR_SETLEN(ir_graph*, irp->graphs, (ARR_LEN(irp->graphs)) - 1);
147       break;
148     }
149   }
150   if (!found) {
151     for (i = 0; i < (ARR_LEN (irp->pseudo_graphs)); i++) {
152       if (irp->pseudo_graphs[i] == irg) {
153               for(; i < (ARR_LEN (irp->pseudo_graphs)) - 1; i++) {
154                 irp->pseudo_graphs[i] = irp->pseudo_graphs[i+1];
155               }
156               ARR_SETLEN(ir_graph*, irp->pseudo_graphs, (ARR_LEN(irp->pseudo_graphs)) - 1);
157               break;
158       }
159     }
160   }
161 }
162
163 /* Removes irg from the list or irgs, shrinks the list by one. */
164 void remove_irp_irg(ir_graph *irg){
165   assert(irg);
166   free_ir_graph(irg);
167   remove_irp_irg_from_list(irg);
168 }
169
170 int (get_irp_n_irgs)(void) {
171   return _get_irp_n_irgs();
172 }
173
174 ir_graph *(get_irp_irg)(int pos){
175   return _get_irp_irg(pos);
176 }
177
178 void set_irp_irg(int pos, ir_graph *irg) {
179   assert (irp && irg);
180   assert (pos < (ARR_LEN((irp)->graphs)));
181   irp->graphs[pos] = irg;
182 }
183
184 /* Gets the number of graphs _and_ pseudo graphs. */
185 int       get_irp_n_allirgs(void) {
186   /* We can not call get_irp_n_irgs, as we end up in a recursion ... */
187   return ARR_LEN((irp)->graphs) + get_irp_n_pseudo_irgs();
188 }
189
190 /* Returns the ir graph at position pos of all graphs (including
191  pseudo graphs).  Visits first graphs, then pseudo graphs. */
192 ir_graph *get_irp_allirg(int pos) {
193   int n_irgs = ARR_LEN((irp)->graphs);
194   assert(0 <= pos);
195   if (pos < n_irgs) {
196     return (irp)->graphs[pos];
197   } else {
198     return get_irp_pseudo_irg(pos-n_irgs);
199   }
200 }
201
202 /* Adds type to the list of types in irp. */
203 void add_irp_type(type *typ) {
204   assert (typ != NULL);
205   assert(irp);
206   ARR_APP1 (type *, irp->types, typ);
207 }
208
209 /* Remove type form the list of types in irp. */
210 void remove_irp_type(type *typ) {
211   int i;
212   assert(typ);
213
214   for (i = ARR_LEN(irp->types) -1; i >= 0; i--) {
215     if (irp->types[i] == typ) {
216       for(; i < (ARR_LEN(irp->types)) - 1; i++) {
217         irp->types[i] = irp->types[i+1];
218       }
219       ARR_SETLEN(type *, irp->types, (ARR_LEN(irp->types)) - 1);
220       break;
221     }
222   }
223 }
224
225 int (get_irp_n_types) (void) {
226   return _get_irp_n_types();
227 }
228
229 type *(get_irp_type) (int pos) {
230   return _get_irp_type(pos);
231 }
232
233 void  set_irp_type(int pos, type *typ) {
234   assert (irp && typ);
235   assert (pos < (ARR_LEN((irp)->types)));
236   irp->types[pos] = typ;
237 }
238
239 /* Returns the number of all modes in the irp. */
240 int (get_irp_n_modes)(void) {
241   return _get_irp_n_modes();
242 }
243
244 /* Returns the mode at position pos in the irp. */
245 ir_mode *(get_irp_mode)(int pos) {
246   return _get_irp_mode(pos);
247 }
248
249 /* Adds mode to the list of modes in irp. */
250 void add_irp_mode(ir_mode *mode) {
251   assert(mode != NULL);
252   assert(irp);
253   ARR_APP1(ir_mode *, irp->modes, mode);
254 }
255
256 #ifdef DEBUG_libfirm
257 int get_irp_new_node_nr() {
258   assert(irp);
259   irp->max_node_nr = irp->max_node_nr + 1;
260   return irp->max_node_nr - 1;
261 }
262 #endif
263
264 /*- File name / executable name or the like -*/
265 void   set_irp_prog_name(ident *name) {
266   irp->name = name;
267 }
268 int irp_prog_name_is_set(void) {
269   return irp->name != new_id_from_str(INITAL_PROG_NAME);
270 }
271 ident *get_irp_prog_ident(void) {
272   return irp->name;
273 }
274 const char  *get_irp_prog_name(void) {
275   return get_id_str(irp->name);
276 }
277
278
279 ir_graph *(get_const_code_irg)(void) {
280   return _get_const_code_irg();
281 }
282
283 irg_phase_state get_irp_phase_state(void) {
284   return irp->phase_state;
285 }
286 void           set_irp_phase_state(irg_phase_state s) {
287   irp->phase_state = s;
288 }
289
290 irg_outs_state get_irp_ip_outs_state(void) {
291   return irp->outs_state;
292 }
293
294 void set_irp_ip_outs_inconsistent(void) {
295   irp->outs_state = outs_inconsistent;
296 }
297
298 void      set_irp_ip_outedges(ir_node ** ip_outedges)
299 {
300   irp->ip_outedges = ip_outedges;
301 }
302
303 ir_node** get_irp_ip_outedges(void)
304 {
305   return irp->ip_outedges;
306 }
307
308
309 irg_callee_info_state get_irp_callee_info_state(void) {
310   return irp->callee_info_state;
311 }
312
313 void set_irp_callee_info_state(irg_callee_info_state s) {
314   irp->callee_info_state = s;
315 }