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, (int32_t)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,  (int32_t)NULL_WORD);
 152   NOT_LP64(movl(val_addr1, (int32_t)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 void InterpreterMacroAssembler::prepare_to_jump_from_interpreted() {
 590   // set sender sp
 591   lea(rsi, Address(rsp, wordSize));
 592   // record last_sp
 593   movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), rsi);
 594 }
 595 
 596 
 597 // Jump to from_interpreted entry of a call unless single stepping is possible
 598 // in this thread in which case we must call the i2i entry
 599 void InterpreterMacroAssembler::jump_from_interpreted(Register method, Register temp) {
 600   prepare_to_jump_from_interpreted();
 601 
 602   if (JvmtiExport::can_post_interpreter_events()) {
 603     Label run_compiled_code;
 604     // JVMTI events, such as single-stepping, are implemented partly by avoiding running
 605     // compiled code in threads for which the event is enabled.  Check here for
 606     // interp_only_mode if these events CAN be enabled.
 607     get_thread(temp);
 608     // interp_only is an int, on little endian it is sufficient to test the byte only
 609     // Is a cmpl faster (ce
 610     cmpb(Address(temp, JavaThread::interp_only_mode_offset()), 0);
 611     jcc(Assembler::zero, run_compiled_code);
 612     jmp(Address(method, methodOopDesc::interpreter_entry_offset()));
 613     bind(run_compiled_code);
 614   }
 615 
 616   jmp(Address(method, methodOopDesc::from_interpreted_offset()));
 617 
 618 }
 619 
 620 
 621 // The following two routines provide a hook so that an implementation
 622 // can schedule the dispatch in two parts.  Intel does not do this.
 623 void InterpreterMacroAssembler::dispatch_prolog(TosState state, int step) {
 624   // Nothing Intel-specific to be done here.
 625 }
 626 
 627 void InterpreterMacroAssembler::dispatch_epilog(TosState state, int step) {
 628   dispatch_next(state, step);
 629 }
 630 
 631 void InterpreterMacroAssembler::dispatch_base(TosState state, address* table,
 632                                               bool verifyoop) {
 633   verify_FPU(1, state);
 634   if (VerifyActivationFrameSize) {
 635     Label L;
 636     mov(rcx, rbp);
 637     subptr(rcx, rsp);
 638     int min_frame_size = (frame::link_offset - frame::interpreter_frame_initial_sp_offset) * wordSize;
 639     cmpptr(rcx, min_frame_size);
 640     jcc(Assembler::greaterEqual, L);
 641     stop("broken stack frame");
 642     bind(L);
 643   }
 644   if (verifyoop) verify_oop(rax, state);
 645   Address index(noreg, rbx, Address::times_ptr);
 646   ExternalAddress tbl((address)table);
 647   ArrayAddress dispatch(tbl, index);
 648   jump(dispatch);
 649 }
 650 
 651 
 652 void InterpreterMacroAssembler::dispatch_only(TosState state) {
 653   dispatch_base(state, Interpreter::dispatch_table(state));
 654 }
 655 
 656 
 657 void InterpreterMacroAssembler::dispatch_only_normal(TosState state) {
 658   dispatch_base(state, Interpreter::normal_table(state));
 659 }
 660 
 661 void InterpreterMacroAssembler::dispatch_only_noverify(TosState state) {
 662   dispatch_base(state, Interpreter::normal_table(state), false);
 663 }
 664 
 665 
 666 void InterpreterMacroAssembler::dispatch_next(TosState state, int step) {
 667   // load next bytecode (load before advancing rsi to prevent AGI)
 668   load_unsigned_byte(rbx, Address(rsi, step));
 669   // advance rsi
 670   increment(rsi, step);
 671   dispatch_base(state, Interpreter::dispatch_table(state));
 672 }
 673 
 674 
 675 void InterpreterMacroAssembler::dispatch_via(TosState state, address* table) {
 676   // load current bytecode
 677   load_unsigned_byte(rbx, Address(rsi, 0));
 678   dispatch_base(state, table);
 679 }
 680 
 681 // remove activation
 682 //
 683 // Unlock the receiver if this is a synchronized method.
 684 // Unlock any Java monitors from syncronized blocks.
 685 // Remove the activation from the stack.
 686 //
 687 // If there are locked Java monitors
 688 //    If throw_monitor_exception
 689 //       throws IllegalMonitorStateException
 690 //    Else if install_monitor_exception
 691 //       installs IllegalMonitorStateException
 692 //    Else
 693 //       no error processing
 694 void InterpreterMacroAssembler::remove_activation(TosState state, Register ret_addr,
 695                                                   bool throw_monitor_exception,
 696                                                   bool install_monitor_exception,
 697                                                   bool notify_jvmdi) {
 698   // Note: Registers rax, rdx and FPU ST(0) may be in use for the result
 699   // check if synchronized method
 700   Label unlocked, unlock, no_unlock;
 701 
 702   get_thread(rcx);
 703   const Address do_not_unlock_if_synchronized(rcx,
 704     in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
 705 
 706   movbool(rbx, do_not_unlock_if_synchronized);
 707   mov(rdi,rbx);
 708   movbool(do_not_unlock_if_synchronized, false); // reset the flag
 709 
 710   movptr(rbx, Address(rbp, frame::interpreter_frame_method_offset * wordSize)); // get method access flags
 711   movl(rcx, Address(rbx, methodOopDesc::access_flags_offset()));
 712 
 713   testl(rcx, JVM_ACC_SYNCHRONIZED);
 714   jcc(Assembler::zero, unlocked);
 715 
 716   // Don't unlock anything if the _do_not_unlock_if_synchronized flag
 717   // is set.
 718   mov(rcx,rdi);
 719   testbool(rcx);
 720   jcc(Assembler::notZero, no_unlock);
 721 
 722   // unlock monitor
 723   push(state);                                   // save result
 724 
 725   // BasicObjectLock will be first in list, since this is a synchronized method. However, need
 726   // to check that the object has not been unlocked by an explicit monitorexit bytecode.
 727   const Address monitor(rbp, frame::interpreter_frame_initial_sp_offset * wordSize - (int)sizeof(BasicObjectLock));
 728   lea   (rdx, monitor);                          // address of first monitor
 729 
 730   movptr (rax, Address(rdx, BasicObjectLock::obj_offset_in_bytes()));
 731   testptr(rax, rax);
 732   jcc    (Assembler::notZero, unlock);
 733 
 734   pop(state);
 735   if (throw_monitor_exception) {
 736     empty_FPU_stack();  // remove possible return value from FPU-stack, otherwise stack could overflow
 737 
 738     // Entry already unlocked, need to throw exception
 739     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_illegal_monitor_state_exception));
 740     should_not_reach_here();
 741   } else {
 742     // Monitor already unlocked during a stack unroll.
 743     // If requested, install an illegal_monitor_state_exception.
 744     // Continue with stack unrolling.
 745     if (install_monitor_exception) {
 746       empty_FPU_stack();  // remove possible return value from FPU-stack, otherwise stack could overflow
 747       call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::new_illegal_monitor_state_exception));
 748     }
 749     jmp(unlocked);
 750   }
 751 
 752   bind(unlock);
 753   unlock_object(rdx);
 754   pop(state);
 755 
 756   // Check that for block-structured locking (i.e., that all locked objects has been unlocked)
 757   bind(unlocked);
 758 
 759   // rax, rdx: Might contain return value
 760 
 761   // Check that all monitors are unlocked
 762   {
 763     Label loop, exception, entry, restart;
 764     const int entry_size               = frame::interpreter_frame_monitor_size()           * wordSize;
 765     const Address monitor_block_top(rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
 766     const Address monitor_block_bot(rbp, frame::interpreter_frame_initial_sp_offset        * wordSize);
 767 
 768     bind(restart);
 769     movptr(rcx, monitor_block_top);           // points to current entry, starting with top-most entry
 770     lea(rbx, monitor_block_bot);              // points to word before bottom of monitor block
 771     jmp(entry);
 772 
 773     // Entry already locked, need to throw exception
 774     bind(exception);
 775 
 776     if (throw_monitor_exception) {
 777       empty_FPU_stack();  // remove possible return value from FPU-stack, otherwise stack could overflow
 778 
 779       // Throw exception
 780       call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_illegal_monitor_state_exception));
 781       should_not_reach_here();
 782     } else {
 783       // Stack unrolling. Unlock object and install illegal_monitor_exception
 784       // Unlock does not block, so don't have to worry about the frame
 785 
 786       push(state);
 787       mov(rdx, rcx);
 788       unlock_object(rdx);
 789       pop(state);
 790 
 791       if (install_monitor_exception) {
 792         empty_FPU_stack();  // remove possible return value from FPU-stack, otherwise stack could overflow
 793         call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::new_illegal_monitor_state_exception));
 794       }
 795 
 796       jmp(restart);
 797     }
 798 
 799     bind(loop);
 800     cmpptr(Address(rcx, BasicObjectLock::obj_offset_in_bytes()), (int32_t)NULL_WORD);  // check if current entry is used
 801     jcc(Assembler::notEqual, exception);
 802 
 803     addptr(rcx, entry_size);                     // otherwise advance to next entry
 804     bind(entry);
 805     cmpptr(rcx, rbx);                            // check if bottom reached
 806     jcc(Assembler::notEqual, loop);              // if not at bottom then check this entry
 807   }
 808 
 809   bind(no_unlock);
 810 
 811   // jvmti support
 812   if (notify_jvmdi) {
 813     notify_method_exit(state, NotifyJVMTI);     // preserve TOSCA
 814   } else {
 815     notify_method_exit(state, SkipNotifyJVMTI); // preserve TOSCA
 816   }
 817 
 818   // remove activation
 819   movptr(rbx, Address(rbp, frame::interpreter_frame_sender_sp_offset * wordSize)); // get sender sp
 820   leave();                                     // remove frame anchor
 821   pop(ret_addr);                               // get return address
 822   mov(rsp, rbx);                               // set sp to sender sp
 823   if (UseSSE) {
 824     // float and double are returned in xmm register in SSE-mode
 825     if (state == ftos && UseSSE >= 1) {
 826       subptr(rsp, wordSize);
 827       fstp_s(Address(rsp, 0));
 828       movflt(xmm0, Address(rsp, 0));
 829       addptr(rsp, wordSize);
 830     } else if (state == dtos && UseSSE >= 2) {
 831       subptr(rsp, 2*wordSize);
 832       fstp_d(Address(rsp, 0));
 833       movdbl(xmm0, Address(rsp, 0));
 834       addptr(rsp, 2*wordSize);
 835     }
 836   }
 837 }
 838 
 839 #endif /* !CC_INTERP */
 840 
 841 
 842 // Lock object
 843 //
 844 // Argument: rdx : Points to BasicObjectLock to be used for locking. Must
 845 // be initialized with object to lock
 846 void InterpreterMacroAssembler::lock_object(Register lock_reg) {
 847   assert(lock_reg == rdx, "The argument is only for looks. It must be rdx");
 848 
 849   if (UseHeavyMonitors) {
 850     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter), lock_reg);
 851   } else {
 852 
 853     Label done;
 854 
 855     const Register swap_reg = rax;  // Must use rax, for cmpxchg instruction
 856     const Register obj_reg  = rcx;  // Will contain the oop
 857 
 858     const int obj_offset = BasicObjectLock::obj_offset_in_bytes();
 859     const int lock_offset = BasicObjectLock::lock_offset_in_bytes ();
 860     const int mark_offset = lock_offset + BasicLock::displaced_header_offset_in_bytes();
 861 
 862     Label slow_case;
 863 
 864     // Load object pointer into obj_reg %rcx
 865     movptr(obj_reg, Address(lock_reg, obj_offset));
 866 
 867     if (UseBiasedLocking) {
 868       // Note: we use noreg for the temporary register since it's hard
 869       // to come up with a free register on all incoming code paths
 870       biased_locking_enter(lock_reg, obj_reg, swap_reg, noreg, false, done, &slow_case);
 871     }
 872 
 873     // Load immediate 1 into swap_reg %rax,
 874     movptr(swap_reg, (int32_t)1);
 875 
 876     // Load (object->mark() | 1) into swap_reg %rax,
 877     orptr(swap_reg, Address(obj_reg, 0));
 878 
 879     // Save (object->mark() | 1) into BasicLock's displaced header
 880     movptr(Address(lock_reg, mark_offset), swap_reg);
 881 
 882     assert(lock_offset == 0, "displached header must be first word in BasicObjectLock");
 883     if (os::is_MP()) {
 884       lock();
 885     }
 886     cmpxchgptr(lock_reg, Address(obj_reg, 0));
 887     if (PrintBiasedLockingStatistics) {
 888       cond_inc32(Assembler::zero,
 889                  ExternalAddress((address) BiasedLocking::fast_path_entry_count_addr()));
 890     }
 891     jcc(Assembler::zero, done);
 892 
 893     // Test if the oopMark is an obvious stack pointer, i.e.,
 894     //  1) (mark & 3) == 0, and
 895     //  2) rsp <= mark < mark + os::pagesize()
 896     //
 897     // These 3 tests can be done by evaluating the following
 898     // expression: ((mark - rsp) & (3 - os::vm_page_size())),
 899     // assuming both stack pointer and pagesize have their
 900     // least significant 2 bits clear.
 901     // NOTE: the oopMark is in swap_reg %rax, as the result of cmpxchg
 902     subptr(swap_reg, rsp);
 903     andptr(swap_reg, 3 - os::vm_page_size());
 904 
 905     // Save the test result, for recursive case, the result is zero
 906     movptr(Address(lock_reg, mark_offset), swap_reg);
 907 
 908     if (PrintBiasedLockingStatistics) {
 909       cond_inc32(Assembler::zero,
 910                  ExternalAddress((address) BiasedLocking::fast_path_entry_count_addr()));
 911     }
 912     jcc(Assembler::zero, done);
 913 
 914     bind(slow_case);
 915 
 916     // Call the runtime routine for slow case
 917     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter), lock_reg);
 918 
 919     bind(done);
 920   }
 921 }
 922 
 923 
 924 // Unlocks an object. Used in monitorexit bytecode and remove_activation.
 925 //
 926 // Argument: rdx : Points to BasicObjectLock structure for lock
 927 // Throw an IllegalMonitorException if object is not locked by current thread
 928 //
 929 // Uses: rax, rbx, rcx, rdx
 930 void InterpreterMacroAssembler::unlock_object(Register lock_reg) {
 931   assert(lock_reg == rdx, "The argument is only for looks. It must be rdx");
 932 
 933   if (UseHeavyMonitors) {
 934     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit), lock_reg);
 935   } else {
 936     Label done;
 937 
 938     const Register swap_reg   = rax;  // Must use rax, for cmpxchg instruction
 939     const Register header_reg = rbx;  // Will contain the old oopMark
 940     const Register obj_reg    = rcx;  // Will contain the oop
 941 
 942     save_bcp(); // Save in case of exception
 943 
 944     // Convert from BasicObjectLock structure to object and BasicLock structure
 945     // Store the BasicLock address into %rax,
 946     lea(swap_reg, Address(lock_reg, BasicObjectLock::lock_offset_in_bytes()));
 947 
 948     // Load oop into obj_reg(%rcx)
 949     movptr(obj_reg, Address(lock_reg, BasicObjectLock::obj_offset_in_bytes ()));
 950 
 951     // Free entry
 952     movptr(Address(lock_reg, BasicObjectLock::obj_offset_in_bytes()), (int32_t)NULL_WORD);
 953 
 954     if (UseBiasedLocking) {
 955       biased_locking_exit(obj_reg, header_reg, done);
 956     }
 957 
 958     // Load the old header from BasicLock structure
 959     movptr(header_reg, Address(swap_reg, BasicLock::displaced_header_offset_in_bytes()));
 960 
 961     // Test for recursion
 962     testptr(header_reg, header_reg);
 963 
 964     // zero for recursive case
 965     jcc(Assembler::zero, done);
 966 
 967     // Atomic swap back the old header
 968     if (os::is_MP()) lock();
 969     cmpxchgptr(header_reg, Address(obj_reg, 0));
 970 
 971     // zero for recursive case
 972     jcc(Assembler::zero, done);
 973 
 974     // Call the runtime routine for slow case.
 975     movptr(Address(lock_reg, BasicObjectLock::obj_offset_in_bytes()), obj_reg); // restore obj
 976     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit), lock_reg);
 977 
 978     bind(done);
 979 
 980     restore_bcp();
 981   }
 982 }
 983 
 984 
 985 #ifndef CC_INTERP
 986 
 987 // Test ImethodDataPtr.  If it is null, continue at the specified label
 988 void InterpreterMacroAssembler::test_method_data_pointer(Register mdp, Label& zero_continue) {
 989   assert(ProfileInterpreter, "must be profiling interpreter");
 990   movptr(mdp, Address(rbp, frame::interpreter_frame_mdx_offset * wordSize));
 991   testptr(mdp, mdp);
 992   jcc(Assembler::zero, zero_continue);
 993 }
 994 
 995 
 996 // Set the method data pointer for the current bcp.
 997 void InterpreterMacroAssembler::set_method_data_pointer_for_bcp() {
 998   assert(ProfileInterpreter, "must be profiling interpreter");
 999   Label zero_continue;
1000   push(rax);
1001   push(rbx);
1002 
1003   get_method(rbx);
1004   // Test MDO to avoid the call if it is NULL.
1005   movptr(rax, Address(rbx, in_bytes(methodOopDesc::method_data_offset())));
1006   testptr(rax, rax);
1007   jcc(Assembler::zero, zero_continue);
1008 
1009   // rbx,: method
1010   // rsi: bcp
1011   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::bcp_to_di), rbx, rsi);
1012   // rax,: mdi
1013 
1014   movptr(rbx, Address(rbx, in_bytes(methodOopDesc::method_data_offset())));
1015   testptr(rbx, rbx);
1016   jcc(Assembler::zero, zero_continue);
1017   addptr(rbx, in_bytes(methodDataOopDesc::data_offset()));
1018   addptr(rbx, rax);
1019   movptr(Address(rbp, frame::interpreter_frame_mdx_offset * wordSize), rbx);
1020 
1021   bind(zero_continue);
1022   pop(rbx);
1023   pop(rax);
1024 }
1025 
1026 void InterpreterMacroAssembler::verify_method_data_pointer() {
1027   assert(ProfileInterpreter, "must be profiling interpreter");
1028 #ifdef ASSERT
1029   Label verify_continue;
1030   push(rax);
1031   push(rbx);
1032   push(rcx);
1033   push(rdx);
1034   test_method_data_pointer(rcx, verify_continue); // If mdp is zero, continue
1035   get_method(rbx);
1036 
1037   // If the mdp is valid, it will point to a DataLayout header which is
1038   // consistent with the bcp.  The converse is highly probable also.
1039   load_unsigned_word(rdx, Address(rcx, in_bytes(DataLayout::bci_offset())));
1040   addptr(rdx, Address(rbx, methodOopDesc::const_offset()));
1041   lea(rdx, Address(rdx, constMethodOopDesc::codes_offset()));
1042   cmpptr(rdx, rsi);
1043   jcc(Assembler::equal, verify_continue);
1044   // rbx,: method
1045   // rsi: bcp
1046   // rcx: mdp
1047   call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::verify_mdp), rbx, rsi, rcx);
1048   bind(verify_continue);
1049   pop(rdx);
1050   pop(rcx);
1051   pop(rbx);
1052   pop(rax);
1053 #endif // ASSERT
1054 }
1055 
1056 
1057 void InterpreterMacroAssembler::set_mdp_data_at(Register mdp_in, int constant, Register value) {
1058   // %%% this seems to be used to store counter data which is surely 32bits
1059   // however 64bit side stores 64 bits which seems wrong
1060   assert(ProfileInterpreter, "must be profiling interpreter");
1061   Address data(mdp_in, constant);
1062   movptr(data, value);
1063 }
1064 
1065 
1066 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in,
1067                                                       int constant,
1068                                                       bool decrement) {
1069   // Counter address
1070   Address data(mdp_in, constant);
1071 
1072   increment_mdp_data_at(data, decrement);
1073 }
1074 
1075 
1076 void InterpreterMacroAssembler::increment_mdp_data_at(Address data,
1077                                                       bool decrement) {
1078 
1079   assert( DataLayout::counter_increment==1, "flow-free idiom only works with 1" );
1080   assert(ProfileInterpreter, "must be profiling interpreter");
1081 
1082   // %%% 64bit treats this as 64 bit which seems unlikely
1083   if (decrement) {
1084     // Decrement the register.  Set condition codes.
1085     addl(data, -DataLayout::counter_increment);
1086     // If the decrement causes the counter to overflow, stay negative
1087     Label L;
1088     jcc(Assembler::negative, L);
1089     addl(data, DataLayout::counter_increment);
1090     bind(L);
1091   } else {
1092     assert(DataLayout::counter_increment == 1,
1093            "flow-free idiom only works with 1");
1094     // Increment the register.  Set carry flag.
1095     addl(data, DataLayout::counter_increment);
1096     // If the increment causes the counter to overflow, pull back by 1.
1097     sbbl(data, 0);
1098   }
1099 }
1100 
1101 
1102 void InterpreterMacroAssembler::increment_mdp_data_at(Register mdp_in,
1103                                                       Register reg,
1104                                                       int constant,
1105                                                       bool decrement) {
1106   Address data(mdp_in, reg, Address::times_1, constant);
1107 
1108   increment_mdp_data_at(data, decrement);
1109 }
1110 
1111 
1112 void InterpreterMacroAssembler::set_mdp_flag_at(Register mdp_in, int flag_byte_constant) {
1113   assert(ProfileInterpreter, "must be profiling interpreter");
1114   int header_offset = in_bytes(DataLayout::header_offset());
1115   int header_bits = DataLayout::flag_mask_to_header_mask(flag_byte_constant);
1116   // Set the flag
1117   orl(Address(mdp_in, header_offset), header_bits);
1118 }
1119 
1120 
1121 
1122 void InterpreterMacroAssembler::test_mdp_data_at(Register mdp_in,
1123                                                  int offset,
1124                                                  Register value,
1125                                                  Register test_value_out,
1126                                                  Label& not_equal_continue) {
1127   assert(ProfileInterpreter, "must be profiling interpreter");
1128   if (test_value_out == noreg) {
1129     cmpptr(value, Address(mdp_in, offset));
1130   } else {
1131     // Put the test value into a register, so caller can use it:
1132     movptr(test_value_out, Address(mdp_in, offset));
1133     cmpptr(test_value_out, value);
1134   }
1135   jcc(Assembler::notEqual, not_equal_continue);
1136 }
1137 
1138 
1139 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in, int offset_of_disp) {
1140   assert(ProfileInterpreter, "must be profiling interpreter");
1141   Address disp_address(mdp_in, offset_of_disp);
1142   addptr(mdp_in,disp_address);
1143   movptr(Address(rbp, frame::interpreter_frame_mdx_offset * wordSize), mdp_in);
1144 }
1145 
1146 
1147 void InterpreterMacroAssembler::update_mdp_by_offset(Register mdp_in, Register reg, int offset_of_disp) {
1148   assert(ProfileInterpreter, "must be profiling interpreter");
1149   Address disp_address(mdp_in, reg, Address::times_1, offset_of_disp);
1150   addptr(mdp_in, disp_address);
1151   movptr(Address(rbp, frame::interpreter_frame_mdx_offset * wordSize), mdp_in);
1152 }
1153 
1154 
1155 void InterpreterMacroAssembler::update_mdp_by_constant(Register mdp_in, int constant) {
1156   assert(ProfileInterpreter, "must be profiling interpreter");
1157   addptr(mdp_in, constant);
1158   movptr(Address(rbp, frame::interpreter_frame_mdx_offset * wordSize), mdp_in);
1159 }
1160 
1161 
1162 void InterpreterMacroAssembler::update_mdp_for_ret(Register return_bci) {
1163   assert(ProfileInterpreter, "must be profiling interpreter");
1164   push(return_bci);             // save/restore across call_VM
1165   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::update_mdp_for_ret), return_bci);
1166   pop(return_bci);
1167 }
1168 
1169 
1170 void InterpreterMacroAssembler::profile_taken_branch(Register mdp, Register bumped_count) {
1171   if (ProfileInterpreter) {
1172     Label profile_continue;
1173 
1174     // If no method data exists, go to profile_continue.
1175     // Otherwise, assign to mdp
1176     test_method_data_pointer(mdp, profile_continue);
1177 
1178     // We are taking a branch.  Increment the taken count.
1179     // We inline increment_mdp_data_at to return bumped_count in a register
1180     //increment_mdp_data_at(mdp, in_bytes(JumpData::taken_offset()));
1181     Address data(mdp, in_bytes(JumpData::taken_offset()));
1182 
1183     // %%% 64bit treats these cells as 64 bit but they seem to be 32 bit
1184     movl(bumped_count,data);
1185     assert( DataLayout::counter_increment==1, "flow-free idiom only works with 1" );
1186     addl(bumped_count, DataLayout::counter_increment);
1187     sbbl(bumped_count, 0);
1188     movl(data,bumped_count);    // Store back out
1189 
1190     // The method data pointer needs to be updated to reflect the new target.
1191     update_mdp_by_offset(mdp, in_bytes(JumpData::displacement_offset()));
1192     bind (profile_continue);
1193   }
1194 }
1195 
1196 
1197 void InterpreterMacroAssembler::profile_not_taken_branch(Register mdp) {
1198   if (ProfileInterpreter) {
1199     Label profile_continue;
1200 
1201     // If no method data exists, go to profile_continue.
1202     test_method_data_pointer(mdp, profile_continue);
1203 
1204     // We are taking a branch.  Increment the not taken count.
1205     increment_mdp_data_at(mdp, in_bytes(BranchData::not_taken_offset()));
1206 
1207     // The method data pointer needs to be updated to correspond to the next bytecode
1208     update_mdp_by_constant(mdp, in_bytes(BranchData::branch_data_size()));
1209     bind (profile_continue);
1210   }
1211 }
1212 
1213 
1214 void InterpreterMacroAssembler::profile_call(Register mdp) {
1215   if (ProfileInterpreter) {
1216     Label profile_continue;
1217 
1218     // If no method data exists, go to profile_continue.
1219     test_method_data_pointer(mdp, profile_continue);
1220 
1221     // We are making a call.  Increment the count.
1222     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1223 
1224     // The method data pointer needs to be updated to reflect the new target.
1225     update_mdp_by_constant(mdp, in_bytes(CounterData::counter_data_size()));
1226     bind (profile_continue);
1227   }
1228 }
1229 
1230 
1231 void InterpreterMacroAssembler::profile_final_call(Register mdp) {
1232   if (ProfileInterpreter) {
1233     Label profile_continue;
1234 
1235     // If no method data exists, go to profile_continue.
1236     test_method_data_pointer(mdp, profile_continue);
1237 
1238     // We are making a call.  Increment the count.
1239     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1240 
1241     // The method data pointer needs to be updated to reflect the new target.
1242     update_mdp_by_constant(mdp, in_bytes(VirtualCallData::virtual_call_data_size()));
1243     bind (profile_continue);
1244   }
1245 }
1246 
1247 
1248 void InterpreterMacroAssembler::profile_virtual_call(Register receiver, Register mdp, Register reg2) {
1249   if (ProfileInterpreter) {
1250     Label profile_continue;
1251 
1252     // If no method data exists, go to profile_continue.
1253     test_method_data_pointer(mdp, profile_continue);
1254 
1255     // We are making a call.  Increment the count.
1256     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1257 
1258     // Record the receiver type.
1259     record_klass_in_profile(receiver, mdp, reg2);
1260 
1261     // The method data pointer needs to be updated to reflect the new target.
1262     update_mdp_by_constant(mdp,
1263                            in_bytes(VirtualCallData::
1264                                     virtual_call_data_size()));
1265     bind(profile_continue);
1266   }
1267 }
1268 
1269 
1270 void InterpreterMacroAssembler::record_klass_in_profile_helper(
1271                                         Register receiver, Register mdp,
1272                                         Register reg2,
1273                                         int start_row, Label& done) {
1274   int last_row = VirtualCallData::row_limit() - 1;
1275   assert(start_row <= last_row, "must be work left to do");
1276   // Test this row for both the receiver and for null.
1277   // Take any of three different outcomes:
1278   //   1. found receiver => increment count and goto done
1279   //   2. found null => keep looking for case 1, maybe allocate this cell
1280   //   3. found something else => keep looking for cases 1 and 2
1281   // Case 3 is handled by a recursive call.
1282   for (int row = start_row; row <= last_row; row++) {
1283     Label next_test;
1284     bool test_for_null_also = (row == start_row);
1285 
1286     // See if the receiver is receiver[n].
1287     int recvr_offset = in_bytes(VirtualCallData::receiver_offset(row));
1288     test_mdp_data_at(mdp, recvr_offset, receiver,
1289                      (test_for_null_also ? reg2 : noreg),
1290                      next_test);
1291     // (Reg2 now contains the receiver from the CallData.)
1292 
1293     // The receiver is receiver[n].  Increment count[n].
1294     int count_offset = in_bytes(VirtualCallData::receiver_count_offset(row));
1295     increment_mdp_data_at(mdp, count_offset);
1296     jmp(done);
1297     bind(next_test);
1298 
1299     if (row == start_row) {
1300       // Failed the equality check on receiver[n]...  Test for null.
1301       testptr(reg2, reg2);
1302       if (start_row == last_row) {
1303         // The only thing left to do is handle the null case.
1304         jcc(Assembler::notZero, done);
1305         break;
1306       }
1307       // Since null is rare, make it be the branch-taken case.
1308       Label found_null;
1309       jcc(Assembler::zero, found_null);
1310 
1311       // Put all the "Case 3" tests here.
1312       record_klass_in_profile_helper(receiver, mdp, reg2, start_row + 1, done);
1313 
1314       // Found a null.  Keep searching for a matching receiver,
1315       // but remember that this is an empty (unused) slot.
1316       bind(found_null);
1317     }
1318   }
1319 
1320   // In the fall-through case, we found no matching receiver, but we
1321   // observed the receiver[start_row] is NULL.
1322 
1323   // Fill in the receiver field and increment the count.
1324   int recvr_offset = in_bytes(VirtualCallData::receiver_offset(start_row));
1325   set_mdp_data_at(mdp, recvr_offset, receiver);
1326   int count_offset = in_bytes(VirtualCallData::receiver_count_offset(start_row));
1327   movptr(reg2, (int32_t)DataLayout::counter_increment);
1328   set_mdp_data_at(mdp, count_offset, reg2);
1329   jmp(done);
1330 }
1331 
1332 void InterpreterMacroAssembler::record_klass_in_profile(Register receiver,
1333                                                         Register mdp,
1334                                                         Register reg2) {
1335   assert(ProfileInterpreter, "must be profiling");
1336   Label done;
1337 
1338   record_klass_in_profile_helper(receiver, mdp, reg2, 0, done);
1339 
1340   bind (done);
1341 }
1342 
1343 void InterpreterMacroAssembler::profile_ret(Register return_bci, Register mdp) {
1344   if (ProfileInterpreter) {
1345     Label profile_continue;
1346     uint row;
1347 
1348     // If no method data exists, go to profile_continue.
1349     test_method_data_pointer(mdp, profile_continue);
1350 
1351     // Update the total ret count.
1352     increment_mdp_data_at(mdp, in_bytes(CounterData::count_offset()));
1353 
1354     for (row = 0; row < RetData::row_limit(); row++) {
1355       Label next_test;
1356 
1357       // See if return_bci is equal to bci[n]:
1358       test_mdp_data_at(mdp, in_bytes(RetData::bci_offset(row)), return_bci,
1359                        noreg, next_test);
1360 
1361       // return_bci is equal to bci[n].  Increment the count.
1362       increment_mdp_data_at(mdp, in_bytes(RetData::bci_count_offset(row)));
1363 
1364       // The method data pointer needs to be updated to reflect the new target.
1365       update_mdp_by_offset(mdp, in_bytes(RetData::bci_displacement_offset(row)));
1366       jmp(profile_continue);
1367       bind(next_test);
1368     }
1369 
1370     update_mdp_for_ret(return_bci);
1371 
1372     bind (profile_continue);
1373   }
1374 }
1375 
1376 
1377 void InterpreterMacroAssembler::profile_null_seen(Register mdp) {
1378   if (ProfileInterpreter) {
1379     Label profile_continue;
1380 
1381     // If no method data exists, go to profile_continue.
1382     test_method_data_pointer(mdp, profile_continue);
1383 
1384     // The method data pointer needs to be updated.
1385     int mdp_delta = in_bytes(BitData::bit_data_size());
1386     if (TypeProfileCasts) {
1387       mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1388     }
1389     update_mdp_by_constant(mdp, mdp_delta);
1390 
1391     bind (profile_continue);
1392   }
1393 }
1394 
1395 
1396 void InterpreterMacroAssembler::profile_typecheck_failed(Register mdp) {
1397   if (ProfileInterpreter && TypeProfileCasts) {
1398     Label profile_continue;
1399 
1400     // If no method data exists, go to profile_continue.
1401     test_method_data_pointer(mdp, profile_continue);
1402 
1403     int count_offset = in_bytes(CounterData::count_offset());
1404     // Back up the address, since we have already bumped the mdp.
1405     count_offset -= in_bytes(VirtualCallData::virtual_call_data_size());
1406 
1407     // *Decrement* the counter.  We expect to see zero or small negatives.
1408     increment_mdp_data_at(mdp, count_offset, true);
1409 
1410     bind (profile_continue);
1411   }
1412 }
1413 
1414 
1415 void InterpreterMacroAssembler::profile_typecheck(Register mdp, Register klass, Register reg2)
1416 {
1417   if (ProfileInterpreter) {
1418     Label profile_continue;
1419 
1420     // If no method data exists, go to profile_continue.
1421     test_method_data_pointer(mdp, profile_continue);
1422 
1423     // The method data pointer needs to be updated.
1424     int mdp_delta = in_bytes(BitData::bit_data_size());
1425     if (TypeProfileCasts) {
1426       mdp_delta = in_bytes(VirtualCallData::virtual_call_data_size());
1427 
1428       // Record the object type.
1429       record_klass_in_profile(klass, mdp, reg2);
1430       assert(reg2 == rdi, "we know how to fix this blown reg");
1431       restore_locals();         // Restore EDI
1432     }
1433     update_mdp_by_constant(mdp, mdp_delta);
1434 
1435     bind(profile_continue);
1436   }
1437 }
1438 
1439 
1440 void InterpreterMacroAssembler::profile_switch_default(Register mdp) {
1441   if (ProfileInterpreter) {
1442     Label profile_continue;
1443 
1444     // If no method data exists, go to profile_continue.
1445     test_method_data_pointer(mdp, profile_continue);
1446 
1447     // Update the default case count
1448     increment_mdp_data_at(mdp, in_bytes(MultiBranchData::default_count_offset()));
1449 
1450     // The method data pointer needs to be updated.
1451     update_mdp_by_offset(mdp, in_bytes(MultiBranchData::default_displacement_offset()));
1452 
1453     bind (profile_continue);
1454   }
1455 }
1456 
1457 
1458 void InterpreterMacroAssembler::profile_switch_case(Register index, Register mdp, Register reg2) {
1459   if (ProfileInterpreter) {
1460     Label profile_continue;
1461 
1462     // If no method data exists, go to profile_continue.
1463     test_method_data_pointer(mdp, profile_continue);
1464 
1465     // Build the base (index * per_case_size_in_bytes()) + case_array_offset_in_bytes()
1466     movptr(reg2, (int32_t)in_bytes(MultiBranchData::per_case_size()));
1467     // index is positive and so should have correct value if this code were
1468     // used on 64bits
1469     imulptr(index, reg2);
1470     addptr(index, in_bytes(MultiBranchData::case_array_offset()));
1471 
1472     // Update the case count
1473     increment_mdp_data_at(mdp, index, in_bytes(MultiBranchData::relative_count_offset()));
1474 
1475     // The method data pointer needs to be updated.
1476     update_mdp_by_offset(mdp, index, in_bytes(MultiBranchData::relative_displacement_offset()));
1477 
1478     bind (profile_continue);
1479   }
1480 }
1481 
1482 #endif // !CC_INTERP
1483 
1484 
1485 
1486 void InterpreterMacroAssembler::verify_oop(Register reg, TosState state) {
1487   if (state == atos) MacroAssembler::verify_oop(reg);
1488 }
1489 
1490 
1491 #ifndef CC_INTERP
1492 void InterpreterMacroAssembler::verify_FPU(int stack_depth, TosState state) {
1493   if (state == ftos || state == dtos) MacroAssembler::verify_FPU(stack_depth);
1494 }
1495 
1496 #endif /* CC_INTERP */
1497 
1498 
1499 void InterpreterMacroAssembler::notify_method_entry() {
1500   // Whenever JVMTI is interp_only_mode, method entry/exit events are sent to
1501   // track stack depth.  If it is possible to enter interp_only_mode we add
1502   // the code to check if the event should be sent.
1503   if (JvmtiExport::can_post_interpreter_events()) {
1504     Label L;
1505     get_thread(rcx);
1506     movl(rcx, Address(rcx, JavaThread::interp_only_mode_offset()));
1507     testl(rcx,rcx);
1508     jcc(Assembler::zero, L);
1509     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_method_entry));
1510     bind(L);
1511   }
1512 
1513   {
1514     SkipIfEqual skip_if(this, &DTraceMethodProbes, 0);
1515     get_thread(rcx);
1516     get_method(rbx);
1517     call_VM_leaf(
1518       CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry), rcx, rbx);
1519   }
1520 }
1521 
1522 
1523 void InterpreterMacroAssembler::notify_method_exit(
1524     TosState state, NotifyMethodExitMode mode) {
1525   // Whenever JVMTI is interp_only_mode, method entry/exit events are sent to
1526   // track stack depth.  If it is possible to enter interp_only_mode we add
1527   // the code to check if the event should be sent.
1528   if (mode == NotifyJVMTI && JvmtiExport::can_post_interpreter_events()) {
1529     Label L;
1530     // Note: frame::interpreter_frame_result has a dependency on how the
1531     // method result is saved across the call to post_method_exit. If this
1532     // is changed then the interpreter_frame_result implementation will
1533     // need to be updated too.
1534 
1535     // For c++ interpreter the result is always stored at a known location in the frame
1536     // template interpreter will leave it on the top of the stack.
1537     NOT_CC_INTERP(push(state);)
1538     get_thread(rcx);
1539     movl(rcx, Address(rcx, JavaThread::interp_only_mode_offset()));
1540     testl(rcx,rcx);
1541     jcc(Assembler::zero, L);
1542     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_method_exit));
1543     bind(L);
1544     NOT_CC_INTERP(pop(state);)
1545   }
1546 
1547   {
1548     SkipIfEqual skip_if(this, &DTraceMethodProbes, 0);
1549     NOT_CC_INTERP(push(state));
1550     get_thread(rbx);
1551     get_method(rcx);
1552     call_VM_leaf(
1553       CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit),
1554       rbx, rcx);
1555     NOT_CC_INTERP(pop(state));
1556   }
1557 }