Initial revision
[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   entity *ent;
58   ir_node *a, *b, *x, *y, *r;
59
60   printf("creating an IR graph: MEMORY_EXAMPLE...\n");
61
62   init_firm ();
63
64   /* a class to get started with, containing the main procedure */
65   owner = new_type_class (id_from_str ("MEMORY_EXAMPLE", 14));
66   ent = new_entity ((type *)owner, id_from_str ("main", 4), NULL);
67
68   /* Generates start and end blocks and nodes and a first, initial block */
69   irg = new_ir_graph (ent, 4);
70
71   /* generate two constant pointers to string constants */
72   /* this simulates two global variables, a and b point to these variables */
73   a = new_Const (mode_p, tarval_p_from_str ("VAR_A"));
74   b = new_Const (mode_p, tarval_p_from_str ("VAR_B"));
75
76   /* set VAR_A and VAR_B to constant values */
77   set_store (new_Proj (new_Store (get_store (), a,
78                                   new_Const (mode_I, tarval_from_long (mode_i, 0))),
79                        mode_M, 0));
80
81   set_store (new_Proj (new_Store (get_store (), b,
82                                   new_Const (mode_I, tarval_from_long (mode_i, 1))),
83                        mode_M, 0));
84
85   /* finish this first block */
86   x = new_Jmp ();
87   mature_block (irg->current_block);
88
89   /* a loop body */
90   r = new_Block ();
91   add_in_edge (r, x);
92
93   /* exchange the content of the two variables. Exceptions not cached. */
94   /* load the value and make it's effects visible. */
95   x = new_Load (get_store (), a);
96     set_store (new_Proj (x, mode_M, 0));
97     x = new_Proj(x, mode_I, 2);
98   /* the same again: load the value and make it's effects visible. */
99   y = new_Load (get_store (), b);
100     set_store (new_Proj (y, mode_M, 0));
101     y = new_Proj(y, mode_I, 2);
102   /* store the exchanged values. */
103   set_store (new_Proj (new_Store (get_store (), a, y), mode_M, 0));
104   set_store (new_Proj (new_Store (get_store (), b, x), mode_M, 0));
105
106   /* test the condition */
107   x = new_Cond (
108         new_Proj (
109           new_Cmp (
110             new_Const (mode_I, tarval_from_long (mode_i, 0)),
111             x),
112           mode_b, Gt));
113   /* build the cfg of the loop */
114   add_in_edge (r, new_Proj (x, mode_X, 0));
115   x = new_Proj (x, mode_X, 1);
116   mature_block(r);
117
118   /* generate the block the loop exits to */
119   r = new_Block ();
120   add_in_edge (r, x);
121
122   /* generate the return block and return the content of VAR_A */
123   {
124      ir_node *in[1];
125      x = new_Load (get_store (), a);
126      in[0] = new_Proj (x, mode_I, 2);
127
128      x = new_Return (new_Proj(x, mode_M, 0), 1, in);
129   }
130   mature_block (r);
131   add_in_edge (irg->end_block, x);
132   mature_block (irg->end_block);
133
134   printf("\nDone building the graph.  Dumping it.\n");
135   dump_ir_block_graph (irg);
136
137   printf("use xvcg to view this graph:\n");
138   printf("/ben/trapp/bin/i486/xvcg GRAPHNAME\n");
139
140   return (0);
141 }