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