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