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...");
36 /** Build type information for the compilation unit. **/
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();
44 /* Basic type information for primitive type int. */
45 int primIntTypeName = Ident.idFromStr("int", 3);
46 int primIntType = Type.newTypePrimitive(primIntTypeName, Irmode.getModeIs());
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);
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);
59 /** Build code for the procedure. **/
61 int irg = Irgraph.newIrGraph (ent, 2);
63 /* Generate two constants */
64 int c0 = Ircons.newConst (Irmode.getModeIs(), Tv.tarvalFromLong (Irmode.getModeIs(), 0));
65 int c1 = Ircons.newConst (Irmode.getModeIs(), Tv.tarvalFromLong (Irmode.getModeIs(), 1));
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 */
71 /* The expression that evaluates the condition */
72 int c2 = Ircons.newConst(Irmode.getModeIs(),
73 Tv.tarvalFromLong (Irmode.getModeIs(), 2));
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 */
80 Ircons.matureBlock (Irgraph.getIrgCurrentBlock(irg));
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 ();
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.tarvalFromLong (Irmode.getModeIs(), 2)));
94 Ircons.matureBlock (b);
95 int x_else = Ircons.newJmp ();
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);
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);
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));
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
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));
119 Ircons.finalizeCons (irg);
121 System.out.println("Done building the graph. Optimizing it.");
122 Irgopt.localOptimizeGraph(irg);
123 Irgopt.deadNodeElimination(irg);
125 Irdump.dumpIrBlockGraph (irg);
126 Irdump.dumpAllTypes();
128 System.out.println("use xvcg to view this graph:");
129 System.out.println("/ben/goetz/bin/xvcg GRAPHNAME\n");