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