Small simplification.
[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
18
19 #include <libfirm/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 ir_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   ir_type     *owner;
66   ir_type     *method;    /* the ir_type of this method */
67   ir_type     *prim_t_int;
68   ir_entity   *ent;
69   ir_node  *a, *b, *x, *y, *r;
70   union symconst_symbol symbol;
71
72   printf("\nCreating an IR graph: MEMORY_EXAMPLE...\n");
73
74   init_firm (NULL);
75
76   /*** Make basic ir_type information for primitive ir_type int. ***/
77   prim_t_int = new_type_primitive(new_id_from_chars ("int", 3), mode_Iu);
78
79   /* a class to get started with, containing the main procedure */
80   owner = new_type_class (new_id_from_chars ("MEMORY_EXAMPLE", 14));
81   method = new_type_method (new_id_from_chars("main", 4), 0, 1);
82   set_method_res_type(method, 0, prim_t_int);
83   ent = new_entity (owner, new_id_from_chars ("main", 4), method);
84   get_entity_ld_name(ent); /* To enforce name mangling for vcg graph name */
85
86   /* Generates start and end blocks and nodes and a first, initial block */
87   irg = new_ir_graph (ent, 4);
88
89   /* create two global variables, a and b point to these variables */
90   symbol.entity_p = new_entity(get_glob_type(),new_id_from_chars("VAR_A",6),prim_t_int);
91   a = new_SymConst(mode_P, symbol, symconst_addr_ent);
92
93   symbol.entity_p = new_entity(get_glob_type(),new_id_from_chars("VAR_B",6),prim_t_int);
94   b = new_SymConst(mode_P, symbol, symconst_addr_ent);
95    /* set VAR_A and VAR_B to constant values */
96   set_store (new_Proj (new_Store (get_store (), a,
97                                   new_Const (mode_Iu, new_tarval_from_long (0, mode_Iu))),
98                        mode_M, pn_Store_M));
99
100   set_store (new_Proj (new_Store (get_store (), b,
101                                   new_Const (mode_Iu, new_tarval_from_long (1, mode_Iu))),
102                        mode_M, pn_Store_M));
103
104   /* finish this first block */
105   x = new_Jmp ();
106   mature_immBlock (get_irg_current_block(irg));
107
108   /* a loop body */
109   r = new_immBlock ();
110   add_immBlock_pred (r, x);
111
112   /* exchange the content of the two variables. Exceptions not cached. */
113   /* load the value and make it's effects visible. */
114   x = new_Load (get_store (), a, mode_Iu);
115     set_store (new_Proj (x, mode_M, pn_Load_M));
116     x = new_Proj(x, mode_Iu, pn_Load_res);
117   /* the same again: load the value and make it's effects visible. */
118   y = new_Load (get_store (), b, mode_Iu);
119     set_store (new_Proj (y, mode_M, pn_Load_M));
120     y = new_Proj(y, mode_Iu, pn_Load_res);
121   /* store the exchanged values. */
122   set_store (new_Proj (new_Store (get_store (), a, y), mode_M, pn_Store_M));
123   set_store (new_Proj (new_Store (get_store (), b, x), mode_M, pn_Store_M));
124
125   /* test the condition */
126   x = new_Cond (
127         new_Proj (
128           new_Cmp (
129             new_Const (mode_Iu, new_tarval_from_long (0, mode_Iu)),
130             x),
131           mode_b, pn_Cmp_Gt));
132
133   /* build the cfg of the loop */
134   add_immBlock_pred (r, new_Proj (x, mode_X, pn_Cond_false));
135   x = new_Proj (x, mode_X, pn_Cond_true);
136   mature_immBlock(r);
137
138   /* generate the block the loop exits to */
139   r = new_immBlock ();
140   add_immBlock_pred (r, x);
141
142   /* generate the return block and return the content of VAR_A */
143   {
144      ir_node *in[1];
145      x = new_Load (get_store (), a, mode_Iu);
146      in[0] = new_Proj (x, mode_Iu, pn_Load_res);
147
148      x = new_Return (new_Proj(x, mode_M, pn_Load_M), 1, in);
149   }
150   mature_immBlock (r);
151   add_immBlock_pred (get_irg_end_block(irg), x);
152   mature_immBlock (get_irg_end_block(irg));
153
154   irg_finalize_cons (irg);
155
156   printf("Optimizing ...\n");
157   dead_node_elimination(irg);
158
159   /* verify the graph */
160   irg_vrfy(irg);
161
162   printf("Done building the graph.  Dumping it.\n");
163   dump_ir_block_graph (irg, 0);
164   printf("Use ycomp to view this graph:\n");
165   printf("ycomp GRAPHNAME\n\n");
166
167   return (0);
168 }