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