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