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