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