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-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
22 #include "irgraph_t.h"
23 #include "pseudo_irg.h"
29 /** The name of the Global Type. */
30 #define GLOBAL_TYPE_NAME "GlobalType"
31 /** The name of the Thread Local STorage Type. */
32 #define TLS_TYPE_NAME "TLS"
33 /** The initial name of the irp program. */
34 #define INITAL_PROG_NAME "no_name_set"
36 /* A variable from where everything in the ir can be accessed. */
38 ir_prog *get_irp(void) { return irp; }
41 * Create a new incomplete ir_prog.
43 static ir_prog *new_incomplete_ir_prog (void) {
46 res = xmalloc(sizeof(*res));
47 memset(res, 0, sizeof(*res));
50 res->kind = k_ir_prog;
51 res->graphs = NEW_ARR_F(ir_graph *, 0);
52 res->pseudo_graphs = NEW_ARR_F(ir_graph *, 0);
53 res->types = NEW_ARR_F(ir_type *, 0);
54 res->modes = NEW_ARR_F(ir_mode *, 0);
55 res->opcodes = NEW_ARR_F(ir_op *, 0);
64 /** Completes an incomplete irprog. */
65 static ir_prog *complete_ir_prog(ir_prog *irp) {
66 #define X(s) s, sizeof(s)-1
67 irp->name = new_id_from_chars(X(INITAL_PROG_NAME));
69 irp->glob_type = new_type_class(new_id_from_chars(X(GLOBAL_TYPE_NAME)));
70 irp->tls_type = new_type_struct(new_id_from_chars(X(TLS_TYPE_NAME)));
71 /* Remove these types from type list. Must be treated differently than
73 remove_irp_type(irp->glob_type);
74 remove_irp_type(irp->tls_type);
76 /* Set these flags for debugging. */
77 irp->glob_type->flags |= tf_global_type;
78 irp->tls_type->flags |= tf_tls_type;
80 /* The global type is a class, but we cannot derive from it, so set
81 the final property to assist optimizations that checks for it. */
82 set_class_final(irp->glob_type, 1);
84 irp->const_code_irg = new_const_code_irg();
86 irp->phase_state = phase_building;
87 irp->outs_state = outs_none;
88 irp->ip_outedges = NULL;
89 irp->trouts_state = outs_none;
90 irp->class_cast_state = ir_class_casts_transitive;
95 /* initializes ir_prog. Constructs only the basic lists */
96 void init_irprog_1(void) {
97 new_incomplete_ir_prog();
100 /* Completes ir_prog. */
101 void init_irprog_2(void) {
102 complete_ir_prog(irp);
105 /* Create a new ir prog. Automatically called by init_firm through
107 ir_prog *new_ir_prog (void) {
108 return complete_ir_prog(new_incomplete_ir_prog());
111 /* frees all memory used by irp. Types in type list, irgs in irg
112 list and entities in global type must be freed by hand before. */
113 void free_ir_prog(void) {
115 free_type(irp->glob_type);
117 free_type(irp->tls_type);
119 /* @@@ * free_ir_graph(irp->const_code_irg); * ?? End has no in?? */
120 DEL_ARR_F(irp->graphs);
121 DEL_ARR_F(irp->pseudo_graphs);
122 DEL_ARR_F(irp->types);
123 DEL_ARR_F(irp->modes);
124 DEL_ARR_F(irp->opcodes);
127 irp->const_code_irg = NULL;
131 /*- Functions to access the fields of ir_prog -*/
134 /* Access the main routine of the compiled program. */
135 ir_graph *get_irp_main_irg(void) {
137 return irp->main_irg;
140 void set_irp_main_irg(ir_graph *main_irg) {
142 irp->main_irg = main_irg;
145 ir_type *(get_glob_type)(void) {
146 return _get_glob_type();
149 ir_type *(get_tls_type)(void) {
150 return _get_tls_type();
153 /* Adds irg to the list of ir graphs in irp. */
154 void add_irp_irg(ir_graph *irg) {
155 assert (irg != NULL);
156 assert(irp && irp->graphs);
157 ARR_APP1 (ir_graph *, irp->graphs, irg);
160 /* Removes irg from the list or irgs, shrinks the list by one. */
161 void remove_irp_irg_from_list(ir_graph *irg){
164 for (i = 0; i < (ARR_LEN (irp->graphs)); i++) {
165 if (irp->graphs[i] == irg) {
167 for(; i < (ARR_LEN (irp->graphs)) - 1; i++) {
168 irp->graphs[i] = irp->graphs[i+1];
170 ARR_SETLEN(ir_graph*, irp->graphs, (ARR_LEN(irp->graphs)) - 1);
175 for (i = 0; i < (ARR_LEN (irp->pseudo_graphs)); i++) {
176 if (irp->pseudo_graphs[i] == irg) {
177 for(; i < (ARR_LEN (irp->pseudo_graphs)) - 1; i++) {
178 irp->pseudo_graphs[i] = irp->pseudo_graphs[i+1];
180 ARR_SETLEN(ir_graph*, irp->pseudo_graphs, (ARR_LEN(irp->pseudo_graphs)) - 1);
187 /* Removes irg from the list or irgs, shrinks the list by one. */
188 void remove_irp_irg(ir_graph *irg){
191 remove_irp_irg_from_list(irg);
194 int (get_irp_n_irgs)(void) {
195 return _get_irp_n_irgs();
198 ir_graph *(get_irp_irg)(int pos){
199 return _get_irp_irg(pos);
202 void set_irp_irg(int pos, ir_graph *irg) {
204 assert(pos < (ARR_LEN(irp->graphs)));
205 irp->graphs[pos] = irg;
208 /* Gets the number of graphs _and_ pseudo graphs. */
209 int get_irp_n_allirgs(void) {
210 /* We can not call get_irp_n_irgs, as we end up in a recursion ... */
211 return ARR_LEN(irp->graphs) + get_irp_n_pseudo_irgs();
214 /* Returns the ir graph at position pos of all graphs (including
215 pseudo graphs). Visits first graphs, then pseudo graphs. */
216 ir_graph *get_irp_allirg(int pos) {
217 int n_irgs = ARR_LEN(irp->graphs);
220 return irp->graphs[pos];
222 return get_irp_pseudo_irg(pos-n_irgs);
226 /* Adds type to the list of types in irp. */
227 void add_irp_type(ir_type *typ) {
230 ARR_APP1 (ir_type *, irp->types, typ);
233 /* Remove type form the list of types in irp. */
234 void remove_irp_type(ir_type *typ) {
238 for (i = ARR_LEN(irp->types) -1; i >= 0; i--) {
239 if (irp->types[i] == typ) {
240 for(; i < (ARR_LEN(irp->types)) - 1; i++) {
241 irp->types[i] = irp->types[i+1];
243 ARR_SETLEN(ir_type *, irp->types, (ARR_LEN(irp->types)) - 1);
249 int (get_irp_n_types) (void) {
250 return _get_irp_n_types();
253 ir_type *(get_irp_type) (int pos) {
254 return _get_irp_type(pos);
257 void set_irp_type(int pos, ir_type *typ) {
259 assert(pos < (ARR_LEN((irp)->types)));
260 irp->types[pos] = typ;
263 /* Returns the number of all modes in the irp. */
264 int (get_irp_n_modes)(void) {
265 return _get_irp_n_modes();
268 /* Returns the mode at position pos in the irp. */
269 ir_mode *(get_irp_mode)(int pos) {
270 return _get_irp_mode(pos);
273 /* Adds mode to the list of modes in irp. */
274 void add_irp_mode(ir_mode *mode) {
275 assert(mode != NULL);
277 ARR_APP1(ir_mode *, irp->modes, mode);
280 /* Adds opcode to the list of opcodes in irp. */
281 void add_irp_opcode(ir_op *opcode) {
282 assert(opcode != NULL);
284 ARR_APP1(ir_op *, irp->opcodes, opcode);
287 /* Removes opcode from the list of opcodes and shrinks the list by one. */
288 void remove_irp_opcode(ir_op *opcode) {
292 for (i = ARR_LEN(irp->opcodes) -1; i >= 0; i--) {
293 if (irp->opcodes[i] == opcode) {
294 for (; i < (ARR_LEN(irp->opcodes)) - 1; i++) {
295 irp->opcodes[i] = irp->opcodes[i+1];
297 ARR_SETLEN(ir_op *, irp->opcodes, (ARR_LEN(irp->opcodes)) - 1);
303 /* Returns the number of all opcodes in the irp. */
304 int (get_irp_n_opcodes)(void) {
305 return _get_irp_n_opcodes();
308 /* Returns the opcode at position pos in the irp. */
309 ir_op *(get_irp_opcode)(int pos) {
310 return _get_irp_opcode(pos);
313 /* Sets the generic function pointer of all opcodes to NULL */
314 void clear_irp_opcodes_generic_func(void) {
317 for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
318 ir_op *op = get_irp_opcode(i);
319 op->ops.generic = (op_func)NULL;
323 /*- File name / executable name or the like -*/
324 void set_irp_prog_name(ident *name) {
327 int irp_prog_name_is_set(void) {
328 return irp->name != new_id_from_str(INITAL_PROG_NAME);
330 ident *get_irp_prog_ident(void) {
333 const char *get_irp_prog_name(void) {
334 return get_id_str(irp->name);
338 ir_graph *(get_const_code_irg)(void) {
339 return _get_const_code_irg();
342 irg_phase_state get_irp_phase_state(void) {
343 return irp->phase_state;
345 void set_irp_phase_state(irg_phase_state s) {
346 irp->phase_state = s;
349 irg_outs_state get_irp_ip_outs_state(void) {
350 return irp->outs_state;
353 void set_irp_ip_outs_inconsistent(void) {
354 irp->outs_state = outs_inconsistent;
357 void set_irp_ip_outedges(ir_node ** ip_outedges) {
358 irp->ip_outedges = ip_outedges;
361 ir_node** get_irp_ip_outedges(void) {
362 return irp->ip_outedges;
366 irg_callee_info_state get_irp_callee_info_state(void) {
367 return irp->callee_info_state;
370 void set_irp_callee_info_state(irg_callee_info_state s) {
371 irp->callee_info_state = s;