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