1 /*
   2  * Copyright 2003-2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any 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_and_index_at_bcp(Register cache,
 189                                                            Register index,
 190                                                            int bcp_offset) {
 191   assert(bcp_offset > 0, "bcp is still pointing to start of bytecode");
 192   assert(cache != index, "must use different registers");
 193   load_unsigned_short(index, Address(r13, bcp_offset));
 194   movptr(cache, Address(rbp, frame::interpreter_frame_cache_offset * wordSize));
 195   assert(sizeof(ConstantPoolCacheEntry) == 4 * wordSize, "adjust code below");
 196   // convert from field index to ConstantPoolCacheEntry index
 197   shll(index, 2);
 198 }
 199 
 200 
 201 void InterpreterMacroAssembler::get_cache_entry_pointer_at_bcp(Register cache,
 202                                                                Register tmp,
 203                                                                int bcp_offset) {
 204   assert(bcp_offset > 0, "bcp is still pointing to start of bytecode");
 205   assert(cache != tmp, "must use different register");
 206   load_unsigned_short(tmp, Address(r13, bcp_offset));
 207   assert(sizeof(ConstantPoolCacheEntry) == 4 * wordSize, "adjust code below");
 208   // convert from field index to ConstantPoolCacheEntry index
 209   // and from word offset to byte offset
 210   shll(tmp, 2 + LogBytesPerWord);
 211   movptr(cache, Address(rbp, frame::interpreter_frame_cache_offset * wordSize));
 212   // skip past the header
 213   addptr(cache, in_bytes(constantPoolCacheOopDesc::base_offset()));
 214   addptr(cache, tmp);  // construct pointer to cache entry
 215 }
 216 
 217 
 218 // Generate a subtype check: branch to ok_is_subtype if sub_klass is a
 219 // subtype of super_klass.
 220 //
 221 // Args:
 222 //      rax: superklass
 223 //      Rsub_klass: subklass
 224 //
 225 // Kills:
 226 //      rcx, rdi
 227 void InterpreterMacroAssembler::gen_subtype_check(Register Rsub_klass,
 228                                                   Label& ok_is_subtype) {
 229   assert(Rsub_klass != rax, "rax holds superklass");
 230   assert(Rsub_klass != r14, "r14 holds locals");
 231   assert(Rsub_klass != r13, "r13 holds bcp");
 232   assert(Rsub_klass != rcx, "rcx holds 2ndary super array length");
 233   assert(Rsub_klass != rdi, "rdi holds 2ndary super array scan ptr");
 234 
 235   // Profile the not-null value's klass.
 236   profile_typecheck(rcx, Rsub_klass, rdi); // blows rcx, reloads rdi
 237 
 238   // Do the check.
 239   check_klass_subtype(Rsub_klass, rax, rcx, ok_is_subtype); // blows rcx
 240 
 241   // Profile the failure of the check.
 242   profile_typecheck_failed(rcx); // blows rcx
 243 }
 244 
 245 
 246 
 247 // Java Expression Stack
 248 
 249 #ifdef ASSERT
 250 // Verifies that the stack tag matches.  Must be called before the stack
 251 // value is popped off the stack.
 252 void InterpreterMacroAssembler::verify_stack_tag(frame::Tag t) {
 253   if (TaggedStackInterpreter) {
 254     frame::Tag tag = t;
 255     if (t == frame::TagCategory2) {
 256       tag = frame::TagValue;
 257       Label hokay;
 258       cmpptr(Address(rsp, 3*wordSize), (int32_t)tag);
 259       jcc(Assembler::equal, hokay);
 260       stop("Java Expression stack tag high value is bad");
 261       bind(hokay);
 262     }
 263     Label okay;
 264     cmpptr(Address(rsp, wordSize), (int32_t)tag);
 265     jcc(Assembler::equal, okay);
 266     // Also compare if the stack value is zero, then the tag might
 267     // not have been set coming from deopt.
 268     cmpptr(Address(rsp, 0), 0);
 269     jcc(Assembler::equal, okay);
 270     stop("Java Expression stack tag value is bad");
 271     bind(okay);
 272   }
 273 }
 274 #endif // ASSERT
 275 
 276 void InterpreterMacroAssembler::pop_ptr(Register r) {
 277   debug_only(verify_stack_tag(frame::TagReference));
 278   pop(r);
 279   if (TaggedStackInterpreter) addptr(rsp, 1 * wordSize);
 280 }
 281 
 282 void InterpreterMacroAssembler::pop_ptr(Register r, Register tag) {
 283   pop(r);
 284   if (TaggedStackInterpreter) pop(tag);
 285 }
 286 
 287 void InterpreterMacroAssembler::pop_i(Register r) {
 288   // XXX can't use pop currently, upper half non clean
 289   debug_only(verify_stack_tag(frame::TagValue));
 290   movl(r, Address(rsp, 0));
 291   addptr(rsp, wordSize);
 292   if (TaggedStackInterpreter) addptr(rsp, 1 * wordSize);
 293 }
 294 
 295 void InterpreterMacroAssembler::pop_l(Register r) {
 296   debug_only(verify_stack_tag(frame::TagCategory2));
 297   movq(r, Address(rsp, 0));
 298   addptr(rsp, 2 * Interpreter::stackElementSize());
 299 }
 300 
 301 void InterpreterMacroAssembler::pop_f(XMMRegister r) {
 302   debug_only(verify_stack_tag(frame::TagValue));
 303   movflt(r, Address(rsp, 0));
 304   addptr(rsp, wordSize);
 305   if (TaggedStackInterpreter) addptr(rsp, 1 * wordSize);
 306 }
 307 
 308 void InterpreterMacroAssembler::pop_d(XMMRegister r) {
 309   debug_only(verify_stack_tag(frame::TagCategory2));
 310   movdbl(r, Address(rsp, 0));
 311   addptr(rsp, 2 * Interpreter::stackElementSize());
 312 }
 313 
 314 void InterpreterMacroAssembler::push_ptr(Register r) {
 315   if (TaggedStackInterpreter) push(frame::TagReference);
 316   push(r);
 317 }
 318 
 319 void InterpreterMacroAssembler::push_ptr(Register r, Register tag) {
 320   if (TaggedStackInterpreter) push(tag);
 321   push(r);
 322 }
 323 
 324 void InterpreterMacroAssembler::push_i(Register r) {
 325   if (TaggedStackInterpreter) push(frame::TagValue);
 326   push(r);
 327 }
 328 
 329 void InterpreterMacroAssembler::push_l(Register r) {
 330   if (TaggedStackInterpreter) {
 331     push(frame::TagValue);
 332     subptr(rsp, 1 * wordSize);
 333     push(frame::TagValue);
 334     subptr(rsp, 1 * wordSize);
 335   } else {
 336     subptr(rsp, 2 * wordSize);
 337   }
 338   movq(Address(rsp, 0), r);
 339 }
 340 
 341 void InterpreterMacroAssembler::push_f(XMMRegister r) {
 342   if (TaggedStackInterpreter) push(frame::TagValue);
 343   subptr(rsp, wordSize);
 344   movflt(Address(rsp, 0), r);
 345 }
 346 
 347 void InterpreterMacroAssembler::push_d(XMMRegister r) {
 348   if (TaggedStackInterpreter) {
 349     push(frame::TagValue);
 350     subptr(rsp, 1 * wordSize);
 351     push(frame::TagValue);
 352     subptr(rsp, 1 * wordSize);
 353   } else {
 354     subptr(rsp, 2 * wordSize);
 355   }
 356   movdbl(Address(rsp, 0), r);
 357 }
 358 
 359 void InterpreterMacroAssembler::pop(TosState state) {
 360   switch (state) {
 361   case atos: pop_ptr();                 break;
 362   case btos:
 363   case ctos:
 364   case stos:
 365   case itos: pop_i();                   break;
 366   case ltos: pop_l();                   break;
 367   case ftos: pop_f();                   break;
 368   case dtos: pop_d();                   break;
 369   case vtos: /* nothing to do */        break;
 370   default:   ShouldNotReachHere();
 371   }
 372   verify_oop(rax, state);
 373 }
 374 
 375 void InterpreterMacroAssembler::push(TosState state) {
 376   verify_oop(rax, state);
 377   switch (state) {
 378   case atos: push_ptr();                break;
 379   case btos:
 380   case ctos:
 381   case stos:
 382   case itos: push_i();                  break;
 383   case ltos: push_l();                  break;
 384   case ftos: push_f();                  break;
 385   case dtos: push_d();                  break;
 386   case vtos: /* nothing to do */        break;
 387   default  : ShouldNotReachHere();
 388   }
 389 }
 390 
 391 
 392 
 393 
 394 // Tagged stack helpers for swap and dup
 395 void InterpreterMacroAssembler::load_ptr_and_tag(int n, Register val,
 396                                                  Register tag) {
 397   movptr(val, Address(rsp, Interpreter::expr_offset_in_bytes(n)));
 398   if (TaggedStackInterpreter) {
 399     movptr(tag, Address(rsp, Interpreter::expr_tag_offset_in_bytes(n)));
 400   }
 401 }
 402 
 403 void InterpreterMacroAssembler::store_ptr_and_tag(int n, Register val,
 404                                                   Register tag) {
 405   movptr(Address(rsp, Interpreter::expr_offset_in_bytes(n)), val);
 406   if (TaggedStackInterpreter) {
 407     movptr(Address(rsp, Interpreter::expr_tag_offset_in_bytes(n)), tag);
 408   }
 409 }
 410 
 411 
 412 // Tagged local support
 413 void InterpreterMacroAssembler::tag_local(frame::Tag tag, int n) {
 414   if (TaggedStackInterpreter) {
 415     if (tag == frame::TagCategory2) {
 416       movptr(Address(r14, Interpreter::local_tag_offset_in_bytes(n+1)),
 417            (int32_t)frame::TagValue);
 418       movptr(Address(r14, Interpreter::local_tag_offset_in_bytes(n)),
 419            (int32_t)frame::TagValue);
 420     } else {
 421       movptr(Address(r14, Interpreter::local_tag_offset_in_bytes(n)), (int32_t)tag);
 422     }
 423   }
 424 }
 425 
 426 void InterpreterMacroAssembler::tag_local(frame::Tag tag, Register idx) {
 427   if (TaggedStackInterpreter) {
 428     if (tag == frame::TagCategory2) {
 429       movptr(Address(r14, idx, Address::times_8,
 430                   Interpreter::local_tag_offset_in_bytes(1)), (int32_t)frame::TagValue);
 431       movptr(Address(r14, idx, Address::times_8,
 432                   Interpreter::local_tag_offset_in_bytes(0)), (int32_t)frame::TagValue);
 433     } else {
 434       movptr(Address(r14, idx, Address::times_8, Interpreter::local_tag_offset_in_bytes(0)),
 435            (int32_t)tag);
 436     }
 437   }
 438 }
 439 
 440 void InterpreterMacroAssembler::tag_local(Register tag, Register idx) {
 441   if (TaggedStackInterpreter) {
 442     // can only be TagValue or TagReference
 443     movptr(Address(r14, idx, Address::times_8, Interpreter::local_tag_offset_in_bytes(0)), tag);
 444   }
 445 }
 446 
 447 
 448 void InterpreterMacroAssembler::tag_local(Register tag, int n) {
 449   if (TaggedStackInterpreter) {
 450     // can only be TagValue or TagReference
 451     movptr(Address(r14, Interpreter::local_tag_offset_in_bytes(n)), tag);
 452   }
 453 }
 454 
 455 #ifdef ASSERT
 456 void InterpreterMacroAssembler::verify_local_tag(frame::Tag tag, int n) {
 457   if (TaggedStackInterpreter) {
 458      frame::Tag t = tag;
 459     if (tag == frame::TagCategory2) {
 460       Label nbl;
 461       t = frame::TagValue;  // change to what is stored in locals
 462       cmpptr(Address(r14, Interpreter::local_tag_offset_in_bytes(n+1)), (int32_t)t);
 463       jcc(Assembler::equal, nbl);
 464       stop("Local tag is bad for long/double");
 465       bind(nbl);
 466     }
 467     Label notBad;
 468     cmpq(Address(r14, Interpreter::local_tag_offset_in_bytes(n)), (int32_t)t);
 469     jcc(Assembler::equal, notBad);
 470     // Also compare if the local value is zero, then the tag might
 471     // not have been set coming from deopt.
 472     cmpptr(Address(r14, Interpreter::local_offset_in_bytes(n)), 0);
 473     jcc(Assembler::equal, notBad);
 474     stop("Local tag is bad");
 475     bind(notBad);
 476   }
 477 }
 478 
 479 void InterpreterMacroAssembler::verify_local_tag(frame::Tag tag, Register idx) {
 480   if (TaggedStackInterpreter) {
 481     frame::Tag t = tag;
 482     if (tag == frame::TagCategory2) {
 483       Label nbl;
 484       t = frame::TagValue;  // change to what is stored in locals
 485       cmpptr(Address(r14, idx, Address::times_8, Interpreter::local_tag_offset_in_bytes(1)), (int32_t)t);
 486       jcc(Assembler::equal, nbl);
 487       stop("Local tag is bad for long/double");
 488       bind(nbl);
 489     }
 490     Label notBad;
 491     cmpptr(Address(r14, idx, Address::times_8, Interpreter::local_tag_offset_in_bytes(0)), (int32_t)t);
 492     jcc(Assembler::equal, notBad);
 493     // Also compare if the local value is zero, then the tag might
 494     // not have been set coming from deopt.
 495     cmpptr(Address(r14, idx, Address::times_8, Interpreter::local_offset_in_bytes(0)), 0);
 496     jcc(Assembler::equal, notBad);
 497     stop("Local tag is bad");
 498     bind(notBad);
 499   }
 500 }
 501 #endif // ASSERT
 502 
 503 
 504 void InterpreterMacroAssembler::super_call_VM_leaf(address entry_point) {
 505   MacroAssembler::call_VM_leaf_base(entry_point, 0);
 506 }
 507 
 508 
 509 void InterpreterMacroAssembler::super_call_VM_leaf(address entry_point,
 510                                                    Register arg_1) {
 511   if (c_rarg0 != arg_1) {
 512     mov(c_rarg0, arg_1);
 513   }
 514   MacroAssembler::call_VM_leaf_base(entry_point, 1);
 515 }
 516 
 517 
 518 void InterpreterMacroAssembler::super_call_VM_leaf(address entry_point,
 519                                                    Register arg_1,
 520                                                    Register arg_2) {
 521   assert(c_rarg0 != arg_2, "smashed argument");
 522   assert(c_rarg1 != arg_1, "smashed argument");
 523   if (c_rarg0 != arg_1) {
 524     mov(c_rarg0, arg_1);
 525   }
 526   if (c_rarg1 != arg_2) {
 527     mov(c_rarg1, arg_2);
 528   }
 529   MacroAssembler::call_VM_leaf_base(entry_point, 2);
 530 }
 531 
 532 void InterpreterMacroAssembler::super_call_VM_leaf(address entry_point,
 533                                                    Register arg_1,
 534                                                    Register arg_2,
 535                                                    Register arg_3) {
 536   assert(c_rarg0 != arg_2, "smashed argument");
 537   assert(c_rarg0 != arg_3, "smashed argument");
 538   assert(c_rarg1 != arg_1, "smashed argument");
 539   assert(c_rarg1 != arg_3, "smashed argument");
 540   assert(c_rarg2 != arg_1, "smashed argument");
 541   assert(c_rarg2 != arg_2, "smashed argument");
 542   if (c_rarg0 != arg_1) {
 543     mov(c_rarg0, arg_1);
 544   }
 545   if (c_rarg1 != arg_2) {
 546     mov(c_rarg1, arg_2);
 547   }
 548   if (c_rarg2 != arg_3) {
 549     mov(c_rarg2, arg_3);
 550   }
 551   MacroAssembler::call_VM_leaf_base(entry_point, 3);
 552 }
 553 
 554 void InterpreterMacroAssembler::prepare_to_jump_from_interpreted() {
 555   // set sender sp
 556   lea(r13, Address(rsp, wordSize));
 557   // record last_sp
 558   movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), r13);
 559 }
 560 
 561 
 562 // Jump to from_interpreted entry of a call unless single stepping is possible
 563 // in this thread in which case we must call the i2i entry
 564 void InterpreterMacroAssembler::jump_from_interpreted(Register method, Register temp) {
 565   prepare_to_jump_from_interpreted();
 566 
 567   if (JvmtiExport::can_post_interpreter_events()) {
 568     Label run_compiled_code;
 569     // JVMTI events, such as single-stepping, are implemented partly by avoiding running
 570     // compiled code in threads for which the event is enabled.  Check here for
 571     // interp_only_mode if these events CAN be enabled.
 572     get_thread(temp);
 573     // interp_only is an int, on little endian it is sufficient to test the byte only
 574     // Is a cmpl faster (ce
 575     cmpb(Address(temp, JavaThread::interp_only_mode_offset()), 0);
 576     jcc(Assembler::zero, run_compiled_code);
 577     jmp(Address(method, methodOopDesc::interpreter_entry_offset()));
 578     bind(run_compiled_code);
 579   }
 580 
 581   jmp(Address(method, methodOopDesc::from_interpreted_offset()));
 582 
 583 }
 584 
 585 
 586 // The following two routines provide a hook so that an implementation
 587 // can schedule the dispatch in two parts.  amd64 does not do this.
 588 void InterpreterMacroAssembler::dispatch_prolog(TosState state, int step) {
 589   // Nothing amd64 specific to be done here
 590 }
 591 
 592 void InterpreterMacroAssembler::dispatch_epilog(TosState state, int step) {
 593   dispatch_next(state, step);
 594 }
 595 
 596 void InterpreterMacroAssembler::dispatch_base(TosState state,
 597                                               address* table,
 598                                               bool verifyoop) {
 599   verify_FPU(1, state);
 600   if (VerifyActivationFrameSize) {
 601     Label L;
 602     mov(rcx, rbp);
 603     subptr(rcx, rsp);
 604     int32_t min_frame_size =
 605       (frame::link_offset - frame::interpreter_frame_initial_sp_offset) *
 606       wordSize;
 607     cmpptr(rcx, (int32_t)min_frame_size);
 608     jcc(Assembler::greaterEqual, L);
 609     stop("broken stack frame");
 610     bind(L);
 611   }
 612   if (verifyoop) {
 613     verify_oop(rax, state);
 614   }
 615   lea(rscratch1, ExternalAddress((address)table));
 616   jmp(Address(rscratch1, rbx, Address::times_8));
 617 }
 618 
 619 void InterpreterMacroAssembler::dispatch_only(TosState state) {
 620   dispatch_base(state, Interpreter::dispatch_table(state));
 621 }
 622 
 623 void InterpreterMacroAssembler::dispatch_only_normal(TosState state) {
 624   dispatch_base(state, Interpreter::normal_table(state));
 625 }
 626 
 627 void InterpreterMacroAssembler::dispatch_only_noverify(TosState state) {
 628   dispatch_base(state, Interpreter::normal_table(state), false);
 629 }
 630 
 631 
 632 void InterpreterMacroAssembler::dispatch_next(TosState state, int step) {
 633   // load next bytecode (load before advancing r13 to prevent AGI)
 634   load_unsigned_byte(rbx, Address(r13, step));
 635   // advance r13
 636   increment(r13, step);
 637   dispatch_base(state, Interpreter::dispatch_table(state));
 638 }
 639 
 640 void InterpreterMacroAssembler::dispatch_via(TosState state, address* table) {
 641   // load current bytecode
 642   load_unsigned_byte(rbx, Address(r13, 0));
 643   dispatch_base(state, table);
 644 }
 645 
 646 // remove activation
 647 //
 648 // Unlock the receiver if this is a synchronized method.
 649 // Unlock any Java monitors from syncronized blocks.
 650 // Remove the activation from the stack.
 651 //
 652 // If there are locked Java monitors
 653 //    If throw_monitor_exception
 654 //       throws IllegalMonitorStateException
 655 //    Else if install_monitor_exception
 656 //       installs IllegalMonitorStateException
 657 //    Else
 658 //       no error processing
 659 void InterpreterMacroAssembler::remove_activation(
 660         TosState state,
 661         Register ret_addr,
 662         bool throw_monitor_exception,
 663         bool install_monitor_exception,
 664         bool notify_jvmdi) {
 665   // Note: Registers rdx xmm0 may be in use for the
 666   // result check if synchronized method
 667   Label unlocked, unlock, no_unlock;
 668 
 669   // get the value of _do_not_unlock_if_synchronized into rdx
 670   const Address do_not_unlock_if_synchronized(r15_thread,
 671     in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
 672   movbool(rdx, do_not_unlock_if_synchronized);
 673   movbool(do_not_unlock_if_synchronized, false); // reset the flag
 674 
 675  // get method access flags
 676   movptr(rbx, Address(rbp, frame::interpreter_frame_method_offset * wordSize));
 677   movl(rcx, Address(rbx, methodOopDesc::access_flags_offset()));
 678   testl(rcx, JVM_ACC_SYNCHRONIZED);
 679   jcc(Assembler::zero, unlocked);
 680 
 681   // Don't unlock anything if the _do_not_unlock_if_synchronized flag
 682   // is set.
 683   testbool(rdx);
 684   jcc(Assembler::notZero, no_unlock);
 685 
 686   // unlock monitor
 687   push(state); // save result
 688 
 689   // BasicObjectLock will be first in list, since this is a
 690   // synchronized method. However, need to check that the object has
 691   // not been unlocked by an explicit monitorexit bytecode.
 692   const Address monitor(rbp, frame::interpreter_frame_initial_sp_offset *
 693                         wordSize - (int) sizeof(BasicObjectLock));
 694   // We use c_rarg1 so that if we go slow path it will be the correct
 695   // register for unlock_object to pass to VM directly
 696   lea(c_rarg1, monitor); // address of first monitor
 697 
 698   movptr(rax, Address(c_rarg1, BasicObjectLock::obj_offset_in_bytes()));
 699   testptr(rax, rax);
 700   jcc(Assembler::notZero, unlock);
 701 
 702   pop(state);
 703   if (throw_monitor_exception) {
 704     // Entry already unlocked, need to throw exception
 705     call_VM(noreg, CAST_FROM_FN_PTR(address,
 706                    InterpreterRuntime::throw_illegal_monitor_state_exception));
 707     should_not_reach_here();
 708   } else {
 709     // Monitor already unlocked during a stack unroll. If requested,
 710     // install an illegal_monitor_state_exception.  Continue with
 711     // stack unrolling.
 712     if (install_monitor_exception) {
 713       call_VM(noreg, CAST_FROM_FN_PTR(address,
 714                      InterpreterRuntime::new_illegal_monitor_state_exception));
 715     }
 716     jmp(unlocked);
 717   }
 718 
 719   bind(unlock);
 720   unlock_object(c_rarg1);
 721   pop(state);
 722 
 723   // Check that for block-structured locking (i.e., that all locked
 724   // objects has been unlocked)
 725   bind(unlocked);
 726 
 727   // rax: Might contain return value
 728 
 729   // Check that all monitors are unlocked
 730   {
 731     Label loop, exception, entry, restart;
 732     const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
 733     const Address monitor_block_top(
 734         rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
 735     const Address monitor_block_bot(
 736         rbp, frame::interpreter_frame_initial_sp_offset * wordSize);
 737 
 738     bind(restart);
 739     // We use c_rarg1 so that if we go slow path it will be the correct
 740     // register for unlock_object to pass to VM directly
 741     movptr(c_rarg1, monitor_block_top); // points to current entry, starting
 742                                   // with top-most entry
 743     lea(rbx, monitor_block_bot);  // points to word before bottom of
 744                                   // monitor block
 745     jmp(entry);
 746 
 747     // Entry already locked, need to throw exception
 748     bind(exception);
 749 
 750     if (throw_monitor_exception) {
 751       // Throw exception
 752       MacroAssembler::call_VM(noreg,
 753                               CAST_FROM_FN_PTR(address, InterpreterRuntime::
 754                                    throw_illegal_monitor_state_exception));
 755       should_not_reach_here();
 756     } else {
 757       // Stack unrolling. Unlock object and install illegal_monitor_exception.
 758       // Unlock does not block, so don't have to worry about the frame.
 759       // We don't have to preserve c_rarg1 since we are going to throw an exception.
 760 
 761       push(state);
 762       unlock_object(c_rarg1);
 763       pop(state);
 764 
 765       if (install_monitor_exception) {
 766         call_VM(noreg, CAST_FROM_FN_PTR(address,
 767                                         InterpreterRuntime::
 768                                         new_illegal_monitor_state_exception));
 769       }
 770 
 771       jmp(restart);
 772     }
 773 
 774     bind(loop);
 775     // check if current entry is used
 776     cmpptr(Address(c_rarg1, BasicObjectLock::obj_offset_in_bytes()), (int32_t) NULL);
 777     jcc(Assembler::notEqual, exception);
 778 
 779     addptr(c_rarg1, entry_size); // otherwise advance to next entry
 780     bind(entry);
 781     cmpptr(c_rarg1, rbx); // check if bottom reached
 782     jcc(Assembler::notEqual, loop); // if not at bottom then check this entry
 783   }
 784 
 785   bind(no_unlock);
 786 
 787   // jvmti support
 788   if (notify_jvmdi) {
 789     notify_method_exit(state, NotifyJVMTI);    // preserve TOSCA
 790   } else {
 791     notify_method_exit(state, SkipNotifyJVMTI); // preserve TOSCA
 792   }
 793 
 794   // remove activation
 795   // get sender sp
 796   movptr(rbx,
 797          Address(rbp, frame::interpreter_frame_sender_sp_offset * wordSize));
 798   leave();                           // remove frame anchor
 799   pop(ret_addr);                     // get return address
 800   mov(rsp, rbx);                     // set sp to sender sp
 801 }
 802 
 803 #endif // C_INTERP
 804 
 805 // Lock object
 806 //
 807 // Args:
 808 //      c_rarg1: BasicObjectLock to be used for locking
 809 //
 810 // Kills:
 811 //      rax
 812 //      c_rarg0, c_rarg1, c_rarg2, c_rarg3, .. (param regs)
 813 //      rscratch1, rscratch2 (scratch regs)
 814 void InterpreterMacroAssembler::lock_object(Register lock_reg) {
 815   assert(lock_reg == c_rarg1, "The argument is only for looks. It must be c_rarg1");
 816 
 817   if (UseHeavyMonitors) {
 818     call_VM(noreg,
 819             CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter),
 820             lock_reg);
 821   } else {
 822     Label done;
 823 
 824     const Register swap_reg = rax; // Must use rax for cmpxchg instruction
 825     const Register obj_reg = c_rarg3; // Will contain the oop
 826 
 827     const int obj_offset = BasicObjectLock::obj_offset_in_bytes();
 828     const int lock_offset = BasicObjectLock::lock_offset_in_bytes ();
 829     const int mark_offset = lock_offset +
 830                             BasicLock::displaced_header_offset_in_bytes();
 831 
 832     Label slow_case;
 833 
 834     // Load object pointer into obj_reg %c_rarg3
 835     movptr(obj_reg, Address(lock_reg, obj_offset));
 836 
 837     if (UseBiasedLocking) {
 838       biased_locking_enter(lock_reg, obj_reg, swap_reg, rscratch1, false, done, &slow_case);
 839     }
 840 
 841     // Load immediate 1 into swap_reg %rax
 842     movl(swap_reg, 1);
 843 
 844     // Load (object->mark() | 1) into swap_reg %rax
 845     orptr(swap_reg, Address(obj_reg, 0));
 846 
 847     // Save (object->mark() | 1) into BasicLock's displaced header
 848     movptr(Address(lock_reg, mark_offset), swap_reg);
 849 
 850     assert(lock_offset == 0,
 851            "displached header must be first word in BasicObjectLock");
 852 
 853     if (os::is_MP()) lock();
 854     cmpxchgptr(lock_reg, Address(obj_reg, 0));
 855     if (PrintBiasedLockingStatistics) {
 856       cond_inc32(Assembler::zero,
 857                  ExternalAddress((address) BiasedLocking::fast_path_entry_count_addr()));
 858     }
 859     jcc(Assembler::zero, done);
 860 
 861     // Test if the oopMark is an obvious stack pointer, i.e.,
 862     //  1) (mark & 7) == 0, and
 863     //  2) rsp <= mark < mark + os::pagesize()
 864     //
 865     // These 3 tests can be done by evaluating the following
 866     // expression: ((mark - rsp) & (7 - os::vm_page_size())),
 867     // assuming both stack pointer and pagesize have their
 868     // least significant 3 bits clear.
 869     // NOTE: the oopMark is in swap_reg %rax as the result of cmpxchg
 870     subptr(swap_reg, rsp);
 871     andptr(swap_reg, 7 - os::vm_page_size());
 872 
 873     // Save the test result, for recursive case, the result is zero
 874     movptr(Address(lock_reg, mark_offset), swap_reg);
 875 
 876     if (PrintBiasedLockingStatistics) {
 877       cond_inc32(Assembler::zero,
 878                  ExternalAddress((address) BiasedLocking::fast_path_entry_count_addr()));
 879     }
 880     jcc(Assembler::zero, done);
 881 
 882     bind(slow_case);
 883 
 884     // Call the runtime routine for slow case
 885     call_VM(noreg,
 886             CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter),
 887             lock_reg);
 888 
 889     bind(done);
 890   }
 891 }
 892 
 893 
 894 // Unlocks an object. Used in monitorexit bytecode and
 895 // remove_activation.  Throws an IllegalMonitorException if object is
 896 // not locked by current thread.
 897 //
 898 // Args:
 899 //      c_rarg1: BasicObjectLock for lock
 900 //
 901 // Kills:
 902 //      rax
 903 //      c_rarg0, c_rarg1, c_rarg2, c_rarg3, ... (param regs)
 904 //      rscratch1, rscratch2 (scratch regs)
 905 void InterpreterMacroAssembler::unlock_object(Register lock_reg) {
 906   assert(lock_reg == c_rarg1, "The argument is only for looks. It must be rarg1");
 907 
 908   if (UseHeavyMonitors) {
 909     call_VM(noreg,
 910             CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit),
 911             lock_reg);
 912   } else {
 913     Label done;
 914 
 915     const Register swap_reg   = rax;  // Must use rax for cmpxchg instruction
 916     const Register header_reg = c_rarg2;  // Will contain the old oopMark
 917     const Register obj_reg    = c_rarg3;  // Will contain the oop
 918 
 919     save_bcp(); // Save in case of exception
 920 
 921     // Convert from BasicObjectLock structure to object and BasicLock
 922     // structure Store the BasicLock address into %rax
 923     lea(swap_reg, Address(lock_reg, BasicObjectLock::lock_offset_in_bytes()));
 924 
 925     // Load oop into obj_reg(%c_rarg3)
 926     movptr(obj_reg, Address(lock_reg, BasicObjectLock::obj_offset_in_bytes()));
 927 
 928     // Free entry
 929     movptr(Address(lock_reg, BasicObjectLock::obj_offset_in_bytes()), (int32_t)NULL_WORD);
 930 
 931     if (UseBiasedLocking) {
 932       biased_locking_exit(obj_reg, header_reg, done);
 933     }
 934 
 935     // Load the old header from BasicLock structure
 936     movptr(header_reg, Address(swap_reg,
 937                                BasicLock::displaced_header_offset_in_bytes()));
 938 
 939     // Test for recursion
 940     testptr(header_reg, header_reg);
 941 
 942     // zero for recursive case
 943     jcc(Assembler::zero, done);
 944 
 945     // Atomic swap back the old header
 946     if (os::is_MP()) lock();
 947     cmpxchgptr(header_reg, Address(obj_reg, 0));
 948 
 949     // zero for recursive case
 950     jcc(Assembler::zero, done);
 951 
 952     // Call the runtime routine for slow case.
 953     movptr(Address(lock_reg, BasicObjectLock::obj_offset_in_bytes()),
 954          obj_reg); // restore obj
 955     call_VM(noreg,
 956             CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit),
 957             lock_reg);
 958 
 959     bind(done);
 960 
 961     restore_bcp();
 962   }
 963 }
 964 
 965 #ifndef CC_INTERP
 966 
 967 void InterpreterMacroAssembler::test_method_data_pointer(Register mdp,
 968                                                          Label& zero_continue) {
 969   assert(ProfileInterpreter, "must be profiling interpreter");
 970   movptr(mdp, Address(rbp, frame::interpreter_frame_mdx_offset * wordSize));
 971   testptr(mdp, mdp);
 972   jcc(Assembler::zero, zero_continue);
 973 }
 974 
 975 
 976 // Set the method data pointer for the current bcp.
 977 void InterpreterMacroAssembler::set_method_data_pointer_for_bcp() {
 978   assert(ProfileInterpreter, "must be profiling interpreter");
 979   Label zero_continue;
 980   push(rax);
 981   push(rbx);
 982 
 983   get_method(rbx);
 984   // Test MDO to avoid the call if it is NULL.
 985   movptr(rax, Address(rbx, in_bytes(methodOopDesc::method_data_offset())));
 986   testptr(rax, rax);
 987   jcc(Assembler::zero, zero_continue);
 988 
 989   // rbx: method
 990   // r13: bcp
 991   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::bcp_to_di), rbx, r13);
 992   // rax: mdi
 993 
 994   movptr(rbx, Address(rbx, in_bytes(methodOopDesc::method_data_offset())));
 995   testptr(rbx, rbx);
 996   jcc(Assembler::zero, zero_continue);
 997   addptr(rbx, in_bytes(methodDataOopDesc::data_offset()));
 998   addptr(rbx, rax);
 999   movptr(Address(rbp, frame::interpreter_frame_mdx_offset * wordSize), rbx);
