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; /* the type of this method */
60 ir_node *a, *b, *x, *y, *r;
62 printf("\nCreating an IR graph: MEMORY_EXAMPLE...\n");
66 set_opt_dead_node_elimination (1);
68 /*** Make basic type information for primitive type int. ***/
69 prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_I);
71 /* a class to get started with, containing the main procedure */
72 owner = new_type_class (id_from_str ("MEMORY_EXAMPLE", 14));
73 method = new_type_method (id_from_str("main", 4), 0, 1);
74 set_method_res_type(method, 0, prim_t_int);
75 ent = new_entity (owner, id_from_str ("main", 4), method);
77 /* Generates start and end blocks and nodes and a first, initial block */
78 irg = new_ir_graph (ent, 4);
80 /* generate two constant pointers to string constants */
81 /* this simulates two global variables, a and b point to these variables */
82 a = new_Const (mode_p, tarval_p_from_str ("VAR_A"));
83 b = new_Const (mode_p, tarval_p_from_str ("VAR_B"));
85 /* set VAR_A and VAR_B to constant values */
86 set_store (new_Proj (new_Store (get_store (), a,
87 new_Const (mode_I, tarval_from_long (mode_i, 0))),
90 set_store (new_Proj (new_Store (get_store (), b,
91 new_Const (mode_I, tarval_from_long (mode_i, 1))),
94 /* finish this first block */
96 mature_block (get_irg_current_block(irg));
102 /* exchange the content of the two variables. Exceptions not cached. */
103 /* load the value and make it's effects visible. */
104 x = new_Load (get_store (), a);
105 set_store (new_Proj (x, mode_M, 0));
106 x = new_Proj(x, mode_I, 2);
107 /* the same again: load the value and make it's effects visible. */
108 y = new_Load (get_store (), b);
109 set_store (new_Proj (y, mode_M, 0));
110 y = new_Proj(y, mode_I, 2);
111 /* store the exchanged values. */
112 set_store (new_Proj (new_Store (get_store (), a, y), mode_M, 0));
113 set_store (new_Proj (new_Store (get_store (), b, x), mode_M, 0));
115 /* test the condition */
119 new_Const (mode_I, tarval_from_long (mode_i, 0)),
123 /* build the cfg of the loop */
124 add_in_edge (r, new_Proj (x, mode_X, 0));
125 x = new_Proj (x, mode_X, 1);
128 /* generate the block the loop exits to */
132 /* generate the return block and return the content of VAR_A */
135 x = new_Load (get_store (), a);
136 in[0] = new_Proj (x, mode_I, 2);
138 x = new_Return (new_Proj(x, mode_M, 0), 1, in);
141 add_in_edge (get_irg_end_block(irg), x);
142 mature_block (get_irg_end_block(irg));
144 printf("Optimizing ...\n");
145 dead_node_elimination(irg);
147 /* verify the graph */
150 printf("Done building the graph. Dumping it.\n");
151 dump_ir_block_graph (irg);
152 printf("Use xvcg to view this graph:\n");
153 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");