Fixed construction of constants (new verifyer detects this)
[libfirm] / testprograms / memory_example.c
1 /*
2  * Project:     libFIRM
3  * File name:   testprograms/memory_example.c
4  * Purpose:     Illustrate memory edges.
5  * Author:      Christian Schaefer, Goetz Lindenmaier
6  * Modified by:
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1999-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13
14 # include <stdio.h>
15 # include <string.h>
16
17 # include "irvrfy.h"
18 # include "irdump.h"
19 # include "firm.h"
20
21 /**
22 *  This file constructs the ir for the following pseudo-program:
23 *
24 *  int VAR_A
25 *  int VAR_B
26 *
27 *  main() {
28 *
29 *    VAR_A = 0
30 *    VAR_B = 1
31 *
32 *    repeat {
33 *      h = VAR_A;
34 *      VAR_A = VAR_B;
35 *      VAR_B = h;
36 *    } until (0 == h)
37 *
38 *    return (VAR_A)
39 *  }
40 *
41 *
42 *  A better example would be the following program:
43 *  (name e.g.: memory-imp_example.c as it models imperative concepts.)
44 *
45 *  In this program a local variable is dereferenced.  It has
46 *  to be modeled as an entity of the stack so that a pointer to it is available.
47 *  It is also an example where an analysis could find out that the
48 *  pointer is never actually used.
49 *
50 *  main () {
51 *    int a;
52 *    int *p;
53 *
54 *    a = 2;
55 *    p = &a;
56 *    return (*p);
57 *  }
58 *
59 **/
60
61 int
62 main(void)
63 {
64   ir_graph *irg;
65   type     *owner;
66   type     *method;    /* the type of this method */
67   type     *prim_t_int;
68   entity   *ent;
69   ir_node  *a, *b, *x, *y, *r;
70
71   printf("\nCreating an IR graph: MEMORY_EXAMPLE...\n");
72
73   init_firm (NULL);
74
75   set_opt_dead_node_elimination (1);
76
77   /*** Make basic type information for primitive type int. ***/
78   prim_t_int = new_type_primitive(new_id_from_chars ("int", 3), mode_Iu);
79
80   /* a class to get started with, containing the main procedure */
81   owner = new_type_class (new_id_from_chars ("MEMORY_EXAMPLE", 14));
82   method = new_type_method (new_id_from_chars("main", 4), 0, 1);
83   set_method_res_type(method, 0, prim_t_int);
84   ent = new_entity (owner, new_id_from_chars ("main", 4), method);
85   get_entity_ld_name(ent); /* To enforce name mangling for vcg graph name */
86
87   /* Generates start and end blocks and nodes and a first, initial block */
88   irg = new_ir_graph (ent, 4);
89
90   /* create two global variables, a and b point to these variables */
91   a = new_simpleSel(
92               get_store(),
93               get_irg_globals(irg),
94               new_entity(get_glob_type(),new_id_from_chars("VAR_A",6),prim_t_int));
95   b = new_simpleSel(
96               get_store(),
97               get_irg_globals(irg),
98               new_entity(get_glob_type(),new_id_from_chars("VAR_B",6),prim_t_int));
99    /* set VAR_A and VAR_B to constant values */
100   set_store (new_Proj (new_Store (get_store (), a,
101                                   new_Const (mode_Iu, new_tarval_from_long (0, mode_Iu))),
102                        mode_M, 0));
103
104   set_store (new_Proj (new_Store (get_store (), b,
105                                   new_Const (mode_Iu, new_tarval_from_long (1, mode_Iu))),
106                        mode_M, 0));
107
108   /* finish this first block */
109   x = new_Jmp ();
110   mature_immBlock (get_irg_current_block(irg));
111
112   /* a loop body */
113   r = new_immBlock ();
114   add_immBlock_pred (r, x);
115
116   /* exchange the content of the two variables. Exceptions not cached. */
117   /* load the value and make it's effects visible. */
118   x = new_Load (get_store (), a, mode_Iu);
119     set_store (new_Proj (x, mode_M, 0));
120     x = new_Proj(x, mode_Iu, 2);
121   /* the same again: load the value and make it's effects visible. */
122   y = new_Load (get_store (), b, mode_Iu);
123     set_store (new_Proj (y, mode_M, 0));
124     y = new_Proj(y, mode_Iu, 2);
125   /* store the exchanged values. */
126   set_store (new_Proj (new_Store (get_store (), a, y), mode_M, 0));
127   set_store (new_Proj (new_Store (get_store (), b, x), mode_M, 0));
128
129   /* test the condition */
130   x = new_Cond (
131         new_Proj (
132           new_Cmp (
133             new_Const (mode_Iu, new_tarval_from_long (0, mode_Iu)),
134             x),
135           mode_b, pn_Cmp_Gt));
136
137   /* build the cfg of the loop */
138   add_immBlock_pred (r, new_Proj (x, mode_X, 0));
139   x = new_Proj (x, mode_X, 1);
140   mature_immBlock(r);
141
142   /* generate the block the loop exits to */
143   r = new_immBlock ();
144   add_immBlock_pred (r, x);
145
146   /* generate the return block and return the content of VAR_A */
147   {
148      ir_node *in[1];
149      x = new_Load (get_store (), a, mode_Iu);
150      in[0] = new_Proj (x, mode_Iu, 2);
151
152      x = new_Return (new_Proj(x, mode_M, 0), 1, in);
153   }
154   mature_immBlock (r);
155   add_immBlock_pred (get_irg_end_block(irg), x);
156   mature_immBlock (get_irg_end_block(irg));
157
158   irg_finalize_cons (irg);
159
160   printf("Optimizing ...\n");
161   dead_node_elimination(irg);
162
163   /* verify the graph */
164   irg_vrfy(irg);
165
166   printf("Done building the graph.  Dumping it.\n");
167   dump_ir_block_graph (irg, 0);
168   printf("Use xvcg to view this graph:\n");
169   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
170
171   return (0);
172 }