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
22 *** while (0 == 0) loop {
36 type_method *proc_main;
38 ir_node *b, *x, *r, *t, *f;
40 printf("creating an IR graph: IF_WHILE_EXAMPLE...\n");
45 set_opt_constant_folding(0);
47 set_opt_dead_node_elimination (1);
49 #define METHODNAME "main"
53 proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
55 owner = new_type_class (id_from_str ("IF_WHILE_EXAMPLE", 16));
56 ent = new_entity ((type *)owner, id_from_str ("main", 4), (type *)proc_main);
58 /* Generates start and end blocks and nodes and a first, initial block */
59 irg = new_ir_graph (ent, 4);
61 /* Generate two constants */
62 set_value (0, new_Const (mode_I, tarval_from_long (mode_i, 0)));
63 set_value (1, new_Const (mode_I, tarval_from_long (mode_i, 1)));
64 mature_block (irg->current_block);
66 /* Generate a conditional branch */
67 x = new_Cond (new_Proj(new_Cmp(new_Const (mode_I, tarval_from_long (mode_i, 0)),
68 new_Const (mode_I, tarval_from_long (mode_i, 0))),
70 f = new_Proj (x, mode_X, 0);
71 t = new_Proj (x, mode_X, 1);
73 /* generate and fill the then block */
76 set_value (0, new_Const (mode_I, tarval_from_long (mode_i, 2)));
80 /* generate the fall through block and add all cfg edges */
87 /* generate a block for the loop header and the conditional branch */
90 x = new_Cond (new_Proj(new_Cmp(new_Const (mode_I, tarval_from_long (mode_i, 0)),
91 new_Const (mode_I, tarval_from_long (mode_i, 0))),
93 f = new_Proj (x, mode_X, 0);
94 t = new_Proj (x, mode_X, 1);
96 /* generate the block for the loop body */
103 /* the code in the loop body,
104 as we are dealing with local variables only the dataflow edges
106 set_value (2, get_value (0, mode_I));
107 set_value (0, get_value (1, mode_I));
108 set_value (1, get_value (2, mode_I));
111 /* generate the return block */
118 in[0] = new_Sub (get_value (0, mode_I), get_value (1, mode_I), mode_I);
120 x = new_Return (get_store (), 1, in);
123 /* finalize the end block generated in new_ir_graph() */
124 add_in_edge (irg->end_block, x);
125 mature_block (irg->end_block);
127 /* verify the graph */
130 dead_node_elimination(irg);
132 /* output the vcg file */
133 printf("\nDone building the graph. Dumping it.\n");
134 dump_ir_block_graph (irg);
136 printf("use xvcg to view this graph:\n");
137 printf("/ben/goetz/bin/xvcg GRAPHNAME\n");