1 /*
   2  * Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2014, 2020, Red Hat Inc. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "asm/macroAssembler.inline.hpp"
  28 #include "classfile/javaClasses.hpp"
  29 #include "compiler/compiler_globals.hpp"
  30 #include "gc/shared/barrierSetAssembler.hpp"
  31 #include "interpreter/bytecodeHistogram.hpp"
  32 #include "interpreter/interpreter.hpp"
  33 #include "interpreter/interpreterRuntime.hpp"
  34 #include "interpreter/interp_masm.hpp"
  35 #include "interpreter/templateInterpreterGenerator.hpp"
  36 #include "interpreter/templateTable.hpp"
  37 #include "interpreter/bytecodeTracer.hpp"
  38 #include "memory/resourceArea.hpp"
  39 #include "oops/arrayOop.hpp"
  40 #include "oops/methodData.hpp"
  41 #include "oops/method.hpp"
  42 #include "oops/oop.inline.hpp"
  43 #include "oops/inlineKlass.hpp"
  44 #include "prims/jvmtiExport.hpp"
  45 #include "prims/jvmtiThreadState.hpp"
  46 #include "runtime/arguments.hpp"
  47 #include "runtime/deoptimization.hpp"
  48 #include "runtime/frame.inline.hpp"
  49 #include "runtime/jniHandles.hpp"
  50 #include "runtime/sharedRuntime.hpp"
  51 #include "runtime/stubRoutines.hpp"
  52 #include "runtime/synchronizer.hpp"
  53 #include "runtime/timer.hpp"
  54 #include "runtime/vframeArray.hpp"
  55 #include "utilities/debug.hpp"
  56 #include "utilities/powerOfTwo.hpp"
  57 #include <sys/types.h>
  58 
  59 #ifndef PRODUCT
  60 #include "oops/method.hpp"
  61 #endif // !PRODUCT
  62 
  63 // Size of interpreter code.  Increase if too small.  Interpreter will
  64 // fail with a guarantee ("not enough space for interpreter generation");
  65 // if too small.
  66 // Run with +PrintInterpreter to get the VM to print out the size.
  67 // Max size with JVMTI
  68 int TemplateInterpreter::InterpreterCodeSize = 200 * 1024;
  69 
  70 #define __ _masm->
  71 
  72 //-----------------------------------------------------------------------------
  73 
  74 extern "C" void entry(CodeBuffer*);
  75 
  76 //-----------------------------------------------------------------------------
  77 
  78 address TemplateInterpreterGenerator::generate_slow_signature_handler() {
  79   address entry = __ pc();
  80 
  81   __ andr(esp, esp, -16);
  82   __ mov(c_rarg3, esp);
  83   // rmethod
  84   // rlocals
  85   // c_rarg3: first stack arg - wordSize
  86 
  87   // adjust sp
  88   __ sub(sp, c_rarg3, 18 * wordSize);
  89   __ str(lr, Address(__ pre(sp, -2 * wordSize)));
  90   __ call_VM(noreg,
  91              CAST_FROM_FN_PTR(address,
  92                               InterpreterRuntime::slow_signature_handler),
  93              rmethod, rlocals, c_rarg3);
  94 
  95   // r0: result handler
  96 
  97   // Stack layout:
  98   // rsp: return address           <- sp
  99   //      1 garbage
 100   //      8 integer args (if static first is unused)
 101   //      1 float/double identifiers
 102   //      8 double args
 103   //        stack args              <- esp
 104   //        garbage
 105   //        expression stack bottom
 106   //        bcp (NULL)
 107   //        ...
 108 
 109   // Restore LR
 110   __ ldr(lr, Address(__ post(sp, 2 * wordSize)));
 111 
 112   // Do FP first so we can use c_rarg3 as temp
 113   __ ldrw(c_rarg3, Address(sp, 9 * wordSize)); // float/double identifiers
 114 
 115   for (int i = 0; i < Argument::n_float_register_parameters_c; i++) {
 116     const FloatRegister r = as_FloatRegister(i);
 117 
 118     Label d, done;
 119 
 120     __ tbnz(c_rarg3, i, d);
 121     __ ldrs(r, Address(sp, (10 + i) * wordSize));
 122     __ b(done);
 123     __ bind(d);
 124     __ ldrd(r, Address(sp, (10 + i) * wordSize));
 125     __ bind(done);
 126   }
 127 
 128   // c_rarg0 contains the result from the call of
 129   // InterpreterRuntime::slow_signature_handler so we don't touch it
 130   // here.  It will be loaded with the JNIEnv* later.
 131   __ ldr(c_rarg1, Address(sp, 1 * wordSize));
 132   for (int i = c_rarg2->encoding(); i <= c_rarg7->encoding(); i += 2) {
 133     Register rm = as_Register(i), rn = as_Register(i+1);
 134     __ ldp(rm, rn, Address(sp, i * wordSize));
 135   }
 136 
 137   __ add(sp, sp, 18 * wordSize);
 138   __ ret(lr);
 139 
 140   return entry;
 141 }
 142 
 143 
 144 //
 145 // Various method entries
 146 //
 147 
 148 address TemplateInterpreterGenerator::generate_math_entry(AbstractInterpreter::MethodKind kind) {
 149   // rmethod: Method*
 150   // r13: sender sp
 151   // esp: args
 152 
 153   if (!InlineIntrinsics) return NULL; // Generate a vanilla entry
 154 
 155   // These don't need a safepoint check because they aren't virtually
 156   // callable. We won't enter these intrinsics from compiled code.
 157   // If in the future we added an intrinsic which was virtually callable
 158   // we'd have to worry about how to safepoint so that this code is used.
 159 
 160   // mathematical functions inlined by compiler
 161   // (interpreter must provide identical implementation
 162   // in order to avoid monotonicity bugs when switching
 163   // from interpreter to compiler in the middle of some
 164   // computation)
 165   //
 166   // stack:
 167   //        [ arg ] <-- esp
 168   //        [ arg ]
 169   // retaddr in lr
 170 
 171   address entry_point = NULL;
 172   Register continuation = lr;
 173   switch (kind) {
 174   case Interpreter::java_lang_math_abs:
 175     entry_point = __ pc();
 176     __ ldrd(v0, Address(esp));
 177     __ fabsd(v0, v0);
 178     __ mov(sp, r13); // Restore caller's SP
 179     break;
 180   case Interpreter::java_lang_math_sqrt:
 181     entry_point = __ pc();
 182     __ ldrd(v0, Address(esp));
 183     __ fsqrtd(v0, v0);
 184     __ mov(sp, r13);
 185     break;
 186   case Interpreter::java_lang_math_sin :
 187   case Interpreter::java_lang_math_cos :
 188   case Interpreter::java_lang_math_tan :
 189   case Interpreter::java_lang_math_log :
 190   case Interpreter::java_lang_math_log10 :
 191   case Interpreter::java_lang_math_exp :
 192     entry_point = __ pc();
 193     __ ldrd(v0, Address(esp));
 194     __ mov(sp, r13);
 195     __ mov(r19, lr);
 196     continuation = r19;  // The first callee-saved register
 197     generate_transcendental_entry(kind, 1);
 198     break;
 199   case Interpreter::java_lang_math_pow :
 200     entry_point = __ pc();
 201     __ mov(r19, lr);
 202     continuation = r19;
 203     __ ldrd(v0, Address(esp, 2 * Interpreter::stackElementSize));
 204     __ ldrd(v1, Address(esp));
 205     __ mov(sp, r13);
 206     generate_transcendental_entry(kind, 2);
 207     break;
 208   case Interpreter::java_lang_math_fmaD :
 209     if (UseFMA) {
 210       entry_point = __ pc();
 211       __ ldrd(v0, Address(esp, 4 * Interpreter::stackElementSize));
 212       __ ldrd(v1, Address(esp, 2 * Interpreter::stackElementSize));
 213       __ ldrd(v2, Address(esp));
 214       __ fmaddd(v0, v0, v1, v2);
 215       __ mov(sp, r13); // Restore caller's SP
 216     }
 217     break;
 218   case Interpreter::java_lang_math_fmaF :
 219     if (UseFMA) {
 220       entry_point = __ pc();
 221       __ ldrs(v0, Address(esp, 2 * Interpreter::stackElementSize));
 222       __ ldrs(v1, Address(esp, Interpreter::stackElementSize));
 223       __ ldrs(v2, Address(esp));
 224       __ fmadds(v0, v0, v1, v2);
 225       __ mov(sp, r13); // Restore caller's SP
 226     }
 227     break;
 228   default:
 229     ;
 230   }
 231   if (entry_point) {
 232     __ br(continuation);
 233   }
 234 
 235   return entry_point;
 236 }
 237 
 238   // double trigonometrics and transcendentals
 239   // static jdouble dsin(jdouble x);
 240   // static jdouble dcos(jdouble x);
 241   // static jdouble dtan(jdouble x);
 242   // static jdouble dlog(jdouble x);
 243   // static jdouble dlog10(jdouble x);
 244   // static jdouble dexp(jdouble x);
 245   // static jdouble dpow(jdouble x, jdouble y);
 246 
 247 void TemplateInterpreterGenerator::generate_transcendental_entry(AbstractInterpreter::MethodKind kind, int fpargs) {
 248   address fn;
 249   switch (kind) {
 250   case Interpreter::java_lang_math_sin :
 251     if (StubRoutines::dsin() == NULL) {
 252       fn = CAST_FROM_FN_PTR(address, SharedRuntime::dsin);
 253     } else {
 254       fn = CAST_FROM_FN_PTR(address, StubRoutines::dsin());
 255     }
 256     break;
 257   case Interpreter::java_lang_math_cos :
 258     if (StubRoutines::dcos() == NULL) {
 259       fn = CAST_FROM_FN_PTR(address, SharedRuntime::dcos);
 260     } else {
 261       fn = CAST_FROM_FN_PTR(address, StubRoutines::dcos());
 262     }
 263     break;
 264   case Interpreter::java_lang_math_tan :
 265     if (StubRoutines::dtan() == NULL) {
 266       fn = CAST_FROM_FN_PTR(address, SharedRuntime::dtan);
 267     } else {
 268       fn = CAST_FROM_FN_PTR(address, StubRoutines::dtan());
 269     }
 270     break;
 271   case Interpreter::java_lang_math_log :
 272     if (StubRoutines::dlog() == NULL) {
 273       fn = CAST_FROM_FN_PTR(address, SharedRuntime::dlog);
 274     } else {
 275       fn = CAST_FROM_FN_PTR(address, StubRoutines::dlog());
 276     }
 277     break;
 278   case Interpreter::java_lang_math_log10 :
 279     if (StubRoutines::dlog10() == NULL) {
 280       fn = CAST_FROM_FN_PTR(address, SharedRuntime::dlog10);
 281     } else {
 282       fn = CAST_FROM_FN_PTR(address, StubRoutines::dlog10());
 283     }
 284     break;
 285   case Interpreter::java_lang_math_exp :
 286     if (StubRoutines::dexp() == NULL) {
 287       fn = CAST_FROM_FN_PTR(address, SharedRuntime::dexp);
 288     } else {
 289       fn = CAST_FROM_FN_PTR(address, StubRoutines::dexp());
 290     }
 291     break;
 292   case Interpreter::java_lang_math_pow :
 293     if (StubRoutines::dpow() == NULL) {
 294       fn = CAST_FROM_FN_PTR(address, SharedRuntime::dpow);
 295     } else {
 296       fn = CAST_FROM_FN_PTR(address, StubRoutines::dpow());
 297     }
 298     break;
 299   default:
 300     ShouldNotReachHere();
 301     fn = NULL;  // unreachable
 302   }
 303   __ mov(rscratch1, fn);
 304   __ blr(rscratch1);
 305 }
 306 
 307 // Abstract method entry
 308 // Attempt to execute abstract method. Throw exception
 309 address TemplateInterpreterGenerator::generate_abstract_entry(void) {
 310   // rmethod: Method*
 311   // r13: sender SP
 312 
 313   address entry_point = __ pc();
 314 
 315   // abstract method entry
 316 
 317   //  pop return address, reset last_sp to NULL
 318   __ empty_expression_stack();
 319   __ restore_bcp();      // bcp must be correct for exception handler   (was destroyed)
 320   __ restore_locals();   // make sure locals pointer is correct as well (was destroyed)
 321 
 322   // throw exception
 323   __ call_VM(noreg, CAST_FROM_FN_PTR(address,
 324                                      InterpreterRuntime::throw_AbstractMethodErrorWithMethod),
 325                                      rmethod);
 326   // the call_VM checks for exception, so we should never return here.
 327   __ should_not_reach_here();
 328 
 329   return entry_point;
 330 }
 331 
 332 address TemplateInterpreterGenerator::generate_StackOverflowError_handler() {
 333   address entry = __ pc();
 334 
 335 #ifdef ASSERT
 336   {
 337     Label L;
 338     __ ldr(rscratch1, Address(rfp,
 339                        frame::interpreter_frame_monitor_block_top_offset *
 340                        wordSize));
 341     __ mov(rscratch2, sp);
 342     __ cmp(rscratch1, rscratch2); // maximal rsp for current rfp (stack
 343                            // grows negative)
 344     __ br(Assembler::HS, L); // check if frame is complete
 345     __ stop ("interpreter frame not set up");
 346     __ bind(L);
 347   }
 348 #endif // ASSERT
 349   // Restore bcp under the assumption that the current frame is still
 350   // interpreted
 351   __ restore_bcp();
 352 
 353   // expression stack must be empty before entering the VM if an
 354   // exception happened
 355   __ empty_expression_stack();
 356   // throw exception
 357   __ call_VM(noreg,
 358              CAST_FROM_FN_PTR(address,
 359                               InterpreterRuntime::throw_StackOverflowError));
 360   return entry;
 361 }
 362 
 363 address TemplateInterpreterGenerator::generate_ArrayIndexOutOfBounds_handler() {
 364   address entry = __ pc();
 365   // expression stack must be empty before entering the VM if an
 366   // exception happened
 367   __ empty_expression_stack();
 368   // setup parameters
 369 
 370   // ??? convention: expect aberrant index in register r1
 371   __ movw(c_rarg2, r1);
 372   // ??? convention: expect array in register r3
 373   __ mov(c_rarg1, r3);
 374   __ call_VM(noreg,
 375              CAST_FROM_FN_PTR(address,
 376                               InterpreterRuntime::
 377                               throw_ArrayIndexOutOfBoundsException),
 378              c_rarg1, c_rarg2);
 379   return entry;
 380 }
 381 
 382 address TemplateInterpreterGenerator::generate_ClassCastException_handler() {
 383   address entry = __ pc();
 384 
 385   // object is at TOS
 386   __ pop(c_rarg1);
 387 
 388   // expression stack must be empty before entering the VM if an
 389   // exception happened
 390   __ empty_expression_stack();
 391 
 392   __ call_VM(noreg,
 393              CAST_FROM_FN_PTR(address,
 394                               InterpreterRuntime::
 395                               throw_ClassCastException),
 396              c_rarg1);
 397   return entry;
 398 }
 399 
 400 address TemplateInterpreterGenerator::generate_exception_handler_common(
 401         const char* name, const char* message, bool pass_oop) {
 402   assert(!pass_oop || message == NULL, "either oop or message but not both");
 403   address entry = __ pc();
 404   if (pass_oop) {
 405     // object is at TOS
 406     __ pop(c_rarg2);
 407   }
 408   // expression stack must be empty before entering the VM if an
 409   // exception happened
 410   __ empty_expression_stack();
 411   // setup parameters
 412   __ lea(c_rarg1, Address((address)name));
 413   if (pass_oop) {
 414     __ call_VM(r0, CAST_FROM_FN_PTR(address,
 415                                     InterpreterRuntime::
 416                                     create_klass_exception),
 417                c_rarg1, c_rarg2);
 418   } else {
 419     // kind of lame ExternalAddress can't take NULL because
 420     // external_word_Relocation will assert.
 421     if (message != NULL) {
 422       __ lea(c_rarg2, Address((address)message));
 423     } else {
 424       __ mov(c_rarg2, NULL_WORD);
 425     }
 426     __ call_VM(r0,
 427                CAST_FROM_FN_PTR(address, InterpreterRuntime::create_exception),
 428                c_rarg1, c_rarg2);
 429   }
 430   // throw exception
 431   __ b(address(Interpreter::throw_exception_entry()));
 432   return entry;
 433 }
 434 
 435 address TemplateInterpreterGenerator::generate_return_entry_for(TosState state, int step, size_t index_size) {
 436   address entry = __ pc();
 437 
 438   // Restore stack bottom in case i2c adjusted stack
 439   __ ldr(esp, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
 440   // and NULL it as marker that esp is now tos until next java call
 441   __ str(zr, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
 442 
 443   if (state == atos && InlineTypeReturnedAsFields) {
 444     __ store_inline_type_fields_to_buf(NULL, true);
 445   }
 446 
 447   __ restore_bcp();
 448   __ restore_locals();
 449   __ restore_constant_pool_cache();
 450   __ get_method(rmethod);
 451 
 452   if (state == atos) {
 453     Register obj = r0;
 454     Register mdp = r1;
 455     Register tmp = r2;
 456     __ profile_return_type(mdp, obj, tmp);
 457   }
 458 
 459   // Pop N words from the stack
 460   __ get_cache_and_index_at_bcp(r1, r2, 1, index_size);
 461   __ ldr(r1, Address(r1, ConstantPoolCache::base_offset() + ConstantPoolCacheEntry::flags_offset()));
 462   __ andr(r1, r1, ConstantPoolCacheEntry::parameter_size_mask);
 463 
 464   __ add(esp, esp, r1, Assembler::LSL, 3);
 465 
 466   // Restore machine SP
 467   __ ldr(rscratch1, Address(rmethod, Method::const_offset()));
 468   __ ldrh(rscratch1, Address(rscratch1, ConstMethod::max_stack_offset()));
 469   __ add(rscratch1, rscratch1, frame::interpreter_frame_monitor_size() + 2);
 470   __ ldr(rscratch2,
 471          Address(rfp, frame::interpreter_frame_initial_sp_offset * wordSize));
 472   __ sub(rscratch1, rscratch2, rscratch1, ext::uxtw, 3);
 473   __ andr(sp, rscratch1, -16);
 474 
 475  __ check_and_handle_popframe(rthread);
 476  __ check_and_handle_earlyret(rthread);
 477 
 478   __ get_dispatch();
 479   __ dispatch_next(state, step);
 480 
 481   return entry;
 482 }
 483 
 484 address TemplateInterpreterGenerator::generate_deopt_entry_for(TosState state,
 485                                                                int step,
 486                                                                address continuation) {
 487   address entry = __ pc();
 488   __ restore_bcp();
 489   __ restore_locals();
 490   __ restore_constant_pool_cache();
 491   __ get_method(rmethod);
 492   __ get_dispatch();
 493 
 494   // Calculate stack limit
 495   __ ldr(rscratch1, Address(rmethod, Method::const_offset()));
 496   __ ldrh(rscratch1, Address(rscratch1, ConstMethod::max_stack_offset()));
 497   __ add(rscratch1, rscratch1, frame::interpreter_frame_monitor_size() + 2);
 498   __ ldr(rscratch2,
 499          Address(rfp, frame::interpreter_frame_initial_sp_offset * wordSize));
 500   __ sub(rscratch1, rscratch2, rscratch1, ext::uxtx, 3);
 501   __ andr(sp, rscratch1, -16);
 502 
 503   // Restore expression stack pointer
 504   __ ldr(esp, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
 505   // NULL last_sp until next java call
 506   __ str(zr, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
 507 
 508 #if INCLUDE_JVMCI
 509   // Check if we need to take lock at entry of synchronized method.  This can
 510   // only occur on method entry so emit it only for vtos with step 0.
 511   if (EnableJVMCI && state == vtos && step == 0) {
 512     Label L;
 513     __ ldrb(rscratch1, Address(rthread, JavaThread::pending_monitorenter_offset()));
 514     __ cbz(rscratch1, L);
 515     // Clear flag.
 516     __ strb(zr, Address(rthread, JavaThread::pending_monitorenter_offset()));
 517     // Take lock.
 518     lock_method();
 519     __ bind(L);
 520   } else {
 521 #ifdef ASSERT
 522     if (EnableJVMCI) {
 523       Label L;
 524       __ ldrb(rscratch1, Address(rthread, JavaThread::pending_monitorenter_offset()));
 525       __ cbz(rscratch1, L);
 526       __ stop("unexpected pending monitor in deopt entry");
 527       __ bind(L);
 528     }
 529 #endif
 530   }
 531 #endif
 532   // handle exceptions
 533   {
 534     Label L;
 535     __ ldr(rscratch1, Address(rthread, Thread::pending_exception_offset()));
 536     __ cbz(rscratch1, L);
 537     __ call_VM(noreg,
 538                CAST_FROM_FN_PTR(address,
 539                                 InterpreterRuntime::throw_pending_exception));
 540     __ should_not_reach_here();
 541     __ bind(L);
 542   }
 543 
 544   if (continuation == NULL) {
 545     __ dispatch_next(state, step);
 546   } else {
 547     __ jump_to_entry(continuation);
 548   }
 549   return entry;
 550 }
 551 
 552 address TemplateInterpreterGenerator::generate_result_handler_for(
 553         BasicType type) {
 554     address entry = __ pc();
 555   switch (type) {
 556   case T_BOOLEAN: __ c2bool(r0);         break;
 557   case T_CHAR   : __ uxth(r0, r0);       break;
 558   case T_BYTE   : __ sxtb(r0, r0);        break;
 559   case T_SHORT  : __ sxth(r0, r0);        break;
 560   case T_INT    : __ uxtw(r0, r0);        break;  // FIXME: We almost certainly don't need this
 561   case T_LONG   : /* nothing to do */        break;
 562   case T_VOID   : /* nothing to do */        break;
 563   case T_FLOAT  : /* nothing to do */        break;
 564   case T_DOUBLE : /* nothing to do */        break;
 565   case T_PRIMITIVE_OBJECT: // fall through (value types are handled with oops)
 566   case T_OBJECT :
 567     // retrieve result from frame
 568     __ ldr(r0, Address(rfp, frame::interpreter_frame_oop_temp_offset*wordSize));
 569     // and verify it
 570     __ verify_oop(r0);
 571     break;
 572   default       : ShouldNotReachHere();
 573   }
 574   __ ret(lr);                                  // return from result handler
 575   return entry;
 576 }
 577 
 578 address TemplateInterpreterGenerator::generate_safept_entry_for(
 579         TosState state,
 580         address runtime_entry) {
 581   address entry = __ pc();
 582   __ push(state);
 583   __ call_VM(noreg, runtime_entry);
 584   __ membar(Assembler::AnyAny);
 585   __ dispatch_via(vtos, Interpreter::_normal_table.table_for(vtos));
 586   return entry;
 587 }
 588 
 589 // Helpers for commoning out cases in the various type of method entries.
 590 //
 591 
 592 
 593 // increment invocation count & check for overflow
 594 //
 595 // Note: checking for negative value instead of overflow
 596 //       so we have a 'sticky' overflow test
 597 //
 598 // rmethod: method
 599 //
 600 void TemplateInterpreterGenerator::generate_counter_incr(Label* overflow) {
 601   Label done;
 602   // Note: In tiered we increment either counters in Method* or in MDO depending if we're profiling or not.
 603   int increment = InvocationCounter::count_increment;
 604   Label no_mdo;
 605   if (ProfileInterpreter) {
 606     // Are we profiling?
 607     __ ldr(r0, Address(rmethod, Method::method_data_offset()));
 608     __ cbz(r0, no_mdo);
 609     // Increment counter in the MDO
 610     const Address mdo_invocation_counter(r0, in_bytes(MethodData::invocation_counter_offset()) +
 611                                               in_bytes(InvocationCounter::counter_offset()));
 612     const Address mask(r0, in_bytes(MethodData::invoke_mask_offset()));
 613     __ increment_mask_and_jump(mdo_invocation_counter, increment, mask, rscratch1, rscratch2, false, Assembler::EQ, overflow);
 614     __ b(done);
 615   }
 616   __ bind(no_mdo);
 617   // Increment counter in MethodCounters
 618   const Address invocation_counter(rscratch2,
 619                 MethodCounters::invocation_counter_offset() +
 620                 InvocationCounter::counter_offset());
 621   __ get_method_counters(rmethod, rscratch2, done);
 622   const Address mask(rscratch2, in_bytes(MethodCounters::invoke_mask_offset()));
 623   __ increment_mask_and_jump(invocation_counter, increment, mask, rscratch1, r1, false, Assembler::EQ, overflow);
 624   __ bind(done);
 625 }
 626 
 627 void TemplateInterpreterGenerator::generate_counter_overflow(Label& do_continue) {
 628 
 629   // Asm interpreter on entry
 630   // On return (i.e. jump to entry_point) [ back to invocation of interpreter ]
 631   // Everything as it was on entry
 632 
 633   // InterpreterRuntime::frequency_counter_overflow takes two
 634   // arguments, the first (thread) is passed by call_VM, the second
 635   // indicates if the counter overflow occurs at a backwards branch
 636   // (NULL bcp).  We pass zero for it.  The call returns the address
 637   // of the verified entry point for the method or NULL if the
 638   // compilation did not complete (either went background or bailed
 639   // out).
 640   __ mov(c_rarg1, 0);
 641   __ call_VM(noreg,
 642              CAST_FROM_FN_PTR(address,
 643                               InterpreterRuntime::frequency_counter_overflow),
 644              c_rarg1);
 645 
 646   __ b(do_continue);
 647 }
 648 
 649 // See if we've got enough room on the stack for locals plus overhead
 650 // below JavaThread::stack_overflow_limit(). If not, throw a StackOverflowError
 651 // without going through the signal handler, i.e., reserved and yellow zones
 652 // will not be made usable. The shadow zone must suffice to handle the
 653 // overflow.
 654 // The expression stack grows down incrementally, so the normal guard
 655 // page mechanism will work for that.
 656 //
 657 // NOTE: Since the additional locals are also always pushed (wasn't
 658 // obvious in generate_method_entry) so the guard should work for them
 659 // too.
 660 //
 661 // Args:
 662 //      r3: number of additional locals this frame needs (what we must check)
 663 //      rmethod: Method*
 664 //
 665 // Kills:
 666 //      r0
 667 void TemplateInterpreterGenerator::generate_stack_overflow_check(void) {
 668 
 669   // monitor entry size: see picture of stack set
 670   // (generate_method_entry) and frame_amd64.hpp
 671   const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
 672 
 673   // total overhead size: entry_size + (saved rbp through expr stack
 674   // bottom).  be sure to change this if you add/subtract anything
 675   // to/from the overhead area
 676   const int overhead_size =
 677     -(frame::interpreter_frame_initial_sp_offset * wordSize) + entry_size;
 678 
 679   const int page_size = os::vm_page_size();
 680 
 681   Label after_frame_check;
 682 
 683   // see if the frame is greater than one page in size. If so,
 684   // then we need to verify there is enough stack space remaining
 685   // for the additional locals.
 686   //
 687   // Note that we use SUBS rather than CMP here because the immediate
 688   // field of this instruction may overflow.  SUBS can cope with this
 689   // because it is a macro that will expand to some number of MOV
 690   // instructions and a register operation.
 691   __ subs(rscratch1, r3, (page_size - overhead_size) / Interpreter::stackElementSize);
 692   __ br(Assembler::LS, after_frame_check);
 693 
 694   // compute rsp as if this were going to be the last frame on
 695   // the stack before the red zone
 696 
 697   // locals + overhead, in bytes
 698   __ mov(r0, overhead_size);
 699   __ add(r0, r0, r3, Assembler::LSL, Interpreter::logStackElementSize);  // 2 slots per parameter.
 700 
 701   const Address stack_limit(rthread, JavaThread::stack_overflow_limit_offset());
 702   __ ldr(rscratch1, stack_limit);
 703 
 704 #ifdef ASSERT
 705   Label limit_okay;
 706   // Verify that thread stack limit is non-zero.
 707   __ cbnz(rscratch1, limit_okay);
 708   __ stop("stack overflow limit is zero");
 709   __ bind(limit_okay);
 710 #endif
 711 
 712   // Add stack limit to locals.
 713   __ add(r0, r0, rscratch1);
 714 
 715   // Check against the current stack bottom.
 716   __ cmp(sp, r0);
 717   __ br(Assembler::HI, after_frame_check);
 718 
 719   // Remove the incoming args, peeling the machine SP back to where it
 720   // was in the caller.  This is not strictly necessary, but unless we
 721   // do so the stack frame may have a garbage FP; this ensures a
 722   // correct call stack that we can always unwind.  The ANDR should be
 723   // unnecessary because the sender SP in r13 is always aligned, but
 724   // it doesn't hurt.
 725   __ andr(sp, r13, -16);
 726 
 727   // Note: the restored frame is not necessarily interpreted.
 728   // Use the shared runtime version of the StackOverflowError.
 729   assert(StubRoutines::throw_StackOverflowError_entry() != NULL, "stub not yet generated");
 730   __ far_jump(RuntimeAddress(StubRoutines::throw_StackOverflowError_entry()));
 731 
 732   // all done with frame size check
 733   __ bind(after_frame_check);
 734 }
 735 
 736 // Allocate monitor and lock method (asm interpreter)
 737 //
 738 // Args:
 739 //      rmethod: Method*
 740 //      rlocals: locals
 741 //
 742 // Kills:
 743 //      r0
 744 //      c_rarg0, c_rarg1, c_rarg2, c_rarg3, ...(param regs)
 745 //      rscratch1, rscratch2 (scratch regs)
 746 void TemplateInterpreterGenerator::lock_method() {
 747   // synchronize method
 748   const Address access_flags(rmethod, Method::access_flags_offset());
 749   const Address monitor_block_top(
 750         rfp,
 751         frame::interpreter_frame_monitor_block_top_offset * wordSize);
 752   const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
 753 
 754 #ifdef ASSERT
 755   {
 756     Label L;
 757     __ ldrw(r0, access_flags);
 758     __ tst(r0, JVM_ACC_SYNCHRONIZED);
 759     __ br(Assembler::NE, L);
 760     __ stop("method doesn't need synchronization");
 761     __ bind(L);
 762   }
 763 #endif // ASSERT
 764 
 765   // get synchronization object
 766   {
 767     Label done;
 768     __ ldrw(r0, access_flags);
 769     __ tst(r0, JVM_ACC_STATIC);
 770     // get receiver (assume this is frequent case)
 771     __ ldr(r0, Address(rlocals, Interpreter::local_offset_in_bytes(0)));
 772     __ br(Assembler::EQ, done);
 773     __ load_mirror(r0, rmethod);
 774 
 775 #ifdef ASSERT
 776     {
 777       Label L;
 778       __ cbnz(r0, L);
 779       __ stop("synchronization object is NULL");
 780       __ bind(L);
 781     }
 782 #endif // ASSERT
 783 
 784     __ bind(done);
 785   }
 786 
 787   // add space for monitor & lock
 788   __ sub(sp, sp, entry_size); // add space for a monitor entry
 789   __ sub(esp, esp, entry_size);
 790   __ mov(rscratch1, esp);
 791   __ str(rscratch1, monitor_block_top);  // set new monitor block top
 792   // store object
 793   __ str(r0, Address(esp, BasicObjectLock::obj_offset_in_bytes()));
 794   __ mov(c_rarg1, esp); // object address
 795   __ lock_object(c_rarg1);
 796 }
 797 
 798 // Generate a fixed interpreter frame. This is identical setup for
 799 // interpreted methods and for native methods hence the shared code.
 800 //
 801 // Args:
 802 //      lr: return address
 803 //      rmethod: Method*
 804 //      rlocals: pointer to locals
 805 //      rcpool: cp cache
 806 //      stack_pointer: previous sp
 807 void TemplateInterpreterGenerator::generate_fixed_frame(bool native_call) {
 808   // initialize fixed part of activation frame
 809   if (native_call) {
 810     __ sub(esp, sp, 14 *  wordSize);
 811     __ mov(rbcp, zr);
 812     __ stp(esp, zr, Address(__ pre(sp, -14 * wordSize)));
 813     // add 2 zero-initialized slots for native calls
 814     __ stp(zr, zr, Address(sp, 12 * wordSize));
 815   } else {
 816     __ sub(esp, sp, 12 *  wordSize);
 817     __ ldr(rscratch1, Address(rmethod, Method::const_offset()));      // get ConstMethod
 818     __ add(rbcp, rscratch1, in_bytes(ConstMethod::codes_offset())); // get codebase
 819     __ stp(esp, rbcp, Address(__ pre(sp, -12 * wordSize)));
 820   }
 821 
 822   if (ProfileInterpreter) {
 823     Label method_data_continue;
 824     __ ldr(rscratch1, Address(rmethod, Method::method_data_offset()));
 825     __ cbz(rscratch1, method_data_continue);
 826     __ lea(rscratch1, Address(rscratch1, in_bytes(MethodData::data_offset())));
 827     __ bind(method_data_continue);
 828     __ stp(rscratch1, rmethod, Address(sp, 6 * wordSize));  // save Method* and mdp (method data pointer)
 829   } else {
 830     __ stp(zr, rmethod, Address(sp, 6 * wordSize));        // save Method* (no mdp)
 831   }
 832 
 833   // Get mirror and store it in the frame as GC root for this Method*
 834   __ load_mirror(r10, rmethod);
 835   __ stp(r10, zr, Address(sp, 4 * wordSize));
 836 
 837   __ ldr(rcpool, Address(rmethod, Method::const_offset()));
 838   __ ldr(rcpool, Address(rcpool, ConstMethod::constants_offset()));
 839   __ ldr(rcpool, Address(rcpool, ConstantPool::cache_offset_in_bytes()));
 840   __ stp(rlocals, rcpool, Address(sp, 2 * wordSize));
 841 
 842   __ protect_return_address();
 843   __ stp(rfp, lr, Address(sp, 10 * wordSize));
 844   __ lea(rfp, Address(sp, 10 * wordSize));
 845 
 846   // set sender sp
 847   // leave last_sp as null
 848   __ stp(zr, r13, Address(sp, 8 * wordSize));
 849 
 850   // Move SP out of the way
 851   if (! native_call) {
 852     __ ldr(rscratch1, Address(rmethod, Method::const_offset()));
 853     __ ldrh(rscratch1, Address(rscratch1, ConstMethod::max_stack_offset()));
 854     __ add(rscratch1, rscratch1, frame::interpreter_frame_monitor_size() + 2);
 855     __ sub(rscratch1, sp, rscratch1, ext::uxtw, 3);
 856     __ andr(sp, rscratch1, -16);
 857   }
 858 }
 859 
 860 // End of helpers
 861 
 862 // Various method entries
 863 //------------------------------------------------------------------------------------------------------------------------
 864 //
 865 //
 866 
 867 // Method entry for java.lang.ref.Reference.get.
 868 address TemplateInterpreterGenerator::generate_Reference_get_entry(void) {
 869   // Code: _aload_0, _getfield, _areturn
 870   // parameter size = 1
 871   //
 872   // The code that gets generated by this routine is split into 2 parts:
 873   //    1. The "intrinsified" code for G1 (or any SATB based GC),
 874   //    2. The slow path - which is an expansion of the regular method entry.
 875   //
 876   // Notes:-
 877   // * In the G1 code we do not check whether we need to block for
 878   //   a safepoint. If G1 is enabled then we must execute the specialized
 879   //   code for Reference.get (except when the Reference object is null)
 880   //   so that we can log the value in the referent field with an SATB
 881   //   update buffer.
 882   //   If the code for the getfield template is modified so that the
 883   //   G1 pre-barrier code is executed when the current method is
 884   //   Reference.get() then going through the normal method entry
 885   //   will be fine.
 886   // * The G1 code can, however, check the receiver object (the instance
 887   //   of java.lang.Reference) and jump to the slow path if null. If the
 888   //   Reference object is null then we obviously cannot fetch the referent
 889   //   and so we don't need to call the G1 pre-barrier. Thus we can use the
 890   //   regular method entry code to generate the NPE.
 891   //
 892   // This code is based on generate_accessor_entry.
 893   //
 894   // rmethod: Method*
 895   // r13: senderSP must preserve for slow path, set SP to it on fast path
 896 
 897   // LR is live.  It must be saved around calls.
 898 
 899   address entry = __ pc();
 900 
 901   const int referent_offset = java_lang_ref_Reference::referent_offset();
 902 
 903   Label slow_path;
 904   const Register local_0 = c_rarg0;
 905   // Check if local 0 != NULL
 906   // If the receiver is null then it is OK to jump to the slow path.
 907   __ ldr(local_0, Address(esp, 0));
 908   __ cbz(local_0, slow_path);
 909 
 910   __ mov(r19, r13);   // Move senderSP to a callee-saved register
 911 
 912   // Load the value of the referent field.
 913   const Address field_address(local_0, referent_offset);
 914   BarrierSetAssembler *bs = BarrierSet::barrier_set()->barrier_set_assembler();
 915   bs->load_at(_masm, IN_HEAP | ON_WEAK_OOP_REF, T_OBJECT, local_0, field_address, /*tmp1*/ rscratch2, /*tmp2*/ rscratch1);
 916 
 917   // areturn
 918   __ andr(sp, r19, -16);  // done with stack
 919   __ ret(lr);
 920 
 921   // generate a vanilla interpreter entry as the slow path
 922   __ bind(slow_path);
 923   __ jump_to_entry(Interpreter::entry_for_kind(Interpreter::zerolocals));
 924   return entry;
 925 
 926 }
 927 
 928 /**
 929  * Method entry for static native methods:
 930  *   int java.util.zip.CRC32.update(int crc, int b)
 931  */
 932 address TemplateInterpreterGenerator::generate_CRC32_update_entry() {
 933   if (UseCRC32Intrinsics) {
 934     address entry = __ pc();
 935 
 936     // rmethod: Method*
 937     // r13: senderSP must preserved for slow path
 938     // esp: args
 939 
 940     Label slow_path;
 941     // If we need a safepoint check, generate full interpreter entry.
 942     __ safepoint_poll(slow_path, false /* at_return */, false /* acquire */, false /* in_nmethod */);
 943 
 944     // We don't generate local frame and don't align stack because
 945     // we call stub code and there is no safepoint on this path.
 946 
 947     // Load parameters
 948     const Register crc = c_rarg0;  // crc
 949     const Register val = c_rarg1;  // source java byte value
 950     const Register tbl = c_rarg2;  // scratch
 951 
 952     // Arguments are reversed on java expression stack
 953     __ ldrw(val, Address(esp, 0));              // byte value
 954     __ ldrw(crc, Address(esp, wordSize));       // Initial CRC
 955 
 956     uint64_t offset;
 957     __ adrp(tbl, ExternalAddress(StubRoutines::crc_table_addr()), offset);
 958     __ add(tbl, tbl, offset);
 959 
 960     __ mvnw(crc, crc); // ~crc
 961     __ update_byte_crc32(crc, val, tbl);
 962     __ mvnw(crc, crc); // ~crc
 963 
 964     // result in c_rarg0
 965 
 966     __ andr(sp, r13, -16);
 967     __ ret(lr);
 968 
 969     // generate a vanilla native entry as the slow path
 970     __ bind(slow_path);
 971     __ jump_to_entry(Interpreter::entry_for_kind(Interpreter::native));
 972     return entry;
 973   }
 974   return NULL;
 975 }
 976 
 977 /**
 978  * Method entry for static native methods:
 979  *   int java.util.zip.CRC32.updateBytes(int crc, byte[] b, int off, int len)
 980  *   int java.util.zip.CRC32.updateByteBuffer(int crc, long buf, int off, int len)
 981  */
 982 address TemplateInterpreterGenerator::generate_CRC32_updateBytes_entry(AbstractInterpreter::MethodKind kind) {
 983   if (UseCRC32Intrinsics) {
 984     address entry = __ pc();
 985 
 986     // rmethod,: Method*
 987     // r13: senderSP must preserved for slow path
 988 
 989     Label slow_path;
 990     // If we need a safepoint check, generate full interpreter entry.
 991     __ safepoint_poll(slow_path, false /* at_return */, false /* acquire */, false /* in_nmethod */);
 992 
 993     // We don't generate local frame and don't align stack because
 994     // we call stub code and there is no safepoint on this path.
 995 
 996     // Load parameters
 997     const Register crc = c_rarg0;  // crc
 998     const Register buf = c_rarg1;  // source java byte array address
 999     const Register len = c_rarg2;  // length
1000     const Register off = len;      // offset (never overlaps with 'len')
1001 
1002     // Arguments are reversed on java expression stack
1003     // Calculate address of start element
1004     if (kind == Interpreter::java_util_zip_CRC32_updateByteBuffer) {
1005       __ ldr(buf, Address(esp, 2*wordSize)); // long buf
1006       __ ldrw(off, Address(esp, wordSize)); // offset
1007       __ add(buf, buf, off); // + offset
1008       __ ldrw(crc,   Address(esp, 4*wordSize)); // Initial CRC
1009     } else {
1010       __ ldr(buf, Address(esp, 2*wordSize)); // byte[] array
1011       __ add(buf, buf, arrayOopDesc::base_offset_in_bytes(T_BYTE)); // + header size
1012       __ ldrw(off, Address(esp, wordSize)); // offset
1013       __ add(buf, buf, off); // + offset
1014       __ ldrw(crc,   Address(esp, 3*wordSize)); // Initial CRC
1015     }
1016     // Can now load 'len' since we're finished with 'off'
1017     __ ldrw(len, Address(esp, 0x0)); // Length
1018 
1019     __ andr(sp, r13, -16); // Restore the caller's SP
1020 
1021     // We are frameless so we can just jump to the stub.
1022     __ b(CAST_FROM_FN_PTR(address, StubRoutines::updateBytesCRC32()));
1023 
1024     // generate a vanilla native entry as the slow path
1025     __ bind(slow_path);
1026     __ jump_to_entry(Interpreter::entry_for_kind(Interpreter::native));
1027     return entry;
1028   }
1029   return NULL;
1030 }
1031 
1032 /**
1033  * Method entry for intrinsic-candidate (non-native) methods:
1034  *   int java.util.zip.CRC32C.updateBytes(int crc, byte[] b, int off, int end)
1035  *   int java.util.zip.CRC32C.updateDirectByteBuffer(int crc, long buf, int off, int end)
1036  * Unlike CRC32, CRC32C does not have any methods marked as native
1037  * CRC32C also uses an "end" variable instead of the length variable CRC32 uses
1038  */
1039 address TemplateInterpreterGenerator::generate_CRC32C_updateBytes_entry(AbstractInterpreter::MethodKind kind) {
1040   if (UseCRC32CIntrinsics) {
1041     address entry = __ pc();
1042 
1043     // Prepare jump to stub using parameters from the stack
1044     const Register crc = c_rarg0; // initial crc
1045     const Register buf = c_rarg1; // source java byte array address
1046     const Register len = c_rarg2; // len argument to the kernel
1047 
1048     const Register end = len; // index of last element to process
1049     const Register off = crc; // offset
1050 
1051     __ ldrw(end, Address(esp)); // int end
1052     __ ldrw(off, Address(esp, wordSize)); // int offset
1053     __ sub(len, end, off);
1054     __ ldr(buf, Address(esp, 2*wordSize)); // byte[] buf | long buf
1055     __ add(buf, buf, off); // + offset
1056     if (kind == Interpreter::java_util_zip_CRC32C_updateDirectByteBuffer) {
1057       __ ldrw(crc, Address(esp, 4*wordSize)); // long crc
1058     } else {
1059       __ add(buf, buf, arrayOopDesc::base_offset_in_bytes(T_BYTE)); // + header size
1060       __ ldrw(crc, Address(esp, 3*wordSize)); // long crc
1061     }
1062 
1063     __ andr(sp, r13, -16); // Restore the caller's SP
1064 
1065     // Jump to the stub.
1066     __ b(CAST_FROM_FN_PTR(address, StubRoutines::updateBytesCRC32C()));
1067 
1068     return entry;
1069   }
1070   return NULL;
1071 }
1072 
1073 void TemplateInterpreterGenerator::bang_stack_shadow_pages(bool native_call) {
1074   // Bang each page in the shadow zone. We can't assume it's been done for
1075   // an interpreter frame with greater than a page of locals, so each page
1076   // needs to be checked.  Only true for non-native.
1077   const int n_shadow_pages = (int)(StackOverflow::stack_shadow_zone_size() / os::vm_page_size());
1078   const int start_page = native_call ? n_shadow_pages : 1;
1079   const int page_size = os::vm_page_size();
1080   for (int pages = start_page; pages <= n_shadow_pages ; pages++) {
1081     __ sub(rscratch2, sp, pages*page_size);
1082     __ str(zr, Address(rscratch2));
1083   }
1084 }
1085 
1086 
1087 // Interpreter stub for calling a native method. (asm interpreter)
1088 // This sets up a somewhat different looking stack for calling the
1089 // native method than the typical interpreter frame setup.
1090 address TemplateInterpreterGenerator::generate_native_entry(bool synchronized) {
1091   // determine code generation flags
1092   bool inc_counter  = UseCompiler || CountCompiledCalls || LogTouchedMethods;
1093 
1094   // r1: Method*
1095   // rscratch1: sender sp
1096 
1097   address entry_point = __ pc();
1098 
1099   const Address constMethod       (rmethod, Method::const_offset());
1100   const Address access_flags      (rmethod, Method::access_flags_offset());
1101   const Address size_of_parameters(r2, ConstMethod::
1102                                        size_of_parameters_offset());
1103 
1104   // get parameter size (always needed)
1105   __ ldr(r2, constMethod);
1106   __ load_unsigned_short(r2, size_of_parameters);
1107 
1108   // Native calls don't need the stack size check since they have no
1109   // expression stack and the arguments are already on the stack and
1110   // we only add a handful of words to the stack.
1111 
1112   // rmethod: Method*
1113   // r2: size of parameters
1114   // rscratch1: sender sp
1115 
1116   // for natives the size of locals is zero
1117 
1118   // compute beginning of parameters (rlocals)
1119   __ add(rlocals, esp, r2, ext::uxtx, 3);
1120   __ add(rlocals, rlocals, -wordSize);
1121 
1122   // Pull SP back to minimum size: this avoids holes in the stack
1123   __ andr(sp, esp, -16);
1124 
1125   // initialize fixed part of activation frame
1126   generate_fixed_frame(true);
1127 
1128   // make sure method is native & not abstract
1129 #ifdef ASSERT
1130   __ ldrw(r0, access_flags);
1131   {
1132     Label L;
1133     __ tst(r0, JVM_ACC_NATIVE);
1134     __ br(Assembler::NE, L);
1135     __ stop("tried to execute non-native method as native");
1136     __ bind(L);
1137   }
1138   {
1139     Label L;
1140     __ tst(r0, JVM_ACC_ABSTRACT);
1141     __ br(Assembler::EQ, L);
1142     __ stop("tried to execute abstract method in interpreter");
1143     __ bind(L);
1144   }
1145 #endif
1146 
1147   // Since at this point in the method invocation the exception
1148   // handler would try to exit the monitor of synchronized methods
1149   // which hasn't been entered yet, we set the thread local variable
1150   // _do_not_unlock_if_synchronized to true. The remove_activation
1151   // will check this flag.
1152 
1153    const Address do_not_unlock_if_synchronized(rthread,
1154         in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
1155   __ mov(rscratch2, true);
1156   __ strb(rscratch2, do_not_unlock_if_synchronized);
1157 
1158   // increment invocation count & check for overflow
1159   Label invocation_counter_overflow;
1160   if (inc_counter) {
1161     generate_counter_incr(&invocation_counter_overflow);
1162   }
1163 
1164   Label continue_after_compile;
1165   __ bind(continue_after_compile);
1166 
1167   bang_stack_shadow_pages(true);
1168 
1169   // reset the _do_not_unlock_if_synchronized flag
1170   __ strb(zr, do_not_unlock_if_synchronized);
1171 
1172   // check for synchronized methods
1173   // Must happen AFTER invocation_counter check and stack overflow check,
1174   // so method is not locked if overflows.
1175   if (synchronized) {
1176     lock_method();
1177   } else {
1178     // no synchronization necessary
1179 #ifdef ASSERT
1180     {
1181       Label L;
1182       __ ldrw(r0, access_flags);
1183       __ tst(r0, JVM_ACC_SYNCHRONIZED);
1184       __ br(Assembler::EQ, L);
1185       __ stop("method needs synchronization");
1186       __ bind(L);
1187     }
1188 #endif
1189   }
1190 
1191   // start execution
1192 #ifdef ASSERT
1193   {
1194     Label L;
1195     const Address monitor_block_top(rfp,
1196                  frame::interpreter_frame_monitor_block_top_offset * wordSize);
1197     __ ldr(rscratch1, monitor_block_top);
1198     __ cmp(esp, rscratch1);
1199     __ br(Assembler::EQ, L);
1200     __ stop("broken stack frame setup in interpreter");
1201     __ bind(L);
1202   }
1203 #endif
1204 
1205   // jvmti support
1206   __ notify_method_entry();
1207 
1208   // work registers
1209   const Register t = r17;
1210   const Register result_handler = r19;
1211 
1212   // allocate space for parameters
1213   __ ldr(t, Address(rmethod, Method::const_offset()));
1214   __ load_unsigned_short(t, Address(t, ConstMethod::size_of_parameters_offset()));
1215 
1216   __ sub(rscratch1, esp, t, ext::uxtx, Interpreter::logStackElementSize);
1217   __ andr(sp, rscratch1, -16);
1218   __ mov(esp, rscratch1);
1219 
1220   // get signature handler
1221   {
1222     Label L;
1223     __ ldr(t, Address(rmethod, Method::signature_handler_offset()));
1224     __ cbnz(t, L);
1225     __ call_VM(noreg,
1226                CAST_FROM_FN_PTR(address,
1227                                 InterpreterRuntime::prepare_native_call),
1228                rmethod);
1229     __ ldr(t, Address(rmethod, Method::signature_handler_offset()));
1230     __ bind(L);
1231   }
1232 
1233   // call signature handler
1234   assert(InterpreterRuntime::SignatureHandlerGenerator::from() == rlocals,
1235          "adjust this code");
1236   assert(InterpreterRuntime::SignatureHandlerGenerator::to() == sp,
1237          "adjust this code");
1238   assert(InterpreterRuntime::SignatureHandlerGenerator::temp() == rscratch1,
1239           "adjust this code");
1240 
1241   // The generated handlers do not touch rmethod (the method).
1242   // However, large signatures cannot be cached and are generated
1243   // each time here.  The slow-path generator can do a GC on return,
1244   // so we must reload it after the call.
1245   __ blr(t);
1246   __ get_method(rmethod);        // slow path can do a GC, reload rmethod
1247 
1248 
1249   // result handler is in r0
1250   // set result handler
1251   __ mov(result_handler, r0);
1252   // pass mirror handle if static call
1253   {
1254     Label L;
1255     __ ldrw(t, Address(rmethod, Method::access_flags_offset()));
1256     __ tbz(t, exact_log2(JVM_ACC_STATIC), L);
1257     // get mirror
1258     __ load_mirror(t, rmethod);
1259     // copy mirror into activation frame
1260     __ str(t, Address(rfp, frame::interpreter_frame_oop_temp_offset * wordSize));
1261     // pass handle to mirror
1262     __ add(c_rarg1, rfp, frame::interpreter_frame_oop_temp_offset * wordSize);
1263     __ bind(L);
1264   }
1265 
1266   // get native function entry point in r10
1267   {
1268     Label L;
1269     __ ldr(r10, Address(rmethod, Method::native_function_offset()));
1270     address unsatisfied = (SharedRuntime::native_method_throw_unsatisfied_link_error_entry());
1271     __ mov(rscratch2, unsatisfied);
1272     __ ldr(rscratch2, rscratch2);
1273     __ cmp(r10, rscratch2);
1274     __ br(Assembler::NE, L);
1275     __ call_VM(noreg,
1276                CAST_FROM_FN_PTR(address,
1277                                 InterpreterRuntime::prepare_native_call),
1278                rmethod);
1279     __ get_method(rmethod);
1280     __ ldr(r10, Address(rmethod, Method::native_function_offset()));
1281     __ bind(L);
1282   }
1283 
1284   // pass JNIEnv
1285   __ add(c_rarg0, rthread, in_bytes(JavaThread::jni_environment_offset()));
1286 
1287   // Set the last Java PC in the frame anchor to be the return address from
1288   // the call to the native method: this will allow the debugger to
1289   // generate an accurate stack trace.
1290   Label native_return;
1291   __ set_last_Java_frame(esp, rfp, native_return, rscratch1);
1292 
1293   // change thread state
1294 #ifdef ASSERT
1295   {
1296     Label L;
1297     __ ldrw(t, Address(rthread, JavaThread::thread_state_offset()));
1298     __ cmp(t, (u1)_thread_in_Java);
1299     __ br(Assembler::EQ, L);
1300     __ stop("Wrong thread state in native stub");
1301     __ bind(L);
1302   }
1303 #endif
1304 
1305   // Change state to native
1306   __ mov(rscratch1, _thread_in_native);
1307   __ lea(rscratch2, Address(rthread, JavaThread::thread_state_offset()));
1308   __ stlrw(rscratch1, rscratch2);
1309 
1310   // Call the native method.
1311   __ blr(r10);
1312   __ bind(native_return);
1313   __ get_method(rmethod);
1314   // result potentially in r0 or v0
1315 
1316   // make room for the pushes we're about to do
1317   __ sub(rscratch1, esp, 4 * wordSize);
1318   __ andr(sp, rscratch1, -16);
1319 
1320   // NOTE: The order of these pushes is known to frame::interpreter_frame_result
1321   // in order to extract the result of a method call. If the order of these
1322   // pushes change or anything else is added to the stack then the code in
1323   // interpreter_frame_result must also change.
1324   __ push(dtos);
1325   __ push(ltos);
1326 
1327   __ verify_sve_vector_length();
1328 
1329   // change thread state
1330   __ mov(rscratch1, _thread_in_native_trans);
1331   __ lea(rscratch2, Address(rthread, JavaThread::thread_state_offset()));
1332   __ stlrw(rscratch1, rscratch2);
1333 
1334   // Force this write out before the read below
1335   __ dmb(Assembler::ISH);
1336 
1337   // check for safepoint operation in progress and/or pending suspend requests
1338   {
1339     Label L, Continue;
1340 
1341     // We need an acquire here to ensure that any subsequent load of the
1342     // global SafepointSynchronize::_state flag is ordered after this load
1343     // of the thread-local polling word.  We don't want this poll to
1344     // return false (i.e. not safepointing) and a later poll of the global
1345     // SafepointSynchronize::_state spuriously to return true.
1346     //
1347     // This is to avoid a race when we're in a native->Java transition
1348     // racing the code which wakes up from a safepoint.
1349     __ safepoint_poll(L, true /* at_return */, true /* acquire */, false /* in_nmethod */);
1350     __ ldrw(rscratch2, Address(rthread, JavaThread::suspend_flags_offset()));
1351     __ cbz(rscratch2, Continue);
1352     __ bind(L);
1353 
1354     // Don't use call_VM as it will see a possible pending exception
1355     // and forward it and never return here preventing us from
1356     // clearing _last_native_pc down below. So we do a runtime call by
1357     // hand.
1358     //
1359     __ mov(c_rarg0, rthread);
1360     __ mov(rscratch2, CAST_FROM_FN_PTR(address, JavaThread::check_special_condition_for_native_trans));
1361     __ blr(rscratch2);
1362     __ get_method(rmethod);
1363     __ reinit_heapbase();
1364     __ bind(Continue);
1365   }
1366 
1367   // change thread state
1368   __ mov(rscratch1, _thread_in_Java);
1369   __ lea(rscratch2, Address(rthread, JavaThread::thread_state_offset()));
1370   __ stlrw(rscratch1, rscratch2);
1371 
1372   // reset_last_Java_frame
1373   __ reset_last_Java_frame(true);
1374 
1375   if (CheckJNICalls) {
1376     // clear_pending_jni_exception_check
1377     __ str(zr, Address(rthread, JavaThread::pending_jni_exception_check_fn_offset()));
1378   }
1379 
1380   // reset handle block
1381   __ ldr(t, Address(rthread, JavaThread::active_handles_offset()));
1382   __ str(zr, Address(t, JNIHandleBlock::top_offset_in_bytes()));
1383 
1384   // If result is an oop unbox and store it in frame where gc will see it
1385   // and result handler will pick it up
1386 
1387   {
1388     Label no_oop;
1389     __ adr(t, ExternalAddress(AbstractInterpreter::result_handler(T_OBJECT)));
1390     __ cmp(t, result_handler);
1391     __ br(Assembler::NE, no_oop);
1392     // Unbox oop result, e.g. JNIHandles::resolve result.
1393     __ pop(ltos);
1394     __ resolve_jobject(r0, rthread, t);
1395     __ str(r0, Address(rfp, frame::interpreter_frame_oop_temp_offset*wordSize));
1396     // keep stack depth as expected by pushing oop which will eventually be discarded
1397     __ push(ltos);
1398     __ bind(no_oop);
1399   }
1400 
1401   {
1402     Label no_reguard;
1403     __ lea(rscratch1, Address(rthread, in_bytes(JavaThread::stack_guard_state_offset())));
1404     __ ldrw(rscratch1, Address(rscratch1));
1405     __ cmp(rscratch1, (u1)StackOverflow::stack_guard_yellow_reserved_disabled);
1406     __ br(Assembler::NE, no_reguard);
1407 
1408     __ push_call_clobbered_registers();
1409     __ mov(c_rarg0, rthread);
1410     __ mov(rscratch2, CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages));
1411     __ blr(rscratch2);
1412     __ pop_call_clobbered_registers();
1413 
1414     __ bind(no_reguard);
1415   }
1416 
1417   // The method register is junk from after the thread_in_native transition
1418   // until here.  Also can't call_VM until the bcp has been
1419   // restored.  Need bcp for throwing exception below so get it now.
1420   __ get_method(rmethod);
1421 
1422   // restore bcp to have legal interpreter frame, i.e., bci == 0 <=>
1423   // rbcp == code_base()
1424   __ ldr(rbcp, Address(rmethod, Method::const_offset()));   // get ConstMethod*
1425   __ add(rbcp, rbcp, in_bytes(ConstMethod::codes_offset()));          // get codebase
1426   // handle exceptions (exception handling will handle unlocking!)
1427   {
1428     Label L;
1429     __ ldr(rscratch1, Address(rthread, Thread::pending_exception_offset()));
1430     __ cbz(rscratch1, L);
1431     // Note: At some point we may want to unify this with the code
1432     // used in call_VM_base(); i.e., we should use the
1433     // StubRoutines::forward_exception code. For now this doesn't work
1434     // here because the rsp is not correctly set at this point.
1435     __ MacroAssembler::call_VM(noreg,
1436                                CAST_FROM_FN_PTR(address,
1437                                InterpreterRuntime::throw_pending_exception));
1438     __ should_not_reach_here();
1439     __ bind(L);
1440   }
1441 
1442   // do unlocking if necessary
1443   {
1444     Label L;
1445     __ ldrw(t, Address(rmethod, Method::access_flags_offset()));
1446     __ tbz(t, exact_log2(JVM_ACC_SYNCHRONIZED), L);
1447     // the code below should be shared with interpreter macro
1448     // assembler implementation
1449     {
1450       Label unlock;
1451       // BasicObjectLock will be first in list, since this is a
1452       // synchronized method. However, need to check that the object
1453       // has not been unlocked by an explicit monitorexit bytecode.
1454 
1455       // monitor expect in c_rarg1 for slow unlock path
1456       __ lea (c_rarg1, Address(rfp,   // address of first monitor
1457                                (intptr_t)(frame::interpreter_frame_initial_sp_offset *
1458                                           wordSize - sizeof(BasicObjectLock))));
1459 
1460       __ ldr(t, Address(c_rarg1, BasicObjectLock::obj_offset_in_bytes()));
1461       __ cbnz(t, unlock);
1462 
1463       // Entry already unlocked, need to throw exception
1464       __ MacroAssembler::call_VM(noreg,
1465                                  CAST_FROM_FN_PTR(address,
1466                    InterpreterRuntime::throw_illegal_monitor_state_exception));
1467       __ should_not_reach_here();
1468 
1469       __ bind(unlock);
1470       __ unlock_object(c_rarg1);
1471     }
1472     __ bind(L);
1473   }
1474 
1475   // jvmti support
1476   // Note: This must happen _after_ handling/throwing any exceptions since
1477   //       the exception handler code notifies the runtime of method exits
1478   //       too. If this happens before, method entry/exit notifications are
1479   //       not properly paired (was bug - gri 11/22/99).
1480   __ notify_method_exit(vtos, InterpreterMacroAssembler::NotifyJVMTI);
1481 
1482   // restore potential result in r0:d0, call result handler to
1483   // restore potential result in ST0 & handle result
1484 
1485   __ pop(ltos);
1486   __ pop(dtos);
1487 
1488   __ blr(result_handler);
1489 
1490   // remove activation
1491   __ ldr(esp, Address(rfp,
1492                     frame::interpreter_frame_sender_sp_offset *
1493                     wordSize)); // get sender sp
1494   // remove frame anchor
1495   __ leave();
1496 
1497   // resture sender sp
1498   __ mov(sp, esp);
1499 
1500   __ ret(lr);
1501 
1502   if (inc_counter) {
1503     // Handle overflow of counter and compile method
1504     __ bind(invocation_counter_overflow);
1505     generate_counter_overflow(continue_after_compile);
1506   }
1507 
1508   return entry_point;
1509 }
1510 
1511 //
1512 // Generic interpreted method entry to (asm) interpreter
1513 //
1514 address TemplateInterpreterGenerator::generate_normal_entry(bool synchronized) {
1515   // determine code generation flags
1516   bool inc_counter  = UseCompiler || CountCompiledCalls || LogTouchedMethods;
1517 
1518   // rscratch1: sender sp
1519   address entry_point = __ pc();
1520 
1521   const Address constMethod(rmethod, Method::const_offset());
1522   const Address access_flags(rmethod, Method::access_flags_offset());
1523   const Address size_of_parameters(r3,
1524                                    ConstMethod::size_of_parameters_offset());
1525   const Address size_of_locals(r3, ConstMethod::size_of_locals_offset());
1526 
1527   // get parameter size (always needed)
1528   // need to load the const method first
1529   __ ldr(r3, constMethod);
1530   __ load_unsigned_short(r2, size_of_parameters);
1531 
1532   // r2: size of parameters
1533 
1534   __ load_unsigned_short(r3, size_of_locals); // get size of locals in words
1535   __ sub(r3, r3, r2); // r3 = no. of additional locals
1536 
1537   // see if we've got enough room on the stack for locals plus overhead.
1538   generate_stack_overflow_check();
1539 
1540   // compute beginning of parameters (rlocals)
1541   __ add(rlocals, esp, r2, ext::uxtx, 3);
1542   __ sub(rlocals, rlocals, wordSize);
1543 
1544   __ mov(rscratch1, esp);
1545 
1546   // r3 - # of additional locals
1547   // allocate space for locals
1548   // explicitly initialize locals
1549   // Initializing memory allocated for locals in the same direction as
1550   // the stack grows to ensure page initialization order according
1551   // to windows-aarch64 stack page growth requirement (see
1552   // https://docs.microsoft.com/en-us/cpp/build/arm64-windows-abi-conventions?view=msvc-160#stack)
1553   {
1554     Label exit, loop;
1555     __ ands(zr, r3, r3);
1556     __ br(Assembler::LE, exit); // do nothing if r3 <= 0
1557     __ bind(loop);
1558     __ str(zr, Address(__ pre(rscratch1, -wordSize)));
1559     __ sub(r3, r3, 1); // until everything initialized
1560     __ cbnz(r3, loop);
1561     __ bind(exit);
1562   }
1563 
1564   // Padding between locals and fixed part of activation frame to ensure
1565   // SP is always 16-byte aligned.
1566   __ andr(sp, rscratch1, -16);
1567 
1568   // And the base dispatch table
1569   __ get_dispatch();
1570 
1571   // initialize fixed part of activation frame
1572   generate_fixed_frame(false);
1573 
1574   // make sure method is not native & not abstract
1575 #ifdef ASSERT
1576   __ ldrw(r0, access_flags);
1577   {
1578     Label L;
1579     __ tst(r0, JVM_ACC_NATIVE);
1580     __ br(Assembler::EQ, L);
1581     __ stop("tried to execute native method as non-native");
1582     __ bind(L);
1583   }
1584  {
1585     Label L;
1586     __ tst(r0, JVM_ACC_ABSTRACT);
1587     __ br(Assembler::EQ, L);
1588     __ stop("tried to execute abstract method in interpreter");
1589     __ bind(L);
1590   }
1591 #endif
1592 
1593   // Since at this point in the method invocation the exception
1594   // handler would try to exit the monitor of synchronized methods
1595   // which hasn't been entered yet, we set the thread local variable
1596   // _do_not_unlock_if_synchronized to true. The remove_activation
1597   // will check this flag.
1598 
1599    const Address do_not_unlock_if_synchronized(rthread,
1600         in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
1601   __ mov(rscratch2, true);
1602   __ strb(rscratch2, do_not_unlock_if_synchronized);
1603 
1604   Register mdp = r3;
1605   __ profile_parameters_type(mdp, r1, r2);
1606 
1607   // increment invocation count & check for overflow
1608   Label invocation_counter_overflow;
1609   if (inc_counter) {
1610     generate_counter_incr(&invocation_counter_overflow);
1611   }
1612 
1613   Label continue_after_compile;
1614   __ bind(continue_after_compile);
1615 
1616   bang_stack_shadow_pages(false);
1617 
1618   // reset the _do_not_unlock_if_synchronized flag
1619   __ strb(zr, do_not_unlock_if_synchronized);
1620 
1621   // check for synchronized methods
1622   // Must happen AFTER invocation_counter check and stack overflow check,
1623   // so method is not locked if overflows.
1624   if (synchronized) {
1625     // Allocate monitor and lock method
1626     lock_method();
1627   } else {
1628     // no synchronization necessary
1629 #ifdef ASSERT
1630     {
1631       Label L;
1632       __ ldrw(r0, access_flags);
1633       __ tst(r0, JVM_ACC_SYNCHRONIZED);
1634       __ br(Assembler::EQ, L);
1635       __ stop("method needs synchronization");
1636       __ bind(L);
1637     }
1638 #endif
1639   }
1640 
1641   // start execution
1642 #ifdef ASSERT
1643   {
1644     Label L;
1645      const Address monitor_block_top (rfp,
1646                  frame::interpreter_frame_monitor_block_top_offset * wordSize);
1647     __ ldr(rscratch1, monitor_block_top);
1648     __ cmp(esp, rscratch1);
1649     __ br(Assembler::EQ, L);
1650     __ stop("broken stack frame setup in interpreter");
1651     __ bind(L);
1652   }
1653 #endif
1654 
1655   // jvmti support
1656   __ notify_method_entry();
1657 
1658   __ dispatch_next(vtos);
1659 
1660   // invocation counter overflow
1661   if (inc_counter) {
1662     // Handle overflow of counter and compile method
1663     __ bind(invocation_counter_overflow);
1664     generate_counter_overflow(continue_after_compile);
1665   }
1666 
1667   return entry_point;
1668 }
1669 
1670 //-----------------------------------------------------------------------------
1671 // Exceptions
1672 
1673 void TemplateInterpreterGenerator::generate_throw_exception() {
1674   // Entry point in previous activation (i.e., if the caller was
1675   // interpreted)
1676   Interpreter::_rethrow_exception_entry = __ pc();
1677   // Restore sp to interpreter_frame_last_sp even though we are going
1678   // to empty the expression stack for the exception processing.
1679   __ str(zr, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
1680   // r0: exception
1681   // r3: return address/pc that threw exception
1682   __ restore_bcp();    // rbcp points to call/send
1683   __ restore_locals();
1684   __ restore_constant_pool_cache();
1685   __ reinit_heapbase();  // restore rheapbase as heapbase.
1686   __ get_dispatch();
1687 
1688   // Entry point for exceptions thrown within interpreter code
1689   Interpreter::_throw_exception_entry = __ pc();
1690   // If we came here via a NullPointerException on the receiver of a
1691   // method, rmethod may be corrupt.
1692   __ get_method(rmethod);
1693   // expression stack is undefined here
1694   // r0: exception
1695   // rbcp: exception bcp
1696   __ verify_oop(r0);
1697   __ mov(c_rarg1, r0);
1698 
1699   // expression stack must be empty before entering the VM in case of
1700   // an exception
1701   __ empty_expression_stack();
1702   // find exception handler address and preserve exception oop
1703   __ call_VM(r3,
1704              CAST_FROM_FN_PTR(address,
1705                           InterpreterRuntime::exception_handler_for_exception),
1706              c_rarg1);
1707 
1708   // Calculate stack limit
1709   __ ldr(rscratch1, Address(rmethod, Method::const_offset()));
1710   __ ldrh(rscratch1, Address(rscratch1, ConstMethod::max_stack_offset()));
1711   __ add(rscratch1, rscratch1, frame::interpreter_frame_monitor_size() + 4);
1712   __ ldr(rscratch2,
1713          Address(rfp, frame::interpreter_frame_initial_sp_offset * wordSize));
1714   __ sub(rscratch1, rscratch2, rscratch1, ext::uxtx, 3);
1715   __ andr(sp, rscratch1, -16);
1716 
1717   // r0: exception handler entry point
1718   // r3: preserved exception oop
1719   // rbcp: bcp for exception handler
1720   __ push_ptr(r3); // push exception which is now the only value on the stack
1721   __ br(r0); // jump to exception handler (may be _remove_activation_entry!)
1722 
1723   // If the exception is not handled in the current frame the frame is
1724   // removed and the exception is rethrown (i.e. exception
1725   // continuation is _rethrow_exception).
1726   //
1727   // Note: At this point the bci is still the bxi for the instruction
1728   // which caused the exception and the expression stack is
1729   // empty. Thus, for any VM calls at this point, GC will find a legal
1730   // oop map (with empty expression stack).
1731 
1732   //
1733   // JVMTI PopFrame support
1734   //
1735 
1736   Interpreter::_remove_activation_preserving_args_entry = __ pc();
1737   __ empty_expression_stack();
1738   // Set the popframe_processing bit in pending_popframe_condition
1739   // indicating that we are currently handling popframe, so that
1740   // call_VMs that may happen later do not trigger new popframe
1741   // handling cycles.
1742   __ ldrw(r3, Address(rthread, JavaThread::popframe_condition_offset()));
1743   __ orr(r3, r3, JavaThread::popframe_processing_bit);
1744   __ strw(r3, Address(rthread, JavaThread::popframe_condition_offset()));
1745 
1746   {
1747     // Check to see whether we are returning to a deoptimized frame.
1748     // (The PopFrame call ensures that the caller of the popped frame is
1749     // either interpreted or compiled and deoptimizes it if compiled.)
1750     // In this case, we can't call dispatch_next() after the frame is
1751     // popped, but instead must save the incoming arguments and restore
1752     // them after deoptimization has occurred.
1753     //
1754     // Note that we don't compare the return PC against the
1755     // deoptimization blob's unpack entry because of the presence of
1756     // adapter frames in C2.
1757     Label caller_not_deoptimized;
1758     __ ldr(c_rarg1, Address(rfp, frame::return_addr_offset * wordSize));
1759     // This is a return address, so requires authenticating for PAC.
1760     __ authenticate_return_address(c_rarg1, rscratch1);
1761     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1762                                InterpreterRuntime::interpreter_contains), c_rarg1);
1763     __ cbnz(r0, caller_not_deoptimized);
1764 
1765     // Compute size of arguments for saving when returning to
1766     // deoptimized caller
1767     __ get_method(r0);
1768     __ ldr(r0, Address(r0, Method::const_offset()));
1769     __ load_unsigned_short(r0, Address(r0, in_bytes(ConstMethod::
1770                                                     size_of_parameters_offset())));
1771     __ lsl(r0, r0, Interpreter::logStackElementSize);
1772     __ restore_locals(); // XXX do we need this?
1773     __ sub(rlocals, rlocals, r0);
1774     __ add(rlocals, rlocals, wordSize);
1775     // Save these arguments
1776     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1777                                            Deoptimization::
1778                                            popframe_preserve_args),
1779                           rthread, r0, rlocals);
1780 
1781     __ remove_activation(vtos,
1782                          /* throw_monitor_exception */ false,
1783                          /* install_monitor_exception */ false,
1784                          /* notify_jvmdi */ false);
1785 
1786     // Inform deoptimization that it is responsible for restoring
1787     // these arguments
1788     __ mov(rscratch1, JavaThread::popframe_force_deopt_reexecution_bit);
1789     __ strw(rscratch1, Address(rthread, JavaThread::popframe_condition_offset()));
1790 
1791     // Continue in deoptimization handler
1792     __ ret(lr);
1793 
1794     __ bind(caller_not_deoptimized);
1795   }
1796 
1797   __ remove_activation(vtos,
1798                        /* throw_monitor_exception */ false,
1799                        /* install_monitor_exception */ false,
1800                        /* notify_jvmdi */ false);
1801 
1802   // Restore the last_sp and null it out
1803   __ ldr(esp, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
1804   __ str(zr, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
1805 
1806   __ restore_bcp();
1807   __ restore_locals();
1808   __ restore_constant_pool_cache();
1809   __ get_method(rmethod);
1810   __ get_dispatch();
1811 
1812   // The method data pointer was incremented already during
1813   // call profiling. We have to restore the mdp for the current bcp.
1814   if (ProfileInterpreter) {
1815     __ set_method_data_pointer_for_bcp();
1816   }
1817 
1818   // Clear the popframe condition flag
1819   __ strw(zr, Address(rthread, JavaThread::popframe_condition_offset()));
1820   assert(JavaThread::popframe_inactive == 0, "fix popframe_inactive");
1821 
1822 #if INCLUDE_JVMTI
1823   {
1824     Label L_done;
1825 
1826     __ ldrb(rscratch1, Address(rbcp, 0));
1827     __ cmpw(rscratch1, Bytecodes::_invokestatic);
1828     __ br(Assembler::NE, L_done);
1829 
1830     // The member name argument must be restored if _invokestatic is re-executed after a PopFrame call.
1831     // Detect such a case in the InterpreterRuntime function and return the member name argument, or NULL.
1832 
1833     __ ldr(c_rarg0, Address(rlocals, 0));
1834     __ call_VM(r0, CAST_FROM_FN_PTR(address, InterpreterRuntime::member_name_arg_or_null), c_rarg0, rmethod, rbcp);
1835 
1836     __ cbz(r0, L_done);
1837 
1838     __ str(r0, Address(esp, 0));
1839     __ bind(L_done);
1840   }
1841 #endif // INCLUDE_JVMTI
1842 
1843   // Restore machine SP
1844   __ ldr(rscratch1, Address(rmethod, Method::const_offset()));
1845   __ ldrh(rscratch1, Address(rscratch1, ConstMethod::max_stack_offset()));
1846   __ add(rscratch1, rscratch1, frame::interpreter_frame_monitor_size() + 4);
1847   __ ldr(rscratch2,
1848          Address(rfp, frame::interpreter_frame_initial_sp_offset * wordSize));
1849   __ sub(rscratch1, rscratch2, rscratch1, ext::uxtw, 3);
1850   __ andr(sp, rscratch1, -16);
1851 
1852   __ dispatch_next(vtos);
1853   // end of PopFrame support
1854 
1855   Interpreter::_remove_activation_entry = __ pc();
1856 
1857   // preserve exception over this code sequence
1858   __ pop_ptr(r0);
1859   __ str(r0, Address(rthread, JavaThread::vm_result_offset()));
1860   // remove the activation (without doing throws on illegalMonitorExceptions)
1861   __ remove_activation(vtos, false, true, false);
1862   // restore exception
1863   __ get_vm_result(r0, rthread);
1864 
1865   // In between activations - previous activation type unknown yet
1866   // compute continuation point - the continuation point expects the
1867   // following registers set up:
1868   //
1869   // r0: exception
1870   // lr: return address/pc that threw exception
1871   // esp: expression stack of caller
1872   // rfp: fp of caller
1873   __ stp(r0, lr, Address(__ pre(sp, -2 * wordSize)));  // save exception & return address
1874   __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1875                           SharedRuntime::exception_handler_for_return_address),
1876                         rthread, lr);
1877   __ mov(r1, r0);                               // save exception handler
1878   __ ldp(r0, lr, Address(__ post(sp, 2 * wordSize)));  // restore exception & return address
1879   // We might be returning to a deopt handler that expects r3 to
1880   // contain the exception pc
1881   __ mov(r3, lr);
1882   // Note that an "issuing PC" is actually the next PC after the call
1883   __ br(r1);                                    // jump to exception
1884                                                 // handler of caller
1885 }
1886 
1887 
1888 //
1889 // JVMTI ForceEarlyReturn support
1890 //
1891 address TemplateInterpreterGenerator::generate_earlyret_entry_for(TosState state) {
1892   address entry = __ pc();
1893 
1894   __ restore_bcp();
1895   __ restore_locals();
1896   __ empty_expression_stack();
1897   __ load_earlyret_value(state);
1898 
1899   __ ldr(rscratch1, Address(rthread, JavaThread::jvmti_thread_state_offset()));
1900   Address cond_addr(rscratch1, JvmtiThreadState::earlyret_state_offset());
1901 
1902   // Clear the earlyret state
1903   assert(JvmtiThreadState::earlyret_inactive == 0, "should be");
1904   __ str(zr, cond_addr);
1905 
1906   __ remove_activation(state,
1907                        false, /* throw_monitor_exception */
1908                        false, /* install_monitor_exception */
1909                        true); /* notify_jvmdi */
1910   __ ret(lr);
1911 
1912   return entry;
1913 } // end of ForceEarlyReturn support
1914 
1915 
1916 
1917 //-----------------------------------------------------------------------------
1918 // Helper for vtos entry point generation
1919 
1920 void TemplateInterpreterGenerator::set_vtos_entry_points(Template* t,
1921                                                          address& bep,
1922                                                          address& cep,
1923                                                          address& sep,
1924                                                          address& aep,
1925                                                          address& iep,
1926                                                          address& lep,
1927                                                          address& fep,
1928                                                          address& dep,
1929                                                          address& vep) {
1930   assert(t->is_valid() && t->tos_in() == vtos, "illegal template");
1931   Label L;
1932   aep = __ pc();  __ push_ptr();  __ b(L);
1933   fep = __ pc();  __ push_f();    __ b(L);
1934   dep = __ pc();  __ push_d();    __ b(L);
1935   lep = __ pc();  __ push_l();    __ b(L);
1936   bep = cep = sep =
1937   iep = __ pc();  __ push_i();
1938   vep = __ pc();
1939   __ bind(L);
1940   generate_and_dispatch(t);
1941 }
1942 
1943 //-----------------------------------------------------------------------------
1944 
1945 // Non-product code
1946 #ifndef PRODUCT
1947 address TemplateInterpreterGenerator::generate_trace_code(TosState state) {
1948   address entry = __ pc();
1949 
1950   __ protect_return_address();
1951   __ push(lr);
1952   __ push(state);
1953   __ push(RegSet::range(r0, r15), sp);
1954   __ mov(c_rarg2, r0);  // Pass itos
1955   __ call_VM(noreg,
1956              CAST_FROM_FN_PTR(address, InterpreterRuntime::trace_bytecode),
1957              c_rarg1, c_rarg2, c_rarg3);
1958   __ pop(RegSet::range(r0, r15), sp);
1959   __ pop(state);
1960   __ pop(lr);
1961   __ authenticate_return_address();
1962   __ ret(lr);                                   // return from result handler
1963 
1964   return entry;
1965 }
1966 
1967 void TemplateInterpreterGenerator::count_bytecode() {
1968   Register rscratch3 = r0;
1969   __ push(rscratch1);
1970   __ push(rscratch2);
1971   __ push(rscratch3);
1972   __ mov(rscratch3, (address) &BytecodeCounter::_counter_value);
1973   __ atomic_add(noreg, 1, rscratch3);
1974   __ pop(rscratch3);
1975   __ pop(rscratch2);
1976   __ pop(rscratch1);
1977 }
1978 
1979 void TemplateInterpreterGenerator::histogram_bytecode(Template* t) { ; }
1980 
1981 void TemplateInterpreterGenerator::histogram_bytecode_pair(Template* t) { ; }
1982 
1983 
1984 void TemplateInterpreterGenerator::trace_bytecode(Template* t) {
1985   // Call a little run-time stub to avoid blow-up for each bytecode.
1986   // The run-time runtime saves the right registers, depending on
1987   // the tosca in-state for the given template.
1988 
1989   assert(Interpreter::trace_code(t->tos_in()) != NULL,
1990          "entry must have been generated");
1991   __ bl(Interpreter::trace_code(t->tos_in()));
1992   __ reinit_heapbase();
1993 }
1994 
1995 
1996 void TemplateInterpreterGenerator::stop_interpreter_at() {
1997   Label L;
1998   __ push(rscratch1);
1999   __ mov(rscratch1, (address) &BytecodeCounter::_counter_value);
2000   __ ldr(rscratch1, Address(rscratch1));
2001   __ mov(rscratch2, StopInterpreterAt);
2002   __ cmpw(rscratch1, rscratch2);
2003   __ br(Assembler::NE, L);
2004   __ brk(0);
2005   __ bind(L);
2006   __ pop(rscratch1);
2007 }
2008 
2009 #endif // !PRODUCT