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   // Add in the index
 516   addptr(result, tmp);
 517   load_heap_oop(result, Address(result, arrayOopDesc::base_offset_in_bytes(T_OBJECT)));
 518 }
 519 
 520 
 521 // Generate a subtype check: branch to ok_is_subtype if sub_klass is a
 522 // subtype of super_klass.
 523 //
 524 // Args:
 525 //      rax: superklass
 526 //      Rsub_klass: subklass
 527 //
 528 // Kills:
 529 //      rcx, rdi
 530 void InterpreterMacroAssembler::gen_subtype_check(Register Rsub_klass,
 531                                                   Label& ok_is_subtype) {
 532   assert(Rsub_klass != rax, "rax holds superklass");
 533   LP64_ONLY(assert(Rsub_klass != r14, "r14 holds locals");)
 534   LP64_ONLY(assert(Rsub_klass != r13, "r13 holds bcp");)
 535   assert(Rsub_klass != rcx, "rcx holds 2ndary super array length");
 536   assert(Rsub_klass != rdi, "rdi holds 2ndary super array scan ptr");
 537 
 538   // Profile the not-null value's klass.
 539   profile_typecheck(rcx, Rsub_klass, rdi); // blows rcx, reloads rdi
 540 
 541   // Do the check.
 542   check_klass_subtype(Rsub_klass, rax, rcx, ok_is_subtype); // blows rcx
 543 
 544   // Profile the failure of the check.
 545   profile_typecheck_failed(rcx); // blows rcx
 546 }
 547 
 548 
 549 #ifndef _LP64
 550 void InterpreterMacroAssembler::f2ieee() {
 551   if (IEEEPrecision) {
 552     fstp_s(Address(rsp, 0));
 553     fld_s(Address(rsp, 0));
 554   }
 555 }
 556 
 557 
 558 void InterpreterMacroAssembler::d2ieee() {
 559   if (IEEEPrecision) {
 560     fstp_d(Address(rsp, 0));
 561     fld_d(Address(rsp, 0));
 562   }
 563 }
 564 #endif // _LP64
 565 
 566 // Java Expression Stack
 567 
 568 void InterpreterMacroAssembler::pop_ptr(Register r) {
 569   pop(r);
 570 }
 571 
 572 void InterpreterMacroAssembler::push_ptr(Register r) {
 573   push(r);
 574 }
 575 
 576 void InterpreterMacroAssembler::push_i(Register r) {
 577   push(r);
 578 }
 579 
 580 void InterpreterMacroAssembler::push_f(XMMRegister r) {
 581   subptr(rsp, wordSize);
 582   movflt(Address(rsp, 0), r);
 583 }
 584 
 585 void InterpreterMacroAssembler::pop_f(XMMRegister r) {
 586   movflt(r, Address(rsp, 0));
 587   addptr(rsp, wordSize);
 588 }
 589 
 590 void InterpreterMacroAssembler::push_d(XMMRegister r) {
 591   subptr(rsp, 2 * wordSize);
 592   movdbl(Address(rsp, 0), r);
 593 }
 594 
 595 void InterpreterMacroAssembler::pop_d(XMMRegister r) {
 596   movdbl(r, Address(rsp, 0));
 597   addptr(rsp, 2 * Interpreter::stackElementSize);
 598 }
 599 
 600 #ifdef _LP64
 601 void InterpreterMacroAssembler::pop_i(Register r) {
 602   // XXX can't use pop currently, upper half non clean
 603   movl(r, Address(rsp, 0));
 604   addptr(rsp, wordSize);
 605 }
 606 
 607 void InterpreterMacroAssembler::pop_l(Register r) {
 608   movq(r, Address(rsp, 0));
 609   addptr(rsp, 2 * Interpreter::stackElementSize);
 610 }
 611 
 612 void InterpreterMacroAssembler::push_l(Register r) {
 613   subptr(rsp, 2 * wordSize);
 614   movptr(Address(rsp, Interpreter::expr_offset_in_bytes(0)), r         );
 615   movptr(Address(rsp, Interpreter::expr_offset_in_bytes(1)), NULL_WORD );
 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   testptr(rax, rax);
 965   jcc(Assembler::notZero, unlock);
 966 
 967   pop(state);
 968   if (throw_monitor_exception) {
 969     // Entry already unlocked, need to throw exception
 970     NOT_LP64(empty_FPU_stack();)  // remove possible return value from FPU-stack, otherwise stack could overflow
 971     call_VM(noreg, CAST_FROM_FN_PTR(address,
 972                    InterpreterRuntime::throw_illegal_monitor_state_exception));
 973     should_not_reach_here();
 974   } else {
 975     // Monitor already unlocked during a stack unroll. If requested,
 976     // install an illegal_monitor_state_exception.  Continue with
 977     // stack unrolling.
 978     if (install_monitor_exception) {
 979       NOT_LP64(empty_FPU_stack();)
 980       call_VM(noreg, CAST_FROM_FN_PTR(address,
 981                      InterpreterRuntime::new_illegal_monitor_state_exception));
 982     }
 983     jmp(unlocked);
 984   }
 985 
 986   bind(unlock);
 987   unlock_object(robj);
 988   pop(state);
 989 
 990   // Check that for block-structured locking (i.e., that all locked
 991   // objects has been unlocked)
 992   bind(unlocked);
 993 
 994   // rax, rdx: Might contain return value
 995 
 996   // Check that all monitors are unlocked
 997   {
 998     Label loop, exception, entry, restart;
 999     const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
1000     const Address monitor_block_top(
1001         rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
1002     const Address monitor_block_bot(
1003         rbp, frame::interpreter_frame_initial_sp_offset * wordSize);
1004 
1005     bind(restart);
1006     // We use c_rarg1 so that if we go slow path it will be the correct
1007     // register for unlock_object to pass to VM directly
1008     movptr(rmon, monitor_block_top); // points to current entry, starting
1009                                   // with top-most entry
1010     lea(rbx, monitor_block_bot);  // points to word before bottom of
1011                                   // monitor block
1012     jmp(entry);
1013 
1014     // Entry already locked, need to throw exception
1015     bind(exception);
1016 
1017     if (throw_monitor_exception) {
1018       // Throw exception
1019       NOT_LP64(empty_FPU_stack();)
1020       MacroAssembler::call_VM(noreg,
1021                               CAST_FROM_FN_PTR(address, InterpreterRuntime::
1022                                    throw_illegal_monitor_state_exception));
1023       should_not_reach_here();
1024     } else {
1025       // Stack unrolling. Unlock object and install illegal_monitor_exception.
1026       // Unlock does not block, so don't have to worry about the frame.
1027       // We don't have to preserve c_rarg1 since we are going to throw an exception.
1028 
1029       push(state);
1030       mov(robj, rmon);   // nop if robj and rmon are the same
1031       unlock_object(robj);
1032       pop(state);
1033 
1034       if (install_monitor_exception) {
1035         NOT_LP64(empty_FPU_stack();)
1036         call_VM(noreg, CAST_FROM_FN_PTR(address,
1037                                         InterpreterRuntime::
1038                                         new_illegal_monitor_state_exception));
1039       }
1040 
1041       jmp(restart);
1042     }
1043 
1044     bind(loop);
1045     // check if current entry is used
1046     cmpptr(Address(rmon, BasicObjectLock::obj_offset_in_bytes()), (int32_t) NULL);
1047     jcc(Assembler::notEqual, exception);
1048 
1049     addptr(rmon, entry_size); // otherwise advance to next entry
1050     bind(entry);
1051     cmpptr(rmon, rbx); // check if bottom reached
1052     jcc(Assembler::notEqual, loop); // if not at bottom then check this entry
1053   }
1054 
1055   bind(no_unlock);
1056 
1057   // jvmti support
1058   if (notify_jvmdi) {
1059     notify_method_exit(state, NotifyJVMTI);    // preserve TOSCA
1060   } else {
1061     notify_method_exit(state, SkipNotifyJVMTI); // preserve TOSCA
1062   }
1063 
1064   // remove activation
1065   // get sender sp
1066   movptr(rbx,
1067          Address(rbp, frame::interpreter_frame_sender_sp_offset * wordSize));
1068   if (StackReservedPages > 0) {
1069     // testing if reserved zone needs to be re-enabled
1070     Register rthread = LP64_ONLY(r15_thread) NOT_LP64(rcx);
1071     Label no_reserved_zone_enabling;
1072 
1073     NOT_LP64(get_thread(rthread);)
1074 
1075     cmpl(Address(rthread, JavaThread::stack_guard_state_offset()), JavaThread::stack_guard_enabled);
1076     jcc(Assembler::equal, no_reserved_zone_enabling);
1077 
1078     cmpptr(rbx, Address(rthread, JavaThread::reserved_stack_activation_offset()));
1079     jcc(Assembler::lessEqual, no_reserved_zone_enabling);
1080 
1081     call_VM_leaf(
1082       CAST_FROM_FN_PTR(address, SharedRuntime::enable_stack_reserved_zone), rthread);
1083     push(rthread);
1084     call_VM(noreg, CAST_FROM_FN_PTR(address,
1085                    InterpreterRuntime::throw_delayed_StackOverflowError));
1086     should_not_reach_here();
1087 
1088     bind(no_reserved_zone_enabling);
1089   }
1090   leave();                           // remove frame anchor
1091   pop(ret_addr);                     // get return address
1092   mov(rsp, rbx);                     // set sp to sender sp
1093 }
1094 
1095 void InterpreterMacroAssembler::get_method_counters(Register method,
1096                                                     Register mcs, Label& skip) {
1097   Label has_counters;
1098   movptr(mcs, Address(method, Method::method_counters_offset()));
1099   testptr(mcs, mcs);
1100   jcc(Assembler::notZero, has_counters);
1101   call_VM(noreg, CAST_FROM_FN_PTR(address,
1102           InterpreterRuntime::build_method_counters), method);
1103   movptr(mcs, Address(method,Method::method_counters_offset()));
1104   testptr(mcs, mcs);
1105   jcc(Assembler::zero, skip); // No MethodCounters allocated, OutOfMemory
1106   bind(has_counters);
1107 }
1108 
1109 
1110 // Lock object
1111 //
1112 // Args:
1113 //      rdx, c_rarg1: BasicObjectLock to be used for locking
1114 //
1115 // Kills:
1116 //      rax, rbx
1117 void InterpreterMacroAssembler::lock_object(Register lock_reg) {
1118   assert(lock_reg == LP64_ONLY(c_rarg1) NOT_LP64(rdx),
1119          "The argument is only for looks. It must be c_rarg1");
1120 
1121   if (UseHeavyMonitors) {
1122     call_VM(noreg,
1123             CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter),
1124             lock_reg);
1125   } else {
1126     Label done;
1127 
1128     const Register swap_reg = rax; // Must use rax for cmpxchg instruction
1129     const Register tmp_reg = rbx; // Will be passed to biased_locking_enter to avoid a
1130                                   // problematic case where tmp_reg = no_reg.
1131     const Register obj_reg = LP64_ONLY(c_rarg3) NOT_LP64(rcx); // Will contain the oop
1132 
1133     const int obj_offset = BasicObjectLock::obj_offset_in_bytes();
1134     const int lock_offset = BasicObjectLock::lock_offset_in_bytes ();
1135     const int mark_offset = lock_offset +
1136                             BasicLock::displaced_header_offset_in_bytes();
1137 
1138     Label slow_case;
1139 
1140     // Load object pointer into obj_reg
1141     movptr(obj_reg, Address(lock_reg, obj_offset));
1142 
1143     if (UseBiasedLocking) {
1144       biased_locking_enter(lock_reg, obj_reg, swap_reg, tmp_reg, false, done, &slow_case);
1145     }
1146 
1147     // Load immediate 1 into swap_reg %rax
1148     movl(swap_reg, (int32_t)1);
1149 
1150     // Load (object->mark() | 1) into swap_reg %rax
1151     orptr(swap_reg, Address(obj_reg, 0));
1152 
1153     // Save (object->mark() | 1) into BasicLock's displaced header
1154     movptr(Address(lock_reg, mark_offset), swap_reg);
1155 
1156     assert(lock_offset == 0,
1157            "displaced header must be first word in BasicObjectLock");
1158 
1159     if (os::is_MP()) lock();
1160     cmpxchgptr(lock_reg, Address(obj_reg, 0));
1161     if (PrintBiasedLockingStatistics) {
1162       cond_inc32(Assembler::zero,
1163                  ExternalAddress((address) BiasedLocking::fast_path_entry_count_addr()));
1164     }
1165     jcc(Assembler::zero, done);
1166 
1167     const int zero_bits = LP64_ONLY(7) NOT_LP64(3);
1168 
1169     // Test if the oopMark is an obvious stack pointer, i.e.,
1170     //  1) (mark & zero_bits) == 0, and
1171     //  2) rsp <= mark < mark + os::pagesize()
1172     //
1173     // These 3 tests can be done by evaluating the following
1174     // expression: ((mark - rsp) & (zero_bits - os::vm_page_size())),
1175     // assuming both stack pointer and pagesize have their
1176     // least significant bits clear.
1177     // NOTE: the oopMark is in swap_reg %rax as the result of cmpxchg
1178     subptr(swap_reg, rsp);
1179     andptr(swap_reg, zero_bits - os::vm_page_size());
1180 
1181     // Save the test result, for recursive case, the result is zero
1182     movptr(Address(lock_reg, mark_offset), swap_reg);
1183 
1184     if (PrintBiasedLockingStatistics) {
1185       cond_inc32(Assembler::zero,
1186                  ExternalAddress((address) BiasedLocking::fast_path_entry_count_addr()));
1187     }
1188     jcc(Assembler::zero, done);
1189 
1190     bind(slow_case);
1191 
1192     // Call the runtime routine for slow case
1193     call_VM(noreg,
1194             CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter),
1195             lock_reg);
1196 
1197     bind(done);
1198   }
1199 }
1200 
1201 
1202 // Unlocks an object. Used in monitorexit bytecode and
1203 // remove_activation.  Throws an IllegalMonitorException if object is
1204 // not locked by current thread.
1205 //
1206 // Args:
1207 //      rdx, c_rarg1: BasicObjectLock for lock
1208 //
1209 // Kills:
1210 //      rax
1211 //      c_rarg0, c_rarg1, c_rarg2, c_rarg3, ... (param regs)
1212 //      rscratch1 (scratch reg)
1213 // rax, rbx, rcx, rdx
1214 void InterpreterMacroAssembler::unlock_object(Register lock_reg) {
1215   assert(lock_reg == LP64_ONLY(c_rarg1) NOT_LP64(rdx),
1216          "The argument is only for looks. It must be c_rarg1");
1217 
1218   if (UseHeavyMonitors) {
1219     call_VM(noreg,
1220             CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit),
1221             lock_reg);
1222   } else {
1223     Label done;
1224 
1225     const Register swap_reg   = rax;  // Must use rax for cmpxchg instruction
1226     const Register header_reg = LP64_ONLY(c_rarg2) NOT_LP64(rbx);  // Will contain the old oopMark
1227     const Register obj_reg    = LP64_ONLY(c_rarg3) NOT_LP64(rcx);  // Will contain the oop
1228 
1229     save_bcp(); // Save in case of exception
1230 
1231     // Convert from BasicObjectLock structure to object and BasicLock
1232     // structure Store the BasicLock address into %rax
1233     lea(swap_reg, Address(lock_reg, BasicObjectLock::lock_offset_in_bytes()));
1234 
1235     // Load oop into obj_reg(%c_rarg3)
1236     movptr(obj_reg, Address(lock_reg, BasicObjectLock::obj_offset_in_bytes()));
1237 
1238     // Free entry
1239     movptr(Address(lock_reg, BasicObjectLock::obj_offset_in_bytes()), (int32_t)NULL_WORD);
1240 
1241     if (UseBiasedLocking) {
1242       biased_locking_exit(obj_reg, header_reg, done);
1243     }
1244 
1245     // Load the old header from BasicLock structure
1246     movptr(header_reg, Address(swap_reg,
1247                                BasicLock::displaced_header_offset_in_bytes()));
1248 
1249     // Test for recursion
1250     testptr(header_reg, header_reg);
1251 
1252     // zero for recursive case
1253     jcc(Assembler::zero, done);
1254 
1255     // Atomic swap back the old header
1256     if (os::is_MP()) lock();
1257     cmpxchgptr(header_reg, Address(obj_reg, 0));
1258 
1259     // zero for simple unlock of a stack-lock case
1260     jcc(Assembler::zero, done);
1261 
1262     // Call the runtime routine for slow case.
1263     movptr(Address(lock_reg, BasicObjectLock::obj_offset_in_bytes()),
1264          obj_reg); // restore obj
1265     call_VM(noreg,
1266             CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit),
1267             lock_reg);
1268 
1269     bind(done);
1270 
1271     restore_bcp();
1272   }
1273 }
1274 
1275 void InterpreterMacroAssembler::test_method_data_pointer(Register mdp,
1276                                                          Label& zero_continue) {
1277   assert(ProfileInterpreter, "must be profiling interpreter");
1278   movptr(mdp, Address(rbp, frame::interpreter_frame_mdp_offset * wordSize));
1279   testptr(mdp, mdp);
1280   jcc(Assembler::zero, zero_continue);
1281 }
1282 
1283 
1284 // Set the method data pointer for the current bcp.
1285 void InterpreterMacroAssembler::set_method_data_pointer_for_bcp() {
1286   assert(ProfileInterpreter, "must be profiling interpreter");
1287   Label set_mdp;
1288   push(rax);
1289   push(rbx);
1290 
1291   get_method(rbx);
1292   // Test MDO to avoid the call if it is NULL.
1293   movptr(rax, Address(rbx, in_bytes(Method::method_data_offset())));
1294   testptr(rax, rax);
1295   jcc(Assembler::zero, set_mdp);
1296   // rbx: method
1297   // _bcp_register: bcp
1298   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::bcp_to_di), rbx, _bcp_register);
1299   // rax: mdi
1300   // mdo is guaranteed to be non-zero here, we checked for it before the call.
1301   movptr(rbx, Address(rbx, in_bytes(Method::method_data_offset())));
1302   addptr(rbx, in_bytes(MethodData::data_offset()));
1303   addptr(rax, rbx);
1304   bind(set_mdp);
1305   movptr(Address(rbp, frame::interpreter_frame_mdp_offset * wordSize), rax);
1306   pop(rbx);
1307   pop(rax);
1308 }
1309 
1310 void InterpreterMacroAssembler::verify_method_data_pointer() {
1311   assert(ProfileInterpreter, "must be profiling interpreter");
1312 #ifdef ASSERT
1313   Label verify_continue;
1314   push(rax);
1315   push(rbx);
1316   Register arg3_reg = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
1317   Register arg2_reg = LP64_ONLY(c_rarg2) NOT_LP64(rdx);
1318   push(arg3_reg);
1319   push(arg2_reg);
1320   test_method_data_pointer(arg3_reg, verify_continue); // If mdp is zero, continue
1321   get_method(rbx);
1322 
1323   // If the mdp is valid, it will point to a DataLayout header which is
1324   // consistent with the bcp.  The converse is highly probable also.
1325   load_unsigned_short(arg2_reg,
1326                       Address(arg3_reg, in_bytes(DataLayout::bci_offset())));
1327   addptr(arg2_reg, Address(rbx, Method::const_offset()));
1328   lea(arg2_reg, Address(arg2_reg, ConstMethod::codes_offset()));
1329   cmpptr(arg2_reg, _bcp_register);
1330   jcc(Assembler::equal, verify_continue);
1331   // rbx: method
1332   // _bcp_register: bcp
1333   // c_rarg3: mdp
1334   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::verify_mdp),
1335                rbx, _bcp_register, arg3_reg);
1336   bind(verify_continue);
1337   pop(arg2_reg);
1338   pop(arg3_reg);
1339   pop(rbx);
1340   pop(rax);
1341 #endif // ASSERT
1342 }
1343 
1344 
1345 void InterpreterMacroAssembler::set_mdp_data_at(Register mdp_in,
1346                                                 int constant,
1347                                                 Register value) {
1348   assert(ProfileInterpreter, "must be profiling interpreter");
1349   Address data(mdp_in, constant);
1350   movptr(data, value);
1351 }
1352 
1353 
1354 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in,
1355                                                       int constant,
1356                                                       bool decrement) {
1357   // Counter address
1358   Address data(mdp_in, constant);
1359 
1360   increment_mdp_data_at(data, decrement);
1361 }
1362 
1363 void InterpreterMacroAssembler::increment_mdp_data_at(Address data,
1364                                                       bool decrement) {
1365   assert(ProfileInterpreter, "must be profiling interpreter");
1366   // %%% this does 64bit counters at best it is wasting space
1367   // at worst it is a rare bug when counters overflow
1368 
1369   if (decrement) {
1370     // Decrement the register.  Set condition codes.
1371     addptr(data, (int32_t) -DataLayout::counter_increment);
1372     // If the decrement causes the counter to overflow, stay negative
1373     Label L;
1374     jcc(Assembler::negative, L);
1375     addptr(data, (int32_t) DataLayout::counter_increment);
1376     bind(L);
1377   } else {
1378     assert(DataLayout::counter_increment == 1,
1379            "flow-free idiom only works with 1");
1380     // Increment the register.  Set carry flag.
1381     addptr(data, DataLayout::counter_increment);
1382     // If the increment causes the counter to overflow, pull back by 1.
1383     sbbptr(data, (int32_t)0);
1384   }
1385 }
1386 
1387 
1388 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in,
1389                                                       Register reg,
1390                                                       int constant,
1391                                                       bool decrement) {
1392   Address data(mdp_in, reg, Address::times_1, constant);
1393 
1394   increment_mdp_data_at(data, decrement);
1395 }
1396 
1397 void InterpreterMacroAssembler::set_mdp_flag_at(Register mdp_in,
1398                                                 int flag_byte_constant) {
1399   assert(ProfileInterpreter, "must be profiling interpreter");
1400   int header_offset = in_bytes(DataLayout::header_offset());
1401   int header_bits = DataLayout::flag_mask_to_header_mask(flag_byte_constant);
1402   // Set the flag
1403   orl(Address(mdp_in, header_offset), header_bits);
1404 }
1405 
1406 
1407 
1408 void InterpreterMacroAssembler::test_mdp_data_at(Register mdp_in,
1409                                                  int offset,
1410                                                  Register value,
1411                                                  Register test_value_out,
1412                                                  Label& not_equal_continue) {
1413   assert(ProfileInterpreter, "must be profiling interpreter");
1414   if (test_value_out == noreg) {
1415     cmpptr(value, Address(mdp_in, offset));
1416   } else {
1417     // Put the test value into a register, so caller can use it:
1418     movptr(test_value_out, Address(mdp_in, offset));
1419     cmpptr(test_value_out, value);
1420   }
1421   jcc(Assembler::notEqual, not_equal_continue);
1422 }
1423 
1424 
1425 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in,
1426                                                      int offset_of_disp) {
1427   assert(ProfileInterpreter, "must be profiling interpreter");
1428   Address disp_address(mdp_in, offset_of_disp);
1429   addptr(mdp_in, disp_address);
1430   movptr(Address(rbp, frame::interpreter_frame_mdp_offset * wordSize), mdp_in);
1431 }
1432 
1433 
1434 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in,
1435                                                      Register reg,
1436                                                      int offset_of_disp) {
1437   assert(ProfileInterpreter, "must be profiling interpreter");
1438   Address disp_address(mdp_in, reg, Address::times_1, offset_of_disp);
1439   addptr(mdp_in, disp_address);
1440   movptr(Address(rbp, frame::interpreter_frame_mdp_offset * wordSize), mdp_in);
1441 }
1442 
1443 
1444 void InterpreterMacroAssembler::update_mdp_by_constant(Register mdp_in,
1445                                                        int constant) {
1446   assert(ProfileInterpreter, "must be profiling interpreter");
1447   addptr(mdp_in, constant);
1448   movptr(Address(rbp, frame::interpreter_frame_mdp_offset * wordSize), mdp_in);
1449 }
1450 
1451 
1452 void InterpreterMacroAssembler::update_mdp_for_ret(Register return_bci) {
1453   assert(ProfileInterpreter, "must be profiling interpreter");
1454   push(return_bci); // save/restore across call_VM
1455   call_VM(noreg,
1456           CAST_FROM_FN_PTR(address, InterpreterRuntime::update_mdp_for_ret),
1457           return_bci);
1458   pop(return_bci);
1459 }
1460 
1461 
1462 void InterpreterMacroAssembler::profile_taken_branch(Register mdp,
1463                                                      Register bumped_count) {
1464   if (ProfileInterpreter) {
1465     Label profile_continue;
1466 
1467     // If no method data exists, go to profile_continue.
1468     // Otherwise, assign to mdp
1469     test_method_data_pointer(mdp, profile_continue);
1470 
1471     // We are taking a branch.  Increment the taken count.
1472     // We inline increment_mdp_data_at to return bumped_count in a register
1473     //increment_mdp_data_at(mdp, in_bytes(JumpData::taken_offset()));
1474     Address data(mdp, in_bytes(JumpData::taken_offset()));
1475     movptr(bumped_count, data);
1476     assert(DataLayout::counter_increment == 1,
1477             "flow-free idiom only works with 1");
1478     addptr(bumped_count, DataLayout::counter_increment);
1479     sbbptr(bumped_count, 0);
1480     movptr(data, bumped_count); // Store back out
1481 
1482     // The method data pointer needs to be updated to reflect the new target.
1483     update_mdp_by_offset(mdp, in_bytes(JumpData::displacement_offset()));
1484     bind(profile_continue);
1485   }
1486 }
1487 
1488 
1489 void InterpreterMacroAssembler::profile_not_taken_branch(Register mdp) {
1490   if (ProfileInterpreter) {
1491     Label profile_continue;
1492 
1493     // If no method data exists, go to profile_continue.
1494     test_method_data_pointer(mdp, profile_continue);
1495 
1496     // We are taking a branch.  Increment the not taken count.
1497     increment_mdp_data_at(mdp, in_bytes(BranchData::not_taken_offset()));
1498 
1499     // The method data pointer needs to be updated to correspond to
1500     // the next bytecode
1501     update_mdp_by_constant(mdp, in_bytes(BranchData::branch_data_size()));
1502     bind(profile_continue);
1503   }
1504 }
1505 
1506 void InterpreterMacroAssembler::profile_call(Register mdp) {
1507   if (ProfileInterpreter) {
1508     Label profile_continue;
1509 
1510     // If no method data exists, go to profile_continue.
1511     test_method_data_pointer(mdp, profile_continue);
1512 
1513     // We are making a call.  Increment the count.
1514     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1515 
1516     // The method data pointer needs to be updated to reflect the new target.
1517     update_mdp_by_constant(mdp, in_bytes(CounterData::counter_data_size()));
1518     bind(profile_continue);
1519   }
1520 }
1521 
1522 
1523 void InterpreterMacroAssembler::profile_final_call(Register mdp) {
1524   if (ProfileInterpreter) {
1525     Label profile_continue;
1526 
1527     // If no method data exists, go to profile_continue.
1528     test_method_data_pointer(mdp, profile_continue);
1529 
1530     // We are making a call.  Increment the count.
1531     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1532 
1533     // The method data pointer needs to be updated to reflect the new target.
1534     update_mdp_by_constant(mdp,
1535                            in_bytes(VirtualCallData::
1536                                     virtual_call_data_size()));
1537     bind(profile_continue);
1538   }
1539 }
1540 
1541 
1542 void InterpreterMacroAssembler::profile_virtual_call(Register receiver,
1543                                                      Register mdp,
1544                                                      Register reg2,
1545                                                      bool receiver_can_be_null) {
1546   if (ProfileInterpreter) {
1547     Label profile_continue;
1548 
1549     // If no method data exists, go to profile_continue.
1550     test_method_data_pointer(mdp, profile_continue);
1551 
1552     Label skip_receiver_profile;
1553     if (receiver_can_be_null) {
1554       Label not_null;
1555       testptr(receiver, receiver);
1556       jccb(Assembler::notZero, not_null);
1557       // We are making a call.  Increment the count for null receiver.
1558       increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1559       jmp(skip_receiver_profile);
1560       bind(not_null);
1561     }
1562 
1563     // Record the receiver type.
1564     record_klass_in_profile(receiver, mdp, reg2, true);
1565     bind(skip_receiver_profile);
1566 
1567     // The method data pointer needs to be updated to reflect the new target.
1568 #if INCLUDE_JVMCI
1569     if (MethodProfileWidth == 0) {
1570       update_mdp_by_constant(mdp, in_bytes(VirtualCallData::virtual_call_data_size()));
1571     }
1572 #else // INCLUDE_JVMCI
1573     update_mdp_by_constant(mdp,
1574                            in_bytes(VirtualCallData::
1575                                     virtual_call_data_size()));
1576 #endif // INCLUDE_JVMCI
1577     bind(profile_continue);
1578   }
1579 }
1580 
1581 #if INCLUDE_JVMCI
1582 void InterpreterMacroAssembler::profile_called_method(Register method, Register mdp, Register reg2) {
1583   assert_different_registers(method, mdp, reg2);
1584   if (ProfileInterpreter && MethodProfileWidth > 0) {
1585     Label profile_continue;
1586 
1587     // If no method data exists, go to profile_continue.
1588     test_method_data_pointer(mdp, profile_continue);
1589 
1590     Label done;
1591     record_item_in_profile_helper(method, mdp, reg2, 0, done, MethodProfileWidth,
1592       &VirtualCallData::method_offset, &VirtualCallData::method_count_offset, in_bytes(VirtualCallData::nonprofiled_receiver_count_offset()));
1593     bind(done);
1594 
1595     update_mdp_by_constant(mdp, in_bytes(VirtualCallData::virtual_call_data_size()));
1596     bind(profile_continue);
1597   }
1598 }
1599 #endif // INCLUDE_JVMCI
1600 
1601 // This routine creates a state machine for updating the multi-row
1602 // type profile at a virtual call site (or other type-sensitive bytecode).
1603 // The machine visits each row (of receiver/count) until the receiver type
1604 // is found, or until it runs out of rows.  At the same time, it remembers
1605 // the location of the first empty row.  (An empty row records null for its
1606 // receiver, and can be allocated for a newly-observed receiver type.)
1607 // Because there are two degrees of freedom in the state, a simple linear
1608 // search will not work; it must be a decision tree.  Hence this helper
1609 // function is recursive, to generate the required tree structured code.
1610 // It's the interpreter, so we are trading off code space for speed.
1611 // See below for example code.
1612 void InterpreterMacroAssembler::record_klass_in_profile_helper(
1613                                         Register receiver, Register mdp,
1614                                         Register reg2, int start_row,
1615                                         Label& done, bool is_virtual_call) {
1616   if (TypeProfileWidth == 0) {
1617     if (is_virtual_call) {
1618       increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1619     }
1620 #if INCLUDE_JVMCI
1621     else if (EnableJVMCI) {
1622       increment_mdp_data_at(mdp, in_bytes(ReceiverTypeData::nonprofiled_receiver_count_offset()));
1623     }
1624 #endif // INCLUDE_JVMCI
1625   } else {
1626     int non_profiled_offset = -1;
1627     if (is_virtual_call) {
1628       non_profiled_offset = in_bytes(CounterData::count_offset());
1629     }
1630 #if INCLUDE_JVMCI
1631     else if (EnableJVMCI) {
1632       non_profiled_offset = in_bytes(ReceiverTypeData::nonprofiled_receiver_count_offset());
1633     }
1634 #endif // INCLUDE_JVMCI
1635 
1636     record_item_in_profile_helper(receiver, mdp, reg2, 0, done, TypeProfileWidth,
1637         &VirtualCallData::receiver_offset, &VirtualCallData::receiver_count_offset, non_profiled_offset);
1638   }
1639 }
1640 
1641 void InterpreterMacroAssembler::record_item_in_profile_helper(Register item, Register mdp,
1642                                         Register reg2, int start_row, Label& done, int total_rows,
1643                                         OffsetFunction item_offset_fn, OffsetFunction item_count_offset_fn,
1644                                         int non_profiled_offset) {
1645   int last_row = total_rows - 1;
1646   assert(start_row <= last_row, "must be work left to do");
1647   // Test this row for both the item and for null.
1648   // Take any of three different outcomes:
1649   //   1. found item => increment count and goto done
1650   //   2. found null => keep looking for case 1, maybe allocate this cell
1651   //   3. found something else => keep looking for cases 1 and 2
1652   // Case 3 is handled by a recursive call.
1653   for (int row = start_row; row <= last_row; row++) {
1654     Label next_test;
1655     bool test_for_null_also = (row == start_row);
1656 
1657     // See if the item is item[n].
1658     int item_offset = in_bytes(item_offset_fn(row));
1659     test_mdp_data_at(mdp, item_offset, item,
1660                      (test_for_null_also ? reg2 : noreg),
1661                      next_test);
1662     // (Reg2 now contains the item from the CallData.)
1663 
1664     // The item is item[n].  Increment count[n].
1665     int count_offset = in_bytes(item_count_offset_fn(row));
1666     increment_mdp_data_at(mdp, count_offset);
1667     jmp(done);
1668     bind(next_test);
1669 
1670     if (test_for_null_also) {
1671       Label found_null;
1672       // Failed the equality check on item[n]...  Test for null.
1673       testptr(reg2, reg2);
1674       if (start_row == last_row) {
1675         // The only thing left to do is handle the null case.
1676         if (non_profiled_offset >= 0) {
1677           jccb(Assembler::zero, found_null);
1678           // Item did not match any saved item and there is no empty row for it.
1679           // Increment total counter to indicate polymorphic case.
1680           increment_mdp_data_at(mdp, non_profiled_offset);
1681           jmp(done);
1682           bind(found_null);
1683         } else {
1684           jcc(Assembler::notZero, done);
1685         }
1686         break;
1687       }
1688       // Since null is rare, make it be the branch-taken case.
1689       jcc(Assembler::zero, found_null);
1690 
1691       // Put all the "Case 3" tests here.
1692       record_item_in_profile_helper(item, mdp, reg2, start_row + 1, done, total_rows,
1693         item_offset_fn, item_count_offset_fn, non_profiled_offset);
1694 
1695       // Found a null.  Keep searching for a matching item,
1696       // but remember that this is an empty (unused) slot.
1697       bind(found_null);
1698     }
1699   }
1700 
1701   // In the fall-through case, we found no matching item, but we
1702   // observed the item[start_row] is NULL.
1703 
1704   // Fill in the item field and increment the count.
1705   int item_offset = in_bytes(item_offset_fn(start_row));
1706   set_mdp_data_at(mdp, item_offset, item);
1707   int count_offset = in_bytes(item_count_offset_fn(start_row));
1708   movl(reg2, DataLayout::counter_increment);
1709   set_mdp_data_at(mdp, count_offset, reg2);
1710   if (start_row > 0) {
1711     jmp(done);
1712   }
1713 }
1714 
1715 // Example state machine code for three profile rows:
1716 //   // main copy of decision tree, rooted at row[1]
1717 //   if (row[0].rec == rec) { row[0].incr(); goto done; }
1718 //   if (row[0].rec != NULL) {
1719 //     // inner copy of decision tree, rooted at row[1]
1720 //     if (row[1].rec == rec) { row[1].incr(); goto done; }
1721 //     if (row[1].rec != NULL) {
1722 //       // degenerate decision tree, rooted at row[2]
1723 //       if (row[2].rec == rec) { row[2].incr(); goto done; }
1724 //       if (row[2].rec != NULL) { count.incr(); goto done; } // overflow
1725 //       row[2].init(rec); goto done;
1726 //     } else {
1727 //       // remember row[1] is empty
1728 //       if (row[2].rec == rec) { row[2].incr(); goto done; }
1729 //       row[1].init(rec); goto done;
1730 //     }
1731 //   } else {
1732 //     // remember row[0] is empty
1733 //     if (row[1].rec == rec) { row[1].incr(); goto done; }
1734 //     if (row[2].rec == rec) { row[2].incr(); goto done; }
1735 //     row[0].init(rec); goto done;
1736 //   }
1737 //   done:
1738 
1739 void InterpreterMacroAssembler::record_klass_in_profile(Register receiver,
1740                                                         Register mdp, Register reg2,
1741                                                         bool is_virtual_call) {
1742   assert(ProfileInterpreter, "must be profiling");
1743   Label done;
1744 
1745   record_klass_in_profile_helper(receiver, mdp, reg2, 0, done, is_virtual_call);
1746 
1747   bind (done);
1748 }
1749 
1750 void InterpreterMacroAssembler::profile_ret(Register return_bci,
1751                                             Register mdp) {
1752   if (ProfileInterpreter) {
1753     Label profile_continue;
1754     uint row;
1755 
1756     // If no method data exists, go to profile_continue.
1757     test_method_data_pointer(mdp, profile_continue);
1758 
1759     // Update the total ret count.
1760     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1761 
1762     for (row = 0; row < RetData::row_limit(); row++) {
1763       Label next_test;
1764 
1765       // See if return_bci is equal to bci[n]:
1766       test_mdp_data_at(mdp,
1767                        in_bytes(RetData::bci_offset(row)),
1768                        return_bci, noreg,
1769                        next_test);
1770 
1771       // return_bci is equal to bci[n].  Increment the count.
1772       increment_mdp_data_at(mdp, in_bytes(RetData::bci_count_offset(row)));
1773 
1774       // The method data pointer needs to be updated to reflect the new target.
1775       update_mdp_by_offset(mdp,
1776                            in_bytes(RetData::bci_displacement_offset(row)));
1777       jmp(profile_continue);
1778       bind(next_test);
1779     }
1780 
1781     update_mdp_for_ret(return_bci);
1782 
1783     bind(profile_continue);
1784   }
1785 }
1786 
1787 
1788 void InterpreterMacroAssembler::profile_null_seen(Register mdp) {
1789   if (ProfileInterpreter) {
1790     Label profile_continue;
1791 
1792     // If no method data exists, go to profile_continue.
1793     test_method_data_pointer(mdp, profile_continue);
1794 
1795     set_mdp_flag_at(mdp, BitData::null_seen_byte_constant());
1796 
1797     // The method data pointer needs to be updated.
1798     int mdp_delta = in_bytes(BitData::bit_data_size());
1799     if (TypeProfileCasts) {
1800       mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1801     }
1802     update_mdp_by_constant(mdp, mdp_delta);
1803 
1804     bind(profile_continue);
1805   }
1806 }
1807 
1808 
1809 void InterpreterMacroAssembler::profile_typecheck_failed(Register mdp) {
1810   if (ProfileInterpreter && TypeProfileCasts) {
1811     Label profile_continue;
1812 
1813     // If no method data exists, go to profile_continue.
1814     test_method_data_pointer(mdp, profile_continue);
1815 
1816     int count_offset = in_bytes(CounterData::count_offset());
1817     // Back up the address, since we have already bumped the mdp.
1818     count_offset -= in_bytes(VirtualCallData::virtual_call_data_size());
1819 
1820     // *Decrement* the counter.  We expect to see zero or small negatives.
1821     increment_mdp_data_at(mdp, count_offset, true);
1822 
1823     bind (profile_continue);
1824   }
1825 }
1826 
1827 
1828 void InterpreterMacroAssembler::profile_typecheck(Register mdp, Register klass, Register reg2) {
1829   if (ProfileInterpreter) {
1830     Label profile_continue;
1831 
1832     // If no method data exists, go to profile_continue.
1833     test_method_data_pointer(mdp, profile_continue);
1834 
1835     // The method data pointer needs to be updated.
1836     int mdp_delta = in_bytes(BitData::bit_data_size());
1837     if (TypeProfileCasts) {
1838       mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1839 
1840       // Record the object type.
1841       record_klass_in_profile(klass, mdp, reg2, false);
1842       NOT_LP64(assert(reg2 == rdi, "we know how to fix this blown reg");)
1843       NOT_LP64(restore_locals();)         // Restore EDI
1844     }
1845     update_mdp_by_constant(mdp, mdp_delta);
1846 
1847     bind(profile_continue);
1848   }
1849 }
1850 
1851 
1852 void InterpreterMacroAssembler::profile_switch_default(Register mdp) {
1853   if (ProfileInterpreter) {
1854     Label profile_continue;
1855 
1856     // If no method data exists, go to profile_continue.
1857     test_method_data_pointer(mdp, profile_continue);
1858 
1859     // Update the default case count
1860     increment_mdp_data_at(mdp,
1861                           in_bytes(MultiBranchData::default_count_offset()));
1862 
1863     // The method data pointer needs to be updated.
1864     update_mdp_by_offset(mdp,
1865                          in_bytes(MultiBranchData::
1866                                   default_displacement_offset()));
1867 
1868     bind(profile_continue);
1869   }
1870 }
1871 
1872 
1873 void InterpreterMacroAssembler::profile_switch_case(Register index,
1874                                                     Register mdp,
1875                                                     Register reg2) {
1876   if (ProfileInterpreter) {
1877     Label profile_continue;
1878 
1879     // If no method data exists, go to profile_continue.
1880     test_method_data_pointer(mdp, profile_continue);
1881 
1882     // Build the base (index * per_case_size_in_bytes()) +
1883     // case_array_offset_in_bytes()
1884     movl(reg2, in_bytes(MultiBranchData::per_case_size()));
1885     imulptr(index, reg2); // XXX l ?
1886     addptr(index, in_bytes(MultiBranchData::case_array_offset())); // XXX l ?
1887 
1888     // Update the case count
1889     increment_mdp_data_at(mdp,
1890                           index,
1891                           in_bytes(MultiBranchData::relative_count_offset()));
1892 
1893     // The method data pointer needs to be updated.
1894     update_mdp_by_offset(mdp,
1895                          index,
1896                          in_bytes(MultiBranchData::
1897                                   relative_displacement_offset()));
1898 
1899     bind(profile_continue);
1900   }
1901 }
1902 
1903 
1904 
1905 void InterpreterMacroAssembler::verify_oop(Register reg, TosState state) {
1906   if (state == atos) {
1907     MacroAssembler::verify_oop(reg);
1908   }
1909 }
1910 
1911 void InterpreterMacroAssembler::verify_FPU(int stack_depth, TosState state) {
1912 #ifndef _LP64
1913   if ((state == ftos && UseSSE < 1) ||
1914       (state == dtos && UseSSE < 2)) {
1915     MacroAssembler::verify_FPU(stack_depth);
1916   }
1917 #endif
1918 }
1919 
1920 // Jump if ((*counter_addr += increment) & mask) satisfies the condition.
1921 void InterpreterMacroAssembler::increment_mask_and_jump(Address counter_addr,
1922                                                         int increment, Address mask,
1923                                                         Register scratch, bool preloaded,
1924                                                         Condition cond, Label* where) {
1925   if (!preloaded) {
1926     movl(scratch, counter_addr);
1927   }
1928   incrementl(scratch, increment);
1929   movl(counter_addr, scratch);
1930   andl(scratch, mask);
1931   jcc(cond, *where);
1932 }
1933 
1934 void InterpreterMacroAssembler::notify_method_entry() {
1935   // Whenever JVMTI is interp_only_mode, method entry/exit events are sent to
1936   // track stack depth.  If it is possible to enter interp_only_mode we add
1937   // the code to check if the event should be sent.
1938   Register rthread = LP64_ONLY(r15_thread) NOT_LP64(rcx);
1939   Register rarg = LP64_ONLY(c_rarg1) NOT_LP64(rbx);
1940   if (JvmtiExport::can_post_interpreter_events()) {
1941     Label L;
1942     NOT_LP64(get_thread(rthread);)
1943     movl(rdx, Address(rthread, JavaThread::interp_only_mode_offset()));
1944     testl(rdx, rdx);
1945     jcc(Assembler::zero, L);
1946     call_VM(noreg, CAST_FROM_FN_PTR(address,
1947                                     InterpreterRuntime::post_method_entry));
1948     bind(L);
1949   }
1950 
1951   {
1952     SkipIfEqual skip(this, &DTraceMethodProbes, false);
1953     NOT_LP64(get_thread(rthread);)
1954     get_method(rarg);
1955     call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry),
1956                  rthread, rarg);
1957   }
1958 
1959   // RedefineClasses() tracing support for obsolete method entry
1960   if (log_is_enabled(Trace, redefine, class, obsolete)) {
1961     NOT_LP64(get_thread(rthread);)
1962     get_method(rarg);
1963     call_VM_leaf(
1964       CAST_FROM_FN_PTR(address, SharedRuntime::rc_trace_method_entry),
1965       rthread, rarg);
1966   }
1967 }
1968 
1969 
1970 void InterpreterMacroAssembler::notify_method_exit(
1971     TosState state, NotifyMethodExitMode mode) {
1972   // Whenever JVMTI is interp_only_mode, method entry/exit events are sent to
1973   // track stack depth.  If it is possible to enter interp_only_mode we add
1974   // the code to check if the event should be sent.
1975   Register rthread = LP64_ONLY(r15_thread) NOT_LP64(rcx);
1976   Register rarg = LP64_ONLY(c_rarg1) NOT_LP64(rbx);
1977   if (mode == NotifyJVMTI && JvmtiExport::can_post_interpreter_events()) {
1978     Label L;
1979     // Note: frame::interpreter_frame_result has a dependency on how the
1980     // method result is saved across the call to post_method_exit. If this
1981     // is changed then the interpreter_frame_result implementation will
1982     // need to be updated too.
1983 
1984     // template interpreter will leave the result on the top of the stack.
1985     push(state);
1986     NOT_LP64(get_thread(rthread);)
1987     movl(rdx, Address(rthread, JavaThread::interp_only_mode_offset()));
1988     testl(rdx, rdx);
1989     jcc(Assembler::zero, L);
1990     call_VM(noreg,
1991             CAST_FROM_FN_PTR(address, InterpreterRuntime::post_method_exit));
1992     bind(L);
1993     pop(state);
1994   }
1995 
1996   {
1997     SkipIfEqual skip(this, &DTraceMethodProbes, false);
1998     push(state);
1999     NOT_LP64(get_thread(rthread);)
2000     get_method(rarg);
2001     call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit),
2002                  rthread, rarg);
2003     pop(state);
2004   }
2005 }