def051e5033b6db83d807c43bf28a2f137354429
[libfirm] / testprograms / oo_program_example.c
1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
3 **
4 ** Authors: Goetz Lindenmaier
5 **
6 ** testprogram.
7 */
8
9 # include "irdump.h"
10 # include "firm.h"
11
12 /**  This file constructs the IR for the following program:
13 ***
14 ***  class PRIMA
15 ***    a: int;
16 ***    b: real;
17 ***    c(int): int;
18 ***    set_a(int): void;
19 ***  end
20 ***
21 ***  c(d: int): int
22 ***    return (d + a);
23 ***  end
24 ***
25 ***  set_a(e:int): void
26 ***    self.a = e;
27 ***  end
28 ***
29 ***  main(): int
30 ***    o: PRIMA;
31 ***    o.set_a(2);
32 ***    return o.c(5);
33 ***  end
34 ***
35 **/
36
37 #define OPTIMIZE_NODE 0
38
39 int
40 main(void)
41 {
42   type_primitive *prim_t_int;
43   type_class   *owner, *class_prima, *class_langint;
44   type_method  *proc_main, *proc_set, *proc_c;
45   type_pointer *class_p_ptr;
46   entity       *proc_main_e, *proc_set_e, *proc_c_e, *a_e;
47
48   ir_graph     *main_irg, *set_a_irg, *c_irg;
49   ir_node      *c2, *c5, *obj_o, *obj_size, *proc_ptr, *call, *x;
50   ir_node      *self, *par1, *a_ptr;
51   ir_node      *a_val;
52
53   init_firm ();
54
55   set_opt_constant_folding(0);
56   set_opt_cse(0);
57
58   /* make basic type information for primitive type int.*/
59   prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_i);
60
61   /* first build procedure main */
62   printf("creating an IR graph: OO_PROGRAM_EXAMPLE...\n");
63   owner = get_glob_type();
64   proc_main = new_type_method(id_from_str("main", 4), 0, 1);
65   set_method_res_type(proc_main, 0, (type *)prim_t_int);
66
67   proc_main_e = new_entity ((type *)owner, id_from_str ("main", 4),
68                             (type *)proc_main);
69   main_irg = new_ir_graph (proc_main_e, 4);
70
71   /* There is only one block in main, it contains the constants and the calls. */
72   c2 = new_Const (mode_i, tarval_from_long (mode_i, 2));
73   c5 = new_Const (mode_i, tarval_from_long (mode_i, 5));
74
75   /* allocate the defined object and generate the type information */
76   class_prima = new_type_class(id_from_str ("PRIMA", 5));
77   obj_size = new_SymConst((type_or_id *)class_prima, size);
78   obj_o    = new_Alloc(get_store(), obj_size, (type *)class_prima, heap_alloc);
79   set_store(new_Proj(obj_o, mode_M, 0));  /* make the changed memory visible */
80   obj_o    = new_Proj(obj_o, mode_p, 1);  /* remember the pointer to the object */
81   /* we need type information for pointers to the class: */
82   class_p_ptr = new_type_pointer (id_from_str ("class_prima_ptr", 15),
83                                   (type *)class_prima);
84
85   /* get the pointer to the procedure from the class type */
86   proc_set = new_type_method(id_from_str("set_a", 5), 2, 0);
87   set_method_param_type(proc_set, 0, (type *)class_p_ptr);
88   set_method_param_type(proc_set, 1, (type *)prim_t_int);
89   proc_set_e = new_entity((type *)class_prima, id_from_str ("set_a", 5),
90                           (type*)proc_set);
91   proc_ptr = new_simpleSel(get_store(),  /* The memory the object is allocated in */
92                       obj_o,             /* The pointer to the object */
93                       proc_set_e );      /* The feature to select */
94
95   /* call procedure set_a, first built array with parameters */
96   {
97     ir_node *in[2];
98     in[0] = obj_o;
99     in[1] = c2;
100     call = new_Call(get_store(), proc_ptr, 2, in, proc_set);
101   }
102   /* make the change to memory visible */
103   set_store(new_Proj(call, mode_M, 0));
104
105   /* get the pointer to the procedure from the class type */
106   proc_c   = new_type_method(id_from_str("c", 1 ), 2, 1);
107   set_method_param_type(proc_c, 0, (type *)class_p_ptr);
108   set_method_param_type(proc_c, 1, (type *)prim_t_int);
109   set_method_res_type(proc_c, 0, (type *)prim_t_int);
110   proc_c_e = new_entity((type *)class_prima, id_from_str ("c", 1),
111                         (type*)proc_c);
112   proc_ptr = new_simpleSel(get_store(), obj_o, proc_c_e);
113
114   /* call procedure c, first built array with parameters */
115   {
116     ir_node *in[2];
117     in[0] = obj_o;
118     in[1] = c5;
119     call = new_Call(get_store(), proc_ptr, 2, in, proc_c);
120   }
121   /* make the change to memory visible */
122   set_store(new_Proj(call, mode_M, 0));
123
124   /* return the results of procedure main */
125   {
126      ir_node *in[1];
127      /* Select the result tuple from the call, then the proper
128         result from the tuple. */
129      in[0] = new_Proj(new_Proj(call, mode_T, 1), mode_I, 0);
130
131      x = new_Return (get_store (), 1, in);
132   }
133   mature_block (main_irg->current_block);
134
135   /* complete the end_block */
136   add_in_edge (main_irg->end_block, x);
137   mature_block (main_irg->end_block);
138
139   printf("\nDone building the graph.\n");
140   vrfy_graph(main_irg);
141   printf("Dumping the graph and a type graph.\n");
142   dump_ir_block_graph (main_irg);
143   dump_type_graph(main_irg);
144
145   /****************************************************************************/
146
147   printf("\ncreating IR graph for set_a: \n");
148
149   set_a_irg = new_ir_graph (proc_set_e, 4);
150
151   /* get the procedure parameter */
152   self = new_Proj(set_a_irg->args, mode_p, 0);
153   par1 = new_Proj(set_a_irg->args, mode_I, 1);
154   /* Create and select the entity to set */
155   class_langint = new_type_class(id_from_str ("Int", 3));
156   a_e = new_entity((type *)class_prima, id_from_str ("a", 1),
157                    (type*)class_langint);
158   a_ptr = new_simpleSel(get_store(), self, a_e);
159   /* perform the assignment */
160   set_store(new_Proj(new_Store(get_store(), a_ptr, par1), mode_M, 0));
161
162   /* return nothing */
163   x = new_Return (get_store (), 0, NULL);
164   mature_block (set_a_irg->current_block);
165
166   /* complete the end_block */
167   add_in_edge (set_a_irg->end_block, x);
168   mature_block (set_a_irg->end_block);
169
170   printf("\nDone building the graph.\n");
171   vrfy_graph(set_a_irg);
172   printf("Dumping the graph and a type graph.\n");
173   dump_ir_block_graph (set_a_irg);
174   dump_type_graph(set_a_irg);
175
176   /****************************************************************************/
177
178   printf("\ncreating IR graph for c: \n");
179
180   c_irg = new_ir_graph (proc_c_e, 4);
181
182   /* get the procedure parameter */
183   self = new_Proj(c_irg->args, mode_p, 0);
184   par1 = new_Proj(c_irg->args, mode_I, 1);
185
186   /* Select the entity and load the value */
187   a_ptr = new_simpleSel(get_store(), self, a_e);
188   a_val = new_Load(get_store(), a_ptr);
189   set_store(new_Proj(a_val, mode_M, 0));
190   a_val = new_Proj(a_val, mode_I, 1);
191
192   /* return the result */
193   {
194     ir_node *in[1];
195     in[0] = new_Add(par1, a_val, mode_I);
196
197     x = new_Return (get_store (), 1, in);
198   }
199   mature_block (c_irg->current_block);
200
201   /* complete the end_block */
202   add_in_edge (c_irg->end_block, x);
203   mature_block (c_irg->end_block);
204
205   /* verify the graph */
206   irg_vrfy(main_irg);
207
208   printf("\nDone building the graph.\n");
209   printf("Dumping the graph and a type graph.\n");
210   dump_ir_block_graph (c_irg);
211   dump_type_graph(c_irg);
212
213   /****************************************************************************/
214
215   printf("\nuse xvcg to view these graphs:\n");
216   printf("/ben/goetz/bin/xvcg GRAPHNAME\n");
217
218   return (1);
219 }