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