1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
4 ** Authors: Goetz Lindenmaier
12 /** This example describes representation of stack allocated variables of
13 *** imperative programs.
14 *** It constructs the IR for the following program:
23 *** The array is placed on the stack, i.e., a pointer to the array
24 *** is obtained by selecting the entity "a" from the stack. The variables
25 *** on the stack are considered to be entities of the method, as locals
26 *** of a method are only visible within the method. (An alternative to
27 *** make the method owner of the stack variables is to give the ownership
28 *** to the class representing the C-file. This would extend the visibility
29 *** of the locals, though.)
33 #define OPTIMIZE_NODE 0
38 /* describes the general structure of a C-file */
39 type *owner; /* the class standing for everything in this file */
40 type *proc_main; /* Typeinformation for method main. */
41 entity *proc_main_e; /* The entity describing that method main is an
42 entity of the fake class representing the file. */
44 /* describes types defined by the language */
47 /* describes the array and its fields. */
48 entity *array_ent; /* the entity representing the array as member
49 of the stack/method */
50 type *array_type; /* the type information for the array */
51 entity *field_ent; /* the entity representing a field of the array */
53 /* Needed while finding the element size. */
55 ir_mode *elt_type_mode;
59 /* holds the graph and nodes. */
61 ir_node *array, *array_ptr, *c3, *elt, *val, *x;
66 printf("\nCreating an IR graph: ARRAY-STACK_EXAMPLE...\n");
68 /* make basic type information for primitive type int.
69 In Sather primitive types are represented by a class.
70 This is the modeling appropriate for other languages.
71 Mode_i says that all language-integers shall be implemented
72 as a 32 bit processor-integer value. */
73 prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_i);
75 /* build typeinformation of procedure main */
76 owner = new_type_class (id_from_str ("ARRAY-STACK_EXAMPLE", 19));
77 proc_main = new_type_method(id_from_str("main_tp", 4), 0, 1);
78 set_method_res_type(proc_main, 0, prim_t_int);
79 proc_main_e = new_entity (owner, id_from_str ("main", 4), proc_main);
81 main_irg = new_ir_graph (proc_main_e, 4);
83 /* make type information for the array and set the bounds */
87 array_type = new_type_array(id_from_str("a_tp", 4), N_DIMS, prim_t_int);
88 set_array_bounds(array_type, 1,
89 new_Const(mode_I, tarval_from_long (mode_I, L_BOUND)),
90 new_Const(mode_I, tarval_from_long (mode_I, U_BOUND)));
91 /* The array is an entity of the method, placed on the mehtod's own memory,
93 array_ent = new_entity(get_cur_frame_type(), id_from_str("a", 1), array_type);
94 /* As the array is accessed by Sel nodes, we need information about
95 the entity the node selects. Entities of an array are it's elements
96 which are, in this case, integers. */
97 /* change entity owner types. */
98 field_ent = get_array_element_entity(array_type);
102 /* Now the "real" program: */
103 /* Select the array from the stack frame. */
104 array_ptr = new_simpleSel(get_store(), get_irg_frame(main_irg), array_ent);
105 /* Load element 3 of the array. For this first generate the pointer
106 to this the element by a select node. (Alternative: increase
107 array pointer by (three * elt_size), but this complicates some
108 optimizations.) The type information accessible via the entity
109 allows to generate the pointer increment later. */
110 c3 = new_Const (mode_I, tarval_from_long (mode_I, 3));
114 elt = new_Sel(get_store(), array_ptr, 1, in, field_ent);
116 val = new_Load(get_store(), elt);
117 set_store(new_Proj(val, mode_M, 0));
118 val = new_Proj(val, mode_i, 2);
120 /* return the result of procedure main */
125 x = new_Return (get_store (), 1, in);
127 mature_block (get_irg_current_block(main_irg));
129 /* complete the end_block */
130 add_in_edge (get_irg_end_block(main_irg), x);
131 mature_block (get_irg_end_block(main_irg));
133 printf("Optimizing ...\n");
134 dead_node_elimination(main_irg);
136 /* verify the graph */
140 printf("Dumping the graph and a type graph.\n");
141 dump_ir_block_graph (main_irg);
142 dump_type_graph(main_irg);
143 dump_ir_block_graph_w_types(main_irg);
144 printf("Use xvcg to view these graphs:\n");
145 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");