1 /* 2 * Copyright (c) 1997, 2024, 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 25 #include "precompiled.hpp" 26 #include "compiler/compiler_globals.hpp" 27 #include "interp_masm_x86.hpp" 28 #include "interpreter/interpreter.hpp" 29 #include "interpreter/interpreterRuntime.hpp" 30 #include "logging/log.hpp" 31 #include "oops/arrayOop.hpp" 32 #include "oops/constMethodFlags.hpp" 33 #include "oops/markWord.hpp" 34 #include "oops/methodData.hpp" 35 #include "oops/method.hpp" 36 #include "oops/inlineKlass.hpp" 37 #include "oops/resolvedFieldEntry.hpp" 38 #include "oops/resolvedIndyEntry.hpp" 39 #include "oops/resolvedMethodEntry.hpp" 40 #include "prims/jvmtiExport.hpp" 41 #include "prims/jvmtiThreadState.hpp" 42 #include "runtime/basicLock.hpp" 43 #include "runtime/frame.inline.hpp" 44 #include "runtime/javaThread.hpp" 45 #include "runtime/safepointMechanism.hpp" 46 #include "runtime/sharedRuntime.hpp" 47 #include "utilities/powerOfTwo.hpp" 48 49 // Implementation of InterpreterMacroAssembler 50 51 void InterpreterMacroAssembler::jump_to_entry(address entry) { 52 assert(entry, "Entry must have been generated by now"); 53 jump(RuntimeAddress(entry)); 54 } 55 56 void InterpreterMacroAssembler::profile_obj_type(Register obj, const Address& mdo_addr) { 57 Label update, next, none; 58 59 #ifdef _LP64 60 assert_different_registers(obj, rscratch1, mdo_addr.base(), mdo_addr.index()); 61 #else 62 assert_different_registers(obj, mdo_addr.base(), mdo_addr.index()); 63 #endif 64 65 interp_verify_oop(obj, atos); 66 67 testptr(obj, obj); 68 jccb(Assembler::notZero, update); 69 testptr(mdo_addr, TypeEntries::null_seen); 70 jccb(Assembler::notZero, next); // null already seen. Nothing to do anymore. 71 // atomic update to prevent overwriting Klass* with 0 72 lock(); 73 orptr(mdo_addr, TypeEntries::null_seen); 74 jmpb(next); 75 76 bind(update); 77 load_klass(obj, obj, rscratch1); 78 #ifdef _LP64 79 mov(rscratch1, obj); 80 #endif 81 82 xorptr(obj, mdo_addr); 83 testptr(obj, TypeEntries::type_klass_mask); 84 jccb(Assembler::zero, next); // klass seen before, nothing to 85 // do. The unknown bit may have been 86 // set already but no need to check. 87 88 testptr(obj, TypeEntries::type_unknown); 89 jccb(Assembler::notZero, next); // already unknown. Nothing to do anymore. 90 91 cmpptr(mdo_addr, 0); 92 jccb(Assembler::equal, none); 93 cmpptr(mdo_addr, TypeEntries::null_seen); 94 jccb(Assembler::equal, none); 95 #ifdef _LP64 96 // There is a chance that the checks above (re-reading profiling 97 // data from memory) fail if another thread has just set the 98 // profiling to this obj's klass 99 mov(obj, rscratch1); 100 xorptr(obj, mdo_addr); 101 testptr(obj, TypeEntries::type_klass_mask); 102 jccb(Assembler::zero, next); 103 #endif 104 105 // different than before. Cannot keep accurate profile. 106 orptr(mdo_addr, TypeEntries::type_unknown); 107 jmpb(next); 108 109 bind(none); 110 // first time here. Set profile type. 111 movptr(mdo_addr, obj); 112 #ifdef ASSERT 113 andptr(obj, TypeEntries::type_klass_mask); 114 verify_klass_ptr(obj); 115 #endif 116 117 bind(next); 118 } 119 120 void InterpreterMacroAssembler::profile_arguments_type(Register mdp, Register callee, Register tmp, bool is_virtual) { 121 if (!ProfileInterpreter) { 122 return; 123 } 124 125 if (MethodData::profile_arguments() || MethodData::profile_return()) { 126 Label profile_continue; 127 128 test_method_data_pointer(mdp, profile_continue); 129 130 int off_to_start = is_virtual ? in_bytes(VirtualCallData::virtual_call_data_size()) : in_bytes(CounterData::counter_data_size()); 131 132 cmpb(Address(mdp, in_bytes(DataLayout::tag_offset()) - off_to_start), is_virtual ? DataLayout::virtual_call_type_data_tag : DataLayout::call_type_data_tag); 133 jcc(Assembler::notEqual, profile_continue); 134 135 if (MethodData::profile_arguments()) { 136 Label done; 137 int off_to_args = in_bytes(TypeEntriesAtCall::args_data_offset()); 138 addptr(mdp, off_to_args); 139 140 for (int i = 0; i < TypeProfileArgsLimit; i++) { 141 if (i > 0 || MethodData::profile_return()) { 142 // If return value type is profiled we may have no argument to profile 143 movptr(tmp, Address(mdp, in_bytes(TypeEntriesAtCall::cell_count_offset())-off_to_args)); 144 subl(tmp, i*TypeStackSlotEntries::per_arg_count()); 145 cmpl(tmp, TypeStackSlotEntries::per_arg_count()); 146 jcc(Assembler::less, done); 147 } 148 movptr(tmp, Address(callee, Method::const_offset())); 149 load_unsigned_short(tmp, Address(tmp, ConstMethod::size_of_parameters_offset())); 150 // stack offset o (zero based) from the start of the argument 151 // list, for n arguments translates into offset n - o - 1 from 152 // the end of the argument list 153 subptr(tmp, Address(mdp, in_bytes(TypeEntriesAtCall::stack_slot_offset(i))-off_to_args)); 154 subl(tmp, 1); 155 Address arg_addr = argument_address(tmp); 156 movptr(tmp, arg_addr); 157 158 Address mdo_arg_addr(mdp, in_bytes(TypeEntriesAtCall::argument_type_offset(i))-off_to_args); 159 profile_obj_type(tmp, mdo_arg_addr); 160 161 int to_add = in_bytes(TypeStackSlotEntries::per_arg_size()); 162 addptr(mdp, to_add); 163 off_to_args += to_add; 164 } 165 166 if (MethodData::profile_return()) { 167 movptr(tmp, Address(mdp, in_bytes(TypeEntriesAtCall::cell_count_offset())-off_to_args)); 168 subl(tmp, TypeProfileArgsLimit*TypeStackSlotEntries::per_arg_count()); 169 } 170 171 bind(done); 172 173 if (MethodData::profile_return()) { 174 // We're right after the type profile for the last 175 // argument. tmp is the number of cells left in the 176 // CallTypeData/VirtualCallTypeData to reach its end. Non null 177 // if there's a return to profile. 178 assert(SingleTypeEntry::static_cell_count() < TypeStackSlotEntries::per_arg_count(), "can't move past ret type"); 179 shll(tmp, log2i_exact((int)DataLayout::cell_size)); 180 addptr(mdp, tmp); 181 } 182 movptr(Address(rbp, frame::interpreter_frame_mdp_offset * wordSize), mdp); 183 } else { 184 assert(MethodData::profile_return(), "either profile call args or call ret"); 185 update_mdp_by_constant(mdp, in_bytes(TypeEntriesAtCall::return_only_size())); 186 } 187 188 // mdp points right after the end of the 189 // CallTypeData/VirtualCallTypeData, right after the cells for the 190 // return value type if there's one 191 192 bind(profile_continue); 193 } 194 } 195 196 void InterpreterMacroAssembler::profile_return_type(Register mdp, Register ret, Register tmp) { 197 assert_different_registers(mdp, ret, tmp, _bcp_register); 198 if (ProfileInterpreter && MethodData::profile_return()) { 199 Label profile_continue; 200 201 test_method_data_pointer(mdp, profile_continue); 202 203 if (MethodData::profile_return_jsr292_only()) { 204 assert(Method::intrinsic_id_size_in_bytes() == 2, "assuming Method::_intrinsic_id is u2"); 205 206 // If we don't profile all invoke bytecodes we must make sure 207 // it's a bytecode we indeed profile. We can't go back to the 208 // beginning of the ProfileData we intend to update to check its 209 // type because we're right after it and we don't known its 210 // length 211 Label do_profile; 212 cmpb(Address(_bcp_register, 0), Bytecodes::_invokedynamic); 213 jcc(Assembler::equal, do_profile); 214 cmpb(Address(_bcp_register, 0), Bytecodes::_invokehandle); 215 jcc(Assembler::equal, do_profile); 216 get_method(tmp); 217 cmpw(Address(tmp, Method::intrinsic_id_offset()), static_cast<int>(vmIntrinsics::_compiledLambdaForm)); 218 jcc(Assembler::notEqual, profile_continue); 219 220 bind(do_profile); 221 } 222 223 Address mdo_ret_addr(mdp, -in_bytes(SingleTypeEntry::size())); 224 mov(tmp, ret); 225 profile_obj_type(tmp, mdo_ret_addr); 226 227 bind(profile_continue); 228 } 229 } 230 231 void InterpreterMacroAssembler::profile_parameters_type(Register mdp, Register tmp1, Register tmp2) { 232 if (ProfileInterpreter && MethodData::profile_parameters()) { 233 Label profile_continue; 234 235 test_method_data_pointer(mdp, profile_continue); 236 237 // Load the offset of the area within the MDO used for 238 // parameters. If it's negative we're not profiling any parameters 239 movl(tmp1, Address(mdp, in_bytes(MethodData::parameters_type_data_di_offset()) - in_bytes(MethodData::data_offset()))); 240 testl(tmp1, tmp1); 241 jcc(Assembler::negative, profile_continue); 242 243 // Compute a pointer to the area for parameters from the offset 244 // and move the pointer to the slot for the last 245 // parameters. Collect profiling from last parameter down. 246 // mdo start + parameters offset + array length - 1 247 addptr(mdp, tmp1); 248 movptr(tmp1, Address(mdp, ArrayData::array_len_offset())); 249 decrement(tmp1, TypeStackSlotEntries::per_arg_count()); 250 251 Label loop; 252 bind(loop); 253 254 int off_base = in_bytes(ParametersTypeData::stack_slot_offset(0)); 255 int type_base = in_bytes(ParametersTypeData::type_offset(0)); 256 Address::ScaleFactor per_arg_scale = Address::times(DataLayout::cell_size); 257 Address arg_off(mdp, tmp1, per_arg_scale, off_base); 258 Address arg_type(mdp, tmp1, per_arg_scale, type_base); 259 260 // load offset on the stack from the slot for this parameter 261 movptr(tmp2, arg_off); 262 negptr(tmp2); 263 // read the parameter from the local area 264 movptr(tmp2, Address(_locals_register, tmp2, Interpreter::stackElementScale())); 265 266 // profile the parameter 267 profile_obj_type(tmp2, arg_type); 268 269 // go to next parameter 270 decrement(tmp1, TypeStackSlotEntries::per_arg_count()); 271 jcc(Assembler::positive, loop); 272 273 bind(profile_continue); 274 } 275 } 276 277 void InterpreterMacroAssembler::call_VM_leaf_base(address entry_point, 278 int number_of_arguments) { 279 // interpreter specific 280 // 281 // Note: No need to save/restore bcp & locals registers 282 // since these are callee saved registers and no blocking/ 283 // GC can happen in leaf calls. 284 // Further Note: DO NOT save/restore bcp/locals. If a caller has 285 // already saved them so that it can use rsi/rdi as temporaries 286 // then a save/restore here will DESTROY the copy the caller 287 // saved! There used to be a save_bcp() that only happened in 288 // the ASSERT path (no restore_bcp). Which caused bizarre failures 289 // when jvm built with ASSERTs. 290 #ifdef ASSERT 291 { 292 Label L; 293 cmpptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), NULL_WORD); 294 jcc(Assembler::equal, L); 295 stop("InterpreterMacroAssembler::call_VM_leaf_base:" 296 " last_sp != null"); 297 bind(L); 298 } 299 #endif 300 // super call 301 MacroAssembler::call_VM_leaf_base(entry_point, number_of_arguments); 302 // interpreter specific 303 // LP64: Used to ASSERT that r13/r14 were equal to frame's bcp/locals 304 // but since they may not have been saved (and we don't want to 305 // save them here (see note above) the assert is invalid. 306 } 307 308 void InterpreterMacroAssembler::call_VM_base(Register oop_result, 309 Register java_thread, 310 Register last_java_sp, 311 address entry_point, 312 int number_of_arguments, 313 bool check_exceptions) { 314 // interpreter specific 315 // 316 // Note: Could avoid restoring locals ptr (callee saved) - however doesn't 317 // really make a difference for these runtime calls, since they are 318 // slow anyway. Btw., bcp must be saved/restored since it may change 319 // due to GC. 320 NOT_LP64(assert(java_thread == noreg , "not expecting a precomputed java thread");) 321 save_bcp(); 322 #ifdef ASSERT 323 { 324 Label L; 325 cmpptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), NULL_WORD); 326 jcc(Assembler::equal, L); 327 stop("InterpreterMacroAssembler::call_VM_base:" 328 " last_sp isn't null"); 329 bind(L); 330 } 331 #endif /* ASSERT */ 332 // super call 333 MacroAssembler::call_VM_base(oop_result, noreg, last_java_sp, 334 entry_point, number_of_arguments, 335 check_exceptions); 336 // interpreter specific 337 restore_bcp(); 338 restore_locals(); 339 } 340 341 void InterpreterMacroAssembler::check_and_handle_popframe(Register java_thread) { 342 if (JvmtiExport::can_pop_frame()) { 343 Label L; 344 // Initiate popframe handling only if it is not already being 345 // processed. If the flag has the popframe_processing bit set, it 346 // means that this code is called *during* popframe handling - we 347 // don't want to reenter. 348 // This method is only called just after the call into the vm in 349 // call_VM_base, so the arg registers are available. 350 Register pop_cond = NOT_LP64(java_thread) // Not clear if any other register is available on 32 bit 351 LP64_ONLY(c_rarg0); 352 movl(pop_cond, Address(java_thread, JavaThread::popframe_condition_offset())); 353 testl(pop_cond, JavaThread::popframe_pending_bit); 354 jcc(Assembler::zero, L); 355 testl(pop_cond, JavaThread::popframe_processing_bit); 356 jcc(Assembler::notZero, L); 357 // Call Interpreter::remove_activation_preserving_args_entry() to get the 358 // address of the same-named entrypoint in the generated interpreter code. 359 call_VM_leaf(CAST_FROM_FN_PTR(address, Interpreter::remove_activation_preserving_args_entry)); 360 jmp(rax); 361 bind(L); 362 NOT_LP64(get_thread(java_thread);) 363 } 364 } 365 366 void InterpreterMacroAssembler::load_earlyret_value(TosState state) { 367 Register thread = LP64_ONLY(r15_thread) NOT_LP64(rcx); 368 NOT_LP64(get_thread(thread);) 369 movptr(rcx, Address(thread, JavaThread::jvmti_thread_state_offset())); 370 const Address tos_addr(rcx, JvmtiThreadState::earlyret_tos_offset()); 371 const Address oop_addr(rcx, JvmtiThreadState::earlyret_oop_offset()); 372 const Address val_addr(rcx, JvmtiThreadState::earlyret_value_offset()); 373 #ifdef _LP64 374 switch (state) { 375 case atos: movptr(rax, oop_addr); 376 movptr(oop_addr, NULL_WORD); 377 interp_verify_oop(rax, state); break; 378 case ltos: movptr(rax, val_addr); break; 379 case btos: // fall through 380 case ztos: // fall through 381 case ctos: // fall through 382 case stos: // fall through 383 case itos: movl(rax, val_addr); break; 384 case ftos: load_float(val_addr); break; 385 case dtos: load_double(val_addr); break; 386 case vtos: /* nothing to do */ break; 387 default : ShouldNotReachHere(); 388 } 389 // Clean up tos value in the thread object 390 movl(tos_addr, ilgl); 391 movl(val_addr, NULL_WORD); 392 #else 393 const Address val_addr1(rcx, JvmtiThreadState::earlyret_value_offset() 394 + in_ByteSize(wordSize)); 395 switch (state) { 396 case atos: movptr(rax, oop_addr); 397 movptr(oop_addr, NULL_WORD); 398 interp_verify_oop(rax, state); break; 399 case ltos: 400 movl(rdx, val_addr1); // fall through 401 case btos: // fall through 402 case ztos: // fall through 403 case ctos: // fall through 404 case stos: // fall through 405 case itos: movl(rax, val_addr); break; 406 case ftos: load_float(val_addr); break; 407 case dtos: load_double(val_addr); break; 408 case vtos: /* nothing to do */ break; 409 default : ShouldNotReachHere(); 410 } 411 #endif // _LP64 412 // Clean up tos value in the thread object 413 movl(tos_addr, ilgl); 414 movptr(val_addr, NULL_WORD); 415 NOT_LP64(movptr(val_addr1, NULL_WORD);) 416 } 417 418 419 void InterpreterMacroAssembler::check_and_handle_earlyret(Register java_thread) { 420 if (JvmtiExport::can_force_early_return()) { 421 Label L; 422 Register tmp = LP64_ONLY(c_rarg0) NOT_LP64(java_thread); 423 Register rthread = LP64_ONLY(r15_thread) NOT_LP64(java_thread); 424 425 movptr(tmp, Address(rthread, JavaThread::jvmti_thread_state_offset())); 426 testptr(tmp, tmp); 427 jcc(Assembler::zero, L); // if (thread->jvmti_thread_state() == nullptr) exit; 428 429 // Initiate earlyret handling only if it is not already being processed. 430 // If the flag has the earlyret_processing bit set, it means that this code 431 // is called *during* earlyret handling - we don't want to reenter. 432 movl(tmp, Address(tmp, JvmtiThreadState::earlyret_state_offset())); 433 cmpl(tmp, JvmtiThreadState::earlyret_pending); 434 jcc(Assembler::notEqual, L); 435 436 // Call Interpreter::remove_activation_early_entry() to get the address of the 437 // same-named entrypoint in the generated interpreter code. 438 NOT_LP64(get_thread(java_thread);) 439 movptr(tmp, Address(rthread, JavaThread::jvmti_thread_state_offset())); 440 #ifdef _LP64 441 movl(tmp, Address(tmp, JvmtiThreadState::earlyret_tos_offset())); 442 call_VM_leaf(CAST_FROM_FN_PTR(address, Interpreter::remove_activation_early_entry), tmp); 443 #else 444 pushl(Address(tmp, JvmtiThreadState::earlyret_tos_offset())); 445 call_VM_leaf(CAST_FROM_FN_PTR(address, Interpreter::remove_activation_early_entry), 1); 446 #endif // _LP64 447 jmp(rax); 448 bind(L); 449 NOT_LP64(get_thread(java_thread);) 450 } 451 } 452 453 void InterpreterMacroAssembler::get_unsigned_2_byte_index_at_bcp(Register reg, int bcp_offset) { 454 assert(bcp_offset >= 0, "bcp is still pointing to start of bytecode"); 455 load_unsigned_short(reg, Address(_bcp_register, bcp_offset)); 456 bswapl(reg); 457 shrl(reg, 16); 458 } 459 460 void InterpreterMacroAssembler::get_cache_index_at_bcp(Register index, 461 int bcp_offset, 462 size_t index_size) { 463 assert(bcp_offset > 0, "bcp is still pointing to start of bytecode"); 464 if (index_size == sizeof(u2)) { 465 load_unsigned_short(index, Address(_bcp_register, bcp_offset)); 466 } else if (index_size == sizeof(u4)) { 467 movl(index, Address(_bcp_register, bcp_offset)); 468 } else if (index_size == sizeof(u1)) { 469 load_unsigned_byte(index, Address(_bcp_register, bcp_offset)); 470 } else { 471 ShouldNotReachHere(); 472 } 473 } 474 475 // Load object from cpool->resolved_references(index) 476 void InterpreterMacroAssembler::load_resolved_reference_at_index(Register result, 477 Register index, 478 Register tmp) { 479 assert_different_registers(result, index); 480 481 get_constant_pool(result); 482 // load pointer for resolved_references[] objArray 483 movptr(result, Address(result, ConstantPool::cache_offset())); 484 movptr(result, Address(result, ConstantPoolCache::resolved_references_offset())); 485 resolve_oop_handle(result, tmp); 486 load_heap_oop(result, Address(result, index, 487 UseCompressedOops ? Address::times_4 : Address::times_ptr, 488 arrayOopDesc::base_offset_in_bytes(T_OBJECT)), tmp); 489 } 490 491 // load cpool->resolved_klass_at(index) 492 void InterpreterMacroAssembler::load_resolved_klass_at_index(Register klass, 493 Register cpool, 494 Register index) { 495 assert_different_registers(cpool, index); 496 497 movw(index, Address(cpool, index, Address::times_ptr, sizeof(ConstantPool))); 498 Register resolved_klasses = cpool; 499 movptr(resolved_klasses, Address(cpool, ConstantPool::resolved_klasses_offset())); 500 movptr(klass, Address(resolved_klasses, index, Address::times_ptr, Array<Klass*>::base_offset_in_bytes())); 501 } 502 503 // Generate a subtype check: branch to ok_is_subtype if sub_klass is a 504 // subtype of super_klass. 505 // 506 // Args: 507 // rax: superklass 508 // Rsub_klass: subklass 509 // 510 // Kills: 511 // rcx, rdi 512 void InterpreterMacroAssembler::gen_subtype_check(Register Rsub_klass, 513 Label& ok_is_subtype, 514 bool profile) { 515 assert(Rsub_klass != rax, "rax holds superklass"); 516 LP64_ONLY(assert(Rsub_klass != r14, "r14 holds locals");) 517 LP64_ONLY(assert(Rsub_klass != r13, "r13 holds bcp");) 518 assert(Rsub_klass != rcx, "rcx holds 2ndary super array length"); 519 assert(Rsub_klass != rdi, "rdi holds 2ndary super array scan ptr"); 520 521 // Profile the not-null value's klass. 522 if (profile) { 523 profile_typecheck(rcx, Rsub_klass, rdi); // blows rcx, reloads rdi 524 } 525 526 // Do the check. 527 check_klass_subtype(Rsub_klass, rax, rcx, ok_is_subtype); // blows rcx 528 } 529 530 531 #ifndef _LP64 532 void InterpreterMacroAssembler::f2ieee() { 533 if (IEEEPrecision) { 534 fstp_s(Address(rsp, 0)); 535 fld_s(Address(rsp, 0)); 536 } 537 } 538 539 540 void InterpreterMacroAssembler::d2ieee() { 541 if (IEEEPrecision) { 542 fstp_d(Address(rsp, 0)); 543 fld_d(Address(rsp, 0)); 544 } 545 } 546 #endif // _LP64 547 548 // Java Expression Stack 549 550 void InterpreterMacroAssembler::pop_ptr(Register r) { 551 pop(r); 552 } 553 554 void InterpreterMacroAssembler::push_ptr(Register r) { 555 push(r); 556 } 557 558 void InterpreterMacroAssembler::push_i(Register r) { 559 push(r); 560 } 561 562 void InterpreterMacroAssembler::push_i_or_ptr(Register r) { 563 push(r); 564 } 565 566 void InterpreterMacroAssembler::push_f(XMMRegister r) { 567 subptr(rsp, wordSize); 568 movflt(Address(rsp, 0), r); 569 } 570 571 void InterpreterMacroAssembler::pop_f(XMMRegister r) { 572 movflt(r, Address(rsp, 0)); 573 addptr(rsp, wordSize); 574 } 575 576 void InterpreterMacroAssembler::push_d(XMMRegister r) { 577 subptr(rsp, 2 * wordSize); 578 movdbl(Address(rsp, 0), r); 579 } 580 581 void InterpreterMacroAssembler::pop_d(XMMRegister r) { 582 movdbl(r, Address(rsp, 0)); 583 addptr(rsp, 2 * Interpreter::stackElementSize); 584 } 585 586 #ifdef _LP64 587 void InterpreterMacroAssembler::pop_i(Register r) { 588 // XXX can't use pop currently, upper half non clean 589 movl(r, Address(rsp, 0)); 590 addptr(rsp, wordSize); 591 } 592 593 void InterpreterMacroAssembler::pop_l(Register r) { 594 movq(r, Address(rsp, 0)); 595 addptr(rsp, 2 * Interpreter::stackElementSize); 596 } 597 598 void InterpreterMacroAssembler::push_l(Register r) { 599 subptr(rsp, 2 * wordSize); 600 movptr(Address(rsp, Interpreter::expr_offset_in_bytes(0)), r ); 601 movptr(Address(rsp, Interpreter::expr_offset_in_bytes(1)), NULL_WORD ); 602 } 603 604 void InterpreterMacroAssembler::pop(TosState state) { 605 switch (state) { 606 case atos: pop_ptr(); break; 607 case btos: 608 case ztos: 609 case ctos: 610 case stos: 611 case itos: pop_i(); break; 612 case ltos: pop_l(); break; 613 case ftos: pop_f(xmm0); break; 614 case dtos: pop_d(xmm0); break; 615 case vtos: /* nothing to do */ break; 616 default: ShouldNotReachHere(); 617 } 618 interp_verify_oop(rax, state); 619 } 620 621 void InterpreterMacroAssembler::push(TosState state) { 622 interp_verify_oop(rax, state); 623 switch (state) { 624 case atos: push_ptr(); break; 625 case btos: 626 case ztos: 627 case ctos: 628 case stos: 629 case itos: push_i(); break; 630 case ltos: push_l(); break; 631 case ftos: push_f(xmm0); break; 632 case dtos: push_d(xmm0); break; 633 case vtos: /* nothing to do */ break; 634 default : ShouldNotReachHere(); 635 } 636 } 637 #else 638 void InterpreterMacroAssembler::pop_i(Register r) { 639 pop(r); 640 } 641 642 void InterpreterMacroAssembler::pop_l(Register lo, Register hi) { 643 pop(lo); 644 pop(hi); 645 } 646 647 void InterpreterMacroAssembler::pop_f() { 648 fld_s(Address(rsp, 0)); 649 addptr(rsp, 1 * wordSize); 650 } 651 652 void InterpreterMacroAssembler::pop_d() { 653 fld_d(Address(rsp, 0)); 654 addptr(rsp, 2 * wordSize); 655 } 656 657 658 void InterpreterMacroAssembler::pop(TosState state) { 659 switch (state) { 660 case atos: pop_ptr(rax); break; 661 case btos: // fall through 662 case ztos: // fall through 663 case ctos: // fall through 664 case stos: // fall through 665 case itos: pop_i(rax); break; 666 case ltos: pop_l(rax, rdx); break; 667 case ftos: 668 if (UseSSE >= 1) { 669 pop_f(xmm0); 670 } else { 671 pop_f(); 672 } 673 break; 674 case dtos: 675 if (UseSSE >= 2) { 676 pop_d(xmm0); 677 } else { 678 pop_d(); 679 } 680 break; 681 case vtos: /* nothing to do */ break; 682 default : ShouldNotReachHere(); 683 } 684 interp_verify_oop(rax, state); 685 } 686 687 688 void InterpreterMacroAssembler::push_l(Register lo, Register hi) { 689 push(hi); 690 push(lo); 691 } 692 693 void InterpreterMacroAssembler::push_f() { 694 // Do not schedule for no AGI! Never write beyond rsp! 695 subptr(rsp, 1 * wordSize); 696 fstp_s(Address(rsp, 0)); 697 } 698 699 void InterpreterMacroAssembler::push_d() { 700 // Do not schedule for no AGI! Never write beyond rsp! 701 subptr(rsp, 2 * wordSize); 702 fstp_d(Address(rsp, 0)); 703 } 704 705 706 void InterpreterMacroAssembler::push(TosState state) { 707 interp_verify_oop(rax, state); 708 switch (state) { 709 case atos: push_ptr(rax); break; 710 case btos: // fall through 711 case ztos: // fall through 712 case ctos: // fall through 713 case stos: // fall through 714 case itos: push_i(rax); break; 715 case ltos: push_l(rax, rdx); break; 716 case ftos: 717 if (UseSSE >= 1) { 718 push_f(xmm0); 719 } else { 720 push_f(); 721 } 722 break; 723 case dtos: 724 if (UseSSE >= 2) { 725 push_d(xmm0); 726 } else { 727 push_d(); 728 } 729 break; 730 case vtos: /* nothing to do */ break; 731 default : ShouldNotReachHere(); 732 } 733 } 734 #endif // _LP64 735 736 737 // Helpers for swap and dup 738 void InterpreterMacroAssembler::load_ptr(int n, Register val) { 739 movptr(val, Address(rsp, Interpreter::expr_offset_in_bytes(n))); 740 } 741 742 void InterpreterMacroAssembler::store_ptr(int n, Register val) { 743 movptr(Address(rsp, Interpreter::expr_offset_in_bytes(n)), val); 744 } 745 746 747 void InterpreterMacroAssembler::prepare_to_jump_from_interpreted() { 748 // set sender sp 749 lea(_bcp_register, Address(rsp, wordSize)); 750 // record last_sp 751 mov(rcx, _bcp_register); 752 subptr(rcx, rbp); 753 sarptr(rcx, LogBytesPerWord); 754 movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), rcx); 755 } 756 757 758 // Jump to from_interpreted entry of a call unless single stepping is possible 759 // in this thread in which case we must call the i2i entry 760 void InterpreterMacroAssembler::jump_from_interpreted(Register method, Register temp) { 761 prepare_to_jump_from_interpreted(); 762 763 if (JvmtiExport::can_post_interpreter_events()) { 764 Label run_compiled_code; 765 // JVMTI events, such as single-stepping, are implemented partly by avoiding running 766 // compiled code in threads for which the event is enabled. Check here for 767 // interp_only_mode if these events CAN be enabled. 768 // interp_only is an int, on little endian it is sufficient to test the byte only 769 // Is a cmpl faster? 770 LP64_ONLY(temp = r15_thread;) 771 NOT_LP64(get_thread(temp);) 772 cmpb(Address(temp, JavaThread::interp_only_mode_offset()), 0); 773 jccb(Assembler::zero, run_compiled_code); 774 jmp(Address(method, Method::interpreter_entry_offset())); 775 bind(run_compiled_code); 776 } 777 778 jmp(Address(method, Method::from_interpreted_offset())); 779 } 780 781 // The following two routines provide a hook so that an implementation 782 // can schedule the dispatch in two parts. x86 does not do this. 783 void InterpreterMacroAssembler::dispatch_prolog(TosState state, int step) { 784 // Nothing x86 specific to be done here 785 } 786 787 void InterpreterMacroAssembler::dispatch_epilog(TosState state, int step) { 788 dispatch_next(state, step); 789 } 790 791 void InterpreterMacroAssembler::dispatch_base(TosState state, 792 address* table, 793 bool verifyoop, 794 bool generate_poll) { 795 verify_FPU(1, state); 796 if (VerifyActivationFrameSize) { 797 Label L; 798 mov(rcx, rbp); 799 subptr(rcx, rsp); 800 int32_t min_frame_size = 801 (frame::link_offset - frame::interpreter_frame_initial_sp_offset) * 802 wordSize; 803 cmpptr(rcx, min_frame_size); 804 jcc(Assembler::greaterEqual, L); 805 stop("broken stack frame"); 806 bind(L); 807 } 808 if (verifyoop) { 809 interp_verify_oop(rax, state); 810 } 811 812 address* const safepoint_table = Interpreter::safept_table(state); 813 #ifdef _LP64 814 Label no_safepoint, dispatch; 815 if (table != safepoint_table && generate_poll) { 816 NOT_PRODUCT(block_comment("Thread-local Safepoint poll")); 817 testb(Address(r15_thread, JavaThread::polling_word_offset()), SafepointMechanism::poll_bit()); 818 819 jccb(Assembler::zero, no_safepoint); 820 lea(rscratch1, ExternalAddress((address)safepoint_table)); 821 jmpb(dispatch); 822 } 823 824 bind(no_safepoint); 825 lea(rscratch1, ExternalAddress((address)table)); 826 bind(dispatch); 827 jmp(Address(rscratch1, rbx, Address::times_8)); 828 829 #else 830 Address index(noreg, rbx, Address::times_ptr); 831 if (table != safepoint_table && generate_poll) { 832 NOT_PRODUCT(block_comment("Thread-local Safepoint poll")); 833 Label no_safepoint; 834 const Register thread = rcx; 835 get_thread(thread); 836 testb(Address(thread, JavaThread::polling_word_offset()), SafepointMechanism::poll_bit()); 837 838 jccb(Assembler::zero, no_safepoint); 839 ArrayAddress dispatch_addr(ExternalAddress((address)safepoint_table), index); 840 jump(dispatch_addr, noreg); 841 bind(no_safepoint); 842 } 843 844 { 845 ArrayAddress dispatch_addr(ExternalAddress((address)table), index); 846 jump(dispatch_addr, noreg); 847 } 848 #endif // _LP64 849 } 850 851 void InterpreterMacroAssembler::dispatch_only(TosState state, bool generate_poll) { 852 dispatch_base(state, Interpreter::dispatch_table(state), true, generate_poll); 853 } 854 855 void InterpreterMacroAssembler::dispatch_only_normal(TosState state) { 856 dispatch_base(state, Interpreter::normal_table(state)); 857 } 858 859 void InterpreterMacroAssembler::dispatch_only_noverify(TosState state) { 860 dispatch_base(state, Interpreter::normal_table(state), false); 861 } 862 863 864 void InterpreterMacroAssembler::dispatch_next(TosState state, int step, bool generate_poll) { 865 // load next bytecode (load before advancing _bcp_register to prevent AGI) 866 load_unsigned_byte(rbx, Address(_bcp_register, step)); 867 // advance _bcp_register 868 increment(_bcp_register, step); 869 dispatch_base(state, Interpreter::dispatch_table(state), true, generate_poll); 870 } 871 872 void InterpreterMacroAssembler::dispatch_via(TosState state, address* table) { 873 // load current bytecode 874 load_unsigned_byte(rbx, Address(_bcp_register, 0)); 875 dispatch_base(state, table); 876 } 877 878 void InterpreterMacroAssembler::narrow(Register result) { 879 880 // Get method->_constMethod->_result_type 881 movptr(rcx, Address(rbp, frame::interpreter_frame_method_offset * wordSize)); 882 movptr(rcx, Address(rcx, Method::const_offset())); 883 load_unsigned_byte(rcx, Address(rcx, ConstMethod::result_type_offset())); 884 885 Label done, notBool, notByte, notChar; 886 887 // common case first 888 cmpl(rcx, T_INT); 889 jcc(Assembler::equal, done); 890 891 // mask integer result to narrower return type. 892 cmpl(rcx, T_BOOLEAN); 893 jcc(Assembler::notEqual, notBool); 894 andl(result, 0x1); 895 jmp(done); 896 897 bind(notBool); 898 cmpl(rcx, T_BYTE); 899 jcc(Assembler::notEqual, notByte); 900 LP64_ONLY(movsbl(result, result);) 901 NOT_LP64(shll(result, 24);) // truncate upper 24 bits 902 NOT_LP64(sarl(result, 24);) // and sign-extend byte 903 jmp(done); 904 905 bind(notByte); 906 cmpl(rcx, T_CHAR); 907 jcc(Assembler::notEqual, notChar); 908 LP64_ONLY(movzwl(result, result);) 909 NOT_LP64(andl(result, 0xFFFF);) // truncate upper 16 bits 910 jmp(done); 911 912 bind(notChar); 913 // cmpl(rcx, T_SHORT); // all that's left 914 // jcc(Assembler::notEqual, done); 915 LP64_ONLY(movswl(result, result);) 916 NOT_LP64(shll(result, 16);) // truncate upper 16 bits 917 NOT_LP64(sarl(result, 16);) // and sign-extend short 918 919 // Nothing to do for T_INT 920 bind(done); 921 } 922 923 // remove activation 924 // 925 // Apply stack watermark barrier. 926 // Unlock the receiver if this is a synchronized method. 927 // Unlock any Java monitors from synchronized blocks. 928 // Remove the activation from the stack. 929 // 930 // If there are locked Java monitors 931 // If throw_monitor_exception 932 // throws IllegalMonitorStateException 933 // Else if install_monitor_exception 934 // installs IllegalMonitorStateException 935 // Else 936 // no error processing 937 void InterpreterMacroAssembler::remove_activation( 938 TosState state, 939 Register ret_addr, 940 bool throw_monitor_exception, 941 bool install_monitor_exception, 942 bool notify_jvmdi) { 943 // Note: Registers rdx xmm0 may be in use for the 944 // result check if synchronized method 945 Label unlocked, unlock, no_unlock; 946 947 const Register rthread = LP64_ONLY(r15_thread) NOT_LP64(rcx); 948 const Register robj = LP64_ONLY(c_rarg1) NOT_LP64(rdx); 949 const Register rmon = LP64_ONLY(c_rarg1) NOT_LP64(rcx); 950 // monitor pointers need different register 951 // because rdx may have the result in it 952 NOT_LP64(get_thread(rthread);) 953 954 // The below poll is for the stack watermark barrier. It allows fixing up frames lazily, 955 // that would normally not be safe to use. Such bad returns into unsafe territory of 956 // the stack, will call InterpreterRuntime::at_unwind. 957 Label slow_path; 958 Label fast_path; 959 safepoint_poll(slow_path, rthread, true /* at_return */, false /* in_nmethod */); 960 jmp(fast_path); 961 bind(slow_path); 962 push(state); 963 set_last_Java_frame(rthread, noreg, rbp, (address)pc(), rscratch1); 964 super_call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::at_unwind), rthread); 965 NOT_LP64(get_thread(rthread);) // call_VM clobbered it, restore 966 reset_last_Java_frame(rthread, true); 967 pop(state); 968 bind(fast_path); 969 970 // get the value of _do_not_unlock_if_synchronized into rdx 971 const Address do_not_unlock_if_synchronized(rthread, 972 in_bytes(JavaThread::do_not_unlock_if_synchronized_offset())); 973 movbool(rbx, do_not_unlock_if_synchronized); 974 movbool(do_not_unlock_if_synchronized, false); // reset the flag 975 976 // get method access flags 977 movptr(rcx, Address(rbp, frame::interpreter_frame_method_offset * wordSize)); 978 movl(rcx, Address(rcx, Method::access_flags_offset())); 979 testl(rcx, JVM_ACC_SYNCHRONIZED); 980 jcc(Assembler::zero, unlocked); 981 982 // Don't unlock anything if the _do_not_unlock_if_synchronized flag 983 // is set. 984 testbool(rbx); 985 jcc(Assembler::notZero, no_unlock); 986 987 // unlock monitor 988 push(state); // save result 989 990 // BasicObjectLock will be first in list, since this is a 991 // synchronized method. However, need to check that the object has 992 // not been unlocked by an explicit monitorexit bytecode. 993 const Address monitor(rbp, frame::interpreter_frame_initial_sp_offset * 994 wordSize - (int) sizeof(BasicObjectLock)); 995 // We use c_rarg1/rdx so that if we go slow path it will be the correct 996 // register for unlock_object to pass to VM directly 997 lea(robj, monitor); // address of first monitor 998 999 movptr(rax, Address(robj, BasicObjectLock::obj_offset())); 1000 testptr(rax, rax); 1001 jcc(Assembler::notZero, unlock); 1002 1003 pop(state); 1004 if (throw_monitor_exception) { 1005 // Entry already unlocked, need to throw exception 1006 NOT_LP64(empty_FPU_stack();) // remove possible return value from FPU-stack, otherwise stack could overflow 1007 call_VM(noreg, CAST_FROM_FN_PTR(address, 1008 InterpreterRuntime::throw_illegal_monitor_state_exception)); 1009 should_not_reach_here(); 1010 } else { 1011 // Monitor already unlocked during a stack unroll. If requested, 1012 // install an illegal_monitor_state_exception. Continue with 1013 // stack unrolling. 1014 if (install_monitor_exception) { 1015 NOT_LP64(empty_FPU_stack();) 1016 call_VM(noreg, CAST_FROM_FN_PTR(address, 1017 InterpreterRuntime::new_illegal_monitor_state_exception)); 1018 } 1019 jmp(unlocked); 1020 } 1021 1022 bind(unlock); 1023 unlock_object(robj); 1024 pop(state); 1025 1026 // Check that for block-structured locking (i.e., that all locked 1027 // objects has been unlocked) 1028 bind(unlocked); 1029 1030 // rax, rdx: Might contain return value 1031 1032 // Check that all monitors are unlocked 1033 { 1034 Label loop, exception, entry, restart; 1035 const int entry_size = frame::interpreter_frame_monitor_size_in_bytes(); 1036 const Address monitor_block_top( 1037 rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize); 1038 const Address monitor_block_bot( 1039 rbp, frame::interpreter_frame_initial_sp_offset * wordSize); 1040 1041 bind(restart); 1042 // We use c_rarg1 so that if we go slow path it will be the correct 1043 // register for unlock_object to pass to VM directly 1044 movptr(rmon, monitor_block_top); // derelativize pointer 1045 lea(rmon, Address(rbp, rmon, Address::times_ptr)); 1046 // c_rarg1 points to current entry, starting with top-most entry 1047 1048 lea(rbx, monitor_block_bot); // points to word before bottom of 1049 // monitor block 1050 jmp(entry); 1051 1052 // Entry already locked, need to throw exception 1053 bind(exception); 1054 1055 if (throw_monitor_exception) { 1056 // Throw exception 1057 NOT_LP64(empty_FPU_stack();) 1058 MacroAssembler::call_VM(noreg, 1059 CAST_FROM_FN_PTR(address, InterpreterRuntime:: 1060 throw_illegal_monitor_state_exception)); 1061 should_not_reach_here(); 1062 } else { 1063 // Stack unrolling. Unlock object and install illegal_monitor_exception. 1064 // Unlock does not block, so don't have to worry about the frame. 1065 // We don't have to preserve c_rarg1 since we are going to throw an exception. 1066 1067 push(state); 1068 mov(robj, rmon); // nop if robj and rmon are the same 1069 unlock_object(robj); 1070 pop(state); 1071 1072 if (install_monitor_exception) { 1073 NOT_LP64(empty_FPU_stack();) 1074 call_VM(noreg, CAST_FROM_FN_PTR(address, 1075 InterpreterRuntime:: 1076 new_illegal_monitor_state_exception)); 1077 } 1078 1079 jmp(restart); 1080 } 1081 1082 bind(loop); 1083 // check if current entry is used 1084 cmpptr(Address(rmon, BasicObjectLock::obj_offset()), NULL_WORD); 1085 jcc(Assembler::notEqual, exception); 1086 1087 addptr(rmon, entry_size); // otherwise advance to next entry 1088 bind(entry); 1089 cmpptr(rmon, rbx); // check if bottom reached 1090 jcc(Assembler::notEqual, loop); // if not at bottom then check this entry 1091 } 1092 1093 bind(no_unlock); 1094 1095 // jvmti support 1096 if (notify_jvmdi) { 1097 notify_method_exit(state, NotifyJVMTI); // preserve TOSCA 1098 } else { 1099 notify_method_exit(state, SkipNotifyJVMTI); // preserve TOSCA 1100 } 1101 1102 if (StackReservedPages > 0) { 1103 movptr(rbx, 1104 Address(rbp, frame::interpreter_frame_sender_sp_offset * wordSize)); 1105 // testing if reserved zone needs to be re-enabled 1106 Register rthread = LP64_ONLY(r15_thread) NOT_LP64(rcx); 1107 Label no_reserved_zone_enabling; 1108 1109 NOT_LP64(get_thread(rthread);) 1110 1111 // check if already enabled - if so no re-enabling needed 1112 assert(sizeof(StackOverflow::StackGuardState) == 4, "unexpected size"); 1113 cmpl(Address(rthread, JavaThread::stack_guard_state_offset()), StackOverflow::stack_guard_enabled); 1114 jcc(Assembler::equal, no_reserved_zone_enabling); 1115 1116 cmpptr(rbx, Address(rthread, JavaThread::reserved_stack_activation_offset())); 1117 jcc(Assembler::lessEqual, no_reserved_zone_enabling); 1118 1119 call_VM_leaf( 1120 CAST_FROM_FN_PTR(address, SharedRuntime::enable_stack_reserved_zone), rthread); 1121 call_VM(noreg, CAST_FROM_FN_PTR(address, 1122 InterpreterRuntime::throw_delayed_StackOverflowError)); 1123 should_not_reach_here(); 1124 1125 bind(no_reserved_zone_enabling); 1126 } 1127 1128 // remove activation 1129 // get sender sp 1130 movptr(rbx, 1131 Address(rbp, frame::interpreter_frame_sender_sp_offset * wordSize)); 1132 1133 if (state == atos && InlineTypeReturnedAsFields) { 1134 // Check if we are returning an non-null inline type and load its fields into registers 1135 Label skip; 1136 test_oop_is_not_inline_type(rax, rscratch1, skip); 1137 1138 #ifndef _LP64 1139 super_call_VM_leaf(StubRoutines::load_inline_type_fields_in_regs()); 1140 #else 1141 // Load fields from a buffered value with an inline class specific handler 1142 load_klass(rdi, rax, rscratch1); 1143 movptr(rdi, Address(rdi, InstanceKlass::adr_inlineklass_fixed_block_offset())); 1144 movptr(rdi, Address(rdi, InlineKlass::unpack_handler_offset())); 1145 // Unpack handler can be null if inline type is not scalarizable in returns 1146 testptr(rdi, rdi); 1147 jcc(Assembler::zero, skip); 1148 call(rdi); 1149 #endif 1150 #ifdef ASSERT 1151 // TODO 8284443 Enable 1152 if (StressCallingConvention && false) { 1153 Label skip_stress; 1154 movptr(rscratch1, Address(rbp, frame::interpreter_frame_method_offset * wordSize)); 1155 movl(rscratch1, Address(rscratch1, Method::flags_offset())); 1156 testl(rcx, MethodFlags::has_scalarized_return_flag()); 1157 jcc(Assembler::zero, skip_stress); 1158 load_klass(rax, rax, rscratch1); 1159 orptr(rax, 1); 1160 bind(skip_stress); 1161 } 1162 #endif 1163 // call above kills the value in rbx. Reload it. 1164 movptr(rbx, Address(rbp, frame::interpreter_frame_sender_sp_offset * wordSize)); 1165 bind(skip); 1166 } 1167 leave(); // remove frame anchor 1168 pop(ret_addr); // get return address 1169 mov(rsp, rbx); // set sp to sender sp 1170 pop_cont_fastpath(); 1171 } 1172 1173 void InterpreterMacroAssembler::get_method_counters(Register method, 1174 Register mcs, Label& skip) { 1175 Label has_counters; 1176 movptr(mcs, Address(method, Method::method_counters_offset())); 1177 testptr(mcs, mcs); 1178 jcc(Assembler::notZero, has_counters); 1179 call_VM(noreg, CAST_FROM_FN_PTR(address, 1180 InterpreterRuntime::build_method_counters), method); 1181 movptr(mcs, Address(method,Method::method_counters_offset())); 1182 testptr(mcs, mcs); 1183 jcc(Assembler::zero, skip); // No MethodCounters allocated, OutOfMemory 1184 bind(has_counters); 1185 } 1186 1187 void InterpreterMacroAssembler::allocate_instance(Register klass, Register new_obj, 1188 Register t1, Register t2, 1189 bool clear_fields, Label& alloc_failed) { 1190 MacroAssembler::allocate_instance(klass, new_obj, t1, t2, clear_fields, alloc_failed); 1191 if (DTraceMethodProbes) { 1192 // Trigger dtrace event for fastpath 1193 push(atos); 1194 call_VM_leaf(CAST_FROM_FN_PTR(address, static_cast<int (*)(oopDesc*)>(SharedRuntime::dtrace_object_alloc)), new_obj); 1195 pop(atos); 1196 } 1197 } 1198 1199 void InterpreterMacroAssembler::read_flat_field(Register entry, Register tmp1, Register tmp2, Register obj) { 1200 Label alloc_failed, empty_value, done; 1201 const Register alloc_temp = LP64_ONLY(rscratch1) NOT_LP64(rsi); 1202 const Register dst_temp = LP64_ONLY(rscratch2) NOT_LP64(rdi); 1203 assert_different_registers(obj, entry, tmp1, tmp2, dst_temp, r8, r9); 1204 1205 // FIXME: code below could be re-written to better use InlineLayoutInfo data structure 1206 // see aarch64 version 1207 1208 // Grap the inline field klass 1209 const Register field_klass = tmp1; 1210 load_unsigned_short(tmp2, Address(entry, in_bytes(ResolvedFieldEntry::field_index_offset()))); 1211 movptr(tmp1, Address(entry, ResolvedFieldEntry::field_holder_offset())); 1212 get_inline_type_field_klass(tmp1, tmp2, field_klass); 1213 1214 //check for empty value klass 1215 test_klass_is_empty_inline_type(field_klass, dst_temp, empty_value); 1216 1217 // allocate buffer 1218 push(obj); // push object being read from // FIXME spilling on stack could probably be avoided by using tmp2 1219 allocate_instance(field_klass, obj, alloc_temp, dst_temp, false, alloc_failed); 1220 1221 // Have an oop instance buffer, copy into it 1222 load_unsigned_short(r9, Address(entry, in_bytes(ResolvedFieldEntry::field_index_offset()))); 1223 movptr(r8, Address(entry, in_bytes(ResolvedFieldEntry::field_holder_offset()))); 1224 inline_layout_info(r8, r9, r8); // holder, index, info => InlineLayoutInfo into r8 1225 1226 data_for_oop(obj, dst_temp, field_klass); 1227 pop(alloc_temp); // restore object being read from 1228 load_sized_value(tmp2, Address(entry, in_bytes(ResolvedFieldEntry::field_offset_offset())), sizeof(int), true /*is_signed*/); 1229 lea(tmp2, Address(alloc_temp, tmp2)); 1230 // call_VM_leaf, clobbers a few regs, save restore new obj 1231 push(obj); 1232 // access_value_copy(IS_DEST_UNINITIALIZED, tmp2, dst_temp, field_klass); 1233 flat_field_copy(IS_DEST_UNINITIALIZED, tmp2, dst_temp, r8); 1234 pop(obj); 1235 jmp(done); 1236 1237 bind(empty_value); 1238 get_empty_inline_type_oop(field_klass, dst_temp, obj); 1239 jmp(done); 1240 1241 bind(alloc_failed); 1242 pop(obj); 1243 call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::read_flat_field), 1244 obj, entry); 1245 get_vm_result(obj, r15_thread); 1246 bind(done); 1247 } 1248 1249 void InterpreterMacroAssembler::read_flat_element(Register array, Register index, 1250 Register t1, Register t2, 1251 Register obj) { 1252 assert_different_registers(array, index, t1, t2); 1253 Label alloc_failed, empty_value, done; 1254 const Register array_klass = t2; 1255 const Register elem_klass = t1; 1256 const Register alloc_temp = LP64_ONLY(rscratch1) NOT_LP64(rsi); 1257 const Register dst_temp = LP64_ONLY(rscratch2) NOT_LP64(rdi); 1258 1259 // load in array->klass()->element_klass() 1260 Register tmp_load_klass = LP64_ONLY(rscratch1) NOT_LP64(noreg); 1261 load_klass(array_klass, array, tmp_load_klass); 1262 movptr(elem_klass, Address(array_klass, ArrayKlass::element_klass_offset())); 1263 1264 //check for empty value klass 1265 test_klass_is_empty_inline_type(elem_klass, dst_temp, empty_value); 1266 1267 // calc source into "array_klass" and free up some regs 1268 const Register src = array_klass; 1269 push(index); // preserve index reg in case alloc_failed 1270 data_for_value_array_index(array, array_klass, index, src); 1271 1272 allocate_instance(elem_klass, obj, alloc_temp, dst_temp, false, alloc_failed); 1273 // Have an oop instance buffer, copy into it 1274 store_ptr(0, obj); // preserve obj (overwrite index, no longer needed) 1275 data_for_oop(obj, dst_temp, elem_klass); 1276 access_value_copy(IS_DEST_UNINITIALIZED, src, dst_temp, elem_klass); 1277 pop(obj); 1278 jmp(done); 1279 1280 bind(empty_value); 1281 get_empty_inline_type_oop(elem_klass, dst_temp, obj); 1282 jmp(done); 1283 1284 bind(alloc_failed); 1285 pop(index); 1286 if (array == c_rarg2) { 1287 mov(elem_klass, array); 1288 array = elem_klass; 1289 } 1290 call_VM(obj, CAST_FROM_FN_PTR(address, InterpreterRuntime::value_array_load), array, index); 1291 1292 bind(done); 1293 } 1294 1295 1296 // Lock object 1297 // 1298 // Args: 1299 // rdx, c_rarg1: BasicObjectLock to be used for locking 1300 // 1301 // Kills: 1302 // rax, rbx 1303 void InterpreterMacroAssembler::lock_object(Register lock_reg) { 1304 assert(lock_reg == LP64_ONLY(c_rarg1) NOT_LP64(rdx), 1305 "The argument is only for looks. It must be c_rarg1"); 1306 1307 if (LockingMode == LM_MONITOR) { 1308 call_VM(noreg, 1309 CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter), 1310 lock_reg); 1311 } else { 1312 Label count_locking, done, slow_case; 1313 1314 const Register swap_reg = rax; // Must use rax for cmpxchg instruction 1315 const Register tmp_reg = rbx; 1316 const Register obj_reg = LP64_ONLY(c_rarg3) NOT_LP64(rcx); // Will contain the oop 1317 const Register rklass_decode_tmp = rscratch1; 1318 1319 const int obj_offset = in_bytes(BasicObjectLock::obj_offset()); 1320 const int lock_offset = in_bytes(BasicObjectLock::lock_offset()); 1321 const int mark_offset = lock_offset + 1322 BasicLock::displaced_header_offset_in_bytes(); 1323 1324 // Load object pointer into obj_reg 1325 movptr(obj_reg, Address(lock_reg, obj_offset)); 1326 1327 if (DiagnoseSyncOnValueBasedClasses != 0) { 1328 load_klass(tmp_reg, obj_reg, rklass_decode_tmp); 1329 testb(Address(tmp_reg, Klass::misc_flags_offset()), KlassFlags::_misc_is_value_based_class); 1330 jcc(Assembler::notZero, slow_case); 1331 } 1332 1333 if (LockingMode == LM_LIGHTWEIGHT) { 1334 #ifdef _LP64 1335 const Register thread = r15_thread; 1336 lightweight_lock(lock_reg, obj_reg, swap_reg, thread, tmp_reg, slow_case); 1337 #else 1338 // Lacking registers and thread on x86_32. Always take slow path. 1339 jmp(slow_case); 1340 #endif 1341 } else if (LockingMode == LM_LEGACY) { 1342 // Load immediate 1 into swap_reg %rax 1343 movl(swap_reg, 1); 1344 1345 // Load (object->mark() | 1) into swap_reg %rax 1346 orptr(swap_reg, Address(obj_reg, oopDesc::mark_offset_in_bytes())); 1347 if (EnableValhalla) { 1348 // Mask inline_type bit such that we go to the slow path if object is an inline type 1349 andptr(swap_reg, ~((int) markWord::inline_type_bit_in_place)); 1350 } 1351 1352 // Save (object->mark() | 1) into BasicLock's displaced header 1353 movptr(Address(lock_reg, mark_offset), swap_reg); 1354 1355 assert(lock_offset == 0, 1356 "displaced header must be first word in BasicObjectLock"); 1357 1358 lock(); 1359 cmpxchgptr(lock_reg, Address(obj_reg, oopDesc::mark_offset_in_bytes())); 1360 jcc(Assembler::zero, count_locking); 1361 1362 const int zero_bits = LP64_ONLY(7) NOT_LP64(3); 1363 1364 // Fast check for recursive lock. 1365 // 1366 // Can apply the optimization only if this is a stack lock 1367 // allocated in this thread. For efficiency, we can focus on 1368 // recently allocated stack locks (instead of reading the stack 1369 // base and checking whether 'mark' points inside the current 1370 // thread stack): 1371 // 1) (mark & zero_bits) == 0, and 1372 // 2) rsp <= mark < mark + os::pagesize() 1373 // 1374 // Warning: rsp + os::pagesize can overflow the stack base. We must 1375 // neither apply the optimization for an inflated lock allocated 1376 // just above the thread stack (this is why condition 1 matters) 1377 // nor apply the optimization if the stack lock is inside the stack 1378 // of another thread. The latter is avoided even in case of overflow 1379 // because we have guard pages at the end of all stacks. Hence, if 1380 // we go over the stack base and hit the stack of another thread, 1381 // this should not be in a writeable area that could contain a 1382 // stack lock allocated by that thread. As a consequence, a stack 1383 // lock less than page size away from rsp is guaranteed to be 1384 // owned by the current thread. 1385 // 1386 // These 3 tests can be done by evaluating the following 1387 // expression: ((mark - rsp) & (zero_bits - os::vm_page_size())), 1388 // assuming both stack pointer and pagesize have their 1389 // least significant bits clear. 1390 // NOTE: the mark is in swap_reg %rax as the result of cmpxchg 1391 subptr(swap_reg, rsp); 1392 andptr(swap_reg, zero_bits - (int)os::vm_page_size()); 1393 1394 // Save the test result, for recursive case, the result is zero 1395 movptr(Address(lock_reg, mark_offset), swap_reg); 1396 jcc(Assembler::notZero, slow_case); 1397 1398 bind(count_locking); 1399 } 1400 inc_held_monitor_count(); 1401 jmp(done); 1402 1403 bind(slow_case); 1404 1405 // Call the runtime routine for slow case 1406 call_VM(noreg, 1407 CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter), 1408 lock_reg); 1409 bind(done); 1410 } 1411 } 1412 1413 1414 // Unlocks an object. Used in monitorexit bytecode and 1415 // remove_activation. Throws an IllegalMonitorException if object is 1416 // not locked by current thread. 1417 // 1418 // Args: 1419 // rdx, c_rarg1: BasicObjectLock for lock 1420 // 1421 // Kills: 1422 // rax 1423 // c_rarg0, c_rarg1, c_rarg2, c_rarg3, ... (param regs) 1424 // rscratch1 (scratch reg) 1425 // rax, rbx, rcx, rdx 1426 void InterpreterMacroAssembler::unlock_object(Register lock_reg) { 1427 assert(lock_reg == LP64_ONLY(c_rarg1) NOT_LP64(rdx), 1428 "The argument is only for looks. It must be c_rarg1"); 1429 1430 if (LockingMode == LM_MONITOR) { 1431 call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit), lock_reg); 1432 } else { 1433 Label count_locking, done, slow_case; 1434 1435 const Register swap_reg = rax; // Must use rax for cmpxchg instruction 1436 const Register header_reg = LP64_ONLY(c_rarg2) NOT_LP64(rbx); // Will contain the old oopMark 1437 const Register obj_reg = LP64_ONLY(c_rarg3) NOT_LP64(rcx); // Will contain the oop 1438 1439 save_bcp(); // Save in case of exception 1440 1441 if (LockingMode != LM_LIGHTWEIGHT) { 1442 // Convert from BasicObjectLock structure to object and BasicLock 1443 // structure Store the BasicLock address into %rax 1444 lea(swap_reg, Address(lock_reg, BasicObjectLock::lock_offset())); 1445 } 1446 1447 // Load oop into obj_reg(%c_rarg3) 1448 movptr(obj_reg, Address(lock_reg, BasicObjectLock::obj_offset())); 1449 1450 // Free entry 1451 movptr(Address(lock_reg, BasicObjectLock::obj_offset()), NULL_WORD); 1452 1453 if (LockingMode == LM_LIGHTWEIGHT) { 1454 #ifdef _LP64 1455 lightweight_unlock(obj_reg, swap_reg, r15_thread, header_reg, slow_case); 1456 #else 1457 // Lacking registers and thread on x86_32. Always take slow path. 1458 jmp(slow_case); 1459 #endif 1460 } else if (LockingMode == LM_LEGACY) { 1461 // Load the old header from BasicLock structure 1462 movptr(header_reg, Address(swap_reg, 1463 BasicLock::displaced_header_offset_in_bytes())); 1464 1465 // Test for recursion 1466 testptr(header_reg, header_reg); 1467 1468 // zero for recursive case 1469 jcc(Assembler::zero, count_locking); 1470 1471 // Atomic swap back the old header 1472 lock(); 1473 cmpxchgptr(header_reg, Address(obj_reg, oopDesc::mark_offset_in_bytes())); 1474 1475 // zero for simple unlock of a stack-lock case 1476 jcc(Assembler::notZero, slow_case); 1477 1478 bind(count_locking); 1479 } 1480 dec_held_monitor_count(); 1481 jmp(done); 1482 1483 bind(slow_case); 1484 // Call the runtime routine for slow case. 1485 movptr(Address(lock_reg, BasicObjectLock::obj_offset()), obj_reg); // restore obj 1486 call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit), lock_reg); 1487 1488 bind(done); 1489 1490 restore_bcp(); 1491 } 1492 } 1493 1494 void InterpreterMacroAssembler::test_method_data_pointer(Register mdp, 1495 Label& zero_continue) { 1496 assert(ProfileInterpreter, "must be profiling interpreter"); 1497 movptr(mdp, Address(rbp, frame::interpreter_frame_mdp_offset * wordSize)); 1498 testptr(mdp, mdp); 1499 jcc(Assembler::zero, zero_continue); 1500 } 1501 1502 1503 // Set the method data pointer for the current bcp. 1504 void InterpreterMacroAssembler::set_method_data_pointer_for_bcp() { 1505 assert(ProfileInterpreter, "must be profiling interpreter"); 1506 Label set_mdp; 1507 push(rax); 1508 push(rbx); 1509 1510 get_method(rbx); 1511 // Test MDO to avoid the call if it is null. 1512 movptr(rax, Address(rbx, in_bytes(Method::method_data_offset()))); 1513 testptr(rax, rax); 1514 jcc(Assembler::zero, set_mdp); 1515 // rbx: method 1516 // _bcp_register: bcp 1517 call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::bcp_to_di), rbx, _bcp_register); 1518 // rax: mdi 1519 // mdo is guaranteed to be non-zero here, we checked for it before the call. 1520 movptr(rbx, Address(rbx, in_bytes(Method::method_data_offset()))); 1521 addptr(rbx, in_bytes(MethodData::data_offset())); 1522 addptr(rax, rbx); 1523 bind(set_mdp); 1524 movptr(Address(rbp, frame::interpreter_frame_mdp_offset * wordSize), rax); 1525 pop(rbx); 1526 pop(rax); 1527 } 1528 1529 void InterpreterMacroAssembler::verify_method_data_pointer() { 1530 assert(ProfileInterpreter, "must be profiling interpreter"); 1531 #ifdef ASSERT 1532 Label verify_continue; 1533 push(rax); 1534 push(rbx); 1535 Register arg3_reg = LP64_ONLY(c_rarg3) NOT_LP64(rcx); 1536 Register arg2_reg = LP64_ONLY(c_rarg2) NOT_LP64(rdx); 1537 push(arg3_reg); 1538 push(arg2_reg); 1539 test_method_data_pointer(arg3_reg, verify_continue); // If mdp is zero, continue 1540 get_method(rbx); 1541 1542 // If the mdp is valid, it will point to a DataLayout header which is 1543 // consistent with the bcp. The converse is highly probable also. 1544 load_unsigned_short(arg2_reg, 1545 Address(arg3_reg, in_bytes(DataLayout::bci_offset()))); 1546 addptr(arg2_reg, Address(rbx, Method::const_offset())); 1547 lea(arg2_reg, Address(arg2_reg, ConstMethod::codes_offset())); 1548 cmpptr(arg2_reg, _bcp_register); 1549 jcc(Assembler::equal, verify_continue); 1550 // rbx: method 1551 // _bcp_register: bcp 1552 // c_rarg3: mdp 1553 call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::verify_mdp), 1554 rbx, _bcp_register, arg3_reg); 1555 bind(verify_continue); 1556 pop(arg2_reg); 1557 pop(arg3_reg); 1558 pop(rbx); 1559 pop(rax); 1560 #endif // ASSERT 1561 } 1562 1563 1564 void InterpreterMacroAssembler::set_mdp_data_at(Register mdp_in, 1565 int constant, 1566 Register value) { 1567 assert(ProfileInterpreter, "must be profiling interpreter"); 1568 Address data(mdp_in, constant); 1569 movptr(data, value); 1570 } 1571 1572 1573 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in, 1574 int constant, 1575 bool decrement) { 1576 // Counter address 1577 Address data(mdp_in, constant); 1578 1579 increment_mdp_data_at(data, decrement); 1580 } 1581 1582 void InterpreterMacroAssembler::increment_mdp_data_at(Address data, 1583 bool decrement) { 1584 assert(ProfileInterpreter, "must be profiling interpreter"); 1585 // %%% this does 64bit counters at best it is wasting space 1586 // at worst it is a rare bug when counters overflow 1587 1588 if (decrement) { 1589 // Decrement the register. Set condition codes. 1590 addptr(data, -DataLayout::counter_increment); 1591 // If the decrement causes the counter to overflow, stay negative 1592 Label L; 1593 jcc(Assembler::negative, L); 1594 addptr(data, DataLayout::counter_increment); 1595 bind(L); 1596 } else { 1597 assert(DataLayout::counter_increment == 1, 1598 "flow-free idiom only works with 1"); 1599 // Increment the register. Set carry flag. 1600 addptr(data, DataLayout::counter_increment); 1601 // If the increment causes the counter to overflow, pull back by 1. 1602 sbbptr(data, 0); 1603 } 1604 } 1605 1606 1607 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in, 1608 Register reg, 1609 int constant, 1610 bool decrement) { 1611 Address data(mdp_in, reg, Address::times_1, constant); 1612 1613 increment_mdp_data_at(data, decrement); 1614 } 1615 1616 void InterpreterMacroAssembler::set_mdp_flag_at(Register mdp_in, 1617 int flag_byte_constant) { 1618 assert(ProfileInterpreter, "must be profiling interpreter"); 1619 int header_offset = in_bytes(DataLayout::flags_offset()); 1620 int header_bits = flag_byte_constant; 1621 // Set the flag 1622 orb(Address(mdp_in, header_offset), header_bits); 1623 } 1624 1625 1626 1627 void InterpreterMacroAssembler::test_mdp_data_at(Register mdp_in, 1628 int offset, 1629 Register value, 1630 Register test_value_out, 1631 Label& not_equal_continue) { 1632 assert(ProfileInterpreter, "must be profiling interpreter"); 1633 if (test_value_out == noreg) { 1634 cmpptr(value, Address(mdp_in, offset)); 1635 } else { 1636 // Put the test value into a register, so caller can use it: 1637 movptr(test_value_out, Address(mdp_in, offset)); 1638 cmpptr(test_value_out, value); 1639 } 1640 jcc(Assembler::notEqual, not_equal_continue); 1641 } 1642 1643 1644 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in, 1645 int offset_of_disp) { 1646 assert(ProfileInterpreter, "must be profiling interpreter"); 1647 Address disp_address(mdp_in, offset_of_disp); 1648 addptr(mdp_in, disp_address); 1649 movptr(Address(rbp, frame::interpreter_frame_mdp_offset * wordSize), mdp_in); 1650 } 1651 1652 1653 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in, 1654 Register reg, 1655 int offset_of_disp) { 1656 assert(ProfileInterpreter, "must be profiling interpreter"); 1657 Address disp_address(mdp_in, reg, Address::times_1, offset_of_disp); 1658 addptr(mdp_in, disp_address); 1659 movptr(Address(rbp, frame::interpreter_frame_mdp_offset * wordSize), mdp_in); 1660 } 1661 1662 1663 void InterpreterMacroAssembler::update_mdp_by_constant(Register mdp_in, 1664 int constant) { 1665 assert(ProfileInterpreter, "must be profiling interpreter"); 1666 addptr(mdp_in, constant); 1667 movptr(Address(rbp, frame::interpreter_frame_mdp_offset * wordSize), mdp_in); 1668 } 1669 1670 1671 void InterpreterMacroAssembler::update_mdp_for_ret(Register return_bci) { 1672 assert(ProfileInterpreter, "must be profiling interpreter"); 1673 push(return_bci); // save/restore across call_VM 1674 call_VM(noreg, 1675 CAST_FROM_FN_PTR(address, InterpreterRuntime::update_mdp_for_ret), 1676 return_bci); 1677 pop(return_bci); 1678 } 1679 1680 1681 void InterpreterMacroAssembler::profile_taken_branch(Register mdp, 1682 Register bumped_count) { 1683 if (ProfileInterpreter) { 1684 Label profile_continue; 1685 1686 // If no method data exists, go to profile_continue. 1687 // Otherwise, assign to mdp 1688 test_method_data_pointer(mdp, profile_continue); 1689 1690 // We are taking a branch. Increment the taken count. 1691 // We inline increment_mdp_data_at to return bumped_count in a register 1692 //increment_mdp_data_at(mdp, in_bytes(JumpData::taken_offset())); 1693 Address data(mdp, in_bytes(JumpData::taken_offset())); 1694 movptr(bumped_count, data); 1695 assert(DataLayout::counter_increment == 1, 1696 "flow-free idiom only works with 1"); 1697 addptr(bumped_count, DataLayout::counter_increment); 1698 sbbptr(bumped_count, 0); 1699 movptr(data, bumped_count); // Store back out 1700 1701 // The method data pointer needs to be updated to reflect the new target. 1702 update_mdp_by_offset(mdp, in_bytes(JumpData::displacement_offset())); 1703 bind(profile_continue); 1704 } 1705 } 1706 1707 1708 void InterpreterMacroAssembler::profile_not_taken_branch(Register mdp, bool acmp) { 1709 if (ProfileInterpreter) { 1710 Label profile_continue; 1711 1712 // If no method data exists, go to profile_continue. 1713 test_method_data_pointer(mdp, profile_continue); 1714 1715 // We are taking a branch. Increment the not taken count. 1716 increment_mdp_data_at(mdp, in_bytes(BranchData::not_taken_offset())); 1717 1718 // The method data pointer needs to be updated to correspond to 1719 // the next bytecode 1720 update_mdp_by_constant(mdp, acmp ? in_bytes(ACmpData::acmp_data_size()): in_bytes(BranchData::branch_data_size())); 1721 bind(profile_continue); 1722 } 1723 } 1724 1725 void InterpreterMacroAssembler::profile_call(Register mdp) { 1726 if (ProfileInterpreter) { 1727 Label profile_continue; 1728 1729 // If no method data exists, go to profile_continue. 1730 test_method_data_pointer(mdp, profile_continue); 1731 1732 // We are making a call. Increment the count. 1733 increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset())); 1734 1735 // The method data pointer needs to be updated to reflect the new target. 1736 update_mdp_by_constant(mdp, in_bytes(CounterData::counter_data_size())); 1737 bind(profile_continue); 1738 } 1739 } 1740 1741 1742 void InterpreterMacroAssembler::profile_final_call(Register mdp) { 1743 if (ProfileInterpreter) { 1744 Label profile_continue; 1745 1746 // If no method data exists, go to profile_continue. 1747 test_method_data_pointer(mdp, profile_continue); 1748 1749 // We are making a call. Increment the count. 1750 increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset())); 1751 1752 // The method data pointer needs to be updated to reflect the new target. 1753 update_mdp_by_constant(mdp, 1754 in_bytes(VirtualCallData:: 1755 virtual_call_data_size())); 1756 bind(profile_continue); 1757 } 1758 } 1759 1760 1761 void InterpreterMacroAssembler::profile_virtual_call(Register receiver, 1762 Register mdp, 1763 Register reg2, 1764 bool receiver_can_be_null) { 1765 if (ProfileInterpreter) { 1766 Label profile_continue; 1767 1768 // If no method data exists, go to profile_continue. 1769 test_method_data_pointer(mdp, profile_continue); 1770 1771 Label skip_receiver_profile; 1772 if (receiver_can_be_null) { 1773 Label not_null; 1774 testptr(receiver, receiver); 1775 jccb(Assembler::notZero, not_null); 1776 // We are making a call. Increment the count for null receiver. 1777 increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset())); 1778 jmp(skip_receiver_profile); 1779 bind(not_null); 1780 } 1781 1782 // Record the receiver type. 1783 record_klass_in_profile(receiver, mdp, reg2); 1784 bind(skip_receiver_profile); 1785 1786 // The method data pointer needs to be updated to reflect the new target. 1787 update_mdp_by_constant(mdp, in_bytes(VirtualCallData::virtual_call_data_size())); 1788 bind(profile_continue); 1789 } 1790 } 1791 1792 // This routine creates a state machine for updating the multi-row 1793 // type profile at a virtual call site (or other type-sensitive bytecode). 1794 // The machine visits each row (of receiver/count) until the receiver type 1795 // is found, or until it runs out of rows. At the same time, it remembers 1796 // the location of the first empty row. (An empty row records null for its 1797 // receiver, and can be allocated for a newly-observed receiver type.) 1798 // Because there are two degrees of freedom in the state, a simple linear 1799 // search will not work; it must be a decision tree. Hence this helper 1800 // function is recursive, to generate the required tree structured code. 1801 // It's the interpreter, so we are trading off code space for speed. 1802 // See below for example code. 1803 void InterpreterMacroAssembler::record_klass_in_profile_helper(Register receiver, Register mdp, 1804 Register reg2, int start_row, 1805 Label& done) { 1806 if (TypeProfileWidth == 0) { 1807 increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset())); 1808 } else { 1809 record_item_in_profile_helper(receiver, mdp, reg2, 0, done, TypeProfileWidth, 1810 &VirtualCallData::receiver_offset, &VirtualCallData::receiver_count_offset); 1811 } 1812 } 1813 1814 void InterpreterMacroAssembler::record_item_in_profile_helper(Register item, Register mdp, Register reg2, int start_row, 1815 Label& done, int total_rows, 1816 OffsetFunction item_offset_fn, 1817 OffsetFunction item_count_offset_fn) { 1818 int last_row = total_rows - 1; 1819 assert(start_row <= last_row, "must be work left to do"); 1820 // Test this row for both the item and for null. 1821 // Take any of three different outcomes: 1822 // 1. found item => increment count and goto done 1823 // 2. found null => keep looking for case 1, maybe allocate this cell 1824 // 3. found something else => keep looking for cases 1 and 2 1825 // Case 3 is handled by a recursive call. 1826 for (int row = start_row; row <= last_row; row++) { 1827 Label next_test; 1828 bool test_for_null_also = (row == start_row); 1829 1830 // See if the item is item[n]. 1831 int item_offset = in_bytes(item_offset_fn(row)); 1832 test_mdp_data_at(mdp, item_offset, item, 1833 (test_for_null_also ? reg2 : noreg), 1834 next_test); 1835 // (Reg2 now contains the item from the CallData.) 1836 1837 // The item is item[n]. Increment count[n]. 1838 int count_offset = in_bytes(item_count_offset_fn(row)); 1839 increment_mdp_data_at(mdp, count_offset); 1840 jmp(done); 1841 bind(next_test); 1842 1843 if (test_for_null_also) { 1844 // Failed the equality check on item[n]... Test for null. 1845 testptr(reg2, reg2); 1846 if (start_row == last_row) { 1847 // The only thing left to do is handle the null case. 1848 Label found_null; 1849 jccb(Assembler::zero, found_null); 1850 // Item did not match any saved item and there is no empty row for it. 1851 // Increment total counter to indicate polymorphic case. 1852 increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset())); 1853 jmp(done); 1854 bind(found_null); 1855 break; 1856 } 1857 Label found_null; 1858 // Since null is rare, make it be the branch-taken case. 1859 jcc(Assembler::zero, found_null); 1860 1861 // Put all the "Case 3" tests here. 1862 record_item_in_profile_helper(item, mdp, reg2, start_row + 1, done, total_rows, 1863 item_offset_fn, item_count_offset_fn); 1864 1865 // Found a null. Keep searching for a matching item, 1866 // but remember that this is an empty (unused) slot. 1867 bind(found_null); 1868 } 1869 } 1870 1871 // In the fall-through case, we found no matching item, but we 1872 // observed the item[start_row] is null. 1873 1874 // Fill in the item field and increment the count. 1875 int item_offset = in_bytes(item_offset_fn(start_row)); 1876 set_mdp_data_at(mdp, item_offset, item); 1877 int count_offset = in_bytes(item_count_offset_fn(start_row)); 1878 movl(reg2, DataLayout::counter_increment); 1879 set_mdp_data_at(mdp, count_offset, reg2); 1880 if (start_row > 0) { 1881 jmp(done); 1882 } 1883 } 1884 1885 // Example state machine code for three profile rows: 1886 // // main copy of decision tree, rooted at row[1] 1887 // if (row[0].rec == rec) { row[0].incr(); goto done; } 1888 // if (row[0].rec != nullptr) { 1889 // // inner copy of decision tree, rooted at row[1] 1890 // if (row[1].rec == rec) { row[1].incr(); goto done; } 1891 // if (row[1].rec != nullptr) { 1892 // // degenerate decision tree, rooted at row[2] 1893 // if (row[2].rec == rec) { row[2].incr(); goto done; } 1894 // if (row[2].rec != nullptr) { count.incr(); goto done; } // overflow 1895 // row[2].init(rec); goto done; 1896 // } else { 1897 // // remember row[1] is empty 1898 // if (row[2].rec == rec) { row[2].incr(); goto done; } 1899 // row[1].init(rec); goto done; 1900 // } 1901 // } else { 1902 // // remember row[0] is empty 1903 // if (row[1].rec == rec) { row[1].incr(); goto done; } 1904 // if (row[2].rec == rec) { row[2].incr(); goto done; } 1905 // row[0].init(rec); goto done; 1906 // } 1907 // done: 1908 1909 void InterpreterMacroAssembler::record_klass_in_profile(Register receiver, Register mdp, Register reg2) { 1910 assert(ProfileInterpreter, "must be profiling"); 1911 Label done; 1912 1913 record_klass_in_profile_helper(receiver, mdp, reg2, 0, done); 1914 1915 bind (done); 1916 } 1917 1918 void InterpreterMacroAssembler::profile_ret(Register return_bci, 1919 Register mdp) { 1920 if (ProfileInterpreter) { 1921 Label profile_continue; 1922 uint row; 1923 1924 // If no method data exists, go to profile_continue. 1925 test_method_data_pointer(mdp, profile_continue); 1926 1927 // Update the total ret count. 1928 increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset())); 1929 1930 for (row = 0; row < RetData::row_limit(); row++) { 1931 Label next_test; 1932 1933 // See if return_bci is equal to bci[n]: 1934 test_mdp_data_at(mdp, 1935 in_bytes(RetData::bci_offset(row)), 1936 return_bci, noreg, 1937 next_test); 1938 1939 // return_bci is equal to bci[n]. Increment the count. 1940 increment_mdp_data_at(mdp, in_bytes(RetData::bci_count_offset(row))); 1941 1942 // The method data pointer needs to be updated to reflect the new target. 1943 update_mdp_by_offset(mdp, 1944 in_bytes(RetData::bci_displacement_offset(row))); 1945 jmp(profile_continue); 1946 bind(next_test); 1947 } 1948 1949 update_mdp_for_ret(return_bci); 1950 1951 bind(profile_continue); 1952 } 1953 } 1954 1955 1956 void InterpreterMacroAssembler::profile_null_seen(Register mdp) { 1957 if (ProfileInterpreter) { 1958 Label profile_continue; 1959 1960 // If no method data exists, go to profile_continue. 1961 test_method_data_pointer(mdp, profile_continue); 1962 1963 set_mdp_flag_at(mdp, BitData::null_seen_byte_constant()); 1964 1965 // The method data pointer needs to be updated. 1966 int mdp_delta = in_bytes(BitData::bit_data_size()); 1967 if (TypeProfileCasts) { 1968 mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size()); 1969 } 1970 update_mdp_by_constant(mdp, mdp_delta); 1971 1972 bind(profile_continue); 1973 } 1974 } 1975 1976 1977 void InterpreterMacroAssembler::profile_typecheck(Register mdp, Register klass, Register reg2) { 1978 if (ProfileInterpreter) { 1979 Label profile_continue; 1980 1981 // If no method data exists, go to profile_continue. 1982 test_method_data_pointer(mdp, profile_continue); 1983 1984 // The method data pointer needs to be updated. 1985 int mdp_delta = in_bytes(BitData::bit_data_size()); 1986 if (TypeProfileCasts) { 1987 mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size()); 1988 1989 // Record the object type. 1990 record_klass_in_profile(klass, mdp, reg2); 1991 NOT_LP64(assert(reg2 == rdi, "we know how to fix this blown reg");) 1992 NOT_LP64(restore_locals();) // Restore EDI 1993 } 1994 update_mdp_by_constant(mdp, mdp_delta); 1995 1996 bind(profile_continue); 1997 } 1998 } 1999 2000 2001 void InterpreterMacroAssembler::profile_switch_default(Register mdp) { 2002 if (ProfileInterpreter) { 2003 Label profile_continue; 2004 2005 // If no method data exists, go to profile_continue. 2006 test_method_data_pointer(mdp, profile_continue); 2007 2008 // Update the default case count 2009 increment_mdp_data_at(mdp, 2010 in_bytes(MultiBranchData::default_count_offset())); 2011 2012 // The method data pointer needs to be updated. 2013 update_mdp_by_offset(mdp, 2014 in_bytes(MultiBranchData:: 2015 default_displacement_offset())); 2016 2017 bind(profile_continue); 2018 } 2019 } 2020 2021 2022 void InterpreterMacroAssembler::profile_switch_case(Register index, 2023 Register mdp, 2024 Register reg2) { 2025 if (ProfileInterpreter) { 2026 Label profile_continue; 2027 2028 // If no method data exists, go to profile_continue. 2029 test_method_data_pointer(mdp, profile_continue); 2030 2031 // Build the base (index * per_case_size_in_bytes()) + 2032 // case_array_offset_in_bytes() 2033 movl(reg2, in_bytes(MultiBranchData::per_case_size())); 2034 imulptr(index, reg2); // XXX l ? 2035 addptr(index, in_bytes(MultiBranchData::case_array_offset())); // XXX l ? 2036 2037 // Update the case count 2038 increment_mdp_data_at(mdp, 2039 index, 2040 in_bytes(MultiBranchData::relative_count_offset())); 2041 2042 // The method data pointer needs to be updated. 2043 update_mdp_by_offset(mdp, 2044 index, 2045 in_bytes(MultiBranchData:: 2046 relative_displacement_offset())); 2047 2048 bind(profile_continue); 2049 } 2050 } 2051 2052 template <class ArrayData> void InterpreterMacroAssembler::profile_array_type(Register mdp, 2053 Register array, 2054 Register tmp) { 2055 if (ProfileInterpreter) { 2056 Label profile_continue; 2057 2058 // If no method data exists, go to profile_continue. 2059 test_method_data_pointer(mdp, profile_continue); 2060 2061 mov(tmp, array); 2062 profile_obj_type(tmp, Address(mdp, in_bytes(ArrayData::array_offset()))); 2063 2064 Label not_flat; 2065 test_non_flat_array_oop(array, tmp, not_flat); 2066 2067 set_mdp_flag_at(mdp, ArrayData::flat_array_byte_constant()); 2068 2069 bind(not_flat); 2070 2071 Label not_null_free; 2072 test_non_null_free_array_oop(array, tmp, not_null_free); 2073 2074 set_mdp_flag_at(mdp, ArrayData::null_free_array_byte_constant()); 2075 2076 bind(not_null_free); 2077 2078 bind(profile_continue); 2079 } 2080 } 2081 2082 template void InterpreterMacroAssembler::profile_array_type<ArrayLoadData>(Register mdp, 2083 Register array, 2084 Register tmp); 2085 template void InterpreterMacroAssembler::profile_array_type<ArrayStoreData>(Register mdp, 2086 Register array, 2087 Register tmp); 2088 2089 2090 void InterpreterMacroAssembler::profile_multiple_element_types(Register mdp, Register element, Register tmp, const Register tmp2) { 2091 if (ProfileInterpreter) { 2092 Label profile_continue; 2093 2094 // If no method data exists, go to profile_continue. 2095 test_method_data_pointer(mdp, profile_continue); 2096 2097 Label done, update; 2098 testptr(element, element); 2099 jccb(Assembler::notZero, update); 2100 set_mdp_flag_at(mdp, BitData::null_seen_byte_constant()); 2101 jmp(done); 2102 2103 bind(update); 2104 load_klass(tmp, element, rscratch1); 2105 2106 // Record the object type. 2107 record_klass_in_profile(tmp, mdp, tmp2); 2108 2109 bind(done); 2110 2111 // The method data pointer needs to be updated. 2112 update_mdp_by_constant(mdp, in_bytes(ArrayStoreData::array_store_data_size())); 2113 2114 bind(profile_continue); 2115 } 2116 } 2117 2118 void InterpreterMacroAssembler::profile_element_type(Register mdp, 2119 Register element, 2120 Register tmp) { 2121 if (ProfileInterpreter) { 2122 Label profile_continue; 2123 2124 // If no method data exists, go to profile_continue. 2125 test_method_data_pointer(mdp, profile_continue); 2126 2127 mov(tmp, element); 2128 profile_obj_type(tmp, Address(mdp, in_bytes(ArrayLoadData::element_offset()))); 2129 2130 // The method data pointer needs to be updated. 2131 update_mdp_by_constant(mdp, in_bytes(ArrayLoadData::array_load_data_size())); 2132 2133 bind(profile_continue); 2134 } 2135 } 2136 2137 void InterpreterMacroAssembler::profile_acmp(Register mdp, 2138 Register left, 2139 Register right, 2140 Register tmp) { 2141 if (ProfileInterpreter) { 2142 Label profile_continue; 2143 2144 // If no method data exists, go to profile_continue. 2145 test_method_data_pointer(mdp, profile_continue); 2146 2147 mov(tmp, left); 2148 profile_obj_type(tmp, Address(mdp, in_bytes(ACmpData::left_offset()))); 2149 2150 Label left_not_inline_type; 2151 test_oop_is_not_inline_type(left, tmp, left_not_inline_type); 2152 set_mdp_flag_at(mdp, ACmpData::left_inline_type_byte_constant()); 2153 bind(left_not_inline_type); 2154 2155 mov(tmp, right); 2156 profile_obj_type(tmp, Address(mdp, in_bytes(ACmpData::right_offset()))); 2157 2158 Label right_not_inline_type; 2159 test_oop_is_not_inline_type(right, tmp, right_not_inline_type); 2160 set_mdp_flag_at(mdp, ACmpData::right_inline_type_byte_constant()); 2161 bind(right_not_inline_type); 2162 2163 bind(profile_continue); 2164 } 2165 } 2166 2167 2168 void InterpreterMacroAssembler::_interp_verify_oop(Register reg, TosState state, const char* file, int line) { 2169 if (state == atos) { 2170 MacroAssembler::_verify_oop_checked(reg, "broken oop", file, line); 2171 } 2172 } 2173 2174 void InterpreterMacroAssembler::verify_FPU(int stack_depth, TosState state) { 2175 #ifndef _LP64 2176 if ((state == ftos && UseSSE < 1) || 2177 (state == dtos && UseSSE < 2)) { 2178 MacroAssembler::verify_FPU(stack_depth); 2179 } 2180 #endif 2181 } 2182 2183 // Jump if ((*counter_addr += increment) & mask) == 0 2184 void InterpreterMacroAssembler::increment_mask_and_jump(Address counter_addr, Address mask, 2185 Register scratch, Label* where) { 2186 // This update is actually not atomic and can lose a number of updates 2187 // under heavy contention, but the alternative of using the (contended) 2188 // atomic update here penalizes profiling paths too much. 2189 movl(scratch, counter_addr); 2190 incrementl(scratch, InvocationCounter::count_increment); 2191 movl(counter_addr, scratch); 2192 andl(scratch, mask); 2193 if (where != nullptr) { 2194 jcc(Assembler::zero, *where); 2195 } 2196 } 2197 2198 void InterpreterMacroAssembler::notify_method_entry() { 2199 // Whenever JVMTI is interp_only_mode, method entry/exit events are sent to 2200 // track stack depth. If it is possible to enter interp_only_mode we add 2201 // the code to check if the event should be sent. 2202 Register rthread = LP64_ONLY(r15_thread) NOT_LP64(rcx); 2203 Register rarg = LP64_ONLY(c_rarg1) NOT_LP64(rbx); 2204 if (JvmtiExport::can_post_interpreter_events()) { 2205 Label L; 2206 NOT_LP64(get_thread(rthread);) 2207 movl(rdx, Address(rthread, JavaThread::interp_only_mode_offset())); 2208 testl(rdx, rdx); 2209 jcc(Assembler::zero, L); 2210 call_VM(noreg, CAST_FROM_FN_PTR(address, 2211 InterpreterRuntime::post_method_entry)); 2212 bind(L); 2213 } 2214 2215 if (DTraceMethodProbes) { 2216 NOT_LP64(get_thread(rthread);) 2217 get_method(rarg); 2218 call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry), 2219 rthread, rarg); 2220 } 2221 2222 // RedefineClasses() tracing support for obsolete method entry 2223 if (log_is_enabled(Trace, redefine, class, obsolete)) { 2224 NOT_LP64(get_thread(rthread);) 2225 get_method(rarg); 2226 call_VM_leaf( 2227 CAST_FROM_FN_PTR(address, SharedRuntime::rc_trace_method_entry), 2228 rthread, rarg); 2229 } 2230 } 2231 2232 2233 void InterpreterMacroAssembler::notify_method_exit( 2234 TosState state, NotifyMethodExitMode mode) { 2235 // Whenever JVMTI is interp_only_mode, method entry/exit events are sent to 2236 // track stack depth. If it is possible to enter interp_only_mode we add 2237 // the code to check if the event should be sent. 2238 Register rthread = LP64_ONLY(r15_thread) NOT_LP64(rcx); 2239 Register rarg = LP64_ONLY(c_rarg1) NOT_LP64(rbx); 2240 if (mode == NotifyJVMTI && JvmtiExport::can_post_interpreter_events()) { 2241 Label L; 2242 // Note: frame::interpreter_frame_result has a dependency on how the 2243 // method result is saved across the call to post_method_exit. If this 2244 // is changed then the interpreter_frame_result implementation will 2245 // need to be updated too. 2246 2247 // template interpreter will leave the result on the top of the stack. 2248 push(state); 2249 NOT_LP64(get_thread(rthread);) 2250 movl(rdx, Address(rthread, JavaThread::interp_only_mode_offset())); 2251 testl(rdx, rdx); 2252 jcc(Assembler::zero, L); 2253 call_VM(noreg, 2254 CAST_FROM_FN_PTR(address, InterpreterRuntime::post_method_exit)); 2255 bind(L); 2256 pop(state); 2257 } 2258 2259 if (DTraceMethodProbes) { 2260 push(state); 2261 NOT_LP64(get_thread(rthread);) 2262 get_method(rarg); 2263 call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit), 2264 rthread, rarg); 2265 pop(state); 2266 } 2267 } 2268 2269 void InterpreterMacroAssembler::load_resolved_indy_entry(Register cache, Register index) { 2270 // Get index out of bytecode pointer 2271 get_cache_index_at_bcp(index, 1, sizeof(u4)); 2272 // Get address of invokedynamic array 2273 movptr(cache, Address(rbp, frame::interpreter_frame_cache_offset * wordSize)); 2274 movptr(cache, Address(cache, in_bytes(ConstantPoolCache::invokedynamic_entries_offset()))); 2275 if (is_power_of_2(sizeof(ResolvedIndyEntry))) { 2276 shll(index, log2i_exact(sizeof(ResolvedIndyEntry))); // Scale index by power of 2 2277 } else { 2278 imull(index, index, sizeof(ResolvedIndyEntry)); // Scale the index to be the entry index * sizeof(ResolvedIndyEntry) 2279 } 2280 lea(cache, Address(cache, index, Address::times_1, Array<ResolvedIndyEntry>::base_offset_in_bytes())); 2281 } 2282 2283 void InterpreterMacroAssembler::load_field_entry(Register cache, Register index, int bcp_offset) { 2284 // Get index out of bytecode pointer 2285 movptr(cache, Address(rbp, frame::interpreter_frame_cache_offset * wordSize)); 2286 get_cache_index_at_bcp(index, bcp_offset, sizeof(u2)); 2287 2288 movptr(cache, Address(cache, ConstantPoolCache::field_entries_offset())); 2289 // Take shortcut if the size is a power of 2 2290 if (is_power_of_2(sizeof(ResolvedFieldEntry))) { 2291 shll(index, log2i_exact(sizeof(ResolvedFieldEntry))); // Scale index by power of 2 2292 } else { 2293 imull(index, index, sizeof(ResolvedFieldEntry)); // Scale the index to be the entry index * sizeof(ResolvedFieldEntry) 2294 } 2295 lea(cache, Address(cache, index, Address::times_1, Array<ResolvedFieldEntry>::base_offset_in_bytes())); 2296 } 2297 2298 void InterpreterMacroAssembler::load_method_entry(Register cache, Register index, int bcp_offset) { 2299 // Get index out of bytecode pointer 2300 movptr(cache, Address(rbp, frame::interpreter_frame_cache_offset * wordSize)); 2301 get_cache_index_at_bcp(index, bcp_offset, sizeof(u2)); 2302 2303 movptr(cache, Address(cache, ConstantPoolCache::method_entries_offset())); 2304 imull(index, index, sizeof(ResolvedMethodEntry)); // Scale the index to be the entry index * sizeof(ResolvedMethodEntry) 2305 lea(cache, Address(cache, index, Address::times_1, Array<ResolvedMethodEntry>::base_offset_in_bytes())); 2306 }