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