changeset 19073:94e88f0d8eef

Small fixes for Graal tutorial classes
author Christian Wimmer <christian.wimmer@oracle.com>
date Mon, 02 Feb 2015 17:21:33 -0800
parents deb2467530e4
children c8b2315651d3
files graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/tutorial/GraalTutorial.java graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/tutorial/StaticAnalysis.java
diffstat 2 files changed, 35 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/tutorial/GraalTutorial.java	Mon Feb 02 14:29:29 2015 -0800
+++ b/graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/tutorial/GraalTutorial.java	Mon Feb 02 17:21:33 2015 -0800
@@ -25,6 +25,8 @@
 import org.junit.*;
 
 import com.oracle.graal.api.code.*;
+import com.oracle.graal.api.meta.*;
+import com.oracle.graal.java.*;
 
 /**
  * Examples for the Graal tutorial. Run them using the unittest harness of the mx script. To look at
@@ -38,15 +40,31 @@
 public class GraalTutorial extends InvokeGraal {
 
     /*
+     * Example for the Graal API: access the Graal API metadata object for a method.
+     */
+
+    @Test
+    public void testPrintBytecodes() {
+        ResolvedJavaMethod method = findMethod(String.class, "hashCode");
+
+        byte[] bytecodes = method.getCode();
+        Assert.assertNotNull(bytecodes);
+
+        System.out.println(new BytecodeDisassembler().disassemble(method));
+    }
+
+    /*
      * A simple Graal compilation example: Compile the method String.hashCode()
      */
 
     @Test
     public void testStringHashCode() throws InvalidInstalledCodeException {
+        int expectedResult = "Hello World".hashCode();
+
         InstalledCode installedCode = compileAndInstallMethod(findMethod(String.class, "hashCode"));
 
         int result = (int) installedCode.executeVarargs("Hello World");
-        Assert.assertEquals(-862545276, result);
+        Assert.assertEquals(expectedResult, result);
     }
 
     /*
@@ -70,7 +88,9 @@
         /*
          * Collect profiling information by running the method in the interpreter.
          */
+
         for (int i = 0; i < 10000; i++) {
+            /* Execute several times so that enough profiling information gets collected. */
             speculativeOptimization(false);
         }
 
@@ -121,13 +141,17 @@
 
     @Test
     public void testInstanceOfUsage() throws InvalidInstalledCodeException {
-        A a = new A();
+        /*
+         * Collect profiling information by running the method in the interpreter.
+         */
 
-        /* Allocate an (unsued) instance of B so that the class B gets loaded. */
+        A a = new A();
+        /* Allocate an (unused) instance of B so that the class B gets loaded. */
         @SuppressWarnings("unused")
         B b = new B();
-
+        int expectedResult = instanceOfUsage(a);
         for (int i = 0; i < 10000; i++) {
+            /* Execute several times so that enough profiling information gets collected. */
             instanceOfUsage(a);
         }
 
@@ -136,7 +160,7 @@
         InstalledCode compiledMethod = compileAndInstallMethod(findMethod(GraalTutorial.class, "instanceOfUsage"));
 
         int result = (int) compiledMethod.executeVarargs(a);
-        Assert.assertEquals(42, result);
+        Assert.assertEquals(expectedResult, result);
     }
 
     /*
@@ -149,9 +173,11 @@
 
     @Test
     public void testIntrinsicUsage() throws InvalidInstalledCodeException {
+        double expectedResult = intrinsicUsage(42d);
+
         InstalledCode compiledMethod = compileAndInstallMethod(findMethod(GraalTutorial.class, "intrinsicUsage"));
 
         double result = (double) compiledMethod.executeVarargs(42d);
-        Assert.assertEquals(Math.sin(42), result, 0);
+        Assert.assertEquals(expectedResult, result, 0);
     }
 }
--- a/graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/tutorial/StaticAnalysis.java	Mon Feb 02 14:29:29 2015 -0800
+++ b/graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/tutorial/StaticAnalysis.java	Mon Feb 02 17:21:33 2015 -0800
@@ -75,7 +75,7 @@
 
     /**
      * Adds a root method to the static analysis. The method must be static and must not have any
-     * parameters, because the possible types of the parametes would not be known.
+     * parameters, because the possible types of the parameters would not be known.
      */
     public void addMethod(ResolvedJavaMethod method) {
         if (!method.isStatic() || method.getSignature().getParameterCount(false) > 0) {
@@ -222,7 +222,7 @@
                      * the code before static analysis, the classes would otherwise be not loaded
                      * yet and the bytecode parser would only create a graph.
                      */
-                    GraphBuilderConfiguration graphBuilderConfig = GraphBuilderConfiguration.getEagerDefault().withOmitAllExceptionEdges(true);
+                    GraphBuilderConfiguration graphBuilderConfig = GraphBuilderConfiguration.getEagerDefault();
                     /*
                      * For simplicity, we ignore all exception handling during the static analysis.
                      * This is a constraint of this example code, a real static analysis needs to
@@ -311,7 +311,7 @@
      * The active element for method invocations. For {@link InvokeKind#Virtual virtual} and
      * {@link InvokeKind#Interface interface} calls, the {@link TypeFlow#getTypes() types} of this
      * node are the receiver types. When a new receiver type is added, a new callee might be added.
-     * Adding a new callee means liking the type flow of the actual parameters with the formal
+     * Adding a new callee means linking the type flow of the actual parameters with the formal
      * parameters of the callee, and linking the return value of the callee with the return value
      * state of the invocation.
      *
@@ -319,7 +319,6 @@
      * {@link InvokeKind#Special special} calls) have only one callee, but use the same code for
      * simplicity.
      */
-
     class InvokeTypeFlow extends TypeFlow {
         private final MethodCallTargetNode callTarget;
         private final TypeFlow[] actualParameters;