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