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