get_divop_resmod() added
[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 "typegmod.h"
41 #include "irop_t.h"
42 #include "irmemory.h"
43
44 /** The name of the Global Type. */
45 #define GLOBAL_TYPE_NAME "GlobalType"
46 /** The name of the Thread Local STorage Type. */
47 #define TLS_TYPE_NAME "TLS"
48 /** The initial name of the irp program. */
49 #define INITAL_PROG_NAME "no_name_set"
50
51 /* A variable from where everything in the ir can be accessed. */
52 ir_prog *irp;
53 ir_prog *get_irp(void) { return irp; }
54
55 /**
56  *  Create a new incomplete ir_prog.
57  */
58 static ir_prog *new_incomplete_ir_prog (void) {
59   ir_prog *res;
60
61   res = xmalloc(sizeof(*res));
62   memset(res, 0, sizeof(*res));
63   irp = 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
72 #ifdef DEBUG_libfirm
73   res->max_node_nr = 0;
74 #endif
75
76   return res;
77 }
78
79 /** Completes an incomplete irprog. */
80 static ir_prog *complete_ir_prog(ir_prog *irp) {
81 #define X(s) s, sizeof(s)-1
82   irp->name      = new_id_from_chars(X(INITAL_PROG_NAME));
83
84   irp->glob_type = new_type_class(new_id_from_chars(X(GLOBAL_TYPE_NAME)));
85   irp->tls_type  = new_type_struct(new_id_from_chars(X(TLS_TYPE_NAME)));
86   /* Remove these types from type list.  Must be treated differently than
87      other types. */
88   remove_irp_type(irp->glob_type);
89   remove_irp_type(irp->tls_type);
90
91   /* Set these flags for debugging. */
92   irp->glob_type->flags |= tf_global_type;
93   irp->tls_type->flags  |= tf_tls_type;
94
95   /* The global type is a class, but we cannot derive from it, so set
96      the final property to assist optimizations that checks for it. */
97   set_class_final(irp->glob_type, 1);
98
99   irp->const_code_irg   = new_const_code_irg();
100
101   irp->phase_state      = phase_building;
102   irp->outs_state       = outs_none;
103   irp->ip_outedges      = NULL;
104   irp->trouts_state     = outs_none;
105   irp->class_cast_state = ir_class_casts_transitive;
106   irp->globals_adr_taken_state = ir_address_taken_not_computed;
107
108   return irp;
109 }
110
111 /* initializes ir_prog. Constructs only the basic lists */
112 void init_irprog_1(void) {
113   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   assert(irg);
180   for (i = 0; i < (ARR_LEN (irp->graphs)); i++) {
181     if (irp->graphs[i] == irg) {
182       found = 1;
183       for(; i < (ARR_LEN (irp->graphs)) - 1; i++) {
184         irp->graphs[i] = irp->graphs[i+1];
185       }
186       ARR_SETLEN(ir_graph*, irp->graphs, (ARR_LEN(irp->graphs)) - 1);
187       break;
188     }
189   }
190   if (!found) {
191     for (i = 0; i < (ARR_LEN (irp->pseudo_graphs)); i++) {
192       if (irp->pseudo_graphs[i] == irg) {
193               for(; i < (ARR_LEN (irp->pseudo_graphs)) - 1; i++) {
194                 irp->pseudo_graphs[i] = irp->pseudo_graphs[i+1];
195               }
196               ARR_SETLEN(ir_graph*, irp->pseudo_graphs, (ARR_LEN(irp->pseudo_graphs)) - 1);
197               break;
198       }
199     }
200   }
201 }
202
203 /* Removes irg from the list or irgs, shrinks the list by one. */
204 void remove_irp_irg(ir_graph *irg){
205   assert(irg);
206   free_ir_graph(irg);
207   remove_irp_irg_from_list(irg);
208 }
209
210 int (get_irp_n_irgs)(void) {
211   return _get_irp_n_irgs();
212 }
213
214 ir_graph *(get_irp_irg)(int pos){
215   return _get_irp_irg(pos);
216 }
217
218 void set_irp_irg(int pos, ir_graph *irg) {
219   assert(irp && irg);
220   assert(pos < (ARR_LEN(irp->graphs)));
221   irp->graphs[pos] = irg;
222 }
223
224 /* Gets the number of graphs _and_ pseudo graphs. */
225 int get_irp_n_allirgs(void) {
226   /* We can not call get_irp_n_irgs, as we end up in a recursion ... */
227   return ARR_LEN(irp->graphs) + get_irp_n_pseudo_irgs();
228 }
229
230 /* Returns the ir graph at position pos of all graphs (including
231  pseudo graphs).  Visits first graphs, then pseudo graphs. */
232 ir_graph *get_irp_allirg(int pos) {
233   int n_irgs = ARR_LEN(irp->graphs);
234   assert(0 <= pos);
235   if (pos < n_irgs) {
236     return irp->graphs[pos];
237   } else {
238     return get_irp_pseudo_irg(pos-n_irgs);
239   }
240 }
241
242 /* Adds type to the list of types in irp. */
243 void add_irp_type(ir_type *typ) {
244   assert(typ != NULL);
245   assert(irp);
246   ARR_APP1 (ir_type *, irp->types, typ);
247 }
248
249 /* Remove type form the list of types in irp. */
250 void remove_irp_type(ir_type *typ) {
251   int i;
252   assert(typ);
253
254   for (i = ARR_LEN(irp->types) -1; i >= 0; i--) {
255     if (irp->types[i] == typ) {
256       for(; i < (ARR_LEN(irp->types)) - 1; i++) {
257         irp->types[i] = irp->types[i+1];
258       }
259       ARR_SETLEN(ir_type *, irp->types, (ARR_LEN(irp->types)) - 1);
260       break;
261     }
262   }
263 }
264
265 int (get_irp_n_types) (void) {
266   return _get_irp_n_types();
267 }
268
269 ir_type *(get_irp_type) (int pos) {
270   return _get_irp_type(pos);
271 }
272
273 void set_irp_type(int pos, ir_type *typ) {
274   assert(irp && typ);
275   assert(pos < (ARR_LEN((irp)->types)));
276   irp->types[pos] = typ;
277 }
278
279 /* Returns the number of all modes in the irp. */
280 int (get_irp_n_modes)(void) {
281   return _get_irp_n_modes();
282 }
283
284 /* Returns the mode at position pos in the irp. */
285 ir_mode *(get_irp_mode)(int pos) {
286   return _get_irp_mode(pos);
287 }
288
289 /* Adds mode to the list of modes in irp. */
290 void add_irp_mode(ir_mode *mode) {
291   assert(mode != NULL);
292   assert(irp);
293   ARR_APP1(ir_mode *, irp->modes, mode);
294 }
295
296 /* Adds opcode to the list of opcodes in irp. */
297 void add_irp_opcode(ir_op *opcode) {
298   assert(opcode != NULL);
299   assert(irp);
300   ARR_APP1(ir_op *, irp->opcodes, opcode);
301 }
302
303 /* Removes opcode from the list of opcodes and shrinks the list by one. */
304 void remove_irp_opcode(ir_op *opcode) {
305   int i;
306
307   assert(opcode);
308   for (i = ARR_LEN(irp->opcodes) -1; i >= 0; i--) {
309     if (irp->opcodes[i] == opcode) {
310       for (; i < (ARR_LEN(irp->opcodes)) - 1; i++) {
311         irp->opcodes[i] = irp->opcodes[i+1];
312       }
313       ARR_SETLEN(ir_op *, irp->opcodes, (ARR_LEN(irp->opcodes)) - 1);
314       break;
315     }
316   }
317 }
318
319 /* Returns the number of all opcodes in the irp. */
320 int (get_irp_n_opcodes)(void) {
321   return _get_irp_n_opcodes();
322 }
323
324 /* Returns the opcode at position pos in the irp. */
325 ir_op *(get_irp_opcode)(int pos) {
326   return _get_irp_opcode(pos);
327 }
328
329 /* Sets the generic function pointer of all opcodes to NULL */
330 void  clear_irp_opcodes_generic_func(void) {
331   int i;
332
333   for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
334     ir_op *op = get_irp_opcode(i);
335     op->ops.generic = (op_func)NULL;
336   }
337 }
338
339 /*- File name / executable name or the like -*/
340 void   set_irp_prog_name(ident *name) {
341   irp->name = name;
342 }
343 int irp_prog_name_is_set(void) {
344   return irp->name != new_id_from_str(INITAL_PROG_NAME);
345 }
346 ident *get_irp_prog_ident(void) {
347   return irp->name;
348 }
349 const char  *get_irp_prog_name(void) {
350   return get_id_str(irp->name);
351 }
352
353
354 ir_graph *(get_const_code_irg)(void) {
355   return _get_const_code_irg();
356 }
357
358 irg_phase_state get_irp_phase_state(void) {
359   return irp->phase_state;
360 }
361 void set_irp_phase_state(irg_phase_state s) {
362   irp->phase_state = s;
363 }
364
365 irg_outs_state get_irp_ip_outs_state(void) {
366   return irp->outs_state;
367 }
368
369 void set_irp_ip_outs_inconsistent(void) {
370   irp->outs_state = outs_inconsistent;
371 }
372
373 void set_irp_ip_outedges(ir_node ** ip_outedges) {
374   irp->ip_outedges = ip_outedges;
375 }
376
377 ir_node** get_irp_ip_outedges(void) {
378   return irp->ip_outedges;
379 }
380
381
382 irg_callee_info_state get_irp_callee_info_state(void) {
383   return irp->callee_info_state;
384 }
385
386 void set_irp_callee_info_state(irg_callee_info_state s) {
387   irp->callee_info_state = s;
388 }