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 /* create two global variables, a and b point to these variables */
90 new_entity(get_glob_type(),id_from_str("VAR_A",6),prim_t_int));
94 new_entity(get_glob_type(),id_from_str("VAR_B",6),prim_t_int));
95 /* set VAR_A and VAR_B to constant values */
96 set_store (new_Proj (new_Store (get_store (), a,
97 new_Const (mode_Iu, new_tarval_from_long (0, mode_Is))),
100 set_store (new_Proj (new_Store (get_store (), b,
101 new_Const (mode_Iu, new_tarval_from_long (1, mode_Is))),
104 /* finish this first block */
106 mature_block (get_irg_current_block(irg));
112 /* exchange the content of the two variables. Exceptions not cached. */
113 /* load the value and make it's effects visible. */
114 x = new_Load (get_store (), a);
115 set_store (new_Proj (x, mode_M, 0));
116 x = new_Proj(x, mode_Iu, 2);
117 /* the same again: load the value and make it's effects visible. */
118 y = new_Load (get_store (), b);
119 set_store (new_Proj (y, mode_M, 0));
120 y = new_Proj(y, mode_Iu, 2);
121 /* store the exchanged values. */
122 set_store (new_Proj (new_Store (get_store (), a, y), mode_M, 0));
123 set_store (new_Proj (new_Store (get_store (), b, x), mode_M, 0));
125 /* test the condition */
129 new_Const (mode_Iu, new_tarval_from_long (0, mode_Is)),
133 /* build the cfg of the loop */
134 add_in_edge (r, new_Proj (x, mode_X, 0));
135 x = new_Proj (x, mode_X, 1);
138 /* generate the block the loop exits to */
142 /* generate the return block and return the content of VAR_A */
145 x = new_Load (get_store (), a);
146 in[0] = new_Proj (x, mode_Iu, 2);
148 x = new_Return (new_Proj(x, mode_M, 0), 1, in);
151 add_in_edge (get_irg_end_block(irg), x);
152 mature_block (get_irg_end_block(irg));
156 printf("Optimizing ...\n");
157 dead_node_elimination(irg);
159 /* verify the graph */
162 printf("Done building the graph. Dumping it.\n");
163 dump_ir_block_graph (irg);
164 printf("Use xvcg to view this graph:\n");
165 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");