better output of testprograms, warning removed,
[libfirm] / testprograms / while_example.c
1 /*
2  * Project:     libFIRM
3  * File name:   testprograms/while_example.c
4  * Purpose:     Construct a loop.
5  * Author:      Goetz Lindenmaier
6  * Modified by:
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1999-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13
14 # include <stdio.h>
15 # include <string.h>
16
17 # include "irvrfy.h"
18 # include "irdump.h"
19 # include "firm.h"
20
21 /**
22 *  This file constructs the ir for the following pseudo-program:
23 *
24 *  main(int a) {        // pos 0
25 *    int b = 1;         // pos 1
26 *    int h;             // pos 2
27 *
28 *    while (0 == 2) loop {
29 *      h = a;
30 *      a = b;
31 *      b = h;
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: WHILE_EXAMPLE...\n");
49
50   init_firm (NULL);
51
52   set_optimize(1);
53   set_opt_constant_folding(1);
54   set_opt_cse(1);
55   set_opt_dead_node_elimination (1);
56
57   prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_Is);
58
59 #define METHODNAME "main_tp"
60 #define NRARGS 1
61 #define NRES 1
62
63   proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
64                               NRARGS, NRES);
65   set_method_param_type(proc_main, 0, prim_t_int);
66   set_method_res_type(proc_main, 0, prim_t_int);
67
68
69   owner = new_type_class (id_from_str ("WHILE_EXAMPLE", 13));
70   ent = new_entity (owner, id_from_str ("main", strlen("main")), proc_main);
71
72   /* Generates start and end blocks and nodes and a first, initial block */
73   irg = new_ir_graph (ent, 4);
74
75   /* Generate two values */
76   set_value (0, new_Proj(get_irg_args(irg), mode_Is, 0));
77   set_value (1, new_Const (mode_Is, new_tarval_from_long (1, mode_Is)));
78   x = new_Jmp();
79   mature_block (get_irg_current_block(irg));
80
81
82   /* generate a block for the loop header and the conditional branch */
83   r = new_immBlock ();
84   add_in_edge (r, x);
85   x = new_Cond (new_Proj(new_Cmp(new_Const (mode_Is, new_tarval_from_long (0, mode_Is)),
86                                  get_value(1, mode_Is)),
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_immBlock ();
93   add_in_edge (b, t);
94   x = new_Jmp ();
95   add_in_edge (r, x);
96
97   /* The code in the loop body,
98      as we are dealing with local variables only the dataflow edges
99      are manipulated. */
100   set_value (2, get_value (0, mode_Is));
101   set_value (0, get_value (1, mode_Is));
102   set_value (1, get_value (2, mode_Is));
103   mature_block (b);
104   mature_block (r);
105
106   /* generate the return block */
107   r = new_immBlock ();
108   add_in_edge (r, f);
109   mature_block (r);
110
111   {
112      ir_node *in[1];
113      in[0] = new_Sub (get_value (0, mode_Is), get_value (1, mode_Is), mode_Is);
114
115      x = new_Return (get_store (), 1, in);
116   }
117
118   /* finalize the end block generated in new_ir_graph() */
119   add_in_edge (get_irg_end_block(irg), x);
120   mature_block (get_irg_end_block(irg));
121
122   finalize_cons (irg);
123
124   printf("Optimizing ...\n");
125
126   local_optimize_graph(irg),
127   dead_node_elimination(irg);
128
129   /* verify the graph */
130   irg_vrfy(irg);
131
132   /* output the vcg file */
133   printf("Done building the graph.  Dumping it.\n");
134   turn_off_edge_labels();
135   dump_all_types();
136   dump_ir_block_graph (irg);
137   printf("Use xvcg to view this graph:\n");
138   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
139
140   return (0);
141 }