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) { // pos 0
28 * while (0 == 2) loop {
46 ir_node *b, *x, *r, *t, *f;
48 printf("\nCreating an IR graph: WHILE_EXAMPLE...\n");
53 set_opt_constant_folding(1);
55 set_opt_dead_node_elimination (1);
57 prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_Is);
59 #define METHODNAME "main_tp"
63 proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
65 set_method_param_type(proc_main, 0, prim_t_int);
66 set_method_res_type(proc_main, 0, prim_t_int);
69 owner = new_type_class (id_from_str ("WHILE_EXAMPLE", 13));
70 ent = new_entity (owner, id_from_str ("main", strlen("main")), proc_main);
72 /* Generates start and end blocks and nodes and a first, initial block */
73 irg = new_ir_graph (ent, 4);
75 /* Generate two values */
76 set_value (0, new_Proj(get_irg_args(irg), mode_Is, 0));
77 set_value (1, new_Const (mode_Is, new_tarval_from_long (1, mode_Is)));
79 mature_block (get_irg_current_block(irg));
82 /* generate a block for the loop header and the conditional branch */
85 x = new_Cond (new_Proj(new_Cmp(new_Const (mode_Is, new_tarval_from_long (0, mode_Is)),
86 get_value(1, mode_Is)),
88 f = new_Proj (x, mode_X, 0);
89 t = new_Proj (x, mode_X, 1);
91 /* generate the block for the loop body */
97 /* The code in the loop body,
98 as we are dealing with local variables only the dataflow edges
100 set_value (2, get_value (0, mode_Is));
101 set_value (0, get_value (1, mode_Is));
102 set_value (1, get_value (2, mode_Is));
106 /* generate the return block */
113 in[0] = new_Sub (get_value (0, mode_Is), get_value (1, mode_Is), mode_Is);
115 x = new_Return (get_store (), 1, in);
118 /* finalize the end block generated in new_ir_graph() */
119 add_in_edge (get_irg_end_block(irg), x);
120 mature_block (get_irg_end_block(irg));
124 printf("Optimizing ...\n");
126 local_optimize_graph(irg),
127 dead_node_elimination(irg);
129 /* verify the graph */
132 /* output the vcg file */
133 printf("Done building the graph. Dumping it.\n");
134 turn_off_edge_labels();
136 dump_ir_block_graph (irg);
137 printf("Use xvcg to view this graph:\n");
138 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");