4d2d7fb0bc18d148007957fbd2a404264b5980dc
[libfirm] / testprograms / endless_loop.c
1 /* (C) 2002 by Universitaet Karlsruhe
2 ** All rights reserved.
3 **
4 ** Authors: Goetz Lindenmaier
5 **
6 ** testprogram.
7 */
8
9 /* $ID$ */
10
11 # include <stdio.h>
12 # include <string.h>
13
14 # include "irvrfy.h"
15 # include "irdump.h"
16 # include "firm.h"
17
18 /**
19 ***  This file constructs the ir for the following pseudo-program:
20 ***
21 ***  VAR_A is some extern variable.
22 ***
23 ***  main(int a) {        // pos 0
24 ***    int b = 1;         // pos 1
25 ***    int h;             // pos 2
26 ***
27 ***    while (0 == 0) loop {
28 ***      h = a;
29 ***      a = b;
30 ***      b = h;
31 ***      VAR_A = b;
32 ***    }
33 ***
34 ***    return a-b;
35 ***  }
36 **/
37
38 int
39 main(void)
40 {
41   type *prim_t_int;
42   ir_graph *irg;
43   type *owner;
44   type *proc_main;
45   entity *ent;
46   ir_node *b, *x, *r, *t, *f;
47
48   printf("\nCreating an IR graph: ENDLESS_LOOP_EXAMPLE...\n");
49
50   init_firm ();
51
52   set_optimize(1);
53   set_opt_constant_folding(1);
54   set_opt_cse(1);
55   set_opt_global_cse(0);
56   set_opt_dead_node_elimination (1);
57
58   prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_Is);
59
60 #define METHODNAME "main_tp"
61 #define NRARGS 1
62 #define NRES 1
63
64   proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
65                               NRARGS, NRES);
66   set_method_param_type(proc_main, 0, prim_t_int);
67   set_method_res_type(proc_main, 0, prim_t_int);
68
69
70   owner = new_type_class (id_from_str ("ENDLESS_LOOP_EXAMPLE", 20));
71   ent = new_entity (owner, id_from_str ("main", strlen("main")), proc_main);
72
73   /* Generates start and end blocks and nodes and a first, initial block */
74   irg = new_ir_graph (ent, 4);
75
76   /* Generate two values */
77   set_value (0, new_Proj(get_irg_args(irg), mode_Is, 0));
78   set_value (1, new_Const (mode_Is, tarval_from_long (mode_Is, 1)));
79
80   x = new_Jmp();
81   mature_block (get_irg_current_block(irg));
82
83   /* generate a block for the loop header and the conditional branch */
84   r = new_immBlock ();
85   add_in_edge (r, x);
86   x = new_Cond (new_Proj(new_Cmp(new_Const (mode_Is, tarval_from_long (mode_Is, 0)),
87                                  new_Const (mode_Is, tarval_from_long (mode_Is, 0))),
88                          mode_b, Eq));
89   f = new_Proj (x, mode_X, 0);
90   t = new_Proj (x, mode_X, 1);
91
92   /* generate the block for the loop body */
93   b = new_immBlock ();
94   add_in_edge (b, t);
95   x = new_Jmp ();
96   add_in_edge (r, x);
97
98   /* The code in the loop body,
99      as we are dealing with local variables only the dataflow edges
100      are manipulated. */
101   set_value (2, get_value (0, mode_Is));
102   set_value (0, get_value (1, mode_Is));
103   set_value (1, get_value (2, mode_Is));
104
105   /* set VAR_A to constant value */
106   set_store (new_Proj (new_Store (get_store (),
107                                   new_Const (mode_P, tarval_P_from_str ("VAR_A")),
108                                   get_value(1, mode_Is)),
109                        mode_M, 0));
110
111   mature_block (b);
112   mature_block (r);
113
114   /* generate the return block */
115   r = new_immBlock ();
116   add_in_edge (r, f);
117   mature_block (r);
118
119   {
120      ir_node *in[1];
121      in[0] = new_Sub (get_value (0, mode_Is), get_value (1, mode_Is), mode_Is);
122
123      x = new_Return (get_store (), 1, in);
124   }
125
126   /* finalize the end block generated in new_ir_graph() */
127   add_in_edge (get_irg_end_block(irg), x);
128   mature_block (get_irg_end_block(irg));
129
130   finalize_cons (irg);
131
132   printf("Optimizing ...\n");
133
134   dead_node_elimination(irg);
135   local_optimize_graph(irg);
136
137   /* verify the graph */
138   irg_vrfy(irg);
139
140   /* output the vcg file */
141   printf("Done building the graph.  Dumping it.\n");
142   //turn_of_edge_labels();
143   dump_keepalive_edges(true);
144   dump_all_types();
145   dump_ir_block_graph (irg);
146   printf("Use xvcg to view this graph:\n");
147   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
148
149   return (0);
150 }