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 * Get the current number of counters allocated for use by JVMCI. Should be the same value as 564 * the flag {@code JVMCICounterSize}. 565 */ 566 native int getCountersSize(); 567 568 /** 569 * Attempt to change the size of the counters allocated for JVMCI. This requires a safepoint to 570 * safely reallocate the storage but it's advisable to increase the size in reasonable chunks. 571 */ 572 native boolean setCountersSize(int newSize); 573 574 /** 575 * Determines if {@code metaspaceMethodData} is mature. 576 */ 577 native boolean isMature(long metaspaceMethodData); 578 579 /** 580 * Generate a unique id to identify the result of the compile. 581 */ 582 native int allocateCompileId(HotSpotResolvedJavaMethodImpl method, int entryBCI); 583 584 /** 585 * Determines if {@code method} has OSR compiled code identified by {@code entryBCI} for 586 * compilation level {@code level}. 587 */ 588 native boolean hasCompiledCodeForOSR(HotSpotResolvedJavaMethodImpl method, int entryBCI, int level); 589 590 /** 591 * Gets the value of {@code metaspaceSymbol} as a String. 592 */ 593 native String getSymbol(long metaspaceSymbol); 594 595 /** 596 * @see jdk.vm.ci.code.stack.StackIntrospection#iterateFrames 597 */ 598 native <T> T iterateFrames(ResolvedJavaMethod[] initialMethods, ResolvedJavaMethod[] matchingMethods, int initialSkip, InspectedFrameVisitor<T> visitor); 599 600 /** 601 * Materializes all virtual objects within {@code stackFrame} and updates its locals. 602 * 603 * @param invalidate if {@code true}, the compiled method for the stack frame will be 604 * invalidated 605 */ 606 native void materializeVirtualObjects(HotSpotStackFrameReference stackFrame, boolean invalidate); 607 608 /** 609 * Gets the v-table index for interface method {@code method} in the receiver {@code type} or 610 * {@link HotSpotVMConfig#invalidVtableIndex} if {@code method} is not in {@code type}'s 611 * v-table. 612 * 613 * @throws InternalError if {@code type} is an interface or {@code method} is not held by an 614 * interface or class represented by {@code type} is not initialized 615 */ 616 native int getVtableIndexForInterfaceMethod(HotSpotResolvedObjectTypeImpl type, HotSpotResolvedJavaMethodImpl method); 617 618 /** 619 * Determines if debug info should also be emitted at non-safepoint locations. 620 */ 621 native boolean shouldDebugNonSafepoints(); 622 623 /** 624 * Writes {@code length} bytes from {@code bytes} starting at offset {@code offset} to HotSpot's 625 * log stream. 626 * 627 * @param flush specifies if the log stream should be flushed after writing 628 * @param canThrow specifies if an error in the {@code bytes}, {@code offset} or {@code length} 629 * arguments should result in an exception or a negative return value 630 * @return 0 on success, -1 if {@code bytes == null && !canThrow}, -2 if {@code !canThrow} and 631 * copying would cause access of data outside array bounds 632 * @throws NullPointerException if {@code bytes == null} 633 * @throws IndexOutOfBoundsException if copying would cause access of data outside array bounds 634 */ 635 native int writeDebugOutput(byte[] bytes, int offset, int length, boolean flush, boolean canThrow); 636 637 /** 638 * Flush HotSpot's log stream. 639 */ 640 native void flushDebugOutput(); 641 642 /** 643 * Read a HotSpot Method* value from the memory location described by {@code base} plus 644 * {@code displacement} and return the {@link HotSpotResolvedJavaMethodImpl} wrapping it. This 645 * method does no checking that the memory location actually contains a valid pointer and may 646 * crash the VM if an invalid location is provided. If the {@code base} is null then 647 * {@code displacement} is used by itself. If {@code base} is a 648 * {@link HotSpotResolvedJavaMethodImpl}, {@link HotSpotConstantPool} or 649 * {@link HotSpotResolvedObjectTypeImpl} then the metaspace pointer is fetched from that object 650 * and added to {@code displacement}. Any other non-null object type causes an 651 * {@link IllegalArgumentException} to be thrown. 652 * 653 * @param base an object to read from or null 654 * @param displacement 655 * @return null or the resolved method for this location 656 */ 657 native HotSpotResolvedJavaMethodImpl getResolvedJavaMethod(HotSpotObjectConstantImpl base, long displacement); 658 659 /** 660 * Gets the {@code ConstantPool*} associated with {@code object} and returns a 661 * {@link HotSpotConstantPool} wrapping it. 662 * 663 * @param object a {@link HotSpotResolvedJavaMethodImpl} or 664 * {@link HotSpotResolvedObjectTypeImpl} object 665 * @return a {@link HotSpotConstantPool} wrapping the {@code ConstantPool*} associated with 666 * {@code object} 667 * @throws NullPointerException if {@code object == null} 668 * @throws IllegalArgumentException if {@code object} is neither a 669 * {@link HotSpotResolvedJavaMethodImpl} nor a {@link HotSpotResolvedObjectTypeImpl} 670 */ 671 native HotSpotConstantPool getConstantPool(MetaspaceObject object); 672 673 /** 674 * Read a HotSpot Klass* value from the memory location described by {@code base} plus 675 * {@code displacement} and return the {@link HotSpotResolvedObjectTypeImpl} wrapping it. This 676 * method does no checking that the memory location actually contains a valid pointer and may 677 * crash the VM if an invalid location is provided. If the {@code base} is null then 678 * {@code displacement} is used by itself. If {@code base} is a 679 * {@link HotSpotResolvedJavaMethodImpl}, {@link HotSpotConstantPool} or 680 * {@link HotSpotResolvedObjectTypeImpl} then the metaspace pointer is fetched from that object 681 * and added to {@code displacement}. Any other non-null object type causes an 682 * {@link IllegalArgumentException} to be thrown. 683 * 684 * @param base an object to read from or null 685 * @param displacement 686 * @param compressed true if the location contains a compressed Klass* 687 * @return null or the resolved method for this location 688 */ 689 private native HotSpotResolvedObjectTypeImpl getResolvedJavaType0(Object base, long displacement, boolean compressed); 690 691 HotSpotResolvedObjectTypeImpl getResolvedJavaType(MetaspaceObject base, long displacement, boolean compressed) { 692 return getResolvedJavaType0(base, displacement, compressed); 693 } 694 695 HotSpotResolvedObjectTypeImpl getResolvedJavaType(HotSpotObjectConstantImpl base, long displacement, boolean compressed) { 696 return getResolvedJavaType0(base, displacement, compressed); 697 } 698 699 HotSpotResolvedObjectTypeImpl getResolvedJavaType(long displacement, boolean compressed) { 700 return getResolvedJavaType0(null, displacement, compressed); 701 } 702 703 /** 704 * Return the size of the HotSpot ProfileData* pointed at by {@code position}. If 705 * {@code position} is outside the space of the MethodData then an 706 * {@link IllegalArgumentException} is thrown. A {@code position} inside the MethodData but that 707 * isn't pointing at a valid ProfileData will crash the VM. 708 * 709 * @param metaspaceMethodData 710 * @param position 711 * @return the size of the ProfileData item pointed at by {@code position} 712 * @throws IllegalArgumentException if an out of range position is given 713 */ 714 native int methodDataProfileDataSize(long metaspaceMethodData, int position); 715 716 /** 717 * Gets the fingerprint for a given Klass*. 718 * 719 * @param metaspaceKlass 720 * @return the value of the fingerprint (zero for arrays and synthetic classes). 721 */ 722 native long getFingerprint(long metaspaceKlass); 723 724 /** 725 * Return the amount of native stack required for the interpreter frames represented by 726 * {@code frame}. This is used when emitting the stack banging code to ensure that there is 727 * enough space for the frames during deoptimization. 728 * 729 * @param frame 730 * @return the number of bytes required for deoptimization of this frame state 731 */ 732 native int interpreterFrameSize(BytecodeFrame frame); 733 734 /** 735 * Invokes non-public method {@code java.lang.invoke.LambdaForm.compileToBytecode()} on 736 * {@code lambdaForm} (which must be a {@code java.lang.invoke.LambdaForm} instance). 737 */ 738 native void compileToBytecode(HotSpotObjectConstantImpl lambdaForm); 739 740 /** 741 * Gets the value of the VM flag named {@code name}. 742 * 743 * @param name name of a VM option 744 * @return {@code this} if the named VM option doesn't exist, a {@link String} or {@code null} 745 * if its type is {@code ccstr} or {@code ccstrlist}, a {@link Double} if its type is 746 * {@code double}, a {@link Boolean} if its type is {@code bool} otherwise a 747 * {@link Long} 748 */ 749 native Object getFlagValue(String name); 750 751 /** 752 * Gets the host class for {@code type}. 753 */ 754 native HotSpotResolvedObjectTypeImpl getHostClass(HotSpotResolvedObjectTypeImpl type); 755 756 /** 757 * Gets the object at the address {@code oopAddress}. 758 * 759 * @param oopAddress a valid {@code oopDesc**} value 760 */ 761 native Object getObjectAtAddress(long oopAddress); 762 763 /** 764 * @see ResolvedJavaType#getInterfaces() 765 */ 766 native HotSpotResolvedObjectTypeImpl[] getInterfaces(HotSpotResolvedObjectTypeImpl type); 767 768 /** 769 * @see ResolvedJavaType#getComponentType() 770 */ 771 native HotSpotResolvedJavaType getComponentType(HotSpotResolvedObjectTypeImpl type); 772 773 /** 774 * Forces initialization of {@code type}. 775 */ 776 native void ensureInitialized(HotSpotResolvedObjectTypeImpl type); 777 778 /** 779 * Checks if {@code object} is a String and is an interned string value. 780 */ 781 native boolean isInternedString(HotSpotObjectConstantImpl object); 782 783 /** 784 * Gets the {@linkplain System#identityHashCode(Object) identity} has code for the object 785 * represented by this constant. 786 */ 787 native int getIdentityHashCode(HotSpotObjectConstantImpl object); 788 789 /** 790 * Converts a constant object representing a boxed primitive into a boxed primitive. 791 */ 792 native Object unboxPrimitive(HotSpotObjectConstantImpl object); 793 794 /** 795 * Converts a boxed primitive into a JavaConstant representing the same value. 796 */ 797 native HotSpotObjectConstantImpl boxPrimitive(Object source); 798 799 /** 800 * Gets the {@link ResolvedJavaMethod}s for all the constructors of the type {@code holder}. 801 */ 802 native ResolvedJavaMethod[] getDeclaredConstructors(HotSpotResolvedObjectTypeImpl holder); 803 804 /** 805 * Gets the {@link ResolvedJavaMethod}s for all the non-constructor methods of the type 806 * {@code holder}. 807 */ 808 native ResolvedJavaMethod[] getDeclaredMethods(HotSpotResolvedObjectTypeImpl holder); 809 810 /** 811 * Reads the current value of a static field. 812 */ 813 native JavaConstant readFieldValue(HotSpotResolvedObjectTypeImpl resolvedObjectType, HotSpotResolvedJavaField field, boolean isVolatile); 814 815 /** 816 * Reads the current value of an instance field. 817 */ 818 native JavaConstant readFieldValue(HotSpotObjectConstantImpl object, HotSpotResolvedJavaField field, boolean isVolatile); 819 820 /** 821 * @see ResolvedJavaType#isInstance(JavaConstant) 822 */ 823 native boolean isInstance(HotSpotResolvedObjectTypeImpl holder, HotSpotObjectConstantImpl object); 824 825 /** 826 * @see ResolvedJavaType#isAssignableFrom(ResolvedJavaType) 827 */ 828 native boolean isAssignableFrom(HotSpotResolvedObjectTypeImpl holder, HotSpotResolvedObjectTypeImpl otherType); 829 830 /** 831 * @see ConstantReflectionProvider#asJavaType(Constant) 832 */ 833 native HotSpotResolvedJavaType asJavaType(HotSpotObjectConstantImpl object); 834 835 /** 836 * Converts a String constant into a String. 837 */ 838 native String asString(HotSpotObjectConstantImpl object); 839 840 /** 841 * Compares the contents of {@code xHandle} and {@code yHandle} for pointer equality. 842 */ 843 native boolean equals(HotSpotObjectConstantImpl x, long xHandle, HotSpotObjectConstantImpl y, long yHandle); 844 845 /** 846 * Gets a {@link JavaConstant} wrapping the {@link java.lang.Class} mirror for {@code type}. 847 */ 848 native HotSpotObjectConstantImpl getJavaMirror(HotSpotResolvedJavaType type); 849 850 /** 851 * Returns the length of the array if {@code object} represents an array or -1 otherwise. 852 */ 853 native int getArrayLength(HotSpotObjectConstantImpl object); 854 855 /** 856 * Reads the element at {@code index} if {@code object} is an array. Elements of an object array 857 * are returned as {@link JavaConstant}s and primitives are returned as boxed values. The value 858 * {@code null} is returned if the {@code index} is out of range or object is not an array. 859 */ 860 native Object readArrayElement(HotSpotObjectConstantImpl object, int index); 861 862 /** 863 * Reads a byte sized value from {@code displacement} in {@code object}. 864 */ 865 native byte getByte(HotSpotObjectConstantImpl object, long displacement); 866 867 /** 868 * Reads a short sized value from {@code displacement} in {@code object}. 869 */ 870 native short getShort(HotSpotObjectConstantImpl object, long displacement); 871 872 /** 873 * Reads an int sized value from {@code displacement} in {@code object}. 874 */ 875 native int getInt(HotSpotObjectConstantImpl object, long displacement); 876 877 /** 878 * Reads a long sized value from {@code displacement} in {@code object}. 879 */ 880 native long getLong(HotSpotObjectConstantImpl object, long displacement); 881 882 /** 883 * Reads a Java object from {@code displacement} in {@code object}. 884 */ 885 native HotSpotObjectConstantImpl getObject(HotSpotObjectConstantImpl object, long displacement); 886 887 /** 888 * @see HotSpotJVMCIRuntime#registerNativeMethods 889 */ 890 native long[] registerNativeMethods(Class<?> clazz); 891 892 /** 893 * @see HotSpotJVMCIRuntime#translate(Object) 894 */ 895 native long translate(Object obj); 896 897 /** 898 * @see HotSpotJVMCIRuntime#unhand(Class, long) 899 */ 900 native Object unhand(long handle); 901 902 /** 903 * Updates {@code address} and {@code entryPoint} fields of {@code nmethodMirror} based on the 904 * current state of the {@code nmethod} identified by {@code address} and 905 * {@code nmethodMirror.compileId} in the code cache. 906 */ 907 native void updateHotSpotNmethod(HotSpotNmethod nmethodMirror); 908 909 /** 910 * @see InstalledCode#getCode() 911 */ 912 native byte[] getCode(HotSpotInstalledCode code); 913 914 /** 915 * Gets a {@link Executable} corresponding to {@code method}. 916 */ 917 native Executable asReflectionExecutable(HotSpotResolvedJavaMethodImpl method); 918 919 /** 920 * Gets a {@link Field} denoted by {@code holder} and {@code index}. 921 * 922 * @param holder the class in which the requested field is declared 923 * @param fieldIndex the {@code fieldDescriptor::index()} denoting the field 924 */ 925 native Field asReflectionField(HotSpotResolvedObjectTypeImpl holder, int fieldIndex); 926 927 /** 928 * @see HotSpotJVMCIRuntime#getIntrinsificationTrustPredicate(Class...) 929 */ 930 native boolean isTrustedForIntrinsics(HotSpotResolvedObjectTypeImpl type); 931 932 /** 933 * Releases the resources backing the global JNI {@code handle}. This is equivalent to the 934 * {@code DeleteGlobalRef} JNI function. 935 */ 936 native void deleteGlobalHandle(long handle); 937 938 /** 939 * Gets the failed speculations pointed to by {@code *failedSpeculationsAddress}. 940 * 941 * @param currentFailures the known failures at {@code failedSpeculationsAddress} 942 * @return the list of failed speculations with each entry being a single speculation in the 943 * format emitted by {@link HotSpotSpeculationEncoding#toByteArray()} 944 */ 945 native byte[][] getFailedSpeculations(long failedSpeculationsAddress, byte[][] currentFailures); 946 947 /** 948 * Gets the address of the {@code MethodData::_failed_speculations} field in the 949 * {@code MethodData} associated with {@code method}. This will create and install the 950 * {@code MethodData} if it didn't already exist. 951 */ 952 native long getFailedSpeculationsAddress(HotSpotResolvedJavaMethodImpl method); 953 954 /** 955 * Frees the failed speculations pointed to by {@code *failedSpeculationsAddress}. 956 */ 957 native void releaseFailedSpeculations(long failedSpeculationsAddress); 958 959 /** 960 * Adds a speculation to the failed speculations pointed to by 961 * {@code *failedSpeculationsAddress}. 962 * 963 * @return {@code false} if the speculation could not be appended to the list 964 */ 965 native boolean addFailedSpeculation(long failedSpeculationsAddress, byte[] speculation); 966 967 /** 968 * @see HotSpotJVMCIRuntime#isCurrentThreadAttached() 969 */ 970 native boolean isCurrentThreadAttached(); 971 972 /** 973 * @see HotSpotJVMCIRuntime#attachCurrentThread 974 */ 975 native boolean attachCurrentThread(boolean asDaemon); 976 977 /** 978 * @see HotSpotJVMCIRuntime#detachCurrentThread() 979 */ 980 native void detachCurrentThread(); 981 }