3 * File name: testprograms/cond_example.c
4 * Purpose: Shows how to represent boolean expressions.
5 * Author: Christian Schaefer, Goetz Lindenmaier
9 * Copyright: (c) 1999-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
22 * This file constructs the ir for the following pseudo-program:
25 * if ((a > 2) && (a < 10))
31 int main(int argc, char **argv)
34 ir_graph *irg; /* this variable contains the irgraph */
35 type *owner; /* the class in which this method is defined */
36 type *method; /* the type of this method */
37 entity *ent; /* represents this method as entity of owner */
38 ir_node *x, *x_then, *arg1, *c2, *c10, *cmpGt, *cmpLt, *and, *f, *t, *b;
40 printf("\nCreating an IR graph: COND_EXAMPLE...\n");
45 /*** Make basic type information for primitive type int. ***/
46 prim_t_int = new_type_primitive(new_id_from_chars ("int", 3), mode_Is);
48 /* FIRM was designed for oo languages where all methods belong to a class.
49 * For imperative languages like C we view a file as a large class containing
50 * all functions as methods in this file.
51 * Therefore we define a class "COND_EXAMPLE" with a method main as an
54 #define CLASSNAME "COND_EXAMPLE"
55 #define ENTITYNAME "main"
57 owner = new_type_class (new_id_from_chars (CLASSNAME, strlen(CLASSNAME)));
58 method = new_type_method (new_id_from_chars("main", 4), 1, 1);
59 set_method_param_type(method, 0, prim_t_int);
60 set_method_res_type(method, 0, prim_t_int);
61 ent = new_entity (owner, new_id_from_chars (ENTITYNAME, strlen(ENTITYNAME)), method);
62 get_entity_ld_name(ent);
65 /* Generates the basic graph for the method represented by entity ent, that
66 * is, generates start and end blocks and nodes and a first, initial block.
67 * The constructor needs to know how many local variables the method has.
69 #define NUM_OF_LOCAL_VARS 1
71 irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
73 /* get the first argument a of method main - see irgraph.h */
74 arg1 = new_Proj(get_irg_args(irg), mode_Is, 0);
76 /* arg1 as first first local variable - makes things simple */
79 /* the expression that evaluates the condition */
81 c2 = new_Const (mode_Is, new_tarval_from_long (2, mode_Is));
82 cmpGt = new_Proj(new_Cmp(get_value(0, mode_Is), c2), mode_b, pn_Cmp_Gt);
83 cmpGt = new_Conv(cmpGt, mode_Is);
86 c10 = new_Const (mode_Is, new_tarval_from_long (10, mode_Is));
87 cmpLt = new_Proj(new_Cmp(get_value(0, mode_Is), c10), mode_b, pn_Cmp_Lt);
88 cmpLt = new_Conv(cmpLt, mode_Is);
91 and = new_And(cmpGt, cmpLt, mode_Is);
92 /* compare result and 0 because we have no cast from integer to bool */
93 and = new_Cmp(and, new_Const (mode_Is, new_tarval_from_long (0, mode_Is)));
94 and = new_Proj(and, mode_b, pn_Cmp_Lg);
96 /* the conditional branch */
98 f = new_Proj (x, mode_X, 0); /* if condition is false */
99 t = new_Proj (x, mode_X, 1); /* if condition is true */
101 mature_immBlock (get_irg_current_block(irg));
103 /* generate and fill the then block */
105 add_immBlock_pred (b, t);
106 set_value (0, new_Const (mode_Is, new_tarval_from_long (1, mode_Is)));
110 /* generate the fall through block and add all cfg edges */
112 add_immBlock_pred (b, x_then);
113 add_immBlock_pred (b, f);
116 /* Generate the return node into current region. */
118 ir_node *in[1]; /* this is the array containing the return parameters */
119 in[0] = get_value(0, mode_Is);
120 x = new_Return (get_store(), 1, in);
122 /* Now generate all instructions for this block and all its predecessor blocks
123 * 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 controlflow to the end block.*/
128 add_immBlock_pred (get_irg_end_block(irg), x);
129 /* Now we can mature the end block as all it's predecessors are known. */
130 mature_immBlock (get_irg_end_block(irg));
132 irg_finalize_cons (irg);
134 printf("Optimizing ...\n");
135 dead_node_elimination(irg);
137 /* verify the graph */
140 printf("Done building the graph. Dumping it.\n");
141 dump_ir_block_graph (irg, 0);
142 printf("Use xvcg to view this graph:\n");
143 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");