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
36 #include "irgraph_t.h"
37 #include "pseudo_irg.h"
44 /** The initial name of the irp program. */
45 #define INITAL_PROG_NAME "no_name_set"
47 /* A variable from where everything in the ir can be accessed. */
49 ir_prog *get_irp(void) { return 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->pseudo_graphs = NEW_ARR_F(ir_graph *, 0);
61 res->types = NEW_ARR_F(ir_type *, 0);
62 res->modes = NEW_ARR_F(ir_mode *, 0);
63 res->opcodes = NEW_ARR_F(ir_op *, 0);
64 res->global_asms = NEW_ARR_F(ident *, 0);
65 res->last_region_nr = 0;
66 res->last_label_nr = 1; /* 0 is reserved as non-label */
76 /** Completes an incomplete irprog. */
77 static ir_prog *complete_ir_prog(ir_prog *irp) {
79 #define IDENT(s) new_id_from_chars(s, sizeof(s)-1)
81 irp->name = IDENT(INITAL_PROG_NAME);
83 irp->segment_types[IR_SEGMENT_GLOBAL] = new_type_class(IDENT("GlobalType"));
84 irp->segment_types[IR_SEGMENT_THREAD_LOCAL]
85 = new_type_struct(IDENT("ThreadLocal"));
86 irp->segment_types[IR_SEGMENT_CONSTRUCTORS]
87 = new_type_struct(IDENT("Constructors"));
88 irp->segment_types[IR_SEGMENT_DESTRUCTORS]
89 = new_type_struct(IDENT("Destructors"));
90 /* Remove these types from type list. Must be treated differently than
92 for (i = 0; i < IR_SEGMENT_COUNT; ++i) {
93 remove_irp_type(irp->segment_types[i]);
96 /* Set these flags for debugging. */
97 irp->segment_types[IR_SEGMENT_GLOBAL]->flags |= tf_global_type;
98 irp->segment_types[IR_SEGMENT_THREAD_LOCAL]->flags |= tf_tls_type;
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();
106 irp->phase_state = phase_building;
107 irp->outs_state = outs_none;
108 irp->ip_outedges = NULL;
109 irp->trouts_state = outs_none;
110 irp->class_cast_state = ir_class_casts_transitive;
111 irp->globals_entity_usage_state = ir_entity_usage_not_computed;
117 /* initializes ir_prog. Constructs only the basic lists. */
118 void init_irprog_1(void) {
119 irp = new_incomplete_ir_prog();
122 /* Completes ir_prog. */
123 void init_irprog_2(void) {
124 complete_ir_prog(irp);
127 /* Create a new ir prog. Automatically called by init_firm through
129 ir_prog *new_ir_prog(void) {
130 return complete_ir_prog(new_incomplete_ir_prog());
133 /* frees all memory used by irp. Types in type list, irgs in irg
134 list and entities in global type must be freed by hand before. */
135 void free_ir_prog(void) {
137 for (i = 0; i < IR_SEGMENT_COUNT; ++i) {
138 free_type(irp->segment_types[i]);
141 free_ir_graph(irp->const_code_irg);
142 DEL_ARR_F(irp->graphs);
143 DEL_ARR_F(irp->pseudo_graphs);
144 DEL_ARR_F(irp->types);
145 DEL_ARR_F(irp->modes);
148 DEL_ARR_F(irp->opcodes);
149 DEL_ARR_F(irp->global_asms);
152 irp->const_code_irg = NULL;
156 /*- Functions to access the fields of ir_prog -*/
159 /* Access the main routine of the compiled program. */
160 ir_graph *get_irp_main_irg(void) {
162 return irp->main_irg;
165 void set_irp_main_irg(ir_graph *main_irg) {
167 irp->main_irg = main_irg;
170 ir_type *(get_segment_type)(ir_segment_t segment) {
171 return _get_segment_type(segment);
174 ir_type *(get_glob_type)(void) {
175 return _get_glob_type();
178 ir_type *(get_tls_type)(void) {
179 return _get_tls_type();
182 /* Adds irg to the list of ir graphs in irp. */
183 void add_irp_irg(ir_graph *irg) {
185 assert(irp && irp->graphs);
186 ARR_APP1(ir_graph *, irp->graphs, irg);
189 /* Removes irg from the list or irgs, shrinks the list by one. */
190 void remove_irp_irg_from_list(ir_graph *irg){
194 for (i = 0; i < (ARR_LEN (irp->graphs)); i++) {
195 if (irp->graphs[i] == irg) {
197 for(; i < (ARR_LEN (irp->graphs)) - 1; i++) {
198 irp->graphs[i] = irp->graphs[i+1];
200 ARR_SETLEN(ir_graph*, irp->graphs, (ARR_LEN(irp->graphs)) - 1);
205 for (i = 0; i < (ARR_LEN (irp->pseudo_graphs)); i++) {
206 if (irp->pseudo_graphs[i] == irg) {
207 for(; i < (ARR_LEN (irp->pseudo_graphs)) - 1; i++) {
208 irp->pseudo_graphs[i] = irp->pseudo_graphs[i+1];
210 ARR_SETLEN(ir_graph*, irp->pseudo_graphs, (ARR_LEN(irp->pseudo_graphs)) - 1);
217 /* Removes irg from the list or irgs, shrinks the list by one. */
218 void remove_irp_irg(ir_graph *irg){
221 remove_irp_irg_from_list(irg);
224 int (get_irp_n_irgs)(void) {
225 return _get_irp_n_irgs();
228 ir_graph *(get_irp_irg)(int pos){
229 return _get_irp_irg(pos);
232 int get_irp_last_idx(void) {
233 return irp->max_irg_idx;
236 void set_irp_irg(int pos, ir_graph *irg) {
238 assert(pos < (ARR_LEN(irp->graphs)));
239 irp->graphs[pos] = irg;
242 /* Gets the number of graphs _and_ pseudo graphs. */
243 int get_irp_n_allirgs(void) {
244 /* We can not call get_irp_n_irgs, as we end up in a recursion ... */
245 return ARR_LEN(irp->graphs) + get_irp_n_pseudo_irgs();
248 /* Returns the ir graph at position pos of all graphs (including
249 pseudo graphs). Visits first graphs, then pseudo graphs. */
250 ir_graph *get_irp_allirg(int pos) {
251 int n_irgs = ARR_LEN(irp->graphs);
254 return irp->graphs[pos];
256 return get_irp_pseudo_irg(pos-n_irgs);
260 /* Adds type to the list of types in irp. */
261 void add_irp_type(ir_type *typ) {
264 ARR_APP1 (ir_type *, irp->types, typ);
267 /* Remove type form the list of types in irp. */
268 void remove_irp_type(ir_type *typ) {
272 for (i = ARR_LEN(irp->types) -1; i >= 0; i--) {
273 if (irp->types[i] == typ) {
274 for(; i < (ARR_LEN(irp->types)) - 1; i++) {
275 irp->types[i] = irp->types[i+1];
277 ARR_SETLEN(ir_type *, irp->types, (ARR_LEN(irp->types)) - 1);
283 int (get_irp_n_types) (void) {
284 return _get_irp_n_types();
287 ir_type *(get_irp_type) (int pos) {
288 return _get_irp_type(pos);
291 void set_irp_type(int pos, ir_type *typ) {
293 assert(pos < (ARR_LEN((irp)->types)));
294 irp->types[pos] = typ;
297 /* Returns the number of all modes in the irp. */
298 int (get_irp_n_modes)(void) {
299 return _get_irp_n_modes();
302 /* Returns the mode at position pos in the irp. */
303 ir_mode *(get_irp_mode)(int pos) {
304 return _get_irp_mode(pos);
307 /* Adds mode to the list of modes in irp. */
308 void add_irp_mode(ir_mode *mode) {
309 assert(mode != NULL);
311 ARR_APP1(ir_mode *, irp->modes, mode);
314 /* Adds opcode to the list of opcodes in irp. */
315 void add_irp_opcode(ir_op *opcode) {
316 assert(opcode != NULL);
318 assert(opcode->code == (unsigned) ARR_LEN(irp->opcodes) && "new_ir_op() called in wrong order");
319 ARR_APP1(ir_op *, irp->opcodes, opcode);
322 /* Removes opcode from the list of opcodes and shrinks the list by one. */
323 void remove_irp_opcode(ir_op *opcode) {
327 for (i = ARR_LEN(irp->opcodes) -1; i >= 0; i--) {
328 if (irp->opcodes[i] != opcode)
330 for (; i < (ARR_LEN(irp->opcodes)) - 1; i++) {
331 irp->opcodes[i] = irp->opcodes[i+1];
333 ARR_SETLEN(ir_op *, irp->opcodes, (ARR_LEN(irp->opcodes)) - 1);
336 panic("Deleting unknown opcode");
339 /* Returns the number of all opcodes in the irp. */
340 int (get_irp_n_opcodes)(void) {
341 return _get_irp_n_opcodes();
344 /* Returns the opcode at position pos in the irp. */
345 ir_op *(get_irp_opcode)(int 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) {
353 for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
354 ir_op *op = get_irp_opcode(i);
355 op->ops.generic = (op_func)NULL;
359 /*- File name / executable name or the like -*/
360 void set_irp_prog_name(ident *name) {
363 int irp_prog_name_is_set(void) {
364 return irp->name != new_id_from_str(INITAL_PROG_NAME);
366 ident *get_irp_prog_ident(void) {
369 const char *get_irp_prog_name(void) {
370 return get_id_str(irp->name);
374 ir_graph *(get_const_code_irg)(void) {
375 return _get_const_code_irg();
378 irg_phase_state get_irp_phase_state(void) {
379 return irp->phase_state;
382 void set_irp_phase_state(irg_phase_state s) {
383 irp->phase_state = s;
386 irg_outs_state get_irp_ip_outs_state(void) {
387 return irp->outs_state;
390 void set_irp_ip_outs_inconsistent(void) {
391 irp->outs_state = outs_inconsistent;
394 void set_irp_ip_outedges(ir_node ** ip_outedges) {
395 irp->ip_outedges = ip_outedges;
398 ir_node** get_irp_ip_outedges(void) {
399 return irp->ip_outedges;
402 irg_callee_info_state get_irp_callee_info_state(void) {
403 return irp->callee_info_state;
406 void set_irp_callee_info_state(irg_callee_info_state s) {
407 irp->callee_info_state = s;
410 /* Returns a new, unique exception region number. */
411 ir_exc_region_t (get_irp_next_region_nr)(void) {
412 return _get_irp_next_region_nr();
415 /* Returns a new, unique label number. */
416 ir_label_t (get_irp_next_label_nr)(void) {
417 return _get_irp_next_label_nr();
420 /* Add a new global asm include */
421 void add_irp_asm(ident *asm_string) {
422 ARR_APP1(ident *, irp->global_asms, asm_string);
425 /* Return the number of global asm includes. */
426 int get_irp_n_asms(void) {
427 return ARR_LEN(irp->global_asms);
430 /* Return the global asm include at position pos. */
431 ident *get_irp_asm(int pos) {
432 assert(pos <= 0 && pos < get_irp_n_asms());
433 return irp->global_asms[pos];