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