f3dcaad56f471421d2c8f157aca817a55c98c8c5
[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 ***
17 ***    int c(d: int) {
18 ***      return (d + self.a);
19 ***    }
20 ***
21 ***    void set_a(e:int) {
22 ***      self.a = e;
23 ***    }
24 ***
25 ***  }
26 ***
27 ***  int main() {
28 ***    o: PRIMA;
29 ***    o = new PRIMA;
30 ***    o.set_a(2);
31 ***    return o.c(5);
32 ***  };
33 ***
34 **/
35
36 int
37 main(void)
38 {
39   type     *prim_t_int;
40   type     *owner, *class_prima;
41   type     *proc_main, *proc_set_a, *proc_c;
42   type     *class_p_ptr;
43   entity   *proc_main_e, *proc_set_a_e, *proc_c_e, *a_e;
44
45   ir_graph     *main_irg, *set_a_irg, *c_irg;
46   ir_node      *c2, *c5, *obj_o, *obj_size, *proc_ptr, *call, *res, *x;
47   ir_node      *self, *par1, *a_ptr;
48   ir_node      *a_val;
49
50   int o_pos, self_pos, e_pos, d_pos;
51
52   int i;
53
54   init_firm ();
55
56   set_opt_constant_folding(1);
57   set_opt_cse(1);
58   set_opt_dead_node_elimination(1);
59
60   /*** Make basic type information for primitive type int. ***/
61   prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_i);
62
63   /*** Make type information for the class (PRIMA). ***/
64   /* The type of the class */
65   class_prima = new_type_class(id_from_str ("PRIMA", 5));
66   /* We need type information for pointers to the class: */
67   class_p_ptr = new_type_pointer (id_from_str ("class_prima_ptr", 15),
68                                   class_prima);
69   /* An entity for the field (a).  The entity constructor automatically adds
70      the entity as member of the owner. */
71   a_e = new_entity(class_prima, id_from_str ("a", 1), prim_t_int);
72   /* An entity for the method set_a.  But first we need type information
73      for the method. */
74   proc_set_a = new_type_method(id_from_str("set_a", 5), 2, 0);
75   set_method_param_type(proc_set_a, 0, class_p_ptr);
76   set_method_param_type(proc_set_a, 1, prim_t_int);
77   proc_set_a_e = new_entity(class_prima, id_from_str ("set_a", 5), proc_set_a);
78   /* An entity for the method c. Implicit argument "self" must be modeled
79      explicit! */
80   proc_c   = new_type_method(id_from_str("c", 1 ), 2, 1);
81   set_method_param_type(proc_c, 0, class_p_ptr);
82   set_method_param_type(proc_c, 1, prim_t_int);
83   set_method_res_type(proc_c, 0, prim_t_int);
84   proc_c_e = new_entity(class_prima, id_from_str ("c", 1), proc_c);
85
86   /*** Now build procedure main. ***/
87   /** Type information for main. **/
88   printf("\nCreating an IR graph: OO_PROGRAM_EXAMPLE...\n");
89   /* Main is not modeled as part of an explicit class here. Therefore the
90      owner is the global type. */
91   owner = get_glob_type();
92   /* Main has zero parameters and one result. */
93   proc_main = new_type_method(id_from_str("main", 4), 0, 1);
94   /* The result type is int. */
95   set_method_res_type(proc_main, 0, prim_t_int);
96
97   /* The entity for main. */
98   proc_main_e = new_entity (owner, id_from_str ("main", 4), proc_main);
99
100   /** Build code for procedure main. **/
101   /* We need one local variable (for "o"). */
102   main_irg = new_ir_graph (proc_main_e, 1);
103   o_pos = 0;
104
105   /* Remark that this irg is the main routine of the program. */
106   set_irp_main_irg(main_irg);
107
108   /* Make the constants.  They are independent of a block. */
109   c2 = new_Const (mode_i, tarval_from_long (mode_i, 2));
110   c5 = new_Const (mode_i, tarval_from_long (mode_i, 5));
111
112   /* There is only one block in main, it contains the allocation and the calls. */
113   /* Allocate the defined object and generate the type information. */
114   obj_size = new_SymConst((type_or_id_p)class_prima, size);
115   obj_o    = new_Alloc(get_store(), obj_size, class_prima, heap_alloc);
116   set_store(new_Proj(obj_o, mode_M, 0));  /* make the changed memory visible */
117   obj_o    = new_Proj(obj_o, mode_p, 2);  /* remember the pointer to the object */
118   set_value(o_pos, obj_o);
119
120   /* Get the pointer to the procedure from the object.  */
121   proc_ptr = new_simpleSel(get_store(),             /* The memory containing the object. */
122                            get_value(o_pos, mode_p),/* The pointer to the object. */
123                            proc_set_a_e );            /* The feature to select. */
124
125   /* Call procedure set_a, first built array with parameters. */
126   {
127     ir_node *in[2];
128     in[0] = get_value(o_pos, mode_p);
129     in[1] = c2;
130     call = new_Call(get_store(), proc_ptr, 2, in, proc_set_a);
131   }
132   /* Make the change to memory visible.  There are no results.  */
133   set_store(new_Proj(call, mode_M, 0));
134
135   /* Get the pointer to the nest procedure from the object. */
136   proc_ptr = new_simpleSel(get_store(), get_value(o_pos, mode_p), proc_c_e);
137
138   /* call procedure c, first built array with parameters */
139   {
140     ir_node *in[2];
141     in[0] = get_value(o_pos, mode_p);
142     in[1] = c5;
143     call = new_Call(get_store(), proc_ptr, 2, in, proc_c);
144   }
145   /* make the change to memory visible */
146   set_store(new_Proj(call, mode_M, 0));
147   /* Get the result of the procedure: select the result tuple from the call,
148      then the proper result from the tuple. */
149   res = new_Proj(new_Proj(call, mode_T, 2), mode_i, 0);
150
151   /* return the results of procedure main */
152   {
153      ir_node *in[1];
154      in[0] = res;
155      x = new_Return (get_store(), 1, in);
156   }
157   mature_block (get_irg_current_block(main_irg));
158
159   /* complete the end_block */
160   add_in_edge (get_irg_end_block(main_irg), x);
161   mature_block (get_irg_end_block(main_irg));
162
163   irg_vrfy(main_irg);
164   finalize_cons (main_irg);
165
166   /****************************************************************************/
167
168   printf("Creating IR graph for set_a: \n");
169
170   /* Local variables: self, e */
171   set_a_irg = new_ir_graph (proc_set_a_e, 2);
172   self_pos = 0; e_pos = 1;
173
174   /* get the procedure parameter */
175   self = new_Proj(get_irg_args(set_a_irg), mode_p, 0);
176   set_value(self_pos, self);
177   par1 = new_Proj(get_irg_args(set_a_irg), mode_i, 1);
178   set_value(e_pos, par1);
179   /* Create and select the entity to set */
180   a_ptr = new_simpleSel(get_store(), self, a_e);
181   /* perform the assignment */
182   set_store(new_Proj(new_Store(get_store(), a_ptr, par1), mode_M, 0));
183
184   /* return nothing */
185   x = new_Return (get_store (), 0, NULL);
186   mature_block (get_irg_current_block(set_a_irg));
187
188   /* complete the end_block */
189   add_in_edge (get_irg_end_block(set_a_irg), x);
190   mature_block (get_irg_end_block(set_a_irg));
191
192   /* verify the graph */
193   irg_vrfy(set_a_irg);
194   finalize_cons (set_a_irg);
195
196   /****************************************************************************/
197
198   printf("Creating IR graph for c: \n");
199
200   /* Local variables self, d */
201   c_irg = new_ir_graph (proc_c_e, 2);
202
203   /* get the procedure parameter */
204   self = new_Proj(get_irg_args(c_irg), mode_p, 0);
205   par1 = new_Proj(get_irg_args(c_irg), mode_i, 1);
206
207   /* Select the entity and load the value */
208   a_ptr = new_simpleSel(get_store(), self, a_e);
209   a_val = new_Load(get_store(), a_ptr);
210   set_store(new_Proj(a_val, mode_M, 0));
211   a_val = new_Proj(a_val, mode_i, 2);
212
213   /* return the result */
214   {
215     ir_node *in[1];
216     in[0] = new_Add(par1, a_val, mode_i);
217
218     x = new_Return (get_store (), 1, in);
219   }
220   mature_block (get_irg_current_block(c_irg));
221
222   /* complete the end_block */
223   add_in_edge (get_irg_end_block(c_irg), x);
224   mature_block (get_irg_end_block(c_irg));
225
226   /* verify the graph */
227   irg_vrfy(c_irg);
228   finalize_cons (c_irg);
229
230   /****************************************************************************/
231
232   printf("Optimizing ...\n");
233   for (i = 0; i < get_irp_n_irgs(); i++) {
234     local_optimize_graph(get_irp_irg(i));
235     dead_node_elimination(get_irp_irg(i));
236   }
237
238   printf("Dumping graphs of all procedures and a type graph.\n");
239   dump_all_ir_graphs(dump_ir_block_graph);
240   dump_all_ir_graphs(dump_ir_block_graph_w_types);
241   dump_all_types();
242
243   printf("Use xvcg to view these graphs:\n");
244   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
245   return (1);
246 }