1 /*
   2  * Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package jdk.vm.ci.hotspot;
  25 
  26 import static jdk.vm.ci.common.InitTimer.timer;
  27 import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.runtime;
  28 
  29 import java.lang.reflect.Executable;
  30 import java.lang.reflect.Field;
  31 
  32 import jdk.vm.ci.code.BytecodeFrame;
  33 import jdk.vm.ci.code.InstalledCode;
  34 import jdk.vm.ci.code.InvalidInstalledCodeException;
  35 import jdk.vm.ci.code.TargetDescription;
  36 import jdk.vm.ci.code.stack.InspectedFrameVisitor;
  37 import jdk.vm.ci.common.InitTimer;
  38 import jdk.vm.ci.common.JVMCIError;
  39 import jdk.vm.ci.meta.Constant;
  40 import jdk.vm.ci.meta.ConstantReflectionProvider;
  41 import jdk.vm.ci.meta.JavaConstant;
  42 import jdk.vm.ci.meta.JavaKind;
  43 import jdk.vm.ci.meta.JavaType;
  44 import jdk.vm.ci.meta.ResolvedJavaMethod;
  45 import jdk.vm.ci.meta.ResolvedJavaType;
  46 
  47 /**
  48  * Calls from Java into HotSpot. The behavior of all the methods in this class that take a native
  49  * pointer as an argument (e.g., {@link #getSymbol(long)}) is undefined if the argument does not
  50  * denote a valid native object.
  51  */
  52 final class CompilerToVM {
  53     /**
  54      * Initializes the native part of the JVMCI runtime.
  55      */
  56     private static native void registerNatives();
  57 
  58     /**
  59      * These values mirror the equivalent values from {@link Unsafe} but are approriate for the JVM
  60      * being compiled against.
  61      */
  62     // Checkstyle: stop
  63     final int ARRAY_BOOLEAN_BASE_OFFSET;
  64     final int ARRAY_BYTE_BASE_OFFSET;
  65     final int ARRAY_SHORT_BASE_OFFSET;
  66     final int ARRAY_CHAR_BASE_OFFSET;
  67     final int ARRAY_INT_BASE_OFFSET;
  68     final int ARRAY_LONG_BASE_OFFSET;
  69     final int ARRAY_FLOAT_BASE_OFFSET;
  70     final int ARRAY_DOUBLE_BASE_OFFSET;
  71     final int ARRAY_OBJECT_BASE_OFFSET;
  72     final int ARRAY_BOOLEAN_INDEX_SCALE;
  73     final int ARRAY_BYTE_INDEX_SCALE;
  74     final int ARRAY_SHORT_INDEX_SCALE;
  75     final int ARRAY_CHAR_INDEX_SCALE;
  76     final int ARRAY_INT_INDEX_SCALE;
  77     final int ARRAY_LONG_INDEX_SCALE;
  78     final int ARRAY_FLOAT_INDEX_SCALE;
  79     final int ARRAY_DOUBLE_INDEX_SCALE;
  80     final int ARRAY_OBJECT_INDEX_SCALE;
  81     // Checkstyle: resume
  82 
  83     @SuppressWarnings("try")
  84     CompilerToVM() {
  85         try (InitTimer t = timer("CompilerToVM.registerNatives")) {
  86             registerNatives();
  87             ARRAY_BOOLEAN_BASE_OFFSET = arrayBaseOffset(JavaKind.Boolean);
  88             ARRAY_BYTE_BASE_OFFSET = arrayBaseOffset(JavaKind.Byte);
  89             ARRAY_SHORT_BASE_OFFSET = arrayBaseOffset(JavaKind.Short);
  90             ARRAY_CHAR_BASE_OFFSET = arrayBaseOffset(JavaKind.Char);
  91             ARRAY_INT_BASE_OFFSET = arrayBaseOffset(JavaKind.Int);
  92             ARRAY_LONG_BASE_OFFSET = arrayBaseOffset(JavaKind.Long);
  93             ARRAY_FLOAT_BASE_OFFSET = arrayBaseOffset(JavaKind.Float);
  94             ARRAY_DOUBLE_BASE_OFFSET = arrayBaseOffset(JavaKind.Double);
  95             ARRAY_OBJECT_BASE_OFFSET = arrayBaseOffset(JavaKind.Object);
  96             ARRAY_BOOLEAN_INDEX_SCALE = arrayIndexScale(JavaKind.Boolean);
  97             ARRAY_BYTE_INDEX_SCALE = arrayIndexScale(JavaKind.Byte);
  98             ARRAY_SHORT_INDEX_SCALE = arrayIndexScale(JavaKind.Short);
  99             ARRAY_CHAR_INDEX_SCALE = arrayIndexScale(JavaKind.Char);
 100             ARRAY_INT_INDEX_SCALE = arrayIndexScale(JavaKind.Int);
 101             ARRAY_LONG_INDEX_SCALE = arrayIndexScale(JavaKind.Long);
 102             ARRAY_FLOAT_INDEX_SCALE = arrayIndexScale(JavaKind.Float);
 103             ARRAY_DOUBLE_INDEX_SCALE = arrayIndexScale(JavaKind.Double);
 104             ARRAY_OBJECT_INDEX_SCALE = arrayIndexScale(JavaKind.Object);
 105         }
 106     }
 107 
 108     native int arrayBaseOffset(JavaKind kind);
 109 
 110     native int arrayIndexScale(JavaKind kind);
 111 
 112     /**
 113      * Gets the {@link CompilerToVM} instance associated with the singleton
 114      * {@link HotSpotJVMCIRuntime} instance.
 115      */
 116     public static CompilerToVM compilerToVM() {
 117         return runtime().getCompilerToVM();
 118     }
 119 
 120     /**
 121      * Copies the original bytecode of {@code method} into a new byte array and returns it.
 122      *
 123      * @return a new byte array containing the original bytecode of {@code method}
 124      */
 125     native byte[] getBytecode(HotSpotResolvedJavaMethodImpl method);
 126 
 127     /**
 128      * Gets the number of entries in {@code method}'s exception handler table or 0 if it has no
 129      * exception handler table.
 130      */
 131     native int getExceptionTableLength(HotSpotResolvedJavaMethodImpl method);
 132 
 133     /**
 134      * Gets the address of the first entry in {@code method}'s exception handler table.
 135      *
 136      * Each entry is a native object described by these fields:
 137      *
 138      * <ul>
 139      * <li>{@link HotSpotVMConfig#exceptionTableElementSize}</li>
 140      * <li>{@link HotSpotVMConfig#exceptionTableElementStartPcOffset}</li>
 141      * <li>{@link HotSpotVMConfig#exceptionTableElementEndPcOffset}</li>
 142      * <li>{@link HotSpotVMConfig#exceptionTableElementHandlerPcOffset}</li>
 143      * <li>{@link HotSpotVMConfig#exceptionTableElementCatchTypeIndexOffset}
 144      * </ul>
 145      *
 146      * @return 0 if {@code method} has no exception handlers (i.e.
 147      *         {@code getExceptionTableLength(method) == 0})
 148      */
 149     native long getExceptionTableStart(HotSpotResolvedJavaMethodImpl method);
 150 
 151     /**
 152      * Determines whether {@code method} is currently compilable by the JVMCI compiler being used by
 153      * the VM. This can return false if JVMCI compilation failed earlier for {@code method}, a
 154      * breakpoint is currently set in {@code method} or {@code method} contains other bytecode
 155      * features that require special handling by the VM.
 156      */
 157     native boolean isCompilable(HotSpotResolvedJavaMethodImpl method);
 158 
 159     /**
 160      * Determines if {@code method} is targeted by a VM directive (e.g.,
 161      * {@code -XX:CompileCommand=dontinline,<pattern>}) or annotation (e.g.,
 162      * {@code jdk.internal.vm.annotation.DontInline}) that specifies it should not be inlined.
 163      */
 164     native boolean hasNeverInlineDirective(HotSpotResolvedJavaMethodImpl method);
 165 
 166     /**
 167      * Determines if {@code method} should be inlined at any cost. This could be because:
 168      * <ul>
 169      * <li>a CompileOracle directive may forces inlining of this methods</li>
 170      * <li>an annotation forces inlining of this method</li>
 171      * </ul>
 172      */
 173     native boolean shouldInlineMethod(HotSpotResolvedJavaMethodImpl method);
 174 
 175     /**
 176      * Used to implement {@link ResolvedJavaType#findUniqueConcreteMethod(ResolvedJavaMethod)}.
 177      *
 178      * @param method the method on which to base the search
 179      * @param actualHolderType the best known type of receiver
 180      * @return the method result or 0 is there is no unique concrete method for {@code method}
 181      */
 182     native HotSpotResolvedJavaMethodImpl findUniqueConcreteMethod(HotSpotResolvedObjectTypeImpl actualHolderType, HotSpotResolvedJavaMethodImpl method);
 183 
 184     /**
 185      * Gets the implementor for the interface class {@code type}.
 186      *
 187      * @return the implementor if there is a single implementor, {@code null} if there is no
 188      *         implementor, or {@code type} itself if there is more than one implementor
 189      * @throws IllegalArgumentException if type is not an interface type
 190      */
 191     native HotSpotResolvedObjectTypeImpl getImplementor(HotSpotResolvedObjectTypeImpl type);
 192 
 193     /**
 194      * Determines if {@code method} is ignored by security stack walks.
 195      */
 196     native boolean methodIsIgnoredBySecurityStackWalk(HotSpotResolvedJavaMethodImpl method);
 197 
 198     /**
 199      * Converts a name to a type.
 200      *
 201      * @param name a well formed Java type in {@linkplain JavaType#getName() internal} format
 202      * @param accessingClass the context of resolution. A value of {@code null} implies that the
 203      *            class should be resolved with the class loader.
 204      * @param resolve force resolution to a {@link ResolvedJavaType}. If true, this method will
 205      *            either return a {@link ResolvedJavaType} or throw an exception
 206      * @return the type for {@code name} or 0 if resolution failed and {@code resolve == false}
 207      * @throws ClassNotFoundException if {@code resolve == true} and the resolution failed
 208      */
 209     native HotSpotResolvedJavaType lookupType(String name, HotSpotResolvedObjectTypeImpl accessingClass, boolean resolve) throws ClassNotFoundException;
 210 
 211     native HotSpotResolvedJavaType lookupClass(Class<?> javaClass);
 212 
 213     /**
 214      * Resolves the entry at index {@code cpi} in {@code constantPool} to an object.
 215      *
 216      * The behavior of this method is undefined if {@code cpi} does not denote one of the following
 217      * entry types: {@code JVM_CONSTANT_MethodHandle}, {@code JVM_CONSTANT_MethodHandleInError},
 218      * {@code JVM_CONSTANT_MethodType} and {@code JVM_CONSTANT_MethodTypeInError}.
 219      */
 220     native HotSpotObjectConstantImpl resolveConstantInPool(HotSpotConstantPool constantPool, int cpi);
 221 
 222     /**
 223      * Resolves the entry at index {@code cpi} in {@code constantPool} to an object, looking in the
 224      * constant pool cache first.
 225      *
 226      * The behavior of this method is undefined if {@code cpi} does not denote a
 227      * {@code JVM_CONSTANT_String} entry.
 228      */
 229     native HotSpotObjectConstantImpl resolvePossiblyCachedConstantInPool(HotSpotConstantPool constantPool, int cpi);
 230 
 231     /**
 232      * Gets the {@code JVM_CONSTANT_NameAndType} index from the entry at index {@code cpi} in
 233      * {@code constantPool}.
 234      *
 235      * The behavior of this method is undefined if {@code cpi} does not denote an entry containing a
 236      * {@code JVM_CONSTANT_NameAndType} index.
 237      */
 238     native int lookupNameAndTypeRefIndexInPool(HotSpotConstantPool constantPool, int cpi);
 239 
 240     /**
 241      * Gets the name of the {@code JVM_CONSTANT_NameAndType} entry referenced by another entry
 242      * denoted by {@code which} in {@code constantPool}.
 243      *
 244      * The behavior of this method is undefined if {@code which} does not denote a entry that
 245      * references a {@code JVM_CONSTANT_NameAndType} entry.
 246      */
 247     native String lookupNameInPool(HotSpotConstantPool constantPool, int which);
 248 
 249     /**
 250      * Gets the signature of the {@code JVM_CONSTANT_NameAndType} entry referenced by another entry
 251      * denoted by {@code which} in {@code constantPool}.
 252      *
 253      * The behavior of this method is undefined if {@code which} does not denote a entry that
 254      * references a {@code JVM_CONSTANT_NameAndType} entry.
 255      */
 256     native String lookupSignatureInPool(HotSpotConstantPool constantPool, int which);
 257 
 258     /**
 259      * Gets the {@code JVM_CONSTANT_Class} index from the entry at index {@code cpi} in
 260      * {@code constantPool}.
 261      *
 262      * The behavior of this method is undefined if {@code cpi} does not denote an entry containing a
 263      * {@code JVM_CONSTANT_Class} index.
 264      */
 265     native int lookupKlassRefIndexInPool(HotSpotConstantPool constantPool, int cpi);
 266 
 267     /**
 268      * Looks up a class denoted by the {@code JVM_CONSTANT_Class} entry at index {@code cpi} in
 269      * {@code constantPool}. This method does not perform any resolution.
 270      *
 271      * The behavior of this method is undefined if {@code cpi} does not denote a
 272      * {@code JVM_CONSTANT_Class} entry.
 273      *
 274      * @return the resolved class entry or a String otherwise
 275      */
 276     native Object lookupKlassInPool(HotSpotConstantPool constantPool, int cpi);
 277 
 278     /**
 279      * Looks up a method denoted by the entry at index {@code cpi} in {@code constantPool}. This
 280      * method does not perform any resolution.
 281      *
 282      * The behavior of this method is undefined if {@code cpi} does not denote an entry representing
 283      * a method.
 284      *
 285      * @param opcode the opcode of the instruction for which the lookup is being performed or
 286      *            {@code -1}. If non-negative, then resolution checks specific to the bytecode it
 287      *            denotes are performed if the method is already resolved. Should any of these
 288      *            checks fail, 0 is returned.
 289      * @return the resolved method entry, 0 otherwise
 290      */
 291     native HotSpotResolvedJavaMethodImpl lookupMethodInPool(HotSpotConstantPool constantPool, int cpi, byte opcode);
 292 
 293     // TODO resolving JVM_CONSTANT_Dynamic
 294 
 295     /**
 296      * Ensures that the type referenced by the specified {@code JVM_CONSTANT_InvokeDynamic} entry at
 297      * index {@code cpi} in {@code constantPool} is loaded and initialized.
 298      *
 299      * The behavior of this method is undefined if {@code cpi} does not denote a
 300      * {@code JVM_CONSTANT_InvokeDynamic} entry.
 301      */
 302     native void resolveInvokeDynamicInPool(HotSpotConstantPool constantPool, int cpi);
 303 
 304     /**
 305      * If {@code cpi} denotes an entry representing a
 306      * <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-2.html#jvms-2.9">signature
 307      * polymorphic</a> method, this method ensures that the type referenced by the entry is loaded
 308      * and initialized. It {@code cpi} does not denote a signature polymorphic method, this method
 309      * does nothing.
 310      */
 311     native void resolveInvokeHandleInPool(HotSpotConstantPool constantPool, int cpi);
 312 
 313     /**
 314      * If {@code cpi} denotes an entry representing a resolved dynamic adapter (see
 315      * {@link #resolveInvokeDynamicInPool} and {@link #resolveInvokeHandleInPool}), return the
 316      * opcode of the instruction for which the resolution was performed ({@code invokedynamic} or
 317      * {@code invokevirtual}), or {@code -1} otherwise.
 318      */
 319     native int isResolvedInvokeHandleInPool(HotSpotConstantPool constantPool, int cpi);
 320 
 321     /**
 322      * Gets the list of type names (in the format of {@link JavaType#getName()}) denoting the
 323      * classes that define signature polymorphic methods.
 324      */
 325     native String[] getSignaturePolymorphicHolders();
 326 
 327     /**
 328      * Gets the resolved type denoted by the entry at index {@code cpi} in {@code constantPool}.
 329      *
 330      * The behavior of this method is undefined if {@code cpi} does not denote an entry representing
 331      * a class.
 332      *
 333      * @throws LinkageError if resolution failed
 334      */
 335     native HotSpotResolvedObjectTypeImpl resolveTypeInPool(HotSpotConstantPool constantPool, int cpi) throws LinkageError;
 336 
 337     /**
 338      * Looks up and attempts to resolve the {@code JVM_CONSTANT_Field} entry for at index
 339      * {@code cpi} in {@code constantPool}. For some opcodes, checks are performed that require the
 340      * {@code method} that contains {@code opcode} to be specified. The values returned in
 341      * {@code info} are:
 342      *
 343      * <pre>
 344      *     [ flags,  // fieldDescriptor::access_flags()
 345      *       offset, // fieldDescriptor::offset()
 346      *       index   // fieldDescriptor::index()
 347      *     ]
 348      * </pre>
 349      *
 350      * The behavior of this method is undefined if {@code cpi} does not denote a
 351      * {@code JVM_CONSTANT_Field} entry.
 352      *
 353      * @param info an array in which the details of the field are returned
 354      * @return the type defining the field if resolution is successful, 0 otherwise
 355      */
 356     native HotSpotResolvedObjectTypeImpl resolveFieldInPool(HotSpotConstantPool constantPool, int cpi, HotSpotResolvedJavaMethodImpl method, byte opcode, int[] info);
 357 
 358     /**
 359      * Converts {@code cpci} from an index into the cache for {@code constantPool} to an index
 360      * directly into {@code constantPool}.
 361      *
 362      * The behavior of this method is undefined if {@code ccpi} is an invalid constant pool cache
 363      * index.
 364      */
 365     native int constantPoolRemapInstructionOperandFromCache(HotSpotConstantPool constantPool, int cpci);
 366 
 367     /**
 368      * Gets the appendix object (if any) associated with the entry at index {@code cpi} in
 369      * {@code constantPool}.
 370      */
 371     native HotSpotObjectConstantImpl lookupAppendixInPool(HotSpotConstantPool constantPool, int cpi);
 372 
 373     /**
 374      * Installs the result of a compilation into the code cache.
 375      *
 376      * @param target the target where this code should be installed
 377      * @param compiledCode the result of a compilation
 378      * @param code the details of the installed CodeBlob are written to this object
 379      * @return the outcome of the installation which will be one of
 380      *         {@link HotSpotVMConfig#codeInstallResultOk},
 381      *         {@link HotSpotVMConfig#codeInstallResultCacheFull},
 382      *         {@link HotSpotVMConfig#codeInstallResultCodeTooLarge},
 383      *         {@link HotSpotVMConfig#codeInstallResultDependenciesFailed} or
 384      *         {@link HotSpotVMConfig#codeInstallResultDependenciesInvalid}.
 385      * @throws JVMCIError if there is something wrong with the compiled code or the associated
 386      *             metadata.
 387      */
 388     native int installCode(TargetDescription target, HotSpotCompiledCode compiledCode, InstalledCode code, long failedSpeculationsAddress, byte[] speculations);
 389 
 390     /**
 391      * Generates the VM metadata for some compiled code and copies them into {@code metaData}. This
 392      * method does not install anything into the code cache.
 393      *
 394      * @param target the target where this code would be installed
 395      * @param compiledCode the result of a compilation
 396      * @param metaData the metadata is written to this object
 397      * @return the outcome of the installation which will be one of
 398      *         {@link HotSpotVMConfig#codeInstallResultOk},
 399      *         {@link HotSpotVMConfig#codeInstallResultCacheFull},
 400      *         {@link HotSpotVMConfig#codeInstallResultCodeTooLarge},
 401      *         {@link HotSpotVMConfig#codeInstallResultDependenciesFailed} or
 402      *         {@link HotSpotVMConfig#codeInstallResultDependenciesInvalid}.
 403      * @throws JVMCIError if there is something wrong with the compiled code or the metadata
 404      */
 405     native int getMetadata(TargetDescription target, HotSpotCompiledCode compiledCode, HotSpotMetaData metaData);
 406 
 407     /**
 408      * Resets all compilation statistics.
 409      */
 410     native void resetCompilationStatistics();
 411 
 412     /**
 413      * Reads the database of VM info. The return value encodes the info in a nested object array
 414      * that is described by the pseudo Java object {@code info} below:
 415      *
 416      * <pre>
 417      *     info = [
 418      *         VMField[] vmFields,
 419      *         [String name, Long size, ...] vmTypeSizes,
 420      *         [String name, Long value, ...] vmConstants,
 421      *         [String name, Long value, ...] vmAddresses,
 422      *         VMFlag[] vmFlags
 423      *         VMIntrinsicMethod[] vmIntrinsics
 424      *     ]
 425      * </pre>
 426      *
 427      * @return VM info as encoded above
 428      */
 429     native Object[] readConfiguration();
 430 
 431     /**
 432      * Resolves the implementation of {@code method} for virtual dispatches on objects of dynamic
 433      * type {@code exactReceiver}. This resolution process only searches "up" the class hierarchy of
 434      * {@code exactReceiver}.
 435      *
 436      * @param caller the caller or context type used to perform access checks
 437      * @return the link-time resolved method (might be abstract) or {@code null} if it is either a
 438      *         signature polymorphic method or can not be linked.
 439      */
 440     native HotSpotResolvedJavaMethodImpl resolveMethod(HotSpotResolvedObjectTypeImpl exactReceiver, HotSpotResolvedJavaMethodImpl method, HotSpotResolvedObjectTypeImpl caller);
 441 
 442     /**
 443      * Gets the static initializer of {@code type}.
 444      *
 445      * @return {@code null} if {@code type} has no static initializer
 446      */
 447     native HotSpotResolvedJavaMethodImpl getClassInitializer(HotSpotResolvedObjectTypeImpl type);
 448 
 449     /**
 450      * Determines if {@code type} or any of its currently loaded subclasses overrides
 451      * {@code Object.finalize()}.
 452      */
 453     native boolean hasFinalizableSubclass(HotSpotResolvedObjectTypeImpl type);
 454 
 455     /**
 456      * Gets the method corresponding to {@code executable}.
 457      */
 458     native HotSpotResolvedJavaMethodImpl asResolvedJavaMethod(Executable executable);
 459 
 460     /**
 461      * Gets the maximum absolute offset of a PC relative call to {@code address} from any position
 462      * in the code cache.
 463      *
 464      * @param address an address that may be called from any code in the code cache
 465      * @return -1 if {@code address == 0}
 466      */
 467     native long getMaxCallTargetOffset(long address);
 468 
 469     /**
 470      * Gets a textual disassembly of {@code codeBlob}.
 471      *
 472      * @return a non-zero length string containing a disassembly of {@code codeBlob} or null if
 473      *         {@code codeBlob} could not be disassembled for some reason
 474      */
 475     // The HotSpot disassembler seems not to be thread safe so it's better to synchronize its usage
 476     synchronized native String disassembleCodeBlob(InstalledCode installedCode);
 477 
 478     /**
 479      * Gets a stack trace element for {@code method} at bytecode index {@code bci}.
 480      */
 481     native StackTraceElement getStackTraceElement(HotSpotResolvedJavaMethodImpl method, int bci);
 482 
 483     /**
 484      * Executes some {@code installedCode} with arguments {@code args}.
 485      *
 486      * @return the result of executing {@code nmethodMirror}
 487      * @throws InvalidInstalledCodeException if {@code nmethodMirror} has been invalidated
 488      */
 489     native Object executeHotSpotNmethod(Object[] args, HotSpotNmethod nmethodMirror) throws InvalidInstalledCodeException;
 490 
 491     /**
 492      * Gets the line number table for {@code method}. The line number table is encoded as (bci,
 493      * source line number) pairs.
 494      *
 495      * @return the line number table for {@code method} or null if it doesn't have one
 496      */
 497     native long[] getLineNumberTable(HotSpotResolvedJavaMethodImpl method);
 498 
 499     /**
 500      * Gets the number of entries in the local variable table for {@code method}.
 501      *
 502      * @return the number of entries in the local variable table for {@code method}
 503      */
 504     native int getLocalVariableTableLength(HotSpotResolvedJavaMethodImpl method);
 505 
 506     /**
 507      * Gets the address of the first entry in the local variable table for {@code method}.
 508      *
 509      * Each entry is a native object described by these fields:
 510      *
 511      * <ul>
 512      * <li>{@link HotSpotVMConfig#localVariableTableElementSize}</li>
 513      * <li>{@link HotSpotVMConfig#localVariableTableElementLengthOffset}</li>
 514      * <li>{@link HotSpotVMConfig#localVariableTableElementNameCpIndexOffset}</li>
 515      * <li>{@link HotSpotVMConfig#localVariableTableElementDescriptorCpIndexOffset}</li>
 516      * <li>{@link HotSpotVMConfig#localVariableTableElementSlotOffset}
 517      * <li>{@link HotSpotVMConfig#localVariableTableElementStartBciOffset}
 518      * </ul>
 519      *
 520      * @return 0 if {@code method} does not have a local variable table
 521      */
 522     native long getLocalVariableTableStart(HotSpotResolvedJavaMethodImpl method);
 523 
 524     /**
 525      * Reads an object pointer within a VM data structure. That is, any {@link VMField} whose
 526      * {@link VMField#type type} is {@code "oop"} (e.g.,
 527      * {@code Klass::_java_mirror}, {@code JavaThread::_threadObj}).
 528      *
 529      * Note that {@link Unsafe#getObject(Object, long)} cannot be used for this since it does a
 530      * {@code narrowOop} read if the VM is using compressed oops whereas oops within VM data
 531      * structures are (currently) always uncompressed.
 532      *
 533      * @param address address of an oop field within a VM data structure
 534      */
 535     native HotSpotObjectConstantImpl readUncompressedOop(long address);
 536 
 537     /**
 538      * Sets flags on {@code method} indicating that it should never be inlined or compiled by the
 539      * VM.
 540      */
 541     native void setNotInlinableOrCompilable(HotSpotResolvedJavaMethodImpl method);
 542 
 543     /**
 544      * Invalidates the profiling information for {@code method} and (re)initializes it such that
 545      * profiling restarts upon its next invocation.
 546      */
 547     native void reprofile(HotSpotResolvedJavaMethodImpl method);
 548 
 549     /**
 550      * Invalidates {@code nmethodMirror} such that {@link InvalidInstalledCodeException} will be
 551      * raised the next time {@code nmethodMirror} is {@linkplain #executeHotSpotNmethod executed}.
 552      * The {@code nmethod} associated with {@code nmethodMirror} is also made non-entrant and any
 553      * current activations of the {@code nmethod} are deoptimized.
 554      */
 555     native void invalidateHotSpotNmethod(HotSpotNmethod nmethodMirror);
 556 
 557     /**
 558      * Collects the current values of all JVMCI benchmark counters, summed up over all threads.
 559      */
 560     native long[] collectCounters();
 561 
 562     /**
 563      * Determines if {@code metaspaceMethodData} is mature.
 564      */
 565     native boolean isMature(long metaspaceMethodData);
 566 
 567     /**
 568      * Generate a unique id to identify the result of the compile.
 569      */
 570     native int allocateCompileId(HotSpotResolvedJavaMethodImpl method, int entryBCI);
 571 
 572     /**
 573      * Determines if {@code method} has OSR compiled code identified by {@code entryBCI} for
 574      * compilation level {@code level}.
 575      */
 576     native boolean hasCompiledCodeForOSR(HotSpotResolvedJavaMethodImpl method, int entryBCI, int level);
 577 
 578     /**
 579      * Gets the value of {@code metaspaceSymbol} as a String.
 580      */
 581     native String getSymbol(long metaspaceSymbol);
 582 
 583     /**
 584      * @see jdk.vm.ci.code.stack.StackIntrospection#iterateFrames
 585      */
 586     native <T> T iterateFrames(ResolvedJavaMethod[] initialMethods, ResolvedJavaMethod[] matchingMethods, int initialSkip, InspectedFrameVisitor<T> visitor);
 587 
 588     /**
 589      * Materializes all virtual objects within {@code stackFrame} and updates its locals.
 590      *
 591      * @param invalidate if {@code true}, the compiled method for the stack frame will be
 592      *            invalidated
 593      */
 594     native void materializeVirtualObjects(HotSpotStackFrameReference stackFrame, boolean invalidate);
 595 
 596     /**
 597      * Gets the v-table index for interface method {@code method} in the receiver {@code type} or
 598      * {@link HotSpotVMConfig#invalidVtableIndex} if {@code method} is not in {@code type}'s
 599      * v-table.
 600      *
 601      * @throws InternalError if {@code type} is an interface or {@code method} is not held by an
 602      *             interface or class represented by {@code type} is not initialized
 603      */
 604     native int getVtableIndexForInterfaceMethod(HotSpotResolvedObjectTypeImpl type, HotSpotResolvedJavaMethodImpl method);
 605 
 606     /**
 607      * Determines if debug info should also be emitted at non-safepoint locations.
 608      */
 609     native boolean shouldDebugNonSafepoints();
 610 
 611     /**
 612      * Writes {@code length} bytes from {@code bytes} starting at offset {@code offset} to HotSpot's
 613      * log stream.
 614      *
 615      * @param flush specifies if the log stream should be flushed after writing
 616      * @param canThrow specifies if an error in the {@code bytes}, {@code offset} or {@code length}
 617      *            arguments should result in an exception or a negative return value
 618      * @return 0 on success, -1 if {@code bytes == null && !canThrow}, -2 if {@code !canThrow} and
 619      *         copying would cause access of data outside array bounds
 620      * @throws NullPointerException if {@code bytes == null}
 621      * @throws IndexOutOfBoundsException if copying would cause access of data outside array bounds
 622      */
 623     native int writeDebugOutput(byte[] bytes, int offset, int length, boolean flush, boolean canThrow);
 624 
 625     /**
 626      * Flush HotSpot's log stream.
 627      */
 628     native void flushDebugOutput();
 629 
 630     /**
 631      * Read a HotSpot Method* value from the memory location described by {@code base} plus
 632      * {@code displacement} and return the {@link HotSpotResolvedJavaMethodImpl} wrapping it. This
 633      * method does no checking that the memory location actually contains a valid pointer and may
 634      * crash the VM if an invalid location is provided. If the {@code base} is null then
 635      * {@code displacement} is used by itself. If {@code base} is a
 636      * {@link HotSpotResolvedJavaMethodImpl}, {@link HotSpotConstantPool} or
 637      * {@link HotSpotResolvedObjectTypeImpl} then the metaspace pointer is fetched from that object
 638      * and added to {@code displacement}. Any other non-null object type causes an
 639      * {@link IllegalArgumentException} to be thrown.
 640      *
 641      * @param base an object to read from or null
 642      * @param displacement
 643      * @return null or the resolved method for this location
 644      */
 645     native HotSpotResolvedJavaMethodImpl getResolvedJavaMethod(HotSpotObjectConstantImpl base, long displacement);
 646 
 647     /**
 648      * Gets the {@code ConstantPool*} associated with {@code object} and returns a
 649      * {@link HotSpotConstantPool} wrapping it.
 650      *
 651      * @param object a {@link HotSpotResolvedJavaMethodImpl} or
 652      *            {@link HotSpotResolvedObjectTypeImpl} object
 653      * @return a {@link HotSpotConstantPool} wrapping the {@code ConstantPool*} associated with
 654      *         {@code object}
 655      * @throws NullPointerException if {@code object == null}
 656      * @throws IllegalArgumentException if {@code object} is neither a
 657      *             {@link HotSpotResolvedJavaMethodImpl} nor a {@link HotSpotResolvedObjectTypeImpl}
 658      */
 659     native HotSpotConstantPool getConstantPool(MetaspaceObject object);
 660 
 661     /**
 662      * Read a HotSpot Klass* value from the memory location described by {@code base} plus
 663      * {@code displacement} and return the {@link HotSpotResolvedObjectTypeImpl} wrapping it. This
 664      * method does no checking that the memory location actually contains a valid pointer and may
 665      * crash the VM if an invalid location is provided. If the {@code base} is null then
 666      * {@code displacement} is used by itself. If {@code base} is a
 667      * {@link HotSpotResolvedJavaMethodImpl}, {@link HotSpotConstantPool} or
 668      * {@link HotSpotResolvedObjectTypeImpl} then the metaspace pointer is fetched from that object
 669      * and added to {@code displacement}. Any other non-null object type causes an
 670      * {@link IllegalArgumentException} to be thrown.
 671      *
 672      * @param base an object to read from or null
 673      * @param displacement
 674      * @param compressed true if the location contains a compressed Klass*
 675      * @return null or the resolved method for this location
 676      */
 677     private native HotSpotResolvedObjectTypeImpl getResolvedJavaType0(Object base, long displacement, boolean compressed);
 678 
 679     HotSpotResolvedObjectTypeImpl getResolvedJavaType(MetaspaceObject base, long displacement, boolean compressed) {
 680         return getResolvedJavaType0(base, displacement, compressed);
 681     }
 682 
 683     HotSpotResolvedObjectTypeImpl getResolvedJavaType(HotSpotObjectConstantImpl base, long displacement, boolean compressed) {
 684         return getResolvedJavaType0(base, displacement, compressed);
 685     }
 686 
 687     HotSpotResolvedObjectTypeImpl getResolvedJavaType(long displacement, boolean compressed) {
 688         return getResolvedJavaType0(null, displacement, compressed);
 689     }
 690 
 691     /**
 692      * Return the size of the HotSpot ProfileData* pointed at by {@code position}. If
 693      * {@code position} is outside the space of the MethodData then an
 694      * {@link IllegalArgumentException} is thrown. A {@code position} inside the MethodData but that
 695      * isn't pointing at a valid ProfileData will crash the VM.
 696      *
 697      * @param metaspaceMethodData
 698      * @param position
 699      * @return the size of the ProfileData item pointed at by {@code position}
 700      * @throws IllegalArgumentException if an out of range position is given
 701      */
 702     native int methodDataProfileDataSize(long metaspaceMethodData, int position);
 703 
 704     /**
 705      * Gets the fingerprint for a given Klass*.
 706      *
 707      * @param metaspaceKlass
 708      * @return the value of the fingerprint (zero for arrays and synthetic classes).
 709      */
 710     native long getFingerprint(long metaspaceKlass);
 711 
 712     /**
 713      * Return the amount of native stack required for the interpreter frames represented by
 714      * {@code frame}. This is used when emitting the stack banging code to ensure that there is
 715      * enough space for the frames during deoptimization.
 716      *
 717      * @param frame
 718      * @return the number of bytes required for deoptimization of this frame state
 719      */
 720     native int interpreterFrameSize(BytecodeFrame frame);
 721 
 722     /**
 723      * Invokes non-public method {@code java.lang.invoke.LambdaForm.compileToBytecode()} on
 724      * {@code lambdaForm} (which must be a {@code java.lang.invoke.LambdaForm} instance).
 725      */
 726     native void compileToBytecode(HotSpotObjectConstantImpl lambdaForm);
 727 
 728     /**
 729      * Gets the value of the VM flag named {@code name}.
 730      *
 731      * @param name name of a VM option
 732      * @return {@code this} if the named VM option doesn't exist, a {@link String} or {@code null}
 733      *         if its type is {@code ccstr} or {@code ccstrlist}, a {@link Double} if its type is
 734      *         {@code double}, a {@link Boolean} if its type is {@code bool} otherwise a
 735      *         {@link Long}
 736      */
 737     native Object getFlagValue(String name);
 738 
 739     /**
 740      * Gets the host class for {@code type}.
 741      */
 742     native HotSpotResolvedObjectTypeImpl getHostClass(HotSpotResolvedObjectTypeImpl type);
 743 
 744     /**
 745      * Gets the object at the address {@code oopAddress}.
 746      *
 747      * @param oopAddress a valid {@code oopDesc**} value
 748      */
 749     native Object getObjectAtAddress(long oopAddress);
 750 
 751     /**
 752      * @see ResolvedJavaType#getInterfaces()
 753      */
 754     native HotSpotResolvedObjectTypeImpl[] getInterfaces(HotSpotResolvedObjectTypeImpl type);
 755 
 756     /**
 757      * @see ResolvedJavaType#getComponentType()
 758      */
 759     native HotSpotResolvedJavaType getComponentType(HotSpotResolvedObjectTypeImpl type);
 760 
 761     /**
 762      * Forces initialization of {@code type}.
 763      */
 764     native void ensureInitialized(HotSpotResolvedObjectTypeImpl type);
 765 
 766     /**
 767      * Checks if {@code object} is a String and is an interned string value.
 768      */
 769     native boolean isInternedString(HotSpotObjectConstantImpl object);
 770 
 771     /**
 772      * Gets the {@linkplain System#identityHashCode(Object) identity} has code for the object
 773      * represented by this constant.
 774      */
 775     native int getIdentityHashCode(HotSpotObjectConstantImpl object);
 776 
 777     /**
 778      * Converts a constant object representing a boxed primitive into a boxed primitive.
 779      */
 780     native Object unboxPrimitive(HotSpotObjectConstantImpl object);
 781 
 782     /**
 783      * Converts a boxed primitive into a JavaConstant representing the same value.
 784      */
 785     native HotSpotObjectConstantImpl boxPrimitive(Object source);
 786 
 787     /**
 788      * Gets the {@link ResolvedJavaMethod}s for all the constructors of the type {@code holder}.
 789      */
 790     native ResolvedJavaMethod[] getDeclaredConstructors(HotSpotResolvedObjectTypeImpl holder);
 791 
 792     /**
 793      * Gets the {@link ResolvedJavaMethod}s for all the non-constructor methods of the type
 794      * {@code holder}.
 795      */
 796     native ResolvedJavaMethod[] getDeclaredMethods(HotSpotResolvedObjectTypeImpl holder);
 797 
 798     /**
 799      * Reads the current value of a static field.
 800      */
 801     native JavaConstant readFieldValue(HotSpotResolvedObjectTypeImpl resolvedObjectType, HotSpotResolvedJavaField field, boolean isVolatile);
 802 
 803     /**
 804      * Reads the current value of an instance field.
 805      */
 806     native JavaConstant readFieldValue(HotSpotObjectConstantImpl object, HotSpotResolvedJavaField field, boolean isVolatile);
 807 
 808     /**
 809      * @see ResolvedJavaType#isInstance(JavaConstant)
 810      */
 811     native boolean isInstance(HotSpotResolvedObjectTypeImpl holder, HotSpotObjectConstantImpl object);
 812 
 813     /**
 814      * @see ResolvedJavaType#isAssignableFrom(ResolvedJavaType)
 815      */
 816     native boolean isAssignableFrom(HotSpotResolvedObjectTypeImpl holder, HotSpotResolvedObjectTypeImpl otherType);
 817 
 818     /**
 819      * @see ConstantReflectionProvider#asJavaType(Constant)
 820      */
 821     native HotSpotResolvedJavaType asJavaType(HotSpotObjectConstantImpl object);
 822 
 823     /**
 824      * Converts a String constant into a String.
 825      */
 826     native String asString(HotSpotObjectConstantImpl object);
 827 
 828     /**
 829      * Compares the contents of {@code xHandle} and {@code yHandle} for pointer equality.
 830      */
 831     native boolean equals(HotSpotObjectConstantImpl x, long xHandle, HotSpotObjectConstantImpl y, long yHandle);
 832 
 833     /**
 834      * Gets a {@link JavaConstant} wrapping the {@link java.lang.Class} mirror for {@code type}.
 835      */
 836     native HotSpotObjectConstantImpl getJavaMirror(HotSpotResolvedJavaType type);
 837 
 838     /**
 839      * Returns the length of the array if {@code object} represents an array or -1 otherwise.
 840      */
 841     native int getArrayLength(HotSpotObjectConstantImpl object);
 842 
 843     /**
 844      * Reads the element at {@code index} if {@code object} is an array. Elements of an object array
 845      * are returned as {@link JavaConstant}s and primitives are returned as boxed values. The value
 846      * {@code null} is returned if the {@code index} is out of range or object is not an array.
 847      */
 848     native Object readArrayElement(HotSpotObjectConstantImpl object, int index);
 849 
 850     /**
 851      * Reads a byte sized value from {@code displacement} in {@code object}.
 852      */
 853     native byte getByte(HotSpotObjectConstantImpl object, long displacement);
 854 
 855     /**
 856      * Reads a short sized value from {@code displacement} in {@code object}.
 857      */
 858     native short getShort(HotSpotObjectConstantImpl object, long displacement);
 859 
 860     /**
 861      * Reads an int sized value from {@code displacement} in {@code object}.
 862      */
 863     native int getInt(HotSpotObjectConstantImpl object, long displacement);
 864 
 865     /**
 866      * Reads a long sized value from {@code displacement} in {@code object}.
 867      */
 868     native long getLong(HotSpotObjectConstantImpl object, long displacement);
 869 
 870     /**
 871      * Reads a Java object from {@code displacement} in {@code object}.
 872      */
 873     native HotSpotObjectConstantImpl getObject(HotSpotObjectConstantImpl object, long displacement);
 874 
 875     /**
 876      * @see HotSpotJVMCIRuntime#registerNativeMethods
 877      */
 878     native long[] registerNativeMethods(Class<?> clazz);
 879 
 880     /**
 881      * @see HotSpotJVMCIRuntime#translate(Object)
 882      */
 883     native long translate(Object obj);
 884 
 885     /**
 886      * @see HotSpotJVMCIRuntime#unhand(Class, long)
 887      */
 888     native Object unhand(long handle);
 889 
 890     /**
 891      * Updates {@code address} and {@code entryPoint} fields of {@code nmethodMirror} based on the
 892      * current state of the {@code nmethod} identified by {@code address} and
 893      * {@code nmethodMirror.compileId} in the code cache.
 894      */
 895     native void updateHotSpotNmethod(HotSpotNmethod nmethodMirror);
 896 
 897     /**
 898      * @see InstalledCode#getCode()
 899      */
 900     native byte[] getCode(HotSpotInstalledCode code);
 901 
 902     /**
 903      * Gets a {@link Executable} corresponding to {@code method}.
 904      */
 905     native Executable asReflectionExecutable(HotSpotResolvedJavaMethodImpl method);
 906 
 907     /**
 908      * Gets a {@link Field} denoted by {@code holder} and {@code index}.
 909      *
 910      * @param holder the class in which the requested field is declared
 911      * @param fieldIndex the {@code fieldDescriptor::index()} denoting the field
 912      */
 913     native Field asReflectionField(HotSpotResolvedObjectTypeImpl holder, int fieldIndex);
 914 
 915     /**
 916      * @see HotSpotJVMCIRuntime#getIntrinsificationTrustPredicate(Class...)
 917      */
 918     native boolean isTrustedForIntrinsics(HotSpotResolvedObjectTypeImpl type);
 919 
 920     /**
 921      * Releases the resources backing the global JNI {@code handle}. This is equivalent to the
 922      * {@code DeleteGlobalRef} JNI function.
 923      */
 924     native void deleteGlobalHandle(long handle);
 925 
 926     /**
 927      * Gets the failed speculations pointed to by {@code *failedSpeculationsAddress}.
 928      *
 929      * @param currentFailures the known failures at {@code failedSpeculationsAddress}
 930      * @return the list of failed speculations with each entry being a single speculation in the
 931      *         format emitted by {@link HotSpotSpeculationEncoding#toByteArray()}
 932      */
 933     native byte[][] getFailedSpeculations(long failedSpeculationsAddress, byte[][] currentFailures);
 934 
 935     /**
 936      * Gets the address of the {@code MethodData::_failed_speculations} field in the
 937      * {@code MethodData} associated with {@code method}. This will create and install the
 938      * {@code MethodData} if it didn't already exist.
 939      */
 940     native long getFailedSpeculationsAddress(HotSpotResolvedJavaMethodImpl method);
 941 
 942     /**
 943      * Frees the failed speculations pointed to by {@code *failedSpeculationsAddress}.
 944      */
 945     native void releaseFailedSpeculations(long failedSpeculationsAddress);
 946 
 947     /**
 948      * Adds a speculation to the failed speculations pointed to by
 949      * {@code *failedSpeculationsAddress}.
 950      *
 951      * @return {@code false} if the speculation could not be appended to the list
 952      */
 953     native boolean addFailedSpeculation(long failedSpeculationsAddress, byte[] speculation);
 954 
 955     /**
 956      * @see HotSpotJVMCIRuntime#isCurrentThreadAttached()
 957      */
 958     native boolean isCurrentThreadAttached();
 959 
 960     /**
 961      * @see HotSpotJVMCIRuntime#attachCurrentThread
 962      */
 963     native boolean attachCurrentThread(boolean asDaemon);
 964 
 965     /**
 966      * @see HotSpotJVMCIRuntime#detachCurrentThread()
 967      */
 968     native void detachCurrentThread();
 969 }