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