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