First implementation of lowering for calls with compound return values
[libfirm] / testprograms / cond_example.c
1 /*
2  * Project:     libFIRM
3  * File name:   testprograms/cond_example.c
4  * Purpose:     Shows how to represent boolean expressions.
5  * Author:      Christian Schaefer, Goetz Lindenmaier
6  * Modified by:
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1999-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13
14 #include <stdio.h>
15 # include <string.h>
16
17 # include "irvrfy.h"
18 # include "irdump.h"
19 # include "firm.h"
20
21 /**
22 *  This file constructs the ir for the following pseudo-program:
23 *
24 *  main(int a) {
25 *    if ((a > 2) && (a < 10))
26 *      { a = 1; }
27 *
28 *    return a;
29 **/
30
31 int main(int argc, char **argv)
32 {
33   type     *prim_t_int;
34   ir_graph *irg;       /* this variable contains the irgraph */
35   type     *owner;     /* the class in which this method is defined */
36   type     *method;    /* the type of this method */
37   entity   *ent;       /* represents this method as entity of owner */
38   ir_node  *x, *x_then, *arg1, *c2, *c10, *cmpGt, *cmpLt, *and, *f, *t, *b;
39
40   printf("\nCreating an IR graph: COND_EXAMPLE...\n");
41
42   /* init library */
43   init_firm (NULL);
44
45   /*** Make basic type information for primitive type int. ***/
46   prim_t_int = new_type_primitive(new_id_from_chars ("int", 3), mode_Is);
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 "COND_EXAMPLE" with a method main as an
52    * entity.
53    */
54 #define CLASSNAME "COND_EXAMPLE"
55 #define ENTITYNAME "main"
56
57   owner = new_type_class (new_id_from_chars (CLASSNAME, strlen(CLASSNAME)));
58   method = new_type_method (new_id_from_chars("main", 4), 1, 1);
59   set_method_param_type(method, 0, prim_t_int);
60   set_method_res_type(method, 0, prim_t_int);
61   ent = new_entity (owner, new_id_from_chars (ENTITYNAME, strlen(ENTITYNAME)), method);
62   get_entity_ld_name(ent);
63
64
65   /* Generates the basic graph for the method represented by entity ent, that
66    * is, generates start and end blocks and nodes and a first, initial block.
67    * The constructor needs to know how many local variables the method has.
68    */
69 #define NUM_OF_LOCAL_VARS 1
70
71   irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
72
73   /* get the first argument a of method main - see irgraph.h */
74   arg1 = new_Proj(get_irg_args(irg), mode_Is, 0);
75
76   /* arg1 as first first local variable - makes things simple */
77   set_value(0, arg1);
78
79   /* the expression that evaluates the condition */
80   /* cmpGt = a > 2 */
81   c2 = new_Const (mode_Is, new_tarval_from_long (2, mode_Is));
82   cmpGt = new_Proj(new_Cmp(get_value(0, mode_Is), c2), mode_b, pn_Cmp_Gt);
83   cmpGt = new_Conv(cmpGt, mode_Is);
84
85   /* cmpLt = a < 10 */
86   c10 = new_Const (mode_Is, new_tarval_from_long (10, mode_Is));
87   cmpLt = new_Proj(new_Cmp(get_value(0, mode_Is), c10), mode_b, pn_Cmp_Lt);
88   cmpLt = new_Conv(cmpLt, mode_Is);
89
90   /* cmpGt && cmpLt */
91   and = new_And(cmpGt, cmpLt, mode_Is);
92   /* compare result and 0 because we have no cast from integer to bool */
93   and = new_Cmp(and, new_Const (mode_Is, new_tarval_from_long (0, mode_Is)));
94   and = new_Proj(and, mode_b, pn_Cmp_Lg);
95
96   /* the conditional branch */
97   x = new_Cond (and);
98   f = new_Proj (x, mode_X, 0); /* if condition is false */
99   t = new_Proj (x, mode_X, 1); /* if condition is true */
100
101   mature_immBlock (get_irg_current_block(irg));
102
103   /* generate and fill the then block */
104   b = new_immBlock ();
105   add_immBlock_pred (b, t);
106   set_value (0, new_Const (mode_Is, new_tarval_from_long (1, mode_Is)));
107   mature_immBlock (b);
108   x_then = new_Jmp ();
109
110   /* generate the fall through block and add all cfg edges */
111   b = new_immBlock ();
112   add_immBlock_pred (b, x_then);
113   add_immBlock_pred (b, f);
114
115
116   /* Generate the return node into current region. */
117   {
118     ir_node *in[1]; /* this is the array containing the return parameters */
119     in[0] = get_value(0, mode_Is);
120     x = new_Return (get_store(), 1, in);
121   }
122   /* Now generate all instructions for this block and all its predecessor blocks
123    * 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 controlflow to the end block.*/
128   add_immBlock_pred (get_irg_end_block(irg), x);
129   /* Now we can mature the end block as all it's predecessors are known. */
130   mature_immBlock (get_irg_end_block(irg));
131
132   irg_finalize_cons (irg);
133
134   printf("Optimizing ...\n");
135   dead_node_elimination(irg);
136
137   /* verify the graph */
138   irg_vrfy(irg);
139
140   printf("Done building the graph.  Dumping it.\n");
141   dump_ir_block_graph (irg, 0);
142   printf("Use xvcg to view this graph:\n");
143   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
144
145   return (0);
146 }