2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Entry point to the representation of a whole program.
23 * @author Goetz Lindenmaier, Michael Beck
32 #include "irgraph_t.h"
34 #include "pseudo_irg.h"
41 /** The initial name of the irp program. */
42 #define INITAL_PROG_NAME "no_name_set"
44 /* A variable from where everything in the ir can be accessed. */
46 ir_prog *get_irp(void) { return irp; }
49 * Create a new incomplete ir_prog.
51 static ir_prog *new_incomplete_ir_prog(void)
53 ir_prog *res = XMALLOCZ(ir_prog);
55 res->kind = k_ir_prog;
56 res->graphs = NEW_ARR_F(ir_graph *, 0);
57 res->pseudo_graphs = NEW_ARR_F(ir_graph *, 0);
58 res->types = NEW_ARR_F(ir_type *, 0);
59 res->modes = NEW_ARR_F(ir_mode *, 0);
60 res->opcodes = NEW_ARR_F(ir_op *, 0);
61 res->global_asms = NEW_ARR_F(ident *, 0);
62 res->last_region_nr = 0;
63 res->last_label_nr = 1; /* 0 is reserved as non-label */
67 res->reserved_resources = 0;
74 * Completes an incomplete irprog.
76 * @param irp the (yet incomplete) irp
77 * @param module_name the (module) name for this irp
79 static ir_prog *complete_ir_prog(ir_prog *irp, const char *module_name)
83 #define IDENT(x) new_id_from_chars(x, sizeof(x) - 1)
85 irp->name = new_id_from_str(module_name);
86 irp->segment_types[IR_SEGMENT_GLOBAL] = new_type_class(IDENT("GlobalType"));
87 irp->segment_types[IR_SEGMENT_THREAD_LOCAL]
88 = new_type_class(IDENT("ThreadLocal"));
89 irp->segment_types[IR_SEGMENT_CONSTRUCTORS]
90 = new_type_class(IDENT("Constructors"));
91 irp->segment_types[IR_SEGMENT_DESTRUCTORS]
92 = new_type_class(IDENT("Destructors"));
93 /* Remove these types from type list. Must be treated differently than
95 for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
96 remove_irp_type(irp->segment_types[s]);
99 /* Set these flags for debugging. */
100 irp->segment_types[IR_SEGMENT_GLOBAL]->flags |= tf_global_type;
101 irp->segment_types[IR_SEGMENT_THREAD_LOCAL]->flags |= tf_tls_type;
102 irp->segment_types[IR_SEGMENT_CONSTRUCTORS]->flags |= tf_constructors;
103 irp->segment_types[IR_SEGMENT_DESTRUCTORS]->flags |= tf_destructors;
105 /* The global type is a class, but we cannot derive from it, so set
106 the final property to assist optimizations that checks for it. */
107 set_class_final(irp->segment_types[IR_SEGMENT_GLOBAL], 1);
109 irp->const_code_irg = new_const_code_irg();
110 irp->phase_state = phase_building;
111 irp->outs_state = outs_none;
112 irp->ip_outedges = NULL;
113 irp->trouts_state = outs_none;
114 irp->class_cast_state = ir_class_casts_transitive;
115 irp->globals_entity_usage_state = ir_entity_usage_not_computed;
121 /* initializes ir_prog. Constructs only the basic lists. */
122 void init_irprog_1(void) {
123 irp = new_incomplete_ir_prog();
126 /* Completes ir_prog. */
127 void init_irprog_2(void) {
128 (void)complete_ir_prog(irp, INITAL_PROG_NAME);
131 /* Create a new ir prog. Automatically called by init_firm through
133 ir_prog *new_ir_prog(const char *name) {
134 return complete_ir_prog(new_incomplete_ir_prog(), name);
137 /* frees all memory used by irp. Types in type list, irgs in irg
138 list and entities in global type must be freed by hand before. */
139 void free_ir_prog(void)
142 for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
143 free_type(irp->segment_types[s]);
146 free_ir_graph(irp->const_code_irg);
147 DEL_ARR_F(irp->graphs);
148 DEL_ARR_F(irp->pseudo_graphs);
149 DEL_ARR_F(irp->types);
150 DEL_ARR_F(irp->modes);
153 DEL_ARR_F(irp->opcodes);
154 DEL_ARR_F(irp->global_asms);
157 irp->const_code_irg = NULL;
161 /*- Functions to access the fields of ir_prog -*/
164 /* Access the main routine of the compiled program. */
165 ir_graph *get_irp_main_irg(void) {
167 return irp->main_irg;
170 void set_irp_main_irg(ir_graph *main_irg) {
172 irp->main_irg = main_irg;
175 ir_type *(get_segment_type)(ir_segment_t segment)
177 return _get_segment_type(segment);
180 void set_segment_type(ir_segment_t segment, ir_type *new_type)
182 assert(segment <= IR_SEGMENT_LAST);
183 irp->segment_types[segment] = new_type;
184 /* segment types are not in the type list... */
185 remove_irp_type(new_type);
188 ir_type *(get_glob_type)(void) {
189 return _get_glob_type();
192 ir_type *(get_tls_type)(void) {
193 return _get_tls_type();
196 /* Adds irg to the list of ir graphs in irp. */
197 void add_irp_irg(ir_graph *irg) {
199 assert(irp && irp->graphs);
200 ARR_APP1(ir_graph *, irp->graphs, irg);
203 /* Removes irg from the list or irgs, shrinks the list by one. */
204 void remove_irp_irg_from_list(ir_graph *irg){
208 l = ARR_LEN(irp->graphs);
209 for (i = 0; i < l; i++) {
210 if (irp->graphs[i] == irg) {
212 for(; i < l - 1; i++) {
213 irp->graphs[i] = irp->graphs[i+1];
215 ARR_SETLEN(ir_graph*, irp->graphs, l - 1);
220 l = ARR_LEN(irp->pseudo_graphs);
221 for (i = 0; i < l; i++) {
222 if (irp->pseudo_graphs[i] == irg) {
223 for(; i < l - 1; i++) {
224 irp->pseudo_graphs[i] = irp->pseudo_graphs[i+1];
226 ARR_SETLEN(ir_graph*, irp->pseudo_graphs, l - 1);
233 /* Removes irg from the list or irgs, shrinks the list by one. */
234 void remove_irp_irg(ir_graph *irg){
236 remove_irp_irg_from_list(irg);
239 int (get_irp_n_irgs)(void) {
240 return _get_irp_n_irgs();
243 ir_graph *(get_irp_irg)(int pos){
244 return _get_irp_irg(pos);
247 int get_irp_last_idx(void) {
248 return irp->max_irg_idx;
251 void set_irp_irg(int pos, ir_graph *irg) {
253 assert(pos < (ARR_LEN(irp->graphs)));
254 irp->graphs[pos] = irg;
257 /* Gets the number of graphs _and_ pseudo graphs. */
258 int get_irp_n_allirgs(void) {
259 /* We can not call get_irp_n_irgs, as we end up in a recursion ... */
260 return ARR_LEN(irp->graphs) + get_irp_n_pseudo_irgs();
263 /* Returns the ir graph at position pos of all graphs (including
264 pseudo graphs). Visits first graphs, then pseudo graphs. */
265 ir_graph *get_irp_allirg(int pos) {
266 int n_irgs = ARR_LEN(irp->graphs);
269 return irp->graphs[pos];
271 return get_irp_pseudo_irg(pos-n_irgs);
275 /* Adds type to the list of types in irp. */
276 void add_irp_type(ir_type *typ) {
279 ARR_APP1(ir_type *, irp->types, typ);
282 /* Remove type from the list of types in irp. */
283 void remove_irp_type(ir_type *typ) {
287 for (i = ARR_LEN(irp->types) - 1; i >= 0; i--) {
288 if (irp->types[i] == typ) {
289 for(; i < (ARR_LEN(irp->types)) - 1; i++) {
290 irp->types[i] = irp->types[i+1];
292 ARR_SETLEN(ir_type *, irp->types, (ARR_LEN(irp->types)) - 1);
298 int (get_irp_n_types) (void) {
299 return _get_irp_n_types();
302 ir_type *(get_irp_type) (int pos) {
303 return _get_irp_type(pos);
306 void set_irp_type(int pos, ir_type *typ) {
308 assert(pos < (ARR_LEN((irp)->types)));
309 irp->types[pos] = typ;
312 /* Returns the number of all modes in the irp. */
313 int (get_irp_n_modes)(void) {
314 return _get_irp_n_modes();
317 /* Returns the mode at position pos in the irp. */
318 ir_mode *(get_irp_mode)(int pos) {
319 return _get_irp_mode(pos);
322 /* Adds mode to the list of modes in irp. */
323 void add_irp_mode(ir_mode *mode) {
324 assert(mode != NULL);
326 ARR_APP1(ir_mode *, irp->modes, mode);
329 /* Adds opcode to the list of opcodes in irp. */
330 void add_irp_opcode(ir_op *opcode) {
333 assert(opcode != NULL);
335 len = ARR_LEN(irp->opcodes);
337 if ((int) code >= len) {
338 ARR_RESIZE(ir_op*, irp->opcodes, code+1);
339 memset(&irp->opcodes[len], 0, (code-len+1) * sizeof(irp->opcodes[0]));
342 assert(irp->opcodes[code] == NULL && "opcode registered twice");
343 irp->opcodes[code] = opcode;
346 /* Removes opcode from the list of opcodes and shrinks the list by one. */
347 void remove_irp_opcode(ir_op *opcode) {
348 assert((int) opcode->code < ARR_LEN(irp->opcodes));
349 irp->opcodes[opcode->code] = NULL;
352 /* Returns the number of all opcodes in the irp. */
353 int (get_irp_n_opcodes)(void) {
354 return _get_irp_n_opcodes();
357 /* Returns the opcode at position pos in the irp. */
358 ir_op *(get_irp_opcode)(int pos) {
359 return _get_irp_opcode(pos);
362 /* Sets the generic function pointer of all opcodes to NULL */
363 void clear_irp_opcodes_generic_func(void) {
366 for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
367 ir_op *op = get_irp_opcode(i);
368 op->ops.generic = (op_func)NULL;
372 /*- File name / executable name or the like -*/
373 void set_irp_prog_name(ident *name) {
376 int irp_prog_name_is_set(void) {
377 return irp->name != new_id_from_str(INITAL_PROG_NAME);
379 ident *get_irp_ident(void) {
382 const char *get_irp_name(void) {
383 return get_id_str(irp->name);
387 ir_graph *(get_const_code_irg)(void) {
388 return _get_const_code_irg();
391 irg_phase_state get_irp_phase_state(void) {
392 return irp->phase_state;
395 void set_irp_phase_state(irg_phase_state s) {
396 irp->phase_state = s;
401 irg_phase_state state;
405 * Wrapper for setting the state of a whole ir_prog.
407 static int set_irp_phase_state_wrapper(ir_prog *irp, void *context) {
408 struct pass_t *pass = context;
409 irg_phase_state state = pass->state;
414 /* set the phase of all graphs */
415 for (i = get_irp_n_irgs() - 1; i >= 0; --i)
416 set_irg_phase_state(get_irp_irg(i), state);
418 /* set the irp phase */
419 set_irp_phase_state(state);
424 ir_prog_pass_t *set_irp_phase_state_pass(const char *name, irg_phase_state state) {
425 struct pass_t *pass = XMALLOCZ(struct pass_t);
427 def_prog_pass_constructor(
428 &pass->pass, name ? name : "set_irp_phase", set_irp_phase_state_wrapper);
432 pass->pass.verify_irprog = ir_prog_no_verify;
433 pass->pass.dump_irprog = ir_prog_no_dump;
438 irg_outs_state get_irp_ip_outs_state(void) {
439 return irp->outs_state;
442 void set_irp_ip_outs_inconsistent(void) {
443 irp->outs_state = outs_inconsistent;
446 void set_irp_ip_outedges(ir_node ** ip_outedges) {
447 irp->ip_outedges = ip_outedges;
450 ir_node** get_irp_ip_outedges(void) {
451 return irp->ip_outedges;
454 irg_callee_info_state get_irp_callee_info_state(void) {
455 return irp->callee_info_state;
458 void set_irp_callee_info_state(irg_callee_info_state s) {
459 irp->callee_info_state = s;
462 /* Returns a new, unique exception region number. */
463 ir_exc_region_t (get_irp_next_region_nr)(void) {
464 return _get_irp_next_region_nr();
467 /* Returns a new, unique label number. */
468 ir_label_t (get_irp_next_label_nr)(void) {
469 return _get_irp_next_label_nr();
472 /* Add a new global asm include */
473 void add_irp_asm(ident *asm_string) {
474 ARR_APP1(ident *, irp->global_asms, asm_string);
477 /* Return the number of global asm includes. */
478 int get_irp_n_asms(void) {
479 return ARR_LEN(irp->global_asms);
482 /* Return the global asm include at position pos. */
483 ident *get_irp_asm(int pos) {
484 assert(0 <= pos && pos < get_irp_n_asms());
485 return irp->global_asms[pos];
489 void irp_reserve_resources(ir_prog *irp, ir_resources_t resources) {
490 assert((resources & ~IR_RESOURCE_GLOBAL_MASK) == 0);
491 assert((irp->reserved_resources & resources) == 0);
492 irp->reserved_resources |= resources;
495 void irp_free_resources(ir_prog *irp, ir_resources_t resources) {
496 assert((irp->reserved_resources & resources) == resources);
497 irp->reserved_resources &= ~resources;
500 ir_resources_t irp_resources_reserved(const ir_prog *irp) {
501 return irp->reserved_resources;