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