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