Yep, I think this is a dangerous feature, so warn about it.
[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 Universitaet 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.newIdFromStr("int");
50         int primIntType = Type.newTypePrimitive(primIntTypeName, Irmode.getModeIs());
51
52         /* The type of the method */
53         int tpName = Ident.newIdFromStr("IF_ELSE_EXAMPLE_main_p");
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.newIdFromStr("IF_ELSE_EXAMPLE_main");
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.pn_Cmp_Gt);
80
81         int x = Ircons.newCond (cmpGt);
82         int f = Ircons.newProj (x, Irmode.getModeX(), 0); /* if condition is false */
83         int t = Ircons.newProj (x, Irmode.getModeX(), 1); /* if condition is true */
84
85         Ircons.matureImmBlock (Irgraph.getIrgCurrentBlock(irg));
86
87         /* generate and fill the then block */
88         int b = Ircons.newImmBlock ();
89         Ircons.addImmBlockPred (b, t);
90         Ircons.setValue (0, Ircons.getValue(1, Irmode.getModeIs()));
91         Ircons.matureImmBlock (b);
92         int x_then = Ircons.newJmp ();
93
94         /* generate and fill the else block */
95         b = Ircons.newImmBlock ();
96         Ircons.addImmBlockPred (b, f);
97         Ircons.setValue (1, Ircons.newConst (Irmode.getModeIs(),
98                                              Tv.newTarvalFromLong (2, Irmode.getModeIs())));
99         Ircons.matureImmBlock (b);
100         int x_else = Ircons.newJmp ();
101
102         /* generate the join block and add all cfg edges */
103         b = Ircons.newImmBlock ();
104         Ircons.addImmBlockPred (b, x_then);
105         Ircons.addImmBlockPred (b, x_else);
106
107         int[] in = new int[2]; /* this is the array containing the return parameters */
108         in[0] = Ircons.getValue(0, Irmode.getModeIs());
109         in[1] = Ircons.getValue(1, Irmode.getModeIs());
110         x = Ircons.newReturn (Ircons.getStore(), 2, in);
111
112         /* Now generate all instructions for this block and all its predecessor
113            blocks so we can mature it. */
114         Ircons.matureImmBlock (Irgraph.getIrgCurrentBlock(irg));
115
116         /* This adds the in edge of the end block which originates at the
117            return statement.  The return node passes control flow to the
118            end block.  */
119         Ircons.addImmBlockPred (Irgraph.getIrgEndBlock(irg), x);
120         /* Now we can mature the end block as all it's predecessors are known. */
121         Ircons.matureImmBlock (Irgraph.getIrgEndBlock(irg));
122
123         Irvrfy.irgVerify(irg, Irvrfy.VRFY_NORMAL);
124         Irgraph.setIrgPhaseState (irg, Irgraph.phase_high);
125
126         System.out.println("Done building the graph.  Optimizing it.");
127         Irgopt.localOptimizeGraph(irg);
128         Irgopt.deadNodeElimination(irg);
129
130         Irdump.dumpIrBlockGraph (irg, "");
131         Irdump.dumpAllTypes("");
132
133         System.out.println("use xvcg to view this graph:");
134         System.out.println("/ben/goetz/bin/xvcg GRAPHNAME\n");
135
136     }
137 }