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