fild and fist can handle 16bit source/destination.
[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
18 #include <libfirm/firm.h>
19
20
21 /**
22  *  This file constructs the ir for the following pseudo-program:
23  *
24  *  main() {
25  *    int a = 0;
26  *    int b = 1;
27  *
28  *    if (a > 2)
29  *      { a = b; }
30  *    else
31  *      { b = 2; }
32  *
33  *    return a, b;
34  */
35
36 int main(void)
37 {
38   ir_type     *prim_t_int;
39   ir_graph *irg;       /* this variable contains the irgraph */
40   ir_type     *owner;     /* the class in which this method is defined */
41   ir_type     *method;    /* the ir_type of this method */
42   ir_entity   *ent;       /* represents this method as ir_entity of owner */
43   ir_node  *x, *x_then, *x_else, *c0, *c1, *c2, *cmpGt, *f, *t, *b;
44
45   printf("\nCreating an IR graph: IF_ELSE_EXAMPLE...\n");
46
47   /* init library */
48   init_firm(NULL);
49
50   /*** Make basic ir_type information for primitive ir_type int. ***/
51   prim_t_int = new_type_primitive(new_id_from_chars("int", 3), mode_Is);
52
53   /* FIRM was designed for oo languages where all methods belong to a class.
54    * For imperative languages like C we view a file as a large class containing
55    * all functions as methods in this file.
56    * Therefore we define a class "IF_ELSE_EXAMPLE" with a method main as an
57    * ir_entity.
58    */
59 #define ENTITYNAME "IF_ELSE_EXAMPLE_main"
60
61   owner = get_glob_type();
62   method = new_type_method(new_id_from_chars(ENTITYNAME, strlen(ENTITYNAME)), 0, 2);
63   set_method_res_type(method, 0, prim_t_int);
64   set_method_res_type(method, 1, prim_t_int);
65
66   ent = new_entity(owner, new_id_from_chars(ENTITYNAME,
67                     strlen(ENTITYNAME)), method);
68
69   /* Generates the basic graph for the method represented by ir_entity ent, that
70    * is, generates start and end blocks and nodes and a first, initial block.
71    * The constructor needs to know how many local variables the method has.
72    */
73 #define NUM_OF_LOCAL_VARS 2
74
75   irg = new_ir_graph(ent, NUM_OF_LOCAL_VARS);
76
77   /* Generate two constants */
78   c0 = new_Const(mode_Is, new_tarval_from_long(0, mode_Is));
79   c1 = new_Const(mode_Is, new_tarval_from_long(1, mode_Is));
80
81   /* set a and b to constants */
82   set_value(0, c0);  /* this (0) is variable a */
83   set_value(1, c1);  /* this (1) is variable b */
84
85   /* the expression that evaluates the condition */
86   c2 = new_Const(mode_Is, new_tarval_from_long(2, mode_Is));
87   cmpGt = new_Proj(new_Cmp(get_value(0, mode_Is), c2), mode_b, pn_Cmp_Gt);
88
89   /* the conditional branch */
90   x = new_Cond(cmpGt);
91   f = new_Proj(x, mode_X, pn_Cond_false); /* if condition is false */
92   t = new_Proj(x, mode_X, pn_Cond_true); /* if condition is true */
93
94   mature_immBlock(get_irg_current_block(irg));
95
96   /* generate and fill the then block */
97   b = new_immBlock();
98   add_immBlock_pred(b, t);
99   set_value(0, get_value(1, mode_Is));
100   mature_immBlock(b);
101   x_then = new_Jmp();
102
103   /* generate and fill the else block */
104   b = new_immBlock();
105   add_immBlock_pred(b, f);
106   set_value(1, new_Const(mode_Is, new_tarval_from_long(2, mode_Is)));
107   mature_immBlock(b);
108   x_else = new_Jmp();
109
110   /* generate the join block and add all cfg edges */
111   b = new_immBlock();
112   add_immBlock_pred(b, x_then);
113   add_immBlock_pred(b, x_else);
114
115   /* Generate the return node into current region. */
116   {
117     ir_node *in[2]; /* this is the array containing the return parameters */
118     in[0] = get_value(0, mode_Is);
119     in[1] = get_value(1, mode_Is);
120     x = new_Return(get_store(), 2, in);
121   }
122   /* Now generate all instructions for this block and all its predecessor
123      blocks so we can mature it. */
124   mature_immBlock(get_irg_current_block(irg));
125
126   /* This adds the in edge of the end block which originates at the
127      return statement.  The return node passes control flow to the
128      end block.  */
129   add_immBlock_pred(get_irg_end_block(irg), x);
130   /* Now we can mature the end block as all it's predecessors are known. */
131   mature_immBlock(get_irg_end_block(irg));
132
133   irg_finalize_cons(irg);
134
135   printf("Optimizing ...\n");
136   local_optimize_graph(irg);
137   dead_node_elimination(irg);
138
139   /* verify the graph */
140   irg_vrfy(irg);
141   irg_finalize_cons(irg);
142
143   printf("Done building the graph.  Dumping it.\n");
144   dump_ir_block_graph(irg, 0);
145   printf("Use ycomp to view this graph:\n");
146   printf("ycomp GRAPHNAME\n\n");
147
148   return 0;
149 }