2728791eb9d2be74f76a251cfe36b7f437172b25
[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   res->glob_type = new_type_class(id_from_str (GLOBAL_TYPE_NAME,
59                                                strlen(GLOBAL_TYPE_NAME)));
60   /* Remove type from type list.  Must be treated differently than
61      other types. */
62   remove_irp_type_from_list(res->glob_type);
63
64   res->const_code_irg = new_const_code_irg();
65
66 #ifdef DEBUG_libfirm
67   res->max_node_nr = 1;
68 #endif
69
70   return res;
71 }
72
73 /** Functions to access the fields of ir_prog **/
74
75
76 /* Access the main routine of the compiled program. */
77 ir_graph *get_irp_main_irg() {
78   assert (irp);
79   return irp->main_irg;
80 }
81
82 void set_irp_main_irg(ir_graph *main_irg) {
83   assert (irp);
84   irp->main_irg = main_irg;
85 }
86
87 type *get_glob_type(void) {
88   assert(irp);
89   return irp->glob_type = skip_tid(irp->glob_type);
90 }
91
92 /* Adds irg to the list of ir graphs in irp. */
93 void add_irp_irg(ir_graph *irg) {
94   assert (irg != NULL);
95   assert(irp && irp->graphs);
96   ARR_APP1 (ir_graph *, irp->graphs, irg);
97 }
98
99 /* Removes irg from the list or irgs, shrinks the list by one. */
100 void remove_irp_irg(ir_graph *irg){
101   int i;
102   assert(irg);
103   free_ir_graph(irg);
104   for (i = 1; i < (ARR_LEN (irp->graphs)); i++) {
105     if (irp->graphs[i] == irg) {
106       for(; i < (ARR_LEN (irp->graphs)) - 1; i++) {
107         irp->graphs[i] = irp->graphs[i+1];
108       }
109       ARR_SETLEN(ir_graph*, irp->graphs, (ARR_LEN(irp->graphs)) - 1);
110       break;
111     }
112   }
113 }
114
115 int get_irp_n_irgs() {
116   assert (irp && irp->graphs);
117   /* Strangely the first element of the array is NULL.  Why??  */
118   return (ARR_LEN((irp)->graphs) - 1);
119 }
120
121 ir_graph *get_irp_irg(int pos){
122   assert (irp && irp->graphs);
123   /* Strangely the first element of the array is NULL.  Why??  */
124   return irp->graphs[pos+1];
125 }
126
127 void set_irp_irg(int pos, ir_graph *irg) {
128   assert (irp && irg);
129   assert (pos < (ARR_LEN((irp)->graphs) - 1));
130   /* Strangely the first element of the array is NULL.  Why??  */
131   irp->graphs[pos+1] = irg;
132 }
133
134 /* Adds type to the list of types in irp. */
135 void add_irp_type(type *typ) {
136   assert (typ != NULL);
137   assert(irp);
138   ARR_APP1 (type *, irp->types, typ);
139 }
140
141 void remove_irp_type(type *typ) {
142   remove_irp_type_from_list (typ);
143 }
144
145 int get_irp_n_types (void) {
146   assert (irp && irp->types);
147   /* Strangely the first element of the array is NULL.  Why??  */
148   return (ARR_LEN((irp)->types) - 1);
149 }
150
151 type *get_irp_type(int pos) {
152   assert (irp && irp->types);
153   /* Strangely the first element of the array is NULL.  Why??  */
154   /* Don't set the skip_tid result so that no double entries are generated. */
155   return skip_tid(irp->types[pos+1]);
156 }
157
158 void  set_irp_type(int pos, type *typ) {
159   assert (irp && typ);
160   assert (pos < (ARR_LEN((irp)->types) - 1));
161   /* Strangely the first element of the array is NULL.  Why??  */
162   irp->types[pos+1] = typ;
163 }
164
165 #ifdef DEBUG_libfirm
166 int get_irp_new_node_nr() {
167   assert(irp);
168   irp->max_node_nr = irp->max_node_nr + 1;
169   return irp->max_node_nr - 1;
170 }
171 #endif
172
173 ir_graph *get_const_code_irg()
174 {
175   return irp->const_code_irg;
176 }