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: load_float(val_addr);                break;
 359     case dtos: load_double(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: 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::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 void InterpreterMacroAssembler::push_f(XMMRegister r) {
 582   subptr(rsp, wordSize);
 583   movflt(Address(rsp, 0), r);
 584 }
 585 
 586 void InterpreterMacroAssembler::pop_f(XMMRegister r) {
 587   movflt(r, Address(rsp, 0));
 588   addptr(rsp, wordSize);
 589 }
 590 
 591 void InterpreterMacroAssembler::push_d(XMMRegister r) {
 592   subptr(rsp, 2 * wordSize);
 593   movdbl(Address(rsp, 0), r);
 594 }
 595 
 596 void InterpreterMacroAssembler::pop_d(XMMRegister r) {
 597   movdbl(r, Address(rsp, 0));
 598   addptr(rsp, 2 * Interpreter::stackElementSize);
 599 }
 600 
 601 #ifdef _LP64
 602 void InterpreterMacroAssembler::pop_i(Register r) {
 603   // XXX can't use pop currently, upper half non clean
 604   movl(r, Address(rsp, 0));
 605   addptr(rsp, wordSize);
 606 }
 607 
 608 void InterpreterMacroAssembler::pop_l(Register r) {
 609   movq(r, Address(rsp, 0));
 610   addptr(rsp, 2 * Interpreter::stackElementSize);
 611 }
 612 
 613 void InterpreterMacroAssembler::push_l(Register r) {
 614   subptr(rsp, 2 * wordSize);
 615   movq(Address(rsp, 0), r);
 616 }
 617 
 618 void InterpreterMacroAssembler::pop(TosState state) {
 619   switch (state) {
 620   case atos: pop_ptr();                 break;
 621   case btos:
 622   case ctos:
 623   case stos:
 624   case itos: pop_i();                   break;
 625   case ltos: pop_l();                   break;
 626   case ftos: pop_f(xmm0);               break;
 627   case dtos: pop_d(xmm0);               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(xmm0);              break;
 644   case dtos: push_d(xmm0);              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:
 679       if (UseSSE >= 1) {
 680         pop_f(xmm0);
 681       } else {
 682         pop_f();
 683       }
 684       break;
 685     case dtos:
 686       if (UseSSE >= 2) {
 687         pop_d(xmm0);
 688       } else {
 689         pop_d();
 690       }
 691       break;
 692     case vtos: /* nothing to do */                           break;
 693     default  : ShouldNotReachHere();
 694   }
 695   verify_oop(rax, state);
 696 }
 697 
 698 
 699 void InterpreterMacroAssembler::push_l(Register lo, Register hi) {
 700   push(hi);
 701   push(lo);
 702 }
 703 
 704 void InterpreterMacroAssembler::push_f() {
 705   // Do not schedule for no AGI! Never write beyond rsp!
 706   subptr(rsp, 1 * wordSize);
 707   fstp_s(Address(rsp, 0));
 708 }
 709 
 710 void InterpreterMacroAssembler::push_d() {
 711   // Do not schedule for no AGI! Never write beyond rsp!
 712   subptr(rsp, 2 * wordSize);
 713   fstp_d(Address(rsp, 0));
 714 }
 715 
 716 
 717 void InterpreterMacroAssembler::push(TosState state) {
 718   verify_oop(rax, state);
 719   switch (state) {
 720     case atos: push_ptr(rax); break;
 721     case btos:                                               // fall through
 722     case ctos:                                               // fall through
 723     case stos:                                               // fall through
 724     case itos: push_i(rax);                                    break;
 725     case ltos: push_l(rax, rdx);                               break;
 726     case ftos:
 727       if (UseSSE >= 1) {
 728         push_f(xmm0);
 729       } else {
 730         push_f();
 731       }
 732       break;
 733     case dtos:
 734       if (UseSSE >= 2) {
 735         push_d(xmm0);
 736       } else {
 737         push_d();
 738       }
 739       break;
 740     case vtos: /* nothing to do */                             break;
 741     default  : ShouldNotReachHere();
 742   }
 743 }
 744 #endif // _LP64
 745 
 746 
 747 // Helpers for swap and dup
 748 void InterpreterMacroAssembler::load_ptr(int n, Register val) {
 749   movptr(val, Address(rsp, Interpreter::expr_offset_in_bytes(n)));
 750 }
 751 
 752 void InterpreterMacroAssembler::store_ptr(int n, Register val) {
 753   movptr(Address(rsp, Interpreter::expr_offset_in_bytes(n)), val);
 754 }
 755 
 756 
 757 void InterpreterMacroAssembler::prepare_to_jump_from_interpreted() {
 758   // set sender sp
 759   lea(_bcp_register, Address(rsp, wordSize));
 760   // record last_sp
 761   movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), _bcp_register);
 762 }
 763 
 764 
 765 // Jump to from_interpreted entry of a call unless single stepping is possible
 766 // in this thread in which case we must call the i2i entry
 767 void InterpreterMacroAssembler::jump_from_interpreted(Register method, Register temp) {
 768   prepare_to_jump_from_interpreted();
 769 
 770   if (JvmtiExport::can_post_interpreter_events()) {
 771     Label run_compiled_code;
 772     // JVMTI events, such as single-stepping, are implemented partly by avoiding running
 773     // compiled code in threads for which the event is enabled.  Check here for
 774     // interp_only_mode if these events CAN be enabled.
 775     // interp_only is an int, on little endian it is sufficient to test the byte only
 776     // Is a cmpl faster?
 777     LP64_ONLY(temp = r15_thread;)
 778     NOT_LP64(get_thread(temp);)
 779     cmpb(Address(temp, JavaThread::interp_only_mode_offset()), 0);
 780     jccb(Assembler::zero, run_compiled_code);
 781     jmp(Address(method, Method::interpreter_entry_offset()));
 782     bind(run_compiled_code);
 783   }
 784 
 785   jmp(Address(method, Method::from_interpreted_offset()));
 786 }
 787 
 788 // The following two routines provide a hook so that an implementation
 789 // can schedule the dispatch in two parts.  x86 does not do this.
 790 void InterpreterMacroAssembler::dispatch_prolog(TosState state, int step) {
 791   // Nothing x86 specific to be done here
 792 }
 793 
 794 void InterpreterMacroAssembler::dispatch_epilog(TosState state, int step) {
 795   dispatch_next(state, step);
 796 }
 797 
 798 void InterpreterMacroAssembler::dispatch_base(TosState state,
 799                                               address* table,
 800                                               bool verifyoop) {
 801   verify_FPU(1, state);
 802   if (VerifyActivationFrameSize) {
 803     Label L;
 804     mov(rcx, rbp);
 805     subptr(rcx, rsp);
 806     int32_t min_frame_size =
 807       (frame::link_offset - frame::interpreter_frame_initial_sp_offset) *
 808       wordSize;
 809     cmpptr(rcx, (int32_t)min_frame_size);
 810     jcc(Assembler::greaterEqual, L);
 811     stop("broken stack frame");
 812     bind(L);
 813   }
 814   if (verifyoop) {
 815     verify_oop(rax, state);
 816   }
 817 #ifdef _LP64
 818   lea(rscratch1, ExternalAddress((address)table));
 819   jmp(Address(rscratch1, rbx, Address::times_8));
 820 #else
 821   Address index(noreg, rbx, Address::times_ptr);
 822   ExternalAddress tbl((address)table);
 823   ArrayAddress dispatch(tbl, index);
 824   jump(dispatch);
 825 #endif // _LP64
 826 }
 827 
 828 void InterpreterMacroAssembler::dispatch_only(TosState state) {
 829   dispatch_base(state, Interpreter::dispatch_table(state));
 830 }
 831 
 832 void InterpreterMacroAssembler::dispatch_only_normal(TosState state) {
 833   dispatch_base(state, Interpreter::normal_table(state));
 834 }
 835 
 836 void InterpreterMacroAssembler::dispatch_only_noverify(TosState state) {
 837   dispatch_base(state, Interpreter::normal_table(state), false);
 838 }
 839 
 840 
 841 void InterpreterMacroAssembler::dispatch_next(TosState state, int step) {
 842   // load next bytecode (load before advancing _bcp_register to prevent AGI)
 843   load_unsigned_byte(rbx, Address(_bcp_register, step));
 844   // advance _bcp_register
 845   increment(_bcp_register, step);
 846   dispatch_base(state, Interpreter::dispatch_table(state));
 847 }
 848 
 849 void InterpreterMacroAssembler::dispatch_via(TosState state, address* table) {
 850   // load current bytecode
 851   load_unsigned_byte(rbx, Address(_bcp_register, 0));
 852   dispatch_base(state, table);
 853 }
 854 
 855 // remove activation
 856 //
 857 // Unlock the receiver if this is a synchronized method.
 858 // Unlock any Java monitors from syncronized blocks.
 859 // Remove the activation from the stack.
 860 //
 861 // If there are locked Java monitors
 862 //    If throw_monitor_exception
 863 //       throws IllegalMonitorStateException
 864 //    Else if install_monitor_exception
 865 //       installs IllegalMonitorStateException
 866 //    Else
 867 //       no error processing
 868 void InterpreterMacroAssembler::remove_activation(
 869         TosState state,
 870         Register ret_addr,
 871         bool throw_monitor_exception,
 872         bool install_monitor_exception,
 873         bool notify_jvmdi) {
 874   // Note: Registers rdx xmm0 may be in use for the
 875   // result check if synchronized method
 876   Label unlocked, unlock, no_unlock;
 877 
 878   const Register rthread = LP64_ONLY(r15_thread) NOT_LP64(rcx);
 879   const Register robj    = LP64_ONLY(c_rarg1) NOT_LP64(rdx);
 880   const Register rmon    = LP64_ONLY(c_rarg1) NOT_LP64(rcx);
 881                               // monitor pointers need different register
 882                               // because rdx may have the result in it
 883   NOT_LP64(get_thread(rcx);)
 884 
 885   // get the value of _do_not_unlock_if_synchronized into rdx
 886   const Address do_not_unlock_if_synchronized(rthread,
 887     in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
 888   movbool(rbx, do_not_unlock_if_synchronized);
 889   movbool(do_not_unlock_if_synchronized, false); // reset the flag
 890 
 891  // get method access flags
 892   movptr(rcx, Address(rbp, frame::interpreter_frame_method_offset * wordSize));
 893   movl(rcx, Address(rcx, Method::access_flags_offset()));
 894   testl(rcx, JVM_ACC_SYNCHRONIZED);
 895   jcc(Assembler::zero, unlocked);
 896 
 897   // Don't unlock anything if the _do_not_unlock_if_synchronized flag
 898   // is set.
 899   testbool(rbx);
 900   jcc(Assembler::notZero, no_unlock);
 901 
 902   // unlock monitor
 903   push(state); // save result
 904 
 905   // BasicObjectLock will be first in list, since this is a
 906   // synchronized method. However, need to check that the object has
 907   // not been unlocked by an explicit monitorexit bytecode.
 908   const Address monitor(rbp, frame::interpreter_frame_initial_sp_offset *
 909                         wordSize - (int) sizeof(BasicObjectLock));
 910   // We use c_rarg1/rdx so that if we go slow path it will be the correct
 911   // register for unlock_object to pass to VM directly
 912   lea(robj, monitor); // address of first monitor
 913 
 914   movptr(rax, Address(robj, BasicObjectLock::obj_offset_in_bytes()));
 915   testptr(rax, rax);
 916   jcc(Assembler::notZero, unlock);
 917 
 918   pop(state);
 919   if (throw_monitor_exception) {
 920     // Entry already unlocked, need to throw exception
 921     NOT_LP64(empty_FPU_stack();)  // remove possible return value from FPU-stack, otherwise stack could overflow
 922     call_VM(noreg, CAST_FROM_FN_PTR(address,
 923                    InterpreterRuntime::throw_illegal_monitor_state_exception));
 924     should_not_reach_here();
 925   } else {
 926     // Monitor already unlocked during a stack unroll. If requested,
 927     // install an illegal_monitor_state_exception.  Continue with
 928     // stack unrolling.
 929     if (install_monitor_exception) {
 930       NOT_LP64(empty_FPU_stack();)
 931       call_VM(noreg, CAST_FROM_FN_PTR(address,
 932                      InterpreterRuntime::new_illegal_monitor_state_exception));
 933     }
 934     jmp(unlocked);
 935   }
 936 
 937   bind(unlock);
 938   unlock_object(robj);
 939   pop(state);
 940 
 941   // Check that for block-structured locking (i.e., that all locked
 942   // objects has been unlocked)
 943   bind(unlocked);
 944 
 945   // rax, rdx: Might contain return value
 946 
 947   // Check that all monitors are unlocked
 948   {
 949     Label loop, exception, entry, restart;
 950     const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
 951     const Address monitor_block_top(
 952         rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
 953     const Address monitor_block_bot(
 954         rbp, frame::interpreter_frame_initial_sp_offset * wordSize);
 955 
 956     bind(restart);
 957     // We use c_rarg1 so that if we go slow path it will be the correct
 958     // register for unlock_object to pass to VM directly
 959     movptr(rmon, monitor_block_top); // points to current entry, starting
 960                                   // with top-most entry
 961     lea(rbx, monitor_block_bot);  // points to word before bottom of
 962                                   // monitor block
 963     jmp(entry);
 964 
 965     // Entry already locked, need to throw exception
 966     bind(exception);
 967 
 968     if (throw_monitor_exception) {
 969       // Throw exception
 970       NOT_LP64(empty_FPU_stack();)
 971       MacroAssembler::call_VM(noreg,
 972                               CAST_FROM_FN_PTR(address, InterpreterRuntime::
 973                                    throw_illegal_monitor_state_exception));
 974       should_not_reach_here();
 975     } else {
 976       // Stack unrolling. Unlock object and install illegal_monitor_exception.
 977       // Unlock does not block, so don't have to worry about the frame.
 978       // We don't have to preserve c_rarg1 since we are going to throw an exception.
 979 
 980       push(state);
 981       mov(robj, rmon);   // nop if robj and rmon are the same
 982       unlock_object(robj);
 983       pop(state);
 984 
 985       if (install_monitor_exception) {
 986         NOT_LP64(empty_FPU_stack();)
 987         call_VM(noreg, CAST_FROM_FN_PTR(address,
 988                                         InterpreterRuntime::
 989                                         new_illegal_monitor_state_exception));
 990       }
 991 
 992       jmp(restart);
 993     }
 994 
 995     bind(loop);
 996     // check if current entry is used
 997     cmpptr(Address(rmon, BasicObjectLock::obj_offset_in_bytes()), (int32_t) NULL);
 998     jcc(Assembler::notEqual, exception);
 999 
1000     addptr(rmon, entry_size); // otherwise advance to next entry
1001     bind(entry);
1002     cmpptr(rmon, rbx); // check if bottom reached
1003     jcc(Assembler::notEqual, loop); // if not at bottom then check this entry
1004   }
1005 
1006   bind(no_unlock);
1007 
1008   // jvmti support
1009   if (notify_jvmdi) {
1010     notify_method_exit(state, NotifyJVMTI);    // preserve TOSCA
1011   } else {
1012     notify_method_exit(state, SkipNotifyJVMTI); // preserve TOSCA
1013   }
1014 
1015   // remove activation
1016   // get sender sp
1017   movptr(rbx,
1018          Address(rbp, frame::interpreter_frame_sender_sp_offset * wordSize));
1019   leave();                           // remove frame anchor
1020   pop(ret_addr);                     // get return address
1021   mov(rsp, rbx);                     // set sp to sender sp
1022 }
1023 #endif // !CC_INTERP
1024 
1025 void InterpreterMacroAssembler::get_method_counters(Register method,
1026                                                     Register mcs, Label& skip) {
1027   Label has_counters;
1028   movptr(mcs, Address(method, Method::method_counters_offset()));
1029   testptr(mcs, mcs);
1030   jcc(Assembler::notZero, has_counters);
1031   call_VM(noreg, CAST_FROM_FN_PTR(address,
1032           InterpreterRuntime::build_method_counters), method);
1033   movptr(mcs, Address(method,Method::method_counters_offset()));
1034   testptr(mcs, mcs);
1035   jcc(Assembler::zero, skip); // No MethodCounters allocated, OutOfMemory
1036   bind(has_counters);
1037 }
1038 
1039 
1040 // Lock object
1041 //
1042 // Args:
1043 //      rdx, c_rarg1: BasicObjectLock to be used for locking
1044 //
1045 // Kills:
1046 //      rax, rbx
1047 void InterpreterMacroAssembler::lock_object(Register lock_reg) {
1048   assert(lock_reg == LP64_ONLY(c_rarg1) NOT_LP64(rdx),
1049          "The argument is only for looks. It must be c_rarg1");
1050 
1051   if (UseHeavyMonitors) {
1052     call_VM(noreg,
1053             CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter),
1054             lock_reg);
1055   } else {
1056     Label done;
1057 
1058     const Register swap_reg = rax; // Must use rax for cmpxchg instruction
1059     const Register tmp_reg = rbx; // Will be passed to biased_locking_enter to avoid a
1060                                   // problematic case where tmp_reg = no_reg.
1061     const Register obj_reg = LP64_ONLY(c_rarg3) NOT_LP64(rcx); // Will contain the oop
1062 
1063     const int obj_offset = BasicObjectLock::obj_offset_in_bytes();
1064     const int lock_offset = BasicObjectLock::lock_offset_in_bytes ();
1065     const int mark_offset = lock_offset +
1066                             BasicLock::displaced_header_offset_in_bytes();
1067 
1068     Label slow_case;
1069 
1070     // Load object pointer into obj_reg
1071     movptr(obj_reg, Address(lock_reg, obj_offset));
1072 
1073     if (UseBiasedLocking) {
1074       biased_locking_enter(lock_reg, obj_reg, swap_reg, tmp_reg, false, done, &slow_case);
1075     }
1076 
1077     // Load immediate 1 into swap_reg %rax
1078     movl(swap_reg, (int32_t)1);
1079 
1080     // Load (object->mark() | 1) into swap_reg %rax
1081     orptr(swap_reg, Address(obj_reg, 0));
1082 
1083     // Save (object->mark() | 1) into BasicLock's displaced header
1084     movptr(Address(lock_reg, mark_offset), swap_reg);
1085 
1086     assert(lock_offset == 0,
1087            "displached header must be first word in BasicObjectLock");
1088 
1089     if (os::is_MP()) lock();
1090     cmpxchgptr(lock_reg, Address(obj_reg, 0));
1091     if (PrintBiasedLockingStatistics) {
1092       cond_inc32(Assembler::zero,
1093                  ExternalAddress((address) BiasedLocking::fast_path_entry_count_addr()));
1094     }
1095     jcc(Assembler::zero, done);
1096 
1097     const int zero_bits = LP64_ONLY(7) NOT_LP64(3);
1098 
1099     // Test if the oopMark is an obvious stack pointer, i.e.,
1100     //  1) (mark & zero_bits) == 0, and
1101     //  2) rsp <= mark < mark + os::pagesize()
1102     //
1103     // These 3 tests can be done by evaluating the following
1104     // expression: ((mark - rsp) & (zero_bits - os::vm_page_size())),
1105     // assuming both stack pointer and pagesize have their
1106     // least significant bits clear.
1107     // NOTE: the oopMark is in swap_reg %rax as the result of cmpxchg
1108     subptr(swap_reg, rsp);
1109     andptr(swap_reg, zero_bits - os::vm_page_size());
1110 
1111     // Save the test result, for recursive case, the result is zero
1112     movptr(Address(lock_reg, mark_offset), swap_reg);
1113 
1114     if (PrintBiasedLockingStatistics) {
1115       cond_inc32(Assembler::zero,
1116                  ExternalAddress((address) BiasedLocking::fast_path_entry_count_addr()));
1117     }
1118     jcc(Assembler::zero, done);
1119 
1120     bind(slow_case);
1121 
1122     // Call the runtime routine for slow case
1123     call_VM(noreg,
1124             CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter),
1125             lock_reg);
1126 
1127     bind(done);
1128   }
1129 }
1130 
1131 
1132 // Unlocks an object. Used in monitorexit bytecode and
1133 // remove_activation.  Throws an IllegalMonitorException if object is
1134 // not locked by current thread.
1135 //
1136 // Args:
1137 //      rdx, c_rarg1: BasicObjectLock for lock
1138 //
1139 // Kills:
1140 //      rax
1141 //      c_rarg0, c_rarg1, c_rarg2, c_rarg3, ... (param regs)
1142 //      rscratch1, rscratch2 (scratch regs)
1143 // rax, rbx, rcx, rdx
1144 void InterpreterMacroAssembler::unlock_object(Register lock_reg) {
1145   assert(lock_reg == LP64_ONLY(c_rarg1) NOT_LP64(rdx),
1146          "The argument is only for looks. It must be c_rarg1");
1147 
1148   if (UseHeavyMonitors) {
1149     call_VM(noreg,
1150             CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit),
1151             lock_reg);
1152   } else {
1153     Label done;
1154 
1155     const Register swap_reg   = rax;  // Must use rax for cmpxchg instruction
1156     const Register header_reg = LP64_ONLY(c_rarg2) NOT_LP64(rbx);  // Will contain the old oopMark
1157     const Register obj_reg    = LP64_ONLY(c_rarg3) NOT_LP64(rcx);  // Will contain the oop
1158 
1159     save_bcp(); // Save in case of exception
1160 
1161     // Convert from BasicObjectLock structure to object and BasicLock
1162     // structure Store the BasicLock address into %rax
1163     lea(swap_reg, Address(lock_reg, BasicObjectLock::lock_offset_in_bytes()));
1164 
1165     // Load oop into obj_reg(%c_rarg3)
1166     movptr(obj_reg, Address(lock_reg, BasicObjectLock::obj_offset_in_bytes()));
1167 
1168     // Free entry
1169     movptr(Address(lock_reg, BasicObjectLock::obj_offset_in_bytes()), (int32_t)NULL_WORD);
1170 
1171     if (UseBiasedLocking) {
1172       biased_locking_exit(obj_reg, header_reg, done);
1173     }
1174 
1175     // Load the old header from BasicLock structure
1176     movptr(header_reg, Address(swap_reg,
1177                                BasicLock::displaced_header_offset_in_bytes()));
1178 
1179     // Test for recursion
1180     testptr(header_reg, header_reg);
1181 
1182     // zero for recursive case
1183     jcc(Assembler::zero, done);
1184 
1185     // Atomic swap back the old header
1186     if (os::is_MP()) lock();
1187     cmpxchgptr(header_reg, Address(obj_reg, 0));
1188 
1189     // zero for recursive case
1190     jcc(Assembler::zero, done);
1191 
1192     // Call the runtime routine for slow case.
1193     movptr(Address(lock_reg, BasicObjectLock::obj_offset_in_bytes()),
1194          obj_reg); // restore obj
1195     call_VM(noreg,
1196             CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit),
1197             lock_reg);
1198 
1199     bind(done);
1200 
1201     restore_bcp();
1202   }
1203 }
1204 #ifndef CC_INTERP
1205 void InterpreterMacroAssembler::test_method_data_pointer(Register mdp,
1206                                                          Label& zero_continue) {
1207   assert(ProfileInterpreter, "must be profiling interpreter");
1208   movptr(mdp, Address(rbp, frame::interpreter_frame_mdp_offset * wordSize));
1209   testptr(mdp, mdp);
1210   jcc(Assembler::zero, zero_continue);
1211 }
1212 
1213 
1214 // Set the method data pointer for the current bcp.
1215 void InterpreterMacroAssembler::set_method_data_pointer_for_bcp() {
1216   assert(ProfileInterpreter, "must be profiling interpreter");
1217   Label set_mdp;
1218   push(rax);
1219   push(rbx);
1220 
1221   get_method(rbx);
1222   // Test MDO to avoid the call if it is NULL.
1223   movptr(rax, Address(rbx, in_bytes(Method::method_data_offset())));
1224   testptr(rax, rax);
1225   jcc(Assembler::zero, set_mdp);
1226   // rbx: method
1227   // _bcp_register: bcp
1228   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::bcp_to_di), rbx, _bcp_register);
1229   // rax: mdi
1230   // mdo is guaranteed to be non-zero here, we checked for it before the call.
1231   movptr(rbx, Address(rbx, in_bytes(Method::method_data_offset())));
1232   addptr(rbx, in_bytes(MethodData::data_offset()));
1233   addptr(rax, rbx);
1234   bind(set_mdp);
1235   movptr(Address(rbp, frame::interpreter_frame_mdp_offset * wordSize), rax);
1236   pop(rbx);
1237   pop(rax);
1238 }
1239 
1240 void InterpreterMacroAssembler::verify_method_data_pointer() {
1241   assert(ProfileInterpreter, "must be profiling interpreter");
1242 #ifdef ASSERT
1243   Label verify_continue;
1244   push(rax);
1245   push(rbx);
1246   Register arg3_reg = LP64_ONLY(c_rarg3) NOT_LP64(rcx);
1247   Register arg2_reg = LP64_ONLY(c_rarg2) NOT_LP64(rdx);
1248   push(arg3_reg);
1249   push(arg2_reg);
1250   test_method_data_pointer(arg3_reg, verify_continue); // If mdp is zero, continue
1251   get_method(rbx);
1252 
1253   // If the mdp is valid, it will point to a DataLayout header which is
1254   // consistent with the bcp.  The converse is highly probable also.
1255   load_unsigned_short(arg2_reg,
1256                       Address(arg3_reg, in_bytes(DataLayout::bci_offset())));
1257   addptr(arg2_reg, Address(rbx, Method::const_offset()));
1258   lea(arg2_reg, Address(arg2_reg, ConstMethod::codes_offset()));
1259   cmpptr(arg2_reg, _bcp_register);
1260   jcc(Assembler::equal, verify_continue);
1261   // rbx: method
1262   // _bcp_register: bcp
1263   // c_rarg3: mdp
1264   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::verify_mdp),
1265                rbx, _bcp_register, arg3_reg);
1266   bind(verify_continue);
1267   pop(arg2_reg);
1268   pop(arg3_reg);
1269   pop(rbx);
1270   pop(rax);
1271 #endif // ASSERT
1272 }
1273 
1274 
1275 void InterpreterMacroAssembler::set_mdp_data_at(Register mdp_in,
1276                                                 int constant,
1277                                                 Register value) {
1278   assert(ProfileInterpreter, "must be profiling interpreter");
1279   Address data(mdp_in, constant);
1280   movptr(data, value);
1281 }
1282 
1283 
1284 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in,
1285                                                       int constant,
1286                                                       bool decrement) {
1287   // Counter address
1288   Address data(mdp_in, constant);
1289 
1290   increment_mdp_data_at(data, decrement);
1291 }
1292 
1293 void InterpreterMacroAssembler::increment_mdp_data_at(Address data,
1294                                                       bool decrement) {
1295   assert(ProfileInterpreter, "must be profiling interpreter");
1296   // %%% this does 64bit counters at best it is wasting space
1297   // at worst it is a rare bug when counters overflow
1298 
1299   if (decrement) {
1300     // Decrement the register.  Set condition codes.
1301     addptr(data, (int32_t) -DataLayout::counter_increment);
1302     // If the decrement causes the counter to overflow, stay negative
1303     Label L;
1304     jcc(Assembler::negative, L);
1305     addptr(data, (int32_t) DataLayout::counter_increment);
1306     bind(L);
1307   } else {
1308     assert(DataLayout::counter_increment == 1,
1309            "flow-free idiom only works with 1");
1310     // Increment the register.  Set carry flag.
1311     addptr(data, DataLayout::counter_increment);
1312     // If the increment causes the counter to overflow, pull back by 1.
1313     sbbptr(data, (int32_t)0);
1314   }
1315 }
1316 
1317 
1318 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in,
1319                                                       Register reg,
1320                                                       int constant,
1321                                                       bool decrement) {
1322   Address data(mdp_in, reg, Address::times_1, constant);
1323 
1324   increment_mdp_data_at(data, decrement);
1325 }
1326 
1327 void InterpreterMacroAssembler::set_mdp_flag_at(Register mdp_in,
1328                                                 int flag_byte_constant) {
1329   assert(ProfileInterpreter, "must be profiling interpreter");
1330   int header_offset = in_bytes(DataLayout::header_offset());
1331   int header_bits = DataLayout::flag_mask_to_header_mask(flag_byte_constant);
1332   // Set the flag
1333   orl(Address(mdp_in, header_offset), header_bits);
1334 }
1335 
1336 
1337 
1338 void InterpreterMacroAssembler::test_mdp_data_at(Register mdp_in,
1339                                                  int offset,
1340                                                  Register value,
1341                                                  Register test_value_out,
1342                                                  Label& not_equal_continue) {
1343   assert(ProfileInterpreter, "must be profiling interpreter");
1344   if (test_value_out == noreg) {
1345     cmpptr(value, Address(mdp_in, offset));
1346   } else {
1347     // Put the test value into a register, so caller can use it:
1348     movptr(test_value_out, Address(mdp_in, offset));
1349     cmpptr(test_value_out, value);
1350   }
1351   jcc(Assembler::notEqual, not_equal_continue);
1352 }
1353 
1354 
1355 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in,
1356                                                      int offset_of_disp) {
1357   assert(ProfileInterpreter, "must be profiling interpreter");
1358   Address disp_address(mdp_in, offset_of_disp);
1359   addptr(mdp_in, disp_address);
1360   movptr(Address(rbp, frame::interpreter_frame_mdp_offset * wordSize), mdp_in);
1361 }
1362 
1363 
1364 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in,
1365                                                      Register reg,
1366                                                      int offset_of_disp) {
1367   assert(ProfileInterpreter, "must be profiling interpreter");
1368   Address disp_address(mdp_in, reg, Address::times_1, offset_of_disp);
1369   addptr(mdp_in, disp_address);
1370   movptr(Address(rbp, frame::interpreter_frame_mdp_offset * wordSize), mdp_in);
1371 }
1372 
1373 
1374 void InterpreterMacroAssembler::update_mdp_by_constant(Register mdp_in,
1375                                                        int constant) {
1376   assert(ProfileInterpreter, "must be profiling interpreter");
1377   addptr(mdp_in, constant);
1378   movptr(Address(rbp, frame::interpreter_frame_mdp_offset * wordSize), mdp_in);
1379 }
1380 
1381 
1382 void InterpreterMacroAssembler::update_mdp_for_ret(Register return_bci) {
1383   assert(ProfileInterpreter, "must be profiling interpreter");
1384   push(return_bci); // save/restore across call_VM
1385   call_VM(noreg,
1386           CAST_FROM_FN_PTR(address, InterpreterRuntime::update_mdp_for_ret),
1387           return_bci);
1388   pop(return_bci);
1389 }
1390 
1391 
1392 void InterpreterMacroAssembler::profile_taken_branch(Register mdp,
1393                                                      Register bumped_count) {
1394   if (ProfileInterpreter) {
1395     Label profile_continue;
1396 
1397     // If no method data exists, go to profile_continue.
1398     // Otherwise, assign to mdp
1399     test_method_data_pointer(mdp, profile_continue);
1400 
1401     // We are taking a branch.  Increment the taken count.
1402     // We inline increment_mdp_data_at to return bumped_count in a register
1403     //increment_mdp_data_at(mdp, in_bytes(JumpData::taken_offset()));
1404     Address data(mdp, in_bytes(JumpData::taken_offset()));
1405     movptr(bumped_count, data);
1406     assert(DataLayout::counter_increment == 1,
1407             "flow-free idiom only works with 1");
1408     addptr(bumped_count, DataLayout::counter_increment);
1409     sbbptr(bumped_count, 0);
1410     movptr(data, bumped_count); // Store back out
1411 
1412     // The method data pointer needs to be updated to reflect the new target.
1413     update_mdp_by_offset(mdp, in_bytes(JumpData::displacement_offset()));
1414     bind(profile_continue);
1415   }
1416 }
1417 
1418 
1419 void InterpreterMacroAssembler::profile_not_taken_branch(Register mdp) {
1420   if (ProfileInterpreter) {
1421     Label profile_continue;
1422 
1423     // If no method data exists, go to profile_continue.
1424     test_method_data_pointer(mdp, profile_continue);
1425 
1426     // We are taking a branch.  Increment the not taken count.
1427     increment_mdp_data_at(mdp, in_bytes(BranchData::not_taken_offset()));
1428 
1429     // The method data pointer needs to be updated to correspond to
1430     // the next bytecode
1431     update_mdp_by_constant(mdp, in_bytes(BranchData::branch_data_size()));
1432     bind(profile_continue);
1433   }
1434 }
1435 
1436 void InterpreterMacroAssembler::profile_call(Register mdp) {
1437   if (ProfileInterpreter) {
1438     Label profile_continue;
1439 
1440     // If no method data exists, go to profile_continue.
1441     test_method_data_pointer(mdp, profile_continue);
1442 
1443     // We are making a call.  Increment the count.
1444     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1445 
1446     // The method data pointer needs to be updated to reflect the new target.
1447     update_mdp_by_constant(mdp, in_bytes(CounterData::counter_data_size()));
1448     bind(profile_continue);
1449   }
1450 }
1451 
1452 
1453 void InterpreterMacroAssembler::profile_final_call(Register mdp) {
1454   if (ProfileInterpreter) {
1455     Label profile_continue;
1456 
1457     // If no method data exists, go to profile_continue.
1458     test_method_data_pointer(mdp, profile_continue);
1459 
1460     // We are making a call.  Increment the count.
1461     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1462 
1463     // The method data pointer needs to be updated to reflect the new target.
1464     update_mdp_by_constant(mdp,
1465                            in_bytes(VirtualCallData::
1466                                     virtual_call_data_size()));
1467     bind(profile_continue);
1468   }
1469 }
1470 
1471 
1472 void InterpreterMacroAssembler::profile_virtual_call(Register receiver,
1473                                                      Register mdp,
1474                                                      Register reg2,
1475                                                      bool receiver_can_be_null) {
1476   if (ProfileInterpreter) {
1477     Label profile_continue;
1478 
1479     // If no method data exists, go to profile_continue.
1480     test_method_data_pointer(mdp, profile_continue);
1481 
1482     Label skip_receiver_profile;
1483     if (receiver_can_be_null) {
1484       Label not_null;
1485       testptr(receiver, receiver);
1486       jccb(Assembler::notZero, not_null);
1487       // We are making a call.  Increment the count for null receiver.
1488       increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1489       jmp(skip_receiver_profile);
1490       bind(not_null);
1491     }
1492 
1493     // Record the receiver type.
1494     record_klass_in_profile(receiver, mdp, reg2, true);
1495     bind(skip_receiver_profile);
1496 
1497     // The method data pointer needs to be updated to reflect the new target.
1498     update_mdp_by_constant(mdp,
1499                            in_bytes(VirtualCallData::
1500                                     virtual_call_data_size()));
1501     bind(profile_continue);
1502   }
1503 }
1504 
1505 // This routine creates a state machine for updating the multi-row
1506 // type profile at a virtual call site (or other type-sensitive bytecode).
1507 // The machine visits each row (of receiver/count) until the receiver type
1508 // is found, or until it runs out of rows.  At the same time, it remembers
1509 // the location of the first empty row.  (An empty row records null for its
1510 // receiver, and can be allocated for a newly-observed receiver type.)
1511 // Because there are two degrees of freedom in the state, a simple linear
1512 // search will not work; it must be a decision tree.  Hence this helper
1513 // function is recursive, to generate the required tree structured code.
1514 // It's the interpreter, so we are trading off code space for speed.
1515 // See below for example code.
1516 void InterpreterMacroAssembler::record_klass_in_profile_helper(
1517                                         Register receiver, Register mdp,
1518                                         Register reg2, int start_row,
1519                                         Label& done, bool is_virtual_call) {
1520   if (TypeProfileWidth == 0) {
1521     if (is_virtual_call) {
1522       increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1523     }
1524     return;
1525   }
1526 
1527   int last_row = VirtualCallData::row_limit() - 1;
1528   assert(start_row <= last_row, "must be work left to do");
1529   // Test this row for both the receiver and for null.
1530   // Take any of three different outcomes:
1531   //   1. found receiver => increment count and goto done
1532   //   2. found null => keep looking for case 1, maybe allocate this cell
1533   //   3. found something else => keep looking for cases 1 and 2
1534   // Case 3 is handled by a recursive call.
1535   for (int row = start_row; row <= last_row; row++) {
1536     Label next_test;
1537     bool test_for_null_also = (row == start_row);
1538 
1539     // See if the receiver is receiver[n].
1540     int recvr_offset = in_bytes(VirtualCallData::receiver_offset(row));
1541     test_mdp_data_at(mdp, recvr_offset, receiver,
1542                      (test_for_null_also ? reg2 : noreg),
1543                      next_test);
1544     // (Reg2 now contains the receiver from the CallData.)
1545 
1546     // The receiver is receiver[n].  Increment count[n].
1547     int count_offset = in_bytes(VirtualCallData::receiver_count_offset(row));
1548     increment_mdp_data_at(mdp, count_offset);
1549     jmp(done);
1550     bind(next_test);
1551 
1552     if (test_for_null_also) {
1553       Label found_null;
1554       // Failed the equality check on receiver[n]...  Test for null.
1555       testptr(reg2, reg2);
1556       if (start_row == last_row) {
1557         // The only thing left to do is handle the null case.
1558         if (is_virtual_call) {
1559           jccb(Assembler::zero, found_null);
1560           // Receiver did not match any saved receiver and there is no empty row for it.
1561           // Increment total counter to indicate polymorphic case.
1562           increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1563           jmp(done);
1564           bind(found_null);
1565         } else {
1566           jcc(Assembler::notZero, done);
1567         }
1568         break;
1569       }
1570       // Since null is rare, make it be the branch-taken case.
1571       jcc(Assembler::zero, found_null);
1572 
1573       // Put all the "Case 3" tests here.
1574       record_klass_in_profile_helper(receiver, mdp, reg2, start_row + 1, done, is_virtual_call);
1575 
1576       // Found a null.  Keep searching for a matching receiver,
1577       // but remember that this is an empty (unused) slot.
1578       bind(found_null);
1579     }
1580   }
1581 
1582   // In the fall-through case, we found no matching receiver, but we
1583   // observed the receiver[start_row] is NULL.
1584 
1585   // Fill in the receiver field and increment the count.
1586   int recvr_offset = in_bytes(VirtualCallData::receiver_offset(start_row));
1587   set_mdp_data_at(mdp, recvr_offset, receiver);
1588   int count_offset = in_bytes(VirtualCallData::receiver_count_offset(start_row));
1589   movl(reg2, DataLayout::counter_increment);
1590   set_mdp_data_at(mdp, count_offset, reg2);
1591   if (start_row > 0) {
1592     jmp(done);
1593   }
1594 }
1595 
1596 // Example state machine code for three profile rows:
1597 //   // main copy of decision tree, rooted at row[1]
1598 //   if (row[0].rec == rec) { row[0].incr(); goto done; }
1599 //   if (row[0].rec != NULL) {
1600 //     // inner copy of decision tree, rooted at row[1]
1601 //     if (row[1].rec == rec) { row[1].incr(); goto done; }
1602 //     if (row[1].rec != NULL) {
1603 //       // degenerate decision tree, rooted at row[2]
1604 //       if (row[2].rec == rec) { row[2].incr(); goto done; }
1605 //       if (row[2].rec != NULL) { count.incr(); goto done; } // overflow
1606 //       row[2].init(rec); goto done;
1607 //     } else {
1608 //       // remember row[1] is empty
1609 //       if (row[2].rec == rec) { row[2].incr(); goto done; }
1610 //       row[1].init(rec); goto done;
1611 //     }
1612 //   } else {
1613 //     // remember row[0] is empty
1614 //     if (row[1].rec == rec) { row[1].incr(); goto done; }
1615 //     if (row[2].rec == rec) { row[2].incr(); goto done; }
1616 //     row[0].init(rec); goto done;
1617 //   }
1618 //   done:
1619 
1620 void InterpreterMacroAssembler::record_klass_in_profile(Register receiver,
1621                                                         Register mdp, Register reg2,
1622                                                         bool is_virtual_call) {
1623   assert(ProfileInterpreter, "must be profiling");
1624   Label done;
1625 
1626   record_klass_in_profile_helper(receiver, mdp, reg2, 0, done, is_virtual_call);
1627 
1628   bind (done);
1629 }
1630 
1631 void InterpreterMacroAssembler::profile_ret(Register return_bci,
1632                                             Register mdp) {
1633   if (ProfileInterpreter) {
1634     Label profile_continue;
1635     uint row;
1636 
1637     // If no method data exists, go to profile_continue.
1638     test_method_data_pointer(mdp, profile_continue);
1639 
1640     // Update the total ret count.
1641     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1642 
1643     for (row = 0; row < RetData::row_limit(); row++) {
1644       Label next_test;
1645 
1646       // See if return_bci is equal to bci[n]:
1647       test_mdp_data_at(mdp,
1648                        in_bytes(RetData::bci_offset(row)),
1649                        return_bci, noreg,
1650                        next_test);
1651 
1652       // return_bci is equal to bci[n].  Increment the count.
1653       increment_mdp_data_at(mdp, in_bytes(RetData::bci_count_offset(row)));
1654 
1655       // The method data pointer needs to be updated to reflect the new target.
1656       update_mdp_by_offset(mdp,
1657                            in_bytes(RetData::bci_displacement_offset(row)));
1658       jmp(profile_continue);
1659       bind(next_test);
1660     }
1661 
1662     update_mdp_for_ret(return_bci);
1663 
1664     bind(profile_continue);
1665   }
1666 }
1667 
1668 
1669 void InterpreterMacroAssembler::profile_null_seen(Register mdp) {
1670   if (ProfileInterpreter) {
1671     Label profile_continue;
1672 
1673     // If no method data exists, go to profile_continue.
1674     test_method_data_pointer(mdp, profile_continue);
1675 
1676     set_mdp_flag_at(mdp, BitData::null_seen_byte_constant());
1677 
1678     // The method data pointer needs to be updated.
1679     int mdp_delta = in_bytes(BitData::bit_data_size());
1680     if (TypeProfileCasts) {
1681       mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1682     }
1683     update_mdp_by_constant(mdp, mdp_delta);
1684 
1685     bind(profile_continue);
1686   }
1687 }
1688 
1689 
1690 void InterpreterMacroAssembler::profile_typecheck_failed(Register mdp) {
1691   if (ProfileInterpreter && TypeProfileCasts) {
1692     Label profile_continue;
1693 
1694     // If no method data exists, go to profile_continue.
1695     test_method_data_pointer(mdp, profile_continue);
1696 
1697     int count_offset = in_bytes(CounterData::count_offset());
1698     // Back up the address, since we have already bumped the mdp.
1699     count_offset -= in_bytes(VirtualCallData::virtual_call_data_size());
1700 
1701     // *Decrement* the counter.  We expect to see zero or small negatives.
1702     increment_mdp_data_at(mdp, count_offset, true);
1703 
1704     bind (profile_continue);
1705   }
1706 }
1707 
1708 
1709 void InterpreterMacroAssembler::profile_typecheck(Register mdp, Register klass, Register reg2) {
1710   if (ProfileInterpreter) {
1711     Label profile_continue;
1712 
1713     // If no method data exists, go to profile_continue.
1714     test_method_data_pointer(mdp, profile_continue);
1715 
1716     // The method data pointer needs to be updated.
1717     int mdp_delta = in_bytes(BitData::bit_data_size());
1718     if (TypeProfileCasts) {
1719       mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1720 
1721       // Record the object type.
1722       record_klass_in_profile(klass, mdp, reg2, false);
1723       NOT_LP64(assert(reg2 == rdi, "we know how to fix this blown reg");)
1724       NOT_LP64(restore_locals();)         // Restore EDI
1725     }
1726     update_mdp_by_constant(mdp, mdp_delta);
1727 
1728     bind(profile_continue);
1729   }
1730 }
1731 
1732 
1733 void InterpreterMacroAssembler::profile_switch_default(Register mdp) {
1734   if (ProfileInterpreter) {
1735     Label profile_continue;
1736 
1737     // If no method data exists, go to profile_continue.
1738     test_method_data_pointer(mdp, profile_continue);
1739 
1740     // Update the default case count
1741     increment_mdp_data_at(mdp,
1742                           in_bytes(MultiBranchData::default_count_offset()));
1743 
1744     // The method data pointer needs to be updated.
1745     update_mdp_by_offset(mdp,
1746                          in_bytes(MultiBranchData::
1747                                   default_displacement_offset()));
1748 
1749     bind(profile_continue);
1750   }
1751 }
1752 
1753 
1754 void InterpreterMacroAssembler::profile_switch_case(Register index,
1755                                                     Register mdp,
1756                                                     Register reg2) {
1757   if (ProfileInterpreter) {
1758     Label profile_continue;
1759 
1760     // If no method data exists, go to profile_continue.
1761     test_method_data_pointer(mdp, profile_continue);
1762 
1763     // Build the base (index * per_case_size_in_bytes()) +
1764     // case_array_offset_in_bytes()
1765     movl(reg2, in_bytes(MultiBranchData::per_case_size()));
1766     imulptr(index, reg2); // XXX l ?
1767     addptr(index, in_bytes(MultiBranchData::case_array_offset())); // XXX l ?
1768 
1769     // Update the case count
1770     increment_mdp_data_at(mdp,
1771                           index,
1772                           in_bytes(MultiBranchData::relative_count_offset()));
1773 
1774     // The method data pointer needs to be updated.
1775     update_mdp_by_offset(mdp,
1776                          index,
1777                          in_bytes(MultiBranchData::
1778                                   relative_displacement_offset()));
1779 
1780     bind(profile_continue);
1781   }
1782 }
1783 
1784 
1785 
1786 void InterpreterMacroAssembler::verify_oop(Register reg, TosState state) {
1787   if (state == atos) {
1788     MacroAssembler::verify_oop(reg);
1789   }
1790 }
1791 
1792 void InterpreterMacroAssembler::verify_FPU(int stack_depth, TosState state) {
1793 #ifndef _LP64
1794   if ((state == ftos && UseSSE < 1) ||
1795       (state == dtos && UseSSE < 2)) {
1796     MacroAssembler::verify_FPU(stack_depth);
1797   }
1798 #endif
1799 }
1800 
1801 // Jump if ((*counter_addr += increment) & mask) satisfies the condition.
1802 void InterpreterMacroAssembler::increment_mask_and_jump(Address counter_addr,
1803                                                         int increment, Address mask,
1804                                                         Register scratch, bool preloaded,
1805                                                         Condition cond, Label* where) {
1806   if (!preloaded) {
1807     movl(scratch, counter_addr);
1808   }
1809   incrementl(scratch, increment);
1810   movl(counter_addr, scratch);
1811   andl(scratch, mask);
1812   jcc(cond, *where);
1813 }
1814 #endif // CC_INTERP
1815 
1816 void InterpreterMacroAssembler::notify_method_entry() {
1817   // Whenever JVMTI is interp_only_mode, method entry/exit events are sent to
1818   // track stack depth.  If it is possible to enter interp_only_mode we add
1819   // the code to check if the event should be sent.
1820   Register rthread = LP64_ONLY(r15_thread) NOT_LP64(rcx);
1821   Register rarg = LP64_ONLY(c_rarg1) NOT_LP64(rbx);
1822   if (JvmtiExport::can_post_interpreter_events()) {
1823     Label L;
1824     NOT_LP64(get_thread(rthread);)
1825     movl(rdx, Address(rthread, JavaThread::interp_only_mode_offset()));
1826     testl(rdx, rdx);
1827     jcc(Assembler::zero, L);
1828     call_VM(noreg, CAST_FROM_FN_PTR(address,
1829                                     InterpreterRuntime::post_method_entry));
1830     bind(L);
1831   }
1832 
1833   {
1834     SkipIfEqual skip(this, &DTraceMethodProbes, false);
1835     NOT_LP64(get_thread(rthread);)
1836     get_method(rarg);
1837     call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry),
1838                  rthread, rarg);
1839   }
1840 
1841   // RedefineClasses() tracing support for obsolete method entry
1842   if (RC_TRACE_IN_RANGE(0x00001000, 0x00002000)) {
1843     NOT_LP64(get_thread(rthread);)
1844     get_method(rarg);
1845     call_VM_leaf(
1846       CAST_FROM_FN_PTR(address, SharedRuntime::rc_trace_method_entry),
1847       rthread, rarg);
1848   }
1849 }
1850 
1851 
1852 void InterpreterMacroAssembler::notify_method_exit(
1853     TosState state, NotifyMethodExitMode mode) {
1854   // Whenever JVMTI is interp_only_mode, method entry/exit events are sent to
1855   // track stack depth.  If it is possible to enter interp_only_mode we add
1856   // the code to check if the event should be sent.
1857   Register rthread = LP64_ONLY(r15_thread) NOT_LP64(rcx);
1858   Register rarg = LP64_ONLY(c_rarg1) NOT_LP64(rbx);
1859   if (mode == NotifyJVMTI && JvmtiExport::can_post_interpreter_events()) {
1860     Label L;
1861     // Note: frame::interpreter_frame_result has a dependency on how the
1862     // method result is saved across the call to post_method_exit. If this
1863     // is changed then the interpreter_frame_result implementation will
1864     // need to be updated too.
1865 
1866     // For c++ interpreter the result is always stored at a known location in the frame
1867     // template interpreter will leave it on the top of the stack.
1868     NOT_CC_INTERP(push(state);)
1869     NOT_LP64(get_thread(rthread);)
1870     movl(rdx, Address(rthread, JavaThread::interp_only_mode_offset()));
1871     testl(rdx, rdx);
1872     jcc(Assembler::zero, L);
1873     call_VM(noreg,
1874             CAST_FROM_FN_PTR(address, InterpreterRuntime::post_method_exit));
1875     bind(L);
1876     NOT_CC_INTERP(pop(state));
1877   }
1878 
1879   {
1880     SkipIfEqual skip(this, &DTraceMethodProbes, false);
1881     NOT_CC_INTERP(push(state));
1882     NOT_LP64(get_thread(rthread);)
1883     get_method(rarg);
1884     call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit),
1885                  rthread, rarg);
1886     NOT_CC_INTERP(pop(state));
1887   }
1888 }