Fix compile errors.
[libfirm] / ir / ir / irprog.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief    Entry point to the representation of a whole program.
23  * @author   Goetz Lindenmaier, Michael Beck
24  * @date     2000
25  * @version  $Id$
26  */
27 #include "config.h"
28
29 #include <string.h>
30
31 #include "irprog_t.h"
32 #include "irgraph_t.h"
33 #include "pseudo_irg.h"
34 #include "array.h"
35 #include "error.h"
36 #include "obst.h"
37 #include "irop_t.h"
38 #include "irmemory.h"
39
40 /** The initial name of the irp program. */
41 #define INITAL_PROG_NAME "no_name_set"
42
43 /* A variable from where everything in the ir can be accessed. */
44 ir_prog *irp;
45 ir_prog *get_irp(void) { return irp; }
46
47 /**
48  *  Create a new incomplete ir_prog.
49  */
50 static ir_prog *new_incomplete_ir_prog(void)
51 {
52         ir_prog *res = XMALLOCZ(ir_prog);
53
54         res->kind           = k_ir_prog;
55         res->graphs         = NEW_ARR_F(ir_graph *, 0);
56         res->pseudo_graphs  = NEW_ARR_F(ir_graph *, 0);
57         res->types          = NEW_ARR_F(ir_type *, 0);
58         res->modes          = NEW_ARR_F(ir_mode *, 0);
59         res->opcodes        = NEW_ARR_F(ir_op *, 0);
60         res->global_asms    = NEW_ARR_F(ident *, 0);
61         res->last_region_nr = 0;
62         res->last_label_nr  = 1;  /* 0 is reserved as non-label */
63         res->max_irg_idx    = 0;
64         res->max_node_nr    = 0;
65 #ifndef NDEBUG
66         res->reserved_resources = 0;
67 #endif
68
69         return res;
70 }
71
72 /**
73  * Completes an incomplete irprog.
74  *
75  * @param irp          the (yet incomplete) irp
76  * @param module_name  the (module) name for this irp
77  */
78 static ir_prog *complete_ir_prog(ir_prog *irp, const char *module_name) {
79         int i;
80
81         irp->name = new_id_from_str(module_name);
82         irp->segment_types[IR_SEGMENT_GLOBAL] = new_type_class(new_id_from_str("GlobalType"));
83         irp->segment_types[IR_SEGMENT_THREAD_LOCAL]
84                 = new_type_struct(new_id_from_str("ThreadLocal"));
85         irp->segment_types[IR_SEGMENT_CONSTRUCTORS]
86                 = new_type_struct(new_id_from_str("Constructors"));
87         irp->segment_types[IR_SEGMENT_DESTRUCTORS]
88                 = new_type_struct(new_id_from_str("Destructors"));
89         /* Remove these types from type list.  Must be treated differently than
90            other types. */
91         for (i = 0; i < IR_SEGMENT_COUNT; ++i) {
92                 remove_irp_type(irp->segment_types[i]);
93         }
94
95         /* Set these flags for debugging. */
96         irp->segment_types[IR_SEGMENT_GLOBAL]->flags       |= tf_global_type;
97         irp->segment_types[IR_SEGMENT_THREAD_LOCAL]->flags |= tf_tls_type;
98         irp->segment_types[IR_SEGMENT_CONSTRUCTORS]->flags |= tf_constructors;
99         irp->segment_types[IR_SEGMENT_DESTRUCTORS]->flags  |= tf_destructors;
100
101         /* The global type is a class, but we cannot derive from it, so set
102            the final property to assist optimizations that checks for it. */
103         set_class_final(irp->segment_types[IR_SEGMENT_GLOBAL], 1);
104
105         irp->const_code_irg             = new_const_code_irg();
106         irp->phase_state                = phase_building;
107         irp->outs_state                 = outs_none;
108         irp->ip_outedges                = NULL;
109         irp->trouts_state               = outs_none;
110         irp->class_cast_state           = ir_class_casts_transitive;
111         irp->globals_entity_usage_state = ir_entity_usage_not_computed;
112
113         return irp;
114 }
115
116 /* initializes ir_prog. Constructs only the basic lists. */
117 void init_irprog_1(void) {
118         irp = new_incomplete_ir_prog();
119 }
120
121 /* Completes ir_prog. */
122 void init_irprog_2(void) {
123         (void)complete_ir_prog(irp, INITAL_PROG_NAME);
124 }
125
126 /* Create a new ir prog. Automatically called by init_firm through
127    init_irprog. */
128 ir_prog *new_ir_prog(void) {
129         return complete_ir_prog(new_incomplete_ir_prog(), INITAL_PROG_NAME);
130 }
131
132 /* frees all memory used by irp.  Types in type list, irgs in irg
133    list and entities in global type must be freed by hand before. */
134 void free_ir_prog(void) {
135         int i;
136         for (i = 0; i < IR_SEGMENT_COUNT; ++i) {
137                 free_type(irp->segment_types[i]);
138         }
139
140         free_ir_graph(irp->const_code_irg);
141         DEL_ARR_F(irp->graphs);
142         DEL_ARR_F(irp->pseudo_graphs);
143         DEL_ARR_F(irp->types);
144         DEL_ARR_F(irp->modes);
145
146         finish_op();
147         DEL_ARR_F(irp->opcodes);
148         DEL_ARR_F(irp->global_asms);
149
150         irp->name           = NULL;
151         irp->const_code_irg = NULL;
152         irp->kind           = k_BAD;
153 }
154
155 /*- Functions to access the fields of ir_prog -*/
156
157
158 /* Access the main routine of the compiled program. */
159 ir_graph *get_irp_main_irg(void) {
160         assert(irp);
161         return irp->main_irg;
162 }
163
164 void set_irp_main_irg(ir_graph *main_irg) {
165         assert(irp);
166         irp->main_irg = main_irg;
167 }
168
169 ir_type *(get_segment_type)(ir_segment_t segment) {
170         return _get_segment_type(segment);
171 }
172
173 ir_type *(get_glob_type)(void) {
174         return _get_glob_type();
175 }
176
177 ir_type *(get_tls_type)(void) {
178         return _get_tls_type();
179 }
180
181 /* Adds irg to the list of ir graphs in irp. */
182 void add_irp_irg(ir_graph *irg) {
183         assert(irg != NULL);
184         assert(irp && irp->graphs);
185         ARR_APP1(ir_graph *, irp->graphs, irg);
186 }
187
188 /* Removes irg from the list or irgs, shrinks the list by one. */
189 void remove_irp_irg_from_list(ir_graph *irg){
190         int i, l, found = 0;
191
192         assert(irg);
193         l = ARR_LEN(irp->graphs);
194         for (i = 0; i < l; i++) {
195                 if (irp->graphs[i] == irg) {
196                         found = 1;
197                         for(; i < l - 1; i++) {
198                                 irp->graphs[i] = irp->graphs[i+1];
199                         }
200                         ARR_SETLEN(ir_graph*, irp->graphs, l - 1);
201                         break;
202                 }
203         }
204         if (!found) {
205                 l = ARR_LEN(irp->pseudo_graphs);
206                 for (i = 0; i < l; i++) {
207                         if (irp->pseudo_graphs[i] == irg) {
208                                 for(; i < l - 1; i++) {
209                                         irp->pseudo_graphs[i] = irp->pseudo_graphs[i+1];
210                                 }
211                                 ARR_SETLEN(ir_graph*, irp->pseudo_graphs, l - 1);
212                                 break;
213                         }
214                 }
215         }
216 }
217
218 /* Removes irg from the list or irgs, shrinks the list by one. */
219 void remove_irp_irg(ir_graph *irg){
220         free_ir_graph(irg);
221         remove_irp_irg_from_list(irg);
222 }
223
224 int (get_irp_n_irgs)(void) {
225         return _get_irp_n_irgs();
226 }
227
228 ir_graph *(get_irp_irg)(int pos){
229         return _get_irp_irg(pos);
230 }
231
232 int get_irp_last_idx(void) {
233         return irp->max_irg_idx;
234 }
235
236 void set_irp_irg(int pos, ir_graph *irg) {
237         assert(irp && irg);
238         assert(pos < (ARR_LEN(irp->graphs)));
239         irp->graphs[pos] = irg;
240 }
241
242 /* Gets the number of graphs _and_ pseudo graphs. */
243 int get_irp_n_allirgs(void) {
244         /* We can not call get_irp_n_irgs, as we end up in a recursion ... */
245         return ARR_LEN(irp->graphs) + get_irp_n_pseudo_irgs();
246 }
247
248 /* Returns the ir graph at position pos of all graphs (including
249  pseudo graphs).  Visits first graphs, then pseudo graphs. */
250 ir_graph *get_irp_allirg(int pos) {
251         int n_irgs = ARR_LEN(irp->graphs);
252         assert(0 <= pos);
253         if (pos < n_irgs) {
254                 return irp->graphs[pos];
255         } else {
256                 return get_irp_pseudo_irg(pos-n_irgs);
257         }
258 }
259
260 /* Adds type to the list of types in irp. */
261 void add_irp_type(ir_type *typ) {
262         assert(typ != NULL);
263         assert(irp);
264         ARR_APP1(ir_type *, irp->types, typ);
265 }
266
267 /* Remove type from the list of types in irp. */
268 void remove_irp_type(ir_type *typ) {
269         int i;
270         assert(typ);
271
272         for (i = ARR_LEN(irp->types) - 1; i >= 0; i--) {
273                 if (irp->types[i] == typ) {
274                         for(; i < (ARR_LEN(irp->types)) - 1; i++) {
275                                 irp->types[i] = irp->types[i+1];
276                         }
277                         ARR_SETLEN(ir_type *, irp->types, (ARR_LEN(irp->types)) - 1);
278                         break;
279                 }
280         }
281 }
282
283 int (get_irp_n_types) (void) {
284         return _get_irp_n_types();
285 }
286
287 ir_type *(get_irp_type) (int pos) {
288         return _get_irp_type(pos);
289 }
290
291 void set_irp_type(int pos, ir_type *typ) {
292         assert(irp && typ);
293         assert(pos < (ARR_LEN((irp)->types)));
294         irp->types[pos] = typ;
295 }
296
297 /* Returns the number of all modes in the irp. */
298 int (get_irp_n_modes)(void) {
299         return _get_irp_n_modes();
300 }
301
302 /* Returns the mode at position pos in the irp. */
303 ir_mode *(get_irp_mode)(int pos) {
304         return _get_irp_mode(pos);
305 }
306
307 /* Adds mode to the list of modes in irp. */
308 void add_irp_mode(ir_mode *mode) {
309         assert(mode != NULL);
310         assert(irp);
311         ARR_APP1(ir_mode *, irp->modes, mode);
312 }
313
314 /* Adds opcode to the list of opcodes in irp. */
315 void add_irp_opcode(ir_op *opcode) {
316         int    len;
317         size_t code;
318         assert(opcode != NULL);
319         assert(irp);
320         len  = ARR_LEN(irp->opcodes);
321         code = opcode->code;
322         if (code >= len) {
323                 ARR_RESIZE(ir_op*, irp->opcodes, code+1);
324                 memset(&irp->opcodes[len], 0, (code-len+1) * sizeof(irp->opcodes[0]));
325         }
326
327         assert(irp->opcodes[code] == NULL && "opcode registered twice");
328         irp->opcodes[code] = opcode;
329 }
330
331 /* Removes opcode from the list of opcodes and shrinks the list by one. */
332 void remove_irp_opcode(ir_op *opcode) {
333         assert(opcode->code < ARR_LEN(irp->opcodes));
334         irp->opcodes[opcode->code] = NULL;
335 }
336
337 /* Returns the number of all opcodes in the irp. */
338 int (get_irp_n_opcodes)(void) {
339         return _get_irp_n_opcodes();
340 }
341
342 /* Returns the opcode at position pos in the irp. */
343 ir_op *(get_irp_opcode)(int pos) {
344         return _get_irp_opcode(pos);
345 }
346
347 /* Sets the generic function pointer of all opcodes to NULL */
348 void  clear_irp_opcodes_generic_func(void) {
349         int i;
350
351         for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
352                 ir_op *op = get_irp_opcode(i);
353                 op->ops.generic = (op_func)NULL;
354         }
355 }
356
357 /*- File name / executable name or the like -*/
358 void   set_irp_prog_name(ident *name) {
359         irp->name = name;
360 }
361 int irp_prog_name_is_set(void) {
362         return irp->name != new_id_from_str(INITAL_PROG_NAME);
363 }
364 ident *get_irp_ident(void) {
365         return irp->name;
366 }
367 const char  *get_irp_name(void) {
368         return get_id_str(irp->name);
369 }
370
371
372 ir_graph *(get_const_code_irg)(void) {
373         return _get_const_code_irg();
374 }
375
376 irg_phase_state get_irp_phase_state(void) {
377         return irp->phase_state;
378 }
379
380 void set_irp_phase_state(irg_phase_state s) {
381         irp->phase_state = s;
382 }
383
384 irg_outs_state get_irp_ip_outs_state(void) {
385         return irp->outs_state;
386 }
387
388 void set_irp_ip_outs_inconsistent(void) {
389         irp->outs_state = outs_inconsistent;
390 }
391
392 void set_irp_ip_outedges(ir_node ** ip_outedges) {
393         irp->ip_outedges = ip_outedges;
394 }
395
396 ir_node** get_irp_ip_outedges(void) {
397         return irp->ip_outedges;
398 }
399
400 irg_callee_info_state get_irp_callee_info_state(void) {
401         return irp->callee_info_state;
402 }
403
404 void set_irp_callee_info_state(irg_callee_info_state s) {
405         irp->callee_info_state = s;
406 }
407
408 /* Returns a new, unique exception region number. */
409 ir_exc_region_t (get_irp_next_region_nr)(void) {
410         return _get_irp_next_region_nr();
411 }
412
413 /* Returns a new, unique label number. */
414 ir_label_t (get_irp_next_label_nr)(void) {
415         return _get_irp_next_label_nr();
416 }
417
418 /* Add a new global asm include */
419 void add_irp_asm(ident *asm_string) {
420         ARR_APP1(ident *, irp->global_asms, asm_string);
421 }
422
423 /* Return the number of global asm includes. */
424 int get_irp_n_asms(void) {
425         return ARR_LEN(irp->global_asms);
426 }
427
428 /* Return the global asm include at position pos. */
429 ident *get_irp_asm(int pos) {
430         assert(0 <= pos && pos < get_irp_n_asms());
431         return irp->global_asms[pos];
432 }
433
434 #ifndef NDEBUG
435 void irp_reserve_resources(ir_prog *irp, ir_resources_t resources) {
436         assert((resources & ~IR_RESOURCE_GLOBAL_MASK) == 0);
437         assert((irp->reserved_resources & resources) == 0);
438         irp->reserved_resources |= resources;
439 }
440
441 void irp_free_resources(ir_prog *irp, ir_resources_t resources) {
442         assert((irp->reserved_resources & resources) == resources);
443         irp->reserved_resources &= ~resources;
444 }
445
446 ir_resources_t irp_resources_reserved(const ir_prog *irp) {
447         return irp->reserved_resources;
448 }
449 #endif