cf3bb66621dc0a529718a46ef9516566c61fd30e
[libfirm] / firmjni / testprograms / IfElseExample.java
1 /*
2  * Project:     libFIRM
3  * File name:   firmjni/testprograms/IfElseExample.java
4  * Purpose:     This is an example of how to use the JNI interface of Firm.
5  * Author:      Goetz Lindenmaier
6  * Modified by:
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2002 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 import firmjni.*;
14
15 /**
16 *  This file constructs the ir for the following pseudo-program:
17 *
18 *  main() {
19 *    int a = 0;
20 *    int b = 1;
21 *
22 *    if (a > 2)
23 *      { a = b; }
24 *    else
25 *      { b = 2; }
26 *
27 *    return a, b;
28 **/
29
30 class IfElseExample {
31
32     public static void main (String[] args) {
33
34         System.out.println("\nCreating an IR graph: IfElseExample...");
35
36         /* init library */
37         Firm.initFirm(0);
38         Dbginfo.dbgInit();
39
40         /** Build type information for the compilation unit. **/
41
42         /* FIRM was designed for oo languages where all methods belong to a class.
43          * For imperative languages like C we view a file as a large class
44          * containing all functions in this file as methods.
45          * This class is generated automatically.        */
46         int owner = Irprog.getGlobType();
47
48         /* Basic type information for primitive type int. */
49         int primIntTypeName = Ident.idFromStr("int", 3);
50         int primIntType = Type.newTypePrimitive(primIntTypeName, Irmode.getModeIs());
51
52         /* The type of the method */
53         int tpName = Ident.idFromStr("IF_ELSE_EXAMPLE_main_p", 23);
54         int procMain = Type.newTypeMethod(tpName, 0, 2);
55         Type.setMethodResType(procMain, 0, primIntType);
56         Type.setMethodResType(procMain, 1, primIntType);
57
58         /* An entity representing the method.  Owner of the entity is the global
59             class type mentioned above. */
60         int name = Ident.idFromStr("IF_ELSE_EXAMPLE_main", 20);
61         int ent = Entity.newEntity (owner, name, procMain);
62
63         /** Build code for the procedure. **/
64
65         int irg = Irgraph.newIrGraph (ent, 2);
66
67         /* Generate two constants */
68         int c0 = Ircons.newConst (Irmode.getModeIs(), Tv.newTarvalFromLong (0, Irmode.getModeIs()));
69         int c1 = Ircons.newConst (Irmode.getModeIs(), Tv.newTarvalFromLong (1, Irmode.getModeIs()));
70
71         /* Set a and b to constants */
72         Ircons.setValue (0, c0);  /* this (0) is variable a */
73         Ircons.setValue (1, c1);  /* this (1) is variable b */
74
75         /* The expression that evaluates the condition */
76         int c2 = Ircons.newConst(Irmode.getModeIs(),
77                                  Tv.newTarvalFromLong (2, Irmode.getModeIs()));
78         int cmpGt = Ircons.newProj(Ircons.newCmp(Ircons.getValue(0, Irmode.getModeIs()), c2),
79                                    Irmode.getModeb(), Irnode.Gt);
80         int x = Ircons.newCond (cmpGt);
81         int f = Ircons.newProj (x, Irmode.getModeX(), 0); /* if condition is false */
82         int t = Ircons.newProj (x, Irmode.getModeX(), 1); /* if condition is true */
83
84         Ircons.matureBlock (Irgraph.getIrgCurrentBlock(irg));
85
86         /* generate and fill the then block */
87         int b = Ircons.newImmBlock ();
88         Ircons.addInEdge (b, t);
89         Ircons.setValue (0, Ircons.getValue(1, Irmode.getModeIs()));
90         Ircons.matureBlock (b);
91         int x_then = Ircons.newJmp ();
92
93         /* generate and fill the else block */
94         b = Ircons.newImmBlock ();
95         Ircons.addInEdge (b, f);
96         Ircons.setValue (1, Ircons.newConst (Irmode.getModeIs(),
97                                              Tv.newTarvalFromLong (2, Irmode.getModeIs())));
98         Ircons.matureBlock (b);
99         int x_else = Ircons.newJmp ();
100
101         /* generate the join block and add all cfg edges */
102         b = Ircons.newImmBlock ();
103         Ircons.addInEdge (b, x_then);
104         Ircons.addInEdge (b, x_else);
105
106         int[] in = new int[2]; /* this is the array containing the return parameters */
107         in[0] = Ircons.getValue(0, Irmode.getModeIs());
108         in[1] = Ircons.getValue(1, Irmode.getModeIs());
109         x = Ircons.newReturn (Ircons.getStore(), 2, in);
110
111         /* Now generate all instructions for this block and all its predecessor
112            blocks so we can mature it. */
113         Ircons.matureBlock (Irgraph.getIrgCurrentBlock(irg));
114
115         /* This adds the in edge of the end block which originates at the
116            return statement.  The return node passes control flow to the
117            end block.  */
118         Ircons.addInEdge (Irgraph.getIrgEndBlock(irg), x);
119         /* Now we can mature the end block as all it's predecessors are known. */
120         Ircons.matureBlock (Irgraph.getIrgEndBlock(irg));
121
122         Irvrfy.irgVrfy(irg);
123         Ircons.finalizeCons (irg);
124
125         System.out.println("Done building the graph.  Optimizing it.");
126         Irgopt.localOptimizeGraph(irg);
127         Irgopt.deadNodeElimination(irg);
128
129         Irdump.dumpIrBlockGraph (irg);
130         Irdump.dumpAllTypes();
131
132         System.out.println("use xvcg to view this graph:");
133         System.out.println("/ben/goetz/bin/xvcg GRAPHNAME\n");
134
135     }
136 }