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