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