1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
4 ** Authors: Goetz Lindenmaier
12 /** This file constructs the IR for the following program:
18 *** return (d + self.a);
21 *** void set_a(e:int) {
40 type *owner, *class_prima;
41 type *proc_main, *proc_set_a, *proc_c;
43 entity *proc_main_e, *proc_set_a_e, *proc_c_e, *a_e;
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;
50 int o_pos, self_pos, e_pos, d_pos;
56 set_opt_constant_folding(1);
58 set_opt_dead_node_elimination(1);
60 /*** Make basic type information for primitive type int. ***/
61 prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_i);
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),
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
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
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);
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);
98 /* The entity for main. */
99 proc_main_e = new_entity (owner, id_from_str ("main", 4), proc_main);
101 /** Build code for procedure main. **/
102 /* We need one local variable (for "o"). */
103 main_irg = new_ir_graph (proc_main_e, 1);
106 /* Remark that this irg is the main routine of the program. */
107 set_irp_main_irg(main_irg);
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));
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, 2); /* remember the pointer to the object */
119 set_value(o_pos, obj_o);
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. */
126 /* Call procedure set_a, first built array with parameters. */
129 in[0] = get_value(o_pos, mode_p);
131 call = new_Call(get_store(), proc_ptr, 2, in, proc_set_a);
133 /* Make the change to memory visible. There are no results. */
134 set_store(new_Proj(call, mode_M, 0));
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);
139 /* call procedure c, first built array with parameters */
142 in[0] = get_value(o_pos, mode_p);
144 call = new_Call(get_store(), proc_ptr, 2, in, proc_c);
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, 2), mode_i, 0);
152 /* return the results of procedure main */
156 x = new_Return (get_store(), 1, in);
158 mature_block (get_irg_current_block(main_irg));
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));
166 /****************************************************************************/
168 printf("Creating IR graph for set_a: \n");
170 /* Local variables: self, e */
171 set_a_irg = new_ir_graph (proc_set_a_e, 2);
172 self_pos = 0; e_pos = 1;
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));
185 x = new_Return (get_store (), 0, NULL);
186 mature_block (get_irg_current_block(set_a_irg));
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));
192 /* verify the graph */
195 /****************************************************************************/
197 printf("Creating IR graph for c: \n");
199 /* Local variables self, d */
200 c_irg = new_ir_graph (proc_c_e, 2);
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);
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, 2);
212 /* return the result */
215 in[0] = new_Add(par1, a_val, mode_i);
217 x = new_Return (get_store (), 1, in);
219 mature_block (get_irg_current_block(c_irg));
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));
225 /* verify the graph */
228 /****************************************************************************/
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));
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);
241 printf("Use xvcg to view these graphs:\n");
242 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");