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