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