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