Make reference for yesterdays changes: SymConst to start block
[libfirm] / testprograms / memory_example.c
1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 * All rights reserved.
3 *
4 * Authors: Christian Schaefer, Goetz Lindenmaier
5 *
6 * testprogram.
7 */
8
9
10 # include <stdio.h>
11 # include <string.h>
12
13 # include "irvrfy.h"
14 # include "irdump.h"
15 # include "firm.h"
16
17 /**
18 *  This file constructs the ir for the following pseudo-program:
19 *
20 *  int VAR_A
21 *  int VAR_B
22 *
23 *  main() {
24 *
25 *    VAR_A = 0
26 *    VAR_B = 1
27 *
28 *    repeat {
29 *      h = VAR_A;
30 *      VAR_A = VAR_B;
31 *      VAR_B = h;
32 *    } until (0 == h)
33 *
34 *    return (VAR_A)
35 *  }
36 *
37 *
38 *  A better example would be the following program:
39 *  (name e.g.: memory-imp_example.c as it models imperative concepts.)
40 *
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.
45 *
46 *  main () {
47 *    int a;
48 *    int *p;
49 *
50 *    a = 2;
51 *    p = &a;
52 *    return (*p);
53 *  }
54 *
55 **/
56
57 int
58 main(void)
59 {
60   ir_graph *irg;
61   type     *owner;
62   type     *method;    /* the type of this method */
63   type     *prim_t_int;
64   entity   *ent;
65   ir_node  *a, *b, *x, *y, *r;
66
67   printf("\nCreating an IR graph: MEMORY_EXAMPLE...\n");
68
69   init_firm (NULL);
70
71   set_opt_dead_node_elimination (1);
72
73   /*** Make basic type information for primitive type int. ***/
74   prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_Iu);
75
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 */
82
83   /* Generates start and end blocks and nodes and a first, initial block */
84   irg = new_ir_graph (ent, 4);
85
86   /* create two global variables, a and b point to these variables */
87   a = new_simpleSel(
88               get_store(),
89               get_irg_globals(irg),
90               new_entity(get_glob_type(),id_from_str("VAR_A",6),prim_t_int));
91   b = new_simpleSel(
92               get_store(),
93               get_irg_globals(irg),
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))),
98                        mode_M, 0));
99
100   set_store (new_Proj (new_Store (get_store (), b,
101                                   new_Const (mode_Iu, new_tarval_from_long (1, mode_Is))),
102                        mode_M, 0));
103
104   /* finish this first block */
105   x = new_Jmp ();
106   mature_block (get_irg_current_block(irg));
107
108   /* a loop body */
109   r = new_immBlock ();
110   add_in_edge (r, x);
111
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));
124
125   /* test the condition */
126   x = new_Cond (
127         new_Proj (
128           new_Cmp (
129             new_Const (mode_Iu, new_tarval_from_long (0, mode_Is)),
130             x),
131           mode_b, Gt));
132
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);
136   mature_block(r);
137
138   /* generate the block the loop exits to */
139   r = new_immBlock ();
140   add_in_edge (r, x);
141
142   /* generate the return block and return the content of VAR_A */
143   {
144      ir_node *in[1];
145      x = new_Load (get_store (), a);
146      in[0] = new_Proj (x, mode_Iu, 2);
147
148      x = new_Return (new_Proj(x, mode_M, 0), 1, in);
149   }
150   mature_block (r);
151   add_in_edge (get_irg_end_block(irg), x);
152   mature_block (get_irg_end_block(irg));
153
154   finalize_cons (irg);
155
156   printf("Optimizing ...\n");
157   dead_node_elimination(irg);
158
159   /* verify the graph */
160   irg_vrfy(irg);
161
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");
166
167   return (0);
168 }