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.
16 #include <libfirm/firm.h>
19 * This file constructs the ir for the following pseudo-program:
22 * if ((a > 2) && (a < 10))
31 ir_graph *irg; /* this variable contains the irgraph */
32 ir_type *owner; /* the class in which this method is defined */
33 ir_type *method; /* the ir_type of this method */
34 ir_entity *ent; /* represents this method as ir_entity of owner */
35 ir_node *x, *x_then, *arg1, *c2, *c10, *cmpGt, *cmpLt, *and, *f, *t, *b;
37 printf("\nCreating an IR graph: COND_EXAMPLE...\n");
42 /*** Make basic ir_type information for primitive ir_type int. ***/
43 prim_t_int = new_type_primitive(new_id_from_chars("int", 3), mode_Is);
45 /* FIRM was designed for oo languages where all methods belong to a class.
46 * For imperative languages like C we view a file as a large class containing
47 * all functions as methods in this file.
48 * Therefore we define a class "COND_EXAMPLE" with a method main as an
51 #define CLASSNAME "COND_EXAMPLE"
52 #define ENTITYNAME "main"
54 owner = new_type_class(new_id_from_chars(CLASSNAME, strlen(CLASSNAME)));
55 method = new_type_method(new_id_from_chars("main", 4), 1, 1);
56 set_method_param_type(method, 0, prim_t_int);
57 set_method_res_type(method, 0, prim_t_int);
58 ent = new_entity(owner, new_id_from_chars(ENTITYNAME, strlen(ENTITYNAME)), method);
59 get_entity_ld_name(ent);
62 /* Generates the basic graph for the method represented by ir_entity ent, that
63 * is, generates start and end blocks and nodes and a first, initial block.
64 * The constructor needs to know how many local variables the method has.
66 #define NUM_OF_LOCAL_VARS 1
68 irg = new_ir_graph(ent, NUM_OF_LOCAL_VARS);
70 /* get the first argument a of method main - see irgraph.h */
71 arg1 = new_Proj(get_irg_args(irg), mode_Is, 0);
73 /* arg1 as first first local variable - makes things simple */
76 /* the expression that evaluates the condition */
78 c2 = new_Const(mode_Is, new_tarval_from_long(2, mode_Is));
79 cmpGt = new_Proj(new_Cmp(get_value(0, mode_Is), c2), mode_b, pn_Cmp_Gt);
80 cmpGt = new_Conv(cmpGt, mode_Is);
83 c10 = new_Const(mode_Is, new_tarval_from_long(10, mode_Is));
84 cmpLt = new_Proj(new_Cmp(get_value(0, mode_Is), c10), mode_b, pn_Cmp_Lt);
85 cmpLt = new_Conv(cmpLt, mode_Is);
88 and = new_And(cmpGt, cmpLt, mode_Is);
89 /* compare result and 0 because we have no cast from integer to bool */
90 and = new_Cmp(and, new_Const(mode_Is, new_tarval_from_long(0, mode_Is)));
91 and = new_Proj(and, mode_b, pn_Cmp_Lg);
93 /* the conditional branch */
95 f = new_Proj(x, mode_X, pn_Cond_false); /* if condition is false */
96 t = new_Proj(x, mode_X, pn_Cond_true); /* if condition is true */
98 mature_immBlock(get_irg_current_block(irg));
100 /* generate and fill the then block */
102 add_immBlock_pred(b, t);
103 set_value(0, new_Const(mode_Is, new_tarval_from_long(1, mode_Is)));
107 /* generate the fall through block and add all cfg edges */
109 add_immBlock_pred(b, x_then);
110 add_immBlock_pred(b, f);
113 /* Generate the return node into current region. */
115 ir_node *in[1]; /* this is the array containing the return parameters */
116 in[0] = get_value(0, mode_Is);
117 x = new_Return(get_store(), 1, in);
119 /* Now generate all instructions for this block and all its predecessor blocks
120 * so we can mature it. */
121 mature_immBlock(get_irg_current_block(irg));
123 /* This adds the in edge of the end block which originates at the
124 return statement. The return node passes controlflow to the end block.*/
125 add_immBlock_pred(get_irg_end_block(irg), x);
126 /* Now we can mature the end block as all it's predecessors are known. */
127 mature_immBlock(get_irg_end_block(irg));
129 irg_finalize_cons(irg);
131 printf("Optimizing ...\n");
132 dead_node_elimination(irg);
134 /* verify the graph */
137 printf("Done building the graph. Dumping it.\n");
138 dump_ir_block_graph(irg, 0);
139 printf("Use ycomp to view this graph:\n");
140 printf("ycomp GRAPHNAME\n\n");