1 /* Copyright (C) 2003 by Universitaet Karlsruhe
4 * Authors: Goetz Lindenmaier
6 * testprogram for Java native interface
12 * This file constructs the ir for the following pseudo-program:
28 public static void main (String[] args) {
30 System.out.println("\nCreating an IR graph: IfElseExample...");
32 /* init library: Java did not support the callback, so ALWAYS use 0 here */
35 /** Build type information for the compilation unit. **/
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();
43 /* Basic type information for primitive type int. */
44 int primIntTypeName = Ident.idFromStr("int", 3);
45 int primIntType = Type.newTypePrimitive(primIntTypeName, Irmode.getModeIs());
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);
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);
58 /** Build code for the procedure. **/
60 int irg = Irgraph.newIrGraph (ent, 2);
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));
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 */
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 */
79 Ircons.matureBlock (Irgraph.getIrgCurrentBlock(irg));
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 ();
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 ();
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);
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);
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));
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
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));
118 Ircons.finalizeCons (irg);
120 System.out.println("Done building the graph. Optimizing it.");
121 Irgopt.localOptimizeGraph(irg);
122 Irgopt.deadNodeElimination(irg);
124 Irdump.dumpIrBlockGraph (irg);
125 Irdump.dumpAllTypes();
127 System.out.println("use xvcg to view this graph:");
128 System.out.println("/ben/goetz/bin/xvcg GRAPHNAME\n");