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