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