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
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 = IR_RESOURCE_NONE;
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]
89 = new_type_class(IDENT("GlobalType"));
90 irp->segment_types[IR_SEGMENT_THREAD_LOCAL]
91 = new_type_struct(IDENT("ThreadLocal"));
92 irp->segment_types[IR_SEGMENT_CONSTRUCTORS]
93 = new_type_class(IDENT("Constructors"));
94 irp->segment_types[IR_SEGMENT_DESTRUCTORS]
95 = new_type_class(IDENT("Destructors"));
96 /* Remove these types from type list. Must be treated differently than
98 for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
99 remove_irp_type(irp->segment_types[s]);
102 /* Set these flags for debugging. */
103 irp->segment_types[IR_SEGMENT_GLOBAL]->flags |= tf_global_type;
104 irp->segment_types[IR_SEGMENT_THREAD_LOCAL]->flags |= tf_tls_type;
105 irp->segment_types[IR_SEGMENT_CONSTRUCTORS]->flags |= tf_constructors;
106 irp->segment_types[IR_SEGMENT_DESTRUCTORS]->flags |= tf_destructors;
108 /* The global type is a class, but we cannot derive from it, so set
109 the final property to assist optimizations that checks for it. */
110 set_class_final(irp->segment_types[IR_SEGMENT_GLOBAL], 1);
112 irp->const_code_irg = new_const_code_irg();
113 irp->phase_state = phase_building;
114 irp->class_cast_state = ir_class_casts_transitive;
115 irp->globals_entity_usage_state = ir_entity_usage_not_computed;
117 current_ir_graph = irp->const_code_irg;
123 /* initializes ir_prog. Constructs only the basic lists. */
124 void init_irprog_1(void)
126 irp = new_incomplete_ir_prog();
129 /* Completes ir_prog. */
130 void init_irprog_2(void)
132 (void)complete_ir_prog(irp, INITAL_PROG_NAME);
135 /* Create a new ir prog. Automatically called by init_firm through
137 ir_prog *new_ir_prog(const char *name)
139 return complete_ir_prog(new_incomplete_ir_prog(), name);
142 /* frees all memory used by irp. Types in type list, irgs in irg
143 list and entities in global type must be freed by hand before. */
144 void free_ir_prog(void)
147 for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
148 free_type(irp->segment_types[s]);
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;
190 /* segment types are not in the type list... */
191 remove_irp_type(new_type);
194 ir_type *(get_glob_type)(void)
196 return _get_glob_type();
199 ir_type *(get_tls_type)(void)
201 return _get_tls_type();
204 /* Adds irg to the list of ir graphs in irp. */
205 void add_irp_irg(ir_graph *irg)
208 assert(irp && irp->graphs);
209 ARR_APP1(ir_graph *, irp->graphs, irg);
212 /* Removes irg from the list or irgs, shrinks the list by one. */
213 void remove_irp_irg_from_list(ir_graph *irg)
218 l = ARR_LEN(irp->graphs);
219 for (i = 0; i < l; ++i) {
220 if (irp->graphs[i] == irg) {
221 for (; i < l - 1; ++i) {
222 irp->graphs[i] = irp->graphs[i+1];
224 ARR_SETLEN(ir_graph*, irp->graphs, l - 1);
230 /* Removes irg from the list or irgs, shrinks the list by one. */
231 void remove_irp_irg(ir_graph *irg)
234 remove_irp_irg_from_list(irg);
237 size_t (get_irp_n_irgs)(void)
239 return _get_irp_n_irgs();
242 ir_graph *(get_irp_irg)(size_t pos)
244 return _get_irp_irg(pos);
247 size_t get_irp_last_idx(void)
249 return irp->max_irg_idx;
252 void set_irp_irg(size_t pos, ir_graph *irg)
255 assert(pos < ARR_LEN(irp->graphs));
256 irp->graphs[pos] = irg;
259 /* Adds type to the list of types in irp. */
260 void add_irp_type(ir_type *typ)
264 ARR_APP1(ir_type *, irp->types, typ);
267 /* Remove type from the list of types in irp. */
268 void remove_irp_type(ir_type *typ)
273 l = ARR_LEN(irp->types);
274 for (i = 0; i < l; ++i) {
275 if (irp->types[i] == typ) {
276 for (; i < l - 1; ++i) {
277 irp->types[i] = irp->types[i+1];
279 ARR_SETLEN(ir_type *, irp->types, l - 1);
285 size_t (get_irp_n_types) (void)
287 return _get_irp_n_types();
290 ir_type *(get_irp_type) (size_t pos)
292 return _get_irp_type(pos);
295 void set_irp_type(size_t pos, ir_type *typ)
298 assert(pos < ARR_LEN((irp)->types));
299 irp->types[pos] = typ;
302 /* Returns the number of all modes in the irp. */
303 size_t (get_irp_n_modes)(void)
305 return _get_irp_n_modes();
308 /* Returns the mode at position pos in the irp. */
309 ir_mode *(get_irp_mode)(size_t pos)
311 return _get_irp_mode(pos);
314 /* Adds mode to the list of modes in irp. */
315 void add_irp_mode(ir_mode *mode)
317 assert(mode != NULL);
319 ARR_APP1(ir_mode *, irp->modes, mode);
322 /* Adds opcode to the list of opcodes in irp. */
323 void add_irp_opcode(ir_op *opcode)
327 assert(opcode != NULL);
329 len = ARR_LEN(irp->opcodes);
332 ARR_RESIZE(ir_op*, irp->opcodes, code+1);
333 memset(&irp->opcodes[len], 0, (code-len+1) * sizeof(irp->opcodes[0]));
336 assert(irp->opcodes[code] == NULL && "opcode registered twice");
337 irp->opcodes[code] = opcode;
340 /* Removes opcode from the list of opcodes and shrinks the list by one. */
341 void remove_irp_opcode(ir_op *opcode)
343 assert(opcode->code < ARR_LEN(irp->opcodes));
344 irp->opcodes[opcode->code] = NULL;
347 /* Returns the number of all opcodes in the irp. */
348 size_t (get_irp_n_opcodes)(void)
350 return _get_irp_n_opcodes();
353 /* Returns the opcode at position pos in the irp. */
354 ir_op *(get_irp_opcode)(size_t pos)
356 return _get_irp_opcode(pos);
359 /* Sets the generic function pointer of all opcodes to NULL */
360 void clear_irp_opcodes_generic_func(void)
364 for (i = 0, n = get_irp_n_opcodes(); i < n; ++i) {
365 ir_op *op = get_irp_opcode(i);
366 op->ops.generic = (op_func)NULL;
370 /*- File name / executable name or the like -*/
371 void set_irp_prog_name(ident *name)
375 int irp_prog_name_is_set(void)
377 return irp->name != new_id_from_str(INITAL_PROG_NAME);
379 ident *get_irp_ident(void)
383 const char *get_irp_name(void)
385 return get_id_str(irp->name);
389 ir_graph *(get_const_code_irg)(void)
391 return _get_const_code_irg();
394 irg_phase_state get_irp_phase_state(void)
396 return irp->phase_state;
399 void set_irp_phase_state(irg_phase_state s)
401 irp->phase_state = s;
404 typedef struct pass_t {
406 irg_phase_state state;
410 * Wrapper for setting the state of a whole ir_prog.
412 static int set_irp_phase_state_wrapper(ir_prog *irp, void *context)
414 pass_t *pass = (pass_t *)context;
415 irg_phase_state state = pass->state;
420 /* set the phase of all graphs */
421 for (i = 0, n = get_irp_n_irgs(); i < n; ++i)
422 set_irg_phase_state(get_irp_irg(i), state);
424 /* set the irp phase */
425 set_irp_phase_state(state);
430 ir_prog_pass_t *set_irp_phase_state_pass(const char *name, irg_phase_state state)
432 struct pass_t *pass = XMALLOCZ(struct pass_t);
434 def_prog_pass_constructor(
435 &pass->pass, name ? name : "set_irp_phase", set_irp_phase_state_wrapper);
439 pass->pass.verify_irprog = ir_prog_no_verify;
440 pass->pass.dump_irprog = ir_prog_no_dump;
445 void set_irp_ip_outedges(ir_node ** ip_outedges)
447 irp->ip_outedges = ip_outedges;
450 ir_node** get_irp_ip_outedges(void)
452 return irp->ip_outedges;
455 irg_callee_info_state get_irp_callee_info_state(void)
457 return irp->callee_info_state;
460 void set_irp_callee_info_state(irg_callee_info_state s)
462 irp->callee_info_state = s;
465 /* Returns a new, unique exception region number. */
466 ir_exc_region_t (get_irp_next_region_nr)(void)
468 return _get_irp_next_region_nr();
471 /* Returns a new, unique label number. */
472 ir_label_t (get_irp_next_label_nr)(void)
474 return _get_irp_next_label_nr();
477 /* Add a new global asm include */
478 void add_irp_asm(ident *asm_string)
480 ARR_APP1(ident *, irp->global_asms, asm_string);
483 /* Return the number of global asm includes. */
484 size_t get_irp_n_asms(void)
486 return ARR_LEN(irp->global_asms);
489 /* Return the global asm include at position pos. */
490 ident *get_irp_asm(size_t pos)
492 assert(pos < get_irp_n_asms());
493 return irp->global_asms[pos];
497 void irp_reserve_resources(ir_prog *irp, irp_resources_t resources)
499 assert((irp->reserved_resources & resources) == 0);
500 irp->reserved_resources |= resources;
503 void irp_free_resources(ir_prog *irp, irp_resources_t resources)
505 assert((irp->reserved_resources & resources) == resources);
506 irp->reserved_resources &= ~resources;
509 irp_resources_t irp_resources_reserved(const ir_prog *irp)
511 return irp->reserved_resources;