be5ca483a5adfe586bef33680dd665a779752b46
[libfirm] / ir / ir / irprog.c
1 /* Copyright (C) 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
3 **
4 ** Authors: Goetz Lindenmaier
5 **
6 ** irprog.c: ir representation of a program
7 */
8
9 /* $Id$ */
10
11 #ifdef HAVE_CONFIG_H
12 # include <config.h>
13 #endif
14
15 # include <string.h>
16
17 # include "irprog_t.h"
18 # include "irgraph_t.h"
19 # include "array.h"
20 # include "obst.h"
21 # include "typegmod.h"
22
23 #define GLOBAL_TYPE_NAME "GlobalType"
24
25 /* A variable from where everything in the ir can be accessed. */
26 ir_prog *irp;
27
28 /* initializes ir_prog. Calles the constructor for an ir_prog. */
29 void init_irprog(void) {
30   new_ir_prog ();
31 }
32
33 /* Create a new ir prog. Automatically called by init_firm through
34    init_irprog. */
35 ir_prog *new_ir_prog (void) {
36   ir_prog *res;
37
38   res = (ir_prog *) malloc (sizeof(ir_prog));
39   irp = res;
40   /* res->obst      = (struct obstack *) xmalloc (sizeof (struct obstack)); */
41   res->graphs = NEW_ARR_F (ir_graph *, 1);
42   res->types  = NEW_ARR_F (type *, 1);
43   res->glob_type = new_type_class(id_from_str (GLOBAL_TYPE_NAME,
44                                                strlen(GLOBAL_TYPE_NAME)));
45   /* Remove type from type list.  Must be treated differently than other types. */
46   remove_irp_type_from_list(res->glob_type);
47   /* @@@ Das ist hier das 2. mal!!
48      add_irp_type(res->glob_type);  */
49
50   res->const_code_irg = new_const_code_irg();
51
52 #ifdef DEBUG_libfirm
53   res->max_node_nr = 1;
54 #endif
55
56   return res;
57 }
58
59 /** Functions to access the fields of ir_prog **/
60
61
62 /* Access the main routine of the compiled program. */
63 ir_graph *get_irp_main_irg() {
64   assert (irp);
65   return irp->main_irg;
66 }
67
68 void set_irp_main_irg(ir_graph *main_irg) {
69   assert (irp);
70   irp->main_irg = main_irg;
71 }
72
73 type *get_glob_type(void) {
74   assert(irp);
75   return irp->glob_type = skip_tid(irp->glob_type);
76 }
77
78 /* Adds irg to the list of ir graphs in irp. */
79 void add_irp_irg(ir_graph *irg) {
80   assert (irg != NULL);
81   assert(irp && irp->graphs);
82   ARR_APP1 (ir_graph *, irp->graphs, irg);
83 }
84
85 /* Removes irg from the list or irgs, shrinks the list by one. */
86 void remove_irp_irg(ir_graph *irg){
87   int i;
88   assert(irg);
89   free_ir_graph(irg);
90   for (i = 1; i < (ARR_LEN (irp->graphs)); i++) {
91     if (irp->graphs[i] == irg) {
92       for(; i < (ARR_LEN (irp->graphs)) - 1; i++) {
93         irp->graphs[i] = irp->graphs[i+1];
94       }
95       ARR_SETLEN(ir_graph*, irp->graphs, (ARR_LEN(irp->graphs)) - 1);
96       break;
97     }
98   }
99 }
100
101 int get_irp_n_irgs() {
102   assert (irp && irp->graphs);
103   /* Strangely the first element of the array is NULL.  Why??  */
104   return (ARR_LEN((irp)->graphs) - 1);
105 }
106
107 ir_graph *get_irp_irg(int pos){
108   assert (irp && irp->graphs);
109   /* Strangely the first element of the array is NULL.  Why??  */
110   return irp->graphs[pos+1];
111 }
112
113 void set_irp_irg(int pos, ir_graph *irg) {
114   assert (irp && irg);
115   assert (pos < (ARR_LEN((irp)->graphs) - 1));
116   /* Strangely the first element of the array is NULL.  Why??  */
117   irp->graphs[pos+1] = irg;
118 }
119
120 /* Adds type to the list of types in irp. */
121 void add_irp_type(type *typ) {
122   assert (typ != NULL);
123   assert(irp);
124   ARR_APP1 (type *, irp->types, typ);
125 }
126
127 INLINE void remove_irp_type_from_list (type *typ) {
128   int i;
129   assert(typ);
130   for (i = 1; i < (ARR_LEN (irp->types)); i++) {
131     if (irp->types[i] == typ) {
132       for(; i < (ARR_LEN (irp->types)) - 1; i++) {
133         irp->types[i] = irp->types[i+1];
134       }
135       ARR_SETLEN(type*, irp->types, (ARR_LEN(irp->types)) - 1);
136       break;
137     }
138   }
139 }
140
141 void remove_irp_type(type *typ) {
142   remove_irp_type_from_list (typ);
143   free_type(typ);
144 }
145
146 int get_irp_n_types (void) {
147   assert (irp && irp->types);
148   /* Strangely the first element of the array is NULL.  Why??  */
149   return (ARR_LEN((irp)->types) - 1);
150 }
151
152 type *get_irp_type(int pos) {
153   assert (irp && irp->types);
154   /* Strangely the first element of the array is NULL.  Why??  */
155   /* Don't set the skip_tid result so that no double entries are generated. */
156   return skip_tid(irp->types[pos+1]);
157 }
158
159 void  set_irp_type(int pos, type *typ) {
160   assert (irp && typ);
161   assert (pos < (ARR_LEN((irp)->types) - 1));
162   /* Strangely the first element of the array is NULL.  Why??  */
163   irp->types[pos+1] = typ;
164 }
165
166 #ifdef DEBUG_libfirm
167 int get_irp_new_node_nr() {
168   assert(irp);
169   irp->max_node_nr = irp->max_node_nr + 1;
170   return irp->max_node_nr - 1;
171 }
172 #endif
173
174 ir_graph *get_const_code_irg()
175 {
176   return irp->const_code_irg;
177 }