1 /*
   2  * Copyright (c) 2008, 2021, 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 "asm/assembler.hpp"
  27 #include "asm/macroAssembler.inline.hpp"
  28 #include "classfile/javaClasses.hpp"
  29 #include "interpreter/bytecodeHistogram.hpp"
  30 #include "interpreter/interp_masm.hpp"
  31 #include "interpreter/interpreter.hpp"
  32 #include "interpreter/interpreterRuntime.hpp"
  33 #include "interpreter/templateInterpreterGenerator.hpp"
  34 #include "interpreter/templateTable.hpp"
  35 #include "oops/arrayOop.hpp"
  36 #include "oops/methodData.hpp"
  37 #include "oops/method.hpp"
  38 #include "oops/oop.inline.hpp"
  39 #include "prims/jvmtiExport.hpp"
  40 #include "prims/jvmtiThreadState.hpp"
  41 #include "prims/methodHandles.hpp"
  42 #include "runtime/arguments.hpp"
  43 #include "runtime/deoptimization.hpp"
  44 #include "runtime/frame.inline.hpp"
  45 #include "runtime/jniHandles.hpp"
  46 #include "runtime/sharedRuntime.hpp"
  47 #include "runtime/stubRoutines.hpp"
  48 #include "runtime/synchronizer.hpp"
  49 #include "runtime/timer.hpp"
  50 #include "runtime/vframeArray.hpp"
  51 #include "utilities/align.hpp"
  52 #include "utilities/debug.hpp"
  53 #include "utilities/macros.hpp"
  54 
  55 // Size of interpreter code.  Increase if too small.  Interpreter will
  56 // fail with a guarantee ("not enough space for interpreter generation");
  57 // if too small.
  58 // Run with +PrintInterpreter to get the VM to print out the size.
  59 // Max size with JVMTI
  60 int TemplateInterpreter::InterpreterCodeSize = 180 * 1024;
  61 
  62 #define __ _masm->
  63 
  64 //------------------------------------------------------------------------------------------------------------------------
  65 
  66 address TemplateInterpreterGenerator::generate_slow_signature_handler() {
  67   address entry = __ pc();
  68 
  69   // callee-save register for saving LR, shared with generate_native_entry
  70   const Register Rsaved_ret_addr = Rtmp_save0;
  71 
  72   __ mov(Rsaved_ret_addr, LR);
  73 
  74   __ mov(R1, Rmethod);
  75   __ mov(R2, Rlocals);
  76   __ mov(R3, SP);
  77 
  78 
  79   // Safer to save R9 (when scratched) since callers may have been
  80   // written assuming R9 survives. This is suboptimal but
  81   // probably not important for this slow case call site.
  82   // Note for R9 saving: slow_signature_handler may copy register
  83   // arguments above the current SP (passed as R3). It is safe for
  84   // call_VM to use push and pop to protect additional values on the
  85   // stack if needed.
  86   __ call_VM(CAST_FROM_FN_PTR(address, InterpreterRuntime::slow_signature_handler), true /* save R9 if needed*/);
  87   __ add(SP, SP, wordSize);     // Skip R0
  88   __ pop(RegisterSet(R1, R3));  // Load arguments passed in registers
  89 #ifdef __ABI_HARD__
  90   // Few alternatives to an always-load-FP-registers approach:
  91   // - parse method signature to detect FP arguments
  92   // - keep a counter/flag on a stack indicationg number of FP arguments in the method.
  93   // The later has been originally implemented and tested but a conditional path could
  94   // eliminate any gain imposed by avoiding 8 double word loads.
  95   __ fldmiad(SP, FloatRegisterSet(D0, 8), writeback);
  96 #endif // __ABI_HARD__
  97 
  98   __ ret(Rsaved_ret_addr);
  99 
 100   return entry;
 101 }
 102 
 103 
 104 //
 105 // Various method entries (that c++ and asm interpreter agree upon)
 106 //------------------------------------------------------------------------------------------------------------------------
 107 //
 108 //
 109 
 110 // Abstract method entry
 111 // Attempt to execute abstract method. Throw exception
 112 address TemplateInterpreterGenerator::generate_abstract_entry(void) {
 113   address entry_point = __ pc();
 114 
 115 
 116   __ empty_expression_stack();
 117 
 118   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_AbstractMethodError));
 119 
 120   DEBUG_ONLY(STOP("generate_abstract_entry");) // Should not reach here
 121   return entry_point;
 122 }
 123 
 124 address TemplateInterpreterGenerator::generate_math_entry(AbstractInterpreter::MethodKind kind) {
 125   if (!InlineIntrinsics) return NULL; // Generate a vanilla entry
 126 
 127   address entry_point = NULL;
 128   Register continuation = LR;
 129   bool use_runtime_call = false;
 130   switch (kind) {
 131   case Interpreter::java_lang_math_abs:
 132     entry_point = __ pc();
 133 #ifdef __SOFTFP__
 134     use_runtime_call = true;
 135     __ ldrd(R0, Address(SP));
 136 #else // !__SOFTFP__
 137     __ ldr_double(D0, Address(SP));
 138     __ abs_double(D0, D0);
 139 #endif // __SOFTFP__
 140     break;
 141   case Interpreter::java_lang_math_sqrt:
 142     entry_point = __ pc();
 143 #ifdef __SOFTFP__
 144     use_runtime_call = true;
 145     __ ldrd(R0, Address(SP));
 146 #else // !__SOFTFP__
 147     __ ldr_double(D0, Address(SP));
 148     __ sqrt_double(D0, D0);
 149 #endif // __SOFTFP__
 150     break;
 151   case Interpreter::java_lang_math_sin:
 152   case Interpreter::java_lang_math_cos:
 153   case Interpreter::java_lang_math_tan:
 154   case Interpreter::java_lang_math_log:
 155   case Interpreter::java_lang_math_log10:
 156   case Interpreter::java_lang_math_exp:
 157     entry_point = __ pc();
 158     use_runtime_call = true;
 159 #ifdef __SOFTFP__
 160     __ ldrd(R0, Address(SP));
 161 #else // !__SOFTFP__
 162     __ ldr_double(D0, Address(SP));
 163 #endif // __SOFTFP__
 164     break;
 165   case Interpreter::java_lang_math_pow:
 166     entry_point = __ pc();
 167     use_runtime_call = true;
 168 #ifdef __SOFTFP__
 169     __ ldrd(R0, Address(SP, 2 * Interpreter::stackElementSize));
 170     __ ldrd(R2, Address(SP));
 171 #else // !__SOFTFP__
 172     __ ldr_double(D0, Address(SP, 2 * Interpreter::stackElementSize));
 173     __ ldr_double(D1, Address(SP));
 174 #endif // __SOFTFP__
 175     break;
 176   case Interpreter::java_lang_math_fmaD:
 177   case Interpreter::java_lang_math_fmaF:
 178     // TODO: Implement intrinsic
 179     break;
 180   default:
 181     ShouldNotReachHere();
 182   }
 183 
 184   if (entry_point != NULL) {
 185     __ mov(SP, Rsender_sp);
 186     if (use_runtime_call) {
 187       __ mov(Rtmp_save0, LR);
 188       continuation = Rtmp_save0;
 189       generate_math_runtime_call(kind);
 190     }
 191     __ ret(continuation);
 192   }
 193   return entry_point;
 194 }
 195 
 196 void TemplateInterpreterGenerator::generate_math_runtime_call(AbstractInterpreter::MethodKind kind) {
 197   address fn;
 198   switch (kind) {
 199 #ifdef __SOFTFP__
 200   case Interpreter::java_lang_math_abs:
 201     fn = CAST_FROM_FN_PTR(address, SharedRuntime::dabs);
 202     break;
 203   case Interpreter::java_lang_math_sqrt:
 204     fn = CAST_FROM_FN_PTR(address, SharedRuntime::dsqrt);
 205     break;
 206 #endif // __SOFTFP__
 207   case Interpreter::java_lang_math_sin:
 208     fn = CAST_FROM_FN_PTR(address, SharedRuntime::dsin);
 209     break;
 210   case Interpreter::java_lang_math_cos:
 211     fn = CAST_FROM_FN_PTR(address, SharedRuntime::dcos);
 212     break;
 213   case Interpreter::java_lang_math_tan:
 214     fn = CAST_FROM_FN_PTR(address, SharedRuntime::dtan);
 215     break;
 216   case Interpreter::java_lang_math_log:
 217     fn = CAST_FROM_FN_PTR(address, SharedRuntime::dlog);
 218     break;
 219   case Interpreter::java_lang_math_log10:
 220     fn = CAST_FROM_FN_PTR(address, SharedRuntime::dlog10);
 221     break;
 222   case Interpreter::java_lang_math_exp:
 223     fn = CAST_FROM_FN_PTR(address, SharedRuntime::dexp);
 224     break;
 225   case Interpreter::java_lang_math_pow:
 226     fn = CAST_FROM_FN_PTR(address, SharedRuntime::dpow);
 227     break;
 228   default:
 229     ShouldNotReachHere();
 230     fn = NULL; // silence "maybe uninitialized" compiler warnings
 231   }
 232   __ call_VM_leaf(fn);
 233 }
 234 
 235 address TemplateInterpreterGenerator::generate_StackOverflowError_handler() {
 236   address entry = __ pc();
 237 
 238   // Note: There should be a minimal interpreter frame set up when stack
 239   // overflow occurs since we check explicitly for it now.
 240   //
 241 #ifdef ASSERT
 242   { Label L;
 243     __ sub(Rtemp, FP, - frame::interpreter_frame_monitor_block_top_offset * wordSize);
 244     __ cmp(SP, Rtemp);  // Rtemp = maximal SP for current FP,
 245                         //  (stack grows negative)
 246     __ b(L, ls); // check if frame is complete
 247     __ stop ("interpreter frame not set up");
 248     __ bind(L);
 249   }
 250 #endif // ASSERT
 251 
 252   // Restore bcp under the assumption that the current frame is still
 253   // interpreted
 254   __ restore_bcp();
 255 
 256   // expression stack must be empty before entering the VM if an exception
 257   // happened
 258   __ empty_expression_stack();
 259 
 260   // throw exception
 261   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_StackOverflowError));
 262 
 263   __ should_not_reach_here();
 264 
 265   return entry;
 266 }
 267 
 268 address TemplateInterpreterGenerator::generate_ArrayIndexOutOfBounds_handler() {
 269   address entry = __ pc();
 270 
 271   // index is in R4_ArrayIndexOutOfBounds_index
 272 
 273   // expression stack must be empty before entering the VM if an exception happened
 274   __ empty_expression_stack();
 275 
 276   // setup parameters
 277   // Array expected in R1.
 278   __ mov(R2, R4_ArrayIndexOutOfBounds_index);
 279 
 280   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_ArrayIndexOutOfBoundsException), R1, R2);
 281 
 282   __ nop(); // to avoid filling CPU pipeline with invalid instructions
 283   __ nop();
 284   __ should_not_reach_here();
 285 
 286   return entry;
 287 }
 288 
 289 address TemplateInterpreterGenerator::generate_ClassCastException_handler() {
 290   address entry = __ pc();
 291 
 292   // object is in R2_ClassCastException_obj
 293 
 294   // expression stack must be empty before entering the VM if an exception
 295   // happened
 296   __ empty_expression_stack();
 297 
 298   __ mov(R1, R2_ClassCastException_obj);
 299   __ call_VM(noreg,
 300              CAST_FROM_FN_PTR(address,
 301                               InterpreterRuntime::throw_ClassCastException),
 302              R1);
 303 
 304   __ should_not_reach_here();
 305 
 306   return entry;
 307 }
 308 
 309 address TemplateInterpreterGenerator::generate_exception_handler_common(const char* name, const char* message, bool pass_oop) {
 310   assert(!pass_oop || message == NULL, "either oop or message but not both");
 311   address entry = __ pc();
 312 
 313   InlinedString Lname(name);
 314   InlinedString Lmessage(message);
 315 
 316   if (pass_oop) {
 317     // object is at TOS
 318     __ pop_ptr(R2);
 319   }
 320 
 321   // expression stack must be empty before entering the VM if an exception happened
 322   __ empty_expression_stack();
 323 
 324   // setup parameters
 325   __ ldr_literal(R1, Lname);
 326 
 327   if (pass_oop) {
 328     __ call_VM(Rexception_obj, CAST_FROM_FN_PTR(address, InterpreterRuntime::create_klass_exception), R1, R2);
 329   } else {
 330     if (message != NULL) {
 331       __ ldr_literal(R2, Lmessage);
 332     } else {
 333       __ mov(R2, 0);
 334     }
 335     __ call_VM(Rexception_obj, CAST_FROM_FN_PTR(address, InterpreterRuntime::create_exception), R1, R2);
 336   }
 337 
 338   // throw exception
 339   __ b(Interpreter::throw_exception_entry());
 340 
 341   __ nop(); // to avoid filling CPU pipeline with invalid instructions
 342   __ nop();
 343   __ bind_literal(Lname);
 344   if (!pass_oop && (message != NULL)) {
 345     __ bind_literal(Lmessage);
 346   }
 347 
 348   return entry;
 349 }
 350 
 351 address TemplateInterpreterGenerator::generate_return_entry_for(TosState state, int step, size_t index_size) {
 352   address entry = __ pc();
 353 
 354   __ interp_verify_oop(R0_tos, state, __FILE__, __LINE__);
 355 
 356   // Restore stack bottom in case i2c adjusted stack
 357   __ ldr(SP, Address(FP, frame::interpreter_frame_last_sp_offset * wordSize));
 358   // and NULL it as marker that SP is now tos until next java call
 359   __ mov(Rtemp, (int)NULL_WORD);
 360   __ str(Rtemp, Address(FP, frame::interpreter_frame_last_sp_offset * wordSize));
 361 
 362   __ restore_method();
 363   __ restore_bcp();
 364   __ restore_dispatch();
 365   __ restore_locals();
 366 
 367   const Register Rcache = R2_tmp;
 368   const Register Rindex = R3_tmp;
 369   __ get_cache_and_index_at_bcp(Rcache, Rindex, 1, index_size);
 370 
 371   __ add(Rtemp, Rcache, AsmOperand(Rindex, lsl, LogBytesPerWord));
 372   __ ldrb(Rtemp, Address(Rtemp, ConstantPoolCache::base_offset() + ConstantPoolCacheEntry::flags_offset()));
 373   __ check_stack_top();
 374   __ add(Rstack_top, Rstack_top, AsmOperand(Rtemp, lsl, Interpreter::logStackElementSize));
 375 
 376   __ convert_retval_to_tos(state);
 377 
 378  __ check_and_handle_popframe();
 379  __ check_and_handle_earlyret();
 380 
 381   __ dispatch_next(state, step);
 382 
 383   return entry;
 384 }
 385 
 386 
 387 address TemplateInterpreterGenerator::generate_deopt_entry_for(TosState state, int step, address continuation) {
 388   address entry = __ pc();
 389 
 390   __ interp_verify_oop(R0_tos, state, __FILE__, __LINE__);
 391 
 392   // The stack is not extended by deopt but we must NULL last_sp as this
 393   // entry is like a "return".
 394   __ mov(Rtemp, 0);
 395   __ str(Rtemp, Address(FP, frame::interpreter_frame_last_sp_offset * wordSize));
 396 
 397   __ restore_method();
 398   __ restore_bcp();
 399   __ restore_dispatch();
 400   __ restore_locals();
 401 
 402   // handle exceptions
 403   { Label L;
 404     __ ldr(Rtemp, Address(Rthread, Thread::pending_exception_offset()));
 405     __ cbz(Rtemp, L);
 406     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_pending_exception));
 407     __ should_not_reach_here();
 408     __ bind(L);
 409   }
 410 
 411   if (continuation == NULL) {
 412     __ dispatch_next(state, step);
 413   } else {
 414     __ jump_to_entry(continuation);
 415   }
 416 
 417   return entry;
 418 }
 419 
 420 address TemplateInterpreterGenerator::generate_result_handler_for(BasicType type) {
 421   address entry = __ pc();
 422 
 423   switch (type) {
 424   case T_CHAR    : /* Nothing to do */  break;
 425   case T_BYTE    : /* Nothing to do */  break;
 426   case T_SHORT   : /* Nothing to do */  break;
 427   case T_INT     : /* Nothing to do */  break;
 428   case T_LONG    : /* Nothing to do */  break;
 429   case T_VOID    : /* Nothing to do */  break;
 430   case T_DOUBLE  : /* Nothing to do */  break;
 431   case T_FLOAT   : /* Nothing to do */  break;
 432   case T_BOOLEAN : __ c2bool(R0);       break;
 433   case T_OBJECT  :
 434     __ ldr(R0, Address(FP, frame::interpreter_frame_oop_temp_offset * wordSize));
 435     __ verify_oop(R0);
 436     break;
 437   default        : __ should_not_reach_here(); break;
 438   }
 439 
 440   __ ret();
 441   return entry;
 442 }
 443 
 444 address TemplateInterpreterGenerator::generate_safept_entry_for(TosState state, address runtime_entry) {
 445   address entry = __ pc();
 446   __ push(state);
 447   __ call_VM(noreg, runtime_entry);
 448 
 449   // load current bytecode
 450   __ ldrb(R3_bytecode, Address(Rbcp));
 451   __ dispatch_only_normal(vtos);
 452   return entry;
 453 }
 454 
 455 
 456 // Helpers for commoning out cases in the various type of method entries.
 457 //
 458 
 459 // increment invocation count & check for overflow
 460 //
 461 // Note: checking for negative value instead of overflow
 462 //       so we have a 'sticky' overflow test
 463 //
 464 // In: Rmethod.
 465 //
 466 // Uses R0, R1, Rtemp.
 467 //
 468 void TemplateInterpreterGenerator::generate_counter_incr(Label* overflow) {
 469   Label done;
 470   const Register Rcounters = Rtemp;
 471   const Address invocation_counter(Rcounters,
 472                 MethodCounters::invocation_counter_offset() +
 473                 InvocationCounter::counter_offset());
 474 
 475   // Note: In tiered we increment either counters in MethodCounters* or
 476   // in MDO depending if we're profiling or not.
 477   int increment = InvocationCounter::count_increment;
 478   Label no_mdo;
 479   if (ProfileInterpreter) {
 480     // Are we profiling?
 481     __ ldr(R1_tmp, Address(Rmethod, Method::method_data_offset()));
 482     __ cbz(R1_tmp, no_mdo);
 483     // Increment counter in the MDO
 484     const Address mdo_invocation_counter(R1_tmp,
 485                   in_bytes(MethodData::invocation_counter_offset()) +
 486                   in_bytes(InvocationCounter::counter_offset()));
 487     const Address mask(R1_tmp, in_bytes(MethodData::invoke_mask_offset()));
 488     __ increment_mask_and_jump(mdo_invocation_counter, increment, mask, R0_tmp, Rtemp, eq, overflow);
 489     __ b(done);
 490   }
 491   __ bind(no_mdo);
 492   __ get_method_counters(Rmethod, Rcounters, done);
 493   const Address mask(Rcounters, in_bytes(MethodCounters::invoke_mask_offset()));
 494   __ increment_mask_and_jump(invocation_counter, increment, mask, R0_tmp, R1_tmp, eq, overflow);
 495   __ bind(done);
 496 }
 497 
 498 void TemplateInterpreterGenerator::generate_counter_overflow(Label& do_continue) {
 499   // InterpreterRuntime::frequency_counter_overflow takes one argument
 500   // indicating if the counter overflow occurs at a backwards branch (non-NULL bcp).
 501   // The call returns the address of the verified entry point for the method or NULL
 502   // if the compilation did not complete (either went background or bailed out).
 503   __ mov(R1, (int)false);
 504   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::frequency_counter_overflow), R1);
 505 
 506   // jump to the interpreted entry.
 507   __ b(do_continue);
 508 }
 509 
 510 void TemplateInterpreterGenerator::generate_stack_overflow_check(void) {
 511   // Check if we've got enough room on the stack for
 512   //  - overhead;
 513   //  - locals;
 514   //  - expression stack.
 515   //
 516   // Registers on entry:
 517   //
 518   // R3 = number of additional locals
 519   // Rthread
 520   // Rmethod
 521   // Registers used: R0, R1, R2, Rtemp.
 522 
 523   const Register Radditional_locals = R3;
 524   const Register RmaxStack = R2;
 525 
 526   // monitor entry size
 527   const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
 528 
 529   // total overhead size: entry_size + (saved registers, thru expr stack bottom).
 530   // be sure to change this if you add/subtract anything to/from the overhead area
 531   const int overhead_size = (frame::sender_sp_offset - frame::interpreter_frame_initial_sp_offset)*wordSize + entry_size;
 532 
 533   // Pages reserved for VM runtime calls and subsequent Java calls.
 534   const int reserved_pages = StackOverflow::stack_shadow_zone_size();
 535 
 536   // Thread::stack_size() includes guard pages, and they should not be touched.
 537   const int guard_pages = StackOverflow::stack_guard_zone_size();
 538 
 539   __ ldr(R0, Address(Rthread, Thread::stack_base_offset()));
 540   __ ldr(R1, Address(Rthread, Thread::stack_size_offset()));
 541   __ ldr(Rtemp, Address(Rmethod, Method::const_offset()));
 542   __ ldrh(RmaxStack, Address(Rtemp, ConstMethod::max_stack_offset()));
 543   __ sub_slow(Rtemp, SP, overhead_size + reserved_pages + guard_pages + Method::extra_stack_words());
 544 
 545   // reserve space for additional locals
 546   __ sub(Rtemp, Rtemp, AsmOperand(Radditional_locals, lsl, Interpreter::logStackElementSize));
 547 
 548   // stack size
 549   __ sub(R0, R0, R1);
 550 
 551   // reserve space for expression stack
 552   __ sub(Rtemp, Rtemp, AsmOperand(RmaxStack, lsl, Interpreter::logStackElementSize));
 553 
 554   __ cmp(Rtemp, R0);
 555 
 556   __ mov(SP, Rsender_sp, ls);  // restore SP
 557   __ b(StubRoutines::throw_StackOverflowError_entry(), ls);
 558 }
 559 
 560 
 561 // Allocate monitor and lock method (asm interpreter)
 562 //
 563 void TemplateInterpreterGenerator::lock_method() {
 564   // synchronize method
 565 
 566   const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
 567   assert ((entry_size % StackAlignmentInBytes) == 0, "should keep stack alignment");
 568 
 569   #ifdef ASSERT
 570     { Label L;
 571       __ ldr_u32(Rtemp, Address(Rmethod, Method::access_flags_offset()));
 572       __ tbnz(Rtemp, JVM_ACC_SYNCHRONIZED_BIT, L);
 573       __ stop("method doesn't need synchronization");
 574       __ bind(L);
 575     }
 576   #endif // ASSERT
 577 
 578   // get synchronization object
 579   { Label done;
 580     __ ldr_u32(Rtemp, Address(Rmethod, Method::access_flags_offset()));
 581     __ tst(Rtemp, JVM_ACC_STATIC);
 582     __ ldr(R0, Address(Rlocals, Interpreter::local_offset_in_bytes(0)), eq); // get receiver (assume this is frequent case)
 583     __ b(done, eq);
 584     __ load_mirror(R0, Rmethod, Rtemp);
 585     __ bind(done);
 586   }
 587 
 588   // add space for monitor & lock
 589 
 590 
 591   __ sub(Rstack_top, Rstack_top, entry_size);
 592   __ check_stack_top_on_expansion();
 593                                               // add space for a monitor entry
 594   __ str(Rstack_top, Address(FP, frame::interpreter_frame_monitor_block_top_offset * wordSize));
 595                                               // set new monitor block top
 596   __ str(R0, Address(Rstack_top, BasicObjectLock::obj_offset_in_bytes()));
 597                                               // store object
 598   __ mov(R1, Rstack_top);                     // monitor entry address
 599   __ lock_object(R1);
 600 }
 601 
 602 
 603 //
 604 // Generate a fixed interpreter frame. This is identical setup for interpreted methods
 605 // and for native methods hence the shared code.
 606 
 607 void TemplateInterpreterGenerator::generate_fixed_frame(bool native_call) {
 608   // Generates the following stack layout:
 609   //
 610   // [ expr. stack bottom ]
 611   // [ saved Rbcp         ]
 612   // [ current Rlocals    ]
 613   // [ cache              ]
 614   // [ mdx                ]
 615   // [ Method*            ]
 616   // [ last_sp            ]
 617   // [ sender_sp          ]
 618   // [ saved FP           ] <--- FP
 619   // [ saved LR           ]
 620 
 621   // initialize fixed part of activation frame
 622   __ push(LR);                                        // save return address
 623   __ push(FP);                                        // save FP
 624   __ mov(FP, SP);                                     // establish new FP
 625 
 626   __ push(Rsender_sp);
 627 
 628   __ mov(R0, 0);
 629   __ push(R0);                                        // leave last_sp as null
 630 
 631   // setup Rbcp
 632   if (native_call) {
 633     __ mov(Rbcp, 0);                                  // bcp = 0 for native calls
 634   } else {
 635     __ ldr(Rtemp, Address(Rmethod, Method::const_offset())); // get ConstMethod*
 636     __ add(Rbcp, Rtemp, ConstMethod::codes_offset()); // get codebase
 637   }
 638 
 639   __ push(Rmethod);                                    // save Method*
 640   // Get mirror and store it in the frame as GC root for this Method*
 641   __ load_mirror(Rtemp, Rmethod, Rtemp);
 642   __ push(Rtemp);
 643 
 644   if (ProfileInterpreter) {
 645     __ ldr(Rtemp, Address(Rmethod, Method::method_data_offset()));
 646     __ tst(Rtemp, Rtemp);
 647     __ add(Rtemp, Rtemp, in_bytes(MethodData::data_offset()), ne);
 648     __ push(Rtemp);                                    // set the mdp (method data pointer)
 649   } else {
 650     __ push(R0);
 651   }
 652 
 653   __ ldr(Rtemp, Address(Rmethod, Method::const_offset()));
 654   __ ldr(Rtemp, Address(Rtemp, ConstMethod::constants_offset()));
 655   __ ldr(Rtemp, Address(Rtemp, ConstantPool::cache_offset_in_bytes()));
 656   __ push(Rtemp);                                      // set constant pool cache
 657   __ push(Rlocals);                                    // set locals pointer
 658   __ push(Rbcp);                                       // set bcp
 659   __ push(R0);                                         // reserve word for pointer to expression stack bottom
 660   __ str(SP, Address(SP, 0));                          // set expression stack bottom
 661 }
 662 
 663 
 664 // End of helpers
 665 
 666 //------------------------------------------------------------------------------------------------------------------------
 667 // Entry points
 668 //
 669 // Here we generate the various kind of entries into the interpreter.
 670 // The two main entry type are generic bytecode methods and native call method.
 671 // These both come in synchronized and non-synchronized versions but the
 672 // frame layout they create is very similar. The other method entry
 673 // types are really just special purpose entries that are really entry
 674 // and interpretation all in one. These are for trivial methods like
 675 // accessor, empty, or special math methods.
 676 //
 677 // When control flow reaches any of the entry types for the interpreter
 678 // the following holds ->
 679 //
 680 // Arguments:
 681 //
 682 // Rmethod: Method*
 683 // Rthread: thread
 684 // Rsender_sp:  sender sp
 685 // Rparams (SP on 32-bit ARM): pointer to method parameters
 686 //
 687 // LR: return address
 688 //
 689 // Stack layout immediately at entry
 690 //
 691 // [ parameter n        ] <--- Rparams (SP on 32-bit ARM)
 692 //   ...
 693 // [ parameter 1        ]
 694 // [ expression stack   ] (caller's java expression stack)
 695 
 696 // Assuming that we don't go to one of the trivial specialized
 697 // entries the stack will look like below when we are ready to execute
 698 // the first bytecode (or call the native routine). The register usage
 699 // will be as the template based interpreter expects.
 700 //
 701 // local variables follow incoming parameters immediately; i.e.
 702 // the return address is saved at the end of the locals.
 703 //
 704 // [ expr. stack        ] <--- Rstack_top (SP on 32-bit ARM)
 705 // [ monitor entry      ]
 706 //   ...
 707 // [ monitor entry      ]
 708 // [ expr. stack bottom ]
 709 // [ saved Rbcp         ]
 710 // [ current Rlocals    ]
 711 // [ cache              ]
 712 // [ mdx                ]
 713 // [ mirror             ]
 714 // [ Method*            ]
 715 //
 716 // 32-bit ARM:
 717 // [ last_sp            ]
 718 //
 719 // [ sender_sp          ]
 720 // [ saved FP           ] <--- FP
 721 // [ saved LR           ]
 722 // [ optional padding(*)]
 723 // [ local variable m   ]
 724 //   ...
 725 // [ local variable 1   ]
 726 // [ parameter n        ]
 727 //   ...
 728 // [ parameter 1        ] <--- Rlocals
 729 //
 730 
 731 address TemplateInterpreterGenerator::generate_Continuation_doYield_entry(void) {
 732   Unimplemented();
 733   return NULL;
 734 }
 735 
 736 address TemplateInterpreterGenerator::generate_Reference_get_entry(void) {
 737   // Code: _aload_0, _getfield, _areturn
 738   // parameter size = 1
 739   //
 740   // The code that gets generated by this routine is split into 2 parts:
 741   //    1. The "intrinsified" code performing an ON_WEAK_OOP_REF load,
 742   //    2. The slow path - which is an expansion of the regular method entry.
 743   //
 744   // Notes:-
 745   // * An intrinsic is always executed, where an ON_WEAK_OOP_REF load is performed.
 746   // * We may jump to the slow path iff the receiver is null. If the
 747   //   Reference object is null then we no longer perform an ON_WEAK_OOP_REF load
 748   //   Thus we can use the regular method entry code to generate the NPE.
 749   //
 750   // Rmethod: Method*
 751   // Rthread: thread
 752   // Rsender_sp: sender sp, must be preserved for slow path, set SP to it on fast path
 753   // Rparams: parameters
 754 
 755   address entry = __ pc();
 756   Label slow_path;
 757   const Register Rthis = R0;
 758   const Register Rret_addr = Rtmp_save1;
 759   assert_different_registers(Rthis, Rret_addr, Rsender_sp);
 760 
 761   const int referent_offset = java_lang_ref_Reference::referent_offset();
 762 
 763   // Check if local 0 != NULL
 764   // If the receiver is null then it is OK to jump to the slow path.
 765   __ ldr(Rthis, Address(Rparams));
 766   __ cbz(Rthis, slow_path);
 767 
 768   // Preserve LR
 769   __ mov(Rret_addr, LR);
 770 
 771   // Load the value of the referent field.
 772   const Address field_address(Rthis, referent_offset);
 773   __ load_heap_oop(R0, field_address, Rtemp, R1_tmp, R2_tmp, ON_WEAK_OOP_REF);
 774 
 775   // _areturn
 776   __ mov(SP, Rsender_sp);
 777   __ ret(Rret_addr);
 778 
 779   // generate a vanilla interpreter entry as the slow path
 780   __ bind(slow_path);
 781   __ jump_to_entry(Interpreter::entry_for_kind(Interpreter::zerolocals));
 782   return entry;
 783 }
 784 
 785 // Not supported
 786 address TemplateInterpreterGenerator::generate_CRC32_update_entry() { return NULL; }
 787 address TemplateInterpreterGenerator::generate_CRC32_updateBytes_entry(AbstractInterpreter::MethodKind kind) { return NULL; }
 788 address TemplateInterpreterGenerator::generate_CRC32C_updateBytes_entry(AbstractInterpreter::MethodKind kind) { return NULL; }
 789 
 790 //
 791 // Interpreter stub for calling a native method. (asm interpreter)
 792 // This sets up a somewhat different looking stack for calling the native method
 793 // than the typical interpreter frame setup.
 794 //
 795 
 796 address TemplateInterpreterGenerator::generate_native_entry(bool synchronized) {
 797   // determine code generation flags
 798   bool inc_counter  = UseCompiler || CountCompiledCalls || LogTouchedMethods;
 799 
 800   // Incoming registers:
 801   //
 802   // Rmethod: Method*
 803   // Rthread: thread
 804   // Rsender_sp: sender sp
 805   // Rparams: parameters
 806 
 807   address entry_point = __ pc();
 808 
 809   // Register allocation
 810   const Register Rsize_of_params = R6;
 811   const Register Rsig_handler    = Rtmp_save0;   // R4
 812   const Register Rnative_code    = Rtmp_save1;   // R5
 813   const Register Rresult_handler = R6;
 814 
 815   const Register Rsaved_result_lo = Rtmp_save0;  // R4
 816   const Register Rsaved_result_hi = Rtmp_save1;  // R5
 817   FloatRegister saved_result_fp;
 818 
 819 
 820   __ ldr(Rsize_of_params, Address(Rmethod, Method::const_offset()));
 821   __ ldrh(Rsize_of_params,  Address(Rsize_of_params, ConstMethod::size_of_parameters_offset()));
 822 
 823   // native calls don't need the stack size check since they have no expression stack
 824   // and the arguments are already on the stack and we only add a handful of words
 825   // to the stack
 826 
 827   // compute beginning of parameters (Rlocals)
 828   __ sub(Rlocals, Rparams, wordSize);
 829   __ add(Rlocals, Rlocals, AsmOperand(Rsize_of_params, lsl, Interpreter::logStackElementSize));
 830 
 831   // reserve stack space for oop_temp
 832   __ mov(R0, 0);
 833   __ push(R0);
 834 
 835   generate_fixed_frame(true); // Note: R9 is now saved in the frame
 836 
 837   // make sure method is native & not abstract
 838 #ifdef ASSERT
 839   __ ldr_u32(Rtemp, Address(Rmethod, Method::access_flags_offset()));
 840   {
 841     Label L;
 842     __ tbnz(Rtemp, JVM_ACC_NATIVE_BIT, L);
 843     __ stop("tried to execute non-native method as native");
 844     __ bind(L);
 845   }
 846   { Label L;
 847     __ tbz(Rtemp, JVM_ACC_ABSTRACT_BIT, L);
 848     __ stop("tried to execute abstract method in interpreter");
 849     __ bind(L);
 850   }
 851 #endif
 852 
 853   // increment invocation count & check for overflow
 854   Label invocation_counter_overflow;
 855   if (inc_counter) {
 856     if (synchronized) {
 857       // Avoid unlocking method's monitor in case of exception, as it has not
 858       // been locked yet.
 859       __ set_do_not_unlock_if_synchronized(true, Rtemp);
 860     }
 861     generate_counter_incr(&invocation_counter_overflow);
 862   }
 863 
 864   Label continue_after_compile;
 865   __ bind(continue_after_compile);
 866 
 867   if (inc_counter && synchronized) {
 868     __ set_do_not_unlock_if_synchronized(false, Rtemp);
 869   }
 870 
 871   // check for synchronized methods
 872   // Must happen AFTER invocation_counter check and stack overflow check,
 873   // so method is not locked if overflows.
 874   //
 875   if (synchronized) {
 876     lock_method();
 877   } else {
 878     // no synchronization necessary
 879 #ifdef ASSERT
 880       { Label L;
 881         __ ldr_u32(Rtemp, Address(Rmethod, Method::access_flags_offset()));
 882         __ tbz(Rtemp, JVM_ACC_SYNCHRONIZED_BIT, L);
 883         __ stop("method needs synchronization");
 884         __ bind(L);
 885       }
 886 #endif
 887   }
 888 
 889   // start execution
 890 #ifdef ASSERT
 891   { Label L;
 892     __ ldr(Rtemp, Address(FP, frame::interpreter_frame_monitor_block_top_offset * wordSize));
 893     __ cmp(Rtemp, Rstack_top);
 894     __ b(L, eq);
 895     __ stop("broken stack frame setup in interpreter");
 896     __ bind(L);
 897   }
 898 #endif
 899   __ check_extended_sp(Rtemp);
 900 
 901   // jvmti/dtrace support
 902   __ notify_method_entry();
 903 #if R9_IS_SCRATCHED
 904   __ restore_method();
 905 #endif
 906 
 907   {
 908     Label L;
 909     __ ldr(Rsig_handler, Address(Rmethod, Method::signature_handler_offset()));
 910     __ cbnz(Rsig_handler, L);
 911     __ mov(R1, Rmethod);
 912     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::prepare_native_call), R1, true);
 913     __ ldr(Rsig_handler, Address(Rmethod, Method::signature_handler_offset()));
 914     __ bind(L);
 915   }
 916 
 917   {
 918     Label L;
 919     __ ldr(Rnative_code, Address(Rmethod, Method::native_function_offset()));
 920     __ cbnz(Rnative_code, L);
 921     __ mov(R1, Rmethod);
 922     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::prepare_native_call), R1);
 923     __ ldr(Rnative_code, Address(Rmethod, Method::native_function_offset()));
 924     __ bind(L);
 925   }
 926 
 927   // Allocate stack space for arguments
 928 
 929 
 930   // C functions need aligned stack
 931   __ bic(SP, SP, StackAlignmentInBytes - 1);
 932   // Multiply by BytesPerLong instead of BytesPerWord, because calling convention
 933   // may require empty slots due to long alignment, e.g. func(int, jlong, int, jlong)
 934   __ sub(SP, SP, AsmOperand(Rsize_of_params, lsl, LogBytesPerLong));
 935 
 936 #ifdef __ABI_HARD__
 937   // Allocate more stack space to accomodate all GP as well as FP registers:
 938   // 4 * wordSize
 939   // 8 * BytesPerLong
 940   int reg_arguments = align_up((4*wordSize) + (8*BytesPerLong), StackAlignmentInBytes);
 941 #else
 942   // Reserve at least 4 words on the stack for loading
 943   // of parameters passed on registers (R0-R3).
 944   // See generate_slow_signature_handler().
 945   // It is also used for JNIEnv & class additional parameters.
 946   int reg_arguments = 4 * wordSize;
 947 #endif // __ABI_HARD__
 948 
 949   __ sub(SP, SP, reg_arguments);
 950 
 951 
 952   // Note: signature handler blows R4 besides all scratch registers.
 953   // See AbstractInterpreterGenerator::generate_slow_signature_handler().
 954   __ call(Rsig_handler);
 955 #if R9_IS_SCRATCHED
 956   __ restore_method();
 957 #endif
 958   __ mov(Rresult_handler, R0);
 959 
 960   // Pass JNIEnv and mirror for static methods
 961   {
 962     Label L;
 963     __ ldr_u32(Rtemp, Address(Rmethod, Method::access_flags_offset()));
 964     __ add(R0, Rthread, in_bytes(JavaThread::jni_environment_offset()));
 965     __ tbz(Rtemp, JVM_ACC_STATIC_BIT, L);
 966     __ load_mirror(Rtemp, Rmethod, Rtemp);
 967     __ add(R1, FP, frame::interpreter_frame_oop_temp_offset * wordSize);
 968     __ str(Rtemp, Address(R1, 0));
 969     __ bind(L);
 970   }
 971 
 972   __ set_last_Java_frame(SP, FP, true, Rtemp);
 973 
 974   // Changing state to _thread_in_native must be the last thing to do
 975   // before the jump to native code. At this moment stack must be
 976   // safepoint-safe and completely prepared for stack walking.
 977 #ifdef ASSERT
 978   {
 979     Label L;
 980     __ ldr_u32(Rtemp, Address(Rthread, JavaThread::thread_state_offset()));
 981     __ cmp_32(Rtemp, _thread_in_Java);
 982     __ b(L, eq);
 983     __ stop("invalid thread state");
 984     __ bind(L);
 985   }
 986 #endif
 987 
 988   // Force all preceding writes to be observed prior to thread state change
 989   __ membar(MacroAssembler::StoreStore, Rtemp);
 990 
 991   __ mov(Rtemp, _thread_in_native);
 992   __ str(Rtemp, Address(Rthread, JavaThread::thread_state_offset()));
 993 
 994   __ call(Rnative_code);
 995 #if R9_IS_SCRATCHED
 996   __ restore_method();
 997 #endif
 998 
 999   // Set FPSCR/FPCR to a known state
