1 /*
   2  * Copyright (c) 2011, 2021, 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 #include "precompiled.hpp"
  25 #include "classfile/javaClasses.inline.hpp"
  26 #include "code/compiledIC.hpp"
  27 #include "compiler/compileBroker.hpp"
  28 #include "compiler/compilerThread.hpp"
  29 #include "compiler/oopMap.hpp"
  30 #include "jvmci/jvmciCodeInstaller.hpp"
  31 #include "jvmci/jvmciCompilerToVM.hpp"
  32 #include "jvmci/jvmciRuntime.hpp"
  33 #include "memory/universe.hpp"
  34 #include "oops/compressedOops.inline.hpp"
  35 #include "oops/klass.inline.hpp"
  36 #include "prims/jvmtiExport.hpp"
  37 #include "prims/methodHandles.hpp"
  38 #include "runtime/interfaceSupport.inline.hpp"
  39 #include "runtime/jniHandles.inline.hpp"
  40 #include "runtime/sharedRuntime.hpp"
  41 #include "utilities/align.hpp"
  42 
  43 // frequently used constants
  44 // Allocate them with new so they are never destroyed (otherwise, a
  45 // forced exit could destroy these objects while they are still in
  46 // use).
  47 ConstantOopWriteValue* CodeInstaller::_oop_null_scope_value = new (ResourceObj::C_HEAP, mtJVMCI) ConstantOopWriteValue(NULL);
  48 ConstantIntValue*      CodeInstaller::_int_m1_scope_value = new (ResourceObj::C_HEAP, mtJVMCI) ConstantIntValue(-1);
  49 ConstantIntValue*      CodeInstaller::_int_0_scope_value =  new (ResourceObj::C_HEAP, mtJVMCI) ConstantIntValue((jint)0);
  50 ConstantIntValue*      CodeInstaller::_int_1_scope_value =  new (ResourceObj::C_HEAP, mtJVMCI) ConstantIntValue(1);
  51 ConstantIntValue*      CodeInstaller::_int_2_scope_value =  new (ResourceObj::C_HEAP, mtJVMCI) ConstantIntValue(2);
  52 LocationValue*         CodeInstaller::_illegal_value = new (ResourceObj::C_HEAP, mtJVMCI) LocationValue(Location());
  53 MarkerValue*           CodeInstaller::_virtual_byte_array_marker = new (ResourceObj::C_HEAP, mtJVMCI) MarkerValue();
  54 
  55 VMReg CodeInstaller::getVMRegFromLocation(JVMCIObject location, int total_frame_size, JVMCI_TRAPS) {
  56   if (location.is_null()) {
  57     JVMCI_THROW_NULL(NullPointerException);
  58   }
  59 
  60   JVMCIObject reg = jvmci_env()->get_code_Location_reg(location);
  61   jint offset = jvmci_env()->get_code_Location_offset(location);
  62 
  63   if (reg.is_non_null()) {
  64     // register
  65     jint number = jvmci_env()->get_code_Register_number(reg);
  66     VMReg vmReg = CodeInstaller::get_hotspot_reg(number, JVMCI_CHECK_NULL);
  67     if (offset % 4 == 0) {
  68       return vmReg->next(offset / 4);
  69     } else {
  70       JVMCI_ERROR_NULL("unaligned subregister offset %d in oop map", offset);
  71     }
  72   } else {
  73     // stack slot
  74     if (offset % 4 == 0) {
  75       VMReg vmReg = VMRegImpl::stack2reg(offset / 4);
  76       if (!OopMapValue::legal_vm_reg_name(vmReg)) {
  77         // This restriction only applies to VMRegs that are used in OopMap but
  78         // since that's the only use of VMRegs it's simplest to put this test
  79         // here.  This test should also be equivalent legal_vm_reg_name but JVMCI
  80         // clients can use max_oop_map_stack_stack_offset to detect this problem
  81         // directly.  The asserts just ensure that the tests are in agreement.
  82         assert(offset > CompilerToVM::Data::max_oop_map_stack_offset(), "illegal VMReg");
  83         JVMCI_ERROR_NULL("stack offset %d is too large to be encoded in OopMap (max %d)",
  84                          offset, CompilerToVM::Data::max_oop_map_stack_offset());
  85       }
  86       assert(OopMapValue::legal_vm_reg_name(vmReg), "illegal VMReg");
  87       return vmReg;
  88     } else {
  89       JVMCI_ERROR_NULL("unaligned stack offset %d in oop map", offset);
  90     }
  91   }
  92 }
  93 
  94 // creates a HotSpot oop map out of the byte arrays provided by DebugInfo
  95 OopMap* CodeInstaller::create_oop_map(JVMCIObject debug_info, JVMCI_TRAPS) {
  96   JVMCIObject reference_map = jvmci_env()->get_DebugInfo_referenceMap(debug_info);
  97   if (reference_map.is_null()) {
  98     JVMCI_THROW_NULL(NullPointerException);
  99   }
 100   if (!jvmci_env()->isa_HotSpotReferenceMap(reference_map)) {
 101     JVMCI_ERROR_NULL("unknown reference map: %s", jvmci_env()->klass_name(reference_map));
 102   }
 103   if (!_has_wide_vector && SharedRuntime::is_wide_vector(jvmci_env()->get_HotSpotReferenceMap_maxRegisterSize(reference_map))) {
 104     if (SharedRuntime::polling_page_vectors_safepoint_handler_blob() == NULL) {
 105       JVMCI_ERROR_NULL("JVMCI is producing code using vectors larger than the runtime supports");
 106     }
 107     _has_wide_vector = true;
 108   }
 109   OopMap* map = new OopMap(_total_frame_size, _parameter_count);
 110   JVMCIObjectArray objects = jvmci_env()->get_HotSpotReferenceMap_objects(reference_map);
 111   JVMCIObjectArray derivedBase = jvmci_env()->get_HotSpotReferenceMap_derivedBase(reference_map);
 112   JVMCIPrimitiveArray sizeInBytes = jvmci_env()->get_HotSpotReferenceMap_sizeInBytes(reference_map);
 113   if (objects.is_null() || derivedBase.is_null() || sizeInBytes.is_null()) {
 114     JVMCI_THROW_NULL(NullPointerException);
 115   }
 116   if (JVMCIENV->get_length(objects) != JVMCIENV->get_length(derivedBase) || JVMCIENV->get_length(objects) != JVMCIENV->get_length(sizeInBytes)) {
 117     JVMCI_ERROR_NULL("arrays in reference map have different sizes: %d %d %d", JVMCIENV->get_length(objects), JVMCIENV->get_length(derivedBase), JVMCIENV->get_length(sizeInBytes));
 118   }
 119   for (int i = 0; i < JVMCIENV->get_length(objects); i++) {
 120     JVMCIObject location = JVMCIENV->get_object_at(objects, i);
 121     JVMCIObject baseLocation = JVMCIENV->get_object_at(derivedBase, i);
 122     jint bytes = JVMCIENV->get_int_at(sizeInBytes, i);
 123 
 124     VMReg vmReg = getVMRegFromLocation(location, _total_frame_size, JVMCI_CHECK_NULL);
 125     if (baseLocation.is_non_null()) {
 126       // derived oop
 127 #ifdef _LP64
 128       if (bytes == 8) {
 129 #else
 130       if (bytes == 4) {
 131 #endif
 132         VMReg baseReg = getVMRegFromLocation(baseLocation, _total_frame_size, JVMCI_CHECK_NULL);
 133         map->set_derived_oop(vmReg, baseReg);
 134       } else {
 135         JVMCI_ERROR_NULL("invalid derived oop size in ReferenceMap: %d", bytes);
 136       }
 137 #ifdef _LP64
 138     } else if (bytes == 8) {
 139       // wide oop
 140       map->set_oop(vmReg);
 141     } else if (bytes == 4) {
 142       // narrow oop
 143       map->set_narrowoop(vmReg);
 144 #else
 145     } else if (bytes == 4) {
 146       map->set_oop(vmReg);
 147 #endif
 148     } else {
 149       JVMCI_ERROR_NULL("invalid oop size in ReferenceMap: %d", bytes);
 150     }
 151   }
 152 
 153   JVMCIObject callee_save_info = jvmci_env()->get_DebugInfo_calleeSaveInfo(debug_info);
 154   if (callee_save_info.is_non_null()) {
 155     JVMCIObjectArray registers = jvmci_env()->get_RegisterSaveLayout_registers(callee_save_info);
 156     JVMCIPrimitiveArray slots = jvmci_env()->get_RegisterSaveLayout_slots(callee_save_info);
 157     for (jint i = 0; i < JVMCIENV->get_length(slots); i++) {
 158       JVMCIObject jvmci_reg = JVMCIENV->get_object_at(registers, i);
 159       jint jvmci_reg_number = jvmci_env()->get_code_Register_number(jvmci_reg);
 160       VMReg hotspot_reg = CodeInstaller::get_hotspot_reg(jvmci_reg_number, JVMCI_CHECK_NULL);
 161       // HotSpot stack slots are 4 bytes
 162       jint jvmci_slot = JVMCIENV->get_int_at(slots, i);
 163       jint hotspot_slot = jvmci_slot * VMRegImpl::slots_per_word;
 164       VMReg hotspot_slot_as_reg = VMRegImpl::stack2reg(hotspot_slot);
 165       map->set_callee_saved(hotspot_slot_as_reg, hotspot_reg);
 166 #ifdef _LP64
 167       // (copied from generate_oop_map() in c1_Runtime1_x86.cpp)
 168       VMReg hotspot_slot_hi_as_reg = VMRegImpl::stack2reg(hotspot_slot + 1);
 169       map->set_callee_saved(hotspot_slot_hi_as_reg, hotspot_reg->next());
 170 #endif
 171     }
 172   }
 173   return map;
 174 }
 175 
 176 void* CodeInstaller::record_metadata_reference(CodeSection* section, address dest, JVMCIObject constant, JVMCI_TRAPS) {
 177   /*
 178    * This method needs to return a raw (untyped) pointer, since the value of a pointer to the base
 179    * class is in general not equal to the pointer of the subclass. When patching metaspace pointers,
 180    * the compiler expects a direct pointer to the subclass (Klass* or Method*), not a pointer to the
 181    * base class (Metadata* or MetaspaceObj*).
 182    */
 183   JVMCIObject obj = jvmci_env()->get_HotSpotMetaspaceConstantImpl_metaspaceObject(constant);
 184   if (jvmci_env()->isa_HotSpotResolvedObjectTypeImpl(obj)) {
 185     Klass* klass = JVMCIENV->asKlass(obj);
 186     assert(!jvmci_env()->get_HotSpotMetaspaceConstantImpl_compressed(constant), "unexpected compressed klass pointer %s @ " INTPTR_FORMAT, klass->name()->as_C_string(), p2i(klass));
 187     int index = _oop_recorder->find_index(klass);
 188     section->relocate(dest, metadata_Relocation::spec(index));
 189     JVMCI_event_3("metadata[%d of %d] = %s", index, _oop_recorder->metadata_count(), klass->name()->as_C_string());
 190     return klass;
 191   } else if (jvmci_env()->isa_HotSpotResolvedJavaMethodImpl(obj)) {
 192     Method* method = jvmci_env()->asMethod(obj);
 193     assert(!jvmci_env()->get_HotSpotMetaspaceConstantImpl_compressed(constant), "unexpected compressed method pointer %s @ " INTPTR_FORMAT, method->name()->as_C_string(), p2i(method));
 194     int index = _oop_recorder->find_index(method);
 195     section->relocate(dest, metadata_Relocation::spec(index));
 196     JVMCI_event_3("metadata[%d of %d] = %s", index, _oop_recorder->metadata_count(), method->name()->as_C_string());
 197     return method;
 198   } else {
 199     JVMCI_ERROR_NULL("unexpected metadata reference for constant of type %s", jvmci_env()->klass_name(obj));
 200   }
 201 }
 202 
 203 #ifdef _LP64
 204 narrowKlass CodeInstaller::record_narrow_metadata_reference(CodeSection* section, address dest, JVMCIObject constant, JVMCI_TRAPS) {
 205   JVMCIObject obj = jvmci_env()->get_HotSpotMetaspaceConstantImpl_metaspaceObject(constant);
 206   assert(jvmci_env()->get_HotSpotMetaspaceConstantImpl_compressed(constant), "unexpected uncompressed pointer");
 207 
 208   if (!jvmci_env()->isa_HotSpotResolvedObjectTypeImpl(obj)) {
 209     JVMCI_ERROR_0("unexpected compressed pointer of type %s", jvmci_env()->klass_name(obj));
 210   }
 211 
 212   Klass* klass = JVMCIENV->asKlass(obj);
 213   int index = _oop_recorder->find_index(klass);
 214   section->relocate(dest, metadata_Relocation::spec(index));
 215   JVMCI_event_3("narrowKlass[%d of %d] = %s", index, _oop_recorder->metadata_count(), klass->name()->as_C_string());
 216   return CompressedKlassPointers::encode(klass);
 217 }
 218 #endif
 219 
 220 Location::Type CodeInstaller::get_oop_type(JVMCIObject value) {
 221   JVMCIObject valueKind = jvmci_env()->get_Value_valueKind(value);
 222   JVMCIObject platformKind = jvmci_env()->get_ValueKind_platformKind(valueKind);
 223 
 224   if (jvmci_env()->equals(platformKind, word_kind())) {
 225     return Location::oop;
 226   } else {
 227     return Location::narrowoop;
 228   }
 229 }
 230 
 231 ScopeValue* CodeInstaller::get_scope_value(JVMCIObject value, BasicType type, GrowableArray<ScopeValue*>* objects, ScopeValue* &second, JVMCI_TRAPS) {
 232   second = NULL;
 233   if (value.is_null()) {
 234     JVMCI_THROW_NULL(NullPointerException);
 235   } else if (JVMCIENV->equals(value, jvmci_env()->get_Value_ILLEGAL())) {
 236     if (type != T_ILLEGAL) {
 237       JVMCI_ERROR_NULL("unexpected illegal value, expected %s", basictype_to_str(type));
 238     }
 239     return _illegal_value;
 240   } else if (jvmci_env()->isa_RegisterValue(value)) {
 241     JVMCIObject reg = jvmci_env()->get_RegisterValue_reg(value);
 242     jint number = jvmci_env()->get_code_Register_number(reg);
 243     VMReg hotspotRegister = get_hotspot_reg(number, JVMCI_CHECK_NULL);
 244     if (is_general_purpose_reg(hotspotRegister)) {
 245       Location::Type locationType;
 246       if (type == T_OBJECT) {
 247         locationType = get_oop_type(value);
 248       } else if (type == T_LONG) {
 249         locationType = Location::lng;
 250       } else if (type == T_INT || type == T_FLOAT || type == T_SHORT || type == T_CHAR || type == T_BYTE || type == T_BOOLEAN) {
 251         locationType = Location::int_in_long;
 252       } else {
 253         JVMCI_ERROR_NULL("unexpected type %s in cpu register", basictype_to_str(type));
 254       }
 255       ScopeValue* value = new LocationValue(Location::new_reg_loc(locationType, hotspotRegister));
 256       if (type == T_LONG) {
 257         second = value;
 258       }
 259       return value;
 260     } else {
 261       Location::Type locationType;
 262       if (type == T_FLOAT) {
 263         // this seems weird, but the same value is used in c1_LinearScan
 264         locationType = Location::normal;
 265       } else if (type == T_DOUBLE) {
 266         locationType = Location::dbl;
 267       } else {
 268         JVMCI_ERROR_NULL("unexpected type %s in floating point register", basictype_to_str(type));
 269       }
 270       ScopeValue* value = new LocationValue(Location::new_reg_loc(locationType, hotspotRegister));
 271       if (type == T_DOUBLE) {
 272         second = value;
 273       }
 274       return value;
 275     }
 276   } else if (jvmci_env()->isa_StackSlot(value)) {
 277     jint offset = jvmci_env()->get_StackSlot_offset(value);
 278     if (jvmci_env()->get_StackSlot_addFrameSize(value)) {
 279       offset += _total_frame_size;
 280     }
 281 
 282     Location::Type locationType;
 283     if (type == T_OBJECT) {
 284       locationType = get_oop_type(value);
 285     } else if (type == T_LONG) {
 286       locationType = Location::lng;
 287     } else if (type == T_DOUBLE) {
 288       locationType = Location::dbl;
 289     } else if (type == T_INT || type == T_FLOAT || type == T_SHORT || type == T_CHAR || type == T_BYTE || type == T_BOOLEAN) {
 290       locationType = Location::normal;
 291     } else {
 292       JVMCI_ERROR_NULL("unexpected type %s in stack slot", basictype_to_str(type));
 293     }
 294     ScopeValue* value = new LocationValue(Location::new_stk_loc(locationType, offset));
 295     if (type == T_DOUBLE || type == T_LONG) {
 296       second = value;
 297     }
 298     return value;
 299   } else if (jvmci_env()->isa_JavaConstant(value)) {
 300     if (jvmci_env()->isa_PrimitiveConstant(value)) {
 301       if (jvmci_env()->isa_RawConstant(value)) {
 302         jlong prim = jvmci_env()->get_PrimitiveConstant_primitive(value);
 303         return new ConstantLongValue(prim);
 304       } else {
 305         BasicType constantType = jvmci_env()->kindToBasicType(jvmci_env()->get_PrimitiveConstant_kind(value), JVMCI_CHECK_NULL);
 306         if (type != constantType) {
 307           JVMCI_ERROR_NULL("primitive constant type doesn't match, expected %s but got %s", basictype_to_str(type), basictype_to_str(constantType));
 308         }
 309         if (type == T_INT || type == T_FLOAT) {
 310           jint prim = (jint)jvmci_env()->get_PrimitiveConstant_primitive(value);
 311           switch (prim) {
 312             case -1: return _int_m1_scope_value;
 313             case  0: return _int_0_scope_value;
 314             case  1: return _int_1_scope_value;
 315             case  2: return _int_2_scope_value;
 316             default: return new ConstantIntValue(prim);
 317           }
 318         } else if (type == T_LONG || type == T_DOUBLE) {
 319           jlong prim = jvmci_env()->get_PrimitiveConstant_primitive(value);
 320           second = _int_1_scope_value;
 321           return new ConstantLongValue(prim);
 322         } else {
 323           JVMCI_ERROR_NULL("unexpected primitive constant type %s", basictype_to_str(type));
 324         }
 325       }
 326     } else if (jvmci_env()->isa_NullConstant(value) || jvmci_env()->isa_HotSpotCompressedNullConstant(value)) {
 327       if (type == T_OBJECT) {
 328         return _oop_null_scope_value;
 329       } else {
 330         JVMCI_ERROR_NULL("unexpected null constant, expected %s", basictype_to_str(type));
 331       }
 332     } else if (jvmci_env()->isa_HotSpotObjectConstantImpl(value)) {
 333       if (type == T_OBJECT) {
 334         Handle obj = jvmci_env()->asConstant(value, JVMCI_CHECK_NULL);
 335         if (obj == NULL) {
 336           JVMCI_ERROR_NULL("null value must be in NullConstant");
 337         }
 338         return new ConstantOopWriteValue(JNIHandles::make_local(obj()));
 339       } else {
 340         JVMCI_ERROR_NULL("unexpected object constant, expected %s", basictype_to_str(type));
 341       }
 342     }
 343   } else if (jvmci_env()->isa_VirtualObject(value)) {
 344     if (type == T_OBJECT) {
 345       int id = jvmci_env()->get_VirtualObject_id(value);
 346       if (0 <= id && id < objects->length()) {
 347         ScopeValue* object = objects->at(id);
 348         if (object != NULL) {
 349           return object;
 350         }
 351       }
 352       JVMCI_ERROR_NULL("unknown virtual object id %d", id);
 353     } else {
 354       JVMCI_ERROR_NULL("unexpected virtual object, expected %s", basictype_to_str(type));
 355     }
 356   }
 357 
 358   JVMCI_ERROR_NULL("unexpected value in scope: %s", jvmci_env()->klass_name(value))
 359 }
 360 
 361 void CodeInstaller::record_object_value(ObjectValue* sv, JVMCIObject value, GrowableArray<ScopeValue*>* objects, JVMCI_TRAPS) {
 362   JVMCIObject type = jvmci_env()->get_VirtualObject_type(value);
 363   int id = jvmci_env()->get_VirtualObject_id(value);
 364   Klass* klass = JVMCIENV->asKlass(type);
 365   bool isLongArray = klass == Universe::longArrayKlassObj();
 366   bool isByteArray = klass == Universe::byteArrayKlassObj();
 367 
 368   JVMCIObjectArray values = jvmci_env()->get_VirtualObject_values(value);
 369   JVMCIObjectArray slotKinds = jvmci_env()->get_VirtualObject_slotKinds(value);
 370   for (jint i = 0; i < JVMCIENV->get_length(values); i++) {
 371     ScopeValue* cur_second = NULL;
 372     JVMCIObject object = JVMCIENV->get_object_at(values, i);
 373     BasicType type = jvmci_env()->kindToBasicType(JVMCIENV->get_object_at(slotKinds, i), JVMCI_CHECK);
 374     ScopeValue* value;
 375     if (JVMCIENV->equals(object, jvmci_env()->get_Value_ILLEGAL())) {
 376       if (isByteArray && type == T_ILLEGAL) {
 377         /*
 378          * The difference between a virtualized large access and a deferred write is the kind stored in the slotKinds
 379          * of the virtual object: in the virtualization case, the kind is illegal, in the deferred write case, the kind
 380          * is access stack kind (an int).
 381          */
 382         value = _virtual_byte_array_marker;
 383       } else {
 384         value = _illegal_value;
 385         if (type == T_DOUBLE || type == T_LONG) {
 386             cur_second = _illegal_value;
 387         }
 388       }
 389     } else {
 390       value = get_scope_value(object, type, objects, cur_second, JVMCI_CHECK);
 391     }
 392 
 393     if (isLongArray && cur_second == NULL) {
 394       // we're trying to put ints into a long array... this isn't really valid, but it's used for some optimizations.
 395       // add an int 0 constant
 396       cur_second = _int_0_scope_value;
 397     }
 398 
 399     if (isByteArray && cur_second != NULL && (type == T_DOUBLE || type == T_LONG)) {
 400       // we are trying to write a long in a byte Array. We will need to count the illegals to restore the type of
 401       // the thing we put inside.
 402       cur_second = NULL;
 403     }
 404 
 405     if (cur_second != NULL) {
 406       sv->field_values()->append(cur_second);
 407     }
 408     assert(value != NULL, "missing value");
 409     sv->field_values()->append(value);
 410   }
 411 }
 412 
 413 MonitorValue* CodeInstaller::get_monitor_value(JVMCIObject value, GrowableArray<ScopeValue*>* objects, JVMCI_TRAPS) {
 414   if (value.is_null()) {
 415     JVMCI_THROW_NULL(NullPointerException);
 416   }
 417   if (!jvmci_env()->isa_StackLockValue(value)) {
 418     JVMCI_ERROR_NULL("Monitors must be of type StackLockValue, got %s", jvmci_env()->klass_name(value));
 419   }
 420 
 421   ScopeValue* second = NULL;
 422   ScopeValue* owner_value = get_scope_value(jvmci_env()->get_StackLockValue_owner(value), T_OBJECT, objects, second, JVMCI_CHECK_NULL);
 423   assert(second == NULL, "monitor cannot occupy two stack slots");
 424 
 425   ScopeValue* lock_data_value = get_scope_value(jvmci_env()->get_StackLockValue_slot(value), T_LONG, objects, second, JVMCI_CHECK_NULL);
 426   assert(second == lock_data_value, "monitor is LONG value that occupies two stack slots");
 427   assert(lock_data_value->is_location(), "invalid monitor location");
 428   Location lock_data_loc = ((LocationValue*)lock_data_value)->location();
 429 
 430   bool eliminated = false;
 431   if (jvmci_env()->get_StackLockValue_eliminated(value)) {
 432     eliminated = true;
 433   }
 434 
 435   return new MonitorValue(owner_value, lock_data_loc, eliminated);
 436 }
 437 
 438 void CodeInstaller::initialize_dependencies(JVMCIObject compiled_code, OopRecorder* oop_recorder, JVMCI_TRAPS) {
 439   JavaThread* thread = JavaThread::current();
 440   CompilerThread* compilerThread = thread->is_Compiler_thread() ? CompilerThread::cast(thread) : NULL;
 441   _oop_recorder = oop_recorder;
 442   _dependencies = new Dependencies(&_arena, _oop_recorder, compilerThread != NULL ? compilerThread->log() : NULL);
 443   JVMCIObjectArray assumptions = jvmci_env()->get_HotSpotCompiledCode_assumptions(compiled_code);
 444   if (assumptions.is_non_null()) {
 445     int length = JVMCIENV->get_length(assumptions);
 446     for (int i = 0; i < length; ++i) {
 447       JVMCIObject assumption = JVMCIENV->get_object_at(assumptions, i);
 448       if (assumption.is_non_null()) {
 449         if (jvmci_env()->isa_Assumptions_NoFinalizableSubclass(assumption)) {
 450           assumption_NoFinalizableSubclass(assumption);
 451         } else if (jvmci_env()->isa_Assumptions_ConcreteSubtype(assumption)) {
 452           assumption_ConcreteSubtype(assumption);
 453         } else if (jvmci_env()->isa_Assumptions_LeafType(assumption)) {
 454           assumption_LeafType(assumption);
 455         } else if (jvmci_env()->isa_Assumptions_ConcreteMethod(assumption)) {
 456           assumption_ConcreteMethod(assumption);
 457         } else if (jvmci_env()->isa_Assumptions_CallSiteTargetValue(assumption)) {
 458           assumption_CallSiteTargetValue(assumption, JVMCI_CHECK);
 459         } else {
 460           JVMCI_ERROR("unexpected Assumption subclass %s", jvmci_env()->klass_name(assumption));
 461         }
 462       }
 463     }
 464   }
 465   if (JvmtiExport::can_hotswap_or_post_breakpoint()) {
 466     JVMCIObjectArray methods = jvmci_env()->get_HotSpotCompiledCode_methods(compiled_code);
 467     if (methods.is_non_null()) {
 468       int length = JVMCIENV->get_length(methods);
 469       for (int i = 0; i < length; ++i) {
 470         JVMCIObject method_handle = JVMCIENV->get_object_at(methods, i);
 471         Method* method = jvmci_env()->asMethod(method_handle);
 472         _dependencies->assert_evol_method(method);
 473       }
 474     }
 475   }
 476 }
 477 
 478 // constructor used to create a method
 479 JVMCI::CodeInstallResult CodeInstaller::install(JVMCICompiler* compiler,
 480     JVMCIObject target,
 481     JVMCIObject compiled_code,
 482     CodeBlob*& cb,
 483     nmethodLocker& nmethod_handle,
 484     JVMCIObject installed_code,
 485     FailedSpeculation** failed_speculations,
 486     char* speculations,
 487     int speculations_len,
 488     JVMCI_TRAPS) {
 489 
 490   CodeBuffer buffer("JVMCI Compiler CodeBuffer");
 491   OopRecorder* recorder = new OopRecorder(&_arena, true);
 492   initialize_dependencies(compiled_code, recorder, JVMCI_CHECK_OK);
 493 
 494   // Get instructions and constants CodeSections early because we need it.
 495   _instructions = buffer.insts();
 496   _constants = buffer.consts();
 497 
 498   initialize_fields(target, compiled_code, JVMCI_CHECK_OK);
 499   JVMCI::CodeInstallResult result = initialize_buffer(buffer, true, JVMCI_CHECK_OK);
 500   if (result != JVMCI::ok) {
 501     return result;
 502   }
 503 
 504   int stack_slots = _total_frame_size / HeapWordSize; // conversion to words
 505 
 506   if (!jvmci_env()->isa_HotSpotCompiledNmethod(compiled_code)) {
 507     JVMCIObject stubName = jvmci_env()->get_HotSpotCompiledCode_name(compiled_code);
 508     if (stubName.is_null()) {
 509       JVMCI_ERROR_OK("stub should have a name");
 510     }
 511     char* name = strdup(jvmci_env()->as_utf8_string(stubName));
 512     cb = RuntimeStub::new_runtime_stub(name,
 513                                        &buffer,
 514                                        _offsets.value(CodeOffsets::Frame_Complete),
 515                                        stack_slots,
 516                                        _debug_recorder->_oopmaps,
 517                                        false);
 518     result = JVMCI::ok;
 519   } else {
 520     JVMCICompileState* compile_state = (JVMCICompileState*) (address) jvmci_env()->get_HotSpotCompiledNmethod_compileState(compiled_code);
 521     if (compile_state != NULL) {
 522       jvmci_env()->set_compile_state(compile_state);
 523     }
 524 
 525     Thread* thread = Thread::current();
 526 
 527     methodHandle method(thread, jvmci_env()->asMethod(jvmci_env()->get_HotSpotCompiledNmethod_method(compiled_code)));
 528     jint entry_bci = jvmci_env()->get_HotSpotCompiledNmethod_entryBCI(compiled_code);
 529     bool has_unsafe_access = jvmci_env()->get_HotSpotCompiledNmethod_hasUnsafeAccess(compiled_code) == JNI_TRUE;
 530     jint id = jvmci_env()->get_HotSpotCompiledNmethod_id(compiled_code);
 531     if (id == -1) {
 532       // Make sure a valid compile_id is associated with every compile
 533       id = CompileBroker::assign_compile_id_unlocked(thread, method, entry_bci);
 534       jvmci_env()->set_HotSpotCompiledNmethod_id(compiled_code, id);
 535     }
 536     if (!jvmci_env()->isa_HotSpotNmethod(installed_code)) {
 537       JVMCI_THROW_MSG_(IllegalArgumentException, "InstalledCode object must be a HotSpotNmethod when installing a HotSpotCompiledNmethod", JVMCI::ok);
 538     }
 539 
 540     JVMCIObject mirror = installed_code;
 541     result = runtime()->register_method(jvmci_env(), method, nmethod_handle, entry_bci, &_offsets, _orig_pc_offset, &buffer,
 542                                         stack_slots, _debug_recorder->_oopmaps, &_exception_handler_table, &_implicit_exception_table,
 543                                         compiler, _debug_recorder, _dependencies, id,
 544                                         has_unsafe_access, _has_wide_vector, compiled_code, mirror,
 545                                         failed_speculations, speculations, speculations_len);
 546     if (result == JVMCI::ok) {
 547       nmethod* nm = nmethod_handle.code()->as_nmethod_or_null();
 548       cb = nm;
 549       if (compile_state == NULL) {
 550         // This compile didn't come through the CompileBroker so perform the printing here
 551         DirectiveSet* directive = DirectivesStack::getMatchingDirective(method, compiler);
 552         nm->maybe_print_nmethod(directive);
 553         DirectivesStack::release(directive);
 554       }
 555     }
 556   }
 557 
 558   if (cb != NULL) {
 559     // Make sure the pre-calculated constants section size was correct.
 560     guarantee((cb->code_begin() - cb->content_begin()) >= _constants_size, "%d < %d", (int)(cb->code_begin() - cb->content_begin()), _constants_size);
 561   }
 562   return result;
 563 }
 564 
 565 void CodeInstaller::initialize_fields(JVMCIObject target, JVMCIObject compiled_code, JVMCI_TRAPS) {
 566   if (jvmci_env()->isa_HotSpotCompiledNmethod(compiled_code)) {
 567     JVMCIObject hotspotJavaMethod = jvmci_env()->get_HotSpotCompiledNmethod_method(compiled_code);
 568     Thread* thread = Thread::current();
 569     methodHandle method(thread, jvmci_env()->asMethod(hotspotJavaMethod));
 570     _parameter_count = method->size_of_parameters();
 571     JVMCI_event_2("installing code for %s", method->name_and_sig_as_C_string());
 572   } else {
 573     // Must be a HotSpotCompiledRuntimeStub.
 574     // Only used in OopMap constructor for non-product builds
 575     _parameter_count = 0;
 576   }
 577   _sites_handle = jvmci_env()->get_HotSpotCompiledCode_sites(compiled_code);
 578 
 579   _code_handle = jvmci_env()->get_HotSpotCompiledCode_targetCode(compiled_code);
 580   _code_size = jvmci_env()->get_HotSpotCompiledCode_targetCodeSize(compiled_code);
 581   _total_frame_size = jvmci_env()->get_HotSpotCompiledCode_totalFrameSize(compiled_code);
 582 
 583   JVMCIObject deoptRescueSlot = jvmci_env()->get_HotSpotCompiledCode_deoptRescueSlot(compiled_code);
 584   if (deoptRescueSlot.is_null()) {
 585     _orig_pc_offset = -1;
 586   } else {
 587     _orig_pc_offset = jvmci_env()->get_StackSlot_offset(deoptRescueSlot);
 588     if (jvmci_env()->get_StackSlot_addFrameSize(deoptRescueSlot)) {
 589       _orig_pc_offset += _total_frame_size;
 590     }
 591     if (_orig_pc_offset < 0) {
 592       JVMCI_ERROR("invalid deopt rescue slot: %d", _orig_pc_offset);
 593     }
 594   }
 595 
 596   // Pre-calculate the constants section size.  This is required for PC-relative addressing.
 597   _data_section_handle = jvmci_env()->get_HotSpotCompiledCode_dataSection(compiled_code);
 598   if ((_constants->alignment() % jvmci_env()->get_HotSpotCompiledCode_dataSectionAlignment(compiled_code)) != 0) {
 599     JVMCI_ERROR("invalid data section alignment: %d", jvmci_env()->get_HotSpotCompiledCode_dataSectionAlignment(compiled_code));
 600   }
 601   _constants_size = JVMCIENV->get_length(data_section());
 602 
 603   _data_section_patches_handle = jvmci_env()->get_HotSpotCompiledCode_dataSectionPatches(compiled_code);
 604 
 605 #ifndef PRODUCT
 606   _comments_handle = jvmci_env()->get_HotSpotCompiledCode_comments(compiled_code);
 607 #endif
 608 
 609   _next_call_type = INVOKE_INVALID;
 610 
 611   _has_wide_vector = false;
 612 
 613   JVMCIObject arch = jvmci_env()->get_TargetDescription_arch(target);
 614   _word_kind_handle = jvmci_env()->get_Architecture_wordKind(arch);
 615 }
 616 
 617 int CodeInstaller::estimate_stubs_size(JVMCI_TRAPS) {
 618   // Estimate the number of static call stubs that might be emitted.
 619   int static_call_stubs = 0;
 620   int trampoline_stubs = 0;
 621   JVMCIObjectArray sites = this->sites();
 622   for (int i = 0; i < JVMCIENV->get_length(sites); i++) {
 623     JVMCIObject site = JVMCIENV->get_object_at(sites, i);
 624     if (!site.is_null()) {
 625       if (jvmci_env()->isa_site_Mark(site)) {
 626         JVMCIObject id_obj = jvmci_env()->get_site_Mark_id(site);
 627         if (id_obj.is_non_null()) {
 628           if (!jvmci_env()->is_boxing_object(T_INT, id_obj)) {
 629             JVMCI_ERROR_0("expected Integer id, got %s", jvmci_env()->klass_name(id_obj));
 630           }
 631           jint id = jvmci_env()->get_boxed_value(T_INT, id_obj).i;
 632           switch (id) {
 633             case INVOKEINTERFACE:
 634             case INVOKEVIRTUAL:
 635               trampoline_stubs++;
 636               break;
 637             case INVOKESTATIC:
 638             case INVOKESPECIAL:
 639               static_call_stubs++;
 640               trampoline_stubs++;
 641               break;
 642             default:
 643               break;
 644           }
 645         }
 646       }
 647     }
 648   }
 649   int size = static_call_stubs * CompiledStaticCall::to_interp_stub_size();
 650   size += trampoline_stubs * CompiledStaticCall::to_trampoline_stub_size();
 651   return size;
 652 }
 653 
 654 // perform data and call relocation on the CodeBuffer
 655 JVMCI::CodeInstallResult CodeInstaller::initialize_buffer(CodeBuffer& buffer, bool check_size, JVMCI_TRAPS) {
 656   HandleMark hm(Thread::current());
 657   JVMCIObjectArray sites = this->sites();
 658   int locs_buffer_size = JVMCIENV->get_length(sites) * (relocInfo::length_limit + sizeof(relocInfo));
 659 
 660   // Allocate enough space in the stub section for the static call
 661   // stubs.  Stubs have extra relocs but they are managed by the stub
 662   // section itself so they don't need to be accounted for in the
 663   // locs_buffer above.
 664   int stubs_size = estimate_stubs_size(JVMCI_CHECK_OK);
 665   int total_size = align_up(_code_size, buffer.insts()->alignment()) + align_up(_constants_size, buffer.consts()->alignment()) + align_up(stubs_size, buffer.stubs()->alignment());
 666 
 667   if (check_size && total_size > JVMCINMethodSizeLimit) {
 668     return JVMCI::code_too_large;
 669   }
 670 
 671   buffer.initialize(total_size, locs_buffer_size);
 672   if (buffer.blob() == NULL) {
 673     return JVMCI::cache_full;
 674   }
 675   buffer.initialize_stubs_size(stubs_size);
 676   buffer.initialize_consts_size(_constants_size);
 677 
 678   _debug_recorder = new DebugInformationRecorder(_oop_recorder);
 679   _debug_recorder->set_oopmaps(new OopMapSet());
 680 
 681   buffer.initialize_oop_recorder(_oop_recorder);
 682 
 683   // copy the constant data into the newly created CodeBuffer
 684   address end_data = _constants->start() + _constants_size;
 685   JVMCIENV->copy_bytes_to(data_section(), (jbyte*) _constants->start(), 0, _constants_size);
 686   _constants->set_end(end_data);
 687 
 688   // copy the code into the newly created CodeBuffer
 689   address end_pc = _instructions->start() + _code_size;
 690   guarantee(_instructions->allocates2(end_pc), "initialize should have reserved enough space for all the code");
 691   JVMCIENV->copy_bytes_to(code(), (jbyte*) _instructions->start(), 0, _code_size);
 692   _instructions->set_end(end_pc);
 693 
 694   for (int i = 0; i < JVMCIENV->get_length(data_section_patches()); i++) {
 695     // HandleMark hm(THREAD);
 696     JVMCIObject patch = JVMCIENV->get_object_at(data_section_patches(), i);
 697     if (patch.is_null()) {
 698       JVMCI_THROW_(NullPointerException, JVMCI::ok);
 699     }
 700     JVMCIObject reference = jvmci_env()->get_site_DataPatch_reference(patch);
 701     if (reference.is_null()) {
 702       JVMCI_THROW_(NullPointerException, JVMCI::ok);
 703     }
 704     if (!jvmci_env()->isa_site_ConstantReference(reference)) {
 705       JVMCI_ERROR_OK("invalid patch in data section: %s", jvmci_env()->klass_name(reference));
 706     }
 707     JVMCIObject constant = jvmci_env()->get_site_ConstantReference_constant(reference);
 708     if (constant.is_null()) {
 709       JVMCI_THROW_(NullPointerException, JVMCI::ok);
 710     }
 711     address dest = _constants->start() + jvmci_env()->get_site_Site_pcOffset(patch);
 712     if (jvmci_env()->isa_HotSpotMetaspaceConstantImpl(constant)) {
 713       if (jvmci_env()->get_HotSpotMetaspaceConstantImpl_compressed(constant)) {
 714 #ifdef _LP64
 715         *((narrowKlass*) dest) = record_narrow_metadata_reference(_constants, dest, constant, JVMCI_CHECK_OK);
 716 #else
 717         JVMCI_ERROR_OK("unexpected compressed Klass* in 32-bit mode");
 718 #endif
 719       } else {
 720         *((void**) dest) = record_metadata_reference(_constants, dest, constant, JVMCI_CHECK_OK);
 721       }
 722     } else if (jvmci_env()->isa_HotSpotObjectConstantImpl(constant)) {
 723       Handle obj = jvmci_env()->asConstant(constant, JVMCI_CHECK_OK);
 724       jobject value = JNIHandles::make_local(obj());
 725       int oop_index = _oop_recorder->find_index(value);
 726 
 727       if (jvmci_env()->get_HotSpotObjectConstantImpl_compressed(constant)) {
 728 #ifdef _LP64
 729         _constants->relocate(dest, oop_Relocation::spec(oop_index), relocInfo::narrow_oop_in_const);
 730 #else
 731         JVMCI_ERROR_OK("unexpected compressed oop in 32-bit mode");
 732 #endif
 733       } else {
 734         _constants->relocate(dest, oop_Relocation::spec(oop_index));
 735       }
 736     } else {
 737       JVMCI_ERROR_OK("invalid constant in data section: %s", jvmci_env()->klass_name(constant));
 738     }
 739   }
 740   jint last_pc_offset = -1;
 741   for (int i = 0; i < JVMCIENV->get_length(sites); i++) {
 742     // HandleMark hm(THREAD);
 743     JVMCIObject site = JVMCIENV->get_object_at(sites, i);
 744     if (site.is_null()) {
 745       JVMCI_THROW_(NullPointerException, JVMCI::ok);
 746     }
 747 
 748     jint pc_offset = jvmci_env()->get_site_Site_pcOffset(site);
 749 
 750     if (jvmci_env()->isa_site_Call(site)) {
 751       JVMCI_event_4("call at %i", pc_offset);
 752       site_Call(buffer, pc_offset, site, JVMCI_CHECK_OK);
 753     } else if (jvmci_env()->isa_site_Infopoint(site)) {
 754       // three reasons for infopoints denote actual safepoints
 755       JVMCIObject reason = jvmci_env()->get_site_Infopoint_reason(site);
 756       if (JVMCIENV->equals(reason, jvmci_env()->get_site_InfopointReason_SAFEPOINT()) ||
 757           JVMCIENV->equals(reason, jvmci_env()->get_site_InfopointReason_CALL()) ||
 758           JVMCIENV->equals(reason, jvmci_env()->get_site_InfopointReason_IMPLICIT_EXCEPTION())) {
 759         JVMCI_event_4("safepoint at %i", pc_offset);
 760         site_Safepoint(buffer, pc_offset, site, JVMCI_CHECK_OK);
 761         if (_orig_pc_offset < 0) {
 762           JVMCI_ERROR_OK("method contains safepoint, but has no deopt rescue slot");
 763         }
 764         if (JVMCIENV->equals(reason, jvmci_env()->get_site_InfopointReason_IMPLICIT_EXCEPTION())) {
 765           if (jvmci_env()->isa_site_ImplicitExceptionDispatch(site)) {
 766             jint dispatch_offset = jvmci_env()->get_site_ImplicitExceptionDispatch_dispatchOffset(site);
 767             JVMCI_event_4("implicit exception at %i, dispatch to %i", pc_offset, dispatch_offset);
 768             _implicit_exception_table.append(pc_offset, dispatch_offset);
 769           } else {
 770             JVMCI_event_4("implicit exception at %i", pc_offset);
 771             _implicit_exception_table.add_deoptimize(pc_offset);
 772           }
 773         }
 774       } else {
 775         JVMCI_event_4("infopoint at %i", pc_offset);
 776         site_Infopoint(buffer, pc_offset, site, JVMCI_CHECK_OK);
 777       }
 778     } else if (jvmci_env()->isa_site_DataPatch(site)) {
 779       JVMCI_event_4("datapatch at %i", pc_offset);
 780       site_DataPatch(buffer, pc_offset, site, JVMCI_CHECK_OK);
 781     } else if (jvmci_env()->isa_site_Mark(site)) {
 782       JVMCI_event_4("mark at %i", pc_offset);
 783       site_Mark(buffer, pc_offset, site, JVMCI_CHECK_OK);
 784     } else if (jvmci_env()->isa_site_ExceptionHandler(site)) {
 785       JVMCI_event_4("exceptionhandler at %i", pc_offset);
 786       site_ExceptionHandler(pc_offset, site);
 787     } else {
 788       JVMCI_ERROR_OK("unexpected site subclass: %s", jvmci_env()->klass_name(site));
 789     }
 790     last_pc_offset = pc_offset;
 791 
 792     JavaThread* thread = JavaThread::current();
 793     if (SafepointMechanism::should_process(thread)) {
 794       // this is a hacky way to force a safepoint check but nothing else was jumping out at me.
 795       ThreadToNativeFromVM ttnfv(thread);
 796     }
 797   }
 798 
 799 #ifndef PRODUCT
 800   if (comments().is_non_null()) {
 801     for (int i = 0; i < JVMCIENV->get_length(comments()); i++) {
 802       JVMCIObject comment = JVMCIENV->get_object_at(comments(), i);
 803       assert(jvmci_env()->isa_HotSpotCompiledCode_Comment(comment), "cce");
 804       jint offset = jvmci_env()->get_HotSpotCompiledCode_Comment_pcOffset(comment);
 805       const char* text = jvmci_env()->as_utf8_string(jvmci_env()->get_HotSpotCompiledCode_Comment_text(comment));
 806       buffer.block_comment(offset, text);
 807     }
 808   }
 809 #endif
 810   if (_has_auto_box) {
 811     JavaThread* THREAD = JavaThread::current(); // For exception macros.
 812     JVMCI::ensure_box_caches_initialized(CHECK_(JVMCI::ok));
 813   }
 814   return JVMCI::ok;
 815 }
 816 
 817 void CodeInstaller::assumption_NoFinalizableSubclass(JVMCIObject assumption) {
 818   JVMCIObject receiverType_handle = jvmci_env()->get_Assumptions_NoFinalizableSubclass_receiverType(assumption);
 819   Klass* receiverType = jvmci_env()->asKlass(receiverType_handle);
 820   _dependencies->assert_has_no_finalizable_subclasses(receiverType);
 821 }
 822 
 823 void CodeInstaller::assumption_ConcreteSubtype(JVMCIObject assumption) {
 824   JVMCIObject context_handle = jvmci_env()->get_Assumptions_ConcreteSubtype_context(assumption);
 825   JVMCIObject subtype_handle = jvmci_env()->get_Assumptions_ConcreteSubtype_subtype(assumption);
 826   Klass* context = jvmci_env()->asKlass(context_handle);
 827   Klass* subtype = jvmci_env()->asKlass(subtype_handle);
 828 
 829   assert(context->is_abstract(), "");
 830   _dependencies->assert_abstract_with_unique_concrete_subtype(context, subtype);
 831 }
 832 
 833 void CodeInstaller::assumption_LeafType(JVMCIObject assumption) {
 834   JVMCIObject context_handle = jvmci_env()->get_Assumptions_LeafType_context(assumption);
 835   Klass* context = jvmci_env()->asKlass(context_handle);
 836 
 837   _dependencies->assert_leaf_type(context);
 838 }
 839 
 840 void CodeInstaller::assumption_ConcreteMethod(JVMCIObject assumption) {
 841   JVMCIObject impl_handle = jvmci_env()->get_Assumptions_ConcreteMethod_impl(assumption);
 842   JVMCIObject context_handle = jvmci_env()->get_Assumptions_ConcreteMethod_context(assumption);
 843 
 844   Method* impl = jvmci_env()->asMethod(impl_handle);
 845   Klass* context = jvmci_env()->asKlass(context_handle);
 846 
 847   _dependencies->assert_unique_concrete_method(context, impl);
 848 }
 849 
 850 void CodeInstaller::assumption_CallSiteTargetValue(JVMCIObject assumption, JVMCI_TRAPS) {
 851   JVMCIObject callSiteConstant = jvmci_env()->get_Assumptions_CallSiteTargetValue_callSite(assumption);
 852   Handle callSite = jvmci_env()->asConstant(callSiteConstant, JVMCI_CHECK);
 853   JVMCIObject methodConstant = jvmci_env()->get_Assumptions_CallSiteTargetValue_methodHandle(assumption);
 854   Handle methodHandle = jvmci_env()->asConstant(methodConstant, JVMCI_CHECK);
 855   _dependencies->assert_call_site_target_value(callSite(), methodHandle());
 856 }
 857 
 858 void CodeInstaller::site_ExceptionHandler(jint pc_offset, JVMCIObject exc) {
 859   jint handler_offset = jvmci_env()->get_site_ExceptionHandler_handlerPos(exc);
 860 
 861   // Subtable header
 862   _exception_handler_table.add_entry(HandlerTableEntry(1, pc_offset, 0));
 863 
 864   // Subtable entry
 865   _exception_handler_table.add_entry(HandlerTableEntry(-1, handler_offset, 0));
 866 }
 867 
 868 // If deoptimization happens, the interpreter should reexecute these bytecodes.
 869 // This function mainly helps the compilers to set up the reexecute bit.
 870 static bool bytecode_should_reexecute(Bytecodes::Code code) {
 871   switch (code) {
 872     case Bytecodes::_invokedynamic:
 873     case Bytecodes::_invokevirtual:
 874     case Bytecodes::_invokeinterface:
 875     case Bytecodes::_invokespecial:
 876     case Bytecodes::_invokestatic:
 877       return false;
 878     default:
 879       return true;
 880     }
 881   return true;
 882 }
 883 
 884 GrowableArray<ScopeValue*>* CodeInstaller::record_virtual_objects(JVMCIObject debug_info, JVMCI_TRAPS) {
 885   JVMCIObjectArray virtualObjects = jvmci_env()->get_DebugInfo_virtualObjectMapping(debug_info);
 886   if (virtualObjects.is_null()) {
 887     return NULL;
 888   }
 889   GrowableArray<ScopeValue*>* objects = new GrowableArray<ScopeValue*>(JVMCIENV->get_length(virtualObjects), JVMCIENV->get_length(virtualObjects), NULL);
 890   // Create the unique ObjectValues
 891   for (int i = 0; i < JVMCIENV->get_length(virtualObjects); i++) {
 892     // HandleMark hm(THREAD);
 893     JVMCIObject value = JVMCIENV->get_object_at(virtualObjects, i);
 894     int id = jvmci_env()->get_VirtualObject_id(value);
 895     JVMCIObject type = jvmci_env()->get_VirtualObject_type(value);
 896     bool is_auto_box = jvmci_env()->get_VirtualObject_isAutoBox(value);
 897     if (is_auto_box) {
 898       _has_auto_box = true;
 899     }
 900     Klass* klass = jvmci_env()->asKlass(type);
 901     oop javaMirror = klass->java_mirror();
 902     ScopeValue *klass_sv = new ConstantOopWriteValue(JNIHandles::make_local(Thread::current(), javaMirror));
 903     ObjectValue* sv = is_auto_box ? new AutoBoxObjectValue(id, klass_sv) : new ObjectValue(id, klass_sv);
 904     if (id < 0 || id >= objects->length()) {
 905       JVMCI_ERROR_NULL("virtual object id %d out of bounds", id);
 906     }
 907     if (objects->at(id) != NULL) {
 908       JVMCI_ERROR_NULL("duplicate virtual object id %d", id);
 909     }
 910     objects->at_put(id, sv);
 911   }
 912   // All the values which could be referenced by the VirtualObjects
 913   // exist, so now describe all the VirtualObjects themselves.
 914   for (int i = 0; i < JVMCIENV->get_length(virtualObjects); i++) {
 915     // HandleMark hm(THREAD);
 916     JVMCIObject value = JVMCIENV->get_object_at(virtualObjects, i);
 917     int id = jvmci_env()->get_VirtualObject_id(value);
 918     record_object_value(objects->at(id)->as_ObjectValue(), value, objects, JVMCI_CHECK_NULL);
 919   }
 920   _debug_recorder->dump_object_pool(objects);
 921 
 922   return objects;
 923 }
 924 
 925 void CodeInstaller::record_scope(jint pc_offset, JVMCIObject debug_info, ScopeMode scope_mode, bool is_mh_invoke, bool return_oop, JVMCI_TRAPS) {
 926   JVMCIObject position = jvmci_env()->get_DebugInfo_bytecodePosition(debug_info);
 927   if (position.is_null()) {
 928     // Stubs do not record scope info, just oop maps
 929     return;
 930   }
 931 
 932   GrowableArray<ScopeValue*>* objectMapping;
 933   if (scope_mode == CodeInstaller::FullFrame) {
 934     objectMapping = record_virtual_objects(debug_info, JVMCI_CHECK);
 935   } else {
 936     objectMapping = NULL;
 937   }
 938   record_scope(pc_offset, position, scope_mode, objectMapping, is_mh_invoke, return_oop, JVMCI_CHECK);
 939 }
 940 
 941 int CodeInstaller::map_jvmci_bci(int bci) {
 942   if (bci < 0) {
 943     if (bci == jvmci_env()->get_BytecodeFrame_BEFORE_BCI()) {
 944       return BeforeBci;
 945     } else if (bci == jvmci_env()->get_BytecodeFrame_AFTER_BCI()) {
 946       return AfterBci;
 947     } else if (bci == jvmci_env()->get_BytecodeFrame_UNWIND_BCI()) {
 948       return UnwindBci;
 949     } else if (bci == jvmci_env()->get_BytecodeFrame_AFTER_EXCEPTION_BCI()) {
 950       return AfterExceptionBci;
 951     } else if (bci == jvmci_env()->get_BytecodeFrame_UNKNOWN_BCI()) {
 952       return UnknownBci;
 953     } else if (bci == jvmci_env()->get_BytecodeFrame_INVALID_FRAMESTATE_BCI()) {
 954       return InvalidFrameStateBci;
 955     }
 956     ShouldNotReachHere();
 957   }
 958   return bci;
 959 }
 960 
 961 void CodeInstaller::record_scope(jint pc_offset, JVMCIObject position, ScopeMode scope_mode, GrowableArray<ScopeValue*>* objects, bool is_mh_invoke, bool return_oop, JVMCI_TRAPS) {
 962   JVMCIObject frame;
 963   if (scope_mode == CodeInstaller::FullFrame) {
 964     if (!jvmci_env()->isa_BytecodeFrame(position)) {
 965       JVMCI_ERROR("Full frame expected for debug info at %i", pc_offset);
 966     }
 967     frame = position;
 968   }
 969   JVMCIObject caller_frame = jvmci_env()->get_BytecodePosition_caller(position);
 970   if (caller_frame.is_non_null()) {
 971     record_scope(pc_offset, caller_frame, scope_mode, objects, is_mh_invoke, return_oop, JVMCI_CHECK);
 972   }
 973 
 974   JVMCIObject hotspot_method = jvmci_env()->get_BytecodePosition_method(position);
 975   Thread* thread = Thread::current();
 976   methodHandle method(thread, jvmci_env()->asMethod(hotspot_method));
 977   jint bci = map_jvmci_bci(jvmci_env()->get_BytecodePosition_bci(position));
 978   if (bci == jvmci_env()->get_BytecodeFrame_BEFORE_BCI()) {
 979     bci = SynchronizationEntryBCI;
 980   }
 981 
 982   JVMCI_event_2("Recording scope pc_offset=%d bci=%d method=%s", pc_offset, bci, method->name_and_sig_as_C_string());
 983 
 984   bool reexecute = false;
 985   if (frame.is_non_null()) {
 986     if (bci < 0){
 987        reexecute = false;
 988     } else {
 989       Bytecodes::Code code = Bytecodes::java_code_at(method(), method->bcp_from(bci));
 990       reexecute = bytecode_should_reexecute(code);
 991       if (frame.is_non_null()) {
 992         reexecute = (jvmci_env()->get_BytecodeFrame_duringCall(frame) == JNI_FALSE);
 993       }
 994     }
 995   }
 996 
 997   DebugToken* locals_token = NULL;
 998   DebugToken* expressions_token = NULL;
 999   DebugToken* monitors_token = NULL;
1000   bool throw_exception = false;
1001 
1002   if (frame.is_non_null()) {
1003     jint local_count = jvmci_env()->get_BytecodeFrame_numLocals(frame);
1004     jint expression_count = jvmci_env()->get_BytecodeFrame_numStack(frame);
1005     jint monitor_count = jvmci_env()->get_BytecodeFrame_numLocks(frame);
1006     JVMCIObjectArray values = jvmci_env()->get_BytecodeFrame_values(frame);
1007     JVMCIObjectArray slotKinds = jvmci_env()->get_BytecodeFrame_slotKinds(frame);
1008 
1009     if (values.is_null() || slotKinds.is_null()) {
1010       JVMCI_THROW(NullPointerException);
1011     }
1012     if (local_count + expression_count + monitor_count != JVMCIENV->get_length(values)) {
1013       JVMCI_ERROR("unexpected values length %d in scope (%d locals, %d expressions, %d monitors)", JVMCIENV->get_length(values), local_count, expression_count, monitor_count);
1014     }
1015     if (local_count + expression_count != JVMCIENV->get_length(slotKinds)) {
1016       JVMCI_ERROR("unexpected slotKinds length %d in scope (%d locals, %d expressions)", JVMCIENV->get_length(slotKinds), local_count, expression_count);
1017     }
1018 
1019     GrowableArray<ScopeValue*>* locals = local_count > 0 ? new GrowableArray<ScopeValue*> (local_count) : NULL;
1020     GrowableArray<ScopeValue*>* expressions = expression_count > 0 ? new GrowableArray<ScopeValue*> (expression_count) : NULL;
1021     GrowableArray<MonitorValue*>* monitors = monitor_count > 0 ? new GrowableArray<MonitorValue*> (monitor_count) : NULL;
1022 
1023     JVMCI_event_2("Scope at bci %d with %d values", bci, JVMCIENV->get_length(values));
1024     JVMCI_event_2("%d locals %d expressions, %d monitors", local_count, expression_count, monitor_count);
1025 
1026     for (jint i = 0; i < JVMCIENV->get_length(values); i++) {
1027       // HandleMark hm(THREAD);
1028       ScopeValue* second = NULL;
1029       JVMCIObject value = JVMCIENV->get_object_at(values, i);
1030       if (i < local_count) {
1031         BasicType type = jvmci_env()->kindToBasicType(JVMCIENV->get_object_at(slotKinds, i), JVMCI_CHECK);
1032         ScopeValue* first = get_scope_value(value, type, objects, second, JVMCI_CHECK);
1033         if (second != NULL) {
1034           locals->append(second);
1035         }
1036         locals->append(first);
1037       } else if (i < local_count + expression_count) {
1038         BasicType type = jvmci_env()->kindToBasicType(JVMCIENV->get_object_at(slotKinds, i), JVMCI_CHECK);
1039         ScopeValue* first = get_scope_value(value, type, objects, second, JVMCI_CHECK);
1040         if (second != NULL) {
1041           expressions->append(second);
1042         }
1043         expressions->append(first);
1044       } else {
1045         MonitorValue *monitor = get_monitor_value(value, objects, JVMCI_CHECK);
1046         monitors->append(monitor);
1047       }
1048       if (second != NULL) {
1049         i++;
1050         if (i >= JVMCIENV->get_length(values) || !JVMCIENV->equals(JVMCIENV->get_object_at(values, i), jvmci_env()->get_Value_ILLEGAL())) {
1051           JVMCI_ERROR("double-slot value not followed by Value.ILLEGAL");
1052         }
1053       }
1054     }
1055 
1056     locals_token = _debug_recorder->create_scope_values(locals);
1057     expressions_token = _debug_recorder->create_scope_values(expressions);
1058     monitors_token = _debug_recorder->create_monitor_values(monitors);
1059 
1060     throw_exception = jvmci_env()->get_BytecodeFrame_rethrowException(frame) == JNI_TRUE;
1061   }
1062 
1063   // has_ea_local_in_scope and arg_escape should be added to JVMCI
1064   const bool is_opt_native         = false;
1065   const bool has_ea_local_in_scope = false;
1066   const bool arg_escape            = false;
1067   _debug_recorder->describe_scope(pc_offset, method, NULL, bci, reexecute, throw_exception, is_mh_invoke, is_opt_native, return_oop,
1068                                   has_ea_local_in_scope, arg_escape,
1069                                   locals_token, expressions_token, monitors_token);
1070 }
1071 
1072 void CodeInstaller::site_Safepoint(CodeBuffer& buffer, jint pc_offset, JVMCIObject site, JVMCI_TRAPS) {
1073   JVMCIObject debug_info = jvmci_env()->get_site_Infopoint_debugInfo(site);
1074   if (debug_info.is_null()) {
1075     JVMCI_ERROR("debug info expected at safepoint at %i", pc_offset);
1076   }
1077 
1078   // address instruction = _instructions->start() + pc_offset;
1079   // jint next_pc_offset = Assembler::locate_next_instruction(instruction) - _instructions->start();
1080   OopMap *map = create_oop_map(debug_info, JVMCI_CHECK);
1081   _debug_recorder->add_safepoint(pc_offset, map);
1082   record_scope(pc_offset, debug_info, CodeInstaller::FullFrame, JVMCI_CHECK);
1083   _debug_recorder->end_safepoint(pc_offset);
1084 }
1085 
1086 void CodeInstaller::site_Infopoint(CodeBuffer& buffer, jint pc_offset, JVMCIObject site, JVMCI_TRAPS) {
1087   JVMCIObject debug_info = jvmci_env()->get_site_Infopoint_debugInfo(site);
1088   if (debug_info.is_null()) {
1089     JVMCI_ERROR("debug info expected at infopoint at %i", pc_offset);
1090   }
1091 
1092   // We'd like to check that pc_offset is greater than the
1093   // last pc recorded with _debug_recorder (raising an exception if not)
1094   // but DebugInformationRecorder doesn't have sufficient public API.
1095 
1096   _debug_recorder->add_non_safepoint(pc_offset);
1097   record_scope(pc_offset, debug_info, CodeInstaller::BytecodePosition, JVMCI_CHECK);
1098   _debug_recorder->end_non_safepoint(pc_offset);
1099 }
1100 
1101 void CodeInstaller::site_Call(CodeBuffer& buffer, jint pc_offset, JVMCIObject site, JVMCI_TRAPS) {
1102   JVMCIObject target = jvmci_env()->get_site_Call_target(site);
1103   JVMCIObject hotspot_method; // JavaMethod
1104   JVMCIObject foreign_call;
1105 
1106   if (jvmci_env()->isa_HotSpotForeignCallTarget(target)) {
1107     foreign_call = target;
1108   } else {
1109     hotspot_method = target;
1110   }
1111 
1112   JVMCIObject debug_info = jvmci_env()->get_site_Infopoint_debugInfo(site);
1113 
1114   assert(hotspot_method.is_non_null() ^ foreign_call.is_non_null(), "Call site needs exactly one type");
1115 
1116   NativeInstruction* inst = nativeInstruction_at(_instructions->start() + pc_offset);
1117   jint next_pc_offset = CodeInstaller::pd_next_offset(inst, pc_offset, hotspot_method, JVMCI_CHECK);
1118 
1119   if (debug_info.is_non_null()) {
1120     OopMap *map = create_oop_map(debug_info, JVMCI_CHECK);
1121     _debug_recorder->add_safepoint(next_pc_offset, map);
1122 
1123     if (hotspot_method.is_non_null()) {
1124       Method *method = jvmci_env()->asMethod(hotspot_method);
1125       vmIntrinsics::ID iid = method->intrinsic_id();
1126       bool is_mh_invoke = false;
1127       if (jvmci_env()->get_site_Call_direct(site)) {
1128         is_mh_invoke = !method->is_static() && (iid == vmIntrinsics::_compiledLambdaForm ||
1129                 (MethodHandles::is_signature_polymorphic(iid) && MethodHandles::is_signature_polymorphic_intrinsic(iid)));
1130       }
1131       bool return_oop = method->is_returning_oop();
1132       record_scope(next_pc_offset, debug_info, CodeInstaller::FullFrame, is_mh_invoke, return_oop, JVMCI_CHECK);
1133     } else {
1134       record_scope(next_pc_offset, debug_info, CodeInstaller::FullFrame, JVMCI_CHECK);
1135     }
1136   }
1137 
1138   if (foreign_call.is_non_null()) {
1139     jlong foreign_call_destination = jvmci_env()->get_HotSpotForeignCallTarget_address(foreign_call);
1140     CodeInstaller::pd_relocate_ForeignCall(inst, foreign_call_destination, JVMCI_CHECK);
1141   } else { // method != NULL
1142     if (debug_info.is_null()) {
1143       JVMCI_ERROR("debug info expected at call at %i", pc_offset);
1144     }
1145 
1146     JVMCI_event_3("method call");
1147     CodeInstaller::pd_relocate_JavaMethod(buffer, hotspot_method, pc_offset, JVMCI_CHECK);
1148     if (_next_call_type == INVOKESTATIC || _next_call_type == INVOKESPECIAL) {
1149       // Need a static call stub for transitions from compiled to interpreted.
1150       CompiledStaticCall::emit_to_interp_stub(buffer, _instructions->start() + pc_offset);
1151     }
1152   }
1153 
1154   _next_call_type = INVOKE_INVALID;
1155 
1156   if (debug_info.is_non_null()) {
1157     _debug_recorder->end_safepoint(next_pc_offset);
1158   }
1159 }
1160 
1161 void CodeInstaller::site_DataPatch(CodeBuffer& buffer, jint pc_offset, JVMCIObject site, JVMCI_TRAPS) {
1162   JVMCIObject reference = jvmci_env()->get_site_DataPatch_reference(site);
1163   if (reference.is_null()) {
1164     JVMCI_THROW(NullPointerException);
1165   } else if (jvmci_env()->isa_site_ConstantReference(reference)) {
1166     JVMCIObject constant = jvmci_env()->get_site_ConstantReference_constant(reference);
1167     if (constant.is_null()) {
1168       JVMCI_THROW(NullPointerException);
1169     } else if (jvmci_env()->isa_DirectHotSpotObjectConstantImpl(constant)) {
1170       if (!JVMCIENV->is_hotspot()) {
1171         JVMCIObject string = JVMCIENV->call_HotSpotJVMCIRuntime_callToString(constant, JVMCI_CHECK);
1172         const char* to_string = JVMCIENV->as_utf8_string(string);
1173         JVMCI_THROW_MSG(IllegalArgumentException, err_msg("Direct object constant reached the backend: %s", to_string));
1174       }
1175       pd_patch_OopConstant(pc_offset, constant, JVMCI_CHECK);
1176     } else if (jvmci_env()->isa_IndirectHotSpotObjectConstantImpl(constant)) {
1177       pd_patch_OopConstant(pc_offset, constant, JVMCI_CHECK);
1178     } else if (jvmci_env()->isa_HotSpotMetaspaceConstantImpl(constant)) {
1179       pd_patch_MetaspaceConstant(pc_offset, constant, JVMCI_CHECK);
1180     } else {
1181       JVMCI_ERROR("unknown constant type in data patch: %s", jvmci_env()->klass_name(constant));
1182     }
1183   } else if (jvmci_env()->isa_site_DataSectionReference(reference)) {
1184     int data_offset = jvmci_env()->get_site_DataSectionReference_offset(reference);
1185     if (0 <= data_offset && data_offset < _constants_size) {
1186       pd_patch_DataSectionReference(pc_offset, data_offset, JVMCI_CHECK);
1187     } else {
1188       JVMCI_ERROR("data offset 0x%X points outside data section (size 0x%X)", data_offset, _constants_size);
1189     }
1190   } else {
1191     JVMCI_ERROR("unknown data patch type: %s", jvmci_env()->klass_name(reference));
1192   }
1193 }
1194 
1195 void CodeInstaller::site_Mark(CodeBuffer& buffer, jint pc_offset, JVMCIObject site, JVMCI_TRAPS) {
1196   JVMCIObject id_obj = jvmci_env()->get_site_Mark_id(site);
1197 
1198   if (id_obj.is_non_null()) {
1199     if (!jvmci_env()->is_boxing_object(T_INT, id_obj)) {
1200       JVMCI_ERROR("expected Integer id, got %s", jvmci_env()->klass_name(id_obj));
1201     }
1202     jint id = jvmci_env()->get_boxed_value(T_INT, id_obj).i;
1203 
1204     address pc = _instructions->start() + pc_offset;
1205 
1206     switch (id) {
1207       case UNVERIFIED_ENTRY:
1208         _offsets.set_value(CodeOffsets::Entry, pc_offset);
1209         break;
1210       case VERIFIED_ENTRY:
1211         _offsets.set_value(CodeOffsets::Verified_Entry, pc_offset);
1212         break;
1213       case OSR_ENTRY:
1214         _offsets.set_value(CodeOffsets::OSR_Entry, pc_offset);
1215         break;
1216       case EXCEPTION_HANDLER_ENTRY:
1217         _offsets.set_value(CodeOffsets::Exceptions, pc_offset);
1218         break;
1219       case DEOPT_HANDLER_ENTRY:
1220         _offsets.set_value(CodeOffsets::Deopt, pc_offset);
1221         break;
1222       case DEOPT_MH_HANDLER_ENTRY:
1223         _offsets.set_value(CodeOffsets::DeoptMH, pc_offset);
1224         break;
1225       case FRAME_COMPLETE:
1226         _offsets.set_value(CodeOffsets::Frame_Complete, pc_offset);
1227         break;
1228       case INVOKEVIRTUAL:
1229       case INVOKEINTERFACE:
1230       case INLINE_INVOKE:
1231       case INVOKESTATIC:
1232       case INVOKESPECIAL:
1233         _next_call_type = (MarkId) id;
1234         _invoke_mark_pc = pc;
1235         break;
1236       case POLL_NEAR:
1237       case POLL_FAR:
1238       case POLL_RETURN_NEAR:
1239       case POLL_RETURN_FAR:
1240         pd_relocate_poll(pc, id, JVMCI_CHECK);
1241         break;
1242       case CARD_TABLE_SHIFT:
1243       case CARD_TABLE_ADDRESS:
1244       case HEAP_TOP_ADDRESS:
1245       case HEAP_END_ADDRESS:
1246       case NARROW_KLASS_BASE_ADDRESS:
1247       case NARROW_OOP_BASE_ADDRESS:
1248       case CRC_TABLE_ADDRESS:
1249       case LOG_OF_HEAP_REGION_GRAIN_BYTES:
1250       case INLINE_CONTIGUOUS_ALLOCATION_SUPPORTED:
1251       case VERIFY_OOPS:
1252       case VERIFY_OOP_BITS:
1253       case VERIFY_OOP_MASK:
1254       case VERIFY_OOP_COUNT_ADDRESS:
1255         break;
1256       default:
1257         JVMCI_ERROR("invalid mark id: %d", id);
1258         break;
1259     }
1260   }
1261 }