more
[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 void remove_irp_type_from_list (type *typ) {
41   int i;
42   assert(typ);
43 #if 0
44   for (i = 0; i < (ARR_LEN (irp->types)); i++) {
45 #else
46   for (i = ARR_LEN (irp->types) -1; i >= 0; i--) {
47 #endif
48     if (irp->types[i] == typ) {
49       for(; i < (ARR_LEN (irp->types)) - 1; i++) {
50         irp->types[i] = irp->types[i+1];
51       }
52       ARR_SETLEN(type*, irp->types, (ARR_LEN(irp->types)) - 1);
53       break;
54     }
55   }
56 }
57
58 /* Create a new ir prog. Automatically called by init_firm through
59    init_irprog. */
60 ir_prog *new_ir_prog (void) {
61   ir_prog *res;
62
63   res = xmalloc (sizeof(*res));
64   memset(res, 0, sizeof(*res));
65   irp = res;
66   /* res->obst      = xmalloc (sizeof(*res->obst)); */
67   res->graphs        = NEW_ARR_F (ir_graph *, 0);
68   res->pseudo_graphs = NEW_ARR_F (ir_graph *, 0);
69   res->types  = NEW_ARR_F (type *, 0);
70   res->name   = new_id_from_str(INITAL_PROG_NAME);
71
72 #ifdef DEBUG_libfirm
73   res->max_node_nr = 0;
74 #endif
75
76   res->glob_type = new_type_class(new_id_from_str (GLOBAL_TYPE_NAME));
77   /* Remove type from type list.  Must be treated differently than
78      other types. */
79   remove_irp_type_from_list(res->glob_type);
80
81   res->const_code_irg = new_const_code_irg();
82
83   res->outs_state   = outs_none;
84   res->ip_outedges  = NULL;
85   res->trouts_state = outs_none;
86
87   return res;
88 }
89
90 /* frees all memory used by irp.  Types in type list, irgs in irg
91     list and entities in global type must be freed by hand before. */
92 void     free_ir_prog(void) {
93   free_type(irp->glob_type);
94   /* @@@ * free_ir_graph(irp->const_code_irg); * ?? End has no in?? */
95   DEL_ARR_F(irp->graphs);
96   DEL_ARR_F(irp->types);
97
98   irp->kind = k_BAD;
99   irp->const_code_irg = NULL;
100 }
101
102 /*- Functions to access the fields of ir_prog -*/
103
104
105 /* Access the main routine of the compiled program. */
106 ir_graph *get_irp_main_irg(void) {
107   assert (irp);
108   return irp->main_irg;
109 }
110
111 void set_irp_main_irg(ir_graph *main_irg) {
112   assert (irp);
113   irp->main_irg = main_irg;
114 }
115
116 type *(get_glob_type)(void) {
117   return __get_glob_type();
118 }
119
120 /* Adds irg to the list of ir graphs in irp. */
121 void add_irp_irg(ir_graph *irg) {
122   assert (irg != NULL);
123   assert(irp && irp->graphs);
124   ARR_APP1 (ir_graph *, irp->graphs, irg);
125 }
126
127 /* Removes irg from the list or irgs, shrinks the list by one. */
128 void remove_irp_irg_from_list(ir_graph *irg){
129   int i, found = false;
130   assert(irg);
131   for (i = 0; i < (ARR_LEN (irp->graphs)); i++) {
132     if (irp->graphs[i] == irg) {
133       found = true;
134       for(; i < (ARR_LEN (irp->graphs)) - 1; i++) {
135         irp->graphs[i] = irp->graphs[i+1];
136       }
137       ARR_SETLEN(ir_graph*, irp->graphs, (ARR_LEN(irp->graphs)) - 1);
138       break;
139     }
140   }
141   if (!found) {
142     for (i = 0; i < (ARR_LEN (irp->pseudo_graphs)); i++) {
143       if (irp->pseudo_graphs[i] == irg) {
144               for(; i < (ARR_LEN (irp->pseudo_graphs)) - 1; i++) {
145                 irp->pseudo_graphs[i] = irp->pseudo_graphs[i+1];
146               }
147               ARR_SETLEN(ir_graph*, irp->pseudo_graphs, (ARR_LEN(irp->pseudo_graphs)) - 1);
148               break;
149       }
150     }
151   }
152 }
153
154 /* Removes irg from the list or irgs, shrinks the list by one. */
155 void remove_irp_irg(ir_graph *irg){
156   assert(irg);
157   free_ir_graph(irg);
158   remove_irp_irg_from_list(irg);
159 }
160
161 int (get_irp_n_irgs)(void) {
162   return __get_irp_n_irgs();
163 }
164
165 ir_graph *(get_irp_irg)(int pos){
166   return __get_irp_irg(pos);
167 }
168
169 void set_irp_irg(int pos, ir_graph *irg) {
170   assert (irp && irg);
171   assert (pos < (ARR_LEN((irp)->graphs)));
172   irp->graphs[pos] = irg;
173 }
174
175 /* Gets the number of graphs _and_ pseudo graphs. */
176 int       get_irp_n_allirgs(void) {
177   /* We can not call get_irp_n_irgs, as we end up in a recursion ... */
178   return ARR_LEN((irp)->graphs) + get_irp_n_pseudo_irgs();
179 }
180
181 /* Returns the ir graph at position pos of all graphs (including
182  pseudo graphs).  Visits first graphs, then pseudo graphs. */
183 ir_graph *get_irp_allirg(int pos) {
184   int n_irgs = ARR_LEN((irp)->graphs);
185   assert(0 <= pos);
186   if (pos < n_irgs) {
187     return (irp)->graphs[pos];
188   } else {
189     return get_irp_pseudo_irg(pos-n_irgs);
190   }
191 }
192
193
194 /* Adds type to the list of types in irp. */
195 void add_irp_type(type *typ) {
196   assert (typ != NULL);
197   assert(irp);
198   ARR_APP1 (type *, irp->types, typ);
199 }
200
201 void remove_irp_type(type *typ) {
202   remove_irp_type_from_list (typ);
203 }
204
205 int (get_irp_n_types) (void) {
206   return __get_irp_n_types();
207 }
208
209 type *(get_irp_type) (int pos) {
210   return __get_irp_type(pos);
211 }
212
213 void  set_irp_type(int pos, type *typ) {
214   assert (irp && typ);
215   assert (pos < (ARR_LEN((irp)->types)));
216   irp->types[pos] = typ;
217 }
218
219 #ifdef DEBUG_libfirm
220 int get_irp_new_node_nr() {
221   assert(irp);
222   irp->max_node_nr = irp->max_node_nr + 1;
223   return irp->max_node_nr - 1;
224 }
225 #endif
226
227 /*- File name / executable name or the like -*/
228 void   set_irp_prog_name(ident *name) {
229   irp->name = name;
230 }
231 int irp_prog_name_is_set(void) {
232   return irp->name != new_id_from_str(INITAL_PROG_NAME);
233 }
234 ident *get_irp_prog_ident(void) {
235   return irp->name;
236 }
237 const char  *get_irp_prog_name(void) {
238   return get_id_str(irp->name);
239 }
240
241
242 ir_graph *(get_const_code_irg)(void)
243 {
244   return __get_const_code_irg();
245 }
246
247 irg_phase_state get_irp_phase_state(void) {
248   return irp->phase_state;
249 }
250 void           set_irp_phase_state(irg_phase_state s) {
251   irp->phase_state = s;
252 }
253
254 irg_outs_state get_irp_ip_outs_state() {
255   return irp->outs_state;
256 }
257
258 void set_irp_ip_outs_inconsistent() {
259   irp->outs_state = outs_inconsistent;
260 }
261
262 void      set_irp_ip_outedges(ir_node ** ip_outedges)
263 {
264   irp->ip_outedges = ip_outedges;
265 }
266
267 ir_node** get_irp_ip_outedges(void)
268 {
269   return irp->ip_outedges;
270 }
271
272
273 irg_callee_info_state get_irp_callee_info_state(void) {
274   return irp->callee_info_state;
275 }
276
277 void set_irp_callee_info_state(irg_callee_info_state s) {
278   irp->callee_info_state = s;
279 }