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::resolved_references_offset_in_bytes(), result);
 759   // JNIHandles::resolve(result)
 760   ld_ptr(result, 0, result);
 761   // Add in the index
 762   add(result, tmp, result);
 763   load_heap_oop(result, arrayOopDesc::base_offset_in_bytes(T_OBJECT), result);
 764 }
 765 
 766 
 767 // Generate a subtype check: branch to ok_is_subtype if sub_klass is
 768 // a subtype of super_klass.  Blows registers Rsuper_klass, Rsub_klass, tmp1, tmp2.
 769 void InterpreterMacroAssembler::gen_subtype_check(Register Rsub_klass,
 770                                                   Register Rsuper_klass,
 771                                                   Register Rtmp1,
 772                                                   Register Rtmp2,
 773                                                   Register Rtmp3,
 774                                                   Label &ok_is_subtype ) {
 775   Label not_subtype;
 776 
 777   // Profile the not-null value's klass.
 778   profile_typecheck(Rsub_klass, Rtmp1);
 779 
 780   check_klass_subtype_fast_path(Rsub_klass, Rsuper_klass,
 781                                 Rtmp1, Rtmp2,
 782                                 &ok_is_subtype, &not_subtype, NULL);
 783 
 784   check_klass_subtype_slow_path(Rsub_klass, Rsuper_klass,
 785                                 Rtmp1, Rtmp2, Rtmp3, /*hack:*/ noreg,
 786                                 &ok_is_subtype, NULL);
 787 
 788   bind(not_subtype);
 789   profile_typecheck_failed(Rtmp1);
 790 }
 791 
 792 // Separate these two to allow for delay slot in middle
 793 // These are used to do a test and full jump to exception-throwing code.
 794 
 795 // %%%%% Could possibly reoptimize this by testing to see if could use
 796 // a single conditional branch (i.e. if span is small enough.
 797 // If you go that route, than get rid of the split and give up
 798 // on the delay-slot hack.
 799 
 800 void InterpreterMacroAssembler::throw_if_not_1_icc( Condition ok_condition,
 801                                                     Label&    ok ) {
 802   assert_not_delayed();
 803   br(ok_condition, true, pt, ok);
 804   // DELAY SLOT
 805 }
 806 
 807 void InterpreterMacroAssembler::throw_if_not_1_xcc( Condition ok_condition,
 808                                                     Label&    ok ) {
 809   assert_not_delayed();
 810   bp( ok_condition, true, Assembler::xcc, pt, ok);
 811   // DELAY SLOT
 812 }
 813 
 814 void InterpreterMacroAssembler::throw_if_not_1_x( Condition ok_condition,
 815                                                   Label&    ok ) {
 816   assert_not_delayed();
 817   brx(ok_condition, true, pt, ok);
 818   // DELAY SLOT
 819 }
 820 
 821 void InterpreterMacroAssembler::throw_if_not_2( address  throw_entry_point,
 822                                                 Register Rscratch,
 823                                                 Label&   ok ) {
 824   assert(throw_entry_point != NULL, "entry point must be generated by now");
 825   AddressLiteral dest(throw_entry_point);
 826   jump_to(dest, Rscratch);
 827   delayed()->nop();
 828   bind(ok);
 829 }
 830 
 831 
 832 // And if you cannot use the delay slot, here is a shorthand:
 833 
 834 void InterpreterMacroAssembler::throw_if_not_icc( Condition ok_condition,
 835                                                   address   throw_entry_point,
 836                                                   Register  Rscratch ) {
 837   Label ok;
 838   if (ok_condition != never) {
 839     throw_if_not_1_icc( ok_condition, ok);
 840     delayed()->nop();
 841   }
 842   throw_if_not_2( throw_entry_point, Rscratch, ok);
 843 }
 844 void InterpreterMacroAssembler::throw_if_not_xcc( Condition ok_condition,
 845                                                   address   throw_entry_point,
 846                                                   Register  Rscratch ) {
 847   Label ok;
 848   if (ok_condition != never) {
 849     throw_if_not_1_xcc( ok_condition, ok);
 850     delayed()->nop();
 851   }
 852   throw_if_not_2( throw_entry_point, Rscratch, ok);
 853 }
 854 void InterpreterMacroAssembler::throw_if_not_x( Condition ok_condition,
 855                                                 address   throw_entry_point,
 856                                                 Register  Rscratch ) {
 857   Label ok;
 858   if (ok_condition != never) {
 859     throw_if_not_1_x( ok_condition, ok);
 860     delayed()->nop();
 861   }
 862   throw_if_not_2( throw_entry_point, Rscratch, ok);
 863 }
 864 
 865 // Check that index is in range for array, then shift index by index_shift, and put arrayOop + shifted_index into res
 866 // Note: res is still shy of address by array offset into object.
 867 
 868 void InterpreterMacroAssembler::index_check_without_pop(Register array, Register index, int index_shift, Register tmp, Register res) {
 869   assert_not_delayed();
 870 
 871   verify_oop(array);
 872   // sign extend since tos (index) can be a 32bit value
 873   sra(index, G0, index);
 874 
 875   // check array
 876   Label ptr_ok;
 877   tst(array);
 878   throw_if_not_1_x( notZero, ptr_ok );
 879   delayed()->ld( array, arrayOopDesc::length_offset_in_bytes(), tmp ); // check index
 880   throw_if_not_2( Interpreter::_throw_NullPointerException_entry, G3_scratch, ptr_ok);
 881 
 882   Label index_ok;
 883   cmp(index, tmp);
 884   throw_if_not_1_icc( lessUnsigned, index_ok );
 885   if (index_shift > 0)  delayed()->sll(index, index_shift, index);
 886   else                  delayed()->add(array, index, res); // addr - const offset in index
 887   // convention: move aberrant index into G3_scratch for exception message
 888   mov(index, G3_scratch);
 889   throw_if_not_2( Interpreter::_throw_ArrayIndexOutOfBoundsException_entry, G4_scratch, index_ok);
 890 
 891   // add offset if didn't do it in delay slot
 892   if (index_shift > 0)   add(array, index, res); // addr - const offset in index
 893 }
 894 
 895 
 896 void InterpreterMacroAssembler::index_check(Register array, Register index, int index_shift, Register tmp, Register res) {
 897   assert_not_delayed();
 898 
 899   // pop array
 900   pop_ptr(array);
 901 
 902   // check array
 903   index_check_without_pop(array, index, index_shift, tmp, res);
 904 }
 905 
 906 
 907 void InterpreterMacroAssembler::get_const(Register Rdst) {
 908   ld_ptr(Lmethod, in_bytes(Method::const_offset()), Rdst);
 909 }
 910 
 911 
 912 void InterpreterMacroAssembler::get_constant_pool(Register Rdst) {
 913   get_const(Rdst);
 914   ld_ptr(Rdst, in_bytes(ConstMethod::constants_offset()), Rdst);
 915 }
 916 
 917 
 918 void InterpreterMacroAssembler::get_constant_pool_cache(Register Rdst) {
 919   get_constant_pool(Rdst);
 920   ld_ptr(Rdst, ConstantPool::cache_offset_in_bytes(), Rdst);
 921 }
 922 
 923 
 924 void InterpreterMacroAssembler::get_cpool_and_tags(Register Rcpool, Register Rtags) {
 925   get_constant_pool(Rcpool);
 926   ld_ptr(Rcpool, ConstantPool::tags_offset_in_bytes(), Rtags);
 927 }
 928 
 929 
 930 // unlock if synchronized method
 931 //
 932 // Unlock the receiver if this is a synchronized method.
 933 // Unlock any Java monitors from syncronized blocks.
 934 //
 935 // If there are locked Java monitors
 936 //    If throw_monitor_exception
 937 //       throws IllegalMonitorStateException
 938 //    Else if install_monitor_exception
 939 //       installs IllegalMonitorStateException
 940 //    Else
 941 //       no error processing
 942 void InterpreterMacroAssembler::unlock_if_synchronized_method(TosState state,
 943                                                               bool throw_monitor_exception,
 944                                                               bool install_monitor_exception) {
 945   Label unlocked, unlock, no_unlock;
 946 
 947   // get the value of _do_not_unlock_if_synchronized into G1_scratch
 948   const Address do_not_unlock_if_synchronized(G2_thread,
 949     JavaThread::do_not_unlock_if_synchronized_offset());
 950   ldbool(do_not_unlock_if_synchronized, G1_scratch);
 951   stbool(G0, do_not_unlock_if_synchronized); // reset the flag
 952 
 953   // check if synchronized method
 954   const Address access_flags(Lmethod, Method::access_flags_offset());
 955   interp_verify_oop(Otos_i, state, __FILE__, __LINE__);
 956   push(state); // save tos
 957   ld(access_flags, G3_scratch); // Load access flags.
 958   btst(JVM_ACC_SYNCHRONIZED, G3_scratch);
 959   br(zero, false, pt, unlocked);
 960   delayed()->nop();
 961 
 962   // Don't unlock anything if the _do_not_unlock_if_synchronized flag
 963   // is set.
 964   cmp_zero_and_br(Assembler::notZero, G1_scratch, no_unlock);
 965   delayed()->nop();
 966 
 967   // BasicObjectLock will be first in list, since this is a synchronized method. However, need
 968   // to check that the object has not been unlocked by an explicit monitorexit bytecode.
 969 
 970   //Intel: if (throw_monitor_exception) ... else ...
 971   // Entry already unlocked, need to throw exception
 972   //...
 973 
 974   // pass top-most monitor elem
 975   add( top_most_monitor(), O1 );
 976 
 977   ld_ptr(O1, BasicObjectLock::obj_offset_in_bytes(), G3_scratch);
 978   br_notnull_short(G3_scratch, pt, unlock);
 979 
 980   if (throw_monitor_exception) {
 981     // Entry already unlocked need to throw an exception
 982     MacroAssembler::call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_illegal_monitor_state_exception));
 983     should_not_reach_here();
 984   } else {
 985     // Monitor already unlocked during a stack unroll.
 986     // If requested, install an illegal_monitor_state_exception.
 987     // Continue with stack unrolling.
 988     if (install_monitor_exception) {
 989       MacroAssembler::call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::new_illegal_monitor_state_exception));
 990     }
 991     ba_short(unlocked);
 992   }
 993 
 994   bind(unlock);
 995 
 996   unlock_object(O1);
 997 
 998   bind(unlocked);
 999 
