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