1 /*
   2  * Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "asm/assembler.hpp"
  27 #include "asm/macroAssembler.inline.hpp"
  28 #include "classfile/javaClasses.hpp"
  29 #include "interpreter/bytecodeHistogram.hpp"
  30 #include "interpreter/interp_masm.hpp"
  31 #include "interpreter/interpreter.hpp"
  32 #include "interpreter/interpreterRuntime.hpp"
  33 #include "interpreter/templateInterpreterGenerator.hpp"
  34 #include "interpreter/templateTable.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 "prims/methodHandles.hpp"
  42 #include "runtime/arguments.hpp"
  43 #include "runtime/deoptimization.hpp"
  44 #include "runtime/frame.inline.hpp"
  45 #include "runtime/jniHandles.hpp"
  46 #include "runtime/sharedRuntime.hpp"
  47 #include "runtime/stubRoutines.hpp"
  48 #include "runtime/synchronizer.hpp"
  49 #include "runtime/timer.hpp"
  50 #include "runtime/vframeArray.hpp"
  51 #include "utilities/align.hpp"
  52 #include "utilities/debug.hpp"
  53 #include "utilities/macros.hpp"
  54 
  55 // Size of interpreter code.  Increase if too small.  Interpreter will
  56 // fail with a guarantee ("not enough space for interpreter generation");
  57 // if too small.
  58 // Run with +PrintInterpreter to get the VM to print out the size.
  59 // Max size with JVMTI
  60 int TemplateInterpreter::InterpreterCodeSize = 180 * 1024;
  61 
  62 #define __ _masm->
  63 
  64 //------------------------------------------------------------------------------------------------------------------------
  65 
  66 address TemplateInterpreterGenerator::generate_slow_signature_handler() {
  67   address entry = __ pc();
  68 
  69   // callee-save register for saving LR, shared with generate_native_entry
  70   const Register Rsaved_ret_addr = Rtmp_save0;
  71 
  72   __ mov(Rsaved_ret_addr, LR);
  73 
  74   __ mov(R1, Rmethod);
  75   __ mov(R2, Rlocals);
  76   __ mov(R3, SP);
  77 
  78 
  79   // Safer to save R9 (when scratched) since callers may have been
  80   // written assuming R9 survives. This is suboptimal but
  81   // probably not important for this slow case call site.
  82   // Note for R9 saving: slow_signature_handler may copy register
  83   // arguments above the current SP (passed as R3). It is safe for
  84   // call_VM to use push and pop to protect additional values on the
  85   // stack if needed.
  86   __ call_VM(CAST_FROM_FN_PTR(address, InterpreterRuntime::slow_signature_handler), true /* save R9 if needed*/);
  87   __ add(SP, SP, wordSize);     // Skip R0
  88   __ pop(RegisterSet(R1, R3));  // Load arguments passed in registers
  89 #ifdef __ABI_HARD__
  90   // Few alternatives to an always-load-FP-registers approach:
  91   // - parse method signature to detect FP arguments
  92   // - keep a counter/flag on a stack indicationg number of FP arguments in the method.
  93   // The later has been originally implemented and tested but a conditional path could
  94   // eliminate any gain imposed by avoiding 8 double word loads.
  95   __ fldmiad(SP, FloatRegisterSet(D0, 8), writeback);
  96 #endif // __ABI_HARD__
  97 
  98   __ ret(Rsaved_ret_addr);
  99 
 100   return entry;
 101 }
 102 
 103 
 104 //
 105 // Various method entries (that c++ and asm interpreter agree upon)
 106 //------------------------------------------------------------------------------------------------------------------------
 107 //
 108 //
 109 
 110 // Abstract method entry
 111 // Attempt to execute abstract method. Throw exception
 112 address TemplateInterpreterGenerator::generate_abstract_entry(void) {
 113   address entry_point = __ pc();
 114 
 115 
 116   __ empty_expression_stack();
 117 
 118   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_AbstractMethodError));
 119 
 120   DEBUG_ONLY(STOP("generate_abstract_entry");) // Should not reach here
 121   return entry_point;
 122 }
 123 
 124 address TemplateInterpreterGenerator::generate_math_entry(AbstractInterpreter::MethodKind kind) {
 125   if (!InlineIntrinsics) return NULL; // Generate a vanilla entry
 126 
 127   address entry_point = NULL;
 128   Register continuation = LR;
 129   bool use_runtime_call = false;
 130   switch (kind) {
 131   case Interpreter::java_lang_math_abs:
 132     entry_point = __ pc();
 133 #ifdef __SOFTFP__
 134     use_runtime_call = true;
 135     __ ldrd(R0, Address(SP));
 136 #else // !__SOFTFP__
 137     __ ldr_double(D0, Address(SP));
 138     __ abs_double(D0, D0);
 139 #endif // __SOFTFP__
 140     break;
 141   case Interpreter::java_lang_math_sqrt:
 142     entry_point = __ pc();
 143 #ifdef __SOFTFP__
 144     use_runtime_call = true;
 145     __ ldrd(R0, Address(SP));
 146 #else // !__SOFTFP__
 147     __ ldr_double(D0, Address(SP));
 148     __ sqrt_double(D0, D0);
 149 #endif // __SOFTFP__
 150     break;
 151   case Interpreter::java_lang_math_sin:
 152   case Interpreter::java_lang_math_cos:
 153   case Interpreter::java_lang_math_tan:
 154   case Interpreter::java_lang_math_log:
 155   case Interpreter::java_lang_math_log10:
 156   case Interpreter::java_lang_math_exp:
 157     entry_point = __ pc();
 158     use_runtime_call = true;
 159 #ifdef __SOFTFP__
 160     __ ldrd(R0, Address(SP));
 161 #else // !__SOFTFP__
 162     __ ldr_double(D0, Address(SP));
 163 #endif // __SOFTFP__
 164     break;
 165   case Interpreter::java_lang_math_pow:
 166     entry_point = __ pc();
 167     use_runtime_call = true;
 168 #ifdef __SOFTFP__
 169     __ ldrd(R0, Address(SP, 2 * Interpreter::stackElementSize));
 170     __ ldrd(R2, Address(SP));
 171 #else // !__SOFTFP__
 172     __ ldr_double(D0, Address(SP, 2 * Interpreter::stackElementSize));
 173     __ ldr_double(D1, Address(SP));
 174 #endif // __SOFTFP__
 175     break;
 176   case Interpreter::java_lang_math_fmaD:
 177   case Interpreter::java_lang_math_fmaF:
 178     // TODO: Implement intrinsic
 179     break;
 180   default:
 181     ShouldNotReachHere();
 182   }
 183 
 184   if (entry_point != NULL) {
 185     __ mov(SP, Rsender_sp);
 186     if (use_runtime_call) {
 187       __ mov(Rtmp_save0, LR);
 188       continuation = Rtmp_save0;
 189       generate_math_runtime_call(kind);
 190     }
 191     __ ret(continuation);
 192   }
 193   return entry_point;
 194 }
 195 
 196 void TemplateInterpreterGenerator::generate_math_runtime_call(AbstractInterpreter::MethodKind kind) {
 197   address fn;
 198   switch (kind) {
 199 #ifdef __SOFTFP__
 200   case Interpreter::java_lang_math_abs:
 201     fn = CAST_FROM_FN_PTR(address, SharedRuntime::dabs);
 202     break;
 203   case Interpreter::java_lang_math_sqrt:
 204     fn = CAST_FROM_FN_PTR(address, SharedRuntime::dsqrt);
 205     break;
 206 #endif // __SOFTFP__
 207   case Interpreter::java_lang_math_sin:
 208     fn = CAST_FROM_FN_PTR(address, SharedRuntime::dsin);
 209     break;
 210   case Interpreter::java_lang_math_cos:
 211     fn = CAST_FROM_FN_PTR(address, SharedRuntime::dcos);
 212     break;
 213   case Interpreter::java_lang_math_tan:
 214     fn = CAST_FROM_FN_PTR(address, SharedRuntime::dtan);
 215     break;
 216   case Interpreter::java_lang_math_log:
 217     fn = CAST_FROM_FN_PTR(address, SharedRuntime::dlog);
 218     break;
 219   case Interpreter::java_lang_math_log10:
 220     fn = CAST_FROM_FN_PTR(address, SharedRuntime::dlog10);
 221     break;
 222   case Interpreter::java_lang_math_exp:
 223     fn = CAST_FROM_FN_PTR(address, SharedRuntime::dexp);
 224     break;
 225   case Interpreter::java_lang_math_pow:
 226     fn = CAST_FROM_FN_PTR(address, SharedRuntime::dpow);
 227     break;
 228   default:
 229     ShouldNotReachHere();
 230     fn = NULL; // silence "maybe uninitialized" compiler warnings
 231   }
 232   __ call_VM_leaf(fn);
 233 }
 234 
 235 address TemplateInterpreterGenerator::generate_StackOverflowError_handler() {
 236   address entry = __ pc();
 237 
 238   // Note: There should be a minimal interpreter frame set up when stack
 239   // overflow occurs since we check explicitly for it now.
 240   //
 241 #ifdef ASSERT
 242   { Label L;
 243     __ sub(Rtemp, FP, - frame::interpreter_frame_monitor_block_top_offset * wordSize);
 244     __ cmp(SP, Rtemp);  // Rtemp = maximal SP for current FP,
 245                         //  (stack grows negative)
 246     __ b(L, ls); // check if frame is complete
 247     __ stop ("interpreter frame not set up");
 248     __ bind(L);
 249   }
 250 #endif // ASSERT
 251 
 252   // Restore bcp under the assumption that the current frame is still
 253   // interpreted
 254   __ restore_bcp();
 255 
 256   // expression stack must be empty before entering the VM if an exception
 257   // happened
 258   __ empty_expression_stack();
 259 
 260   // throw exception
 261   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_StackOverflowError));
 262 
 263   __ should_not_reach_here();
 264 
 265   return entry;
 266 }
 267 
 268 address TemplateInterpreterGenerator::generate_ArrayIndexOutOfBounds_handler() {
 269   address entry = __ pc();
 270 
 271   // index is in R4_ArrayIndexOutOfBounds_index
 272 
 273   // expression stack must be empty before entering the VM if an exception happened
 274   __ empty_expression_stack();
 275 
 276   // setup parameters
 277   // Array expected in R1.
 278   __ mov(R2, R4_ArrayIndexOutOfBounds_index);
 279 
 280   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_ArrayIndexOutOfBoundsException), R1, R2);
 281 
 282   __ nop(); // to avoid filling CPU pipeline with invalid instructions
 283   __ nop();
 284   __ should_not_reach_here();
 285 
 286   return entry;
 287 }
 288 
 289 address TemplateInterpreterGenerator::generate_ClassCastException_handler() {
 290   address entry = __ pc();
 291 
 292   // object is in R2_ClassCastException_obj
 293 
 294   // expression stack must be empty before entering the VM if an exception
 295   // happened
 296   __ empty_expression_stack();
 297 
 298   __ mov(R1, R2_ClassCastException_obj);
 299   __ call_VM(noreg,
 300              CAST_FROM_FN_PTR(address,
 301                               InterpreterRuntime::throw_ClassCastException),
 302              R1);
 303 
 304   __ should_not_reach_here();
 305 
 306   return entry;
 307 }
 308 
 309 address TemplateInterpreterGenerator::generate_exception_handler_common(const char* name, const char* message, bool pass_oop) {
 310   assert(!pass_oop || message == NULL, "either oop or message but not both");
 311   address entry = __ pc();
 312 
 313   InlinedString Lname(name);
 314   InlinedString Lmessage(message);
 315 
 316   if (pass_oop) {
 317     // object is at TOS
 318     __ pop_ptr(R2);
 319   }
 320 
 321   // expression stack must be empty before entering the VM if an exception happened
 322   __ empty_expression_stack();
 323 
 324   // setup parameters
 325   __ ldr_literal(R1, Lname);
 326 
 327   if (pass_oop) {
 328     __ call_VM(Rexception_obj, CAST_FROM_FN_PTR(address, InterpreterRuntime::create_klass_exception), R1, R2);
 329   } else {
 330     if (message != NULL) {
 331       __ ldr_literal(R2, Lmessage);
 332     } else {
 333       __ mov(R2, 0);
 334     }
 335     __ call_VM(Rexception_obj, CAST_FROM_FN_PTR(address, InterpreterRuntime::create_exception), R1, R2);
 336   }
 337 
 338   // throw exception
 339   __ b(Interpreter::throw_exception_entry());
 340 
 341   __ nop(); // to avoid filling CPU pipeline with invalid instructions
 342   __ nop();
 343   __ bind_literal(Lname);
 344   if (!pass_oop && (message != NULL)) {
 345     __ bind_literal(Lmessage);
 346   }
 347 
 348   return entry;
 349 }
 350 
 351 address TemplateInterpreterGenerator::generate_return_entry_for(TosState state, int step, size_t index_size) {
 352   address entry = __ pc();
 353 
 354   __ interp_verify_oop(R0_tos, state, __FILE__, __LINE__);
 355 
 356   // Restore stack bottom in case i2c adjusted stack
 357   __ ldr(SP, Address(FP, frame::interpreter_frame_last_sp_offset * wordSize));
 358   // and NULL it as marker that SP is now tos until next java call
 359   __ mov(Rtemp, (int)NULL_WORD);
 360   __ str(Rtemp, Address(FP, frame::interpreter_frame_last_sp_offset * wordSize));
 361 
 362   __ restore_method();
 363   __ restore_bcp();
 364   __ restore_dispatch();
 365   __ restore_locals();
 366 
 367   const Register Rcache = R2_tmp;
 368   const Register Rindex = R3_tmp;
 369   __ get_cache_and_index_at_bcp(Rcache, Rindex, 1, index_size);
 370 
 371   __ add(Rtemp, Rcache, AsmOperand(Rindex, lsl, LogBytesPerWord));
 372   __ ldrb(Rtemp, Address(Rtemp, ConstantPoolCache::base_offset() + ConstantPoolCacheEntry::flags_offset()));
 373   __ check_stack_top();
 374   __ add(Rstack_top, Rstack_top, AsmOperand(Rtemp, lsl, Interpreter::logStackElementSize));
 375 
 376   __ convert_retval_to_tos(state);
 377 
 378  __ check_and_handle_popframe();
 379  __ check_and_handle_earlyret();
 380 
 381   __ dispatch_next(state, step);
 382 
 383   return entry;
 384 }
 385 
 386 
 387 address TemplateInterpreterGenerator::generate_deopt_entry_for(TosState state, int step, address continuation) {
 388   address entry = __ pc();
 389 
 390   __ interp_verify_oop(R0_tos, state, __FILE__, __LINE__);
 391 
 392   // The stack is not extended by deopt but we must NULL last_sp as this
 393   // entry is like a "return".
 394   __ mov(Rtemp, 0);
 395   __ str(Rtemp, Address(FP, frame::interpreter_frame_last_sp_offset * wordSize));
 396 
 397   __ restore_method();
 398   __ restore_bcp();
 399   __ restore_dispatch();
 400   __ restore_locals();
 401 
 402   // handle exceptions
 403   { Label L;
 404     __ ldr(Rtemp, Address(Rthread, Thread::pending_exception_offset()));
 405     __ cbz(Rtemp, L);
 406     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_pending_exception));
 407     __ should_not_reach_here();
 408     __ bind(L);
 409   }
 410 
 411   if (continuation == NULL) {
 412     __ dispatch_next(state, step);
 413   } else {
 414     __ jump_to_entry(continuation);
 415   }
 416 
 417   return entry;
 418 }
 419 
 420 address TemplateInterpreterGenerator::generate_result_handler_for(BasicType type) {
 421   address entry = __ pc();
 422 
 423   switch (type) {
 424   case T_CHAR    : /* Nothing to do */  break;
 425   case T_BYTE    : /* Nothing to do */  break;
 426   case T_SHORT   : /* Nothing to do */  break;
 427   case T_INT     : /* Nothing to do */  break;
 428   case T_LONG    : /* Nothing to do */  break;
 429   case T_VOID    : /* Nothing to do */  break;
 430   case T_DOUBLE  : /* Nothing to do */  break;
 431   case T_FLOAT   : /* Nothing to do */  break;
 432   case T_BOOLEAN : __ c2bool(R0);       break;
 433   case T_OBJECT  :
 434     __ ldr(R0, Address(FP, frame::interpreter_frame_oop_temp_offset * wordSize));
 435     __ verify_oop(R0);
 436     break;
 437   default        : __ should_not_reach_here(); break;
 438   }
 439 
 440   __ ret();
 441   return entry;
 442 }
 443 
 444 address TemplateInterpreterGenerator::generate_safept_entry_for(TosState state, address runtime_entry) {
 445   address entry = __ pc();
 446   __ push(state);
 447   __ call_VM(noreg, runtime_entry);
 448 
 449   // load current bytecode
 450   __ ldrb(R3_bytecode, Address(Rbcp));
 451   __ dispatch_only_normal(vtos);
 452   return entry;
 453 }
 454 
 455 
 456 // Helpers for commoning out cases in the various type of method entries.
 457 //
 458 
 459 // increment invocation count & check for overflow
 460 //
 461 // Note: checking for negative value instead of overflow
 462 //       so we have a 'sticky' overflow test
 463 //
 464 // In: Rmethod.
 465 //
 466 // Uses R0, R1, Rtemp.
 467 //
 468 void TemplateInterpreterGenerator::generate_counter_incr(Label* overflow) {
 469   Label done;
 470   const Register Rcounters = Rtemp;
 471   const Address invocation_counter(Rcounters,
 472                 MethodCounters::invocation_counter_offset() +
 473                 InvocationCounter::counter_offset());
 474 
 475   // Note: In tiered we increment either counters in MethodCounters* or
 476   // in MDO depending if we're profiling or not.
 477   int increment = InvocationCounter::count_increment;
 478   Label no_mdo;
 479   if (ProfileInterpreter) {
 480     // Are we profiling?
 481     __ ldr(R1_tmp, Address(Rmethod, Method::method_data_offset()));
 482     __ cbz(R1_tmp, no_mdo);
 483     // Increment counter in the MDO
 484     const Address mdo_invocation_counter(R1_tmp,
 485                   in_bytes(MethodData::invocation_counter_offset()) +
 486                   in_bytes(InvocationCounter::counter_offset()));
 487     const Address mask(R1_tmp, in_bytes(MethodData::invoke_mask_offset()));
 488     __ increment_mask_and_jump(mdo_invocation_counter, increment, mask, R0_tmp, Rtemp, eq, overflow);
 489     __ b(done);
 490   }
 491   __ bind(no_mdo);
 492   __ get_method_counters(Rmethod, Rcounters, done);
 493   const Address mask(Rcounters, in_bytes(MethodCounters::invoke_mask_offset()));
 494   __ increment_mask_and_jump(invocation_counter, increment, mask, R0_tmp, R1_tmp, eq, overflow);
 495   __ bind(done);
 496 }
 497 
 498 void TemplateInterpreterGenerator::generate_counter_overflow(Label& do_continue) {
 499   // InterpreterRuntime::frequency_counter_overflow takes one argument
 500   // indicating if the counter overflow occurs at a backwards branch (non-NULL bcp).
 501   // The call returns the address of the verified entry point for the method or NULL
 502   // if the compilation did not complete (either went background or bailed out).
 503   __ mov(R1, (int)false);
 504   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::frequency_counter_overflow), R1);
 505 
 506   // jump to the interpreted entry.
 507   __ b(do_continue);
 508 }
 509 
 510 void TemplateInterpreterGenerator::generate_stack_overflow_check(void) {
 511   // Check if we've got enough room on the stack for
 512   //  - overhead;
 513   //  - locals;
 514   //  - expression stack.
 515   //
 516   // Registers on entry:
 517   //
 518   // R3 = number of additional locals
 519   // Rthread
 520   // Rmethod
 521   // Registers used: R0, R1, R2, Rtemp.
 522 
 523   const Register Radditional_locals = R3;
 524   const Register RmaxStack = R2;
 525 
 526   // monitor entry size
 527   const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
 528 
 529   // total overhead size: entry_size + (saved registers, thru expr stack bottom).
 530   // be sure to change this if you add/subtract anything to/from the overhead area
 531   const int overhead_size = (frame::sender_sp_offset - frame::interpreter_frame_initial_sp_offset)*wordSize + entry_size;
 532 
 533   // Pages reserved for VM runtime calls and subsequent Java calls.
 534   const int reserved_pages = StackOverflow::stack_shadow_zone_size();
 535 
 536   // Thread::stack_size() includes guard pages, and they should not be touched.
 537   const int guard_pages = StackOverflow::stack_guard_zone_size();
 538 
 539   __ ldr(R0, Address(Rthread, Thread::stack_base_offset()));
 540   __ ldr(R1, Address(Rthread, Thread::stack_size_offset()));
 541   __ ldr(Rtemp, Address(Rmethod, Method::const_offset()));
 542   __ ldrh(RmaxStack, Address(Rtemp, ConstMethod::max_stack_offset()));
 543   __ sub_slow(Rtemp, SP, overhead_size + reserved_pages + guard_pages + Method::extra_stack_words());
 544 
 545   // reserve space for additional locals
 546   __ sub(Rtemp, Rtemp, AsmOperand(Radditional_locals, lsl, Interpreter::logStackElementSize));
 547 
 548   // stack size
 549   __ sub(R0, R0, R1);
 550 
 551   // reserve space for expression stack
 552   __ sub(Rtemp, Rtemp, AsmOperand(RmaxStack, lsl, Interpreter::logStackElementSize));
 553 
 554   __ cmp(Rtemp, R0);
 555 
 556   __ mov(SP, Rsender_sp, ls);  // restore SP
 557   __ b(StubRoutines::throw_StackOverflowError_entry(), ls);
 558 }
 559 
 560 
 561 // Allocate monitor and lock method (asm interpreter)
 562 //
 563 void TemplateInterpreterGenerator::lock_method() {
 564   // synchronize method
 565 
 566   const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
 567   assert ((entry_size % StackAlignmentInBytes) == 0, "should keep stack alignment");
 568 
 569   #ifdef ASSERT
 570     { Label L;
 571       __ ldr_u32(Rtemp, Address(Rmethod, Method::access_flags_offset()));
 572       __ tbnz(Rtemp, JVM_ACC_SYNCHRONIZED_BIT, L);
 573       __ stop("method doesn't need synchronization");
 574       __ bind(L);
 575     }
 576   #endif // ASSERT
 577 
 578   // get synchronization object
 579   { Label done;
 580     __ ldr_u32(Rtemp, Address(Rmethod, Method::access_flags_offset()));
 581     __ tst(Rtemp, JVM_ACC_STATIC);
 582     __ ldr(R0, Address(Rlocals, Interpreter::local_offset_in_bytes(0)), eq); // get receiver (assume this is frequent case)
 583     __ b(done, eq);
 584     __ load_mirror(R0, Rmethod, Rtemp);
 585     __ bind(done);
 586   }
 587 
 588   // add space for monitor & lock
 589 
 590 
 591   __ sub(Rstack_top, Rstack_top, entry_size);
 592   __ check_stack_top_on_expansion();
 593                                               // add space for a monitor entry
 594   __ str(Rstack_top, Address(FP, frame::interpreter_frame_monitor_block_top_offset * wordSize));
 595                                               // set new monitor block top
 596   __ str(R0, Address(Rstack_top, BasicObjectLock::obj_offset_in_bytes()));
 597                                               // store object
 598   __ mov(R1, Rstack_top);                     // monitor entry address
 599   __ lock_object(R1);
 600 }
 601 
 602 
 603 //
 604 // Generate a fixed interpreter frame. This is identical setup for interpreted methods
 605 // and for native methods hence the shared code.
 606 
 607 void TemplateInterpreterGenerator::generate_fixed_frame(bool native_call) {
 608   // Generates the following stack layout:
 609   //
 610   // [ expr. stack bottom ]
 611   // [ saved Rbcp         ]
 612   // [ current Rlocals    ]
 613   // [ cache              ]
 614   // [ mdx                ]
 615   // [ Method*            ]
 616   // [ last_sp            ]
 617   // [ sender_sp          ]
 618   // [ saved FP           ] <--- FP
 619   // [ saved LR           ]
 620 
 621   // initialize fixed part of activation frame
 622   __ push(LR);                                        // save return address
 623   __ push(FP);                                        // save FP
 624   __ mov(FP, SP);                                     // establish new FP
 625 
 626   __ push(Rsender_sp);
 627 
 628   __ mov(R0, 0);
 629   __ push(R0);                                        // leave last_sp as null
 630 
 631   // setup Rbcp
 632   if (native_call) {
 633     __ mov(Rbcp, 0);                                  // bcp = 0 for native calls
 634   } else {
 635     __ ldr(Rtemp, Address(Rmethod, Method::const_offset())); // get ConstMethod*
 636     __ add(Rbcp, Rtemp, ConstMethod::codes_offset()); // get codebase
 637   }
 638 
 639   __ push(Rmethod);                                    // save Method*
 640   // Get mirror and store it in the frame as GC root for this Method*
 641   __ load_mirror(Rtemp, Rmethod, Rtemp);
 642   __ push(Rtemp);
 643 
 644   if (ProfileInterpreter) {
 645     __ ldr(Rtemp, Address(Rmethod, Method::method_data_offset()));
 646     __ tst(Rtemp, Rtemp);
 647     __ add(Rtemp, Rtemp, in_bytes(MethodData::data_offset()), ne);
 648     __ push(Rtemp);                                    // set the mdp (method data pointer)
 649   } else {
 650     __ push(R0);
 651   }
 652 
 653   __ ldr(Rtemp, Address(Rmethod, Method::const_offset()));
 654   __ ldr(Rtemp, Address(Rtemp, ConstMethod::constants_offset()));
 655   __ ldr(Rtemp, Address(Rtemp, ConstantPool::cache_offset_in_bytes()));
 656   __ push(Rtemp);                                      // set constant pool cache
 657   __ push(Rlocals);                                    // set locals pointer
 658   __ push(Rbcp);                                       // set bcp
 659   __ push(R0);                                         // reserve word for pointer to expression stack bottom
 660   __ str(SP, Address(SP, 0));                          // set expression stack bottom
 661 }
 662 
 663 
 664 // End of helpers
 665 
 666 //------------------------------------------------------------------------------------------------------------------------
 667 // Entry points
 668 //
 669 // Here we generate the various kind of entries into the interpreter.
 670 // The two main entry type are generic bytecode methods and native call method.
 671 // These both come in synchronized and non-synchronized versions but the
 672 // frame layout they create is very similar. The other method entry
 673 // types are really just special purpose entries that are really entry
 674 // and interpretation all in one. These are for trivial methods like
 675 // accessor, empty, or special math methods.
 676 //
 677 // When control flow reaches any of the entry types for the interpreter
 678 // the following holds ->
 679 //
 680 // Arguments:
 681 //
 682 // Rmethod: Method*
 683 // Rthread: thread
 684 // Rsender_sp:  sender sp
 685 // Rparams (SP on 32-bit ARM): pointer to method parameters
 686 //
 687 // LR: return address
 688 //
 689 // Stack layout immediately at entry
 690 //
 691 // [ parameter n        ] <--- Rparams (SP on 32-bit ARM)
 692 //   ...
 693 // [ parameter 1        ]
 694 // [ expression stack   ] (caller's java expression stack)
 695 
 696 // Assuming that we don't go to one of the trivial specialized
 697 // entries the stack will look like below when we are ready to execute
 698 // the first bytecode (or call the native routine). The register usage
 699 // will be as the template based interpreter expects.
 700 //
 701 // local variables follow incoming parameters immediately; i.e.
 702 // the return address is saved at the end of the locals.
 703 //
 704 // [ expr. stack        ] <--- Rstack_top (SP on 32-bit ARM)
 705 // [ monitor entry      ]
 706 //   ...
 707 // [ monitor entry      ]
 708 // [ expr. stack bottom ]
 709 // [ saved Rbcp         ]
 710 // [ current Rlocals    ]
 711 // [ cache              ]
 712 // [ mdx                ]
 713 // [ mirror             ]
 714 // [ Method*            ]
 715 //
 716 // 32-bit ARM:
 717 // [ last_sp            ]
 718 //
 719 // [ sender_sp          ]
 720 // [ saved FP           ] <--- FP
 721 // [ saved LR           ]
 722 // [ optional padding(*)]
 723 // [ local variable m   ]
 724 //   ...
 725 // [ local variable 1   ]
 726 // [ parameter n        ]
 727 //   ...
 728 // [ parameter 1        ] <--- Rlocals
 729 //
 730 
 731 address TemplateInterpreterGenerator::generate_Reference_get_entry(void) {
 732   // Code: _aload_0, _getfield, _areturn
 733   // parameter size = 1
 734   //
 735   // The code that gets generated by this routine is split into 2 parts:
 736   //    1. The "intrinsified" code performing an ON_WEAK_OOP_REF load,
 737   //    2. The slow path - which is an expansion of the regular method entry.
 738   //
 739   // Notes:-
 740   // * An intrinsic is always executed, where an ON_WEAK_OOP_REF load is performed.
 741   // * We may jump to the slow path iff the receiver is null. If the
 742   //   Reference object is null then we no longer perform an ON_WEAK_OOP_REF load
 743   //   Thus we can use the regular method entry code to generate the NPE.
 744   //
 745   // Rmethod: Method*
 746   // Rthread: thread
 747   // Rsender_sp: sender sp, must be preserved for slow path, set SP to it on fast path
 748   // Rparams: parameters
 749 
 750   address entry = __ pc();
 751   Label slow_path;
 752   const Register Rthis = R0;
 753   const Register Rret_addr = Rtmp_save1;
 754   assert_different_registers(Rthis, Rret_addr, Rsender_sp);
 755 
 756   const int referent_offset = java_lang_ref_Reference::referent_offset();
 757 
 758   // Check if local 0 != NULL
 759   // If the receiver is null then it is OK to jump to the slow path.
 760   __ ldr(Rthis, Address(Rparams));
 761   __ cbz(Rthis, slow_path);
 762 
 763   // Preserve LR
 764   __ mov(Rret_addr, LR);
 765 
 766   // Load the value of the referent field.
 767   const Address field_address(Rthis, referent_offset);
 768   __ load_heap_oop(R0, field_address, Rtemp, R1_tmp, R2_tmp, ON_WEAK_OOP_REF);
 769 
 770   // _areturn
 771   __ mov(SP, Rsender_sp);
 772   __ ret(Rret_addr);
 773 
 774   // generate a vanilla interpreter entry as the slow path
 775   __ bind(slow_path);
 776   __ jump_to_entry(Interpreter::entry_for_kind(Interpreter::zerolocals));
 777   return entry;
 778 }
 779 
 780 // Not supported
 781 address TemplateInterpreterGenerator::generate_CRC32_update_entry() { return NULL; }
 782 address TemplateInterpreterGenerator::generate_CRC32_updateBytes_entry(AbstractInterpreter::MethodKind kind) { return NULL; }
 783 address TemplateInterpreterGenerator::generate_CRC32C_updateBytes_entry(AbstractInterpreter::MethodKind kind) { return NULL; }
 784 
 785 //
 786 // Interpreter stub for calling a native method. (asm interpreter)
 787 // This sets up a somewhat different looking stack for calling the native method
 788 // than the typical interpreter frame setup.
 789 //
 790 
 791 address TemplateInterpreterGenerator::generate_native_entry(bool synchronized) {
 792   // determine code generation flags
 793   bool inc_counter  = UseCompiler || CountCompiledCalls || LogTouchedMethods;
 794 
 795   // Incoming registers:
 796   //
 797   // Rmethod: Method*
 798   // Rthread: thread
 799   // Rsender_sp: sender sp
 800   // Rparams: parameters
 801 
 802   address entry_point = __ pc();
 803 
 804   // Register allocation
 805   const Register Rsize_of_params = R6;
 806   const Register Rsig_handler    = Rtmp_save0;   // R4
 807   const Register Rnative_code    = Rtmp_save1;   // R5
 808   const Register Rresult_handler = R6;
 809 
 810   const Register Rsaved_result_lo = Rtmp_save0;  // R4
 811   const Register Rsaved_result_hi = Rtmp_save1;  // R5
 812   FloatRegister saved_result_fp;
 813 
 814 
 815   __ ldr(Rsize_of_params, Address(Rmethod, Method::const_offset()));
 816   __ ldrh(Rsize_of_params,  Address(Rsize_of_params, ConstMethod::size_of_parameters_offset()));
 817 
 818   // native calls don't need the stack size check since they have no expression stack
 819   // and the arguments are already on the stack and we only add a handful of words
 820   // to the stack
 821 
 822   // compute beginning of parameters (Rlocals)
 823   __ sub(Rlocals, Rparams, wordSize);
 824   __ add(Rlocals, Rlocals, AsmOperand(Rsize_of_params, lsl, Interpreter::logStackElementSize));
 825 
 826   // reserve stack space for oop_temp
 827   __ mov(R0, 0);
 828   __ push(R0);
 829 
 830   generate_fixed_frame(true); // Note: R9 is now saved in the frame
 831 
 832   // make sure method is native & not abstract
 833 #ifdef ASSERT
 834   __ ldr_u32(Rtemp, Address(Rmethod, Method::access_flags_offset()));
 835   {
 836     Label L;
 837     __ tbnz(Rtemp, JVM_ACC_NATIVE_BIT, L);
 838     __ stop("tried to execute non-native method as native");
 839     __ bind(L);
 840   }
 841   { Label L;
 842     __ tbz(Rtemp, JVM_ACC_ABSTRACT_BIT, L);
 843     __ stop("tried to execute abstract method in interpreter");
 844     __ bind(L);
 845   }
 846 #endif
 847 
 848   // increment invocation count & check for overflow
 849   Label invocation_counter_overflow;
 850   if (inc_counter) {
 851     if (synchronized) {
 852       // Avoid unlocking method's monitor in case of exception, as it has not
 853       // been locked yet.
 854       __ set_do_not_unlock_if_synchronized(true, Rtemp);
 855     }
 856     generate_counter_incr(&invocation_counter_overflow);
 857   }
 858 
 859   Label continue_after_compile;
 860   __ bind(continue_after_compile);
 861 
 862   if (inc_counter && synchronized) {
 863     __ set_do_not_unlock_if_synchronized(false, Rtemp);
 864   }
 865 
 866   // check for synchronized methods
 867   // Must happen AFTER invocation_counter check and stack overflow check,
 868   // so method is not locked if overflows.
 869   //
 870   if (synchronized) {
 871     lock_method();
 872   } else {
 873     // no synchronization necessary
 874 #ifdef ASSERT
 875       { Label L;
 876         __ ldr_u32(Rtemp, Address(Rmethod, Method::access_flags_offset()));
 877         __ tbz(Rtemp, JVM_ACC_SYNCHRONIZED_BIT, L);
 878         __ stop("method needs synchronization");
 879         __ bind(L);
 880       }
 881 #endif
 882   }
 883 
 884   // start execution
 885 #ifdef ASSERT
 886   { Label L;
 887     __ ldr(Rtemp, Address(FP, frame::interpreter_frame_monitor_block_top_offset * wordSize));
 888     __ cmp(Rtemp, Rstack_top);
 889     __ b(L, eq);
 890     __ stop("broken stack frame setup in interpreter");
 891     __ bind(L);
 892   }
 893 #endif
 894   __ check_extended_sp(Rtemp);
 895 
 896   // jvmti/dtrace support
 897   __ notify_method_entry();
 898 #if R9_IS_SCRATCHED
 899   __ restore_method();
 900 #endif
 901 
 902   {
 903     Label L;
 904     __ ldr(Rsig_handler, Address(Rmethod, Method::signature_handler_offset()));
 905     __ cbnz(Rsig_handler, L);
 906     __ mov(R1, Rmethod);
 907     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::prepare_native_call), R1, true);
 908     __ ldr(Rsig_handler, Address(Rmethod, Method::signature_handler_offset()));
 909     __ bind(L);
 910   }
 911 
 912   {
 913     Label L;
 914     __ ldr(Rnative_code, Address(Rmethod, Method::native_function_offset()));
 915     __ cbnz(Rnative_code, L);
 916     __ mov(R1, Rmethod);
 917     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::prepare_native_call), R1);
 918     __ ldr(Rnative_code, Address(Rmethod, Method::native_function_offset()));
 919     __ bind(L);
 920   }
 921 
 922   // Allocate stack space for arguments
 923 
 924 
 925   // C functions need aligned stack
 926   __ bic(SP, SP, StackAlignmentInBytes - 1);
 927   // Multiply by BytesPerLong instead of BytesPerWord, because calling convention
 928   // may require empty slots due to long alignment, e.g. func(int, jlong, int, jlong)
 929   __ sub(SP, SP, AsmOperand(Rsize_of_params, lsl, LogBytesPerLong));
 930 
 931 #ifdef __ABI_HARD__
 932   // Allocate more stack space to accomodate all GP as well as FP registers:
 933   // 4 * wordSize
 934   // 8 * BytesPerLong
 935   int reg_arguments = align_up((4*wordSize) + (8*BytesPerLong), StackAlignmentInBytes);
 936 #else
 937   // Reserve at least 4 words on the stack for loading
 938   // of parameters passed on registers (R0-R3).
 939   // See generate_slow_signature_handler().
 940   // It is also used for JNIEnv & class additional parameters.
 941   int reg_arguments = 4 * wordSize;
 942 #endif // __ABI_HARD__
 943 
 944   __ sub(SP, SP, reg_arguments);
 945 
 946 
 947   // Note: signature handler blows R4 besides all scratch registers.
 948   // See AbstractInterpreterGenerator::generate_slow_signature_handler().
 949   __ call(Rsig_handler);
 950 #if R9_IS_SCRATCHED
 951   __ restore_method();
 952 #endif
 953   __ mov(Rresult_handler, R0);
 954 
 955   // Pass JNIEnv and mirror for static methods
 956   {
 957     Label L;
 958     __ ldr_u32(Rtemp, Address(Rmethod, Method::access_flags_offset()));
 959     __ add(R0, Rthread, in_bytes(JavaThread::jni_environment_offset()));
 960     __ tbz(Rtemp, JVM_ACC_STATIC_BIT, L);
 961     __ load_mirror(Rtemp, Rmethod, Rtemp);
 962     __ add(R1, FP, frame::interpreter_frame_oop_temp_offset * wordSize);
 963     __ str(Rtemp, Address(R1, 0));
 964     __ bind(L);
 965   }
 966 
 967   __ set_last_Java_frame(SP, FP, true, Rtemp);
 968 
 969   // Changing state to _thread_in_native must be the last thing to do
 970   // before the jump to native code. At this moment stack must be
 971   // safepoint-safe and completely prepared for stack walking.
 972 #ifdef ASSERT
 973   {
 974     Label L;
 975     __ ldr_u32(Rtemp, Address(Rthread, JavaThread::thread_state_offset()));
 976     __ cmp_32(Rtemp, _thread_in_Java);
 977     __ b(L, eq);
 978     __ stop("invalid thread state");
 979     __ bind(L);
 980   }
 981 #endif
 982 
 983   // Force all preceding writes to be observed prior to thread state change
 984   __ membar(MacroAssembler::StoreStore, Rtemp);
 985 
 986   __ mov(Rtemp, _thread_in_native);
 987   __ str(Rtemp, Address(Rthread, JavaThread::thread_state_offset()));
 988 
 989   __ call(Rnative_code);
 990 #if R9_IS_SCRATCHED
 991   __ restore_method();
 992 #endif
 993 
 994   // Set FPSCR/FPCR to a known state
 995   if (AlwaysRestoreFPU) {
 996     __ restore_default_fp_mode();
 997   }
 998 
 999   // Do safepoint check