1000   // I0, I1: Might contain return value
1001 
1002   // Check that all monitors are unlocked
1003   { Label loop, exception, entry, restart;
1004 
1005     Register Rmptr   = O0;
1006     Register Rtemp   = O1;
1007     Register Rlimit  = Lmonitors;
1008     const jint delta = frame::interpreter_frame_monitor_size() * wordSize;
1009     assert( (delta & LongAlignmentMask) == 0,
1010             "sizeof BasicObjectLock must be even number of doublewords");
1011 
1012     #ifdef ASSERT
1013     add(top_most_monitor(), Rmptr, delta);
1014     { Label L;
1015       // ensure that Rmptr starts out above (or at) Rlimit
1016       cmp_and_brx_short(Rmptr, Rlimit, Assembler::greaterEqualUnsigned, pn, L);
1017       stop("monitor stack has negative size");
1018       bind(L);
1019     }
1020     #endif
1021     bind(restart);
1022     ba(entry);
1023     delayed()->
1024     add(top_most_monitor(), Rmptr, delta);      // points to current entry, starting with bottom-most entry
1025 
1026     // Entry is still locked, need to throw exception
1027     bind(exception);
1028     if (throw_monitor_exception) {
1029       MacroAssembler::call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_illegal_monitor_state_exception));
1030       should_not_reach_here();
1031     } else {
1032       // Stack unrolling. Unlock object and if requested, install illegal_monitor_exception.
1033       // Unlock does not block, so don't have to worry about the frame
1034       unlock_object(Rmptr);
1035       if (install_monitor_exception) {
1036         MacroAssembler::call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::new_illegal_monitor_state_exception));
1037       }
1038       ba_short(restart);
1039     }
1040 
1041     bind(loop);
1042     cmp(Rtemp, G0);                             // check if current entry is used
1043     brx(Assembler::notEqual, false, pn, exception);
1044     delayed()->
1045     dec(Rmptr, delta);                          // otherwise advance to next entry
1046     #ifdef ASSERT
1047     { Label L;
1048       // ensure that Rmptr has not somehow stepped below Rlimit
1049       cmp_and_brx_short(Rmptr, Rlimit, Assembler::greaterEqualUnsigned, pn, L);
1050       stop("ran off the end of the monitor stack");
1051       bind(L);
1052     }
1053     #endif
1054     bind(entry);
1055     cmp(Rmptr, Rlimit);                         // check if bottom reached
1056     brx(Assembler::notEqual, true, pn, loop);   // if not at bottom then check this entry
1057     delayed()->
1058     ld_ptr(Rmptr, BasicObjectLock::obj_offset_in_bytes() - delta, Rtemp);
1059   }
1060 
1061   bind(no_unlock);
1062   pop(state);
1063   interp_verify_oop(Otos_i, state, __FILE__, __LINE__);
1064 }
1065 
1066 void InterpreterMacroAssembler::narrow(Register result) {
1067 
1068   ld_ptr(Address(Lmethod, Method::const_offset()), G3_scratch);
1069   ldub(G3_scratch, in_bytes(ConstMethod::result_type_offset()), G3_scratch);
1070 
1071   Label notBool, notByte, notChar, done;
1072 
1073   // common case first
1074   cmp(G3_scratch, T_INT);
1075   br(Assembler::equal, true, pn, done);
1076   delayed()->nop();
1077 
1078   cmp(G3_scratch, T_BOOLEAN);
1079   br(Assembler::notEqual, true, pn, notBool);
1080   delayed()->cmp(G3_scratch, T_BYTE);
1081   and3(result, 1, result);
1082   ba(done);
1083   delayed()->nop();
1084 
1085   bind(notBool);
1086   // cmp(G3_scratch, T_BYTE);
1087   br(Assembler::notEqual, true, pn, notByte);
1088   delayed()->cmp(G3_scratch, T_CHAR);
1089   sll(result, 24, result);
1090   sra(result, 24, result);
1091   ba(done);
1092   delayed()->nop();
1093 
1094   bind(notByte);
1095   // cmp(G3_scratch, T_CHAR);
1096   sll(result, 16, result);
1097   br(Assembler::notEqual, true, pn, done);
1098   delayed()->sra(result, 16, result);
1099   // sll(result, 16, result);
1100   srl(result, 16, result);
1101 
1102   // bind(notChar);
1103   // must be short, instructions already executed in delay slot
1104   // sll(result, 16, result);
1105   // sra(result, 16, result);
1106 
1107   bind(done);
1108 }
1109 
1110 // remove activation
1111 //
1112 // Unlock the receiver if this is a synchronized method.
1113 // Unlock any Java monitors from syncronized blocks.
1114 // Remove the activation from the stack.
1115 //
1116 // If there are locked Java monitors
1117 //    If throw_monitor_exception
1118 //       throws IllegalMonitorStateException
1119 //    Else if install_monitor_exception
1120 //       installs IllegalMonitorStateException
1121 //    Else
1122 //       no error processing
1123 void InterpreterMacroAssembler::remove_activation(TosState state,
1124                                                   bool throw_monitor_exception,
1125                                                   bool install_monitor_exception) {
1126 
1127   unlock_if_synchronized_method(state, throw_monitor_exception, install_monitor_exception);
1128 
1129   // save result (push state before jvmti call and pop it afterwards) and notify jvmti
1130   notify_method_exit(false, state, NotifyJVMTI);
1131 
1132   if (StackReservedPages > 0) {
1133     // testing if Stack Reserved Area needs to be re-enabled
1134     Label no_reserved_zone_enabling;
1135     ld_ptr(G2_thread, JavaThread::reserved_stack_activation_offset(), G3_scratch);
1136     cmp_and_brx_short(SP, G3_scratch, Assembler::lessUnsigned, Assembler::pt, no_reserved_zone_enabling);
1137 
1138     call_VM_leaf(noreg, CAST_FROM_FN_PTR(address, SharedRuntime::enable_stack_reserved_zone), G2_thread);
1139     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_delayed_StackOverflowError), G2_thread);
1140     should_not_reach_here();
1141 
1142     bind(no_reserved_zone_enabling);
1143   }
1144 
1145   interp_verify_oop(Otos_i, state, __FILE__, __LINE__);
1146   verify_thread();
1147 
1148   // return tos
1149   assert(Otos_l1 == Otos_i, "adjust code below");
1150   switch (state) {
1151   case ltos: mov(Otos_l, Otos_l->after_save()); break; // O0 -> I0
1152   case btos:                                      // fall through
1153   case ztos:                                      // fall through
1154   case ctos:
1155   case stos:                                      // fall through
1156   case atos:                                      // fall through
1157   case itos: mov(Otos_l1, Otos_l1->after_save());    break;        // O0 -> I0
1158   case ftos:                                      // fall through
1159   case dtos:                                      // fall through
1160   case vtos: /* nothing to do */                     break;
1161   default  : ShouldNotReachHere();
1162   }
1163 }
1164 
1165 // Lock object
1166 //
1167 // Argument - lock_reg points to the BasicObjectLock to be used for locking,
1168 //            it must be initialized with the object to lock
1169 void InterpreterMacroAssembler::lock_object(Register lock_reg, Register Object) {
1170   if (UseHeavyMonitors) {
1171     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter), lock_reg);
1172   }
1173   else {
1174     Register obj_reg = Object;
1175     Register mark_reg = G4_scratch;
1176     Register temp_reg = G1_scratch;
1177     Address  lock_addr(lock_reg, BasicObjectLock::lock_offset_in_bytes());
1178     Address  mark_addr(obj_reg, oopDesc::mark_offset_in_bytes());
1179     Label    done;
1180 
1181     Label slow_case;
1182 
1183     assert_different_registers(lock_reg, obj_reg, mark_reg, temp_reg);
1184 
1185     // load markOop from object into mark_reg
1186     ld_ptr(mark_addr, mark_reg);
1187 
1188     if (UseBiasedLocking) {
1189       biased_locking_enter(obj_reg, mark_reg, temp_reg, done, &slow_case);
1190     }
1191 
1192     // get the address of basicLock on stack that will be stored in the object
1193     // we need a temporary register here as we do not want to clobber lock_reg
1194     // (cas clobbers the destination register)
1195     mov(lock_reg, temp_reg);
1196     // set mark reg to be (markOop of object | UNLOCK_VALUE)
1197     or3(mark_reg, markOopDesc::unlocked_value, mark_reg);
1198     // initialize the box  (Must happen before we update the object mark!)
1199     st_ptr(mark_reg, lock_addr, BasicLock::displaced_header_offset_in_bytes());
1200     // compare and exchange object_addr, markOop | 1, stack address of basicLock
1201     assert(mark_addr.disp() == 0, "cas must take a zero displacement");
1202     cas_ptr(mark_addr.base(), mark_reg, temp_reg);
1203 
1204     // if the compare and exchange succeeded we are done (we saw an unlocked object)
1205     cmp_and_brx_short(mark_reg, temp_reg, Assembler::equal, Assembler::pt, done);
1206 
1207     // We did not see an unlocked object so try the fast recursive case
1208 
1209     // Check if owner is self by comparing the value in the markOop of object
1210     // with the stack pointer
1211     sub(temp_reg, SP, temp_reg);
1212     sub(temp_reg, STACK_BIAS, temp_reg);
1213     assert(os::vm_page_size() > 0xfff, "page size too small - change the constant");
1214 
1215     // Composite "andcc" test:
1216     // (a) %sp -vs- markword proximity check, and,
1217     // (b) verify mark word LSBs == 0 (Stack-locked).
1218     //
1219     // FFFFF003/FFFFFFFFFFFF003 is (markOopDesc::lock_mask_in_place | -os::vm_page_size())
1220     // Note that the page size used for %sp proximity testing is arbitrary and is
1221     // unrelated to the actual MMU page size.  We use a 'logical' page size of
1222     // 4096 bytes.   F..FFF003 is designed to fit conveniently in the SIMM13 immediate
1223     // field of the andcc instruction.
1224     andcc (temp_reg, 0xFFFFF003, G0) ;
1225 
1226     // if condition is true we are done and hence we can store 0 in the displaced
1227     // header indicating it is a recursive lock and be done
1228     brx(Assembler::zero, true, Assembler::pt, done);
1229     delayed()->st_ptr(G0, lock_addr, BasicLock::displaced_header_offset_in_bytes());
1230 
1231     // none of the above fast optimizations worked so we have to get into the
1232     // slow case of monitor enter
1233     bind(slow_case);
1234     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorenter), lock_reg);
1235 
1236     bind(done);
1237   }
1238 }
1239 
1240 // Unlocks an object. Used in monitorexit bytecode and remove_activation.
1241 //
1242 // Argument - lock_reg points to the BasicObjectLock for lock
1243 // Throw IllegalMonitorException if object is not locked by current thread
1244 void InterpreterMacroAssembler::unlock_object(Register lock_reg) {
1245   if (UseHeavyMonitors) {
1246     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit), lock_reg);
1247   } else {
1248     Register obj_reg = G3_scratch;
1249     Register mark_reg = G4_scratch;
1250     Register displaced_header_reg = G1_scratch;
1251     Address  lockobj_addr(lock_reg, BasicObjectLock::obj_offset_in_bytes());
1252     Address  mark_addr(obj_reg, oopDesc::mark_offset_in_bytes());
1253     Label    done;
1254 
1255     if (UseBiasedLocking) {
1256       // load the object out of the BasicObjectLock
1257       ld_ptr(lockobj_addr, obj_reg);
1258       biased_locking_exit(mark_addr, mark_reg, done, true);
1259       st_ptr(G0, lockobj_addr);  // free entry
1260     }
1261 
1262     // Test first if we are in the fast recursive case
1263     Address lock_addr(lock_reg, BasicObjectLock::lock_offset_in_bytes() + BasicLock::displaced_header_offset_in_bytes());
1264     ld_ptr(lock_addr, displaced_header_reg);
1265     br_null(displaced_header_reg, true, Assembler::pn, done);
1266     delayed()->st_ptr(G0, lockobj_addr);  // free entry
1267 
1268     // See if it is still a light weight lock, if so we just unlock
1269     // the object and we are done
1270 
1271     if (!UseBiasedLocking) {
1272       // load the object out of the BasicObjectLock
1273       ld_ptr(lockobj_addr, obj_reg);
1274     }
1275 
1276     // we have the displaced header in displaced_header_reg
1277     // we expect to see the stack address of the basicLock in case the
1278     // lock is still a light weight lock (lock_reg)
1279     assert(mark_addr.disp() == 0, "cas must take a zero displacement");
1280     cas_ptr(mark_addr.base(), lock_reg, displaced_header_reg);
1281     cmp(lock_reg, displaced_header_reg);
1282     brx(Assembler::equal, true, Assembler::pn, done);
1283     delayed()->st_ptr(G0, lockobj_addr);  // free entry
1284 
1285     // The lock has been converted into a heavy lock and hence
1286     // we need to get into the slow case
1287 
1288     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::monitorexit), lock_reg);
1289 
1290     bind(done);
1291   }
1292 }
1293 
1294 // Get the method data pointer from the Method* and set the
1295 // specified register to its value.
1296 
1297 void InterpreterMacroAssembler::set_method_data_pointer() {
1298   assert(ProfileInterpreter, "must be profiling interpreter");
1299   Label get_continue;
1300 
1301   ld_ptr(Lmethod, in_bytes(Method::method_data_offset()), ImethodDataPtr);
1302   test_method_data_pointer(get_continue);
1303   add(ImethodDataPtr, in_bytes(MethodData::data_offset()), ImethodDataPtr);
1304   bind(get_continue);
1305 }
1306 
1307 // Set the method data pointer for the current bcp.
1308 
1309 void InterpreterMacroAssembler::set_method_data_pointer_for_bcp() {
1310   assert(ProfileInterpreter, "must be profiling interpreter");
1311   Label zero_continue;
1312 
1313   // Test MDO to avoid the call if it is NULL.
1314   ld_ptr(Lmethod, in_bytes(Method::method_data_offset()), ImethodDataPtr);
1315   test_method_data_pointer(zero_continue);
1316   call_VM_leaf(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::bcp_to_di), Lmethod, Lbcp);
1317   add(ImethodDataPtr, in_bytes(MethodData::data_offset()), ImethodDataPtr);
1318   add(ImethodDataPtr, O0, ImethodDataPtr);
1319   bind(zero_continue);
1320 }
1321 
1322 // Test ImethodDataPtr.  If it is null, continue at the specified label
1323 
1324 void InterpreterMacroAssembler::test_method_data_pointer(Label& zero_continue) {
1325   assert(ProfileInterpreter, "must be profiling interpreter");
1326   br_null_short(ImethodDataPtr, Assembler::pn, zero_continue);
1327 }
1328 
1329 void InterpreterMacroAssembler::verify_method_data_pointer() {
1330   assert(ProfileInterpreter, "must be profiling interpreter");
1331 #ifdef ASSERT
1332   Label verify_continue;
1333   test_method_data_pointer(verify_continue);
1334 
1335   // If the mdp is valid, it will point to a DataLayout header which is
1336   // consistent with the bcp.  The converse is highly probable also.
1337   lduh(ImethodDataPtr, in_bytes(DataLayout::bci_offset()), G3_scratch);
1338   ld_ptr(Lmethod, Method::const_offset(), O5);
1339   add(G3_scratch, in_bytes(ConstMethod::codes_offset()), G3_scratch);
1340   add(G3_scratch, O5, G3_scratch);
1341   cmp(Lbcp, G3_scratch);
1342   brx(Assembler::equal, false, Assembler::pt, verify_continue);
1343 
1344   Register temp_reg = O5;
1345   delayed()->mov(ImethodDataPtr, temp_reg);
1346   // %%% should use call_VM_leaf here?
1347   //call_VM_leaf(noreg, ..., Lmethod, Lbcp, ImethodDataPtr);
1348   save_frame_and_mov(sizeof(jdouble) / wordSize, Lmethod, O0, Lbcp, O1);
1349   Address d_save(FP, -sizeof(jdouble) + STACK_BIAS);
1350   stf(FloatRegisterImpl::D, Ftos_d, d_save);
1351   mov(temp_reg->after_save(), O2);
1352   save_thread(L7_thread_cache);
1353   call(CAST_FROM_FN_PTR(address, InterpreterRuntime::verify_mdp), relocInfo::none);
1354   delayed()->nop();
1355   restore_thread(L7_thread_cache);
1356   ldf(FloatRegisterImpl::D, d_save, Ftos_d);
1357   restore();
1358   bind(verify_continue);
1359 #endif // ASSERT
1360 }
1361 
1362 void InterpreterMacroAssembler::test_invocation_counter_for_mdp(Register invocation_count,
1363                                                                 Register method_counters,
1364                                                                 Register Rtmp,
1365                                                                 Label &profile_continue) {
1366   assert(ProfileInterpreter, "must be profiling interpreter");
1367   // Control will flow to "profile_continue" if the counter is less than the
1368   // limit or if we call profile_method()
1369 
1370   Label done;
1371 
1372   // if no method data exists, and the counter is high enough, make one
1373   br_notnull_short(ImethodDataPtr, Assembler::pn, done);
1374 
1375   // Test to see if we should create a method data oop
1376   Address profile_limit(method_counters, MethodCounters::interpreter_profile_limit_offset());
1377   ld(profile_limit, Rtmp);
1378   cmp(invocation_count, Rtmp);
1379   // Use long branches because call_VM() code and following code generated by
1380   // test_backedge_count_for_osr() is large in debug VM.
1381   br(Assembler::lessUnsigned, false, Assembler::pn, profile_continue);
1382   delayed()->nop();
1383 
1384   // Build it now.
1385   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::profile_method));
1386   set_method_data_pointer_for_bcp();
1387   ba(profile_continue);
1388   delayed()->nop();
1389   bind(done);
1390 }
1391 
1392 // Store a value at some constant offset from the method data pointer.
1393 
1394 void InterpreterMacroAssembler::set_mdp_data_at(int constant, Register value) {
1395   assert(ProfileInterpreter, "must be profiling interpreter");
1396   st_ptr(value, ImethodDataPtr, constant);
1397 }
1398 
1399 void InterpreterMacroAssembler::increment_mdp_data_at(Address counter,
1400                                                       Register bumped_count,
1401                                                       bool decrement) {
1402   assert(ProfileInterpreter, "must be profiling interpreter");
1403 
1404   // Load the counter.
1405   ld_ptr(counter, bumped_count);
1406 
1407   if (decrement) {
1408     // Decrement the register.  Set condition codes.
1409     subcc(bumped_count, DataLayout::counter_increment, bumped_count);
1410 
1411     // If the decrement causes the counter to overflow, stay negative
1412     Label L;
1413     brx(Assembler::negative, true, Assembler::pn, L);
1414 
1415     // Store the decremented counter, if it is still negative.
1416     delayed()->st_ptr(bumped_count, counter);
1417     bind(L);
1418   } else {
1419     // Increment the register.  Set carry flag.
1420     addcc(bumped_count, DataLayout::counter_increment, bumped_count);
1421 
1422     // If the increment causes the counter to overflow, pull back by 1.
1423     assert(DataLayout::counter_increment == 1, "subc works");
1424     subc(bumped_count, G0, bumped_count);
1425 
1426     // Store the incremented counter.
1427     st_ptr(bumped_count, counter);
1428   }
1429 }
1430 
1431 // Increment the value at some constant offset from the method data pointer.
1432 
1433 void InterpreterMacroAssembler::increment_mdp_data_at(int constant,
1434                                                       Register bumped_count,
1435                                                       bool decrement) {
1436   // Locate the counter at a fixed offset from the mdp:
1437   Address counter(ImethodDataPtr, constant);
1438   increment_mdp_data_at(counter, bumped_count, decrement);
1439 }
1440 
1441 // Increment the value at some non-fixed (reg + constant) offset from
1442 // the method data pointer.
1443 
1444 void InterpreterMacroAssembler::increment_mdp_data_at(Register reg,
1445                                                       int constant,
1446                                                       Register bumped_count,
1447                                                       Register scratch2,
1448                                                       bool decrement) {
1449   // Add the constant to reg to get the offset.
1450   add(ImethodDataPtr, reg, scratch2);
1451   Address counter(scratch2, constant);
1452   increment_mdp_data_at(counter, bumped_count, decrement);
1453 }
1454 
1455 // Set a flag value at the current method data pointer position.
1456 // Updates a single byte of the header, to avoid races with other header bits.
1457 
1458 void InterpreterMacroAssembler::set_mdp_flag_at(int flag_constant,
1459                                                 Register scratch) {
1460   assert(ProfileInterpreter, "must be profiling interpreter");
1461   // Load the data header
1462   ldub(ImethodDataPtr, in_bytes(DataLayout::flags_offset()), scratch);
1463 
1464   // Set the flag
1465   or3(scratch, flag_constant, scratch);
1466 
1467   // Store the modified header.
1468   stb(scratch, ImethodDataPtr, in_bytes(DataLayout::flags_offset()));
1469 }
1470 
1471 // Test the location at some offset from the method data pointer.
1472 // If it is not equal to value, branch to the not_equal_continue Label.
1473 // Set condition codes to match the nullness of the loaded value.
1474 
1475 void InterpreterMacroAssembler::test_mdp_data_at(int offset,
1476                                                  Register value,
1477                                                  Label& not_equal_continue,
1478                                                  Register scratch) {
1479   assert(ProfileInterpreter, "must be profiling interpreter");
1480   ld_ptr(ImethodDataPtr, offset, scratch);
1481   cmp(value, scratch);
1482   brx(Assembler::notEqual, false, Assembler::pn, not_equal_continue);
1483   delayed()->tst(scratch);
1484 }
1485 
1486 // Update the method data pointer by the displacement located at some fixed
1487 // offset from the method data pointer.
1488 
1489 void InterpreterMacroAssembler::update_mdp_by_offset(int offset_of_disp,
1490                                                      Register scratch) {
1491   assert(ProfileInterpreter, "must be profiling interpreter");
1492   ld_ptr(ImethodDataPtr, offset_of_disp, scratch);
1493   add(ImethodDataPtr, scratch, ImethodDataPtr);
1494 }
1495 
1496 // Update the method data pointer by the displacement located at the
1497 // offset (reg + offset_of_disp).
1498 
1499 void InterpreterMacroAssembler::update_mdp_by_offset(Register reg,
1500                                                      int offset_of_disp,
1501                                                      Register scratch) {
1502   assert(ProfileInterpreter, "must be profiling interpreter");
1503   add(reg, offset_of_disp, scratch);
1504   ld_ptr(ImethodDataPtr, scratch, scratch);
1505   add(ImethodDataPtr, scratch, ImethodDataPtr);
1506 }
1507 
1508 // Update the method data pointer by a simple constant displacement.
1509 
1510 void InterpreterMacroAssembler::update_mdp_by_constant(int constant) {
1511   assert(ProfileInterpreter, "must be profiling interpreter");
1512   add(ImethodDataPtr, constant, ImethodDataPtr);
1513 }
1514 
1515 // Update the method data pointer for a _ret bytecode whose target
1516 // was not among our cached targets.
1517 
1518 void InterpreterMacroAssembler::update_mdp_for_ret(TosState state,
1519                                                    Register return_bci) {
1520   assert(ProfileInterpreter, "must be profiling interpreter");
1521   push(state);
1522   st_ptr(return_bci, l_tmp);  // protect return_bci, in case it is volatile
1523   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::update_mdp_for_ret), return_bci);
1524   ld_ptr(l_tmp, return_bci);
1525   pop(state);
1526 }
1527 
1528 // Count a taken branch in the bytecodes.
1529 
1530 void InterpreterMacroAssembler::profile_taken_branch(Register scratch, Register bumped_count) {
1531   if (ProfileInterpreter) {
1532     Label profile_continue;
1533 
1534     // If no method data exists, go to profile_continue.
1535     test_method_data_pointer(profile_continue);
1536 
1537     // We are taking a branch.  Increment the taken count.
1538     increment_mdp_data_at(in_bytes(JumpData::taken_offset()), bumped_count);
1539 
1540     // The method data pointer needs to be updated to reflect the new target.
1541     update_mdp_by_offset(in_bytes(JumpData::displacement_offset()), scratch);
1542     bind (profile_continue);
1543   }
1544 }
1545 
1546 
1547 // Count a not-taken branch in the bytecodes.
1548 
1549 void InterpreterMacroAssembler::profile_not_taken_branch(Register scratch) {
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 not taken count.
1557     increment_mdp_data_at(in_bytes(BranchData::not_taken_offset()), scratch);
1558 
1559     // The method data pointer needs to be updated to correspond to the
1560     // next bytecode.
1561     update_mdp_by_constant(in_bytes(BranchData::branch_data_size()));
1562     bind (profile_continue);
1563   }
1564 }
1565 
1566 
1567 // Count a non-virtual call in the bytecodes.
1568 
1569 void InterpreterMacroAssembler::profile_call(Register scratch) {
1570   if (ProfileInterpreter) {
1571     Label profile_continue;
1572 
1573     // If no method data exists, go to profile_continue.
1574     test_method_data_pointer(profile_continue);
1575 
1576     // We are making a call.  Increment the count.
1577     increment_mdp_data_at(in_bytes(CounterData::count_offset()), scratch);
1578 
1579     // The method data pointer needs to be updated to reflect the new target.
1580     update_mdp_by_constant(in_bytes(CounterData::counter_data_size()));
1581     bind (profile_continue);
1582   }
1583 }
1584 
1585 
1586 // Count a final call in the bytecodes.
1587 
1588 void InterpreterMacroAssembler::profile_final_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(VirtualCallData::virtual_call_data_size()));
1600     bind (profile_continue);
1601   }
1602 }
1603 
1604 
1605 // Count a virtual call in the bytecodes.
1606 
1607 void InterpreterMacroAssembler::profile_virtual_call(Register receiver,
1608                                                      Register scratch,
1609                                                      bool receiver_can_be_null) {
1610   if (ProfileInterpreter) {
1611     Label profile_continue;
1612 
1613     // If no method data exists, go to profile_continue.
1614     test_method_data_pointer(profile_continue);
1615 
1616 
1617     Label skip_receiver_profile;
1618     if (receiver_can_be_null) {
1619       Label not_null;
1620       br_notnull_short(receiver, Assembler::pt, not_null);
1621       // We are making a call.  Increment the count for null receiver.
1622       increment_mdp_data_at(in_bytes(CounterData::count_offset()), scratch);
1623       ba_short(skip_receiver_profile);
1624       bind(not_null);
1625     }
1626 
1627     // Record the receiver type.
1628     record_klass_in_profile(receiver, scratch, true);
1629     bind(skip_receiver_profile);
1630 
1631     // The method data pointer needs to be updated to reflect the new target.
1632 #if INCLUDE_JVMCI
1633     if (MethodProfileWidth == 0) {
1634       update_mdp_by_constant(in_bytes(VirtualCallData::virtual_call_data_size()));
1635     }
1636 #else
1637     update_mdp_by_constant(in_bytes(VirtualCallData::virtual_call_data_size()));
1638 #endif
1639     bind(profile_continue);
1640   }
1641 }
1642 
1643 #if INCLUDE_JVMCI
1644 void InterpreterMacroAssembler::profile_called_method(Register method, Register scratch) {
1645   assert_different_registers(method, scratch);
1646   if (ProfileInterpreter && MethodProfileWidth > 0) {
1647     Label profile_continue;
1648 
1649     // If no method data exists, go to profile_continue.
1650     test_method_data_pointer(profile_continue);
1651 
1652     Label done;
1653     record_item_in_profile_helper(method, scratch, 0, done, MethodProfileWidth,
1654       &VirtualCallData::method_offset, &VirtualCallData::method_count_offset, in_bytes(VirtualCallData::nonprofiled_receiver_count_offset()));
1655     bind(done);
1656 
1657     update_mdp_by_constant(in_bytes(VirtualCallData::virtual_call_data_size()));
1658     bind(profile_continue);
1659   }
1660 }
1661 #endif // INCLUDE_JVMCI
1662 
1663 void InterpreterMacroAssembler::record_klass_in_profile_helper(Register receiver, Register scratch,
1664                                                                Label& done, bool is_virtual_call) {
1665   if (TypeProfileWidth == 0) {
1666     if (is_virtual_call) {
1667       increment_mdp_data_at(in_bytes(CounterData::count_offset()), scratch);
1668     }
1669 #if INCLUDE_JVMCI
1670     else if (EnableJVMCI) {
1671       increment_mdp_data_at(in_bytes(ReceiverTypeData::nonprofiled_receiver_count_offset()), scratch);
1672     }
1673 #endif
1674   } else {
1675     int non_profiled_offset = -1;
1676     if (is_virtual_call) {
1677       non_profiled_offset = in_bytes(CounterData::count_offset());
1678     }
1679 #if INCLUDE_JVMCI
1680     else if (EnableJVMCI) {
1681       non_profiled_offset = in_bytes(ReceiverTypeData::nonprofiled_receiver_count_offset());
1682     }
1683 #endif
1684 
1685     record_item_in_profile_helper(receiver, scratch, 0, done, TypeProfileWidth,
1686       &VirtualCallData::receiver_offset, &VirtualCallData::receiver_count_offset, non_profiled_offset);
1687   }
1688 }
1689 
1690 void InterpreterMacroAssembler::record_item_in_profile_helper(Register item,
1691                                           Register scratch, int start_row, Label& done, int total_rows,
1692                                           OffsetFunction item_offset_fn, OffsetFunction item_count_offset_fn,
1693                                           int non_profiled_offset) {
1694   int last_row = total_rows - 1;
1695   assert(start_row <= last_row, "must be work left to do");
1696   // Test this row for both the item and for null.
1697   // Take any of three different outcomes:
1698   //   1. found item => increment count and goto done
1699   //   2. found null => keep looking for case 1, maybe allocate this cell
1700   //   3. found something else => keep looking for cases 1 and 2
1701   // Case 3 is handled by a recursive call.
1702   for (int row = start_row; row <= last_row; row++) {
1703     Label next_test;
1704     bool test_for_null_also = (row == start_row);
1705 
1706     // See if the item is item[n].
1707     int item_offset = in_bytes(item_offset_fn(row));
1708     test_mdp_data_at(item_offset, item, next_test, scratch);
1709     // delayed()->tst(scratch);
1710 
1711     // The receiver is item[n].  Increment count[n].
1712     int count_offset = in_bytes(item_count_offset_fn(row));
1713     increment_mdp_data_at(count_offset, scratch);
1714     ba_short(done);
1715     bind(next_test);
1716 
1717     if (test_for_null_also) {
1718       Label found_null;
1719       // Failed the equality check on item[n]...  Test for null.
1720       if (start_row == last_row) {
1721         // The only thing left to do is handle the null case.
1722         if (non_profiled_offset >= 0) {
1723           brx(Assembler::zero, false, Assembler::pn, found_null);
1724           delayed()->nop();
1725           // Item did not match any saved item and there is no empty row for it.
1726           // Increment total counter to indicate polymorphic case.
1727           increment_mdp_data_at(non_profiled_offset, scratch);
1728           ba_short(done);
1729           bind(found_null);
1730         } else {
1731           brx(Assembler::notZero, false, Assembler::pt, done);
1732           delayed()->nop();
1733         }
1734         break;
1735       }
1736       // Since null is rare, make it be the branch-taken case.
1737       brx(Assembler::zero, false, Assembler::pn, found_null);
1738       delayed()->nop();
1739 
1740       // Put all the "Case 3" tests here.
1741       record_item_in_profile_helper(item, scratch, start_row + 1, done, total_rows,
1742         item_offset_fn, item_count_offset_fn, non_profiled_offset);
1743 
1744       // Found a null.  Keep searching for a matching item,
1745       // but remember that this is an empty (unused) slot.
1746       bind(found_null);
1747     }
1748   }
1749 
1750   // In the fall-through case, we found no matching item, but we
1751   // observed the item[start_row] is NULL.
1752 
1753   // Fill in the item field and increment the count.
1754   int item_offset = in_bytes(item_offset_fn(start_row));
1755   set_mdp_data_at(item_offset, item);
1756   int count_offset = in_bytes(item_count_offset_fn(start_row));
1757   mov(DataLayout::counter_increment, scratch);
1758   set_mdp_data_at(count_offset, scratch);
1759   if (start_row > 0) {
1760     ba_short(done);
1761   }
1762 }
1763 
1764 void InterpreterMacroAssembler::record_klass_in_profile(Register receiver,
1765                                                         Register scratch, bool is_virtual_call) {
1766   assert(ProfileInterpreter, "must be profiling");
1767   Label done;
1768 
1769   record_klass_in_profile_helper(receiver, scratch, done, is_virtual_call);
1770 
1771   bind (done);
1772 }
1773 
1774 
1775 // Count a ret in the bytecodes.
1776 
1777 void InterpreterMacroAssembler::profile_ret(TosState state,
1778                                             Register return_bci,
1779                                             Register scratch) {
1780   if (ProfileInterpreter) {
1781     Label profile_continue;
1782     uint row;
1783 
1784     // If no method data exists, go to profile_continue.
1785     test_method_data_pointer(profile_continue);
1786 
1787     // Update the total ret count.
1788     increment_mdp_data_at(in_bytes(CounterData::count_offset()), scratch);
1789 
1790     for (row = 0; row < RetData::row_limit(); row++) {
1791       Label next_test;
1792 
1793       // See if return_bci is equal to bci[n]:
1794       test_mdp_data_at(in_bytes(RetData::bci_offset(row)),
1795                        return_bci, next_test, scratch);
1796 
1797       // return_bci is equal to bci[n].  Increment the count.
1798       increment_mdp_data_at(in_bytes(RetData::bci_count_offset(row)), scratch);
1799 
1800       // The method data pointer needs to be updated to reflect the new target.
1801       update_mdp_by_offset(in_bytes(RetData::bci_displacement_offset(row)), scratch);
1802       ba_short(profile_continue);
1803       bind(next_test);
1804     }
1805 
1806     update_mdp_for_ret(state, return_bci);
1807 
1808     bind (profile_continue);
1809   }
1810 }
1811 
1812 // Profile an unexpected null in the bytecodes.
1813 void InterpreterMacroAssembler::profile_null_seen(Register scratch) {
1814   if (ProfileInterpreter) {
1815     Label profile_continue;
1816 
1817     // If no method data exists, go to profile_continue.
1818     test_method_data_pointer(profile_continue);
1819 
1820     set_mdp_flag_at(BitData::null_seen_byte_constant(), scratch);
1821 
1822     // The method data pointer needs to be updated.
1823     int mdp_delta = in_bytes(BitData::bit_data_size());
1824     if (TypeProfileCasts) {
1825       mdp_delta = in_bytes(ReceiverTypeData::receiver_type_data_size());
1826     }
1827     update_mdp_by_constant(mdp_delta);
1828 
1829     bind (profile_continue);
1830   }
1831 }
1832 
1833 void InterpreterMacroAssembler::profile_typecheck(Register klass,
1834                                                   Register scratch) {
1835   if (ProfileInterpreter) {
1836     Label profile_continue;
1837 
1838     // If no method data exists, go to profile_continue.
1839     test_method_data_pointer(profile_continue);
1840 
1841     int mdp_delta = in_bytes(BitData::bit_data_size());
1842     if (TypeProfileCasts) {
1843       mdp_delta = in_bytes(ReceiverTypeData::receiver_type_data_size());
1844 
1845       // Record the object type.
1846       record_klass_in_profile(klass, scratch, false);
1847     }
1848 
1849     // The method data pointer needs to be updated.
1850     update_mdp_by_constant(mdp_delta);
1851 
1852     bind (profile_continue);
1853   }
1854 }
1855 
1856 void InterpreterMacroAssembler::profile_typecheck_failed(Register scratch) {
1857   if (ProfileInterpreter && TypeProfileCasts) {
1858     Label profile_continue;
1859 
1860     // If no method data exists, go to profile_continue.
1861     test_method_data_pointer(profile_continue);
1862 
1863     int count_offset = in_bytes(CounterData::count_offset());
1864     // Back up the address, since we have already bumped the mdp.
1865     count_offset -= in_bytes(ReceiverTypeData::receiver_type_data_size());
1866 
1867     // *Decrement* the counter.  We expect to see zero or small negatives.
1868     increment_mdp_data_at(count_offset, scratch, true);
1869 
1870     bind (profile_continue);
1871   }
1872 }
1873 
1874 // Count the default case of a switch construct.
1875 
1876 void InterpreterMacroAssembler::profile_switch_default(Register scratch) {
1877   if (ProfileInterpreter) {
1878     Label profile_continue;
1879 
1880     // If no method data exists, go to profile_continue.
1881     test_method_data_pointer(profile_continue);
1882 
1883     // Update the default case count
1884     increment_mdp_data_at(in_bytes(MultiBranchData::default_count_offset()),
1885                           scratch);
1886 
1887     // The method data pointer needs to be updated.
1888     update_mdp_by_offset(
1889                     in_bytes(MultiBranchData::default_displacement_offset()),
1890                     scratch);
1891 
1892     bind (profile_continue);
1893   }
1894 }
1895 
1896 // Count the index'th case of a switch construct.
1897 
1898 void InterpreterMacroAssembler::profile_switch_case(Register index,
1899                                                     Register scratch,
1900                                                     Register scratch2,
1901                                                     Register scratch3) {
1902   if (ProfileInterpreter) {
1903     Label profile_continue;
1904 
1905     // If no method data exists, go to profile_continue.
1906     test_method_data_pointer(profile_continue);
1907 
1908     // Build the base (index * per_case_size_in_bytes()) + case_array_offset_in_bytes()
1909     set(in_bytes(MultiBranchData::per_case_size()), scratch);
1910     smul(index, scratch, scratch);
1911     add(scratch, in_bytes(MultiBranchData::case_array_offset()), scratch);
1912 
1913     // Update the case count
1914     increment_mdp_data_at(scratch,
1915                           in_bytes(MultiBranchData::relative_count_offset()),
1916                           scratch2,
1917                           scratch3);
1918 
1919     // The method data pointer needs to be updated.
1920     update_mdp_by_offset(scratch,
1921                      in_bytes(MultiBranchData::relative_displacement_offset()),
1922                      scratch2);
1923 
1924     bind (profile_continue);
1925   }
1926 }
1927 
1928 void InterpreterMacroAssembler::profile_obj_type(Register obj, const Address& mdo_addr, Register tmp) {
1929   Label not_null, do_nothing, do_update;
1930 
1931   assert_different_registers(obj, mdo_addr.base(), tmp);
1932 
1933   verify_oop(obj);
1934 
1935   ld_ptr(mdo_addr, tmp);
1936 
1937   br_notnull_short(obj, pt, not_null);
1938   or3(tmp, TypeEntries::null_seen, tmp);
1939   ba_short(do_update);
1940 
1941   bind(not_null);
1942   load_klass(obj, obj);
1943 
1944   xor3(obj, tmp, obj);
1945   btst(TypeEntries::type_klass_mask, obj);
1946   // klass seen before, nothing to do. The unknown bit may have been
1947   // set already but no need to check.
1948   brx(zero, false, pt, do_nothing);
1949   delayed()->
1950 
1951   btst(TypeEntries::type_unknown, obj);
1952   // already unknown. Nothing to do anymore.
1953   brx(notZero, false, pt, do_nothing);
1954   delayed()->
1955 
1956   btst(TypeEntries::type_mask, tmp);
1957   brx(zero, true, pt, do_update);
1958   // first time here. Set profile type.
1959   delayed()->or3(tmp, obj, tmp);
1960 
1961   // different than before. Cannot keep accurate profile.
1962   or3(tmp, TypeEntries::type_unknown, tmp);
1963 
1964   bind(do_update);
1965   // update profile
1966   st_ptr(tmp, mdo_addr);
1967 
1968   bind(do_nothing);
1969 }
1970 
1971 void InterpreterMacroAssembler::profile_arguments_type(Register callee, Register tmp1, Register tmp2, bool is_virtual) {
1972   if (!ProfileInterpreter) {
1973     return;
1974   }
1975 
1976   assert_different_registers(callee, tmp1, tmp2, ImethodDataPtr);
1977 
1978   if (MethodData::profile_arguments() || MethodData::profile_return()) {
1979     Label profile_continue;
1980 
1981     test_method_data_pointer(profile_continue);
1982 
1983     int off_to_start = is_virtual ? in_bytes(VirtualCallData::virtual_call_data_size()) : in_bytes(CounterData::counter_data_size());
1984 
1985     ldub(ImethodDataPtr, in_bytes(DataLayout::tag_offset()) - off_to_start, tmp1);
1986     cmp_and_br_short(tmp1, is_virtual ? DataLayout::virtual_call_type_data_tag : DataLayout::call_type_data_tag, notEqual, pn, profile_continue);
1987 
1988     if (MethodData::profile_arguments()) {
1989       Label done;
1990       int off_to_args = in_bytes(TypeEntriesAtCall::args_data_offset());
1991       add(ImethodDataPtr, off_to_args, ImethodDataPtr);
1992 
1993       for (int i = 0; i < TypeProfileArgsLimit; i++) {
1994         if (i > 0 || MethodData::profile_return()) {
1995           // If return value type is profiled we may have no argument to profile
1996           ld_ptr(ImethodDataPtr, in_bytes(TypeEntriesAtCall::cell_count_offset())-off_to_args, tmp1);
1997           sub(tmp1, i*TypeStackSlotEntries::per_arg_count(), tmp1);
1998           cmp_and_br_short(tmp1, TypeStackSlotEntries::per_arg_count(), less, pn, done);
1999         }
2000         ld_ptr(Address(callee, Method::const_offset()), tmp1);
2001         lduh(Address(tmp1, ConstMethod::size_of_parameters_offset()), tmp1);
2002         // stack offset o (zero based) from the start of the argument
2003         // list, for n arguments translates into offset n - o - 1 from
2004         // the end of the argument list. But there's an extra slot at
2005         // the stop of the stack. So the offset is n - o from Lesp.
2006         ld_ptr(ImethodDataPtr, in_bytes(TypeEntriesAtCall::stack_slot_offset(i))-off_to_args, tmp2);
2007         sub(tmp1, tmp2, tmp1);
2008 
2009         // Can't use MacroAssembler::argument_address() which needs Gargs to be set up
2010         sll(tmp1, Interpreter::logStackElementSize, tmp1);
2011         ld_ptr(Lesp, tmp1, tmp1);
2012 
2013         Address mdo_arg_addr(ImethodDataPtr, in_bytes(TypeEntriesAtCall::argument_type_offset(i))-off_to_args);
2014         profile_obj_type(tmp1, mdo_arg_addr, tmp2);
2015 
2016         int to_add = in_bytes(TypeStackSlotEntries::per_arg_size());
2017         add(ImethodDataPtr, to_add, ImethodDataPtr);
2018         off_to_args += to_add;
2019       }
2020 
2021       if (MethodData::profile_return()) {
2022         ld_ptr(ImethodDataPtr, in_bytes(TypeEntriesAtCall::cell_count_offset())-off_to_args, tmp1);
2023         sub(tmp1, TypeProfileArgsLimit*TypeStackSlotEntries::per_arg_count(), tmp1);
2024       }
2025 
2026       bind(done);
2027 
2028       if (MethodData::profile_return()) {
2029         // We're right after the type profile for the last
2030         // argument. tmp1 is the number of cells left in the
2031         // CallTypeData/VirtualCallTypeData to reach its end. Non null
2032         // if there's a return to profile.
2033         assert(ReturnTypeEntry::static_cell_count() < TypeStackSlotEntries::per_arg_count(), "can't move past ret type");
2034         sll(tmp1, exact_log2(DataLayout::cell_size), tmp1);
2035         add(ImethodDataPtr, tmp1, ImethodDataPtr);
2036       }
2037     } else {
2038       assert(MethodData::profile_return(), "either profile call args or call ret");
2039       update_mdp_by_constant(in_bytes(TypeEntriesAtCall::return_only_size()));
2040     }
2041 
2042     // mdp points right after the end of the
2043     // CallTypeData/VirtualCallTypeData, right after the cells for the
2044     // return value type if there's one.
2045 
2046     bind(profile_continue);
2047   }
2048 }
2049 
2050 void InterpreterMacroAssembler::profile_return_type(Register ret, Register tmp1, Register tmp2) {
2051   assert_different_registers(ret, tmp1, tmp2);
2052   if (ProfileInterpreter && MethodData::profile_return()) {
2053     Label profile_continue, done;
2054 
2055     test_method_data_pointer(profile_continue);
2056 
2057     if (MethodData::profile_return_jsr292_only()) {
2058       assert(Method::intrinsic_id_size_in_bytes() == 2, "assuming Method::_intrinsic_id is u2");
2059 
2060       // If we don't profile all invoke bytecodes we must make sure
2061       // it's a bytecode we indeed profile. We can't go back to the
2062       // begining of the ProfileData we intend to update to check its
2063       // type because we're right after it and we don't known its
2064       // length.
2065       Label do_profile;
2066       ldub(Lbcp, 0, tmp1);
2067       cmp_and_br_short(tmp1, Bytecodes::_invokedynamic, equal, pn, do_profile);
2068       cmp(tmp1, Bytecodes::_invokehandle);
2069       br(equal, false, pn, do_profile);
2070       delayed()->lduh(Lmethod, Method::intrinsic_id_offset_in_bytes(), tmp1);
2071       cmp_and_br_short(tmp1, vmIntrinsics::_compiledLambdaForm, notEqual, pt, profile_continue);
2072 
2073       bind(do_profile);
2074     }
2075 
2076     Address mdo_ret_addr(ImethodDataPtr, -in_bytes(ReturnTypeEntry::size()));
2077     mov(ret, tmp1);
2078     profile_obj_type(tmp1, mdo_ret_addr, tmp2);
2079 
2080     bind(profile_continue);
2081   }
2082 }
2083 
2084 void InterpreterMacroAssembler::profile_parameters_type(Register tmp1, Register tmp2, Register tmp3, Register tmp4) {
2085   if (ProfileInterpreter && MethodData::profile_parameters()) {
2086     Label profile_continue, done;
2087 
2088     test_method_data_pointer(profile_continue);
2089 
2090     // Load the offset of the area within the MDO used for
2091     // parameters. If it's negative we're not profiling any parameters.
2092     lduw(ImethodDataPtr, in_bytes(MethodData::parameters_type_data_di_offset()) - in_bytes(MethodData::data_offset()), tmp1);
2093     cmp_and_br_short(tmp1, 0, less, pn, profile_continue);
2094 
2095     // Compute a pointer to the area for parameters from the offset
2096     // and move the pointer to the slot for the last
2097     // parameters. Collect profiling from last parameter down.
2098     // mdo start + parameters offset + array length - 1
2099 
2100     // Pointer to the parameter area in the MDO
2101     Register mdp = tmp1;
2102     add(ImethodDataPtr, tmp1, mdp);
2103 
2104     // offset of the current profile entry to update
2105     Register entry_offset = tmp2;
2106     // entry_offset = array len in number of cells
2107     ld_ptr(mdp, ArrayData::array_len_offset(), entry_offset);
2108 
2109     int off_base = in_bytes(ParametersTypeData::stack_slot_offset(0));
2110     assert(off_base % DataLayout::cell_size == 0, "should be a number of cells");
2111 
2112     // entry_offset (number of cells)  = array len - size of 1 entry + offset of the stack slot field
2113     sub(entry_offset, TypeStackSlotEntries::per_arg_count() - (off_base / DataLayout::cell_size), entry_offset);
2114     // entry_offset in bytes
2115     sll(entry_offset, exact_log2(DataLayout::cell_size), entry_offset);
2116 
2117     Label loop;
2118     bind(loop);
2119 
2120     // load offset on the stack from the slot for this parameter
2121     ld_ptr(mdp, entry_offset, tmp3);
2122     sll(tmp3,Interpreter::logStackElementSize, tmp3);
2123     neg(tmp3);
2124     // read the parameter from the local area
2125     ld_ptr(Llocals, tmp3, tmp3);
2126 
2127     // make entry_offset now point to the type field for this parameter
2128     int type_base = in_bytes(ParametersTypeData::type_offset(0));
2129     assert(type_base > off_base, "unexpected");
2130     add(entry_offset, type_base - off_base, entry_offset);
2131 
2132     // profile the parameter
2133     Address arg_type(mdp, entry_offset);
2134     profile_obj_type(tmp3, arg_type, tmp4);
2135 
2136     // go to next parameter
2137     sub(entry_offset, TypeStackSlotEntries::per_arg_count() * DataLayout::cell_size + (type_base - off_base), entry_offset);
2138     cmp_and_br_short(entry_offset, off_base, greaterEqual, pt, loop);
2139 
2140     bind(profile_continue);
2141   }
2142 }
2143 
2144 // add a InterpMonitorElem to stack (see frame_sparc.hpp)
2145 
2146 void InterpreterMacroAssembler::add_monitor_to_stack( bool stack_is_empty,
2147                                                       Register Rtemp,
2148                                                       Register Rtemp2 ) {
2149 
2150   Register Rlimit = Lmonitors;
2151   const jint delta = frame::interpreter_frame_monitor_size() * wordSize;
2152   assert( (delta & LongAlignmentMask) == 0,
2153           "sizeof BasicObjectLock must be even number of doublewords");
2154 
2155   sub( SP,        delta, SP);
2156   sub( Lesp,      delta, Lesp);
2157   sub( Lmonitors, delta, Lmonitors);
2158 
2159   if (!stack_is_empty) {
2160 
2161     // must copy stack contents down
2162 
2163     Label start_copying, next;
2164 
2165     // untested("monitor stack expansion");
2166     compute_stack_base(Rtemp);
2167     ba(start_copying);
2168     delayed()->cmp(Rtemp, Rlimit); // done? duplicated below
2169 
2170     // note: must copy from low memory upwards
2171     // On entry to loop,
2172     // Rtemp points to new base of stack, Lesp points to new end of stack (1 past TOS)
2173     // Loop mutates Rtemp
2174 
2175     bind( next);
2176 
2177     st_ptr(Rtemp2, Rtemp, 0);
2178     inc(Rtemp, wordSize);
2179     cmp(Rtemp, Rlimit); // are we done? (duplicated above)
2180 
2181     bind( start_copying );
2182 
2183     brx( notEqual, true, pn, next );
2184     delayed()->ld_ptr( Rtemp, delta, Rtemp2 );
2185 
2186     // done copying stack
2187   }
2188 }
2189 
2190 // Locals
2191 void InterpreterMacroAssembler::access_local_ptr( Register index, Register dst ) {
2192   assert_not_delayed();
2193   sll(index, Interpreter::logStackElementSize, index);
2194   sub(Llocals, index, index);
2195   ld_ptr(index, 0, dst);
2196   // Note:  index must hold the effective address--the iinc template uses it
2197 }
2198 
2199 // Just like access_local_ptr but the tag is a returnAddress
2200 void InterpreterMacroAssembler::access_local_returnAddress(Register index,
2201                                                            Register dst ) {
2202   assert_not_delayed();
2203   sll(index, Interpreter::logStackElementSize, index);
2204   sub(Llocals, index, index);
2205   ld_ptr(index, 0, dst);
2206 }
2207 
2208 void InterpreterMacroAssembler::access_local_int( Register index, Register dst ) {
2209   assert_not_delayed();
2210   sll(index, Interpreter::logStackElementSize, index);
2211   sub(Llocals, index, index);
2212   ld(index, 0, dst);
2213   // Note:  index must hold the effective address--the iinc template uses it
2214 }
2215 
2216 
2217 void InterpreterMacroAssembler::access_local_long( Register index, Register dst ) {
2218   assert_not_delayed();
2219   sll(index, Interpreter::logStackElementSize, index);
2220   sub(Llocals, index, index);
2221   // First half stored at index n+1 (which grows down from Llocals[n])
2222   load_unaligned_long(index, Interpreter::local_offset_in_bytes(1), dst);
2223 }
2224 
2225 
2226 void InterpreterMacroAssembler::access_local_float( Register index, FloatRegister dst ) {
2227   assert_not_delayed();
2228   sll(index, Interpreter::logStackElementSize, index);
2229   sub(Llocals, index, index);
2230   ldf(FloatRegisterImpl::S, index, 0, dst);
2231 }
2232 
2233 
2234 void InterpreterMacroAssembler::access_local_double( Register index, FloatRegister dst ) {
2235   assert_not_delayed();
2236   sll(index, Interpreter::logStackElementSize, index);
2237   sub(Llocals, index, index);
2238   load_unaligned_double(index, Interpreter::local_offset_in_bytes(1), dst);
2239 }
2240 
2241 
2242 #ifdef ASSERT
2243 void InterpreterMacroAssembler::check_for_regarea_stomp(Register Rindex, int offset, Register Rlimit, Register Rscratch, Register Rscratch1) {
2244   Label L;
2245 
2246   assert(Rindex != Rscratch, "Registers cannot be same");
2247   assert(Rindex != Rscratch1, "Registers cannot be same");
2248   assert(Rlimit != Rscratch, "Registers cannot be same");
2249   assert(Rlimit != Rscratch1, "Registers cannot be same");
2250   assert(Rscratch1 != Rscratch, "Registers cannot be same");
2251 
2252   // untested("reg area corruption");
2253   add(Rindex, offset, Rscratch);
2254   add(Rlimit, 64 + STACK_BIAS, Rscratch1);
2255   cmp_and_brx_short(Rscratch, Rscratch1, Assembler::greaterEqualUnsigned, pn, L);
2256   stop("regsave area is being clobbered");
2257   bind(L);
2258 }
2259 #endif // ASSERT
2260 
2261 
2262 void InterpreterMacroAssembler::store_local_int( Register index, Register src ) {
2263   assert_not_delayed();
2264   sll(index, Interpreter::logStackElementSize, index);
2265   sub(Llocals, index, index);
2266   debug_only(check_for_regarea_stomp(index, 0, FP, G1_scratch, G4_scratch);)
2267   st(src, index, 0);
2268 }
2269 
2270 void InterpreterMacroAssembler::store_local_ptr( Register index, Register src ) {
2271   assert_not_delayed();
2272   sll(index, Interpreter::logStackElementSize, index);
2273   sub(Llocals, index, index);
2274 #ifdef ASSERT
2275   check_for_regarea_stomp(index, 0, FP, G1_scratch, G4_scratch);
2276 #endif
2277   st_ptr(src, index, 0);
2278 }
2279 
2280 
2281 
2282 void InterpreterMacroAssembler::store_local_ptr( int n, Register src ) {
2283   st_ptr(src, Llocals, Interpreter::local_offset_in_bytes(n));
2284 }
2285 
2286 void InterpreterMacroAssembler::store_local_long( Register index, Register src ) {
2287   assert_not_delayed();
2288   sll(index, Interpreter::logStackElementSize, index);
2289   sub(Llocals, index, index);
2290 #ifdef ASSERT
2291   check_for_regarea_stomp(index, Interpreter::local_offset_in_bytes(1), FP, G1_scratch, G4_scratch);
2292 #endif
2293   store_unaligned_long(src, index, Interpreter::local_offset_in_bytes(1)); // which is n+1
2294 }
2295 
2296 
2297 void InterpreterMacroAssembler::store_local_float( Register index, FloatRegister src ) {
2298   assert_not_delayed();
2299   sll(index, Interpreter::logStackElementSize, index);
2300   sub(Llocals, index, index);
2301 #ifdef ASSERT
2302   check_for_regarea_stomp(index, 0, FP, G1_scratch, G4_scratch);
2303 #endif
2304   stf(FloatRegisterImpl::S, src, index, 0);
2305 }
2306 
2307 
2308 void InterpreterMacroAssembler::store_local_double( Register index, FloatRegister src ) {
2309   assert_not_delayed();
2310   sll(index, Interpreter::logStackElementSize, index);
2311   sub(Llocals, index, index);
2312 #ifdef ASSERT
2313   check_for_regarea_stomp(index, Interpreter::local_offset_in_bytes(1), FP, G1_scratch, G4_scratch);
2314 #endif
2315   store_unaligned_double(src, index, Interpreter::local_offset_in_bytes(1));
2316 }
2317 
2318 
2319 int InterpreterMacroAssembler::top_most_monitor_byte_offset() {
2320   const jint delta = frame::interpreter_frame_monitor_size() * wordSize;
2321   int rounded_vm_local_words = ::round_to(frame::interpreter_frame_vm_local_words, WordsPerLong);
2322   return ((-rounded_vm_local_words * wordSize) - delta ) + STACK_BIAS;
2323 }
2324 
2325 
2326 Address InterpreterMacroAssembler::top_most_monitor() {
2327   return Address(FP, top_most_monitor_byte_offset());
2328 }
2329 
2330 
2331 void InterpreterMacroAssembler::compute_stack_base( Register Rdest ) {
2332   add( Lesp,      wordSize,                                    Rdest );
2333 }
2334 
2335 void InterpreterMacroAssembler::get_method_counters(Register method,
2336                                                     Register Rcounters,
2337                                                     Label& skip) {
2338   Label has_counters;
2339   Address method_counters(method, in_bytes(Method::method_counters_offset()));
2340   ld_ptr(method_counters, Rcounters);
2341   br_notnull_short(Rcounters, Assembler::pt, has_counters);
2342   call_VM(noreg, CAST_FROM_FN_PTR(address,
2343           InterpreterRuntime::build_method_counters), method);
2344   ld_ptr(method_counters, Rcounters);
2345   br_null(Rcounters, false, Assembler::pn, skip); // No MethodCounters, OutOfMemory
2346   delayed()->nop();
2347   bind(has_counters);
2348 }
2349 
2350 void InterpreterMacroAssembler::increment_invocation_counter( Register Rcounters, Register Rtmp, Register Rtmp2 ) {
2351   assert(UseCompiler || LogTouchedMethods, "incrementing must be useful");
2352   assert_different_registers(Rcounters, Rtmp, Rtmp2);
2353 
2354   Address inv_counter(Rcounters, MethodCounters::invocation_counter_offset() +
2355                                  InvocationCounter::counter_offset());
2356   Address be_counter (Rcounters, MethodCounters::backedge_counter_offset() +
2357                                  InvocationCounter::counter_offset());
2358   int delta = InvocationCounter::count_increment;
2359 
2360   // Load each counter in a register
2361   ld( inv_counter, Rtmp );
2362   ld( be_counter, Rtmp2 );
2363 
2364   assert( is_simm13( delta ), " delta too large.");
2365 
2366   // Add the delta to the invocation counter and store the result
2367   add( Rtmp, delta, Rtmp );
2368 
2369   // Mask the backedge counter
2370   and3( Rtmp2, InvocationCounter::count_mask_value, Rtmp2 );
2371 
2372   // Store value
2373   st( Rtmp, inv_counter);
2374 
2375   // Add invocation counter + backedge counter
2376   add( Rtmp, Rtmp2, Rtmp);
2377 
2378   // Note that this macro must leave the backedge_count + invocation_count in Rtmp!
2379 }
2380 
2381 void InterpreterMacroAssembler::increment_backedge_counter( Register Rcounters, Register Rtmp, Register Rtmp2 ) {
2382   assert(UseCompiler, "incrementing must be useful");
2383   assert_different_registers(Rcounters, Rtmp, Rtmp2);
2384 
2385   Address be_counter (Rcounters, MethodCounters::backedge_counter_offset() +
2386                                  InvocationCounter::counter_offset());
2387   Address inv_counter(Rcounters, MethodCounters::invocation_counter_offset() +
2388                                  InvocationCounter::counter_offset());
2389 
2390   int delta = InvocationCounter::count_increment;
2391   // Load each counter in a register
2392   ld( be_counter, Rtmp );
2393   ld( inv_counter, Rtmp2 );
2394 
2395   // Add the delta to the backedge counter
2396   add( Rtmp, delta, Rtmp );
2397 
2398   // Mask the invocation counter, add to backedge counter
2399   and3( Rtmp2, InvocationCounter::count_mask_value, Rtmp2 );
2400 
2401   // and store the result to memory
2402   st( Rtmp, be_counter );
2403 
2404   // Add backedge + invocation counter
2405   add( Rtmp, Rtmp2, Rtmp );
2406 
2407   // Note that this macro must leave backedge_count + invocation_count in Rtmp!
2408 }
2409 
2410 void InterpreterMacroAssembler::test_backedge_count_for_osr( Register backedge_count,
2411                                                              Register method_counters,
2412                                                              Register branch_bcp,
2413                                                              Register Rtmp ) {
2414   Label did_not_overflow;
2415   Label overflow_with_error;
2416   assert_different_registers(backedge_count, Rtmp, branch_bcp);
2417   assert(UseOnStackReplacement,"Must UseOnStackReplacement to test_backedge_count_for_osr");
2418 
2419   Address limit(method_counters, in_bytes(MethodCounters::interpreter_backward_branch_limit_offset()));
2420   ld(limit, Rtmp);
2421   cmp_and_br_short(backedge_count, Rtmp, Assembler::lessUnsigned, Assembler::pt, did_not_overflow);
2422 
2423   // When ProfileInterpreter is on, the backedge_count comes from the
2424   // MethodData*, which value does not get reset on the call to
2425   // frequency_counter_overflow().  To avoid excessive calls to the overflow
2426   // routine while the method is being compiled, add a second test to make sure
2427   // the overflow function is called only once every overflow_frequency.
2428   if (ProfileInterpreter) {
2429     const int overflow_frequency = 1024;
2430     andcc(backedge_count, overflow_frequency-1, Rtmp);
2431     brx(Assembler::notZero, false, Assembler::pt, did_not_overflow);
2432     delayed()->nop();
2433   }
2434 
2435   // overflow in loop, pass branch bytecode
2436   set(6,Rtmp);
2437   call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::frequency_counter_overflow), branch_bcp, Rtmp);
2438 
2439   // Was an OSR adapter generated?
2440   // O0 = osr nmethod
2441   br_null_short(O0, Assembler::pn, overflow_with_error);
2442 
2443   // Has the nmethod been invalidated already?
2444   ldub(O0, nmethod::state_offset(), O2);
2445   cmp_and_br_short(O2, nmethod::in_use, Assembler::notEqual, Assembler::pn, overflow_with_error);
2446 
2447   // migrate the interpreter frame off of the stack
2448 
2449   mov(G2_thread, L7);
2450   // save nmethod
2451   mov(O0, L6);
2452   set_last_Java_frame(SP, noreg);
2453   call_VM_leaf(noreg, CAST_FROM_FN_PTR(address, SharedRuntime::OSR_migration_begin), L7);
2454   reset_last_Java_frame();
2455   mov(L7, G2_thread);
2456 
2457   // move OSR nmethod to I1
2458   mov(L6, I1);
2459 
2460   // OSR buffer to I0
2461   mov(O0, I0);
2462 
2463   // remove the interpreter frame
2464   restore(I5_savedSP, 0, SP);
2465 
2466   // Jump to the osr code.
2467   ld_ptr(O1, nmethod::osr_entry_point_offset(), O2);
2468   jmp(O2, G0);
2469   delayed()->nop();
2470 
2471   bind(overflow_with_error);
2472 
2473   bind(did_not_overflow);
2474 }
2475 
2476 
2477 
2478 void InterpreterMacroAssembler::interp_verify_oop(Register reg, TosState state, const char * file, int line) {
2479   if (state == atos) { MacroAssembler::_verify_oop(reg, "broken oop ", file, line); }
2480 }
2481 
2482 
2483 // local helper function for the verify_oop_or_return_address macro
2484 static bool verify_return_address(Method* m, int bci) {
2485 #ifndef PRODUCT
2486   address pc = (address)(m->constMethod())
2487              + in_bytes(ConstMethod::codes_offset()) + bci;
2488   // assume it is a valid return address if it is inside m and is preceded by a jsr
2489   if (!m->contains(pc))                                          return false;
2490   address jsr_pc;
2491   jsr_pc = pc - Bytecodes::length_for(Bytecodes::_jsr);
2492   if (*jsr_pc == Bytecodes::_jsr   && jsr_pc >= m->code_base())    return true;
2493   jsr_pc = pc - Bytecodes::length_for(Bytecodes::_jsr_w);
2494   if (*jsr_pc == Bytecodes::_jsr_w && jsr_pc >= m->code_base())    return true;
2495 #endif // PRODUCT
2496   return false;
2497 }
2498 
2499 
2500 void InterpreterMacroAssembler::verify_oop_or_return_address(Register reg, Register Rtmp) {
2501   if (!VerifyOops)  return;
2502   // the VM documentation for the astore[_wide] bytecode allows
2503   // the TOS to be not only an oop but also a return address
2504   Label test;
2505   Label skip;
2506   // See if it is an address (in the current method):
2507 
2508   mov(reg, Rtmp);
2509   const int log2_bytecode_size_limit = 16;
2510   srl(Rtmp, log2_bytecode_size_limit, Rtmp);
2511   br_notnull_short( Rtmp, pt, test );
2512 
2513   // %%% should use call_VM_leaf here?
2514   save_frame_and_mov(0, Lmethod, O0, reg, O1);
2515   save_thread(L7_thread_cache);
2516   call(CAST_FROM_FN_PTR(address,verify_return_address), relocInfo::none);
2517   delayed()->nop();
2518   restore_thread(L7_thread_cache);
2519   br_notnull( O0, false, pt, skip );
2520   delayed()->restore();
2521 
2522   // Perform a more elaborate out-of-line call
2523   // Not an address; verify it:
2524   bind(test);
2525   verify_oop(reg);
2526   bind(skip);
2527 }
2528 
2529 
2530 void InterpreterMacroAssembler::verify_FPU(int stack_depth, TosState state) {
2531   if (state == ftos || state == dtos) MacroAssembler::verify_FPU(stack_depth);
2532 }
2533 
2534 
2535 // Jump if ((*counter_addr += increment) & mask) satisfies the condition.
2536 void InterpreterMacroAssembler::increment_mask_and_jump(Address counter_addr,
2537                                                         int increment, Address mask_addr,
2538                                                         Register scratch1, Register scratch2,
2539                                                         Condition cond, Label *where) {
2540   ld(counter_addr, scratch1);
2541   add(scratch1, increment, scratch1);
2542   ld(mask_addr, scratch2);
2543   andcc(scratch1, scratch2,  G0);
2544   br(cond, false, Assembler::pn, *where);
2545   delayed()->st(scratch1, counter_addr);
2546 }
2547 
2548 // Inline assembly for:
2549 //
2550 // if (thread is in interp_only_mode) {
2551 //   InterpreterRuntime::post_method_entry();
2552 // }
2553 // if (DTraceMethodProbes) {
2554 //   SharedRuntime::dtrace_method_entry(method, receiver);
2555 // }
2556 // if (RC_TRACE_IN_RANGE(0x00001000, 0x00002000)) {
2557 //   SharedRuntime::rc_trace_method_entry(method, receiver);
2558 // }
2559 
2560 void InterpreterMacroAssembler::notify_method_entry() {
2561 
2562   // Whenever JVMTI puts a thread in interp_only_mode, method
2563   // entry/exit events are sent for that thread to track stack
2564   // depth.  If it is possible to enter interp_only_mode we add
2565   // the code to check if the event should be sent.
2566   if (JvmtiExport::can_post_interpreter_events()) {
2567     Label L;
2568     Register temp_reg = O5;
2569     const Address interp_only(G2_thread, JavaThread::interp_only_mode_offset());
2570     ld(interp_only, temp_reg);
2571     cmp_and_br_short(temp_reg, 0, equal, pt, L);
2572     call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::post_method_entry));
2573     bind(L);
2574   }
2575 
2576   {
2577     Register temp_reg = O5;
2578     SkipIfEqual skip_if(this, temp_reg, &DTraceMethodProbes, zero);
2579     call_VM_leaf(noreg,
2580       CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry),
2581       G2_thread, Lmethod);
2582   }
2583 
2584   // RedefineClasses() tracing support for obsolete method entry
2585   if (log_is_enabled(Trace, redefine, class, obsolete)) {
2586     call_VM_leaf(noreg,
2587       CAST_FROM_FN_PTR(address, SharedRuntime::rc_trace_method_entry),
2588       G2_thread, Lmethod);
2589   }
2590 }
2591 
2592 
2593 // Inline assembly for:
2594 //
2595 // if (thread is in interp_only_mode) {
2596 //   // save result
2597 //   InterpreterRuntime::post_method_exit();
2598 //   // restore result
2599 // }
2600 // if (DTraceMethodProbes) {
2601 //   SharedRuntime::dtrace_method_exit(thread, method);
2602 // }
2603 //
2604 // Native methods have their result stored in d_tmp and l_tmp
2605 // Java methods have their result stored in the expression stack
2606 
2607 void InterpreterMacroAssembler::notify_method_exit(bool is_native_method,
2608                                                    TosState state,
2609                                                    NotifyMethodExitMode mode) {
2610 
2611   // Whenever JVMTI puts a thread in interp_only_mode, method
2612   // entry/exit events are sent for that thread to track stack
2613   // depth.  If it is possible to enter interp_only_mode we add
2614   // the code to check if the event should be sent.
2615   if (mode == NotifyJVMTI && JvmtiExport::can_post_interpreter_events()) {
2616     Label L;
2617     Register temp_reg = O5;
2618     const Address interp_only(G2_thread, JavaThread::interp_only_mode_offset());
2619     ld(interp_only, temp_reg);
2620     cmp_and_br_short(temp_reg, 0, equal, pt, L);
2621 
2622     // Note: frame::interpreter_frame_result has a dependency on how the
2623     // method result is saved across the call to post_method_exit. For
2624     // native methods it assumes the result registers are saved to
2625     // l_scratch and d_scratch. If this changes then the interpreter_frame_result
2626     // implementation will need to be updated too.
2627 
2628     save_return_value(state, is_native_method);
2629     call_VM(noreg,
2630             CAST_FROM_FN_PTR(address, InterpreterRuntime::post_method_exit));
2631     restore_return_value(state, is_native_method);
2632     bind(L);
2633   }
2634 
2635   {
2636     Register temp_reg = O5;
2637     // Dtrace notification
2638     SkipIfEqual skip_if(this, temp_reg, &DTraceMethodProbes, zero);
2639     save_return_value(state, is_native_method);
2640     call_VM_leaf(
2641       noreg,
2642       CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit),
2643       G2_thread, Lmethod);
2644     restore_return_value(state, is_native_method);
2645   }
2646 }
2647 
2648 void InterpreterMacroAssembler::save_return_value(TosState state, bool is_native_call) {
2649   if (is_native_call) {
2650     stf(FloatRegisterImpl::D, F0, d_tmp);
2651     stx(O0, l_tmp);
2652   } else {
2653     push(state);
2654   }
2655 }
2656 
2657 void InterpreterMacroAssembler::restore_return_value( TosState state, bool is_native_call) {
2658   if (is_native_call) {
2659     ldf(FloatRegisterImpl::D, d_tmp, F0);
2660     ldx(l_tmp, O0);
2661   } else {
2662     pop(state);
2663   }
2664 }