5b9ca0e604042770b27e6f46a8d74403a909daae
[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 ir_prog *get_irp() { return irp; }
28
29 /* initializes ir_prog. Calles the constructor for an ir_prog. */
30 void init_irprog(void) {
31   new_ir_prog ();
32 }
33
34 INLINE void remove_irp_type_from_list (type *typ) {
35   int i;
36   assert(typ);
37   for (i = 1; i < (ARR_LEN (irp->types)); i++) {
38     if (irp->types[i] == typ) {
39       for(; i < (ARR_LEN (irp->types)) - 1; i++) {
40         irp->types[i] = irp->types[i+1];
41       }
42       ARR_SETLEN(type*, irp->types, (ARR_LEN(irp->types)) - 1);
43       break;
44     }
45   }
46 }
47
48 /* Create a new ir prog. Automatically called by init_firm through
49    init_irprog. */
50 ir_prog *new_ir_prog (void) {
51   ir_prog *res;
52
53   res = (ir_prog *) malloc (sizeof(ir_prog));
54   irp = res;
55   /* res->obst      = (struct obstack *) xmalloc (sizeof (struct obstack)); */
56   res->graphs = NEW_ARR_F (ir_graph *, 1);
57   res->types  = NEW_ARR_F (type *, 1);
58
59 #ifdef DEBUG_libfirm
60   res->max_node_nr = 0;
61 #endif
62
63   res->glob_type = new_type_class(id_from_str (GLOBAL_TYPE_NAME,
64                                                strlen(GLOBAL_TYPE_NAME)));
65   /* Remove type from type list.  Must be treated differently than
66      other types. */
67   remove_irp_type_from_list(res->glob_type);
68
69   res->const_code_irg = new_const_code_irg();
70
71
72   return res;
73 }
74
75 /** Functions to access the fields of ir_prog **/
76
77
78 /* Access the main routine of the compiled program. */
79 ir_graph *get_irp_main_irg() {
80   assert (irp);
81   return irp->main_irg;
82 }
83
84 void set_irp_main_irg(ir_graph *main_irg) {
85   assert (irp);
86   irp->main_irg = main_irg;
87 }
88
89 type *get_glob_type(void) {
90   assert(irp);
91   return irp->glob_type = skip_tid(irp->glob_type);
92 }
93
94 /* Adds irg to the list of ir graphs in irp. */
95 void add_irp_irg(ir_graph *irg) {
96   assert (irg != NULL);
97   assert(irp && irp->graphs);
98   ARR_APP1 (ir_graph *, irp->graphs, irg);
99 }
100
101 /* Removes irg from the list or irgs, shrinks the list by one. */
102 void remove_irp_irg(ir_graph *irg){
103   int i;
104   assert(irg);
105   free_ir_graph(irg);
106   for (i = 1; i < (ARR_LEN (irp->graphs)); i++) {
107     if (irp->graphs[i] == irg) {
108       for(; i < (ARR_LEN (irp->graphs)) - 1; i++) {
109         irp->graphs[i] = irp->graphs[i+1];
110       }
111       ARR_SETLEN(ir_graph*, irp->graphs, (ARR_LEN(irp->graphs)) - 1);
112       break;
113     }
114   }
115 }
116
117 int get_irp_n_irgs() {
118   assert (irp && irp->graphs);
119   /* Strangely the first element of the array is NULL.  Why??  */
120   return (ARR_LEN((irp)->graphs) - 1);
121 }
122
123 ir_graph *get_irp_irg(int pos){
124   assert (irp && irp->graphs);
125   /* Strangely the first element of the array is NULL.  Why??  */
126   return irp->graphs[pos+1];
127 }
128
129 void set_irp_irg(int pos, ir_graph *irg) {
130   assert (irp && irg);
131   assert (pos < (ARR_LEN((irp)->graphs) - 1));
132   /* Strangely the first element of the array is NULL.  Why??  */
133   irp->graphs[pos+1] = irg;
134 }
135
136 /* Adds type to the list of types in irp. */
137 void add_irp_type(type *typ) {
138   assert (typ != NULL);
139   assert(irp);
140   ARR_APP1 (type *, irp->types, typ);
141 }
142
143 void remove_irp_type(type *typ) {
144   remove_irp_type_from_list (typ);
145 }
146
147 int get_irp_n_types (void) {
148   assert (irp && irp->types);
149   /* Strangely the first element of the array is NULL.  Why??  */
150   return (ARR_LEN((irp)->types) - 1);
151 }
152
153 type *get_irp_type(int pos) {
154   assert (irp && irp->types);
155   /* Strangely the first element of the array is NULL.  Why??  */
156   /* Don't set the skip_tid result so that no double entries are generated. */
157   return skip_tid(irp->types[pos+1]);
158 }
159
160 void  set_irp_type(int pos, type *typ) {
161   assert (irp && typ);
162   assert (pos < (ARR_LEN((irp)->types) - 1));
163   /* Strangely the first element of the array is NULL.  Why??  */
164   irp->types[pos+1] = typ;
165 }
166
167 #ifdef DEBUG_libfirm
168 int get_irp_new_node_nr() {
169   assert(irp);
170   irp->max_node_nr = irp->max_node_nr + 1;
171   return irp->max_node_nr - 1;
172 }
173 #endif
174
175 ir_graph *get_const_code_irg()
176 {
177   return irp->const_code_irg;
178 }