*** 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_opt_constant_folding(0);  /* so that the stupid tests are not optimized. */
47   set_opt_cse(1);
48   set_opt_dead_node_elimination (1);
49
50 #define METHODNAME "main"
51 #define NRARGS 0
52 #define NRES 0
53
54   proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
55                               NRARGS, NRES);
56   owner = new_type_class (id_from_str ("IF_WHILE_EXAMPLE", 16));
57   ent = new_entity ((type *)owner, id_from_str ("main", 4), (type *)proc_main);
58
59   /* Generates start and end blocks and nodes and a first, initial block */
60   irg = new_ir_graph (ent, 4);
61
62   /* Generate two constants */
63   set_value (0, new_Const (mode_I, tarval_from_long (mode_i, 0)));
64   set_value (1, new_Const (mode_I, tarval_from_long (mode_i, 1)));
65   mature_block (irg->current_block);
66
67   /* Generate a conditional branch */
68   x = new_Cond (new_Proj(new_Cmp(new_Const (mode_I, tarval_from_long (mode_i, 0)),
69                                  new_Const (mode_I, tarval_from_long (mode_i, 0))),
70                          mode_b, Eq));
71   f = new_Proj (x, mode_X, 0);
72   t = new_Proj (x, mode_X, 1);
73
74   /* generate and fill the then block */
75   r = new_Block ();
76   add_in_edge (r, t);
77   set_value (0, new_Const (mode_I, tarval_from_long (mode_i, 2)));
78   mature_block (r);
79   x = new_Jmp ();
80
81   /* generate the fall through block and add all cfg edges */
82   r = new_Block ();
83   add_in_edge (r, f);
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_Block ();
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_Block ();
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_Block ();
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 (irg->end_block, x);
126   mature_block (irg->end_block);
127
128   printf("Optimizing ...\n");
129   dead_node_elimination(irg);
130
131   /* verify the graph */
132   irg_vrfy(irg);
133
134   /* output the vcg file */
135   printf("Done building the graph.  Dumping it.\n");
136   dump_ir_block_graph (irg);
137   printf("Use xvcg to view this graph:\n");
138   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
139
140   return (0);
141 }