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 Universität 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.idFromStr("int", 3);
50 int primIntType = Type.newTypePrimitive(primIntTypeName, Irmode.getModeIs());
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);
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);
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.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 */
84 Ircons.matureBlock (Irgraph.getIrgCurrentBlock(irg));
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 ();
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 ();
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);
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);
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));
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
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));
123 Ircons.finalizeCons (irg);
125 System.out.println("Done building the graph. Optimizing it.");
126 Irgopt.localOptimizeGraph(irg);
127 Irgopt.deadNodeElimination(irg);
129 Irdump.dumpIrBlockGraph (irg);
130 Irdump.dumpAllTypes();
132 System.out.println("use xvcg to view this graph:");
133 System.out.println("/ben/goetz/bin/xvcg GRAPHNAME\n");