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