changeset 7912:06ecee106195

added unit tests for bytecode and machine code disassembly
author Doug Simon <doug.simon@oracle.com>
date Sat, 02 Mar 2013 09:53:47 +0400
parents 983f7bdb85ff
children 0df252296c8d
files graal/com.oracle.graal.api.code/src/com/oracle/graal/api/code/DisassemblerProvider.java graal/com.oracle.graal.api.meta.test/src/com/oracle/graal/api/meta/test/TestBytecodeDisassemblerProvider.java graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/BytecodeDisassemblerProvider.java graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/ResolvedJavaMethod.java graal/com.oracle.graal.asm.test/src/com/oracle/graal/asm/test/AssemblerTest.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotResolvedJavaMethod.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotRuntime.java
diffstat 7 files changed, 87 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.api.code/src/com/oracle/graal/api/code/DisassemblerProvider.java	Fri Mar 01 19:51:45 2013 +1000
+++ b/graal/com.oracle.graal.api.code/src/com/oracle/graal/api/code/DisassemblerProvider.java	Sat Mar 02 09:53:47 2013 +0400
@@ -29,6 +29,10 @@
 
     /**
      * Gets a textual disassembly of some given installed code.
+     * 
+     * @return a non-zero length string containing a disassembly of {@code code} or null if
+     *         {@code code} is {@link InstalledCode#isValid() invalid} or it could not be
+     *         disassembled for some other reason
      */
     String disassemble(InstalledCode code);
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.graal.api.meta.test/src/com/oracle/graal/api/meta/test/TestBytecodeDisassemblerProvider.java	Sat Mar 02 09:53:47 2013 +0400
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.graal.api.meta.test;
+
+import org.junit.*;
+
+import com.oracle.graal.api.meta.*;
+import com.oracle.graal.api.runtime.*;
+
+/**
+ * Tests for {@link BytecodeDisassemblerProvider}.
+ */
+public class TestBytecodeDisassemblerProvider {
+
+    public TestBytecodeDisassemblerProvider() {
+    }
+
+    /**
+     * Tests that successive disassembling of the same method produces the same result.
+     */
+    @Test
+    public void disassembleTest() {
+        BytecodeDisassemblerProvider dis = Graal.getRuntime().getCapability(BytecodeDisassemblerProvider.class);
+        if (dis != null) {
+            int count = 0;
+            for (ResolvedJavaMethod m : TestJavaMethod.methods.values()) {
+                String disasm1 = dis.disassemble(m);
+                String disasm2 = dis.disassemble(m);
+                if (disasm1 == null) {
+                    Assert.assertTrue(disasm2 == null);
+                } else {
+                    Assert.assertTrue(String.valueOf(m), disasm1.length() > 0);
+                    Assert.assertEquals(String.valueOf(m), disasm1, disasm2);
+                }
+                if (count++ > 20) {
+                    break;
+                }
+            }
+        }
+    }
+}
--- a/graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/BytecodeDisassemblerProvider.java	Fri Mar 01 19:51:45 2013 +1000
+++ b/graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/BytecodeDisassemblerProvider.java	Sat Mar 02 09:53:47 2013 +0400
@@ -28,7 +28,11 @@
 public interface BytecodeDisassemblerProvider {
 
     /**
-     * Gets a textual disassembly of the bytecode for a given method.
+     * Gets a textual disassembly of the bytecode for a given method. In the absence of bytecode
+     * rewriting, disassembling a method will produce the same result.
+     * 
+     * @return a non-zero length string containing a disassembly of {@code method}'s bytecode or
+     *         null if {@code method} has no bytecode (i.e., {@code method.getCodeSize() == 0})
      */
     String disassemble(ResolvedJavaMethod method);
 }
--- a/graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/ResolvedJavaMethod.java	Fri Mar 01 19:51:45 2013 +1000
+++ b/graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/ResolvedJavaMethod.java	Sat Mar 02 09:53:47 2013 +0400
@@ -34,18 +34,18 @@
 public interface ResolvedJavaMethod extends JavaMethod, InvokeTarget {
 
     /**
-     * Returns the bytecodes of this method, if the method has code. The returned byte array does
-     * not contain breakpoints or non-Java bytecodes.
+     * Returns the bytecode of this method, if the method has code. The returned byte array does not
+     * contain breakpoints or non-Java bytecodes.
      * 
-     * @return the bytecodes of the method, or {@code null} if none is available
+     * @return the bytecode of the method, or {@code null} if none is available
      */
     byte[] getCode();
 
     /**
-     * Returns the size of the bytecodes of this method, if the method has code. This is equivalent
+     * Returns the size of the bytecode of this method, if the method has code. This is equivalent
      * to {@link #getCode()}. {@code length} if the method has code.
      * 
-     * @return the size of the bytecodes in bytes, or 0 if no bytecodes is available
+     * @return the size of the bytecode in bytes, or 0 if no bytecode is available
      */
     int getCodeSize();
 
--- a/graal/com.oracle.graal.asm.test/src/com/oracle/graal/asm/test/AssemblerTest.java	Fri Mar 01 19:51:45 2013 +1000
+++ b/graal/com.oracle.graal.asm.test/src/com/oracle/graal/asm/test/AssemblerTest.java	Sat Mar 02 09:53:47 2013 +0400
@@ -54,7 +54,14 @@
         Buffer codeBuffer = test.generateCode(compResult, codeCache.getTarget(), registerConfig, cc);
         compResult.setTargetCode(codeBuffer.close(true), codeBuffer.position());
 
-        return codeCache.addMethod(method, compResult, null);
+        InstalledCode code = codeCache.addMethod(method, compResult, null);
+
+        DisassemblerProvider dis = Graal.getRuntime().getCapability(DisassemblerProvider.class);
+        if (dis != null) {
+            String disasm = dis.disassemble(code);
+            Assert.assertTrue(String.valueOf(code.getMethod()), disasm == null || disasm.length() > 0);
+        }
+        return code;
     }
 
     protected Object runTest(String methodName, CodeGenTest test, Object... args) {
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotResolvedJavaMethod.java	Fri Mar 01 19:51:45 2013 +1000
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotResolvedJavaMethod.java	Sat Mar 02 09:53:47 2013 +0400
@@ -86,6 +86,9 @@
 
     @Override
     public byte[] getCode() {
+        if (codeSize == 0) {
+            return null;
+        }
         if (code == null) {
             code = HotSpotGraalRuntime.getInstance().getCompilerToVM().initializeBytecode(metaspaceMethod, new byte[codeSize]);
             assert code.length == codeSize : "expected: " + codeSize + ", actual: " + code.length;
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotRuntime.java	Fri Mar 01 19:51:45 2013 +1000
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotRuntime.java	Sat Mar 02 09:53:47 2013 +0400
@@ -953,7 +953,7 @@
             long nmethod = ((HotSpotInstalledCode) code).nmethod;
             return graalRuntime.getCompilerToVM().disassembleNMethod(nmethod);
         }
-        return "";
+        return null;
     }
 
     public String disassemble(ResolvedJavaMethod method) {