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