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