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