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