2 * Copyright (C) 1995-2011 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
31 #include "irgraph_t.h"
39 /** The initial name of the irp program. */
40 #define INITAL_PROG_NAME "no_name_set"
42 /* A variable from where everything in the ir can be accessed. */
44 ir_prog *get_irp(void) { return irp; }
45 void set_irp(ir_prog *new_irp)
51 * Create a new incomplete ir_prog.
53 static ir_prog *new_incomplete_ir_prog(void)
55 ir_prog *res = XMALLOCZ(ir_prog);
57 res->kind = k_ir_prog;
58 res->graphs = NEW_ARR_F(ir_graph *, 0);
59 res->types = NEW_ARR_F(ir_type *, 0);
60 res->modes = NEW_ARR_F(ir_mode *, 0);
61 res->opcodes = NEW_ARR_F(ir_op *, 0);
62 res->global_asms = NEW_ARR_F(ident *, 0);
63 res->last_region_nr = 0;
64 res->last_label_nr = 1; /* 0 is reserved as non-label */
68 res->reserved_resources = IR_RESOURCE_NONE;
75 * Completes an incomplete irprog.
77 * @param irp the (yet incomplete) irp
78 * @param module_name the (module) name for this irp
80 static ir_prog *complete_ir_prog(ir_prog *irp, const char *module_name)
82 #define IDENT(x) new_id_from_chars(x, sizeof(x) - 1)
84 irp->name = new_id_from_str(module_name);
85 irp->segment_types[IR_SEGMENT_GLOBAL]
86 = new_type_class(IDENT("GlobalType"));
87 irp->segment_types[IR_SEGMENT_THREAD_LOCAL]
88 = new_type_struct(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"));
94 /* Set these flags for debugging. */
95 irp->segment_types[IR_SEGMENT_GLOBAL]->flags |= tf_segment|tf_global_type;
96 irp->segment_types[IR_SEGMENT_THREAD_LOCAL]->flags |= tf_segment|tf_tls_type;
97 irp->segment_types[IR_SEGMENT_CONSTRUCTORS]->flags |= tf_segment|tf_constructors;
98 irp->segment_types[IR_SEGMENT_DESTRUCTORS]->flags |= tf_segment|tf_destructors;
100 /* The global type is a class, but we cannot derive from it, so set
101 the final property to assist optimizations that checks for it. */
102 set_class_final(irp->segment_types[IR_SEGMENT_GLOBAL], 1);
104 irp->const_code_irg = new_const_code_irg();
105 irp->phase_state = phase_building;
106 irp->class_cast_state = ir_class_casts_transitive;
107 irp->globals_entity_usage_state = ir_entity_usage_not_computed;
109 current_ir_graph = irp->const_code_irg;
115 /* initializes ir_prog. Constructs only the basic lists. */
116 void init_irprog_1(void)
118 irp = new_incomplete_ir_prog();
121 /* Completes ir_prog. */
122 void init_irprog_2(void)
124 (void)complete_ir_prog(irp, INITAL_PROG_NAME);
127 /* Create a new ir prog. Automatically called by init_firm through
129 ir_prog *new_ir_prog(const char *name)
131 return complete_ir_prog(new_incomplete_ir_prog(), name);
134 /* frees all memory used by irp. Types in type list, irgs in irg
135 list and entities in global type must be freed by hand before. */
136 void free_ir_prog(void)
139 /* must iterate backwards here */
140 for (i = get_irp_n_irgs(); i > 0;)
141 free_ir_graph(get_irp_irg(--i));
143 free_type_entities(get_glob_type());
144 /* must iterate backwards here */
145 for (i = get_irp_n_types(); i > 0;)
146 free_type_entities(get_irp_type(--i));
148 for (i = get_irp_n_types(); i > 0;)
149 free_type(get_irp_type(--i));
151 free_ir_graph(irp->const_code_irg);
152 DEL_ARR_F(irp->graphs);
153 DEL_ARR_F(irp->types);
154 DEL_ARR_F(irp->modes);
157 DEL_ARR_F(irp->opcodes);
158 DEL_ARR_F(irp->global_asms);
161 irp->const_code_irg = NULL;
165 /*- Functions to access the fields of ir_prog -*/
168 /* Access the main routine of the compiled program. */
169 ir_graph *get_irp_main_irg(void)
172 return irp->main_irg;
175 void set_irp_main_irg(ir_graph *main_irg)
178 irp->main_irg = main_irg;
181 ir_type *(get_segment_type)(ir_segment_t segment)
183 return get_segment_type_(segment);
186 void set_segment_type(ir_segment_t segment, ir_type *new_type)
188 assert(segment <= IR_SEGMENT_LAST);
189 irp->segment_types[segment] = new_type;
192 ir_type *(get_glob_type)(void)
194 return get_glob_type_();
197 ir_type *(get_tls_type)(void)
199 return get_tls_type_();
202 /* Adds irg to the list of ir graphs in irp. */
203 void add_irp_irg(ir_graph *irg)
206 assert(irp && irp->graphs);
207 ARR_APP1(ir_graph *, irp->graphs, irg);
210 /* Removes irg from the list or irgs, shrinks the list by one. */
211 void remove_irp_irg_from_list(ir_graph *irg)
216 l = ARR_LEN(irp->graphs);
217 for (i = 0; i < l; ++i) {
218 if (irp->graphs[i] == irg) {
219 for (; i < l - 1; ++i) {
220 irp->graphs[i] = irp->graphs[i+1];
222 ARR_SETLEN(ir_graph*, irp->graphs, l - 1);
228 /* Removes irg from the list or irgs, shrinks the list by one. */
229 void remove_irp_irg(ir_graph *irg)
232 remove_irp_irg_from_list(irg);
235 size_t (get_irp_n_irgs)(void)
237 return get_irp_n_irgs_();
240 ir_graph *(get_irp_irg)(size_t pos)
242 return get_irp_irg_(pos);
245 size_t get_irp_last_idx(void)
247 return irp->max_irg_idx;
250 void set_irp_irg(size_t pos, ir_graph *irg)
253 assert(pos < ARR_LEN(irp->graphs));
254 irp->graphs[pos] = irg;
257 /* Adds type to the list of types in irp. */
258 void add_irp_type(ir_type *typ)
262 ARR_APP1(ir_type *, irp->types, typ);
265 /* Remove type from the list of types in irp. */
266 void remove_irp_type(ir_type *typ)
271 l = ARR_LEN(irp->types);
272 for (i = 0; i < l; ++i) {
273 if (irp->types[i] == typ) {
274 for (; i < l - 1; ++i) {
275 irp->types[i] = irp->types[i+1];
277 ARR_SETLEN(ir_type *, irp->types, l - 1);
283 size_t (get_irp_n_types) (void)
285 return get_irp_n_types_();
288 ir_type *(get_irp_type) (size_t pos)
290 return get_irp_type_(pos);
293 void set_irp_type(size_t pos, ir_type *typ)
296 assert(pos < ARR_LEN((irp)->types));
297 irp->types[pos] = typ;
300 /* Returns the number of all modes in the irp. */
301 size_t (get_irp_n_modes)(void)
303 return get_irp_n_modes_();
306 /* Returns the mode at position pos in the irp. */
307 ir_mode *(get_irp_mode)(size_t pos)
309 return get_irp_mode_(pos);
312 /* Adds mode to the list of modes in irp. */
313 void add_irp_mode(ir_mode *mode)
315 assert(mode != NULL);
317 ARR_APP1(ir_mode *, irp->modes, mode);
320 /* Adds opcode to the list of opcodes in irp. */
321 void add_irp_opcode(ir_op *opcode)
325 assert(opcode != NULL);
327 len = ARR_LEN(irp->opcodes);
330 ARR_RESIZE(ir_op*, irp->opcodes, code+1);
331 memset(&irp->opcodes[len], 0, (code-len+1) * sizeof(irp->opcodes[0]));
334 assert(irp->opcodes[code] == NULL && "opcode registered twice");
335 irp->opcodes[code] = opcode;
338 /* Removes opcode from the list of opcodes and shrinks the list by one. */
339 void remove_irp_opcode(ir_op *opcode)
341 assert(opcode->code < ARR_LEN(irp->opcodes));
342 irp->opcodes[opcode->code] = NULL;
345 /* Returns the number of all opcodes in the irp. */
346 size_t (get_irp_n_opcodes)(void)
348 return get_irp_n_opcodes_();
351 /* Returns the opcode at position pos in the irp. */
352 ir_op *(get_irp_opcode)(size_t pos)
354 return get_irp_opcode_(pos);
357 /* Sets the generic function pointer of all opcodes to NULL */
358 void clear_irp_opcodes_generic_func(void)
362 for (i = 0, n = get_irp_n_opcodes(); i < n; ++i) {
363 ir_op *op = get_irp_opcode(i);
364 op->ops.generic = (op_func)NULL;
368 /*- File name / executable name or the like -*/
369 void set_irp_prog_name(ident *name)
373 int irp_prog_name_is_set(void)
375 return irp->name != new_id_from_str(INITAL_PROG_NAME);
377 ident *get_irp_ident(void)
381 const char *get_irp_name(void)
383 return get_id_str(irp->name);
387 ir_graph *(get_const_code_irg)(void)
389 return get_const_code_irg_();
392 irg_phase_state get_irp_phase_state(void)
394 return irp->phase_state;
397 void set_irp_phase_state(irg_phase_state s)
399 irp->phase_state = s;
402 typedef struct pass_t {
404 irg_phase_state state;
408 * Wrapper for setting the state of a whole ir_prog.
410 static int set_irp_phase_state_wrapper(ir_prog *irp, void *context)
412 pass_t *pass = (pass_t *)context;
413 irg_phase_state state = pass->state;
418 /* set the phase of all graphs */
419 for (i = 0, n = get_irp_n_irgs(); i < n; ++i)
420 set_irg_phase_state(get_irp_irg(i), state);
422 /* set the irp phase */
423 set_irp_phase_state(state);
428 ir_prog_pass_t *set_irp_phase_state_pass(const char *name, irg_phase_state state)
430 struct pass_t *pass = XMALLOCZ(struct pass_t);
432 def_prog_pass_constructor(
433 &pass->pass, name ? name : "set_irp_phase", set_irp_phase_state_wrapper);
437 pass->pass.verify_irprog = ir_prog_no_verify;
438 pass->pass.dump_irprog = ir_prog_no_dump;
443 void set_irp_ip_outedges(ir_node ** ip_outedges)
445 irp->ip_outedges = ip_outedges;
448 ir_node** get_irp_ip_outedges(void)
450 return irp->ip_outedges;
453 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)
460 irp->callee_info_state = s;
463 /* Returns a new, unique exception region number. */
464 ir_exc_region_t (get_irp_next_region_nr)(void)
466 return get_irp_next_region_nr_();
469 /* Returns a new, unique label number. */
470 ir_label_t (get_irp_next_label_nr)(void)
472 return get_irp_next_label_nr_();
475 /* Add a new global asm include */
476 void add_irp_asm(ident *asm_string)
478 ARR_APP1(ident *, irp->global_asms, asm_string);
481 /* Return the number of global asm includes. */
482 size_t get_irp_n_asms(void)
484 return ARR_LEN(irp->global_asms);
487 /* Return the global asm include at position pos. */
488 ident *get_irp_asm(size_t pos)
490 assert(pos < get_irp_n_asms());
491 return irp->global_asms[pos];
494 /** Return whether optimization dump vcg graphs */
495 int (get_irp_optimization_dumps)(void)
497 return get_irp_optimization_dumps_();
500 /** Enable vcg dumping of optimization */
501 void (enable_irp_optimization_dumps)(void)
503 enable_irp_optimization_dumps_();
507 void irp_reserve_resources(ir_prog *irp, irp_resources_t resources)
509 assert((irp->reserved_resources & resources) == 0);
510 irp->reserved_resources |= resources;
513 void irp_free_resources(ir_prog *irp, irp_resources_t resources)
515 assert((irp->reserved_resources & resources) == resources);
516 irp->reserved_resources &= ~resources;
519 irp_resources_t irp_resources_reserved(const ir_prog *irp)
521 return irp->reserved_resources;