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 irp->const_code_irg = new_const_code_irg();
82 irp->phase_state = phase_building;
83 irp->outs_state = outs_none;
84 irp->ip_outedges = NULL;
85 irp->trouts_state = outs_none;
86 irp->class_cast_state = ir_class_casts_transitive;
91 /* initializes ir_prog. Constructs only the basic lists */
92 void init_irprog_1(void) {
93 new_incomplete_ir_prog();
96 /* Completes ir_prog. */
97 void init_irprog_2(void) {
98 complete_ir_prog(irp);
101 /* Create a new ir prog. Automatically called by init_firm through
103 ir_prog *new_ir_prog (void) {
104 return complete_ir_prog(new_incomplete_ir_prog());
107 /* frees all memory used by irp. Types in type list, irgs in irg
108 list and entities in global type must be freed by hand before. */
109 void free_ir_prog(void) {
111 free_type(irp->glob_type);
113 free_type(irp->tls_type);
115 /* @@@ * free_ir_graph(irp->const_code_irg); * ?? End has no in?? */
116 DEL_ARR_F(irp->graphs);
117 DEL_ARR_F(irp->pseudo_graphs);
118 DEL_ARR_F(irp->types);
119 DEL_ARR_F(irp->modes);
120 DEL_ARR_F(irp->opcodes);
123 irp->const_code_irg = NULL;
127 /*- Functions to access the fields of ir_prog -*/
130 /* Access the main routine of the compiled program. */
131 ir_graph *get_irp_main_irg(void) {
133 return irp->main_irg;
136 void set_irp_main_irg(ir_graph *main_irg) {
138 irp->main_irg = main_irg;
141 ir_type *(get_glob_type)(void) {
142 return _get_glob_type();
145 ir_type *(get_tls_type)(void) {
146 return _get_tls_type();
149 /* Adds irg to the list of ir graphs in irp. */
150 void add_irp_irg(ir_graph *irg) {
151 assert (irg != NULL);
152 assert(irp && irp->graphs);
153 ARR_APP1 (ir_graph *, irp->graphs, irg);
156 /* Removes irg from the list or irgs, shrinks the list by one. */
157 void remove_irp_irg_from_list(ir_graph *irg){
160 for (i = 0; i < (ARR_LEN (irp->graphs)); i++) {
161 if (irp->graphs[i] == irg) {
163 for(; i < (ARR_LEN (irp->graphs)) - 1; i++) {
164 irp->graphs[i] = irp->graphs[i+1];
166 ARR_SETLEN(ir_graph*, irp->graphs, (ARR_LEN(irp->graphs)) - 1);
171 for (i = 0; i < (ARR_LEN (irp->pseudo_graphs)); i++) {
172 if (irp->pseudo_graphs[i] == irg) {
173 for(; i < (ARR_LEN (irp->pseudo_graphs)) - 1; i++) {
174 irp->pseudo_graphs[i] = irp->pseudo_graphs[i+1];
176 ARR_SETLEN(ir_graph*, irp->pseudo_graphs, (ARR_LEN(irp->pseudo_graphs)) - 1);
183 /* Removes irg from the list or irgs, shrinks the list by one. */
184 void remove_irp_irg(ir_graph *irg){
187 remove_irp_irg_from_list(irg);
190 int (get_irp_n_irgs)(void) {
191 return _get_irp_n_irgs();
194 ir_graph *(get_irp_irg)(int pos){
195 return _get_irp_irg(pos);
198 void set_irp_irg(int pos, ir_graph *irg) {
200 assert(pos < (ARR_LEN(irp->graphs)));
201 irp->graphs[pos] = irg;
204 /* Gets the number of graphs _and_ pseudo graphs. */
205 int get_irp_n_allirgs(void) {
206 /* We can not call get_irp_n_irgs, as we end up in a recursion ... */
207 return ARR_LEN(irp->graphs) + get_irp_n_pseudo_irgs();
210 /* Returns the ir graph at position pos of all graphs (including
211 pseudo graphs). Visits first graphs, then pseudo graphs. */
212 ir_graph *get_irp_allirg(int pos) {
213 int n_irgs = ARR_LEN(irp->graphs);
216 return irp->graphs[pos];
218 return get_irp_pseudo_irg(pos-n_irgs);
222 /* Adds type to the list of types in irp. */
223 void add_irp_type(ir_type *typ) {
226 ARR_APP1 (ir_type *, irp->types, typ);
229 /* Remove type form the list of types in irp. */
230 void remove_irp_type(ir_type *typ) {
234 for (i = ARR_LEN(irp->types) -1; i >= 0; i--) {
235 if (irp->types[i] == typ) {
236 for(; i < (ARR_LEN(irp->types)) - 1; i++) {
237 irp->types[i] = irp->types[i+1];
239 ARR_SETLEN(ir_type *, irp->types, (ARR_LEN(irp->types)) - 1);
245 int (get_irp_n_types) (void) {
246 return _get_irp_n_types();
249 ir_type *(get_irp_type) (int pos) {
250 return _get_irp_type(pos);
253 void set_irp_type(int pos, ir_type *typ) {
255 assert(pos < (ARR_LEN((irp)->types)));
256 irp->types[pos] = typ;
259 /* Returns the number of all modes in the irp. */
260 int (get_irp_n_modes)(void) {
261 return _get_irp_n_modes();
264 /* Returns the mode at position pos in the irp. */
265 ir_mode *(get_irp_mode)(int pos) {
266 return _get_irp_mode(pos);
269 /* Adds mode to the list of modes in irp. */
270 void add_irp_mode(ir_mode *mode) {
271 assert(mode != NULL);
273 ARR_APP1(ir_mode *, irp->modes, mode);
276 /* Adds opcode to the list of opcodes in irp. */
277 void add_irp_opcode(ir_op *opcode) {
278 assert(opcode != NULL);
280 ARR_APP1(ir_op *, irp->opcodes, opcode);
283 /* Removes opcode from the list of opcodes and shrinks the list by one. */
284 void remove_irp_opcode(ir_op *opcode) {
288 for (i = ARR_LEN(irp->opcodes) -1; i >= 0; i--) {
289 if (irp->opcodes[i] == opcode) {
290 for (; i < (ARR_LEN(irp->opcodes)) - 1; i++) {
291 irp->opcodes[i] = irp->opcodes[i+1];
293 ARR_SETLEN(ir_op *, irp->opcodes, (ARR_LEN(irp->opcodes)) - 1);
299 /* Returns the number of all opcodes in the irp. */
300 int (get_irp_n_opcodes)(void) {
301 return _get_irp_n_opcodes();
304 /* Returns the opcode at position pos in the irp. */
305 ir_op *(get_irp_opcode)(int pos) {
306 return _get_irp_opcode(pos);
309 /* Sets the generic function pointer of all opcodes to NULL */
310 void clear_irp_opcodes_generic_func(void) {
313 for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
314 ir_op *op = get_irp_opcode(i);
315 op->ops.generic = (op_func)NULL;
319 /*- File name / executable name or the like -*/
320 void set_irp_prog_name(ident *name) {
323 int irp_prog_name_is_set(void) {
324 return irp->name != new_id_from_str(INITAL_PROG_NAME);
326 ident *get_irp_prog_ident(void) {
329 const char *get_irp_prog_name(void) {
330 return get_id_str(irp->name);
334 ir_graph *(get_const_code_irg)(void) {
335 return _get_const_code_irg();
338 irg_phase_state get_irp_phase_state(void) {
339 return irp->phase_state;
341 void set_irp_phase_state(irg_phase_state s) {
342 irp->phase_state = s;
345 irg_outs_state get_irp_ip_outs_state(void) {
346 return irp->outs_state;
349 void set_irp_ip_outs_inconsistent(void) {
350 irp->outs_state = outs_inconsistent;
353 void set_irp_ip_outedges(ir_node ** ip_outedges) {
354 irp->ip_outedges = ip_outedges;
357 ir_node** get_irp_ip_outedges(void) {
358 return irp->ip_outedges;
362 irg_callee_info_state get_irp_callee_info_state(void) {
363 return irp->callee_info_state;
366 void set_irp_callee_info_state(irg_callee_info_state s) {
367 irp->callee_info_state = s;