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(new_id_from_chars ("int", 3), mode_Is);
59 #define METHODNAME "main_tp"
63 proc_main = new_type_method(new_id_from_chars(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 (new_id_from_chars ("WHILE_EXAMPLE", 13));
70 ent = new_entity (owner, new_id_from_chars ("main", strlen("main")), proc_main);
71 get_entity_ld_name(ent); /* force name mangling */
73 /* Generates start and end blocks and nodes and a first, initial block */
74 irg = new_ir_graph (ent, 4);
76 /* Generate two values */
77 set_value (0, new_Proj(get_irg_args(irg), mode_Is, 0));
78 set_value (1, new_Const (mode_Is, new_tarval_from_long (1, mode_Is)));
80 mature_immBlock (get_irg_current_block(irg));
83 /* generate a block for the loop header and the conditional branch */
85 add_immBlock_pred (r, x);
86 x = new_Cond (new_Proj(new_Cmp(new_Const (mode_Is, new_tarval_from_long (0, mode_Is)),
87 get_value(1, mode_Is)),
89 f = new_Proj (x, mode_X, 0);
90 t = new_Proj (x, mode_X, 1);
92 /* generate the block for the loop body */
94 add_immBlock_pred (b, t);
96 add_immBlock_pred (r, x);
98 /* The code in the loop body,
99 as we are dealing with local variables only the dataflow edges
101 set_value (2, get_value (0, mode_Is));
102 set_value (0, get_value (1, mode_Is));
103 set_value (1, get_value (2, mode_Is));
107 /* generate the return block */
109 add_immBlock_pred (r, f);
114 in[0] = new_Sub (get_value (0, mode_Is), get_value (1, mode_Is), mode_Is);
116 x = new_Return (get_store (), 1, in);
119 /* finalize the end block generated in new_ir_graph() */
120 add_immBlock_pred (get_irg_end_block(irg), x);
121 mature_immBlock (get_irg_end_block(irg));
123 irg_finalize_cons (irg);
125 printf("Optimizing ...\n");
127 local_optimize_graph(irg),
128 dead_node_elimination(irg);
130 /* verify the graph */
133 /* output the vcg file */
134 printf("Done building the graph. Dumping it.\n");
135 turn_off_edge_labels();
137 dump_all_types(suffix);
138 dump_ir_block_graph (irg, suffix);
139 printf("Use xvcg to view this graph:\n");
140 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");