3 * File name: testprograms/if_else_example.c
4 * Purpose: Shows construction of if ... else control flow.
5 * Tests Phi construction.
6 * Author: Christian Schaefer, Goetz Lindenmaier
10 * Copyright: (c) 1999-2003 Universität Karlsruhe
11 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
22 * das leere FIRM Programm
26 * This file constructs the ir for the following pseudo-program:
40 int main(int argc, char **argv)
43 ir_graph *irg; /* this variable contains the irgraph */
44 type *owner; /* the class in which this method is defined */
45 type *method; /* the type of this method */
46 entity *ent; /* represents this method as entity of owner */
47 ir_node *x, *x_then, *x_else, *c0, *c1, *c2, *cmpGt, *f, *t, *b;
49 printf("\nCreating an IR graph: IF_ELSE_EXAMPLE...\n");
54 /*** Make basic type information for primitive type int. ***/
55 prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_Is);
57 /* FIRM was designed for oo languages where all methods belong to a class.
58 * For imperative languages like C we view a file as a large class containing
59 * all functions as methods in this file.
60 * Therefore we define a class "IF_ELSE_EXAMPLE" with a method main as an
63 #define ENTITYNAME "IF_ELSE_EXAMPLE_main"
65 owner = get_glob_type();
66 method = new_type_method (id_from_str(ENTITYNAME, strlen(ENTITYNAME)), 0, 2);
67 set_method_res_type(method, 0, prim_t_int);
68 set_method_res_type(method, 1, prim_t_int);
70 ent = new_entity (owner, id_from_str (ENTITYNAME,
71 strlen(ENTITYNAME)), method);
73 /* Generates the basic graph for the method represented by entity ent, that
74 * is, generates start and end blocks and nodes and a first, initial block.
75 * The constructor needs to know how many local variables the method has.
77 #define NUM_OF_LOCAL_VARS 2
79 irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
81 /* Generate two constants */
82 c0 = new_Const (mode_Is, new_tarval_from_long (0, mode_Is));
83 c1 = new_Const (mode_Is, new_tarval_from_long (1, mode_Is));
85 /* set a and b to constants */
86 set_value (0, c0); /* this (0) is variable a */
87 set_value (1, c1); /* this (1) is variable b */
89 /* the expression that evaluates the condition */
90 c2 = new_Const (mode_Is, new_tarval_from_long (2, mode_Is));
91 cmpGt = new_Proj(new_Cmp(get_value(0, mode_Is), c2), mode_b, Gt);
93 /* the conditional branch */
95 f = new_Proj (x, mode_X, 0); /* if condition is false */
96 t = new_Proj (x, mode_X, 1); /* if condition is true */
98 mature_block (get_irg_current_block(irg));
100 /* generate and fill the then block */
103 set_value (0, get_value(1, mode_Is));
107 /* generate and fill the else block */
110 set_value (1, new_Const (mode_Is, new_tarval_from_long (2, mode_Is)));
114 /* generate the join block and add all cfg edges */
116 add_in_edge (b, x_then);
117 add_in_edge (b, x_else);
119 /* Generate the return node into current region. */
121 ir_node *in[2]; /* this is the array containing the return parameters */
122 in[0] = get_value(0, mode_Is);
123 in[1] = get_value(1, mode_Is);
124 x = new_Return (get_store(), 2, in);
126 /* Now generate all instructions for this block and all its predecessor
127 blocks so we can mature it. */
128 mature_block (get_irg_current_block(irg));
130 /* This adds the in edge of the end block which originates at the
131 return statement. The return node passes control flow to the
133 add_in_edge (get_irg_end_block(irg), x);
134 /* Now we can mature the end block as all it's predecessors are known. */
135 mature_block (get_irg_end_block(irg));
139 printf("Optimizing ...\n");
140 local_optimize_graph(irg);
141 dead_node_elimination(irg);
143 /* verify the graph */
147 printf("Done building the graph. Dumping it.\n");
148 dump_ir_block_graph (irg);
149 printf("use xvcg to view this graph:\n");
150 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");