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