Initial revision
[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;
17 ***    int b = 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   entity *ent;
37   ir_node *b, *x, *r, *t, *f;
38
39   printf("creating an IR graph: IF_WHILE_EXAMPLE...\n");
40
41   init_firm ();
42
43   owner = new_type_class (id_from_str ("IF_WHILE_EXAMPLE", 16));
44   ent = new_entity ((type *)owner, id_from_str ("main", 4), NULL);
45
46   /* Generates start and end blocks and nodes and a first, initial block */
47   irg = new_ir_graph (ent, 4);
48
49   /* Generate two constants */
50   set_value (0, new_Const (mode_I, tarval_from_long (mode_i, 0)));
51   set_value (1, new_Const (mode_I, tarval_from_long (mode_i, 1)));
52   mature_block (irg->current_block);
53
54   /* Generate a conditional branch */
55   x = new_Cond (new_Proj(new_Cmp(new_Const (mode_I, tarval_from_long (mode_i, 0)),
56                                  new_Const (mode_I, tarval_from_long (mode_i, 0))),
57                          mode_b, Eq));
58   f = new_Proj (x, mode_X, 0);
59   t = new_Proj (x, mode_X, 1);
60
61   /* generate and fill the then block */
62   r = new_Block ();
63   add_in_edge (r, t);
64   set_value (0, new_Const (mode_I, tarval_from_long (mode_i, 2)));
65   mature_block (r);
66   x = new_Jmp ();
67
68   /* generate the fall through block and add all cfg edges */
69   r = new_Block ();
70   add_in_edge (r, f);
71   add_in_edge (r, x);
72   mature_block (r);
73   x = new_Jmp ();
74
75   /* generate a block for the loop header and the conditional branch */
76   r = new_Block ();
77   add_in_edge (r, x);
78   x = new_Cond (new_Proj(new_Cmp(new_Const (mode_I, tarval_from_long (mode_i, 0)),
79                                  new_Const (mode_I, tarval_from_long (mode_i, 0))),
80                          mode_b, Eq));
81   f = new_Proj (x, mode_X, 0);
82   t = new_Proj (x, mode_X, 1);
83
84   /* generate the block for the loop body */
85   b = new_Block ();
86   add_in_edge (b,t);
87   x = new_Jmp ();
88   add_in_edge (r, x);
89   mature_block (r);
90
91   /* the code in the loop body,
92      as we are dealing with local variable only the dataflow edges are manipulated */
93   set_value (2, get_value (0, mode_I));
94   set_value (0, get_value (1, mode_I));
95   set_value (1, get_value (2, mode_I));
96   mature_block (b);
97
98   /* generate the return block */
99   r = new_Block ();
100   add_in_edge (r, f);
101   mature_block (r);
102
103   {
104      ir_node *in[1];
105      in[0] = new_Sub (get_value (0, mode_I), get_value (1, mode_I), mode_I);
106
107      x = new_Return (get_store (), 1, in);
108   }
109
110   /* finalize the end block generated in new_ir_graph() */
111   add_in_edge (irg->end_block, x);
112   mature_block (irg->end_block);
113
114   /* output the vcg file */
115   printf("\nDone building the graph.  Dumping it.\n");
116   dump_ir_block_graph (irg);
117
118   printf("use xvcg to view this graph:\n");
119   printf("/ben/trapp/bin/i486/xvcg GRAPHNAME\n");
120
121   return (0);
122 }