00497660efc8b9f82cbe63654c145664fbd6555a
[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 ();
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
82   /* Generates start and end blocks and nodes and a first, initial block */
83   irg = new_ir_graph (ent, 4);
84
85   /* generate two constant pointers to string constants */
86   /* this simulates two global variables, a and b point to these variables */
87   a = new_Const (mode_P, tarval_P_from_str ("VAR_A"));
88   b = new_Const (mode_P, tarval_P_from_str ("VAR_B"));
89
90   /* set VAR_A and VAR_B to constant values */
91   set_store (new_Proj (new_Store (get_store (), a,
92                                   new_Const (mode_Iu, tarval_from_long (mode_Is, 0))),
93                        mode_M, 0));
94
95   set_store (new_Proj (new_Store (get_store (), b,
96                                   new_Const (mode_Iu, tarval_from_long (mode_Is, 1))),
97                        mode_M, 0));
98
99   /* finish this first block */
100   x = new_Jmp ();
101   mature_block (get_irg_current_block(irg));
102
103   /* a loop body */
104   r = new_immBlock ();
105   add_in_edge (r, x);
106
107   /* exchange the content of the two variables. Exceptions not cached. */
108   /* load the value and make it's effects visible. */
109   x = new_Load (get_store (), a);
110     set_store (new_Proj (x, mode_M, 0));
111     x = new_Proj(x, mode_Iu, 2);
112   /* the same again: load the value and make it's effects visible. */
113   y = new_Load (get_store (), b);
114     set_store (new_Proj (y, mode_M, 0));
115     y = new_Proj(y, mode_Iu, 2);
116   /* store the exchanged values. */
117   set_store (new_Proj (new_Store (get_store (), a, y), mode_M, 0));
118   set_store (new_Proj (new_Store (get_store (), b, x), mode_M, 0));
119
120   /* test the condition */
121   x = new_Cond (
122         new_Proj (
123           new_Cmp (
124             new_Const (mode_Iu, tarval_from_long (mode_Is, 0)),
125             x),
126           mode_b, Gt));
127
128   /* build the cfg of the loop */
129   add_in_edge (r, new_Proj (x, mode_X, 0));
130   x = new_Proj (x, mode_X, 1);
131   mature_block(r);
132
133   /* generate the block the loop exits to */
134   r = new_immBlock ();
135   add_in_edge (r, x);
136
137   /* generate the return block and return the content of VAR_A */
138   {
139      ir_node *in[1];
140      x = new_Load (get_store (), a);
141      in[0] = new_Proj (x, mode_Iu, 2);
142
143      x = new_Return (new_Proj(x, mode_M, 0), 1, in);
144   }
145   mature_block (r);
146   add_in_edge (get_irg_end_block(irg), x);
147   mature_block (get_irg_end_block(irg));
148
149   finalize_cons (irg);
150
151   printf("Optimizing ...\n");
152   dead_node_elimination(irg);
153
154   /* verify the graph */
155   irg_vrfy(irg);
156
157   printf("Done building the graph.  Dumping it.\n");
158   dump_ir_block_graph (irg);
159   printf("Use xvcg to view this graph:\n");
160   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
161
162   return (0);
163 }