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