5a4a2c150860eda576bb9a73aedcbdae49827c38
[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
76   return res;
77 }
78
79 /* frees all memory used by irp.  Types in type list, irgs in irg
80     list and entities in global type must be freed by hand before. */
81 void     free_ir_prog() {
82   free_type(irp->glob_type);
83   /* @@@ * free_ir_graph(irp->const_code_irg); * ?? End has no in?? */
84   DEL_ARR_F(irp->graphs);
85   DEL_ARR_F(irp->types);
86
87   irp->kind = k_BAD;
88   irp->const_code_irg = NULL;
89 }
90
91 /** Functions to access the fields of ir_prog **/
92
93
94 /* Access the main routine of the compiled program. */
95 ir_graph *get_irp_main_irg() {
96   assert (irp);
97   return irp->main_irg;
98 }
99
100 void set_irp_main_irg(ir_graph *main_irg) {
101   assert (irp);
102   irp->main_irg = main_irg;
103 }
104
105 type *get_glob_type(void) {
106   assert(irp);
107   return irp->glob_type = skip_tid(irp->glob_type);
108 }
109
110 /* Adds irg to the list of ir graphs in irp. */
111 void add_irp_irg(ir_graph *irg) {
112   assert (irg != NULL);
113   assert(irp && irp->graphs);
114   ARR_APP1 (ir_graph *, irp->graphs, irg);
115 }
116
117 /* Removes irg from the list or irgs, shrinks the list by one. */
118 void remove_irp_irg(ir_graph *irg){
119   int i;
120   assert(irg);
121   free_ir_graph(irg);
122   for (i = 1; i < (ARR_LEN (irp->graphs)); i++) {
123     if (irp->graphs[i] == irg) {
124       for(; i < (ARR_LEN (irp->graphs)) - 1; i++) {
125         irp->graphs[i] = irp->graphs[i+1];
126       }
127       ARR_SETLEN(ir_graph*, irp->graphs, (ARR_LEN(irp->graphs)) - 1);
128       break;
129     }
130   }
131 }
132
133 int get_irp_n_irgs() {
134   assert (irp && irp->graphs);
135   /* Strangely the first element of the array is NULL.  Why??  */
136   return (ARR_LEN((irp)->graphs) - 1);
137 }
138
139 ir_graph *get_irp_irg(int pos){
140   assert (irp && irp->graphs);
141   /* Strangely the first element of the array is NULL.  Why??  */
142   return irp->graphs[pos+1];
143 }
144
145 void set_irp_irg(int pos, ir_graph *irg) {
146   assert (irp && irg);
147   assert (pos < (ARR_LEN((irp)->graphs) - 1));
148   /* Strangely the first element of the array is NULL.  Why??  */
149   irp->graphs[pos+1] = 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   assert (irp && irp->types);
165   /* Strangely the first element of the array is NULL.  Why??  */
166   return (ARR_LEN((irp)->types) - 1);
167 }
168
169 type *get_irp_type(int pos) {
170   assert (irp && irp->types);
171   /* Strangely the first element of the array is NULL.  Why??  */
172   /* Don't set the skip_tid result so that no double entries are generated. */
173   return skip_tid(irp->types[pos+1]);
174 }
175
176 void  set_irp_type(int pos, type *typ) {
177   assert (irp && typ);
178   assert (pos < (ARR_LEN((irp)->types) - 1));
179   /* Strangely the first element of the array is NULL.  Why??  */
180   irp->types[pos+1] = typ;
181 }
182
183 #ifdef DEBUG_libfirm
184 int get_irp_new_node_nr() {
185   assert(irp);
186   irp->max_node_nr = irp->max_node_nr + 1;
187   return irp->max_node_nr - 1;
188 }
189 #endif
190
191 /** File name / executable name or the like **/
192 void   set_irp_prog_name(ident *name) {
193   irp->name = name;
194 }
195 ident *get_irp_prog_ident(void) {
196   return irp->name;
197 }
198 const char  *get_irp_prog_name(void) {
199   return get_id_str(irp->name);
200 }
201
202
203 ir_graph *get_const_code_irg(void)
204 {
205   return irp->const_code_irg;
206 }
207
208 void      set_irp_ip_outedges(ir_node ** ip_outedges)
209 {
210   irp -> ip_outedges = ip_outedges;
211 }
212
213 ir_node** get_irp_ip_outedges(void)
214 {
215   return(irp -> ip_outedges);
216 }