Clean up need_constraint_copy().
[libfirm] / testprograms / cond_example.c
1 /*
2  * Project:     libFIRM
3  * File name:   testprograms/cond_example.c
4  * Purpose:     Shows how to represent boolean expressions.
5  * Author:      Christian Schaefer, Goetz Lindenmaier
6  * Modified by:
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1999-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #include <stdio.h>
14 #include <string.h>
15
16 #include <libfirm/firm.h>
17
18 /**
19 *  This file constructs the ir for the following pseudo-program:
20 *
21 *  main(int a) {
22 *    if ((a > 2) && (a < 10))
23 *      { a = 1; }
24 *
25 *    return a;
26 **/
27
28 int main(void)
29 {
30   ir_type     *prim_t_int;
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;
36
37   printf("\nCreating an IR graph: COND_EXAMPLE...\n");
38
39   /* init library */
40   init_firm(NULL);
41
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);
44
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
49    * ir_entity.
50    */
51 #define CLASSNAME "COND_EXAMPLE"
52 #define ENTITYNAME "main"
53
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);
60
61
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.
65    */
66 #define NUM_OF_LOCAL_VARS 1
67
68   irg = new_ir_graph(ent, NUM_OF_LOCAL_VARS);
69
70   /* get the first argument a of method main - see irgraph.h */
71   arg1 = new_Proj(get_irg_args(irg), mode_Is, 0);
72
73   /* arg1 as first first local variable - makes things simple */
74   set_value(0, arg1);
75
76   /* the expression that evaluates the condition */
77   /* cmpGt = a > 2 */
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);
81
82   /* cmpLt = a < 10 */
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);
86
87   /* cmpGt && cmpLt */
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);
92
93   /* the conditional branch */
94   x = new_Cond(and);
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 */
97
98   mature_immBlock(get_irg_current_block(irg));
99
100   /* generate and fill the then block */
101   b = new_immBlock();
102   add_immBlock_pred(b, t);
103   set_value(0, new_Const(mode_Is, new_tarval_from_long(1, mode_Is)));
104   mature_immBlock(b);
105   x_then = new_Jmp();
106
107   /* generate the fall through block and add all cfg edges */
108   b = new_immBlock();
109   add_immBlock_pred(b, x_then);
110   add_immBlock_pred(b, f);
111
112
113   /* Generate the return node into current region. */
114   {
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);
118   }
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));
122
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));
128
129   irg_finalize_cons(irg);
130
131   printf("Optimizing ...\n");
132   dead_node_elimination(irg);
133
134   /* verify the graph */
135   irg_vrfy(irg);
136
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");
141
142   return 0;
143 }