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