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