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