821551ebfb6aef2bf093b6a405af37274d5c589f
[libfirm] / ir / ir / irprog.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/irprog.c
4  * Purpose:     Entry point to the representation of a whole program.
5  * Author:      Goetz Lindenmaier
6  * Modified by:
7  * Created:     2000
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2000-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16
17 #ifdef HAVE_STRING_H
18 # include <string.h>
19 #endif
20
21 #include "irprog_t.h"
22 #include "irgraph_t.h"
23 #include "pseudo_irg.h"
24 #include "array.h"
25 #include "obst.h"
26 #include "typegmod.h"
27 #include "irop_t.h"
28
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"
35
36 /* A variable from where everything in the ir can be accessed. */
37 ir_prog *irp;
38 ir_prog *get_irp(void) { return irp; }
39
40 /**
41  *  Create a new incomplete ir_prog.
42  */
43 static ir_prog *new_incomplete_ir_prog (void) {
44   ir_prog *res;
45
46   res = xmalloc(sizeof(*res));
47   memset(res, 0, sizeof(*res));
48   irp = res;
49
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);
56
57 #ifdef DEBUG_libfirm
58   res->max_node_nr = 0;
59 #endif
60
61   return res;
62 }
63
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));
68
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
72      other types. */
73   remove_irp_type(irp->glob_type);
74   remove_irp_type(irp->tls_type);
75
76   /* Set these flags for debugging. */
77   irp->glob_type->flags |= tf_global_type;
78   irp->tls_type->flags  |= tf_tls_type;
79
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);
83
84   irp->const_code_irg   = new_const_code_irg();
85
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;
91
92   return irp;
93 }
94
95 /* initializes ir_prog. Constructs only the basic lists */
96 void init_irprog_1(void) {
97   new_incomplete_ir_prog();
98 }
99
100 /* Completes ir_prog. */
101 void init_irprog_2(void) {
102   complete_ir_prog(irp);
103 }
104
105 /* Create a new ir prog. Automatically called by init_firm through
106    init_irprog. */
107 ir_prog *new_ir_prog (void) {
108   return complete_ir_prog(new_incomplete_ir_prog());
109 }
110
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) {
114   if (irp->glob_type)
115     free_type(irp->glob_type);
116   if (irp->tls_type)
117     free_type(irp->tls_type);
118
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);
125
126   irp->name           = NULL;
127   irp->const_code_irg = NULL;
128   irp->kind           = k_BAD;
129 }
130
131 /*- Functions to access the fields of ir_prog -*/
132
133
134 /* Access the main routine of the compiled program. */
135 ir_graph *get_irp_main_irg(void) {
136   assert(irp);
137   return irp->main_irg;
138 }
139
140 void set_irp_main_irg(ir_graph *main_irg) {
141   assert (irp);
142   irp->main_irg = main_irg;
143 }
144
145 ir_type *(get_glob_type)(void) {
146   return _get_glob_type();
147 }
148
149 ir_type *(get_tls_type)(void) {
150   return _get_tls_type();
151 }
152
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);
158 }
159
160 /* Removes irg from the list or irgs, shrinks the list by one. */
161 void remove_irp_irg_from_list(ir_graph *irg){
162   int i, found = 0;
163   assert(irg);
164   for (i = 0; i < (ARR_LEN (irp->graphs)); i++) {
165     if (irp->graphs[i] == irg) {
166       found = 1;
167       for(; i < (ARR_LEN (irp->graphs)) - 1; i++) {
168         irp->graphs[i] = irp->graphs[i+1];
169       }
170       ARR_SETLEN(ir_graph*, irp->graphs, (ARR_LEN(irp->graphs)) - 1);
171       break;
172     }
173   }
174   if (!found) {
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];
179               }
180               ARR_SETLEN(ir_graph*, irp->pseudo_graphs, (ARR_LEN(irp->pseudo_graphs)) - 1);
181               break;
182       }
183     }
184   }
185 }
186
187 /* Removes irg from the list or irgs, shrinks the list by one. */
188 void remove_irp_irg(ir_graph *irg){
189   assert(irg);
190   free_ir_graph(irg);
191   remove_irp_irg_from_list(irg);
192 }
193
194 int (get_irp_n_irgs)(void) {
195   return _get_irp_n_irgs();
196 }
197
198 ir_graph *(get_irp_irg)(int pos){
199   return _get_irp_irg(pos);
200 }
201
202 void set_irp_irg(int pos, ir_graph *irg) {
203   assert(irp && irg);
204   assert(pos < (ARR_LEN(irp->graphs)));
205   irp->graphs[pos] = irg;
206 }
207
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();
212 }
213
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);
218   assert(0 <= pos);
219   if (pos < n_irgs) {
220     return irp->graphs[pos];
221   } else {
222     return get_irp_pseudo_irg(pos-n_irgs);
223   }
224 }
225
226 /* Adds type to the list of types in irp. */
227 void add_irp_type(ir_type *typ) {
228   assert(typ != NULL);
229   assert(irp);
230   ARR_APP1 (ir_type *, irp->types, typ);
231 }
232
233 /* Remove type form the list of types in irp. */
234 void remove_irp_type(ir_type *typ) {
235   int i;
236   assert(typ);
237
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];
242       }
243       ARR_SETLEN(ir_type *, irp->types, (ARR_LEN(irp->types)) - 1);
244       break;
245     }
246   }
247 }
248
249 int (get_irp_n_types) (void) {
250   return _get_irp_n_types();
251 }
252
253 ir_type *(get_irp_type) (int pos) {
254   return _get_irp_type(pos);
255 }
256
257 void set_irp_type(int pos, ir_type *typ) {
258   assert(irp && typ);
259   assert(pos < (ARR_LEN((irp)->types)));
260   irp->types[pos] = typ;
261 }
262
263 /* Returns the number of all modes in the irp. */
264 int (get_irp_n_modes)(void) {
265   return _get_irp_n_modes();
266 }
267
268 /* Returns the mode at position pos in the irp. */
269 ir_mode *(get_irp_mode)(int pos) {
270   return _get_irp_mode(pos);
271 }
272
273 /* Adds mode to the list of modes in irp. */
274 void add_irp_mode(ir_mode *mode) {
275   assert(mode != NULL);
276   assert(irp);
277   ARR_APP1(ir_mode *, irp->modes, mode);
278 }
279
280 /* Adds opcode to the list of opcodes in irp. */
281 void add_irp_opcode(ir_op *opcode) {
282   assert(opcode != NULL);
283   assert(irp);
284   ARR_APP1(ir_op *, irp->opcodes, opcode);
285 }
286
287 /* Removes opcode from the list of opcodes and shrinks the list by one. */
288 void remove_irp_opcode(ir_op *opcode) {
289   int i;
290
291   assert(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];
296       }
297       ARR_SETLEN(ir_op *, irp->opcodes, (ARR_LEN(irp->opcodes)) - 1);
298       break;
299     }
300   }
301 }
302
303 /* Returns the number of all opcodes in the irp. */
304 int (get_irp_n_opcodes)(void) {
305   return _get_irp_n_opcodes();
306 }
307
308 /* Returns the opcode at position pos in the irp. */
309 ir_op *(get_irp_opcode)(int pos) {
310   return _get_irp_opcode(pos);
311 }
312
313 /* Sets the generic function pointer of all opcodes to NULL */
314 void  clear_irp_opcodes_generic_func(void) {
315   int i;
316
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;
320   }
321 }
322
323 /*- File name / executable name or the like -*/
324 void   set_irp_prog_name(ident *name) {
325   irp->name = name;
326 }
327 int irp_prog_name_is_set(void) {
328   return irp->name != new_id_from_str(INITAL_PROG_NAME);
329 }
330 ident *get_irp_prog_ident(void) {
331   return irp->name;
332 }
333 const char  *get_irp_prog_name(void) {
334   return get_id_str(irp->name);
335 }
336
337
338 ir_graph *(get_const_code_irg)(void) {
339   return _get_const_code_irg();
340 }
341
342 irg_phase_state get_irp_phase_state(void) {
343   return irp->phase_state;
344 }
345 void set_irp_phase_state(irg_phase_state s) {
346   irp->phase_state = s;
347 }
348
349 irg_outs_state get_irp_ip_outs_state(void) {
350   return irp->outs_state;
351 }
352
353 void set_irp_ip_outs_inconsistent(void) {
354   irp->outs_state = outs_inconsistent;
355 }
356
357 void set_irp_ip_outedges(ir_node ** ip_outedges) {
358   irp->ip_outedges = ip_outedges;
359 }
360
361 ir_node** get_irp_ip_outedges(void) {
362   return irp->ip_outedges;
363 }
364
365
366 irg_callee_info_state get_irp_callee_info_state(void) {
367   return irp->callee_info_state;
368 }
369
370 void set_irp_callee_info_state(irg_callee_info_state s) {
371   irp->callee_info_state = s;
372 }