d7173f62f206097ee336f9e2cab19a432961fdf2
[libfirm] / while_example.c
1 /* Copyright (C) 2001 by Universitaet Karlsruhe
2 ** All rights reserved.
3 **
4 ** Authors: 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(int a) {        // pos 0
16 ***    int b = 1;         // pos 1
17 ***    int h;             // pos 2
18 ***
19 ***    while (0 == 2) loop {
20 ***      h = a;
21 ***      a = b;
22 ***      b = h;
23 ***    }
24 ***
25 ***    return a-b;
26 ***  }
27 **/
28
29 int
30 main(void)
31 {
32   type *prim_t_int;
33   ir_graph *irg;
34   type *owner;
35   type *proc_main;
36   entity *ent;
37   ir_node *b, *x, *r, *t, *f;
38
39   printf("\nCreating an IR graph: WHILE_EXAMPLE...\n");
40
41   init_firm ();
42
43   set_optimize(1);
44   set_opt_constant_folding(1);
45   set_opt_cse(1);
46   set_opt_dead_node_elimination (1);
47
48   prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_i);
49
50 #define METHODNAME "main"
51 #define NRARGS 1
52 #define NRES 0
53
54   proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
55                               NRARGS, NRES);
56   set_method_param_type(proc_main, 0, prim_t_int);
57
58   owner = new_type_class (id_from_str ("WHILE_EXAMPLE", 16));
59   ent = new_entity (owner, id_from_str ("main", 4), 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_Proj(get_irg_args(irg), mode_I, 0));
66   set_value (1, new_Const (mode_I, tarval_from_long (mode_I, 1)));
67   x = new_Jmp();
68   mature_block (get_irg_current_block(irg));
69
70   /* generate a block for the loop header and the conditional branch */
71   r = new_immBlock ();
72   add_in_edge (r, x);
73   x = new_Cond (new_Proj(new_Cmp(new_Const (mode_I, tarval_from_long (mode_i, 0)),
74                                  new_Const (mode_I, tarval_from_long (mode_i, 0))),
75                          mode_b, Eq));
76   f = new_Proj (x, mode_X, 0);
77   t = new_Proj (x, mode_X, 1);
78
79   /* generate the block for the loop body */
80   b = new_immBlock ();
81   add_in_edge (b, t);
82   x = new_Jmp ();
83   add_in_edge (r, x);
84
85   /* The code in the loop body,
86      as we are dealing with local variables only the dataflow edges
87      are manipulated. */
88   set_value (2, get_value (0, mode_I));
89   set_value (0, get_value (1, mode_I));
90   set_value (1, get_value (2, mode_I));
91   mature_block (b);
92   mature_block (r);
93
94   /* generate the return block */
95   r = new_immBlock ();
96   add_in_edge (r, f);
97   mature_block (r);
98
99   {
100      ir_node *in[1];
101      in[0] = new_Sub (get_value (0, mode_I), get_value (1, mode_I), mode_I);
102
103      x = new_Return (get_store (), 1, in);
104   }
105
106   /* finalize the end block generated in new_ir_graph() */
107   add_in_edge (get_irg_end_block(irg), x);
108   mature_block (get_irg_end_block(irg));
109
110   printf("Optimizing ...\n");
111
112   local_optimize_graph(irg),
113   dead_node_elimination(irg);
114
115   /* verify the graph */
116   irg_vrfy(irg);
117
118   /* output the vcg file */
119   printf("Done building the graph.  Dumping it.\n");
120   dump_ir_block_graph (irg);
121   printf("Use xvcg to view this graph:\n");
122   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
123
124   return (0);
125 }