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