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   __ call_VM(noreg, runtime_entry);

 577   __ membar(Assembler::AnyAny);
 578   __ dispatch_via(vtos, Interpreter::_normal_table.table_for(vtos));
 579   return entry;
 580 }
 581 
 582 // Helpers for commoning out cases in the various type of method entries.
 583 //
 584 
 585 
 586 // increment invocation count & check for overflow
 587 //
 588 // Note: checking for negative value instead of overflow
 589 //       so we have a 'sticky' overflow test
 590 //
 591 // rmethod: method
 592 //
 593 void TemplateInterpreterGenerator::generate_counter_incr(Label* overflow) {
 594   Label done;
 595   // Note: In tiered we increment either counters in Method* or in MDO depending if we're profiling or not.
 596   int increment = InvocationCounter::count_increment;
 597   Label no_mdo;
 598   if (ProfileInterpreter) {
 599     // Are we profiling?
 600     __ ldr(r0, Address(rmethod, Method::method_data_offset()));
 601     __ cbz(r0, no_mdo);
 602     // Increment counter in the MDO
 603     const Address mdo_invocation_counter(r0, in_bytes(MethodData::invocation_counter_offset()) +
 604                                               in_bytes(InvocationCounter::counter_offset()));
 605     const Address mask(r0, in_bytes(MethodData::invoke_mask_offset()));
 606     __ increment_mask_and_jump(mdo_invocation_counter, increment, mask, rscratch1, rscratch2, false, Assembler::EQ, overflow);
 607     __ b(done);
 608   }
 609   __ bind(no_mdo);
 610   // Increment counter in MethodCounters
 611   const Address invocation_counter(rscratch2,
 612                 MethodCounters::invocation_counter_offset() +
 613                 InvocationCounter::counter_offset());
 614   __ get_method_counters(rmethod, rscratch2, done);
 615   const Address mask(rscratch2, in_bytes(MethodCounters::invoke_mask_offset()));
 616   __ increment_mask_and_jump(invocation_counter, increment, mask, rscratch1, r1, false, Assembler::EQ, overflow);
 617   __ bind(done);
 618 }
 619 
 620 void TemplateInterpreterGenerator::generate_counter_overflow(Label& do_continue) {
 621 
 622   // Asm interpreter on entry
 623   // On return (i.e. jump to entry_point) [ back to invocation of interpreter ]
 624   // Everything as it was on entry
 625 
 626   // InterpreterRuntime::frequency_counter_overflow takes two
 627   // arguments, the first (thread) is passed by call_VM, the second
 628   // indicates if the counter overflow occurs at a backwards branch
 629   // (NULL bcp).  We pass zero for it.  The call returns the address
 630   // of the verified entry point for the method or NULL if the
 631   // compilation did not complete (either went background or bailed
 632   // out).
 633   __ mov(c_rarg1, 0);
 634   __ call_VM(noreg,
 635              CAST_FROM_FN_PTR(address,
 636                               InterpreterRuntime::frequency_counter_overflow),
 637              c_rarg1);
 638 
 639   __ b(do_continue);
 640 }
 641 
 642 // See if we've got enough room on the stack for locals plus overhead
 643 // below JavaThread::stack_overflow_limit(). If not, throw a StackOverflowError
 644 // without going through the signal handler, i.e., reserved and yellow zones
 645 // will not be made usable. The shadow zone must suffice to handle the
 646 // overflow.
 647 // The expression stack grows down incrementally, so the normal guard
 648 // page mechanism will work for that.
 649 //
 650 // NOTE: Since the additional locals are also always pushed (wasn't
 651 // obvious in generate_method_entry) so the guard should work for them
 652 // too.
 653 //
 654 // Args:
 655 //      r3: number of additional locals this frame needs (what we must check)
 656 //      rmethod: Method*
 657 //
 658 // Kills:
 659 //      r0
 660 void TemplateInterpreterGenerator::generate_stack_overflow_check(void) {
 661 
 662   // monitor entry size: see picture of stack set
 663   // (generate_method_entry) and frame_amd64.hpp
 664   const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
 665 
 666   // total overhead size: entry_size + (saved rbp through expr stack
 667   // bottom).  be sure to change this if you add/subtract anything
 668   // to/from the overhead area
 669   const int overhead_size =
 670     -(frame::interpreter_frame_initial_sp_offset * wordSize) + entry_size;
 671 
 672   const int page_size = os::vm_page_size();
 673 
 674   Label after_frame_check;
 675 
 676   // see if the frame is greater than one page in size. If so,
 677   // then we need to verify there is enough stack space remaining
 678   // for the additional locals.
 679   //
 680   // Note that we use SUBS rather than CMP here because the immediate
 681   // field of this instruction may overflow.  SUBS can cope with this
 682   // because it is a macro that will expand to some number of MOV
 683   // instructions and a register operation.
 684   __ subs(rscratch1, r3, (page_size - overhead_size) / Interpreter::stackElementSize);
 685   __ br(Assembler::LS, after_frame_check);
 686 
 687   // compute rsp as if this were going to be the last frame on
 688   // the stack before the red zone
 689 
 690   // locals + overhead, in bytes
 691   __ mov(r0, overhead_size);
 692   __ add(r0, r0, r3, Assembler::LSL, Interpreter::logStackElementSize);  // 2 slots per parameter.
 693 
 694   const Address stack_limit(rthread, JavaThread::stack_overflow_limit_offset());
 695   __ ldr(rscratch1, stack_limit);
 696 
 697 #ifdef ASSERT
 698   Label limit_okay;
 699   // Verify that thread stack limit is non-zero.
 700   __ cbnz(rscratch1, limit_okay);
 701   __ stop("stack overflow limit is zero");
 702   __ bind(limit_okay);
 703 #endif
 704 
 705   // Add stack limit to locals.
 706   __ add(r0, r0, rscratch1);
 707 
 708   // Check against the current stack bottom.
 709   __ cmp(sp, r0);
 710   __ br(Assembler::HI, after_frame_check);
 711 
 712   // Remove the incoming args, peeling the machine SP back to where it
 713   // was in the caller.  This is not strictly necessary, but unless we
 714   // do so the stack frame may have a garbage FP; this ensures a
 715   // correct call stack that we can always unwind.  The ANDR should be
 716   // unnecessary because the sender SP in r13 is always aligned, but
 717   // it doesn't hurt.
 718   __ andr(sp, r13, -16);
 719 
 720   // Note: the restored frame is not necessarily interpreted.
 721   // Use the shared runtime version of the StackOverflowError.
 722   assert(StubRoutines::throw_StackOverflowError_entry() != NULL, "stub not yet generated");
 723   __ far_jump(RuntimeAddress(StubRoutines::throw_StackOverflowError_entry()));
 724 
 725   // all done with frame size check
 726   __ bind(after_frame_check);
 727 }
 728 
 729 // Allocate monitor and lock method (asm interpreter)
 730 //
 731 // Args:
 732 //      rmethod: Method*
 733 //      rlocals: locals
 734 //
 735 // Kills:
 736 //      r0
 737 //      c_rarg0, c_rarg1, c_rarg2, c_rarg3, ...(param regs)
 738 //      rscratch1, rscratch2 (scratch regs)
 739 void TemplateInterpreterGenerator::lock_method() {
 740   // synchronize method
 741   const Address access_flags(rmethod, Method::access_flags_offset());
 742   const Address monitor_block_top(
 743         rfp,
 744         frame::interpreter_frame_monitor_block_top_offset * wordSize);
 745   const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
 746 
 747 #ifdef ASSERT
 748   {
 749     Label L;
 750     __ ldrw(r0, access_flags);
 751     __ tst(r0, JVM_ACC_SYNCHRONIZED);
 752     __ br(Assembler::NE, L);
 753     __ stop("method doesn't need synchronization");
 754     __ bind(L);
 755   }
 756 #endif // ASSERT
 757 
 758   // get synchronization object
 759   {
 760     Label done;
 761     __ ldrw(r0, access_flags);
 762     __ tst(r0, JVM_ACC_STATIC);
 763     // get receiver (assume this is frequent case)
 764     __ ldr(r0, Address(rlocals, Interpreter::local_offset_in_bytes(0)));
 765     __ br(Assembler::EQ, done);
 766     __ load_mirror(r0, rmethod);
 767 
 768 #ifdef ASSERT
 769     {
 770       Label L;
 771       __ cbnz(r0, L);
 772       __ stop("synchronization object is NULL");
 773       __ bind(L);
 774     }
 775 #endif // ASSERT
 776 
 777     __ bind(done);
 778   }
 779 
 780   // add space for monitor & lock
 781   __ sub(sp, sp, entry_size); // add space for a monitor entry
 782   __ sub(esp, esp, entry_size);
 783   __ mov(rscratch1, esp);
 784   __ str(rscratch1, monitor_block_top);  // set new monitor block top
 785   // store object
 786   __ str(r0, Address(esp, BasicObjectLock::obj_offset_in_bytes()));
 787   __ mov(c_rarg1, esp); // object address
 788   __ lock_object(c_rarg1);

 789 }
 790 
 791 // Generate a fixed interpreter frame. This is identical setup for
 792 // interpreted methods and for native methods hence the shared code.
 793 //
 794 // Args:
 795 //      lr: return address
 796 //      rmethod: Method*
 797 //      rlocals: pointer to locals
 798 //      rcpool: cp cache
 799 //      stack_pointer: previous sp
 800 void TemplateInterpreterGenerator::generate_fixed_frame(bool native_call) {
 801   // initialize fixed part of activation frame
 802   if (native_call) {
 803     __ sub(esp, sp, 14 *  wordSize);
 804     __ mov(rbcp, zr);
 805     __ stp(esp, zr, Address(__ pre(sp, -14 * wordSize)));
 806     // add 2 zero-initialized slots for native calls
 807     __ stp(zr, zr, Address(sp, 12 * wordSize));
 808   } else {
 809     __ sub(esp, sp, 12 *  wordSize);
 810     __ ldr(rscratch1, Address(rmethod, Method::const_offset()));      // get ConstMethod
 811     __ add(rbcp, rscratch1, in_bytes(ConstMethod::codes_offset())); // get codebase
 812     __ stp(esp, rbcp, Address(__ pre(sp, -12 * wordSize)));
 813   }
 814 
 815   if (ProfileInterpreter) {
 816     Label method_data_continue;
 817     __ ldr(rscratch1, Address(rmethod, Method::method_data_offset()));
 818     __ cbz(rscratch1, method_data_continue);
 819     __ lea(rscratch1, Address(rscratch1, in_bytes(MethodData::data_offset())));
 820     __ bind(method_data_continue);
 821     __ stp(rscratch1, rmethod, Address(sp, 6 * wordSize));  // save Method* and mdp (method data pointer)
 822   } else {
 823     __ stp(zr, rmethod, Address(sp, 6 * wordSize));        // save Method* (no mdp)
 824   }
 825 
 826   // Get mirror and store it in the frame as GC root for this Method*
 827   __ load_mirror(r10, rmethod);
 828   __ stp(r10, zr, Address(sp, 4 * wordSize));
 829 
 830   __ ldr(rcpool, Address(rmethod, Method::const_offset()));
 831   __ ldr(rcpool, Address(rcpool, ConstMethod::constants_offset()));
 832   __ ldr(rcpool, Address(rcpool, ConstantPool::cache_offset_in_bytes()));
 833   __ stp(rlocals, rcpool, Address(sp, 2 * wordSize));
 834 
 835   __ stp(rfp, lr, Address(sp, 10 * wordSize));
 836   __ lea(rfp, Address(sp, 10 * wordSize));
 837 
 838   // set sender sp
 839   // leave last_sp as null
 840   __ stp(zr, r13, Address(sp, 8 * wordSize));
 841 
 842   // Move SP out of the way
 843   if (! native_call) {
 844     __ ldr(rscratch1, Address(rmethod, Method::const_offset()));
 845     __ ldrh(rscratch1, Address(rscratch1, ConstMethod::max_stack_offset()));
 846     __ add(rscratch1, rscratch1, frame::interpreter_frame_monitor_size() + 2);
 847     __ sub(rscratch1, sp, rscratch1, ext::uxtw, 3);
 848     __ andr(sp, rscratch1, -16);
 849   }
 850 }
 851 
 852 // End of helpers
 853 










 854 // Various method entries
 855 //------------------------------------------------------------------------------------------------------------------------
 856 //
 857 //
 858 
 859 // Method entry for java.lang.ref.Reference.get.
 860 address TemplateInterpreterGenerator::generate_Reference_get_entry(void) {
 861   // Code: _aload_0, _getfield, _areturn
 862   // parameter size = 1
 863   //
 864   // The code that gets generated by this routine is split into 2 parts:
 865   //    1. The "intrinsified" code for G1 (or any SATB based GC),
 866   //    2. The slow path - which is an expansion of the regular method entry.
 867   //
 868   // Notes:-
 869   // * In the G1 code we do not check whether we need to block for
 870   //   a safepoint. If G1 is enabled then we must execute the specialized
 871   //   code for Reference.get (except when the Reference object is null)
 872   //   so that we can log the value in the referent field with an SATB
 873   //   update buffer.
 874   //   If the code for the getfield template is modified so that the
 875   //   G1 pre-barrier code is executed when the current method is
 876   //   Reference.get() then going through the normal method entry
 877   //   will be fine.
 878   // * The G1 code can, however, check the receiver object (the instance
 879   //   of java.lang.Reference) and jump to the slow path if null. If the
 880   //   Reference object is null then we obviously cannot fetch the referent
 881   //   and so we don't need to call the G1 pre-barrier. Thus we can use the
 882   //   regular method entry code to generate the NPE.
 883   //
 884   // This code is based on generate_accessor_entry.
 885   //
 886   // rmethod: Method*
 887   // r13: senderSP must preserve for slow path, set SP to it on fast path
 888 
 889   // LR is live.  It must be saved around calls.
 890 
 891   address entry = __ pc();
 892 
 893   const int referent_offset = java_lang_ref_Reference::referent_offset();
 894 
 895   Label slow_path;
 896   const Register local_0 = c_rarg0;
 897   // Check if local 0 != NULL
 898   // If the receiver is null then it is OK to jump to the slow path.
 899   __ ldr(local_0, Address(esp, 0));
 900   __ cbz(local_0, slow_path);
 901 
 902   __ mov(r19, r13);   // Move senderSP to a callee-saved register
 903 
 904   // Load the value of the referent field.
 905   const Address field_address(local_0, referent_offset);
 906   BarrierSetAssembler *bs = BarrierSet::barrier_set()->barrier_set_assembler();
 907   bs->load_at(_masm, IN_HEAP | ON_WEAK_OOP_REF, T_OBJECT, local_0, field_address, /*tmp1*/ rscratch2, /*tmp2*/ rscratch1);
 908 
 909   // areturn
 910   __ andr(sp, r19, -16);  // done with stack
 911   __ ret(lr);
 912 
 913   // generate a vanilla interpreter entry as the slow path
 914   __ bind(slow_path);
 915   __ jump_to_entry(Interpreter::entry_for_kind(Interpreter::zerolocals));
 916   return entry;
 917 
 918 }
 919 
 920 /**
 921  * Method entry for static native methods:
 922  *   int java.util.zip.CRC32.update(int crc, int b)
 923  */
 924 address TemplateInterpreterGenerator::generate_CRC32_update_entry() {
 925   if (UseCRC32Intrinsics) {
 926     address entry = __ pc();
 927 
 928     // rmethod: Method*
 929     // r13: senderSP must preserved for slow path
 930     // esp: args
 931 
 932     Label slow_path;
 933     // If we need a safepoint check, generate full interpreter entry.
 934     __ safepoint_poll(slow_path, false /* at_return */, false /* acquire */, false /* in_nmethod */);
 935 
 936     // We don't generate local frame and don't align stack because
 937     // we call stub code and there is no safepoint on this path.
 938 
 939     // Load parameters
 940     const Register crc = c_rarg0;  // crc
 941     const Register val = c_rarg1;  // source java byte value
 942     const Register tbl = c_rarg2;  // scratch
 943 
 944     // Arguments are reversed on java expression stack
 945     __ ldrw(val, Address(esp, 0));              // byte value
 946     __ ldrw(crc, Address(esp, wordSize));       // Initial CRC
 947 
 948     uint64_t offset;
 949     __ adrp(tbl, ExternalAddress(StubRoutines::crc_table_addr()), offset);
 950     __ add(tbl, tbl, offset);
 951 
 952     __ mvnw(crc, crc); // ~crc
 953     __ update_byte_crc32(crc, val, tbl);
 954     __ mvnw(crc, crc); // ~crc
 955 
 956     // result in c_rarg0
 957 
 958     __ andr(sp, r13, -16);
 959     __ ret(lr);
 960 
 961     // generate a vanilla native entry as the slow path
 962     __ bind(slow_path);
 963     __ jump_to_entry(Interpreter::entry_for_kind(Interpreter::native));
 964     return entry;
 965   }
 966   return NULL;
 967 }
 968 
 969 /**
 970  * Method entry for static native methods:
 971  *   int java.util.zip.CRC32.updateBytes(int crc, byte[] b, int off, int len)
 972  *   int java.util.zip.CRC32.updateByteBuffer(int crc, long buf, int off, int len)
 973  */
 974 address TemplateInterpreterGenerator::generate_CRC32_updateBytes_entry(AbstractInterpreter::MethodKind kind) {
 975   if (UseCRC32Intrinsics) {
 976     address entry = __ pc();
 977 
 978     // rmethod,: Method*
 979     // r13: senderSP must preserved for slow path
 980 
 981     Label slow_path;
 982     // If we need a safepoint check, generate full interpreter entry.
 983     __ safepoint_poll(slow_path, false /* at_return */, false /* acquire */, false /* in_nmethod */);
 984 
 985     // We don't generate local frame and don't align stack because
 986     // we call stub code and there is no safepoint on this path.
 987 
 988     // Load parameters
 989     const Register crc = c_rarg0;  // crc
 990     const Register buf = c_rarg1;  // source java byte array address
 991     const Register len = c_rarg2;  // length
 992     const Register off = len;      // offset (never overlaps with 'len')
 993 
 994     // Arguments are reversed on java expression stack
 995     // Calculate address of start element
 996     if (kind == Interpreter::java_util_zip_CRC32_updateByteBuffer) {
 997       __ ldr(buf, Address(esp, 2*wordSize)); // long buf
 998       __ ldrw(off, Address(esp, wordSize)); // offset
 999       __ add(buf, buf, off); // + offset
1000       __ ldrw(crc,   Address(esp, 4*wordSize)); // Initial CRC
1001     } else {
1002       __ ldr(buf, Address(esp, 2*wordSize)); // byte[] array
1003       __ add(buf, buf, arrayOopDesc::base_offset_in_bytes(T_BYTE)); // + header size
1004       __ ldrw(off, Address(esp, wordSize)); // offset
1005       __ add(buf, buf, off); // + offset
1006       __ ldrw(crc,   Address(esp, 3*wordSize)); // Initial CRC
1007     }
1008     // Can now load 'len' since we're finished with 'off'
1009     __ ldrw(len, Address(esp, 0x0)); // Length
1010 
1011     __ andr(sp, r13, -16); // Restore the caller's SP
1012 
1013     // We are frameless so we can just jump to the stub.
1014     __ b(CAST_FROM_FN_PTR(address, StubRoutines::updateBytesCRC32()));
1015 
1016     // generate a vanilla native entry as the slow path
1017     __ bind(slow_path);
1018     __ jump_to_entry(Interpreter::entry_for_kind(Interpreter::native));
1019     return entry;
1020   }
1021   return NULL;
1022 }
1023 
1024 /**
1025  * Method entry for intrinsic-candidate (non-native) methods:
1026  *   int java.util.zip.CRC32C.updateBytes(int crc, byte[] b, int off, int end)
1027  *   int java.util.zip.CRC32C.updateDirectByteBuffer(int crc, long buf, int off, int end)
1028  * Unlike CRC32, CRC32C does not have any methods marked as native
1029  * CRC32C also uses an "end" variable instead of the length variable CRC32 uses
1030  */
1031 address TemplateInterpreterGenerator::generate_CRC32C_updateBytes_entry(AbstractInterpreter::MethodKind kind) {
1032   if (UseCRC32CIntrinsics) {
1033     address entry = __ pc();
1034 
1035     // Prepare jump to stub using parameters from the stack
1036     const Register crc = c_rarg0; // initial crc
1037     const Register buf = c_rarg1; // source java byte array address
1038     const Register len = c_rarg2; // len argument to the kernel
1039 
1040     const Register end = len; // index of last element to process
1041     const Register off = crc; // offset
1042 
1043     __ ldrw(end, Address(esp)); // int end
1044     __ ldrw(off, Address(esp, wordSize)); // int offset
1045     __ sub(len, end, off);
1046     __ ldr(buf, Address(esp, 2*wordSize)); // byte[] buf | long buf
1047     __ add(buf, buf, off); // + offset
1048     if (kind == Interpreter::java_util_zip_CRC32C_updateDirectByteBuffer) {
1049       __ ldrw(crc, Address(esp, 4*wordSize)); // long crc
1050     } else {
1051       __ add(buf, buf, arrayOopDesc::base_offset_in_bytes(T_BYTE)); // + header size
1052       __ ldrw(crc, Address(esp, 3*wordSize)); // long crc
1053     }
1054 
1055     __ andr(sp, r13, -16); // Restore the caller's SP
1056 
1057     // Jump to the stub.
1058     __ b(CAST_FROM_FN_PTR(address, StubRoutines::updateBytesCRC32C()));
1059 
1060     return entry;
1061   }
1062   return NULL;
1063 }
1064 
1065 void TemplateInterpreterGenerator::bang_stack_shadow_pages(bool native_call) {
1066   // Bang each page in the shadow zone. We can't assume it's been done for
1067   // an interpreter frame with greater than a page of locals, so each page
1068   // needs to be checked.  Only true for non-native.
1069   const int n_shadow_pages = (int)(StackOverflow::stack_shadow_zone_size() / os::vm_page_size());
1070   const int start_page = native_call ? n_shadow_pages : 1;
1071   const int page_size = os::vm_page_size();
1072   for (int pages = start_page; pages <= n_shadow_pages ; pages++) {
1073     __ sub(rscratch2, sp, pages*page_size);
1074     __ str(zr, Address(rscratch2));
1075   }
1076 }
1077 
1078 
1079 // Interpreter stub for calling a native method. (asm interpreter)
1080 // This sets up a somewhat different looking stack for calling the
1081 // native method than the typical interpreter frame setup.
1082 address TemplateInterpreterGenerator::generate_native_entry(bool synchronized) {
1083   // determine code generation flags
1084   bool inc_counter  = UseCompiler || CountCompiledCalls || LogTouchedMethods;
1085 
1086   // r1: Method*
1087   // rscratch1: sender sp
1088 
1089   address entry_point = __ pc();
1090 
1091   const Address constMethod       (rmethod, Method::const_offset());
1092   const Address access_flags      (rmethod, Method::access_flags_offset());
1093   const Address size_of_parameters(r2, ConstMethod::
1094                                        size_of_parameters_offset());
1095 
1096   // get parameter size (always needed)
1097   __ ldr(r2, constMethod);
1098   __ load_unsigned_short(r2, size_of_parameters);
1099 
1100   // Native calls don't need the stack size check since they have no
1101   // expression stack and the arguments are already on the stack and
1102   // we only add a handful of words to the stack.
1103 
1104   // rmethod: Method*
1105   // r2: size of parameters
1106   // rscratch1: sender sp
1107 
1108   // for natives the size of locals is zero
1109 
1110   // compute beginning of parameters (rlocals)
1111   __ add(rlocals, esp, r2, ext::uxtx, 3);
1112   __ add(rlocals, rlocals, -wordSize);
1113 
1114   // Pull SP back to minimum size: this avoids holes in the stack
1115   __ andr(sp, esp, -16);
1116 
1117   // initialize fixed part of activation frame
1118   generate_fixed_frame(true);
1119 
1120   // make sure method is native & not abstract
1121 #ifdef ASSERT
1122   __ ldrw(r0, access_flags);
1123   {
1124     Label L;
1125     __ tst(r0, JVM_ACC_NATIVE);
1126     __ br(Assembler::NE, L);
1127     __ stop("tried to execute non-native method as native");
1128     __ bind(L);
1129   }
1130   {
1131     Label L;
1132     __ tst(r0, JVM_ACC_ABSTRACT);
1133     __ br(Assembler::EQ, L);
1134     __ stop("tried to execute abstract method in interpreter");
1135     __ bind(L);
1136   }
1137 #endif
1138 
1139   // Since at this point in the method invocation the exception
1140   // handler would try to exit the monitor of synchronized methods
1141   // which hasn't been entered yet, we set the thread local variable
1142   // _do_not_unlock_if_synchronized to true. The remove_activation
1143   // will check this flag.
1144 
1145    const Address do_not_unlock_if_synchronized(rthread,
1146         in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
1147   __ mov(rscratch2, true);
1148   __ strb(rscratch2, do_not_unlock_if_synchronized);
1149 
1150   // increment invocation count & check for overflow
1151   Label invocation_counter_overflow;
1152   if (inc_counter) {
1153     generate_counter_incr(&invocation_counter_overflow);
1154   }
1155 
1156   Label continue_after_compile;
1157   __ bind(continue_after_compile);
1158 
1159   bang_stack_shadow_pages(true);
1160 
1161   // reset the _do_not_unlock_if_synchronized flag
1162   __ strb(zr, do_not_unlock_if_synchronized);
1163 
1164   // check for synchronized methods
1165   // Must happen AFTER invocation_counter check and stack overflow check,
1166   // so method is not locked if overflows.
1167   if (synchronized) {
1168     lock_method();
1169   } else {
1170     // no synchronization necessary
1171 #ifdef ASSERT
1172     {
1173       Label L;
1174       __ ldrw(r0, access_flags);
1175       __ tst(r0, JVM_ACC_SYNCHRONIZED);
1176       __ br(Assembler::EQ, L);
1177       __ stop("method needs synchronization");
1178       __ bind(L);
1179     }
1180 #endif
1181   }
1182 
1183   // start execution
1184 #ifdef ASSERT
1185   {
1186     Label L;
1187     const Address monitor_block_top(rfp,
1188                  frame::interpreter_frame_monitor_block_top_offset * wordSize);
1189     __ ldr(rscratch1, monitor_block_top);
1190     __ cmp(esp, rscratch1);
1191     __ br(Assembler::EQ, L);
1192     __ stop("broken stack frame setup in interpreter");
1193     __ bind(L);
1194   }
1195 #endif
1196 
1197   // jvmti support
1198   __ notify_method_entry();
1199 
1200   // work registers
1201   const Register t = r17;
1202   const Register result_handler = r19;
1203 
1204   // allocate space for parameters
1205   __ ldr(t, Address(rmethod, Method::const_offset()));
1206   __ load_unsigned_short(t, Address(t, ConstMethod::size_of_parameters_offset()));
1207 
1208   __ sub(rscratch1, esp, t, ext::uxtx, Interpreter::logStackElementSize);
1209   __ andr(sp, rscratch1, -16);
1210   __ mov(esp, rscratch1);
1211 
1212   // get signature handler
1213   {
1214     Label L;
1215     __ ldr(t, Address(rmethod, Method::signature_handler_offset()));
1216     __ cbnz(t, L);
1217     __ call_VM(noreg,
1218                CAST_FROM_FN_PTR(address,
1219                                 InterpreterRuntime::prepare_native_call),
1220                rmethod);
1221     __ ldr(t, Address(rmethod, Method::signature_handler_offset()));
1222     __ bind(L);
1223   }
1224 
1225   // call signature handler
1226   assert(InterpreterRuntime::SignatureHandlerGenerator::from() == rlocals,
1227          "adjust this code");
1228   assert(InterpreterRuntime::SignatureHandlerGenerator::to() == sp,
1229          "adjust this code");
1230   assert(InterpreterRuntime::SignatureHandlerGenerator::temp() == rscratch1,
1231           "adjust this code");
1232 
1233   // The generated handlers do not touch rmethod (the method).
1234   // However, large signatures cannot be cached and are generated
1235   // each time here.  The slow-path generator can do a GC on return,
1236   // so we must reload it after the call.
1237   __ blr(t);
1238   __ get_method(rmethod);        // slow path can do a GC, reload rmethod
1239 
1240 
1241   // result handler is in r0
1242   // set result handler
1243   __ mov(result_handler, r0);
1244   // pass mirror handle if static call
1245   {
1246     Label L;
1247     __ ldrw(t, Address(rmethod, Method::access_flags_offset()));
1248     __ tbz(t, exact_log2(JVM_ACC_STATIC), L);
1249     // get mirror
1250     __ load_mirror(t, rmethod);
1251     // copy mirror into activation frame
1252     __ str(t, Address(rfp, frame::interpreter_frame_oop_temp_offset * wordSize));
1253     // pass handle to mirror
1254     __ add(c_rarg1, rfp, frame::interpreter_frame_oop_temp_offset * wordSize);
1255     __ bind(L);
1256   }
1257 
1258   // get native function entry point in r10
1259   {
1260     Label L;
1261     __ ldr(r10, Address(rmethod, Method::native_function_offset()));
1262     address unsatisfied = (SharedRuntime::native_method_throw_unsatisfied_link_error_entry());
1263     __ mov(rscratch2, unsatisfied);
1264     __ ldr(rscratch2, rscratch2);
1265     __ cmp(r10, rscratch2);
1266     __ br(Assembler::NE, L);
1267     __ call_VM(noreg,
1268                CAST_FROM_FN_PTR(address,
1269                                 InterpreterRuntime::prepare_native_call),
1270                rmethod);
1271     __ get_method(rmethod);
1272     __ ldr(r10, Address(rmethod, Method::native_function_offset()));
1273     __ bind(L);
1274   }
1275 
1276   // pass JNIEnv
1277   __ add(c_rarg0, rthread, in_bytes(JavaThread::jni_environment_offset()));
1278 
1279   // Set the last Java PC in the frame anchor to be the return address from
1280   // the call to the native method: this will allow the debugger to
1281   // generate an accurate stack trace.
1282   Label native_return;
1283   __ set_last_Java_frame(esp, rfp, native_return, rscratch1);
1284 
1285   // change thread state
1286 #ifdef ASSERT
1287   {
1288     Label L;
1289     __ ldrw(t, Address(rthread, JavaThread::thread_state_offset()));
1290     __ cmp(t, (u1)_thread_in_Java);
1291     __ br(Assembler::EQ, L);
1292     __ stop("Wrong thread state in native stub");
1293     __ bind(L);
1294   }
1295 #endif
1296 
1297   // Change state to native
1298   __ mov(rscratch1, _thread_in_native);
1299   __ lea(rscratch2, Address(rthread, JavaThread::thread_state_offset()));
1300   __ stlrw(rscratch1, rscratch2);
1301 
1302   // Call the native method.
1303   __ blr(r10);
1304   __ bind(native_return);
1305   __ get_method(rmethod);
1306   // result potentially in r0 or v0
1307 
1308   // make room for the pushes we're about to do
1309   __ sub(rscratch1, esp, 4 * wordSize);
1310   __ andr(sp, rscratch1, -16);
1311 
1312   // NOTE: The order of these pushes is known to frame::interpreter_frame_result
1313   // in order to extract the result of a method call. If the order of these
1314   // pushes change or anything else is added to the stack then the code in
1315   // interpreter_frame_result must also change.
1316   __ push(dtos);
1317   __ push(ltos);
1318 
1319   __ verify_sve_vector_length();
1320 
1321   // change thread state
1322   __ mov(rscratch1, _thread_in_native_trans);
1323   __ lea(rscratch2, Address(rthread, JavaThread::thread_state_offset()));
1324   __ stlrw(rscratch1, rscratch2);
1325 
1326   // Force this write out before the read below
1327   __ dmb(Assembler::ISH);
1328 
1329   // check for safepoint operation in progress and/or pending suspend requests
1330   {
1331     Label L, Continue;
1332 
1333     // We need an acquire here to ensure that any subsequent load of the
1334     // global SafepointSynchronize::_state flag is ordered after this load
1335     // of the thread-local polling word.  We don't want this poll to
1336     // return false (i.e. not safepointing) and a later poll of the global
1337     // SafepointSynchronize::_state spuriously to return true.
1338     //
1339     // This is to avoid a race when we're in a native->Java transition
1340     // racing the code which wakes up from a safepoint.
1341     __ safepoint_poll(L, true /* at_return */, true /* acquire */, false /* in_nmethod */);
1342     __ ldrw(rscratch2, Address(rthread, JavaThread::suspend_flags_offset()));
1343     __ cbz(rscratch2, Continue);
1344     __ bind(L);
1345 
1346     // Don't use call_VM as it will see a possible pending exception
1347     // and forward it and never return here preventing us from
1348     // clearing _last_native_pc down below. So we do a runtime call by
1349     // hand.
1350     //
1351     __ mov(c_rarg0, rthread);
1352     __ mov(rscratch2, CAST_FROM_FN_PTR(address, JavaThread::check_special_condition_for_native_trans));
1353     __ blr(rscratch2);
1354     __ get_method(rmethod);
1355     __ reinit_heapbase();
1356     __ bind(Continue);
1357   }
1358 
1359   // change thread state
1360   __ mov(rscratch1, _thread_in_Java);
1361   __ lea(rscratch2, Address(rthread, JavaThread::thread_state_offset()));
1362   __ stlrw(rscratch1, rscratch2);
1363 
1364   // reset_last_Java_frame
1365   __ reset_last_Java_frame(true);
1366 
1367   if (CheckJNICalls) {
1368     // clear_pending_jni_exception_check
1369     __ str(zr, Address(rthread, JavaThread::pending_jni_exception_check_fn_offset()));
1370   }
1371 
1372   // reset handle block
1373   __ ldr(t, Address(rthread, JavaThread::active_handles_offset()));
1374   __ str(zr, Address(t, JNIHandleBlock::top_offset_in_bytes()));
1375 
1376   // If result is an oop unbox and store it in frame where gc will see it
1377   // and result handler will pick it up
1378 
1379   {
1380     Label no_oop;
1381     __ adr(t, ExternalAddress(AbstractInterpreter::result_handler(T_OBJECT)));
1382     __ cmp(t, result_handler);
1383     __ br(Assembler::NE, no_oop);
1384     // Unbox oop result, e.g. JNIHandles::resolve result.
1385     __ pop(ltos);
1386     __ resolve_jobject(r0, rthread, t);
1387     __ str(r0, Address(rfp, frame::interpreter_frame_oop_temp_offset*wordSize));
1388     // keep stack depth as expected by pushing oop which will eventually be discarded
1389     __ push(ltos);
1390     __ bind(no_oop);
1391   }
1392 
1393   {
1394     Label no_reguard;
1395     __ lea(rscratch1, Address(rthread, in_bytes(JavaThread::stack_guard_state_offset())));
1396     __ ldrw(rscratch1, Address(rscratch1));
1397     __ cmp(rscratch1, (u1)StackOverflow::stack_guard_yellow_reserved_disabled);
1398     __ br(Assembler::NE, no_reguard);
1399 
1400     __ push_call_clobbered_registers();
1401     __ mov(c_rarg0, rthread);
1402     __ mov(rscratch2, CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages));
1403     __ blr(rscratch2);
1404     __ pop_call_clobbered_registers();
1405 
1406     __ bind(no_reguard);
1407   }
1408 
1409   // The method register is junk from after the thread_in_native transition
1410   // until here.  Also can't call_VM until the bcp has been
1411   // restored.  Need bcp for throwing exception below so get it now.
1412   __ get_method(rmethod);
1413 
1414   // restore bcp to have legal interpreter frame, i.e., bci == 0 <=>
1415   // rbcp == code_base()
1416   __ ldr(rbcp, Address(rmethod, Method::const_offset()));   // get ConstMethod*
1417   __ add(rbcp, rbcp, in_bytes(ConstMethod::codes_offset()));          // get codebase
1418   // handle exceptions (exception handling will handle unlocking!)
1419   {
1420     Label L;
1421     __ ldr(rscratch1, Address(rthread, Thread::pending_exception_offset()));
1422     __ cbz(rscratch1, L);
1423     // Note: At some point we may want to unify this with the code
1424     // used in call_VM_base(); i.e., we should use the
1425     // StubRoutines::forward_exception code. For now this doesn't work
1426     // here because the rsp is not correctly set at this point.
1427     __ MacroAssembler::call_VM(noreg,
1428                                CAST_FROM_FN_PTR(address,
1429                                InterpreterRuntime::throw_pending_exception));
1430     __ should_not_reach_here();
1431     __ bind(L);
1432   }
1433 
1434   // do unlocking if necessary
1435   {
1436     Label L;
1437     __ ldrw(t, Address(rmethod, Method::access_flags_offset()));
1438     __ tbz(t, exact_log2(JVM_ACC_SYNCHRONIZED), L);
1439     // the code below should be shared with interpreter macro
1440     // assembler implementation
1441     {
1442       Label unlock;
1443       // BasicObjectLock will be first in list, since this is a
1444       // synchronized method. However, need to check that the object
1445       // has not been unlocked by an explicit monitorexit bytecode.
1446 
1447       // monitor expect in c_rarg1 for slow unlock path
1448       __ lea (c_rarg1, Address(rfp,   // address of first monitor
1449                                (intptr_t)(frame::interpreter_frame_initial_sp_offset *
1450                                           wordSize - sizeof(BasicObjectLock))));
1451 
1452       __ ldr(t, Address(c_rarg1, BasicObjectLock::obj_offset_in_bytes()));
1453       __ cbnz(t, unlock);
1454 
1455       // Entry already unlocked, need to throw exception
1456       __ MacroAssembler::call_VM(noreg,
1457                                  CAST_FROM_FN_PTR(address,
1458                    InterpreterRuntime::throw_illegal_monitor_state_exception));
1459       __ should_not_reach_here();
1460 
1461       __ bind(unlock);
1462       __ unlock_object(c_rarg1);

1463     }
1464     __ bind(L);
1465   }
1466 
1467   // jvmti support
1468   // Note: This must happen _after_ handling/throwing any exceptions since
1469   //       the exception handler code notifies the runtime of method exits
1470   //       too. If this happens before, method entry/exit notifications are
1471   //       not properly paired (was bug - gri 11/22/99).
1472   __ notify_method_exit(vtos, InterpreterMacroAssembler::NotifyJVMTI);
1473 
1474   // restore potential result in r0:d0, call result handler to
1475   // restore potential result in ST0 & handle result
1476 
1477   __ pop(ltos);
1478   __ pop(dtos);
1479 
1480   __ blr(result_handler);
1481 
1482   // remove activation
1483   __ ldr(esp, Address(rfp,
1484                     frame::interpreter_frame_sender_sp_offset *
1485                     wordSize)); // get sender sp
1486   // remove frame anchor
1487   __ leave();
1488 
1489   // resture sender sp
1490   __ mov(sp, esp);
1491 
1492   __ ret(lr);
1493 
1494   if (inc_counter) {
1495     // Handle overflow of counter and compile method
1496     __ bind(invocation_counter_overflow);
1497     generate_counter_overflow(continue_after_compile);
1498   }
1499 
1500   return entry_point;
1501 }
1502 
1503 //
1504 // Generic interpreted method entry to (asm) interpreter
1505 //
1506 address TemplateInterpreterGenerator::generate_normal_entry(bool synchronized) {
1507   // determine code generation flags
1508   bool inc_counter  = UseCompiler || CountCompiledCalls || LogTouchedMethods;
1509 
1510   // rscratch1: sender sp
1511   address entry_point = __ pc();
1512 
1513   const Address constMethod(rmethod, Method::const_offset());
1514   const Address access_flags(rmethod, Method::access_flags_offset());
1515   const Address size_of_parameters(r3,
1516                                    ConstMethod::size_of_parameters_offset());
1517   const Address size_of_locals(r3, ConstMethod::size_of_locals_offset());
1518 
1519   // get parameter size (always needed)
1520   // need to load the const method first
1521   __ ldr(r3, constMethod);
1522   __ load_unsigned_short(r2, size_of_parameters);
1523 
1524   // r2: size of parameters
1525 
1526   __ load_unsigned_short(r3, size_of_locals); // get size of locals in words
1527   __ sub(r3, r3, r2); // r3 = no. of additional locals
1528 
1529   // see if we've got enough room on the stack for locals plus overhead.
1530   generate_stack_overflow_check();
1531 
1532   // compute beginning of parameters (rlocals)
1533   __ add(rlocals, esp, r2, ext::uxtx, 3);
1534   __ sub(rlocals, rlocals, wordSize);
1535 
1536   __ mov(rscratch1, esp);
1537 
1538   // r3 - # of additional locals
1539   // allocate space for locals
1540   // explicitly initialize locals
1541   // Initializing memory allocated for locals in the same direction as
1542   // the stack grows to ensure page initialization order according
1543   // to windows-aarch64 stack page growth requirement (see
1544   // https://docs.microsoft.com/en-us/cpp/build/arm64-windows-abi-conventions?view=msvc-160#stack)
1545   {
1546     Label exit, loop;
1547     __ ands(zr, r3, r3);
1548     __ br(Assembler::LE, exit); // do nothing if r3 <= 0
1549     __ bind(loop);
1550     __ str(zr, Address(__ pre(rscratch1, -wordSize)));
1551     __ sub(r3, r3, 1); // until everything initialized
1552     __ cbnz(r3, loop);
1553     __ bind(exit);
1554   }
1555 
1556   // Padding between locals and fixed part of activation frame to ensure
1557   // SP is always 16-byte aligned.
1558   __ andr(sp, rscratch1, -16);
1559 
1560   // And the base dispatch table
1561   __ get_dispatch();
1562 
1563   // initialize fixed part of activation frame
1564   generate_fixed_frame(false);
1565 
1566   // make sure method is not native & not abstract
1567 #ifdef ASSERT
1568   __ ldrw(r0, access_flags);
1569   {
1570     Label L;
1571     __ tst(r0, JVM_ACC_NATIVE);
1572     __ br(Assembler::EQ, L);
1573     __ stop("tried to execute native method as non-native");
1574     __ bind(L);
1575   }
1576  {
1577     Label L;
1578     __ tst(r0, JVM_ACC_ABSTRACT);
1579     __ br(Assembler::EQ, L);
1580     __ stop("tried to execute abstract method in interpreter");
1581     __ bind(L);
1582   }
1583 #endif
1584 
1585   // Since at this point in the method invocation the exception
1586   // handler would try to exit the monitor of synchronized methods
1587   // which hasn't been entered yet, we set the thread local variable
1588   // _do_not_unlock_if_synchronized to true. The remove_activation
1589   // will check this flag.
1590 
1591    const Address do_not_unlock_if_synchronized(rthread,
1592         in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
1593   __ mov(rscratch2, true);
1594   __ strb(rscratch2, do_not_unlock_if_synchronized);
1595 
1596   Register mdp = r3;
1597   __ profile_parameters_type(mdp, r1, r2);
1598 
1599   // increment invocation count & check for overflow
1600   Label invocation_counter_overflow;
1601   if (inc_counter) {
1602     generate_counter_incr(&invocation_counter_overflow);
1603   }
1604 
1605   Label continue_after_compile;
1606   __ bind(continue_after_compile);
1607 
1608   bang_stack_shadow_pages(false);
1609 
1610   // reset the _do_not_unlock_if_synchronized flag
1611   __ strb(zr, do_not_unlock_if_synchronized);
1612 
1613   // check for synchronized methods
1614   // Must happen AFTER invocation_counter check and stack overflow check,
1615   // so method is not locked if overflows.
1616   if (synchronized) {
1617     // Allocate monitor and lock method
1618     lock_method();
1619   } else {
1620     // no synchronization necessary
1621 #ifdef ASSERT
1622     {
1623       Label L;
1624       __ ldrw(r0, access_flags);
1625       __ tst(r0, JVM_ACC_SYNCHRONIZED);
1626       __ br(Assembler::EQ, L);
1627       __ stop("method needs synchronization");
1628       __ bind(L);
1629     }
1630 #endif
1631   }
1632 
1633   // start execution
1634 #ifdef ASSERT
1635   {
1636     Label L;
1637      const Address monitor_block_top (rfp,
1638                  frame::interpreter_frame_monitor_block_top_offset * wordSize);
1639     __ ldr(rscratch1, monitor_block_top);
1640     __ cmp(esp, rscratch1);
1641     __ br(Assembler::EQ, L);
1642     __ stop("broken stack frame setup in interpreter");
1643     __ bind(L);
1644   }
1645 #endif
1646 
1647   // jvmti support
1648   __ notify_method_entry();
1649 
1650   __ dispatch_next(vtos);
1651 
1652   // invocation counter overflow
1653   if (inc_counter) {
1654     // Handle overflow of counter and compile method
1655     __ bind(invocation_counter_overflow);
1656     generate_counter_overflow(continue_after_compile);
1657   }
1658 
1659   return entry_point;
1660 }
1661 
1662 //-----------------------------------------------------------------------------
1663 // Exceptions
1664 
1665 void TemplateInterpreterGenerator::generate_throw_exception() {
1666   // Entry point in previous activation (i.e., if the caller was
1667   // interpreted)
1668   Interpreter::_rethrow_exception_entry = __ pc();
1669   // Restore sp to interpreter_frame_last_sp even though we are going
1670   // to empty the expression stack for the exception processing.
1671   __ str(zr, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
1672   // r0: exception
1673   // r3: return address/pc that threw exception
1674   __ restore_bcp();    // rbcp points to call/send
1675   __ restore_locals();
1676   __ restore_constant_pool_cache();
1677   __ reinit_heapbase();  // restore rheapbase as heapbase.
1678   __ get_dispatch();
1679 
1680   // Entry point for exceptions thrown within interpreter code
1681   Interpreter::_throw_exception_entry = __ pc();
1682   // If we came here via a NullPointerException on the receiver of a
1683   // method, rmethod may be corrupt.
1684   __ get_method(rmethod);
1685   // expression stack is undefined here
1686   // r0: exception
1687   // rbcp: exception bcp
1688   __ verify_oop(r0);
1689   __ mov(c_rarg1, r0);
1690 
1691   // expression stack must be empty before entering the VM in case of
1692   // an exception
1693   __ empty_expression_stack();
1694   // find exception handler address and preserve exception oop
1695   __ call_VM(r3,
1696              CAST_FROM_FN_PTR(address,
1697                           InterpreterRuntime::exception_handler_for_exception),
1698              c_rarg1);
1699 
1700   // Calculate stack limit
1701   __ ldr(rscratch1, Address(rmethod, Method::const_offset()));
1702   __ ldrh(rscratch1, Address(rscratch1, ConstMethod::max_stack_offset()));
1703   __ add(rscratch1, rscratch1, frame::interpreter_frame_monitor_size() + 4);
1704   __ ldr(rscratch2,
1705          Address(rfp, frame::interpreter_frame_initial_sp_offset * wordSize));
1706   __ sub(rscratch1, rscratch2, rscratch1, ext::uxtx, 3);
1707   __ andr(sp, rscratch1, -16);
1708 
1709   // r0: exception handler entry point
1710   // r3: preserved exception oop
1711   // rbcp: bcp for exception handler
1712   __ push_ptr(r3); // push exception which is now the only value on the stack
1713   __ br(r0); // jump to exception handler (may be _remove_activation_entry!)
1714 
1715   // If the exception is not handled in the current frame the frame is
1716   // removed and the exception is rethrown (i.e. exception
1717   // continuation is _rethrow_exception).
1718   //
1719   // Note: At this point the bci is still the bxi for the instruction
1720   // which caused the exception and the expression stack is
1721   // empty. Thus, for any VM calls at this point, GC will find a legal
1722   // oop map (with empty expression stack).
1723 
1724   //
1725   // JVMTI PopFrame support
1726   //
1727 
1728   Interpreter::_remove_activation_preserving_args_entry = __ pc();
1729   __ empty_expression_stack();
1730   // Set the popframe_processing bit in pending_popframe_condition
1731   // indicating that we are currently handling popframe, so that
1732   // call_VMs that may happen later do not trigger new popframe
1733   // handling cycles.
1734   __ ldrw(r3, Address(rthread, JavaThread::popframe_condition_offset()));
1735   __ orr(r3, r3, JavaThread::popframe_processing_bit);
1736   __ strw(r3, Address(rthread, JavaThread::popframe_condition_offset()));
1737 
1738   {
1739     // Check to see whether we are returning to a deoptimized frame.
1740     // (The PopFrame call ensures that the caller of the popped frame is
1741     // either interpreted or compiled and deoptimizes it if compiled.)
1742     // In this case, we can't call dispatch_next() after the frame is
1743     // popped, but instead must save the incoming arguments and restore
1744     // them after deoptimization has occurred.
1745     //
1746     // Note that we don't compare the return PC against the
1747     // deoptimization blob's unpack entry because of the presence of
1748     // adapter frames in C2.
1749     Label caller_not_deoptimized;
1750     __ ldr(c_rarg1, Address(rfp, frame::return_addr_offset * wordSize));
1751     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1752                                InterpreterRuntime::interpreter_contains), c_rarg1);
1753     __ cbnz(r0, caller_not_deoptimized);
1754 
1755     // Compute size of arguments for saving when returning to
1756     // deoptimized caller
1757     __ get_method(r0);
1758     __ ldr(r0, Address(r0, Method::const_offset()));
1759     __ load_unsigned_short(r0, Address(r0, in_bytes(ConstMethod::
1760                                                     size_of_parameters_offset())));
1761     __ lsl(r0, r0, Interpreter::logStackElementSize);
1762     __ restore_locals(); // XXX do we need this?
1763     __ sub(rlocals, rlocals, r0);
1764     __ add(rlocals, rlocals, wordSize);
1765     // Save these arguments
1766     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1767                                            Deoptimization::
1768                                            popframe_preserve_args),
1769                           rthread, r0, rlocals);
1770 
1771     __ remove_activation(vtos,
1772                          /* throw_monitor_exception */ false,
1773                          /* install_monitor_exception */ false,
1774                          /* notify_jvmdi */ false);
1775 
1776     // Inform deoptimization that it is responsible for restoring
1777     // these arguments
1778     __ mov(rscratch1, JavaThread::popframe_force_deopt_reexecution_bit);
1779     __ strw(rscratch1, Address(rthread, JavaThread::popframe_condition_offset()));
1780 
1781     // Continue in deoptimization handler
1782     __ ret(lr);
1783 
1784     __ bind(caller_not_deoptimized);
1785   }
1786 
1787   __ remove_activation(vtos,
1788                        /* throw_monitor_exception */ false,
1789                        /* install_monitor_exception */ false,
1790                        /* notify_jvmdi */ false);
1791 
1792   // Restore the last_sp and null it out
1793   __ ldr(esp, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
1794   __ str(zr, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
1795 
1796   __ restore_bcp();
1797   __ restore_locals();
1798   __ restore_constant_pool_cache();
1799   __ get_method(rmethod);
1800   __ get_dispatch();
1801 
1802   // The method data pointer was incremented already during
1803   // call profiling. We have to restore the mdp for the current bcp.
1804   if (ProfileInterpreter) {
1805     __ set_method_data_pointer_for_bcp();
1806   }
1807 
1808   // Clear the popframe condition flag
1809   __ strw(zr, Address(rthread, JavaThread::popframe_condition_offset()));
1810   assert(JavaThread::popframe_inactive == 0, "fix popframe_inactive");
1811 
1812 #if INCLUDE_JVMTI
1813   {
1814     Label L_done;
1815 
1816     __ ldrb(rscratch1, Address(rbcp, 0));
1817     __ cmpw(rscratch1, Bytecodes::_invokestatic);
1818     __ br(Assembler::NE, L_done);
1819 
1820     // The member name argument must be restored if _invokestatic is re-executed after a PopFrame call.
1821     // Detect such a case in the InterpreterRuntime function and return the member name argument, or NULL.
1822 
1823     __ ldr(c_rarg0, Address(rlocals, 0));
1824     __ call_VM(r0, CAST_FROM_FN_PTR(address, InterpreterRuntime::member_name_arg_or_null), c_rarg0, rmethod, rbcp);
1825 
1826     __ cbz(r0, L_done);
1827 
1828     __ str(r0, Address(esp, 0));
1829     __ bind(L_done);
1830   }
1831 #endif // INCLUDE_JVMTI
1832 
1833   // Restore machine SP
1834   __ ldr(rscratch1, Address(rmethod, Method::const_offset()));
1835   __ ldrh(rscratch1, Address(rscratch1, ConstMethod::max_stack_offset()));
1836   __ add(rscratch1, rscratch1, frame::interpreter_frame_monitor_size() + 4);
1837   __ ldr(rscratch2,
1838          Address(rfp, frame::interpreter_frame_initial_sp_offset * wordSize));
1839   __ sub(rscratch1, rscratch2, rscratch1, ext::uxtw, 3);
1840   __ andr(sp, rscratch1, -16);
1841 
1842   __ dispatch_next(vtos);
1843   // end of PopFrame support
1844 
1845   Interpreter::_remove_activation_entry = __ pc();
1846 
1847   // preserve exception over this code sequence
1848   __ pop_ptr(r0);
1849   __ str(r0, Address(rthread, JavaThread::vm_result_offset()));
1850   // remove the activation (without doing throws on illegalMonitorExceptions)
1851   __ remove_activation(vtos, false, true, false);
1852   // restore exception
1853   __ get_vm_result(r0, rthread);
1854 
1855   // In between activations - previous activation type unknown yet
1856   // compute continuation point - the continuation point expects the
1857   // following registers set up:
1858   //
1859   // r0: exception
1860   // lr: return address/pc that threw exception
1861   // esp: expression stack of caller
1862   // rfp: fp of caller
1863   __ stp(r0, lr, Address(__ pre(sp, -2 * wordSize)));  // save exception & return address
1864   __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1865                           SharedRuntime::exception_handler_for_return_address),
1866                         rthread, lr);
1867   __ mov(r1, r0);                               // save exception handler
1868   __ ldp(r0, lr, Address(__ post(sp, 2 * wordSize)));  // restore exception & return address
1869   // We might be returning to a deopt handler that expects r3 to
1870   // contain the exception pc
1871   __ mov(r3, lr);
1872   // Note that an "issuing PC" is actually the next PC after the call
1873   __ br(r1);                                    // jump to exception
1874                                                 // handler of caller
1875 }
1876 
1877 
1878 //
1879 // JVMTI ForceEarlyReturn support
1880 //
1881 address TemplateInterpreterGenerator::generate_earlyret_entry_for(TosState state) {
1882   address entry = __ pc();
1883 
1884   __ restore_bcp();
1885   __ restore_locals();
1886   __ empty_expression_stack();
1887   __ load_earlyret_value(state);
1888 
1889   __ ldr(rscratch1, Address(rthread, JavaThread::jvmti_thread_state_offset()));
1890   Address cond_addr(rscratch1, JvmtiThreadState::earlyret_state_offset());
1891 
1892   // Clear the earlyret state
1893   assert(JvmtiThreadState::earlyret_inactive == 0, "should be");
1894   __ str(zr, cond_addr);
1895 
1896   __ remove_activation(state,
1897                        false, /* throw_monitor_exception */
1898                        false, /* install_monitor_exception */
1899                        true); /* notify_jvmdi */
1900   __ ret(lr);
1901 
1902   return entry;
1903 } // end of ForceEarlyReturn support
1904 
1905 
1906 
1907 //-----------------------------------------------------------------------------
1908 // Helper for vtos entry point generation
1909 
1910 void TemplateInterpreterGenerator::set_vtos_entry_points(Template* t,
1911                                                          address& bep,
1912                                                          address& cep,
1913                                                          address& sep,
1914                                                          address& aep,
1915                                                          address& iep,
1916                                                          address& lep,
1917                                                          address& fep,
1918                                                          address& dep,
1919                                                          address& vep) {
1920   assert(t->is_valid() && t->tos_in() == vtos, "illegal template");
1921   Label L;
1922   aep = __ pc();  __ push_ptr();  __ b(L);
1923   fep = __ pc();  __ push_f();    __ b(L);
1924   dep = __ pc();  __ push_d();    __ b(L);
1925   lep = __ pc();  __ push_l();    __ b(L);
1926   bep = cep = sep =
1927   iep = __ pc();  __ push_i();
1928   vep = __ pc();
1929   __ bind(L);
1930   generate_and_dispatch(t);
1931 }
1932 
1933 //-----------------------------------------------------------------------------
1934 
1935 // Non-product code
1936 #ifndef PRODUCT
1937 address TemplateInterpreterGenerator::generate_trace_code(TosState state) {
1938   address entry = __ pc();
1939 
1940   __ push(lr);
1941   __ push(state);
1942   __ push(RegSet::range(r0, r15), sp);
1943   __ mov(c_rarg2, r0);  // Pass itos
1944   __ call_VM(noreg,
1945              CAST_FROM_FN_PTR(address, InterpreterRuntime::trace_bytecode),
1946              c_rarg1, c_rarg2, c_rarg3);
1947   __ pop(RegSet::range(r0, r15), sp);
1948   __ pop(state);
1949   __ pop(lr);
1950   __ ret(lr);                                   // return from result handler
1951 
1952   return entry;
1953 }
1954 
1955 void TemplateInterpreterGenerator::count_bytecode() {
1956   Register rscratch3 = r0;
1957   __ push(rscratch1);
1958   __ push(rscratch2);
1959   __ push(rscratch3);
1960   __ mov(rscratch3, (address) &BytecodeCounter::_counter_value);
1961   __ atomic_add(noreg, 1, rscratch3);
1962   __ pop(rscratch3);
1963   __ pop(rscratch2);
1964   __ pop(rscratch1);
1965 }
1966 
1967 void TemplateInterpreterGenerator::histogram_bytecode(Template* t) { ; }
1968 
1969 void TemplateInterpreterGenerator::histogram_bytecode_pair(Template* t) { ; }
1970 
1971 
1972 void TemplateInterpreterGenerator::trace_bytecode(Template* t) {
1973   // Call a little run-time stub to avoid blow-up for each bytecode.
1974   // The run-time runtime saves the right registers, depending on
1975   // the tosca in-state for the given template.
1976 
1977   assert(Interpreter::trace_code(t->tos_in()) != NULL,
1978          "entry must have been generated");
1979   __ bl(Interpreter::trace_code(t->tos_in()));
1980   __ reinit_heapbase();
1981 }
1982 
1983 
1984 void TemplateInterpreterGenerator::stop_interpreter_at() {
1985   Label L;
1986   __ push(rscratch1);
1987   __ mov(rscratch1, (address) &BytecodeCounter::_counter_value);
1988   __ ldr(rscratch1, Address(rscratch1));
1989   __ mov(rscratch2, StopInterpreterAt);
1990   __ cmpw(rscratch1, rscratch2);
1991   __ br(Assembler::NE, L);
1992   __ brk(0);
1993   __ bind(L);
1994   __ pop(rscratch1);
1995 }
1996 
1997 #endif // !PRODUCT
--- EOF ---