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