Initial revision
[libfirm] / testprograms / cond_example.c
1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
3 **
4 ** Authors: Christian Schaefer, Goetz Lindenmaier
5 **
6 ** testprogram.
7 */
8
9 #include <stdio.h>
10
11 # include "irdump.h"
12 # include "firm.h"
13
14 /**
15 ***  This file constructs the ir for the following pseudo-program:
16 ***
17 ***  main(int a) {
18 ***    if ((a > 2) && (a < 10))
19 ***      { a = 1; }
20 ***
21 ***    return a;
22 **/
23
24 int main(int argc, char **argv)
25 {
26   ir_graph *irg;          /* this variable contains the irgraph */
27   type_class *owner;      /* the class in which this method is defined */
28   entity *ent;            /* represents this method as entity of owner */
29   ir_node *x, *x_then, *arg1, *c2, *c10, *cmpGt, *cmpLt, *and, *f, *t, *b;
30
31   printf("creating an IR graph: COND_EXAMPLE...\n");
32
33   /* init library */
34   init_firm ();
35
36   /* FIRM was designed for oo languages where all methods belong to a class.
37    * For imperative languages like C we view a file as a large class containing
38    * all functions as methods in this file.
39    * Therefore we define a class "COND_EXAMPLE" with a method main as an
40    * entity.
41    */
42 #define CLASSNAME "COND_EXAMPLE"
43 #define ENTITYNAME "main"
44
45   owner = new_type_class (id_from_str (CLASSNAME, strlen(CLASSNAME)));
46   ent = new_entity ((type *)owner, id_from_str (ENTITYNAME, strlen(ENTITYNAME)), NULL);
47
48
49   /* Generates the basic graph for the method represented by entity ent, that
50    * is, generates start and end blocks and nodes and a first, initial block.
51    * The constructor needs to know how many local variables the method has.
52    */
53 #define NUM_OF_LOCAL_VARS 1
54
55   irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
56
57   /* get the first argument a of method main - see irgraph.h */
58   arg1 = new_Proj(irg->args, mode_i, 0);
59
60   /* arg1 as first first local variable - makes things simple */
61   set_value(0, arg1);
62
63   /* the expression that evaluates the condition */
64   /* cmpGt = a > 2 */
65   c2 = new_Const (mode_i, tarval_from_long (mode_i, 2));
66   cmpGt = new_Proj(new_Cmp(get_value(0, mode_i), c2), mode_b, Gt);
67   cmpGt = new_Conv(cmpGt, mode_i);
68
69   /* cmpLt = a < 10 */
70   c10 = new_Const (mode_i, tarval_from_long (mode_i, 10));
71   cmpLt = new_Proj(new_Cmp(get_value(0, mode_i), c10), mode_b, Lt);
72   cmpLt = new_Conv(cmpLt, mode_i);
73
74   /* cmpGt && cmpLt */
75   and = new_And(cmpGt, cmpLt, mode_i);
76   /* compare result and 0 because we have no cast from integer to bool */
77   and = new_Cmp(and, new_Const (mode_i, tarval_from_long (mode_i, 0)));
78   and = new_Proj(and, mode_b, Ne);
79
80   /* the conditional branch */
81   x = new_Cond (and);
82   f = new_Proj (x, mode_X, 0); /* if condition is false */
83   t = new_Proj (x, mode_X, 1); /* if condition is true */
84
85   mature_block (irg->current_block);
86
87   /* generate and fill the then block */
88   b = new_Block ();
89   add_in_edge (b, t);
90   set_value (0, new_Const (mode_i, tarval_from_long (mode_i, 1)));
91   mature_block (b);
92   x_then = new_Jmp ();
93
94   /* generate the fall through block and add all cfg edges */
95   b = new_Block ();
96   add_in_edge (b, x_then);
97   add_in_edge (b, f);
98
99
100   /* Generate the return node into current region. */
101   {
102     ir_node *in[1]; /* this is the array containing the return parameters */
103     in[0] = get_value(0, mode_i);
104     x = new_Return (get_store(), 1, in);
105   }
106   /* Now generate all instructions for this block and all its predecessor blocks
107    * so we can mature it. */
108   mature_block (irg->current_block);
109
110   /* This adds the in edge of the end block which originates at the return statement.
111    * The return node passes controlflow to the end block.  */
112   add_in_edge (irg->end_block, x);
113   /* Now we can mature the end block as all it's predecessors are known. */
114   mature_block (irg->end_block);
115
116   printf("\nDone building the graph.  Dumping it.\n");
117   dump_ir_block_graph (irg);
118
119   printf("use xvcg to view this graph:\n");
120   printf("/ben/trapp/bin/i486/xvcg GRAPHNAME\n");
121
122   return (0);
123
124
125 }