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