2 * Copyright (C) 1995-2007 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"
43 /** The name of the Global type. */
44 #define GLOBAL_TYPE_NAME "GlobalType"
45 /** The name of the Thread Local Storage type. */
46 #define TLS_TYPE_NAME "TLS"
47 /** The initial name of the irp program. */
48 #define INITAL_PROG_NAME "no_name_set"
50 /* A variable from where everything in the ir can be accessed. */
52 ir_prog *get_irp(void) { return irp; }
55 * Create a new incomplete ir_prog.
57 static ir_prog *new_incomplete_ir_prog(void) {
60 res = xmalloc(sizeof(*res));
61 memset(res, 0, sizeof(*res));
63 res->kind = k_ir_prog;
64 res->graphs = NEW_ARR_F(ir_graph *, 0);
65 res->pseudo_graphs = NEW_ARR_F(ir_graph *, 0);
66 res->types = NEW_ARR_F(ir_type *, 0);
67 res->modes = NEW_ARR_F(ir_mode *, 0);
68 res->opcodes = NEW_ARR_F(ir_op *, 0);
69 res->last_region_nr = 0;
70 res->last_label_nr = 1; /* 0 is reserved as non-label */
79 /** Completes an incomplete irprog. */
80 static ir_prog *complete_ir_prog(ir_prog *irp) {
81 #define IDENT(s) new_id_from_chars(s, sizeof(s)-1)
83 irp->name = IDENT(INITAL_PROG_NAME);
84 irp->glob_type = new_type_class(IDENT(GLOBAL_TYPE_NAME));
85 irp->tls_type = new_type_struct(IDENT(TLS_TYPE_NAME));
86 /* Remove these types from type list. Must be treated differently than
88 remove_irp_type(irp->glob_type);
89 remove_irp_type(irp->tls_type);
91 /* Set these flags for debugging. */
92 irp->glob_type->flags |= tf_global_type;
93 irp->tls_type->flags |= tf_tls_type;
95 /* The global type is a class, but we cannot derive from it, so set
96 the final property to assist optimizations that checks for it. */
97 set_class_final(irp->glob_type, 1);
99 irp->const_code_irg = new_const_code_irg();
101 irp->phase_state = phase_building;
102 irp->outs_state = outs_none;
103 irp->ip_outedges = NULL;
104 irp->trouts_state = outs_none;
105 irp->class_cast_state = ir_class_casts_transitive;
106 irp->globals_adr_taken_state = ir_address_taken_not_computed;
112 /* initializes ir_prog. Constructs only the basic lists. */
113 void init_irprog_1(void) {
114 irp = new_incomplete_ir_prog();
117 /* Completes ir_prog. */
118 void init_irprog_2(void) {
119 complete_ir_prog(irp);
122 /* Create a new ir prog. Automatically called by init_firm through
124 ir_prog *new_ir_prog (void) {
125 return complete_ir_prog(new_incomplete_ir_prog());
128 /* frees all memory used by irp. Types in type list, irgs in irg
129 list and entities in global type must be freed by hand before. */
130 void free_ir_prog(void) {
132 free_type(irp->glob_type);
134 free_type(irp->tls_type);
136 /* @@@ * free_ir_graph(irp->const_code_irg); * ?? End has no in?? */
137 DEL_ARR_F(irp->graphs);
138 DEL_ARR_F(irp->pseudo_graphs);
139 DEL_ARR_F(irp->types);
140 DEL_ARR_F(irp->modes);
141 DEL_ARR_F(irp->opcodes);
144 irp->const_code_irg = NULL;
148 /*- Functions to access the fields of ir_prog -*/
151 /* Access the main routine of the compiled program. */
152 ir_graph *get_irp_main_irg(void) {
154 return irp->main_irg;
157 void set_irp_main_irg(ir_graph *main_irg) {
159 irp->main_irg = main_irg;
162 ir_type *(get_glob_type)(void) {
163 return _get_glob_type();
166 ir_type *(get_tls_type)(void) {
167 return _get_tls_type();
170 /* Adds irg to the list of ir graphs in irp. */
171 void add_irp_irg(ir_graph *irg) {
173 assert(irp && irp->graphs);
174 ARR_APP1(ir_graph *, irp->graphs, irg);
177 /* Removes irg from the list or irgs, shrinks the list by one. */
178 void remove_irp_irg_from_list(ir_graph *irg){
182 for (i = 0; i < (ARR_LEN (irp->graphs)); i++) {
183 if (irp->graphs[i] == irg) {
185 for(; i < (ARR_LEN (irp->graphs)) - 1; i++) {
186 irp->graphs[i] = irp->graphs[i+1];
188 ARR_SETLEN(ir_graph*, irp->graphs, (ARR_LEN(irp->graphs)) - 1);
193 for (i = 0; i < (ARR_LEN (irp->pseudo_graphs)); i++) {
194 if (irp->pseudo_graphs[i] == irg) {
195 for(; i < (ARR_LEN (irp->pseudo_graphs)) - 1; i++) {
196 irp->pseudo_graphs[i] = irp->pseudo_graphs[i+1];
198 ARR_SETLEN(ir_graph*, irp->pseudo_graphs, (ARR_LEN(irp->pseudo_graphs)) - 1);
205 /* Removes irg from the list or irgs, shrinks the list by one. */
206 void remove_irp_irg(ir_graph *irg){
209 remove_irp_irg_from_list(irg);
212 int (get_irp_n_irgs)(void) {
213 return _get_irp_n_irgs();
216 ir_graph *(get_irp_irg)(int pos){
217 return _get_irp_irg(pos);
220 void set_irp_irg(int pos, ir_graph *irg) {
222 assert(pos < (ARR_LEN(irp->graphs)));
223 irp->graphs[pos] = irg;
226 /* Gets the number of graphs _and_ pseudo graphs. */
227 int get_irp_n_allirgs(void) {
228 /* We can not call get_irp_n_irgs, as we end up in a recursion ... */
229 return ARR_LEN(irp->graphs) + get_irp_n_pseudo_irgs();
232 /* Returns the ir graph at position pos of all graphs (including
233 pseudo graphs). Visits first graphs, then pseudo graphs. */
234 ir_graph *get_irp_allirg(int pos) {
235 int n_irgs = ARR_LEN(irp->graphs);
238 return irp->graphs[pos];
240 return get_irp_pseudo_irg(pos-n_irgs);
244 /* Adds type to the list of types in irp. */
245 void add_irp_type(ir_type *typ) {
248 ARR_APP1 (ir_type *, irp->types, typ);
251 /* Remove type form the list of types in irp. */
252 void remove_irp_type(ir_type *typ) {
256 for (i = ARR_LEN(irp->types) -1; i >= 0; i--) {
257 if (irp->types[i] == typ) {
258 for(; i < (ARR_LEN(irp->types)) - 1; i++) {
259 irp->types[i] = irp->types[i+1];
261 ARR_SETLEN(ir_type *, irp->types, (ARR_LEN(irp->types)) - 1);
267 int (get_irp_n_types) (void) {
268 return _get_irp_n_types();
271 ir_type *(get_irp_type) (int pos) {
272 return _get_irp_type(pos);
275 void set_irp_type(int pos, ir_type *typ) {
277 assert(pos < (ARR_LEN((irp)->types)));
278 irp->types[pos] = typ;
281 /* Returns the number of all modes in the irp. */
282 int (get_irp_n_modes)(void) {
283 return _get_irp_n_modes();
286 /* Returns the mode at position pos in the irp. */
287 ir_mode *(get_irp_mode)(int pos) {
288 return _get_irp_mode(pos);
291 /* Adds mode to the list of modes in irp. */
292 void add_irp_mode(ir_mode *mode) {
293 assert(mode != NULL);
295 ARR_APP1(ir_mode *, irp->modes, mode);
298 /* Adds opcode to the list of opcodes in irp. */
299 void add_irp_opcode(ir_op *opcode) {
300 assert(opcode != NULL);
302 assert(opcode->code == (unsigned) ARR_LEN(irp->opcodes) && "new_ir_op() called in wrong order");
303 ARR_APP1(ir_op *, irp->opcodes, opcode);
306 /* Removes opcode from the list of opcodes and shrinks the list by one. */
307 void remove_irp_opcode(ir_op *opcode) {
311 for (i = ARR_LEN(irp->opcodes) -1; i >= 0; i--) {
312 if (irp->opcodes[i] == opcode) {
313 for (; i < (ARR_LEN(irp->opcodes)) - 1; i++) {
314 irp->opcodes[i] = irp->opcodes[i+1];
316 ARR_SETLEN(ir_op *, irp->opcodes, (ARR_LEN(irp->opcodes)) - 1);
322 /* Returns the number of all opcodes in the irp. */
323 int (get_irp_n_opcodes)(void) {
324 return _get_irp_n_opcodes();
327 /* Returns the opcode at position pos in the irp. */
328 ir_op *(get_irp_opcode)(int pos) {
329 return _get_irp_opcode(pos);
332 /* Sets the generic function pointer of all opcodes to NULL */
333 void clear_irp_opcodes_generic_func(void) {
336 for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
337 ir_op *op = get_irp_opcode(i);
338 op->ops.generic = (op_func)NULL;
342 /*- File name / executable name or the like -*/
343 void set_irp_prog_name(ident *name) {
346 int irp_prog_name_is_set(void) {
347 return irp->name != new_id_from_str(INITAL_PROG_NAME);
349 ident *get_irp_prog_ident(void) {
352 const char *get_irp_prog_name(void) {
353 return get_id_str(irp->name);
357 ir_graph *(get_const_code_irg)(void) {
358 return _get_const_code_irg();
361 irg_phase_state get_irp_phase_state(void) {
362 return irp->phase_state;
365 void set_irp_phase_state(irg_phase_state s) {
366 irp->phase_state = s;
369 irg_outs_state get_irp_ip_outs_state(void) {
370 return irp->outs_state;
373 void set_irp_ip_outs_inconsistent(void) {
374 irp->outs_state = outs_inconsistent;
377 void set_irp_ip_outedges(ir_node ** ip_outedges) {
378 irp->ip_outedges = ip_outedges;
381 ir_node** get_irp_ip_outedges(void) {
382 return irp->ip_outedges;
386 irg_callee_info_state get_irp_callee_info_state(void) {
387 return irp->callee_info_state;
390 void set_irp_callee_info_state(irg_callee_info_state s) {
391 irp->callee_info_state = s;
394 /* Returns a new, unique exception region number. */
395 ir_exc_region_t (get_irp_next_region_nr)(void) {
396 return _get_irp_next_region_nr();
399 /* Returns a new, unique label number. */
400 ir_label_t (get_irp_next_label_nr)(void) {
401 return _get_irp_next_label_nr();