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:
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");
44 #define METHODNAME "main"
48 proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
50 owner = new_type_class (id_from_str ("IF_WHILE_EXAMPLE", 16));
51 ent = new_entity ((type *)owner, id_from_str ("main", 4), (type *)proc_main);
53 /* Generates start and end blocks and nodes and a first, initial block */
54 irg = new_ir_graph (ent, 4);
56 /* Generate two constants */
57 set_value (0, new_Const (mode_I, tarval_from_long (mode_i, 0)));
58 set_value (1, new_Const (mode_I, tarval_from_long (mode_i, 1)));
59 mature_block (irg->current_block);
61 /* Generate a conditional branch */
62 x = new_Cond (new_Proj(new_Cmp(new_Const (mode_I, tarval_from_long (mode_i, 0)),
63 new_Const (mode_I, tarval_from_long (mode_i, 0))),
65 f = new_Proj (x, mode_X, 0);
66 t = new_Proj (x, mode_X, 1);
68 /* generate and fill the then block */
71 set_value (0, new_Const (mode_I, tarval_from_long (mode_i, 2)));
75 /* generate the fall through block and add all cfg edges */
82 /* generate a block for the loop header and the conditional branch */
85 x = new_Cond (new_Proj(new_Cmp(new_Const (mode_I, tarval_from_long (mode_i, 0)),
86 new_Const (mode_I, tarval_from_long (mode_i, 0))),
88 f = new_Proj (x, mode_X, 0);
89 t = new_Proj (x, mode_X, 1);
91 /* generate the block for the loop body */
98 /* the code in the loop body,
99 as we are dealing with local variable only the dataflow edges are manipulated */
100 set_value (2, get_value (0, mode_I));
101 set_value (0, get_value (1, mode_I));
102 set_value (1, get_value (2, mode_I));
105 /* generate the return block */
112 in[0] = new_Sub (get_value (0, mode_I), get_value (1, mode_I), mode_I);
114 x = new_Return (get_store (), 1, in);
117 /* finalize the end block generated in new_ir_graph() */
118 add_in_edge (irg->end_block, x);
119 mature_block (irg->end_block);
121 /* verify the graph */
124 /* output the vcg file */
125 printf("\nDone building the graph. Dumping it.\n");
126 dump_ir_block_graph (irg);
128 printf("use xvcg to view this graph:\n");
129 printf("/ben/goetz/bin/xvcg GRAPHNAME\n");