--- /dev/null 2015-10-08 07:25:07.000000000 -1000 +++ new/src/jdk.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/CompilerToVM.java 2015-10-08 07:25:07.000000000 -1000 @@ -0,0 +1,574 @@ +/* + * Copyright (c) 2011, 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 jdk.vm.ci.hotspot; + +import static jdk.vm.ci.inittimer.InitTimer.timer; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; + +import jdk.vm.ci.code.InstalledCode; +import jdk.vm.ci.code.InvalidInstalledCodeException; +import jdk.vm.ci.code.TargetDescription; +import jdk.vm.ci.hotspotvmconfig.HotSpotVMField; +import jdk.vm.ci.inittimer.InitTimer; +import jdk.vm.ci.meta.JavaType; +import jdk.vm.ci.meta.ResolvedJavaMethod; +import jdk.vm.ci.meta.ResolvedJavaType; +import jdk.vm.ci.meta.SpeculationLog; +import sun.misc.Unsafe; + +/** + * Calls from Java into HotSpot. The behavior of all the methods in this class that take a native + * pointer as an argument (e.g., {@link #getSymbol(long)}) is undefined if the argument does not + * denote a valid native object. + */ +public final class CompilerToVM { + /** + * Initializes the native part of the JVMCI runtime. + */ + private static native void registerNatives(); + + static { + initialize(); + } + + @SuppressWarnings("try") + private static void initialize() { + try (InitTimer t = timer("CompilerToVM.registerNatives")) { + registerNatives(); + } + } + + /** + * Copies the original bytecode of {@code method} into a new byte array and returns it. + * + * @return a new byte array containing the original bytecode of {@code method} + */ + native byte[] getBytecode(HotSpotResolvedJavaMethodImpl method); + + /** + * Gets the number of entries in {@code method}'s exception handler table or 0 if it has not + * exception handler table. + */ + native int getExceptionTableLength(HotSpotResolvedJavaMethodImpl method); + + /** + * Gets the address of the first entry in {@code method}'s exception handler table. + * + * Each entry is a native object described by these fields: + * + *
+ * [(int) flags, // only valid if field is resolved + * (int) offset] // only valid if field is resolved + *+ * + * The behavior of this method is undefined if {@code cpi} does not denote a + * {@code JVM_CONSTANT_Field} entry. + * + * @param info an array in which the details of the field are returned + * @return the type defining the field if resolution is successful, 0 otherwise + */ + native HotSpotResolvedObjectTypeImpl resolveFieldInPool(HotSpotConstantPool constantPool, int cpi, byte opcode, long[] info); + + /** + * Converts {@code cpci} from an index into the cache for {@code constantPool} to an index + * directly into {@code constantPool}. + * + * The behavior of this method is undefined if {@code ccpi} is an invalid constant pool cache + * index. + */ + native int constantPoolRemapInstructionOperandFromCache(HotSpotConstantPool constantPool, int cpci); + + /** + * Gets the appendix object (if any) associated with the entry at index {@code cpi} in + * {@code constantPool}. + */ + native Object lookupAppendixInPool(HotSpotConstantPool constantPool, int cpi); + + /** + * Installs the result of a compilation into the code cache. + * + * @param target the target where this code should be installed + * @param compiledCode the result of a compilation + * @param code the details of the installed CodeBlob are written to this object + * @return the outcome of the installation which will be one of + * {@link HotSpotVMConfig#codeInstallResultOk}, + * {@link HotSpotVMConfig#codeInstallResultCacheFull}, + * {@link HotSpotVMConfig#codeInstallResultCodeTooLarge}, + * {@link HotSpotVMConfig#codeInstallResultDependenciesFailed} or + * {@link HotSpotVMConfig#codeInstallResultDependenciesInvalid}. + */ + public native int installCode(TargetDescription target, HotSpotCompiledCode compiledCode, InstalledCode code, SpeculationLog speculationLog); + + public native int getMetadata(TargetDescription target, HotSpotCompiledCode compiledCode, HotSpotMetaData metaData); + + /** + * Notifies the VM of statistics for a completed compilation. + * + * @param id the identifier of the compilation + * @param method the method compiled + * @param osr specifies if the compilation was for on-stack-replacement + * @param processedBytecodes the number of bytecodes processed during the compilation, including + * the bytecodes of all inlined methods + * @param time the amount time spent compiling {@code method} + * @param timeUnitsPerSecond the granularity of the units for the {@code time} value + * @param installedCode the nmethod installed as a result of the compilation + */ + public synchronized native void notifyCompilationStatistics(int id, HotSpotResolvedJavaMethodImpl method, boolean osr, int processedBytecodes, long time, long timeUnitsPerSecond, + InstalledCode installedCode); + + /** + * Resets all compilation statistics. + */ + public native void resetCompilationStatistics(); + + /** + * Initializes the fields of {@code config}. + */ + native long initializeConfiguration(); + + /** + * Resolves the implementation of {@code method} for virtual dispatches on objects of dynamic + * type {@code exactReceiver}. This resolution process only searches "up" the class hierarchy of + * {@code exactReceiver}. + * + * @param caller the caller or context type used to perform access checks + * @return the link-time resolved method (might be abstract) or {@code 0} if it can not be + * linked + */ + native HotSpotResolvedJavaMethodImpl resolveMethod(HotSpotResolvedObjectTypeImpl exactReceiver, HotSpotResolvedJavaMethodImpl method, HotSpotResolvedObjectTypeImpl caller); + + /** + * Gets the static initializer of {@code type}. + * + * @return 0 if {@code type} has no static initializer + */ + native HotSpotResolvedJavaMethodImpl getClassInitializer(HotSpotResolvedObjectTypeImpl type); + + /** + * Determines if {@code type} or any of its currently loaded subclasses overrides + * {@code Object.finalize()}. + */ + native boolean hasFinalizableSubclass(HotSpotResolvedObjectTypeImpl type); + + /** + * Gets the method corresponding to {@code holder} and slot number {@code slot} (i.e. + * {@link Method#slot} or {@link Constructor#slot}). + */ + native HotSpotResolvedJavaMethodImpl getResolvedJavaMethodAtSlot(Class> holder, int slot); + + /** + * Gets the maximum absolute offset of a PC relative call to {@code address} from any position + * in the code cache. + * + * @param address an address that may be called from any code in the code cache + * @return -1 if {@code address == 0} + */ + public native long getMaxCallTargetOffset(long address); + + /** + * Gets a textual disassembly of {@code codeBlob}. + * + * @return a non-zero length string containing a disassembly of {@code codeBlob} or null if + * {@code codeBlob} could not be disassembled for some reason + */ + // The HotSpot disassembler seems not to be thread safe so it's better to synchronize its usage + public synchronized native String disassembleCodeBlob(long codeBlob); + + /** + * Gets a stack trace element for {@code method} at bytecode index {@code bci}. + */ + native StackTraceElement getStackTraceElement(HotSpotResolvedJavaMethodImpl method, int bci); + + /** + * Executes some {@code installedCode} with arguments {@code args}. + * + * @return the result of executing {@code installedCode} + * @throws InvalidInstalledCodeException if {@code installedCode} has been invalidated + */ + native Object executeInstalledCode(Object[] args, InstalledCode installedCode) throws InvalidInstalledCodeException; + + /** + * Gets the line number table for {@code method}. The line number table is encoded as (bci, + * source line number) pairs. + * + * @return the line number table for {@code method} or null if it doesn't have one + */ + native long[] getLineNumberTable(HotSpotResolvedJavaMethodImpl method); + + /** + * Gets the number of entries in the local variable table for {@code method}. + * + * @return the number of entries in the local variable table for {@code method} + */ + native int getLocalVariableTableLength(HotSpotResolvedJavaMethodImpl method); + + /** + * Gets the address of the first entry in the local variable table for {@code method}. + * + * Each entry is a native object described by these fields: + * + *
bytes
is null
.
+ * @exception IndexOutOfBoundsException if copying would cause access of data outside array
+ * bounds.
+ */
+ public native void writeDebugOutput(byte[] bytes, int offset, int length);
+
+ /**
+ * Flush HotSpot's log stream.
+ */
+ public native void flushDebugOutput();
+
+ /**
+ * Read a value representing a metaspace Method* and return the
+ * {@link HotSpotResolvedJavaMethodImpl} wrapping it. This method does no checking that the
+ * location actually contains a valid Method*. If the {@code base} object is a
+ * {@link HotSpotResolvedJavaMethodImpl}, {@link HotSpotConstantPool} or
+ * {@link HotSpotResolvedObjectTypeImpl} then the metaspace pointer is fetched from that object
+ * and used as the base. Otherwise the object itself is used as the base.
+ *
+ * @param base an object to read from or null
+ * @param displacement
+ * @return null or the resolved method for this location
+ */
+ native HotSpotResolvedJavaMethodImpl getResolvedJavaMethod(Object base, long displacement);
+
+ /**
+ * Read a value representing a metaspace ConstantPool* and return the
+ * {@link HotSpotConstantPool} wrapping it. This method does no checking that the location
+ * actually contains a valid ConstantPool*. If the {@code base} object is a
+ * {@link HotSpotResolvedJavaMethodImpl}, {@link HotSpotConstantPool} or
+ * {@link HotSpotResolvedObjectTypeImpl} then the metaspace pointer is fetched from that object
+ * and used as the base. Otherwise the object itself is used as the base.
+ *
+ * @param base an object to read from or null
+ * @param displacement
+ * @return null or the resolved method for this location
+ */
+ native HotSpotConstantPool getConstantPool(Object base, long displacement);
+
+ /**
+ * Read a value representing a metaspace Klass* and return the
+ * {@link HotSpotResolvedObjectTypeImpl} wrapping it. The method does no checking that the
+ * location actually contains a valid Klass*. If the {@code base} object is a
+ * {@link HotSpotResolvedJavaMethodImpl}, {@link HotSpotConstantPool} or
+ * {@link HotSpotResolvedObjectTypeImpl} then the metaspace pointer is fetched from that object
+ * and used as the base. Otherwise the object itself is used as the base.
+ *
+ * @param base an object to read from or null
+ * @param displacement
+ * @param compressed true if the location contains a compressed Klass*
+ * @return null or the resolved method for this location
+ */
+ native HotSpotResolvedObjectTypeImpl getResolvedJavaType(Object base, long displacement, boolean compressed);
+}