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