1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
4 ** Authors: Christian Schaefer, Goetz Lindenmaier
15 *** This file constructs the ir for the following pseudo-program:
18 *** if ((a > 2) && (a < 10))
24 int main(int argc, char **argv)
27 ir_graph *irg; /* this variable contains the irgraph */
28 type *owner; /* the class in which this method is defined */
29 type *method; /* the type of this method */
30 entity *ent; /* represents this method as entity of owner */
31 ir_node *x, *x_then, *arg1, *c2, *c10, *cmpGt, *cmpLt, *and, *f, *t, *b;
33 printf("\nCreating an IR graph: COND_EXAMPLE...\n");
38 /*** Make basic type information for primitive type int. ***/
39 prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_i);
41 /* FIRM was designed for oo languages where all methods belong to a class.
42 * For imperative languages like C we view a file as a large class containing
43 * all functions as methods in this file.
44 * Therefore we define a class "COND_EXAMPLE" with a method main as an
47 #define CLASSNAME "COND_EXAMPLE"
48 #define ENTITYNAME "main"
50 owner = new_type_class (id_from_str (CLASSNAME, strlen(CLASSNAME)));
51 method = new_type_method (id_from_str("main", 4), 1, 1);
52 set_method_param_type(method, 0, prim_t_int);
53 set_method_res_type(method, 0, prim_t_int);
54 ent = new_entity (owner, id_from_str (ENTITYNAME, strlen(ENTITYNAME)), method);
57 /* Generates the basic graph for the method represented by entity ent, that
58 * is, generates start and end blocks and nodes and a first, initial block.
59 * The constructor needs to know how many local variables the method has.
61 #define NUM_OF_LOCAL_VARS 1
63 irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
65 /* get the first argument a of method main - see irgraph.h */
66 arg1 = new_Proj(get_irg_args(irg), mode_i, 0);
68 /* arg1 as first first local variable - makes things simple */
71 /* the expression that evaluates the condition */
73 c2 = new_Const (mode_i, tarval_from_long (mode_i, 2));
74 cmpGt = new_Proj(new_Cmp(get_value(0, mode_i), c2), mode_b, Gt);
75 cmpGt = new_Conv(cmpGt, mode_i);
78 c10 = new_Const (mode_i, tarval_from_long (mode_i, 10));
79 cmpLt = new_Proj(new_Cmp(get_value(0, mode_i), c10), mode_b, Lt);
80 cmpLt = new_Conv(cmpLt, mode_i);
83 and = new_And(cmpGt, cmpLt, mode_i);
84 /* compare result and 0 because we have no cast from integer to bool */
85 and = new_Cmp(and, new_Const (mode_i, tarval_from_long (mode_i, 0)));
86 and = new_Proj(and, mode_b, Ne);
88 /* the conditional branch */
90 f = new_Proj (x, mode_X, 0); /* if condition is false */
91 t = new_Proj (x, mode_X, 1); /* if condition is true */
93 mature_block (get_irg_current_block(irg));
95 /* generate and fill the then block */
98 set_value (0, new_Const (mode_i, tarval_from_long (mode_i, 1)));
102 /* generate the fall through block and add all cfg edges */
104 add_in_edge (b, x_then);
108 /* Generate the return node into current region. */
110 ir_node *in[1]; /* this is the array containing the return parameters */
111 in[0] = get_value(0, mode_i);
112 x = new_Return (get_store(), 1, in);
114 /* Now generate all instructions for this block and all its predecessor blocks
115 * so we can mature it. */
116 mature_block (get_irg_current_block(irg));
118 /* This adds the in edge of the end block which originates at the
119 return statement. The return node passes controlflow to the end block.*/
120 add_in_edge (get_irg_end_block(irg), x);
121 /* Now we can mature the end block as all it's predecessors are known. */
122 mature_block (get_irg_end_block(irg));
126 printf("Optimizing ...\n");
127 dead_node_elimination(irg);
129 /* verify the graph */
132 printf("Done building the graph. Dumping it.\n");
133 dump_ir_block_graph (irg);
134 printf("Use xvcg to view this graph:\n");
135 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");