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