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