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