1 /*
   2  * Copyright (c) 2003, 2011, 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_64.hpp"
  27 #include "interpreter/interpreter.hpp"
  28 #include "interpreter/interpreterRuntime.hpp"
  29 #include "oops/arrayOop.hpp"
  30 #include "oops/markOop.hpp"
  31 #include "oops/methodDataOop.hpp"
  32 #include "oops/methodOop.hpp"
  33 #include "prims/jvmtiExport.hpp"
  34 #include "prims/jvmtiRedefineClassesTrace.hpp"
  35 #include "prims/jvmtiThreadState.hpp"
  36 #include "runtime/basicLock.hpp"
  37 #include "runtime/biasedLocking.hpp"
  38 #include "runtime/sharedRuntime.hpp"
  39 #ifdef TARGET_OS_FAMILY_linux
  40 # include "thread_linux.inline.hpp"
  41 #endif
  42 #ifdef TARGET_OS_FAMILY_solaris
  43 # include "thread_solaris.inline.hpp"
  44 #endif
  45 #ifdef TARGET_OS_FAMILY_windows
  46 # include "thread_windows.inline.hpp"
  47 #endif
  48 
  49 
  50 // Implementation of InterpreterMacroAssembler
  51 
  52 #ifdef CC_INTERP
  53 void InterpreterMacroAssembler::get_method(Register reg) {
  54   movptr(reg, Address(rbp, -((int)sizeof(BytecodeInterpreter) + 2 * wordSize)));
  55   movptr(reg, Address(reg, byte_offset_of(BytecodeInterpreter, _method)));
  56 }
  57 #endif // CC_INTERP
  58 
  59 #ifndef CC_INTERP
  60 
  61 void InterpreterMacroAssembler::call_VM_leaf_base(address entry_point,
  62                                                   int number_of_arguments) {
  63   // interpreter specific
  64   //
  65   // Note: No need to save/restore bcp & locals (r13 & r14) pointer
  66   //       since these are callee saved registers and no blocking/
  67   //       GC can happen in leaf calls.
  68   // Further Note: DO NOT save/restore bcp/locals. If a caller has
  69   // already saved them so that it can use esi/edi as temporaries
  70   // then a save/restore here will DESTROY the copy the caller
  71   // saved! There used to be a save_bcp() that only happened in
  72   // the ASSERT path (no restore_bcp). Which caused bizarre failures
  73   // when jvm built with ASSERTs.
  74 #ifdef ASSERT
  75   {
  76     Label L;
  77     cmpptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), (int32_t)NULL_WORD);
  78     jcc(Assembler::equal, L);
  79     stop("InterpreterMacroAssembler::call_VM_leaf_base:"
  80          " last_sp != NULL");
  81     bind(L);
  82   }
  83 #endif
  84   // super call
  85   MacroAssembler::call_VM_leaf_base(entry_point, number_of_arguments);
  86   // interpreter specific
  87   // Used to ASSERT that r13/r14 were equal to frame's bcp/locals
  88   // but since they may not have been saved (and we don't want to
  89   // save thme here (see note above) the assert is invalid.
  90 }
  91 
  92 void InterpreterMacroAssembler::call_VM_base(Register oop_result,
  93                                              Register java_thread,
  94                                              Register last_java_sp,
  95                                              address  entry_point,
  96                                              int      number_of_arguments,
  97                                              bool     check_exceptions) {
  98   // interpreter specific
  99   //
 100   // Note: Could avoid restoring locals ptr (callee saved) - however doesn't
 101   //       really make a difference for these runtime calls, since they are
 102   //       slow anyway. Btw., bcp must be saved/restored since it may change
 103   //       due to GC.
 104   // assert(java_thread == noreg , "not expecting a precomputed java thread");
 105   save_bcp();
 106 #ifdef ASSERT
 107   {
 108     Label L;
 109     cmpptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), (int32_t)NULL_WORD);
 110     jcc(Assembler::equal, L);
 111     stop("InterpreterMacroAssembler::call_VM_leaf_base:"
 112          " last_sp != NULL");
 113     bind(L);
 114   }
 115 #endif /* ASSERT */
 116   // super call
 117   MacroAssembler::call_VM_base(oop_result, noreg, last_java_sp,
 118                                entry_point, number_of_arguments,
 119                                check_exceptions);
 120   // interpreter specific
 121   restore_bcp();
 122   restore_locals();
 123 }
 124 
 125 
 126 void InterpreterMacroAssembler::check_and_handle_popframe(Register java_thread) {
 127   if (JvmtiExport::can_pop_frame()) {
 128     Label L;
 129     // Initiate popframe handling only if it is not already being
 130     // processed.  If the flag has the popframe_processing bit set, it
 131     // means that this code is called *during* popframe handling - we
 132     // don't want to reenter.
 133     // This method is only called just after the call into the vm in
 134     // call_VM_base, so the arg registers are available.
 135     movl(c_rarg0, Address(r15_thread, JavaThread::popframe_condition_offset()));
 136     testl(c_rarg0, JavaThread::popframe_pending_bit);
 137     jcc(Assembler::zero, L);
 138     testl(c_rarg0, JavaThread::popframe_processing_bit);
 139     jcc(Assembler::notZero, L);
 140     // Call Interpreter::remove_activation_preserving_args_entry() to get the
 141     // address of the same-named entrypoint in the generated interpreter code.
 142     call_VM_leaf(CAST_FROM_FN_PTR(address, Interpreter::remove_activation_preserving_args_entry));
 143     jmp(rax);
 144     bind(L);
 145   }
 146 }
 147 
 148 
 149 void InterpreterMacroAssembler::load_earlyret_value(TosState state) {
 150   movptr(rcx, Address(r15_thread, JavaThread::jvmti_thread_state_offset()));
 151   const Address tos_addr(rcx, JvmtiThreadState::earlyret_tos_offset());
 152   const Address oop_addr(rcx, JvmtiThreadState::earlyret_oop_offset());
 153   const Address val_addr(rcx, JvmtiThreadState::earlyret_value_offset());
 154   switch (state) {
 155     case atos: movptr(rax, oop_addr);
 156                movptr(oop_addr, (int32_t)NULL_WORD);
 157                verify_oop(rax, state);              break;
 158     case ltos: movptr(rax, val_addr);                 break;
 159     case btos:                                   // fall through
 160     case ctos:                                   // fall through
 161     case stos:                                   // fall through
 162     case itos: movl(rax, val_addr);                 break;
 163     case ftos: movflt(xmm0, val_addr);              break;
 164     case dtos: movdbl(xmm0, val_addr);              break;
 165     case vtos: /* nothing to do */                  break;
 166     default  : ShouldNotReachHere();
 167   }
 168   // Clean up tos value in the thread object
 169   movl(tos_addr,  (int) ilgl);
 170   movl(val_addr,  (int32_t) NULL_WORD);
 171 }
 172 
 173 
 174 void InterpreterMacroAssembler::check_and_handle_earlyret(Register java_thread) {
 175   if (JvmtiExport::can_force_early_return()) {
 176     Label L;
 177     movptr(c_rarg0, Address(r15_thread, JavaThread::jvmti_thread_state_offset()));
 178     testptr(c_rarg0, c_rarg0);
 179     jcc(Assembler::zero, L); // if (thread->jvmti_thread_state() == NULL) exit;
 180 
 181     // Initiate earlyret handling only if it is not already being processed.
 182     // If the flag has the earlyret_processing bit set, it means that this code
 183     // is called *during* earlyret handling - we don't want to reenter.
 184     movl(c_rarg0, Address(c_rarg0, JvmtiThreadState::earlyret_state_offset()));
 185     cmpl(c_rarg0, JvmtiThreadState::earlyret_pending);
 186     jcc(Assembler::notEqual, L);
 187 
 188     // Call Interpreter::remove_activation_early_entry() to get the address of the
 189     // same-named entrypoint in the generated interpreter code.
 190     movptr(c_rarg0, Address(r15_thread, JavaThread::jvmti_thread_state_offset()));
 191     movl(c_rarg0, Address(c_rarg0, JvmtiThreadState::earlyret_tos_offset()));
 192     call_VM_leaf(CAST_FROM_FN_PTR(address, Interpreter::remove_activation_early_entry), c_rarg0);
 193     jmp(rax);
 194     bind(L);
 195   }
 196 }
 197 
 198 
 199 void InterpreterMacroAssembler::get_unsigned_2_byte_index_at_bcp(
 200   Register reg,
 201   int bcp_offset) {
 202   assert(bcp_offset >= 0, "bcp is still pointing to start of bytecode");
 203   movl(reg, Address(r13, bcp_offset));
 204   bswapl(reg);
 205   shrl(reg, 16);
 206 }
 207 
 208 
 209 void InterpreterMacroAssembler::get_cache_index_at_bcp(Register index,
 210                                                        int bcp_offset,
 211                                                        size_t index_size) {
 212   assert(bcp_offset > 0, "bcp is still pointing to start of bytecode");
 213   if (index_size == sizeof(u2)) {
 214     load_unsigned_short(index, Address(r13, bcp_offset));
 215   } else if (index_size == sizeof(u4)) {
 216     assert(EnableInvokeDynamic, "giant index used only for JSR 292");
 217     movl(index, Address(r13, bcp_offset));
 218     // Check if the secondary index definition is still ~x, otherwise
 219     // we have to change the following assembler code to calculate the
 220     // plain index.
 221     assert(constantPoolCacheOopDesc::decode_secondary_index(~123) == 123, "else change next line");
 222     notl(index);  // convert to plain index
 223   } else if (index_size == sizeof(u1)) {
 224     assert(EnableInvokeDynamic, "tiny index used only for JSR 292");
 225     load_unsigned_byte(index, Address(r13, bcp_offset));
 226   } else {
 227     ShouldNotReachHere();
 228   }
 229 }
 230 
 231 
 232 void InterpreterMacroAssembler::get_cache_and_index_at_bcp(Register cache,
 233                                                            Register index,
 234                                                            int bcp_offset,
 235                                                            size_t index_size) {
 236   assert_different_registers(cache, index);
 237   get_cache_index_at_bcp(index, bcp_offset, index_size);
 238   movptr(cache, Address(rbp, frame::interpreter_frame_cache_offset * wordSize));
 239   assert(sizeof(ConstantPoolCacheEntry) == 4 * wordSize, "adjust code below");
 240   // convert from field index to ConstantPoolCacheEntry index
 241   shll(index, 2);
 242 }
 243 
 244 
 245 void InterpreterMacroAssembler::get_cache_and_index_and_bytecode_at_bcp(Register cache,
 246                                                                         Register index,
 247                                                                         Register bytecode,
 248                                                                         int byte_no,
 249                                                                         int bcp_offset,
 250                                                                         size_t index_size) {
 251   get_cache_and_index_at_bcp(cache, index, bcp_offset, index_size);
 252   // We use a 32-bit load here since the layout of 64-bit words on
 253   // little-endian machines allow us that.
 254   movl(bytecode, Address(cache, index, Address::times_ptr, constantPoolCacheOopDesc::base_offset() + ConstantPoolCacheEntry::indices_offset()));
 255   const int shift_count = (1 + byte_no) * BitsPerByte;
 256   shrl(bytecode, shift_count);
 257   andl(bytecode, 0xFF);
 258 }
 259 
 260 
 261 void InterpreterMacroAssembler::get_cache_entry_pointer_at_bcp(Register cache,
 262                                                                Register tmp,
 263                                                                int bcp_offset,
 264                                                                size_t index_size) {
 265   assert(cache != tmp, "must use different register");
 266   get_cache_index_at_bcp(tmp, bcp_offset, index_size);
 267   assert(sizeof(ConstantPoolCacheEntry) == 4 * wordSize, "adjust code below");
 268   // convert from field index to ConstantPoolCacheEntry index
 269   // and from word offset to byte offset
 270   shll(tmp, 2 + LogBytesPerWord);
 271   movptr(cache, Address(rbp, frame::interpreter_frame_cache_offset * wordSize));
 272   // skip past the header
 273   addptr(cache, in_bytes(constantPoolCacheOopDesc::base_offset()));
 274   addptr(cache, tmp);  // construct pointer to cache entry
 275 }
 276 
 277 
 278 // Generate a subtype check: branch to ok_is_subtype if sub_klass is a
 279 // subtype of super_klass.
 280 //
 281 // Args:
 282 //      rax: superklass
 283 //      Rsub_klass: subklass
 284 //
 285 // Kills:
 286 //      rcx, rdi
 287 void InterpreterMacroAssembler::gen_subtype_check(Register Rsub_klass,
 288                                                   Label& ok_is_subtype) {
 289   assert(Rsub_klass != rax, "rax holds superklass");
 290   assert(Rsub_klass != r14, "r14 holds locals");
 291   assert(Rsub_klass != r13, "r13 holds bcp");
 292   assert(Rsub_klass != rcx, "rcx holds 2ndary super array length");
 293   assert(Rsub_klass != rdi, "rdi holds 2ndary super array scan ptr");
 294 
 295   // Profile the not-null value's klass.
 296   profile_typecheck(rcx, Rsub_klass, rdi); // blows rcx, reloads rdi
 297 
 298   // Do the check.
 299   check_klass_subtype(Rsub_klass, rax, rcx, ok_is_subtype); // blows rcx
 300 
 301   // Profile the failure of the check.
 302   profile_typecheck_failed(rcx); // blows rcx
 303 }
 304 
 305 
 306 
 307 // Java Expression Stack
 308 
 309 void InterpreterMacroAssembler::pop_ptr(Register r) {
 310   pop(r);
 311 }
 312 
 313 void InterpreterMacroAssembler::pop_i(Register r) {
 314   // XXX can't use pop currently, upper half non clean
 315   movl(r, Address(rsp, 0));
 316   addptr(rsp, wordSize);
 317 }
 318 
 319 void InterpreterMacroAssembler::pop_l(Register r) {
 320   movq(r, Address(rsp, 0));
 321   addptr(rsp, 2 * Interpreter::stackElementSize);
 322 }
 323 
 324 void InterpreterMacroAssembler::pop_f(XMMRegister r) {
 325   movflt(r, Address(rsp, 0));
 326   addptr(rsp, wordSize);
 327 }
 328 
 329 void InterpreterMacroAssembler::pop_d(XMMRegister r) {
 330   movdbl(r, Address(rsp, 0));
 331   addptr(rsp, 2 * Interpreter::stackElementSize);
 332 }
 333 
 334 void InterpreterMacroAssembler::push_ptr(Register r) {
 335   push(r);
 336 }
 337 
 338 void InterpreterMacroAssembler::push_i(Register r) {
 339   push(r);
 340 }
 341 
 342 void InterpreterMacroAssembler::push_l(Register r) {
 343   subptr(rsp, 2 * wordSize);
 344   movq(Address(rsp, 0), r);
 345 }
 346 
 347 void InterpreterMacroAssembler::push_f(XMMRegister r) {
 348   subptr(rsp, wordSize);
 349   movflt(Address(rsp, 0), r);
 350 }
 351 
 352 void InterpreterMacroAssembler::push_d(XMMRegister r) {
 353   subptr(rsp, 2 * wordSize);
 354   movdbl(Address(rsp, 0), r);
 355 }
 356 
 357 void InterpreterMacroAssembler::pop(TosState state) {
 358   switch (state) {
 359   case atos: pop_ptr();                 break;
 360   case btos:
 361   case ctos:
 362   case stos:
 363   case itos: pop_i();                   break;
 364   case ltos: pop_l();                   break;
 365   case ftos: pop_f();                   break;
 366   case dtos: pop_d();                   break;
 367   case vtos: /* nothing to do */        break;
 368   default:   ShouldNotReachHere();
 369   }
 370   verify_oop(rax, state);
 371 }
 372 
 373 void InterpreterMacroAssembler::push(TosState state) {
 374   verify_oop(rax, state);
 375   switch (state) {
 376   case atos: push_ptr();                break;
 377   case btos:
 378   case ctos:
 379   case stos:
 380   case itos: push_i();                  break;
 381   case ltos: push_l();                  break;
 382   case ftos: push_f();                  break;
 383   case dtos: push_d();                  break;
 384   case vtos: /* nothing to do */        break;
 385   default  : ShouldNotReachHere();
 386   }
 387 }
 388 
 389 
 390 // Helpers for swap and dup
 391 void InterpreterMacroAssembler::load_ptr(int n, Register val) {
 392   movptr(val, Address(rsp, Interpreter::expr_offset_in_bytes(n)));
 393 }
 394 
 395 void InterpreterMacroAssembler::store_ptr(int n, Register val) {
 396   movptr(Address(rsp, Interpreter::expr_offset_in_bytes(n)), val);
 397 }
 398 
 399 
 400 void InterpreterMacroAssembler::prepare_to_jump_from_interpreted() {
 401   // set sender sp
 402   lea(r13, Address(rsp, wordSize));
 403   // record last_sp
 404   movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), r13);
 405 }
 406 
 407 
 408 // Jump to from_interpreted entry of a call unless single stepping is possible
 409 // in this thread in which case we must call the i2i entry
 410 void InterpreterMacroAssembler::jump_from_interpreted(Register method, Register temp) {
 411   prepare_to_jump_from_interpreted();
 412 
 413   if (JvmtiExport::can_post_interpreter_events()) {
 414     Label run_compiled_code;
 415     // JVMTI events, such as single-stepping, are implemented partly by avoiding running
 416     // compiled code in threads for which the event is enabled.  Check here for
 417     // interp_only_mode if these events CAN be enabled.
 418     // interp_only is an int, on little endian it is sufficient to test the byte only
 419     // Is a cmpl faster?
 420     cmpb(Address(r15_thread, JavaThread::interp_only_mode_offset()), 0);
 421     jccb(Assembler::zero, run_compiled_code);
 422     jmp(Address(method, methodOopDesc::interpreter_entry_offset()));
 423     bind(run_compiled_code);
 424   }
 425 
 426   jmp(Address(method, methodOopDesc::from_interpreted_offset()));
 427 
 428 }
 429 
 430 
 431 // The following two routines provide a hook so that an implementation
 432 // can schedule the dispatch in two parts.  amd64 does not do this.
 433 void InterpreterMacroAssembler::dispatch_prolog(TosState state, int step) {
 434   // Nothing amd64 specific to be done here
 435 }
 436 
 437 void InterpreterMacroAssembler::dispatch_epilog(TosState state, int step) {
 438   dispatch_next(state, step);
 439 }
 440 
 441 void InterpreterMacroAssembler::dispatch_base(TosState state,
 442                                               address* table,
 443                                               bool verifyoop) {
 444   verify_FPU(1, state);
 445   if (VerifyActivationFrameSize) {
 446     Label L;
 447     mov(rcx, rbp);
 448     subptr(rcx, rsp);
 449     int32_t min_frame_size =
 450       (frame::link_offset - frame::interpreter_frame_initial_sp_offset) *
 451       wordSize;
 452     cmpptr(rcx, (int32_t)min_frame_size);
 453     jcc(Assembler::greaterEqual, L);
 454     stop("broken stack frame");
 455     bind(L);
 456   }
 457   if (verifyoop) {
 458     verify_oop(rax, state);
 459   }
 460   lea(rscratch1, ExternalAddress((address)table));
 461   jmp(Address(rscratch1, rbx, Address::times_8));
 462 }
 463 
 464 void InterpreterMacroAssembler::dispatch_only(TosState state) {
 465   dispatch_base(state, Interpreter::dispatch_table(state));
 466 }
 467 
 468 void InterpreterMacroAssembler::dispatch_only_normal(TosState state) {
 469   dispatch_base(state, Interpreter::normal_table(state));
 470 }
 471 
 472 void InterpreterMacroAssembler::dispatch_only_noverify(TosState state) {
 473   dispatch_base(state, Interpreter::normal_table(state), false);
 474 }
 475 
 476 
 477 void InterpreterMacroAssembler::dispatch_next(TosState state, int step) {
 478   // load next bytecode (load before advancing r13 to prevent AGI)
 479   load_unsigned_byte(rbx, Address(r13, step));
 480   // advance r13
 481   increment(r13, step);
 482   dispatch_base(state, Interpreter::dispatch_table(state));
 483 }
 484 
 485 void InterpreterMacroAssembler::dispatch_via(TosState state, address* table) {
 486   // load current bytecode
 487   load_unsigned_byte(rbx, Address(r13, 0));
 488   dispatch_base(state, table);
 489 }
 490 
 491 // remove activation
 492 //
 493 // Unlock the receiver if this is a synchronized method.
 494 // Unlock any Java monitors from syncronized blocks.
 495 // Remove the activation from the stack.
 496 //
 497 // If there are locked Java monitors
 498 //    If throw_monitor_exception
 499 //       throws IllegalMonitorStateException
 500 //    Else if install_monitor_exception
 501 //       installs IllegalMonitorStateException
 502 //    Else
 503 //       no error processing
 504 void InterpreterMacroAssembler::remove_activation(
 505         TosState state,
 506         Register ret_addr,
 507         bool throw_monitor_exception,
 508         bool install_monitor_exception,
 509         bool notify_jvmdi) {
 510   // Note: Registers rdx xmm0 may be in use for the
 511   // result check if synchronized method
 512   Label unlocked, unlock, no_unlock;
 513 
 514   // get the value of _do_not_unlock_if_synchronized into rdx
 515   const Address do_not_unlock_if_synchronized(r15_thread,
 516     in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
 517   movbool(rdx, do_not_unlock_if_synchronized);
 518   movbool(do_not_unlock_if_synchronized, false); // reset the flag
 519 
 520  // get method access flags
 521   movptr(rbx, Address(rbp, frame::interpreter_frame_method_offset * wordSize));
 522   movl(rcx, Address(rbx, methodOopDesc::access_flags_offset()));
 523   testl(rcx, JVM_ACC_SYNCHRONIZED);
 524   jcc(Assembler::zero, unlocked);
 525 
 526   // Don't unlock anything if the _do_not_unlock_if_synchronized flag
 527   // is set.
 528   testbool(rdx);
 529   jcc(Assembler::notZero, no_unlock);
 530 
 531   // unlock monitor
 532   push(state); // save result
 533 
 534   // BasicObjectLock will be first in list, since this is a
 535   // synchronized method. However, need to check that the object has
 536   // not been unlocked by an explicit monitorexit bytecode.
 537   const Address monitor(rbp, frame::interpreter_frame_initial_sp_offset *
 538                         wordSize - (int) sizeof(BasicObjectLock));
 539   // We use c_rarg1 so that if we go slow path it will be the correct
 540   // register for unlock_object to pass to VM directly
 541   lea(c_rarg1, monitor); // address of first monitor
 542 
 543   movptr(rax, Address(c_rarg1, BasicObjectLock::obj_offset_in_bytes()));
 544   testptr(rax, rax);
 545   jcc(Assembler::notZero, unlock);
 546 
 547   pop(state);
 548   if (throw_monitor_exception) {
 549     // Entry already unlocked, need to throw exception
 550     call_VM(noreg, CAST_FROM_FN_PTR(address,
 551                    InterpreterRuntime::throw_illegal_monitor_state_exception));
 552     should_not_reach_here();
 553   } else {
 554     // Monitor already unlocked during a stack unroll. If requested,
 555     // install an illegal_monitor_state_exception.  Continue with
 556     // stack unrolling.
 557     if (install_monitor_exception) {
 558       call_VM(noreg, CAST_FROM_FN_PTR(address,
 559                      InterpreterRuntime::new_illegal_monitor_state_exception));
 560     }
 561     jmp(unlocked);
 562   }
 563 
 564   bind(unlock);
 565   unlock_object(c_rarg1);
 566   pop(state);
 567 
 568   // Check that for block-structured locking (i.e., that all locked
 569   // objects has been unlocked)
 570   bind(unlocked);
 571 
 572   // rax: Might contain return value
 573 
 574   // Check that all monitors are unlocked
 575   {
 576     Label loop, exception, entry, restart;
 577     const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
 578     const Address monitor_block_top(
 579         rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
 580     const Address monitor_block_bot(
 581         rbp, frame::interpreter_frame_initial_sp_offset * wordSize);
 582 
 583     bind(restart);
 584     // We use c_rarg1 so that if we go slow path it will be the correct
 585     // register for unlock_object to pass to VM directly
 586     movptr(c_rarg1, monitor_block_top); // points to current entry, starting
 587                                   // with top-most entry
 588     lea(rbx, monitor_block_bot);  // points to word before bottom of
 589                                   // monitor block
 590     jmp(entry);
 591 
 592     // Entry already locked, need to throw exception
 593     bind(exception);
 594 
 595     if (throw_monitor_exception) {
 596       // Throw exception
 597       MacroAssembler::call_VM(noreg,
 598                               CAST_FROM_FN_PTR(address, InterpreterRuntime::
 599                                    throw_illegal_monitor_state_exception));
 600       should_not_reach_here();
 601     } else {
 602       // Stack unrolling. Unlock object and install illegal_monitor_exception.
 603       // Unlock does not block, so don't have to worry about the frame.
 604       // We don't have to preserve c_rarg1 since we are going to throw an exception.
 605 
 606       push(state);
 607       unlock_object(c_rarg1);
 608       pop(state);
 609 
 610       if (install_monitor_exception) {
 611         call_VM(noreg, CAST_FROM_FN_PTR(address,
 612                                         InterpreterRuntime::
 613                                         new_illegal_monitor_state_exception));
 614       }
 615 
 616       jmp(restart);
 617     }
 618 
 619     bind(loop);
 620     // check if current entry is used
 621     cmpptr(Address(c_rarg1, BasicObjectLock::obj_offset_in_bytes()), (int32_t) NULL);
 622     jcc(Assembler::notEqual, exception);
 623 
 624     addptr(c_rarg1, entry_size); // otherwise advance to next entry
 625     bind(entry);
 626     cmpptr(c_rarg1, rbx); // check if bottom reached
 627     jcc(Assembler::notEqual, loop); // if not at bottom then check this entry
 628   }
 629 
 630   bind(no_unlock);
 631 
 632   // jvmti support
 633   if (notify_jvmdi) {
 634     notify_method_exit(state, NotifyJVMTI);    // preserve TOSCA
 635   } else {
 636     notify_method_exit(state, SkipNotifyJVMTI); // preserve TOSCA
 637   }
 638 
 639   // remove activation
 640   // get sender sp
 641   movptr(rbx,
 642          Address(rbp, frame::interpreter_frame_sender_sp_offset * wordSize));
 643   leave();                           // remove frame anchor
 644   pop(ret_addr);                     // get return address
 645   mov(rsp, rbx);                     // set sp to sender sp
 646 }
 647 
 648 #endif // C_INTERP
 649 
 650 // Lock object
 651 //
 652 // Args:
 653 //      c_rarg1: BasicObjectLock to be used for locking
 654 //
 655 // Kills:
 656 //      rax
 657 //      c_rarg0, c_rarg1, c_rarg2, c_rarg3, .. (param regs)
 658 //      rscratch1, rscratch2 (scratch regs)
 659 void InterpreterMacroAssembler::lock_object(Register lock_reg) {
 660   assert(lock_reg == c_rarg1, "The argument is only for looks. It must be c_rarg1");
 661 
 662   if (UseHeavyMonitors) {
 663     call_VM(noreg,
 664             CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter),
 665             lock_reg);
 666   } else {
 667     Label done;
 668 
 669     const Register swap_reg = rax; // Must use rax for cmpxchg instruction
 670     const Register obj_reg = c_rarg3; // Will contain the oop
 671 
 672     const int obj_offset = BasicObjectLock::obj_offset_in_bytes();
 673     const int lock_offset = BasicObjectLock::lock_offset_in_bytes ();
 674     const int mark_offset = lock_offset +
 675                             BasicLock::displaced_header_offset_in_bytes();
 676 
 677     Label slow_case;
 678 
 679     // Load object pointer into obj_reg %c_rarg3
 680     movptr(obj_reg, Address(lock_reg, obj_offset));
 681 
 682     if (UseBiasedLocking) {
 683       biased_locking_enter(lock_reg, obj_reg, swap_reg, rscratch1, false, done, &slow_case);
 684     }
 685 
 686     // Load immediate 1 into swap_reg %rax
 687     movl(swap_reg, 1);
 688 
 689     // Load (object->mark() | 1) into swap_reg %rax
 690     orptr(swap_reg, Address(obj_reg, 0));
 691 
 692     // Save (object->mark() | 1) into BasicLock's displaced header
 693     movptr(Address(lock_reg, mark_offset), swap_reg);
 694 
 695     assert(lock_offset == 0,
 696            "displached header must be first word in BasicObjectLock");
 697 
 698     if (os::is_MP()) lock();
 699     cmpxchgptr(lock_reg, Address(obj_reg, 0));
 700     if (PrintBiasedLockingStatistics) {
 701       cond_inc32(Assembler::zero,
 702                  ExternalAddress((address) BiasedLocking::fast_path_entry_count_addr()));
 703     }
 704     jcc(Assembler::zero, done);
 705 
 706     // Test if the oopMark is an obvious stack pointer, i.e.,
 707     //  1) (mark & 7) == 0, and
 708     //  2) rsp <= mark < mark + os::pagesize()
 709     //
 710     // These 3 tests can be done by evaluating the following
 711     // expression: ((mark - rsp) & (7 - os::vm_page_size())),
 712     // assuming both stack pointer and pagesize have their
 713     // least significant 3 bits clear.
 714     // NOTE: the oopMark is in swap_reg %rax as the result of cmpxchg
 715     subptr(swap_reg, rsp);
 716     andptr(swap_reg, 7 - os::vm_page_size());
 717 
 718     // Save the test result, for recursive case, the result is zero
 719     movptr(Address(lock_reg, mark_offset), swap_reg);
 720 
 721     if (PrintBiasedLockingStatistics) {
 722       cond_inc32(Assembler::zero,
 723                  ExternalAddress((address) BiasedLocking::fast_path_entry_count_addr()));
 724     }
 725     jcc(Assembler::zero, done);
 726 
 727     bind(slow_case);
 728 
 729     // Call the runtime routine for slow case
 730     call_VM(noreg,
 731             CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter),
 732             lock_reg);
 733 
 734     bind(done);
 735   }
 736 }
 737 
 738 
 739 // Unlocks an object. Used in monitorexit bytecode and
 740 // remove_activation.  Throws an IllegalMonitorException if object is
 741 // not locked by current thread.
 742 //
 743 // Args:
 744 //      c_rarg1: BasicObjectLock for lock
 745 //
 746 // Kills:
 747 //      rax
 748 //      c_rarg0, c_rarg1, c_rarg2, c_rarg3, ... (param regs)
 749 //      rscratch1, rscratch2 (scratch regs)
 750 void InterpreterMacroAssembler::unlock_object(Register lock_reg) {
 751   assert(lock_reg == c_rarg1, "The argument is only for looks. It must be rarg1");
 752 
 753   if (UseHeavyMonitors) {
 754     call_VM(noreg,
 755             CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit),
 756             lock_reg);
 757   } else {
 758     Label done;
 759 
 760     const Register swap_reg   = rax;  // Must use rax for cmpxchg instruction
 761     const Register header_reg = c_rarg2;  // Will contain the old oopMark
 762     const Register obj_reg    = c_rarg3;  // Will contain the oop
 763 
 764     save_bcp(); // Save in case of exception
 765 
 766     // Convert from BasicObjectLock structure to object and BasicLock
 767     // structure Store the BasicLock address into %rax
 768     lea(swap_reg, Address(lock_reg, BasicObjectLock::lock_offset_in_bytes()));
 769 
 770     // Load oop into obj_reg(%c_rarg3)
 771     movptr(obj_reg, Address(lock_reg, BasicObjectLock::obj_offset_in_bytes()));
 772 
 773     // Free entry
 774     movptr(Address(lock_reg, BasicObjectLock::obj_offset_in_bytes()), (int32_t)NULL_WORD);
 775 
 776     if (UseBiasedLocking) {
 777       biased_locking_exit(obj_reg, header_reg, done);
 778     }
 779 
 780     // Load the old header from BasicLock structure
 781     movptr(header_reg, Address(swap_reg,
 782                                BasicLock::displaced_header_offset_in_bytes()));
 783 
 784     // Test for recursion
 785     testptr(header_reg, header_reg);
 786 
 787     // zero for recursive case
 788     jcc(Assembler::zero, done);
 789 
 790     // Atomic swap back the old header
 791     if (os::is_MP()) lock();
 792     cmpxchgptr(header_reg, Address(obj_reg, 0));
 793 
 794     // zero for recursive case
 795     jcc(Assembler::zero, done);
 796 
 797     // Call the runtime routine for slow case.
 798     movptr(Address(lock_reg, BasicObjectLock::obj_offset_in_bytes()),
 799          obj_reg); // restore obj
 800     call_VM(noreg,
 801             CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit),
 802             lock_reg);
 803 
 804     bind(done);
 805 
 806     restore_bcp();
 807   }
 808 }
 809 
 810 #ifndef CC_INTERP
 811 
 812 void InterpreterMacroAssembler::test_method_data_pointer(Register mdp,
 813                                                          Label& zero_continue) {
 814   assert(ProfileInterpreter, "must be profiling interpreter");
 815   movptr(mdp, Address(rbp, frame::interpreter_frame_mdx_offset * wordSize));
 816   testptr(mdp, mdp);
 817   jcc(Assembler::zero, zero_continue);
 818 }
 819 
 820 
 821 // Set the method data pointer for the current bcp.
 822 void InterpreterMacroAssembler::set_method_data_pointer_for_bcp() {
 823   assert(ProfileInterpreter, "must be profiling interpreter");
 824   Label set_mdp;
 825   push(rax);
 826   push(rbx);
 827 
 828   get_method(rbx);
 829   // Test MDO to avoid the call if it is NULL.
 830   movptr(rax, Address(rbx, in_bytes(methodOopDesc::method_data_offset())));
 831   testptr(rax, rax);
 832   jcc(Assembler::zero, set_mdp);
 833   // rbx: method
 834   // r13: bcp
 835   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::bcp_to_di), rbx, r13);
 836   // rax: mdi
 837   // mdo is guaranteed to be non-zero here, we checked for it before the call.
 838   movptr(rbx, Address(rbx, in_bytes(methodOopDesc::method_data_offset())));
 839   addptr(rbx, in_bytes(methodDataOopDesc::data_offset()));
 840   addptr(rax, rbx);
 841   bind(set_mdp);
 842   movptr(Address(rbp, frame::interpreter_frame_mdx_offset * wordSize), rax);
 843   pop(rbx);
 844   pop(rax);
 845 }
 846 
 847 void InterpreterMacroAssembler::verify_method_data_pointer() {
 848   assert(ProfileInterpreter, "must be profiling interpreter");
 849 #ifdef ASSERT
 850   Label verify_continue;
 851   push(rax);
 852   push(rbx);
 853   push(c_rarg3);
 854   push(c_rarg2);
 855   test_method_data_pointer(c_rarg3, verify_continue); // If mdp is zero, continue
 856   get_method(rbx);
 857 
 858   // If the mdp is valid, it will point to a DataLayout header which is
 859   // consistent with the bcp.  The converse is highly probable also.
 860   load_unsigned_short(c_rarg2,
 861                       Address(c_rarg3, in_bytes(DataLayout::bci_offset())));
 862   addptr(c_rarg2, Address(rbx, methodOopDesc::const_offset()));
 863   lea(c_rarg2, Address(c_rarg2, constMethodOopDesc::codes_offset()));
 864   cmpptr(c_rarg2, r13);
 865   jcc(Assembler::equal, verify_continue);
 866   // rbx: method
 867   // r13: bcp
 868   // c_rarg3: mdp
 869   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::verify_mdp),
 870                rbx, r13, c_rarg3);
 871   bind(verify_continue);
 872   pop(c_rarg2);
 873   pop(c_rarg3);
 874   pop(rbx);
 875   pop(rax);
 876 #endif // ASSERT
 877 }
 878 
 879 
 880 void InterpreterMacroAssembler::set_mdp_data_at(Register mdp_in,
 881                                                 int constant,
 882                                                 Register value) {
 883   assert(ProfileInterpreter, "must be profiling interpreter");
 884   Address data(mdp_in, constant);
 885   movptr(data, value);
 886 }
 887 
 888 
 889 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in,
 890                                                       int constant,
 891                                                       bool decrement) {
 892   // Counter address
 893   Address data(mdp_in, constant);
 894 
 895   increment_mdp_data_at(data, decrement);
 896 }
 897 
 898 void InterpreterMacroAssembler::increment_mdp_data_at(Address data,
 899                                                       bool decrement) {
 900   assert(ProfileInterpreter, "must be profiling interpreter");
 901   // %%% this does 64bit counters at best it is wasting space
 902   // at worst it is a rare bug when counters overflow
 903 
 904   if (decrement) {
 905     // Decrement the register.  Set condition codes.
 906     addptr(data, (int32_t) -DataLayout::counter_increment);
 907     // If the decrement causes the counter to overflow, stay negative
 908     Label L;
 909     jcc(Assembler::negative, L);
 910     addptr(data, (int32_t) DataLayout::counter_increment);
 911     bind(L);
 912   } else {
 913     assert(DataLayout::counter_increment == 1,
 914            "flow-free idiom only works with 1");
 915     // Increment the register.  Set carry flag.
 916     addptr(data, DataLayout::counter_increment);
 917     // If the increment causes the counter to overflow, pull back by 1.
 918     sbbptr(data, (int32_t)0);
 919   }
 920 }
 921 
 922 
 923 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in,
 924                                                       Register reg,
 925                                                       int constant,
 926                                                       bool decrement) {
 927   Address data(mdp_in, reg, Address::times_1, constant);
 928 
 929   increment_mdp_data_at(data, decrement);
 930 }
 931 
 932 void InterpreterMacroAssembler::set_mdp_flag_at(Register mdp_in,
 933                                                 int flag_byte_constant) {
 934   assert(ProfileInterpreter, "must be profiling interpreter");
 935   int header_offset = in_bytes(DataLayout::header_offset());
 936   int header_bits = DataLayout::flag_mask_to_header_mask(flag_byte_constant);
 937   // Set the flag
 938   orl(Address(mdp_in, header_offset), header_bits);
 939 }
 940 
 941 
 942 
 943 void InterpreterMacroAssembler::test_mdp_data_at(Register mdp_in,
 944                                                  int offset,
 945                                                  Register value,
 946                                                  Register test_value_out,
 947                                                  Label& not_equal_continue) {
 948   assert(ProfileInterpreter, "must be profiling interpreter");
 949   if (test_value_out == noreg) {
 950     cmpptr(value, Address(mdp_in, offset));
 951   } else {
 952     // Put the test value into a register, so caller can use it:
 953     movptr(test_value_out, Address(mdp_in, offset));
 954     cmpptr(test_value_out, value);
 955   }
 956   jcc(Assembler::notEqual, not_equal_continue);
 957 }
 958 
 959 
 960 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in,
 961                                                      int offset_of_disp) {
 962   assert(ProfileInterpreter, "must be profiling interpreter");
 963   Address disp_address(mdp_in, offset_of_disp);
 964   addptr(mdp_in, disp_address);
 965   movptr(Address(rbp, frame::interpreter_frame_mdx_offset * wordSize), mdp_in);
 966 }
 967 
 968 
 969 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in,
 970                                                      Register reg,
 971                                                      int offset_of_disp) {
 972   assert(ProfileInterpreter, "must be profiling interpreter");
 973   Address disp_address(mdp_in, reg, Address::times_1, offset_of_disp);
 974   addptr(mdp_in, disp_address);
 975   movptr(Address(rbp, frame::interpreter_frame_mdx_offset * wordSize), mdp_in);
 976 }
 977 
 978 
 979 void InterpreterMacroAssembler::update_mdp_by_constant(Register mdp_in,
 980                                                        int constant) {
 981   assert(ProfileInterpreter, "must be profiling interpreter");
 982   addptr(mdp_in, constant);
 983   movptr(Address(rbp, frame::interpreter_frame_mdx_offset * wordSize), mdp_in);
 984 }
 985 
 986 
 987 void InterpreterMacroAssembler::update_mdp_for_ret(Register return_bci) {
 988   assert(ProfileInterpreter, "must be profiling interpreter");
 989   push(return_bci); // save/restore across call_VM
 990   call_VM(noreg,
 991           CAST_FROM_FN_PTR(address, InterpreterRuntime::update_mdp_for_ret),
 992           return_bci);
 993   pop(return_bci);
 994 }
 995 
 996 
 997 void InterpreterMacroAssembler::profile_taken_branch(Register mdp,
 998                                                      Register bumped_count) {
 999   if (ProfileInterpreter) {
1000     Label profile_continue;
1001 
1002     // If no method data exists, go to profile_continue.
1003     // Otherwise, assign to mdp
1004     test_method_data_pointer(mdp, profile_continue);
1005 
1006     // We are taking a branch.  Increment the taken count.
1007     // We inline increment_mdp_data_at to return bumped_count in a register
1008     //increment_mdp_data_at(mdp, in_bytes(JumpData::taken_offset()));
1009     Address data(mdp, in_bytes(JumpData::taken_offset()));
1010     movptr(bumped_count, data);
1011     assert(DataLayout::counter_increment == 1,
1012             "flow-free idiom only works with 1");
1013     addptr(bumped_count, DataLayout::counter_increment);
1014     sbbptr(bumped_count, 0);
1015     movptr(data, bumped_count); // Store back out
1016 
1017     // The method data pointer needs to be updated to reflect the new target.
1018     update_mdp_by_offset(mdp, in_bytes(JumpData::displacement_offset()));
1019     bind(profile_continue);
1020   }
1021 }
1022 
1023 
1024 void InterpreterMacroAssembler::profile_not_taken_branch(Register mdp) {
1025   if (ProfileInterpreter) {
1026     Label profile_continue;
1027 
1028     // If no method data exists, go to profile_continue.
1029     test_method_data_pointer(mdp, profile_continue);
1030 
1031     // We are taking a branch.  Increment the not taken count.
1032     increment_mdp_data_at(mdp, in_bytes(BranchData::not_taken_offset()));
1033 
1034     // The method data pointer needs to be updated to correspond to
1035     // the next bytecode
1036     update_mdp_by_constant(mdp, in_bytes(BranchData::branch_data_size()));
1037     bind(profile_continue);
1038   }
1039 }
1040 
1041 
1042 void InterpreterMacroAssembler::profile_call(Register mdp) {
1043   if (ProfileInterpreter) {
1044     Label profile_continue;
1045 
1046     // If no method data exists, go to profile_continue.
1047     test_method_data_pointer(mdp, profile_continue);
1048 
1049     // We are making a call.  Increment the count.
1050     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1051 
1052     // The method data pointer needs to be updated to reflect the new target.
1053     update_mdp_by_constant(mdp, in_bytes(CounterData::counter_data_size()));
1054     bind(profile_continue);
1055   }
1056 }
1057 
1058 
1059 void InterpreterMacroAssembler::profile_final_call(Register mdp) {
1060   if (ProfileInterpreter) {
1061     Label profile_continue;
1062 
1063     // If no method data exists, go to profile_continue.
1064     test_method_data_pointer(mdp, profile_continue);
1065 
1066     // We are making a call.  Increment the count.
1067     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1068 
1069     // The method data pointer needs to be updated to reflect the new target.
1070     update_mdp_by_constant(mdp,
1071                            in_bytes(VirtualCallData::
1072                                     virtual_call_data_size()));
1073     bind(profile_continue);
1074   }
1075 }
1076 
1077 
1078 void InterpreterMacroAssembler::profile_virtual_call(Register receiver,
1079                                                      Register mdp,
1080                                                      Register reg2,
1081                                                      bool receiver_can_be_null) {
1082   if (ProfileInterpreter) {
1083     Label profile_continue;
1084 
1085     // If no method data exists, go to profile_continue.
1086     test_method_data_pointer(mdp, profile_continue);
1087 
1088     Label skip_receiver_profile;
1089     if (receiver_can_be_null) {
1090       Label not_null;
1091       testptr(receiver, receiver);
1092       jccb(Assembler::notZero, not_null);
1093       // We are making a call.  Increment the count for null receiver.
1094       increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1095       jmp(skip_receiver_profile);
1096       bind(not_null);
1097     }
1098 
1099     // Record the receiver type.
1100     record_klass_in_profile(receiver, mdp, reg2, true);
1101     bind(skip_receiver_profile);
1102 
1103     // The method data pointer needs to be updated to reflect the new target.
1104     update_mdp_by_constant(mdp,
1105                            in_bytes(VirtualCallData::
1106                                     virtual_call_data_size()));
1107     bind(profile_continue);
1108   }
1109 }
1110 
1111 // This routine creates a state machine for updating the multi-row
1112 // type profile at a virtual call site (or other type-sensitive bytecode).
1113 // The machine visits each row (of receiver/count) until the receiver type
1114 // is found, or until it runs out of rows.  At the same time, it remembers
1115 // the location of the first empty row.  (An empty row records null for its
1116 // receiver, and can be allocated for a newly-observed receiver type.)
1117 // Because there are two degrees of freedom in the state, a simple linear
1118 // search will not work; it must be a decision tree.  Hence this helper
1119 // function is recursive, to generate the required tree structured code.
1120 // It's the interpreter, so we are trading off code space for speed.
1121 // See below for example code.
1122 void InterpreterMacroAssembler::record_klass_in_profile_helper(
1123                                         Register receiver, Register mdp,
1124                                         Register reg2, int start_row,
1125                                         Label& done, bool is_virtual_call) {
1126   if (TypeProfileWidth == 0) {
1127     if (is_virtual_call) {
1128       increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1129     }
1130     return;
1131   }
1132 
1133   int last_row = VirtualCallData::row_limit() - 1;
1134   assert(start_row <= last_row, "must be work left to do");
1135   // Test this row for both the receiver and for null.
1136   // Take any of three different outcomes:
1137   //   1. found receiver => increment count and goto done
1138   //   2. found null => keep looking for case 1, maybe allocate this cell
1139   //   3. found something else => keep looking for cases 1 and 2
1140   // Case 3 is handled by a recursive call.
1141   for (int row = start_row; row <= last_row; row++) {
1142     Label next_test;
1143     bool test_for_null_also = (row == start_row);
1144 
1145     // See if the receiver is receiver[n].
1146     int recvr_offset = in_bytes(VirtualCallData::receiver_offset(row));
1147     test_mdp_data_at(mdp, recvr_offset, receiver,
1148                      (test_for_null_also ? reg2 : noreg),
1149                      next_test);
1150     // (Reg2 now contains the receiver from the CallData.)
1151 
1152     // The receiver is receiver[n].  Increment count[n].
1153     int count_offset = in_bytes(VirtualCallData::receiver_count_offset(row));
1154     increment_mdp_data_at(mdp, count_offset);
1155     jmp(done);
1156     bind(next_test);
1157 
1158     if (test_for_null_also) {
1159       Label found_null;
1160       // Failed the equality check on receiver[n]...  Test for null.
1161       testptr(reg2, reg2);
1162       if (start_row == last_row) {
1163         // The only thing left to do is handle the null case.
1164         if (is_virtual_call) {
1165           jccb(Assembler::zero, found_null);
1166           // Receiver did not match any saved receiver and there is no empty row for it.
1167           // Increment total counter to indicate polymorphic case.
1168           increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1169           jmp(done);
1170           bind(found_null);
1171         } else {
1172           jcc(Assembler::notZero, done);
1173         }
1174         break;
1175       }
1176       // Since null is rare, make it be the branch-taken case.
1177       jcc(Assembler::zero, found_null);
1178 
1179       // Put all the "Case 3" tests here.
1180       record_klass_in_profile_helper(receiver, mdp, reg2, start_row + 1, done, is_virtual_call);
1181 
1182       // Found a null.  Keep searching for a matching receiver,
1183       // but remember that this is an empty (unused) slot.
1184       bind(found_null);
1185     }
1186   }
1187 
1188   // In the fall-through case, we found no matching receiver, but we
1189   // observed the receiver[start_row] is NULL.
1190 
1191   // Fill in the receiver field and increment the count.
1192   int recvr_offset = in_bytes(VirtualCallData::receiver_offset(start_row));
1193   set_mdp_data_at(mdp, recvr_offset, receiver);
1194   int count_offset = in_bytes(VirtualCallData::receiver_count_offset(start_row));
1195   movl(reg2, DataLayout::counter_increment);
1196   set_mdp_data_at(mdp, count_offset, reg2);
1197   if (start_row > 0) {
1198     jmp(done);
1199   }
1200 }
1201 
1202 // Example state machine code for three profile rows:
1203 //   // main copy of decision tree, rooted at row[1]
1204 //   if (row[0].rec == rec) { row[0].incr(); goto done; }
1205 //   if (row[0].rec != NULL) {
1206 //     // inner copy of decision tree, rooted at row[1]
1207 //     if (row[1].rec == rec) { row[1].incr(); goto done; }
1208 //     if (row[1].rec != NULL) {
1209 //       // degenerate decision tree, rooted at row[2]
1210 //       if (row[2].rec == rec) { row[2].incr(); goto done; }
1211 //       if (row[2].rec != NULL) { count.incr(); goto done; } // overflow
1212 //       row[2].init(rec); goto done;
1213 //     } else {
1214 //       // remember row[1] is empty
1215 //       if (row[2].rec == rec) { row[2].incr(); goto done; }
1216 //       row[1].init(rec); goto done;
1217 //     }
1218 //   } else {
1219 //     // remember row[0] is empty
1220 //     if (row[1].rec == rec) { row[1].incr(); goto done; }
1221 //     if (row[2].rec == rec) { row[2].incr(); goto done; }
1222 //     row[0].init(rec); goto done;
1223 //   }
1224 //   done:
1225 
1226 void InterpreterMacroAssembler::record_klass_in_profile(Register receiver,
1227                                                         Register mdp, Register reg2,
1228                                                         bool is_virtual_call) {
1229   assert(ProfileInterpreter, "must be profiling");
1230   Label done;
1231 
1232   record_klass_in_profile_helper(receiver, mdp, reg2, 0, done, is_virtual_call);
1233 
1234   bind (done);
1235 }
1236 
1237 void InterpreterMacroAssembler::profile_ret(Register return_bci,
1238                                             Register mdp) {
1239   if (ProfileInterpreter) {
1240     Label profile_continue;
1241     uint row;
1242 
1243     // If no method data exists, go to profile_continue.
1244     test_method_data_pointer(mdp, profile_continue);
1245 
1246     // Update the total ret count.
1247     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1248 
1249     for (row = 0; row < RetData::row_limit(); row++) {
1250       Label next_test;
1251 
1252       // See if return_bci is equal to bci[n]:
1253       test_mdp_data_at(mdp,
1254                        in_bytes(RetData::bci_offset(row)),
1255                        return_bci, noreg,
1256                        next_test);
1257 
1258       // return_bci is equal to bci[n].  Increment the count.
1259       increment_mdp_data_at(mdp, in_bytes(RetData::bci_count_offset(row)));
1260 
1261       // The method data pointer needs to be updated to reflect the new target.
1262       update_mdp_by_offset(mdp,
1263                            in_bytes(RetData::bci_displacement_offset(row)));
1264       jmp(profile_continue);
1265       bind(next_test);
1266     }
1267 
1268     update_mdp_for_ret(return_bci);
1269 
1270     bind(profile_continue);
1271   }
1272 }
1273 
1274 
1275 void InterpreterMacroAssembler::profile_null_seen(Register mdp) {
1276   if (ProfileInterpreter) {
1277     Label profile_continue;
1278 
1279     // If no method data exists, go to profile_continue.
1280     test_method_data_pointer(mdp, profile_continue);
1281 
1282     set_mdp_flag_at(mdp, BitData::null_seen_byte_constant());
1283 
1284     // The method data pointer needs to be updated.
1285     int mdp_delta = in_bytes(BitData::bit_data_size());
1286     if (TypeProfileCasts) {
1287       mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1288     }
1289     update_mdp_by_constant(mdp, mdp_delta);
1290 
1291     bind(profile_continue);
1292   }
1293 }
1294 
1295 
1296 void InterpreterMacroAssembler::profile_typecheck_failed(Register mdp) {
1297   if (ProfileInterpreter && TypeProfileCasts) {
1298     Label profile_continue;
1299 
1300     // If no method data exists, go to profile_continue.
1301     test_method_data_pointer(mdp, profile_continue);
1302 
1303     int count_offset = in_bytes(CounterData::count_offset());
1304     // Back up the address, since we have already bumped the mdp.
1305     count_offset -= in_bytes(VirtualCallData::virtual_call_data_size());
1306 
1307     // *Decrement* the counter.  We expect to see zero or small negatives.
1308     increment_mdp_data_at(mdp, count_offset, true);
1309 
1310     bind (profile_continue);
1311   }
1312 }
1313 
1314 
1315 void InterpreterMacroAssembler::profile_typecheck(Register mdp, Register klass, Register reg2) {
1316   if (ProfileInterpreter) {
1317     Label profile_continue;
1318 
1319     // If no method data exists, go to profile_continue.
1320     test_method_data_pointer(mdp, profile_continue);
1321 
1322     // The method data pointer needs to be updated.
1323     int mdp_delta = in_bytes(BitData::bit_data_size());
1324     if (TypeProfileCasts) {
1325       mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1326 
1327       // Record the object type.
1328       record_klass_in_profile(klass, mdp, reg2, false);
1329     }
1330     update_mdp_by_constant(mdp, mdp_delta);
1331 
1332     bind(profile_continue);
1333   }
1334 }
1335 
1336 
1337 void InterpreterMacroAssembler::profile_switch_default(Register mdp) {
1338   if (ProfileInterpreter) {
1339     Label profile_continue;
1340 
1341     // If no method data exists, go to profile_continue.
1342     test_method_data_pointer(mdp, profile_continue);
1343 
1344     // Update the default case count
1345     increment_mdp_data_at(mdp,
1346                           in_bytes(MultiBranchData::default_count_offset()));
1347 
1348     // The method data pointer needs to be updated.
1349     update_mdp_by_offset(mdp,
1350                          in_bytes(MultiBranchData::
1351                                   default_displacement_offset()));
1352 
1353     bind(profile_continue);
1354   }
1355 }
1356 
1357 
1358 void InterpreterMacroAssembler::profile_switch_case(Register index,
1359                                                     Register mdp,
1360                                                     Register reg2) {
1361   if (ProfileInterpreter) {
1362     Label profile_continue;
1363 
1364     // If no method data exists, go to profile_continue.
1365     test_method_data_pointer(mdp, profile_continue);
1366 
1367     // Build the base (index * per_case_size_in_bytes()) +
1368     // case_array_offset_in_bytes()
1369     movl(reg2, in_bytes(MultiBranchData::per_case_size()));
1370     imulptr(index, reg2); // XXX l ?
1371     addptr(index, in_bytes(MultiBranchData::case_array_offset())); // XXX l ?
1372 
1373     // Update the case count
1374     increment_mdp_data_at(mdp,
1375                           index,
1376                           in_bytes(MultiBranchData::relative_count_offset()));
1377 
1378     // The method data pointer needs to be updated.
1379     update_mdp_by_offset(mdp,
1380                          index,
1381                          in_bytes(MultiBranchData::
1382                                   relative_displacement_offset()));
1383 
1384     bind(profile_continue);
1385   }
1386 }
1387 
1388 
1389 
1390 void InterpreterMacroAssembler::verify_oop(Register reg, TosState state) {
1391   if (state == atos) {
1392     MacroAssembler::verify_oop(reg);
1393   }
1394 }
1395 
1396 void InterpreterMacroAssembler::verify_FPU(int stack_depth, TosState state) {
1397 }
1398 #endif // !CC_INTERP
1399 
1400 
1401 void InterpreterMacroAssembler::notify_method_entry() {
1402   // Whenever JVMTI is interp_only_mode, method entry/exit events are sent to
1403   // track stack depth.  If it is possible to enter interp_only_mode we add
1404   // the code to check if the event should be sent.
1405   if (JvmtiExport::can_post_interpreter_events()) {
1406     Label L;
1407     movl(rdx, Address(r15_thread, JavaThread::interp_only_mode_offset()));
1408     testl(rdx, rdx);
1409     jcc(Assembler::zero, L);
1410     call_VM(noreg, CAST_FROM_FN_PTR(address,
1411                                     InterpreterRuntime::post_method_entry));
1412     bind(L);
1413   }
1414 
1415   {
1416     SkipIfEqual skip(this, &DTraceMethodProbes, false);
1417     get_method(c_rarg1);
1418     call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry),
1419                  r15_thread, c_rarg1);
1420   }
1421 
1422   // RedefineClasses() tracing support for obsolete method entry
1423   if (RC_TRACE_IN_RANGE(0x00001000, 0x00002000)) {
1424     get_method(c_rarg1);
1425     call_VM_leaf(
1426       CAST_FROM_FN_PTR(address, SharedRuntime::rc_trace_method_entry),
1427       r15_thread, c_rarg1);
1428   }
1429 }
1430 
1431 
1432 void InterpreterMacroAssembler::notify_method_exit(
1433     TosState state, NotifyMethodExitMode mode) {
1434   // Whenever JVMTI is interp_only_mode, method entry/exit events are sent to
1435   // track stack depth.  If it is possible to enter interp_only_mode we add
1436   // the code to check if the event should be sent.
1437   if (mode == NotifyJVMTI && JvmtiExport::can_post_interpreter_events()) {
1438     Label L;
1439     // Note: frame::interpreter_frame_result has a dependency on how the
1440     // method result is saved across the call to post_method_exit. If this
1441     // is changed then the interpreter_frame_result implementation will
1442     // need to be updated too.
1443 
1444     // For c++ interpreter the result is always stored at a known location in the frame
1445     // template interpreter will leave it on the top of the stack.
1446     NOT_CC_INTERP(push(state);)
1447     movl(rdx, Address(r15_thread, JavaThread::interp_only_mode_offset()));
1448     testl(rdx, rdx);
1449     jcc(Assembler::zero, L);
1450     call_VM(noreg,
1451             CAST_FROM_FN_PTR(address, InterpreterRuntime::post_method_exit));
1452     bind(L);
1453     NOT_CC_INTERP(pop(state));
1454   }
1455 
1456   {
1457     SkipIfEqual skip(this, &DTraceMethodProbes, false);
1458     NOT_CC_INTERP(push(state));
1459     get_method(c_rarg1);
1460     call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit),
1461                  r15_thread, c_rarg1);
1462     NOT_CC_INTERP(pop(state));
1463   }
1464 }
1465 
1466 // Jump if ((*counter_addr += increment) & mask) satisfies the condition.
1467 void InterpreterMacroAssembler::increment_mask_and_jump(Address counter_addr,
1468                                                         int increment, int mask,
1469                                                         Register scratch, bool preloaded,
1470                                                         Condition cond, Label* where) {
1471   if (!preloaded) {
1472     movl(scratch, counter_addr);
1473   }
1474   incrementl(scratch, increment);
1475   movl(counter_addr, scratch);
1476   andl(scratch, mask);
1477   jcc(cond, *where);
1478 }