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 irp->const_code_irg = new_const_code_irg();
78 irp->phase_state = phase_building;
79 irp->outs_state = outs_none;
80 irp->ip_outedges = NULL;
81 irp->trouts_state = outs_none;
82 irp->class_cast_state = ir_class_casts_transitive;
87 /* initializes ir_prog. Constructs only the basic lists */
88 void init_irprog_1(void) {
89 new_incomplete_ir_prog();
92 /* Completes ir_prog. */
93 void init_irprog_2(void) {
94 complete_ir_prog(irp);
97 /* Create a new ir prog. Automatically called by init_firm through
99 ir_prog *new_ir_prog (void) {
100 return complete_ir_prog(new_incomplete_ir_prog());
103 /* frees all memory used by irp. Types in type list, irgs in irg
104 list and entities in global type must be freed by hand before. */
105 void free_ir_prog(void) {
107 free_type(irp->glob_type);
109 free_type(irp->tls_type);
111 /* @@@ * free_ir_graph(irp->const_code_irg); * ?? End has no in?? */
112 DEL_ARR_F(irp->graphs);
113 DEL_ARR_F(irp->pseudo_graphs);
114 DEL_ARR_F(irp->types);
115 DEL_ARR_F(irp->modes);
116 DEL_ARR_F(irp->opcodes);
119 irp->const_code_irg = NULL;
123 /*- Functions to access the fields of ir_prog -*/
126 /* Access the main routine of the compiled program. */
127 ir_graph *get_irp_main_irg(void) {
129 return irp->main_irg;
132 void set_irp_main_irg(ir_graph *main_irg) {
134 irp->main_irg = main_irg;
137 ir_type *(get_glob_type)(void) {
138 return _get_glob_type();
141 ir_type *(get_tls_type)(void) {
142 return _get_tls_type();
145 /* Adds irg to the list of ir graphs in irp. */
146 void add_irp_irg(ir_graph *irg) {
147 assert (irg != NULL);
148 assert(irp && irp->graphs);
149 ARR_APP1 (ir_graph *, irp->graphs, irg);
152 /* Removes irg from the list or irgs, shrinks the list by one. */
153 void remove_irp_irg_from_list(ir_graph *irg){
156 for (i = 0; i < (ARR_LEN (irp->graphs)); i++) {
157 if (irp->graphs[i] == irg) {
159 for(; i < (ARR_LEN (irp->graphs)) - 1; i++) {
160 irp->graphs[i] = irp->graphs[i+1];
162 ARR_SETLEN(ir_graph*, irp->graphs, (ARR_LEN(irp->graphs)) - 1);
167 for (i = 0; i < (ARR_LEN (irp->pseudo_graphs)); i++) {
168 if (irp->pseudo_graphs[i] == irg) {
169 for(; i < (ARR_LEN (irp->pseudo_graphs)) - 1; i++) {
170 irp->pseudo_graphs[i] = irp->pseudo_graphs[i+1];
172 ARR_SETLEN(ir_graph*, irp->pseudo_graphs, (ARR_LEN(irp->pseudo_graphs)) - 1);
179 /* Removes irg from the list or irgs, shrinks the list by one. */
180 void remove_irp_irg(ir_graph *irg){
183 remove_irp_irg_from_list(irg);
186 int (get_irp_n_irgs)(void) {
187 return _get_irp_n_irgs();
190 ir_graph *(get_irp_irg)(int pos){
191 return _get_irp_irg(pos);
194 void set_irp_irg(int pos, ir_graph *irg) {
196 assert (pos < (ARR_LEN(irp->graphs)));
197 irp->graphs[pos] = irg;
200 /* Gets the number of graphs _and_ pseudo graphs. */
201 int get_irp_n_allirgs(void) {
202 /* We can not call get_irp_n_irgs, as we end up in a recursion ... */
203 return ARR_LEN(irp->graphs) + get_irp_n_pseudo_irgs();
206 /* Returns the ir graph at position pos of all graphs (including
207 pseudo graphs). Visits first graphs, then pseudo graphs. */
208 ir_graph *get_irp_allirg(int pos) {
209 int n_irgs = ARR_LEN(irp->graphs);
212 return irp->graphs[pos];
214 return get_irp_pseudo_irg(pos-n_irgs);
218 /* Adds type to the list of types in irp. */
219 void add_irp_type(ir_type *typ) {
220 assert (typ != NULL);
222 ARR_APP1 (ir_type *, irp->types, typ);
225 /* Remove type form the list of types in irp. */
226 void remove_irp_type(ir_type *typ) {
230 for (i = ARR_LEN(irp->types) -1; i >= 0; i--) {
231 if (irp->types[i] == typ) {
232 for(; i < (ARR_LEN(irp->types)) - 1; i++) {
233 irp->types[i] = irp->types[i+1];
235 ARR_SETLEN(ir_type *, irp->types, (ARR_LEN(irp->types)) - 1);
241 int (get_irp_n_types) (void) {
242 return _get_irp_n_types();
245 ir_type *(get_irp_type) (int pos) {
246 return _get_irp_type(pos);
249 void set_irp_type(int pos, ir_type *typ) {
251 assert (pos < (ARR_LEN((irp)->types)));
252 irp->types[pos] = typ;
255 /* Returns the number of all modes in the irp. */
256 int (get_irp_n_modes)(void) {
257 return _get_irp_n_modes();
260 /* Returns the mode at position pos in the irp. */
261 ir_mode *(get_irp_mode)(int pos) {
262 return _get_irp_mode(pos);
265 /* Adds mode to the list of modes in irp. */
266 void add_irp_mode(ir_mode *mode) {
267 assert(mode != NULL);
269 ARR_APP1(ir_mode *, irp->modes, mode);
272 /* Adds opcode to the list of opcodes in irp. */
273 void add_irp_opcode(ir_op *opcode) {
274 assert(opcode != NULL);
276 ARR_APP1(ir_op *, irp->opcodes, opcode);
279 /* Removes opcode from the list of opcodes and shrinks the list by one. */
280 void remove_irp_opcode(ir_op *opcode) {
284 for (i = ARR_LEN(irp->opcodes) -1; i >= 0; i--) {
285 if (irp->opcodes[i] == opcode) {
286 for(; i < (ARR_LEN(irp->opcodes)) - 1; i++) {
287 irp->opcodes[i] = irp->opcodes[i+1];
289 ARR_SETLEN(ir_op *, irp->opcodes, (ARR_LEN(irp->opcodes)) - 1);
295 /* Returns the number of all opcodes in the irp. */
296 int (get_irp_n_opcodes)(void) {
297 return _get_irp_n_opcodes();
300 /* Returns the opcode at position pos in the irp. */
301 ir_op *(get_irp_opcode)(int pos) {
302 return _get_irp_opcode(pos);
305 /* Sets the generic function pointer of all opcodes to NULL */
306 void clear_irp_opcodes_generic_func(void) {
309 for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
310 ir_op *op = get_irp_opcode(i);
311 op->ops.generic = (op_func)NULL;
315 /*- File name / executable name or the like -*/
316 void set_irp_prog_name(ident *name) {
319 int irp_prog_name_is_set(void) {
320 return irp->name != new_id_from_str(INITAL_PROG_NAME);
322 ident *get_irp_prog_ident(void) {
325 const char *get_irp_prog_name(void) {
326 return get_id_str(irp->name);
330 ir_graph *(get_const_code_irg)(void) {
331 return _get_const_code_irg();
334 irg_phase_state get_irp_phase_state(void) {
335 return irp->phase_state;
337 void set_irp_phase_state(irg_phase_state s) {
338 irp->phase_state = s;
341 irg_outs_state get_irp_ip_outs_state(void) {
342 return irp->outs_state;
345 void set_irp_ip_outs_inconsistent(void) {
346 irp->outs_state = outs_inconsistent;
349 void set_irp_ip_outedges(ir_node ** ip_outedges)
351 irp->ip_outedges = ip_outedges;
354 ir_node** get_irp_ip_outedges(void)
356 return irp->ip_outedges;
360 irg_callee_info_state get_irp_callee_info_state(void) {
361 return irp->callee_info_state;
364 void set_irp_callee_info_state(irg_callee_info_state s) {
365 irp->callee_info_state = s;