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