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:
25 *** set_a(e:int): void
37 #define OPTIMIZE_NODE 0
42 type_primitive *prim_t_int;
43 type_class *owner, *class_prima, *class_langint;
44 type_method *proc_main, *proc_set, *proc_c;
45 type_pointer *class_p_ptr;
46 entity *proc_main_e, *proc_set_e, *proc_c_e, *a_e;
48 ir_graph *main_irg, *set_a_irg, *c_irg;
49 ir_node *c2, *c5, *obj_o, *obj_size, *proc_ptr, *call, *x;
50 ir_node *self, *par1, *a_ptr;
57 set_opt_constant_folding(0);
60 /* make basic type information for primitive type int.*/
61 prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_i);
63 /* first build procedure main */
64 printf("creating an IR graph: OO_PROGRAM_EXAMPLE...\n");
65 owner = get_glob_type();
66 proc_main = new_type_method(id_from_str("main", 4), 0, 1);
67 set_method_res_type(proc_main, 0, (type *)prim_t_int);
69 proc_main_e = new_entity ((type *)owner, id_from_str ("main", 4),
71 main_irg = new_ir_graph (proc_main_e, 4);
72 /* Remark that this irg is the main routine of the program. */
73 set_irp_main_irg(main_irg);
75 /* There is only one block in main, it contains the constants and the calls. */
76 c2 = new_Const (mode_i, tarval_from_long (mode_i, 2));
77 c5 = new_Const (mode_i, tarval_from_long (mode_i, 5));
79 /* allocate the defined object and generate the type information */
80 class_prima = new_type_class(id_from_str ("PRIMA", 5));
81 obj_size = new_SymConst((type_or_id *)class_prima, size);
82 obj_o = new_Alloc(get_store(), obj_size, (type *)class_prima, heap_alloc);
83 set_store(new_Proj(obj_o, mode_M, 0)); /* make the changed memory visible */
84 obj_o = new_Proj(obj_o, mode_p, 1); /* remember the pointer to the object */
85 /* we need type information for pointers to the class: */
86 class_p_ptr = new_type_pointer (id_from_str ("class_prima_ptr", 15),
89 /* get the pointer to the procedure from the class type */
90 proc_set = new_type_method(id_from_str("set_a", 5), 2, 0);
91 set_method_param_type(proc_set, 0, (type *)class_p_ptr);
92 set_method_param_type(proc_set, 1, (type *)prim_t_int);
93 proc_set_e = new_entity((type *)class_prima, id_from_str ("set_a", 5),
95 proc_ptr = new_simpleSel(get_store(), /* The memory the object is allocated in */
96 obj_o, /* The pointer to the object */
97 proc_set_e ); /* The feature to select */
99 /* call procedure set_a, first built array with parameters */
104 call = new_Call(get_store(), proc_ptr, 2, in, proc_set);
106 /* make the change to memory visible */
107 set_store(new_Proj(call, mode_M, 0));
109 /* get the pointer to the procedure from the class type */
110 proc_c = new_type_method(id_from_str("c", 1 ), 2, 1);
111 set_method_param_type(proc_c, 0, (type *)class_p_ptr);
112 set_method_param_type(proc_c, 1, (type *)prim_t_int);
113 set_method_res_type(proc_c, 0, (type *)prim_t_int);
114 proc_c_e = new_entity((type *)class_prima, id_from_str ("c", 1),
116 proc_ptr = new_simpleSel(get_store(), obj_o, proc_c_e);
118 /* call procedure c, first built array with parameters */
123 call = new_Call(get_store(), proc_ptr, 2, in, proc_c);
125 /* make the change to memory visible */
126 set_store(new_Proj(call, mode_M, 0));
128 /* return the results of procedure main */
131 /* Select the result tuple from the call, then the proper
132 result from the tuple. */
133 in[0] = new_Proj(new_Proj(call, mode_T, 1), mode_I, 0);
135 x = new_Return (get_store (), 1, in);
137 mature_block (main_irg->current_block);
139 /* complete the end_block */
140 add_in_edge (main_irg->end_block, x);
141 mature_block (main_irg->end_block);
143 printf("\nDone building the graph.\n");
146 /****************************************************************************/
148 printf("\ncreating IR graph for set_a: \n");
150 set_a_irg = new_ir_graph (proc_set_e, 4);
152 /* get the procedure parameter */
153 self = new_Proj(set_a_irg->args, mode_p, 0);
154 par1 = new_Proj(set_a_irg->args, mode_I, 1);
155 /* Create and select the entity to set */
156 class_langint = new_type_class(id_from_str ("Int", 3));
157 a_e = new_entity((type *)class_prima, id_from_str ("a", 1),
158 (type*)class_langint);
159 a_ptr = new_simpleSel(get_store(), self, a_e);
160 /* perform the assignment */
161 set_store(new_Proj(new_Store(get_store(), a_ptr, par1), mode_M, 0));
164 x = new_Return (get_store (), 0, NULL);
165 mature_block (set_a_irg->current_block);
167 /* complete the end_block */
168 add_in_edge (set_a_irg->end_block, x);
169 mature_block (set_a_irg->end_block);
171 printf("\nDone building the graph.\n");
174 /****************************************************************************/
176 printf("\ncreating IR graph for c: \n");
178 c_irg = new_ir_graph (proc_c_e, 4);
180 /* get the procedure parameter */
181 self = new_Proj(c_irg->args, mode_p, 0);
182 par1 = new_Proj(c_irg->args, mode_I, 1);
184 /* Select the entity and load the value */
185 a_ptr = new_simpleSel(get_store(), self, a_e);
186 a_val = new_Load(get_store(), a_ptr);
187 set_store(new_Proj(a_val, mode_M, 0));
188 a_val = new_Proj(a_val, mode_I, 1);
190 /* return the result */
193 in[0] = new_Add(par1, a_val, mode_I);
195 x = new_Return (get_store (), 1, in);
197 mature_block (c_irg->current_block);
199 /* complete the end_block */
200 add_in_edge (c_irg->end_block, x);
201 mature_block (c_irg->end_block);
203 /* verify the graph */
206 printf("\nDone building the graph.\n");
208 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));
215 /****************************************************************************/
217 printf("\nuse xvcg to view these graphs:\n");
218 printf("/ben/goetz/bin/xvcg GRAPHNAME\n");