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