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
9 * Copyright: (c) 2002 Universitaet Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
16 * This file constructs the ir for the following pseudo-program:
32 public static void main (String[] args) {
34 System.out.println("\nCreating an IR graph: IfElseExample...");
40 /** Build type information for the compilation unit. **/
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();
48 /* Basic type information for primitive type int. */
49 int primIntTypeName = Ident.newIdFromStr("int");
50 int primIntType = Type.newTypePrimitive(primIntTypeName, Irmode.getModeIs());
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);
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);
63 /** Build code for the procedure. **/
65 int irg = Irgraph.newIrGraph (ent, 2);
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()));
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 */
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);
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 */
85 Ircons.matureImmBlock (Irgraph.getIrgCurrentBlock(irg));
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 ();
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 ();
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);
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);
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));
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
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));
123 Irvrfy.irgVerify(irg, Irvrfy.VRFY_NORMAL);
124 Ircons.irgFinalizeCons (irg);
126 System.out.println("Done building the graph. Optimizing it.");
127 Irgopt.localOptimizeGraph(irg);
128 Irgopt.deadNodeElimination(irg);
130 Irdump.dumpIrBlockGraph (irg, "");
131 Irdump.dumpAllTypes("");
133 System.out.println("use xvcg to view this graph:");
134 System.out.println("/ben/goetz/bin/xvcg GRAPHNAME\n");