3 * File name: testprograms/if_while_example.c
4 * Purpose: Shows more complex control flow.
5 * Author: Christian Schaefer, Goetz Lindenmaier
9 * Copyright: (c) 1999-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
23 * This file constructs the ir for the following pseudo-program:
33 * while (0 == 0) loop {
51 ir_node *b, *x, *r, *t, *f;
53 printf("\nCreating an IR graph: IF_WHILE_EXAMPLE...\n");
56 turn_off_edge_labels();
59 set_opt_constant_folding(0); /* so that the stupid tests are not optimized. */
60 /* if optimized no path to End remains!! */
62 set_opt_dead_node_elimination (1);
64 /*** Make basic type information for primitive type int. ***/
65 prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_Iu);
67 #define METHODNAME "main"
71 proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
73 set_method_res_type(proc_main, 0, prim_t_int);
74 owner = new_type_class (id_from_str ("IF_WHILE_EXAMPLE", 16));
75 ent = new_entity (owner, id_from_str ("main", 4), proc_main);
76 get_entity_ld_name(ent);
78 /* Generates start and end blocks and nodes and a first, initial block */
79 irg = new_ir_graph (ent, 4);
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_block (get_irg_current_block(irg));
86 /* Generate a conditional branch */
89 /* generate the fall through block and add all cfg edges */
95 /* generate a block for the loop header and the conditional branch */
98 x = new_Cond (new_Proj(new_Cmp(new_Const (mode_Iu, new_tarval_from_long (0, mode_Is)),
99 new_Const (mode_Iu, new_tarval_from_long (0, mode_Is))),
101 f = new_Proj (x, mode_X, 0);
102 t = new_Proj (x, mode_X, 1);
104 /* generate the block for the loop body */
111 /* the code in the loop body,
112 as we are dealing with local variables only the dataflow edges
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));
119 /* generate the return block */
126 in[0] = new_Sub (get_value (0, mode_Iu), get_value (1, mode_Iu), mode_Iu);
128 x = new_Return (get_store (), 1, in);
131 /* finalize the end block generated in new_ir_graph() */
132 add_in_edge (get_irg_end_block(irg), x);
133 mature_block (get_irg_end_block(irg));
137 printf("Optimizing ...\n");
139 local_optimize_graph(irg);
140 dead_node_elimination(irg);
144 /* verify the graph */
147 /* output the vcg file */
148 printf("Done building the graph. Dumping it with out-edges.\n");
151 printf("Use xvcg to view this graph:\n");
152 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");