1 /* 2 * Copyright (c) 1997, 2023, 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 // Check if the secondary index definition is still ~x, otherwise 469 // we have to change the following assembler code to calculate the 470 // plain index. 471 assert(ConstantPool::decode_invokedynamic_index(~123) == 123, "else change next line"); 472 notl(index); // convert to plain index 473 } else if (index_size == sizeof(u1)) { 474 load_unsigned_byte(index, Address(_bcp_register, bcp_offset)); 475 } else { 476 ShouldNotReachHere(); 477 } 478 } 479 480 // Load object from cpool->resolved_references(index) 481 void InterpreterMacroAssembler::load_resolved_reference_at_index(Register result, 482 Register index, 483 Register tmp) { 484 assert_different_registers(result, index); 485 486 get_constant_pool(result); 487 // load pointer for resolved_references[] objArray 488 movptr(result, Address(result, ConstantPool::cache_offset())); 489 movptr(result, Address(result, ConstantPoolCache::resolved_references_offset())); 490 resolve_oop_handle(result, tmp); 491 load_heap_oop(result, Address(result, index, 492 UseCompressedOops ? Address::times_4 : Address::times_ptr, 493 arrayOopDesc::base_offset_in_bytes(T_OBJECT)), tmp); 494 } 495 496 // load cpool->resolved_klass_at(index) 497 void InterpreterMacroAssembler::load_resolved_klass_at_index(Register klass, 498 Register cpool, 499 Register index) { 500 assert_different_registers(cpool, index); 501 502 movw(index, Address(cpool, index, Address::times_ptr, sizeof(ConstantPool))); 503 Register resolved_klasses = cpool; 504 movptr(resolved_klasses, Address(cpool, ConstantPool::resolved_klasses_offset())); 505 movptr(klass, Address(resolved_klasses, index, Address::times_ptr, Array<Klass*>::base_offset_in_bytes())); 506 } 507 508 // Generate a subtype check: branch to ok_is_subtype if sub_klass is a 509 // subtype of super_klass. 510 // 511 // Args: 512 // rax: superklass 513 // Rsub_klass: subklass 514 // 515 // Kills: 516 // rcx, rdi 517 void InterpreterMacroAssembler::gen_subtype_check(Register Rsub_klass, 518 Label& ok_is_subtype, 519 bool profile) { 520 assert(Rsub_klass != rax, "rax holds superklass"); 521 LP64_ONLY(assert(Rsub_klass != r14, "r14 holds locals");) 522 LP64_ONLY(assert(Rsub_klass != r13, "r13 holds bcp");) 523 assert(Rsub_klass != rcx, "rcx holds 2ndary super array length"); 524 assert(Rsub_klass != rdi, "rdi holds 2ndary super array scan ptr"); 525 526 // Profile the not-null value's klass. 527 if (profile) { 528 profile_typecheck(rcx, Rsub_klass, rdi); // blows rcx, reloads rdi 529 } 530 531 // Do the check. 532 check_klass_subtype(Rsub_klass, rax, rcx, ok_is_subtype); // blows rcx 533 } 534 535 536 #ifndef _LP64 537 void InterpreterMacroAssembler::f2ieee() { 538 if (IEEEPrecision) { 539 fstp_s(Address(rsp, 0)); 540 fld_s(Address(rsp, 0)); 541 } 542 } 543 544 545 void InterpreterMacroAssembler::d2ieee() { 546 if (IEEEPrecision) { 547 fstp_d(Address(rsp, 0)); 548 fld_d(Address(rsp, 0)); 549 } 550 } 551 #endif // _LP64 552 553 // Java Expression Stack 554 555 void InterpreterMacroAssembler::pop_ptr(Register r) { 556 pop(r); 557 } 558 559 void InterpreterMacroAssembler::push_ptr(Register r) { 560 push(r); 561 } 562 563 void InterpreterMacroAssembler::push_i(Register r) { 564 push(r); 565 } 566 567 void InterpreterMacroAssembler::push_i_or_ptr(Register r) { 568 push(r); 569 } 570 571 void InterpreterMacroAssembler::push_f(XMMRegister r) { 572 subptr(rsp, wordSize); 573 movflt(Address(rsp, 0), r); 574 } 575 576 void InterpreterMacroAssembler::pop_f(XMMRegister r) { 577 movflt(r, Address(rsp, 0)); 578 addptr(rsp, wordSize); 579 } 580 581 void InterpreterMacroAssembler::push_d(XMMRegister r) { 582 subptr(rsp, 2 * wordSize); 583 movdbl(Address(rsp, 0), r); 584 } 585 586 void InterpreterMacroAssembler::pop_d(XMMRegister r) { 587 movdbl(r, Address(rsp, 0)); 588 addptr(rsp, 2 * Interpreter::stackElementSize); 589 } 590 591 #ifdef _LP64 592 void InterpreterMacroAssembler::pop_i(Register r) { 593 // XXX can't use pop currently, upper half non clean 594 movl(r, Address(rsp, 0)); 595 addptr(rsp, wordSize); 596 } 597 598 void InterpreterMacroAssembler::pop_l(Register r) { 599 movq(r, Address(rsp, 0)); 600 addptr(rsp, 2 * Interpreter::stackElementSize); 601 } 602 603 void InterpreterMacroAssembler::push_l(Register r) { 604 subptr(rsp, 2 * wordSize); 605 movptr(Address(rsp, Interpreter::expr_offset_in_bytes(0)), r ); 606 movptr(Address(rsp, Interpreter::expr_offset_in_bytes(1)), NULL_WORD ); 607 } 608 609 void InterpreterMacroAssembler::pop(TosState state) { 610 switch (state) { 611 case atos: pop_ptr(); break; 612 case btos: 613 case ztos: 614 case ctos: 615 case stos: 616 case itos: pop_i(); break; 617 case ltos: pop_l(); break; 618 case ftos: pop_f(xmm0); break; 619 case dtos: pop_d(xmm0); break; 620 case vtos: /* nothing to do */ break; 621 default: ShouldNotReachHere(); 622 } 623 interp_verify_oop(rax, state); 624 } 625 626 void InterpreterMacroAssembler::push(TosState state) { 627 interp_verify_oop(rax, state); 628 switch (state) { 629 case atos: push_ptr(); break; 630 case btos: 631 case ztos: 632 case ctos: 633 case stos: 634 case itos: push_i(); break; 635 case ltos: push_l(); break; 636 case ftos: push_f(xmm0); break; 637 case dtos: push_d(xmm0); break; 638 case vtos: /* nothing to do */ break; 639 default : ShouldNotReachHere(); 640 } 641 } 642 #else 643 void InterpreterMacroAssembler::pop_i(Register r) { 644 pop(r); 645 } 646 647 void InterpreterMacroAssembler::pop_l(Register lo, Register hi) { 648 pop(lo); 649 pop(hi); 650 } 651 652 void InterpreterMacroAssembler::pop_f() { 653 fld_s(Address(rsp, 0)); 654 addptr(rsp, 1 * wordSize); 655 } 656 657 void InterpreterMacroAssembler::pop_d() { 658 fld_d(Address(rsp, 0)); 659 addptr(rsp, 2 * wordSize); 660 } 661 662 663 void InterpreterMacroAssembler::pop(TosState state) { 664 switch (state) { 665 case atos: pop_ptr(rax); break; 666 case btos: // fall through 667 case ztos: // fall through 668 case ctos: // fall through 669 case stos: // fall through 670 case itos: pop_i(rax); break; 671 case ltos: pop_l(rax, rdx); break; 672 case ftos: 673 if (UseSSE >= 1) { 674 pop_f(xmm0); 675 } else { 676 pop_f(); 677 } 678 break; 679 case dtos: 680 if (UseSSE >= 2) { 681 pop_d(xmm0); 682 } else { 683 pop_d(); 684 } 685 break; 686 case vtos: /* nothing to do */ break; 687 default : ShouldNotReachHere(); 688 } 689 interp_verify_oop(rax, state); 690 } 691 692 693 void InterpreterMacroAssembler::push_l(Register lo, Register hi) { 694 push(hi); 695 push(lo); 696 } 697 698 void InterpreterMacroAssembler::push_f() { 699 // Do not schedule for no AGI! Never write beyond rsp! 700 subptr(rsp, 1 * wordSize); 701 fstp_s(Address(rsp, 0)); 702 } 703 704 void InterpreterMacroAssembler::push_d() { 705 // Do not schedule for no AGI! Never write beyond rsp! 706 subptr(rsp, 2 * wordSize); 707 fstp_d(Address(rsp, 0)); 708 } 709 710 711 void InterpreterMacroAssembler::push(TosState state) { 712 interp_verify_oop(rax, state); 713 switch (state) { 714 case atos: push_ptr(rax); break; 715 case btos: // fall through 716 case ztos: // fall through 717 case ctos: // fall through 718 case stos: // fall through 719 case itos: push_i(rax); break; 720 case ltos: push_l(rax, rdx); break; 721 case ftos: 722 if (UseSSE >= 1) { 723 push_f(xmm0); 724 } else { 725 push_f(); 726 } 727 break; 728 case dtos: 729 if (UseSSE >= 2) { 730 push_d(xmm0); 731 } else { 732 push_d(); 733 } 734 break; 735 case vtos: /* nothing to do */ break; 736 default : ShouldNotReachHere(); 737 } 738 } 739 #endif // _LP64 740 741 742 // Helpers for swap and dup 743 void InterpreterMacroAssembler::load_ptr(int n, Register val) { 744 movptr(val, Address(rsp, Interpreter::expr_offset_in_bytes(n))); 745 } 746 747 void InterpreterMacroAssembler::store_ptr(int n, Register val) { 748 movptr(Address(rsp, Interpreter::expr_offset_in_bytes(n)), val); 749 } 750 751 752 void InterpreterMacroAssembler::prepare_to_jump_from_interpreted() { 753 // set sender sp 754 lea(_bcp_register, Address(rsp, wordSize)); 755 // record last_sp 756 mov(rcx, _bcp_register); 757 subptr(rcx, rbp); 758 sarptr(rcx, LogBytesPerWord); 759 movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), rcx); 760 } 761 762 763 // Jump to from_interpreted entry of a call unless single stepping is possible 764 // in this thread in which case we must call the i2i entry 765 void InterpreterMacroAssembler::jump_from_interpreted(Register method, Register temp) { 766 prepare_to_jump_from_interpreted(); 767 768 if (JvmtiExport::can_post_interpreter_events()) { 769 Label run_compiled_code; 770 // JVMTI events, such as single-stepping, are implemented partly by avoiding running 771 // compiled code in threads for which the event is enabled. Check here for 772 // interp_only_mode if these events CAN be enabled. 773 // interp_only is an int, on little endian it is sufficient to test the byte only 774 // Is a cmpl faster? 775 LP64_ONLY(temp = r15_thread;) 776 NOT_LP64(get_thread(temp);) 777 cmpb(Address(temp, JavaThread::interp_only_mode_offset()), 0); 778 jccb(Assembler::zero, run_compiled_code); 779 jmp(Address(method, Method::interpreter_entry_offset())); 780 bind(run_compiled_code); 781 } 782 783 jmp(Address(method, Method::from_interpreted_offset())); 784 } 785 786 // The following two routines provide a hook so that an implementation 787 // can schedule the dispatch in two parts. x86 does not do this. 788 void InterpreterMacroAssembler::dispatch_prolog(TosState state, int step) { 789 // Nothing x86 specific to be done here 790 } 791 792 void InterpreterMacroAssembler::dispatch_epilog(TosState state, int step) { 793 dispatch_next(state, step); 794 } 795 796 void InterpreterMacroAssembler::dispatch_base(TosState state, 797 address* table, 798 bool verifyoop, 799 bool generate_poll) { 800 verify_FPU(1, state); 801 if (VerifyActivationFrameSize) { 802 Label L; 803 mov(rcx, rbp); 804 subptr(rcx, rsp); 805 int32_t min_frame_size = 806 (frame::link_offset - frame::interpreter_frame_initial_sp_offset) * 807 wordSize; 808 cmpptr(rcx, min_frame_size); 809 jcc(Assembler::greaterEqual, L); 810 stop("broken stack frame"); 811 bind(L); 812 } 813 if (verifyoop) { 814 interp_verify_oop(rax, state); 815 } 816 817 address* const safepoint_table = Interpreter::safept_table(state); 818 #ifdef _LP64 819 Label no_safepoint, dispatch; 820 if (table != safepoint_table && generate_poll) { 821 NOT_PRODUCT(block_comment("Thread-local Safepoint poll")); 822 testb(Address(r15_thread, JavaThread::polling_word_offset()), SafepointMechanism::poll_bit()); 823 824 jccb(Assembler::zero, no_safepoint); 825 lea(rscratch1, ExternalAddress((address)safepoint_table)); 826 jmpb(dispatch); 827 } 828 829 bind(no_safepoint); 830 lea(rscratch1, ExternalAddress((address)table)); 831 bind(dispatch); 832 jmp(Address(rscratch1, rbx, Address::times_8)); 833 834 #else 835 Address index(noreg, rbx, Address::times_ptr); 836 if (table != safepoint_table && generate_poll) { 837 NOT_PRODUCT(block_comment("Thread-local Safepoint poll")); 838 Label no_safepoint; 839 const Register thread = rcx; 840 get_thread(thread); 841 testb(Address(thread, JavaThread::polling_word_offset()), SafepointMechanism::poll_bit()); 842 843 jccb(Assembler::zero, no_safepoint); 844 ArrayAddress dispatch_addr(ExternalAddress((address)safepoint_table), index); 845 jump(dispatch_addr, noreg); 846 bind(no_safepoint); 847 } 848 849 { 850 ArrayAddress dispatch_addr(ExternalAddress((address)table), index); 851 jump(dispatch_addr, noreg); 852 } 853 #endif // _LP64 854 } 855 856 void InterpreterMacroAssembler::dispatch_only(TosState state, bool generate_poll) { 857 dispatch_base(state, Interpreter::dispatch_table(state), true, generate_poll); 858 } 859 860 void InterpreterMacroAssembler::dispatch_only_normal(TosState state) { 861 dispatch_base(state, Interpreter::normal_table(state)); 862 } 863 864 void InterpreterMacroAssembler::dispatch_only_noverify(TosState state) { 865 dispatch_base(state, Interpreter::normal_table(state), false); 866 } 867 868 869 void InterpreterMacroAssembler::dispatch_next(TosState state, int step, bool generate_poll) { 870 // load next bytecode (load before advancing _bcp_register to prevent AGI) 871 load_unsigned_byte(rbx, Address(_bcp_register, step)); 872 // advance _bcp_register 873 increment(_bcp_register, step); 874 dispatch_base(state, Interpreter::dispatch_table(state), true, generate_poll); 875 } 876 877 void InterpreterMacroAssembler::dispatch_via(TosState state, address* table) { 878 // load current bytecode 879 load_unsigned_byte(rbx, Address(_bcp_register, 0)); 880 dispatch_base(state, table); 881 } 882 883 void InterpreterMacroAssembler::narrow(Register result) { 884 885 // Get method->_constMethod->_result_type 886 movptr(rcx, Address(rbp, frame::interpreter_frame_method_offset * wordSize)); 887 movptr(rcx, Address(rcx, Method::const_offset())); 888 load_unsigned_byte(rcx, Address(rcx, ConstMethod::result_type_offset())); 889 890 Label done, notBool, notByte, notChar; 891 892 // common case first 893 cmpl(rcx, T_INT); 894 jcc(Assembler::equal, done); 895 896 // mask integer result to narrower return type. 897 cmpl(rcx, T_BOOLEAN); 898 jcc(Assembler::notEqual, notBool); 899 andl(result, 0x1); 900 jmp(done); 901 902 bind(notBool); 903 cmpl(rcx, T_BYTE); 904 jcc(Assembler::notEqual, notByte); 905 LP64_ONLY(movsbl(result, result);) 906 NOT_LP64(shll(result, 24);) // truncate upper 24 bits 907 NOT_LP64(sarl(result, 24);) // and sign-extend byte 908 jmp(done); 909 910 bind(notByte); 911 cmpl(rcx, T_CHAR); 912 jcc(Assembler::notEqual, notChar); 913 LP64_ONLY(movzwl(result, result);) 914 NOT_LP64(andl(result, 0xFFFF);) // truncate upper 16 bits 915 jmp(done); 916 917 bind(notChar); 918 // cmpl(rcx, T_SHORT); // all that's left 919 // jcc(Assembler::notEqual, done); 920 LP64_ONLY(movswl(result, result);) 921 NOT_LP64(shll(result, 16);) // truncate upper 16 bits 922 NOT_LP64(sarl(result, 16);) // and sign-extend short 923 924 // Nothing to do for T_INT 925 bind(done); 926 } 927 928 // remove activation 929 // 930 // Apply stack watermark barrier. 931 // Unlock the receiver if this is a synchronized method. 932 // Unlock any Java monitors from synchronized blocks. 933 // Remove the activation from the stack. 934 // 935 // If there are locked Java monitors 936 // If throw_monitor_exception 937 // throws IllegalMonitorStateException 938 // Else if install_monitor_exception 939 // installs IllegalMonitorStateException 940 // Else 941 // no error processing 942 void InterpreterMacroAssembler::remove_activation( 943 TosState state, 944 Register ret_addr, 945 bool throw_monitor_exception, 946 bool install_monitor_exception, 947 bool notify_jvmdi) { 948 // Note: Registers rdx xmm0 may be in use for the 949 // result check if synchronized method 950 Label unlocked, unlock, no_unlock; 951 952 const Register rthread = LP64_ONLY(r15_thread) NOT_LP64(rcx); 953 const Register robj = LP64_ONLY(c_rarg1) NOT_LP64(rdx); 954 const Register rmon = LP64_ONLY(c_rarg1) NOT_LP64(rcx); 955 // monitor pointers need different register 956 // because rdx may have the result in it 957 NOT_LP64(get_thread(rthread);) 958 959 // The below poll is for the stack watermark barrier. It allows fixing up frames lazily, 960 // that would normally not be safe to use. Such bad returns into unsafe territory of 961 // the stack, will call InterpreterRuntime::at_unwind. 962 Label slow_path; 963 Label fast_path; 964 safepoint_poll(slow_path, rthread, true /* at_return */, false /* in_nmethod */); 965 jmp(fast_path); 966 bind(slow_path); 967 push(state); 968 set_last_Java_frame(rthread, noreg, rbp, (address)pc(), rscratch1); 969 super_call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::at_unwind), rthread); 970 NOT_LP64(get_thread(rthread);) // call_VM clobbered it, restore 971 reset_last_Java_frame(rthread, true); 972 pop(state); 973 bind(fast_path); 974 975 // get the value of _do_not_unlock_if_synchronized into rdx 976 const Address do_not_unlock_if_synchronized(rthread, 977 in_bytes(JavaThread::do_not_unlock_if_synchronized_offset())); 978 movbool(rbx, do_not_unlock_if_synchronized); 979 movbool(do_not_unlock_if_synchronized, false); // reset the flag 980 981 // get method access flags 982 movptr(rcx, Address(rbp, frame::interpreter_frame_method_offset * wordSize)); 983 movl(rcx, Address(rcx, Method::access_flags_offset())); 984 testl(rcx, JVM_ACC_SYNCHRONIZED); 985 jcc(Assembler::zero, unlocked); 986 987 // Don't unlock anything if the _do_not_unlock_if_synchronized flag 988 // is set. 989 testbool(rbx); 990 jcc(Assembler::notZero, no_unlock); 991 992 // unlock monitor 993 push(state); // save result 994 995 // BasicObjectLock will be first in list, since this is a 996 // synchronized method. However, need to check that the object has 997 // not been unlocked by an explicit monitorexit bytecode. 998 const Address monitor(rbp, frame::interpreter_frame_initial_sp_offset * 999 wordSize - (int) sizeof(BasicObjectLock)); 1000 // We use c_rarg1/rdx so that if we go slow path it will be the correct 1001 // register for unlock_object to pass to VM directly 1002 lea(robj, monitor); // address of first monitor 1003 1004 movptr(rax, Address(robj, BasicObjectLock::obj_offset())); 1005 testptr(rax, rax); 1006 jcc(Assembler::notZero, unlock); 1007 1008 pop(state); 1009 if (throw_monitor_exception) { 1010 // Entry already unlocked, need to throw exception 1011 NOT_LP64(empty_FPU_stack();) // remove possible return value from FPU-stack, otherwise stack could overflow 1012 call_VM(noreg, CAST_FROM_FN_PTR(address, 1013 InterpreterRuntime::throw_illegal_monitor_state_exception)); 1014 should_not_reach_here(); 1015 } else { 1016 // Monitor already unlocked during a stack unroll. If requested, 1017 // install an illegal_monitor_state_exception. Continue with 1018 // stack unrolling. 1019 if (install_monitor_exception) { 1020 NOT_LP64(empty_FPU_stack();) 1021 call_VM(noreg, CAST_FROM_FN_PTR(address, 1022 InterpreterRuntime::new_illegal_monitor_state_exception)); 1023 } 1024 jmp(unlocked); 1025 } 1026 1027 bind(unlock); 1028 unlock_object(robj); 1029 pop(state); 1030 1031 // Check that for block-structured locking (i.e., that all locked 1032 // objects has been unlocked) 1033 bind(unlocked); 1034 1035 // rax, rdx: Might contain return value 1036 1037 // Check that all monitors are unlocked 1038 { 1039 Label loop, exception, entry, restart; 1040 const int entry_size = frame::interpreter_frame_monitor_size_in_bytes(); 1041 const Address monitor_block_top( 1042 rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize); 1043 const Address monitor_block_bot( 1044 rbp, frame::interpreter_frame_initial_sp_offset * wordSize); 1045 1046 bind(restart); 1047 // We use c_rarg1 so that if we go slow path it will be the correct 1048 // register for unlock_object to pass to VM directly 1049 movptr(rmon, monitor_block_top); // derelativize pointer 1050 lea(rmon, Address(rbp, rmon, Address::times_ptr)); 1051 // c_rarg1 points to current entry, starting with top-most entry 1052 1053 lea(rbx, monitor_block_bot); // points to word before bottom of 1054 // monitor block 1055 jmp(entry); 1056 1057 // Entry already locked, need to throw exception 1058 bind(exception); 1059 1060 if (throw_monitor_exception) { 1061 // Throw exception 1062 NOT_LP64(empty_FPU_stack();) 1063 MacroAssembler::call_VM(noreg, 1064 CAST_FROM_FN_PTR(address, InterpreterRuntime:: 1065 throw_illegal_monitor_state_exception)); 1066 should_not_reach_here(); 1067 } else { 1068 // Stack unrolling. Unlock object and install illegal_monitor_exception. 1069 // Unlock does not block, so don't have to worry about the frame. 1070 // We don't have to preserve c_rarg1 since we are going to throw an exception. 1071 1072 push(state); 1073 mov(robj, rmon); // nop if robj and rmon are the same 1074 unlock_object(robj); 1075 pop(state); 1076 1077 if (install_monitor_exception) { 1078 NOT_LP64(empty_FPU_stack();) 1079 call_VM(noreg, CAST_FROM_FN_PTR(address, 1080 InterpreterRuntime:: 1081 new_illegal_monitor_state_exception)); 1082 } 1083 1084 jmp(restart); 1085 } 1086 1087 bind(loop); 1088 // check if current entry is used 1089 cmpptr(Address(rmon, BasicObjectLock::obj_offset()), NULL_WORD); 1090 jcc(Assembler::notEqual, exception); 1091 1092 addptr(rmon, entry_size); // otherwise advance to next entry 1093 bind(entry); 1094 cmpptr(rmon, rbx); // check if bottom reached 1095 jcc(Assembler::notEqual, loop); // if not at bottom then check this entry 1096 } 1097 1098 bind(no_unlock); 1099 1100 // jvmti support 1101 if (notify_jvmdi) { 1102 notify_method_exit(state, NotifyJVMTI); // preserve TOSCA 1103 } else { 1104 notify_method_exit(state, SkipNotifyJVMTI); // preserve TOSCA 1105 } 1106 1107 if (StackReservedPages > 0) { 1108 movptr(rbx, 1109 Address(rbp, frame::interpreter_frame_sender_sp_offset * wordSize)); 1110 // testing if reserved zone needs to be re-enabled 1111 Register rthread = LP64_ONLY(r15_thread) NOT_LP64(rcx); 1112 Label no_reserved_zone_enabling; 1113 1114 NOT_LP64(get_thread(rthread);) 1115 1116 // check if already enabled - if so no re-enabling needed 1117 assert(sizeof(StackOverflow::StackGuardState) == 4, "unexpected size"); 1118 cmpl(Address(rthread, JavaThread::stack_guard_state_offset()), StackOverflow::stack_guard_enabled); 1119 jcc(Assembler::equal, no_reserved_zone_enabling); 1120 1121 cmpptr(rbx, Address(rthread, JavaThread::reserved_stack_activation_offset())); 1122 jcc(Assembler::lessEqual, no_reserved_zone_enabling); 1123 1124 call_VM_leaf( 1125 CAST_FROM_FN_PTR(address, SharedRuntime::enable_stack_reserved_zone), rthread); 1126 call_VM(noreg, CAST_FROM_FN_PTR(address, 1127 InterpreterRuntime::throw_delayed_StackOverflowError)); 1128 should_not_reach_here(); 1129 1130 bind(no_reserved_zone_enabling); 1131 } 1132 1133 // remove activation 1134 // get sender sp 1135 movptr(rbx, 1136 Address(rbp, frame::interpreter_frame_sender_sp_offset * wordSize)); 1137 1138 if (state == atos && InlineTypeReturnedAsFields) { 1139 // Check if we are returning an non-null inline type and load its fields into registers 1140 Label skip; 1141 test_oop_is_not_inline_type(rax, rscratch1, skip); 1142 1143 #ifndef _LP64 1144 super_call_VM_leaf(StubRoutines::load_inline_type_fields_in_regs()); 1145 #else 1146 // Load fields from a buffered value with an inline class specific handler 1147 load_klass(rdi, rax, rscratch1); 1148 movptr(rdi, Address(rdi, InstanceKlass::adr_inlineklass_fixed_block_offset())); 1149 movptr(rdi, Address(rdi, InlineKlass::unpack_handler_offset())); 1150 // Unpack handler can be null if inline type is not scalarizable in returns 1151 testptr(rdi, rdi); 1152 jcc(Assembler::zero, skip); 1153 call(rdi); 1154 #endif 1155 #ifdef ASSERT 1156 // TODO 8284443 Enable 1157 if (StressCallingConvention && false) { 1158 Label skip_stress; 1159 movptr(rscratch1, Address(rbp, frame::interpreter_frame_method_offset * wordSize)); 1160 movl(rscratch1, Address(rscratch1, Method::flags_offset())); 1161 testl(rcx, ConstMethodFlags::has_scalarized_return_flag()); 1162 jcc(Assembler::zero, skip_stress); 1163 load_klass(rax, rax, rscratch1); 1164 orptr(rax, 1); 1165 bind(skip_stress); 1166 } 1167 #endif 1168 // call above kills the value in rbx. Reload it. 1169 movptr(rbx, Address(rbp, frame::interpreter_frame_sender_sp_offset * wordSize)); 1170 bind(skip); 1171 } 1172 leave(); // remove frame anchor 1173 pop(ret_addr); // get return address 1174 mov(rsp, rbx); // set sp to sender sp 1175 pop_cont_fastpath(); 1176 } 1177 1178 void InterpreterMacroAssembler::get_method_counters(Register method, 1179 Register mcs, Label& skip) { 1180 Label has_counters; 1181 movptr(mcs, Address(method, Method::method_counters_offset())); 1182 testptr(mcs, mcs); 1183 jcc(Assembler::notZero, has_counters); 1184 call_VM(noreg, CAST_FROM_FN_PTR(address, 1185 InterpreterRuntime::build_method_counters), method); 1186 movptr(mcs, Address(method,Method::method_counters_offset())); 1187 testptr(mcs, mcs); 1188 jcc(Assembler::zero, skip); // No MethodCounters allocated, OutOfMemory 1189 bind(has_counters); 1190 } 1191 1192 void InterpreterMacroAssembler::allocate_instance(Register klass, Register new_obj, 1193 Register t1, Register t2, 1194 bool clear_fields, Label& alloc_failed) { 1195 MacroAssembler::allocate_instance(klass, new_obj, t1, t2, clear_fields, alloc_failed); 1196 { 1197 SkipIfEqual skip_if(this, &DTraceAllocProbes, 0, rscratch1); 1198 // Trigger dtrace event for fastpath 1199 push(atos); 1200 call_VM_leaf(CAST_FROM_FN_PTR(address, static_cast<int (*)(oopDesc*)>(SharedRuntime::dtrace_object_alloc)), new_obj); 1201 pop(atos); 1202 } 1203 } 1204 1205 1206 void InterpreterMacroAssembler::read_flat_field(Register holder_klass, 1207 Register field_index, Register field_offset, 1208 Register obj) { 1209 Label alloc_failed, empty_value, done; 1210 const Register src = field_offset; 1211 const Register alloc_temp = LP64_ONLY(rscratch1) NOT_LP64(rsi); 1212 const Register dst_temp = LP64_ONLY(rscratch2) NOT_LP64(rdi); 1213 assert_different_registers(obj, holder_klass, field_index, field_offset, dst_temp); 1214 1215 // Grap the inline field klass 1216 push(holder_klass); 1217 const Register field_klass = holder_klass; 1218 get_inline_type_field_klass(holder_klass, field_index, field_klass); 1219 1220 //check for empty value klass 1221 test_klass_is_empty_inline_type(field_klass, dst_temp, empty_value); 1222 1223 // allocate buffer 1224 push(obj); // save holder 1225 allocate_instance(field_klass, obj, alloc_temp, dst_temp, false, alloc_failed); 1226 1227 // Have an oop instance buffer, copy into it 1228 data_for_oop(obj, dst_temp, field_klass); 1229 pop(alloc_temp); // restore holder 1230 lea(src, Address(alloc_temp, field_offset)); 1231 // call_VM_leaf, clobbers a few regs, save restore new obj 1232 push(obj); 1233 access_value_copy(IS_DEST_UNINITIALIZED, src, dst_temp, field_klass); 1234 pop(obj); 1235 pop(holder_klass); 1236 jmp(done); 1237 1238 bind(empty_value); 1239 get_empty_inline_type_oop(field_klass, dst_temp, obj); 1240 pop(holder_klass); 1241 jmp(done); 1242 1243 bind(alloc_failed); 1244 pop(obj); 1245 pop(holder_klass); 1246 call_VM(obj, CAST_FROM_FN_PTR(address, InterpreterRuntime::read_flat_field), 1247 obj, field_index, holder_klass); 1248 1249 bind(done); 1250 } 1251 1252 void InterpreterMacroAssembler::read_flat_element(Register array, Register index, 1253 Register t1, Register t2, 1254 Register obj) { 1255 assert_different_registers(array, index, t1, t2); 1256 Label alloc_failed, empty_value, done; 1257 const Register array_klass = t2; 1258 const Register elem_klass = t1; 1259 const Register alloc_temp = LP64_ONLY(rscratch1) NOT_LP64(rsi); 1260 const Register dst_temp = LP64_ONLY(rscratch2) NOT_LP64(rdi); 1261 1262 // load in array->klass()->element_klass() 1263 Register tmp_load_klass = LP64_ONLY(rscratch1) NOT_LP64(noreg); 1264 load_klass(array_klass, array, tmp_load_klass); 1265 movptr(elem_klass, Address(array_klass, ArrayKlass::element_klass_offset())); 1266 1267 //check for empty value klass 1268 test_klass_is_empty_inline_type(elem_klass, dst_temp, empty_value); 1269 1270 // calc source into "array_klass" and free up some regs 1271 const Register src = array_klass; 1272 push(index); // preserve index reg in case alloc_failed 1273 data_for_value_array_index(array, array_klass, index, src); 1274 1275 allocate_instance(elem_klass, obj, alloc_temp, dst_temp, false, alloc_failed); 1276 // Have an oop instance buffer, copy into it 1277 store_ptr(0, obj); // preserve obj (overwrite index, no longer needed) 1278 data_for_oop(obj, dst_temp, elem_klass); 1279 access_value_copy(IS_DEST_UNINITIALIZED, src, dst_temp, elem_klass); 1280 pop(obj); 1281 jmp(done); 1282 1283 bind(empty_value); 1284 get_empty_inline_type_oop(elem_klass, dst_temp, obj); 1285 jmp(done); 1286 1287 bind(alloc_failed); 1288 pop(index); 1289 if (array == c_rarg2) { 1290 mov(elem_klass, array); 1291 array = elem_klass; 1292 } 1293 call_VM(obj, CAST_FROM_FN_PTR(address, InterpreterRuntime::value_array_load), array, index); 1294 1295 bind(done); 1296 } 1297 1298 1299 // Lock object 1300 // 1301 // Args: 1302 // rdx, c_rarg1: BasicObjectLock to be used for locking 1303 // 1304 // Kills: 1305 // rax, rbx 1306 void InterpreterMacroAssembler::lock_object(Register lock_reg) { 1307 assert(lock_reg == LP64_ONLY(c_rarg1) NOT_LP64(rdx), 1308 "The argument is only for looks. It must be c_rarg1"); 1309 1310 if (LockingMode == LM_MONITOR) { 1311 call_VM(noreg, 1312 CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter), 1313 lock_reg); 1314 } else { 1315 Label count_locking, done, slow_case; 1316 1317 const Register swap_reg = rax; // Must use rax for cmpxchg instruction 1318 const Register tmp_reg = rbx; 1319 const Register obj_reg = LP64_ONLY(c_rarg3) NOT_LP64(rcx); // Will contain the oop 1320 const Register rklass_decode_tmp = rscratch1; 1321 1322 const int obj_offset = in_bytes(BasicObjectLock::obj_offset()); 1323 const int lock_offset = in_bytes(BasicObjectLock::lock_offset()); 1324 const int mark_offset = lock_offset + 1325 BasicLock::displaced_header_offset_in_bytes(); 1326 1327 // Load object pointer into obj_reg 1328 movptr(obj_reg, Address(lock_reg, obj_offset)); 1329 1330 if (DiagnoseSyncOnValueBasedClasses != 0) { 1331 load_klass(tmp_reg, obj_reg, rklass_decode_tmp); 1332 movl(tmp_reg, Address(tmp_reg, Klass::access_flags_offset())); 1333 testl(tmp_reg, JVM_ACC_IS_VALUE_BASED_CLASS); 1334 jcc(Assembler::notZero, slow_case); 1335 } 1336 1337 if (LockingMode == LM_LIGHTWEIGHT) { 1338 #ifdef _LP64 1339 const Register thread = r15_thread; 1340 #else 1341 const Register thread = lock_reg; 1342 get_thread(thread); 1343 #endif 1344 // Load object header, prepare for CAS from unlocked to locked. 1345 movptr(swap_reg, Address(obj_reg, oopDesc::mark_offset_in_bytes())); 1346 lightweight_lock(obj_reg, swap_reg, thread, tmp_reg, slow_case); 1347 } else if (LockingMode == LM_LEGACY) { 1348 // Load immediate 1 into swap_reg %rax 1349 movl(swap_reg, 1); 1350 1351 // Load (object->mark() | 1) into swap_reg %rax 1352 orptr(swap_reg, Address(obj_reg, oopDesc::mark_offset_in_bytes())); 1353 if (EnableValhalla) { 1354 // Mask inline_type bit such that we go to the slow path if object is an inline type 1355 andptr(swap_reg, ~((int) markWord::inline_type_bit_in_place)); 1356 } 1357 1358 // Save (object->mark() | 1) into BasicLock's displaced header 1359 movptr(Address(lock_reg, mark_offset), swap_reg); 1360 1361 assert(lock_offset == 0, 1362 "displaced header must be first word in BasicObjectLock"); 1363 1364 lock(); 1365 cmpxchgptr(lock_reg, Address(obj_reg, oopDesc::mark_offset_in_bytes())); 1366 jcc(Assembler::zero, count_locking); 1367 1368 const int zero_bits = LP64_ONLY(7) NOT_LP64(3); 1369 1370 // Fast check for recursive lock. 1371 // 1372 // Can apply the optimization only if this is a stack lock 1373 // allocated in this thread. For efficiency, we can focus on 1374 // recently allocated stack locks (instead of reading the stack 1375 // base and checking whether 'mark' points inside the current 1376 // thread stack): 1377 // 1) (mark & zero_bits) == 0, and 1378 // 2) rsp <= mark < mark + os::pagesize() 1379 // 1380 // Warning: rsp + os::pagesize can overflow the stack base. We must 1381 // neither apply the optimization for an inflated lock allocated 1382 // just above the thread stack (this is why condition 1 matters) 1383 // nor apply the optimization if the stack lock is inside the stack 1384 // of another thread. The latter is avoided even in case of overflow 1385 // because we have guard pages at the end of all stacks. Hence, if 1386 // we go over the stack base and hit the stack of another thread, 1387 // this should not be in a writeable area that could contain a 1388 // stack lock allocated by that thread. As a consequence, a stack 1389 // lock less than page size away from rsp is guaranteed to be 1390 // owned by the current thread. 1391 // 1392 // These 3 tests can be done by evaluating the following 1393 // expression: ((mark - rsp) & (zero_bits - os::vm_page_size())), 1394 // assuming both stack pointer and pagesize have their 1395 // least significant bits clear. 1396 // NOTE: the mark is in swap_reg %rax as the result of cmpxchg 1397 subptr(swap_reg, rsp); 1398 andptr(swap_reg, zero_bits - (int)os::vm_page_size()); 1399 1400 // Save the test result, for recursive case, the result is zero 1401 movptr(Address(lock_reg, mark_offset), swap_reg); 1402 jcc(Assembler::notZero, slow_case); 1403 1404 bind(count_locking); 1405 } 1406 inc_held_monitor_count(); 1407 jmp(done); 1408 1409 bind(slow_case); 1410 1411 // Call the runtime routine for slow case 1412 if (LockingMode == LM_LIGHTWEIGHT) { 1413 call_VM(noreg, 1414 CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter_obj), 1415 obj_reg); 1416 } else { 1417 call_VM(noreg, 1418 CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter), 1419 lock_reg); 1420 } 1421 bind(done); 1422 } 1423 } 1424 1425 1426 // Unlocks an object. Used in monitorexit bytecode and 1427 // remove_activation. Throws an IllegalMonitorException if object is 1428 // not locked by current thread. 1429 // 1430 // Args: 1431 // rdx, c_rarg1: BasicObjectLock for lock 1432 // 1433 // Kills: 1434 // rax 1435 // c_rarg0, c_rarg1, c_rarg2, c_rarg3, ... (param regs) 1436 // rscratch1 (scratch reg) 1437 // rax, rbx, rcx, rdx 1438 void InterpreterMacroAssembler::unlock_object(Register lock_reg) { 1439 assert(lock_reg == LP64_ONLY(c_rarg1) NOT_LP64(rdx), 1440 "The argument is only for looks. It must be c_rarg1"); 1441 1442 if (LockingMode == LM_MONITOR) { 1443 call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit), lock_reg); 1444 } else { 1445 Label count_locking, done, slow_case; 1446 1447 const Register swap_reg = rax; // Must use rax for cmpxchg instruction 1448 const Register header_reg = LP64_ONLY(c_rarg2) NOT_LP64(rbx); // Will contain the old oopMark 1449 const Register obj_reg = LP64_ONLY(c_rarg3) NOT_LP64(rcx); // Will contain the oop 1450 1451 save_bcp(); // Save in case of exception 1452 1453 if (LockingMode != LM_LIGHTWEIGHT) { 1454 // Convert from BasicObjectLock structure to object and BasicLock 1455 // structure Store the BasicLock address into %rax 1456 lea(swap_reg, Address(lock_reg, BasicObjectLock::lock_offset())); 1457 } 1458 1459 // Load oop into obj_reg(%c_rarg3) 1460 movptr(obj_reg, Address(lock_reg, BasicObjectLock::obj_offset())); 1461 1462 // Free entry 1463 movptr(Address(lock_reg, BasicObjectLock::obj_offset()), NULL_WORD); 1464 1465 if (LockingMode == LM_LIGHTWEIGHT) { 1466 #ifdef _LP64 1467 const Register thread = r15_thread; 1468 #else 1469 const Register thread = header_reg; 1470 get_thread(thread); 1471 #endif 1472 // Handle unstructured locking. 1473 Register tmp = swap_reg; 1474 movl(tmp, Address(thread, JavaThread::lock_stack_top_offset())); 1475 cmpptr(obj_reg, Address(thread, tmp, Address::times_1, -oopSize)); 1476 jcc(Assembler::notEqual, slow_case); 1477 // Try to swing header from locked to unlocked. 1478 movptr(swap_reg, Address(obj_reg, oopDesc::mark_offset_in_bytes())); 1479 andptr(swap_reg, ~(int32_t)markWord::lock_mask_in_place); 1480 lightweight_unlock(obj_reg, swap_reg, header_reg, slow_case); 1481 } else if (LockingMode == LM_LEGACY) { 1482 // Load the old header from BasicLock structure 1483 movptr(header_reg, Address(swap_reg, 1484 BasicLock::displaced_header_offset_in_bytes())); 1485 1486 // Test for recursion 1487 testptr(header_reg, header_reg); 1488 1489 // zero for recursive case 1490 jcc(Assembler::zero, count_locking); 1491 1492 // Atomic swap back the old header 1493 lock(); 1494 cmpxchgptr(header_reg, Address(obj_reg, oopDesc::mark_offset_in_bytes())); 1495 1496 // zero for simple unlock of a stack-lock case 1497 jcc(Assembler::notZero, slow_case); 1498 1499 bind(count_locking); 1500 } 1501 dec_held_monitor_count(); 1502 jmp(done); 1503 1504 bind(slow_case); 1505 // Call the runtime routine for slow case. 1506 movptr(Address(lock_reg, BasicObjectLock::obj_offset()), obj_reg); // restore obj 1507 call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit), lock_reg); 1508 1509 bind(done); 1510 1511 restore_bcp(); 1512 } 1513 } 1514 1515 void InterpreterMacroAssembler::test_method_data_pointer(Register mdp, 1516 Label& zero_continue) { 1517 assert(ProfileInterpreter, "must be profiling interpreter"); 1518 movptr(mdp, Address(rbp, frame::interpreter_frame_mdp_offset * wordSize)); 1519 testptr(mdp, mdp); 1520 jcc(Assembler::zero, zero_continue); 1521 } 1522 1523 1524 // Set the method data pointer for the current bcp. 1525 void InterpreterMacroAssembler::set_method_data_pointer_for_bcp() { 1526 assert(ProfileInterpreter, "must be profiling interpreter"); 1527 Label set_mdp; 1528 push(rax); 1529 push(rbx); 1530 1531 get_method(rbx); 1532 // Test MDO to avoid the call if it is null. 1533 movptr(rax, Address(rbx, in_bytes(Method::method_data_offset()))); 1534 testptr(rax, rax); 1535 jcc(Assembler::zero, set_mdp); 1536 // rbx: method 1537 // _bcp_register: bcp 1538 call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::bcp_to_di), rbx, _bcp_register); 1539 // rax: mdi 1540 // mdo is guaranteed to be non-zero here, we checked for it before the call. 1541 movptr(rbx, Address(rbx, in_bytes(Method::method_data_offset()))); 1542 addptr(rbx, in_bytes(MethodData::data_offset())); 1543 addptr(rax, rbx); 1544 bind(set_mdp); 1545 movptr(Address(rbp, frame::interpreter_frame_mdp_offset * wordSize), rax); 1546 pop(rbx); 1547 pop(rax); 1548 } 1549 1550 void InterpreterMacroAssembler::verify_method_data_pointer() { 1551 assert(ProfileInterpreter, "must be profiling interpreter"); 1552 #ifdef ASSERT 1553 Label verify_continue; 1554 push(rax); 1555 push(rbx); 1556 Register arg3_reg = LP64_ONLY(c_rarg3) NOT_LP64(rcx); 1557 Register arg2_reg = LP64_ONLY(c_rarg2) NOT_LP64(rdx); 1558 push(arg3_reg); 1559 push(arg2_reg); 1560 test_method_data_pointer(arg3_reg, verify_continue); // If mdp is zero, continue 1561 get_method(rbx); 1562 1563 // If the mdp is valid, it will point to a DataLayout header which is 1564 // consistent with the bcp. The converse is highly probable also. 1565 load_unsigned_short(arg2_reg, 1566 Address(arg3_reg, in_bytes(DataLayout::bci_offset()))); 1567 addptr(arg2_reg, Address(rbx, Method::const_offset())); 1568 lea(arg2_reg, Address(arg2_reg, ConstMethod::codes_offset())); 1569 cmpptr(arg2_reg, _bcp_register); 1570 jcc(Assembler::equal, verify_continue); 1571 // rbx: method 1572 // _bcp_register: bcp 1573 // c_rarg3: mdp 1574 call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::verify_mdp), 1575 rbx, _bcp_register, arg3_reg); 1576 bind(verify_continue); 1577 pop(arg2_reg); 1578 pop(arg3_reg); 1579 pop(rbx); 1580 pop(rax); 1581 #endif // ASSERT 1582 } 1583 1584 1585 void InterpreterMacroAssembler::set_mdp_data_at(Register mdp_in, 1586 int constant, 1587 Register value) { 1588 assert(ProfileInterpreter, "must be profiling interpreter"); 1589 Address data(mdp_in, constant); 1590 movptr(data, value); 1591 } 1592 1593 1594 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in, 1595 int constant, 1596 bool decrement) { 1597 // Counter address 1598 Address data(mdp_in, constant); 1599 1600 increment_mdp_data_at(data, decrement); 1601 } 1602 1603 void InterpreterMacroAssembler::increment_mdp_data_at(Address data, 1604 bool decrement) { 1605 assert(ProfileInterpreter, "must be profiling interpreter"); 1606 // %%% this does 64bit counters at best it is wasting space 1607 // at worst it is a rare bug when counters overflow 1608 1609 if (decrement) { 1610 // Decrement the register. Set condition codes. 1611 addptr(data, -DataLayout::counter_increment); 1612 // If the decrement causes the counter to overflow, stay negative 1613 Label L; 1614 jcc(Assembler::negative, L); 1615 addptr(data, DataLayout::counter_increment); 1616 bind(L); 1617 } else { 1618 assert(DataLayout::counter_increment == 1, 1619 "flow-free idiom only works with 1"); 1620 // Increment the register. Set carry flag. 1621 addptr(data, DataLayout::counter_increment); 1622 // If the increment causes the counter to overflow, pull back by 1. 1623 sbbptr(data, 0); 1624 } 1625 } 1626 1627 1628 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in, 1629 Register reg, 1630 int constant, 1631 bool decrement) { 1632 Address data(mdp_in, reg, Address::times_1, constant); 1633 1634 increment_mdp_data_at(data, decrement); 1635 } 1636 1637 void InterpreterMacroAssembler::set_mdp_flag_at(Register mdp_in, 1638 int flag_byte_constant) { 1639 assert(ProfileInterpreter, "must be profiling interpreter"); 1640 int header_offset = in_bytes(DataLayout::flags_offset()); 1641 int header_bits = flag_byte_constant; 1642 // Set the flag 1643 orb(Address(mdp_in, header_offset), header_bits); 1644 } 1645 1646 1647 1648 void InterpreterMacroAssembler::test_mdp_data_at(Register mdp_in, 1649 int offset, 1650 Register value, 1651 Register test_value_out, 1652 Label& not_equal_continue) { 1653 assert(ProfileInterpreter, "must be profiling interpreter"); 1654 if (test_value_out == noreg) { 1655 cmpptr(value, Address(mdp_in, offset)); 1656 } else { 1657 // Put the test value into a register, so caller can use it: 1658 movptr(test_value_out, Address(mdp_in, offset)); 1659 cmpptr(test_value_out, value); 1660 } 1661 jcc(Assembler::notEqual, not_equal_continue); 1662 } 1663 1664 1665 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in, 1666 int offset_of_disp) { 1667 assert(ProfileInterpreter, "must be profiling interpreter"); 1668 Address disp_address(mdp_in, offset_of_disp); 1669 addptr(mdp_in, disp_address); 1670 movptr(Address(rbp, frame::interpreter_frame_mdp_offset * wordSize), mdp_in); 1671 } 1672 1673 1674 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in, 1675 Register reg, 1676 int offset_of_disp) { 1677 assert(ProfileInterpreter, "must be profiling interpreter"); 1678 Address disp_address(mdp_in, reg, Address::times_1, offset_of_disp); 1679 addptr(mdp_in, disp_address); 1680 movptr(Address(rbp, frame::interpreter_frame_mdp_offset * wordSize), mdp_in); 1681 } 1682 1683 1684 void InterpreterMacroAssembler::update_mdp_by_constant(Register mdp_in, 1685 int constant) { 1686 assert(ProfileInterpreter, "must be profiling interpreter"); 1687 addptr(mdp_in, constant); 1688 movptr(Address(rbp, frame::interpreter_frame_mdp_offset * wordSize), mdp_in); 1689 } 1690 1691 1692 void InterpreterMacroAssembler::update_mdp_for_ret(Register return_bci) { 1693 assert(ProfileInterpreter, "must be profiling interpreter"); 1694 push(return_bci); // save/restore across call_VM 1695 call_VM(noreg, 1696 CAST_FROM_FN_PTR(address, InterpreterRuntime::update_mdp_for_ret), 1697 return_bci); 1698 pop(return_bci); 1699 } 1700 1701 1702 void InterpreterMacroAssembler::profile_taken_branch(Register mdp, 1703 Register bumped_count) { 1704 if (ProfileInterpreter) { 1705 Label profile_continue; 1706 1707 // If no method data exists, go to profile_continue. 1708 // Otherwise, assign to mdp 1709 test_method_data_pointer(mdp, profile_continue); 1710 1711 // We are taking a branch. Increment the taken count. 1712 // We inline increment_mdp_data_at to return bumped_count in a register 1713 //increment_mdp_data_at(mdp, in_bytes(JumpData::taken_offset())); 1714 Address data(mdp, in_bytes(JumpData::taken_offset())); 1715 movptr(bumped_count, data); 1716 assert(DataLayout::counter_increment == 1, 1717 "flow-free idiom only works with 1"); 1718 addptr(bumped_count, DataLayout::counter_increment); 1719 sbbptr(bumped_count, 0); 1720 movptr(data, bumped_count); // Store back out 1721 1722 // The method data pointer needs to be updated to reflect the new target. 1723 update_mdp_by_offset(mdp, in_bytes(JumpData::displacement_offset())); 1724 bind(profile_continue); 1725 } 1726 } 1727 1728 1729 void InterpreterMacroAssembler::profile_not_taken_branch(Register mdp, bool acmp) { 1730 if (ProfileInterpreter) { 1731 Label profile_continue; 1732 1733 // If no method data exists, go to profile_continue. 1734 test_method_data_pointer(mdp, profile_continue); 1735 1736 // We are taking a branch. Increment the not taken count. 1737 increment_mdp_data_at(mdp, in_bytes(BranchData::not_taken_offset())); 1738 1739 // The method data pointer needs to be updated to correspond to 1740 // the next bytecode 1741 update_mdp_by_constant(mdp, acmp ? in_bytes(ACmpData::acmp_data_size()): in_bytes(BranchData::branch_data_size())); 1742 bind(profile_continue); 1743 } 1744 } 1745 1746 void InterpreterMacroAssembler::profile_call(Register mdp) { 1747 if (ProfileInterpreter) { 1748 Label profile_continue; 1749 1750 // If no method data exists, go to profile_continue. 1751 test_method_data_pointer(mdp, profile_continue); 1752 1753 // We are making a call. Increment the count. 1754 increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset())); 1755 1756 // The method data pointer needs to be updated to reflect the new target. 1757 update_mdp_by_constant(mdp, in_bytes(CounterData::counter_data_size())); 1758 bind(profile_continue); 1759 } 1760 } 1761 1762 1763 void InterpreterMacroAssembler::profile_final_call(Register mdp) { 1764 if (ProfileInterpreter) { 1765 Label profile_continue; 1766 1767 // If no method data exists, go to profile_continue. 1768 test_method_data_pointer(mdp, profile_continue); 1769 1770 // We are making a call. Increment the count. 1771 increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset())); 1772 1773 // The method data pointer needs to be updated to reflect the new target. 1774 update_mdp_by_constant(mdp, 1775 in_bytes(VirtualCallData:: 1776 virtual_call_data_size())); 1777 bind(profile_continue); 1778 } 1779 } 1780 1781 1782 void InterpreterMacroAssembler::profile_virtual_call(Register receiver, 1783 Register mdp, 1784 Register reg2, 1785 bool receiver_can_be_null) { 1786 if (ProfileInterpreter) { 1787 Label profile_continue; 1788 1789 // If no method data exists, go to profile_continue. 1790 test_method_data_pointer(mdp, profile_continue); 1791 1792 Label skip_receiver_profile; 1793 if (receiver_can_be_null) { 1794 Label not_null; 1795 testptr(receiver, receiver); 1796 jccb(Assembler::notZero, not_null); 1797 // We are making a call. Increment the count for null receiver. 1798 increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset())); 1799 jmp(skip_receiver_profile); 1800 bind(not_null); 1801 } 1802 1803 // Record the receiver type. 1804 record_klass_in_profile(receiver, mdp, reg2); 1805 bind(skip_receiver_profile); 1806 1807 // The method data pointer needs to be updated to reflect the new target. 1808 update_mdp_by_constant(mdp, in_bytes(VirtualCallData::virtual_call_data_size())); 1809 bind(profile_continue); 1810 } 1811 } 1812 1813 // This routine creates a state machine for updating the multi-row 1814 // type profile at a virtual call site (or other type-sensitive bytecode). 1815 // The machine visits each row (of receiver/count) until the receiver type 1816 // is found, or until it runs out of rows. At the same time, it remembers 1817 // the location of the first empty row. (An empty row records null for its 1818 // receiver, and can be allocated for a newly-observed receiver type.) 1819 // Because there are two degrees of freedom in the state, a simple linear 1820 // search will not work; it must be a decision tree. Hence this helper 1821 // function is recursive, to generate the required tree structured code. 1822 // It's the interpreter, so we are trading off code space for speed. 1823 // See below for example code. 1824 void InterpreterMacroAssembler::record_klass_in_profile_helper(Register receiver, Register mdp, 1825 Register reg2, int start_row, 1826 Label& done) { 1827 if (TypeProfileWidth == 0) { 1828 increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset())); 1829 } else { 1830 record_item_in_profile_helper(receiver, mdp, reg2, 0, done, TypeProfileWidth, 1831 &VirtualCallData::receiver_offset, &VirtualCallData::receiver_count_offset); 1832 } 1833 } 1834 1835 void InterpreterMacroAssembler::record_item_in_profile_helper(Register item, Register mdp, Register reg2, int start_row, 1836 Label& done, int total_rows, 1837 OffsetFunction item_offset_fn, 1838 OffsetFunction item_count_offset_fn) { 1839 int last_row = total_rows - 1; 1840 assert(start_row <= last_row, "must be work left to do"); 1841 // Test this row for both the item and for null. 1842 // Take any of three different outcomes: 1843 // 1. found item => increment count and goto done 1844 // 2. found null => keep looking for case 1, maybe allocate this cell 1845 // 3. found something else => keep looking for cases 1 and 2 1846 // Case 3 is handled by a recursive call. 1847 for (int row = start_row; row <= last_row; row++) { 1848 Label next_test; 1849 bool test_for_null_also = (row == start_row); 1850 1851 // See if the item is item[n]. 1852 int item_offset = in_bytes(item_offset_fn(row)); 1853 test_mdp_data_at(mdp, item_offset, item, 1854 (test_for_null_also ? reg2 : noreg), 1855 next_test); 1856 // (Reg2 now contains the item from the CallData.) 1857 1858 // The item is item[n]. Increment count[n]. 1859 int count_offset = in_bytes(item_count_offset_fn(row)); 1860 increment_mdp_data_at(mdp, count_offset); 1861 jmp(done); 1862 bind(next_test); 1863 1864 if (test_for_null_also) { 1865 // Failed the equality check on item[n]... Test for null. 1866 testptr(reg2, reg2); 1867 if (start_row == last_row) { 1868 // The only thing left to do is handle the null case. 1869 Label found_null; 1870 jccb(Assembler::zero, found_null); 1871 // Item did not match any saved item and there is no empty row for it. 1872 // Increment total counter to indicate polymorphic case. 1873 increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset())); 1874 jmp(done); 1875 bind(found_null); 1876 break; 1877 } 1878 Label found_null; 1879 // Since null is rare, make it be the branch-taken case. 1880 jcc(Assembler::zero, found_null); 1881 1882 // Put all the "Case 3" tests here. 1883 record_item_in_profile_helper(item, mdp, reg2, start_row + 1, done, total_rows, 1884 item_offset_fn, item_count_offset_fn); 1885 1886 // Found a null. Keep searching for a matching item, 1887 // but remember that this is an empty (unused) slot. 1888 bind(found_null); 1889 } 1890 } 1891 1892 // In the fall-through case, we found no matching item, but we 1893 // observed the item[start_row] is null. 1894 1895 // Fill in the item field and increment the count. 1896 int item_offset = in_bytes(item_offset_fn(start_row)); 1897 set_mdp_data_at(mdp, item_offset, item); 1898 int count_offset = in_bytes(item_count_offset_fn(start_row)); 1899 movl(reg2, DataLayout::counter_increment); 1900 set_mdp_data_at(mdp, count_offset, reg2); 1901 if (start_row > 0) { 1902 jmp(done); 1903 } 1904 } 1905 1906 // Example state machine code for three profile rows: 1907 // // main copy of decision tree, rooted at row[1] 1908 // if (row[0].rec == rec) { row[0].incr(); goto done; } 1909 // if (row[0].rec != nullptr) { 1910 // // inner copy of decision tree, rooted at row[1] 1911 // if (row[1].rec == rec) { row[1].incr(); goto done; } 1912 // if (row[1].rec != nullptr) { 1913 // // degenerate decision tree, rooted at row[2] 1914 // if (row[2].rec == rec) { row[2].incr(); goto done; } 1915 // if (row[2].rec != nullptr) { count.incr(); goto done; } // overflow 1916 // row[2].init(rec); goto done; 1917 // } else { 1918 // // remember row[1] is empty 1919 // if (row[2].rec == rec) { row[2].incr(); goto done; } 1920 // row[1].init(rec); goto done; 1921 // } 1922 // } else { 1923 // // remember row[0] is empty 1924 // if (row[1].rec == rec) { row[1].incr(); goto done; } 1925 // if (row[2].rec == rec) { row[2].incr(); goto done; } 1926 // row[0].init(rec); goto done; 1927 // } 1928 // done: 1929 1930 void InterpreterMacroAssembler::record_klass_in_profile(Register receiver, Register mdp, Register reg2) { 1931 assert(ProfileInterpreter, "must be profiling"); 1932 Label done; 1933 1934 record_klass_in_profile_helper(receiver, mdp, reg2, 0, done); 1935 1936 bind (done); 1937 } 1938 1939 void InterpreterMacroAssembler::profile_ret(Register return_bci, 1940 Register mdp) { 1941 if (ProfileInterpreter) { 1942 Label profile_continue; 1943 uint row; 1944 1945 // If no method data exists, go to profile_continue. 1946 test_method_data_pointer(mdp, profile_continue); 1947 1948 // Update the total ret count. 1949 increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset())); 1950 1951 for (row = 0; row < RetData::row_limit(); row++) { 1952 Label next_test; 1953 1954 // See if return_bci is equal to bci[n]: 1955 test_mdp_data_at(mdp, 1956 in_bytes(RetData::bci_offset(row)), 1957 return_bci, noreg, 1958 next_test); 1959 1960 // return_bci is equal to bci[n]. Increment the count. 1961 increment_mdp_data_at(mdp, in_bytes(RetData::bci_count_offset(row))); 1962 1963 // The method data pointer needs to be updated to reflect the new target. 1964 update_mdp_by_offset(mdp, 1965 in_bytes(RetData::bci_displacement_offset(row))); 1966 jmp(profile_continue); 1967 bind(next_test); 1968 } 1969 1970 update_mdp_for_ret(return_bci); 1971 1972 bind(profile_continue); 1973 } 1974 } 1975 1976 1977 void InterpreterMacroAssembler::profile_null_seen(Register mdp) { 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 set_mdp_flag_at(mdp, BitData::null_seen_byte_constant()); 1985 1986 // The method data pointer needs to be updated. 1987 int mdp_delta = in_bytes(BitData::bit_data_size()); 1988 if (TypeProfileCasts) { 1989 mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size()); 1990 } 1991 update_mdp_by_constant(mdp, mdp_delta); 1992 1993 bind(profile_continue); 1994 } 1995 } 1996 1997 1998 void InterpreterMacroAssembler::profile_typecheck(Register mdp, Register klass, Register reg2) { 1999 if (ProfileInterpreter) { 2000 Label profile_continue; 2001 2002 // If no method data exists, go to profile_continue. 2003 test_method_data_pointer(mdp, profile_continue); 2004 2005 // The method data pointer needs to be updated. 2006 int mdp_delta = in_bytes(BitData::bit_data_size()); 2007 if (TypeProfileCasts) { 2008 mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size()); 2009 2010 // Record the object type. 2011 record_klass_in_profile(klass, mdp, reg2); 2012 NOT_LP64(assert(reg2 == rdi, "we know how to fix this blown reg");) 2013 NOT_LP64(restore_locals();) // Restore EDI 2014 } 2015 update_mdp_by_constant(mdp, mdp_delta); 2016 2017 bind(profile_continue); 2018 } 2019 } 2020 2021 2022 void InterpreterMacroAssembler::profile_switch_default(Register mdp) { 2023 if (ProfileInterpreter) { 2024 Label profile_continue; 2025 2026 // If no method data exists, go to profile_continue. 2027 test_method_data_pointer(mdp, profile_continue); 2028 2029 // Update the default case count 2030 increment_mdp_data_at(mdp, 2031 in_bytes(MultiBranchData::default_count_offset())); 2032 2033 // The method data pointer needs to be updated. 2034 update_mdp_by_offset(mdp, 2035 in_bytes(MultiBranchData:: 2036 default_displacement_offset())); 2037 2038 bind(profile_continue); 2039 } 2040 } 2041 2042 2043 void InterpreterMacroAssembler::profile_switch_case(Register index, 2044 Register mdp, 2045 Register reg2) { 2046 if (ProfileInterpreter) { 2047 Label profile_continue; 2048 2049 // If no method data exists, go to profile_continue. 2050 test_method_data_pointer(mdp, profile_continue); 2051 2052 // Build the base (index * per_case_size_in_bytes()) + 2053 // case_array_offset_in_bytes() 2054 movl(reg2, in_bytes(MultiBranchData::per_case_size())); 2055 imulptr(index, reg2); // XXX l ? 2056 addptr(index, in_bytes(MultiBranchData::case_array_offset())); // XXX l ? 2057 2058 // Update the case count 2059 increment_mdp_data_at(mdp, 2060 index, 2061 in_bytes(MultiBranchData::relative_count_offset())); 2062 2063 // The method data pointer needs to be updated. 2064 update_mdp_by_offset(mdp, 2065 index, 2066 in_bytes(MultiBranchData:: 2067 relative_displacement_offset())); 2068 2069 bind(profile_continue); 2070 } 2071 } 2072 2073 template <class ArrayData> void InterpreterMacroAssembler::profile_array_type(Register mdp, 2074 Register array, 2075 Register tmp) { 2076 if (ProfileInterpreter) { 2077 Label profile_continue; 2078 2079 // If no method data exists, go to profile_continue. 2080 test_method_data_pointer(mdp, profile_continue); 2081 2082 mov(tmp, array); 2083 profile_obj_type(tmp, Address(mdp, in_bytes(ArrayData::array_offset()))); 2084 2085 Label not_flat; 2086 test_non_flat_array_oop(array, tmp, not_flat); 2087 2088 set_mdp_flag_at(mdp, ArrayData::flat_array_byte_constant()); 2089 2090 bind(not_flat); 2091 2092 Label not_null_free; 2093 test_non_null_free_array_oop(array, tmp, not_null_free); 2094 2095 set_mdp_flag_at(mdp, ArrayData::null_free_array_byte_constant()); 2096 2097 bind(not_null_free); 2098 2099 bind(profile_continue); 2100 } 2101 } 2102 2103 template void InterpreterMacroAssembler::profile_array_type<ArrayLoadData>(Register mdp, 2104 Register array, 2105 Register tmp); 2106 template void InterpreterMacroAssembler::profile_array_type<ArrayStoreData>(Register mdp, 2107 Register array, 2108 Register tmp); 2109 2110 2111 void InterpreterMacroAssembler::profile_multiple_element_types(Register mdp, Register element, Register tmp, const Register tmp2) { 2112 if (ProfileInterpreter) { 2113 Label profile_continue; 2114 2115 // If no method data exists, go to profile_continue. 2116 test_method_data_pointer(mdp, profile_continue); 2117 2118 Label done, update; 2119 testptr(element, element); 2120 jccb(Assembler::notZero, update); 2121 set_mdp_flag_at(mdp, BitData::null_seen_byte_constant()); 2122 jmp(done); 2123 2124 bind(update); 2125 load_klass(tmp, element, rscratch1); 2126 2127 // Record the object type. 2128 record_klass_in_profile(tmp, mdp, tmp2); 2129 2130 bind(done); 2131 2132 // The method data pointer needs to be updated. 2133 update_mdp_by_constant(mdp, in_bytes(ArrayStoreData::array_store_data_size())); 2134 2135 bind(profile_continue); 2136 } 2137 } 2138 2139 void InterpreterMacroAssembler::profile_element_type(Register mdp, 2140 Register element, 2141 Register tmp) { 2142 if (ProfileInterpreter) { 2143 Label profile_continue; 2144 2145 // If no method data exists, go to profile_continue. 2146 test_method_data_pointer(mdp, profile_continue); 2147 2148 mov(tmp, element); 2149 profile_obj_type(tmp, Address(mdp, in_bytes(ArrayLoadData::element_offset()))); 2150 2151 // The method data pointer needs to be updated. 2152 update_mdp_by_constant(mdp, in_bytes(ArrayLoadData::array_load_data_size())); 2153 2154 bind(profile_continue); 2155 } 2156 } 2157 2158 void InterpreterMacroAssembler::profile_acmp(Register mdp, 2159 Register left, 2160 Register right, 2161 Register tmp) { 2162 if (ProfileInterpreter) { 2163 Label profile_continue; 2164 2165 // If no method data exists, go to profile_continue. 2166 test_method_data_pointer(mdp, profile_continue); 2167 2168 mov(tmp, left); 2169 profile_obj_type(tmp, Address(mdp, in_bytes(ACmpData::left_offset()))); 2170 2171 Label left_not_inline_type; 2172 test_oop_is_not_inline_type(left, tmp, left_not_inline_type); 2173 set_mdp_flag_at(mdp, ACmpData::left_inline_type_byte_constant()); 2174 bind(left_not_inline_type); 2175 2176 mov(tmp, right); 2177 profile_obj_type(tmp, Address(mdp, in_bytes(ACmpData::right_offset()))); 2178 2179 Label right_not_inline_type; 2180 test_oop_is_not_inline_type(right, tmp, right_not_inline_type); 2181 set_mdp_flag_at(mdp, ACmpData::right_inline_type_byte_constant()); 2182 bind(right_not_inline_type); 2183 2184 bind(profile_continue); 2185 } 2186 } 2187 2188 2189 void InterpreterMacroAssembler::_interp_verify_oop(Register reg, TosState state, const char* file, int line) { 2190 if (state == atos) { 2191 MacroAssembler::_verify_oop_checked(reg, "broken oop", file, line); 2192 } 2193 } 2194 2195 void InterpreterMacroAssembler::verify_FPU(int stack_depth, TosState state) { 2196 #ifndef _LP64 2197 if ((state == ftos && UseSSE < 1) || 2198 (state == dtos && UseSSE < 2)) { 2199 MacroAssembler::verify_FPU(stack_depth); 2200 } 2201 #endif 2202 } 2203 2204 // Jump if ((*counter_addr += increment) & mask) == 0 2205 void InterpreterMacroAssembler::increment_mask_and_jump(Address counter_addr, Address mask, 2206 Register scratch, Label* where) { 2207 // This update is actually not atomic and can lose a number of updates 2208 // under heavy contention, but the alternative of using the (contended) 2209 // atomic update here penalizes profiling paths too much. 2210 movl(scratch, counter_addr); 2211 incrementl(scratch, InvocationCounter::count_increment); 2212 movl(counter_addr, scratch); 2213 andl(scratch, mask); 2214 if (where != nullptr) { 2215 jcc(Assembler::zero, *where); 2216 } 2217 } 2218 2219 void InterpreterMacroAssembler::notify_method_entry() { 2220 // Whenever JVMTI is interp_only_mode, method entry/exit events are sent to 2221 // track stack depth. If it is possible to enter interp_only_mode we add 2222 // the code to check if the event should be sent. 2223 Register rthread = LP64_ONLY(r15_thread) NOT_LP64(rcx); 2224 Register rarg = LP64_ONLY(c_rarg1) NOT_LP64(rbx); 2225 if (JvmtiExport::can_post_interpreter_events()) { 2226 Label L; 2227 NOT_LP64(get_thread(rthread);) 2228 movl(rdx, Address(rthread, JavaThread::interp_only_mode_offset())); 2229 testl(rdx, rdx); 2230 jcc(Assembler::zero, L); 2231 call_VM(noreg, CAST_FROM_FN_PTR(address, 2232 InterpreterRuntime::post_method_entry)); 2233 bind(L); 2234 } 2235 2236 { 2237 SkipIfEqual skip(this, &DTraceMethodProbes, false, rscratch1); 2238 NOT_LP64(get_thread(rthread);) 2239 get_method(rarg); 2240 call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry), 2241 rthread, rarg); 2242 } 2243 2244 // RedefineClasses() tracing support for obsolete method entry 2245 if (log_is_enabled(Trace, redefine, class, obsolete)) { 2246 NOT_LP64(get_thread(rthread);) 2247 get_method(rarg); 2248 call_VM_leaf( 2249 CAST_FROM_FN_PTR(address, SharedRuntime::rc_trace_method_entry), 2250 rthread, rarg); 2251 } 2252 } 2253 2254 2255 void InterpreterMacroAssembler::notify_method_exit( 2256 TosState state, NotifyMethodExitMode mode) { 2257 // Whenever JVMTI is interp_only_mode, method entry/exit events are sent to 2258 // track stack depth. If it is possible to enter interp_only_mode we add 2259 // the code to check if the event should be sent. 2260 Register rthread = LP64_ONLY(r15_thread) NOT_LP64(rcx); 2261 Register rarg = LP64_ONLY(c_rarg1) NOT_LP64(rbx); 2262 if (mode == NotifyJVMTI && JvmtiExport::can_post_interpreter_events()) { 2263 Label L; 2264 // Note: frame::interpreter_frame_result has a dependency on how the 2265 // method result is saved across the call to post_method_exit. If this 2266 // is changed then the interpreter_frame_result implementation will 2267 // need to be updated too. 2268 2269 // template interpreter will leave the result on the top of the stack. 2270 push(state); 2271 NOT_LP64(get_thread(rthread);) 2272 movl(rdx, Address(rthread, JavaThread::interp_only_mode_offset())); 2273 testl(rdx, rdx); 2274 jcc(Assembler::zero, L); 2275 call_VM(noreg, 2276 CAST_FROM_FN_PTR(address, InterpreterRuntime::post_method_exit)); 2277 bind(L); 2278 pop(state); 2279 } 2280 2281 { 2282 SkipIfEqual skip(this, &DTraceMethodProbes, false, rscratch1); 2283 push(state); 2284 NOT_LP64(get_thread(rthread);) 2285 get_method(rarg); 2286 call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit), 2287 rthread, rarg); 2288 pop(state); 2289 } 2290 } 2291 2292 void InterpreterMacroAssembler::load_resolved_indy_entry(Register cache, Register index) { 2293 // Get index out of bytecode pointer 2294 get_cache_index_at_bcp(index, 1, sizeof(u4)); 2295 // Get address of invokedynamic array 2296 movptr(cache, Address(rbp, frame::interpreter_frame_cache_offset * wordSize)); 2297 movptr(cache, Address(cache, in_bytes(ConstantPoolCache::invokedynamic_entries_offset()))); 2298 if (is_power_of_2(sizeof(ResolvedIndyEntry))) { 2299 shll(index, log2i_exact(sizeof(ResolvedIndyEntry))); // Scale index by power of 2 2300 } else { 2301 imull(index, index, sizeof(ResolvedIndyEntry)); // Scale the index to be the entry index * sizeof(ResolvedIndyEntry) 2302 } 2303 lea(cache, Address(cache, index, Address::times_1, Array<ResolvedIndyEntry>::base_offset_in_bytes())); 2304 } 2305 2306 void InterpreterMacroAssembler::load_field_entry(Register cache, Register index, int bcp_offset) { 2307 // Get index out of bytecode pointer 2308 movptr(cache, Address(rbp, frame::interpreter_frame_cache_offset * wordSize)); 2309 get_cache_index_at_bcp(index, bcp_offset, sizeof(u2)); 2310 2311 movptr(cache, Address(cache, ConstantPoolCache::field_entries_offset())); 2312 // Take shortcut if the size is a power of 2 2313 if (is_power_of_2(sizeof(ResolvedFieldEntry))) { 2314 shll(index, log2i_exact(sizeof(ResolvedFieldEntry))); // Scale index by power of 2 2315 } else { 2316 imull(index, index, sizeof(ResolvedFieldEntry)); // Scale the index to be the entry index * sizeof(ResolvedFieldEntry) 2317 } 2318 lea(cache, Address(cache, index, Address::times_1, Array<ResolvedFieldEntry>::base_offset_in_bytes())); 2319 } 2320 2321 void InterpreterMacroAssembler::load_method_entry(Register cache, Register index, int bcp_offset) { 2322 // Get index out of bytecode pointer 2323 movptr(cache, Address(rbp, frame::interpreter_frame_cache_offset * wordSize)); 2324 get_cache_index_at_bcp(index, bcp_offset, sizeof(u2)); 2325 2326 movptr(cache, Address(cache, ConstantPoolCache::method_entries_offset())); 2327 imull(index, index, sizeof(ResolvedMethodEntry)); // Scale the index to be the entry index * sizeof(ResolvedMethodEntry) 2328 lea(cache, Address(cache, index, Address::times_1, Array<ResolvedMethodEntry>::base_offset_in_bytes())); 2329 }