3 * File name: testprograms/memory_example.c
4 * Purpose: Illustrate memory edges.
5 * Author: Christian Schaefer, Goetz Lindenmaier
9 * Copyright: (c) 1999-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
22 * This file constructs the ir for the following pseudo-program:
42 * A better example would be the following program:
43 * (name e.g.: memory-imp_example.c as it models imperative concepts.)
45 * In this program a local variable is dereferenced. It has
46 * to be modeled as an entity of the stack so that a pointer to it is available.
47 * It is also an example where an analysis could find out that the
48 * pointer is never actually used.
66 type *method; /* the type of this method */
69 ir_node *a, *b, *x, *y, *r;
71 printf("\nCreating an IR graph: MEMORY_EXAMPLE...\n");
75 set_opt_dead_node_elimination (1);
77 /*** Make basic type information for primitive type int. ***/
78 prim_t_int = new_type_primitive(new_id_from_chars ("int", 3), mode_Iu);
80 /* a class to get started with, containing the main procedure */
81 owner = new_type_class (new_id_from_chars ("MEMORY_EXAMPLE", 14));
82 method = new_type_method (new_id_from_chars("main", 4), 0, 1);
83 set_method_res_type(method, 0, prim_t_int);
84 ent = new_entity (owner, new_id_from_chars ("main", 4), method);
85 get_entity_ld_name(ent); /* To enforce name mangling for vcg graph name */
87 /* Generates start and end blocks and nodes and a first, initial block */
88 irg = new_ir_graph (ent, 4);
90 /* create two global variables, a and b point to these variables */
94 new_entity(get_glob_type(),new_id_from_chars("VAR_A",6),prim_t_int));
98 new_entity(get_glob_type(),new_id_from_chars("VAR_B",6),prim_t_int));
99 /* set VAR_A and VAR_B to constant values */
100 set_store (new_Proj (new_Store (get_store (), a,
101 new_Const (mode_Iu, new_tarval_from_long (0, mode_Is))),
104 set_store (new_Proj (new_Store (get_store (), b,
105 new_Const (mode_Iu, new_tarval_from_long (1, mode_Is))),
108 /* finish this first block */
110 mature_immBlock (get_irg_current_block(irg));
114 add_immBlock_pred (r, x);
116 /* exchange the content of the two variables. Exceptions not cached. */
117 /* load the value and make it's effects visible. */
118 x = new_Load (get_store (), a, mode_Iu);
119 set_store (new_Proj (x, mode_M, 0));
120 x = new_Proj(x, mode_Iu, 2);
121 /* the same again: load the value and make it's effects visible. */
122 y = new_Load (get_store (), b, mode_Iu);
123 set_store (new_Proj (y, mode_M, 0));
124 y = new_Proj(y, mode_Iu, 2);
125 /* store the exchanged values. */
126 set_store (new_Proj (new_Store (get_store (), a, y), mode_M, 0));
127 set_store (new_Proj (new_Store (get_store (), b, x), mode_M, 0));
129 /* test the condition */
133 new_Const (mode_Iu, new_tarval_from_long (0, mode_Is)),
137 /* build the cfg of the loop */
138 add_immBlock_pred (r, new_Proj (x, mode_X, 0));
139 x = new_Proj (x, mode_X, 1);
142 /* generate the block the loop exits to */
144 add_immBlock_pred (r, x);
146 /* generate the return block and return the content of VAR_A */
149 x = new_Load (get_store (), a, mode_Iu);
150 in[0] = new_Proj (x, mode_Iu, 2);
152 x = new_Return (new_Proj(x, mode_M, 0), 1, in);
155 add_immBlock_pred (get_irg_end_block(irg), x);
156 mature_immBlock (get_irg_end_block(irg));
160 printf("Optimizing ...\n");
161 dead_node_elimination(irg);
163 /* verify the graph */
166 printf("Done building the graph. Dumping it.\n");
167 dump_ir_block_graph (irg, 0);
168 printf("Use xvcg to view this graph:\n");
169 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");