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 * This file constructs the ir for the following pseudo-program:
36 int main(int argc, char **argv)
39 ir_graph *irg; /* this variable contains the irgraph */
40 type *owner; /* the class in which this method is defined */
41 type *method; /* the type of this method */
42 entity *ent; /* represents this method as entity of owner */
43 ir_node *x, *x_then, *x_else, *c0, *c1, *c2, *cmpGt, *f, *t, *b;
45 printf("\nCreating an IR graph: IF_ELSE_EXAMPLE...\n");
50 /*** Make basic type information for primitive type int. ***/
51 prim_t_int = new_type_primitive(new_id_from_chars ("int", 3), mode_Is);
53 /* FIRM was designed for oo languages where all methods belong to a class.
54 * For imperative languages like C we view a file as a large class containing
55 * all functions as methods in this file.
56 * Therefore we define a class "IF_ELSE_EXAMPLE" with a method main as an
59 #define ENTITYNAME "IF_ELSE_EXAMPLE_main"
61 owner = get_glob_type();
62 method = new_type_method (new_id_from_chars(ENTITYNAME, strlen(ENTITYNAME)), 0, 2);
63 set_method_res_type(method, 0, prim_t_int);
64 set_method_res_type(method, 1, prim_t_int);
66 ent = new_entity (owner, new_id_from_chars (ENTITYNAME,
67 strlen(ENTITYNAME)), method);
69 /* Generates the basic graph for the method represented by entity ent, that
70 * is, generates start and end blocks and nodes and a first, initial block.
71 * The constructor needs to know how many local variables the method has.
73 #define NUM_OF_LOCAL_VARS 2
75 irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
77 /* Generate two constants */
78 c0 = new_Const (mode_Is, new_tarval_from_long (0, mode_Is));
79 c1 = new_Const (mode_Is, new_tarval_from_long (1, mode_Is));
81 /* set a and b to constants */
82 set_value (0, c0); /* this (0) is variable a */
83 set_value (1, c1); /* this (1) is variable b */
85 /* the expression that evaluates the condition */
86 c2 = new_Const (mode_Is, new_tarval_from_long (2, mode_Is));
87 cmpGt = new_Proj(new_Cmp(get_value(0, mode_Is), c2), mode_b, pn_Cmp_Gt);
89 /* the conditional branch */
91 f = new_Proj (x, mode_X, 0); /* if condition is false */
92 t = new_Proj (x, mode_X, 1); /* if condition is true */
94 mature_immBlock (get_irg_current_block(irg));
96 /* generate and fill the then block */
98 add_immBlock_pred (b, t);
99 set_value (0, get_value(1, mode_Is));
103 /* generate and fill the else block */
105 add_immBlock_pred (b, f);
106 set_value (1, new_Const (mode_Is, new_tarval_from_long (2, mode_Is)));
110 /* generate the join block and add all cfg edges */
112 add_immBlock_pred (b, x_then);
113 add_immBlock_pred (b, x_else);
115 /* Generate the return node into current region. */
117 ir_node *in[2]; /* this is the array containing the return parameters */
118 in[0] = get_value(0, mode_Is);
119 in[1] = get_value(1, mode_Is);
120 x = new_Return (get_store(), 2, in);
122 /* Now generate all instructions for this block and all its predecessor
123 blocks so we can mature it. */
124 mature_immBlock (get_irg_current_block(irg));
126 /* This adds the in edge of the end block which originates at the
127 return statement. The return node passes control flow to the
129 add_immBlock_pred (get_irg_end_block(irg), x);
130 /* Now we can mature the end block as all it's predecessors are known. */
131 mature_immBlock (get_irg_end_block(irg));
133 irg_finalize_cons (irg);
135 printf("Optimizing ...\n");
136 local_optimize_graph(irg);
137 dead_node_elimination(irg);
139 /* verify the graph */
141 irg_finalize_cons (irg);
143 printf("Done building the graph. Dumping it.\n");
144 dump_ir_block_graph (irg, 0);
145 printf("use xvcg to view this graph:\n");
146 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");