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