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