removed INLIEN before global functions
[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 = 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
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_from_list(ir_graph *irg){
128   int i, found = false;
129   assert(irg);
130   for (i = 0; i < (ARR_LEN (irp->graphs)); i++) {
131     if (irp->graphs[i] == irg) {
132       found = true;
133       for(; i < (ARR_LEN (irp->graphs)) - 1; i++) {
134         irp->graphs[i] = irp->graphs[i+1];
135       }
136       ARR_SETLEN(ir_graph*, irp->graphs, (ARR_LEN(irp->graphs)) - 1);
137       break;
138     }
139   }
140   if (!found) {
141     for (i = 0; i < (ARR_LEN (irp->pseudo_graphs)); i++) {
142       if (irp->pseudo_graphs[i] == irg) {
143         for(; i < (ARR_LEN (irp->pseudo_graphs)) - 1; i++) {
144           irp->pseudo_graphs[i] = irp->pseudo_graphs[i+1];
145         }
146         ARR_SETLEN(ir_graph*, irp->pseudo_graphs, (ARR_LEN(irp->pseudo_graphs)) - 1);
147         break;
148       }
149     }
150   }
151 }
152
153 /* Removes irg from the list or irgs, shrinks the list by one. */
154 void remove_irp_irg(ir_graph *irg){
155   assert(irg);
156   free_ir_graph(irg);
157   remove_irp_irg_from_list(irg);
158 }
159
160 int (get_irp_n_irgs)(void) {
161   return __get_irp_n_irgs();
162 }
163
164 ir_graph *(get_irp_irg)(int pos){
165   return __get_irp_irg(pos);
166 }
167
168 void set_irp_irg(int pos, ir_graph *irg) {
169   assert (irp && irg);
170   assert (pos < (ARR_LEN((irp)->graphs)));
171   irp->graphs[pos] = irg;
172 }
173
174 /* Gets the number of graphs _and_ pseudo graphs. */
175 int       get_irp_n_allirgs(void) {
176   /* We can not call get_irp_n_irgs, as we end up in a recursion ... */
177   return ARR_LEN((irp)->graphs) + get_irp_n_pseudo_irgs();
178 }
179
180 /* Returns the ir graph at position pos of all graphs (including
181  pseudo graphs).  Visits first graphs, then pseudo graphs. */
182 ir_graph *get_irp_allirg(int pos) {
183   int n_irgs = ARR_LEN((irp)->graphs);
184   assert(0 <= pos);
185   if (pos < n_irgs) {
186     return (irp)->graphs[pos];
187   } else {
188     return get_irp_pseudo_irg(pos-n_irgs);
189   }
190 }
191
192
193 /* Adds type to the list of types in irp. */
194 void add_irp_type(type *typ) {
195   assert (typ != NULL);
196   assert(irp);
197   ARR_APP1 (type *, irp->types, typ);
198 }
199
200 void remove_irp_type(type *typ) {
201   remove_irp_type_from_list (typ);
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 #ifdef DEBUG_libfirm
219 int get_irp_new_node_nr() {
220   assert(irp);
221   irp->max_node_nr = irp->max_node_nr + 1;
222   return irp->max_node_nr - 1;
223 }
224 #endif
225
226 /*- File name / executable name or the like -*/
227 void   set_irp_prog_name(ident *name) {
228   irp->name = name;
229 }
230 int irp_prog_name_is_set(void) {
231   return irp->name != new_id_from_str(INITAL_PROG_NAME);
232 }
233 ident *get_irp_prog_ident(void) {
234   return irp->name;
235 }
236 const char  *get_irp_prog_name(void) {
237   return get_id_str(irp->name);
238 }
239
240
241 ir_graph *(get_const_code_irg)(void)
242 {
243   return __get_const_code_irg();
244 }
245
246 irg_outs_state get_irp_ip_outs_state() {
247   return irp->outs_state;
248 }
249
250 void set_irp_ip_outs_inconsistent() {
251   irp->outs_state = outs_inconsistent;
252 }
253
254 void      set_irp_ip_outedges(ir_node ** ip_outedges)
255 {
256   irp -> ip_outedges = ip_outedges;
257 }
258
259 ir_node** get_irp_ip_outedges(void)
260 {
261   return(irp -> ip_outedges);
262 }
263
264
265 irg_callee_info_state get_irp_callee_info_state(void) {
266   return irp->callee_info_state;
267 }
268
269 void set_irp_callee_info_state(irg_callee_info_state s) {
270   irp->callee_info_state = s;
271 }