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