b0a44eb3f4ad23f211dbe818e42746bb8cceb384
[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;
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       for(; i < (ARR_LEN (irp->graphs)) - 1; i++) {
132         irp->graphs[i] = irp->graphs[i+1];
133       }
134       ARR_SETLEN(ir_graph*, irp->graphs, (ARR_LEN(irp->graphs)) - 1);
135       break;
136     }
137   }
138 }
139
140 int (get_irp_n_irgs)(void) {
141   return __get_irp_n_irgs();
142 }
143
144 ir_graph *(get_irp_irg)(int pos){
145   return __get_irp_irg(pos);
146 }
147
148 void set_irp_irg(int pos, ir_graph *irg) {
149   assert (irp && irg);
150   assert (pos < (ARR_LEN((irp)->graphs)));
151   irp->graphs[pos] = irg;
152 }
153
154 /* Gets the number of graphs _and_ pseudo graphs. */
155 int       get_irp_n_allirgs(void) {
156   /* We can not call get_irp_n_irgs, as we end up in a recursion ... */
157   return ARR_LEN((irp)->graphs) + get_irp_n_pseudo_irgs();
158 }
159
160 /* Returns the ir graph at position pos of all graphs (including
161  pseudo graphs).  Visits first graphs, then pseudo graphs. */
162 ir_graph *get_irp_allirg(int pos) {
163   int n_irgs = get_irp_n_irgs();
164   if (pos < n_irgs)
165     return (irp)->graphs[pos];
166   else
167     return get_irp_pseudo_irg(pos-n_irgs);
168 }
169
170
171 /* Adds type to the list of types in irp. */
172 void add_irp_type(type *typ) {
173   assert (typ != NULL);
174   assert(irp);
175   ARR_APP1 (type *, irp->types, typ);
176 }
177
178 void remove_irp_type(type *typ) {
179   remove_irp_type_from_list (typ);
180 }
181
182 int (get_irp_n_types) (void) {
183   return __get_irp_n_types();
184 }
185
186 type *(get_irp_type) (int pos) {
187   return __get_irp_type(pos);
188 }
189
190 void  set_irp_type(int pos, type *typ) {
191   assert (irp && typ);
192   assert (pos < (ARR_LEN((irp)->types)));
193   irp->types[pos] = typ;
194 }
195
196 #ifdef DEBUG_libfirm
197 int get_irp_new_node_nr() {
198   assert(irp);
199   irp->max_node_nr = irp->max_node_nr + 1;
200   return irp->max_node_nr - 1;
201 }
202 #endif
203
204 /*- File name / executable name or the like -*/
205 void   set_irp_prog_name(ident *name) {
206   irp->name = name;
207 }
208 int irp_prog_name_is_set(void) {
209   return irp->name != new_id_from_str(INITAL_PROG_NAME);
210 }
211 ident *get_irp_prog_ident(void) {
212   return irp->name;
213 }
214 const char  *get_irp_prog_name(void) {
215   return get_id_str(irp->name);
216 }
217
218
219 ir_graph *(get_const_code_irg)(void)
220 {
221   return __get_const_code_irg();
222 }
223
224 irg_outs_state get_irp_ip_outs_state() {
225   return irp->outs_state;
226 }
227
228 void set_irp_ip_outs_inconsistent() {
229   irp->outs_state = outs_inconsistent;
230 }
231
232 void      set_irp_ip_outedges(ir_node ** ip_outedges)
233 {
234   irp -> ip_outedges = ip_outedges;
235 }
236
237 ir_node** get_irp_ip_outedges(void)
238 {
239   return(irp -> ip_outedges);
240 }
241
242
243 irg_callee_info_state get_irp_callee_info_state(void) {
244   return irp->callee_info_state;
245 }
246
247 void set_irp_callee_info_state(irg_callee_info_state s) {
248   irp->callee_info_state = s;
249 }