- implemented get_irg_value_param_type()
[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 #include <stdio.h>
13 #include <string.h>
14
15 #include <libfirm/firm.h>
16
17 /**
18 *  This file constructs the ir for the following pseudo-program:
19 *
20 *  main() {
21 *    int a = 0;         //  pos 0
22 *    int b = 1;         //  pos 1
23 *    int h;             //  pos 2
24 *
25 *    if (0 == 0)
26 *      { a = 2; }
27 *
28 *    while (0 == 0) 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   ir_graph *irg;
42   ir_type *owner;
43   ir_type *proc_main;
44   ir_type *prim_t_int;
45   ir_entity *ent;
46   ir_node *b, *x, *r, *t, *f;
47
48   printf("\nCreating an IR graph: IF_WHILE_EXAMPLE...\n");
49
50   init_firm (NULL);
51   turn_off_edge_labels();
52
53   set_optimize(1);
54   set_opt_constant_folding(0);  /* so that the stupid tests are not optimized. */
55                                 /* if optimized no path to End remains!! */
56   set_opt_cse(1);
57
58   /*** Make basic ir_type information for primitive ir_type int. ***/
59   prim_t_int = new_type_primitive(new_id_from_chars ("int", 3), mode_Iu);
60
61 #define METHODNAME "main"
62 #define NRARGS 0
63 #define NRES 1
64
65   proc_main = new_type_method(new_id_from_chars(METHODNAME, strlen(METHODNAME)),
66                               NRARGS, NRES);
67   set_method_res_type(proc_main, 0, prim_t_int);
68   owner = new_type_class (new_id_from_chars ("IF_WHILE_EXAMPLE", 16));
69   ent = new_entity (owner, new_id_from_chars ("main", 4), proc_main);
70   get_entity_ld_name(ent);
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 constants */
76   set_value(0, new_Const(mode_Iu, new_tarval_from_long(0, mode_Iu)));
77   set_value(1, new_Const(mode_Iu, new_tarval_from_long(1, mode_Iu)));
78   mature_immBlock(get_irg_current_block(irg));
79
80   /* Generate a conditional branch */
81   x = new_Jmp();
82
83   /* generate the fall through block and add all cfg edges */
84   r = new_immBlock();
85   add_immBlock_pred(r, x);
86   mature_immBlock(r);
87   x = new_Jmp();
88
89   /* generate a block for the loop header and the conditional branch */
90   r = new_immBlock();
91   add_immBlock_pred(r, x);
92   x = new_Cond(new_Proj(new_Cmp(new_Const(mode_Iu, new_tarval_from_long(0, mode_Iu)),
93                                 new_Const(mode_Iu, new_tarval_from_long(0, mode_Iu))),
94                         mode_b, pn_Cmp_Eq));
95   f = new_Proj(x, mode_X, pn_Cond_false);
96   t = new_Proj(x, mode_X, pn_Cond_true);
97
98   /* generate the block for the loop body */
99   b = new_immBlock();
100   add_immBlock_pred(b,t);
101   x = new_Jmp();
102   add_immBlock_pred(r, x);
103   mature_immBlock(r);
104
105   /* the code in the loop body,
106      as we are dealing with local variables only the dataflow edges
107      are manipulated */
108   set_value(2, get_value(0, mode_Iu));
109   set_value(0, get_value(1, mode_Iu));
110   set_value(1, get_value(2, mode_Iu));
111   mature_immBlock(b);
112
113   /* generate the return block */
114   r = new_immBlock();
115   add_immBlock_pred(r, f);
116   mature_immBlock(r);
117
118   {
119      ir_node *in[1];
120      in[0] = new_Sub(get_value(0, mode_Iu), get_value(1, mode_Iu), mode_Iu);
121
122      x = new_Return(get_store(), 1, in);
123   }
124
125   /* finalize the end block generated in new_ir_graph() */
126   add_immBlock_pred(get_irg_end_block(irg), x);
127   mature_immBlock(get_irg_end_block(irg));
128
129   irg_finalize_cons(irg);
130
131   printf("Optimizing ...\n");
132
133   local_optimize_graph(irg);
134   dead_node_elimination(irg);
135
136   compute_irg_outs(irg);
137
138   /* verify the graph */
139   irg_vrfy(irg);
140
141   /* output the vcg file */
142   printf("Done building the graph.  Dumping it with out-edges.\n");
143   dump_out_edges(1);
144   dump_ir_graph(irg, 0);
145   printf("Use ycomp to view this graph:\n");
146   printf("ycomp GRAPHNAME\n\n");
147
148   return 0;
149 }