1000 
1001   bind(zero_continue);
1002   pop(rbx);
1003   pop(rax);
1004 }
1005 
1006 void InterpreterMacroAssembler::verify_method_data_pointer() {
1007   assert(ProfileInterpreter, "must be profiling interpreter");
1008 #ifdef ASSERT
1009   Label verify_continue;
1010   push(rax);
1011   push(rbx);
1012   push(c_rarg3);
1013   push(c_rarg2);
1014   test_method_data_pointer(c_rarg3, verify_continue); // If mdp is zero, continue
1015   get_method(rbx);
1016 
1017   // If the mdp is valid, it will point to a DataLayout header which is
1018   // consistent with the bcp.  The converse is highly probable also.
1019   load_unsigned_short(c_rarg2,
1020                       Address(c_rarg3, in_bytes(DataLayout::bci_offset())));
1021   addptr(c_rarg2, Address(rbx, methodOopDesc::const_offset()));
1022   lea(c_rarg2, Address(c_rarg2, constMethodOopDesc::codes_offset()));
1023   cmpptr(c_rarg2, r13);
1024   jcc(Assembler::equal, verify_continue);
1025   // rbx: method
1026   // r13: bcp
1027   // c_rarg3: mdp
1028   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::verify_mdp),
1029                rbx, r13, c_rarg3);
1030   bind(verify_continue);
1031   pop(c_rarg2);
1032   pop(c_rarg3);
1033   pop(rbx);
1034   pop(rax);
1035 #endif // ASSERT
1036 }
1037 
1038 
1039 void InterpreterMacroAssembler::set_mdp_data_at(Register mdp_in,
1040                                                 int constant,
1041                                                 Register value) {
1042   assert(ProfileInterpreter, "must be profiling interpreter");
1043   Address data(mdp_in, constant);
1044   movptr(data, value);
1045 }
1046 
1047 
1048 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in,
1049                                                       int constant,
1050                                                       bool decrement) {
1051   // Counter address
1052   Address data(mdp_in, constant);
1053 
1054   increment_mdp_data_at(data, decrement);
1055 }
1056 
1057 void InterpreterMacroAssembler::increment_mdp_data_at(Address data,
1058                                                       bool decrement) {
1059   assert(ProfileInterpreter, "must be profiling interpreter");
1060   // %%% this does 64bit counters at best it is wasting space
1061   // at worst it is a rare bug when counters overflow
1062 
1063   if (decrement) {
1064     // Decrement the register.  Set condition codes.
1065     addptr(data, (int32_t) -DataLayout::counter_increment);
1066     // If the decrement causes the counter to overflow, stay negative
1067     Label L;
1068     jcc(Assembler::negative, L);
1069     addptr(data, (int32_t) DataLayout::counter_increment);
1070     bind(L);
1071   } else {
1072     assert(DataLayout::counter_increment == 1,
1073            "flow-free idiom only works with 1");
1074     // Increment the register.  Set carry flag.
1075     addptr(data, DataLayout::counter_increment);
1076     // If the increment causes the counter to overflow, pull back by 1.
1077     sbbptr(data, (int32_t)0);
1078   }
1079 }
1080 
1081 
1082 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in,
1083                                                       Register reg,
1084                                                       int constant,
1085                                                       bool decrement) {
1086   Address data(mdp_in, reg, Address::times_1, constant);
1087 
1088   increment_mdp_data_at(data, decrement);
1089 }
1090 
1091 void InterpreterMacroAssembler::set_mdp_flag_at(Register mdp_in,
1092                                                 int flag_byte_constant) {
1093   assert(ProfileInterpreter, "must be profiling interpreter");
1094   int header_offset = in_bytes(DataLayout::header_offset());
1095   int header_bits = DataLayout::flag_mask_to_header_mask(flag_byte_constant);
1096   // Set the flag
1097   orl(Address(mdp_in, header_offset), header_bits);
1098 }
1099 
1100 
1101 
1102 void InterpreterMacroAssembler::test_mdp_data_at(Register mdp_in,
1103                                                  int offset,
1104                                                  Register value,
1105                                                  Register test_value_out,
1106                                                  Label& not_equal_continue) {
1107   assert(ProfileInterpreter, "must be profiling interpreter");
1108   if (test_value_out == noreg) {
1109     cmpptr(value, Address(mdp_in, offset));
1110   } else {
1111     // Put the test value into a register, so caller can use it:
1112     movptr(test_value_out, Address(mdp_in, offset));
1113     cmpptr(test_value_out, value);
1114   }
1115   jcc(Assembler::notEqual, not_equal_continue);
1116 }
1117 
1118 
1119 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in,
1120                                                      int offset_of_disp) {
1121   assert(ProfileInterpreter, "must be profiling interpreter");
1122   Address disp_address(mdp_in, offset_of_disp);
1123   addptr(mdp_in, disp_address);
1124   movptr(Address(rbp, frame::interpreter_frame_mdx_offset * wordSize), mdp_in);
1125 }
1126 
1127 
1128 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in,
1129                                                      Register reg,
1130                                                      int offset_of_disp) {
1131   assert(ProfileInterpreter, "must be profiling interpreter");
1132   Address disp_address(mdp_in, reg, Address::times_1, offset_of_disp);
1133   addptr(mdp_in, disp_address);
1134   movptr(Address(rbp, frame::interpreter_frame_mdx_offset * wordSize), mdp_in);
1135 }
1136 
1137 
1138 void InterpreterMacroAssembler::update_mdp_by_constant(Register mdp_in,
1139                                                        int constant) {
1140   assert(ProfileInterpreter, "must be profiling interpreter");
1141   addptr(mdp_in, constant);
1142   movptr(Address(rbp, frame::interpreter_frame_mdx_offset * wordSize), mdp_in);
1143 }
1144 
1145 
1146 void InterpreterMacroAssembler::update_mdp_for_ret(Register return_bci) {
1147   assert(ProfileInterpreter, "must be profiling interpreter");
1148   push(return_bci); // save/restore across call_VM
1149   call_VM(noreg,
1150           CAST_FROM_FN_PTR(address, InterpreterRuntime::update_mdp_for_ret),
1151           return_bci);
1152   pop(return_bci);
1153 }
1154 
1155 
1156 void InterpreterMacroAssembler::profile_taken_branch(Register mdp,
1157                                                      Register bumped_count) {
1158   if (ProfileInterpreter) {
1159     Label profile_continue;
1160 
1161     // If no method data exists, go to profile_continue.
1162     // Otherwise, assign to mdp
1163     test_method_data_pointer(mdp, profile_continue);
1164 
1165     // We are taking a branch.  Increment the taken count.
1166     // We inline increment_mdp_data_at to return bumped_count in a register
1167     //increment_mdp_data_at(mdp, in_bytes(JumpData::taken_offset()));
1168     Address data(mdp, in_bytes(JumpData::taken_offset()));
1169     movptr(bumped_count, data);
1170     assert(DataLayout::counter_increment == 1,
1171             "flow-free idiom only works with 1");
1172     addptr(bumped_count, DataLayout::counter_increment);
1173     sbbptr(bumped_count, 0);
1174     movptr(data, bumped_count); // Store back out
1175 
1176     // The method data pointer needs to be updated to reflect the new target.
1177     update_mdp_by_offset(mdp, in_bytes(JumpData::displacement_offset()));
1178     bind(profile_continue);
1179   }
1180 }
1181 
1182 
1183 void InterpreterMacroAssembler::profile_not_taken_branch(Register mdp) {
1184   if (ProfileInterpreter) {
1185     Label profile_continue;
1186 
1187     // If no method data exists, go to profile_continue.
1188     test_method_data_pointer(mdp, profile_continue);
1189 
1190     // We are taking a branch.  Increment the not taken count.
1191     increment_mdp_data_at(mdp, in_bytes(BranchData::not_taken_offset()));
1192 
1193     // The method data pointer needs to be updated to correspond to
1194     // the next bytecode
1195     update_mdp_by_constant(mdp, in_bytes(BranchData::branch_data_size()));
1196     bind(profile_continue);
1197   }
1198 }
1199 
1200 
1201 void InterpreterMacroAssembler::profile_call(Register mdp) {
1202   if (ProfileInterpreter) {
1203     Label profile_continue;
1204 
1205     // If no method data exists, go to profile_continue.
1206     test_method_data_pointer(mdp, profile_continue);
1207 
1208     // We are making a call.  Increment the count.
1209     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1210 
1211     // The method data pointer needs to be updated to reflect the new target.
1212     update_mdp_by_constant(mdp, in_bytes(CounterData::counter_data_size()));
1213     bind(profile_continue);
1214   }
1215 }
1216 
1217 
1218 void InterpreterMacroAssembler::profile_final_call(Register mdp) {
1219   if (ProfileInterpreter) {
1220     Label profile_continue;
1221 
1222     // If no method data exists, go to profile_continue.
1223     test_method_data_pointer(mdp, profile_continue);
1224 
1225     // We are making a call.  Increment the count.
1226     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1227 
1228     // The method data pointer needs to be updated to reflect the new target.
1229     update_mdp_by_constant(mdp,
1230                            in_bytes(VirtualCallData::
1231                                     virtual_call_data_size()));
1232     bind(profile_continue);
1233   }
1234 }
1235 
1236 
1237 void InterpreterMacroAssembler::profile_virtual_call(Register receiver,
1238                                                      Register mdp,
1239                                                      Register reg2) {
1240   if (ProfileInterpreter) {
1241     Label profile_continue;
1242 
1243     // If no method data exists, go to profile_continue.
1244     test_method_data_pointer(mdp, profile_continue);
1245 
1246     // We are making a call.  Increment the count.
1247     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1248 
1249     // Record the receiver type.
1250     record_klass_in_profile(receiver, mdp, reg2);
1251 
1252     // The method data pointer needs to be updated to reflect the new target.
1253     update_mdp_by_constant(mdp,
1254                            in_bytes(VirtualCallData::
1255                                     virtual_call_data_size()));
1256     bind(profile_continue);
1257   }
1258 }
1259 
1260 // This routine creates a state machine for updating the multi-row
1261 // type profile at a virtual call site (or other type-sensitive bytecode).
1262 // The machine visits each row (of receiver/count) until the receiver type
1263 // is found, or until it runs out of rows.  At the same time, it remembers
1264 // the location of the first empty row.  (An empty row records null for its
1265 // receiver, and can be allocated for a newly-observed receiver type.)
1266 // Because there are two degrees of freedom in the state, a simple linear
1267 // search will not work; it must be a decision tree.  Hence this helper
1268 // function is recursive, to generate the required tree structured code.
1269 // It's the interpreter, so we are trading off code space for speed.
1270 // See below for example code.
1271 void InterpreterMacroAssembler::record_klass_in_profile_helper(
1272                                         Register receiver, Register mdp,
1273                                         Register reg2,
1274                                         int start_row, Label& done) {
1275   if (TypeProfileWidth == 0)
1276     return;
1277 
1278   int last_row = VirtualCallData::row_limit() - 1;
1279   assert(start_row <= last_row, "must be work left to do");
1280   // Test this row for both the receiver and for null.
1281   // Take any of three different outcomes:
1282   //   1. found receiver => increment count and goto done
1283   //   2. found null => keep looking for case 1, maybe allocate this cell
1284   //   3. found something else => keep looking for cases 1 and 2
1285   // Case 3 is handled by a recursive call.
1286   for (int row = start_row; row <= last_row; row++) {
1287     Label next_test;
1288     bool test_for_null_also = (row == start_row);
1289 
1290     // See if the receiver is receiver[n].
1291     int recvr_offset = in_bytes(VirtualCallData::receiver_offset(row));
1292     test_mdp_data_at(mdp, recvr_offset, receiver,
1293                      (test_for_null_also ? reg2 : noreg),
1294                      next_test);
1295     // (Reg2 now contains the receiver from the CallData.)
1296 
1297     // The receiver is receiver[n].  Increment count[n].
1298     int count_offset = in_bytes(VirtualCallData::receiver_count_offset(row));
1299     increment_mdp_data_at(mdp, count_offset);
1300     jmp(done);
1301     bind(next_test);
1302 
1303     if (test_for_null_also) {
1304       // Failed the equality check on receiver[n]...  Test for null.
1305       testptr(reg2, reg2);
1306       if (start_row == last_row) {
1307         // The only thing left to do is handle the null case.
1308         jcc(Assembler::notZero, done);
1309         break;
1310       }
1311       // Since null is rare, make it be the branch-taken case.
1312       Label found_null;
1313       jcc(Assembler::zero, found_null);
1314 
1315       // Put all the "Case 3" tests here.
1316       record_klass_in_profile_helper(receiver, mdp, reg2, start_row + 1, done);
1317 
1318       // Found a null.  Keep searching for a matching receiver,
1319       // but remember that this is an empty (unused) slot.
1320       bind(found_null);
1321     }
1322   }
1323 
1324   // In the fall-through case, we found no matching receiver, but we
1325   // observed the receiver[start_row] is NULL.
1326 
1327   // Fill in the receiver field and increment the count.
1328   int recvr_offset = in_bytes(VirtualCallData::receiver_offset(start_row));
1329   set_mdp_data_at(mdp, recvr_offset, receiver);
1330   int count_offset = in_bytes(VirtualCallData::receiver_count_offset(start_row));
1331   movl(reg2, DataLayout::counter_increment);
1332   set_mdp_data_at(mdp, count_offset, reg2);
1333   jmp(done);
1334 }
1335 
1336 // Example state machine code for three profile rows:
1337 //   // main copy of decision tree, rooted at row[1]
1338 //   if (row[0].rec == rec) { row[0].incr(); goto done; }
1339 //   if (row[0].rec != NULL) {
1340 //     // inner copy of decision tree, rooted at row[1]
1341 //     if (row[1].rec == rec) { row[1].incr(); goto done; }
1342 //     if (row[1].rec != NULL) {
1343 //       // degenerate decision tree, rooted at row[2]
1344 //       if (row[2].rec == rec) { row[2].incr(); goto done; }
1345 //       if (row[2].rec != NULL) { goto done; } // overflow
1346 //       row[2].init(rec); goto done;
1347 //     } else {
1348 //       // remember row[1] is empty
1349 //       if (row[2].rec == rec) { row[2].incr(); goto done; }
1350 //       row[1].init(rec); goto done;
1351 //     }
1352 //   } else {
1353 //     // remember row[0] is empty
1354 //     if (row[1].rec == rec) { row[1].incr(); goto done; }
1355 //     if (row[2].rec == rec) { row[2].incr(); goto done; }
1356 //     row[0].init(rec); goto done;
1357 //   }
1358 
1359 void InterpreterMacroAssembler::record_klass_in_profile(Register receiver,
1360                                                         Register mdp,
1361                                                         Register reg2) {
1362   assert(ProfileInterpreter, "must be profiling");
1363   Label done;
1364 
1365   record_klass_in_profile_helper(receiver, mdp, reg2, 0, done);
1366 
1367   bind (done);
1368 }
1369 
1370 void InterpreterMacroAssembler::profile_ret(Register return_bci,
1371                                             Register mdp) {
1372   if (ProfileInterpreter) {
1373     Label profile_continue;
1374     uint row;
1375 
1376     // If no method data exists, go to profile_continue.
1377     test_method_data_pointer(mdp, profile_continue);
1378 
1379     // Update the total ret count.
1380     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1381 
1382     for (row = 0; row < RetData::row_limit(); row++) {
1383       Label next_test;
1384 
1385       // See if return_bci is equal to bci[n]:
1386       test_mdp_data_at(mdp,
1387                        in_bytes(RetData::bci_offset(row)),
1388                        return_bci, noreg,
1389                        next_test);
1390 
1391       // return_bci is equal to bci[n].  Increment the count.
1392       increment_mdp_data_at(mdp, in_bytes(RetData::bci_count_offset(row)));
1393 
1394       // The method data pointer needs to be updated to reflect the new target.
1395       update_mdp_by_offset(mdp,
1396                            in_bytes(RetData::bci_displacement_offset(row)));
1397       jmp(profile_continue);
1398       bind(next_test);
1399     }
1400 
1401     update_mdp_for_ret(return_bci);
1402 
1403     bind(profile_continue);
1404   }
1405 }
1406 
1407 
1408 void InterpreterMacroAssembler::profile_null_seen(Register mdp) {
1409   if (ProfileInterpreter) {
1410     Label profile_continue;
1411 
1412     // If no method data exists, go to profile_continue.
1413     test_method_data_pointer(mdp, profile_continue);
1414 
1415     set_mdp_flag_at(mdp, BitData::null_seen_byte_constant());
1416 
1417     // The method data pointer needs to be updated.
1418     int mdp_delta = in_bytes(BitData::bit_data_size());
1419     if (TypeProfileCasts) {
1420       mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1421     }
1422     update_mdp_by_constant(mdp, mdp_delta);
1423 
1424     bind(profile_continue);
1425   }
1426 }
1427 
1428 
1429 void InterpreterMacroAssembler::profile_typecheck_failed(Register mdp) {
1430   if (ProfileInterpreter && TypeProfileCasts) {
1431     Label profile_continue;
1432 
1433     // If no method data exists, go to profile_continue.
1434     test_method_data_pointer(mdp, profile_continue);
1435 
1436     int count_offset = in_bytes(CounterData::count_offset());
1437     // Back up the address, since we have already bumped the mdp.
1438     count_offset -= in_bytes(VirtualCallData::virtual_call_data_size());
1439 
1440     // *Decrement* the counter.  We expect to see zero or small negatives.
1441     increment_mdp_data_at(mdp, count_offset, true);
1442 
1443     bind (profile_continue);
1444   }
1445 }
1446 
1447 
1448 void InterpreterMacroAssembler::profile_typecheck(Register mdp, Register klass, Register reg2) {
1449   if (ProfileInterpreter) {
1450     Label profile_continue;
1451 
1452     // If no method data exists, go to profile_continue.
1453     test_method_data_pointer(mdp, profile_continue);
1454 
1455     // The method data pointer needs to be updated.
1456     int mdp_delta = in_bytes(BitData::bit_data_size());
1457     if (TypeProfileCasts) {
1458       mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1459 
1460       // Record the object type.
1461       record_klass_in_profile(klass, mdp, reg2);
1462     }
1463     update_mdp_by_constant(mdp, mdp_delta);
1464 
1465     bind(profile_continue);
1466   }
1467 }
1468 
1469 
1470 void InterpreterMacroAssembler::profile_switch_default(Register mdp) {
1471   if (ProfileInterpreter) {
1472     Label profile_continue;
1473 
1474     // If no method data exists, go to profile_continue.
1475     test_method_data_pointer(mdp, profile_continue);
1476 
1477     // Update the default case count
1478     increment_mdp_data_at(mdp,
1479                           in_bytes(MultiBranchData::default_count_offset()));
1480 
1481     // The method data pointer needs to be updated.
1482     update_mdp_by_offset(mdp,
1483                          in_bytes(MultiBranchData::
1484                                   default_displacement_offset()));
1485 
1486     bind(profile_continue);
1487   }
1488 }
1489 
1490 
1491 void InterpreterMacroAssembler::profile_switch_case(Register index,
1492                                                     Register mdp,
1493                                                     Register reg2) {
1494   if (ProfileInterpreter) {
1495     Label profile_continue;
1496 
1497     // If no method data exists, go to profile_continue.
1498     test_method_data_pointer(mdp, profile_continue);
1499 
1500     // Build the base (index * per_case_size_in_bytes()) +
1501     // case_array_offset_in_bytes()
1502     movl(reg2, in_bytes(MultiBranchData::per_case_size()));
1503     imulptr(index, reg2); // XXX l ?
1504     addptr(index, in_bytes(MultiBranchData::case_array_offset())); // XXX l ?
1505 
1506     // Update the case count
1507     increment_mdp_data_at(mdp,
1508                           index,
1509                           in_bytes(MultiBranchData::relative_count_offset()));
1510 
1511     // The method data pointer needs to be updated.
1512     update_mdp_by_offset(mdp,
1513                          index,
1514                          in_bytes(MultiBranchData::
1515                                   relative_displacement_offset()));
1516 
1517     bind(profile_continue);
1518   }
1519 }
1520 
1521 
1522 
1523 void InterpreterMacroAssembler::verify_oop(Register reg, TosState state) {
1524   if (state == atos) {
1525     MacroAssembler::verify_oop(reg);
1526   }
1527 }
1528 
1529 void InterpreterMacroAssembler::verify_FPU(int stack_depth, TosState state) {
1530 }
1531 #endif // !CC_INTERP
1532 
1533 
1534 void InterpreterMacroAssembler::notify_method_entry() {
1535   // Whenever JVMTI is interp_only_mode, method entry/exit events are sent to
1536   // track stack depth.  If it is possible to enter interp_only_mode we add
1537   // the code to check if the event should be sent.
1538   if (JvmtiExport::can_post_interpreter_events()) {
1539     Label L;
1540     movl(rdx, Address(r15_thread, JavaThread::interp_only_mode_offset()));
1541     testl(rdx, rdx);
1542     jcc(Assembler::zero, L);
1543     call_VM(noreg, CAST_FROM_FN_PTR(address,
1544                                     InterpreterRuntime::post_method_entry));
1545     bind(L);
1546   }
1547 
1548   {
1549     SkipIfEqual skip(this, &DTraceMethodProbes, false);
1550     get_method(c_rarg1);
1551     call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry),
1552                  r15_thread, c_rarg1);
1553   }
1554 
1555   // RedefineClasses() tracing support for obsolete method entry
1556   if (RC_TRACE_IN_RANGE(0x00001000, 0x00002000)) {
1557     get_method(c_rarg1);
1558     call_VM_leaf(
1559       CAST_FROM_FN_PTR(address, SharedRuntime::rc_trace_method_entry),
1560       r15_thread, c_rarg1);
1561   }
1562 }
1563 
1564 
1565 void InterpreterMacroAssembler::notify_method_exit(
1566     TosState state, NotifyMethodExitMode mode) {
1567   // Whenever JVMTI is interp_only_mode, method entry/exit events are sent to
1568   // track stack depth.  If it is possible to enter interp_only_mode we add
1569   // the code to check if the event should be sent.
1570   if (mode == NotifyJVMTI && JvmtiExport::can_post_interpreter_events()) {
1571     Label L;
1572     // Note: frame::interpreter_frame_result has a dependency on how the
1573     // method result is saved across the call to post_method_exit. If this
1574     // is changed then the interpreter_frame_result implementation will
1575     // need to be updated too.
1576 
1577     // For c++ interpreter the result is always stored at a known location in the frame
1578     // template interpreter will leave it on the top of the stack.
1579     NOT_CC_INTERP(push(state);)
1580     movl(rdx, Address(r15_thread, JavaThread::interp_only_mode_offset()));
1581     testl(rdx, rdx);
1582     jcc(Assembler::zero, L);
1583     call_VM(noreg,
1584             CAST_FROM_FN_PTR(address, InterpreterRuntime::post_method_exit));
1585     bind(L);
1586     NOT_CC_INTERP(pop(state));
1587   }
1588 
1589   {
1590     SkipIfEqual skip(this, &DTraceMethodProbes, false);
1591     NOT_CC_INTERP(push(state));
1592     get_method(c_rarg1);
1593     call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit),
1594                  r15_thread, c_rarg1);
1595     NOT_CC_INTERP(pop(state));
1596   }
1597 }