rev 50027 : 8201593: Print array length in ArrayIndexOutOfBoundsException.
Reviewed-by: dholmes, mdoerr, smonteith, shade

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