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