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