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