removed critical_edges flag. Other code depends on remove_critical_edges() doing...
[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 # include "irop_t.h"
28
29 #define GLOBAL_TYPE_NAME "GlobalType"
30 #define INITAL_PROG_NAME "no_name_set"
31
32 /* A variable from where everything in the ir can be accessed. */
33 ir_prog *irp;
34 ir_prog *get_irp(void) { return irp; }
35
36 /**
37  *  Create a new incomplete ir_prog.
38  */
39 static ir_prog *new_incomplete_ir_prog (void) {
40   ir_prog *res;
41
42   res = xmalloc (sizeof(*res));
43   memset(res, 0, sizeof(*res));
44   irp = res;
45
46   res->kind          = k_ir_prog;
47   res->graphs        = NEW_ARR_F(ir_graph *, 0);
48   res->pseudo_graphs = NEW_ARR_F(ir_graph *, 0);
49   res->types         = NEW_ARR_F(ir_type *, 0);
50   res->modes         = NEW_ARR_F(ir_mode *, 0);
51   res->opcodes       = NEW_ARR_F(ir_op *, 0);
52
53 #ifdef DEBUG_libfirm
54   res->max_node_nr = 0;
55 #endif
56
57   return res;
58 }
59
60 /** Completes an incomplete irprog. */
61 static ir_prog *complete_ir_prog(ir_prog *irp) {
62
63   irp->name      = new_id_from_str(INITAL_PROG_NAME);
64
65   irp->glob_type = new_type_class(new_id_from_str (GLOBAL_TYPE_NAME));
66   /* Remove type from type list.  Must be treated differently than
67      other types. */
68   remove_irp_type(irp->glob_type);
69
70   irp->const_code_irg   = new_const_code_irg();
71
72   irp->outs_state       = outs_none;
73   irp->ip_outedges      = NULL;
74   irp->trouts_state     = outs_none;
75   irp->class_cast_state = ir_class_casts_transitive;
76
77   return irp;
78 }
79
80 /* initializes ir_prog. Constructs only the basic lists */
81 void init_irprog_1(void) {
82   new_incomplete_ir_prog();
83 }
84
85 /* Completes ir_prog. */
86 void init_irprog_2(void) {
87   complete_ir_prog(irp);
88 }
89
90 /* Create a new ir prog. Automatically called by init_firm through
91    init_irprog. */
92 ir_prog *new_ir_prog (void) {
93   return complete_ir_prog(new_incomplete_ir_prog());
94 }
95
96 /* frees all memory used by irp.  Types in type list, irgs in irg
97     list and entities in global type must be freed by hand before. */
98 void     free_ir_prog(void) {
99   if (irp->glob_type)
100     free_type(irp->glob_type);
101
102   /* @@@ * free_ir_graph(irp->const_code_irg); * ?? End has no in?? */
103   DEL_ARR_F(irp->graphs);
104   DEL_ARR_F(irp->pseudo_graphs);
105   DEL_ARR_F(irp->types);
106   DEL_ARR_F(irp->modes);
107   DEL_ARR_F(irp->opcodes);
108
109   irp->name           = NULL;
110   irp->const_code_irg = NULL;
111   irp->kind           = k_BAD;
112 }
113
114 /*- Functions to access the fields of ir_prog -*/
115
116
117 /* Access the main routine of the compiled program. */
118 ir_graph *get_irp_main_irg(void) {
119   assert (irp);
120   return irp->main_irg;
121 }
122
123 void set_irp_main_irg(ir_graph *main_irg) {
124   assert (irp);
125   irp->main_irg = main_irg;
126 }
127
128 ir_type *(get_glob_type)(void) {
129   return _get_glob_type();
130 }
131
132 /* Adds irg to the list of ir graphs in irp. */
133 void add_irp_irg(ir_graph *irg) {
134   assert (irg != NULL);
135   assert(irp && irp->graphs);
136   ARR_APP1 (ir_graph *, irp->graphs, irg);
137 }
138
139 /* Removes irg from the list or irgs, shrinks the list by one. */
140 void remove_irp_irg_from_list(ir_graph *irg){
141   int i, found = 0;
142   assert(irg);
143   for (i = 0; i < (ARR_LEN (irp->graphs)); i++) {
144     if (irp->graphs[i] == irg) {
145       found = 1;
146       for(; i < (ARR_LEN (irp->graphs)) - 1; i++) {
147         irp->graphs[i] = irp->graphs[i+1];
148       }
149       ARR_SETLEN(ir_graph*, irp->graphs, (ARR_LEN(irp->graphs)) - 1);
150       break;
151     }
152   }
153   if (!found) {
154     for (i = 0; i < (ARR_LEN (irp->pseudo_graphs)); i++) {
155       if (irp->pseudo_graphs[i] == irg) {
156               for(; i < (ARR_LEN (irp->pseudo_graphs)) - 1; i++) {
157                 irp->pseudo_graphs[i] = irp->pseudo_graphs[i+1];
158               }
159               ARR_SETLEN(ir_graph*, irp->pseudo_graphs, (ARR_LEN(irp->pseudo_graphs)) - 1);
160               break;
161       }
162     }
163   }
164 }
165
166 /* Removes irg from the list or irgs, shrinks the list by one. */
167 void remove_irp_irg(ir_graph *irg){
168   assert(irg);
169   free_ir_graph(irg);
170   remove_irp_irg_from_list(irg);
171 }
172
173 int (get_irp_n_irgs)(void) {
174   return _get_irp_n_irgs();
175 }
176
177 ir_graph *(get_irp_irg)(int pos){
178   return _get_irp_irg(pos);
179 }
180
181 void set_irp_irg(int pos, ir_graph *irg) {
182   assert (irp && irg);
183   assert (pos < (ARR_LEN(irp->graphs)));
184   irp->graphs[pos] = irg;
185 }
186
187 /* Gets the number of graphs _and_ pseudo graphs. */
188 int       get_irp_n_allirgs(void) {
189   /* We can not call get_irp_n_irgs, as we end up in a recursion ... */
190   return ARR_LEN(irp->graphs) + get_irp_n_pseudo_irgs();
191 }
192
193 /* Returns the ir graph at position pos of all graphs (including
194  pseudo graphs).  Visits first graphs, then pseudo graphs. */
195 ir_graph *get_irp_allirg(int pos) {
196   int n_irgs = ARR_LEN(irp->graphs);
197   assert(0 <= pos);
198   if (pos < n_irgs) {
199     return irp->graphs[pos];
200   } else {
201     return get_irp_pseudo_irg(pos-n_irgs);
202   }
203 }
204
205 /* Adds type to the list of types in irp. */
206 void add_irp_type(ir_type *typ) {
207   assert (typ != NULL);
208   assert(irp);
209   ARR_APP1 (ir_type *, irp->types, typ);
210 }
211
212 /* Remove type form the list of types in irp. */
213 void remove_irp_type(ir_type *typ) {
214   int i;
215   assert(typ);
216
217   for (i = ARR_LEN(irp->types) -1; i >= 0; i--) {
218     if (irp->types[i] == typ) {
219       for(; i < (ARR_LEN(irp->types)) - 1; i++) {
220         irp->types[i] = irp->types[i+1];
221       }
222       ARR_SETLEN(ir_type *, irp->types, (ARR_LEN(irp->types)) - 1);
223       break;
224     }
225   }
226 }
227
228 int (get_irp_n_types) (void) {
229   return _get_irp_n_types();
230 }
231
232 ir_type *(get_irp_type) (int pos) {
233   return _get_irp_type(pos);
234 }
235
236 void set_irp_type(int pos, ir_type *typ) {
237   assert (irp && typ);
238   assert (pos < (ARR_LEN((irp)->types)));
239   irp->types[pos] = typ;
240 }
241
242 /* Returns the number of all modes in the irp. */
243 int (get_irp_n_modes)(void) {
244   return _get_irp_n_modes();
245 }
246
247 /* Returns the mode at position pos in the irp. */
248 ir_mode *(get_irp_mode)(int pos) {
249   return _get_irp_mode(pos);
250 }
251
252 /* Adds mode to the list of modes in irp. */
253 void add_irp_mode(ir_mode *mode) {
254   assert(mode != NULL);
255   assert(irp);
256   ARR_APP1(ir_mode *, irp->modes, mode);
257 }
258
259 /* Adds opcode to the list of opcodes in irp. */
260 void add_irp_opcode(ir_op *opcode) {
261   assert(opcode != NULL);
262   assert(irp);
263   ARR_APP1(ir_op *, irp->opcodes, opcode);
264 }
265
266 /* Removes opcode from the list of opcodes and shrinks the list by one. */
267 void remove_irp_opcode(ir_op *opcode) {
268   int i;
269   assert(opcode);
270
271   for (i = ARR_LEN(irp->opcodes) -1; i >= 0; i--) {
272     if (irp->opcodes[i] == opcode) {
273       for(; i < (ARR_LEN(irp->opcodes)) - 1; i++) {
274         irp->opcodes[i] = irp->opcodes[i+1];
275       }
276       ARR_SETLEN(ir_op *, irp->opcodes, (ARR_LEN(irp->opcodes)) - 1);
277       break;
278     }
279   }
280 }
281
282 /* Returns the number of all opcodes in the irp. */
283 int (get_irp_n_opcodes)(void) {
284   return _get_irp_n_opcodes();
285 }
286
287 /* Returns the opcode at position pos in the irp. */
288 ir_op *(get_irp_opcode)(int pos) {
289   return _get_irp_opcode(pos);
290 }
291
292 /* Sets the generic function pointer of all opcodes to NULL */
293 void  clear_irp_opcodes_generic_func(void) {
294   int i;
295
296   for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
297     ir_op *op = get_irp_opcode(i);
298     op->ops.generic = (op_func)NULL;
299   }
300 }
301
302 /*- File name / executable name or the like -*/
303 void   set_irp_prog_name(ident *name) {
304   irp->name = name;
305 }
306 int irp_prog_name_is_set(void) {
307   return irp->name != new_id_from_str(INITAL_PROG_NAME);
308 }
309 ident *get_irp_prog_ident(void) {
310   return irp->name;
311 }
312 const char  *get_irp_prog_name(void) {
313   return get_id_str(irp->name);
314 }
315
316
317 ir_graph *(get_const_code_irg)(void) {
318   return _get_const_code_irg();
319 }
320
321 irg_phase_state get_irp_phase_state(void) {
322   return irp->phase_state;
323 }
324 void           set_irp_phase_state(irg_phase_state s) {
325   irp->phase_state = s;
326 }
327
328 irg_outs_state get_irp_ip_outs_state(void) {
329   return irp->outs_state;
330 }
331
332 void set_irp_ip_outs_inconsistent(void) {
333   irp->outs_state = outs_inconsistent;
334 }
335
336 void      set_irp_ip_outedges(ir_node ** ip_outedges)
337 {
338   irp->ip_outedges = ip_outedges;
339 }
340
341 ir_node** get_irp_ip_outedges(void)
342 {
343   return irp->ip_outedges;
344 }
345
346
347 irg_callee_info_state get_irp_callee_info_state(void) {
348   return irp->callee_info_state;
349 }
350
351 void set_irp_callee_info_state(irg_callee_info_state s) {
352   irp->callee_info_state = s;
353 }