rev 54670 : Port of valuetypes to aarch64

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