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