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