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