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.
19 #include <libfirm/firm.h>
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 ir_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 ir_type *method; /* the ir_type of this method */
69 ir_node *a, *b, *x, *y, *r;
70 union symconst_symbol symbol;
72 printf("\nCreating an IR graph: MEMORY_EXAMPLE...\n");
76 /*** Make basic ir_type information for primitive ir_type int. ***/
77 prim_t_int = new_type_primitive(new_id_from_chars ("int", 3), mode_Iu);
79 /* a class to get started with, containing the main procedure */
80 owner = new_type_class (new_id_from_chars ("MEMORY_EXAMPLE", 14));
81 method = new_type_method (new_id_from_chars("main", 4), 0, 1);
82 set_method_res_type(method, 0, prim_t_int);
83 ent = new_entity (owner, new_id_from_chars ("main", 4), method);
84 get_entity_ld_name(ent); /* To enforce name mangling for vcg graph name */
86 /* Generates start and end blocks and nodes and a first, initial block */
87 irg = new_ir_graph (ent, 4);
89 /* create two global variables, a and b point to these variables */
90 symbol.entity_p = new_entity(get_glob_type(),new_id_from_chars("VAR_A",6),prim_t_int);
91 a = new_SymConst(mode_P, symbol, symconst_addr_ent);
93 symbol.entity_p = new_entity(get_glob_type(),new_id_from_chars("VAR_B",6),prim_t_int);
94 b = new_SymConst(mode_P, symbol, symconst_addr_ent);
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_Iu))),
100 set_store (new_Proj (new_Store (get_store (), b,
101 new_Const (mode_Iu, new_tarval_from_long (1, mode_Iu))),
102 mode_M, pn_Store_M));
104 /* finish this first block */
106 mature_immBlock (get_irg_current_block(irg));
110 add_immBlock_pred (r, x);
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, mode_Iu);
115 set_store (new_Proj (x, mode_M, pn_Load_M));
116 x = new_Proj(x, mode_Iu, pn_Load_res);
117 /* the same again: load the value and make it's effects visible. */
118 y = new_Load (get_store (), b, mode_Iu);
119 set_store (new_Proj (y, mode_M, pn_Load_M));
120 y = new_Proj(y, mode_Iu, pn_Load_res);
121 /* store the exchanged values. */
122 set_store (new_Proj (new_Store (get_store (), a, y), mode_M, pn_Store_M));
123 set_store (new_Proj (new_Store (get_store (), b, x), mode_M, pn_Store_M));
125 /* test the condition */
129 new_Const (mode_Iu, new_tarval_from_long (0, mode_Iu)),
133 /* build the cfg of the loop */
134 add_immBlock_pred (r, new_Proj (x, mode_X, pn_Cond_false));
135 x = new_Proj (x, mode_X, pn_Cond_true);
138 /* generate the block the loop exits to */
140 add_immBlock_pred (r, x);
142 /* generate the return block and return the content of VAR_A */
145 x = new_Load (get_store (), a, mode_Iu);
146 in[0] = new_Proj (x, mode_Iu, pn_Load_res);
148 x = new_Return (new_Proj(x, mode_M, pn_Load_M), 1, in);
151 add_immBlock_pred (get_irg_end_block(irg), x);
152 mature_immBlock (get_irg_end_block(irg));
154 irg_finalize_cons (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, 0);
164 printf("Use ycomp to view this graph:\n");
165 printf("ycomp GRAPHNAME\n\n");