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