852dd666352a5e88c0368c38749a6d4531a3b67c
[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 ***    c(int): int;
17 ***    set_a(int): void;
18 ***  end
19 ***
20 ***  c(d: int): int
21 ***    return (d + a);
22 ***  end
23 ***
24 ***  set_a(e:int): void
25 ***    self.a = e;
26 ***  end
27 ***
28 ***  main(): int
29 ***    o: PRIMA;
30 ***    o.set_a(2);
31 ***    return o.c(5);
32 ***  end
33 ***
34 **/
35
36 int
37 main(void)
38 {
39   type_primitive *prim_t_int;
40   type_class   *owner, *class_prima, *class_langint;
41   type_method  *proc_main, *proc_set, *proc_c;
42   type_pointer *class_p_ptr;
43   entity       *proc_main_e, *proc_set_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, *x;
47   ir_node      *self, *par1, *a_ptr;
48   ir_node      *a_val;
49
50   int i;
51
52   init_firm ();
53
54   set_opt_constant_folding(1);
55   set_opt_cse(1);
56   set_opt_dead_node_elimination(1);
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("\nCreating 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   /* Remark that this irg is the main routine of the program. */
71   set_irp_main_irg(main_irg);
72
73   /* There is only one block in main, it contains the constants and the calls. */
74   c2 = new_Const (mode_i, tarval_from_long (mode_i, 2));
75   c5 = new_Const (mode_i, tarval_from_long (mode_i, 5));
76
77   /* allocate the defined object and generate the type information */
78   class_prima = new_type_class(id_from_str ("PRIMA", 5));
79   obj_size = new_SymConst((type_or_id_p)class_prima, size);
80   obj_o    = new_Alloc(get_store(), obj_size, (type *)class_prima, heap_alloc);
81   set_store(new_Proj(obj_o, mode_M, 0));  /* make the changed memory visible */
82   obj_o    = new_Proj(obj_o, mode_p, 1);  /* remember the pointer to the object */
83   /* we need type information for pointers to the class: */
84   class_p_ptr = new_type_pointer (id_from_str ("class_prima_ptr", 15),
85                                   (type *)class_prima);
86
87   /* get the pointer to the procedure from the class type */
88   proc_set = new_type_method(id_from_str("set_a", 5), 2, 0);
89   set_method_param_type(proc_set, 0, (type *)class_p_ptr);
90   set_method_param_type(proc_set, 1, (type *)prim_t_int);
91   proc_set_e = new_entity((type *)class_prima, id_from_str ("set_a", 5),
92                           (type*)proc_set);
93   proc_ptr = new_simpleSel(get_store(),  /* The memory the object is allocated in */
94                       obj_o,             /* The pointer to the object */
95                       proc_set_e );      /* The feature to select */
96
97   /* call procedure set_a, first built array with parameters */
98   {
99     ir_node *in[2];
100     in[0] = obj_o;
101     in[1] = c2;
102     call = new_Call(get_store(), proc_ptr, 2, in, proc_set);
103   }
104   /* make the change to memory visible */
105   set_store(new_Proj(call, mode_M, 0));
106
107   /* get the pointer to the procedure from the class type */
108   proc_c   = new_type_method(id_from_str("c", 1 ), 2, 1);
109   set_method_param_type(proc_c, 0, (type *)class_p_ptr);
110   set_method_param_type(proc_c, 1, (type *)prim_t_int);
111   set_method_res_type(proc_c, 0, (type *)prim_t_int);
112   proc_c_e = new_entity((type *)class_prima, id_from_str ("c", 1),
113                         (type*)proc_c);
114   proc_ptr = new_simpleSel(get_store(), obj_o, proc_c_e);
115
116   /* call procedure c, first built array with parameters */
117   {
118     ir_node *in[2];
119     in[0] = obj_o;
120     in[1] = c5;
121     call = new_Call(get_store(), proc_ptr, 2, in, proc_c);
122   }
123   /* make the change to memory visible */
124   set_store(new_Proj(call, mode_M, 0));
125
126   /* return the results of procedure main */
127   {
128      ir_node *in[1];
129      /* Select the result tuple from the call, then the proper
130         result from the tuple. */
131      in[0] = new_Proj(new_Proj(call, mode_T, 1), mode_I, 0);
132
133      x = new_Return (get_store (), 1, in);
134   }
135   mature_block (get_irg_current_block(main_irg));
136
137   /* complete the end_block */
138   add_in_edge (get_irg_end_block(main_irg), x);
139   mature_block (get_irg_end_block(main_irg));
140
141   irg_vrfy(main_irg);
142
143   /****************************************************************************/
144
145   printf("Creating IR graph for set_a: \n");
146
147   set_a_irg = new_ir_graph (proc_set_e, 4);
148
149   /* get the procedure parameter */
150   self = new_Proj(get_irg_args(set_a_irg), mode_p, 0);
151   par1 = new_Proj(get_irg_args(set_a_irg), mode_I, 1);
152   /* Create and select the entity to set */
153   class_langint = new_type_class(id_from_str ("Int", 3));
154   a_e = new_entity((type *)class_prima, id_from_str ("a", 1),
155                    (type*)class_langint);
156   a_ptr = new_simpleSel(get_store(), self, a_e);
157   /* perform the assignment */
158   set_store(new_Proj(new_Store(get_store(), a_ptr, par1), mode_M, 0));
159
160   /* return nothing */
161   x = new_Return (get_store (), 0, NULL);
162   mature_block (get_irg_current_block(set_a_irg));
163
164   /* complete the end_block */
165   add_in_edge (get_irg_end_block(set_a_irg), x);
166   mature_block (get_irg_end_block(set_a_irg));
167
168   irg_vrfy(set_a_irg);
169
170   /****************************************************************************/
171
172   printf("Creating IR graph for c: \n");
173
174   c_irg = new_ir_graph (proc_c_e, 4);
175
176   /* get the procedure parameter */
177   self = new_Proj(get_irg_args(c_irg), mode_p, 0);
178   par1 = new_Proj(get_irg_args(c_irg), mode_I, 1);
179
180   /* Select the entity and load the value */
181   a_ptr = new_simpleSel(get_store(), self, a_e);
182   a_val = new_Load(get_store(), a_ptr);
183   set_store(new_Proj(a_val, mode_M, 0));
184   a_val = new_Proj(a_val, mode_I, 1);
185
186   /* return the result */
187   {
188     ir_node *in[1];
189     in[0] = new_Add(par1, a_val, mode_I);
190
191     x = new_Return (get_store (), 1, in);
192   }
193   mature_block (get_irg_current_block(c_irg));
194
195   /* complete the end_block */
196   add_in_edge (get_irg_end_block(c_irg), x);
197   mature_block (get_irg_end_block(c_irg));
198
199   /* verify the graph */
200   irg_vrfy(main_irg);
201
202   printf("Optimizing ...\n");
203   for (i = 0; i < get_irp_n_irgs(); i++) {
204     local_optimize_graph(get_irp_irg(i));
205     dead_node_elimination(get_irp_irg(i));
206   }
207   /****************************************************************************/
208
209   printf("Dumping graphs of all procedures.\n");
210
211   dump_all_ir_graphs(dump_ir_block_graph);
212   dump_all_ir_graphs(dump_type_graph);
213   dump_all_types();
214
215   printf("Use xvcg to view these graphs:\n");
216   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
217   return (1);
218 }