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