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