rename set_using_visited to set_using_irn_visited, some cosmetics, remove obsolete...
[libfirm] / ir / ir / irprog.c
1 /*
2  * Copyright (C) 1995-2008 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, Michael Beck
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         res->last_label_nr  = 1;  /* 0 is reserved as non-label */
71         res->max_irg_idx    = 0;
72
73 #ifdef DEBUG_libfirm
74         res->max_node_nr = 0;
75 #endif
76
77         return res;
78 }
79
80 /** Completes an incomplete irprog. */
81 static ir_prog *complete_ir_prog(ir_prog *irp) {
82 #define IDENT(s) new_id_from_chars(s, sizeof(s)-1)
83
84         irp->name      = IDENT(INITAL_PROG_NAME);
85         irp->glob_type = new_type_class(IDENT(GLOBAL_TYPE_NAME));
86         irp->tls_type  = new_type_struct(IDENT(TLS_TYPE_NAME));
87         /* Remove these types from type list.  Must be treated differently than
88            other types. */
89         remove_irp_type(irp->glob_type);
90         remove_irp_type(irp->tls_type);
91
92         /* Set these flags for debugging. */
93         irp->glob_type->flags |= tf_global_type;
94         irp->tls_type->flags  |= tf_tls_type;
95
96         /* The global type is a class, but we cannot derive from it, so set
97            the final property to assist optimizations that checks for it. */
98         set_class_final(irp->glob_type, 1);
99
100         irp->const_code_irg   = new_const_code_irg();
101
102         irp->phase_state             = phase_building;
103         irp->outs_state              = outs_none;
104         irp->ip_outedges             = NULL;
105         irp->trouts_state            = outs_none;
106         irp->class_cast_state        = ir_class_casts_transitive;
107         irp->globals_adr_taken_state = ir_address_taken_not_computed;
108
109         return irp;
110 #undef IDENT
111 }
112
113 /* initializes ir_prog. Constructs only the basic lists. */
114 void init_irprog_1(void) {
115         irp = new_incomplete_ir_prog();
116 }
117
118 /* Completes ir_prog. */
119 void init_irprog_2(void) {
120         complete_ir_prog(irp);
121 }
122
123 /* Create a new ir prog. Automatically called by init_firm through
124    init_irprog. */
125 ir_prog *new_ir_prog (void) {
126         return complete_ir_prog(new_incomplete_ir_prog());
127 }
128
129 /* frees all memory used by irp.  Types in type list, irgs in irg
130    list and entities in global type must be freed by hand before. */
131 void free_ir_prog(void) {
132         if (irp->glob_type)
133                 free_type(irp->glob_type);
134         if (irp->tls_type)
135                 free_type(irp->tls_type);
136
137         /* @@@ * free_ir_graph(irp->const_code_irg); * ?? End has no in?? */
138         DEL_ARR_F(irp->graphs);
139         DEL_ARR_F(irp->pseudo_graphs);
140         DEL_ARR_F(irp->types);
141         DEL_ARR_F(irp->modes);
142         DEL_ARR_F(irp->opcodes);
143
144         irp->name           = NULL;
145         irp->const_code_irg = NULL;
146         irp->kind           = k_BAD;
147 }
148
149 /*- Functions to access the fields of ir_prog -*/
150
151
152 /* Access the main routine of the compiled program. */
153 ir_graph *get_irp_main_irg(void) {
154         assert(irp);
155         return irp->main_irg;
156 }
157
158 void set_irp_main_irg(ir_graph *main_irg) {
159         assert (irp);
160         irp->main_irg = main_irg;
161 }
162
163 ir_type *(get_glob_type)(void) {
164         return _get_glob_type();
165 }
166
167 ir_type *(get_tls_type)(void) {
168         return _get_tls_type();
169 }
170
171 /* Adds irg to the list of ir graphs in irp. */
172 void add_irp_irg(ir_graph *irg) {
173         assert(irg != NULL);
174         assert(irp && irp->graphs);
175         ARR_APP1(ir_graph *, irp->graphs, irg);
176 }
177
178 /* Removes irg from the list or irgs, shrinks the list by one. */
179 void remove_irp_irg_from_list(ir_graph *irg){
180         int i, found = 0;
181
182         assert(irg);
183         for (i = 0; i < (ARR_LEN (irp->graphs)); i++) {
184                 if (irp->graphs[i] == irg) {
185                         found = 1;
186                         for(; i < (ARR_LEN (irp->graphs)) - 1; i++) {
187                                 irp->graphs[i] = irp->graphs[i+1];
188                         }
189                         ARR_SETLEN(ir_graph*, irp->graphs, (ARR_LEN(irp->graphs)) - 1);
190                         break;
191                 }
192         }
193         if (!found) {
194                 for (i = 0; i < (ARR_LEN (irp->pseudo_graphs)); i++) {
195                         if (irp->pseudo_graphs[i] == irg) {
196                                 for(; i < (ARR_LEN (irp->pseudo_graphs)) - 1; i++) {
197                                         irp->pseudo_graphs[i] = irp->pseudo_graphs[i+1];
198                                 }
199                                 ARR_SETLEN(ir_graph*, irp->pseudo_graphs, (ARR_LEN(irp->pseudo_graphs)) - 1);
200                                 break;
201                         }
202                 }
203         }
204 }
205
206 /* Removes irg from the list or irgs, shrinks the list by one. */
207 void remove_irp_irg(ir_graph *irg){
208         assert(irg);
209         free_ir_graph(irg);
210         remove_irp_irg_from_list(irg);
211 }
212
213 int (get_irp_n_irgs)(void) {
214         return _get_irp_n_irgs();
215 }
216
217 ir_graph *(get_irp_irg)(int pos){
218         return _get_irp_irg(pos);
219 }
220
221 int get_irp_last_idx(void) {
222         return irp->max_irg_idx;
223 }
224
225 void set_irp_irg(int pos, ir_graph *irg) {
226         assert(irp && irg);
227         assert(pos < (ARR_LEN(irp->graphs)));
228         irp->graphs[pos] = irg;
229 }
230
231 /* Gets the number of graphs _and_ pseudo graphs. */
232 int get_irp_n_allirgs(void) {
233         /* We can not call get_irp_n_irgs, as we end up in a recursion ... */
234         return ARR_LEN(irp->graphs) + get_irp_n_pseudo_irgs();
235 }
236
237 /* Returns the ir graph at position pos of all graphs (including
238  pseudo graphs).  Visits first graphs, then pseudo graphs. */
239 ir_graph *get_irp_allirg(int pos) {
240         int n_irgs = ARR_LEN(irp->graphs);
241         assert(0 <= pos);
242         if (pos < n_irgs) {
243                 return irp->graphs[pos];
244         } else {
245                 return get_irp_pseudo_irg(pos-n_irgs);
246         }
247 }
248
249 /* Adds type to the list of types in irp. */
250 void add_irp_type(ir_type *typ) {
251         assert(typ != NULL);
252         assert(irp);
253         ARR_APP1 (ir_type *, irp->types, typ);
254 }
255
256 /* Remove type form the list of types in irp. */
257 void remove_irp_type(ir_type *typ) {
258         int i;
259         assert(typ);
260
261         for (i = ARR_LEN(irp->types) -1; i >= 0; i--) {
262                 if (irp->types[i] == typ) {
263                         for(; i < (ARR_LEN(irp->types)) - 1; i++) {
264                                 irp->types[i] = irp->types[i+1];
265                         }
266                         ARR_SETLEN(ir_type *, irp->types, (ARR_LEN(irp->types)) - 1);
267                         break;
268                 }
269         }
270 }
271
272 int (get_irp_n_types) (void) {
273         return _get_irp_n_types();
274 }
275
276 ir_type *(get_irp_type) (int pos) {
277         return _get_irp_type(pos);
278 }
279
280 void set_irp_type(int pos, ir_type *typ) {
281         assert(irp && typ);
282         assert(pos < (ARR_LEN((irp)->types)));
283         irp->types[pos] = typ;
284 }
285
286 /* Returns the number of all modes in the irp. */
287 int (get_irp_n_modes)(void) {
288         return _get_irp_n_modes();
289 }
290
291 /* Returns the mode at position pos in the irp. */
292 ir_mode *(get_irp_mode)(int pos) {
293         return _get_irp_mode(pos);
294 }
295
296 /* Adds mode to the list of modes in irp. */
297 void add_irp_mode(ir_mode *mode) {
298         assert(mode != NULL);
299         assert(irp);
300         ARR_APP1(ir_mode *, irp->modes, mode);
301 }
302
303 /* Adds opcode to the list of opcodes in irp. */
304 void add_irp_opcode(ir_op *opcode) {
305         assert(opcode != NULL);
306         assert(irp);
307         assert(opcode->code == (unsigned) ARR_LEN(irp->opcodes) && "new_ir_op() called in wrong order");
308         ARR_APP1(ir_op *, irp->opcodes, opcode);
309 }
310
311 /* Removes opcode from the list of opcodes and shrinks the list by one. */
312 void remove_irp_opcode(ir_op *opcode) {
313         int i;
314
315         assert(opcode);
316         for (i = ARR_LEN(irp->opcodes) -1; i >= 0; i--) {
317                 if (irp->opcodes[i] == opcode) {
318                         for (; i < (ARR_LEN(irp->opcodes)) - 1; i++) {
319                                 irp->opcodes[i] = irp->opcodes[i+1];
320                         }
321                         ARR_SETLEN(ir_op *, irp->opcodes, (ARR_LEN(irp->opcodes)) - 1);
322                         break;
323                 }
324         }
325 }
326
327 /* Returns the number of all opcodes in the irp. */
328 int (get_irp_n_opcodes)(void) {
329         return _get_irp_n_opcodes();
330 }
331
332 /* Returns the opcode at position pos in the irp. */
333 ir_op *(get_irp_opcode)(int pos) {
334         return _get_irp_opcode(pos);
335 }
336
337 /* Sets the generic function pointer of all opcodes to NULL */
338 void  clear_irp_opcodes_generic_func(void) {
339         int i;
340
341         for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
342                 ir_op *op = get_irp_opcode(i);
343                 op->ops.generic = (op_func)NULL;
344         }
345 }
346
347 /*- File name / executable name or the like -*/
348 void   set_irp_prog_name(ident *name) {
349         irp->name = name;
350 }
351 int irp_prog_name_is_set(void) {
352         return irp->name != new_id_from_str(INITAL_PROG_NAME);
353 }
354 ident *get_irp_prog_ident(void) {
355         return irp->name;
356 }
357 const char  *get_irp_prog_name(void) {
358         return get_id_str(irp->name);
359 }
360
361
362 ir_graph *(get_const_code_irg)(void) {
363         return _get_const_code_irg();
364 }
365
366 irg_phase_state get_irp_phase_state(void) {
367         return irp->phase_state;
368 }
369
370 void set_irp_phase_state(irg_phase_state s) {
371         irp->phase_state = s;
372 }
373
374 irg_outs_state get_irp_ip_outs_state(void) {
375         return irp->outs_state;
376 }
377
378 void set_irp_ip_outs_inconsistent(void) {
379         irp->outs_state = outs_inconsistent;
380 }
381
382 void set_irp_ip_outedges(ir_node ** ip_outedges) {
383         irp->ip_outedges = ip_outedges;
384 }
385
386 ir_node** get_irp_ip_outedges(void) {
387         return irp->ip_outedges;
388 }
389
390
391 irg_callee_info_state get_irp_callee_info_state(void) {
392         return irp->callee_info_state;
393 }
394
395 void set_irp_callee_info_state(irg_callee_info_state s) {
396         irp->callee_info_state = s;
397 }
398
399 /* Returns a new, unique exception region number. */
400 ir_exc_region_t (get_irp_next_region_nr)(void) {
401         return _get_irp_next_region_nr();
402 }
403
404 /* Returns a new, unique label number. */
405 ir_label_t (get_irp_next_label_nr)(void) {
406         return _get_irp_next_label_nr();
407 }