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