Added walker for const code
[libfirm] / testprograms / if_while_example.c
1 /* (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
3 **
4 ** Authors: Christian Schaefer, Goetz Lindenmaier
5 **
6 ** testprogram.
7 */
8
9 # include <stdio.h>
10 # include <string.h>
11
12 # include "irvrfy.h"
13 # include "irdump.h"
14 # include "firm.h"
15
16 /**
17 ***  This file constructs the ir for the following pseudo-program:
18 ***
19 ***  main() {
20 ***    int a = 0;         // pos 0
21 ***    int b = 1;         // pos 1
22 ***    int h;             // pos 2
23 ***
24 ***    if (0 == 0)
25 ***      { a = 2; }
26 ***
27 ***    while (0 == 0) loop {
28 ***      h = a;
29 ***      a = b;
30 ***      b = h;
31 ***    }
32 ***
33 ***    return a-b;
34 ***  }
35 **/
36
37 int
38 main(void)
39 {
40   ir_graph *irg;
41   type *owner;
42   type *proc_main;
43   type     *prim_t_int;
44   entity *ent;
45   ir_node *b, *x, *r, *t, *f;
46
47   printf("\nCreating an IR graph: IF_WHILE_EXAMPLE...\n");
48
49   init_firm ();
50   turn_off_edge_labels();
51
52   set_optimize(1);
53   set_opt_constant_folding(0);  /* so that the stupid tests are not optimized. */
54                                 /* if optimized no path to End remains!! */
55   set_opt_cse(1);
56   set_opt_dead_node_elimination (1);
57
58   /*** Make basic type information for primitive type int. ***/
59   prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_I);
60
61 #define METHODNAME "main"
62 #define NRARGS 0
63 #define NRES 1
64
65   proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
66                               NRARGS, NRES);
67   set_method_res_type(proc_main, 0, prim_t_int);
68   owner = new_type_class (id_from_str ("IF_WHILE_EXAMPLE", 16));
69   ent = new_entity (owner, id_from_str ("main", 4), proc_main);
70
71   /* Generates start and end blocks and nodes and a first, initial block */
72   irg = new_ir_graph (ent, 4);
73
74   /* Generate two constants */
75   set_value (0, new_Const (mode_I, tarval_from_long (mode_I, 0)));
76   set_value (1, new_Const (mode_I, tarval_from_long (mode_I, 1)));
77   mature_block (get_irg_current_block(irg));
78
79   /* Generate a conditional branch */
80   x = new_Jmp();
81
82   /* generate the fall through block and add all cfg edges */
83   r = new_immBlock ();
84   add_in_edge (r, x);
85   mature_block (r);
86   x = new_Jmp ();
87
88   /* generate a block for the loop header and the conditional branch */
89   r = new_immBlock ();
90   add_in_edge (r, x);
91   x = new_Cond (new_Proj(new_Cmp(new_Const (mode_I, tarval_from_long (mode_i, 0)),
92                                  new_Const (mode_I, tarval_from_long (mode_i, 0))),
93                          mode_b, Eq));
94   f = new_Proj (x, mode_X, 0);
95   t = new_Proj (x, mode_X, 1);
96
97   /* generate the block for the loop body */
98   b = new_immBlock ();
99   add_in_edge (b,t);
100   x = new_Jmp ();
101   add_in_edge (r, x);
102   mature_block (r);
103
104   /* the code in the loop body,
105      as we are dealing with local variables only the dataflow edges
106      are manipulated */
107   set_value (2, get_value (0, mode_I));
108   set_value (0, get_value (1, mode_I));
109   set_value (1, get_value (2, mode_I));
110   mature_block (b);
111
112   /* generate the return block */
113   r = new_immBlock ();
114   add_in_edge (r, f);
115   mature_block (r);
116
117   {
118      ir_node *in[1];
119      in[0] = new_Sub (get_value (0, mode_I), get_value (1, mode_I), mode_I);
120
121      x = new_Return (get_store (), 1, in);
122   }
123
124   /* finalize the end block generated in new_ir_graph() */
125   add_in_edge (get_irg_end_block(irg), x);
126   mature_block (get_irg_end_block(irg));
127
128   finalize_cons (irg);
129
130   printf("Optimizing ...\n");
131
132   local_optimize_graph(irg);
133   dead_node_elimination(irg);
134
135   compute_outs(irg);
136
137   /* verify the graph */
138   irg_vrfy(irg);
139
140   /* output the vcg file */
141   printf("Done building the graph.  Dumping it with out-edges.\n");
142   dump_out_edges();
143   dump_ir_graph (irg);
144   printf("Use xvcg to view this graph:\n");
145   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
146
147   return (0);
148 }