1 /*
   2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "interp_masm_sparc.hpp"
  27 #include "interpreter/interpreter.hpp"
  28 #include "interpreter/interpreterRuntime.hpp"
  29 #include "logging/log.hpp"
  30 #include "oops/arrayOop.hpp"
  31 #include "oops/markOop.hpp"
  32 #include "oops/methodData.hpp"
  33 #include "oops/method.hpp"
  34 #include "oops/methodCounters.hpp"
  35 #include "prims/jvmtiExport.hpp"
  36 #include "prims/jvmtiThreadState.hpp"
  37 #include "runtime/basicLock.hpp"
  38 #include "runtime/biasedLocking.hpp"
  39 #include "runtime/sharedRuntime.hpp"
  40 #include "runtime/thread.inline.hpp"
  41 
  42 #ifndef FAST_DISPATCH
  43 #define FAST_DISPATCH 1
  44 #endif
  45 #undef FAST_DISPATCH
  46 
  47 // Implementation of InterpreterMacroAssembler
  48 
  49 // This file specializes the assember with interpreter-specific macros
  50 
  51 const Address InterpreterMacroAssembler::l_tmp(FP, (frame::interpreter_frame_l_scratch_fp_offset * wordSize) + STACK_BIAS);
  52 const Address InterpreterMacroAssembler::d_tmp(FP, (frame::interpreter_frame_d_scratch_fp_offset * wordSize) + STACK_BIAS);
  53 
  54 void InterpreterMacroAssembler::jump_to_entry(address entry) {
  55   assert(entry, "Entry must have been generated by now");
  56   AddressLiteral al(entry);
  57   jump_to(al, G3_scratch);
  58   delayed()->nop();
  59 }
  60 
  61 void InterpreterMacroAssembler::compute_extra_locals_size_in_bytes(Register args_size, Register locals_size, Register delta) {
  62   // Note: this algorithm is also used by C1's OSR entry sequence.
  63   // Any changes should also be applied to CodeEmitter::emit_osr_entry().
  64   assert_different_registers(args_size, locals_size);
  65   // max_locals*2 for TAGS.  Assumes that args_size has already been adjusted.
  66   subcc(locals_size, args_size, delta);// extra space for non-arguments locals in words
  67   // Use br/mov combination because it works on both V8 and V9 and is
  68   // faster.
  69   Label skip_move;
  70   br(Assembler::negative, true, Assembler::pt, skip_move);
  71   delayed()->mov(G0, delta);
  72   bind(skip_move);
  73   round_to(delta, WordsPerLong);       // make multiple of 2 (SP must be 2-word aligned)
  74   sll(delta, LogBytesPerWord, delta);  // extra space for locals in bytes
  75 }
  76 
  77 // Dispatch code executed in the prolog of a bytecode which does not do it's
  78 // own dispatch. The dispatch address is computed and placed in IdispatchAddress
  79 void InterpreterMacroAssembler::dispatch_prolog(TosState state, int bcp_incr) {
  80   assert_not_delayed();
  81 #ifdef FAST_DISPATCH
  82   // FAST_DISPATCH and ProfileInterpreter are mutually exclusive since
  83   // they both use I2.
  84   assert(!ProfileInterpreter, "FAST_DISPATCH and +ProfileInterpreter are mutually exclusive");
  85   ldub(Lbcp, bcp_incr, Lbyte_code);                     // load next bytecode
  86   add(Lbyte_code, Interpreter::distance_from_dispatch_table(state), Lbyte_code);
  87                                                         // add offset to correct dispatch table
  88   sll(Lbyte_code, LogBytesPerWord, Lbyte_code);         // multiply by wordSize
  89   ld_ptr(IdispatchTables, Lbyte_code, IdispatchAddress);// get entry addr
  90 #else
  91   ldub( Lbcp, bcp_incr, Lbyte_code);                    // load next bytecode
  92   // dispatch table to use
  93   AddressLiteral tbl(Interpreter::dispatch_table(state));
  94   sll(Lbyte_code, LogBytesPerWord, Lbyte_code);         // multiply by wordSize
  95   set(tbl, G3_scratch);                                 // compute addr of table
  96   ld_ptr(G3_scratch, Lbyte_code, IdispatchAddress);     // get entry addr
  97 #endif
  98 }
  99 
 100 
 101 // Dispatch code executed in the epilog of a bytecode which does not do it's
 102 // own dispatch. The dispatch address in IdispatchAddress is used for the
 103 // dispatch.
 104 void InterpreterMacroAssembler::dispatch_epilog(TosState state, int bcp_incr) {
 105   assert_not_delayed();
 106   verify_FPU(1, state);
 107   interp_verify_oop(Otos_i, state, __FILE__, __LINE__);
 108   jmp( IdispatchAddress, 0 );
 109   if (bcp_incr != 0)  delayed()->inc(Lbcp, bcp_incr);
 110   else                delayed()->nop();
 111 }
 112 
 113 
 114 void InterpreterMacroAssembler::dispatch_next(TosState state, int bcp_incr) {
 115   // %%%% consider branching to a single shared dispatch stub (for each bcp_incr)
 116   assert_not_delayed();
 117   ldub( Lbcp, bcp_incr, Lbyte_code);               // load next bytecode
 118   dispatch_Lbyte_code(state, Interpreter::dispatch_table(state), bcp_incr);
 119 }
 120 
 121 
 122 void InterpreterMacroAssembler::dispatch_next_noverify_oop(TosState state, int bcp_incr) {
 123   // %%%% consider branching to a single shared dispatch stub (for each bcp_incr)
 124   assert_not_delayed();
 125   ldub( Lbcp, bcp_incr, Lbyte_code);               // load next bytecode
 126   dispatch_Lbyte_code(state, Interpreter::dispatch_table(state), bcp_incr, false);
 127 }
 128 
 129 
 130 void InterpreterMacroAssembler::dispatch_via(TosState state, address* table) {
 131   // load current bytecode
 132   assert_not_delayed();
 133   ldub( Lbcp, 0, Lbyte_code);               // load next bytecode
 134   dispatch_base(state, table);
 135 }
 136 
 137 
 138 void InterpreterMacroAssembler::call_VM_leaf_base(
 139   Register java_thread,
 140   address  entry_point,
 141   int      number_of_arguments
 142 ) {
 143   if (!java_thread->is_valid())
 144     java_thread = L7_thread_cache;
 145   // super call
 146   MacroAssembler::call_VM_leaf_base(java_thread, entry_point, number_of_arguments);
 147 }
 148 
 149 
 150 void InterpreterMacroAssembler::call_VM_base(
 151   Register        oop_result,
 152   Register        java_thread,
 153   Register        last_java_sp,
 154   address         entry_point,
 155   int             number_of_arguments,
 156   bool            check_exception
 157 ) {
 158   if (!java_thread->is_valid())
 159     java_thread = L7_thread_cache;
 160   // See class ThreadInVMfromInterpreter, which assumes that the interpreter
 161   // takes responsibility for setting its own thread-state on call-out.
 162   // However, ThreadInVMfromInterpreter resets the state to "in_Java".
 163 
 164   //save_bcp();                                  // save bcp
 165   MacroAssembler::call_VM_base(oop_result, java_thread, last_java_sp, entry_point, number_of_arguments, check_exception);
 166   //restore_bcp();                               // restore bcp
 167   //restore_locals();                            // restore locals pointer
 168 }
 169 
 170 
 171 void InterpreterMacroAssembler::check_and_handle_popframe(Register scratch_reg) {
 172   if (JvmtiExport::can_pop_frame()) {
 173     Label L;
 174 
 175     // Check the "pending popframe condition" flag in the current thread
 176     ld(G2_thread, JavaThread::popframe_condition_offset(), scratch_reg);
 177 
 178     // Initiate popframe handling only if it is not already being processed.  If the flag
 179     // has the popframe_processing bit set, it means that this code is called *during* popframe
 180     // handling - we don't want to reenter.
 181     btst(JavaThread::popframe_pending_bit, scratch_reg);
 182     br(zero, false, pt, L);
 183     delayed()->nop();
 184     btst(JavaThread::popframe_processing_bit, scratch_reg);
 185     br(notZero, false, pt, L);
 186     delayed()->nop();
 187 
 188     // Call Interpreter::remove_activation_preserving_args_entry() to get the
 189     // address of the same-named entrypoint in the generated interpreter code.
 190     call_VM_leaf(noreg, CAST_FROM_FN_PTR(address, Interpreter::remove_activation_preserving_args_entry));
 191 
 192     // Jump to Interpreter::_remove_activation_preserving_args_entry
 193     jmpl(O0, G0, G0);
 194     delayed()->nop();
 195     bind(L);
 196   }
 197 }
 198 
 199 
 200 void InterpreterMacroAssembler::load_earlyret_value(TosState state) {
 201   Register thr_state = G4_scratch;
 202   ld_ptr(G2_thread, JavaThread::jvmti_thread_state_offset(), thr_state);
 203   const Address tos_addr(thr_state, JvmtiThreadState::earlyret_tos_offset());
 204   const Address oop_addr(thr_state, JvmtiThreadState::earlyret_oop_offset());
 205   const Address val_addr(thr_state, JvmtiThreadState::earlyret_value_offset());
 206   switch (state) {
 207   case ltos: ld_long(val_addr, Otos_l);                   break;
 208   case atos: ld_ptr(oop_addr, Otos_l);
 209              st_ptr(G0, oop_addr);                        break;
 210   case btos:                                           // fall through
 211   case ztos:                                           // fall through
 212   case ctos:                                           // fall through
 213   case stos:                                           // fall through
 214   case itos: ld(val_addr, Otos_l1);                       break;
 215   case ftos: ldf(FloatRegisterImpl::S, val_addr, Ftos_f); break;
 216   case dtos: ldf(FloatRegisterImpl::D, val_addr, Ftos_d); break;
 217   case vtos: /* nothing to do */                          break;
 218   default  : ShouldNotReachHere();
 219   }
 220   // Clean up tos value in the jvmti thread state
 221   or3(G0, ilgl, G3_scratch);
 222   stw(G3_scratch, tos_addr);
 223   st_long(G0, val_addr);
 224   interp_verify_oop(Otos_i, state, __FILE__, __LINE__);
 225 }
 226 
 227 
 228 void InterpreterMacroAssembler::check_and_handle_earlyret(Register scratch_reg) {
 229   if (JvmtiExport::can_force_early_return()) {
 230     Label L;
 231     Register thr_state = G3_scratch;
 232     ld_ptr(G2_thread, JavaThread::jvmti_thread_state_offset(), thr_state);
 233     br_null_short(thr_state, pt, L); // if (thread->jvmti_thread_state() == NULL) exit;
 234 
 235     // Initiate earlyret handling only if it is not already being processed.
 236     // If the flag has the earlyret_processing bit set, it means that this code
 237     // is called *during* earlyret handling - we don't want to reenter.
 238     ld(thr_state, JvmtiThreadState::earlyret_state_offset(), G4_scratch);
 239     cmp_and_br_short(G4_scratch, JvmtiThreadState::earlyret_pending, Assembler::notEqual, pt, L);
 240 
 241     // Call Interpreter::remove_activation_early_entry() to get the address of the
 242     // same-named entrypoint in the generated interpreter code
 243     ld(thr_state, JvmtiThreadState::earlyret_tos_offset(), Otos_l1);
 244     call_VM_leaf(noreg, CAST_FROM_FN_PTR(address, Interpreter::remove_activation_early_entry), Otos_l1);
 245 
 246     // Jump to Interpreter::_remove_activation_early_entry
 247     jmpl(O0, G0, G0);
 248     delayed()->nop();
 249     bind(L);
 250   }
 251 }
 252 
 253 
 254 void InterpreterMacroAssembler::super_call_VM_leaf(Register thread_cache, address entry_point, Register arg_1, Register arg_2) {
 255   mov(arg_1, O0);
 256   mov(arg_2, O1);
 257   MacroAssembler::call_VM_leaf_base(thread_cache, entry_point, 2);
 258 }
 259 
 260 void InterpreterMacroAssembler::dispatch_base(TosState state, address* table) {
 261   assert_not_delayed();
 262   dispatch_Lbyte_code(state, table);
 263 }
 264 
 265 
 266 void InterpreterMacroAssembler::dispatch_normal(TosState state) {
 267   dispatch_base(state, Interpreter::normal_table(state));
 268 }
 269 
 270 
 271 void InterpreterMacroAssembler::dispatch_only(TosState state) {
 272   dispatch_base(state, Interpreter::dispatch_table(state));
 273 }
 274 
 275 
 276 // common code to dispatch and dispatch_only
 277 // dispatch value in Lbyte_code and increment Lbcp
 278 
 279 void InterpreterMacroAssembler::dispatch_Lbyte_code(TosState state, address* table, int bcp_incr, bool verify) {
 280   verify_FPU(1, state);
 281   // %%%%% maybe implement +VerifyActivationFrameSize here
 282   //verify_thread(); //too slow; we will just verify on method entry & exit
 283   if (verify) interp_verify_oop(Otos_i, state, __FILE__, __LINE__);
 284 #ifdef FAST_DISPATCH
 285   if (table == Interpreter::dispatch_table(state)) {
 286     // use IdispatchTables
 287     add(Lbyte_code, Interpreter::distance_from_dispatch_table(state), Lbyte_code);
 288                                                         // add offset to correct dispatch table
 289     sll(Lbyte_code, LogBytesPerWord, Lbyte_code);       // multiply by wordSize
 290     ld_ptr(IdispatchTables, Lbyte_code, G3_scratch);    // get entry addr
 291   } else {
 292 #endif
 293     // dispatch table to use
 294     AddressLiteral tbl(table);
 295     sll(Lbyte_code, LogBytesPerWord, Lbyte_code);       // multiply by wordSize
 296     set(tbl, G3_scratch);                               // compute addr of table
 297     ld_ptr(G3_scratch, Lbyte_code, G3_scratch);         // get entry addr
 298 #ifdef FAST_DISPATCH
 299   }
 300 #endif
 301   jmp( G3_scratch, 0 );
 302   if (bcp_incr != 0)  delayed()->inc(Lbcp, bcp_incr);
 303   else                delayed()->nop();
 304 }
 305 
 306 
 307 // Helpers for expression stack
 308 
 309 // Longs and doubles are Category 2 computational types in the
 310 // JVM specification (section 3.11.1) and take 2 expression stack or
 311 // local slots.
 312 // Aligning them on 32 bit with tagged stacks is hard because the code generated
 313 // for the dup* bytecodes depends on what types are already on the stack.
 314 // If the types are split into the two stack/local slots, that is much easier
 315 // (and we can use 0 for non-reference tags).
 316 
 317 // Known good alignment in _LP64 but unknown otherwise
 318 void InterpreterMacroAssembler::load_unaligned_double(Register r1, int offset, FloatRegister d) {
 319   assert_not_delayed();
 320 
 321   ldf(FloatRegisterImpl::D, r1, offset, d);
 322 }
 323 
 324 // Known good alignment in _LP64 but unknown otherwise
 325 void InterpreterMacroAssembler::store_unaligned_double(FloatRegister d, Register r1, int offset) {
 326   assert_not_delayed();
 327 
 328   stf(FloatRegisterImpl::D, d, r1, offset);
 329   // store something more useful here
 330   debug_only(stx(G0, r1, offset+Interpreter::stackElementSize);)
 331 }
 332 
 333 
 334 // Known good alignment in _LP64 but unknown otherwise
 335 void InterpreterMacroAssembler::load_unaligned_long(Register r1, int offset, Register rd) {
 336   assert_not_delayed();
 337   ldx(r1, offset, rd);
 338 }
 339 
 340 // Known good alignment in _LP64 but unknown otherwise
 341 void InterpreterMacroAssembler::store_unaligned_long(Register l, Register r1, int offset) {
 342   assert_not_delayed();
 343 
 344   stx(l, r1, offset);
 345   // store something more useful here
 346   stx(G0, r1, offset+Interpreter::stackElementSize);
 347 }
 348 
 349 void InterpreterMacroAssembler::pop_i(Register r) {
 350   assert_not_delayed();
 351   ld(Lesp, Interpreter::expr_offset_in_bytes(0), r);
 352   inc(Lesp, Interpreter::stackElementSize);
 353   debug_only(verify_esp(Lesp));
 354 }
 355 
 356 void InterpreterMacroAssembler::pop_ptr(Register r, Register scratch) {
 357   assert_not_delayed();
 358   ld_ptr(Lesp, Interpreter::expr_offset_in_bytes(0), r);
 359   inc(Lesp, Interpreter::stackElementSize);
 360   debug_only(verify_esp(Lesp));
 361 }
 362 
 363 void InterpreterMacroAssembler::pop_l(Register r) {
 364   assert_not_delayed();
 365   load_unaligned_long(Lesp, Interpreter::expr_offset_in_bytes(0), r);
 366   inc(Lesp, 2*Interpreter::stackElementSize);
 367   debug_only(verify_esp(Lesp));
 368 }
 369 
 370 
 371 void InterpreterMacroAssembler::pop_f(FloatRegister f, Register scratch) {
 372   assert_not_delayed();
 373   ldf(FloatRegisterImpl::S, Lesp, Interpreter::expr_offset_in_bytes(0), f);
 374   inc(Lesp, Interpreter::stackElementSize);
 375   debug_only(verify_esp(Lesp));
 376 }
 377 
 378 
 379 void InterpreterMacroAssembler::pop_d(FloatRegister f, Register scratch) {
 380   assert_not_delayed();
 381   load_unaligned_double(Lesp, Interpreter::expr_offset_in_bytes(0), f);
 382   inc(Lesp, 2*Interpreter::stackElementSize);
 383   debug_only(verify_esp(Lesp));
 384 }
 385 
 386 
 387 void InterpreterMacroAssembler::push_i(Register r) {
 388   assert_not_delayed();
 389   debug_only(verify_esp(Lesp));
 390   st(r, Lesp, 0);
 391   dec(Lesp, Interpreter::stackElementSize);
 392 }
 393 
 394 void InterpreterMacroAssembler::push_ptr(Register r) {
 395   assert_not_delayed();
 396   st_ptr(r, Lesp, 0);
 397   dec(Lesp, Interpreter::stackElementSize);
 398 }
 399 
 400 // remember: our convention for longs in SPARC is:
 401 // O0 (Otos_l1) has high-order part in first word,
 402 // O1 (Otos_l2) has low-order part in second word
 403 
 404 void InterpreterMacroAssembler::push_l(Register r) {
 405   assert_not_delayed();
 406   debug_only(verify_esp(Lesp));
 407   // Longs are stored in memory-correct order, even if unaligned.
 408   int offset = -Interpreter::stackElementSize;
 409   store_unaligned_long(r, Lesp, offset);
 410   dec(Lesp, 2 * Interpreter::stackElementSize);
 411 }
 412 
 413 
 414 void InterpreterMacroAssembler::push_f(FloatRegister f) {
 415   assert_not_delayed();
 416   debug_only(verify_esp(Lesp));
 417   stf(FloatRegisterImpl::S, f, Lesp, 0);
 418   dec(Lesp, Interpreter::stackElementSize);
 419 }
 420 
 421 
 422 void InterpreterMacroAssembler::push_d(FloatRegister d)   {
 423   assert_not_delayed();
 424   debug_only(verify_esp(Lesp));
 425   // Longs are stored in memory-correct order, even if unaligned.
 426   int offset = -Interpreter::stackElementSize;
 427   store_unaligned_double(d, Lesp, offset);
 428   dec(Lesp, 2 * Interpreter::stackElementSize);
 429 }
 430 
 431 
 432 void InterpreterMacroAssembler::push(TosState state) {
 433   interp_verify_oop(Otos_i, state, __FILE__, __LINE__);
 434   switch (state) {
 435     case atos: push_ptr();            break;
 436     case btos:                        // fall through
 437     case ztos:                        // fall through
 438     case ctos:                        // fall through
 439     case stos:                        // fall through
 440     case itos: push_i();              break;
 441     case ltos: push_l();              break;
 442     case ftos: push_f();              break;
 443     case dtos: push_d();              break;
 444     case vtos: /* nothing to do */    break;
 445     default  : ShouldNotReachHere();
 446   }
 447 }
 448 
 449 
 450 void InterpreterMacroAssembler::pop(TosState state) {
 451   switch (state) {
 452     case atos: pop_ptr();            break;
 453     case btos:                       // fall through
 454     case ztos:                       // fall through
 455     case ctos:                       // fall through
 456     case stos:                       // fall through
 457     case itos: pop_i();              break;
 458     case ltos: pop_l();              break;
 459     case ftos: pop_f();              break;
 460     case dtos: pop_d();              break;
 461     case vtos: /* nothing to do */   break;
 462     default  : ShouldNotReachHere();
 463   }
 464   interp_verify_oop(Otos_i, state, __FILE__, __LINE__);
 465 }
 466 
 467 
 468 // Helpers for swap and dup
 469 void InterpreterMacroAssembler::load_ptr(int n, Register val) {
 470   ld_ptr(Lesp, Interpreter::expr_offset_in_bytes(n), val);
 471 }
 472 void InterpreterMacroAssembler::store_ptr(int n, Register val) {
 473   st_ptr(val, Lesp, Interpreter::expr_offset_in_bytes(n));
 474 }
 475 
 476 
 477 void InterpreterMacroAssembler::load_receiver(Register param_count,
 478                                               Register recv) {
 479   sll(param_count, Interpreter::logStackElementSize, param_count);
 480   ld_ptr(Lesp, param_count, recv);  // gets receiver oop
 481 }
 482 
 483 void InterpreterMacroAssembler::empty_expression_stack() {
 484   // Reset Lesp.
 485   sub( Lmonitors, wordSize, Lesp );
 486 
 487   // Reset SP by subtracting more space from Lesp.
 488   Label done;
 489   assert(G4_scratch != Gframe_size, "Only you can prevent register aliasing!");
 490 
 491   // A native does not need to do this, since its callee does not change SP.
 492   ld(Lmethod, Method::access_flags_offset(), Gframe_size);  // Load access flags.
 493   btst(JVM_ACC_NATIVE, Gframe_size);
 494   br(Assembler::notZero, false, Assembler::pt, done);
 495   delayed()->nop();
 496 
 497   // Compute max expression stack+register save area
 498   ld_ptr(Lmethod, in_bytes(Method::const_offset()), Gframe_size);
 499   lduh(Gframe_size, in_bytes(ConstMethod::max_stack_offset()), Gframe_size);  // Load max stack.
 500   add(Gframe_size, frame::memory_parameter_word_sp_offset+Method::extra_stack_entries(), Gframe_size );
 501 
 502   //
 503   // now set up a stack frame with the size computed above
 504   //
 505   //round_to( Gframe_size, WordsPerLong ); // -- moved down to the "and" below
 506   sll( Gframe_size, LogBytesPerWord, Gframe_size );
 507   sub( Lesp, Gframe_size, Gframe_size );
 508   and3( Gframe_size, -(2 * wordSize), Gframe_size );          // align SP (downwards) to an 8/16-byte boundary
 509   debug_only(verify_sp(Gframe_size, G4_scratch));
 510   sub(Gframe_size, STACK_BIAS, Gframe_size );
 511   mov(Gframe_size, SP);
 512 
 513   bind(done);
 514 }
 515 
 516 
 517 #ifdef ASSERT
 518 void InterpreterMacroAssembler::verify_sp(Register Rsp, Register Rtemp) {
 519   Label Bad, OK;
 520 
 521   // Saved SP must be aligned.
 522   btst(2*BytesPerWord-1, Rsp);
 523   br(Assembler::notZero, false, Assembler::pn, Bad);
 524   delayed()->nop();
 525 
 526   // Saved SP, plus register window size, must not be above FP.
 527   add(Rsp, frame::register_save_words * wordSize, Rtemp);
 528   sub(Rtemp, STACK_BIAS, Rtemp);  // Bias Rtemp before cmp to FP
 529   cmp_and_brx_short(Rtemp, FP, Assembler::greaterUnsigned, Assembler::pn, Bad);
 530 
 531   // Saved SP must not be ridiculously below current SP.
 532   size_t maxstack = MAX2(JavaThread::stack_size_at_create(), (size_t) 4*K*K);
 533   set(maxstack, Rtemp);
 534   sub(SP, Rtemp, Rtemp);
 535   add(Rtemp, STACK_BIAS, Rtemp);  // Unbias Rtemp before cmp to Rsp
 536   cmp_and_brx_short(Rsp, Rtemp, Assembler::lessUnsigned, Assembler::pn, Bad);
 537 
 538   ba_short(OK);
 539 
 540   bind(Bad);
 541   stop("on return to interpreted call, restored SP is corrupted");
 542 
 543   bind(OK);
 544 }
 545 
 546 
 547 void InterpreterMacroAssembler::verify_esp(Register Resp) {
 548   // about to read or write Resp[0]
 549   // make sure it is not in the monitors or the register save area
 550   Label OK1, OK2;
 551 
 552   cmp(Resp, Lmonitors);
 553   brx(Assembler::lessUnsigned, true, Assembler::pt, OK1);
 554   delayed()->sub(Resp, frame::memory_parameter_word_sp_offset * wordSize, Resp);
 555   stop("too many pops:  Lesp points into monitor area");
 556   bind(OK1);
 557   sub(Resp, STACK_BIAS, Resp);
 558   cmp(Resp, SP);
 559   brx(Assembler::greaterEqualUnsigned, false, Assembler::pt, OK2);
 560   delayed()->add(Resp, STACK_BIAS + frame::memory_parameter_word_sp_offset * wordSize, Resp);
 561   stop("too many pushes:  Lesp points into register window");
 562   bind(OK2);
 563 }
 564 #endif // ASSERT
 565 
 566 // Load compiled (i2c) or interpreter entry when calling from interpreted and
 567 // do the call. Centralized so that all interpreter calls will do the same actions.
 568 // If jvmti single stepping is on for a thread we must not call compiled code.
 569 void InterpreterMacroAssembler::call_from_interpreter(Register target, Register scratch, Register Rret) {
 570 
 571   // Assume we want to go compiled if available
 572 
 573   ld_ptr(G5_method, in_bytes(Method::from_interpreted_offset()), target);
 574 
 575   if (JvmtiExport::can_post_interpreter_events()) {
 576     // JVMTI events, such as single-stepping, are implemented partly by avoiding running
 577     // compiled code in threads for which the event is enabled.  Check here for
 578     // interp_only_mode if these events CAN be enabled.
 579     verify_thread();
 580     Label skip_compiled_code;
 581 
 582     const Address interp_only(G2_thread, JavaThread::interp_only_mode_offset());
 583     ld(interp_only, scratch);
 584     cmp_zero_and_br(Assembler::notZero, scratch, skip_compiled_code, true, Assembler::pn);
 585     delayed()->ld_ptr(G5_method, in_bytes(Method::interpreter_entry_offset()), target);
 586     bind(skip_compiled_code);
 587   }
 588 
 589   // the i2c_adapters need Method* in G5_method (right? %%%)
 590   // do the call
 591 #ifdef ASSERT
 592   {
 593     Label ok;
 594     br_notnull_short(target, Assembler::pt, ok);
 595     stop("null entry point");
 596     bind(ok);
 597   }
 598 #endif // ASSERT
 599 
 600   // Adjust Rret first so Llast_SP can be same as Rret
 601   add(Rret, -frame::pc_return_offset, O7);
 602   add(Lesp, BytesPerWord, Gargs); // setup parameter pointer
 603   // Record SP so we can remove any stack space allocated by adapter transition
 604   jmp(target, 0);
 605   delayed()->mov(SP, Llast_SP);
 606 }
 607 
 608 void InterpreterMacroAssembler::if_cmp(Condition cc, bool ptr_compare) {
 609   assert_not_delayed();
 610 
 611   Label not_taken;
 612   if (ptr_compare) brx(cc, false, Assembler::pn, not_taken);
 613   else             br (cc, false, Assembler::pn, not_taken);
 614   delayed()->nop();
 615 
 616   TemplateTable::branch(false,false);
 617 
 618   bind(not_taken);
 619 
 620   profile_not_taken_branch(G3_scratch);
 621 }
 622 
 623 
 624 void InterpreterMacroAssembler::get_2_byte_integer_at_bcp(
 625                                   int         bcp_offset,
 626                                   Register    Rtmp,
 627                                   Register    Rdst,
 628                                   signedOrNot is_signed,
 629                                   setCCOrNot  should_set_CC ) {
 630   assert(Rtmp != Rdst, "need separate temp register");
 631   assert_not_delayed();
 632   switch (is_signed) {
 633    default: ShouldNotReachHere();
 634 
 635    case   Signed:  ldsb( Lbcp, bcp_offset, Rdst  );  break; // high byte
 636    case Unsigned:  ldub( Lbcp, bcp_offset, Rdst  );  break; // high byte
 637   }
 638   ldub( Lbcp, bcp_offset + 1, Rtmp ); // low byte
 639   sll( Rdst, BitsPerByte, Rdst);
 640   switch (should_set_CC ) {
 641    default: ShouldNotReachHere();
 642 
 643    case      set_CC:  orcc( Rdst, Rtmp, Rdst ); break;
 644    case dont_set_CC:  or3(  Rdst, Rtmp, Rdst ); break;
 645   }
 646 }
 647 
 648 
 649 void InterpreterMacroAssembler::get_4_byte_integer_at_bcp(
 650                                   int        bcp_offset,
 651                                   Register   Rtmp,
 652                                   Register   Rdst,
 653                                   setCCOrNot should_set_CC ) {
 654   assert(Rtmp != Rdst, "need separate temp register");
 655   assert_not_delayed();
 656   add( Lbcp, bcp_offset, Rtmp);
 657   andcc( Rtmp, 3, G0);
 658   Label aligned;
 659   switch (should_set_CC ) {
 660    default: ShouldNotReachHere();
 661 
 662    case      set_CC: break;
 663    case dont_set_CC: break;
 664   }
 665 
 666   br(Assembler::zero, true, Assembler::pn, aligned);
 667   delayed()->ldsw(Rtmp, 0, Rdst);
 668 
 669   ldub(Lbcp, bcp_offset + 3, Rdst);
 670   ldub(Lbcp, bcp_offset + 2, Rtmp);  sll(Rtmp,  8, Rtmp);  or3(Rtmp, Rdst, Rdst);
 671   ldub(Lbcp, bcp_offset + 1, Rtmp);  sll(Rtmp, 16, Rtmp);  or3(Rtmp, Rdst, Rdst);
 672   ldsb(Lbcp, bcp_offset + 0, Rtmp);  sll(Rtmp, 24, Rtmp);
 673   or3(Rtmp, Rdst, Rdst );
 674 
 675   bind(aligned);
 676   if (should_set_CC == set_CC) tst(Rdst);
 677 }
 678 
 679 void InterpreterMacroAssembler::get_cache_index_at_bcp(Register temp, Register index,
 680                                                        int bcp_offset, size_t index_size) {
 681   assert(bcp_offset > 0, "bcp is still pointing to start of bytecode");
 682   if (index_size == sizeof(u2)) {
 683     get_2_byte_integer_at_bcp(bcp_offset, temp, index, Unsigned);
 684   } else if (index_size == sizeof(u4)) {
 685     get_4_byte_integer_at_bcp(bcp_offset, temp, index);
 686     assert(ConstantPool::decode_invokedynamic_index(~123) == 123, "else change next line");
 687     xor3(index, -1, index);  // convert to plain index
 688   } else if (index_size == sizeof(u1)) {
 689     ldub(Lbcp, bcp_offset, index);
 690   } else {
 691     ShouldNotReachHere();
 692   }
 693 }
 694 
 695 
 696 void InterpreterMacroAssembler::get_cache_and_index_at_bcp(Register cache, Register tmp,
 697                                                            int bcp_offset, size_t index_size) {
 698   assert(bcp_offset > 0, "bcp is still pointing to start of bytecode");
 699   assert_different_registers(cache, tmp);
 700   assert_not_delayed();
 701   get_cache_index_at_bcp(cache, tmp, bcp_offset, index_size);
 702   // convert from field index to ConstantPoolCacheEntry index and from
 703   // word index to byte offset
 704   sll(tmp, exact_log2(in_words(ConstantPoolCacheEntry::size()) * BytesPerWord), tmp);
 705   add(LcpoolCache, tmp, cache);
 706 }
 707 
 708 
 709 void InterpreterMacroAssembler::get_cache_and_index_and_bytecode_at_bcp(Register cache,
 710                                                                         Register temp,
 711                                                                         Register bytecode,
 712                                                                         int byte_no,
 713                                                                         int bcp_offset,
 714                                                                         size_t index_size) {
 715   get_cache_and_index_at_bcp(cache, temp, bcp_offset, index_size);
 716   ld_ptr(cache, ConstantPoolCache::base_offset() + ConstantPoolCacheEntry::indices_offset(), bytecode);
 717   const int shift_count = (1 + byte_no) * BitsPerByte;
 718   assert((byte_no == TemplateTable::f1_byte && shift_count == ConstantPoolCacheEntry::bytecode_1_shift) ||
 719          (byte_no == TemplateTable::f2_byte && shift_count == ConstantPoolCacheEntry::bytecode_2_shift),
 720          "correct shift count");
 721   srl(bytecode, shift_count, bytecode);
 722   assert(ConstantPoolCacheEntry::bytecode_1_mask == ConstantPoolCacheEntry::bytecode_2_mask, "common mask");
 723   and3(bytecode, ConstantPoolCacheEntry::bytecode_1_mask, bytecode);
 724 }
 725 
 726 
 727 void InterpreterMacroAssembler::get_cache_entry_pointer_at_bcp(Register cache, Register tmp,
 728                                                                int bcp_offset, size_t index_size) {
 729   assert(bcp_offset > 0, "bcp is still pointing to start of bytecode");
 730   assert_different_registers(cache, tmp);
 731   assert_not_delayed();
 732   if (index_size == sizeof(u2)) {
 733     get_2_byte_integer_at_bcp(bcp_offset, cache, tmp, Unsigned);
 734   } else {
 735     ShouldNotReachHere();  // other sizes not supported here
 736   }
 737               // convert from field index to ConstantPoolCacheEntry index
 738               // and from word index to byte offset
 739   sll(tmp, exact_log2(in_words(ConstantPoolCacheEntry::size()) * BytesPerWord), tmp);
 740               // skip past the header
 741   add(tmp, in_bytes(ConstantPoolCache::base_offset()), tmp);
 742               // construct pointer to cache entry
 743   add(LcpoolCache, tmp, cache);
 744 }
 745 
 746 
 747 // Load object from cpool->resolved_references(index)
 748 void InterpreterMacroAssembler::load_resolved_reference_at_index(
 749                                            Register result, Register index) {
 750   assert_different_registers(result, index);
 751   assert_not_delayed();
 752   // convert from field index to resolved_references() index and from
 753   // word index to byte offset. Since this is a java object, it can be compressed
 754   Register tmp = index;  // reuse
 755   sll(index, LogBytesPerHeapOop, tmp);
 756   get_constant_pool(result);
 757   // load pointer for resolved_references[] objArray
 758   ld_ptr(result, ConstantPool::cache_offset_in_bytes(), result);
 759   ld_ptr(result, ConstantPoolCache::resolved_references_offset_in_bytes(), result);
 760   // JNIHandles::resolve(result)
 761   ld_ptr(result, 0, result);
 762   // Add in the index
 763   add(result, tmp, result);
 764   load_heap_oop(result, arrayOopDesc::base_offset_in_bytes(T_OBJECT), result);
 765 }
 766 
 767 
 768 // load cpool->resolved_klass_at(index)
 769 void InterpreterMacroAssembler::load_resolved_klass_at_offset(Register Rcpool,
 770                                            Register Roffset, Register Rklass) {
 771   // int value = *this_cp->int_at_addr(which);
 772   // int resolved_klass_index = extract_low_short_from_int(value);
 773   //
 774   // Because SPARC is big-endian, the low_short is at (cpool->int_at_addr(which) + 2 bytes)
 775   add(Roffset, Rcpool, Roffset);
 776   lduh(Roffset, sizeof(ConstantPool) + 2, Roffset);  // Roffset = resolved_klass_index
 777 
 778   Register Rresolved_klasses = Rklass;
 779   ld_ptr(Rcpool, ConstantPool::resolved_klasses_offset_in_bytes(), Rresolved_klasses);
 780   sll(Roffset, LogBytesPerWord, Roffset); 
 781   add(Roffset, Array<Klass*>::base_offset_in_bytes(), Roffset);
 782   ld_ptr(Rresolved_klasses, Roffset, Rklass);
 783 }
 784 
 785 
 786 // Generate a subtype check: branch to ok_is_subtype if sub_klass is
 787 // a subtype of super_klass.  Blows registers Rsuper_klass, Rsub_klass, tmp1, tmp2.
 788 void InterpreterMacroAssembler::gen_subtype_check(Register Rsub_klass,
 789                                                   Register Rsuper_klass,
 790                                                   Register Rtmp1,
 791                                                   Register Rtmp2,
 792                                                   Register Rtmp3,
 793                                                   Label &ok_is_subtype ) {
 794   Label not_subtype;
 795 
 796   // Profile the not-null value's klass.
 797   profile_typecheck(Rsub_klass, Rtmp1);
 798 
 799   check_klass_subtype_fast_path(Rsub_klass, Rsuper_klass,
 800                                 Rtmp1, Rtmp2,
 801                                 &ok_is_subtype, &not_subtype, NULL);
 802 
 803   check_klass_subtype_slow_path(Rsub_klass, Rsuper_klass,
 804                                 Rtmp1, Rtmp2, Rtmp3, /*hack:*/ noreg,
 805                                 &ok_is_subtype, NULL);
 806 
 807   bind(not_subtype);
 808   profile_typecheck_failed(Rtmp1);
 809 }
 810 
 811 // Separate these two to allow for delay slot in middle
 812 // These are used to do a test and full jump to exception-throwing code.
 813 
 814 // %%%%% Could possibly reoptimize this by testing to see if could use
 815 // a single conditional branch (i.e. if span is small enough.
 816 // If you go that route, than get rid of the split and give up
 817 // on the delay-slot hack.
 818 
 819 void InterpreterMacroAssembler::throw_if_not_1_icc( Condition ok_condition,
 820                                                     Label&    ok ) {
 821   assert_not_delayed();
 822   br(ok_condition, true, pt, ok);
 823   // DELAY SLOT
 824 }
 825 
 826 void InterpreterMacroAssembler::throw_if_not_1_xcc( Condition ok_condition,
 827                                                     Label&    ok ) {
 828   assert_not_delayed();
 829   bp( ok_condition, true, Assembler::xcc, pt, ok);
 830   // DELAY SLOT
 831 }
 832 
 833 void InterpreterMacroAssembler::throw_if_not_1_x( Condition ok_condition,
 834                                                   Label&    ok ) {
 835   assert_not_delayed();
 836   brx(ok_condition, true, pt, ok);
 837   // DELAY SLOT
 838 }
 839 
 840 void InterpreterMacroAssembler::throw_if_not_2( address  throw_entry_point,
 841                                                 Register Rscratch,
 842                                                 Label&   ok ) {
 843   assert(throw_entry_point != NULL, "entry point must be generated by now");
 844   AddressLiteral dest(throw_entry_point);
 845   jump_to(dest, Rscratch);
 846   delayed()->nop();
 847   bind(ok);
 848 }
 849 
 850 
 851 // And if you cannot use the delay slot, here is a shorthand:
 852 
 853 void InterpreterMacroAssembler::throw_if_not_icc( Condition ok_condition,
 854                                                   address   throw_entry_point,
 855                                                   Register  Rscratch ) {
 856   Label ok;
 857   if (ok_condition != never) {
 858     throw_if_not_1_icc( ok_condition, ok);
 859     delayed()->nop();
 860   }
 861   throw_if_not_2( throw_entry_point, Rscratch, ok);
 862 }
 863 void InterpreterMacroAssembler::throw_if_not_xcc( Condition ok_condition,
 864                                                   address   throw_entry_point,
 865                                                   Register  Rscratch ) {
 866   Label ok;
 867   if (ok_condition != never) {
 868     throw_if_not_1_xcc( ok_condition, ok);
 869     delayed()->nop();
 870   }
 871   throw_if_not_2( throw_entry_point, Rscratch, ok);
 872 }
 873 void InterpreterMacroAssembler::throw_if_not_x( Condition ok_condition,
 874                                                 address   throw_entry_point,
 875                                                 Register  Rscratch ) {
 876   Label ok;
 877   if (ok_condition != never) {
 878     throw_if_not_1_x( ok_condition, ok);
 879     delayed()->nop();
 880   }
 881   throw_if_not_2( throw_entry_point, Rscratch, ok);
 882 }
 883 
 884 // Check that index is in range for array, then shift index by index_shift, and put arrayOop + shifted_index into res
 885 // Note: res is still shy of address by array offset into object.
 886 
 887 void InterpreterMacroAssembler::index_check_without_pop(Register array, Register index, int index_shift, Register tmp, Register res) {
 888   assert_not_delayed();
 889 
 890   verify_oop(array);
 891   // sign extend since tos (index) can be a 32bit value
 892   sra(index, G0, index);
 893 
 894   // check array
 895   Label ptr_ok;
 896   tst(array);
 897   throw_if_not_1_x( notZero, ptr_ok );
 898   delayed()->ld( array, arrayOopDesc::length_offset_in_bytes(), tmp ); // check index
 899   throw_if_not_2( Interpreter::_throw_NullPointerException_entry, G3_scratch, ptr_ok);
 900 
 901   Label index_ok;
 902   cmp(index, tmp);
 903   throw_if_not_1_icc( lessUnsigned, index_ok );
 904   if (index_shift > 0)  delayed()->sll(index, index_shift, index);
 905   else                  delayed()->add(array, index, res); // addr - const offset in index
 906   // convention: move aberrant index into G3_scratch for exception message
 907   mov(index, G3_scratch);
 908   throw_if_not_2( Interpreter::_throw_ArrayIndexOutOfBoundsException_entry, G4_scratch, index_ok);
 909 
 910   // add offset if didn't do it in delay slot
 911   if (index_shift > 0)   add(array, index, res); // addr - const offset in index
 912 }
 913 
 914 
 915 void InterpreterMacroAssembler::index_check(Register array, Register index, int index_shift, Register tmp, Register res) {
 916   assert_not_delayed();
 917 
 918   // pop array
 919   pop_ptr(array);
 920 
 921   // check array
 922   index_check_without_pop(array, index, index_shift, tmp, res);
 923 }
 924 
 925 
 926 void InterpreterMacroAssembler::get_const(Register Rdst) {
 927   ld_ptr(Lmethod, in_bytes(Method::const_offset()), Rdst);
 928 }
 929 
 930 
 931 void InterpreterMacroAssembler::get_constant_pool(Register Rdst) {
 932   get_const(Rdst);
 933   ld_ptr(Rdst, in_bytes(ConstMethod::constants_offset()), Rdst);
 934 }
 935 
 936 
 937 void InterpreterMacroAssembler::get_constant_pool_cache(Register Rdst) {
 938   get_constant_pool(Rdst);
 939   ld_ptr(Rdst, ConstantPool::cache_offset_in_bytes(), Rdst);
 940 }
 941 
 942 
 943 void InterpreterMacroAssembler::get_cpool_and_tags(Register Rcpool, Register Rtags) {
 944   get_constant_pool(Rcpool);
 945   ld_ptr(Rcpool, ConstantPool::tags_offset_in_bytes(), Rtags);
 946 }
 947 
 948 
 949 // unlock if synchronized method
 950 //
 951 // Unlock the receiver if this is a synchronized method.
 952 // Unlock any Java monitors from syncronized blocks.
 953 //
 954 // If there are locked Java monitors
 955 //    If throw_monitor_exception
 956 //       throws IllegalMonitorStateException
 957 //    Else if install_monitor_exception
 958 //       installs IllegalMonitorStateException
 959 //    Else
 960 //       no error processing
 961 void InterpreterMacroAssembler::unlock_if_synchronized_method(TosState state,
 962                                                               bool throw_monitor_exception,
 963                                                               bool install_monitor_exception) {
 964   Label unlocked, unlock, no_unlock;
 965 
 966   // get the value of _do_not_unlock_if_synchronized into G1_scratch
 967   const Address do_not_unlock_if_synchronized(G2_thread,
 968     JavaThread::do_not_unlock_if_synchronized_offset());
 969   ldbool(do_not_unlock_if_synchronized, G1_scratch);
 970   stbool(G0, do_not_unlock_if_synchronized); // reset the flag
 971 
 972   // check if synchronized method
 973   const Address access_flags(Lmethod, Method::access_flags_offset());
 974   interp_verify_oop(Otos_i, state, __FILE__, __LINE__);
 975   push(state); // save tos
 976   ld(access_flags, G3_scratch); // Load access flags.
 977   btst(JVM_ACC_SYNCHRONIZED, G3_scratch);
 978   br(zero, false, pt, unlocked);
 979   delayed()->nop();
 980 
 981   // Don't unlock anything if the _do_not_unlock_if_synchronized flag
 982   // is set.
 983   cmp_zero_and_br(Assembler::notZero, G1_scratch, no_unlock);
 984   delayed()->nop();
 985 
 986   // BasicObjectLock will be first in list, since this is a synchronized method. However, need
 987   // to check that the object has not been unlocked by an explicit monitorexit bytecode.
 988 
 989   //Intel: if (throw_monitor_exception) ... else ...
 990   // Entry already unlocked, need to throw exception
 991   //...
 992 
 993   // pass top-most monitor elem
 994   add( top_most_monitor(), O1 );
 995 
 996   ld_ptr(O1, BasicObjectLock::obj_offset_in_bytes(), G3_scratch);
 997   br_notnull_short(G3_scratch, pt, unlock);
 998 
 999   if (throw_monitor_exception) {
1000     // Entry already unlocked need to throw an exception
1001     MacroAssembler::call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_illegal_monitor_state_exception));
1002     should_not_reach_here();
1003   } else {
1004     // Monitor already unlocked during a stack unroll.
1005     // If requested, install an illegal_monitor_state_exception.
1006     // Continue with stack unrolling.
1007     if (install_monitor_exception) {
1008       MacroAssembler::call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::new_illegal_monitor_state_exception));
1009     }
1010     ba_short(unlocked);
1011   }
1012 
1013   bind(unlock);
1014 
1015   unlock_object(O1);
1016 
1017   bind(unlocked);
1018 
1019   // I0, I1: Might contain return value
1020 
1021   // Check that all monitors are unlocked
1022   { Label loop, exception, entry, restart;
1023 
1024     Register Rmptr   = O0;
1025     Register Rtemp   = O1;
1026     Register Rlimit  = Lmonitors;
1027     const jint delta = frame::interpreter_frame_monitor_size() * wordSize;
1028     assert( (delta & LongAlignmentMask) == 0,
1029             "sizeof BasicObjectLock must be even number of doublewords");
1030 
1031     #ifdef ASSERT
1032     add(top_most_monitor(), Rmptr, delta);
1033     { Label L;
1034       // ensure that Rmptr starts out above (or at) Rlimit
1035       cmp_and_brx_short(Rmptr, Rlimit, Assembler::greaterEqualUnsigned, pn, L);
1036       stop("monitor stack has negative size");
1037       bind(L);
1038     }
1039     #endif
1040     bind(restart);
1041     ba(entry);
1042     delayed()->
1043     add(top_most_monitor(), Rmptr, delta);      // points to current entry, starting with bottom-most entry
1044 
1045     // Entry is still locked, need to throw exception
1046     bind(exception);
1047     if (throw_monitor_exception) {
1048       MacroAssembler::call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_illegal_monitor_state_exception));
1049       should_not_reach_here();
1050     } else {
1051       // Stack unrolling. Unlock object and if requested, install illegal_monitor_exception.
1052       // Unlock does not block, so don't have to worry about the frame
1053       unlock_object(Rmptr);
1054       if (install_monitor_exception) {
1055         MacroAssembler::call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::new_illegal_monitor_state_exception));
1056       }
1057       ba_short(restart);
1058     }
1059 
1060     bind(loop);
1061     cmp(Rtemp, G0);                             // check if current entry is used
1062     brx(Assembler::notEqual, false, pn, exception);
1063     delayed()->
1064     dec(Rmptr, delta);                          // otherwise advance to next entry
1065     #ifdef ASSERT
1066     { Label L;
1067       // ensure that Rmptr has not somehow stepped below Rlimit
1068       cmp_and_brx_short(Rmptr, Rlimit, Assembler::greaterEqualUnsigned, pn, L);
1069       stop("ran off the end of the monitor stack");
1070       bind(L);
1071     }
1072     #endif
1073     bind(entry);
1074     cmp(Rmptr, Rlimit);                         // check if bottom reached
1075     brx(Assembler::notEqual, true, pn, loop);   // if not at bottom then check this entry
1076     delayed()->
1077     ld_ptr(Rmptr, BasicObjectLock::obj_offset_in_bytes() - delta, Rtemp);
1078   }
1079 
1080   bind(no_unlock);
1081   pop(state);
1082   interp_verify_oop(Otos_i, state, __FILE__, __LINE__);
1083 }
1084 
1085 void InterpreterMacroAssembler::narrow(Register result) {
1086 
1087   ld_ptr(Address(Lmethod, Method::const_offset()), G3_scratch);
1088   ldub(G3_scratch, in_bytes(ConstMethod::result_type_offset()), G3_scratch);
1089 
1090   Label notBool, notByte, notChar, done;
1091 
1092   // common case first
1093   cmp(G3_scratch, T_INT);
1094   br(Assembler::equal, true, pn, done);
1095   delayed()->nop();
1096 
1097   cmp(G3_scratch, T_BOOLEAN);
1098   br(Assembler::notEqual, true, pn, notBool);
1099   delayed()->cmp(G3_scratch, T_BYTE);
1100   and3(result, 1, result);
1101   ba(done);
1102   delayed()->nop();
1103 
1104   bind(notBool);
1105   // cmp(G3_scratch, T_BYTE);
1106   br(Assembler::notEqual, true, pn, notByte);
1107   delayed()->cmp(G3_scratch, T_CHAR);
1108   sll(result, 24, result);
1109   sra(result, 24, result);
1110   ba(done);
1111   delayed()->nop();
1112 
1113   bind(notByte);
1114   // cmp(G3_scratch, T_CHAR);
1115   sll(result, 16, result);
1116   br(Assembler::notEqual, true, pn, done);
1117   delayed()->sra(result, 16, result);
1118   // sll(result, 16, result);
1119   srl(result, 16, result);
1120 
1121   // bind(notChar);
1122   // must be short, instructions already executed in delay slot
1123   // sll(result, 16, result);
1124   // sra(result, 16, result);
1125 
1126   bind(done);
1127 }
1128 
1129 // remove activation
1130 //
1131 // Unlock the receiver if this is a synchronized method.
1132 // Unlock any Java monitors from syncronized blocks.
1133 // Remove the activation from the stack.
1134 //
1135 // If there are locked Java monitors
1136 //    If throw_monitor_exception
1137 //       throws IllegalMonitorStateException
1138 //    Else if install_monitor_exception
1139 //       installs IllegalMonitorStateException
1140 //    Else
1141 //       no error processing
1142 void InterpreterMacroAssembler::remove_activation(TosState state,
1143                                                   bool throw_monitor_exception,
1144                                                   bool install_monitor_exception) {
1145 
1146   unlock_if_synchronized_method(state, throw_monitor_exception, install_monitor_exception);
1147 
1148   // save result (push state before jvmti call and pop it afterwards) and notify jvmti
1149   notify_method_exit(false, state, NotifyJVMTI);
1150 
1151   if (StackReservedPages > 0) {
1152     // testing if Stack Reserved Area needs to be re-enabled
1153     Label no_reserved_zone_enabling;
1154     ld_ptr(G2_thread, JavaThread::reserved_stack_activation_offset(), G3_scratch);
1155     cmp_and_brx_short(SP, G3_scratch, Assembler::lessUnsigned, Assembler::pt, no_reserved_zone_enabling);
1156 
1157     call_VM_leaf(noreg, CAST_FROM_FN_PTR(address, SharedRuntime::enable_stack_reserved_zone), G2_thread);
1158     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_delayed_StackOverflowError), G2_thread);
1159     should_not_reach_here();
1160 
1161     bind(no_reserved_zone_enabling);
1162   }
1163 
1164   interp_verify_oop(Otos_i, state, __FILE__, __LINE__);
1165   verify_thread();
1166 
1167   // return tos
1168   assert(Otos_l1 == Otos_i, "adjust code below");
1169   switch (state) {
1170   case ltos: mov(Otos_l, Otos_l->after_save()); break; // O0 -> I0
1171   case btos:                                      // fall through
1172   case ztos:                                      // fall through
1173   case ctos:
1174   case stos:                                      // fall through
1175   case atos:                                      // fall through
1176   case itos: mov(Otos_l1, Otos_l1->after_save());    break;        // O0 -> I0
1177   case ftos:                                      // fall through
1178   case dtos:                                      // fall through
1179   case vtos: /* nothing to do */                     break;
1180   default  : ShouldNotReachHere();
1181   }
1182 }
1183 
1184 // Lock object
1185 //
1186 // Argument - lock_reg points to the BasicObjectLock to be used for locking,
1187 //            it must be initialized with the object to lock
1188 void InterpreterMacroAssembler::lock_object(Register lock_reg, Register Object) {
1189   if (UseHeavyMonitors) {
1190     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter), lock_reg);
1191   }
1192   else {
1193     Register obj_reg = Object;
1194     Register mark_reg = G4_scratch;
1195     Register temp_reg = G1_scratch;
1196     Address  lock_addr(lock_reg, BasicObjectLock::lock_offset_in_bytes());
1197     Address  mark_addr(obj_reg, oopDesc::mark_offset_in_bytes());
1198     Label    done;
1199 
1200     Label slow_case;
1201 
1202     assert_different_registers(lock_reg, obj_reg, mark_reg, temp_reg);
1203 
1204     // load markOop from object into mark_reg
1205     ld_ptr(mark_addr, mark_reg);
1206 
1207     if (UseBiasedLocking) {
1208       biased_locking_enter(obj_reg, mark_reg, temp_reg, done, &slow_case);
1209     }
1210 
1211     // get the address of basicLock on stack that will be stored in the object
1212     // we need a temporary register here as we do not want to clobber lock_reg
1213     // (cas clobbers the destination register)
1214     mov(lock_reg, temp_reg);
1215     // set mark reg to be (markOop of object | UNLOCK_VALUE)
1216     or3(mark_reg, markOopDesc::unlocked_value, mark_reg);
1217     // initialize the box  (Must happen before we update the object mark!)
1218     st_ptr(mark_reg, lock_addr, BasicLock::displaced_header_offset_in_bytes());
1219     // compare and exchange object_addr, markOop | 1, stack address of basicLock
1220     assert(mark_addr.disp() == 0, "cas must take a zero displacement");
1221     cas_ptr(mark_addr.base(), mark_reg, temp_reg);
1222 
1223     // if the compare and exchange succeeded we are done (we saw an unlocked object)
1224     cmp_and_brx_short(mark_reg, temp_reg, Assembler::equal, Assembler::pt, done);
1225 
1226     // We did not see an unlocked object so try the fast recursive case
1227 
1228     // Check if owner is self by comparing the value in the markOop of object
1229     // with the stack pointer
1230     sub(temp_reg, SP, temp_reg);
1231     sub(temp_reg, STACK_BIAS, temp_reg);
1232     assert(os::vm_page_size() > 0xfff, "page size too small - change the constant");
1233 
1234     // Composite "andcc" test:
1235     // (a) %sp -vs- markword proximity check, and,
1236     // (b) verify mark word LSBs == 0 (Stack-locked).
1237     //
1238     // FFFFF003/FFFFFFFFFFFF003 is (markOopDesc::lock_mask_in_place | -os::vm_page_size())
1239     // Note that the page size used for %sp proximity testing is arbitrary and is
1240     // unrelated to the actual MMU page size.  We use a 'logical' page size of
1241     // 4096 bytes.   F..FFF003 is designed to fit conveniently in the SIMM13 immediate
1242     // field of the andcc instruction.
1243     andcc (temp_reg, 0xFFFFF003, G0) ;
1244 
1245     // if condition is true we are done and hence we can store 0 in the displaced
1246     // header indicating it is a recursive lock and be done
1247     brx(Assembler::zero, true, Assembler::pt, done);
1248     delayed()->st_ptr(G0, lock_addr, BasicLock::displaced_header_offset_in_bytes());
1249 
1250     // none of the above fast optimizations worked so we have to get into the
1251     // slow case of monitor enter
1252     bind(slow_case);
1253     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter), lock_reg);
1254 
1255     bind(done);
1256   }
1257 }
1258 
1259 // Unlocks an object. Used in monitorexit bytecode and remove_activation.
1260 //
1261 // Argument - lock_reg points to the BasicObjectLock for lock
1262 // Throw IllegalMonitorException if object is not locked by current thread
1263 void InterpreterMacroAssembler::unlock_object(Register lock_reg) {
1264   if (UseHeavyMonitors) {
1265     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit), lock_reg);
1266   } else {
1267     Register obj_reg = G3_scratch;
1268     Register mark_reg = G4_scratch;
1269     Register displaced_header_reg = G1_scratch;
1270     Address  lockobj_addr(lock_reg, BasicObjectLock::obj_offset_in_bytes());
1271     Address  mark_addr(obj_reg, oopDesc::mark_offset_in_bytes());
1272     Label    done;
1273 
1274     if (UseBiasedLocking) {
1275       // load the object out of the BasicObjectLock
1276       ld_ptr(lockobj_addr, obj_reg);
1277       biased_locking_exit(mark_addr, mark_reg, done, true);
1278       st_ptr(G0, lockobj_addr);  // free entry
1279     }
1280 
1281     // Test first if we are in the fast recursive case
1282     Address lock_addr(lock_reg, BasicObjectLock::lock_offset_in_bytes() + BasicLock::displaced_header_offset_in_bytes());
1283     ld_ptr(lock_addr, displaced_header_reg);
1284     br_null(displaced_header_reg, true, Assembler::pn, done);
1285     delayed()->st_ptr(G0, lockobj_addr);  // free entry
1286 
1287     // See if it is still a light weight lock, if so we just unlock
1288     // the object and we are done
1289 
1290     if (!UseBiasedLocking) {
1291       // load the object out of the BasicObjectLock
1292       ld_ptr(lockobj_addr, obj_reg);
1293     }
1294 
1295     // we have the displaced header in displaced_header_reg
1296     // we expect to see the stack address of the basicLock in case the
1297     // lock is still a light weight lock (lock_reg)
1298     assert(mark_addr.disp() == 0, "cas must take a zero displacement");
1299     cas_ptr(mark_addr.base(), lock_reg, displaced_header_reg);
1300     cmp(lock_reg, displaced_header_reg);
1301     brx(Assembler::equal, true, Assembler::pn, done);
1302     delayed()->st_ptr(G0, lockobj_addr);  // free entry
1303 
1304     // The lock has been converted into a heavy lock and hence
1305     // we need to get into the slow case
1306 
1307     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit), lock_reg);
1308 
1309     bind(done);
1310   }
1311 }
1312 
1313 // Get the method data pointer from the Method* and set the
1314 // specified register to its value.
1315 
1316 void InterpreterMacroAssembler::set_method_data_pointer() {
1317   assert(ProfileInterpreter, "must be profiling interpreter");
1318   Label get_continue;
1319 
1320   ld_ptr(Lmethod, in_bytes(Method::method_data_offset()), ImethodDataPtr);
1321   test_method_data_pointer(get_continue);
1322   add(ImethodDataPtr, in_bytes(MethodData::data_offset()), ImethodDataPtr);
1323   bind(get_continue);
1324 }
1325 
1326 // Set the method data pointer for the current bcp.
1327 
1328 void InterpreterMacroAssembler::set_method_data_pointer_for_bcp() {
1329   assert(ProfileInterpreter, "must be profiling interpreter");
1330   Label zero_continue;
1331 
1332   // Test MDO to avoid the call if it is NULL.
1333   ld_ptr(Lmethod, in_bytes(Method::method_data_offset()), ImethodDataPtr);
1334   test_method_data_pointer(zero_continue);
1335   call_VM_leaf(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::bcp_to_di), Lmethod, Lbcp);
1336   add(ImethodDataPtr, in_bytes(MethodData::data_offset()), ImethodDataPtr);
1337   add(ImethodDataPtr, O0, ImethodDataPtr);
1338   bind(zero_continue);
1339 }
1340 
1341 // Test ImethodDataPtr.  If it is null, continue at the specified label
1342 
1343 void InterpreterMacroAssembler::test_method_data_pointer(Label& zero_continue) {
1344   assert(ProfileInterpreter, "must be profiling interpreter");
1345   br_null_short(ImethodDataPtr, Assembler::pn, zero_continue);
1346 }
1347 
1348 void InterpreterMacroAssembler::verify_method_data_pointer() {
1349   assert(ProfileInterpreter, "must be profiling interpreter");
1350 #ifdef ASSERT
1351   Label verify_continue;
1352   test_method_data_pointer(verify_continue);
1353 
1354   // If the mdp is valid, it will point to a DataLayout header which is
1355   // consistent with the bcp.  The converse is highly probable also.
1356   lduh(ImethodDataPtr, in_bytes(DataLayout::bci_offset()), G3_scratch);
1357   ld_ptr(Lmethod, Method::const_offset(), O5);
1358   add(G3_scratch, in_bytes(ConstMethod::codes_offset()), G3_scratch);
1359   add(G3_scratch, O5, G3_scratch);
1360   cmp(Lbcp, G3_scratch);
1361   brx(Assembler::equal, false, Assembler::pt, verify_continue);
1362 
1363   Register temp_reg = O5;
1364   delayed()->mov(ImethodDataPtr, temp_reg);
1365   // %%% should use call_VM_leaf here?
1366   //call_VM_leaf(noreg, ..., Lmethod, Lbcp, ImethodDataPtr);
1367   save_frame_and_mov(sizeof(jdouble) / wordSize, Lmethod, O0, Lbcp, O1);
1368   Address d_save(FP, -sizeof(jdouble) + STACK_BIAS);
1369   stf(FloatRegisterImpl::D, Ftos_d, d_save);
1370   mov(temp_reg->after_save(), O2);
1371   save_thread(L7_thread_cache);
1372   call(CAST_FROM_FN_PTR(address, InterpreterRuntime::verify_mdp), relocInfo::none);
1373   delayed()->nop();
1374   restore_thread(L7_thread_cache);
1375   ldf(FloatRegisterImpl::D, d_save, Ftos_d);
1376   restore();
1377   bind(verify_continue);
1378 #endif // ASSERT
1379 }
1380 
1381 void InterpreterMacroAssembler::test_invocation_counter_for_mdp(Register invocation_count,
1382                                                                 Register method_counters,
1383                                                                 Register Rtmp,
1384                                                                 Label &profile_continue) {
1385   assert(ProfileInterpreter, "must be profiling interpreter");
1386   // Control will flow to "profile_continue" if the counter is less than the
1387   // limit or if we call profile_method()
1388 
1389   Label done;
1390 
1391   // if no method data exists, and the counter is high enough, make one
1392   br_notnull_short(ImethodDataPtr, Assembler::pn, done);
1393 
1394   // Test to see if we should create a method data oop
1395   Address profile_limit(method_counters, MethodCounters::interpreter_profile_limit_offset());
1396   ld(profile_limit, Rtmp);
1397   cmp(invocation_count, Rtmp);
1398   // Use long branches because call_VM() code and following code generated by
1399   // test_backedge_count_for_osr() is large in debug VM.
1400   br(Assembler::lessUnsigned, false, Assembler::pn, profile_continue);
1401   delayed()->nop();
1402 
1403   // Build it now.
1404   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::profile_method));
1405   set_method_data_pointer_for_bcp();
1406   ba(profile_continue);
1407   delayed()->nop();
1408   bind(done);
1409 }
1410 
1411 // Store a value at some constant offset from the method data pointer.
1412 
1413 void InterpreterMacroAssembler::set_mdp_data_at(int constant, Register value) {
1414   assert(ProfileInterpreter, "must be profiling interpreter");
1415   st_ptr(value, ImethodDataPtr, constant);
1416 }
1417 
1418 void InterpreterMacroAssembler::increment_mdp_data_at(Address counter,
1419                                                       Register bumped_count,
1420                                                       bool decrement) {
1421   assert(ProfileInterpreter, "must be profiling interpreter");
1422 
1423   // Load the counter.
1424   ld_ptr(counter, bumped_count);
1425 
1426   if (decrement) {
1427     // Decrement the register.  Set condition codes.
1428     subcc(bumped_count, DataLayout::counter_increment, bumped_count);
1429 
1430     // If the decrement causes the counter to overflow, stay negative
1431     Label L;
1432     brx(Assembler::negative, true, Assembler::pn, L);
1433 
1434     // Store the decremented counter, if it is still negative.
1435     delayed()->st_ptr(bumped_count, counter);
1436     bind(L);
1437   } else {
1438     // Increment the register.  Set carry flag.
1439     addcc(bumped_count, DataLayout::counter_increment, bumped_count);
1440 
1441     // If the increment causes the counter to overflow, pull back by 1.
1442     assert(DataLayout::counter_increment == 1, "subc works");
1443     subc(bumped_count, G0, bumped_count);
1444 
1445     // Store the incremented counter.
1446     st_ptr(bumped_count, counter);
1447   }
1448 }
1449 
1450 // Increment the value at some constant offset from the method data pointer.
1451 
1452 void InterpreterMacroAssembler::increment_mdp_data_at(int constant,
1453                                                       Register bumped_count,
1454                                                       bool decrement) {
1455   // Locate the counter at a fixed offset from the mdp:
1456   Address counter(ImethodDataPtr, constant);
1457   increment_mdp_data_at(counter, bumped_count, decrement);
1458 }
1459 
1460 // Increment the value at some non-fixed (reg + constant) offset from
1461 // the method data pointer.
1462 
1463 void InterpreterMacroAssembler::increment_mdp_data_at(Register reg,
1464                                                       int constant,
1465                                                       Register bumped_count,
1466                                                       Register scratch2,
1467                                                       bool decrement) {
1468   // Add the constant to reg to get the offset.
1469   add(ImethodDataPtr, reg, scratch2);
1470   Address counter(scratch2, constant);
1471   increment_mdp_data_at(counter, bumped_count, decrement);
1472 }
1473 
1474 // Set a flag value at the current method data pointer position.
1475 // Updates a single byte of the header, to avoid races with other header bits.
1476 
1477 void InterpreterMacroAssembler::set_mdp_flag_at(int flag_constant,
1478                                                 Register scratch) {
1479   assert(ProfileInterpreter, "must be profiling interpreter");
1480   // Load the data header
1481   ldub(ImethodDataPtr, in_bytes(DataLayout::flags_offset()), scratch);
1482 
1483   // Set the flag
1484   or3(scratch, flag_constant, scratch);
1485 
1486   // Store the modified header.
1487   stb(scratch, ImethodDataPtr, in_bytes(DataLayout::flags_offset()));
1488 }
1489 
1490 // Test the location at some offset from the method data pointer.
1491 // If it is not equal to value, branch to the not_equal_continue Label.
1492 // Set condition codes to match the nullness of the loaded value.
1493 
1494 void InterpreterMacroAssembler::test_mdp_data_at(int offset,
1495                                                  Register value,
1496                                                  Label& not_equal_continue,
1497                                                  Register scratch) {
1498   assert(ProfileInterpreter, "must be profiling interpreter");
1499   ld_ptr(ImethodDataPtr, offset, scratch);
1500   cmp(value, scratch);
1501   brx(Assembler::notEqual, false, Assembler::pn, not_equal_continue);
1502   delayed()->tst(scratch);
1503 }
1504 
1505 // Update the method data pointer by the displacement located at some fixed
1506 // offset from the method data pointer.
1507 
1508 void InterpreterMacroAssembler::update_mdp_by_offset(int offset_of_disp,
1509                                                      Register scratch) {
1510   assert(ProfileInterpreter, "must be profiling interpreter");
1511   ld_ptr(ImethodDataPtr, offset_of_disp, scratch);
1512   add(ImethodDataPtr, scratch, ImethodDataPtr);
1513 }
1514 
1515 // Update the method data pointer by the displacement located at the
1516 // offset (reg + offset_of_disp).
1517 
1518 void InterpreterMacroAssembler::update_mdp_by_offset(Register reg,
1519                                                      int offset_of_disp,
1520                                                      Register scratch) {
1521   assert(ProfileInterpreter, "must be profiling interpreter");
1522   add(reg, offset_of_disp, scratch);
1523   ld_ptr(ImethodDataPtr, scratch, scratch);
1524   add(ImethodDataPtr, scratch, ImethodDataPtr);
1525 }
1526 
1527 // Update the method data pointer by a simple constant displacement.
1528 
1529 void InterpreterMacroAssembler::update_mdp_by_constant(int constant) {
1530   assert(ProfileInterpreter, "must be profiling interpreter");
1531   add(ImethodDataPtr, constant, ImethodDataPtr);
1532 }
1533 
1534 // Update the method data pointer for a _ret bytecode whose target
1535 // was not among our cached targets.
1536 
1537 void InterpreterMacroAssembler::update_mdp_for_ret(TosState state,
1538                                                    Register return_bci) {
1539   assert(ProfileInterpreter, "must be profiling interpreter");
1540   push(state);
1541   st_ptr(return_bci, l_tmp);  // protect return_bci, in case it is volatile
1542   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::update_mdp_for_ret), return_bci);
1543   ld_ptr(l_tmp, return_bci);
1544   pop(state);
1545 }
1546 
1547 // Count a taken branch in the bytecodes.
1548 
1549 void InterpreterMacroAssembler::profile_taken_branch(Register scratch, Register bumped_count) {
1550   if (ProfileInterpreter) {
1551     Label profile_continue;
1552 
1553     // If no method data exists, go to profile_continue.
1554     test_method_data_pointer(profile_continue);
1555 
1556     // We are taking a branch.  Increment the taken count.
1557     increment_mdp_data_at(in_bytes(JumpData::taken_offset()), bumped_count);
1558 
1559     // The method data pointer needs to be updated to reflect the new target.
1560     update_mdp_by_offset(in_bytes(JumpData::displacement_offset()), scratch);
1561     bind (profile_continue);
1562   }
1563 }
1564 
1565 
1566 // Count a not-taken branch in the bytecodes.
1567 
1568 void InterpreterMacroAssembler::profile_not_taken_branch(Register scratch) {
1569   if (ProfileInterpreter) {
1570     Label profile_continue;
1571 
1572     // If no method data exists, go to profile_continue.
1573     test_method_data_pointer(profile_continue);
1574 
1575     // We are taking a branch.  Increment the not taken count.
1576     increment_mdp_data_at(in_bytes(BranchData::not_taken_offset()), scratch);
1577 
1578     // The method data pointer needs to be updated to correspond to the
1579     // next bytecode.
1580     update_mdp_by_constant(in_bytes(BranchData::branch_data_size()));
1581     bind (profile_continue);
1582   }
1583 }
1584 
1585 
1586 // Count a non-virtual call in the bytecodes.
1587 
1588 void InterpreterMacroAssembler::profile_call(Register scratch) {
1589   if (ProfileInterpreter) {
1590     Label profile_continue;
1591 
1592     // If no method data exists, go to profile_continue.
1593     test_method_data_pointer(profile_continue);
1594 
1595     // We are making a call.  Increment the count.
1596     increment_mdp_data_at(in_bytes(CounterData::count_offset()), scratch);
1597 
1598     // The method data pointer needs to be updated to reflect the new target.
1599     update_mdp_by_constant(in_bytes(CounterData::counter_data_size()));
1600     bind (profile_continue);
1601   }
1602 }
1603 
1604 
1605 // Count a final call in the bytecodes.
1606 
1607 void InterpreterMacroAssembler::profile_final_call(Register scratch) {
1608   if (ProfileInterpreter) {
1609     Label profile_continue;
1610 
1611     // If no method data exists, go to profile_continue.
1612     test_method_data_pointer(profile_continue);
1613 
1614     // We are making a call.  Increment the count.
1615     increment_mdp_data_at(in_bytes(CounterData::count_offset()), scratch);
1616 
1617     // The method data pointer needs to be updated to reflect the new target.
1618     update_mdp_by_constant(in_bytes(VirtualCallData::virtual_call_data_size()));
1619     bind (profile_continue);
1620   }
1621 }
1622 
1623 
1624 // Count a virtual call in the bytecodes.
1625 
1626 void InterpreterMacroAssembler::profile_virtual_call(Register receiver,
1627                                                      Register scratch,
1628                                                      bool receiver_can_be_null) {
1629   if (ProfileInterpreter) {
1630     Label profile_continue;
1631 
1632     // If no method data exists, go to profile_continue.
1633     test_method_data_pointer(profile_continue);
1634 
1635 
1636     Label skip_receiver_profile;
1637     if (receiver_can_be_null) {
1638       Label not_null;
1639       br_notnull_short(receiver, Assembler::pt, not_null);
1640       // We are making a call.  Increment the count for null receiver.
1641       increment_mdp_data_at(in_bytes(CounterData::count_offset()), scratch);
1642       ba_short(skip_receiver_profile);
1643       bind(not_null);
1644     }
1645 
1646     // Record the receiver type.
1647     record_klass_in_profile(receiver, scratch, true);
1648     bind(skip_receiver_profile);
1649 
1650     // The method data pointer needs to be updated to reflect the new target.
1651 #if INCLUDE_JVMCI
1652     if (MethodProfileWidth == 0) {
1653       update_mdp_by_constant(in_bytes(VirtualCallData::virtual_call_data_size()));
1654     }
1655 #else
1656     update_mdp_by_constant(in_bytes(VirtualCallData::virtual_call_data_size()));
1657 #endif
1658     bind(profile_continue);
1659   }
1660 }
1661 
1662 #if INCLUDE_JVMCI
1663 void InterpreterMacroAssembler::profile_called_method(Register method, Register scratch) {
1664   assert_different_registers(method, scratch);
1665   if (ProfileInterpreter && MethodProfileWidth > 0) {
1666     Label profile_continue;
1667 
1668     // If no method data exists, go to profile_continue.
1669     test_method_data_pointer(profile_continue);
1670 
1671     Label done;
1672     record_item_in_profile_helper(method, scratch, 0, done, MethodProfileWidth,
1673       &VirtualCallData::method_offset, &VirtualCallData::method_count_offset, in_bytes(VirtualCallData::nonprofiled_receiver_count_offset()));
1674     bind(done);
1675 
1676     update_mdp_by_constant(in_bytes(VirtualCallData::virtual_call_data_size()));
1677     bind(profile_continue);
1678   }
1679 }
1680 #endif // INCLUDE_JVMCI
1681 
1682 void InterpreterMacroAssembler::record_klass_in_profile_helper(Register receiver, Register scratch,
1683                                                                Label& done, bool is_virtual_call) {
1684   if (TypeProfileWidth == 0) {
1685     if (is_virtual_call) {
1686       increment_mdp_data_at(in_bytes(CounterData::count_offset()), scratch);
1687     }
1688 #if INCLUDE_JVMCI
1689     else if (EnableJVMCI) {
1690       increment_mdp_data_at(in_bytes(ReceiverTypeData::nonprofiled_receiver_count_offset()), scratch);
1691     }
1692 #endif
1693   } else {
1694     int non_profiled_offset = -1;
1695     if (is_virtual_call) {
1696       non_profiled_offset = in_bytes(CounterData::count_offset());
1697     }
1698 #if INCLUDE_JVMCI
1699     else if (EnableJVMCI) {
1700       non_profiled_offset = in_bytes(ReceiverTypeData::nonprofiled_receiver_count_offset());
1701     }
1702 #endif
1703 
1704     record_item_in_profile_helper(receiver, scratch, 0, done, TypeProfileWidth,
1705       &VirtualCallData::receiver_offset, &VirtualCallData::receiver_count_offset, non_profiled_offset);
1706   }
1707 }
1708 
1709 void InterpreterMacroAssembler::record_item_in_profile_helper(Register item,
1710                                           Register scratch, int start_row, Label& done, int total_rows,
1711                                           OffsetFunction item_offset_fn, OffsetFunction item_count_offset_fn,
1712                                           int non_profiled_offset) {
1713   int last_row = total_rows - 1;
1714   assert(start_row <= last_row, "must be work left to do");
1715   // Test this row for both the item and for null.
1716   // Take any of three different outcomes:
1717   //   1. found item => increment count and goto done
1718   //   2. found null => keep looking for case 1, maybe allocate this cell
1719   //   3. found something else => keep looking for cases 1 and 2
1720   // Case 3 is handled by a recursive call.
1721   for (int row = start_row; row <= last_row; row++) {
1722     Label next_test;
1723     bool test_for_null_also = (row == start_row);
1724 
1725     // See if the item is item[n].
1726     int item_offset = in_bytes(item_offset_fn(row));
1727     test_mdp_data_at(item_offset, item, next_test, scratch);
1728     // delayed()->tst(scratch);
1729 
1730     // The receiver is item[n].  Increment count[n].
1731     int count_offset = in_bytes(item_count_offset_fn(row));
1732     increment_mdp_data_at(count_offset, scratch);
1733     ba_short(done);
1734     bind(next_test);
1735 
1736     if (test_for_null_also) {
1737       Label found_null;
1738       // Failed the equality check on item[n]...  Test for null.
1739       if (start_row == last_row) {
1740         // The only thing left to do is handle the null case.
1741         if (non_profiled_offset >= 0) {
1742           brx(Assembler::zero, false, Assembler::pn, found_null);
1743           delayed()->nop();
1744           // Item did not match any saved item and there is no empty row for it.
1745           // Increment total counter to indicate polymorphic case.
1746           increment_mdp_data_at(non_profiled_offset, scratch);
1747           ba_short(done);
1748           bind(found_null);
1749         } else {
1750           brx(Assembler::notZero, false, Assembler::pt, done);
1751           delayed()->nop();
1752         }
1753         break;
1754       }
1755       // Since null is rare, make it be the branch-taken case.
1756       brx(Assembler::zero, false, Assembler::pn, found_null);
1757       delayed()->nop();
1758 
1759       // Put all the "Case 3" tests here.
1760       record_item_in_profile_helper(item, scratch, start_row + 1, done, total_rows,
1761         item_offset_fn, item_count_offset_fn, non_profiled_offset);
1762 
1763       // Found a null.  Keep searching for a matching item,
1764       // but remember that this is an empty (unused) slot.
1765       bind(found_null);
1766     }
1767   }
1768 
1769   // In the fall-through case, we found no matching item, but we
1770   // observed the item[start_row] is NULL.
1771 
1772   // Fill in the item field and increment the count.
1773   int item_offset = in_bytes(item_offset_fn(start_row));
1774   set_mdp_data_at(item_offset, item);
1775   int count_offset = in_bytes(item_count_offset_fn(start_row));
1776   mov(DataLayout::counter_increment, scratch);
1777   set_mdp_data_at(count_offset, scratch);
1778   if (start_row > 0) {
1779     ba_short(done);
1780   }
1781 }
1782 
1783 void InterpreterMacroAssembler::record_klass_in_profile(Register receiver,
1784                                                         Register scratch, bool is_virtual_call) {
1785   assert(ProfileInterpreter, "must be profiling");
1786   Label done;
1787 
1788   record_klass_in_profile_helper(receiver, scratch, done, is_virtual_call);
1789 
1790   bind (done);
1791 }
1792 
1793 
1794 // Count a ret in the bytecodes.
1795 
1796 void InterpreterMacroAssembler::profile_ret(TosState state,
1797                                             Register return_bci,
1798                                             Register scratch) {
1799   if (ProfileInterpreter) {
1800     Label profile_continue;
1801     uint row;
1802 
1803     // If no method data exists, go to profile_continue.
1804     test_method_data_pointer(profile_continue);
1805 
1806     // Update the total ret count.
1807     increment_mdp_data_at(in_bytes(CounterData::count_offset()), scratch);
1808 
1809     for (row = 0; row < RetData::row_limit(); row++) {
1810       Label next_test;
1811 
1812       // See if return_bci is equal to bci[n]:
1813       test_mdp_data_at(in_bytes(RetData::bci_offset(row)),
1814                        return_bci, next_test, scratch);
1815 
1816       // return_bci is equal to bci[n].  Increment the count.
1817       increment_mdp_data_at(in_bytes(RetData::bci_count_offset(row)), scratch);
1818 
1819       // The method data pointer needs to be updated to reflect the new target.
1820       update_mdp_by_offset(in_bytes(RetData::bci_displacement_offset(row)), scratch);
1821       ba_short(profile_continue);
1822       bind(next_test);
1823     }
1824 
1825     update_mdp_for_ret(state, return_bci);
1826 
1827     bind (profile_continue);
1828   }
1829 }
1830 
1831 // Profile an unexpected null in the bytecodes.
1832 void InterpreterMacroAssembler::profile_null_seen(Register scratch) {
1833   if (ProfileInterpreter) {
1834     Label profile_continue;
1835 
1836     // If no method data exists, go to profile_continue.
1837     test_method_data_pointer(profile_continue);
1838 
1839     set_mdp_flag_at(BitData::null_seen_byte_constant(), scratch);
1840 
1841     // The method data pointer needs to be updated.
1842     int mdp_delta = in_bytes(BitData::bit_data_size());
1843     if (TypeProfileCasts) {
1844       mdp_delta = in_bytes(ReceiverTypeData::receiver_type_data_size());
1845     }
1846     update_mdp_by_constant(mdp_delta);
1847 
1848     bind (profile_continue);
1849   }
1850 }
1851 
1852 void InterpreterMacroAssembler::profile_typecheck(Register klass,
1853                                                   Register scratch) {
1854   if (ProfileInterpreter) {
1855     Label profile_continue;
1856 
1857     // If no method data exists, go to profile_continue.
1858     test_method_data_pointer(profile_continue);
1859 
1860     int mdp_delta = in_bytes(BitData::bit_data_size());
1861     if (TypeProfileCasts) {
1862       mdp_delta = in_bytes(ReceiverTypeData::receiver_type_data_size());
1863 
1864       // Record the object type.
1865       record_klass_in_profile(klass, scratch, false);
1866     }
1867 
1868     // The method data pointer needs to be updated.
1869     update_mdp_by_constant(mdp_delta);
1870 
1871     bind (profile_continue);
1872   }
1873 }
1874 
1875 void InterpreterMacroAssembler::profile_typecheck_failed(Register scratch) {
1876   if (ProfileInterpreter && TypeProfileCasts) {
1877     Label profile_continue;
1878 
1879     // If no method data exists, go to profile_continue.
1880     test_method_data_pointer(profile_continue);
1881 
1882     int count_offset = in_bytes(CounterData::count_offset());
1883     // Back up the address, since we have already bumped the mdp.
1884     count_offset -= in_bytes(ReceiverTypeData::receiver_type_data_size());
1885 
1886     // *Decrement* the counter.  We expect to see zero or small negatives.
1887     increment_mdp_data_at(count_offset, scratch, true);
1888 
1889     bind (profile_continue);
1890   }
1891 }
1892 
1893 // Count the default case of a switch construct.
1894 
1895 void InterpreterMacroAssembler::profile_switch_default(Register scratch) {
1896   if (ProfileInterpreter) {
1897     Label profile_continue;
1898 
1899     // If no method data exists, go to profile_continue.
1900     test_method_data_pointer(profile_continue);
1901 
1902     // Update the default case count
1903     increment_mdp_data_at(in_bytes(MultiBranchData::default_count_offset()),
1904                           scratch);
1905 
1906     // The method data pointer needs to be updated.
1907     update_mdp_by_offset(
1908                     in_bytes(MultiBranchData::default_displacement_offset()),
1909                     scratch);
1910 
1911     bind (profile_continue);
1912   }
1913 }
1914 
1915 // Count the index'th case of a switch construct.
1916 
1917 void InterpreterMacroAssembler::profile_switch_case(Register index,
1918                                                     Register scratch,
1919                                                     Register scratch2,
1920                                                     Register scratch3) {
1921   if (ProfileInterpreter) {
1922     Label profile_continue;
1923 
1924     // If no method data exists, go to profile_continue.
1925     test_method_data_pointer(profile_continue);
1926 
1927     // Build the base (index * per_case_size_in_bytes()) + case_array_offset_in_bytes()
1928     set(in_bytes(MultiBranchData::per_case_size()), scratch);
1929     smul(index, scratch, scratch);
1930     add(scratch, in_bytes(MultiBranchData::case_array_offset()), scratch);
1931 
1932     // Update the case count
1933     increment_mdp_data_at(scratch,
1934                           in_bytes(MultiBranchData::relative_count_offset()),
1935                           scratch2,
1936                           scratch3);
1937 
1938     // The method data pointer needs to be updated.
1939     update_mdp_by_offset(scratch,
1940                      in_bytes(MultiBranchData::relative_displacement_offset()),
1941                      scratch2);
1942 
1943     bind (profile_continue);
1944   }
1945 }
1946 
1947 void InterpreterMacroAssembler::profile_obj_type(Register obj, const Address& mdo_addr, Register tmp) {
1948   Label not_null, do_nothing, do_update;
1949 
1950   assert_different_registers(obj, mdo_addr.base(), tmp);
1951 
1952   verify_oop(obj);
1953 
1954   ld_ptr(mdo_addr, tmp);
1955 
1956   br_notnull_short(obj, pt, not_null);
1957   or3(tmp, TypeEntries::null_seen, tmp);
1958   ba_short(do_update);
1959 
1960   bind(not_null);
1961   load_klass(obj, obj);
1962 
1963   xor3(obj, tmp, obj);
1964   btst(TypeEntries::type_klass_mask, obj);
1965   // klass seen before, nothing to do. The unknown bit may have been
1966   // set already but no need to check.
1967   brx(zero, false, pt, do_nothing);
1968   delayed()->
1969 
1970   btst(TypeEntries::type_unknown, obj);
1971   // already unknown. Nothing to do anymore.
1972   brx(notZero, false, pt, do_nothing);
1973   delayed()->
1974 
1975   btst(TypeEntries::type_mask, tmp);
1976   brx(zero, true, pt, do_update);
1977   // first time here. Set profile type.
1978   delayed()->or3(tmp, obj, tmp);
1979 
1980   // different than before. Cannot keep accurate profile.
1981   or3(tmp, TypeEntries::type_unknown, tmp);
1982 
1983   bind(do_update);
1984   // update profile
1985   st_ptr(tmp, mdo_addr);
1986 
1987   bind(do_nothing);
1988 }
1989 
1990 void InterpreterMacroAssembler::profile_arguments_type(Register callee, Register tmp1, Register tmp2, bool is_virtual) {
1991   if (!ProfileInterpreter) {
1992     return;
1993   }
1994 
1995   assert_different_registers(callee, tmp1, tmp2, ImethodDataPtr);
1996 
1997   if (MethodData::profile_arguments() || MethodData::profile_return()) {
1998     Label profile_continue;
1999 
2000     test_method_data_pointer(profile_continue);
2001 
2002     int off_to_start = is_virtual ? in_bytes(VirtualCallData::virtual_call_data_size()) : in_bytes(CounterData::counter_data_size());
2003 
2004     ldub(ImethodDataPtr, in_bytes(DataLayout::tag_offset()) - off_to_start, tmp1);
2005     cmp_and_br_short(tmp1, is_virtual ? DataLayout::virtual_call_type_data_tag : DataLayout::call_type_data_tag, notEqual, pn, profile_continue);
2006 
2007     if (MethodData::profile_arguments()) {
2008       Label done;
2009       int off_to_args = in_bytes(TypeEntriesAtCall::args_data_offset());
2010       add(ImethodDataPtr, off_to_args, ImethodDataPtr);
2011 
2012       for (int i = 0; i < TypeProfileArgsLimit; i++) {
2013         if (i > 0 || MethodData::profile_return()) {
2014           // If return value type is profiled we may have no argument to profile
2015           ld_ptr(ImethodDataPtr, in_bytes(TypeEntriesAtCall::cell_count_offset())-off_to_args, tmp1);
2016           sub(tmp1, i*TypeStackSlotEntries::per_arg_count(), tmp1);
2017           cmp_and_br_short(tmp1, TypeStackSlotEntries::per_arg_count(), less, pn, done);
2018         }
2019         ld_ptr(Address(callee, Method::const_offset()), tmp1);
2020         lduh(Address(tmp1, ConstMethod::size_of_parameters_offset()), tmp1);
2021         // stack offset o (zero based) from the start of the argument
2022         // list, for n arguments translates into offset n - o - 1 from
2023         // the end of the argument list. But there's an extra slot at
2024         // the stop of the stack. So the offset is n - o from Lesp.
2025         ld_ptr(ImethodDataPtr, in_bytes(TypeEntriesAtCall::stack_slot_offset(i))-off_to_args, tmp2);
2026         sub(tmp1, tmp2, tmp1);
2027 
2028         // Can't use MacroAssembler::argument_address() which needs Gargs to be set up
2029         sll(tmp1, Interpreter::logStackElementSize, tmp1);
2030         ld_ptr(Lesp, tmp1, tmp1);
2031 
2032         Address mdo_arg_addr(ImethodDataPtr, in_bytes(TypeEntriesAtCall::argument_type_offset(i))-off_to_args);
2033         profile_obj_type(tmp1, mdo_arg_addr, tmp2);
2034 
2035         int to_add = in_bytes(TypeStackSlotEntries::per_arg_size());
2036         add(ImethodDataPtr, to_add, ImethodDataPtr);
2037         off_to_args += to_add;
2038       }
2039 
2040       if (MethodData::profile_return()) {
2041         ld_ptr(ImethodDataPtr, in_bytes(TypeEntriesAtCall::cell_count_offset())-off_to_args, tmp1);
2042         sub(tmp1, TypeProfileArgsLimit*TypeStackSlotEntries::per_arg_count(), tmp1);
2043       }
2044 
2045       bind(done);
2046 
2047       if (MethodData::profile_return()) {
2048         // We're right after the type profile for the last
2049         // argument. tmp1 is the number of cells left in the
2050         // CallTypeData/VirtualCallTypeData to reach its end. Non null
2051         // if there's a return to profile.
2052         assert(ReturnTypeEntry::static_cell_count() < TypeStackSlotEntries::per_arg_count(), "can't move past ret type");
2053         sll(tmp1, exact_log2(DataLayout::cell_size), tmp1);
2054         add(ImethodDataPtr, tmp1, ImethodDataPtr);
2055       }
2056     } else {
2057       assert(MethodData::profile_return(), "either profile call args or call ret");
2058       update_mdp_by_constant(in_bytes(TypeEntriesAtCall::return_only_size()));
2059     }
2060 
2061     // mdp points right after the end of the
2062     // CallTypeData/VirtualCallTypeData, right after the cells for the
2063     // return value type if there's one.
2064 
2065     bind(profile_continue);
2066   }
2067 }
2068 
2069 void InterpreterMacroAssembler::profile_return_type(Register ret, Register tmp1, Register tmp2) {
2070   assert_different_registers(ret, tmp1, tmp2);
2071   if (ProfileInterpreter && MethodData::profile_return()) {
2072     Label profile_continue, done;
2073 
2074     test_method_data_pointer(profile_continue);
2075 
2076     if (MethodData::profile_return_jsr292_only()) {
2077       assert(Method::intrinsic_id_size_in_bytes() == 2, "assuming Method::_intrinsic_id is u2");
2078 
2079       // If we don't profile all invoke bytecodes we must make sure
2080       // it's a bytecode we indeed profile. We can't go back to the
2081       // begining of the ProfileData we intend to update to check its
2082       // type because we're right after it and we don't known its
2083       // length.
2084       Label do_profile;
2085       ldub(Lbcp, 0, tmp1);
2086       cmp_and_br_short(tmp1, Bytecodes::_invokedynamic, equal, pn, do_profile);
2087       cmp(tmp1, Bytecodes::_invokehandle);
2088       br(equal, false, pn, do_profile);
2089       delayed()->lduh(Lmethod, Method::intrinsic_id_offset_in_bytes(), tmp1);
2090       cmp_and_br_short(tmp1, vmIntrinsics::_compiledLambdaForm, notEqual, pt, profile_continue);
2091 
2092       bind(do_profile);
2093     }
2094 
2095     Address mdo_ret_addr(ImethodDataPtr, -in_bytes(ReturnTypeEntry::size()));
2096     mov(ret, tmp1);
2097     profile_obj_type(tmp1, mdo_ret_addr, tmp2);
2098 
2099     bind(profile_continue);
2100   }
2101 }
2102 
2103 void InterpreterMacroAssembler::profile_parameters_type(Register tmp1, Register tmp2, Register tmp3, Register tmp4) {
2104   if (ProfileInterpreter && MethodData::profile_parameters()) {
2105     Label profile_continue, done;
2106 
2107     test_method_data_pointer(profile_continue);
2108 
2109     // Load the offset of the area within the MDO used for
2110     // parameters. If it's negative we're not profiling any parameters.
2111     lduw(ImethodDataPtr, in_bytes(MethodData::parameters_type_data_di_offset()) - in_bytes(MethodData::data_offset()), tmp1);
2112     cmp_and_br_short(tmp1, 0, less, pn, profile_continue);
2113 
2114     // Compute a pointer to the area for parameters from the offset
2115     // and move the pointer to the slot for the last
2116     // parameters. Collect profiling from last parameter down.
2117     // mdo start + parameters offset + array length - 1
2118 
2119     // Pointer to the parameter area in the MDO
2120     Register mdp = tmp1;
2121     add(ImethodDataPtr, tmp1, mdp);
2122 
2123     // offset of the current profile entry to update
2124     Register entry_offset = tmp2;
2125     // entry_offset = array len in number of cells
2126     ld_ptr(mdp, ArrayData::array_len_offset(), entry_offset);
2127 
2128     int off_base = in_bytes(ParametersTypeData::stack_slot_offset(0));
2129     assert(off_base % DataLayout::cell_size == 0, "should be a number of cells");
2130 
2131     // entry_offset (number of cells)  = array len - size of 1 entry + offset of the stack slot field
2132     sub(entry_offset, TypeStackSlotEntries::per_arg_count() - (off_base / DataLayout::cell_size), entry_offset);
2133     // entry_offset in bytes
2134     sll(entry_offset, exact_log2(DataLayout::cell_size), entry_offset);
2135 
2136     Label loop;
2137     bind(loop);
2138 
2139     // load offset on the stack from the slot for this parameter
2140     ld_ptr(mdp, entry_offset, tmp3);
2141     sll(tmp3,Interpreter::logStackElementSize, tmp3);
2142     neg(tmp3);
2143     // read the parameter from the local area
2144     ld_ptr(Llocals, tmp3, tmp3);
2145 
2146     // make entry_offset now point to the type field for this parameter
2147     int type_base = in_bytes(ParametersTypeData::type_offset(0));
2148     assert(type_base > off_base, "unexpected");
2149     add(entry_offset, type_base - off_base, entry_offset);
2150 
2151     // profile the parameter
2152     Address arg_type(mdp, entry_offset);
2153     profile_obj_type(tmp3, arg_type, tmp4);
2154 
2155     // go to next parameter
2156     sub(entry_offset, TypeStackSlotEntries::per_arg_count() * DataLayout::cell_size + (type_base - off_base), entry_offset);
2157     cmp_and_br_short(entry_offset, off_base, greaterEqual, pt, loop);
2158 
2159     bind(profile_continue);
2160   }
2161 }
2162 
2163 // add a InterpMonitorElem to stack (see frame_sparc.hpp)
2164 
2165 void InterpreterMacroAssembler::add_monitor_to_stack( bool stack_is_empty,
2166                                                       Register Rtemp,
2167                                                       Register Rtemp2 ) {
2168 
2169   Register Rlimit = Lmonitors;
2170   const jint delta = frame::interpreter_frame_monitor_size() * wordSize;
2171   assert( (delta & LongAlignmentMask) == 0,
2172           "sizeof BasicObjectLock must be even number of doublewords");
2173 
2174   sub( SP,        delta, SP);
2175   sub( Lesp,      delta, Lesp);
2176   sub( Lmonitors, delta, Lmonitors);
2177 
2178   if (!stack_is_empty) {
2179 
2180     // must copy stack contents down
2181 
2182     Label start_copying, next;
2183 
2184     // untested("monitor stack expansion");
2185     compute_stack_base(Rtemp);
2186     ba(start_copying);
2187     delayed()->cmp(Rtemp, Rlimit); // done? duplicated below
2188 
2189     // note: must copy from low memory upwards
2190     // On entry to loop,
2191     // Rtemp points to new base of stack, Lesp points to new end of stack (1 past TOS)
2192     // Loop mutates Rtemp
2193 
2194     bind( next);
2195 
2196     st_ptr(Rtemp2, Rtemp, 0);
2197     inc(Rtemp, wordSize);
2198     cmp(Rtemp, Rlimit); // are we done? (duplicated above)
2199 
2200     bind( start_copying );
2201 
2202     brx( notEqual, true, pn, next );
2203     delayed()->ld_ptr( Rtemp, delta, Rtemp2 );
2204 
2205     // done copying stack
2206   }
2207 }
2208 
2209 // Locals
2210 void InterpreterMacroAssembler::access_local_ptr( Register index, Register dst ) {
2211   assert_not_delayed();
2212   sll(index, Interpreter::logStackElementSize, index);
2213   sub(Llocals, index, index);
2214   ld_ptr(index, 0, dst);
2215   // Note:  index must hold the effective address--the iinc template uses it
2216 }
2217 
2218 // Just like access_local_ptr but the tag is a returnAddress
2219 void InterpreterMacroAssembler::access_local_returnAddress(Register index,
2220                                                            Register dst ) {
2221   assert_not_delayed();
2222   sll(index, Interpreter::logStackElementSize, index);
2223   sub(Llocals, index, index);
2224   ld_ptr(index, 0, dst);
2225 }
2226 
2227 void InterpreterMacroAssembler::access_local_int( Register index, Register dst ) {
2228   assert_not_delayed();
2229   sll(index, Interpreter::logStackElementSize, index);
2230   sub(Llocals, index, index);
2231   ld(index, 0, dst);
2232   // Note:  index must hold the effective address--the iinc template uses it
2233 }
2234 
2235 
2236 void InterpreterMacroAssembler::access_local_long( Register index, Register dst ) {
2237   assert_not_delayed();
2238   sll(index, Interpreter::logStackElementSize, index);
2239   sub(Llocals, index, index);
2240   // First half stored at index n+1 (which grows down from Llocals[n])
2241   load_unaligned_long(index, Interpreter::local_offset_in_bytes(1), dst);
2242 }
2243 
2244 
2245 void InterpreterMacroAssembler::access_local_float( Register index, FloatRegister dst ) {
2246   assert_not_delayed();
2247   sll(index, Interpreter::logStackElementSize, index);
2248   sub(Llocals, index, index);
2249   ldf(FloatRegisterImpl::S, index, 0, dst);
2250 }
2251 
2252 
2253 void InterpreterMacroAssembler::access_local_double( Register index, FloatRegister dst ) {
2254   assert_not_delayed();
2255   sll(index, Interpreter::logStackElementSize, index);
2256   sub(Llocals, index, index);
2257   load_unaligned_double(index, Interpreter::local_offset_in_bytes(1), dst);
2258 }
2259 
2260 
2261 #ifdef ASSERT
2262 void InterpreterMacroAssembler::check_for_regarea_stomp(Register Rindex, int offset, Register Rlimit, Register Rscratch, Register Rscratch1) {
2263   Label L;
2264 
2265   assert(Rindex != Rscratch, "Registers cannot be same");
2266   assert(Rindex != Rscratch1, "Registers cannot be same");
2267   assert(Rlimit != Rscratch, "Registers cannot be same");
2268   assert(Rlimit != Rscratch1, "Registers cannot be same");
2269   assert(Rscratch1 != Rscratch, "Registers cannot be same");
2270 
2271   // untested("reg area corruption");
2272   add(Rindex, offset, Rscratch);
2273   add(Rlimit, 64 + STACK_BIAS, Rscratch1);
2274   cmp_and_brx_short(Rscratch, Rscratch1, Assembler::greaterEqualUnsigned, pn, L);
2275   stop("regsave area is being clobbered");
2276   bind(L);
2277 }
2278 #endif // ASSERT
2279 
2280 
2281 void InterpreterMacroAssembler::store_local_int( Register index, Register src ) {
2282   assert_not_delayed();
2283   sll(index, Interpreter::logStackElementSize, index);
2284   sub(Llocals, index, index);
2285   debug_only(check_for_regarea_stomp(index, 0, FP, G1_scratch, G4_scratch);)
2286   st(src, index, 0);
2287 }
2288 
2289 void InterpreterMacroAssembler::store_local_ptr( Register index, Register src ) {
2290   assert_not_delayed();
2291   sll(index, Interpreter::logStackElementSize, index);
2292   sub(Llocals, index, index);
2293 #ifdef ASSERT
2294   check_for_regarea_stomp(index, 0, FP, G1_scratch, G4_scratch);
2295 #endif
2296   st_ptr(src, index, 0);
2297 }
2298 
2299 
2300 
2301 void InterpreterMacroAssembler::store_local_ptr( int n, Register src ) {
2302   st_ptr(src, Llocals, Interpreter::local_offset_in_bytes(n));
2303 }
2304 
2305 void InterpreterMacroAssembler::store_local_long( Register index, Register src ) {
2306   assert_not_delayed();
2307   sll(index, Interpreter::logStackElementSize, index);
2308   sub(Llocals, index, index);
2309 #ifdef ASSERT
2310   check_for_regarea_stomp(index, Interpreter::local_offset_in_bytes(1), FP, G1_scratch, G4_scratch);
2311 #endif
2312   store_unaligned_long(src, index, Interpreter::local_offset_in_bytes(1)); // which is n+1
2313 }
2314 
2315 
2316 void InterpreterMacroAssembler::store_local_float( Register index, FloatRegister src ) {
2317   assert_not_delayed();
2318   sll(index, Interpreter::logStackElementSize, index);
2319   sub(Llocals, index, index);
2320 #ifdef ASSERT
2321   check_for_regarea_stomp(index, 0, FP, G1_scratch, G4_scratch);
2322 #endif
2323   stf(FloatRegisterImpl::S, src, index, 0);
2324 }
2325 
2326 
2327 void InterpreterMacroAssembler::store_local_double( Register index, FloatRegister src ) {
2328   assert_not_delayed();
2329   sll(index, Interpreter::logStackElementSize, index);
2330   sub(Llocals, index, index);
2331 #ifdef ASSERT
2332   check_for_regarea_stomp(index, Interpreter::local_offset_in_bytes(1), FP, G1_scratch, G4_scratch);
2333 #endif
2334   store_unaligned_double(src, index, Interpreter::local_offset_in_bytes(1));
2335 }
2336 
2337 
2338 int InterpreterMacroAssembler::top_most_monitor_byte_offset() {
2339   const jint delta = frame::interpreter_frame_monitor_size() * wordSize;
2340   int rounded_vm_local_words = ::round_to(frame::interpreter_frame_vm_local_words, WordsPerLong);
2341   return ((-rounded_vm_local_words * wordSize) - delta ) + STACK_BIAS;
2342 }
2343 
2344 
2345 Address InterpreterMacroAssembler::top_most_monitor() {
2346   return Address(FP, top_most_monitor_byte_offset());
2347 }
2348 
2349 
2350 void InterpreterMacroAssembler::compute_stack_base( Register Rdest ) {
2351   add( Lesp,      wordSize,                                    Rdest );
2352 }
2353 
2354 void InterpreterMacroAssembler::get_method_counters(Register method,
2355                                                     Register Rcounters,
2356                                                     Label& skip) {
2357   Label has_counters;
2358   Address method_counters(method, in_bytes(Method::method_counters_offset()));
2359   ld_ptr(method_counters, Rcounters);
2360   br_notnull_short(Rcounters, Assembler::pt, has_counters);
2361   call_VM(noreg, CAST_FROM_FN_PTR(address,
2362           InterpreterRuntime::build_method_counters), method);
2363   ld_ptr(method_counters, Rcounters);
2364   br_null(Rcounters, false, Assembler::pn, skip); // No MethodCounters, OutOfMemory
2365   delayed()->nop();
2366   bind(has_counters);
2367 }
2368 
2369 void InterpreterMacroAssembler::increment_invocation_counter( Register Rcounters, Register Rtmp, Register Rtmp2 ) {
2370   assert(UseCompiler || LogTouchedMethods, "incrementing must be useful");
2371   assert_different_registers(Rcounters, Rtmp, Rtmp2);
2372 
2373   Address inv_counter(Rcounters, MethodCounters::invocation_counter_offset() +
2374                                  InvocationCounter::counter_offset());
2375   Address be_counter (Rcounters, MethodCounters::backedge_counter_offset() +
2376                                  InvocationCounter::counter_offset());
2377   int delta = InvocationCounter::count_increment;
2378 
2379   // Load each counter in a register
2380   ld( inv_counter, Rtmp );
2381   ld( be_counter, Rtmp2 );
2382 
2383   assert( is_simm13( delta ), " delta too large.");
2384 
2385   // Add the delta to the invocation counter and store the result
2386   add( Rtmp, delta, Rtmp );
2387 
2388   // Mask the backedge counter
2389   and3( Rtmp2, InvocationCounter::count_mask_value, Rtmp2 );
2390 
2391   // Store value
2392   st( Rtmp, inv_counter);
2393 
2394   // Add invocation counter + backedge counter
2395   add( Rtmp, Rtmp2, Rtmp);
2396 
2397   // Note that this macro must leave the backedge_count + invocation_count in Rtmp!
2398 }
2399 
2400 void InterpreterMacroAssembler::increment_backedge_counter( Register Rcounters, Register Rtmp, Register Rtmp2 ) {
2401   assert(UseCompiler, "incrementing must be useful");
2402   assert_different_registers(Rcounters, Rtmp, Rtmp2);
2403 
2404   Address be_counter (Rcounters, MethodCounters::backedge_counter_offset() +
2405                                  InvocationCounter::counter_offset());
2406   Address inv_counter(Rcounters, MethodCounters::invocation_counter_offset() +
2407                                  InvocationCounter::counter_offset());
2408 
2409   int delta = InvocationCounter::count_increment;
2410   // Load each counter in a register
2411   ld( be_counter, Rtmp );
2412   ld( inv_counter, Rtmp2 );
2413 
2414   // Add the delta to the backedge counter
2415   add( Rtmp, delta, Rtmp );
2416 
2417   // Mask the invocation counter, add to backedge counter
2418   and3( Rtmp2, InvocationCounter::count_mask_value, Rtmp2 );
2419 
2420   // and store the result to memory
2421   st( Rtmp, be_counter );
2422 
2423   // Add backedge + invocation counter
2424   add( Rtmp, Rtmp2, Rtmp );
2425 
2426   // Note that this macro must leave backedge_count + invocation_count in Rtmp!
2427 }
2428 
2429 void InterpreterMacroAssembler::test_backedge_count_for_osr( Register backedge_count,
2430                                                              Register method_counters,
2431                                                              Register branch_bcp,
2432                                                              Register Rtmp ) {
2433   Label did_not_overflow;
2434   Label overflow_with_error;
2435   assert_different_registers(backedge_count, Rtmp, branch_bcp);
2436   assert(UseOnStackReplacement,"Must UseOnStackReplacement to test_backedge_count_for_osr");
2437 
2438   Address limit(method_counters, in_bytes(MethodCounters::interpreter_backward_branch_limit_offset()));
2439   ld(limit, Rtmp);
2440   cmp_and_br_short(backedge_count, Rtmp, Assembler::lessUnsigned, Assembler::pt, did_not_overflow);
2441 
2442   // When ProfileInterpreter is on, the backedge_count comes from the
2443   // MethodData*, which value does not get reset on the call to
2444   // frequency_counter_overflow().  To avoid excessive calls to the overflow
2445   // routine while the method is being compiled, add a second test to make sure
2446   // the overflow function is called only once every overflow_frequency.
2447   if (ProfileInterpreter) {
2448     const int overflow_frequency = 1024;
2449     andcc(backedge_count, overflow_frequency-1, Rtmp);
2450     brx(Assembler::notZero, false, Assembler::pt, did_not_overflow);
2451     delayed()->nop();
2452   }
2453 
2454   // overflow in loop, pass branch bytecode
2455   set(6,Rtmp);
2456   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::frequency_counter_overflow), branch_bcp, Rtmp);
2457 
2458   // Was an OSR adapter generated?
2459   // O0 = osr nmethod
2460   br_null_short(O0, Assembler::pn, overflow_with_error);
2461 
2462   // Has the nmethod been invalidated already?
2463   ldub(O0, nmethod::state_offset(), O2);
2464   cmp_and_br_short(O2, nmethod::in_use, Assembler::notEqual, Assembler::pn, overflow_with_error);
2465 
2466   // migrate the interpreter frame off of the stack
2467 
2468   mov(G2_thread, L7);
2469   // save nmethod
2470   mov(O0, L6);
2471   set_last_Java_frame(SP, noreg);
2472   call_VM_leaf(noreg, CAST_FROM_FN_PTR(address, SharedRuntime::OSR_migration_begin), L7);
2473   reset_last_Java_frame();
2474   mov(L7, G2_thread);
2475 
2476   // move OSR nmethod to I1
2477   mov(L6, I1);
2478 
2479   // OSR buffer to I0
2480   mov(O0, I0);
2481 
2482   // remove the interpreter frame
2483   restore(I5_savedSP, 0, SP);
2484 
2485   // Jump to the osr code.
2486   ld_ptr(O1, nmethod::osr_entry_point_offset(), O2);
2487   jmp(O2, G0);
2488   delayed()->nop();
2489 
2490   bind(overflow_with_error);
2491 
2492   bind(did_not_overflow);
2493 }
2494 
2495 
2496 
2497 void InterpreterMacroAssembler::interp_verify_oop(Register reg, TosState state, const char * file, int line) {
2498   if (state == atos) { MacroAssembler::_verify_oop(reg, "broken oop ", file, line); }
2499 }
2500 
2501 
2502 // local helper function for the verify_oop_or_return_address macro
2503 static bool verify_return_address(Method* m, int bci) {
2504 #ifndef PRODUCT
2505   address pc = (address)(m->constMethod())
2506              + in_bytes(ConstMethod::codes_offset()) + bci;
2507   // assume it is a valid return address if it is inside m and is preceded by a jsr
2508   if (!m->contains(pc))                                          return false;
2509   address jsr_pc;
2510   jsr_pc = pc - Bytecodes::length_for(Bytecodes::_jsr);
2511   if (*jsr_pc == Bytecodes::_jsr   && jsr_pc >= m->code_base())    return true;
2512   jsr_pc = pc - Bytecodes::length_for(Bytecodes::_jsr_w);
2513   if (*jsr_pc == Bytecodes::_jsr_w && jsr_pc >= m->code_base())    return true;
2514 #endif // PRODUCT
2515   return false;
2516 }
2517 
2518 
2519 void InterpreterMacroAssembler::verify_oop_or_return_address(Register reg, Register Rtmp) {
2520   if (!VerifyOops)  return;
2521   // the VM documentation for the astore[_wide] bytecode allows
2522   // the TOS to be not only an oop but also a return address
2523   Label test;
2524   Label skip;
2525   // See if it is an address (in the current method):
2526 
2527   mov(reg, Rtmp);
2528   const int log2_bytecode_size_limit = 16;
2529   srl(Rtmp, log2_bytecode_size_limit, Rtmp);
2530   br_notnull_short( Rtmp, pt, test );
2531 
2532   // %%% should use call_VM_leaf here?
2533   save_frame_and_mov(0, Lmethod, O0, reg, O1);
2534   save_thread(L7_thread_cache);
2535   call(CAST_FROM_FN_PTR(address,verify_return_address), relocInfo::none);
2536   delayed()->nop();
2537   restore_thread(L7_thread_cache);
2538   br_notnull( O0, false, pt, skip );
2539   delayed()->restore();
2540 
2541   // Perform a more elaborate out-of-line call
2542   // Not an address; verify it:
2543   bind(test);
2544   verify_oop(reg);
2545   bind(skip);
2546 }
2547 
2548 
2549 void InterpreterMacroAssembler::verify_FPU(int stack_depth, TosState state) {
2550   if (state == ftos || state == dtos) MacroAssembler::verify_FPU(stack_depth);
2551 }
2552 
2553 
2554 // Jump if ((*counter_addr += increment) & mask) satisfies the condition.
2555 void InterpreterMacroAssembler::increment_mask_and_jump(Address counter_addr,
2556                                                         int increment, Address mask_addr,
2557                                                         Register scratch1, Register scratch2,
2558                                                         Condition cond, Label *where) {
2559   ld(counter_addr, scratch1);
2560   add(scratch1, increment, scratch1);
2561   ld(mask_addr, scratch2);
2562   andcc(scratch1, scratch2,  G0);
2563   br(cond, false, Assembler::pn, *where);
2564   delayed()->st(scratch1, counter_addr);
2565 }
2566 
2567 // Inline assembly for:
2568 //
2569 // if (thread is in interp_only_mode) {
2570 //   InterpreterRuntime::post_method_entry();
2571 // }
2572 // if (DTraceMethodProbes) {
2573 //   SharedRuntime::dtrace_method_entry(method, receiver);
2574 // }
2575 // if (RC_TRACE_IN_RANGE(0x00001000, 0x00002000)) {
2576 //   SharedRuntime::rc_trace_method_entry(method, receiver);
2577 // }
2578 
2579 void InterpreterMacroAssembler::notify_method_entry() {
2580 
2581   // Whenever JVMTI puts a thread in interp_only_mode, method
2582   // entry/exit events are sent for that thread to track stack
2583   // depth.  If it is possible to enter interp_only_mode we add
2584   // the code to check if the event should be sent.
2585   if (JvmtiExport::can_post_interpreter_events()) {
2586     Label L;
2587     Register temp_reg = O5;
2588     const Address interp_only(G2_thread, JavaThread::interp_only_mode_offset());
2589     ld(interp_only, temp_reg);
2590     cmp_and_br_short(temp_reg, 0, equal, pt, L);
2591     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_method_entry));
2592     bind(L);
2593   }
2594 
2595   {
2596     Register temp_reg = O5;
2597     SkipIfEqual skip_if(this, temp_reg, &DTraceMethodProbes, zero);
2598     call_VM_leaf(noreg,
2599       CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry),
2600       G2_thread, Lmethod);
2601   }
2602 
2603   // RedefineClasses() tracing support for obsolete method entry
2604   if (log_is_enabled(Trace, redefine, class, obsolete)) {
2605     call_VM_leaf(noreg,
2606       CAST_FROM_FN_PTR(address, SharedRuntime::rc_trace_method_entry),
2607       G2_thread, Lmethod);
2608   }
2609 }
2610 
2611 
2612 // Inline assembly for:
2613 //
2614 // if (thread is in interp_only_mode) {
2615 //   // save result
2616 //   InterpreterRuntime::post_method_exit();
2617 //   // restore result
2618 // }
2619 // if (DTraceMethodProbes) {
2620 //   SharedRuntime::dtrace_method_exit(thread, method);
2621 // }
2622 //
2623 // Native methods have their result stored in d_tmp and l_tmp
2624 // Java methods have their result stored in the expression stack
2625 
2626 void InterpreterMacroAssembler::notify_method_exit(bool is_native_method,
2627                                                    TosState state,
2628                                                    NotifyMethodExitMode mode) {
2629 
2630   // Whenever JVMTI puts a thread in interp_only_mode, method
2631   // entry/exit events are sent for that thread to track stack
2632   // depth.  If it is possible to enter interp_only_mode we add
2633   // the code to check if the event should be sent.
2634   if (mode == NotifyJVMTI && JvmtiExport::can_post_interpreter_events()) {
2635     Label L;
2636     Register temp_reg = O5;
2637     const Address interp_only(G2_thread, JavaThread::interp_only_mode_offset());
2638     ld(interp_only, temp_reg);
2639     cmp_and_br_short(temp_reg, 0, equal, pt, L);
2640 
2641     // Note: frame::interpreter_frame_result has a dependency on how the
2642     // method result is saved across the call to post_method_exit. For
2643     // native methods it assumes the result registers are saved to
2644     // l_scratch and d_scratch. If this changes then the interpreter_frame_result
2645     // implementation will need to be updated too.
2646 
2647     save_return_value(state, is_native_method);
2648     call_VM(noreg,
2649             CAST_FROM_FN_PTR(address, InterpreterRuntime::post_method_exit));
2650     restore_return_value(state, is_native_method);
2651     bind(L);
2652   }
2653 
2654   {
2655     Register temp_reg = O5;
2656     // Dtrace notification
2657     SkipIfEqual skip_if(this, temp_reg, &DTraceMethodProbes, zero);
2658     save_return_value(state, is_native_method);
2659     call_VM_leaf(
2660       noreg,
2661       CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit),
2662       G2_thread, Lmethod);
2663     restore_return_value(state, is_native_method);
2664   }
2665 }
2666 
2667 void InterpreterMacroAssembler::save_return_value(TosState state, bool is_native_call) {
2668   if (is_native_call) {
2669     stf(FloatRegisterImpl::D, F0, d_tmp);
2670     stx(O0, l_tmp);
2671   } else {
2672     push(state);
2673   }
2674 }
2675 
2676 void InterpreterMacroAssembler::restore_return_value( TosState state, bool is_native_call) {
2677   if (is_native_call) {
2678     ldf(FloatRegisterImpl::D, d_tmp, F0);
2679     ldx(l_tmp, O0);
2680   } else {
2681     pop(state);
2682   }
2683 }