dddb2d2a7f98fb67376ee1c213e7b541c62da648
[libfirm] / testprograms / if_while_example.c
1 /*
2  * Project:     libFIRM
3  * File name:   testprograms/if_while_example.c
4  * Purpose:     Shows more complex control flow.
5  * Author:      Christian Schaefer, 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() {
25 *    int a = 0;         // pos 0
26 *    int b = 1;         // pos 1
27 *    int h;             // pos 2
28 *
29 *    if (0 == 0)
30 *      { a = 2; }
31 *
32 *    while (0 == 0) loop {
33 *      h = a;
34 *      a = b;
35 *      b = h;
36 *    }
37 *
38 *    return a-b;
39 *  }
40 **/
41
42 int
43 main(void)
44 {
45   ir_graph *irg;
46   type *owner;
47   type *proc_main;
48   type     *prim_t_int;
49   entity *ent;
50   ir_node *b, *x, *r, *t, *f;
51
52   printf("\nCreating an IR graph: IF_WHILE_EXAMPLE...\n");
53
54   init_firm (NULL);
55   turn_off_edge_labels();
56
57   set_optimize(1);
58   set_opt_constant_folding(0);  /* so that the stupid tests are not optimized. */
59                                 /* if optimized no path to End remains!! */
60   set_opt_cse(1);
61   set_opt_dead_node_elimination (1);
62
63   /*** Make basic type information for primitive type int. ***/
64   prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_Iu);
65
66 #define METHODNAME "main"
67 #define NRARGS 0
68 #define NRES 1
69
70   proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
71                               NRARGS, NRES);
72   set_method_res_type(proc_main, 0, prim_t_int);
73   owner = new_type_class (id_from_str ("IF_WHILE_EXAMPLE", 16));
74   ent = new_entity (owner, id_from_str ("main", 4), proc_main);
75   get_entity_ld_name(ent);
76
77   /* Generates start and end blocks and nodes and a first, initial block */
78   irg = new_ir_graph (ent, 4);
79
80   /* Generate two constants */
81   set_value (0, new_Const (mode_Iu, new_tarval_from_long (0, mode_Iu)));
82   set_value (1, new_Const (mode_Iu, new_tarval_from_long (1, mode_Iu)));
83   mature_block (get_irg_current_block(irg));
84
85   /* Generate a conditional branch */
86   x = new_Jmp();
87
88   /* generate the fall through block and add all cfg edges */
89   r = new_immBlock ();
90   add_in_edge (r, x);
91   mature_block (r);
92   x = new_Jmp ();
93
94   /* generate a block for the loop header and the conditional branch */
95   r = new_immBlock ();
96   add_in_edge (r, x);
97   x = new_Cond (new_Proj(new_Cmp(new_Const (mode_Iu, new_tarval_from_long (0, mode_Is)),
98                                  new_Const (mode_Iu, new_tarval_from_long (0, mode_Is))),
99                          mode_b, Eq));
100   f = new_Proj (x, mode_X, 0);
101   t = new_Proj (x, mode_X, 1);
102
103   /* generate the block for the loop body */
104   b = new_immBlock ();
105   add_in_edge (b,t);
106   x = new_Jmp ();
107   add_in_edge (r, x);
108   mature_block (r);
109
110   /* the code in the loop body,
111      as we are dealing with local variables only the dataflow edges
112      are manipulated */
113   set_value (2, get_value (0, mode_Iu));
114   set_value (0, get_value (1, mode_Iu));
115   set_value (1, get_value (2, mode_Iu));
116   mature_block (b);
117
118   /* generate the return block */
119   r = new_immBlock ();
120   add_in_edge (r, f);
121   mature_block (r);
122
123   {
124      ir_node *in[1];
125      in[0] = new_Sub (get_value (0, mode_Iu), get_value (1, mode_Iu), mode_Iu);
126
127      x = new_Return (get_store (), 1, in);
128   }
129
130   /* finalize the end block generated in new_ir_graph() */
131   add_in_edge (get_irg_end_block(irg), x);
132   mature_block (get_irg_end_block(irg));
133
134   finalize_cons (irg);
135
136   printf("Optimizing ...\n");
137
138   local_optimize_graph(irg);
139   dead_node_elimination(irg);
140
141   compute_outs(irg);
142
143   /* verify the graph */
144   irg_vrfy(irg);
145
146   /* output the vcg file */
147   printf("Done building the graph.  Dumping it with out-edges.\n");
148   dump_out_edges();
149   dump_ir_graph (irg);
150   printf("Use xvcg to view this graph:\n");
151   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
152
153   return (0);
154 }