3 * File name: testprograms/while_example.c
4 * Purpose: Construct a loop.
5 * Author: Goetz Lindenmaier
9 * Copyright: (c) 1999-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
22 * This file constructs the ir for the following pseudo-program:
24 * main(int a, int b) { // pos 0, pos 1
50 ir_node *h1, *b1, *h2, *b2, *x, *r, *t1, *f1, *t2, *f2;
52 printf("\nCreating an IR graph: NESTED_PHI...\n");
55 // set_opt_normalize (0);
57 set_opt_constant_folding(1);
59 set_opt_dead_node_elimination (1);
61 prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_Is);
63 #define METHODNAME "main_tp"
67 proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
69 set_method_param_type(proc_main, 0, prim_t_int);
70 set_method_res_type(proc_main, 0, prim_t_int);
72 owner = new_type_class (new_id_from_str ("NESTED_PHI"));
73 ent = new_entity (owner, new_id_from_str ("main"), proc_main);
75 /* Generates start and end blocks and nodes and a first, initial block */
76 irg = new_ir_graph (ent, 4);
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)));
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)),
88 f1 = new_Proj (x, mode_X, 0);
89 t1 = new_Proj (x, mode_X, 1);
91 /* generate the block for the loop body */
95 /* The loop body is the head of the inner loop */
97 x = new_Cond (new_Proj(new_Cmp(get_value(a_pos, mode_Is), get_value(d_pos, mode_Is)),
99 f2 = new_Proj (x, mode_X, 0);
100 t2 = new_Proj (x, mode_X, 1);
104 /* The inner loop body */
105 b2 = new_immBlock ();
106 add_in_edge (b2, t2);
112 /* generate the return block */
119 in[0] = new_Sub (get_value (a_pos, mode_Is), get_value (b_pos, mode_Is), mode_Is);
121 x = new_Return (get_store (), 1, in);
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));
128 irg_finalize_cons (irg);
130 printf("Optimizing ...\n");
132 //local_optimize_graph(irg),
133 //dead_node_elimination(irg);
135 /* verify the graph */
138 /* output the vcg file */
139 printf("Done building the graph. Dumping it.\n");
140 turn_off_edge_labels();
142 dump_ir_block_graph (irg);
143 printf("Use xvcg to view this graph:\n");
144 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");