Typo fixed.
[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 */
33         Firm.initFirm(0);
34         Dbginfo.dbgInit();
35
36         /** Build type information for the compilation unit. **/
37
38         /* FIRM was designed for oo languages where all methods belong to a class.
39          * For imperative languages like C we view a file as a large class
40          * containing all functions in this file as methods.
41          * This class is generated automatically.        */
42         int owner = Irprog.getGlobType();
43
44         /* Basic type information for primitive type int. */
45         int primIntTypeName = Ident.idFromStr("int", 3);
46         int primIntType = Type.newTypePrimitive(primIntTypeName, Irmode.getModeIs());
47
48         /* The type of the method */
49         int tpName = Ident.idFromStr("IF_ELSE_EXAMPLE_main_p", 23);
50         int procMain = Type.newTypeMethod(tpName, 0, 2);
51         Type.setMethodResType(procMain, 0, primIntType);
52         Type.setMethodResType(procMain, 1, primIntType);
53
54         /* An entity representing the method.  Owner of the entity is the global
55             class type mentioned above. */
56         int name = Ident.idFromStr("IF_ELSE_EXAMPLE_main", 20);
57         int ent = Entity.newEntity (owner, name, procMain);
58
59         /** Build code for the procedure. **/
60
61         int irg = Irgraph.newIrGraph (ent, 2);
62
63         /* Generate two constants */
64         int c0 = Ircons.newConst (Irmode.getModeIs(), Tv.newTarvalFromLong (0, Irmode.getModeIs()));
65         int c1 = Ircons.newConst (Irmode.getModeIs(), Tv.newTarvalFromLong (1, Irmode.getModeIs()));
66
67         /* Set a and b to constants */
68         Ircons.setValue (0, c0);  /* this (0) is variable a */
69         Ircons.setValue (1, c1);  /* this (1) is variable b */
70
71         /* The expression that evaluates the condition */
72         int c2 = Ircons.newConst(Irmode.getModeIs(),
73                                  Tv.newTarvalFromLong (2, Irmode.getModeIs()));
74         int cmpGt = Ircons.newProj(Ircons.newCmp(Ircons.getValue(0, Irmode.getModeIs()), c2),
75                                    Irmode.getModeb(), Irnode.Gt);
76         int x = Ircons.newCond (cmpGt);
77         int f = Ircons.newProj (x, Irmode.getModeX(), 0); /* if condition is false */
78         int t = Ircons.newProj (x, Irmode.getModeX(), 1); /* if condition is true */
79
80         Ircons.matureBlock (Irgraph.getIrgCurrentBlock(irg));
81
82         /* generate and fill the then block */
83         int b = Ircons.newImmBlock ();
84         Ircons.addInEdge (b, t);
85         Ircons.setValue (0, Ircons.getValue(1, Irmode.getModeIs()));
86         Ircons.matureBlock (b);
87         int x_then = Ircons.newJmp ();
88
89         /* generate and fill the else block */
90         b = Ircons.newImmBlock ();
91         Ircons.addInEdge (b, f);
92         Ircons.setValue (1, Ircons.newConst (Irmode.getModeIs(),
93                                              Tv.newTarvalFromLong (2, Irmode.getModeIs())));
94         Ircons.matureBlock (b);
95         int x_else = Ircons.newJmp ();
96
97         /* generate the join block and add all cfg edges */
98         b = Ircons.newImmBlock ();
99         Ircons.addInEdge (b, x_then);
100         Ircons.addInEdge (b, x_else);
101
102         int[] in = new int[2]; /* this is the array containing the return parameters */
103         in[0] = Ircons.getValue(0, Irmode.getModeIs());
104         in[1] = Ircons.getValue(1, Irmode.getModeIs());
105         x = Ircons.newReturn (Ircons.getStore(), 2, in);
106
107         /* Now generate all instructions for this block and all its predecessor
108            blocks so we can mature it. */
109         Ircons.matureBlock (Irgraph.getIrgCurrentBlock(irg));
110
111         /* This adds the in edge of the end block which originates at the
112            return statement.  The return node passes control flow to the
113            end block.  */
114         Ircons.addInEdge (Irgraph.getIrgEndBlock(irg), x);
115         /* Now we can mature the end block as all it's predecessors are known. */
116         Ircons.matureBlock (Irgraph.getIrgEndBlock(irg));
117
118         Irvrfy.irgVrfy(irg);
119         Ircons.finalizeCons (irg);
120
121         System.out.println("Done building the graph.  Optimizing it.");
122         Irgopt.localOptimizeGraph(irg);
123         Irgopt.deadNodeElimination(irg);
124
125         Irdump.dumpIrBlockGraph (irg);
126         Irdump.dumpAllTypes();
127
128         System.out.println("use xvcg to view this graph:");
129         System.out.println("/ben/goetz/bin/xvcg GRAPHNAME\n");
130
131     }
132 }