New testprogram
authorGötz Lindenmaier <goetz@ipd.info.uni-karlsruhe.de>
Wed, 26 Feb 2003 11:01:40 +0000 (11:01 +0000)
committerGötz Lindenmaier <goetz@ipd.info.uni-karlsruhe.de>
Wed, 26 Feb 2003 11:01:40 +0000 (11:01 +0000)
[r833]

firmjni/testprograms/Empty.java
firmjni/testprograms/IfElseExample.java [new file with mode: 0644]
firmjni/testprograms/Makefile.in

index a3a1474..c9c4108 100644 (file)
@@ -39,7 +39,7 @@ class Empty {
        int owner = Irprog.getGlobType();
 
        /* The type of the method */
-       int name = Ident.idFromStr("main", 4);
+       int name = Ident.idFromStr("EMPTY_main", 10);
        int proc_main = Type.newTypeMethod(name, 0, 0);
        /* An entity representing the method.  Owner of the entity is the global
           class type mentioned above. */
diff --git a/firmjni/testprograms/IfElseExample.java b/firmjni/testprograms/IfElseExample.java
new file mode 100644 (file)
index 0000000..4696acf
--- /dev/null
@@ -0,0 +1,131 @@
+/* Copyright (C) 2003 by Universitaet Karlsruhe
+* All rights reserved.
+*
+* Authors: Goetz Lindenmaier
+*
+* testprogram for Java native interface
+*/
+
+import firmjni.*;
+
+/**
+*  This file constructs the ir for the following pseudo-program:
+*
+*  main() {
+*    int a = 0;
+*    int b = 1;
+*
+*    if (a > 2)
+*      { a = b; }
+*    else
+*      { b = 2; }
+*
+*    return a, b;
+**/
+
+class IfElseExample {
+
+    public static void main (String[] args) {
+
+       System.out.println("\nCreating an IR graph: IfElseExample...");
+
+       /* init library */
+       Firm.initFirm();
+
+       /** Build type information for the compilation unit. **/
+
+       /* FIRM was designed for oo languages where all methods belong to a class.
+        * For imperative languages like C we view a file as a large class
+        * containing all functions in this file as methods.
+        * This class is generated automatically.        */
+       int owner = Irprog.getGlobType();
+
+       /* Basic type information for primitive type int. */
+       int primIntTypeName = Ident.idFromStr("int", 3);
+       int primIntType = Type.newTypePrimitive(primIntTypeName, Irmode.getModeIs());
+
+       /* The type of the method */
+       int tpName = Ident.idFromStr("IF_ELSE_EXAMPLE_main_p", 23);
+       int procMain = Type.newTypeMethod(tpName, 0, 2);
+       Type.setMethodResType(procMain, 0, primIntType);
+       Type.setMethodResType(procMain, 1, primIntType);
+
+       /* An entity representing the method.  Owner of the entity is the global
+           class type mentioned above. */
+       int name = Ident.idFromStr("IF_ELSE_EXAMPLE_main", 20);
+       int ent = Entity.newEntity (owner, name, procMain);
+
+       /** Build code for the procedure. **/
+
+       int irg = Irgraph.newIrGraph (ent, 2);
+
+       /* Generate two constants */
+       int c0 = Ircons.newConst (Irmode.getModeIs(), Tv.tarvalFromLong (Irmode.getModeIs(), 0));
+       int c1 = Ircons.newConst (Irmode.getModeIs(), Tv.tarvalFromLong (Irmode.getModeIs(), 1));
+
+       /* Set a and b to constants */
+       Ircons.setValue (0, c0);  /* this (0) is variable a */
+       Ircons.setValue (1, c1);  /* this (1) is variable b */
+
+       /* The expression that evaluates the condition */
+       int c2 = Ircons.newConst(Irmode.getModeIs(),
+                                Tv.tarvalFromLong (Irmode.getModeIs(), 2));
+       int cmpGt = Ircons.newProj(Ircons.newCmp(Ircons.getValue(0, Irmode.getModeIs()), c2),
+                                  Irmode.getModeb(), Irnode.Gt);
+       int x = Ircons.newCond (cmpGt);
+       int f = Ircons.newProj (x, Irmode.getModeX(), 0); /* if condition is false */
+       int t = Ircons.newProj (x, Irmode.getModeX(), 1); /* if condition is true */
+
+       Ircons.matureBlock (Irgraph.getIrgCurrentBlock(irg));
+
+       /* generate and fill the then block */
+       int b = Ircons.newImmBlock ();
+       Ircons.addInEdge (b, t);
+       Ircons.setValue (0, Ircons.getValue(1, Irmode.getModeIs()));
+       Ircons.matureBlock (b);
+       int x_then = Ircons.newJmp ();
+
+       /* generate and fill the else block */
+       b = Ircons.newImmBlock ();
+       Ircons.addInEdge (b, f);
+       Ircons.setValue (1, Ircons.newConst (Irmode.getModeIs(),
+                                            Tv.tarvalFromLong (Irmode.getModeIs(), 2)));
+       Ircons.matureBlock (b);
+       int x_else = Ircons.newJmp ();
+
+       /* generate the join block and add all cfg edges */
+       b = Ircons.newImmBlock ();
+       Ircons.addInEdge (b, x_then);
+       Ircons.addInEdge (b, x_else);
+
+       int[] in = new int[2]; /* this is the array containing the return parameters */
+       in[0] = Ircons.getValue(0, Irmode.getModeIs());
+       in[1] = Ircons.getValue(1, Irmode.getModeIs());
+       x = Ircons.newReturn (Ircons.getStore(), 2, in);
+
+       /* Now generate all instructions for this block and all its predecessor
+          blocks so we can mature it. */
+       Ircons.matureBlock (Irgraph.getIrgCurrentBlock(irg));
+
+       /* This adds the in edge of the end block which originates at the
+          return statement.  The return node passes control flow to the
+          end block.  */
+       Ircons.addInEdge (Irgraph.getIrgEndBlock(irg), x);
+       /* Now we can mature the end block as all it's predecessors are known. */
+       Ircons.matureBlock (Irgraph.getIrgEndBlock(irg));
+
+       Irvrfy.irgVrfy(irg);
+       Ircons.finalizeCons (irg);
+
+       System.out.println("Done building the graph.  Optimizing it.");
+       Irgopt.localOptimizeGraph(irg);
+       Irgopt.deadNodeElimination(irg);
+
+       Irdump.dumpIrBlockGraph (irg);
+       Irdump.dumpAllTypes();
+
+       System.out.println("use xvcg to view this graph:");
+       System.out.println("/ben/goetz/bin/xvcg GRAPHNAME\n");
+
+    }
+}
index 9c4d622..9bb7a5a 100644 (file)
@@ -13,7 +13,7 @@ subdir = testprograms
 SOURCEPATH = @top_srcdir@/firmjni/testprograms/
 
 SOURCES := Makefile.in \
-       Empty.java
+       Empty.java IfElseExample.java
 
 MYCLASSPATH = $(CLASSPATH):../..
 
@@ -23,15 +23,18 @@ LD_LIBRARY_PATH += $srcdir/firmjni
 %.class: $(top_srcdir)/firmjni/testprograms/%.java
        javac -d . $<
 
-all:   empty
+all:   empty ifelseexample
 
 # bad hack ;-(
 empty:
        javac -classpath $(MYCLASSPATH) -d . $(SOURCEPATH)Empty.java
 
+ifelseexample:
+       javac -classpath $(MYCLASSPATH) -d . $(SOURCEPATH)IfElseExample.java
 
-run:   empty
+run:   empty ifelseexample
        java -cp $(MYCLASSPATH) -Djava.library.path=$(topdir) Empty
+       java -cp $(MYCLASSPATH) -Djava.library.path=$(topdir) IfElseExample
 
 clean:
        -rm *.class