3 * File name: testprograms/endless_loop.c
4 * Purpose: Representation of an endless loop.
5 * Author: 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:
25 * VAR_A is some extern variable.
27 * main(int a) { // pos 0
31 * while (0 == 0) loop {
50 ir_node *b, *x, *r, *t, *f;
52 printf("\nCreating an IR graph: ENDLESS_LOOP_EXAMPLE...\n");
57 set_opt_constant_folding(1);
59 set_opt_global_cse(0);
60 set_opt_dead_node_elimination (1);
62 prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_Is);
64 #define METHODNAME "main_tp"
68 proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
70 set_method_param_type(proc_main, 0, prim_t_int);
71 set_method_res_type(proc_main, 0, prim_t_int);
74 owner = new_type_class (id_from_str ("ENDLESS_LOOP_EXAMPLE", 20));
75 ent = new_entity (owner, id_from_str ("main", strlen("main")), proc_main);
77 /* Generates start and end blocks and nodes and a first, initial block */
78 irg = new_ir_graph (ent, 4);
80 /* Generate two values */
81 set_value (0, new_Proj(get_irg_args(irg), mode_Is, 0));
82 set_value (1, new_Const (mode_Is, new_tarval_from_long (1, mode_Is)));
85 mature_block (get_irg_current_block(irg));
87 /* generate a block for the loop header and the conditional branch */
90 x = new_Cond (new_Proj(new_Cmp(new_Const (mode_Is, new_tarval_from_long (0, mode_Is)),
91 new_Const (mode_Is, new_tarval_from_long (0, mode_Is))),
93 f = new_Proj (x, mode_X, 0);
94 t = new_Proj (x, mode_X, 1);
96 /* generate the block for the loop body */
102 /* The code in the loop body,
103 as we are dealing with local variables only the dataflow edges
105 set_value (2, get_value (0, mode_Is));
106 set_value (0, get_value (1, mode_Is));
107 set_value (1, get_value (2, mode_Is));
109 /* set VAR_A to constant value */
110 set_store (new_Proj (new_Store (get_store (),
113 get_irg_globals(irg),
114 new_entity(get_glob_type(),id_from_str("VAR_A",6),prim_t_int)),
115 get_value(1, mode_Is)),
121 /* generate the return block */
128 in[0] = new_Sub (get_value (0, mode_Is), get_value (1, mode_Is), mode_Is);
130 x = new_Return (get_store (), 1, in);
133 /* finalize the end block generated in new_ir_graph() */
134 add_in_edge (get_irg_end_block(irg), x);
135 mature_block (get_irg_end_block(irg));
139 printf("Optimizing ...\n");
141 dead_node_elimination(irg);
142 local_optimize_graph(irg);
144 /* verify the graph */
147 /* output the vcg file */
148 printf("Done building the graph. Dumping it.\n");
149 //turn_of_edge_labels();
150 dump_keepalive_edges(true);
152 dump_ir_block_graph (irg);
153 printf("Use xvcg to view this graph:\n");
154 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");