1000   if (AlwaysRestoreFPU) {
1001     __ restore_default_fp_mode();
1002   }
1003 
1004   // Do safepoint check
1005   __ mov(Rtemp, _thread_in_native_trans);
1006   __ str_32(Rtemp, Address(Rthread, JavaThread::thread_state_offset()));
1007 
1008     // Force this write out before the read below
1009   __ membar(MacroAssembler::StoreLoad, Rtemp);
1010 
1011   // Protect the return value in the interleaved code: save it to callee-save registers.
1012   __ mov(Rsaved_result_lo, R0);
1013   __ mov(Rsaved_result_hi, R1);
1014 #ifdef __ABI_HARD__
1015   // preserve native FP result in a callee-saved register
1016   saved_result_fp = D8;
1017   __ fcpyd(saved_result_fp, D0);
1018 #else
1019   saved_result_fp = fnoreg;
1020 #endif // __ABI_HARD__
1021 
1022   {
1023   Label call, skip_call;
1024   __ safepoint_poll(Rtemp, call);
1025   __ ldr_u32(R3, Address(Rthread, JavaThread::suspend_flags_offset()));
1026   __ cmp(R3, 0);
1027   __ b(skip_call, eq);
1028   __ bind(call);
1029   __ mov(R0, Rthread);
1030   __ call(CAST_FROM_FN_PTR(address, JavaThread::check_special_condition_for_native_trans), relocInfo::none);
1031   __ bind(skip_call);
1032 
1033 #if R9_IS_SCRATCHED
1034   __ restore_method();
1035 #endif
1036   }
1037 
1038   // Perform Native->Java thread transition
1039   __ mov(Rtemp, _thread_in_Java);
1040   __ str_32(Rtemp, Address(Rthread, JavaThread::thread_state_offset()));
1041 
1042   // Zero handles and last_java_sp
1043   __ reset_last_Java_frame(Rtemp);
1044   __ ldr(R3, Address(Rthread, JavaThread::active_handles_offset()));
1045   __ str_32(__ zero_register(Rtemp), Address(R3, JNIHandleBlock::top_offset_in_bytes()));
1046   if (CheckJNICalls) {
1047     __ str(__ zero_register(Rtemp), Address(Rthread, JavaThread::pending_jni_exception_check_fn_offset()));
1048   }
1049 
1050   // Unbox oop result, e.g. JNIHandles::resolve result if it's an oop.
1051   {
1052     Label Lnot_oop;
1053     __ mov_slow(Rtemp, AbstractInterpreter::result_handler(T_OBJECT));
1054     __ cmp(Rtemp, Rresult_handler);
1055     __ b(Lnot_oop, ne);
1056     Register value = Rsaved_result_lo;
1057     __ resolve_jobject(value,   // value
1058                        Rtemp,   // tmp1
1059                        R1_tmp); // tmp2
1060     // Store resolved result in frame for GC visibility.
1061     __ str(value, Address(FP, frame::interpreter_frame_oop_temp_offset * wordSize));
1062     __ bind(Lnot_oop);
1063   }
1064 
1065 
1066   // reguard stack if StackOverflow exception happened while in native.
1067   {
1068     __ ldr_u32(Rtemp, Address(Rthread, JavaThread::stack_guard_state_offset()));
1069     __ cmp_32(Rtemp, StackOverflow::stack_guard_yellow_reserved_disabled);
1070   __ call(CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages), relocInfo::none, eq);
1071 #if R9_IS_SCRATCHED
1072   __ restore_method();
1073 #endif
1074   }
1075 
1076   // check pending exceptions
1077   {
1078     __ ldr(Rtemp, Address(Rthread, Thread::pending_exception_offset()));
1079     __ cmp(Rtemp, 0);
1080     __ mov(Rexception_pc, PC, ne);
1081     __ b(StubRoutines::forward_exception_entry(), ne);
1082   }
1083 
1084   if (synchronized) {
1085     // address of first monitor
1086     __ sub(R0, FP, - (frame::interpreter_frame_monitor_block_bottom_offset - frame::interpreter_frame_monitor_size()) * wordSize);
1087     __ unlock_object(R0);
1088   }
1089 
1090   // jvmti/dtrace support
1091   // Note: This must happen _after_ handling/throwing any exceptions since
1092   //       the exception handler code notifies the runtime of method exits
1093   //       too. If this happens before, method entry/exit notifications are
1094   //       not properly paired (was bug - gri 11/22/99).
1095   __ notify_method_exit(vtos, InterpreterMacroAssembler::NotifyJVMTI, true, Rsaved_result_lo, Rsaved_result_hi, saved_result_fp);
1096 
1097   // Restore the result. Oop result is restored from the stack by the
1098   // result handler.
1099   __ mov(R0, Rsaved_result_lo);
1100   __ mov(R1, Rsaved_result_hi);
1101 
1102 #ifdef __ABI_HARD__
1103   // reload native FP result
1104   __ fcpyd(D0, D8);
1105 #endif // __ABI_HARD__
1106 
1107   __ blx(Rresult_handler);
1108 
1109   // Restore FP/LR, sender_sp and return
1110   __ mov(Rtemp, FP);
1111   __ ldmia(FP, RegisterSet(FP) | RegisterSet(LR));
1112   __ ldr(SP, Address(Rtemp, frame::interpreter_frame_sender_sp_offset * wordSize));
1113 
1114   __ ret();
1115 
1116   if (inc_counter) {
1117     // Handle overflow of counter and compile method
1118     __ bind(invocation_counter_overflow);
1119     generate_counter_overflow(continue_after_compile);
1120   }
1121 
1122   return entry_point;
1123 }
1124 
1125 //
1126 // Generic interpreted method entry to (asm) interpreter
1127 //
1128 address TemplateInterpreterGenerator::generate_normal_entry(bool synchronized) {
1129   // determine code generation flags
1130   bool inc_counter  = UseCompiler || CountCompiledCalls || LogTouchedMethods;
1131 
1132   // Rmethod: Method*
1133   // Rthread: thread
1134   // Rsender_sp: sender sp (could differ from SP if we were called via c2i)
1135   // Rparams: pointer to the last parameter in the stack
1136 
1137   address entry_point = __ pc();
1138 
1139   const Register RconstMethod = R3;
1140 
1141 
1142   __ ldr(RconstMethod, Address(Rmethod, Method::const_offset()));
1143 
1144   __ ldrh(R2, Address(RconstMethod, ConstMethod::size_of_parameters_offset()));
1145   __ ldrh(R3, Address(RconstMethod, ConstMethod::size_of_locals_offset()));
1146 
1147   // setup Rlocals
1148   __ sub(Rlocals, Rparams, wordSize);
1149   __ add(Rlocals, Rlocals, AsmOperand(R2, lsl, Interpreter::logStackElementSize));
1150 
1151   __ sub(R3, R3, R2); // number of additional locals
1152 
1153 
1154   // see if we've got enough room on the stack for locals plus overhead.
1155   generate_stack_overflow_check();
1156 
1157   // allocate space for locals
1158   // explicitly initialize locals
1159 
1160   // Loop is unrolled 4 times
1161   Label loop;
1162   __ mov(R0, 0);
1163   __ bind(loop);
1164 
1165   // #1
1166   __ subs(R3, R3, 1);
1167   __ push(R0, ge);
1168 
1169   // #2
1170   __ subs(R3, R3, 1, ge);
1171   __ push(R0, ge);
1172 
1173   // #3
1174   __ subs(R3, R3, 1, ge);
1175   __ push(R0, ge);
1176 
1177   // #4
1178   __ subs(R3, R3, 1, ge);
1179   __ push(R0, ge);
1180 
1181   __ b(loop, gt);
1182 
1183   // initialize fixed part of activation frame
1184   generate_fixed_frame(false);
1185 
1186   __ restore_dispatch();
1187 
1188   // make sure method is not native & not abstract
1189 #ifdef ASSERT
1190   __ ldr_u32(Rtemp, Address(Rmethod, Method::access_flags_offset()));
1191   {
1192     Label L;
1193     __ tbz(Rtemp, JVM_ACC_NATIVE_BIT, L);
1194     __ stop("tried to execute native method as non-native");
1195     __ bind(L);
1196   }
1197   { Label L;
1198     __ tbz(Rtemp, JVM_ACC_ABSTRACT_BIT, L);
1199     __ stop("tried to execute abstract method in interpreter");
1200     __ bind(L);
1201   }
1202 #endif
1203 
1204   // increment invocation count & check for overflow
1205   Label invocation_counter_overflow;
1206   if (inc_counter) {
1207     if (synchronized) {
1208       // Avoid unlocking method's monitor in case of exception, as it has not
1209       // been locked yet.
1210       __ set_do_not_unlock_if_synchronized(true, Rtemp);
1211     }
1212     generate_counter_incr(&invocation_counter_overflow);
1213   }
1214   Label continue_after_compile;
1215   __ bind(continue_after_compile);
1216 
1217   if (inc_counter && synchronized) {
1218     __ set_do_not_unlock_if_synchronized(false, Rtemp);
1219   }
1220 #if R9_IS_SCRATCHED
1221   __ restore_method();
1222 #endif
1223 
1224   // check for synchronized methods
1225   // Must happen AFTER invocation_counter check and stack overflow check,
1226   // so method is not locked if overflows.
1227   //
1228   if (synchronized) {
1229     // Allocate monitor and lock method
1230     lock_method();
1231   } else {
1232     // no synchronization necessary
1233 #ifdef ASSERT
1234       { Label L;
1235         __ ldr_u32(Rtemp, Address(Rmethod, Method::access_flags_offset()));
1236         __ tbz(Rtemp, JVM_ACC_SYNCHRONIZED_BIT, L);
1237         __ stop("method needs synchronization");
1238         __ bind(L);
1239       }
1240 #endif
1241   }
1242 
1243   // start execution
1244 #ifdef ASSERT
1245   { Label L;
1246     __ ldr(Rtemp, Address(FP, frame::interpreter_frame_monitor_block_top_offset * wordSize));
1247     __ cmp(Rtemp, Rstack_top);
1248     __ b(L, eq);
1249     __ stop("broken stack frame setup in interpreter");
1250     __ bind(L);
1251   }
1252 #endif
1253   __ check_extended_sp(Rtemp);
1254 
1255   // jvmti support
1256   __ notify_method_entry();
1257 #if R9_IS_SCRATCHED
1258   __ restore_method();
1259 #endif
1260 
1261   __ dispatch_next(vtos);
1262 
1263   // invocation counter overflow
1264   if (inc_counter) {
1265     // Handle overflow of counter and compile method
1266     __ bind(invocation_counter_overflow);
1267     generate_counter_overflow(continue_after_compile);
1268   }
1269 
1270   return entry_point;
1271 }
1272 
1273 //------------------------------------------------------------------------------------------------------------------------
1274 // Exceptions
1275 
1276 void TemplateInterpreterGenerator::generate_throw_exception() {
1277   // Entry point in previous activation (i.e., if the caller was interpreted)
1278   Interpreter::_rethrow_exception_entry = __ pc();
1279   // Rexception_obj: exception
1280 
1281   // Clear interpreter_frame_last_sp.
1282   __ mov(Rtemp, 0);
1283   __ str(Rtemp, Address(FP, frame::interpreter_frame_last_sp_offset * wordSize));
1284 
1285 #if R9_IS_SCRATCHED
1286   __ restore_method();
1287 #endif
1288   __ restore_bcp();
1289   __ restore_dispatch();
1290   __ restore_locals();
1291 
1292 
1293   // Entry point for exceptions thrown within interpreter code
1294   Interpreter::_throw_exception_entry = __ pc();
1295 
1296   // expression stack is undefined here
1297   // Rexception_obj: exception
1298   // Rbcp: exception bcp
1299   __ verify_oop(Rexception_obj);
1300 
1301   // expression stack must be empty before entering the VM in case of an exception
1302   __ empty_expression_stack();
1303   // find exception handler address and preserve exception oop
1304   __ mov(R1, Rexception_obj);
1305   __ call_VM(Rexception_obj, CAST_FROM_FN_PTR(address, InterpreterRuntime::exception_handler_for_exception), R1);
1306   // R0: exception handler entry point
1307   // Rexception_obj: preserved exception oop
1308   // Rbcp: bcp for exception handler
1309   __ push_ptr(Rexception_obj);                    // push exception which is now the only value on the stack
1310   __ jump(R0);                                    // jump to exception handler (may be _remove_activation_entry!)
1311 
1312   // If the exception is not handled in the current frame the frame is removed and
1313   // the exception is rethrown (i.e. exception continuation is _rethrow_exception).
1314   //
1315   // Note: At this point the bci is still the bxi for the instruction which caused
1316   //       the exception and the expression stack is empty. Thus, for any VM calls
1317   //       at this point, GC will find a legal oop map (with empty expression stack).
1318 
1319   // In current activation
1320   // tos: exception
1321   // Rbcp: exception bcp
1322 
1323   //
1324   // JVMTI PopFrame support
1325   //
1326    Interpreter::_remove_activation_preserving_args_entry = __ pc();
1327 
1328 
1329   __ empty_expression_stack();
1330 
1331   // Set the popframe_processing bit in _popframe_condition indicating that we are
1332   // currently handling popframe, so that call_VMs that may happen later do not trigger new
1333   // popframe handling cycles.
1334 
1335   __ ldr_s32(Rtemp, Address(Rthread, JavaThread::popframe_condition_offset()));
1336   __ orr(Rtemp, Rtemp, (unsigned)JavaThread::popframe_processing_bit);
1337   __ str_32(Rtemp, Address(Rthread, JavaThread::popframe_condition_offset()));
1338 
1339   {
1340     // Check to see whether we are returning to a deoptimized frame.
1341     // (The PopFrame call ensures that the caller of the popped frame is
1342     // either interpreted or compiled and deoptimizes it if compiled.)
1343     // In this case, we can't call dispatch_next() after the frame is
1344     // popped, but instead must save the incoming arguments and restore
1345     // them after deoptimization has occurred.
1346     //
1347     // Note that we don't compare the return PC against the
1348     // deoptimization blob's unpack entry because of the presence of
1349     // adapter frames in C2.
1350     Label caller_not_deoptimized;
1351     __ ldr(R0, Address(FP, frame::return_addr_offset * wordSize));
1352     __ call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::interpreter_contains), R0);
1353     __ cbnz_32(R0, caller_not_deoptimized);
1354 
1355     // Compute size of arguments for saving when returning to deoptimized caller
1356     __ restore_method();
1357     __ ldr(R0, Address(Rmethod, Method::const_offset()));
1358     __ ldrh(R0, Address(R0, ConstMethod::size_of_parameters_offset()));
1359 
1360     __ logical_shift_left(R1, R0, Interpreter::logStackElementSize);
1361     // Save these arguments
1362     __ restore_locals();
1363     __ sub(R2, Rlocals, R1);
1364     __ add(R2, R2, wordSize);
1365     __ mov(R0, Rthread);
1366     __ call_VM_leaf(CAST_FROM_FN_PTR(address, Deoptimization::popframe_preserve_args), R0, R1, R2);
1367 
1368     __ remove_activation(vtos, LR,
1369                          /* throw_monitor_exception */ false,
1370                          /* install_monitor_exception */ false,
1371                          /* notify_jvmdi */ false);
1372 
1373     // Inform deoptimization that it is responsible for restoring these arguments
1374     __ mov(Rtemp, JavaThread::popframe_force_deopt_reexecution_bit);
1375     __ str_32(Rtemp, Address(Rthread, JavaThread::popframe_condition_offset()));
1376 
1377     // Continue in deoptimization handler
1378     __ ret();
1379 
1380     __ bind(caller_not_deoptimized);
1381   }
1382 
1383   __ remove_activation(vtos, R4,
1384                        /* throw_monitor_exception */ false,
1385                        /* install_monitor_exception */ false,
1386                        /* notify_jvmdi */ false);
1387 
1388   // Finish with popframe handling
1389   // A previous I2C followed by a deoptimization might have moved the
1390   // outgoing arguments further up the stack. PopFrame expects the
1391   // mutations to those outgoing arguments to be preserved and other
1392   // constraints basically require this frame to look exactly as
1393   // though it had previously invoked an interpreted activation with
1394   // no space between the top of the expression stack (current
1395   // last_sp) and the top of stack. Rather than force deopt to
1396   // maintain this kind of invariant all the time we call a small
1397   // fixup routine to move the mutated arguments onto the top of our
1398   // expression stack if necessary.
1399   __ mov(R1, SP);
1400   __ ldr(R2, Address(FP, frame::interpreter_frame_last_sp_offset * wordSize));
1401   // PC must point into interpreter here
1402   __ set_last_Java_frame(SP, FP, true, Rtemp);
1403   __ mov(R0, Rthread);
1404   __ call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::popframe_move_outgoing_args), R0, R1, R2);
1405   __ reset_last_Java_frame(Rtemp);
1406 
1407   // Restore the last_sp and null it out
1408   __ ldr(SP, Address(FP, frame::interpreter_frame_last_sp_offset * wordSize));
1409   __ mov(Rtemp, (int)NULL_WORD);
1410   __ str(Rtemp, Address(FP, frame::interpreter_frame_last_sp_offset * wordSize));
1411 
1412   __ restore_bcp();
1413   __ restore_dispatch();
1414   __ restore_locals();
1415   __ restore_method();
1416 
1417   // The method data pointer was incremented already during
1418   // call profiling. We have to restore the mdp for the current bcp.
1419   if (ProfileInterpreter) {
1420     __ set_method_data_pointer_for_bcp();
1421   }
1422 
1423   // Clear the popframe condition flag
1424   assert(JavaThread::popframe_inactive == 0, "adjust this code");
1425   __ str_32(__ zero_register(Rtemp), Address(Rthread, JavaThread::popframe_condition_offset()));
1426 
1427 #if INCLUDE_JVMTI
1428   {
1429     Label L_done;
1430 
1431     __ ldrb(Rtemp, Address(Rbcp, 0));
1432     __ cmp(Rtemp, Bytecodes::_invokestatic);
1433     __ b(L_done, ne);
1434 
1435     // The member name argument must be restored if _invokestatic is re-executed after a PopFrame call.
1436     // Detect such a case in the InterpreterRuntime function and return the member name argument, or NULL.
1437 
1438     // get local0
1439     __ ldr(R1, Address(Rlocals, 0));
1440     __ mov(R2, Rmethod);
1441     __ mov(R3, Rbcp);
1442     __ call_VM(R0, CAST_FROM_FN_PTR(address, InterpreterRuntime::member_name_arg_or_null), R1, R2, R3);
1443 
1444     __ cbz(R0, L_done);
1445 
1446     __ str(R0, Address(Rstack_top));
1447     __ bind(L_done);
1448   }
1449 #endif // INCLUDE_JVMTI
1450 
1451   __ dispatch_next(vtos);
1452   // end of PopFrame support
1453 
1454   Interpreter::_remove_activation_entry = __ pc();
1455 
1456   // preserve exception over this code sequence
1457   __ pop_ptr(R0_tos);
1458   __ str(R0_tos, Address(Rthread, JavaThread::vm_result_offset()));
1459   // remove the activation (without doing throws on illegalMonitorExceptions)
1460   __ remove_activation(vtos, Rexception_pc, false, true, false);
1461   // restore exception
1462   __ get_vm_result(Rexception_obj, Rtemp);
1463 
1464   // Inbetween activations - previous activation type unknown yet
1465   // compute continuation point - the continuation point expects
1466   // the following registers set up:
1467   //
1468   // Rexception_obj: exception
1469   // Rexception_pc: return address/pc that threw exception
1470   // SP: expression stack of caller
1471   // FP: frame pointer of caller
1472   __ mov(c_rarg0, Rthread);
1473   __ mov(c_rarg1, Rexception_pc);
1474   __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address), c_rarg0, c_rarg1);
1475   // Note that an "issuing PC" is actually the next PC after the call
1476 
1477   __ jump(R0);                             // jump to exception handler of caller
1478 }
1479 
1480 
1481 //
1482 // JVMTI ForceEarlyReturn support
1483 //
1484 address TemplateInterpreterGenerator::generate_earlyret_entry_for(TosState state) {
1485   address entry = __ pc();
1486 
1487 
1488   __ restore_bcp();
1489   __ restore_dispatch();
1490   __ restore_locals();
1491 
1492   __ empty_expression_stack();
1493 
1494   __ load_earlyret_value(state);
1495 
1496   // Clear the earlyret state
1497   __ ldr(Rtemp, Address(Rthread, JavaThread::jvmti_thread_state_offset()));
1498 
1499   assert(JvmtiThreadState::earlyret_inactive == 0, "adjust this code");
1500   __ str_32(__ zero_register(R2), Address(Rtemp, JvmtiThreadState::earlyret_state_offset()));
1501 
1502   __ remove_activation(state, LR,
1503                        false, /* throw_monitor_exception */
1504                        false, /* install_monitor_exception */
1505                        true); /* notify_jvmdi */
1506 
1507   // According to interpreter calling conventions, result is returned in R0/R1,
1508   // so ftos (S0) and dtos (D0) are moved to R0/R1.
1509   // This conversion should be done after remove_activation, as it uses
1510   // push(state) & pop(state) to preserve return value.
1511   __ convert_tos_to_retval(state);
1512   __ ret();
1513 
1514   return entry;
1515 } // end of ForceEarlyReturn support
1516 
1517 
1518 //------------------------------------------------------------------------------------------------------------------------
1519 // Helper for vtos entry point generation
1520 
1521 void TemplateInterpreterGenerator::set_vtos_entry_points (Template* t, address& bep, address& cep, address& sep, address& aep, address& iep, address& lep, address& fep, address& dep, address& vep) {
1522   assert(t->is_valid() && t->tos_in() == vtos, "illegal template");
1523   Label L;
1524 
1525 #ifdef __SOFTFP__
1526   dep = __ pc();                // fall through
1527 #else
1528   fep = __ pc(); __ push(ftos); __ b(L);
1529   dep = __ pc(); __ push(dtos); __ b(L);
1530 #endif // __SOFTFP__
1531 
1532   lep = __ pc(); __ push(ltos); __ b(L);
1533 
1534   if (VerifyOops) {  // can't share atos entry if VerifyOops
1535     aep = __ pc(); __ push(atos); __ b(L);
1536   } else {
1537     aep = __ pc();              // fall through
1538   }
1539 
1540 #ifdef __SOFTFP__
1541   fep = __ pc();                // fall through
1542 #endif // __SOFTFP__
1543 
1544   bep = cep = sep =             // fall through
1545   iep = __ pc(); __ push(itos); // fall through
1546   vep = __ pc(); __ bind(L);    // fall through
1547   generate_and_dispatch(t);
1548 }
1549 
1550 //------------------------------------------------------------------------------------------------------------------------
1551 
1552 // Non-product code
1553 #ifndef PRODUCT
1554 address TemplateInterpreterGenerator::generate_trace_code(TosState state) {
1555   address entry = __ pc();
1556 
1557   // prepare expression stack
1558   __ push(state);       // save tosca
1559 
1560   // pass tosca registers as arguments
1561   __ mov(R2, R0_tos);
1562   __ mov(R3, R1_tos_hi);
1563   __ mov(R1, LR);       // save return address
1564 
1565   // call tracer
1566   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::trace_bytecode), R1, R2, R3);
1567 
1568   __ mov(LR, R0);       // restore return address
1569   __ pop(state);        // restore tosca
1570 
1571   // return
1572   __ ret();
1573 
1574   return entry;
1575 }
1576 
1577 
1578 void TemplateInterpreterGenerator::count_bytecode() {
1579   __ inc_global_counter((address) &BytecodeCounter::_counter_value, 0, Rtemp, R2_tmp, true);
1580 }
1581 
1582 
1583 void TemplateInterpreterGenerator::histogram_bytecode(Template* t) {
1584   __ inc_global_counter((address)&BytecodeHistogram::_counters[0], sizeof(BytecodeHistogram::_counters[0]) * t->bytecode(), Rtemp, R2_tmp, true);
1585 }
1586 
1587 
1588 void TemplateInterpreterGenerator::histogram_bytecode_pair(Template* t) {
1589   const Register Rindex_addr = R2_tmp;
1590   Label Lcontinue;
1591   InlinedAddress Lcounters((address)BytecodePairHistogram::_counters);
1592   InlinedAddress Lindex((address)&BytecodePairHistogram::_index);
1593   const Register Rcounters_addr = R2_tmp;
1594   const Register Rindex = R4_tmp;
1595 
1596   // calculate new index for counter:
1597   // index = (_index >> log2_number_of_codes) | (bytecode << log2_number_of_codes).
1598   // (_index >> log2_number_of_codes) is previous bytecode
1599 
1600   __ ldr_literal(Rindex_addr, Lindex);
1601   __ ldr_s32(Rindex, Address(Rindex_addr));
1602   __ mov_slow(Rtemp, ((int)t->bytecode()) << BytecodePairHistogram::log2_number_of_codes);
1603   __ orr(Rindex, Rtemp, AsmOperand(Rindex, lsr, BytecodePairHistogram::log2_number_of_codes));
1604   __ str_32(Rindex, Address(Rindex_addr));
1605 
1606   // Rindex (R4) contains index of counter
1607 
1608   __ ldr_literal(Rcounters_addr, Lcounters);
1609   __ ldr_s32(Rtemp, Address::indexed_32(Rcounters_addr, Rindex));
1610   __ adds_32(Rtemp, Rtemp, 1);
1611   __ b(Lcontinue, mi);                           // avoid overflow
1612   __ str_32(Rtemp, Address::indexed_32(Rcounters_addr, Rindex));
1613 
1614   __ b(Lcontinue);
1615 
1616   __ bind_literal(Lindex);
1617   __ bind_literal(Lcounters);
1618 
1619   __ bind(Lcontinue);
1620 }
1621 
1622 
1623 void TemplateInterpreterGenerator::trace_bytecode(Template* t) {
1624   // Call a little run-time stub to avoid blow-up for each bytecode.
1625   // The run-time runtime saves the right registers, depending on
1626   // the tosca in-state for the given template.
1627   assert(Interpreter::trace_code(t->tos_in()) != NULL,
1628          "entry must have been generated");
1629   address trace_entry = Interpreter::trace_code(t->tos_in());
1630   __ call(trace_entry, relocInfo::none);
1631 }
1632 
1633 
1634 void TemplateInterpreterGenerator::stop_interpreter_at() {
1635   Label Lcontinue;
1636   const Register stop_at = R2_tmp;
1637 
1638   __ ldr_global_s32(Rtemp, (address) &BytecodeCounter::_counter_value);
1639   __ mov_slow(stop_at, StopInterpreterAt);
1640 
1641   // test bytecode counter
1642   __ cmp(Rtemp, stop_at);
1643   __ b(Lcontinue, ne);
1644 
1645   __ trace_state("stop_interpreter_at");
1646   __ breakpoint();
1647 
1648   __ bind(Lcontinue);
1649 }
1650 #endif // !PRODUCT