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