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