1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
4 ** Authors: Christian Schaefer, Goetz Lindenmaier
15 * das leere FIRM Programm
19 *** This file constructs the ir for the following pseudo-program:
33 int main(int argc, char **argv)
35 ir_graph *irg; /* this variable contains the irgraph */
36 type_class *owner; /* the class in which this method is defined */
37 entity *ent; /* represents this method as entity of owner */
38 ir_node *x, *x_then, *x_else, *c0, *c1, *c2, *cmpGt, *f, *t, *b;
40 printf("creating an IR graph: IF_ELSE_EXAMPLE...\n");
45 /* FIRM was designed for oo languages where all methods belong to a class.
46 * For imperative languages like C we view a file as a large class containing
47 * all functions as methods in this file.
48 * Therefore we define a class "IF_ELSE_EXAMPLE" with a method main as an
51 #define CLASSNAME "IF_ELSE_EXAMPLE"
52 #define ENTITYNAME "main"
54 owner = new_type_class (id_from_str (CLASSNAME, strlen(CLASSNAME)));
55 ent = new_entity ((type *)owner, id_from_str (ENTITYNAME, strlen(ENTITYNAME)), NULL);
58 /* Generates the basic graph for the method represented by entity ent, that
59 * is, generates start and end blocks and nodes and a first, initial block.
60 * The constructor needs to know how many local variables the method has.
62 #define NUM_OF_LOCAL_VARS 2
64 irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
66 /* Generate two constants */
67 c0 = new_Const (mode_i, tarval_from_long (mode_i, 0));
68 c1 = new_Const (mode_i, tarval_from_long (mode_i, 1));
70 /* set a and b to constants */
71 set_value (0, c0); /* this (0) is variable a */
72 set_value (1, c1); /* this (1) is variable b */
74 /* the expression that evaluates the condition */
75 c2 = new_Const (mode_i, tarval_from_long (mode_i, 2));
76 cmpGt = new_Proj(new_Cmp(get_value(0, mode_i), c2), mode_b, Gt);
78 /* the conditional branch */
80 f = new_Proj (x, mode_X, 0); /* if condition is false */
81 t = new_Proj (x, mode_X, 1); /* if condition is true */
83 mature_block (irg->current_block);
85 /* generate and fill the then block */
88 set_value (0, get_value(1, mode_i));
92 /* generate and fill the else block */
95 set_value (1, new_Const (mode_i, tarval_from_long (mode_i, 2)));
99 /* generate the join block and add all cfg edges */
101 add_in_edge (b, x_then);
102 add_in_edge (b, x_else);
105 /* Generate the return node into current region. */
107 ir_node *in[2]; /* this is the array containing the return parameters */
108 in[0] = get_value(0, mode_i);
109 in[1] = get_value(1, mode_i);
110 x = new_Return (get_store(), 2, in);
112 /* Now generate all instructions for this block and all its predecessor blocks
113 * so we can mature it. */
114 mature_block (irg->current_block);
116 /* This adds the in edge of the end block which originates at the return statement.
117 * The return node passes controlflow to the end block. */
118 add_in_edge (irg->end_block, x);
119 /* Now we can mature the end block as all it's predecessors are known. */
120 mature_block (irg->end_block);
122 printf("\nDone building the graph. Dumping it.\n");
123 dump_ir_block_graph (irg);
125 printf("use xvcg to view this graph:\n");
126 printf("/ben/trapp/bin/i486/xvcg GRAPHNAME\n");