changed so that vcg output is not overwritten
[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 # include <string.h>
11
12 # include "irvrfy.h"
13 # include "irdump.h"
14 # include "firm.h"
15
16 /**
17 *  This file constructs the ir for the following pseudo-program:
18 *
19 *  main(int a) {
20 *    if ((a > 2) && (a < 10))
21 *      { a = 1; }
22 *
23 *    return a;
24 **/
25
26 int main(int argc, char **argv)
27 {
28   type     *prim_t_int;
29   ir_graph *irg;       /* this variable contains the irgraph */
30   type     *owner;     /* the class in which this method is defined */
31   type     *method;    /* the type of this method */
32   entity   *ent;       /* represents this method as entity of owner */
33   ir_node  *x, *x_then, *arg1, *c2, *c10, *cmpGt, *cmpLt, *and, *f, *t, *b;
34
35   printf("\nCreating an IR graph: COND_EXAMPLE...\n");
36
37   /* init library */
38   init_firm ();
39
40   /*** Make basic type information for primitive type int. ***/
41   prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_Is);
42
43   /* FIRM was designed for oo languages where all methods belong to a class.
44    * For imperative languages like C we view a file as a large class containing
45    * all functions as methods in this file.
46    * Therefore we define a class "COND_EXAMPLE" with a method main as an
47    * entity.
48    */
49 #define CLASSNAME "COND_EXAMPLE"
50 #define ENTITYNAME "main"
51
52   owner = new_type_class (id_from_str (CLASSNAME, strlen(CLASSNAME)));
53   method = new_type_method (id_from_str("main", 4), 1, 1);
54   set_method_param_type(method, 0, prim_t_int);
55   set_method_res_type(method, 0, prim_t_int);
56   ent = new_entity (owner, id_from_str (ENTITYNAME, strlen(ENTITYNAME)), method);
57   get_entity_ld_name(ent);
58
59
60   /* Generates the basic graph for the method represented by entity ent, that
61    * is, generates start and end blocks and nodes and a first, initial block.
62    * The constructor needs to know how many local variables the method has.
63    */
64 #define NUM_OF_LOCAL_VARS 1
65
66   irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
67
68   /* get the first argument a of method main - see irgraph.h */
69   arg1 = new_Proj(get_irg_args(irg), mode_Is, 0);
70
71   /* arg1 as first first local variable - makes things simple */
72   set_value(0, arg1);
73
74   /* the expression that evaluates the condition */
75   /* cmpGt = a > 2 */
76   c2 = new_Const (mode_Is, tarval_from_long (mode_Is, 2));
77   cmpGt = new_Proj(new_Cmp(get_value(0, mode_Is), c2), mode_b, Gt);
78   cmpGt = new_Conv(cmpGt, mode_Is);
79
80   /* cmpLt = a < 10 */
81   c10 = new_Const (mode_Is, tarval_from_long (mode_Is, 10));
82   cmpLt = new_Proj(new_Cmp(get_value(0, mode_Is), c10), mode_b, Lt);
83   cmpLt = new_Conv(cmpLt, mode_Is);
84
85   /* cmpGt && cmpLt */
86   and = new_And(cmpGt, cmpLt, mode_Is);
87   /* compare result and 0 because we have no cast from integer to bool */
88   and = new_Cmp(and, new_Const (mode_Is, tarval_from_long (mode_Is, 0)));
89   and = new_Proj(and, mode_b, Ne);
90
91   /* the conditional branch */
92   x = new_Cond (and);
93   f = new_Proj (x, mode_X, 0); /* if condition is false */
94   t = new_Proj (x, mode_X, 1); /* if condition is true */
95
96   mature_block (get_irg_current_block(irg));
97
98   /* generate and fill the then block */
99   b = new_immBlock ();
100   add_in_edge (b, t);
101   set_value (0, new_Const (mode_Is, tarval_from_long (mode_Is, 1)));
102   mature_block (b);
103   x_then = new_Jmp ();
104
105   /* generate the fall through block and add all cfg edges */
106   b = new_immBlock ();
107   add_in_edge (b, x_then);
108   add_in_edge (b, f);
109
110
111   /* Generate the return node into current region. */
112   {
113     ir_node *in[1]; /* this is the array containing the return parameters */
114     in[0] = get_value(0, mode_Is);
115     x = new_Return (get_store(), 1, in);
116   }
117   /* Now generate all instructions for this block and all its predecessor blocks
118    * so we can mature it. */
119   mature_block (get_irg_current_block(irg));
120
121   /* This adds the in edge of the end block which originates at the
122      return statement. The return node passes controlflow to the end block.*/
123   add_in_edge (get_irg_end_block(irg), x);
124   /* Now we can mature the end block as all it's predecessors are known. */
125   mature_block (get_irg_end_block(irg));
126
127   finalize_cons (irg);
128
129   printf("Optimizing ...\n");
130   dead_node_elimination(irg);
131
132   /* verify the graph */
133   irg_vrfy(irg);
134
135   printf("Done building the graph.  Dumping it.\n");
136   dump_ir_block_graph (irg);
137   printf("Use xvcg to view this graph:\n");
138   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
139
140   return (0);
141 }