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