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