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