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