removed entity_visited flag - use type_visited instead
[libfirm] / testprograms / if_else_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 "firm.h"
14 # include "irdump.h"
15
16 /*
17  * das leere FIRM Programm
18  */
19
20 /**
21 ***  This file constructs the ir for the following pseudo-program:
22 ***
23 ***  main() {
24 ***    int a = 0;
25 ***    int b = 1;
26 ***
27 ***    if (a > 2)
28 ***      { a = b; }
29 ***    else
30 ***      { b = 2; }
31 ***
32 ***    return a, b;
33 **/
34
35 int main(int argc, char **argv)
36 {
37   ir_graph *irg;       /* this variable contains the irgraph */
38   type     *owner;     /* the class in which this method is defined */
39   type     *method;    /* the type of this method */
40   entity   *ent;       /* represents this method as entity of owner */
41   ir_node  *x, *x_then, *x_else, *c0, *c1, *c2, *cmpGt, *f, *t, *b;
42
43   printf("\ncreating an IR graph: IF_ELSE_EXAMPLE...\n");
44
45   /* init library */
46   init_firm ();
47
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 "IF_ELSE_EXAMPLE" with a method main as an
52    * entity.
53    */
54 #define ENTITYNAME "main"
55
56   owner = get_glob_type();
57   method = new_type_method (id_from_str("main", 4), 0, 2);
58   ent = new_entity (owner, id_from_str (ENTITYNAME,
59                     strlen(ENTITYNAME)), method);
60
61   /* Generates the basic graph for the method represented by entity ent, that
62    * is, generates start and end blocks and nodes and a first, initial block.
63    * The constructor needs to know how many local variables the method has.
64    */
65 #define NUM_OF_LOCAL_VARS 2
66
67   irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
68
69   /* Generate two constants */
70   c0 = new_Const (mode_i, tarval_from_long (mode_i, 0));
71   c1 = new_Const (mode_i, tarval_from_long (mode_i, 1));
72
73   /* set a and b to constants */
74   set_value (0, c0);  /* this (0) is variable a */
75   set_value (1, c1);  /* this (1) is variable b */
76
77   /* the expression that evaluates the condition */
78   c2 = new_Const (mode_i, tarval_from_long (mode_i, 2));
79   cmpGt = new_Proj(new_Cmp(get_value(0, mode_i), c2), mode_b, Gt);
80
81   /* the conditional branch */
82   x = new_Cond (cmpGt);
83   f = new_Proj (x, mode_X, 0); /* if condition is false */
84   t = new_Proj (x, mode_X, 1); /* if condition is true */
85
86   mature_block (get_irg_current_block(irg));
87
88   /* generate and fill the then block */
89   b = new_immBlock ();
90   add_in_edge (b, t);
91   set_value (0, get_value(1, mode_i));
92   mature_block (b);
93   x_then = new_Jmp ();
94
95   /* generate and fill the else block */
96   b = new_immBlock ();
97   add_in_edge (b, f);
98   set_value (1, new_Const (mode_i, tarval_from_long (mode_i, 2)));
99   mature_block (b);
100   x_else = new_Jmp ();
101
102   /* generate the join block and add all cfg edges */
103   b = new_immBlock ();
104   add_in_edge (b, x_then);
105   add_in_edge (b, x_else);
106
107   /* Generate the return node into current region. */
108   {
109     ir_node *in[2]; /* this is the array containing the return parameters */
110     in[0] = get_value(0, mode_i);
111     in[1] = get_value(1, mode_i);
112     x = new_Return (get_store(), 2, in);
113   }
114   /* Now generate all instructions for this block and all its predecessor
115      blocks so we can mature it. */
116   mature_block (get_irg_current_block(irg));
117
118   /* This adds the in edge of the end block which originates at the
119      return statement.  The return node passes control flow to the
120      end block.  */
121   add_in_edge (get_irg_end_block(irg), x);
122   /* Now we can mature the end block as all it's predecessors are known. */
123   mature_block (get_irg_end_block(irg));
124
125   printf("\nOptimizing ...\n");
126   local_optimize_graph(irg);
127   dead_node_elimination(irg);
128
129   /* verify the graph */
130   irg_vrfy(irg);
131   finalize_cons (irg);
132
133   printf("Done building the graph.  Dumping it.\n");
134   dump_ir_block_graph (irg);
135   printf("use xvcg to view this graph:\n");
136   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
137
138   return (0);
139 }