fixed doxygen output
[libfirm] / testprograms / nested_phi.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, int b) { //  pos 0, pos 1
25  *    int c = 1;         //  pos 2
26  *    int d = 2;         //  pos 3
27  *
28  *    while (a < c) {
29  *      while (a < d) {
30  *      }
31  *    }
32  *
33  *    return a-b;
34  *  }
35  */
36
37 #define a_pos 0
38 #define b_pos 1
39 #define c_pos 2
40 #define d_pos 3
41
42 int
43 main(void)
44 {
45   type *prim_t_int;
46   ir_graph *irg;
47   type *owner;
48   type *proc_main;
49   entity *ent;
50   ir_node *h1, *b1, *h2, *b2, *x, *r, *t1, *f1, *t2, *f2;
51
52   printf("\nCreating an IR graph: NESTED_PHI...\n");
53
54   init_firm (NULL);
55   //  set_opt_normalize (0);
56   set_optimize(1);
57   set_opt_constant_folding(1);
58   set_opt_cse(1);
59   set_opt_dead_node_elimination (1);
60
61   prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_Is);
62
63 #define METHODNAME "main_tp"
64 #define NRARGS 1
65 #define NRES 1
66
67   proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
68                               NRARGS, NRES);
69   set_method_param_type(proc_main, 0, prim_t_int);
70   set_method_res_type(proc_main, 0, prim_t_int);
71
72   owner = new_type_class (new_id_from_str ("NESTED_PHI"));
73   ent = new_entity (owner, new_id_from_str ("main"), proc_main);
74
75   /* Generates start and end blocks and nodes and a first, initial block */
76   irg = new_ir_graph (ent, 4);
77
78   /* Generate two values */
79   set_value (a_pos, new_Proj(get_irg_args(irg), mode_Is, 0));
80   set_value (b_pos, new_Proj(get_irg_args(irg), mode_Is, 0));
81   set_value (c_pos, new_Const (mode_Is, new_tarval_from_long (1, mode_Is)));
82   set_value (d_pos, new_Const (mode_Is, new_tarval_from_long (2, mode_Is)));
83
84   /* a block for the outer loop header and the conditional branch */
85   h1 = get_irg_current_block(irg);
86   x = new_Cond (new_Proj(new_Cmp(get_value(a_pos, mode_Is), get_value(c_pos, mode_Is)),
87                          mode_b, Le));
88   f1 = new_Proj (x, mode_X, 0);
89   t1 = new_Proj (x, mode_X, 1);
90
91   /* generate the block for the loop body */
92   b1 = new_immBlock ();
93   add_in_edge (b1, t1);
94
95   /* The loop body is the head of the inner loop */
96   h2 = b1;
97   x = new_Cond (new_Proj(new_Cmp(get_value(a_pos, mode_Is), get_value(d_pos, mode_Is)),
98                          mode_b, Le));
99   f2 = new_Proj (x, mode_X, 0);
100   t2 = new_Proj (x, mode_X, 1);
101   add_in_edge(h1, f2);
102   mature_block(h1);
103
104   /* The inner loop body */
105   b2 = new_immBlock ();
106   add_in_edge (b2, t2);
107   mature_block(b2);
108   x = new_Jmp();
109   add_in_edge (h2, x);
110   mature_block(h2);
111
112   /* generate the return block */
113   r = new_immBlock ();
114   add_in_edge (r, f1);
115   mature_block (r);
116
117   {
118      ir_node *in[1];
119      in[0] = new_Sub (get_value (a_pos, mode_Is), get_value (b_pos, mode_Is), mode_Is);
120
121      x = new_Return (get_store (), 1, in);
122   }
123
124   /* finalize the end block generated in new_ir_graph() */
125   add_in_edge (get_irg_end_block(irg), x);
126   mature_block (get_irg_end_block(irg));
127
128   irg_finalize_cons (irg);
129
130   printf("Optimizing ...\n");
131
132   //local_optimize_graph(irg),
133   //dead_node_elimination(irg);
134
135   /* verify the graph */
136   irg_vrfy(irg);
137
138   /* output the vcg file */
139   printf("Done building the graph.  Dumping it.\n");
140   turn_off_edge_labels();
141   dump_all_types();
142   dump_ir_block_graph (irg);
143   printf("Use xvcg to view this graph:\n");
144   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
145
146   return (0);
147 }