1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
4 ** Authors: Christian Schaefer, Goetz Lindenmaier
12 /** This example demonstrates the use of memory edges.
13 *** This file constructs the ir for the following pseudo-program:
33 *** A better example would be the following program:
34 *** (name e.g.: memory-imp_example.c as it models imperative concepts.)
36 *** In this program a local variable is dereferenced. It has
37 *** to be modeled as an entity of the stack so that a pointer to it is available.
38 *** It is also an example where an analysis could find out that the
39 *** pointer is never actually used.
57 type_method *method; /* the type of this method */
59 ir_node *a, *b, *x, *y, *r;
61 printf("creating an IR graph: MEMORY_EXAMPLE...\n");
65 /* a class to get started with, containing the main procedure */
66 owner = new_type_class (id_from_str ("MEMORY_EXAMPLE", 14));
67 method = new_type_method (id_from_str("main", 4), 0, 2);
68 ent = new_entity ((type *)owner, id_from_str ("main", 4), (type *)method);
70 /* Generates start and end blocks and nodes and a first, initial block */
71 irg = new_ir_graph (ent, 4);
73 /* generate two constant pointers to string constants */
74 /* this simulates two global variables, a and b point to these variables */
75 a = new_Const (mode_p, tarval_p_from_str ("VAR_A"));
76 b = new_Const (mode_p, tarval_p_from_str ("VAR_B"));
78 /* set VAR_A and VAR_B to constant values */
79 set_store (new_Proj (new_Store (get_store (), a,
80 new_Const (mode_I, tarval_from_long (mode_i, 0))),
83 set_store (new_Proj (new_Store (get_store (), b,
84 new_Const (mode_I, tarval_from_long (mode_i, 1))),
87 /* finish this first block */
89 mature_block (irg->current_block);
95 /* exchange the content of the two variables. Exceptions not cached. */
96 /* load the value and make it's effects visible. */
97 x = new_Load (get_store (), a);
98 set_store (new_Proj (x, mode_M, 0));
99 x = new_Proj(x, mode_I, 2);
100 /* the same again: load the value and make it's effects visible. */
101 y = new_Load (get_store (), b);
102 set_store (new_Proj (y, mode_M, 0));
103 y = new_Proj(y, mode_I, 2);
104 /* store the exchanged values. */
105 set_store (new_Proj (new_Store (get_store (), a, y), mode_M, 0));
106 set_store (new_Proj (new_Store (get_store (), b, x), mode_M, 0));
108 /* test the condition */
112 new_Const (mode_I, tarval_from_long (mode_i, 0)),
115 /* build the cfg of the loop */
116 add_in_edge (r, new_Proj (x, mode_X, 0));
117 x = new_Proj (x, mode_X, 1);
120 /* generate the block the loop exits to */
124 /* generate the return block and return the content of VAR_A */
127 x = new_Load (get_store (), a);
128 in[0] = new_Proj (x, mode_I, 2);
130 x = new_Return (new_Proj(x, mode_M, 0), 1, in);
133 add_in_edge (irg->end_block, x);
134 mature_block (irg->end_block);
136 /* verify the graph */
139 printf("\nDone building the graph. Dumping it.\n");
140 dump_ir_block_graph (irg);
142 printf("use xvcg to view this graph:\n");
143 printf("/ben/goetz/bin/xvcg GRAPHNAME\n");