all available opcodes are now stored in an irp list
[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 /**
36  *  Create a new incomplete ir_prog.
37  */
38 static ir_prog *new_incomplete_ir_prog (void) {
39   ir_prog *res;
40
41   res = xmalloc (sizeof(*res));
42   memset(res, 0, sizeof(*res));
43   irp = res;
44
45   res->kind          = k_ir_prog;
46   res->graphs        = NEW_ARR_F(ir_graph *, 0);
47   res->pseudo_graphs = NEW_ARR_F(ir_graph *, 0);
48   res->types         = NEW_ARR_F(ir_type *, 0);
49   res->modes         = NEW_ARR_F(ir_mode *, 0);
50   res->opcodes       = NEW_ARR_F(ir_op *, 0);
51
52 #ifdef DEBUG_libfirm
53   res->max_node_nr = 0;
54 #endif
55
56   return res;
57 }
58
59 /** Completes an incomplete irprog. */
60 static ir_prog *complete_ir_prog(ir_prog *irp) {
61
62   irp->name      = new_id_from_str(INITAL_PROG_NAME);
63
64   irp->glob_type = new_type_class(new_id_from_str (GLOBAL_TYPE_NAME));
65   /* Remove type from type list.  Must be treated differently than
66      other types. */
67   remove_irp_type(irp->glob_type);
68
69   irp->const_code_irg   = new_const_code_irg();
70
71   irp->outs_state       = outs_none;
72   irp->ip_outedges      = NULL;
73   irp->trouts_state     = outs_none;
74   irp->class_cast_state = ir_class_casts_transitive;
75
76   return irp;
77 }
78
79 /* initializes ir_prog. Constructs only the basic lists */
80 void init_irprog_1(void) {
81   new_incomplete_ir_prog();
82 }
83
84 /* Completes ir_prog. */
85 void init_irprog_2(void) {
86   complete_ir_prog(irp);
87 }
88
89 /* Create a new ir prog. Automatically called by init_firm through
90    init_irprog. */
91 ir_prog *new_ir_prog (void) {
92   return complete_ir_prog(new_incomplete_ir_prog());
93 }
94
95 /* frees all memory used by irp.  Types in type list, irgs in irg
96     list and entities in global type must be freed by hand before. */
97 void     free_ir_prog(void) {
98   if (irp->glob_type)
99     free_type(irp->glob_type);
100
101   /* @@@ * free_ir_graph(irp->const_code_irg); * ?? End has no in?? */
102   DEL_ARR_F(irp->graphs);
103   DEL_ARR_F(irp->pseudo_graphs);
104   DEL_ARR_F(irp->types);
105   DEL_ARR_F(irp->modes);
106   DEL_ARR_F(irp->opcodes);
107
108   irp->name           = NULL;
109   irp->const_code_irg = NULL;
110   irp->kind           = k_BAD;
111 }
112
113 /*- Functions to access the fields of ir_prog -*/
114
115
116 /* Access the main routine of the compiled program. */
117 ir_graph *get_irp_main_irg(void) {
118   assert (irp);
119   return irp->main_irg;
120 }
121
122 void set_irp_main_irg(ir_graph *main_irg) {
123   assert (irp);
124   irp->main_irg = main_irg;
125 }
126
127 ir_type *(get_glob_type)(void) {
128   return _get_glob_type();
129 }
130
131 /* Adds irg to the list of ir graphs in irp. */
132 void add_irp_irg(ir_graph *irg) {
133   assert (irg != NULL);
134   assert(irp && irp->graphs);
135   ARR_APP1 (ir_graph *, irp->graphs, irg);
136 }
137
138 /* Removes irg from the list or irgs, shrinks the list by one. */
139 void remove_irp_irg_from_list(ir_graph *irg){
140   int i, found = 0;
141   assert(irg);
142   for (i = 0; i < (ARR_LEN (irp->graphs)); i++) {
143     if (irp->graphs[i] == irg) {
144       found = 1;
145       for(; i < (ARR_LEN (irp->graphs)) - 1; i++) {
146         irp->graphs[i] = irp->graphs[i+1];
147       }
148       ARR_SETLEN(ir_graph*, irp->graphs, (ARR_LEN(irp->graphs)) - 1);
149       break;
150     }
151   }
152   if (!found) {
153     for (i = 0; i < (ARR_LEN (irp->pseudo_graphs)); i++) {
154       if (irp->pseudo_graphs[i] == irg) {
155               for(; i < (ARR_LEN (irp->pseudo_graphs)) - 1; i++) {
156                 irp->pseudo_graphs[i] = irp->pseudo_graphs[i+1];
157               }
158               ARR_SETLEN(ir_graph*, irp->pseudo_graphs, (ARR_LEN(irp->pseudo_graphs)) - 1);
159               break;
160       }
161     }
162   }
163 }
164
165 /* Removes irg from the list or irgs, shrinks the list by one. */
166 void remove_irp_irg(ir_graph *irg){
167   assert(irg);
168   free_ir_graph(irg);
169   remove_irp_irg_from_list(irg);
170 }
171
172 int (get_irp_n_irgs)(void) {
173   return _get_irp_n_irgs();
174 }
175
176 ir_graph *(get_irp_irg)(int pos){
177   return _get_irp_irg(pos);
178 }
179
180 void set_irp_irg(int pos, ir_graph *irg) {
181   assert (irp && irg);
182   assert (pos < (ARR_LEN(irp->graphs)));
183   irp->graphs[pos] = irg;
184 }
185
186 /* Gets the number of graphs _and_ pseudo graphs. */
187 int       get_irp_n_allirgs(void) {
188   /* We can not call get_irp_n_irgs, as we end up in a recursion ... */
189   return ARR_LEN(irp->graphs) + get_irp_n_pseudo_irgs();
190 }
191
192 /* Returns the ir graph at position pos of all graphs (including
193  pseudo graphs).  Visits first graphs, then pseudo graphs. */
194 ir_graph *get_irp_allirg(int pos) {
195   int n_irgs = ARR_LEN(irp->graphs);
196   assert(0 <= pos);
197   if (pos < n_irgs) {
198     return irp->graphs[pos];
199   } else {
200     return get_irp_pseudo_irg(pos-n_irgs);
201   }
202 }
203
204 /* Adds type to the list of types in irp. */
205 void add_irp_type(ir_type *typ) {
206   assert (typ != NULL);
207   assert(irp);
208   ARR_APP1 (ir_type *, irp->types, typ);
209 }
210
211 /* Remove type form the list of types in irp. */
212 void remove_irp_type(ir_type *typ) {
213   int i;
214   assert(typ);
215
216   for (i = ARR_LEN(irp->types) -1; i >= 0; i--) {
217     if (irp->types[i] == typ) {
218       for(; i < (ARR_LEN(irp->types)) - 1; i++) {
219         irp->types[i] = irp->types[i+1];
220       }
221       ARR_SETLEN(ir_type *, irp->types, (ARR_LEN(irp->types)) - 1);
222       break;
223     }
224   }
225 }
226
227 int (get_irp_n_types) (void) {
228   return _get_irp_n_types();
229 }
230
231 ir_type *(get_irp_type) (int pos) {
232   return _get_irp_type(pos);
233 }
234
235 void set_irp_type(int pos, ir_type *typ) {
236   assert (irp && typ);
237   assert (pos < (ARR_LEN((irp)->types)));
238   irp->types[pos] = typ;
239 }
240
241 /* Returns the number of all modes in the irp. */
242 int (get_irp_n_modes)(void) {
243   return _get_irp_n_modes();
244 }
245
246 /* Returns the mode at position pos in the irp. */
247 ir_mode *(get_irp_mode)(int pos) {
248   return _get_irp_mode(pos);
249 }
250
251 /* Adds mode to the list of modes in irp. */
252 void add_irp_mode(ir_mode *mode) {
253   assert(mode != NULL);
254   assert(irp);
255   ARR_APP1(ir_mode *, irp->modes, mode);
256 }
257
258 /* Adds opcode to the list of opcodes in irp. */
259 void add_irp_opcode(ir_op *opcode) {
260   assert(opcode != NULL);
261   assert(irp);
262   ARR_APP1(ir_op *, irp->opcodes, opcode);
263 }
264
265 /* Removes opcode from the list of opcodes and shrinks the list by one. */
266 void remove_irp_opcode(ir_op *opcode) {
267   int i;
268   assert(opcode);
269
270   for (i = ARR_LEN(irp->opcodes) -1; i >= 0; i--) {
271     if (irp->opcodes[i] == opcode) {
272       for(; i < (ARR_LEN(irp->opcodes)) - 1; i++) {
273         irp->opcodes[i] = irp->opcodes[i+1];
274       }
275       ARR_SETLEN(ir_op *, irp->opcodes, (ARR_LEN(irp->opcodes)) - 1);
276       break;
277     }
278   }
279 }
280
281 /* Returns the number of all opcodes in the irp. */
282 int (get_irp_n_opcodes)(void) {
283   return _get_irp_n_opcodes();
284 }
285
286 /* Returns the opcode at position pos in the irp. */
287 ir_op *(get_irp_opcode)(int pos) {
288   return _get_irp_opcode(pos);
289 }
290
291
292
293 /*- File name / executable name or the like -*/
294 void   set_irp_prog_name(ident *name) {
295   irp->name = name;
296 }
297 int irp_prog_name_is_set(void) {
298   return irp->name != new_id_from_str(INITAL_PROG_NAME);
299 }
300 ident *get_irp_prog_ident(void) {
301   return irp->name;
302 }
303 const char  *get_irp_prog_name(void) {
304   return get_id_str(irp->name);
305 }
306
307
308 ir_graph *(get_const_code_irg)(void) {
309   return _get_const_code_irg();
310 }
311
312 irg_phase_state get_irp_phase_state(void) {
313   return irp->phase_state;
314 }
315 void           set_irp_phase_state(irg_phase_state s) {
316   irp->phase_state = s;
317 }
318
319 irg_outs_state get_irp_ip_outs_state(void) {
320   return irp->outs_state;
321 }
322
323 void set_irp_ip_outs_inconsistent(void) {
324   irp->outs_state = outs_inconsistent;
325 }
326
327 void      set_irp_ip_outedges(ir_node ** ip_outedges)
328 {
329   irp->ip_outedges = ip_outedges;
330 }
331
332 ir_node** get_irp_ip_outedges(void)
333 {
334   return irp->ip_outedges;
335 }
336
337
338 irg_callee_info_state get_irp_callee_info_state(void) {
339   return irp->callee_info_state;
340 }
341
342 void set_irp_callee_info_state(irg_callee_info_state s) {
343   irp->callee_info_state = s;
344 }