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"
40 /** The initial name of the irp program. */
41 #define INITAL_PROG_NAME "no_name_set"
43 /* A variable from where everything in the ir can be accessed. */
45 ir_prog *get_irp(void) { return irp; }
46 void set_irp(ir_prog *new_irp)
52 * Create a new incomplete ir_prog.
54 static ir_prog *new_incomplete_ir_prog(void)
56 ir_prog *res = XMALLOCZ(ir_prog);
58 res->kind = k_ir_prog;
59 res->graphs = NEW_ARR_F(ir_graph *, 0);
60 res->types = NEW_ARR_F(ir_type *, 0);
61 res->modes = NEW_ARR_F(ir_mode *, 0);
62 res->opcodes = NEW_ARR_F(ir_op *, 0);
63 res->global_asms = NEW_ARR_F(ident *, 0);
64 res->last_region_nr = 0;
65 res->last_label_nr = 1; /* 0 is reserved as non-label */
69 res->reserved_resources = 0;
76 * Completes an incomplete irprog.
78 * @param irp the (yet incomplete) irp
79 * @param module_name the (module) name for this irp
81 static ir_prog *complete_ir_prog(ir_prog *irp, const char *module_name)
85 #define IDENT(x) new_id_from_chars(x, sizeof(x) - 1)
87 irp->name = new_id_from_str(module_name);
88 irp->segment_types[IR_SEGMENT_GLOBAL] = new_type_class(IDENT("GlobalType"));
89 irp->segment_types[IR_SEGMENT_THREAD_LOCAL]
90 = new_type_class(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->outs_state = outs_none;
114 irp->ip_outedges = NULL;
115 irp->trouts_state = outs_none;
116 irp->class_cast_state = ir_class_casts_transitive;
117 irp->globals_entity_usage_state = ir_entity_usage_not_computed;
119 current_ir_graph = irp->const_code_irg;
125 /* initializes ir_prog. Constructs only the basic lists. */
126 void init_irprog_1(void)
128 irp = new_incomplete_ir_prog();
131 /* Completes ir_prog. */
132 void init_irprog_2(void)
134 (void)complete_ir_prog(irp, INITAL_PROG_NAME);
137 /* Create a new ir prog. Automatically called by init_firm through
139 ir_prog *new_ir_prog(const char *name)
141 return complete_ir_prog(new_incomplete_ir_prog(), name);
144 /* frees all memory used by irp. Types in type list, irgs in irg
145 list and entities in global type must be freed by hand before. */
146 void free_ir_prog(void)
149 for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
150 free_type(irp->segment_types[s]);
153 free_ir_graph(irp->const_code_irg);
154 DEL_ARR_F(irp->graphs);
155 DEL_ARR_F(irp->types);
156 DEL_ARR_F(irp->modes);
159 DEL_ARR_F(irp->opcodes);
160 DEL_ARR_F(irp->global_asms);
163 irp->const_code_irg = NULL;
167 /*- Functions to access the fields of ir_prog -*/
170 /* Access the main routine of the compiled program. */
171 ir_graph *get_irp_main_irg(void)
174 return irp->main_irg;
177 void set_irp_main_irg(ir_graph *main_irg)
180 irp->main_irg = main_irg;
183 ir_type *(get_segment_type)(ir_segment_t segment)
185 return _get_segment_type(segment);
188 void set_segment_type(ir_segment_t segment, ir_type *new_type)
190 assert(segment <= IR_SEGMENT_LAST);
191 irp->segment_types[segment] = new_type;
192 /* segment types are not in the type list... */
193 remove_irp_type(new_type);
196 ir_type *(get_glob_type)(void)
198 return _get_glob_type();
201 ir_type *(get_tls_type)(void)
203 return _get_tls_type();
206 /* Adds irg to the list of ir graphs in irp. */
207 void add_irp_irg(ir_graph *irg)
210 assert(irp && irp->graphs);
211 ARR_APP1(ir_graph *, irp->graphs, irg);
214 /* Removes irg from the list or irgs, shrinks the list by one. */
215 void remove_irp_irg_from_list(ir_graph *irg)
220 l = ARR_LEN(irp->graphs);
221 for (i = 0; i < l; i++) {
222 if (irp->graphs[i] == irg) {
224 for (; i < l - 1; i++) {
225 irp->graphs[i] = irp->graphs[i+1];
227 ARR_SETLEN(ir_graph*, irp->graphs, l - 1);
233 /* Removes irg from the list or irgs, shrinks the list by one. */
234 void remove_irp_irg(ir_graph *irg)
237 remove_irp_irg_from_list(irg);
240 int (get_irp_n_irgs)(void)
242 return _get_irp_n_irgs();
245 ir_graph *(get_irp_irg)(int pos)
247 return _get_irp_irg(pos);
250 int get_irp_last_idx(void)
252 return irp->max_irg_idx;
255 void set_irp_irg(int pos, ir_graph *irg)
258 assert(pos < (ARR_LEN(irp->graphs)));
259 irp->graphs[pos] = irg;
262 /* Adds type to the list of types in irp. */
263 void add_irp_type(ir_type *typ)
267 ARR_APP1(ir_type *, irp->types, typ);
270 /* Remove type from the list of types in irp. */
271 void remove_irp_type(ir_type *typ)
276 for (i = ARR_LEN(irp->types) - 1; i >= 0; i--) {
277 if (irp->types[i] == typ) {
278 for (; i < (ARR_LEN(irp->types)) - 1; i++) {
279 irp->types[i] = irp->types[i+1];
281 ARR_SETLEN(ir_type *, irp->types, (ARR_LEN(irp->types)) - 1);
287 int (get_irp_n_types) (void)
289 return _get_irp_n_types();
292 ir_type *(get_irp_type) (int pos)
294 return _get_irp_type(pos);
297 void set_irp_type(int pos, ir_type *typ)
300 assert(pos < (ARR_LEN((irp)->types)));
301 irp->types[pos] = typ;
304 /* Returns the number of all modes in the irp. */
305 int (get_irp_n_modes)(void)
307 return _get_irp_n_modes();
310 /* Returns the mode at position pos in the irp. */
311 ir_mode *(get_irp_mode)(int pos)
313 return _get_irp_mode(pos);
316 /* Adds mode to the list of modes in irp. */
317 void add_irp_mode(ir_mode *mode)
319 assert(mode != NULL);
321 ARR_APP1(ir_mode *, irp->modes, mode);
324 /* Adds opcode to the list of opcodes in irp. */
325 void add_irp_opcode(ir_op *opcode)
329 assert(opcode != NULL);
331 len = ARR_LEN(irp->opcodes);
333 if ((int) code >= len) {
334 ARR_RESIZE(ir_op*, irp->opcodes, code+1);
335 memset(&irp->opcodes[len], 0, (code-len+1) * sizeof(irp->opcodes[0]));
338 assert(irp->opcodes[code] == NULL && "opcode registered twice");
339 irp->opcodes[code] = opcode;
342 /* Removes opcode from the list of opcodes and shrinks the list by one. */
343 void remove_irp_opcode(ir_op *opcode)
345 assert((int) opcode->code < ARR_LEN(irp->opcodes));
346 irp->opcodes[opcode->code] = NULL;
349 /* Returns the number of all opcodes in the irp. */
350 int (get_irp_n_opcodes)(void)
352 return _get_irp_n_opcodes();
355 /* Returns the opcode at position pos in the irp. */
356 ir_op *(get_irp_opcode)(int pos)
358 return _get_irp_opcode(pos);
361 /* Sets the generic function pointer of all opcodes to NULL */
362 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)
377 int irp_prog_name_is_set(void)
379 return irp->name != new_id_from_str(INITAL_PROG_NAME);
381 ident *get_irp_ident(void)
385 const char *get_irp_name(void)
387 return get_id_str(irp->name);
391 ir_graph *(get_const_code_irg)(void)
393 return _get_const_code_irg();
396 irg_phase_state get_irp_phase_state(void)
398 return irp->phase_state;
401 void set_irp_phase_state(irg_phase_state s)
403 irp->phase_state = s;
408 irg_phase_state state;
412 * Wrapper for setting the state of a whole ir_prog.
414 static int set_irp_phase_state_wrapper(ir_prog *irp, void *context)
416 struct pass_t *pass = context;
417 irg_phase_state state = pass->state;
422 /* set the phase of all graphs */
423 for (i = get_irp_n_irgs() - 1; i >= 0; --i)
424 set_irg_phase_state(get_irp_irg(i), state);
426 /* set the irp phase */
427 set_irp_phase_state(state);
432 ir_prog_pass_t *set_irp_phase_state_pass(const char *name, irg_phase_state state)
434 struct pass_t *pass = XMALLOCZ(struct pass_t);
436 def_prog_pass_constructor(
437 &pass->pass, name ? name : "set_irp_phase", set_irp_phase_state_wrapper);
441 pass->pass.verify_irprog = ir_prog_no_verify;
442 pass->pass.dump_irprog = ir_prog_no_dump;
447 irg_outs_state get_irp_ip_outs_state(void)
449 return irp->outs_state;
452 void set_irp_ip_outs_inconsistent(void)
454 irp->outs_state = outs_inconsistent;
457 void set_irp_ip_outedges(ir_node ** ip_outedges)
459 irp->ip_outedges = ip_outedges;
462 ir_node** get_irp_ip_outedges(void)
464 return irp->ip_outedges;
467 irg_callee_info_state get_irp_callee_info_state(void)
469 return irp->callee_info_state;
472 void set_irp_callee_info_state(irg_callee_info_state s)
474 irp->callee_info_state = s;
477 /* Returns a new, unique exception region number. */
478 ir_exc_region_t (get_irp_next_region_nr)(void)
480 return _get_irp_next_region_nr();
483 /* Returns a new, unique label number. */
484 ir_label_t (get_irp_next_label_nr)(void)
486 return _get_irp_next_label_nr();
489 /* Add a new global asm include */
490 void add_irp_asm(ident *asm_string)
492 ARR_APP1(ident *, irp->global_asms, asm_string);
495 /* Return the number of global asm includes. */
496 int get_irp_n_asms(void)
498 return ARR_LEN(irp->global_asms);
501 /* Return the global asm include at position pos. */
502 ident *get_irp_asm(int pos)
504 assert(0 <= pos && pos < get_irp_n_asms());
505 return irp->global_asms[pos];
509 void irp_reserve_resources(ir_prog *irp, ir_resources_t resources)
511 assert((resources & ~IR_RESOURCE_GLOBAL_MASK) == 0);
512 assert((irp->reserved_resources & resources) == 0);
513 irp->reserved_resources |= resources;
516 void irp_free_resources(ir_prog *irp, ir_resources_t resources)
518 assert((irp->reserved_resources & resources) == resources);
519 irp->reserved_resources &= ~resources;
522 ir_resources_t irp_resources_reserved(const ir_prog *irp)
524 return irp->reserved_resources;