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