New directory: ana for analyses. Adapted configure/makefiles
[libfirm] / testprograms / 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 # include "irnode.h"
12
13 /**
14 ***  This file constructs the ir for the following pseudo-program:
15 ***
16 ***  main(int a) {        // pos 0
17 ***    int b = 1;         // pos 1
18 ***    int h;             // pos 2
19 ***
20 ***    while (0 == 2) loop {
21 ***      h = a;
22 ***      a = b;
23 ***      b = h;
24 ***    }
25 ***
26 ***    return a-b;
27 ***  }
28 **/
29
30 int
31 main(void)
32 {
33   type *prim_t_int;
34   ir_graph *irg;
35   type *owner;
36   type *proc_main;
37   entity *ent;
38   ir_node *b, *x, *r, *t, *f;
39
40   printf("\nCreating an IR graph: WHILE_EXAMPLE...\n");
41
42   init_firm ();
43
44   set_optimize(1);
45   set_opt_constant_folding(1);
46   set_opt_cse(1);
47   set_opt_dead_node_elimination (1);
48
49   prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_i);
50
51 #define METHODNAME "main_tp"
52 #define NRARGS 1
53 #define NRES 1
54
55   proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
56                               NRARGS, NRES);
57   set_method_param_type(proc_main, 0, prim_t_int);
58   set_method_res_type(proc_main, 0, prim_t_int);
59
60
61   owner = new_type_class (id_from_str ("WHILE_EXAMPLE", 13));
62   ent = new_entity (owner, id_from_str ("main", strlen("main")), proc_main);
63
64   /* Generates start and end blocks and nodes and a first, initial block */
65   irg = new_ir_graph (ent, 4);
66
67   /* Generate two values */
68   set_value (0, new_Proj(get_irg_args(irg), mode_i, 0));
69   set_value (1, new_Const (mode_i, tarval_from_long (mode_i, 1)));
70   x = new_Jmp();
71   mature_block (get_irg_current_block(irg));
72
73
74   /* generate a block for the loop header and the conditional branch */
75   r = new_immBlock ();
76   add_in_edge (r, x);
77   x = new_Cond (new_Proj(new_Cmp(new_Const (mode_i, tarval_from_long (mode_i, 0)),
78                                  get_value(1, mode_i)),
79                          mode_b, Eq));
80   f = new_Proj (x, mode_X, 0);
81   t = new_Proj (x, mode_X, 1);
82
83   /* generate the block for the loop body */
84   b = new_immBlock ();
85   add_in_edge (b, t);
86   x = new_Jmp ();
87   add_in_edge (r, x);
88
89   /* The code in the loop body,
90      as we are dealing with local variables only the dataflow edges
91      are manipulated. */
92   set_value (2, get_value (0, mode_i));
93   set_value (0, get_value (1, mode_i));
94   set_value (1, get_value (2, mode_i));
95   mature_block (b);
96   mature_block (r);
97
98   /* generate the return block */
99   r = new_immBlock ();
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 (get_irg_end_block(irg), x);
112   mature_block (get_irg_end_block(irg));
113
114   finalize_cons (irg);
115
116   printf("Optimizing ...\n");
117
118   local_optimize_graph(irg),
119   dead_node_elimination(irg);
120
121   /* verify the graph */
122   irg_vrfy(irg);
123
124   /* output the vcg file */
125   printf("Done building the graph.  Dumping it.\n");
126   turn_of_edge_labels();
127   dump_all_types();
128   dump_ir_block_graph (irg);
129   printf("Use xvcg to view this graph:\n");
130   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
131
132   return (0);
133 }