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