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