9a70facb9f85569f8d8b4424a9ba98d4f35a3ca8
[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 # include "irdump.h"
10 # include "firm.h"
11
12 /**  This example demonstrates the use of memory edges.
13 ***  This file constructs the ir for the following pseudo-program:
14 ***
15 ***  int VAR_A
16 ***  int VAR_B
17 ***
18 ***  main() {
19 ***
20 ***    VAR_A = 0
21 ***    VAR_B = 1
22 ***
23 ***    repeat {
24 ***      h = VAR_A;
25 ***      VAR_A = VAR_B;
26 ***      VAR_B = h;
27 ***    } until (0 == h)
28 ***
29 ***    return (VAR_A)
30 ***  }
31 ***
32 ***
33 ***  A better example would be the following program:
34 ***  (name e.g.: memory-imp_example.c as it models imperative concepts.)
35 ***
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.
40 ***
41 ***  main () {
42 ***    int a;
43 ***    int *p;
44 ***
45 ***    a = 2;
46 ***    p = &a;
47 ***    return (*p);
48 ***  }
49 ***
50 **/
51
52 int
53 main(void)
54 {
55   ir_graph *irg;
56   type_class *owner;
57   type_method *method;    /* the type of this method */
58   entity *ent;
59   ir_node *a, *b, *x, *y, *r;
60
61   printf("\nCreating an IR graph: MEMORY_EXAMPLE...\n");
62
63   init_firm ();
64
65   set_opt_dead_node_elimination (1);
66
67   /* a class to get started with, containing the main procedure */
68   owner = new_type_class (id_from_str ("MEMORY_EXAMPLE", 14));
69   method = new_type_method (id_from_str("main", 4), 0, 2);
70   ent = new_entity ((type *)owner, id_from_str ("main", 4), (type *)method);
71
72   /* Generates start and end blocks and nodes and a first, initial block */
73   irg = new_ir_graph (ent, 4);
74
75   /* generate two constant pointers to string constants */
76   /* this simulates two global variables, a and b point to these variables */
77   a = new_Const (mode_p, tarval_p_from_str ("VAR_A"));
78   b = new_Const (mode_p, tarval_p_from_str ("VAR_B"));
79
80   /* set VAR_A and VAR_B to constant values */
81   set_store (new_Proj (new_Store (get_store (), a,
82                                   new_Const (mode_I, tarval_from_long (mode_i, 0))),
83                        mode_M, 0));
84
85   set_store (new_Proj (new_Store (get_store (), b,
86                                   new_Const (mode_I, tarval_from_long (mode_i, 1))),
87                        mode_M, 0));
88
89   /* finish this first block */
90   x = new_Jmp ();
91   mature_block (get_irg_current_block(irg));
92
93   /* a loop body */
94   r = new_immBlock ();
95   add_in_edge (r, x);
96
97   /* exchange the content of the two variables. Exceptions not cached. */
98   /* load the value and make it's effects visible. */
99   x = new_Load (get_store (), a);
100     set_store (new_Proj (x, mode_M, 0));
101     x = new_Proj(x, mode_I, 2);
102   /* the same again: load the value and make it's effects visible. */
103   y = new_Load (get_store (), b);
104     set_store (new_Proj (y, mode_M, 0));
105     y = new_Proj(y, mode_I, 2);
106   /* store the exchanged values. */
107   set_store (new_Proj (new_Store (get_store (), a, y), mode_M, 0));
108   set_store (new_Proj (new_Store (get_store (), b, x), mode_M, 0));
109
110   /* test the condition */
111   x = new_Cond (
112         new_Proj (
113           new_Cmp (
114             new_Const (mode_I, tarval_from_long (mode_i, 0)),
115             x),
116           mode_b, Gt));
117
118   /* build the cfg of the loop */
119   add_in_edge (r, new_Proj (x, mode_X, 0));
120   x = new_Proj (x, mode_X, 1);
121   mature_block(r);
122
123   /* generate the block the loop exits to */
124   r = new_immBlock ();
125   add_in_edge (r, x);
126
127   /* generate the return block and return the content of VAR_A */
128   {
129      ir_node *in[1];
130      x = new_Load (get_store (), a);
131      in[0] = new_Proj (x, mode_I, 2);
132
133      x = new_Return (new_Proj(x, mode_M, 0), 1, in);
134   }
135   mature_block (r);
136   add_in_edge (get_irg_end_block(irg), x);
137   mature_block (get_irg_end_block(irg));
138
139   printf("Optimizing ...\n");
140   dead_node_elimination(irg);
141
142   /* verify the graph */
143   irg_vrfy(irg);
144
145   printf("Done building the graph.  Dumping it.\n");
146   dump_ir_block_graph (irg);
147   printf("Use xvcg to view this graph:\n");
148   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
149
150   return (0);
151 }