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