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)
84 #define IDENT(x) new_id_from_chars(x, sizeof(x) - 1)
86 irp->name = new_id_from_str(module_name);
87 irp->segment_types[IR_SEGMENT_GLOBAL]
88 = new_type_class(IDENT("GlobalType"));
89 irp->segment_types[IR_SEGMENT_THREAD_LOCAL]
90 = new_type_struct(IDENT("ThreadLocal"));
91 irp->segment_types[IR_SEGMENT_CONSTRUCTORS]
92 = new_type_class(IDENT("Constructors"));
93 irp->segment_types[IR_SEGMENT_DESTRUCTORS]
94 = new_type_class(IDENT("Destructors"));
95 /* Remove these types from type list. Must be treated differently than
97 for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
98 remove_irp_type(irp->segment_types[s]);
101 /* Set these flags for debugging. */
102 irp->segment_types[IR_SEGMENT_GLOBAL]->flags |= tf_global_type;
103 irp->segment_types[IR_SEGMENT_THREAD_LOCAL]->flags |= tf_tls_type;
104 irp->segment_types[IR_SEGMENT_CONSTRUCTORS]->flags |= tf_constructors;
105 irp->segment_types[IR_SEGMENT_DESTRUCTORS]->flags |= tf_destructors;
107 /* The global type is a class, but we cannot derive from it, so set
108 the final property to assist optimizations that checks for it. */
109 set_class_final(irp->segment_types[IR_SEGMENT_GLOBAL], 1);
111 irp->const_code_irg = new_const_code_irg();
112 irp->phase_state = phase_building;
113 irp->class_cast_state = ir_class_casts_transitive;
114 irp->globals_entity_usage_state = ir_entity_usage_not_computed;
116 current_ir_graph = irp->const_code_irg;
122 /* initializes ir_prog. Constructs only the basic lists. */
123 void init_irprog_1(void)
125 irp = new_incomplete_ir_prog();
128 /* Completes ir_prog. */
129 void init_irprog_2(void)
131 (void)complete_ir_prog(irp, INITAL_PROG_NAME);
134 /* Create a new ir prog. Automatically called by init_firm through
136 ir_prog *new_ir_prog(const char *name)
138 return complete_ir_prog(new_incomplete_ir_prog(), name);
141 /* frees all memory used by irp. Types in type list, irgs in irg
142 list and entities in global type must be freed by hand before. */
143 void free_ir_prog(void)
146 for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
147 free_type(irp->segment_types[s]);
150 free_ir_graph(irp->const_code_irg);
151 DEL_ARR_F(irp->graphs);
152 DEL_ARR_F(irp->types);
153 DEL_ARR_F(irp->modes);
156 DEL_ARR_F(irp->opcodes);
157 DEL_ARR_F(irp->global_asms);
160 irp->const_code_irg = NULL;
164 /*- Functions to access the fields of ir_prog -*/
167 /* Access the main routine of the compiled program. */
168 ir_graph *get_irp_main_irg(void)
171 return irp->main_irg;
174 void set_irp_main_irg(ir_graph *main_irg)
177 irp->main_irg = main_irg;
180 ir_type *(get_segment_type)(ir_segment_t segment)
182 return get_segment_type_(segment);
185 void set_segment_type(ir_segment_t segment, ir_type *new_type)
187 assert(segment <= IR_SEGMENT_LAST);
188 irp->segment_types[segment] = new_type;
189 /* segment types are not in the type list... */
190 remove_irp_type(new_type);
193 ir_type *(get_glob_type)(void)
195 return get_glob_type_();
198 ir_type *(get_tls_type)(void)
200 return get_tls_type_();
203 /* Adds irg to the list of ir graphs in irp. */
204 void add_irp_irg(ir_graph *irg)
207 assert(irp && irp->graphs);
208 ARR_APP1(ir_graph *, irp->graphs, irg);
211 /* Removes irg from the list or irgs, shrinks the list by one. */
212 void remove_irp_irg_from_list(ir_graph *irg)
217 l = ARR_LEN(irp->graphs);
218 for (i = 0; i < l; ++i) {
219 if (irp->graphs[i] == irg) {
220 for (; i < l - 1; ++i) {
221 irp->graphs[i] = irp->graphs[i+1];
223 ARR_SETLEN(ir_graph*, irp->graphs, l - 1);
229 /* Removes irg from the list or irgs, shrinks the list by one. */
230 void remove_irp_irg(ir_graph *irg)
233 remove_irp_irg_from_list(irg);
236 size_t (get_irp_n_irgs)(void)
238 return get_irp_n_irgs_();
241 ir_graph *(get_irp_irg)(size_t pos)
243 return get_irp_irg_(pos);
246 size_t get_irp_last_idx(void)
248 return irp->max_irg_idx;
251 void set_irp_irg(size_t pos, ir_graph *irg)
254 assert(pos < ARR_LEN(irp->graphs));
255 irp->graphs[pos] = irg;
258 /* Adds type to the list of types in irp. */
259 void add_irp_type(ir_type *typ)
263 ARR_APP1(ir_type *, irp->types, typ);
266 /* Remove type from the list of types in irp. */
267 void remove_irp_type(ir_type *typ)
272 l = ARR_LEN(irp->types);
273 for (i = 0; i < l; ++i) {
274 if (irp->types[i] == typ) {
275 for (; i < l - 1; ++i) {
276 irp->types[i] = irp->types[i+1];
278 ARR_SETLEN(ir_type *, irp->types, l - 1);
284 size_t (get_irp_n_types) (void)
286 return get_irp_n_types_();
289 ir_type *(get_irp_type) (size_t pos)
291 return get_irp_type_(pos);
294 void set_irp_type(size_t pos, ir_type *typ)
297 assert(pos < ARR_LEN((irp)->types));
298 irp->types[pos] = typ;
301 /* Returns the number of all modes in the irp. */
302 size_t (get_irp_n_modes)(void)
304 return get_irp_n_modes_();
307 /* Returns the mode at position pos in the irp. */
308 ir_mode *(get_irp_mode)(size_t pos)
310 return get_irp_mode_(pos);
313 /* Adds mode to the list of modes in irp. */
314 void add_irp_mode(ir_mode *mode)
316 assert(mode != NULL);
318 ARR_APP1(ir_mode *, irp->modes, mode);
321 /* Adds opcode to the list of opcodes in irp. */
322 void add_irp_opcode(ir_op *opcode)
326 assert(opcode != NULL);
328 len = ARR_LEN(irp->opcodes);
331 ARR_RESIZE(ir_op*, irp->opcodes, code+1);
332 memset(&irp->opcodes[len], 0, (code-len+1) * sizeof(irp->opcodes[0]));
335 assert(irp->opcodes[code] == NULL && "opcode registered twice");
336 irp->opcodes[code] = opcode;
339 /* Removes opcode from the list of opcodes and shrinks the list by one. */
340 void remove_irp_opcode(ir_op *opcode)
342 assert(opcode->code < ARR_LEN(irp->opcodes));
343 irp->opcodes[opcode->code] = NULL;
346 /* Returns the number of all opcodes in the irp. */
347 size_t (get_irp_n_opcodes)(void)
349 return get_irp_n_opcodes_();
352 /* Returns the opcode at position pos in the irp. */
353 ir_op *(get_irp_opcode)(size_t pos)
355 return get_irp_opcode_(pos);
358 /* Sets the generic function pointer of all opcodes to NULL */
359 void clear_irp_opcodes_generic_func(void)
363 for (i = 0, n = get_irp_n_opcodes(); i < n; ++i) {
364 ir_op *op = get_irp_opcode(i);
365 op->ops.generic = (op_func)NULL;
369 /*- File name / executable name or the like -*/
370 void set_irp_prog_name(ident *name)
374 int irp_prog_name_is_set(void)
376 return irp->name != new_id_from_str(INITAL_PROG_NAME);
378 ident *get_irp_ident(void)
382 const char *get_irp_name(void)
384 return get_id_str(irp->name);
388 ir_graph *(get_const_code_irg)(void)
390 return get_const_code_irg_();
393 irg_phase_state get_irp_phase_state(void)
395 return irp->phase_state;
398 void set_irp_phase_state(irg_phase_state s)
400 irp->phase_state = s;
403 typedef struct pass_t {
405 irg_phase_state state;
409 * Wrapper for setting the state of a whole ir_prog.
411 static int set_irp_phase_state_wrapper(ir_prog *irp, void *context)
413 pass_t *pass = (pass_t *)context;
414 irg_phase_state state = pass->state;
419 /* set the phase of all graphs */
420 for (i = 0, n = get_irp_n_irgs(); i < n; ++i)
421 set_irg_phase_state(get_irp_irg(i), state);
423 /* set the irp phase */
424 set_irp_phase_state(state);
429 ir_prog_pass_t *set_irp_phase_state_pass(const char *name, irg_phase_state state)
431 struct pass_t *pass = XMALLOCZ(struct pass_t);
433 def_prog_pass_constructor(
434 &pass->pass, name ? name : "set_irp_phase", set_irp_phase_state_wrapper);
438 pass->pass.verify_irprog = ir_prog_no_verify;
439 pass->pass.dump_irprog = ir_prog_no_dump;
444 void set_irp_ip_outedges(ir_node ** ip_outedges)
446 irp->ip_outedges = ip_outedges;
449 ir_node** get_irp_ip_outedges(void)
451 return irp->ip_outedges;
454 irg_callee_info_state get_irp_callee_info_state(void)
456 return irp->callee_info_state;
459 void set_irp_callee_info_state(irg_callee_info_state s)
461 irp->callee_info_state = s;
464 /* Returns a new, unique exception region number. */
465 ir_exc_region_t (get_irp_next_region_nr)(void)
467 return get_irp_next_region_nr_();
470 /* Returns a new, unique label number. */
471 ir_label_t (get_irp_next_label_nr)(void)
473 return get_irp_next_label_nr_();
476 /* Add a new global asm include */
477 void add_irp_asm(ident *asm_string)
479 ARR_APP1(ident *, irp->global_asms, asm_string);
482 /* Return the number of global asm includes. */
483 size_t get_irp_n_asms(void)
485 return ARR_LEN(irp->global_asms);
488 /* Return the global asm include at position pos. */
489 ident *get_irp_asm(size_t pos)
491 assert(pos < get_irp_n_asms());
492 return irp->global_asms[pos];
495 /** Return whether optimization dump vcg graphs */
496 int (get_irp_optimization_dumps)(void)
498 return get_irp_optimization_dumps_();
501 /** Enable vcg dumping of optimization */
502 void (enable_irp_optimization_dumps)(void)
504 enable_irp_optimization_dumps_();
508 void irp_reserve_resources(ir_prog *irp, irp_resources_t resources)
510 assert((irp->reserved_resources & resources) == 0);
511 irp->reserved_resources |= resources;
514 void irp_free_resources(ir_prog *irp, irp_resources_t resources)
516 assert((irp->reserved_resources & resources) == resources);
517 irp->reserved_resources &= ~resources;
520 irp_resources_t irp_resources_reserved(const ir_prog *irp)
522 return irp->reserved_resources;