1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
4 * Authors: Christian Schaefer, Goetz Lindenmaier
18 * This file constructs the ir for the following pseudo-program:
38 * A better example would be the following program:
39 * (name e.g.: memory-imp_example.c as it models imperative concepts.)
41 * In this program a local variable is dereferenced. It has
42 * to be modeled as an entity of the stack so that a pointer to it is available.
43 * It is also an example where an analysis could find out that the
44 * pointer is never actually used.
62 type *method; /* the type of this method */
65 ir_node *a, *b, *x, *y, *r;
67 printf("\nCreating an IR graph: MEMORY_EXAMPLE...\n");
71 set_opt_dead_node_elimination (1);
73 /*** Make basic type information for primitive type int. ***/
74 prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_Iu);
76 /* a class to get started with, containing the main procedure */
77 owner = new_type_class (id_from_str ("MEMORY_EXAMPLE", 14));
78 method = new_type_method (id_from_str("main", 4), 0, 1);
79 set_method_res_type(method, 0, prim_t_int);
80 ent = new_entity (owner, id_from_str ("main", 4), method);
81 get_entity_ld_name(ent); /* To enforce name mangling for vcg graph name */
83 /* Generates start and end blocks and nodes and a first, initial block */
84 irg = new_ir_graph (ent, 4);
86 /* generate two constant pointers to string constants */
87 /* this simulates two global variables, a and b point to these variables */
88 a = new_Const (mode_P, tarval_P_from_str ("VAR_A"));
89 b = new_Const (mode_P, tarval_P_from_str ("VAR_B"));
91 /* set VAR_A and VAR_B to constant values */
92 set_store (new_Proj (new_Store (get_store (), a,
93 new_Const (mode_Iu, tarval_from_long (mode_Is, 0))),
96 set_store (new_Proj (new_Store (get_store (), b,
97 new_Const (mode_Iu, tarval_from_long (mode_Is, 1))),
100 /* finish this first block */
102 mature_block (get_irg_current_block(irg));
108 /* exchange the content of the two variables. Exceptions not cached. */
109 /* load the value and make it's effects visible. */
110 x = new_Load (get_store (), a);
111 set_store (new_Proj (x, mode_M, 0));
112 x = new_Proj(x, mode_Iu, 2);
113 /* the same again: load the value and make it's effects visible. */
114 y = new_Load (get_store (), b);
115 set_store (new_Proj (y, mode_M, 0));
116 y = new_Proj(y, mode_Iu, 2);
117 /* store the exchanged values. */
118 set_store (new_Proj (new_Store (get_store (), a, y), mode_M, 0));
119 set_store (new_Proj (new_Store (get_store (), b, x), mode_M, 0));
121 /* test the condition */
125 new_Const (mode_Iu, tarval_from_long (mode_Is, 0)),
129 /* build the cfg of the loop */
130 add_in_edge (r, new_Proj (x, mode_X, 0));
131 x = new_Proj (x, mode_X, 1);
134 /* generate the block the loop exits to */
138 /* generate the return block and return the content of VAR_A */
141 x = new_Load (get_store (), a);
142 in[0] = new_Proj (x, mode_Iu, 2);
144 x = new_Return (new_Proj(x, mode_M, 0), 1, in);
147 add_in_edge (get_irg_end_block(irg), x);
148 mature_block (get_irg_end_block(irg));
152 printf("Optimizing ...\n");
153 dead_node_elimination(irg);
155 /* verify the graph */
158 printf("Done building the graph. Dumping it.\n");
159 dump_ir_block_graph (irg);
160 printf("Use xvcg to view this graph:\n");
161 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");