d8a2e59db10f65791972c3974f5e8190c61465b4
[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 #define INITAL_PROG_NAME "no_name_set"
27
28 /* A variable from where everything in the ir can be accessed. */
29 ir_prog *irp;
30 ir_prog *get_irp() { return irp; }
31
32 /* initializes ir_prog. Calles the constructor for an ir_prog. */
33 void init_irprog(void) {
34   new_ir_prog ();
35 }
36
37 INLINE void remove_irp_type_from_list (type *typ) {
38   int i;
39   assert(typ);
40 #if 0
41   for (i = 0; i < (ARR_LEN (irp->types)); i++) {
42 #else
43   for (i = ARR_LEN (irp->types) -1; i >= 0; i--) {
44 #endif
45     if (irp->types[i] == typ) {
46       for(; i < (ARR_LEN (irp->types)) - 1; i++) {
47         irp->types[i] = irp->types[i+1];
48       }
49       ARR_SETLEN(type*, irp->types, (ARR_LEN(irp->types)) - 1);
50       break;
51     }
52   }
53 }
54
55 /* Create a new ir prog. Automatically called by init_firm through
56    init_irprog. */
57 ir_prog *new_ir_prog (void) {
58   ir_prog *res;
59
60   res = (ir_prog *) malloc (sizeof(ir_prog));
61   memset(res, 0, sizeof(res));
62   irp = res;
63   /* res->obst      = (struct obstack *) xmalloc (sizeof (struct obstack)); */
64   res->graphs = NEW_ARR_F (ir_graph *, 0);
65   res->types  = NEW_ARR_F (type *, 0);
66   res->name   = new_id_from_str(INITAL_PROG_NAME);
67
68 #ifdef DEBUG_libfirm
69   res->max_node_nr = 0;
70 #endif
71
72   res->glob_type = new_type_class(new_id_from_str (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 = outs_none;
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   return __get_glob_type();
113 }
114
115 /* Adds irg to the list of ir graphs in irp. */
116 void add_irp_irg(ir_graph *irg) {
117   assert (irg != NULL);
118   assert(irp && irp->graphs);
119   ARR_APP1 (ir_graph *, irp->graphs, irg);
120 }
121
122 /* Removes irg from the list or irgs, shrinks the list by one. */
123 void remove_irp_irg(ir_graph *irg){
124   int i;
125   assert(irg);
126   free_ir_graph(irg);
127   for (i = 0; i < (ARR_LEN (irp->graphs)); i++) {
128     if (irp->graphs[i] == irg) {
129       for(; i < (ARR_LEN (irp->graphs)) - 1; i++) {
130         irp->graphs[i] = irp->graphs[i+1];
131       }
132       ARR_SETLEN(ir_graph*, irp->graphs, (ARR_LEN(irp->graphs)) - 1);
133       break;
134     }
135   }
136 }
137
138 int (get_irp_n_irgs)(void) {
139   return __get_irp_n_irgs();
140 }
141
142 ir_graph *(get_irp_irg)(int pos){
143   return __get_irp_irg(pos);
144 }
145
146 void set_irp_irg(int pos, ir_graph *irg) {
147   assert (irp && irg);
148   assert (pos < (ARR_LEN((irp)->graphs)));
149   irp->graphs[pos] = irg;
150 }
151
152 /* Adds type to the list of types in irp. */
153 void add_irp_type(type *typ) {
154   assert (typ != NULL);
155   assert(irp);
156   ARR_APP1 (type *, irp->types, typ);
157 }
158
159 void remove_irp_type(type *typ) {
160   remove_irp_type_from_list (typ);
161 }
162
163 int (get_irp_n_types) (void) {
164   return __get_irp_n_types();
165 }
166
167 type *(get_irp_type) (int pos) {
168   return __get_irp_type(pos);
169 }
170
171 void  set_irp_type(int pos, type *typ) {
172   assert (irp && typ);
173   assert (pos < (ARR_LEN((irp)->types)));
174   irp->types[pos] = typ;
175 }
176
177 #ifdef DEBUG_libfirm
178 int get_irp_new_node_nr() {
179   assert(irp);
180   irp->max_node_nr = irp->max_node_nr + 1;
181   return irp->max_node_nr - 1;
182 }
183 #endif
184
185 /*- File name / executable name or the like -*/
186 void   set_irp_prog_name(ident *name) {
187   irp->name = name;
188 }
189 int irp_prog_name_is_set(void) {
190   return irp->name != new_id_from_str(INITAL_PROG_NAME);
191 }
192 ident *get_irp_prog_ident(void) {
193   return irp->name;
194 }
195 const char  *get_irp_prog_name(void) {
196   return get_id_str(irp->name);
197 }
198
199
200 ir_graph *(get_const_code_irg)(void)
201 {
202   return __get_const_code_irg();
203 }
204
205 irg_outs_state get_irp_ip_outs_state() {
206   return irp->outs_state;
207 }
208
209 void set_irp_ip_outs_inconsistent() {
210   irp->outs_state = outs_inconsistent;
211 }
212
213 void      set_irp_ip_outedges(ir_node ** ip_outedges)
214 {
215   irp -> ip_outedges = ip_outedges;
216 }
217
218 ir_node** get_irp_ip_outedges(void)
219 {
220   return(irp -> ip_outedges);
221 }
222
223
224 irg_callee_info_state get_irp_callee_info_state(void) {
225   return irp->callee_info_state;
226 }
227
228 void set_irp_callee_info_state(irg_callee_info_state s) {
229   irp->callee_info_state = s;
230 }