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