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