New
[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     *owner;
57   type     *method;    /* the type of this method */
58   type     *prim_t_int;
59   entity   *ent;
60   ir_node  *a, *b, *x, *y, *r;
61
62   printf("\nCreating an IR graph: MEMORY_EXAMPLE...\n");
63
64   init_firm ();
65
66   set_opt_dead_node_elimination (1);
67
68   /*** Make basic type information for primitive type int. ***/
69   prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_I);
70
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);
76
77   /* Generates start and end blocks and nodes and a first, initial block */
78   irg = new_ir_graph (ent, 4);
79
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"));
84
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))),
88                        mode_M, 0));
89
90   set_store (new_Proj (new_Store (get_store (), b,
91                                   new_Const (mode_I, tarval_from_long (mode_i, 1))),
92                        mode_M, 0));
93
94   /* finish this first block */
95   x = new_Jmp ();
96   mature_block (get_irg_current_block(irg));
97
98   /* a loop body */
99   r = new_immBlock ();
100   add_in_edge (r, x);
101
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));
114
115   /* test the condition */
116   x = new_Cond (
117         new_Proj (
118           new_Cmp (
119             new_Const (mode_I, tarval_from_long (mode_i, 0)),
120             x),
121           mode_b, Gt));
122
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);
126   mature_block(r);
127
128   /* generate the block the loop exits to */
129   r = new_immBlock ();
130   add_in_edge (r, x);
131
132   /* generate the return block and return the content of VAR_A */
133   {
134      ir_node *in[1];
135      x = new_Load (get_store (), a);
136      in[0] = new_Proj (x, mode_I, 2);
137
138      x = new_Return (new_Proj(x, mode_M, 0), 1, in);
139   }
140   mature_block (r);
141   add_in_edge (get_irg_end_block(irg), x);
142   mature_block (get_irg_end_block(irg));
143
144   finalize_cons (irg);
145
146   printf("Optimizing ...\n");
147   dead_node_elimination(irg);
148
149   /* verify the graph */
150   irg_vrfy(irg);
151
152   printf("Done building the graph.  Dumping it.\n");
153   dump_ir_block_graph (irg);
154   printf("Use xvcg to view this graph:\n");
155   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
156
157   return (0);
158 }