used new ir_exc_region_t type
[libfirm] / ir / ir / irprog.c
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief    Entry point to the representation of a whole program.
23  * @author   Goetz Lindenmaier
24  * @date     2000
25  * @version  $Id$
26  */
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #ifdef HAVE_STRING_H
32 # include <string.h>
33 #endif
34
35 #include "irprog_t.h"
36 #include "irgraph_t.h"
37 #include "pseudo_irg.h"
38 #include "array.h"
39 #include "obst.h"
40 #include "irop_t.h"
41 #include "irmemory.h"
42
43 /** The name of the Global type. */
44 #define GLOBAL_TYPE_NAME "GlobalType"
45 /** The name of the Thread Local Storage type. */
46 #define TLS_TYPE_NAME "TLS"
47 /** The initial name of the irp program. */
48 #define INITAL_PROG_NAME "no_name_set"
49
50 /* A variable from where everything in the ir can be accessed. */
51 ir_prog *irp;
52 ir_prog *get_irp(void) { return irp; }
53
54 /**
55  *  Create a new incomplete ir_prog.
56  */
57 static ir_prog *new_incomplete_ir_prog(void) {
58         ir_prog *res;
59
60         res = xmalloc(sizeof(*res));
61         memset(res, 0, sizeof(*res));
62
63         res->kind           = k_ir_prog;
64         res->graphs         = NEW_ARR_F(ir_graph *, 0);
65         res->pseudo_graphs  = NEW_ARR_F(ir_graph *, 0);
66         res->types          = NEW_ARR_F(ir_type *, 0);
67         res->modes          = NEW_ARR_F(ir_mode *, 0);
68         res->opcodes        = NEW_ARR_F(ir_op *, 0);
69         res->last_region_nr = 0;
70
71 #ifdef DEBUG_libfirm
72         res->max_node_nr = 0;
73 #endif
74
75         return res;
76 }
77
78 /** Completes an incomplete irprog. */
79 static ir_prog *complete_ir_prog(ir_prog *irp) {
80 #define IDENT(s) new_id_from_chars(s, sizeof(s)-1)
81
82         irp->name      = IDENT(INITAL_PROG_NAME);
83         irp->glob_type = new_type_class(IDENT(GLOBAL_TYPE_NAME));
84         irp->tls_type  = new_type_struct(IDENT(TLS_TYPE_NAME));
85         /* Remove these types from type list.  Must be treated differently than
86            other types. */
87         remove_irp_type(irp->glob_type);
88         remove_irp_type(irp->tls_type);
89
90         /* Set these flags for debugging. */
91         irp->glob_type->flags |= tf_global_type;
92         irp->tls_type->flags  |= tf_tls_type;
93
94         /* The global type is a class, but we cannot derive from it, so set
95            the final property to assist optimizations that checks for it. */
96         set_class_final(irp->glob_type, 1);
97
98         irp->const_code_irg   = new_const_code_irg();
99
100         irp->phase_state             = phase_building;
101         irp->outs_state              = outs_none;
102         irp->ip_outedges             = NULL;
103         irp->trouts_state            = outs_none;
104         irp->class_cast_state        = ir_class_casts_transitive;
105         irp->globals_adr_taken_state = ir_address_taken_not_computed;
106
107         return irp;
108 #undef IDENT
109 }
110
111 /* initializes ir_prog. Constructs only the basic lists. */
112 void init_irprog_1(void) {
113         irp = new_incomplete_ir_prog();
114 }
115
116 /* Completes ir_prog. */
117 void init_irprog_2(void) {
118         complete_ir_prog(irp);
119 }
120
121 /* Create a new ir prog. Automatically called by init_firm through
122    init_irprog. */
123 ir_prog *new_ir_prog (void) {
124         return complete_ir_prog(new_incomplete_ir_prog());
125 }
126
127 /* frees all memory used by irp.  Types in type list, irgs in irg
128    list and entities in global type must be freed by hand before. */
129 void free_ir_prog(void) {
130         if (irp->glob_type)
131                 free_type(irp->glob_type);
132         if (irp->tls_type)
133                 free_type(irp->tls_type);
134
135         /* @@@ * free_ir_graph(irp->const_code_irg); * ?? End has no in?? */
136         DEL_ARR_F(irp->graphs);
137         DEL_ARR_F(irp->pseudo_graphs);
138         DEL_ARR_F(irp->types);
139         DEL_ARR_F(irp->modes);
140         DEL_ARR_F(irp->opcodes);
141
142         irp->name           = NULL;
143         irp->const_code_irg = NULL;
144         irp->kind           = k_BAD;
145 }
146
147 /*- Functions to access the fields of ir_prog -*/
148
149
150 /* Access the main routine of the compiled program. */
151 ir_graph *get_irp_main_irg(void) {
152         assert(irp);
153         return irp->main_irg;
154 }
155
156 void set_irp_main_irg(ir_graph *main_irg) {
157         assert (irp);
158         irp->main_irg = main_irg;
159 }
160
161 ir_type *(get_glob_type)(void) {
162         return _get_glob_type();
163 }
164
165 ir_type *(get_tls_type)(void) {
166         return _get_tls_type();
167 }
168
169 /* Adds irg to the list of ir graphs in irp. */
170 void add_irp_irg(ir_graph *irg) {
171         assert(irg != NULL);
172         assert(irp && irp->graphs);
173         ARR_APP1(ir_graph *, irp->graphs, irg);
174 }
175
176 /* Removes irg from the list or irgs, shrinks the list by one. */
177 void remove_irp_irg_from_list(ir_graph *irg){
178         int i, found = 0;
179
180         assert(irg);
181         for (i = 0; i < (ARR_LEN (irp->graphs)); i++) {
182                 if (irp->graphs[i] == irg) {
183                         found = 1;
184                         for(; i < (ARR_LEN (irp->graphs)) - 1; i++) {
185                                 irp->graphs[i] = irp->graphs[i+1];
186                         }
187                         ARR_SETLEN(ir_graph*, irp->graphs, (ARR_LEN(irp->graphs)) - 1);
188                         break;
189                 }
190         }
191         if (!found) {
192                 for (i = 0; i < (ARR_LEN (irp->pseudo_graphs)); i++) {
193                         if (irp->pseudo_graphs[i] == irg) {
194                                 for(; i < (ARR_LEN (irp->pseudo_graphs)) - 1; i++) {
195                                         irp->pseudo_graphs[i] = irp->pseudo_graphs[i+1];
196                                 }
197                                 ARR_SETLEN(ir_graph*, irp->pseudo_graphs, (ARR_LEN(irp->pseudo_graphs)) - 1);
198                                 break;
199                         }
200                 }
201         }
202 }
203
204 /* Removes irg from the list or irgs, shrinks the list by one. */
205 void remove_irp_irg(ir_graph *irg){
206         assert(irg);
207         free_ir_graph(irg);
208         remove_irp_irg_from_list(irg);
209 }
210
211 int (get_irp_n_irgs)(void) {
212         return _get_irp_n_irgs();
213 }
214
215 ir_graph *(get_irp_irg)(int pos){
216         return _get_irp_irg(pos);
217 }
218
219 void set_irp_irg(int pos, ir_graph *irg) {
220         assert(irp && irg);
221         assert(pos < (ARR_LEN(irp->graphs)));
222         irp->graphs[pos] = irg;
223 }
224
225 /* Gets the number of graphs _and_ pseudo graphs. */
226 int get_irp_n_allirgs(void) {
227         /* We can not call get_irp_n_irgs, as we end up in a recursion ... */
228         return ARR_LEN(irp->graphs) + get_irp_n_pseudo_irgs();
229 }
230
231 /* Returns the ir graph at position pos of all graphs (including
232  pseudo graphs).  Visits first graphs, then pseudo graphs. */
233 ir_graph *get_irp_allirg(int pos) {
234         int n_irgs = ARR_LEN(irp->graphs);
235         assert(0 <= pos);
236         if (pos < n_irgs) {
237                 return irp->graphs[pos];
238         } else {
239                 return get_irp_pseudo_irg(pos-n_irgs);
240         }
241 }
242
243 /* Adds type to the list of types in irp. */
244 void add_irp_type(ir_type *typ) {
245         assert(typ != NULL);
246         assert(irp);
247         ARR_APP1 (ir_type *, irp->types, typ);
248 }
249
250 /* Remove type form the list of types in irp. */
251 void remove_irp_type(ir_type *typ) {
252         int i;
253         assert(typ);
254
255         for (i = ARR_LEN(irp->types) -1; i >= 0; i--) {
256                 if (irp->types[i] == typ) {
257                         for(; i < (ARR_LEN(irp->types)) - 1; i++) {
258                                 irp->types[i] = irp->types[i+1];
259                         }
260                         ARR_SETLEN(ir_type *, irp->types, (ARR_LEN(irp->types)) - 1);
261                         break;
262                 }
263         }
264 }
265
266 int (get_irp_n_types) (void) {
267         return _get_irp_n_types();
268 }
269
270 ir_type *(get_irp_type) (int pos) {
271         return _get_irp_type(pos);
272 }
273
274 void set_irp_type(int pos, ir_type *typ) {
275         assert(irp && typ);
276         assert(pos < (ARR_LEN((irp)->types)));
277         irp->types[pos] = typ;
278 }
279
280 /* Returns the number of all modes in the irp. */
281 int (get_irp_n_modes)(void) {
282         return _get_irp_n_modes();
283 }
284
285 /* Returns the mode at position pos in the irp. */
286 ir_mode *(get_irp_mode)(int pos) {
287         return _get_irp_mode(pos);
288 }
289
290 /* Adds mode to the list of modes in irp. */
291 void add_irp_mode(ir_mode *mode) {
292         assert(mode != NULL);
293         assert(irp);
294         ARR_APP1(ir_mode *, irp->modes, mode);
295 }
296
297 /* Adds opcode to the list of opcodes in irp. */
298 void add_irp_opcode(ir_op *opcode) {
299         assert(opcode != NULL);
300         assert(irp);
301         assert(opcode->code == ARR_LEN(irp->opcodes) && "new_ir_op() called in wrong order");
302         ARR_APP1(ir_op *, irp->opcodes, opcode);
303 }
304
305 /* Removes opcode from the list of opcodes and shrinks the list by one. */
306 void remove_irp_opcode(ir_op *opcode) {
307         int i;
308
309         assert(opcode);
310         for (i = ARR_LEN(irp->opcodes) -1; i >= 0; i--) {
311                 if (irp->opcodes[i] == opcode) {
312                         for (; i < (ARR_LEN(irp->opcodes)) - 1; i++) {
313                                 irp->opcodes[i] = irp->opcodes[i+1];
314                         }
315                         ARR_SETLEN(ir_op *, irp->opcodes, (ARR_LEN(irp->opcodes)) - 1);
316                         break;
317                 }
318         }
319 }
320
321 /* Returns the number of all opcodes in the irp. */
322 int (get_irp_n_opcodes)(void) {
323         return _get_irp_n_opcodes();
324 }
325
326 /* Returns the opcode at position pos in the irp. */
327 ir_op *(get_irp_opcode)(int pos) {
328         return _get_irp_opcode(pos);
329 }
330
331 /* Sets the generic function pointer of all opcodes to NULL */
332 void  clear_irp_opcodes_generic_func(void) {
333         int i;
334
335         for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
336                 ir_op *op = get_irp_opcode(i);
337                 op->ops.generic = (op_func)NULL;
338         }
339 }
340
341 /*- File name / executable name or the like -*/
342 void   set_irp_prog_name(ident *name) {
343         irp->name = name;
344 }
345 int irp_prog_name_is_set(void) {
346         return irp->name != new_id_from_str(INITAL_PROG_NAME);
347 }
348 ident *get_irp_prog_ident(void) {
349         return irp->name;
350 }
351 const char  *get_irp_prog_name(void) {
352         return get_id_str(irp->name);
353 }
354
355
356 ir_graph *(get_const_code_irg)(void) {
357         return _get_const_code_irg();
358 }
359
360 irg_phase_state get_irp_phase_state(void) {
361         return irp->phase_state;
362 }
363
364 void set_irp_phase_state(irg_phase_state s) {
365         irp->phase_state = s;
366 }
367
368 irg_outs_state get_irp_ip_outs_state(void) {
369         return irp->outs_state;
370 }
371
372 void set_irp_ip_outs_inconsistent(void) {
373         irp->outs_state = outs_inconsistent;
374 }
375
376 void set_irp_ip_outedges(ir_node ** ip_outedges) {
377         irp->ip_outedges = ip_outedges;
378 }
379
380 ir_node** get_irp_ip_outedges(void) {
381         return irp->ip_outedges;
382 }
383
384
385 irg_callee_info_state get_irp_callee_info_state(void) {
386         return irp->callee_info_state;
387 }
388
389 void set_irp_callee_info_state(irg_callee_info_state s) {
390         irp->callee_info_state = s;
391 }
392
393 /* Returns a new, unique exception region number. */
394 ir_exc_region_t (get_irp_next_region_nr)(void) {
395         return _get_irp_next_region_nr();
396 }