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:
24 *** set_a(e:int): void
40 type *owner, *class_prima, *class_langint;
41 type *proc_main, *proc_set, *proc_c;
43 entity *proc_main_e, *proc_set_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, *x;
47 ir_node *self, *par1, *a_ptr;
54 set_opt_constant_folding(1);
56 set_opt_dead_node_elimination(1);
58 /* make basic type information for primitive type int.*/
59 prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_i);
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, prim_t_int);
67 proc_main_e = new_entity (owner, id_from_str ("main", 4), proc_main);
68 main_irg = new_ir_graph (proc_main_e, 4);
69 /* Remark that this irg is the main routine of the program. */
70 set_irp_main_irg(main_irg);
72 /* There is only one block in main, it contains the constants and the calls. */
73 c2 = new_Const (mode_i, tarval_from_long (mode_i, 2));
74 c5 = new_Const (mode_i, tarval_from_long (mode_i, 5));
76 /* allocate the defined object and generate the type information */
77 class_prima = new_type_class(id_from_str ("PRIMA", 5));
78 obj_size = new_SymConst((type_or_id_p)class_prima, size);
79 obj_o = new_Alloc(get_store(), obj_size, class_prima, heap_alloc);
80 set_store(new_Proj(obj_o, mode_M, 0)); /* make the changed memory visible */
81 obj_o = new_Proj(obj_o, mode_p, 1); /* remember the pointer to the object */
82 /* we need type information for pointers to the class: */
83 class_p_ptr = new_type_pointer (id_from_str ("class_prima_ptr", 15),
86 /* get the pointer to the procedure from the class type */
87 proc_set = new_type_method(id_from_str("set_a", 5), 2, 0);
88 set_method_param_type(proc_set, 0, class_p_ptr);
89 set_method_param_type(proc_set, 1, prim_t_int);
90 proc_set_e = new_entity(class_prima, id_from_str ("set_a", 5), proc_set);
91 proc_ptr = new_simpleSel(get_store(), /* The memory the object is allocated in */
92 obj_o, /* The pointer to the object */
93 proc_set_e ); /* The feature to select */
95 /* call procedure set_a, first built array with parameters */
100 call = new_Call(get_store(), proc_ptr, 2, in, proc_set);
102 /* make the change to memory visible */
103 set_store(new_Proj(call, mode_M, 0));
105 /* get the pointer to the procedure from the class type */
106 proc_c = new_type_method(id_from_str("c", 1 ), 2, 1);
107 set_method_param_type(proc_c, 0, class_p_ptr);
108 set_method_param_type(proc_c, 1, prim_t_int);
109 set_method_res_type(proc_c, 0, prim_t_int);
110 proc_c_e = new_entity(class_prima, id_from_str ("c", 1), proc_c);
111 proc_ptr = new_simpleSel(get_store(), obj_o, proc_c_e);
113 /* call procedure c, first built array with parameters */
118 call = new_Call(get_store(), proc_ptr, 2, in, proc_c);
120 /* make the change to memory visible */
121 set_store(new_Proj(call, mode_M, 0));
123 /* return the results of procedure main */
126 /* Select the result tuple from the call, then the proper
127 result from the tuple. */
128 in[0] = new_Proj(new_Proj(call, mode_T, 1), mode_I, 0);
130 x = new_Return (get_store (), 1, in);
132 mature_block (get_irg_current_block(main_irg));
134 /* complete the end_block */
135 add_in_edge (get_irg_end_block(main_irg), x);
136 mature_block (get_irg_end_block(main_irg));
140 /****************************************************************************/
142 printf("Creating IR graph for set_a: \n");
144 set_a_irg = new_ir_graph (proc_set_e, 4);
146 /* get the procedure parameter */
147 self = new_Proj(get_irg_args(set_a_irg), mode_p, 0);
148 par1 = new_Proj(get_irg_args(set_a_irg), mode_I, 1);
149 /* Create and select the entity to set */
150 class_langint = new_type_class(id_from_str ("Int", 3));
151 a_e = new_entity(class_prima, id_from_str ("a", 1),
153 a_ptr = new_simpleSel(get_store(), self, a_e);
154 /* perform the assignment */
155 set_store(new_Proj(new_Store(get_store(), a_ptr, par1), mode_M, 0));
158 x = new_Return (get_store (), 0, NULL);
159 mature_block (get_irg_current_block(set_a_irg));
161 /* complete the end_block */
162 add_in_edge (get_irg_end_block(set_a_irg), x);
163 mature_block (get_irg_end_block(set_a_irg));
167 /****************************************************************************/
169 printf("Creating IR graph for c: \n");
171 c_irg = new_ir_graph (proc_c_e, 4);
173 /* get the procedure parameter */
174 self = new_Proj(get_irg_args(c_irg), mode_p, 0);
175 par1 = new_Proj(get_irg_args(c_irg), mode_I, 1);
177 /* Select the entity and load the value */
178 a_ptr = new_simpleSel(get_store(), self, a_e);
179 a_val = new_Load(get_store(), a_ptr);
180 set_store(new_Proj(a_val, mode_M, 0));
181 a_val = new_Proj(a_val, mode_I, 1);
183 /* return the result */
186 in[0] = new_Add(par1, a_val, mode_I);
188 x = new_Return (get_store (), 1, in);
190 mature_block (get_irg_current_block(c_irg));
192 /* complete the end_block */
193 add_in_edge (get_irg_end_block(c_irg), x);
194 mature_block (get_irg_end_block(c_irg));
196 /* verify the graph */
199 printf("Optimizing ...\n");
200 for (i = 0; i < get_irp_n_irgs(); i++) {
201 local_optimize_graph(get_irp_irg(i));
202 dead_node_elimination(get_irp_irg(i));
204 /****************************************************************************/
206 printf("Dumping graphs of all procedures.\n");
208 dump_all_ir_graphs(dump_ir_block_graph);
209 dump_all_ir_graphs(dump_type_graph);
212 printf("Use xvcg to view these graphs:\n");
213 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");