1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
4 ** Authors: Christian Schaefer, Goetz Lindenmaier
13 *** This file constructs the ir for the following pseudo-program:
16 *** int a = 0; // pos 0
17 *** int b = 1; // pos 1
23 *** while (0 == 0) loop {
38 type_method *proc_main;
40 ir_node *b, *x, *r, *t, *f;
42 printf("\nCreating an IR graph: IF_WHILE_EXAMPLE...\n");
47 set_opt_constant_folding(0); /* so that the stupid tests are not optimized. */
48 /* if 1 aborts ????? @@@ */
50 set_opt_dead_node_elimination (1);
52 #define METHODNAME "main"
56 proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
58 owner = new_type_class (id_from_str ("IF_WHILE_EXAMPLE", 16));
59 ent = new_entity ((type *)owner, id_from_str ("main", 4), (type *)proc_main);
61 /* Generates start and end blocks and nodes and a first, initial block */
62 irg = new_ir_graph (ent, 4);
64 /* Generate two constants */
65 set_value (0, new_Const (mode_I, tarval_from_long (mode_i, 0)));
66 set_value (1, new_Const (mode_I, tarval_from_long (mode_i, 1)));
67 mature_block (get_irg_current_block(irg));
69 /* Generate a conditional branch */
70 x = new_Cond (new_Proj(new_Cmp(new_Const (mode_I, tarval_from_long (mode_i, 0)),
71 new_Const (mode_I, tarval_from_long (mode_i, 0))),
73 f = new_Proj (x, mode_X, 0);
74 t = new_Proj (x, mode_X, 1);
76 /* generate and fill the then block */
79 set_value (0, new_Const (mode_I, tarval_from_long (mode_i, 2)));
83 /* generate the fall through block and add all cfg edges */
90 /* generate a block for the loop header and the conditional branch */
93 x = new_Cond (new_Proj(new_Cmp(new_Const (mode_I, tarval_from_long (mode_i, 0)),
94 new_Const (mode_I, tarval_from_long (mode_i, 0))),
96 f = new_Proj (x, mode_X, 0);
97 t = new_Proj (x, mode_X, 1);
99 /* generate the block for the loop body */
106 /* the code in the loop body,
107 as we are dealing with local variables only the dataflow edges
109 set_value (2, get_value (0, mode_I));
110 set_value (0, get_value (1, mode_I));
111 set_value (1, get_value (2, mode_I));
114 /* generate the return block */
121 in[0] = new_Sub (get_value (0, mode_I), get_value (1, mode_I), mode_I);
123 x = new_Return (get_store (), 1, in);
126 /* finalize the end block generated in new_ir_graph() */
127 add_in_edge (get_irg_end_block(irg), x);
128 mature_block (get_irg_end_block(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 dump_ir_block_graph (irg);
141 printf("Use xvcg to view this graph:\n");
142 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");