1 /*
   2  * Copyright (c) 2003, 2021, 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_INLINE_TYPE: // 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   __ stp(rfp, lr, Address(sp, 10 * wordSize));
 843   __ lea(rfp, Address(sp, 10 * wordSize));
 844 
 845   // set sender sp
 846   // leave last_sp as null
 847   __ stp(zr, r13, Address(sp, 8 * wordSize));
 848 
 849   // Move SP out of the way
 850   if (! native_call) {
 851     __ ldr(rscratch1, Address(rmethod, Method::const_offset()));
 852     __ ldrh(rscratch1, Address(rscratch1, ConstMethod::max_stack_offset()));
 853     __ add(rscratch1, rscratch1, frame::interpreter_frame_monitor_size() + 2);
 854     __ sub(rscratch1, sp, rscratch1, ext::uxtw, 3);
 855     __ andr(sp, rscratch1, -16);
 856   }
 857 }
 858 
 859 // End of helpers
 860 
 861 // Various method entries
 862 //------------------------------------------------------------------------------------------------------------------------
 863 //
 864 //
 865 
 866 // Method entry for java.lang.ref.Reference.get.
 867 address TemplateInterpreterGenerator::generate_Reference_get_entry(void) {
 868   // Code: _aload_0, _getfield, _areturn
 869   // parameter size = 1
 870   //
 871   // The code that gets generated by this routine is split into 2 parts:
 872   //    1. The "intrinsified" code for G1 (or any SATB based GC),
 873   //    2. The slow path - which is an expansion of the regular method entry.
 874   //
 875   // Notes:-
 876   // * In the G1 code we do not check whether we need to block for
 877   //   a safepoint. If G1 is enabled then we must execute the specialized
 878   //   code for Reference.get (except when the Reference object is null)
 879   //   so that we can log the value in the referent field with an SATB
 880   //   update buffer.
 881   //   If the code for the getfield template is modified so that the
 882   //   G1 pre-barrier code is executed when the current method is
 883   //   Reference.get() then going through the normal method entry
 884   //   will be fine.
 885   // * The G1 code can, however, check the receiver object (the instance
 886   //   of java.lang.Reference) and jump to the slow path if null. If the
 887   //   Reference object is null then we obviously cannot fetch the referent
 888   //   and so we don't need to call the G1 pre-barrier. Thus we can use the
 889   //   regular method entry code to generate the NPE.
 890   //
 891   // This code is based on generate_accessor_entry.
 892   //
 893   // rmethod: Method*
 894   // r13: senderSP must preserve for slow path, set SP to it on fast path
 895 
 896   // LR is live.  It must be saved around calls.
 897 
 898   address entry = __ pc();
 899 
 900   const int referent_offset = java_lang_ref_Reference::referent_offset();
 901 
 902   Label slow_path;
 903   const Register local_0 = c_rarg0;
 904   // Check if local 0 != NULL
 905   // If the receiver is null then it is OK to jump to the slow path.
 906   __ ldr(local_0, Address(esp, 0));
 907   __ cbz(local_0, slow_path);
 908 
 909   __ mov(r19, r13);   // Move senderSP to a callee-saved register
 910 
 911   // Load the value of the referent field.
 912   const Address field_address(local_0, referent_offset);
 913   BarrierSetAssembler *bs = BarrierSet::barrier_set()->barrier_set_assembler();
 914   bs->load_at(_masm, IN_HEAP | ON_WEAK_OOP_REF, T_OBJECT, local_0, field_address, /*tmp1*/ rscratch2, /*tmp2*/ rscratch1);
 915 
 916   // areturn
 917   __ andr(sp, r19, -16);  // done with stack
 918   __ ret(lr);
 919 
 920   // generate a vanilla interpreter entry as the slow path
 921   __ bind(slow_path);
 922   __ jump_to_entry(Interpreter::entry_for_kind(Interpreter::zerolocals));
 923   return entry;
 924 
 925 }
 926 
 927 /**
 928  * Method entry for static native methods:
 929  *   int java.util.zip.CRC32.update(int crc, int b)
 930  */
 931 address TemplateInterpreterGenerator::generate_CRC32_update_entry() {
 932   if (UseCRC32Intrinsics) {
 933     address entry = __ pc();
 934 
 935     // rmethod: Method*
 936     // r13: senderSP must preserved for slow path
 937     // esp: args
 938 
 939     Label slow_path;
 940     // If we need a safepoint check, generate full interpreter entry.
 941     __ safepoint_poll(slow_path, false /* at_return */, false /* acquire */, false /* in_nmethod */);
 942 
 943     // We don't generate local frame and don't align stack because
 944     // we call stub code and there is no safepoint on this path.
 945 
 946     // Load parameters
 947     const Register crc = c_rarg0;  // crc
 948     const Register val = c_rarg1;  // source java byte value
 949     const Register tbl = c_rarg2;  // scratch
 950 
 951     // Arguments are reversed on java expression stack
 952     __ ldrw(val, Address(esp, 0));              // byte value
 953     __ ldrw(crc, Address(esp, wordSize));       // Initial CRC
 954 
 955     uint64_t offset;
 956     __ adrp(tbl, ExternalAddress(StubRoutines::crc_table_addr()), offset);
 957     __ add(tbl, tbl, offset);
 958 
 959     __ mvnw(crc, crc); // ~crc
 960     __ update_byte_crc32(crc, val, tbl);
 961     __ mvnw(crc, crc); // ~crc
 962 
 963     // result in c_rarg0
 964 
 965     __ andr(sp, r13, -16);
 966     __ ret(lr);
 967 
 968     // generate a vanilla native entry as the slow path
 969     __ bind(slow_path);
 970     __ jump_to_entry(Interpreter::entry_for_kind(Interpreter::native));
 971     return entry;
 972   }
 973   return NULL;
 974 }
 975 
 976 /**
 977  * Method entry for static native methods:
 978  *   int java.util.zip.CRC32.updateBytes(int crc, byte[] b, int off, int len)
 979  *   int java.util.zip.CRC32.updateByteBuffer(int crc, long buf, int off, int len)
 980  */
 981 address TemplateInterpreterGenerator::generate_CRC32_updateBytes_entry(AbstractInterpreter::MethodKind kind) {
 982   if (UseCRC32Intrinsics) {
 983     address entry = __ pc();
 984 
 985     // rmethod,: Method*
 986     // r13: senderSP must preserved for slow path
 987 
 988     Label slow_path;
 989     // If we need a safepoint check, generate full interpreter entry.
 990     __ safepoint_poll(slow_path, false /* at_return */, false /* acquire */, false /* in_nmethod */);
 991 
 992     // We don't generate local frame and don't align stack because
 993     // we call stub code and there is no safepoint on this path.
 994 
 995     // Load parameters
 996     const Register crc = c_rarg0;  // crc
 997     const Register buf = c_rarg1;  // source java byte array address
 998     const Register len = c_rarg2;  // length
 999     const Register off = len;      // offset (never overlaps with 'len')
1000 
1001     // Arguments are reversed on java expression stack
1002     // Calculate address of start element
1003     if (kind == Interpreter::java_util_zip_CRC32_updateByteBuffer) {
1004       __ ldr(buf, Address(esp, 2*wordSize)); // long buf
1005       __ ldrw(off, Address(esp, wordSize)); // offset
1006       __ add(buf, buf, off); // + offset
1007       __ ldrw(crc,   Address(esp, 4*wordSize)); // Initial CRC
1008     } else {
1009       __ ldr(buf, Address(esp, 2*wordSize)); // byte[] array
1010       __ add(buf, buf, arrayOopDesc::base_offset_in_bytes(T_BYTE)); // + header size
1011       __ ldrw(off, Address(esp, wordSize)); // offset
1012       __ add(buf, buf, off); // + offset
1013       __ ldrw(crc,   Address(esp, 3*wordSize)); // Initial CRC
1014     }
1015     // Can now load 'len' since we're finished with 'off'
1016     __ ldrw(len, Address(esp, 0x0)); // Length
1017 
1018     __ andr(sp, r13, -16); // Restore the caller's SP
1019 
1020     // We are frameless so we can just jump to the stub.
1021     __ b(CAST_FROM_FN_PTR(address, StubRoutines::updateBytesCRC32()));
1022 
1023     // generate a vanilla native entry as the slow path
1024     __ bind(slow_path);
1025     __ jump_to_entry(Interpreter::entry_for_kind(Interpreter::native));
1026     return entry;
1027   }
1028   return NULL;
1029 }
1030 
1031 /**
1032  * Method entry for intrinsic-candidate (non-native) methods:
1033  *   int java.util.zip.CRC32C.updateBytes(int crc, byte[] b, int off, int end)
1034  *   int java.util.zip.CRC32C.updateDirectByteBuffer(int crc, long buf, int off, int end)
1035  * Unlike CRC32, CRC32C does not have any methods marked as native
1036  * CRC32C also uses an "end" variable instead of the length variable CRC32 uses
1037  */
1038 address TemplateInterpreterGenerator::generate_CRC32C_updateBytes_entry(AbstractInterpreter::MethodKind kind) {
1039   if (UseCRC32CIntrinsics) {
1040     address entry = __ pc();
1041 
1042     // Prepare jump to stub using parameters from the stack
1043     const Register crc = c_rarg0; // initial crc
1044     const Register buf = c_rarg1; // source java byte array address
1045     const Register len = c_rarg2; // len argument to the kernel
1046 
1047     const Register end = len; // index of last element to process
1048     const Register off = crc; // offset
1049 
1050     __ ldrw(end, Address(esp)); // int end
1051     __ ldrw(off, Address(esp, wordSize)); // int offset
1052     __ sub(len, end, off);
1053     __ ldr(buf, Address(esp, 2*wordSize)); // byte[] buf | long buf
1054     __ add(buf, buf, off); // + offset
1055     if (kind == Interpreter::java_util_zip_CRC32C_updateDirectByteBuffer) {
1056       __ ldrw(crc, Address(esp, 4*wordSize)); // long crc
1057     } else {
1058       __ add(buf, buf, arrayOopDesc::base_offset_in_bytes(T_BYTE)); // + header size
1059       __ ldrw(crc, Address(esp, 3*wordSize)); // long crc
1060     }
1061 
1062     __ andr(sp, r13, -16); // Restore the caller's SP
1063 
1064     // Jump to the stub.
1065     __ b(CAST_FROM_FN_PTR(address, StubRoutines::updateBytesCRC32C()));
1066 
1067     return entry;
1068   }
1069   return NULL;
1070 }
1071 
1072 void TemplateInterpreterGenerator::bang_stack_shadow_pages(bool native_call) {
1073   // Bang each page in the shadow zone. We can't assume it's been done for
1074   // an interpreter frame with greater than a page of locals, so each page
1075   // needs to be checked.  Only true for non-native.
1076   const int n_shadow_pages = (int)(StackOverflow::stack_shadow_zone_size() / os::vm_page_size());
1077   const int start_page = native_call ? n_shadow_pages : 1;
1078   const int page_size = os::vm_page_size();
1079   for (int pages = start_page; pages <= n_shadow_pages ; pages++) {
1080     __ sub(rscratch2, sp, pages*page_size);
1081     __ str(zr, Address(rscratch2));
1082   }
1083 }
1084 
1085 
1086 // Interpreter stub for calling a native method. (asm interpreter)
1087 // This sets up a somewhat different looking stack for calling the
1088 // native method than the typical interpreter frame setup.
1089 address TemplateInterpreterGenerator::generate_native_entry(bool synchronized) {
1090   // determine code generation flags
1091   bool inc_counter  = UseCompiler || CountCompiledCalls || LogTouchedMethods;
1092 
1093   // r1: Method*
1094   // rscratch1: sender sp
1095 
1096   address entry_point = __ pc();
1097 
1098   const Address constMethod       (rmethod, Method::const_offset());
1099   const Address access_flags      (rmethod, Method::access_flags_offset());
1100   const Address size_of_parameters(r2, ConstMethod::
1101                                        size_of_parameters_offset());
1102 
1103   // get parameter size (always needed)
1104   __ ldr(r2, constMethod);
1105   __ load_unsigned_short(r2, size_of_parameters);
1106 
1107   // Native calls don't need the stack size check since they have no
1108   // expression stack and the arguments are already on the stack and
1109   // we only add a handful of words to the stack.
1110 
1111   // rmethod: Method*
1112   // r2: size of parameters
1113   // rscratch1: sender sp
1114 
1115   // for natives the size of locals is zero
1116 
1117   // compute beginning of parameters (rlocals)
1118   __ add(rlocals, esp, r2, ext::uxtx, 3);
1119   __ add(rlocals, rlocals, -wordSize);
1120 
1121   // Pull SP back to minimum size: this avoids holes in the stack
1122   __ andr(sp, esp, -16);
1123 
1124   // initialize fixed part of activation frame
1125   generate_fixed_frame(true);
1126 
1127   // make sure method is native & not abstract
1128 #ifdef ASSERT
1129   __ ldrw(r0, access_flags);
1130   {
1131     Label L;
1132     __ tst(r0, JVM_ACC_NATIVE);
1133     __ br(Assembler::NE, L);
1134     __ stop("tried to execute non-native method as native");
1135     __ bind(L);
1136   }
1137   {
1138     Label L;
1139     __ tst(r0, JVM_ACC_ABSTRACT);
1140     __ br(Assembler::EQ, L);
1141     __ stop("tried to execute abstract method in interpreter");
1142     __ bind(L);
1143   }
1144 #endif
1145 
1146   // Since at this point in the method invocation the exception
1147   // handler would try to exit the monitor of synchronized methods
1148   // which hasn't been entered yet, we set the thread local variable
1149   // _do_not_unlock_if_synchronized to true. The remove_activation
1150   // will check this flag.
1151 
1152    const Address do_not_unlock_if_synchronized(rthread,
1153         in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
1154   __ mov(rscratch2, true);
1155   __ strb(rscratch2, do_not_unlock_if_synchronized);
1156 
1157   // increment invocation count & check for overflow
1158   Label invocation_counter_overflow;
1159   if (inc_counter) {
1160     generate_counter_incr(&invocation_counter_overflow);
1161   }
1162 
1163   Label continue_after_compile;
1164   __ bind(continue_after_compile);
1165 
1166   bang_stack_shadow_pages(true);
1167 
1168   // reset the _do_not_unlock_if_synchronized flag
1169   __ strb(zr, do_not_unlock_if_synchronized);
1170 
1171   // check for synchronized methods
1172   // Must happen AFTER invocation_counter check and stack overflow check,
1173   // so method is not locked if overflows.
1174   if (synchronized) {
1175     lock_method();
1176   } else {
1177     // no synchronization necessary
1178 #ifdef ASSERT
1179     {
1180       Label L;
1181       __ ldrw(r0, access_flags);
1182       __ tst(r0, JVM_ACC_SYNCHRONIZED);
1183       __ br(Assembler::EQ, L);
1184       __ stop("method needs synchronization");
1185       __ bind(L);
1186     }
1187 #endif
1188   }
1189 
1190   // start execution
1191 #ifdef ASSERT
1192   {
1193     Label L;
1194     const Address monitor_block_top(rfp,
1195                  frame::interpreter_frame_monitor_block_top_offset * wordSize);
1196     __ ldr(rscratch1, monitor_block_top);
1197     __ cmp(esp, rscratch1);
1198     __ br(Assembler::EQ, L);
1199     __ stop("broken stack frame setup in interpreter");
1200     __ bind(L);
1201   }
1202 #endif
1203 
1204   // jvmti support
1205   __ notify_method_entry();
1206 
1207   // work registers
1208   const Register t = r17;
1209   const Register result_handler = r19;
1210 
1211   // allocate space for parameters
1212   __ ldr(t, Address(rmethod, Method::const_offset()));
1213   __ load_unsigned_short(t, Address(t, ConstMethod::size_of_parameters_offset()));
1214 
1215   __ sub(rscratch1, esp, t, ext::uxtx, Interpreter::logStackElementSize);
1216   __ andr(sp, rscratch1, -16);
1217   __ mov(esp, rscratch1);
1218 
1219   // get signature handler
1220   {
1221     Label L;
1222     __ ldr(t, Address(rmethod, Method::signature_handler_offset()));
1223     __ cbnz(t, L);
1224     __ call_VM(noreg,
1225                CAST_FROM_FN_PTR(address,
1226                                 InterpreterRuntime::prepare_native_call),
1227                rmethod);
1228     __ ldr(t, Address(rmethod, Method::signature_handler_offset()));
1229     __ bind(L);
1230   }
1231 
1232   // call signature handler
1233   assert(InterpreterRuntime::SignatureHandlerGenerator::from() == rlocals,
1234          "adjust this code");
1235   assert(InterpreterRuntime::SignatureHandlerGenerator::to() == sp,
1236          "adjust this code");
1237   assert(InterpreterRuntime::SignatureHandlerGenerator::temp() == rscratch1,
1238           "adjust this code");
1239 
1240   // The generated handlers do not touch rmethod (the method).
1241   // However, large signatures cannot be cached and are generated
1242   // each time here.  The slow-path generator can do a GC on return,
1243   // so we must reload it after the call.
1244   __ blr(t);
1245   __ get_method(rmethod);        // slow path can do a GC, reload rmethod
1246 
1247 
1248   // result handler is in r0
1249   // set result handler
1250   __ mov(result_handler, r0);
1251   // pass mirror handle if static call
1252   {
1253     Label L;
1254     __ ldrw(t, Address(rmethod, Method::access_flags_offset()));
1255     __ tbz(t, exact_log2(JVM_ACC_STATIC), L);
1256     // get mirror
1257     __ load_mirror(t, rmethod);
1258     // copy mirror into activation frame
1259     __ str(t, Address(rfp, frame::interpreter_frame_oop_temp_offset * wordSize));
1260     // pass handle to mirror
1261     __ add(c_rarg1, rfp, frame::interpreter_frame_oop_temp_offset * wordSize);
1262     __ bind(L);
1263   }
1264 
1265   // get native function entry point in r10
1266   {
1267     Label L;
1268     __ ldr(r10, Address(rmethod, Method::native_function_offset()));
1269     address unsatisfied = (SharedRuntime::native_method_throw_unsatisfied_link_error_entry());
1270     __ mov(rscratch2, unsatisfied);
1271     __ ldr(rscratch2, rscratch2);
1272     __ cmp(r10, rscratch2);
1273     __ br(Assembler::NE, L);
1274     __ call_VM(noreg,
1275                CAST_FROM_FN_PTR(address,
1276                                 InterpreterRuntime::prepare_native_call),
1277                rmethod);
1278     __ get_method(rmethod);
1279     __ ldr(r10, Address(rmethod, Method::native_function_offset()));
1280     __ bind(L);
1281   }
1282 
1283   // pass JNIEnv
1284   __ add(c_rarg0, rthread, in_bytes(JavaThread::jni_environment_offset()));
1285 
1286   // Set the last Java PC in the frame anchor to be the return address from
1287   // the call to the native method: this will allow the debugger to
1288   // generate an accurate stack trace.
1289   Label native_return;
1290   __ set_last_Java_frame(esp, rfp, native_return, rscratch1);
1291 
1292   // change thread state
1293 #ifdef ASSERT
1294   {
1295     Label L;
1296     __ ldrw(t, Address(rthread, JavaThread::thread_state_offset()));
1297     __ cmp(t, (u1)_thread_in_Java);
1298     __ br(Assembler::EQ, L);
1299     __ stop("Wrong thread state in native stub");
1300     __ bind(L);
1301   }
1302 #endif
1303 
1304   // Change state to native
1305   __ mov(rscratch1, _thread_in_native);
1306   __ lea(rscratch2, Address(rthread, JavaThread::thread_state_offset()));
1307   __ stlrw(rscratch1, rscratch2);
1308 
1309   // Call the native method.
1310   __ blr(r10);
1311   __ bind(native_return);
1312   __ get_method(rmethod);
1313   // result potentially in r0 or v0
1314 
1315   // make room for the pushes we're about to do
1316   __ sub(rscratch1, esp, 4 * wordSize);
1317   __ andr(sp, rscratch1, -16);
1318 
1319   // NOTE: The order of these pushes is known to frame::interpreter_frame_result
1320   // in order to extract the result of a method call. If the order of these
1321   // pushes change or anything else is added to the stack then the code in
1322   // interpreter_frame_result must also change.
1323   __ push(dtos);
1324   __ push(ltos);
1325 
1326   __ verify_sve_vector_length();
1327 
1328   // change thread state
1329   __ mov(rscratch1, _thread_in_native_trans);
1330   __ lea(rscratch2, Address(rthread, JavaThread::thread_state_offset()));
1331   __ stlrw(rscratch1, rscratch2);
1332 
1333   // Force this write out before the read below
1334   __ dmb(Assembler::ISH);
1335 
1336   // check for safepoint operation in progress and/or pending suspend requests
1337   {
1338     Label L, Continue;
1339 
1340     // We need an acquire here to ensure that any subsequent load of the
1341     // global SafepointSynchronize::_state flag is ordered after this load
1342     // of the thread-local polling word.  We don't want this poll to
1343     // return false (i.e. not safepointing) and a later poll of the global
1344     // SafepointSynchronize::_state spuriously to return true.
1345     //
1346     // This is to avoid a race when we're in a native->Java transition
1347     // racing the code which wakes up from a safepoint.
1348     __ safepoint_poll(L, true /* at_return */, true /* acquire */, false /* in_nmethod */);
1349     __ ldrw(rscratch2, Address(rthread, JavaThread::suspend_flags_offset()));
1350     __ cbz(rscratch2, Continue);
1351     __ bind(L);
1352 
1353     // Don't use call_VM as it will see a possible pending exception
1354     // and forward it and never return here preventing us from
1355     // clearing _last_native_pc down below. So we do a runtime call by
1356     // hand.
1357     //
1358     __ mov(c_rarg0, rthread);
1359     __ mov(rscratch2, CAST_FROM_FN_PTR(address, JavaThread::check_special_condition_for_native_trans));
1360     __ blr(rscratch2);
1361     __ get_method(rmethod);
1362     __ reinit_heapbase();
1363     __ bind(Continue);
1364   }
1365 
1366   // change thread state
1367   __ mov(rscratch1, _thread_in_Java);
1368   __ lea(rscratch2, Address(rthread, JavaThread::thread_state_offset()));
1369   __ stlrw(rscratch1, rscratch2);
1370 
1371   // reset_last_Java_frame
1372   __ reset_last_Java_frame(true);
1373 
1374   if (CheckJNICalls) {
1375     // clear_pending_jni_exception_check
1376     __ str(zr, Address(rthread, JavaThread::pending_jni_exception_check_fn_offset()));
1377   }
1378 
1379   // reset handle block
1380   __ ldr(t, Address(rthread, JavaThread::active_handles_offset()));
1381   __ str(zr, Address(t, JNIHandleBlock::top_offset_in_bytes()));
1382 
1383   // If result is an oop unbox and store it in frame where gc will see it
1384   // and result handler will pick it up
1385 
1386   {
1387     Label no_oop;
1388     __ adr(t, ExternalAddress(AbstractInterpreter::result_handler(T_OBJECT)));
1389     __ cmp(t, result_handler);
1390     __ br(Assembler::NE, no_oop);
1391     // Unbox oop result, e.g. JNIHandles::resolve result.
1392     __ pop(ltos);
1393     __ resolve_jobject(r0, rthread, t);
1394     __ str(r0, Address(rfp, frame::interpreter_frame_oop_temp_offset*wordSize));
1395     // keep stack depth as expected by pushing oop which will eventually be discarded
1396     __ push(ltos);
1397     __ bind(no_oop);
1398   }
1399 
1400   {
1401     Label no_reguard;
1402     __ lea(rscratch1, Address(rthread, in_bytes(JavaThread::stack_guard_state_offset())));
1403     __ ldrw(rscratch1, Address(rscratch1));
1404     __ cmp(rscratch1, (u1)StackOverflow::stack_guard_yellow_reserved_disabled);
1405     __ br(Assembler::NE, no_reguard);
1406 
1407     __ pusha(); // XXX only save smashed registers
1408     __ mov(c_rarg0, rthread);
1409     __ mov(rscratch2, CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages));
1410     __ blr(rscratch2);
1411     __ popa(); // XXX only restore smashed registers
1412     __ bind(no_reguard);
1413   }
1414 
1415   // The method register is junk from after the thread_in_native transition
1416   // until here.  Also can't call_VM until the bcp has been
1417   // restored.  Need bcp for throwing exception below so get it now.
1418   __ get_method(rmethod);
1419 
1420   // restore bcp to have legal interpreter frame, i.e., bci == 0 <=>
1421   // rbcp == code_base()
1422   __ ldr(rbcp, Address(rmethod, Method::const_offset()));   // get ConstMethod*
1423   __ add(rbcp, rbcp, in_bytes(ConstMethod::codes_offset()));          // get codebase
1424   // handle exceptions (exception handling will handle unlocking!)
1425   {
1426     Label L;
1427     __ ldr(rscratch1, Address(rthread, Thread::pending_exception_offset()));
1428     __ cbz(rscratch1, L);
1429     // Note: At some point we may want to unify this with the code
1430     // used in call_VM_base(); i.e., we should use the
1431     // StubRoutines::forward_exception code. For now this doesn't work
1432     // here because the rsp is not correctly set at this point.
1433     __ MacroAssembler::call_VM(noreg,
1434                                CAST_FROM_FN_PTR(address,
1435                                InterpreterRuntime::throw_pending_exception));
1436     __ should_not_reach_here();
1437     __ bind(L);
1438   }
1439 
1440   // do unlocking if necessary
1441   {
1442     Label L;
1443     __ ldrw(t, Address(rmethod, Method::access_flags_offset()));
1444     __ tbz(t, exact_log2(JVM_ACC_SYNCHRONIZED), L);
1445     // the code below should be shared with interpreter macro
1446     // assembler implementation
1447     {
1448       Label unlock;
1449       // BasicObjectLock will be first in list, since this is a
1450       // synchronized method. However, need to check that the object
1451       // has not been unlocked by an explicit monitorexit bytecode.
1452 
1453       // monitor expect in c_rarg1 for slow unlock path
1454       __ lea (c_rarg1, Address(rfp,   // address of first monitor
1455                                (intptr_t)(frame::interpreter_frame_initial_sp_offset *
1456                                           wordSize - sizeof(BasicObjectLock))));
1457 
1458       __ ldr(t, Address(c_rarg1, BasicObjectLock::obj_offset_in_bytes()));
1459       __ cbnz(t, unlock);
1460 
1461       // Entry already unlocked, need to throw exception
1462       __ MacroAssembler::call_VM(noreg,
1463                                  CAST_FROM_FN_PTR(address,
1464                    InterpreterRuntime::throw_illegal_monitor_state_exception));
1465       __ should_not_reach_here();
1466 
1467       __ bind(unlock);
1468       __ unlock_object(c_rarg1);
1469     }
1470     __ bind(L);
1471   }
1472 
1473   // jvmti support
1474   // Note: This must happen _after_ handling/throwing any exceptions since
1475   //       the exception handler code notifies the runtime of method exits
1476   //       too. If this happens before, method entry/exit notifications are
1477   //       not properly paired (was bug - gri 11/22/99).
1478   __ notify_method_exit(vtos, InterpreterMacroAssembler::NotifyJVMTI);
1479 
1480   // restore potential result in r0:d0, call result handler to
1481   // restore potential result in ST0 & handle result
1482 
1483   __ pop(ltos);
1484   __ pop(dtos);
1485 
1486   __ blr(result_handler);
1487 
1488   // remove activation
1489   __ ldr(esp, Address(rfp,
1490                     frame::interpreter_frame_sender_sp_offset *
1491                     wordSize)); // get sender sp
1492   // remove frame anchor
1493   __ leave();
1494 
1495   // resture sender sp
1496   __ mov(sp, esp);
1497 
1498   __ ret(lr);
1499 
1500   if (inc_counter) {
1501     // Handle overflow of counter and compile method
1502     __ bind(invocation_counter_overflow);
1503     generate_counter_overflow(continue_after_compile);
1504   }
1505 
1506   return entry_point;
1507 }
1508 
1509 //
1510 // Generic interpreted method entry to (asm) interpreter
1511 //
1512 address TemplateInterpreterGenerator::generate_normal_entry(bool synchronized) {
1513   // determine code generation flags
1514   bool inc_counter  = UseCompiler || CountCompiledCalls || LogTouchedMethods;
1515 
1516   // rscratch1: sender sp
1517   address entry_point = __ pc();
1518 
1519   const Address constMethod(rmethod, Method::const_offset());
1520   const Address access_flags(rmethod, Method::access_flags_offset());
1521   const Address size_of_parameters(r3,
1522                                    ConstMethod::size_of_parameters_offset());
1523   const Address size_of_locals(r3, ConstMethod::size_of_locals_offset());
1524 
1525   // get parameter size (always needed)
1526   // need to load the const method first
1527   __ ldr(r3, constMethod);
1528   __ load_unsigned_short(r2, size_of_parameters);
1529 
1530   // r2: size of parameters
1531 
1532   __ load_unsigned_short(r3, size_of_locals); // get size of locals in words
1533   __ sub(r3, r3, r2); // r3 = no. of additional locals
1534 
1535   // see if we've got enough room on the stack for locals plus overhead.
1536   generate_stack_overflow_check();
1537 
1538   // compute beginning of parameters (rlocals)
1539   __ add(rlocals, esp, r2, ext::uxtx, 3);
1540   __ sub(rlocals, rlocals, wordSize);
1541 
1542   __ mov(rscratch1, esp);
1543 
1544   // r3 - # of additional locals
1545   // allocate space for locals
1546   // explicitly initialize locals
1547   // Initializing memory allocated for locals in the same direction as
1548   // the stack grows to ensure page initialization order according
1549   // to windows-aarch64 stack page growth requirement (see
1550   // https://docs.microsoft.com/en-us/cpp/build/arm64-windows-abi-conventions?view=msvc-160#stack)
1551   {
1552     Label exit, loop;
1553     __ ands(zr, r3, r3);
1554     __ br(Assembler::LE, exit); // do nothing if r3 <= 0
1555     __ bind(loop);
1556     __ str(zr, Address(__ pre(rscratch1, -wordSize)));
1557     __ sub(r3, r3, 1); // until everything initialized
1558     __ cbnz(r3, loop);
1559     __ bind(exit);
1560   }
1561 
1562   // Padding between locals and fixed part of activation frame to ensure
1563   // SP is always 16-byte aligned.
1564   __ andr(sp, rscratch1, -16);
1565 
1566   // And the base dispatch table
1567   __ get_dispatch();
1568 
1569   // initialize fixed part of activation frame
1570   generate_fixed_frame(false);
1571 
1572   // make sure method is not native & not abstract
1573 #ifdef ASSERT
1574   __ ldrw(r0, access_flags);
1575   {
1576     Label L;
1577     __ tst(r0, JVM_ACC_NATIVE);
1578     __ br(Assembler::EQ, L);
1579     __ stop("tried to execute native method as non-native");
1580     __ bind(L);
1581   }
1582  {
1583     Label L;
1584     __ tst(r0, JVM_ACC_ABSTRACT);
1585     __ br(Assembler::EQ, L);
1586     __ stop("tried to execute abstract method in interpreter");
1587     __ bind(L);
1588   }
1589 #endif
1590 
1591   // Since at this point in the method invocation the exception
1592   // handler would try to exit the monitor of synchronized methods
1593   // which hasn't been entered yet, we set the thread local variable
1594   // _do_not_unlock_if_synchronized to true. The remove_activation
1595   // will check this flag.
1596 
1597    const Address do_not_unlock_if_synchronized(rthread,
1598         in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
1599   __ mov(rscratch2, true);
1600   __ strb(rscratch2, do_not_unlock_if_synchronized);
1601 
1602   Register mdp = r3;
1603   __ profile_parameters_type(mdp, r1, r2);
1604 
1605   // increment invocation count & check for overflow
1606   Label invocation_counter_overflow;
1607   if (inc_counter) {
1608     generate_counter_incr(&invocation_counter_overflow);
1609   }
1610 
1611   Label continue_after_compile;
1612   __ bind(continue_after_compile);
1613 
1614   bang_stack_shadow_pages(false);
1615 
1616   // reset the _do_not_unlock_if_synchronized flag
1617   __ strb(zr, do_not_unlock_if_synchronized);
1618 
1619   // check for synchronized methods
1620   // Must happen AFTER invocation_counter check and stack overflow check,
1621   // so method is not locked if overflows.
1622   if (synchronized) {
1623     // Allocate monitor and lock method
1624     lock_method();
1625   } else {
1626     // no synchronization necessary
1627 #ifdef ASSERT
1628     {
1629       Label L;
1630       __ ldrw(r0, access_flags);
1631       __ tst(r0, JVM_ACC_SYNCHRONIZED);
1632       __ br(Assembler::EQ, L);
1633       __ stop("method needs synchronization");
1634       __ bind(L);
1635     }
1636 #endif
1637   }
1638 
1639   // start execution
1640 #ifdef ASSERT
1641   {
1642     Label L;
1643      const Address monitor_block_top (rfp,
1644                  frame::interpreter_frame_monitor_block_top_offset * wordSize);
1645     __ ldr(rscratch1, monitor_block_top);
1646     __ cmp(esp, rscratch1);
1647     __ br(Assembler::EQ, L);
1648     __ stop("broken stack frame setup in interpreter");
1649     __ bind(L);
1650   }
1651 #endif
1652 
1653   // jvmti support
1654   __ notify_method_entry();
1655 
1656   __ dispatch_next(vtos);
1657 
1658   // invocation counter overflow
1659   if (inc_counter) {
1660     // Handle overflow of counter and compile method
1661     __ bind(invocation_counter_overflow);
1662     generate_counter_overflow(continue_after_compile);
1663   }
1664 
1665   return entry_point;
1666 }
1667 
1668 //-----------------------------------------------------------------------------
1669 // Exceptions
1670 
1671 void TemplateInterpreterGenerator::generate_throw_exception() {
1672   // Entry point in previous activation (i.e., if the caller was
1673   // interpreted)
1674   Interpreter::_rethrow_exception_entry = __ pc();
1675   // Restore sp to interpreter_frame_last_sp even though we are going
1676   // to empty the expression stack for the exception processing.
1677   __ str(zr, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
1678   // r0: exception
1679   // r3: return address/pc that threw exception
1680   __ restore_bcp();    // rbcp points to call/send
1681   __ restore_locals();
1682   __ restore_constant_pool_cache();
1683   __ reinit_heapbase();  // restore rheapbase as heapbase.
1684   __ get_dispatch();
1685 
1686   // Entry point for exceptions thrown within interpreter code
1687   Interpreter::_throw_exception_entry = __ pc();
1688   // If we came here via a NullPointerException on the receiver of a
1689   // method, rmethod may be corrupt.
1690   __ get_method(rmethod);
1691   // expression stack is undefined here
1692   // r0: exception
1693   // rbcp: exception bcp
1694   __ verify_oop(r0);
1695   __ mov(c_rarg1, r0);
1696 
1697   // expression stack must be empty before entering the VM in case of
1698   // an exception
1699   __ empty_expression_stack();
1700   // find exception handler address and preserve exception oop
1701   __ call_VM(r3,
1702              CAST_FROM_FN_PTR(address,
1703                           InterpreterRuntime::exception_handler_for_exception),
1704              c_rarg1);
1705 
1706   // Calculate stack limit
1707   __ ldr(rscratch1, Address(rmethod, Method::const_offset()));
1708   __ ldrh(rscratch1, Address(rscratch1, ConstMethod::max_stack_offset()));
1709   __ add(rscratch1, rscratch1, frame::interpreter_frame_monitor_size() + 4);
1710   __ ldr(rscratch2,
1711          Address(rfp, frame::interpreter_frame_initial_sp_offset * wordSize));
1712   __ sub(rscratch1, rscratch2, rscratch1, ext::uxtx, 3);
1713   __ andr(sp, rscratch1, -16);
1714 
1715   // r0: exception handler entry point
1716   // r3: preserved exception oop
1717   // rbcp: bcp for exception handler
1718   __ push_ptr(r3); // push exception which is now the only value on the stack
1719   __ br(r0); // jump to exception handler (may be _remove_activation_entry!)
1720 
1721   // If the exception is not handled in the current frame the frame is
1722   // removed and the exception is rethrown (i.e. exception
1723   // continuation is _rethrow_exception).
1724   //
1725   // Note: At this point the bci is still the bxi for the instruction
1726   // which caused the exception and the expression stack is
1727   // empty. Thus, for any VM calls at this point, GC will find a legal
1728   // oop map (with empty expression stack).
1729 
1730   //
1731   // JVMTI PopFrame support
1732   //
1733 
1734   Interpreter::_remove_activation_preserving_args_entry = __ pc();
1735   __ empty_expression_stack();
1736   // Set the popframe_processing bit in pending_popframe_condition
1737   // indicating that we are currently handling popframe, so that
1738   // call_VMs that may happen later do not trigger new popframe
1739   // handling cycles.
1740   __ ldrw(r3, Address(rthread, JavaThread::popframe_condition_offset()));
1741   __ orr(r3, r3, JavaThread::popframe_processing_bit);
1742   __ strw(r3, Address(rthread, JavaThread::popframe_condition_offset()));
1743 
1744   {
1745     // Check to see whether we are returning to a deoptimized frame.
1746     // (The PopFrame call ensures that the caller of the popped frame is
1747     // either interpreted or compiled and deoptimizes it if compiled.)
1748     // In this case, we can't call dispatch_next() after the frame is
1749     // popped, but instead must save the incoming arguments and restore
1750     // them after deoptimization has occurred.
1751     //
1752     // Note that we don't compare the return PC against the
1753     // deoptimization blob's unpack entry because of the presence of
1754     // adapter frames in C2.
1755     Label caller_not_deoptimized;
1756     __ ldr(c_rarg1, Address(rfp, frame::return_addr_offset * wordSize));
1757     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1758                                InterpreterRuntime::interpreter_contains), c_rarg1);
1759     __ cbnz(r0, caller_not_deoptimized);
1760 
1761     // Compute size of arguments for saving when returning to
1762     // deoptimized caller
1763     __ get_method(r0);
1764     __ ldr(r0, Address(r0, Method::const_offset()));
1765     __ load_unsigned_short(r0, Address(r0, in_bytes(ConstMethod::
1766                                                     size_of_parameters_offset())));
1767     __ lsl(r0, r0, Interpreter::logStackElementSize);
1768     __ restore_locals(); // XXX do we need this?
1769     __ sub(rlocals, rlocals, r0);
1770     __ add(rlocals, rlocals, wordSize);
1771     // Save these arguments
1772     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1773                                            Deoptimization::
1774                                            popframe_preserve_args),
1775                           rthread, r0, rlocals);
1776 
1777     __ remove_activation(vtos,
1778                          /* throw_monitor_exception */ false,
1779                          /* install_monitor_exception */ false,
1780                          /* notify_jvmdi */ false);
1781 
1782     // Inform deoptimization that it is responsible for restoring
1783     // these arguments
1784     __ mov(rscratch1, JavaThread::popframe_force_deopt_reexecution_bit);
1785     __ strw(rscratch1, Address(rthread, JavaThread::popframe_condition_offset()));
1786 
1787     // Continue in deoptimization handler
1788     __ ret(lr);
1789 
1790     __ bind(caller_not_deoptimized);
1791   }
1792 
1793   __ remove_activation(vtos,
1794                        /* throw_monitor_exception */ false,
1795                        /* install_monitor_exception */ false,
1796                        /* notify_jvmdi */ false);
1797 
1798   // Restore the last_sp and null it out
1799   __ ldr(esp, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
1800   __ str(zr, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
1801 
1802   __ restore_bcp();
1803   __ restore_locals();
1804   __ restore_constant_pool_cache();
1805   __ get_method(rmethod);
1806   __ get_dispatch();
1807 
1808   // The method data pointer was incremented already during
1809   // call profiling. We have to restore the mdp for the current bcp.
1810   if (ProfileInterpreter) {
1811     __ set_method_data_pointer_for_bcp();
1812   }
1813 
1814   // Clear the popframe condition flag
1815   __ strw(zr, Address(rthread, JavaThread::popframe_condition_offset()));
1816   assert(JavaThread::popframe_inactive == 0, "fix popframe_inactive");
1817 
1818 #if INCLUDE_JVMTI
1819   {
1820     Label L_done;
1821 
1822     __ ldrb(rscratch1, Address(rbcp, 0));
1823     __ cmpw(rscratch1, Bytecodes::_invokestatic);
1824     __ br(Assembler::NE, L_done);
1825 
1826     // The member name argument must be restored if _invokestatic is re-executed after a PopFrame call.
1827     // Detect such a case in the InterpreterRuntime function and return the member name argument, or NULL.
1828 
1829     __ ldr(c_rarg0, Address(rlocals, 0));
1830     __ call_VM(r0, CAST_FROM_FN_PTR(address, InterpreterRuntime::member_name_arg_or_null), c_rarg0, rmethod, rbcp);
1831 
1832     __ cbz(r0, L_done);
1833 
1834     __ str(r0, Address(esp, 0));
1835     __ bind(L_done);
1836   }
1837 #endif // INCLUDE_JVMTI
1838 
1839   // Restore machine SP
1840   __ ldr(rscratch1, Address(rmethod, Method::const_offset()));
1841   __ ldrh(rscratch1, Address(rscratch1, ConstMethod::max_stack_offset()));
1842   __ add(rscratch1, rscratch1, frame::interpreter_frame_monitor_size() + 4);
1843   __ ldr(rscratch2,
1844          Address(rfp, frame::interpreter_frame_initial_sp_offset * wordSize));
1845   __ sub(rscratch1, rscratch2, rscratch1, ext::uxtw, 3);
1846   __ andr(sp, rscratch1, -16);
1847 
1848   __ dispatch_next(vtos);
1849   // end of PopFrame support
1850 
1851   Interpreter::_remove_activation_entry = __ pc();
1852 
1853   // preserve exception over this code sequence
1854   __ pop_ptr(r0);
1855   __ str(r0, Address(rthread, JavaThread::vm_result_offset()));
1856   // remove the activation (without doing throws on illegalMonitorExceptions)
1857   __ remove_activation(vtos, false, true, false);
1858   // restore exception
1859   __ get_vm_result(r0, rthread);
1860 
1861   // In between activations - previous activation type unknown yet
1862   // compute continuation point - the continuation point expects the
1863   // following registers set up:
1864   //
1865   // r0: exception
1866   // lr: return address/pc that threw exception
1867   // esp: expression stack of caller
1868   // rfp: fp of caller
1869   __ stp(r0, lr, Address(__ pre(sp, -2 * wordSize)));  // save exception & return address
1870   __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1871                           SharedRuntime::exception_handler_for_return_address),
1872                         rthread, lr);
1873   __ mov(r1, r0);                               // save exception handler
1874   __ ldp(r0, lr, Address(__ post(sp, 2 * wordSize)));  // restore exception & return address
1875   // We might be returning to a deopt handler that expects r3 to
1876   // contain the exception pc
1877   __ mov(r3, lr);
1878   // Note that an "issuing PC" is actually the next PC after the call
1879   __ br(r1);                                    // jump to exception
1880                                                 // handler of caller
1881 }
1882 
1883 
1884 //
1885 // JVMTI ForceEarlyReturn support
1886 //
1887 address TemplateInterpreterGenerator::generate_earlyret_entry_for(TosState state) {
1888   address entry = __ pc();
1889 
1890   __ restore_bcp();
1891   __ restore_locals();
1892   __ empty_expression_stack();
1893   __ load_earlyret_value(state);
1894 
1895   __ ldr(rscratch1, Address(rthread, JavaThread::jvmti_thread_state_offset()));
1896   Address cond_addr(rscratch1, JvmtiThreadState::earlyret_state_offset());
1897 
1898   // Clear the earlyret state
1899   assert(JvmtiThreadState::earlyret_inactive == 0, "should be");
1900   __ str(zr, cond_addr);
1901 
1902   __ remove_activation(state,
1903                        false, /* throw_monitor_exception */
1904                        false, /* install_monitor_exception */
1905                        true); /* notify_jvmdi */
1906   __ ret(lr);
1907 
1908   return entry;
1909 } // end of ForceEarlyReturn support
1910 
1911 
1912 
1913 //-----------------------------------------------------------------------------
1914 // Helper for vtos entry point generation
1915 
1916 void TemplateInterpreterGenerator::set_vtos_entry_points(Template* t,
1917                                                          address& bep,
1918                                                          address& cep,
1919                                                          address& sep,
1920                                                          address& aep,
1921                                                          address& iep,
1922                                                          address& lep,
1923                                                          address& fep,
1924                                                          address& dep,
1925                                                          address& vep) {
1926   assert(t->is_valid() && t->tos_in() == vtos, "illegal template");
1927   Label L;
1928   aep = __ pc();  __ push_ptr();  __ b(L);
1929   fep = __ pc();  __ push_f();    __ b(L);
1930   dep = __ pc();  __ push_d();    __ b(L);
1931   lep = __ pc();  __ push_l();    __ b(L);
1932   bep = cep = sep =
1933   iep = __ pc();  __ push_i();
1934   vep = __ pc();
1935   __ bind(L);
1936   generate_and_dispatch(t);
1937 }
1938 
1939 //-----------------------------------------------------------------------------
1940 
1941 // Non-product code
1942 #ifndef PRODUCT
1943 address TemplateInterpreterGenerator::generate_trace_code(TosState state) {
1944   address entry = __ pc();
1945 
1946   __ push(lr);
1947   __ push(state);
1948   __ push(RegSet::range(r0, r15), sp);
1949   __ mov(c_rarg2, r0);  // Pass itos
1950   __ call_VM(noreg,
1951              CAST_FROM_FN_PTR(address, InterpreterRuntime::trace_bytecode),
1952              c_rarg1, c_rarg2, c_rarg3);
1953   __ pop(RegSet::range(r0, r15), sp);
1954   __ pop(state);
1955   __ pop(lr);
1956   __ ret(lr);                                   // return from result handler
1957 
1958   return entry;
1959 }
1960 
1961 void TemplateInterpreterGenerator::count_bytecode() {
1962   Register rscratch3 = r0;
1963   __ push(rscratch1);
1964   __ push(rscratch2);
1965   __ push(rscratch3);
1966   __ mov(rscratch3, (address) &BytecodeCounter::_counter_value);
1967   __ atomic_add(noreg, 1, rscratch3);
1968   __ pop(rscratch3);
1969   __ pop(rscratch2);
1970   __ pop(rscratch1);
1971 }
1972 
1973 void TemplateInterpreterGenerator::histogram_bytecode(Template* t) { ; }
1974 
1975 void TemplateInterpreterGenerator::histogram_bytecode_pair(Template* t) { ; }
1976 
1977 
1978 void TemplateInterpreterGenerator::trace_bytecode(Template* t) {
1979   // Call a little run-time stub to avoid blow-up for each bytecode.
1980   // The run-time runtime saves the right registers, depending on
1981   // the tosca in-state for the given template.
1982 
1983   assert(Interpreter::trace_code(t->tos_in()) != NULL,
1984          "entry must have been generated");
1985   __ bl(Interpreter::trace_code(t->tos_in()));
1986   __ reinit_heapbase();
1987 }
1988 
1989 
1990 void TemplateInterpreterGenerator::stop_interpreter_at() {
1991   Label L;
1992   __ push(rscratch1);
1993   __ mov(rscratch1, (address) &BytecodeCounter::_counter_value);
1994   __ ldr(rscratch1, Address(rscratch1));
1995   __ mov(rscratch2, StopInterpreterAt);
1996   __ cmpw(rscratch1, rscratch2);
1997   __ br(Assembler::NE, L);
1998   __ brk(0);
1999   __ bind(L);
2000   __ pop(rscratch1);
2001 }
2002 
2003 #endif // !PRODUCT