minor imporvements: comments, output
[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
87   /*** Now build procedure main. ***/
88   /** Type information for main. **/
89   printf("\nCreating an IR graph: OO_PROGRAM_EXAMPLE...\n");
90   /* Main is not modeled as part of an explicit class here. Therefore the
91      owner is the global type. */
92   owner = get_glob_type();
93   /* Main has zero parameters and one result. */
94   proc_main = new_type_method(id_from_str("main", 4), 0, 1);
95   /* The result type is int. */
96   set_method_res_type(proc_main, 0, prim_t_int);
97
98   /* The entity for main. */
99   proc_main_e = new_entity (owner, id_from_str ("main", 4), proc_main);
100
101   /** Build code for procedure main. **/
102   /* We need one local variable (for "o"). */
103   main_irg = new_ir_graph (proc_main_e, 1);
104   o_pos = 0;
105
106   /* Remark that this irg is the main routine of the program. */
107   set_irp_main_irg(main_irg);
108
109   /* Make the constants.  They are independent of a block. */
110   c2 = new_Const (mode_i, tarval_from_long (mode_i, 2));
111   c5 = new_Const (mode_i, tarval_from_long (mode_i, 5));
112
113   /* There is only one block in main, it contains the allocation and the calls. */
114   /* Allocate the defined object and generate the type information. */
115   obj_size = new_SymConst((type_or_id_p)class_prima, size);
116   obj_o    = new_Alloc(get_store(), obj_size, class_prima, heap_alloc);
117   set_store(new_Proj(obj_o, mode_M, 0));  /* make the changed memory visible */
118   obj_o    = new_Proj(obj_o, mode_p, 1);  /* remember the pointer to the object */
119   set_value(o_pos, obj_o);
120
121   /* Get the pointer to the procedure from the object.  */
122   proc_ptr = new_simpleSel(get_store(),             /* The memory containing the object. */
123                            get_value(o_pos, mode_p),/* The pointer to the object. */
124                            proc_set_a_e );            /* The feature to select. */
125
126   /* Call procedure set_a, first built array with parameters. */
127   {
128     ir_node *in[2];
129     in[0] = get_value(o_pos, mode_p);
130     in[1] = c2;
131     call = new_Call(get_store(), proc_ptr, 2, in, proc_set_a);
132   }
133   /* Make the change to memory visible.  There are no results.  */
134   set_store(new_Proj(call, mode_M, 0));
135
136   /* Get the pointer to the nest procedure from the object. */
137   proc_ptr = new_simpleSel(get_store(), get_value(o_pos, mode_p), proc_c_e);
138
139   /* call procedure c, first built array with parameters */
140   {
141     ir_node *in[2];
142     in[0] = get_value(o_pos, mode_p);
143     in[1] = c5;
144     call = new_Call(get_store(), proc_ptr, 2, in, proc_c);
145   }
146   /* make the change to memory visible */
147   set_store(new_Proj(call, mode_M, 0));
148   /* Get the result of the procedure: select the result tuple from the call,
149      then the proper result from the tuple. */
150   res = new_Proj(new_Proj(call, mode_T, 1), mode_I, 0);
151
152   /* return the results of procedure main */
153   {
154      ir_node *in[1];
155      in[0] = res;
156      x = new_Return (get_store(), 1, in);
157   }
158   mature_block (get_irg_current_block(main_irg));
159
160   /* complete the end_block */
161   add_in_edge (get_irg_end_block(main_irg), x);
162   mature_block (get_irg_end_block(main_irg));
163
164   irg_vrfy(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
195   /****************************************************************************/
196
197   printf("Creating IR graph for c: \n");
198
199   /* Local variables self, d */
200   c_irg = new_ir_graph (proc_c_e, 2);
201
202   /* get the procedure parameter */
203   self = new_Proj(get_irg_args(c_irg), mode_p, 0);
204   par1 = new_Proj(get_irg_args(c_irg), mode_I, 1);
205
206   /* Select the entity and load the value */
207   a_ptr = new_simpleSel(get_store(), self, a_e);
208   a_val = new_Load(get_store(), a_ptr);
209   set_store(new_Proj(a_val, mode_M, 0));
210   a_val = new_Proj(a_val, mode_I, 1);
211
212   /* return the result */
213   {
214     ir_node *in[1];
215     in[0] = new_Add(par1, a_val, mode_I);
216
217     x = new_Return (get_store (), 1, in);
218   }
219   mature_block (get_irg_current_block(c_irg));
220
221   /* complete the end_block */
222   add_in_edge (get_irg_end_block(c_irg), x);
223   mature_block (get_irg_end_block(c_irg));
224
225   /* verify the graph */
226   irg_vrfy(c_irg);
227
228   /****************************************************************************/
229
230   printf("Optimizing ...\n");
231   for (i = 0; i < get_irp_n_irgs(); i++) {
232     local_optimize_graph(get_irp_irg(i));
233     dead_node_elimination(get_irp_irg(i));
234   }
235
236   printf("Dumping graphs of all procedures and a type graph.\n");
237   dump_all_ir_graphs(dump_ir_block_graph);
238   dump_all_ir_graphs(dump_ir_block_graph_w_types);
239   dump_all_types();
240
241   printf("Use xvcg to view these graphs:\n");
242   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
243   return (1);
244 }