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