Improved support for multiple inheritance
[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 "irprog_t.h"
16 # include "irgraph_t.h"
17 # include "array.h"
18 # include "obst.h"
19 # include "typegmod.h"
20
21 #define GLOBAL_TYPE_NAME "GlobalType"
22
23 /* A variable from where everything in the ir can be accessed. */
24 ir_prog *irp;
25
26 /* initializes ir_prog. Calles the constructor for an ir_prog. */
27 void init_irprog(void) {
28   new_ir_prog ();
29 }
30
31 /* Create a new ir prog. Automatically called by init_firm through
32    init_irprog. */
33 ir_prog *new_ir_prog (void) {
34   ir_prog *res;
35
36   res = (ir_prog *) malloc (sizeof(ir_prog));
37   irp = res;
38   /* res->obst      = (struct obstack *) xmalloc (sizeof (struct obstack)); */
39   res->graphs = NEW_ARR_F (ir_graph *, 1);
40   res->types  = NEW_ARR_F (type *, 1);
41   res->glob_type = new_type_class(id_from_str (GLOBAL_TYPE_NAME,
42                                                strlen(GLOBAL_TYPE_NAME)));
43   add_irp_type(res->glob_type);
44
45   res->const_code_irg = new_const_code_irg();
46
47 #ifdef DEBUG_libfirm
48   res->max_node_nr = 1;
49 #endif
50
51   return res;
52 }
53
54 /** Functions to access the fields of ir_prog **/
55
56
57 /* Access the main routine of the compiled program. */
58 ir_graph *get_irp_main_irg() {
59   assert (irp);
60   return irp->main_irg;
61 }
62
63 void set_irp_main_irg(ir_graph *main_irg) {
64   assert (irp);
65   irp->main_irg = main_irg;
66 }
67
68 type *get_glob_type(void) {
69   assert(irp);
70   return irp->glob_type = skip_tid(irp->glob_type);
71 }
72
73 /* Adds irg to the list of ir graphs in irp. */
74 void add_irp_irg(ir_graph *irg) {
75   assert (irg != NULL);
76   assert(irp && irp->graphs);
77   ARR_APP1 (ir_graph *, irp->graphs, irg);
78 }
79
80 /* Removes irg from the list or irgs, shrinks the list by one.
81    @@@ does not work properly. */
82 void remove_irp_irg(ir_graph *irg){
83   int i;
84   assert(irg);
85   free_ir_graph(irg);
86   for (i = 1; i < (ARR_LEN (irp->graphs)); i++) {
87     if (irp->graphs[i] == irg) {
88       for(; i < (ARR_LEN (irp->graphs)) - 1; i++) {
89         irp->graphs[i] = irp->graphs[i+1];
90       }
91       ARR_SETLEN(ir_graph*, irp->graphs, (ARR_LEN(irp->graphs)) - 1);
92       break;
93     }
94   }
95 }
96
97 int get_irp_n_irgs() {
98   assert (irp && irp->graphs);
99   /* Strangely the first element of the array is NULL.  Why??  */
100   return (ARR_LEN((irp)->graphs) - 1);
101 }
102
103 ir_graph *get_irp_irg(int pos){
104   assert (irp && irp->graphs);
105   /* Strangely the first element of the array is NULL.  Why??  */
106   return irp->graphs[pos+1];
107 }
108
109 void set_irp_irg(int pos, ir_graph *irg) {
110   assert (irp && irg);
111   assert (pos < (ARR_LEN((irp)->graphs) - 1));
112   /* Strangely the first element of the array is NULL.  Why??  */
113   irp->graphs[pos+1] = irg;
114 }
115
116 /* Adds type to the list of types in irp. */
117 void add_irp_type(type *typ) {
118   assert (typ != NULL);
119   assert(irp);
120   ARR_APP1 (type *, irp->types, typ);
121 }
122
123 int get_irp_n_types (void) {
124   assert (irp && irp->types);
125   /* Strangely the first element of the array is NULL.  Why??  */
126   return (ARR_LEN((irp)->types) - 1);
127 }
128
129 type *get_irp_type(int pos) {
130   assert (irp && irp->types);
131   /* Strangely the first element of the array is NULL.  Why??  */
132   /* Don't set the skip_tid result so that no double entries are generated. */
133   return skip_tid(irp->types[pos+1]);
134 }
135
136 void  set_irp_type(int pos, type *typ) {
137   assert (irp && typ);
138   assert (pos < (ARR_LEN((irp)->types) - 1));
139   /* Strangely the first element of the array is NULL.  Why??  */
140   irp->types[pos+1] = typ;
141 }
142
143 #ifdef DEBUG_libfirm
144 int get_irp_new_node_nr() {
145   assert(irp);
146   irp->max_node_nr = irp->max_node_nr + 1;
147   return irp->max_node_nr - 1;
148 }
149 #endif
150
151 ir_graph *get_const_code_irg()
152 {
153   return irp->const_code_irg;
154 }