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     if (UseShenandoahGC) {
 713       __ push_call_clobbered_registers();
 714     }
 715     __ g1_write_barrier_pre(noreg /* obj */,
 716                             local_0 /* pre_val */,
 717                             rthread /* thread */,
 718                             rscratch2 /* tmp */,
 719                             true /* tosca_live */,
 720                             true /* expand_call */);
 721     if (UseShenandoahGC) {
 722       __ pop_call_clobbered_registers();
 723     }
 724     __ leave();
 725     // areturn
 726     __ andr(sp, r19, -16);  // done with stack
 727     __ ret(lr);
 728 
 729     // generate a vanilla interpreter entry as the slow path
 730     __ bind(slow_path);
 731     (void) generate_normal_entry(false);
 732 
 733     return entry;
 734   }
 735 #endif // INCLUDE_ALL_GCS
 736 
 737   // If G1 is not enabled then attempt to go through the accessor entry point
 738   // Reference.get is an accessor
 739   return generate_accessor_entry();
 740 }
 741 
 742 /**
 743  * Method entry for static native methods:
 744  *   int java.util.zip.CRC32.update(int crc, int b)
 745  */
 746 address InterpreterGenerator::generate_CRC32_update_entry() {
 747   if (UseCRC32Intrinsics) {
 748     address entry = __ pc();
 749 
 750     // rmethod: Method*
 751     // r13: senderSP must preserved for slow path
 752     // esp: args
 753 
 754     Label slow_path;
 755     // If we need a safepoint check, generate full interpreter entry.
 756     ExternalAddress state(SafepointSynchronize::address_of_state());
 757     unsigned long offset;
 758     __ adrp(rscratch1, ExternalAddress(SafepointSynchronize::address_of_state()), offset);
 759     __ ldrw(rscratch1, Address(rscratch1, offset));
 760     assert(SafepointSynchronize::_not_synchronized == 0, "rewrite this code");
 761     __ cbnz(rscratch1, slow_path);
 762 
 763     // We don't generate local frame and don't align stack because
 764     // we call stub code and there is no safepoint on this path.
 765 
 766     // Load parameters
 767     const Register crc = c_rarg0;  // crc
 768     const Register val = c_rarg1;  // source java byte value
 769     const Register tbl = c_rarg2;  // scratch
 770 
 771     // Arguments are reversed on java expression stack
 772     __ ldrw(val, Address(esp, 0));              // byte value
 773     __ ldrw(crc, Address(esp, wordSize));       // Initial CRC
 774 
 775     __ adrp(tbl, ExternalAddress(StubRoutines::crc_table_addr()), offset);
 776     __ add(tbl, tbl, offset);
 777 
 778     __ ornw(crc, zr, crc); // ~crc
 779     __ update_byte_crc32(crc, val, tbl);
 780     __ ornw(crc, zr, crc); // ~crc
 781 
 782     // result in c_rarg0
 783 
 784     __ andr(sp, r13, -16);
 785     __ ret(lr);
 786 
 787     // generate a vanilla native entry as the slow path
 788     __ bind(slow_path);
 789 
 790     (void) generate_native_entry(false);
 791 
 792     return entry;
 793   }
 794   return generate_native_entry(false);
 795 }
 796 
 797 /**
 798  * Method entry for static native methods:
 799  *   int java.util.zip.CRC32.updateBytes(int crc, byte[] b, int off, int len)
 800  *   int java.util.zip.CRC32.updateByteBuffer(int crc, long buf, int off, int len)
 801  */
 802 address InterpreterGenerator::generate_CRC32_updateBytes_entry(AbstractInterpreter::MethodKind kind) {
 803   if (UseCRC32Intrinsics) {
 804     address entry = __ pc();
 805 
 806     // rmethod,: Method*
 807     // r13: senderSP must preserved for slow path
 808 
 809     Label slow_path;
 810     // If we need a safepoint check, generate full interpreter entry.
 811     ExternalAddress state(SafepointSynchronize::address_of_state());
 812     unsigned long offset;
 813     __ adrp(rscratch1, ExternalAddress(SafepointSynchronize::address_of_state()), offset);
 814     __ ldrw(rscratch1, Address(rscratch1, offset));
 815     assert(SafepointSynchronize::_not_synchronized == 0, "rewrite this code");
 816     __ cbnz(rscratch1, slow_path);
 817 
 818     // We don't generate local frame and don't align stack because
 819     // we call stub code and there is no safepoint on this path.
 820 
 821     // Load parameters
 822     const Register crc = c_rarg0;  // crc
 823     const Register buf = c_rarg1;  // source java byte array address
 824     const Register len = c_rarg2;  // length
 825     const Register off = len;      // offset (never overlaps with 'len')
 826 
 827     // Arguments are reversed on java expression stack
 828     // Calculate address of start element
 829     if (kind == Interpreter::java_util_zip_CRC32_updateByteBuffer) {
 830       __ ldr(buf, Address(esp, 2*wordSize)); // long buf
 831       __ ldrw(off, Address(esp, wordSize)); // offset
 832       __ add(buf, buf, off); // + offset
 833       __ ldrw(crc,   Address(esp, 4*wordSize)); // Initial CRC
 834     } else {
 835       __ ldr(buf, Address(esp, 2*wordSize)); // byte[] array
 836       __ add(buf, buf, arrayOopDesc::base_offset_in_bytes(T_BYTE)); // + header size
 837       __ ldrw(off, Address(esp, wordSize)); // offset
 838       __ add(buf, buf, off); // + offset
 839       __ ldrw(crc,   Address(esp, 3*wordSize)); // Initial CRC
 840     }
 841     // Can now load 'len' since we're finished with 'off'
 842     __ ldrw(len, Address(esp, 0x0)); // Length
 843 
 844     __ andr(sp, r13, -16); // Restore the caller's SP
 845 
 846     // We are frameless so we can just jump to the stub.
 847     __ b(CAST_FROM_FN_PTR(address, StubRoutines::updateBytesCRC32()));
 848 
 849     // generate a vanilla native entry as the slow path
 850     __ bind(slow_path);
 851 
 852     (void) generate_native_entry(false);
 853 
 854     return entry;
 855   }
 856   return generate_native_entry(false);
 857 }
 858 
 859 void InterpreterGenerator::bang_stack_shadow_pages(bool native_call) {
 860   // Bang each page in the shadow zone. We can't assume it's been done for
 861   // an interpreter frame with greater than a page of locals, so each page
 862   // needs to be checked.  Only true for non-native.
 863   if (UseStackBanging) {
 864     const int start_page = native_call ? StackShadowPages : 1;
 865     const int page_size = os::vm_page_size();
 866     for (int pages = start_page; pages <= StackShadowPages ; pages++) {
 867       __ sub(rscratch2, sp, pages*page_size);
 868       __ str(zr, Address(rscratch2));
 869     }
 870   }
 871 }
 872 
 873 
 874 // Interpreter stub for calling a native method. (asm interpreter)
 875 // This sets up a somewhat different looking stack for calling the
 876 // native method than the typical interpreter frame setup.
 877 address InterpreterGenerator::generate_native_entry(bool synchronized) {
 878   // determine code generation flags
 879   bool inc_counter  = UseCompiler || CountCompiledCalls;
 880 
 881   // r1: Method*
 882   // rscratch1: sender sp
 883 
 884   address entry_point = __ pc();
 885 
 886   const Address constMethod       (rmethod, Method::const_offset());
 887   const Address access_flags      (rmethod, Method::access_flags_offset());
 888   const Address size_of_parameters(r2, ConstMethod::
 889                                        size_of_parameters_offset());
 890 
 891   // get parameter size (always needed)
 892   __ ldr(r2, constMethod);
 893   __ load_unsigned_short(r2, size_of_parameters);
 894 
 895   // native calls don't need the stack size check since they have no
 896   // expression stack and the arguments are already on the stack and
 897   // we only add a handful of words to the stack
 898 
 899   // rmethod: Method*
 900   // r2: size of parameters
 901   // rscratch1: sender sp
 902 
 903   // for natives the size of locals is zero
 904 
 905   // compute beginning of parameters (rlocals)
 906   __ add(rlocals, esp, r2, ext::uxtx, 3);
 907   __ add(rlocals, rlocals, -wordSize);
 908 
 909   // Pull SP back to minimum size: this avoids holes in the stack
 910   __ andr(sp, esp, -16);
 911 
 912   // initialize fixed part of activation frame
 913   generate_fixed_frame(true);
 914 
 915   // make sure method is native & not abstract
 916 #ifdef ASSERT
 917   __ ldrw(r0, access_flags);
 918   {
 919     Label L;
 920     __ tst(r0, JVM_ACC_NATIVE);
 921     __ br(Assembler::NE, L);
 922     __ stop("tried to execute non-native method as native");
 923     __ bind(L);
 924   }
 925   {
 926     Label L;
 927     __ tst(r0, JVM_ACC_ABSTRACT);
 928     __ br(Assembler::EQ, L);
 929     __ stop("tried to execute abstract method in interpreter");
 930     __ bind(L);
 931   }
 932 #endif
 933 
 934   // Since at this point in the method invocation the exception
 935   // handler would try to exit the monitor of synchronized methods
 936   // which hasn't been entered yet, we set the thread local variable
 937   // _do_not_unlock_if_synchronized to true. The remove_activation
 938   // will check this flag.
 939 
 940    const Address do_not_unlock_if_synchronized(rthread,
 941         in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
 942   __ mov(rscratch2, true);
 943   __ strb(rscratch2, do_not_unlock_if_synchronized);
 944 
 945   // increment invocation count & check for overflow
 946   Label invocation_counter_overflow;
 947   if (inc_counter) {
 948     generate_counter_incr(&invocation_counter_overflow, NULL, NULL);
 949   }
 950 
 951   Label continue_after_compile;
 952   __ bind(continue_after_compile);
 953 
 954   bang_stack_shadow_pages(true);
 955 
 956   // reset the _do_not_unlock_if_synchronized flag
 957   __ strb(zr, do_not_unlock_if_synchronized);
 958 
 959   // check for synchronized methods
 960   // Must happen AFTER invocation_counter check and stack overflow check,
 961   // so method is not locked if overflows.
 962   if (synchronized) {
 963     lock_method();
 964   } else {
 965     // no synchronization necessary
 966 #ifdef ASSERT
 967     {
 968       Label L;
 969       __ ldrw(r0, access_flags);
 970       __ tst(r0, JVM_ACC_SYNCHRONIZED);
 971       __ br(Assembler::EQ, L);
 972       __ stop("method needs synchronization");
 973       __ bind(L);
 974     }
 975 #endif
 976   }
 977 
 978   // start execution
 979 #ifdef ASSERT
 980   {
 981     Label L;
 982     const Address monitor_block_top(rfp,
 983                  frame::interpreter_frame_monitor_block_top_offset * wordSize);
 984     __ ldr(rscratch1, monitor_block_top);
 985     __ cmp(esp, rscratch1);
 986     __ br(Assembler::EQ, L);
 987     __ stop("broken stack frame setup in interpreter");
 988     __ bind(L);
 989   }
 990 #endif
 991 
 992   // jvmti support
 993   __ notify_method_entry();
 994 
 995   // work registers
 996   const Register t = r17;
 997   const Register result_handler = r19;
 998 
 999   // allocate space for parameters
1000   __ ldr(t, Address(rmethod, Method::const_offset()));
1001   __ load_unsigned_short(t, Address(t, ConstMethod::size_of_parameters_offset()));
1002 
1003   __ sub(rscratch1, esp, t, ext::uxtx, Interpreter::logStackElementSize);
1004   __ andr(sp, rscratch1, -16);
1005   __ mov(esp, rscratch1);
1006 
1007   // get signature handler
1008   {
1009     Label L;
1010     __ ldr(t, Address(rmethod, Method::signature_handler_offset()));
1011     __ cbnz(t, L);
1012     __ call_VM(noreg,
1013                CAST_FROM_FN_PTR(address,
1014                                 InterpreterRuntime::prepare_native_call),
1015                rmethod);
1016     __ ldr(t, Address(rmethod, Method::signature_handler_offset()));
1017     __ bind(L);
1018   }
1019 
1020   // call signature handler
1021   assert(InterpreterRuntime::SignatureHandlerGenerator::from() == rlocals,
1022          "adjust this code");
1023   assert(InterpreterRuntime::SignatureHandlerGenerator::to() == sp,
1024          "adjust this code");
1025   assert(InterpreterRuntime::SignatureHandlerGenerator::temp() == rscratch1,
1026           "adjust this code");
1027 
1028   // The generated handlers do not touch rmethod (the method).
1029   // However, large signatures cannot be cached and are generated
1030   // each time here.  The slow-path generator can do a GC on return,
1031   // so we must reload it after the call.
1032   __ blr(t);
1033   __ get_method(rmethod);        // slow path can do a GC, reload rmethod
1034 
1035 
1036   // result handler is in r0
1037   // set result handler
1038   __ mov(result_handler, r0);
1039   // pass mirror handle if static call
1040   {
1041     Label L;
1042     const int mirror_offset = in_bytes(Klass::java_mirror_offset());
1043     __ ldrw(t, Address(rmethod, Method::access_flags_offset()));
1044     __ tst(t, JVM_ACC_STATIC);
1045     __ br(Assembler::EQ, L);
1046     // get mirror
1047     __ ldr(t, Address(rmethod, Method::const_offset()));
1048     __ ldr(t, Address(t, ConstMethod::constants_offset()));
1049     __ ldr(t, Address(t, ConstantPool::pool_holder_offset_in_bytes()));
1050     __ ldr(t, Address(t, mirror_offset));
1051     // copy mirror into activation frame
1052     __ str(t, Address(rfp, frame::interpreter_frame_oop_temp_offset * wordSize));
1053     // pass handle to mirror
1054     __ add(c_rarg1, rfp, frame::interpreter_frame_oop_temp_offset * wordSize);
1055     __ bind(L);
1056   }
1057 
1058   // get native function entry point in r10
1059   {
1060     Label L;
1061     __ ldr(r10, Address(rmethod, Method::native_function_offset()));
1062     address unsatisfied = (SharedRuntime::native_method_throw_unsatisfied_link_error_entry());
1063     __ mov(rscratch2, unsatisfied);
1064     __ ldr(rscratch2, rscratch2);
1065     __ cmp(r10, rscratch2);
1066     __ br(Assembler::NE, L);
1067     __ call_VM(noreg,
1068                CAST_FROM_FN_PTR(address,
1069                                 InterpreterRuntime::prepare_native_call),
1070                rmethod);
1071     __ get_method(rmethod);
1072     __ ldr(r10, Address(rmethod, Method::native_function_offset()));
1073     __ bind(L);
1074   }
1075 
1076   // pass JNIEnv
1077   __ add(c_rarg0, rthread, in_bytes(JavaThread::jni_environment_offset()));
1078 
1079   // Set the last Java PC in the frame anchor to be the return address from
1080   // the call to the native method: this will allow the debugger to
1081   // generate an accurate stack trace.
1082   Label native_return;
1083   __ set_last_Java_frame(esp, rfp, native_return, rscratch1);
1084 
1085   // change thread state
1086 #ifdef ASSERT
1087   {
1088     Label L;
1089     __ ldrw(t, Address(rthread, JavaThread::thread_state_offset()));
1090     __ cmp(t, _thread_in_Java);
1091     __ br(Assembler::EQ, L);
1092     __ stop("Wrong thread state in native stub");
1093     __ bind(L);
1094   }
1095 #endif
1096 
1097   // Change state to native
1098   __ mov(rscratch1, _thread_in_native);
1099   __ lea(rscratch2, Address(rthread, JavaThread::thread_state_offset()));
1100   __ stlrw(rscratch1, rscratch2);
1101 
1102   // Call the native method.
1103   __ blr(r10);
1104   __ bind(native_return);
1105   __ maybe_isb();
1106   __ get_method(rmethod);
1107   // result potentially in r0 or v0
1108 
1109   // make room for the pushes we're about to do
1110   __ sub(rscratch1, esp, 4 * wordSize);
1111   __ andr(sp, rscratch1, -16);
1112 
1113   // NOTE: The order of these pushes is known to frame::interpreter_frame_result
1114   // in order to extract the result of a method call. If the order of these
1115   // pushes change or anything else is added to the stack then the code in
1116   // interpreter_frame_result must also change.
1117   __ push(dtos);
1118   __ push(ltos);
1119 
1120   // change thread state
1121   __ mov(rscratch1, _thread_in_native_trans);
1122   __ lea(rscratch2, Address(rthread, JavaThread::thread_state_offset()));
1123   __ stlrw(rscratch1, rscratch2);
1124 
1125   if (os::is_MP()) {
1126     if (UseMembar) {
1127       // Force this write out before the read below
1128       __ dsb(Assembler::SY);
1129     } else {
1130       // Write serialization page so VM thread can do a pseudo remote membar.
1131       // We use the current thread pointer to calculate a thread specific
1132       // offset to write to within the page. This minimizes bus traffic
1133       // due to cache line collision.
1134       __ serialize_memory(rthread, rscratch2);
1135     }
1136   }
1137 
1138   // check for safepoint operation in progress and/or pending suspend requests
1139   {
1140     Label Continue;
1141     {
1142       unsigned long offset;
1143       __ adrp(rscratch2, SafepointSynchronize::address_of_state(), offset);
1144       __ ldrw(rscratch2, Address(rscratch2, offset));
1145     }
1146     assert(SafepointSynchronize::_not_synchronized == 0,
1147            "SafepointSynchronize::_not_synchronized");
1148     Label L;
1149     __ cbnz(rscratch2, L);
1150     __ ldrw(rscratch2, Address(rthread, JavaThread::suspend_flags_offset()));
1151     __ cbz(rscratch2, Continue);
1152     __ bind(L);
1153 
1154     // Don't use call_VM as it will see a possible pending exception
1155     // and forward it and never return here preventing us from
1156     // clearing _last_native_pc down below.  Also can't use
1157     // call_VM_leaf either as it will check to see if r13 & r14 are
1158     // preserved and correspond to the bcp/locals pointers. So we do a
1159     // runtime call by hand.
1160     //
1161     __ mov(c_rarg0, rthread);
1162     __ mov(rscratch2, CAST_FROM_FN_PTR(address, JavaThread::check_special_condition_for_native_trans));
1163     __ blr(rscratch2);
1164     __ maybe_isb();
1165     __ get_method(rmethod);
1166     __ reinit_heapbase();
1167     __ bind(Continue);
1168   }
1169 
1170   // change thread state
1171   __ mov(rscratch1, _thread_in_Java);
1172   __ lea(rscratch2, Address(rthread, JavaThread::thread_state_offset()));
1173   __ stlrw(rscratch1, rscratch2);
1174 
1175   // reset_last_Java_frame
1176   __ reset_last_Java_frame(true);
1177 
1178   // reset handle block
1179   __ ldr(t, Address(rthread, JavaThread::active_handles_offset()));
1180   __ str(zr, Address(t, JNIHandleBlock::top_offset_in_bytes()));
1181 
1182   // If result is an oop unbox and store it in frame where gc will see it
1183   // and result handler will pick it up
1184 
1185   {
1186     Label no_oop, not_weak, store_result;
1187     __ adr(t, ExternalAddress(AbstractInterpreter::result_handler(T_OBJECT)));
1188     __ cmp(t, result_handler);
1189     __ br(Assembler::NE, no_oop);
1190     // Unbox oop result, e.g. JNIHandles::resolve result.
1191     __ pop(ltos);
1192     __ cbz(r0, store_result);   // Use NULL as-is.
1193     STATIC_ASSERT(JNIHandles::weak_tag_mask == 1u);
1194     __ tbz(r0, 0, not_weak);    // Test for jweak tag.
1195     // Resolve jweak.
1196     __ ldr(r0, Address(r0, -JNIHandles::weak_tag_value));
1197 #if INCLUDE_ALL_GCS
1198     if (UseG1GC || (UseShenandoahGC && ShenandoahSATBBarrier)) {
1199       __ enter();                   // Barrier may call runtime.
1200       __ g1_write_barrier_pre(noreg /* obj */,
1201                               r0 /* pre_val */,
1202                               rthread /* thread */,
1203                               t /* tmp */,
1204                               true /* tosca_live */,
1205                               true /* expand_call */);
1206       __ leave();
1207     }
1208 #endif // INCLUDE_ALL_GCS
1209     __ b(store_result);
1210     __ bind(not_weak);
1211     // Resolve (untagged) jobject.
1212     __ ldr(r0, Address(r0, 0));
1213     __ bind(store_result);
1214     __ str(r0, Address(rfp, frame::interpreter_frame_oop_temp_offset*wordSize));
1215     // keep stack depth as expected by pushing oop which will eventually be discarded
1216     __ push(ltos);
1217     __ bind(no_oop);
1218   }
1219 
1220   {
1221     Label no_reguard;
1222     __ lea(rscratch1, Address(rthread, in_bytes(JavaThread::stack_guard_state_offset())));
1223     __ ldrb(rscratch1, Address(rscratch1));
1224     __ cmp(rscratch1, JavaThread::stack_guard_yellow_disabled);
1225     __ br(Assembler::NE, no_reguard);
1226 
1227     __ pusha(); // XXX only save smashed registers
1228     __ mov(c_rarg0, rthread);
1229     __ mov(rscratch2, CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages));
1230     __ blr(rscratch2);
1231     __ popa(); // XXX only restore smashed registers
1232     __ bind(no_reguard);
1233   }
1234 
1235   // The method register is junk from after the thread_in_native transition
1236   // until here.  Also can't call_VM until the bcp has been
1237   // restored.  Need bcp for throwing exception below so get it now.
1238   __ get_method(rmethod);
1239 
1240   // restore bcp to have legal interpreter frame, i.e., bci == 0 <=>
1241   // rbcp == code_base()
1242   __ ldr(rbcp, Address(rmethod, Method::const_offset()));   // get ConstMethod*
1243   __ add(rbcp, rbcp, in_bytes(ConstMethod::codes_offset()));          // get codebase
1244   // handle exceptions (exception handling will handle unlocking!)
1245   {
1246     Label L;
1247     __ ldr(rscratch1, Address(rthread, Thread::pending_exception_offset()));
1248     __ cbz(rscratch1, L);
1249     // Note: At some point we may want to unify this with the code
1250     // used in call_VM_base(); i.e., we should use the
1251     // StubRoutines::forward_exception code. For now this doesn't work
1252     // here because the rsp is not correctly set at this point.
1253     __ MacroAssembler::call_VM(noreg,
1254                                CAST_FROM_FN_PTR(address,
1255                                InterpreterRuntime::throw_pending_exception));
1256     __ should_not_reach_here();
1257     __ bind(L);
1258   }
1259 
1260   // do unlocking if necessary
1261   {
1262     Label L;
1263     __ ldrw(t, Address(rmethod, Method::access_flags_offset()));
1264     __ tst(t, JVM_ACC_SYNCHRONIZED);
1265     __ br(Assembler::EQ, L);
1266     // the code below should be shared with interpreter macro
1267     // assembler implementation
1268     {
1269       Label unlock;
1270       // BasicObjectLock will be first in list, since this is a
1271       // synchronized method. However, need to check that the object
1272       // has not been unlocked by an explicit monitorexit bytecode.
1273 
1274       // monitor expect in c_rarg1 for slow unlock path
1275       __ lea (c_rarg1, Address(rfp,   // address of first monitor
1276                                (intptr_t)(frame::interpreter_frame_initial_sp_offset *
1277                                           wordSize - sizeof(BasicObjectLock))));
1278 
1279       __ ldr(t, Address(c_rarg1, BasicObjectLock::obj_offset_in_bytes()));
1280       __ cbnz(t, unlock);
1281 
1282       // Entry already unlocked, need to throw exception
1283       __ MacroAssembler::call_VM(noreg,
1284                                  CAST_FROM_FN_PTR(address,
1285                    InterpreterRuntime::throw_illegal_monitor_state_exception));
1286       __ should_not_reach_here();
1287 
1288       __ bind(unlock);
1289       __ unlock_object(c_rarg1);
1290     }
1291     __ bind(L);
1292   }
1293 
1294   // jvmti support
1295   // Note: This must happen _after_ handling/throwing any exceptions since
1296   //       the exception handler code notifies the runtime of method exits
1297   //       too. If this happens before, method entry/exit notifications are
1298   //       not properly paired (was bug - gri 11/22/99).
1299   __ notify_method_exit(vtos, InterpreterMacroAssembler::NotifyJVMTI);
1300 
1301   // restore potential result in r0:d0, call result handler to
1302   // restore potential result in ST0 & handle result
1303 
1304   __ pop(ltos);
1305   __ pop(dtos);
1306 
1307   __ blr(result_handler);
1308 
1309   // remove activation
1310   __ ldr(esp, Address(rfp,
1311                     frame::interpreter_frame_sender_sp_offset *
1312                     wordSize)); // get sender sp
1313   // remove frame anchor
1314   __ leave();
1315 
1316   // resture sender sp
1317   __ mov(sp, esp);
1318 
1319   __ ret(lr);
1320 
1321   if (inc_counter) {
1322     // Handle overflow of counter and compile method
1323     __ bind(invocation_counter_overflow);
1324     generate_counter_overflow(&continue_after_compile);
1325   }
1326 
1327   return entry_point;
1328 }
1329 
1330 //
1331 // Generic interpreted method entry to (asm) interpreter
1332 //
1333 address InterpreterGenerator::generate_normal_entry(bool synchronized) {
1334   // determine code generation flags
1335   bool inc_counter  = UseCompiler || CountCompiledCalls;
1336 
1337   // rscratch1: sender sp
1338   address entry_point = __ pc();
1339 
1340   const Address constMethod(rmethod, Method::const_offset());
1341   const Address access_flags(rmethod, Method::access_flags_offset());
1342   const Address size_of_parameters(r3,
1343                                    ConstMethod::size_of_parameters_offset());
1344   const Address size_of_locals(r3, ConstMethod::size_of_locals_offset());
1345 
1346   // get parameter size (always needed)
1347   // need to load the const method first
1348   __ ldr(r3, constMethod);
1349   __ load_unsigned_short(r2, size_of_parameters);
1350 
1351   // r2: size of parameters
1352 
1353   __ load_unsigned_short(r3, size_of_locals); // get size of locals in words
1354   __ sub(r3, r3, r2); // r3 = no. of additional locals
1355 
1356   // see if we've got enough room on the stack for locals plus overhead.
1357   generate_stack_overflow_check();
1358 
1359   // compute beginning of parameters (rlocals)
1360   __ add(rlocals, esp, r2, ext::uxtx, 3);
1361   __ sub(rlocals, rlocals, wordSize);
1362 
1363   // Make room for locals
1364   __ sub(rscratch1, esp, r3, ext::uxtx, 3);
1365   __ andr(sp, rscratch1, -16);
1366 
1367   // r3 - # of additional locals
1368   // allocate space for locals
1369   // explicitly initialize locals
1370   {
1371     Label exit, loop;
1372     __ ands(zr, r3, r3);
1373     __ br(Assembler::LE, exit); // do nothing if r3 <= 0
1374     __ bind(loop);
1375     __ str(zr, Address(__ post(rscratch1, wordSize)));
1376     __ sub(r3, r3, 1); // until everything initialized
1377     __ cbnz(r3, loop);
1378     __ bind(exit);
1379   }
1380 
1381   // And the base dispatch table
1382   __ get_dispatch();
1383 
1384   // initialize fixed part of activation frame
1385   generate_fixed_frame(false);
1386 
1387   // make sure method is not native & not abstract
1388 #ifdef ASSERT
1389   __ ldrw(r0, access_flags);
1390   {
1391     Label L;
1392     __ tst(r0, JVM_ACC_NATIVE);
1393     __ br(Assembler::EQ, L);
1394     __ stop("tried to execute native method as non-native");
1395     __ bind(L);
1396   }
1397  {
1398     Label L;
1399     __ tst(r0, JVM_ACC_ABSTRACT);
1400     __ br(Assembler::EQ, L);
1401     __ stop("tried to execute abstract method in interpreter");
1402     __ bind(L);
1403   }
1404 #endif
1405 
1406   // Since at this point in the method invocation the exception
1407   // handler would try to exit the monitor of synchronized methods
1408   // which hasn't been entered yet, we set the thread local variable
1409   // _do_not_unlock_if_synchronized to true. The remove_activation
1410   // will check this flag.
1411 
1412    const Address do_not_unlock_if_synchronized(rthread,
1413         in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
1414   __ mov(rscratch2, true);
1415   __ strb(rscratch2, do_not_unlock_if_synchronized);
1416 
1417   // increment invocation count & check for overflow
1418   Label invocation_counter_overflow;
1419   Label profile_method;
1420   Label profile_method_continue;
1421   if (inc_counter) {
1422     generate_counter_incr(&invocation_counter_overflow,
1423                           &profile_method,
1424                           &profile_method_continue);
1425     if (ProfileInterpreter) {
1426       __ bind(profile_method_continue);
1427     }
1428   }
1429 
1430   Label continue_after_compile;
1431   __ bind(continue_after_compile);
1432 
1433   bang_stack_shadow_pages(false);
1434 
1435   // reset the _do_not_unlock_if_synchronized flag
1436   __ strb(zr, do_not_unlock_if_synchronized);
1437 
1438   // check for synchronized methods
1439   // Must happen AFTER invocation_counter check and stack overflow check,
1440   // so method is not locked if overflows.
1441   if (synchronized) {
1442     // Allocate monitor and lock method
1443     lock_method();
1444   } else {
1445     // no synchronization necessary
1446 #ifdef ASSERT
1447     {
1448       Label L;
1449       __ ldrw(r0, access_flags);
1450       __ tst(r0, JVM_ACC_SYNCHRONIZED);
1451       __ br(Assembler::EQ, L);
1452       __ stop("method needs synchronization");
1453       __ bind(L);
1454     }
1455 #endif
1456   }
1457 
1458   // start execution
1459 #ifdef ASSERT
1460   {
1461     Label L;
1462      const Address monitor_block_top (rfp,
1463                  frame::interpreter_frame_monitor_block_top_offset * wordSize);
1464     __ ldr(rscratch1, monitor_block_top);
1465     __ cmp(esp, rscratch1);
1466     __ br(Assembler::EQ, L);
1467     __ stop("broken stack frame setup in interpreter");
1468     __ bind(L);
1469   }
1470 #endif
1471 
1472   // jvmti support
1473   __ notify_method_entry();
1474 
1475   __ dispatch_next(vtos);
1476 
1477   // invocation counter overflow
1478   if (inc_counter) {
1479     if (ProfileInterpreter) {
1480       // We have decided to profile this method in the interpreter
1481       __ bind(profile_method);
1482       __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::profile_method));
1483       __ set_method_data_pointer_for_bcp();
1484       // don't think we need this
1485       __ get_method(r1);
1486       __ b(profile_method_continue);
1487     }
1488     // Handle overflow of counter and compile method
1489     __ bind(invocation_counter_overflow);
1490     generate_counter_overflow(&continue_after_compile);
1491   }
1492 
1493   return entry_point;
1494 }
1495 
1496 // Entry points
1497 //
1498 // Here we generate the various kind of entries into the interpreter.
1499 // The two main entry type are generic bytecode methods and native
1500 // call method.  These both come in synchronized and non-synchronized
1501 // versions but the frame layout they create is very similar. The
1502 // other method entry types are really just special purpose entries
1503 // that are really entry and interpretation all in one. These are for
1504 // trivial methods like accessor, empty, or special math methods.
1505 //
1506 // When control flow reaches any of the entry types for the interpreter
1507 // the following holds ->
1508 //
1509 // Arguments:
1510 //
1511 // rmethod: Method*
1512 //
1513 // Stack layout immediately at entry
1514 //
1515 // [ return address     ] <--- rsp
1516 // [ parameter n        ]
1517 //   ...
1518 // [ parameter 1        ]
1519 // [ expression stack   ] (caller's java expression stack)
1520 
1521 // Assuming that we don't go to one of the trivial specialized entries
1522 // the stack will look like below when we are ready to execute the
1523 // first bytecode (or call the native routine). The register usage
1524 // will be as the template based interpreter expects (see
1525 // interpreter_aarch64.hpp).
1526 //
1527 // local variables follow incoming parameters immediately; i.e.
1528 // the return address is moved to the end of the locals).
1529 //
1530 // [ monitor entry      ] <--- esp
1531 //   ...
1532 // [ monitor entry      ]
1533 // [ expr. stack bottom ]
1534 // [ saved rbcp         ]
1535 // [ current rlocals    ]
1536 // [ Method*            ]
1537 // [ saved rfp          ] <--- rfp
1538 // [ return address     ]
1539 // [ local variable m   ]
1540 //   ...
1541 // [ local variable 1   ]
1542 // [ parameter n        ]
1543 //   ...
1544 // [ parameter 1        ] <--- rlocals
1545 
1546 address AbstractInterpreterGenerator::generate_method_entry(
1547                                         AbstractInterpreter::MethodKind kind) {
1548   // determine code generation flags
1549   bool synchronized = false;
1550   address entry_point = NULL;
1551 
1552   switch (kind) {
1553   case Interpreter::zerolocals             :                                                                             break;
1554   case Interpreter::zerolocals_synchronized: synchronized = true;                                                        break;
1555   case Interpreter::native                 : entry_point = ((InterpreterGenerator*) this)->generate_native_entry(false); break;
1556   case Interpreter::native_synchronized    : entry_point = ((InterpreterGenerator*) this)->generate_native_entry(true);  break;
1557   case Interpreter::empty                  : entry_point = ((InterpreterGenerator*) this)->generate_empty_entry();       break;
1558   case Interpreter::accessor               : entry_point = ((InterpreterGenerator*) this)->generate_accessor_entry();    break;
1559   case Interpreter::abstract               : entry_point = ((InterpreterGenerator*) this)->generate_abstract_entry();    break;
1560 
1561   case Interpreter::java_lang_math_sin     : // fall thru
1562   case Interpreter::java_lang_math_cos     : // fall thru
1563   case Interpreter::java_lang_math_tan     : // fall thru
1564   case Interpreter::java_lang_math_abs     : // fall thru
1565   case Interpreter::java_lang_math_log     : // fall thru
1566   case Interpreter::java_lang_math_log10   : // fall thru
1567   case Interpreter::java_lang_math_sqrt    : // fall thru
1568   case Interpreter::java_lang_math_pow     : // fall thru
1569   case Interpreter::java_lang_math_exp     : entry_point = ((InterpreterGenerator*) this)->generate_math_entry(kind);    break;
1570   case Interpreter::java_lang_ref_reference_get
1571                                            : entry_point = ((InterpreterGenerator*)this)->generate_Reference_get_entry(); break;
1572   case Interpreter::java_util_zip_CRC32_update
1573                                            : entry_point = ((InterpreterGenerator*)this)->generate_CRC32_update_entry();  break;
1574   case Interpreter::java_util_zip_CRC32_updateBytes
1575                                            : // fall thru
1576   case Interpreter::java_util_zip_CRC32_updateByteBuffer
1577                                            : entry_point = ((InterpreterGenerator*)this)->generate_CRC32_updateBytes_entry(kind); break;
1578   default                                  : ShouldNotReachHere();                                                       break;
1579   }
1580 
1581   if (entry_point) {
1582     return entry_point;
1583   }
1584 
1585   return ((InterpreterGenerator*) this)->
1586                                 generate_normal_entry(synchronized);
1587 }
1588 
1589 
1590 // These should never be compiled since the interpreter will prefer
1591 // the compiled version to the intrinsic version.
1592 bool AbstractInterpreter::can_be_compiled(methodHandle m) {
1593   switch (method_kind(m)) {
1594     case Interpreter::java_lang_math_sin     : // fall thru
1595     case Interpreter::java_lang_math_cos     : // fall thru
1596     case Interpreter::java_lang_math_tan     : // fall thru
1597     case Interpreter::java_lang_math_abs     : // fall thru
1598     case Interpreter::java_lang_math_log     : // fall thru
1599     case Interpreter::java_lang_math_log10   : // fall thru
1600     case Interpreter::java_lang_math_sqrt    : // fall thru
1601     case Interpreter::java_lang_math_pow     : // fall thru
1602     case Interpreter::java_lang_math_exp     :
1603       return false;
1604     default:
1605       return true;
1606   }
1607 }
1608 
1609 // How much stack a method activation needs in words.
1610 int AbstractInterpreter::size_top_interpreter_activation(Method* method) {
1611   const int entry_size = frame::interpreter_frame_monitor_size();
1612 
1613   // total overhead size: entry_size + (saved rfp thru expr stack
1614   // bottom).  be sure to change this if you add/subtract anything
1615   // to/from the overhead area
1616   const int overhead_size =
1617     -(frame::interpreter_frame_initial_sp_offset) + entry_size;
1618 
1619   const int stub_code = frame::entry_frame_after_call_words;
1620   const int method_stack = (method->max_locals() + method->max_stack()) *
1621                            Interpreter::stackElementWords;
1622   return (overhead_size + method_stack + stub_code);
1623 }
1624 
1625 // asm based interpreter deoptimization helpers
1626 int AbstractInterpreter::size_activation(int max_stack,
1627                                          int temps,
1628                                          int extra_args,
1629                                          int monitors,
1630                                          int callee_params,
1631                                          int callee_locals,
1632                                          bool is_top_frame) {
1633   // Note: This calculation must exactly parallel the frame setup
1634   // in AbstractInterpreterGenerator::generate_method_entry.
1635 
1636   // fixed size of an interpreter frame:
1637   int overhead = frame::sender_sp_offset -
1638                  frame::interpreter_frame_initial_sp_offset;
1639   // Our locals were accounted for by the caller (or last_frame_adjust
1640   // on the transistion) Since the callee parameters already account
1641   // for the callee's params we only need to account for the extra
1642   // locals.
1643   int size = overhead +
1644          (callee_locals - callee_params) +
1645          monitors * frame::interpreter_frame_monitor_size() +
1646          // On the top frame, at all times SP <= ESP, and SP is
1647          // 16-aligned.  We ensure this by adjusting SP on method
1648          // entry and re-entry to allow room for the maximum size of
1649          // the expression stack.  When we call another method we bump
1650          // SP so that no stack space is wasted.  So, only on the top
1651          // frame do we need to allow max_stack words.
1652          (is_top_frame ? max_stack : temps + extra_args);
1653 
1654   // On AArch64 we always keep the stack pointer 16-aligned, so we
1655   // must round up here.
1656   size = round_to(size, 2);
1657 
1658   return size;
1659 }
1660 
1661 void AbstractInterpreter::layout_activation(Method* method,
1662                                             int tempcount,
1663                                             int popframe_extra_args,
1664                                             int moncount,
1665                                             int caller_actual_parameters,
1666                                             int callee_param_count,
1667                                             int callee_locals,
1668                                             frame* caller,
1669                                             frame* interpreter_frame,
1670                                             bool is_top_frame,
1671                                             bool is_bottom_frame) {
1672   // The frame interpreter_frame is guaranteed to be the right size,
1673   // as determined by a previous call to the size_activation() method.
1674   // It is also guaranteed to be walkable even though it is in a
1675   // skeletal state
1676 
1677   int max_locals = method->max_locals() * Interpreter::stackElementWords;
1678   int extra_locals = (method->max_locals() - method->size_of_parameters()) *
1679     Interpreter::stackElementWords;
1680 
1681 #ifdef ASSERT
1682   assert(caller->sp() == interpreter_frame->sender_sp(), "Frame not properly walkable");
1683 #endif
1684 
1685   interpreter_frame->interpreter_frame_set_method(method);
1686   // NOTE the difference in using sender_sp and
1687   // interpreter_frame_sender_sp interpreter_frame_sender_sp is
1688   // the original sp of the caller (the unextended_sp) and
1689   // sender_sp is fp+8/16 (32bit/64bit) XXX
1690   intptr_t* locals = interpreter_frame->sender_sp() + max_locals - 1;
1691 
1692 #ifdef ASSERT
1693   if (caller->is_interpreted_frame()) {
1694     assert(locals < caller->fp() + frame::interpreter_frame_initial_sp_offset, "bad placement");
1695   }
1696 #endif
1697 
1698   interpreter_frame->interpreter_frame_set_locals(locals);
1699   BasicObjectLock* montop = interpreter_frame->interpreter_frame_monitor_begin();
1700   BasicObjectLock* monbot = montop - moncount;
1701   interpreter_frame->interpreter_frame_set_monitor_end(monbot);
1702 
1703   // Set last_sp
1704   intptr_t*  esp = (intptr_t*) monbot -
1705     tempcount*Interpreter::stackElementWords -
1706     popframe_extra_args;
1707   interpreter_frame->interpreter_frame_set_last_sp(esp);
1708 
1709   // All frames but the initial (oldest) interpreter frame we fill in have
1710   // a value for sender_sp that allows walking the stack but isn't
1711   // truly correct. Correct the value here.
1712   if (extra_locals != 0 &&
1713       interpreter_frame->sender_sp() ==
1714       interpreter_frame->interpreter_frame_sender_sp()) {
1715     interpreter_frame->set_interpreter_frame_sender_sp(caller->sp() +
1716                                                        extra_locals);
1717   }
1718   *interpreter_frame->interpreter_frame_cache_addr() =
1719     method->constants()->cache();
1720 }
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()
1765          + (EnableInvokeDynamic ? 2 : 0) + 2);
1766   __ ldr(rscratch2,
1767          Address(rfp, frame::interpreter_frame_initial_sp_offset * wordSize));
1768   __ sub(rscratch1, rscratch2, rscratch1, ext::uxtx, 3);
1769   __ andr(sp, rscratch1, -16);
1770 
1771   // r0: exception handler entry point
1772   // r3: preserved exception oop
1773   // rbcp: bcp for exception handler
1774   __ push_ptr(r3); // push exception which is now the only value on the stack
1775   __ br(r0); // jump to exception handler (may be _remove_activation_entry!)
1776 
1777   // If the exception is not handled in the current frame the frame is
1778   // removed and the exception is rethrown (i.e. exception
1779   // continuation is _rethrow_exception).
1780   //
1781   // Note: At this point the bci is still the bxi for the instruction
1782   // which caused the exception and the expression stack is
1783   // empty. Thus, for any VM calls at this point, GC will find a legal
1784   // oop map (with empty expression stack).
1785 
1786   //
1787   // JVMTI PopFrame support
1788   //
1789 
1790   Interpreter::_remove_activation_preserving_args_entry = __ pc();
1791   __ empty_expression_stack();
1792   // Set the popframe_processing bit in pending_popframe_condition
1793   // indicating that we are currently handling popframe, so that
1794   // call_VMs that may happen later do not trigger new popframe
1795   // handling cycles.
1796   __ ldrw(r3, Address(rthread, JavaThread::popframe_condition_offset()));
1797   __ orr(r3, r3, JavaThread::popframe_processing_bit);
1798   __ strw(r3, Address(rthread, JavaThread::popframe_condition_offset()));
1799 
1800   {
1801     // Check to see whether we are returning to a deoptimized frame.
1802     // (The PopFrame call ensures that the caller of the popped frame is
1803     // either interpreted or compiled and deoptimizes it if compiled.)
1804     // In this case, we can't call dispatch_next() after the frame is
1805     // popped, but instead must save the incoming arguments and restore
1806     // them after deoptimization has occurred.
1807     //
1808     // Note that we don't compare the return PC against the
1809     // deoptimization blob's unpack entry because of the presence of
1810     // adapter frames in C2.
1811     Label caller_not_deoptimized;
1812     __ ldr(c_rarg1, Address(rfp, frame::return_addr_offset * wordSize));
1813     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1814                                InterpreterRuntime::interpreter_contains), c_rarg1);
1815     __ cbnz(r0, caller_not_deoptimized);
1816 
1817     // Compute size of arguments for saving when returning to
1818     // deoptimized caller
1819     __ get_method(r0);
1820     __ ldr(r0, Address(r0, Method::const_offset()));
1821     __ load_unsigned_short(r0, Address(r0, in_bytes(ConstMethod::
1822                                                     size_of_parameters_offset())));
1823     __ lsl(r0, r0, Interpreter::logStackElementSize);
1824     __ restore_locals(); // XXX do we need this?
1825     __ sub(rlocals, rlocals, r0);
1826     __ add(rlocals, rlocals, wordSize);
1827     // Save these arguments
1828     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1829                                            Deoptimization::
1830                                            popframe_preserve_args),
1831                           rthread, r0, rlocals);
1832 
1833     __ remove_activation(vtos,
1834                          /* throw_monitor_exception */ false,
1835                          /* install_monitor_exception */ false,
1836                          /* notify_jvmdi */ false);
1837 
1838     // Inform deoptimization that it is responsible for restoring
1839     // these arguments
1840     __ mov(rscratch1, JavaThread::popframe_force_deopt_reexecution_bit);
1841     __ strw(rscratch1, Address(rthread, JavaThread::popframe_condition_offset()));
1842 
1843     // Continue in deoptimization handler
1844     __ ret(lr);
1845 
1846     __ bind(caller_not_deoptimized);
1847   }
1848 
1849   __ remove_activation(vtos,
1850                        /* throw_monitor_exception */ false,
1851                        /* install_monitor_exception */ false,
1852                        /* notify_jvmdi */ false);
1853 
1854   // Restore the last_sp and null it out
1855   __ ldr(esp, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
1856   __ str(zr, Address(rfp, frame::interpreter_frame_last_sp_offset * wordSize));
1857 
1858   __ restore_bcp();
1859   __ restore_locals();
1860   __ restore_constant_pool_cache();
1861   __ get_method(rmethod);
1862   __ get_dispatch();
1863 
1864   // The method data pointer was incremented already during
1865   // call profiling. We have to restore the mdp for the current bcp.
1866   if (ProfileInterpreter) {
1867     __ set_method_data_pointer_for_bcp();
1868   }
1869 
1870   // Clear the popframe condition flag
1871   __ strw(zr, Address(rthread, JavaThread::popframe_condition_offset()));
1872   assert(JavaThread::popframe_inactive == 0, "fix popframe_inactive");
1873 
1874 #if INCLUDE_JVMTI
1875   if (EnableInvokeDynamic) {
1876     Label L_done;
1877 
1878     __ ldrb(rscratch1, Address(rbcp, 0));
1879     __ cmpw(rscratch1, Bytecodes::_invokestatic);
1880     __ br(Assembler::NE, L_done);
1881 
1882     // The member name argument must be restored if _invokestatic is re-executed after a PopFrame call.
1883     // Detect such a case in the InterpreterRuntime function and return the member name argument, or NULL.
1884 
1885     __ ldr(c_rarg0, Address(rlocals, 0));
1886     __ call_VM(r0, CAST_FROM_FN_PTR(address, InterpreterRuntime::member_name_arg_or_null), c_rarg0, rmethod, rbcp);
1887 
1888     __ cbz(r0, L_done);
1889 
1890     __ str(r0, Address(esp, 0));
1891     __ bind(L_done);
1892   }
1893 #endif // INCLUDE_JVMTI
1894 
1895   // Restore machine SP
1896   __ ldr(rscratch1, Address(rmethod, Method::const_offset()));
1897   __ ldrh(rscratch1, Address(rscratch1, ConstMethod::max_stack_offset()));
1898   __ add(rscratch1, rscratch1, frame::interpreter_frame_monitor_size()
1899          + (EnableInvokeDynamic ? 2 : 0));
1900   __ ldr(rscratch2,
1901          Address(rfp, frame::interpreter_frame_initial_sp_offset * wordSize));
1902   __ sub(rscratch1, rscratch2, rscratch1, ext::uxtw, 3);
1903   __ andr(sp, rscratch1, -16);
1904 
1905   __ dispatch_next(vtos);
1906   // end of PopFrame support
1907 
1908   Interpreter::_remove_activation_entry = __ pc();
1909 
1910   // preserve exception over this code sequence
1911   __ pop_ptr(r0);
1912   __ str(r0, Address(rthread, JavaThread::vm_result_offset()));
1913   // remove the activation (without doing throws on illegalMonitorExceptions)
1914   __ remove_activation(vtos, false, true, false);
1915   // restore exception
1916   __ get_vm_result(r0, rthread);
1917 
1918   // In between activations - previous activation type unknown yet
1919   // compute continuation point - the continuation point expects the
1920   // following registers set up:
1921   //
1922   // r0: exception
1923   // lr: return address/pc that threw exception
1924   // esp: expression stack of caller
1925   // rfp: fp of caller
1926   __ stp(r0, lr, Address(__ pre(sp, -2 * wordSize)));  // save exception & return address
1927   __ super_call_VM_leaf(CAST_FROM_FN_PTR(address,
1928                           SharedRuntime::exception_handler_for_return_address),
1929                         rthread, lr);
1930   __ mov(r1, r0);                               // save exception handler
1931   __ ldp(r0, lr, Address(__ post(sp, 2 * wordSize)));  // restore exception & return address
1932   // We might be returning to a deopt handler that expects r3 to
1933   // contain the exception pc
1934   __ mov(r3, lr);
1935   // Note that an "issuing PC" is actually the next PC after the call
1936   __ br(r1);                                    // jump to exception
1937                                                 // handler of caller
1938 }
1939 
1940 
1941 //
1942 // JVMTI ForceEarlyReturn support
1943 //
1944 address TemplateInterpreterGenerator::generate_earlyret_entry_for(TosState state) {
1945   address entry = __ pc();
1946 
1947   __ restore_bcp();
1948   __ restore_locals();
1949   __ empty_expression_stack();
1950   __ load_earlyret_value(state);
1951 
1952   __ ldr(rscratch1, Address(rthread, JavaThread::jvmti_thread_state_offset()));
1953   Address cond_addr(rscratch1, JvmtiThreadState::earlyret_state_offset());
1954 
1955   // Clear the earlyret state
1956   assert(JvmtiThreadState::earlyret_inactive == 0, "should be");
1957   __ str(zr, cond_addr);
1958 
1959   __ remove_activation(state,
1960                        false, /* throw_monitor_exception */
1961                        false, /* install_monitor_exception */
1962                        true); /* notify_jvmdi */
1963   __ ret(lr);
1964 
1965   return entry;
1966 } // end of ForceEarlyReturn support
1967 
1968 
1969 
1970 //-----------------------------------------------------------------------------
1971 // Helper for vtos entry point generation
1972 
1973 void TemplateInterpreterGenerator::set_vtos_entry_points(Template* t,
1974                                                          address& bep,
1975                                                          address& cep,
1976                                                          address& sep,
1977                                                          address& aep,
1978                                                          address& iep,
1979                                                          address& lep,
1980                                                          address& fep,
1981                                                          address& dep,
1982                                                          address& vep) {
1983   assert(t->is_valid() && t->tos_in() == vtos, "illegal template");
1984   Label L;
1985   aep = __ pc();  __ push_ptr();  __ b(L);
1986   fep = __ pc();  __ push_f();    __ b(L);
1987   dep = __ pc();  __ push_d();    __ b(L);
1988   lep = __ pc();  __ push_l();    __ b(L);
1989   bep = cep = sep =
1990   iep = __ pc();  __ push_i();
1991   vep = __ pc();
1992   __ bind(L);
1993   generate_and_dispatch(t);
1994 }
1995 
1996 //-----------------------------------------------------------------------------
1997 // Generation of individual instructions
1998 
1999 // helpers for generate_and_dispatch
2000 
2001 
2002 InterpreterGenerator::InterpreterGenerator(StubQueue* code)
2003   : TemplateInterpreterGenerator(code) {
2004    generate_all(); // down here so it can be "virtual"
2005 }
2006 
2007 //-----------------------------------------------------------------------------
2008 
2009 // Non-product code
2010 #ifndef PRODUCT
2011 address TemplateInterpreterGenerator::generate_trace_code(TosState state) {
2012   address entry = __ pc();
2013 
2014   __ push(lr);
2015   __ push(state);
2016   __ push(RegSet::range(r0, r15), sp);
2017   __ mov(c_rarg2, r0);  // Pass itos
2018   __ call_VM(noreg,
2019              CAST_FROM_FN_PTR(address, SharedRuntime::trace_bytecode),
2020              c_rarg1, c_rarg2, c_rarg3);
2021   __ pop(RegSet::range(r0, r15), sp);
2022   __ pop(state);
2023   __ pop(lr);
2024   __ ret(lr);                                   // return from result handler
2025 
2026   return entry;
2027 }
2028 
2029 void TemplateInterpreterGenerator::count_bytecode() {
2030   Register rscratch3 = r0;
2031   __ push(rscratch1);
2032   __ push(rscratch2);
2033   __ push(rscratch3);
2034   __ mov(rscratch3, (address) &BytecodeCounter::_counter_value);
2035   __ atomic_add(noreg, 1, rscratch3);
2036   __ pop(rscratch3);
2037   __ pop(rscratch2);
2038   __ pop(rscratch1);
2039 }
2040 
2041 void TemplateInterpreterGenerator::histogram_bytecode(Template* t) { ; }
2042 
2043 void TemplateInterpreterGenerator::histogram_bytecode_pair(Template* t) { ; }
2044 
2045 
2046 void TemplateInterpreterGenerator::trace_bytecode(Template* t) {
2047   // Call a little run-time stub to avoid blow-up for each bytecode.
2048   // The run-time runtime saves the right registers, depending on
2049   // the tosca in-state for the given template.
2050 
2051   assert(Interpreter::trace_code(t->tos_in()) != NULL,
2052          "entry must have been generated");
2053   __ bl(Interpreter::trace_code(t->tos_in()));
2054   __ reinit_heapbase();
2055 }
2056 
2057 
2058 void TemplateInterpreterGenerator::stop_interpreter_at() {
2059   Label L;
2060   __ push(rscratch1);
2061   __ mov(rscratch1, (address) &BytecodeCounter::_counter_value);
2062   __ ldr(rscratch1, Address(rscratch1));
2063   __ mov(rscratch2, StopInterpreterAt);
2064   __ cmpw(rscratch1, rscratch2);
2065   __ br(Assembler::NE, L);
2066   __ brk(0);
2067   __ bind(L);
2068   __ pop(rscratch1);
2069 }
2070 
2071 #endif // !PRODUCT
2072 #endif // ! CC_INTERP