*** empty log message ***
[libfirm] / testprograms / if_while_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 # 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_class *owner;
38   type_method *proc_main;
39   entity *ent;
40   ir_node *b, *x, *r, *t, *f;
41
42   printf("\nCreating an IR graph: IF_WHILE_EXAMPLE...\n");
43
44   init_firm ();
45
46   set_optimize(1);
47   set_opt_constant_folding(0);  /* so that the stupid tests are not optimized. */
48                                 /* if 1 aborts ????? @@@ */
49   set_opt_cse(1);
50   set_opt_dead_node_elimination (1);
51
52 #define METHODNAME "main"
53 #define NRARGS 0
54 #define NRES 0
55
56   proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
57                               NRARGS, NRES);
58   owner = new_type_class (id_from_str ("IF_WHILE_EXAMPLE", 16));
59   ent = new_entity ((type *)owner, id_from_str ("main", 4), (type *)proc_main);
60
61   /* Generates start and end blocks and nodes and a first, initial block */
62   irg = new_ir_graph (ent, 4);
63
64   /* Generate two constants */
65   set_value (0, new_Const (mode_I, tarval_from_long (mode_i, 0)));
66   set_value (1, new_Const (mode_I, tarval_from_long (mode_i, 1)));
67   mature_block (get_irg_current_block(irg));
68
69   /* Generate a conditional branch */
70   x = new_Cond (new_Proj(new_Cmp(new_Const (mode_I, tarval_from_long (mode_i, 0)),
71                                  new_Const (mode_I, tarval_from_long (mode_i, 0))),
72                          mode_b, Eq));
73   f = new_Proj (x, mode_X, 0);
74   t = new_Proj (x, mode_X, 1);
75
76   /* generate and fill the then block */
77   r = new_immBlock ();
78   add_in_edge (r, t);
79   set_value (0, new_Const (mode_I, tarval_from_long (mode_i, 2)));
80   mature_block (r);
81   x = new_Jmp ();
82
83   /* generate the fall through block and add all cfg edges */
84   r = new_immBlock ();
85   add_in_edge (r, f);
86   add_in_edge (r, x);
87   mature_block (r);
88   x = new_Jmp ();
89
90   /* generate a block for the loop header and the conditional branch */
91   r = new_immBlock ();
92   add_in_edge (r, x);
93   x = new_Cond (new_Proj(new_Cmp(new_Const (mode_I, tarval_from_long (mode_i, 0)),
94                                  new_Const (mode_I, tarval_from_long (mode_i, 0))),
95                          mode_b, Eq));
96   f = new_Proj (x, mode_X, 0);
97   t = new_Proj (x, mode_X, 1);
98
99   /* generate the block for the loop body */
100   b = new_immBlock ();
101   add_in_edge (b,t);
102   x = new_Jmp ();
103   add_in_edge (r, x);
104   mature_block (r);
105
106   /* the code in the loop body,
107      as we are dealing with local variables only the dataflow edges
108      are manipulated */
109   set_value (2, get_value (0, mode_I));
110   set_value (0, get_value (1, mode_I));
111   set_value (1, get_value (2, mode_I));
112   mature_block (b);
113
114   /* generate the return block */
115   r = new_immBlock ();
116   add_in_edge (r, f);
117   mature_block (r);
118
119   {
120      ir_node *in[1];
121      in[0] = new_Sub (get_value (0, mode_I), get_value (1, mode_I), mode_I);
122
123      x = new_Return (get_store (), 1, in);
124   }
125
126   /* finalize the end block generated in new_ir_graph() */
127   add_in_edge (get_irg_end_block(irg), x);
128   mature_block (get_irg_end_block(irg));
129
130   printf("Optimizing ...\n");
131
132   local_optimize_graph(irg),
133   dead_node_elimination(irg);
134
135   /* verify the graph */
136   irg_vrfy(irg);
137
138   /* output the vcg file */
139   printf("Done building the graph.  Dumping it.\n");
140   dump_ir_block_graph (irg);
141   printf("Use xvcg to view this graph:\n");
142   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
143
144   return (0);
145 }