b3ade18226c11b1b1c46bffad961862aa4b5a992
[libfirm] / testprograms / if_else_example.c
1 /*
2  * Project:     libFIRM
3  * File name:   testprograms/if_else_example.c
4  * Purpose:     Shows construction of if ... else control flow.
5  *              Tests Phi construction.
6  * Author:      Christian Schaefer, Goetz Lindenmaier
7  * Modified by:
8  * Created:
9  * CVS-ID:      $Id$
10  * Copyright:   (c) 1999-2003 Universität Karlsruhe
11  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
12  */
13
14 # include <stdio.h>
15 # include <string.h>
16
17 # include "irvrfy.h"
18 # include "firm.h"
19 # include "irdump.h"
20
21 /*
22  * das leere FIRM Programm
23  */
24
25 /**
26 *  This file constructs the ir for the following pseudo-program:
27 *
28 *  main() {
29 *    int a = 0;
30 *    int b = 1;
31 *
32 *    if (a > 2)
33 *      { a = b; }
34 *    else
35 *      { b = 2; }
36 *
37 *    return a, b;
38 **/
39
40 int main(int argc, char **argv)
41 {
42   type     *prim_t_int;
43   ir_graph *irg;       /* this variable contains the irgraph */
44   type     *owner;     /* the class in which this method is defined */
45   type     *method;    /* the type of this method */
46   entity   *ent;       /* represents this method as entity of owner */
47   ir_node  *x, *x_then, *x_else, *c0, *c1, *c2, *cmpGt, *f, *t, *b;
48
49   printf("\nCreating an IR graph: IF_ELSE_EXAMPLE...\n");
50
51   /* init library */
52   init_firm (NULL);
53
54   /*** Make basic type information for primitive type int. ***/
55   prim_t_int = new_type_primitive(new_id_from_chars ("int", 3), mode_Is);
56
57   /* FIRM was designed for oo languages where all methods belong to a class.
58    * For imperative languages like C we view a file as a large class containing
59    * all functions as methods in this file.
60    * Therefore we define a class "IF_ELSE_EXAMPLE" with a method main as an
61    * entity.
62    */
63 #define ENTITYNAME "IF_ELSE_EXAMPLE_main"
64
65   owner = get_glob_type();
66   method = new_type_method (new_id_from_chars(ENTITYNAME, strlen(ENTITYNAME)), 0, 2);
67   set_method_res_type(method, 0, prim_t_int);
68   set_method_res_type(method, 1, prim_t_int);
69
70   ent = new_entity (owner, new_id_from_chars (ENTITYNAME,
71                     strlen(ENTITYNAME)), method);
72
73   /* Generates the basic graph for the method represented by entity ent, that
74    * is, generates start and end blocks and nodes and a first, initial block.
75    * The constructor needs to know how many local variables the method has.
76    */
77 #define NUM_OF_LOCAL_VARS 2
78
79   irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
80
81   /* Generate two constants */
82   c0 = new_Const (mode_Is, new_tarval_from_long (0, mode_Is));
83   c1 = new_Const (mode_Is, new_tarval_from_long (1, mode_Is));
84
85   /* set a and b to constants */
86   set_value (0, c0);  /* this (0) is variable a */
87   set_value (1, c1);  /* this (1) is variable b */
88
89   /* the expression that evaluates the condition */
90   c2 = new_Const (mode_Is, new_tarval_from_long (2, mode_Is));
91   cmpGt = new_Proj(new_Cmp(get_value(0, mode_Is), c2), mode_b, Gt);
92
93   /* the conditional branch */
94   x = new_Cond (cmpGt);
95   f = new_Proj (x, mode_X, 0); /* if condition is false */
96   t = new_Proj (x, mode_X, 1); /* 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, get_value(1, mode_Is));
104   mature_immBlock (b);
105   x_then = new_Jmp ();
106
107   /* generate and fill the else block */
108   b = new_immBlock ();
109   add_immBlock_pred (b, f);
110   set_value (1, new_Const (mode_Is, new_tarval_from_long (2, mode_Is)));
111   mature_immBlock (b);
112   x_else = new_Jmp ();
113
114   /* generate the join block and add all cfg edges */
115   b = new_immBlock ();
116   add_immBlock_pred (b, x_then);
117   add_immBlock_pred (b, x_else);
118
119   /* Generate the return node into current region. */
120   {
121     ir_node *in[2]; /* this is the array containing the return parameters */
122     in[0] = get_value(0, mode_Is);
123     in[1] = get_value(1, mode_Is);
124     x = new_Return (get_store(), 2, in);
125   }
126   /* Now generate all instructions for this block and all its predecessor
127      blocks so we can mature it. */
128   mature_immBlock (get_irg_current_block(irg));
129
130   /* This adds the in edge of the end block which originates at the
131      return statement.  The return node passes control flow to the
132      end block.  */
133   add_immBlock_pred (get_irg_end_block(irg), x);
134   /* Now we can mature the end block as all it's predecessors are known. */
135   mature_immBlock (get_irg_end_block(irg));
136
137   finalize_cons (irg);
138
139   printf("Optimizing ...\n");
140   local_optimize_graph(irg);
141   dead_node_elimination(irg);
142
143   /* verify the graph */
144   irg_vrfy(irg);
145   finalize_cons (irg);
146
147   printf("Done building the graph.  Dumping it.\n");
148   dump_ir_block_graph (irg, 0);
149   printf("use xvcg to view this graph:\n");
150   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
151
152   return (0);
153 }