1000   __ mov(Rtemp, _thread_in_native_trans);
1001   __ str_32(Rtemp, Address(Rthread, JavaThread::thread_state_offset()));
1002 
1003     // Force this write out before the read below
1004   __ membar(MacroAssembler::StoreLoad, Rtemp);
1005 
1006   // Protect the return value in the interleaved code: save it to callee-save registers.
1007   __ mov(Rsaved_result_lo, R0);
1008   __ mov(Rsaved_result_hi, R1);
1009 #ifdef __ABI_HARD__
1010   // preserve native FP result in a callee-saved register
1011   saved_result_fp = D8;
1012   __ fcpyd(saved_result_fp, D0);
1013 #else
1014   saved_result_fp = fnoreg;
1015 #endif // __ABI_HARD__
1016 
1017   {
1018   Label call, skip_call;
1019   __ safepoint_poll(Rtemp, call);
1020   __ ldr_u32(R3, Address(Rthread, JavaThread::suspend_flags_offset()));
1021   __ cmp(R3, 0);
1022   __ b(skip_call, eq);
1023   __ bind(call);
1024   __ mov(R0, Rthread);
1025   __ call(CAST_FROM_FN_PTR(address, JavaThread::check_special_condition_for_native_trans), relocInfo::none);
1026   __ bind(skip_call);
1027 
1028 #if R9_IS_SCRATCHED
1029   __ restore_method();
1030 #endif
1031   }
1032 
1033   // Perform Native->Java thread transition
1034   __ mov(Rtemp, _thread_in_Java);
1035   __ str_32(Rtemp, Address(Rthread, JavaThread::thread_state_offset()));
1036 
1037   // Zero handles and last_java_sp
1038   __ reset_last_Java_frame(Rtemp);
1039   __ ldr(R3, Address(Rthread, JavaThread::active_handles_offset()));
1040   __ str_32(__ zero_register(Rtemp), Address(R3, JNIHandleBlock::top_offset_in_bytes()));
1041   if (CheckJNICalls) {
1042     __ str(__ zero_register(Rtemp), Address(Rthread, JavaThread::pending_jni_exception_check_fn_offset()));
1043   }
1044 
1045   // Unbox oop result, e.g. JNIHandles::resolve result if it's an oop.
1046   {
1047     Label Lnot_oop;
1048     __ mov_slow(Rtemp, AbstractInterpreter::result_handler(T_OBJECT));
1049     __ cmp(Rtemp, Rresult_handler);
1050     __ b(Lnot_oop, ne);
1051     Register value = Rsaved_result_lo;
1052     __ resolve_jobject(value,   // value
1053                        Rtemp,   // tmp1
1054                        R1_tmp); // tmp2
1055     // Store resolved result in frame for GC visibility.
1056     __ str(value, Address(FP, frame::interpreter_frame_oop_temp_offset * wordSize));
1057     __ bind(Lnot_oop);
1058   }
1059 
1060 
1061   // reguard stack if StackOverflow exception happened while in native.
1062   {
1063     __ ldr_u32(Rtemp, Address(Rthread, JavaThread::stack_guard_state_offset()));
1064     __ cmp_32(Rtemp, StackOverflow::stack_guard_yellow_reserved_disabled);
1065   __ call(CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages), relocInfo::none, eq);
1066 #if R9_IS_SCRATCHED
1067   __ restore_method();
1068 #endif
1069   }
1070 
1071   // check pending exceptions
1072   {
1073     __ ldr(Rtemp, Address(Rthread, Thread::pending_exception_offset()));
1074     __ cmp(Rtemp, 0);
1075     __ mov(Rexception_pc, PC, ne);
1076     __ b(StubRoutines::forward_exception_entry(), ne);
1077   }
1078 
1079   if (synchronized) {
1080     // address of first monitor
1081     __ sub(R0, FP, - (frame::interpreter_frame_monitor_block_bottom_offset - frame::interpreter_frame_monitor_size()) * wordSize);
1082     __ unlock_object(R0);
1083   }
1084 
1085   // jvmti/dtrace support
1086   // Note: This must happen _after_ handling/throwing any exceptions since
1087   //       the exception handler code notifies the runtime of method exits
1088   //       too. If this happens before, method entry/exit notifications are
1089   //       not properly paired (was bug - gri 11/22/99).
1090   __ notify_method_exit(vtos, InterpreterMacroAssembler::NotifyJVMTI, true, Rsaved_result_lo, Rsaved_result_hi, saved_result_fp);
1091 
1092   // Restore the result. Oop result is restored from the stack by the
1093   // result handler.
1094   __ mov(R0, Rsaved_result_lo);
1095   __ mov(R1, Rsaved_result_hi);
1096 
1097 #ifdef __ABI_HARD__
1098   // reload native FP result
1099   __ fcpyd(D0, D8);
1100 #endif // __ABI_HARD__
1101 
1102   __ blx(Rresult_handler);
1103 
1104   // Restore FP/LR, sender_sp and return
1105   __ mov(Rtemp, FP);
1106   __ ldmia(FP, RegisterSet(FP) | RegisterSet(LR));
1107   __ ldr(SP, Address(Rtemp, frame::interpreter_frame_sender_sp_offset * wordSize));
1108 
1109   __ ret();
1110 
1111   if (inc_counter) {
1112     // Handle overflow of counter and compile method
1113     __ bind(invocation_counter_overflow);
1114     generate_counter_overflow(continue_after_compile);
1115   }
1116 
1117   return entry_point;
1118 }
1119 
1120 //
1121 // Generic interpreted method entry to (asm) interpreter
1122 //
1123 address TemplateInterpreterGenerator::generate_normal_entry(bool synchronized) {
1124   // determine code generation flags
1125   bool inc_counter  = UseCompiler || CountCompiledCalls || LogTouchedMethods;
1126 
1127   // Rmethod: Method*
1128   // Rthread: thread
1129   // Rsender_sp: sender sp (could differ from SP if we were called via c2i)
1130   // Rparams: pointer to the last parameter in the stack
1131 
1132   address entry_point = __ pc();
1133 
1134   const Register RconstMethod = R3;
1135 
1136 
1137   __ ldr(RconstMethod, Address(Rmethod, Method::const_offset()));
1138 
1139   __ ldrh(R2, Address(RconstMethod, ConstMethod::size_of_parameters_offset()));
1140   __ ldrh(R3, Address(RconstMethod, ConstMethod::size_of_locals_offset()));
1141 
1142   // setup Rlocals
1143   __ sub(Rlocals, Rparams, wordSize);
1144   __ add(Rlocals, Rlocals, AsmOperand(R2, lsl, Interpreter::logStackElementSize));
1145 
1146   __ sub(R3, R3, R2); // number of additional locals
1147 
1148 
1149   // see if we've got enough room on the stack for locals plus overhead.
1150   generate_stack_overflow_check();
1151 
1152   // allocate space for locals
1153   // explicitly initialize locals
1154 
1155   // Loop is unrolled 4 times
1156   Label loop;
1157   __ mov(R0, 0);
1158   __ bind(loop);
1159 
1160   // #1
1161   __ subs(R3, R3, 1);
1162   __ push(R0, ge);
1163 
1164   // #2
1165   __ subs(R3, R3, 1, ge);
1166   __ push(R0, ge);
1167 
1168   // #3
1169   __ subs(R3, R3, 1, ge);
1170   __ push(R0, ge);
1171 
1172   // #4
1173   __ subs(R3, R3, 1, ge);
1174   __ push(R0, ge);
1175 
1176   __ b(loop, gt);
1177 
1178   // initialize fixed part of activation frame
1179   generate_fixed_frame(false);
1180 
1181   __ restore_dispatch();
1182 
1183   // make sure method is not native & not abstract
1184 #ifdef ASSERT
1185   __ ldr_u32(Rtemp, Address(Rmethod, Method::access_flags_offset()));
1186   {
1187     Label L;
1188     __ tbz(Rtemp, JVM_ACC_NATIVE_BIT, L);
1189     __ stop("tried to execute native method as non-native");
1190     __ bind(L);
1191   }
1192   { Label L;
1193     __ tbz(Rtemp, JVM_ACC_ABSTRACT_BIT, L);
1194     __ stop("tried to execute abstract method in interpreter");
1195     __ bind(L);
1196   }
1197 #endif
1198 
1199   // increment invocation count & check for overflow
1200   Label invocation_counter_overflow;
1201   if (inc_counter) {
1202     if (synchronized) {
1203       // Avoid unlocking method's monitor in case of exception, as it has not
1204       // been locked yet.
1205       __ set_do_not_unlock_if_synchronized(true, Rtemp);
1206     }
1207     generate_counter_incr(&invocation_counter_overflow);
1208   }
1209   Label continue_after_compile;
1210   __ bind(continue_after_compile);
1211 
1212   if (inc_counter && synchronized) {
1213     __ set_do_not_unlock_if_synchronized(false, Rtemp);
1214   }
1215 #if R9_IS_SCRATCHED
1216   __ restore_method();
1217 #endif
1218 
1219   // check for synchronized methods
1220   // Must happen AFTER invocation_counter check and stack overflow check,
1221   // so method is not locked if overflows.
1222   //
1223   if (synchronized) {
1224     // Allocate monitor and lock method
1225     lock_method();
1226   } else {
1227     // no synchronization necessary
1228 #ifdef ASSERT
1229       { Label L;
1230         __ ldr_u32(Rtemp, Address(Rmethod, Method::access_flags_offset()));
1231         __ tbz(Rtemp, JVM_ACC_SYNCHRONIZED_BIT, L);
1232         __ stop("method needs synchronization");
1233         __ bind(L);
1234       }
1235 #endif
1236   }
1237 
1238   // start execution
1239 #ifdef ASSERT
1240   { Label L;
1241     __ ldr(Rtemp, Address(FP, frame::interpreter_frame_monitor_block_top_offset * wordSize));
1242     __ cmp(Rtemp, Rstack_top);
1243     __ b(L, eq);
1244     __ stop("broken stack frame setup in interpreter");
1245     __ bind(L);
1246   }
1247 #endif
1248   __ check_extended_sp(Rtemp);
1249 
1250   // jvmti support
1251   __ notify_method_entry();
1252 #if R9_IS_SCRATCHED
1253   __ restore_method();
1254 #endif
1255 
1256   __ dispatch_next(vtos);
1257 
1258   // invocation counter overflow
1259   if (inc_counter) {
1260     // Handle overflow of counter and compile method
1261     __ bind(invocation_counter_overflow);
1262     generate_counter_overflow(continue_after_compile);
1263   }
1264 
1265   return entry_point;
1266 }
1267 
1268 //------------------------------------------------------------------------------------------------------------------------
1269 // Exceptions
1270 
1271 void TemplateInterpreterGenerator::generate_throw_exception() {
1272   // Entry point in previous activation (i.e., if the caller was interpreted)
1273   Interpreter::_rethrow_exception_entry = __ pc();
1274   // Rexception_obj: exception
1275 
1276   // Clear interpreter_frame_last_sp.
1277   __ mov(Rtemp, 0);
1278   __ str(Rtemp, Address(FP, frame::interpreter_frame_last_sp_offset * wordSize));
1279 
1280 #if R9_IS_SCRATCHED
1281   __ restore_method();
1282 #endif
1283   __ restore_bcp();
1284   __ restore_dispatch();
1285   __ restore_locals();
1286 
1287 
1288   // Entry point for exceptions thrown within interpreter code
1289   Interpreter::_throw_exception_entry = __ pc();
1290 
1291   // expression stack is undefined here
1292   // Rexception_obj: exception
1293   // Rbcp: exception bcp
1294   __ verify_oop(Rexception_obj);
1295 
1296   // expression stack must be empty before entering the VM in case of an exception
1297   __ empty_expression_stack();
1298   // find exception handler address and preserve exception oop
1299   __ mov(R1, Rexception_obj);
1300   __ call_VM(Rexception_obj, CAST_FROM_FN_PTR(address, InterpreterRuntime::exception_handler_for_exception), R1);
1301   // R0: exception handler entry point
1302   // Rexception_obj: preserved exception oop
1303   // Rbcp: bcp for exception handler
1304   __ push_ptr(Rexception_obj);                    // push exception which is now the only value on the stack
1305   __ jump(R0);                                    // jump to exception handler (may be _remove_activation_entry!)
1306 
1307   // If the exception is not handled in the current frame the frame is removed and
1308   // the exception is rethrown (i.e. exception continuation is _rethrow_exception).
1309   //
1310   // Note: At this point the bci is still the bxi for the instruction which caused
1311   //       the exception and the expression stack is empty. Thus, for any VM calls
1312   //       at this point, GC will find a legal oop map (with empty expression stack).
1313 
1314   // In current activation
1315   // tos: exception
1316   // Rbcp: exception bcp
1317 
1318   //
1319   // JVMTI PopFrame support
1320   //
1321    Interpreter::_remove_activation_preserving_args_entry = __ pc();
1322 
1323 
1324   __ empty_expression_stack();
1325 
1326   // Set the popframe_processing bit in _popframe_condition indicating that we are
1327   // currently handling popframe, so that call_VMs that may happen later do not trigger new
1328   // popframe handling cycles.
1329 
1330   __ ldr_s32(Rtemp, Address(Rthread, JavaThread::popframe_condition_offset()));
1331   __ orr(Rtemp, Rtemp, (unsigned)JavaThread::popframe_processing_bit);
1332   __ str_32(Rtemp, Address(Rthread, JavaThread::popframe_condition_offset()));
1333 
1334   {
1335     // Check to see whether we are returning to a deoptimized frame.
1336     // (The PopFrame call ensures that the caller of the popped frame is
1337     // either interpreted or compiled and deoptimizes it if compiled.)
1338     // In this case, we can't call dispatch_next() after the frame is
1339     // popped, but instead must save the incoming arguments and restore
1340     // them after deoptimization has occurred.
1341     //
1342     // Note that we don't compare the return PC against the
1343     // deoptimization blob's unpack entry because of the presence of
1344     // adapter frames in C2.
1345     Label caller_not_deoptimized;
1346     __ ldr(R0, Address(FP, frame::return_addr_offset * wordSize));
1347     __ call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::interpreter_contains), R0);
1348     __ cbnz_32(R0, caller_not_deoptimized);
1349 
1350     // Compute size of arguments for saving when returning to deoptimized caller
1351     __ restore_method();
1352     __ ldr(R0, Address(Rmethod, Method::const_offset()));
1353     __ ldrh(R0, Address(R0, ConstMethod::size_of_parameters_offset()));
1354 
1355     __ logical_shift_left(R1, R0, Interpreter::logStackElementSize);
1356     // Save these arguments
1357     __ restore_locals();
1358     __ sub(R2, Rlocals, R1);
1359     __ add(R2, R2, wordSize);
1360     __ mov(R0, Rthread);
1361     __ call_VM_leaf(CAST_FROM_FN_PTR(address, Deoptimization::popframe_preserve_args), R0, R1, R2);
1362 
1363     __ remove_activation(vtos, LR,
1364                          /* throw_monitor_exception */ false,
1365                          /* install_monitor_exception */ false,
1366                          /* notify_jvmdi */ false);
1367 
1368     // Inform deoptimization that it is responsible for restoring these arguments
1369     __ mov(Rtemp, JavaThread::popframe_force_deopt_reexecution_bit);
1370     __ str_32(Rtemp, Address(Rthread, JavaThread::popframe_condition_offset()));
1371 
1372     // Continue in deoptimization handler
1373     __ ret();
1374 
1375     __ bind(caller_not_deoptimized);
1376   }
1377 
1378   __ remove_activation(vtos, R4,
1379                        /* throw_monitor_exception */ false,
1380                        /* install_monitor_exception */ false,
1381                        /* notify_jvmdi */ false);
1382 
1383   // Finish with popframe handling
1384   // A previous I2C followed by a deoptimization might have moved the
1385   // outgoing arguments further up the stack. PopFrame expects the
1386   // mutations to those outgoing arguments to be preserved and other
1387   // constraints basically require this frame to look exactly as
1388   // though it had previously invoked an interpreted activation with
1389   // no space between the top of the expression stack (current
1390   // last_sp) and the top of stack. Rather than force deopt to
1391   // maintain this kind of invariant all the time we call a small
1392   // fixup routine to move the mutated arguments onto the top of our
1393   // expression stack if necessary.
1394   __ mov(R1, SP);
1395   __ ldr(R2, Address(FP, frame::interpreter_frame_last_sp_offset * wordSize));
1396   // PC must point into interpreter here
1397   __ set_last_Java_frame(SP, FP, true, Rtemp);
1398   __ mov(R0, Rthread);
1399   __ call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::popframe_move_outgoing_args), R0, R1, R2);
1400   __ reset_last_Java_frame(Rtemp);
1401 
1402   // Restore the last_sp and null it out
1403   __ ldr(SP, Address(FP, frame::interpreter_frame_last_sp_offset * wordSize));
1404   __ mov(Rtemp, (int)NULL_WORD);
1405   __ str(Rtemp, Address(FP, frame::interpreter_frame_last_sp_offset * wordSize));
1406 
1407   __ restore_bcp();
1408   __ restore_dispatch();
1409   __ restore_locals();
1410   __ restore_method();
1411 
1412   // The method data pointer was incremented already during
1413   // call profiling. We have to restore the mdp for the current bcp.
1414   if (ProfileInterpreter) {
1415     __ set_method_data_pointer_for_bcp();
1416   }
1417 
1418   // Clear the popframe condition flag
1419   assert(JavaThread::popframe_inactive == 0, "adjust this code");
1420   __ str_32(__ zero_register(Rtemp), Address(Rthread, JavaThread::popframe_condition_offset()));
1421 
1422 #if INCLUDE_JVMTI
1423   {
1424     Label L_done;
1425 
1426     __ ldrb(Rtemp, Address(Rbcp, 0));
1427     __ cmp(Rtemp, Bytecodes::_invokestatic);
1428     __ b(L_done, ne);
1429 
1430     // The member name argument must be restored if _invokestatic is re-executed after a PopFrame call.
1431     // Detect such a case in the InterpreterRuntime function and return the member name argument, or NULL.
1432 
1433     // get local0
1434     __ ldr(R1, Address(Rlocals, 0));
1435     __ mov(R2, Rmethod);
1436     __ mov(R3, Rbcp);
1437     __ call_VM(R0, CAST_FROM_FN_PTR(address, InterpreterRuntime::member_name_arg_or_null), R1, R2, R3);
1438 
1439     __ cbz(R0, L_done);
1440 
1441     __ str(R0, Address(Rstack_top));
1442     __ bind(L_done);
1443   }
1444 #endif // INCLUDE_JVMTI
1445 
1446   __ dispatch_next(vtos);
1447   // end of PopFrame support
1448 
1449   Interpreter::_remove_activation_entry = __ pc();
1450 
1451   // preserve exception over this code sequence
1452   __ pop_ptr(R0_tos);
1453   __ str(R0_tos, Address(Rthread, JavaThread::vm_result_offset()));
1454   // remove the activation (without doing throws on illegalMonitorExceptions)
1455   __ remove_activation(vtos, Rexception_pc, false, true, false);
1456   // restore exception
1457   __ get_vm_result(Rexception_obj, Rtemp);
1458 
1459   // Inbetween activations - previous activation type unknown yet
1460   // compute continuation point - the continuation point expects
1461   // the following registers set up:
1462   //
1463   // Rexception_obj: exception
1464   // Rexception_pc: return address/pc that threw exception
1465   // SP: expression stack of caller
1466   // FP: frame pointer of caller
1467   __ mov(c_rarg0, Rthread);
1468   __ mov(c_rarg1, Rexception_pc);
1469   __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address), c_rarg0, c_rarg1);
1470   // Note that an "issuing PC" is actually the next PC after the call
1471 
1472   __ jump(R0);                             // jump to exception handler of caller
1473 }
1474 
1475 
1476 //
1477 // JVMTI ForceEarlyReturn support
1478 //
1479 address TemplateInterpreterGenerator::generate_earlyret_entry_for(TosState state) {
1480   address entry = __ pc();
1481 
1482 
1483   __ restore_bcp();
1484   __ restore_dispatch();
1485   __ restore_locals();
1486 
1487   __ empty_expression_stack();
1488 
1489   __ load_earlyret_value(state);
1490 
1491   // Clear the earlyret state
1492   __ ldr(Rtemp, Address(Rthread, JavaThread::jvmti_thread_state_offset()));
1493 
1494   assert(JvmtiThreadState::earlyret_inactive == 0, "adjust this code");
1495   __ str_32(__ zero_register(R2), Address(Rtemp, JvmtiThreadState::earlyret_state_offset()));
1496 
1497   __ remove_activation(state, LR,
1498                        false, /* throw_monitor_exception */
1499                        false, /* install_monitor_exception */
1500                        true); /* notify_jvmdi */
1501 
1502   // According to interpreter calling conventions, result is returned in R0/R1,
1503   // so ftos (S0) and dtos (D0) are moved to R0/R1.
1504   // This conversion should be done after remove_activation, as it uses
1505   // push(state) & pop(state) to preserve return value.
1506   __ convert_tos_to_retval(state);
1507   __ ret();
1508 
1509   return entry;
1510 } // end of ForceEarlyReturn support
1511 
1512 
1513 //------------------------------------------------------------------------------------------------------------------------
1514 // Helper for vtos entry point generation
1515 
1516 void TemplateInterpreterGenerator::set_vtos_entry_points (Template* t, address& bep, address& cep, address& sep, address& aep, address& iep, address& lep, address& fep, address& dep, address& vep) {
1517   assert(t->is_valid() && t->tos_in() == vtos, "illegal template");
1518   Label L;
1519 
1520 #ifdef __SOFTFP__
1521   dep = __ pc();                // fall through
1522 #else
1523   fep = __ pc(); __ push(ftos); __ b(L);
1524   dep = __ pc(); __ push(dtos); __ b(L);
1525 #endif // __SOFTFP__
1526 
1527   lep = __ pc(); __ push(ltos); __ b(L);
1528 
1529   if (VerifyOops) {  // can't share atos entry if VerifyOops
1530     aep = __ pc(); __ push(atos); __ b(L);
1531   } else {
1532     aep = __ pc();              // fall through
1533   }
1534 
1535 #ifdef __SOFTFP__
1536   fep = __ pc();                // fall through
1537 #endif // __SOFTFP__
1538 
1539   bep = cep = sep =             // fall through
1540   iep = __ pc(); __ push(itos); // fall through
1541   vep = __ pc(); __ bind(L);    // fall through
1542   generate_and_dispatch(t);
1543 }
1544 
1545 //------------------------------------------------------------------------------------------------------------------------
1546 
1547 // Non-product code
1548 #ifndef PRODUCT
1549 address TemplateInterpreterGenerator::generate_trace_code(TosState state) {
1550   address entry = __ pc();
1551 
1552   // prepare expression stack
1553   __ push(state);       // save tosca
1554 
1555   // pass tosca registers as arguments
1556   __ mov(R2, R0_tos);
1557   __ mov(R3, R1_tos_hi);
1558   __ mov(R1, LR);       // save return address
1559 
1560   // call tracer
1561   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::trace_bytecode), R1, R2, R3);
1562 
1563   __ mov(LR, R0);       // restore return address
1564   __ pop(state);        // restore tosca
1565 
1566   // return
1567   __ ret();
1568 
1569   return entry;
1570 }
1571 
1572 
1573 void TemplateInterpreterGenerator::count_bytecode() {
1574   __ inc_global_counter((address) &BytecodeCounter::_counter_value, 0, Rtemp, R2_tmp, true);
1575 }
1576 
1577 
1578 void TemplateInterpreterGenerator::histogram_bytecode(Template* t) {
1579   __ inc_global_counter((address)&BytecodeHistogram::_counters[0], sizeof(BytecodeHistogram::_counters[0]) * t->bytecode(), Rtemp, R2_tmp, true);
1580 }
1581 
1582 
1583 void TemplateInterpreterGenerator::histogram_bytecode_pair(Template* t) {
1584   const Register Rindex_addr = R2_tmp;
1585   Label Lcontinue;
1586   InlinedAddress Lcounters((address)BytecodePairHistogram::_counters);
1587   InlinedAddress Lindex((address)&BytecodePairHistogram::_index);
1588   const Register Rcounters_addr = R2_tmp;
1589   const Register Rindex = R4_tmp;
1590 
1591   // calculate new index for counter:
1592   // index = (_index >> log2_number_of_codes) | (bytecode << log2_number_of_codes).
1593   // (_index >> log2_number_of_codes) is previous bytecode
1594 
1595   __ ldr_literal(Rindex_addr, Lindex);
1596   __ ldr_s32(Rindex, Address(Rindex_addr));
1597   __ mov_slow(Rtemp, ((int)t->bytecode()) << BytecodePairHistogram::log2_number_of_codes);
1598   __ orr(Rindex, Rtemp, AsmOperand(Rindex, lsr, BytecodePairHistogram::log2_number_of_codes));
1599   __ str_32(Rindex, Address(Rindex_addr));
1600 
1601   // Rindex (R4) contains index of counter
1602 
1603   __ ldr_literal(Rcounters_addr, Lcounters);
1604   __ ldr_s32(Rtemp, Address::indexed_32(Rcounters_addr, Rindex));
1605   __ adds_32(Rtemp, Rtemp, 1);
1606   __ b(Lcontinue, mi);                           // avoid overflow
1607   __ str_32(Rtemp, Address::indexed_32(Rcounters_addr, Rindex));
1608 
1609   __ b(Lcontinue);
1610 
1611   __ bind_literal(Lindex);
1612   __ bind_literal(Lcounters);
1613 
1614   __ bind(Lcontinue);
1615 }
1616 
1617 
1618 void TemplateInterpreterGenerator::trace_bytecode(Template* t) {
1619   // Call a little run-time stub to avoid blow-up for each bytecode.
1620   // The run-time runtime saves the right registers, depending on
1621   // the tosca in-state for the given template.
1622   assert(Interpreter::trace_code(t->tos_in()) != NULL,
1623          "entry must have been generated");
1624   address trace_entry = Interpreter::trace_code(t->tos_in());
1625   __ call(trace_entry, relocInfo::none);
1626 }
1627 
1628 
1629 void TemplateInterpreterGenerator::stop_interpreter_at() {
1630   Label Lcontinue;
1631   const Register stop_at = R2_tmp;
1632 
1633   __ ldr_global_s32(Rtemp, (address) &BytecodeCounter::_counter_value);
1634   __ mov_slow(stop_at, StopInterpreterAt);
1635 
1636   // test bytecode counter
1637   __ cmp(Rtemp, stop_at);
1638   __ b(Lcontinue, ne);
1639 
1640   __ trace_state("stop_interpreter_at");
1641   __ breakpoint();
1642 
1643   __ bind(Lcontinue);
1644 }
1645 #endif // !PRODUCT