Path: blob/master/src/hotspot/share/jvmci/jvmciCodeInstaller.cpp
64440 views
/*1* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#include "precompiled.hpp"24#include "classfile/javaClasses.inline.hpp"25#include "code/compiledIC.hpp"26#include "compiler/compileBroker.hpp"27#include "compiler/compilerThread.hpp"28#include "compiler/oopMap.hpp"29#include "jvmci/jvmciCodeInstaller.hpp"30#include "jvmci/jvmciCompilerToVM.hpp"31#include "jvmci/jvmciRuntime.hpp"32#include "memory/universe.hpp"33#include "oops/compressedOops.inline.hpp"34#include "oops/klass.inline.hpp"35#include "prims/jvmtiExport.hpp"36#include "prims/methodHandles.hpp"37#include "runtime/interfaceSupport.inline.hpp"38#include "runtime/jniHandles.inline.hpp"39#include "runtime/sharedRuntime.hpp"40#include "utilities/align.hpp"4142// frequently used constants43// Allocate them with new so they are never destroyed (otherwise, a44// forced exit could destroy these objects while they are still in45// use).46ConstantOopWriteValue* CodeInstaller::_oop_null_scope_value = new (ResourceObj::C_HEAP, mtJVMCI) ConstantOopWriteValue(NULL);47ConstantIntValue* CodeInstaller::_int_m1_scope_value = new (ResourceObj::C_HEAP, mtJVMCI) ConstantIntValue(-1);48ConstantIntValue* CodeInstaller::_int_0_scope_value = new (ResourceObj::C_HEAP, mtJVMCI) ConstantIntValue((jint)0);49ConstantIntValue* CodeInstaller::_int_1_scope_value = new (ResourceObj::C_HEAP, mtJVMCI) ConstantIntValue(1);50ConstantIntValue* CodeInstaller::_int_2_scope_value = new (ResourceObj::C_HEAP, mtJVMCI) ConstantIntValue(2);51LocationValue* CodeInstaller::_illegal_value = new (ResourceObj::C_HEAP, mtJVMCI) LocationValue(Location());52MarkerValue* CodeInstaller::_virtual_byte_array_marker = new (ResourceObj::C_HEAP, mtJVMCI) MarkerValue();5354VMReg CodeInstaller::getVMRegFromLocation(JVMCIObject location, int total_frame_size, JVMCI_TRAPS) {55if (location.is_null()) {56JVMCI_THROW_NULL(NullPointerException);57}5859JVMCIObject reg = jvmci_env()->get_code_Location_reg(location);60jint offset = jvmci_env()->get_code_Location_offset(location);6162if (reg.is_non_null()) {63// register64jint number = jvmci_env()->get_code_Register_number(reg);65VMReg vmReg = CodeInstaller::get_hotspot_reg(number, JVMCI_CHECK_NULL);66if (offset % 4 == 0) {67return vmReg->next(offset / 4);68} else {69JVMCI_ERROR_NULL("unaligned subregister offset %d in oop map", offset);70}71} else {72// stack slot73if (offset % 4 == 0) {74VMReg vmReg = VMRegImpl::stack2reg(offset / 4);75if (!OopMapValue::legal_vm_reg_name(vmReg)) {76// This restriction only applies to VMRegs that are used in OopMap but77// since that's the only use of VMRegs it's simplest to put this test78// here. This test should also be equivalent legal_vm_reg_name but JVMCI79// clients can use max_oop_map_stack_stack_offset to detect this problem80// directly. The asserts just ensure that the tests are in agreement.81assert(offset > CompilerToVM::Data::max_oop_map_stack_offset(), "illegal VMReg");82JVMCI_ERROR_NULL("stack offset %d is too large to be encoded in OopMap (max %d)",83offset, CompilerToVM::Data::max_oop_map_stack_offset());84}85assert(OopMapValue::legal_vm_reg_name(vmReg), "illegal VMReg");86return vmReg;87} else {88JVMCI_ERROR_NULL("unaligned stack offset %d in oop map", offset);89}90}91}9293// creates a HotSpot oop map out of the byte arrays provided by DebugInfo94OopMap* CodeInstaller::create_oop_map(JVMCIObject debug_info, JVMCI_TRAPS) {95JVMCIObject reference_map = jvmci_env()->get_DebugInfo_referenceMap(debug_info);96if (reference_map.is_null()) {97JVMCI_THROW_NULL(NullPointerException);98}99if (!jvmci_env()->isa_HotSpotReferenceMap(reference_map)) {100JVMCI_ERROR_NULL("unknown reference map: %s", jvmci_env()->klass_name(reference_map));101}102if (!_has_wide_vector && SharedRuntime::is_wide_vector(jvmci_env()->get_HotSpotReferenceMap_maxRegisterSize(reference_map))) {103if (SharedRuntime::polling_page_vectors_safepoint_handler_blob() == NULL) {104JVMCI_ERROR_NULL("JVMCI is producing code using vectors larger than the runtime supports");105}106_has_wide_vector = true;107}108OopMap* map = new OopMap(_total_frame_size, _parameter_count);109JVMCIObjectArray objects = jvmci_env()->get_HotSpotReferenceMap_objects(reference_map);110JVMCIObjectArray derivedBase = jvmci_env()->get_HotSpotReferenceMap_derivedBase(reference_map);111JVMCIPrimitiveArray sizeInBytes = jvmci_env()->get_HotSpotReferenceMap_sizeInBytes(reference_map);112if (objects.is_null() || derivedBase.is_null() || sizeInBytes.is_null()) {113JVMCI_THROW_NULL(NullPointerException);114}115if (JVMCIENV->get_length(objects) != JVMCIENV->get_length(derivedBase) || JVMCIENV->get_length(objects) != JVMCIENV->get_length(sizeInBytes)) {116JVMCI_ERROR_NULL("arrays in reference map have different sizes: %d %d %d", JVMCIENV->get_length(objects), JVMCIENV->get_length(derivedBase), JVMCIENV->get_length(sizeInBytes));117}118for (int i = 0; i < JVMCIENV->get_length(objects); i++) {119JVMCIObject location = JVMCIENV->get_object_at(objects, i);120JVMCIObject baseLocation = JVMCIENV->get_object_at(derivedBase, i);121jint bytes = JVMCIENV->get_int_at(sizeInBytes, i);122123VMReg vmReg = getVMRegFromLocation(location, _total_frame_size, JVMCI_CHECK_NULL);124if (baseLocation.is_non_null()) {125// derived oop126#ifdef _LP64127if (bytes == 8) {128#else129if (bytes == 4) {130#endif131VMReg baseReg = getVMRegFromLocation(baseLocation, _total_frame_size, JVMCI_CHECK_NULL);132map->set_derived_oop(vmReg, baseReg);133} else {134JVMCI_ERROR_NULL("invalid derived oop size in ReferenceMap: %d", bytes);135}136#ifdef _LP64137} else if (bytes == 8) {138// wide oop139map->set_oop(vmReg);140} else if (bytes == 4) {141// narrow oop142map->set_narrowoop(vmReg);143#else144} else if (bytes == 4) {145map->set_oop(vmReg);146#endif147} else {148JVMCI_ERROR_NULL("invalid oop size in ReferenceMap: %d", bytes);149}150}151152JVMCIObject callee_save_info = jvmci_env()->get_DebugInfo_calleeSaveInfo(debug_info);153if (callee_save_info.is_non_null()) {154JVMCIObjectArray registers = jvmci_env()->get_RegisterSaveLayout_registers(callee_save_info);155JVMCIPrimitiveArray slots = jvmci_env()->get_RegisterSaveLayout_slots(callee_save_info);156for (jint i = 0; i < JVMCIENV->get_length(slots); i++) {157JVMCIObject jvmci_reg = JVMCIENV->get_object_at(registers, i);158jint jvmci_reg_number = jvmci_env()->get_code_Register_number(jvmci_reg);159VMReg hotspot_reg = CodeInstaller::get_hotspot_reg(jvmci_reg_number, JVMCI_CHECK_NULL);160// HotSpot stack slots are 4 bytes161jint jvmci_slot = JVMCIENV->get_int_at(slots, i);162jint hotspot_slot = jvmci_slot * VMRegImpl::slots_per_word;163VMReg hotspot_slot_as_reg = VMRegImpl::stack2reg(hotspot_slot);164map->set_callee_saved(hotspot_slot_as_reg, hotspot_reg);165#ifdef _LP64166// (copied from generate_oop_map() in c1_Runtime1_x86.cpp)167VMReg hotspot_slot_hi_as_reg = VMRegImpl::stack2reg(hotspot_slot + 1);168map->set_callee_saved(hotspot_slot_hi_as_reg, hotspot_reg->next());169#endif170}171}172return map;173}174175void* CodeInstaller::record_metadata_reference(CodeSection* section, address dest, JVMCIObject constant, JVMCI_TRAPS) {176/*177* This method needs to return a raw (untyped) pointer, since the value of a pointer to the base178* class is in general not equal to the pointer of the subclass. When patching metaspace pointers,179* the compiler expects a direct pointer to the subclass (Klass* or Method*), not a pointer to the180* base class (Metadata* or MetaspaceObj*).181*/182JVMCIObject obj = jvmci_env()->get_HotSpotMetaspaceConstantImpl_metaspaceObject(constant);183if (jvmci_env()->isa_HotSpotResolvedObjectTypeImpl(obj)) {184Klass* klass = JVMCIENV->asKlass(obj);185assert(!jvmci_env()->get_HotSpotMetaspaceConstantImpl_compressed(constant), "unexpected compressed klass pointer %s @ " INTPTR_FORMAT, klass->name()->as_C_string(), p2i(klass));186int index = _oop_recorder->find_index(klass);187section->relocate(dest, metadata_Relocation::spec(index));188JVMCI_event_3("metadata[%d of %d] = %s", index, _oop_recorder->metadata_count(), klass->name()->as_C_string());189return klass;190} else if (jvmci_env()->isa_HotSpotResolvedJavaMethodImpl(obj)) {191Method* method = jvmci_env()->asMethod(obj);192assert(!jvmci_env()->get_HotSpotMetaspaceConstantImpl_compressed(constant), "unexpected compressed method pointer %s @ " INTPTR_FORMAT, method->name()->as_C_string(), p2i(method));193int index = _oop_recorder->find_index(method);194section->relocate(dest, metadata_Relocation::spec(index));195JVMCI_event_3("metadata[%d of %d] = %s", index, _oop_recorder->metadata_count(), method->name()->as_C_string());196return method;197} else {198JVMCI_ERROR_NULL("unexpected metadata reference for constant of type %s", jvmci_env()->klass_name(obj));199}200}201202#ifdef _LP64203narrowKlass CodeInstaller::record_narrow_metadata_reference(CodeSection* section, address dest, JVMCIObject constant, JVMCI_TRAPS) {204JVMCIObject obj = jvmci_env()->get_HotSpotMetaspaceConstantImpl_metaspaceObject(constant);205assert(jvmci_env()->get_HotSpotMetaspaceConstantImpl_compressed(constant), "unexpected uncompressed pointer");206207if (!jvmci_env()->isa_HotSpotResolvedObjectTypeImpl(obj)) {208JVMCI_ERROR_0("unexpected compressed pointer of type %s", jvmci_env()->klass_name(obj));209}210211Klass* klass = JVMCIENV->asKlass(obj);212int index = _oop_recorder->find_index(klass);213section->relocate(dest, metadata_Relocation::spec(index));214JVMCI_event_3("narrowKlass[%d of %d] = %s", index, _oop_recorder->metadata_count(), klass->name()->as_C_string());215return CompressedKlassPointers::encode(klass);216}217#endif218219Location::Type CodeInstaller::get_oop_type(JVMCIObject value) {220JVMCIObject valueKind = jvmci_env()->get_Value_valueKind(value);221JVMCIObject platformKind = jvmci_env()->get_ValueKind_platformKind(valueKind);222223if (jvmci_env()->equals(platformKind, word_kind())) {224return Location::oop;225} else {226return Location::narrowoop;227}228}229230ScopeValue* CodeInstaller::get_scope_value(JVMCIObject value, BasicType type, GrowableArray<ScopeValue*>* objects, ScopeValue* &second, JVMCI_TRAPS) {231second = NULL;232if (value.is_null()) {233JVMCI_THROW_NULL(NullPointerException);234} else if (JVMCIENV->equals(value, jvmci_env()->get_Value_ILLEGAL())) {235if (type != T_ILLEGAL) {236JVMCI_ERROR_NULL("unexpected illegal value, expected %s", basictype_to_str(type));237}238return _illegal_value;239} else if (jvmci_env()->isa_RegisterValue(value)) {240JVMCIObject reg = jvmci_env()->get_RegisterValue_reg(value);241jint number = jvmci_env()->get_code_Register_number(reg);242VMReg hotspotRegister = get_hotspot_reg(number, JVMCI_CHECK_NULL);243if (is_general_purpose_reg(hotspotRegister)) {244Location::Type locationType;245if (type == T_OBJECT) {246locationType = get_oop_type(value);247} else if (type == T_LONG) {248locationType = Location::lng;249} else if (type == T_INT || type == T_FLOAT || type == T_SHORT || type == T_CHAR || type == T_BYTE || type == T_BOOLEAN) {250locationType = Location::int_in_long;251} else {252JVMCI_ERROR_NULL("unexpected type %s in cpu register", basictype_to_str(type));253}254ScopeValue* value = new LocationValue(Location::new_reg_loc(locationType, hotspotRegister));255if (type == T_LONG) {256second = value;257}258return value;259} else {260Location::Type locationType;261if (type == T_FLOAT) {262// this seems weird, but the same value is used in c1_LinearScan263locationType = Location::normal;264} else if (type == T_DOUBLE) {265locationType = Location::dbl;266} else {267JVMCI_ERROR_NULL("unexpected type %s in floating point register", basictype_to_str(type));268}269ScopeValue* value = new LocationValue(Location::new_reg_loc(locationType, hotspotRegister));270if (type == T_DOUBLE) {271second = value;272}273return value;274}275} else if (jvmci_env()->isa_StackSlot(value)) {276jint offset = jvmci_env()->get_StackSlot_offset(value);277if (jvmci_env()->get_StackSlot_addFrameSize(value)) {278offset += _total_frame_size;279}280281Location::Type locationType;282if (type == T_OBJECT) {283locationType = get_oop_type(value);284} else if (type == T_LONG) {285locationType = Location::lng;286} else if (type == T_DOUBLE) {287locationType = Location::dbl;288} else if (type == T_INT || type == T_FLOAT || type == T_SHORT || type == T_CHAR || type == T_BYTE || type == T_BOOLEAN) {289locationType = Location::normal;290} else {291JVMCI_ERROR_NULL("unexpected type %s in stack slot", basictype_to_str(type));292}293ScopeValue* value = new LocationValue(Location::new_stk_loc(locationType, offset));294if (type == T_DOUBLE || type == T_LONG) {295second = value;296}297return value;298} else if (jvmci_env()->isa_JavaConstant(value)) {299if (jvmci_env()->isa_PrimitiveConstant(value)) {300if (jvmci_env()->isa_RawConstant(value)) {301jlong prim = jvmci_env()->get_PrimitiveConstant_primitive(value);302return new ConstantLongValue(prim);303} else {304BasicType constantType = jvmci_env()->kindToBasicType(jvmci_env()->get_PrimitiveConstant_kind(value), JVMCI_CHECK_NULL);305if (type != constantType) {306JVMCI_ERROR_NULL("primitive constant type doesn't match, expected %s but got %s", basictype_to_str(type), basictype_to_str(constantType));307}308if (type == T_INT || type == T_FLOAT) {309jint prim = (jint)jvmci_env()->get_PrimitiveConstant_primitive(value);310switch (prim) {311case -1: return _int_m1_scope_value;312case 0: return _int_0_scope_value;313case 1: return _int_1_scope_value;314case 2: return _int_2_scope_value;315default: return new ConstantIntValue(prim);316}317} else if (type == T_LONG || type == T_DOUBLE) {318jlong prim = jvmci_env()->get_PrimitiveConstant_primitive(value);319second = _int_1_scope_value;320return new ConstantLongValue(prim);321} else {322JVMCI_ERROR_NULL("unexpected primitive constant type %s", basictype_to_str(type));323}324}325} else if (jvmci_env()->isa_NullConstant(value) || jvmci_env()->isa_HotSpotCompressedNullConstant(value)) {326if (type == T_OBJECT) {327return _oop_null_scope_value;328} else {329JVMCI_ERROR_NULL("unexpected null constant, expected %s", basictype_to_str(type));330}331} else if (jvmci_env()->isa_HotSpotObjectConstantImpl(value)) {332if (type == T_OBJECT) {333Handle obj = jvmci_env()->asConstant(value, JVMCI_CHECK_NULL);334if (obj == NULL) {335JVMCI_ERROR_NULL("null value must be in NullConstant");336}337return new ConstantOopWriteValue(JNIHandles::make_local(obj()));338} else {339JVMCI_ERROR_NULL("unexpected object constant, expected %s", basictype_to_str(type));340}341}342} else if (jvmci_env()->isa_VirtualObject(value)) {343if (type == T_OBJECT) {344int id = jvmci_env()->get_VirtualObject_id(value);345if (0 <= id && id < objects->length()) {346ScopeValue* object = objects->at(id);347if (object != NULL) {348return object;349}350}351JVMCI_ERROR_NULL("unknown virtual object id %d", id);352} else {353JVMCI_ERROR_NULL("unexpected virtual object, expected %s", basictype_to_str(type));354}355}356357JVMCI_ERROR_NULL("unexpected value in scope: %s", jvmci_env()->klass_name(value))358}359360void CodeInstaller::record_object_value(ObjectValue* sv, JVMCIObject value, GrowableArray<ScopeValue*>* objects, JVMCI_TRAPS) {361JVMCIObject type = jvmci_env()->get_VirtualObject_type(value);362int id = jvmci_env()->get_VirtualObject_id(value);363Klass* klass = JVMCIENV->asKlass(type);364bool isLongArray = klass == Universe::longArrayKlassObj();365bool isByteArray = klass == Universe::byteArrayKlassObj();366367JVMCIObjectArray values = jvmci_env()->get_VirtualObject_values(value);368JVMCIObjectArray slotKinds = jvmci_env()->get_VirtualObject_slotKinds(value);369for (jint i = 0; i < JVMCIENV->get_length(values); i++) {370ScopeValue* cur_second = NULL;371JVMCIObject object = JVMCIENV->get_object_at(values, i);372BasicType type = jvmci_env()->kindToBasicType(JVMCIENV->get_object_at(slotKinds, i), JVMCI_CHECK);373ScopeValue* value;374if (JVMCIENV->equals(object, jvmci_env()->get_Value_ILLEGAL())) {375if (isByteArray && type == T_ILLEGAL) {376/*377* The difference between a virtualized large access and a deferred write is the kind stored in the slotKinds378* of the virtual object: in the virtualization case, the kind is illegal, in the deferred write case, the kind379* is access stack kind (an int).380*/381value = _virtual_byte_array_marker;382} else {383value = _illegal_value;384if (type == T_DOUBLE || type == T_LONG) {385cur_second = _illegal_value;386}387}388} else {389value = get_scope_value(object, type, objects, cur_second, JVMCI_CHECK);390}391392if (isLongArray && cur_second == NULL) {393// we're trying to put ints into a long array... this isn't really valid, but it's used for some optimizations.394// add an int 0 constant395cur_second = _int_0_scope_value;396}397398if (isByteArray && cur_second != NULL && (type == T_DOUBLE || type == T_LONG)) {399// we are trying to write a long in a byte Array. We will need to count the illegals to restore the type of400// the thing we put inside.401cur_second = NULL;402}403404if (cur_second != NULL) {405sv->field_values()->append(cur_second);406}407assert(value != NULL, "missing value");408sv->field_values()->append(value);409}410}411412MonitorValue* CodeInstaller::get_monitor_value(JVMCIObject value, GrowableArray<ScopeValue*>* objects, JVMCI_TRAPS) {413if (value.is_null()) {414JVMCI_THROW_NULL(NullPointerException);415}416if (!jvmci_env()->isa_StackLockValue(value)) {417JVMCI_ERROR_NULL("Monitors must be of type StackLockValue, got %s", jvmci_env()->klass_name(value));418}419420ScopeValue* second = NULL;421ScopeValue* owner_value = get_scope_value(jvmci_env()->get_StackLockValue_owner(value), T_OBJECT, objects, second, JVMCI_CHECK_NULL);422assert(second == NULL, "monitor cannot occupy two stack slots");423424ScopeValue* lock_data_value = get_scope_value(jvmci_env()->get_StackLockValue_slot(value), T_LONG, objects, second, JVMCI_CHECK_NULL);425assert(second == lock_data_value, "monitor is LONG value that occupies two stack slots");426assert(lock_data_value->is_location(), "invalid monitor location");427Location lock_data_loc = ((LocationValue*)lock_data_value)->location();428429bool eliminated = false;430if (jvmci_env()->get_StackLockValue_eliminated(value)) {431eliminated = true;432}433434return new MonitorValue(owner_value, lock_data_loc, eliminated);435}436437void CodeInstaller::initialize_dependencies(JVMCIObject compiled_code, OopRecorder* oop_recorder, JVMCI_TRAPS) {438JavaThread* thread = JavaThread::current();439CompilerThread* compilerThread = thread->is_Compiler_thread() ? CompilerThread::cast(thread) : NULL;440_oop_recorder = oop_recorder;441_dependencies = new Dependencies(&_arena, _oop_recorder, compilerThread != NULL ? compilerThread->log() : NULL);442JVMCIObjectArray assumptions = jvmci_env()->get_HotSpotCompiledCode_assumptions(compiled_code);443if (assumptions.is_non_null()) {444int length = JVMCIENV->get_length(assumptions);445for (int i = 0; i < length; ++i) {446JVMCIObject assumption = JVMCIENV->get_object_at(assumptions, i);447if (assumption.is_non_null()) {448if (jvmci_env()->isa_Assumptions_NoFinalizableSubclass(assumption)) {449assumption_NoFinalizableSubclass(assumption);450} else if (jvmci_env()->isa_Assumptions_ConcreteSubtype(assumption)) {451assumption_ConcreteSubtype(assumption);452} else if (jvmci_env()->isa_Assumptions_LeafType(assumption)) {453assumption_LeafType(assumption);454} else if (jvmci_env()->isa_Assumptions_ConcreteMethod(assumption)) {455assumption_ConcreteMethod(assumption);456} else if (jvmci_env()->isa_Assumptions_CallSiteTargetValue(assumption)) {457assumption_CallSiteTargetValue(assumption, JVMCI_CHECK);458} else {459JVMCI_ERROR("unexpected Assumption subclass %s", jvmci_env()->klass_name(assumption));460}461}462}463}464if (JvmtiExport::can_hotswap_or_post_breakpoint()) {465JVMCIObjectArray methods = jvmci_env()->get_HotSpotCompiledCode_methods(compiled_code);466if (methods.is_non_null()) {467int length = JVMCIENV->get_length(methods);468for (int i = 0; i < length; ++i) {469JVMCIObject method_handle = JVMCIENV->get_object_at(methods, i);470Method* method = jvmci_env()->asMethod(method_handle);471_dependencies->assert_evol_method(method);472}473}474}475}476477// constructor used to create a method478JVMCI::CodeInstallResult CodeInstaller::install(JVMCICompiler* compiler,479JVMCIObject target,480JVMCIObject compiled_code,481CodeBlob*& cb,482nmethodLocker& nmethod_handle,483JVMCIObject installed_code,484FailedSpeculation** failed_speculations,485char* speculations,486int speculations_len,487JVMCI_TRAPS) {488489CodeBuffer buffer("JVMCI Compiler CodeBuffer");490OopRecorder* recorder = new OopRecorder(&_arena, true);491initialize_dependencies(compiled_code, recorder, JVMCI_CHECK_OK);492493// Get instructions and constants CodeSections early because we need it.494_instructions = buffer.insts();495_constants = buffer.consts();496497initialize_fields(target, compiled_code, JVMCI_CHECK_OK);498JVMCI::CodeInstallResult result = initialize_buffer(buffer, true, JVMCI_CHECK_OK);499if (result != JVMCI::ok) {500return result;501}502503int stack_slots = _total_frame_size / HeapWordSize; // conversion to words504505if (!jvmci_env()->isa_HotSpotCompiledNmethod(compiled_code)) {506JVMCIObject stubName = jvmci_env()->get_HotSpotCompiledCode_name(compiled_code);507if (stubName.is_null()) {508JVMCI_ERROR_OK("stub should have a name");509}510char* name = strdup(jvmci_env()->as_utf8_string(stubName));511cb = RuntimeStub::new_runtime_stub(name,512&buffer,513_offsets.value(CodeOffsets::Frame_Complete),514stack_slots,515_debug_recorder->_oopmaps,516false);517result = JVMCI::ok;518} else {519JVMCICompileState* compile_state = (JVMCICompileState*) (address) jvmci_env()->get_HotSpotCompiledNmethod_compileState(compiled_code);520if (compile_state != NULL) {521jvmci_env()->set_compile_state(compile_state);522}523524Thread* thread = Thread::current();525526methodHandle method(thread, jvmci_env()->asMethod(jvmci_env()->get_HotSpotCompiledNmethod_method(compiled_code)));527jint entry_bci = jvmci_env()->get_HotSpotCompiledNmethod_entryBCI(compiled_code);528bool has_unsafe_access = jvmci_env()->get_HotSpotCompiledNmethod_hasUnsafeAccess(compiled_code) == JNI_TRUE;529jint id = jvmci_env()->get_HotSpotCompiledNmethod_id(compiled_code);530if (id == -1) {531// Make sure a valid compile_id is associated with every compile532id = CompileBroker::assign_compile_id_unlocked(thread, method, entry_bci);533jvmci_env()->set_HotSpotCompiledNmethod_id(compiled_code, id);534}535if (!jvmci_env()->isa_HotSpotNmethod(installed_code)) {536JVMCI_THROW_MSG_(IllegalArgumentException, "InstalledCode object must be a HotSpotNmethod when installing a HotSpotCompiledNmethod", JVMCI::ok);537}538539JVMCIObject mirror = installed_code;540result = runtime()->register_method(jvmci_env(), method, nmethod_handle, entry_bci, &_offsets, _orig_pc_offset, &buffer,541stack_slots, _debug_recorder->_oopmaps, &_exception_handler_table, &_implicit_exception_table,542compiler, _debug_recorder, _dependencies, id,543has_unsafe_access, _has_wide_vector, compiled_code, mirror,544failed_speculations, speculations, speculations_len);545if (result == JVMCI::ok) {546nmethod* nm = nmethod_handle.code()->as_nmethod_or_null();547cb = nm;548if (compile_state == NULL) {549// This compile didn't come through the CompileBroker so perform the printing here550DirectiveSet* directive = DirectivesStack::getMatchingDirective(method, compiler);551nm->maybe_print_nmethod(directive);552DirectivesStack::release(directive);553}554}555}556557if (cb != NULL) {558// Make sure the pre-calculated constants section size was correct.559guarantee((cb->code_begin() - cb->content_begin()) >= _constants_size, "%d < %d", (int)(cb->code_begin() - cb->content_begin()), _constants_size);560}561return result;562}563564void CodeInstaller::initialize_fields(JVMCIObject target, JVMCIObject compiled_code, JVMCI_TRAPS) {565if (jvmci_env()->isa_HotSpotCompiledNmethod(compiled_code)) {566JVMCIObject hotspotJavaMethod = jvmci_env()->get_HotSpotCompiledNmethod_method(compiled_code);567Thread* thread = Thread::current();568methodHandle method(thread, jvmci_env()->asMethod(hotspotJavaMethod));569_parameter_count = method->size_of_parameters();570JVMCI_event_2("installing code for %s", method->name_and_sig_as_C_string());571} else {572// Must be a HotSpotCompiledRuntimeStub.573// Only used in OopMap constructor for non-product builds574_parameter_count = 0;575}576_sites_handle = jvmci_env()->get_HotSpotCompiledCode_sites(compiled_code);577578_code_handle = jvmci_env()->get_HotSpotCompiledCode_targetCode(compiled_code);579_code_size = jvmci_env()->get_HotSpotCompiledCode_targetCodeSize(compiled_code);580_total_frame_size = jvmci_env()->get_HotSpotCompiledCode_totalFrameSize(compiled_code);581582JVMCIObject deoptRescueSlot = jvmci_env()->get_HotSpotCompiledCode_deoptRescueSlot(compiled_code);583if (deoptRescueSlot.is_null()) {584_orig_pc_offset = -1;585} else {586_orig_pc_offset = jvmci_env()->get_StackSlot_offset(deoptRescueSlot);587if (jvmci_env()->get_StackSlot_addFrameSize(deoptRescueSlot)) {588_orig_pc_offset += _total_frame_size;589}590if (_orig_pc_offset < 0) {591JVMCI_ERROR("invalid deopt rescue slot: %d", _orig_pc_offset);592}593}594595// Pre-calculate the constants section size. This is required for PC-relative addressing.596_data_section_handle = jvmci_env()->get_HotSpotCompiledCode_dataSection(compiled_code);597if ((_constants->alignment() % jvmci_env()->get_HotSpotCompiledCode_dataSectionAlignment(compiled_code)) != 0) {598JVMCI_ERROR("invalid data section alignment: %d", jvmci_env()->get_HotSpotCompiledCode_dataSectionAlignment(compiled_code));599}600_constants_size = JVMCIENV->get_length(data_section());601602_data_section_patches_handle = jvmci_env()->get_HotSpotCompiledCode_dataSectionPatches(compiled_code);603604#ifndef PRODUCT605_comments_handle = jvmci_env()->get_HotSpotCompiledCode_comments(compiled_code);606#endif607608_next_call_type = INVOKE_INVALID;609610_has_wide_vector = false;611612JVMCIObject arch = jvmci_env()->get_TargetDescription_arch(target);613_word_kind_handle = jvmci_env()->get_Architecture_wordKind(arch);614}615616int CodeInstaller::estimate_stubs_size(JVMCI_TRAPS) {617// Estimate the number of static call stubs that might be emitted.618int static_call_stubs = 0;619int trampoline_stubs = 0;620JVMCIObjectArray sites = this->sites();621for (int i = 0; i < JVMCIENV->get_length(sites); i++) {622JVMCIObject site = JVMCIENV->get_object_at(sites, i);623if (!site.is_null()) {624if (jvmci_env()->isa_site_Mark(site)) {625JVMCIObject id_obj = jvmci_env()->get_site_Mark_id(site);626if (id_obj.is_non_null()) {627if (!jvmci_env()->is_boxing_object(T_INT, id_obj)) {628JVMCI_ERROR_0("expected Integer id, got %s", jvmci_env()->klass_name(id_obj));629}630jint id = jvmci_env()->get_boxed_value(T_INT, id_obj).i;631switch (id) {632case INVOKEINTERFACE:633case INVOKEVIRTUAL:634trampoline_stubs++;635break;636case INVOKESTATIC:637case INVOKESPECIAL:638static_call_stubs++;639trampoline_stubs++;640break;641default:642break;643}644}645}646}647}648int size = static_call_stubs * CompiledStaticCall::to_interp_stub_size();649size += trampoline_stubs * CompiledStaticCall::to_trampoline_stub_size();650return size;651}652653// perform data and call relocation on the CodeBuffer654JVMCI::CodeInstallResult CodeInstaller::initialize_buffer(CodeBuffer& buffer, bool check_size, JVMCI_TRAPS) {655HandleMark hm(Thread::current());656JVMCIObjectArray sites = this->sites();657int locs_buffer_size = JVMCIENV->get_length(sites) * (relocInfo::length_limit + sizeof(relocInfo));658659// Allocate enough space in the stub section for the static call660// stubs. Stubs have extra relocs but they are managed by the stub661// section itself so they don't need to be accounted for in the662// locs_buffer above.663int stubs_size = estimate_stubs_size(JVMCI_CHECK_OK);664int total_size = align_up(_code_size, buffer.insts()->alignment()) + align_up(_constants_size, buffer.consts()->alignment()) + align_up(stubs_size, buffer.stubs()->alignment());665666if (check_size && total_size > JVMCINMethodSizeLimit) {667return JVMCI::code_too_large;668}669670buffer.initialize(total_size, locs_buffer_size);671if (buffer.blob() == NULL) {672return JVMCI::cache_full;673}674buffer.initialize_stubs_size(stubs_size);675buffer.initialize_consts_size(_constants_size);676677_debug_recorder = new DebugInformationRecorder(_oop_recorder);678_debug_recorder->set_oopmaps(new OopMapSet());679680buffer.initialize_oop_recorder(_oop_recorder);681682// copy the constant data into the newly created CodeBuffer683address end_data = _constants->start() + _constants_size;684JVMCIENV->copy_bytes_to(data_section(), (jbyte*) _constants->start(), 0, _constants_size);685_constants->set_end(end_data);686687// copy the code into the newly created CodeBuffer688address end_pc = _instructions->start() + _code_size;689guarantee(_instructions->allocates2(end_pc), "initialize should have reserved enough space for all the code");690JVMCIENV->copy_bytes_to(code(), (jbyte*) _instructions->start(), 0, _code_size);691_instructions->set_end(end_pc);692693for (int i = 0; i < JVMCIENV->get_length(data_section_patches()); i++) {694// HandleMark hm(THREAD);695JVMCIObject patch = JVMCIENV->get_object_at(data_section_patches(), i);696if (patch.is_null()) {697JVMCI_THROW_(NullPointerException, JVMCI::ok);698}699JVMCIObject reference = jvmci_env()->get_site_DataPatch_reference(patch);700if (reference.is_null()) {701JVMCI_THROW_(NullPointerException, JVMCI::ok);702}703if (!jvmci_env()->isa_site_ConstantReference(reference)) {704JVMCI_ERROR_OK("invalid patch in data section: %s", jvmci_env()->klass_name(reference));705}706JVMCIObject constant = jvmci_env()->get_site_ConstantReference_constant(reference);707if (constant.is_null()) {708JVMCI_THROW_(NullPointerException, JVMCI::ok);709}710address dest = _constants->start() + jvmci_env()->get_site_Site_pcOffset(patch);711if (jvmci_env()->isa_HotSpotMetaspaceConstantImpl(constant)) {712if (jvmci_env()->get_HotSpotMetaspaceConstantImpl_compressed(constant)) {713#ifdef _LP64714*((narrowKlass*) dest) = record_narrow_metadata_reference(_constants, dest, constant, JVMCI_CHECK_OK);715#else716JVMCI_ERROR_OK("unexpected compressed Klass* in 32-bit mode");717#endif718} else {719*((void**) dest) = record_metadata_reference(_constants, dest, constant, JVMCI_CHECK_OK);720}721} else if (jvmci_env()->isa_HotSpotObjectConstantImpl(constant)) {722Handle obj = jvmci_env()->asConstant(constant, JVMCI_CHECK_OK);723jobject value = JNIHandles::make_local(obj());724int oop_index = _oop_recorder->find_index(value);725726if (jvmci_env()->get_HotSpotObjectConstantImpl_compressed(constant)) {727#ifdef _LP64728_constants->relocate(dest, oop_Relocation::spec(oop_index), relocInfo::narrow_oop_in_const);729#else730JVMCI_ERROR_OK("unexpected compressed oop in 32-bit mode");731#endif732} else {733_constants->relocate(dest, oop_Relocation::spec(oop_index));734}735} else {736JVMCI_ERROR_OK("invalid constant in data section: %s", jvmci_env()->klass_name(constant));737}738}739jint last_pc_offset = -1;740for (int i = 0; i < JVMCIENV->get_length(sites); i++) {741// HandleMark hm(THREAD);742JVMCIObject site = JVMCIENV->get_object_at(sites, i);743if (site.is_null()) {744JVMCI_THROW_(NullPointerException, JVMCI::ok);745}746747jint pc_offset = jvmci_env()->get_site_Site_pcOffset(site);748749if (jvmci_env()->isa_site_Call(site)) {750JVMCI_event_4("call at %i", pc_offset);751site_Call(buffer, pc_offset, site, JVMCI_CHECK_OK);752} else if (jvmci_env()->isa_site_Infopoint(site)) {753// three reasons for infopoints denote actual safepoints754JVMCIObject reason = jvmci_env()->get_site_Infopoint_reason(site);755if (JVMCIENV->equals(reason, jvmci_env()->get_site_InfopointReason_SAFEPOINT()) ||756JVMCIENV->equals(reason, jvmci_env()->get_site_InfopointReason_CALL()) ||757JVMCIENV->equals(reason, jvmci_env()->get_site_InfopointReason_IMPLICIT_EXCEPTION())) {758JVMCI_event_4("safepoint at %i", pc_offset);759site_Safepoint(buffer, pc_offset, site, JVMCI_CHECK_OK);760if (_orig_pc_offset < 0) {761JVMCI_ERROR_OK("method contains safepoint, but has no deopt rescue slot");762}763if (JVMCIENV->equals(reason, jvmci_env()->get_site_InfopointReason_IMPLICIT_EXCEPTION())) {764if (jvmci_env()->isa_site_ImplicitExceptionDispatch(site)) {765jint dispatch_offset = jvmci_env()->get_site_ImplicitExceptionDispatch_dispatchOffset(site);766JVMCI_event_4("implicit exception at %i, dispatch to %i", pc_offset, dispatch_offset);767_implicit_exception_table.append(pc_offset, dispatch_offset);768} else {769JVMCI_event_4("implicit exception at %i", pc_offset);770_implicit_exception_table.add_deoptimize(pc_offset);771}772}773} else {774JVMCI_event_4("infopoint at %i", pc_offset);775site_Infopoint(buffer, pc_offset, site, JVMCI_CHECK_OK);776}777} else if (jvmci_env()->isa_site_DataPatch(site)) {778JVMCI_event_4("datapatch at %i", pc_offset);779site_DataPatch(buffer, pc_offset, site, JVMCI_CHECK_OK);780} else if (jvmci_env()->isa_site_Mark(site)) {781JVMCI_event_4("mark at %i", pc_offset);782site_Mark(buffer, pc_offset, site, JVMCI_CHECK_OK);783} else if (jvmci_env()->isa_site_ExceptionHandler(site)) {784JVMCI_event_4("exceptionhandler at %i", pc_offset);785site_ExceptionHandler(pc_offset, site);786} else {787JVMCI_ERROR_OK("unexpected site subclass: %s", jvmci_env()->klass_name(site));788}789last_pc_offset = pc_offset;790791JavaThread* thread = JavaThread::current();792if (SafepointMechanism::should_process(thread)) {793// this is a hacky way to force a safepoint check but nothing else was jumping out at me.794ThreadToNativeFromVM ttnfv(thread);795}796}797798#ifndef PRODUCT799if (comments().is_non_null()) {800for (int i = 0; i < JVMCIENV->get_length(comments()); i++) {801JVMCIObject comment = JVMCIENV->get_object_at(comments(), i);802assert(jvmci_env()->isa_HotSpotCompiledCode_Comment(comment), "cce");803jint offset = jvmci_env()->get_HotSpotCompiledCode_Comment_pcOffset(comment);804const char* text = jvmci_env()->as_utf8_string(jvmci_env()->get_HotSpotCompiledCode_Comment_text(comment));805buffer.block_comment(offset, text);806}807}808#endif809if (_has_auto_box) {810JavaThread* THREAD = JavaThread::current(); // For exception macros.811JVMCI::ensure_box_caches_initialized(CHECK_(JVMCI::ok));812}813return JVMCI::ok;814}815816void CodeInstaller::assumption_NoFinalizableSubclass(JVMCIObject assumption) {817JVMCIObject receiverType_handle = jvmci_env()->get_Assumptions_NoFinalizableSubclass_receiverType(assumption);818Klass* receiverType = jvmci_env()->asKlass(receiverType_handle);819_dependencies->assert_has_no_finalizable_subclasses(receiverType);820}821822void CodeInstaller::assumption_ConcreteSubtype(JVMCIObject assumption) {823JVMCIObject context_handle = jvmci_env()->get_Assumptions_ConcreteSubtype_context(assumption);824JVMCIObject subtype_handle = jvmci_env()->get_Assumptions_ConcreteSubtype_subtype(assumption);825Klass* context = jvmci_env()->asKlass(context_handle);826Klass* subtype = jvmci_env()->asKlass(subtype_handle);827828assert(context->is_abstract(), "");829_dependencies->assert_abstract_with_unique_concrete_subtype(context, subtype);830}831832void CodeInstaller::assumption_LeafType(JVMCIObject assumption) {833JVMCIObject context_handle = jvmci_env()->get_Assumptions_LeafType_context(assumption);834Klass* context = jvmci_env()->asKlass(context_handle);835836_dependencies->assert_leaf_type(context);837}838839void CodeInstaller::assumption_ConcreteMethod(JVMCIObject assumption) {840JVMCIObject impl_handle = jvmci_env()->get_Assumptions_ConcreteMethod_impl(assumption);841JVMCIObject context_handle = jvmci_env()->get_Assumptions_ConcreteMethod_context(assumption);842843Method* impl = jvmci_env()->asMethod(impl_handle);844Klass* context = jvmci_env()->asKlass(context_handle);845846_dependencies->assert_unique_concrete_method(context, impl);847}848849void CodeInstaller::assumption_CallSiteTargetValue(JVMCIObject assumption, JVMCI_TRAPS) {850JVMCIObject callSiteConstant = jvmci_env()->get_Assumptions_CallSiteTargetValue_callSite(assumption);851Handle callSite = jvmci_env()->asConstant(callSiteConstant, JVMCI_CHECK);852JVMCIObject methodConstant = jvmci_env()->get_Assumptions_CallSiteTargetValue_methodHandle(assumption);853Handle methodHandle = jvmci_env()->asConstant(methodConstant, JVMCI_CHECK);854_dependencies->assert_call_site_target_value(callSite(), methodHandle());855}856857void CodeInstaller::site_ExceptionHandler(jint pc_offset, JVMCIObject exc) {858jint handler_offset = jvmci_env()->get_site_ExceptionHandler_handlerPos(exc);859860// Subtable header861_exception_handler_table.add_entry(HandlerTableEntry(1, pc_offset, 0));862863// Subtable entry864_exception_handler_table.add_entry(HandlerTableEntry(-1, handler_offset, 0));865}866867// If deoptimization happens, the interpreter should reexecute these bytecodes.868// This function mainly helps the compilers to set up the reexecute bit.869static bool bytecode_should_reexecute(Bytecodes::Code code) {870switch (code) {871case Bytecodes::_invokedynamic:872case Bytecodes::_invokevirtual:873case Bytecodes::_invokeinterface:874case Bytecodes::_invokespecial:875case Bytecodes::_invokestatic:876return false;877default:878return true;879}880return true;881}882883GrowableArray<ScopeValue*>* CodeInstaller::record_virtual_objects(JVMCIObject debug_info, JVMCI_TRAPS) {884JVMCIObjectArray virtualObjects = jvmci_env()->get_DebugInfo_virtualObjectMapping(debug_info);885if (virtualObjects.is_null()) {886return NULL;887}888GrowableArray<ScopeValue*>* objects = new GrowableArray<ScopeValue*>(JVMCIENV->get_length(virtualObjects), JVMCIENV->get_length(virtualObjects), NULL);889// Create the unique ObjectValues890for (int i = 0; i < JVMCIENV->get_length(virtualObjects); i++) {891// HandleMark hm(THREAD);892JVMCIObject value = JVMCIENV->get_object_at(virtualObjects, i);893int id = jvmci_env()->get_VirtualObject_id(value);894JVMCIObject type = jvmci_env()->get_VirtualObject_type(value);895bool is_auto_box = jvmci_env()->get_VirtualObject_isAutoBox(value);896if (is_auto_box) {897_has_auto_box = true;898}899Klass* klass = jvmci_env()->asKlass(type);900oop javaMirror = klass->java_mirror();901ScopeValue *klass_sv = new ConstantOopWriteValue(JNIHandles::make_local(Thread::current(), javaMirror));902ObjectValue* sv = is_auto_box ? new AutoBoxObjectValue(id, klass_sv) : new ObjectValue(id, klass_sv);903if (id < 0 || id >= objects->length()) {904JVMCI_ERROR_NULL("virtual object id %d out of bounds", id);905}906if (objects->at(id) != NULL) {907JVMCI_ERROR_NULL("duplicate virtual object id %d", id);908}909objects->at_put(id, sv);910}911// All the values which could be referenced by the VirtualObjects912// exist, so now describe all the VirtualObjects themselves.913for (int i = 0; i < JVMCIENV->get_length(virtualObjects); i++) {914// HandleMark hm(THREAD);915JVMCIObject value = JVMCIENV->get_object_at(virtualObjects, i);916int id = jvmci_env()->get_VirtualObject_id(value);917record_object_value(objects->at(id)->as_ObjectValue(), value, objects, JVMCI_CHECK_NULL);918}919_debug_recorder->dump_object_pool(objects);920921return objects;922}923924void CodeInstaller::record_scope(jint pc_offset, JVMCIObject debug_info, ScopeMode scope_mode, bool is_mh_invoke, bool return_oop, JVMCI_TRAPS) {925JVMCIObject position = jvmci_env()->get_DebugInfo_bytecodePosition(debug_info);926if (position.is_null()) {927// Stubs do not record scope info, just oop maps928return;929}930931GrowableArray<ScopeValue*>* objectMapping;932if (scope_mode == CodeInstaller::FullFrame) {933objectMapping = record_virtual_objects(debug_info, JVMCI_CHECK);934} else {935objectMapping = NULL;936}937record_scope(pc_offset, position, scope_mode, objectMapping, is_mh_invoke, return_oop, JVMCI_CHECK);938}939940int CodeInstaller::map_jvmci_bci(int bci) {941if (bci < 0) {942if (bci == jvmci_env()->get_BytecodeFrame_BEFORE_BCI()) {943return BeforeBci;944} else if (bci == jvmci_env()->get_BytecodeFrame_AFTER_BCI()) {945return AfterBci;946} else if (bci == jvmci_env()->get_BytecodeFrame_UNWIND_BCI()) {947return UnwindBci;948} else if (bci == jvmci_env()->get_BytecodeFrame_AFTER_EXCEPTION_BCI()) {949return AfterExceptionBci;950} else if (bci == jvmci_env()->get_BytecodeFrame_UNKNOWN_BCI()) {951return UnknownBci;952} else if (bci == jvmci_env()->get_BytecodeFrame_INVALID_FRAMESTATE_BCI()) {953return InvalidFrameStateBci;954}955ShouldNotReachHere();956}957return bci;958}959960void CodeInstaller::record_scope(jint pc_offset, JVMCIObject position, ScopeMode scope_mode, GrowableArray<ScopeValue*>* objects, bool is_mh_invoke, bool return_oop, JVMCI_TRAPS) {961JVMCIObject frame;962if (scope_mode == CodeInstaller::FullFrame) {963if (!jvmci_env()->isa_BytecodeFrame(position)) {964JVMCI_ERROR("Full frame expected for debug info at %i", pc_offset);965}966frame = position;967}968JVMCIObject caller_frame = jvmci_env()->get_BytecodePosition_caller(position);969if (caller_frame.is_non_null()) {970record_scope(pc_offset, caller_frame, scope_mode, objects, is_mh_invoke, return_oop, JVMCI_CHECK);971}972973JVMCIObject hotspot_method = jvmci_env()->get_BytecodePosition_method(position);974Thread* thread = Thread::current();975methodHandle method(thread, jvmci_env()->asMethod(hotspot_method));976jint bci = map_jvmci_bci(jvmci_env()->get_BytecodePosition_bci(position));977if (bci == jvmci_env()->get_BytecodeFrame_BEFORE_BCI()) {978bci = SynchronizationEntryBCI;979}980981JVMCI_event_2("Recording scope pc_offset=%d bci=%d method=%s", pc_offset, bci, method->name_and_sig_as_C_string());982983bool reexecute = false;984if (frame.is_non_null()) {985if (bci < 0){986reexecute = false;987} else {988Bytecodes::Code code = Bytecodes::java_code_at(method(), method->bcp_from(bci));989reexecute = bytecode_should_reexecute(code);990if (frame.is_non_null()) {991reexecute = (jvmci_env()->get_BytecodeFrame_duringCall(frame) == JNI_FALSE);992}993}994}995996DebugToken* locals_token = NULL;997DebugToken* expressions_token = NULL;998DebugToken* monitors_token = NULL;999bool throw_exception = false;10001001if (frame.is_non_null()) {1002jint local_count = jvmci_env()->get_BytecodeFrame_numLocals(frame);1003jint expression_count = jvmci_env()->get_BytecodeFrame_numStack(frame);1004jint monitor_count = jvmci_env()->get_BytecodeFrame_numLocks(frame);1005JVMCIObjectArray values = jvmci_env()->get_BytecodeFrame_values(frame);1006JVMCIObjectArray slotKinds = jvmci_env()->get_BytecodeFrame_slotKinds(frame);10071008if (values.is_null() || slotKinds.is_null()) {1009JVMCI_THROW(NullPointerException);1010}1011if (local_count + expression_count + monitor_count != JVMCIENV->get_length(values)) {1012JVMCI_ERROR("unexpected values length %d in scope (%d locals, %d expressions, %d monitors)", JVMCIENV->get_length(values), local_count, expression_count, monitor_count);1013}1014if (local_count + expression_count != JVMCIENV->get_length(slotKinds)) {1015JVMCI_ERROR("unexpected slotKinds length %d in scope (%d locals, %d expressions)", JVMCIENV->get_length(slotKinds), local_count, expression_count);1016}10171018GrowableArray<ScopeValue*>* locals = local_count > 0 ? new GrowableArray<ScopeValue*> (local_count) : NULL;1019GrowableArray<ScopeValue*>* expressions = expression_count > 0 ? new GrowableArray<ScopeValue*> (expression_count) : NULL;1020GrowableArray<MonitorValue*>* monitors = monitor_count > 0 ? new GrowableArray<MonitorValue*> (monitor_count) : NULL;10211022JVMCI_event_2("Scope at bci %d with %d values", bci, JVMCIENV->get_length(values));1023JVMCI_event_2("%d locals %d expressions, %d monitors", local_count, expression_count, monitor_count);10241025for (jint i = 0; i < JVMCIENV->get_length(values); i++) {1026// HandleMark hm(THREAD);1027ScopeValue* second = NULL;1028JVMCIObject value = JVMCIENV->get_object_at(values, i);1029if (i < local_count) {1030BasicType type = jvmci_env()->kindToBasicType(JVMCIENV->get_object_at(slotKinds, i), JVMCI_CHECK);1031ScopeValue* first = get_scope_value(value, type, objects, second, JVMCI_CHECK);1032if (second != NULL) {1033locals->append(second);1034}1035locals->append(first);1036} else if (i < local_count + expression_count) {1037BasicType type = jvmci_env()->kindToBasicType(JVMCIENV->get_object_at(slotKinds, i), JVMCI_CHECK);1038ScopeValue* first = get_scope_value(value, type, objects, second, JVMCI_CHECK);1039if (second != NULL) {1040expressions->append(second);1041}1042expressions->append(first);1043} else {1044MonitorValue *monitor = get_monitor_value(value, objects, JVMCI_CHECK);1045monitors->append(monitor);1046}1047if (second != NULL) {1048i++;1049if (i >= JVMCIENV->get_length(values) || !JVMCIENV->equals(JVMCIENV->get_object_at(values, i), jvmci_env()->get_Value_ILLEGAL())) {1050JVMCI_ERROR("double-slot value not followed by Value.ILLEGAL");1051}1052}1053}10541055locals_token = _debug_recorder->create_scope_values(locals);1056expressions_token = _debug_recorder->create_scope_values(expressions);1057monitors_token = _debug_recorder->create_monitor_values(monitors);10581059throw_exception = jvmci_env()->get_BytecodeFrame_rethrowException(frame) == JNI_TRUE;1060}10611062// has_ea_local_in_scope and arg_escape should be added to JVMCI1063const bool is_opt_native = false;1064const bool has_ea_local_in_scope = false;1065const bool arg_escape = false;1066_debug_recorder->describe_scope(pc_offset, method, NULL, bci, reexecute, throw_exception, is_mh_invoke, is_opt_native, return_oop,1067has_ea_local_in_scope, arg_escape,1068locals_token, expressions_token, monitors_token);1069}10701071void CodeInstaller::site_Safepoint(CodeBuffer& buffer, jint pc_offset, JVMCIObject site, JVMCI_TRAPS) {1072JVMCIObject debug_info = jvmci_env()->get_site_Infopoint_debugInfo(site);1073if (debug_info.is_null()) {1074JVMCI_ERROR("debug info expected at safepoint at %i", pc_offset);1075}10761077// address instruction = _instructions->start() + pc_offset;1078// jint next_pc_offset = Assembler::locate_next_instruction(instruction) - _instructions->start();1079OopMap *map = create_oop_map(debug_info, JVMCI_CHECK);1080_debug_recorder->add_safepoint(pc_offset, map);1081record_scope(pc_offset, debug_info, CodeInstaller::FullFrame, JVMCI_CHECK);1082_debug_recorder->end_safepoint(pc_offset);1083}10841085void CodeInstaller::site_Infopoint(CodeBuffer& buffer, jint pc_offset, JVMCIObject site, JVMCI_TRAPS) {1086JVMCIObject debug_info = jvmci_env()->get_site_Infopoint_debugInfo(site);1087if (debug_info.is_null()) {1088JVMCI_ERROR("debug info expected at infopoint at %i", pc_offset);1089}10901091// We'd like to check that pc_offset is greater than the1092// last pc recorded with _debug_recorder (raising an exception if not)1093// but DebugInformationRecorder doesn't have sufficient public API.10941095_debug_recorder->add_non_safepoint(pc_offset);1096record_scope(pc_offset, debug_info, CodeInstaller::BytecodePosition, JVMCI_CHECK);1097_debug_recorder->end_non_safepoint(pc_offset);1098}10991100void CodeInstaller::site_Call(CodeBuffer& buffer, jint pc_offset, JVMCIObject site, JVMCI_TRAPS) {1101JVMCIObject target = jvmci_env()->get_site_Call_target(site);1102JVMCIObject hotspot_method; // JavaMethod1103JVMCIObject foreign_call;11041105if (jvmci_env()->isa_HotSpotForeignCallTarget(target)) {1106foreign_call = target;1107} else {1108hotspot_method = target;1109}11101111JVMCIObject debug_info = jvmci_env()->get_site_Infopoint_debugInfo(site);11121113assert(hotspot_method.is_non_null() ^ foreign_call.is_non_null(), "Call site needs exactly one type");11141115NativeInstruction* inst = nativeInstruction_at(_instructions->start() + pc_offset);1116jint next_pc_offset = CodeInstaller::pd_next_offset(inst, pc_offset, hotspot_method, JVMCI_CHECK);11171118if (debug_info.is_non_null()) {1119OopMap *map = create_oop_map(debug_info, JVMCI_CHECK);1120_debug_recorder->add_safepoint(next_pc_offset, map);11211122if (hotspot_method.is_non_null()) {1123Method *method = jvmci_env()->asMethod(hotspot_method);1124vmIntrinsics::ID iid = method->intrinsic_id();1125bool is_mh_invoke = false;1126if (jvmci_env()->get_site_Call_direct(site)) {1127is_mh_invoke = !method->is_static() && (iid == vmIntrinsics::_compiledLambdaForm ||1128(MethodHandles::is_signature_polymorphic(iid) && MethodHandles::is_signature_polymorphic_intrinsic(iid)));1129}1130bool return_oop = method->is_returning_oop();1131record_scope(next_pc_offset, debug_info, CodeInstaller::FullFrame, is_mh_invoke, return_oop, JVMCI_CHECK);1132} else {1133record_scope(next_pc_offset, debug_info, CodeInstaller::FullFrame, JVMCI_CHECK);1134}1135}11361137if (foreign_call.is_non_null()) {1138jlong foreign_call_destination = jvmci_env()->get_HotSpotForeignCallTarget_address(foreign_call);1139CodeInstaller::pd_relocate_ForeignCall(inst, foreign_call_destination, JVMCI_CHECK);1140} else { // method != NULL1141if (debug_info.is_null()) {1142JVMCI_ERROR("debug info expected at call at %i", pc_offset);1143}11441145JVMCI_event_3("method call");1146CodeInstaller::pd_relocate_JavaMethod(buffer, hotspot_method, pc_offset, JVMCI_CHECK);1147if (_next_call_type == INVOKESTATIC || _next_call_type == INVOKESPECIAL) {1148// Need a static call stub for transitions from compiled to interpreted.1149CompiledStaticCall::emit_to_interp_stub(buffer, _instructions->start() + pc_offset);1150}1151}11521153_next_call_type = INVOKE_INVALID;11541155if (debug_info.is_non_null()) {1156_debug_recorder->end_safepoint(next_pc_offset);1157}1158}11591160void CodeInstaller::site_DataPatch(CodeBuffer& buffer, jint pc_offset, JVMCIObject site, JVMCI_TRAPS) {1161JVMCIObject reference = jvmci_env()->get_site_DataPatch_reference(site);1162if (reference.is_null()) {1163JVMCI_THROW(NullPointerException);1164} else if (jvmci_env()->isa_site_ConstantReference(reference)) {1165JVMCIObject constant = jvmci_env()->get_site_ConstantReference_constant(reference);1166if (constant.is_null()) {1167JVMCI_THROW(NullPointerException);1168} else if (jvmci_env()->isa_DirectHotSpotObjectConstantImpl(constant)) {1169if (!JVMCIENV->is_hotspot()) {1170JVMCIObject string = JVMCIENV->call_HotSpotJVMCIRuntime_callToString(constant, JVMCI_CHECK);1171const char* to_string = JVMCIENV->as_utf8_string(string);1172JVMCI_THROW_MSG(IllegalArgumentException, err_msg("Direct object constant reached the backend: %s", to_string));1173}1174pd_patch_OopConstant(pc_offset, constant, JVMCI_CHECK);1175} else if (jvmci_env()->isa_IndirectHotSpotObjectConstantImpl(constant)) {1176pd_patch_OopConstant(pc_offset, constant, JVMCI_CHECK);1177} else if (jvmci_env()->isa_HotSpotMetaspaceConstantImpl(constant)) {1178pd_patch_MetaspaceConstant(pc_offset, constant, JVMCI_CHECK);1179} else {1180JVMCI_ERROR("unknown constant type in data patch: %s", jvmci_env()->klass_name(constant));1181}1182} else if (jvmci_env()->isa_site_DataSectionReference(reference)) {1183int data_offset = jvmci_env()->get_site_DataSectionReference_offset(reference);1184if (0 <= data_offset && data_offset < _constants_size) {1185pd_patch_DataSectionReference(pc_offset, data_offset, JVMCI_CHECK);1186} else {1187JVMCI_ERROR("data offset 0x%X points outside data section (size 0x%X)", data_offset, _constants_size);1188}1189} else {1190JVMCI_ERROR("unknown data patch type: %s", jvmci_env()->klass_name(reference));1191}1192}11931194void CodeInstaller::site_Mark(CodeBuffer& buffer, jint pc_offset, JVMCIObject site, JVMCI_TRAPS) {1195JVMCIObject id_obj = jvmci_env()->get_site_Mark_id(site);11961197if (id_obj.is_non_null()) {1198if (!jvmci_env()->is_boxing_object(T_INT, id_obj)) {1199JVMCI_ERROR("expected Integer id, got %s", jvmci_env()->klass_name(id_obj));1200}1201jint id = jvmci_env()->get_boxed_value(T_INT, id_obj).i;12021203address pc = _instructions->start() + pc_offset;12041205switch (id) {1206case UNVERIFIED_ENTRY:1207_offsets.set_value(CodeOffsets::Entry, pc_offset);1208break;1209case VERIFIED_ENTRY:1210_offsets.set_value(CodeOffsets::Verified_Entry, pc_offset);1211break;1212case OSR_ENTRY:1213_offsets.set_value(CodeOffsets::OSR_Entry, pc_offset);1214break;1215case EXCEPTION_HANDLER_ENTRY:1216_offsets.set_value(CodeOffsets::Exceptions, pc_offset);1217break;1218case DEOPT_HANDLER_ENTRY:1219_offsets.set_value(CodeOffsets::Deopt, pc_offset);1220break;1221case DEOPT_MH_HANDLER_ENTRY:1222_offsets.set_value(CodeOffsets::DeoptMH, pc_offset);1223break;1224case FRAME_COMPLETE:1225_offsets.set_value(CodeOffsets::Frame_Complete, pc_offset);1226break;1227case INVOKEVIRTUAL:1228case INVOKEINTERFACE:1229case INLINE_INVOKE:1230case INVOKESTATIC:1231case INVOKESPECIAL:1232_next_call_type = (MarkId) id;1233_invoke_mark_pc = pc;1234break;1235case POLL_NEAR:1236case POLL_FAR:1237case POLL_RETURN_NEAR:1238case POLL_RETURN_FAR:1239pd_relocate_poll(pc, id, JVMCI_CHECK);1240break;1241case CARD_TABLE_SHIFT:1242case CARD_TABLE_ADDRESS:1243case HEAP_TOP_ADDRESS:1244case HEAP_END_ADDRESS:1245case NARROW_KLASS_BASE_ADDRESS:1246case NARROW_OOP_BASE_ADDRESS:1247case CRC_TABLE_ADDRESS:1248case LOG_OF_HEAP_REGION_GRAIN_BYTES:1249case INLINE_CONTIGUOUS_ALLOCATION_SUPPORTED:1250case VERIFY_OOPS:1251case VERIFY_OOP_BITS:1252case VERIFY_OOP_MASK:1253case VERIFY_OOP_COUNT_ADDRESS:1254break;1255default:1256JVMCI_ERROR("invalid mark id: %d", id);1257break;1258}1259}1260}126112621263