split modeP_mach into modeP_code and modeP_data
[libfirm] / ir / ir / irprog.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/irprog.c
4  * Purpose:     Entry point to the representation of a whole program.
5  * Author:      Goetz Lindenmaier
6  * Modified by:
7  * Created:     2000
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2000-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16
17 #ifdef HAVE_STRING_H
18 # include <string.h>
19 #endif
20
21 # include "irprog_t.h"
22 # include "irgraph_t.h"
23 # include "pseudo_irg.h"
24 # include "array.h"
25 # include "obst.h"
26 # include "typegmod.h"
27
28 #define GLOBAL_TYPE_NAME "GlobalType"
29 #define INITAL_PROG_NAME "no_name_set"
30
31 /* A variable from where everything in the ir can be accessed. */
32 ir_prog *irp;
33 ir_prog *get_irp(void) { return irp; }
34
35 /* initializes ir_prog. Calles the constructor for an ir_prog. */
36 void init_irprog(void) {
37   new_ir_prog ();
38 }
39
40 /* Create a new ir prog. Automatically called by init_firm through
41    init_irprog. */
42 ir_prog *new_ir_prog (void) {
43   ir_prog *res;
44
45   res = xmalloc (sizeof(*res));
46   memset(res, 0, sizeof(*res));
47   irp = res;
48
49   res->kind          = k_ir_prog;
50   res->graphs        = NEW_ARR_F(ir_graph *, 0);
51   res->pseudo_graphs = NEW_ARR_F(ir_graph *, 0);
52   res->types         = NEW_ARR_F(type *, 0);
53   res->modes         = NEW_ARR_F(ir_mode *, 0);
54   res->name          = new_id_from_str(INITAL_PROG_NAME);
55
56 #ifdef DEBUG_libfirm
57   res->max_node_nr = 0;
58 #endif
59
60   res->glob_type = new_type_class(new_id_from_str (GLOBAL_TYPE_NAME));
61   /* Remove type from type list.  Must be treated differently than
62      other types. */
63   remove_irp_type(res->glob_type);
64
65   res->const_code_irg = new_const_code_irg();
66
67   res->outs_state   = outs_none;
68   res->ip_outedges  = NULL;
69   res->trouts_state = outs_none;
70   res->class_cast_state = ir_class_casts_transitive;
71
72   return res;
73 }
74
75 /* frees all memory used by irp.  Types in type list, irgs in irg
76     list and entities in global type must be freed by hand before. */
77 void     free_ir_prog(void) {
78   free_type(irp->glob_type);
79   /* @@@ * free_ir_graph(irp->const_code_irg); * ?? End has no in?? */
80   DEL_ARR_F(irp->graphs);
81   DEL_ARR_F(irp->pseudo_graphs);
82   DEL_ARR_F(irp->types);
83   DEL_ARR_F(irp->modes);
84
85   irp->name           = NULL;
86   irp->const_code_irg = NULL;
87   irp->kind           = k_BAD;
88 }
89
90 /*- Functions to access the fields of ir_prog -*/
91
92
93 /* Access the main routine of the compiled program. */
94 ir_graph *get_irp_main_irg(void) {
95   assert (irp);
96   return irp->main_irg;
97 }
98
99 void set_irp_main_irg(ir_graph *main_irg) {
100   assert (irp);
101   irp->main_irg = main_irg;
102 }
103
104 type *(get_glob_type)(void) {
105   return _get_glob_type();
106 }
107
108 /* Adds irg to the list of ir graphs in irp. */
109 void add_irp_irg(ir_graph *irg) {
110   assert (irg != NULL);
111   assert(irp && irp->graphs);
112   ARR_APP1 (ir_graph *, irp->graphs, irg);
113 }
114
115 /* Removes irg from the list or irgs, shrinks the list by one. */
116 void remove_irp_irg_from_list(ir_graph *irg){
117   int i, found = false;
118   assert(irg);
119   for (i = 0; i < (ARR_LEN (irp->graphs)); i++) {
120     if (irp->graphs[i] == irg) {
121       found = true;
122       for(; i < (ARR_LEN (irp->graphs)) - 1; i++) {
123         irp->graphs[i] = irp->graphs[i+1];
124       }
125       ARR_SETLEN(ir_graph*, irp->graphs, (ARR_LEN(irp->graphs)) - 1);
126       break;
127     }
128   }
129   if (!found) {
130     for (i = 0; i < (ARR_LEN (irp->pseudo_graphs)); i++) {
131       if (irp->pseudo_graphs[i] == irg) {
132               for(; i < (ARR_LEN (irp->pseudo_graphs)) - 1; i++) {
133                 irp->pseudo_graphs[i] = irp->pseudo_graphs[i+1];
134               }
135               ARR_SETLEN(ir_graph*, irp->pseudo_graphs, (ARR_LEN(irp->pseudo_graphs)) - 1);
136               break;
137       }
138     }
139   }
140 }
141
142 /* Removes irg from the list or irgs, shrinks the list by one. */
143 void remove_irp_irg(ir_graph *irg){
144   assert(irg);
145   free_ir_graph(irg);
146   remove_irp_irg_from_list(irg);
147 }
148
149 int (get_irp_n_irgs)(void) {
150   return _get_irp_n_irgs();
151 }
152
153 ir_graph *(get_irp_irg)(int pos){
154   return _get_irp_irg(pos);
155 }
156
157 void set_irp_irg(int pos, ir_graph *irg) {
158   assert (irp && irg);
159   assert (pos < (ARR_LEN((irp)->graphs)));
160   irp->graphs[pos] = irg;
161 }
162
163 /* Gets the number of graphs _and_ pseudo graphs. */
164 int       get_irp_n_allirgs(void) {
165   /* We can not call get_irp_n_irgs, as we end up in a recursion ... */
166   return ARR_LEN((irp)->graphs) + get_irp_n_pseudo_irgs();
167 }
168
169 /* Returns the ir graph at position pos of all graphs (including
170  pseudo graphs).  Visits first graphs, then pseudo graphs. */
171 ir_graph *get_irp_allirg(int pos) {
172   int n_irgs = ARR_LEN((irp)->graphs);
173   assert(0 <= pos);
174   if (pos < n_irgs) {
175     return (irp)->graphs[pos];
176   } else {
177     return get_irp_pseudo_irg(pos-n_irgs);
178   }
179 }
180
181 /* Adds type to the list of types in irp. */
182 void add_irp_type(type *typ) {
183   assert (typ != NULL);
184   assert(irp);
185   ARR_APP1 (type *, irp->types, typ);
186 }
187
188 /* Remove type form the list of types in irp. */
189 void remove_irp_type(type *typ) {
190   int i;
191   assert(typ);
192
193   for (i = ARR_LEN(irp->types) -1; i >= 0; i--) {
194     if (irp->types[i] == typ) {
195       for(; i < (ARR_LEN(irp->types)) - 1; i++) {
196         irp->types[i] = irp->types[i+1];
197       }
198       ARR_SETLEN(type *, irp->types, (ARR_LEN(irp->types)) - 1);
199       break;
200     }
201   }
202 }
203
204 int (get_irp_n_types) (void) {
205   return _get_irp_n_types();
206 }
207
208 type *(get_irp_type) (int pos) {
209   return _get_irp_type(pos);
210 }
211
212 void  set_irp_type(int pos, type *typ) {
213   assert (irp && typ);
214   assert (pos < (ARR_LEN((irp)->types)));
215   irp->types[pos] = typ;
216 }
217
218 /* Returns the number of all modes in the irp. */
219 int (get_irp_n_modes)(void) {
220   return _get_irp_n_modes();
221 }
222
223 /* Returns the mode at position pos in the irp. */
224 ir_mode *(get_irp_mode)(int pos) {
225   return _get_irp_mode(pos);
226 }
227
228 /* Adds mode to the list of modes in irp. */
229 void add_irp_mode(ir_mode *mode) {
230   assert(mode != NULL);
231   assert(irp);
232   ARR_APP1(ir_mode *, irp->modes, mode);
233 }
234
235 #ifdef DEBUG_libfirm
236 int get_irp_new_node_nr() {
237   assert(irp);
238   irp->max_node_nr = irp->max_node_nr + 1;
239   return irp->max_node_nr - 1;
240 }
241 #endif
242
243 /*- File name / executable name or the like -*/
244 void   set_irp_prog_name(ident *name) {
245   irp->name = name;
246 }
247 int irp_prog_name_is_set(void) {
248   return irp->name != new_id_from_str(INITAL_PROG_NAME);
249 }
250 ident *get_irp_prog_ident(void) {
251   return irp->name;
252 }
253 const char  *get_irp_prog_name(void) {
254   return get_id_str(irp->name);
255 }
256
257
258 ir_graph *(get_const_code_irg)(void) {
259   return _get_const_code_irg();
260 }
261
262 irg_phase_state get_irp_phase_state(void) {
263   return irp->phase_state;
264 }
265 void           set_irp_phase_state(irg_phase_state s) {
266   irp->phase_state = s;
267 }
268
269 irg_outs_state get_irp_ip_outs_state() {
270   return irp->outs_state;
271 }
272
273 void set_irp_ip_outs_inconsistent() {
274   irp->outs_state = outs_inconsistent;
275 }
276
277 void      set_irp_ip_outedges(ir_node ** ip_outedges)
278 {
279   irp->ip_outedges = ip_outedges;
280 }
281
282 ir_node** get_irp_ip_outedges(void)
283 {
284   return irp->ip_outedges;
285 }
286
287
288 irg_callee_info_state get_irp_callee_info_state(void) {
289   return irp->callee_info_state;
290 }
291
292 void set_irp_callee_info_state(irg_callee_info_state s) {
293   irp->callee_info_state = s;
294 }