2e66e0a66c48fa07eade0deaf7b041ab91cddc8e
[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
11 # include "irdump.h"
12 # include "firm.h"
13
14 /*
15  * das leere FIRM Programm
16  */
17
18 /**
19 ***  This file constructs the ir for the following pseudo-program:
20 ***
21 ***  main() {
22 ***    int a = 0;
23 ***    int b = 1;
24 ***
25 ***    if (a > 2)
26 ***      { a = b; }
27 ***    else
28 ***      { b = 2; }
29 ***
30 ***    return a, b;
31 **/
32
33 int main(int argc, char **argv)
34 {
35   ir_graph *irg;          /* this variable contains the irgraph */
36   type_class *owner;      /* the class in which this method is defined */
37   type_method *method;    /* the type of this method */
38   entity *ent;            /* represents this method as entity of owner */
39   ir_node *x, *x_then, *x_else, *c0, *c1, *c2, *cmpGt, *f, *t, *b;
40
41   printf("creating an IR graph: IF_ELSE_EXAMPLE...\n");
42
43   /* init library */
44   init_firm ();
45
46   /* FIRM was designed for oo languages where all methods belong to a class.
47    * For imperative languages like C we view a file as a large class containing
48    * all functions as methods in this file.
49    * Therefore we define a class "IF_ELSE_EXAMPLE" with a method main as an
50    * entity.
51    */
52 #define CLASSNAME "IF_ELSE_EXAMPLE"
53 #define ENTITYNAME "main"
54
55   owner = new_type_class (id_from_str (CLASSNAME, strlen(CLASSNAME)));
56   method = new_type_method (id_from_str("main", 4), 0, 2);
57   ent = new_entity ((type *)owner, id_from_str (ENTITYNAME, strlen(ENTITYNAME)), (type *)method);
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 2
65
66   irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
67
68   /* Generate two constants */
69   c0 = new_Const (mode_i, tarval_from_long (mode_i, 0));
70   c1 = new_Const (mode_i, tarval_from_long (mode_i, 1));
71
72   /* set a and b to constants */
73   set_value (0, c0);  /* this (0) is variable a */
74   set_value (1, c1);  /* this (1) is variable b */
75
76   /* the expression that evaluates the condition */
77   c2 = new_Const (mode_i, tarval_from_long (mode_i, 2));
78   cmpGt = new_Proj(new_Cmp(get_value(0, mode_i), c2), mode_b, Gt);
79
80   /* the conditional branch */
81   x = new_Cond (cmpGt);
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, get_value(1, mode_i));
91   mature_block (b);
92   x_then = new_Jmp ();
93
94   /* generate and fill the else block */
95   b = new_Block ();
96   add_in_edge (b, f);
97   set_value (1, new_Const (mode_i, tarval_from_long (mode_i, 2)));
98   mature_block (b);
99   x_else = new_Jmp ();
100
101   /* generate the join block and add all cfg edges */
102   b = new_Block ();
103   add_in_edge (b, x_then);
104   add_in_edge (b, x_else);
105
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 blocks
115    * so we can mature it. */
116   mature_block (irg->current_block);
117
118   /* This adds the in edge of the end block which originates at the return statement.
119    * The return node passes controlflow to the end block.  */
120   add_in_edge (irg->end_block, x);
121   /* Now we can mature the end block as all it's predecessors are known. */
122   mature_block (irg->end_block);
123
124   /* verify the graph */
125   irg_vrfy(irg);
126
127   printf("\nDone building the graph.  Dumping it.\n");
128   dump_ir_block_graph (irg);
129
130   printf("use xvcg to view this graph:\n");
131   printf("/ben/goetz/bin/xvcg GRAPHNAME\n");
132
133   return (0);
134 }