3 * File name: ir/ir/irprog.c
4 * Purpose: Entry point to the representation of a whole program.
5 * Author: Goetz Lindenmaier
9 * Copyright: (c) 2000-2007 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
22 #include "irgraph_t.h"
23 #include "pseudo_irg.h"
30 /** The name of the Global Type. */
31 #define GLOBAL_TYPE_NAME "GlobalType"
32 /** The name of the Thread Local STorage Type. */
33 #define TLS_TYPE_NAME "TLS"
34 /** The initial name of the irp program. */
35 #define INITAL_PROG_NAME "no_name_set"
37 /* A variable from where everything in the ir can be accessed. */
39 ir_prog *get_irp(void) { return irp; }
42 * Create a new incomplete ir_prog.
44 static ir_prog *new_incomplete_ir_prog (void) {
47 res = xmalloc(sizeof(*res));
48 memset(res, 0, sizeof(*res));
51 res->kind = k_ir_prog;
52 res->graphs = NEW_ARR_F(ir_graph *, 0);
53 res->pseudo_graphs = NEW_ARR_F(ir_graph *, 0);
54 res->types = NEW_ARR_F(ir_type *, 0);
55 res->modes = NEW_ARR_F(ir_mode *, 0);
56 res->opcodes = NEW_ARR_F(ir_op *, 0);
65 /** Completes an incomplete irprog. */
66 static ir_prog *complete_ir_prog(ir_prog *irp) {
67 #define X(s) s, sizeof(s)-1
68 irp->name = new_id_from_chars(X(INITAL_PROG_NAME));
70 irp->glob_type = new_type_class(new_id_from_chars(X(GLOBAL_TYPE_NAME)));
71 irp->tls_type = new_type_struct(new_id_from_chars(X(TLS_TYPE_NAME)));
72 /* Remove these types from type list. Must be treated differently than
74 remove_irp_type(irp->glob_type);
75 remove_irp_type(irp->tls_type);
77 /* Set these flags for debugging. */
78 irp->glob_type->flags |= tf_global_type;
79 irp->tls_type->flags |= tf_tls_type;
81 /* The global type is a class, but we cannot derive from it, so set
82 the final property to assist optimizations that checks for it. */
83 set_class_final(irp->glob_type, 1);
85 irp->const_code_irg = new_const_code_irg();
87 irp->phase_state = phase_building;
88 irp->outs_state = outs_none;
89 irp->ip_outedges = NULL;
90 irp->trouts_state = outs_none;
91 irp->class_cast_state = ir_class_casts_transitive;
92 irp->globals_adr_taken_state = ir_address_taken_not_computed;
97 /* initializes ir_prog. Constructs only the basic lists */
98 void init_irprog_1(void) {
99 new_incomplete_ir_prog();
102 /* Completes ir_prog. */
103 void init_irprog_2(void) {
104 complete_ir_prog(irp);
107 /* Create a new ir prog. Automatically called by init_firm through
109 ir_prog *new_ir_prog (void) {
110 return complete_ir_prog(new_incomplete_ir_prog());
113 /* frees all memory used by irp. Types in type list, irgs in irg
114 list and entities in global type must be freed by hand before. */
115 void free_ir_prog(void) {
117 free_type(irp->glob_type);
119 free_type(irp->tls_type);
121 /* @@@ * free_ir_graph(irp->const_code_irg); * ?? End has no in?? */
122 DEL_ARR_F(irp->graphs);
123 DEL_ARR_F(irp->pseudo_graphs);
124 DEL_ARR_F(irp->types);
125 DEL_ARR_F(irp->modes);
126 DEL_ARR_F(irp->opcodes);
129 irp->const_code_irg = NULL;
133 /*- Functions to access the fields of ir_prog -*/
136 /* Access the main routine of the compiled program. */
137 ir_graph *get_irp_main_irg(void) {
139 return irp->main_irg;
142 void set_irp_main_irg(ir_graph *main_irg) {
144 irp->main_irg = main_irg;
147 ir_type *(get_glob_type)(void) {
148 return _get_glob_type();
151 ir_type *(get_tls_type)(void) {
152 return _get_tls_type();
155 /* Adds irg to the list of ir graphs in irp. */
156 void add_irp_irg(ir_graph *irg) {
157 assert (irg != NULL);
158 assert(irp && irp->graphs);
159 ARR_APP1 (ir_graph *, irp->graphs, irg);
162 /* Removes irg from the list or irgs, shrinks the list by one. */
163 void remove_irp_irg_from_list(ir_graph *irg){
166 for (i = 0; i < (ARR_LEN (irp->graphs)); i++) {
167 if (irp->graphs[i] == irg) {
169 for(; i < (ARR_LEN (irp->graphs)) - 1; i++) {
170 irp->graphs[i] = irp->graphs[i+1];
172 ARR_SETLEN(ir_graph*, irp->graphs, (ARR_LEN(irp->graphs)) - 1);
177 for (i = 0; i < (ARR_LEN (irp->pseudo_graphs)); i++) {
178 if (irp->pseudo_graphs[i] == irg) {
179 for(; i < (ARR_LEN (irp->pseudo_graphs)) - 1; i++) {
180 irp->pseudo_graphs[i] = irp->pseudo_graphs[i+1];
182 ARR_SETLEN(ir_graph*, irp->pseudo_graphs, (ARR_LEN(irp->pseudo_graphs)) - 1);
189 /* Removes irg from the list or irgs, shrinks the list by one. */
190 void remove_irp_irg(ir_graph *irg){
193 remove_irp_irg_from_list(irg);
196 int (get_irp_n_irgs)(void) {
197 return _get_irp_n_irgs();
200 ir_graph *(get_irp_irg)(int pos){
201 return _get_irp_irg(pos);
204 void set_irp_irg(int pos, ir_graph *irg) {
206 assert(pos < (ARR_LEN(irp->graphs)));
207 irp->graphs[pos] = irg;
210 /* Gets the number of graphs _and_ pseudo graphs. */
211 int get_irp_n_allirgs(void) {
212 /* We can not call get_irp_n_irgs, as we end up in a recursion ... */
213 return ARR_LEN(irp->graphs) + get_irp_n_pseudo_irgs();
216 /* Returns the ir graph at position pos of all graphs (including
217 pseudo graphs). Visits first graphs, then pseudo graphs. */
218 ir_graph *get_irp_allirg(int pos) {
219 int n_irgs = ARR_LEN(irp->graphs);
222 return irp->graphs[pos];
224 return get_irp_pseudo_irg(pos-n_irgs);
228 /* Adds type to the list of types in irp. */
229 void add_irp_type(ir_type *typ) {
232 ARR_APP1 (ir_type *, irp->types, typ);
235 /* Remove type form the list of types in irp. */
236 void remove_irp_type(ir_type *typ) {
240 for (i = ARR_LEN(irp->types) -1; i >= 0; i--) {
241 if (irp->types[i] == typ) {
242 for(; i < (ARR_LEN(irp->types)) - 1; i++) {
243 irp->types[i] = irp->types[i+1];
245 ARR_SETLEN(ir_type *, irp->types, (ARR_LEN(irp->types)) - 1);
251 int (get_irp_n_types) (void) {
252 return _get_irp_n_types();
255 ir_type *(get_irp_type) (int pos) {
256 return _get_irp_type(pos);
259 void set_irp_type(int pos, ir_type *typ) {
261 assert(pos < (ARR_LEN((irp)->types)));
262 irp->types[pos] = typ;
265 /* Returns the number of all modes in the irp. */
266 int (get_irp_n_modes)(void) {
267 return _get_irp_n_modes();
270 /* Returns the mode at position pos in the irp. */
271 ir_mode *(get_irp_mode)(int pos) {
272 return _get_irp_mode(pos);
275 /* Adds mode to the list of modes in irp. */
276 void add_irp_mode(ir_mode *mode) {
277 assert(mode != NULL);
279 ARR_APP1(ir_mode *, irp->modes, mode);
282 /* Adds opcode to the list of opcodes in irp. */
283 void add_irp_opcode(ir_op *opcode) {
284 assert(opcode != NULL);
286 ARR_APP1(ir_op *, irp->opcodes, opcode);
289 /* Removes opcode from the list of opcodes and shrinks the list by one. */
290 void remove_irp_opcode(ir_op *opcode) {
294 for (i = ARR_LEN(irp->opcodes) -1; i >= 0; i--) {
295 if (irp->opcodes[i] == opcode) {
296 for (; i < (ARR_LEN(irp->opcodes)) - 1; i++) {
297 irp->opcodes[i] = irp->opcodes[i+1];
299 ARR_SETLEN(ir_op *, irp->opcodes, (ARR_LEN(irp->opcodes)) - 1);
305 /* Returns the number of all opcodes in the irp. */
306 int (get_irp_n_opcodes)(void) {
307 return _get_irp_n_opcodes();
310 /* Returns the opcode at position pos in the irp. */
311 ir_op *(get_irp_opcode)(int pos) {
312 return _get_irp_opcode(pos);
315 /* Sets the generic function pointer of all opcodes to NULL */
316 void clear_irp_opcodes_generic_func(void) {
319 for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
320 ir_op *op = get_irp_opcode(i);
321 op->ops.generic = (op_func)NULL;
325 /*- File name / executable name or the like -*/
326 void set_irp_prog_name(ident *name) {
329 int irp_prog_name_is_set(void) {
330 return irp->name != new_id_from_str(INITAL_PROG_NAME);
332 ident *get_irp_prog_ident(void) {
335 const char *get_irp_prog_name(void) {
336 return get_id_str(irp->name);
340 ir_graph *(get_const_code_irg)(void) {
341 return _get_const_code_irg();
344 irg_phase_state get_irp_phase_state(void) {
345 return irp->phase_state;
347 void set_irp_phase_state(irg_phase_state s) {
348 irp->phase_state = s;
351 irg_outs_state get_irp_ip_outs_state(void) {
352 return irp->outs_state;
355 void set_irp_ip_outs_inconsistent(void) {
356 irp->outs_state = outs_inconsistent;
359 void set_irp_ip_outedges(ir_node ** ip_outedges) {
360 irp->ip_outedges = ip_outedges;
363 ir_node** get_irp_ip_outedges(void) {
364 return irp->ip_outedges;
368 irg_callee_info_state get_irp_callee_info_state(void) {
369 return irp->callee_info_state;
372 void set_irp_callee_info_state(irg_callee_info_state s) {
373 irp->callee_info_state = s;