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