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