1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
4 * Authors: Christian Schaefer, Goetz Lindenmaier
17 * das leere FIRM Programm
21 * This file constructs the ir for the following pseudo-program:
35 int main(int argc, char **argv)
38 ir_graph *irg; /* this variable contains the irgraph */
39 type *owner; /* the class in which this method is defined */
40 type *method; /* the type of this method */
41 entity *ent; /* represents this method as entity of owner */
42 ir_node *x, *x_then, *x_else, *c0, *c1, *c2, *cmpGt, *f, *t, *b;
44 printf("\ncreating an IR graph: IF_ELSE_EXAMPLE...\n");
49 /*** Make basic type information for primitive type int. ***/
50 prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_Is);
52 /* FIRM was designed for oo languages where all methods belong to a class.
53 * For imperative languages like C we view a file as a large class containing
54 * all functions as methods in this file.
55 * Therefore we define a class "IF_ELSE_EXAMPLE" with a method main as an
58 #define ENTITYNAME "IF_ELSE_EXAMPLE_main"
60 owner = get_glob_type();
61 method = new_type_method (id_from_str(ENTITYNAME, strlen(ENTITYNAME)), 0, 2);
62 set_method_res_type(method, 0, prim_t_int);
63 set_method_res_type(method, 1, prim_t_int);
65 ent = new_entity (owner, id_from_str (ENTITYNAME,
66 strlen(ENTITYNAME)), method);
68 /* Generates the basic graph for the method represented by entity ent, that
69 * is, generates start and end blocks and nodes and a first, initial block.
70 * The constructor needs to know how many local variables the method has.
72 #define NUM_OF_LOCAL_VARS 2
74 irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
76 /* Generate two constants */
77 c0 = new_Const (mode_Is, tarval_from_long (mode_Is, 0));
78 c1 = new_Const (mode_Is, tarval_from_long (mode_Is, 1));
80 /* set a and b to constants */
81 set_value (0, c0); /* this (0) is variable a */
82 set_value (1, c1); /* this (1) is variable b */
84 /* the expression that evaluates the condition */
85 c2 = new_Const (mode_Is, tarval_from_long (mode_Is, 2));
86 cmpGt = new_Proj(new_Cmp(get_value(0, mode_Is), c2), mode_b, Gt);
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, get_value(1, mode_Is));
102 /* generate and fill the else block */
105 set_value (1, new_Const (mode_Is, tarval_from_long (mode_Is, 2)));
109 /* generate the join block and add all cfg edges */
111 add_in_edge (b, x_then);
112 add_in_edge (b, x_else);
114 /* Generate the return node into current region. */
116 ir_node *in[2]; /* this is the array containing the return parameters */
117 in[0] = get_value(0, mode_Is);
118 in[1] = get_value(1, mode_Is);
119 x = new_Return (get_store(), 2, in);
121 /* Now generate all instructions for this block and all its predecessor
122 blocks so we can mature it. */
123 mature_block (get_irg_current_block(irg));
125 /* This adds the in edge of the end block which originates at the
126 return statement. The return node passes control flow to the
128 add_in_edge (get_irg_end_block(irg), x);
129 /* Now we can mature the end block as all it's predecessors are known. */
130 mature_block (get_irg_end_block(irg));
134 printf("\nOptimizing ...\n");
135 local_optimize_graph(irg);
136 dead_node_elimination(irg);
138 /* verify the graph */
142 printf("Done building the graph. Dumping it.\n");
143 dump_ir_block_graph (irg);
144 printf("use xvcg to view this graph:\n");
145 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");