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