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