used ircons_t.h now
[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 "array.h"
22 # include "obst.h"
23 # include "typegmod.h"
24
25 #define GLOBAL_TYPE_NAME "GlobalType"
26
27 /* A variable from where everything in the ir can be accessed. */
28 ir_prog *irp;
29 ir_prog *get_irp() { return irp; }
30
31 /* initializes ir_prog. Calles the constructor for an ir_prog. */
32 void init_irprog(void) {
33   new_ir_prog ();
34 }
35
36 INLINE void remove_irp_type_from_list (type *typ) {
37   int i;
38   assert(typ);
39 #if 0
40   for (i = 1; i < (ARR_LEN (irp->types)); i++) {
41 #else
42   for (i = ARR_LEN (irp->types) -1; i >= 0; i--) {
43 #endif
44     if (irp->types[i] == typ) {
45       for(; i < (ARR_LEN (irp->types)) - 1; i++) {
46         irp->types[i] = irp->types[i+1];
47       }
48       ARR_SETLEN(type*, irp->types, (ARR_LEN(irp->types)) - 1);
49       break;
50     }
51   }
52 }
53
54 /* Create a new ir prog. Automatically called by init_firm through
55    init_irprog. */
56 ir_prog *new_ir_prog (void) {
57   ir_prog *res;
58
59   res = (ir_prog *) malloc (sizeof(ir_prog));
60   memset(res, 0, sizeof(res));
61   irp = res;
62   /* res->obst      = (struct obstack *) xmalloc (sizeof (struct obstack)); */
63   res->graphs = NEW_ARR_F (ir_graph *, 1);
64   res->types  = NEW_ARR_F (type *, 1);
65   res->name   = new_id_from_str("no_name_set");
66
67 #ifdef DEBUG_libfirm
68   res->max_node_nr = 0;
69 #endif
70
71   res->glob_type = new_type_class(id_from_str (GLOBAL_TYPE_NAME,
72                                                strlen(GLOBAL_TYPE_NAME)));
73   /* Remove type from type list.  Must be treated differently than
74      other types. */
75   remove_irp_type_from_list(res->glob_type);
76
77   res->const_code_irg = new_const_code_irg();
78
79   res->outs_state = no_outs;
80   res->ip_outedges = NULL;
81
82   return res;
83 }
84
85 /* frees all memory used by irp.  Types in type list, irgs in irg
86     list and entities in global type must be freed by hand before. */
87 void     free_ir_prog() {
88   free_type(irp->glob_type);
89   /* @@@ * free_ir_graph(irp->const_code_irg); * ?? End has no in?? */
90   DEL_ARR_F(irp->graphs);
91   DEL_ARR_F(irp->types);
92
93   irp->kind = k_BAD;
94   irp->const_code_irg = NULL;
95 }
96
97 /*- Functions to access the fields of ir_prog -*/
98
99
100 /* Access the main routine of the compiled program. */
101 ir_graph *get_irp_main_irg() {
102   assert (irp);
103   return irp->main_irg;
104 }
105
106 void set_irp_main_irg(ir_graph *main_irg) {
107   assert (irp);
108   irp->main_irg = main_irg;
109 }
110
111 type *get_glob_type(void) {
112   assert(irp);
113   return irp->glob_type = skip_tid(irp->glob_type);
114 }
115
116 /* Adds irg to the list of ir graphs in irp. */
117 void add_irp_irg(ir_graph *irg) {
118   assert (irg != NULL);
119   assert(irp && irp->graphs);
120   ARR_APP1 (ir_graph *, irp->graphs, irg);
121 }
122
123 /* Removes irg from the list or irgs, shrinks the list by one. */
124 void remove_irp_irg(ir_graph *irg){
125   int i;
126   assert(irg);
127   free_ir_graph(irg);
128   for (i = 1; i < (ARR_LEN (irp->graphs)); i++) {
129     if (irp->graphs[i] == irg) {
130       for(; i < (ARR_LEN (irp->graphs)) - 1; i++) {
131         irp->graphs[i] = irp->graphs[i+1];
132       }
133       ARR_SETLEN(ir_graph*, irp->graphs, (ARR_LEN(irp->graphs)) - 1);
134       break;
135     }
136   }
137 }
138
139 int get_irp_n_irgs() {
140   assert (irp && irp->graphs);
141   /* Strangely the first element of the array is NULL.  Why??  */
142   return (ARR_LEN((irp)->graphs) - 1);
143 }
144
145 ir_graph *get_irp_irg(int pos){
146   assert (irp && irp->graphs);
147   /* Strangely the first element of the array is NULL.  Why??  */
148   return irp->graphs[pos+1];
149 }
150
151 void set_irp_irg(int pos, ir_graph *irg) {
152   assert (irp && irg);
153   assert (pos < (ARR_LEN((irp)->graphs) - 1));
154   /* Strangely the first element of the array is NULL.  Why??  */
155   irp->graphs[pos+1] = irg;
156 }
157
158 /* Adds type to the list of types in irp. */
159 void add_irp_type(type *typ) {
160   assert (typ != NULL);
161   assert(irp);
162   ARR_APP1 (type *, irp->types, typ);
163 }
164
165 void remove_irp_type(type *typ) {
166   remove_irp_type_from_list (typ);
167 }
168
169 int get_irp_n_types (void) {
170   assert (irp && irp->types);
171   /* Strangely the first element of the array is NULL.  Why??  */
172   return (ARR_LEN((irp)->types) - 1);
173 }
174
175 type *get_irp_type(int pos) {
176   assert (irp && irp->types);
177   /* Strangely the first element of the array is NULL.  Why??  */
178   /* Don't set the skip_tid result so that no double entries are generated. */
179   return skip_tid(irp->types[pos+1]);
180 }
181
182 void  set_irp_type(int pos, type *typ) {
183   assert (irp && typ);
184   assert (pos < (ARR_LEN((irp)->types) - 1));
185   /* Strangely the first element of the array is NULL.  Why??  */
186   irp->types[pos+1] = typ;
187 }
188
189 #ifdef DEBUG_libfirm
190 int get_irp_new_node_nr() {
191   assert(irp);
192   irp->max_node_nr = irp->max_node_nr + 1;
193   return irp->max_node_nr - 1;
194 }
195 #endif
196
197 /*- File name / executable name or the like -*/
198 void   set_irp_prog_name(ident *name) {
199   irp->name = name;
200 }
201 ident *get_irp_prog_ident(void) {
202   return irp->name;
203 }
204 const char  *get_irp_prog_name(void) {
205   return get_id_str(irp->name);
206 }
207
208
209 ir_graph *get_const_code_irg(void)
210 {
211   return irp->const_code_irg;
212 }
213
214 irg_outs_state get_irp_ip_outs_state() {
215   return irp->outs_state;
216 }
217 void set_irp_ip_outs_inconsistent() {
218   irp->outs_state = outs_inconsistent;
219 }
220 void      set_irp_ip_outedges(ir_node ** ip_outedges)
221 {
222   irp -> ip_outedges = ip_outedges;
223 }
224
225 ir_node** get_irp_ip_outedges(void)
226 {
227   return(irp -> ip_outedges);
228 }