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