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