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_global_type;
96 irp->segment_types[IR_SEGMENT_THREAD_LOCAL]->flags |= tf_tls_type;
97 irp->segment_types[IR_SEGMENT_CONSTRUCTORS]->flags |= tf_constructors;
98 irp->segment_types[IR_SEGMENT_DESTRUCTORS]->flags |= 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 for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
140 free_type(irp->segment_types[s]);
143 free_ir_graph(irp->const_code_irg);
144 DEL_ARR_F(irp->graphs);
145 DEL_ARR_F(irp->types);
146 DEL_ARR_F(irp->modes);
149 DEL_ARR_F(irp->opcodes);
150 DEL_ARR_F(irp->global_asms);
153 irp->const_code_irg = NULL;
157 /*- Functions to access the fields of ir_prog -*/
160 /* Access the main routine of the compiled program. */
161 ir_graph *get_irp_main_irg(void)
164 return irp->main_irg;
167 void set_irp_main_irg(ir_graph *main_irg)
170 irp->main_irg = main_irg;
173 ir_type *(get_segment_type)(ir_segment_t segment)
175 return get_segment_type_(segment);
178 void set_segment_type(ir_segment_t segment, ir_type *new_type)
180 assert(segment <= IR_SEGMENT_LAST);
181 irp->segment_types[segment] = new_type;
184 ir_type *(get_glob_type)(void)
186 return get_glob_type_();
189 ir_type *(get_tls_type)(void)
191 return get_tls_type_();
194 /* Adds irg to the list of ir graphs in irp. */
195 void add_irp_irg(ir_graph *irg)
198 assert(irp && irp->graphs);
199 ARR_APP1(ir_graph *, irp->graphs, irg);
202 /* Removes irg from the list or irgs, shrinks the list by one. */
203 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) {
211 for (; i < l - 1; ++i) {
212 irp->graphs[i] = irp->graphs[i+1];
214 ARR_SETLEN(ir_graph*, irp->graphs, l - 1);
220 /* Removes irg from the list or irgs, shrinks the list by one. */
221 void remove_irp_irg(ir_graph *irg)
224 remove_irp_irg_from_list(irg);
227 size_t (get_irp_n_irgs)(void)
229 return get_irp_n_irgs_();
232 ir_graph *(get_irp_irg)(size_t pos)
234 return get_irp_irg_(pos);
237 size_t get_irp_last_idx(void)
239 return irp->max_irg_idx;
242 void set_irp_irg(size_t pos, ir_graph *irg)
245 assert(pos < ARR_LEN(irp->graphs));
246 irp->graphs[pos] = irg;
249 /* Adds type to the list of types in irp. */
250 void add_irp_type(ir_type *typ)
254 ARR_APP1(ir_type *, irp->types, typ);
257 /* Remove type from the list of types in irp. */
258 void remove_irp_type(ir_type *typ)
263 l = ARR_LEN(irp->types);
264 for (i = 0; i < l; ++i) {
265 if (irp->types[i] == typ) {
266 for (; i < l - 1; ++i) {
267 irp->types[i] = irp->types[i+1];
269 ARR_SETLEN(ir_type *, irp->types, l - 1);
275 size_t (get_irp_n_types) (void)
277 return get_irp_n_types_();
280 ir_type *(get_irp_type) (size_t pos)
282 return get_irp_type_(pos);
285 void set_irp_type(size_t pos, ir_type *typ)
288 assert(pos < ARR_LEN((irp)->types));
289 irp->types[pos] = typ;
292 /* Returns the number of all modes in the irp. */
293 size_t (get_irp_n_modes)(void)
295 return get_irp_n_modes_();
298 /* Returns the mode at position pos in the irp. */
299 ir_mode *(get_irp_mode)(size_t pos)
301 return get_irp_mode_(pos);
304 /* Adds mode to the list of modes in irp. */
305 void add_irp_mode(ir_mode *mode)
307 assert(mode != NULL);
309 ARR_APP1(ir_mode *, irp->modes, mode);
312 /* Adds opcode to the list of opcodes in irp. */
313 void add_irp_opcode(ir_op *opcode)
317 assert(opcode != NULL);
319 len = ARR_LEN(irp->opcodes);
322 ARR_RESIZE(ir_op*, irp->opcodes, code+1);
323 memset(&irp->opcodes[len], 0, (code-len+1) * sizeof(irp->opcodes[0]));
326 assert(irp->opcodes[code] == NULL && "opcode registered twice");
327 irp->opcodes[code] = opcode;
330 /* Removes opcode from the list of opcodes and shrinks the list by one. */
331 void remove_irp_opcode(ir_op *opcode)
333 assert(opcode->code < ARR_LEN(irp->opcodes));
334 irp->opcodes[opcode->code] = NULL;
337 /* Returns the number of all opcodes in the irp. */
338 size_t (get_irp_n_opcodes)(void)
340 return get_irp_n_opcodes_();
343 /* Returns the opcode at position pos in the irp. */
344 ir_op *(get_irp_opcode)(size_t pos)
346 return get_irp_opcode_(pos);
349 /* Sets the generic function pointer of all opcodes to NULL */
350 void clear_irp_opcodes_generic_func(void)
354 for (i = 0, n = get_irp_n_opcodes(); i < n; ++i) {
355 ir_op *op = get_irp_opcode(i);
356 op->ops.generic = (op_func)NULL;
360 /*- File name / executable name or the like -*/
361 void set_irp_prog_name(ident *name)
365 int irp_prog_name_is_set(void)
367 return irp->name != new_id_from_str(INITAL_PROG_NAME);
369 ident *get_irp_ident(void)
373 const char *get_irp_name(void)
375 return get_id_str(irp->name);
379 ir_graph *(get_const_code_irg)(void)
381 return get_const_code_irg_();
384 irg_phase_state get_irp_phase_state(void)
386 return irp->phase_state;
389 void set_irp_phase_state(irg_phase_state s)
391 irp->phase_state = s;
394 typedef struct pass_t {
396 irg_phase_state state;
400 * Wrapper for setting the state of a whole ir_prog.
402 static int set_irp_phase_state_wrapper(ir_prog *irp, void *context)
404 pass_t *pass = (pass_t *)context;
405 irg_phase_state state = pass->state;
410 /* set the phase of all graphs */
411 for (i = 0, n = get_irp_n_irgs(); i < n; ++i)
412 set_irg_phase_state(get_irp_irg(i), state);
414 /* set the irp phase */
415 set_irp_phase_state(state);
420 ir_prog_pass_t *set_irp_phase_state_pass(const char *name, irg_phase_state state)
422 struct pass_t *pass = XMALLOCZ(struct pass_t);
424 def_prog_pass_constructor(
425 &pass->pass, name ? name : "set_irp_phase", set_irp_phase_state_wrapper);
429 pass->pass.verify_irprog = ir_prog_no_verify;
430 pass->pass.dump_irprog = ir_prog_no_dump;
435 void set_irp_ip_outedges(ir_node ** ip_outedges)
437 irp->ip_outedges = ip_outedges;
440 ir_node** get_irp_ip_outedges(void)
442 return irp->ip_outedges;
445 irg_callee_info_state get_irp_callee_info_state(void)
447 return irp->callee_info_state;
450 void set_irp_callee_info_state(irg_callee_info_state s)
452 irp->callee_info_state = s;
455 /* Returns a new, unique exception region number. */
456 ir_exc_region_t (get_irp_next_region_nr)(void)
458 return get_irp_next_region_nr_();
461 /* Returns a new, unique label number. */
462 ir_label_t (get_irp_next_label_nr)(void)
464 return get_irp_next_label_nr_();
467 /* Add a new global asm include */
468 void add_irp_asm(ident *asm_string)
470 ARR_APP1(ident *, irp->global_asms, asm_string);
473 /* Return the number of global asm includes. */
474 size_t get_irp_n_asms(void)
476 return ARR_LEN(irp->global_asms);
479 /* Return the global asm include at position pos. */
480 ident *get_irp_asm(size_t pos)
482 assert(pos < get_irp_n_asms());
483 return irp->global_asms[pos];
486 /** Return whether optimization dump vcg graphs */
487 int (get_irp_optimization_dumps)(void)
489 return get_irp_optimization_dumps_();
492 /** Enable vcg dumping of optimization */
493 void (enable_irp_optimization_dumps)(void)
495 enable_irp_optimization_dumps_();
499 void irp_reserve_resources(ir_prog *irp, irp_resources_t resources)
501 assert((irp->reserved_resources & resources) == 0);
502 irp->reserved_resources |= resources;
505 void irp_free_resources(ir_prog *irp, irp_resources_t resources)
507 assert((irp->reserved_resources & resources) == resources);
508 irp->reserved_resources &= ~resources;
511 irp_resources_t irp_resources_reserved(const ir_prog *irp)
513 return irp->reserved_resources;