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