1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2013, 2014 SAP AG. 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 #ifndef CC_INTERP
  28 #include "asm/macroAssembler.inline.hpp"
  29 #include "interpreter/bytecodeHistogram.hpp"
  30 #include "interpreter/interpreter.hpp"
  31 #include "interpreter/interpreterGenerator.hpp"
  32 #include "interpreter/interpreterRuntime.hpp"
  33 #include "interpreter/interp_masm.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 "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 "utilities/macros.hpp"
  51 
  52 #undef __
  53 #define __ _masm->
  54 
  55 #ifdef PRODUCT
  56 #define BLOCK_COMMENT(str) /* nothing */
  57 #else
  58 #define BLOCK_COMMENT(str) __ block_comment(str)
  59 #endif
  60 
  61 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
  62 
  63 //-----------------------------------------------------------------------------
  64 
  65 // Actually we should never reach here since we do stack overflow checks before pushing any frame.
  66 address TemplateInterpreterGenerator::generate_StackOverflowError_handler() {
  67   address entry = __ pc();
  68   __ unimplemented("generate_StackOverflowError_handler");
  69   return entry;
  70 }
  71 
  72 address TemplateInterpreterGenerator::generate_ArrayIndexOutOfBounds_handler(const char* name) {
  73   address entry = __ pc();
  74   __ empty_expression_stack();
  75   __ load_const_optimized(R4_ARG2, (address) name);
  76   // Index is in R17_tos.
  77   __ mr(R5_ARG3, R17_tos);
  78   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_ArrayIndexOutOfBoundsException));
  79   return entry;
  80 }
  81 
  82 #if 0
  83 // Call special ClassCastException constructor taking object to cast
  84 // and target class as arguments.
  85 address TemplateInterpreterGenerator::generate_ClassCastException_verbose_handler() {
  86   address entry = __ pc();
  87 
  88   // Expression stack must be empty before entering the VM if an
  89   // exception happened.
  90   __ empty_expression_stack();
  91 
  92   // Thread will be loaded to R3_ARG1.
  93   // Target class oop is in register R5_ARG3 by convention!
  94   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_ClassCastException_verbose, R17_tos, R5_ARG3));
  95   // Above call must not return here since exception pending.
  96   DEBUG_ONLY(__ should_not_reach_here();)
  97   return entry;
  98 }
  99 #endif
 100 
 101 address TemplateInterpreterGenerator::generate_ClassCastException_handler() {
 102   address entry = __ pc();
 103   // Expression stack must be empty before entering the VM if an
 104   // exception happened.
 105   __ empty_expression_stack();
 106 
 107   // Load exception object.
 108   // Thread will be loaded to R3_ARG1.
 109   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_ClassCastException), R17_tos);
 110 #ifdef ASSERT
 111   // Above call must not return here since exception pending.
 112   __ should_not_reach_here();
 113 #endif
 114   return entry;
 115 }
 116 
 117 address TemplateInterpreterGenerator::generate_exception_handler_common(const char* name, const char* message, bool pass_oop) {
 118   address entry = __ pc();
 119   //__ untested("generate_exception_handler_common");
 120   Register Rexception = R17_tos;
 121 
 122   // Expression stack must be empty before entering the VM if an exception happened.
 123   __ empty_expression_stack();
 124 
 125   __ load_const_optimized(R4_ARG2, (address) name, R11_scratch1);
 126   if (pass_oop) {
 127     __ mr(R5_ARG3, Rexception);
 128     __ call_VM(Rexception, CAST_FROM_FN_PTR(address, InterpreterRuntime::create_klass_exception), false);
 129   } else {
 130     __ load_const_optimized(R5_ARG3, (address) message, R11_scratch1);
 131     __ call_VM(Rexception, CAST_FROM_FN_PTR(address, InterpreterRuntime::create_exception), false);
 132   }
 133 
 134   // Throw exception.
 135   __ mr(R3_ARG1, Rexception);
 136   __ load_const_optimized(R11_scratch1, Interpreter::throw_exception_entry(), R12_scratch2);
 137   __ mtctr(R11_scratch1);
 138   __ bctr();
 139 
 140   return entry;
 141 }
 142 
 143 address TemplateInterpreterGenerator::generate_continuation_for(TosState state) {
 144   address entry = __ pc();
 145   __ unimplemented("generate_continuation_for");
 146   return entry;
 147 }
 148 
 149 // This entry is returned to when a call returns to the interpreter.
 150 // When we arrive here, we expect that the callee stack frame is already popped.
 151 address TemplateInterpreterGenerator::generate_return_entry_for(TosState state, int step, size_t index_size) {
 152   address entry = __ pc();
 153 
 154   // Move the value out of the return register back to the TOS cache of current frame.
 155   switch (state) {
 156     case ltos:
 157     case btos:
 158     case ctos:
 159     case stos:
 160     case atos:
 161     case itos: __ mr(R17_tos, R3_RET); break;   // RET -> TOS cache
 162     case ftos:
 163     case dtos: __ fmr(F15_ftos, F1_RET); break; // TOS cache -> GR_FRET
 164     case vtos: break;                           // Nothing to do, this was a void return.
 165     default  : ShouldNotReachHere();
 166   }
 167 
 168   __ restore_interpreter_state(R11_scratch1); // Sets R11_scratch1 = fp.
 169   __ ld(R12_scratch2, _ijava_state_neg(top_frame_sp), R11_scratch1);
 170   __ resize_frame_absolute(R12_scratch2, R11_scratch1, R0);
 171 
 172   // Compiled code destroys templateTableBase, reload.
 173   __ load_const_optimized(R25_templateTableBase, (address)Interpreter::dispatch_table((TosState)0), R12_scratch2);
 174 
 175   const Register cache = R11_scratch1;
 176   const Register size  = R12_scratch2;
 177   __ get_cache_and_index_at_bcp(cache, 1, index_size);
 178 
 179   // Get least significant byte of 64 bit value:
 180 #if defined(VM_LITTLE_ENDIAN)
 181   __ lbz(size, in_bytes(ConstantPoolCache::base_offset() + ConstantPoolCacheEntry::flags_offset()), cache);
 182 #else
 183   __ lbz(size, in_bytes(ConstantPoolCache::base_offset() + ConstantPoolCacheEntry::flags_offset()) + 7, cache);
 184 #endif
 185   __ sldi(size, size, Interpreter::logStackElementSize);
 186   __ add(R15_esp, R15_esp, size);
 187   __ dispatch_next(state, step);
 188   return entry;
 189 }
 190 
 191 address TemplateInterpreterGenerator::generate_deopt_entry_for(TosState state, int step) {
 192   address entry = __ pc();
 193   // If state != vtos, we're returning from a native method, which put it's result
 194   // into the result register. So move the value out of the return register back
 195   // to the TOS cache of current frame.
 196 
 197   switch (state) {
 198     case ltos:
 199     case btos:
 200     case ctos:
 201     case stos:
 202     case atos:
 203     case itos: __ mr(R17_tos, R3_RET); break;   // GR_RET -> TOS cache
 204     case ftos:
 205     case dtos: __ fmr(F15_ftos, F1_RET); break; // TOS cache -> GR_FRET
 206     case vtos: break;                           // Nothing to do, this was a void return.
 207     default  : ShouldNotReachHere();
 208   }
 209 
 210   // Load LcpoolCache @@@ should be already set!
 211   __ get_constant_pool_cache(R27_constPoolCache);
 212 
 213   // Handle a pending exception, fall through if none.
 214   __ check_and_forward_exception(R11_scratch1, R12_scratch2);
 215 
 216   // Start executing bytecodes.
 217   __ dispatch_next(state, step);
 218 
 219   return entry;
 220 }
 221 
 222 // A result handler converts the native result into java format.
 223 // Use the shared code between c++ and template interpreter.
 224 address TemplateInterpreterGenerator::generate_result_handler_for(BasicType type) {
 225   return AbstractInterpreterGenerator::generate_result_handler_for(type);
 226 }
 227 
 228 address TemplateInterpreterGenerator::generate_safept_entry_for(TosState state, address runtime_entry) {
 229   address entry = __ pc();
 230 
 231   __ push(state);
 232   __ call_VM(noreg, runtime_entry);
 233   __ dispatch_via(vtos, Interpreter::_normal_table.table_for(vtos));
 234 
 235   return entry;
 236 }
 237 
 238 // Helpers for commoning out cases in the various type of method entries.
 239 
 240 // Increment invocation count & check for overflow.
 241 //
 242 // Note: checking for negative value instead of overflow
 243 //       so we have a 'sticky' overflow test.
 244 //
 245 void TemplateInterpreterGenerator::generate_counter_incr(Label* overflow, Label* profile_method, Label* profile_method_continue) {
 246   // Note: In tiered we increment either counters in method or in MDO depending if we're profiling or not.
 247   Register Rscratch1   = R11_scratch1;
 248   Register Rscratch2   = R12_scratch2;
 249   Register R3_counters = R3_ARG1;
 250   Label done;
 251 
 252   if (TieredCompilation) {
 253     const int increment = InvocationCounter::count_increment;
 254     const int mask = ((1 << Tier0InvokeNotifyFreqLog) - 1) << InvocationCounter::count_shift;
 255     Label no_mdo;
 256     if (ProfileInterpreter) {
 257       const Register Rmdo = Rscratch1;
 258       // If no method data exists, go to profile_continue.
 259       __ ld(Rmdo, in_bytes(Method::method_data_offset()), R19_method);
 260       __ cmpdi(CCR0, Rmdo, 0);
 261       __ beq(CCR0, no_mdo);
 262 
 263       // Increment backedge counter in the MDO.
 264       const int mdo_bc_offs = in_bytes(MethodData::backedge_counter_offset()) + in_bytes(InvocationCounter::counter_offset());
 265       __ lwz(Rscratch2, mdo_bc_offs, Rmdo);
 266       __ addi(Rscratch2, Rscratch2, increment);
 267       __ stw(Rscratch2, mdo_bc_offs, Rmdo);
 268       __ load_const_optimized(Rscratch1, mask, R0);
 269       __ and_(Rscratch1, Rscratch2, Rscratch1);
 270       __ bne(CCR0, done);
 271       __ b(*overflow);
 272     }
 273 
 274     // Increment counter in MethodCounters*.
 275     const int mo_bc_offs = in_bytes(MethodCounters::backedge_counter_offset()) + in_bytes(InvocationCounter::counter_offset());
 276     __ bind(no_mdo);
 277     __ get_method_counters(R19_method, R3_counters, done);
 278     __ lwz(Rscratch2, mo_bc_offs, R3_counters);
 279     __ addi(Rscratch2, Rscratch2, increment);
 280     __ stw(Rscratch2, mo_bc_offs, R3_counters);
 281     __ load_const_optimized(Rscratch1, mask, R0);
 282     __ and_(Rscratch1, Rscratch2, Rscratch1);
 283     __ beq(CCR0, *overflow);
 284 
 285     __ bind(done);
 286 
 287   } else {
 288 
 289     // Update standard invocation counters.
 290     Register Rsum_ivc_bec = R4_ARG2;
 291     __ get_method_counters(R19_method, R3_counters, done);
 292     __ increment_invocation_counter(R3_counters, Rsum_ivc_bec, R12_scratch2);
 293     // Increment interpreter invocation counter.
 294     if (ProfileInterpreter) {  // %%% Merge this into methodDataOop.
 295       __ lwz(R12_scratch2, in_bytes(MethodCounters::interpreter_invocation_counter_offset()), R3_counters);
 296       __ addi(R12_scratch2, R12_scratch2, 1);
 297       __ stw(R12_scratch2, in_bytes(MethodCounters::interpreter_invocation_counter_offset()), R3_counters);
 298     }
 299     // Check if we must create a method data obj.
 300     if (ProfileInterpreter && profile_method != NULL) {
 301       const Register profile_limit = Rscratch1;
 302       int pl_offs = __ load_const_optimized(profile_limit, &InvocationCounter::InterpreterProfileLimit, R0, true);
 303       __ lwz(profile_limit, pl_offs, profile_limit);
 304       // Test to see if we should create a method data oop.
 305       __ cmpw(CCR0, Rsum_ivc_bec, profile_limit);
 306       __ blt(CCR0, *profile_method_continue);
 307       // If no method data exists, go to profile_method.
 308       __ test_method_data_pointer(*profile_method);
 309     }
 310     // Finally check for counter overflow.
 311     if (overflow) {
 312       const Register invocation_limit = Rscratch1;
 313       int il_offs = __ load_const_optimized(invocation_limit, &InvocationCounter::InterpreterInvocationLimit, R0, true);
 314       __ lwz(invocation_limit, il_offs, invocation_limit);
 315       assert(4 == sizeof(InvocationCounter::InterpreterInvocationLimit), "unexpected field size");
 316       __ cmpw(CCR0, Rsum_ivc_bec, invocation_limit);
 317       __ bge(CCR0, *overflow);
 318     }
 319 
 320     __ bind(done);
 321   }
 322 }
 323 
 324 // Generate code to initiate compilation on invocation counter overflow.
 325 void TemplateInterpreterGenerator::generate_counter_overflow(Label& continue_entry) {
 326   // Generate code to initiate compilation on the counter overflow.
 327 
 328   // InterpreterRuntime::frequency_counter_overflow takes one arguments,
 329   // which indicates if the counter overflow occurs at a backwards branch (NULL bcp)
 330   // We pass zero in.
 331   // The call returns the address of the verified entry point for the method or NULL
 332   // if the compilation did not complete (either went background or bailed out).
 333   //
 334   // Unlike the C++ interpreter above: Check exceptions!
 335   // Assumption: Caller must set the flag "do_not_unlock_if_sychronized" if the monitor of a sync'ed
 336   // method has not yet been created. Thus, no unlocking of a non-existing monitor can occur.
 337 
 338   __ li(R4_ARG2, 0);
 339   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::frequency_counter_overflow), R4_ARG2, true);
 340 
 341   // Returns verified_entry_point or NULL.
 342   // We ignore it in any case.
 343   __ b(continue_entry);
 344 }
 345 
 346 void TemplateInterpreterGenerator::generate_stack_overflow_check(Register Rmem_frame_size, Register Rscratch1) {
 347   assert_different_registers(Rmem_frame_size, Rscratch1);
 348   __ generate_stack_overflow_check_with_compare_and_throw(Rmem_frame_size, Rscratch1);
 349 }
 350 
 351 void TemplateInterpreterGenerator::unlock_method(bool check_exceptions) {
 352   __ unlock_object(R26_monitor, check_exceptions);
 353 }
 354 
 355 // Lock the current method, interpreter register window must be set up!
 356 void TemplateInterpreterGenerator::lock_method(Register Rflags, Register Rscratch1, Register Rscratch2, bool flags_preloaded) {
 357   const Register Robj_to_lock = Rscratch2;
 358 
 359   {
 360     if (!flags_preloaded) {
 361       __ lwz(Rflags, method_(access_flags));
 362     }
 363 
 364 #ifdef ASSERT
 365     // Check if methods needs synchronization.
 366     {
 367       Label Lok;
 368       __ testbitdi(CCR0, R0, Rflags, JVM_ACC_SYNCHRONIZED_BIT);
 369       __ btrue(CCR0,Lok);
 370       __ stop("method doesn't need synchronization");
 371       __ bind(Lok);
 372     }
 373 #endif // ASSERT
 374   }
 375 
 376   // Get synchronization object to Rscratch2.
 377   {
 378     const int mirror_offset = in_bytes(Klass::java_mirror_offset());
 379     Label Lstatic;
 380     Label Ldone;
 381 
 382     __ testbitdi(CCR0, R0, Rflags, JVM_ACC_STATIC_BIT);
 383     __ btrue(CCR0, Lstatic);
 384 
 385     // Non-static case: load receiver obj from stack and we're done.
 386     __ ld(Robj_to_lock, R18_locals);
 387     __ b(Ldone);
 388 
 389     __ bind(Lstatic); // Static case: Lock the java mirror
 390     __ ld(Robj_to_lock, in_bytes(Method::const_offset()), R19_method);
 391     __ ld(Robj_to_lock, in_bytes(ConstMethod::constants_offset()), Robj_to_lock);
 392     __ ld(Robj_to_lock, ConstantPool::pool_holder_offset_in_bytes(), Robj_to_lock);
 393     __ ld(Robj_to_lock, mirror_offset, Robj_to_lock);
 394 
 395     __ bind(Ldone);
 396     __ verify_oop(Robj_to_lock);
 397   }
 398 
 399   // Got the oop to lock => execute!
 400   __ add_monitor_to_stack(true, Rscratch1, R0);
 401 
 402   __ std(Robj_to_lock, BasicObjectLock::obj_offset_in_bytes(), R26_monitor);
 403   __ lock_object(R26_monitor, Robj_to_lock);
 404 }
 405 
 406 // Generate a fixed interpreter frame for pure interpreter
 407 // and I2N native transition frames.
 408 //
 409 // Before (stack grows downwards):
 410 //
 411 //         |  ...         |
 412 //         |------------- |
 413 //         |  java arg0   |
 414 //         |  ...         |
 415 //         |  java argn   |
 416 //         |              |   <-   R15_esp
 417 //         |              |
 418 //         |--------------|
 419 //         | abi_112      |
 420 //         |              |   <-   R1_SP
 421 //         |==============|
 422 //
 423 //
 424 // After:
 425 //
 426 //         |  ...         |
 427 //         |  java arg0   |<-   R18_locals
 428 //         |  ...         |
 429 //         |  java argn   |
 430 //         |--------------|
 431 //         |              |
 432 //         |  java locals |
 433 //         |              |
 434 //         |--------------|
 435 //         |  abi_48      |
 436 //         |==============|
 437 //         |              |
 438 //         |   istate     |
 439 //         |              |
 440 //         |--------------|
 441 //         |   monitor    |<-   R26_monitor
 442 //         |--------------|
 443 //         |              |<-   R15_esp
 444 //         | expression   |
 445 //         | stack        |
 446 //         |              |
 447 //         |--------------|
 448 //         |              |
 449 //         | abi_112      |<-   R1_SP
 450 //         |==============|
 451 //
 452 // The top most frame needs an abi space of 112 bytes. This space is needed,
 453 // since we call to c. The c function may spill their arguments to the caller
 454 // frame. When we call to java, we don't need these spill slots. In order to save
 455 // space on the stack, we resize the caller. However, java local reside in
 456 // the caller frame and the frame has to be increased. The frame_size for the
 457 // current frame was calculated based on max_stack as size for the expression
 458 // stack. At the call, just a part of the expression stack might be used.
 459 // We don't want to waste this space and cut the frame back accordingly.
 460 // The resulting amount for resizing is calculated as follows:
 461 // resize =   (number_of_locals - number_of_arguments) * slot_size
 462 //          + (R1_SP - R15_esp) + 48
 463 //
 464 // The size for the callee frame is calculated:
 465 // framesize = 112 + max_stack + monitor + state_size
 466 //
 467 // maxstack:   Max number of slots on the expression stack, loaded from the method.
 468 // monitor:    We statically reserve room for one monitor object.
 469 // state_size: We save the current state of the interpreter to this area.
 470 //
 471 void TemplateInterpreterGenerator::generate_fixed_frame(bool native_call, Register Rsize_of_parameters, Register Rsize_of_locals) {
 472   Register parent_frame_resize = R6_ARG4, // Frame will grow by this number of bytes.
 473            top_frame_size      = R7_ARG5,
 474            Rconst_method       = R8_ARG6;
 475 
 476   assert_different_registers(Rsize_of_parameters, Rsize_of_locals, parent_frame_resize, top_frame_size);
 477 
 478   __ ld(Rconst_method, method_(const));
 479   __ lhz(Rsize_of_parameters /* number of params */,
 480          in_bytes(ConstMethod::size_of_parameters_offset()), Rconst_method);
 481   if (native_call) {
 482     // If we're calling a native method, we reserve space for the worst-case signature
 483     // handler varargs vector, which is max(Argument::n_register_parameters, parameter_count+2).
 484     // We add two slots to the parameter_count, one for the jni
 485     // environment and one for a possible native mirror.
 486     Label skip_native_calculate_max_stack;
 487     __ addi(top_frame_size, Rsize_of_parameters, 2);
 488     __ cmpwi(CCR0, top_frame_size, Argument::n_register_parameters);
 489     __ bge(CCR0, skip_native_calculate_max_stack);
 490     __ li(top_frame_size, Argument::n_register_parameters);
 491     __ bind(skip_native_calculate_max_stack);
 492     __ sldi(Rsize_of_parameters, Rsize_of_parameters, Interpreter::logStackElementSize);
 493     __ sldi(top_frame_size, top_frame_size, Interpreter::logStackElementSize);
 494     __ sub(parent_frame_resize, R1_SP, R15_esp); // <0, off by Interpreter::stackElementSize!
 495     assert(Rsize_of_locals == noreg, "Rsize_of_locals not initialized"); // Only relevant value is Rsize_of_parameters.
 496   } else {
 497     __ lhz(Rsize_of_locals /* number of params */, in_bytes(ConstMethod::size_of_locals_offset()), Rconst_method);
 498     __ sldi(Rsize_of_parameters, Rsize_of_parameters, Interpreter::logStackElementSize);
 499     __ sldi(Rsize_of_locals, Rsize_of_locals, Interpreter::logStackElementSize);
 500     __ lhz(top_frame_size, in_bytes(ConstMethod::max_stack_offset()), Rconst_method);
 501     __ sub(R11_scratch1, Rsize_of_locals, Rsize_of_parameters); // >=0
 502     __ sub(parent_frame_resize, R1_SP, R15_esp); // <0, off by Interpreter::stackElementSize!
 503     __ sldi(top_frame_size, top_frame_size, Interpreter::logStackElementSize);
 504     __ add(parent_frame_resize, parent_frame_resize, R11_scratch1);
 505   }
 506 
 507   // Compute top frame size.
 508   __ addi(top_frame_size, top_frame_size, frame::abi_reg_args_size + frame::ijava_state_size);
 509 
 510   // Cut back area between esp and max_stack.
 511   __ addi(parent_frame_resize, parent_frame_resize, frame::abi_minframe_size - Interpreter::stackElementSize);
 512 
 513   __ round_to(top_frame_size, frame::alignment_in_bytes);
 514   __ round_to(parent_frame_resize, frame::alignment_in_bytes);
 515   // parent_frame_resize = (locals-parameters) - (ESP-SP-ABI48) Rounded to frame alignment size.
 516   // Enlarge by locals-parameters (not in case of native_call), shrink by ESP-SP-ABI48.
 517 
 518   {
 519     // --------------------------------------------------------------------------
 520     // Stack overflow check
 521 
 522     Label cont;
 523     __ add(R11_scratch1, parent_frame_resize, top_frame_size);
 524     generate_stack_overflow_check(R11_scratch1, R12_scratch2);
 525   }
 526 
 527   // Set up interpreter state registers.
 528 
 529   __ add(R18_locals, R15_esp, Rsize_of_parameters);
 530   __ ld(R27_constPoolCache, in_bytes(ConstMethod::constants_offset()), Rconst_method);
 531   __ ld(R27_constPoolCache, ConstantPool::cache_offset_in_bytes(), R27_constPoolCache);
 532 
 533   // Set method data pointer.
 534   if (ProfileInterpreter) {
 535     Label zero_continue;
 536     __ ld(R28_mdx, method_(method_data));
 537     __ cmpdi(CCR0, R28_mdx, 0);
 538     __ beq(CCR0, zero_continue);
 539     __ addi(R28_mdx, R28_mdx, in_bytes(MethodData::data_offset()));
 540     __ bind(zero_continue);
 541   }
 542 
 543   if (native_call) {
 544     __ li(R14_bcp, 0); // Must initialize.
 545   } else {
 546     __ add(R14_bcp, in_bytes(ConstMethod::codes_offset()), Rconst_method);
 547   }
 548 
 549   // Resize parent frame.
 550   __ mflr(R12_scratch2);
 551   __ neg(parent_frame_resize, parent_frame_resize);
 552   __ resize_frame(parent_frame_resize, R11_scratch1);
 553   __ std(R12_scratch2, _abi(lr), R1_SP);
 554 
 555   __ addi(R26_monitor, R1_SP, - frame::ijava_state_size);
 556   __ addi(R15_esp, R26_monitor, - Interpreter::stackElementSize);
 557 
 558   // Store values.
 559   // R15_esp, R14_bcp, R26_monitor, R28_mdx are saved at java calls
 560   // in InterpreterMacroAssembler::call_from_interpreter.
 561   __ std(R19_method, _ijava_state_neg(method), R1_SP);
 562   __ std(R21_sender_SP, _ijava_state_neg(sender_sp), R1_SP);
 563   __ std(R27_constPoolCache, _ijava_state_neg(cpoolCache), R1_SP);
 564   __ std(R18_locals, _ijava_state_neg(locals), R1_SP);
 565 
 566   // Note: esp, bcp, monitor, mdx live in registers. Hence, the correct version can only
 567   // be found in the frame after save_interpreter_state is done. This is always true
 568   // for non-top frames. But when a signal occurs, dumping the top frame can go wrong,
 569   // because e.g. frame::interpreter_frame_bcp() will not access the correct value
 570   // (Enhanced Stack Trace).
 571   // The signal handler does not save the interpreter state into the frame.
 572   __ li(R0, 0);
 573 #ifdef ASSERT
 574   // Fill remaining slots with constants.
 575   __ load_const_optimized(R11_scratch1, 0x5afe);
 576   __ load_const_optimized(R12_scratch2, 0xdead);
 577 #endif
 578   // We have to initialize some frame slots for native calls (accessed by GC).
 579   if (native_call) {
 580     __ std(R26_monitor, _ijava_state_neg(monitors), R1_SP);
 581     __ std(R14_bcp, _ijava_state_neg(bcp), R1_SP);
 582     if (ProfileInterpreter) { __ std(R28_mdx, _ijava_state_neg(mdx), R1_SP); }
 583   }
 584 #ifdef ASSERT
 585   else {
 586     __ std(R12_scratch2, _ijava_state_neg(monitors), R1_SP);
 587     __ std(R12_scratch2, _ijava_state_neg(bcp), R1_SP);
 588     __ std(R12_scratch2, _ijava_state_neg(mdx), R1_SP);
 589   }
 590   __ std(R11_scratch1, _ijava_state_neg(ijava_reserved), R1_SP);
 591   __ std(R12_scratch2, _ijava_state_neg(esp), R1_SP);
 592   __ std(R12_scratch2, _ijava_state_neg(lresult), R1_SP);
 593   __ std(R12_scratch2, _ijava_state_neg(fresult), R1_SP);
 594 #endif
 595   __ subf(R12_scratch2, top_frame_size, R1_SP);
 596   __ std(R0, _ijava_state_neg(oop_tmp), R1_SP);
 597   __ std(R12_scratch2, _ijava_state_neg(top_frame_sp), R1_SP);
 598 
 599   // Push top frame.
 600   __ push_frame(top_frame_size, R11_scratch1);
 601 }
 602 
 603 // End of helpers
 604 
 605 
 606 // Support abs and sqrt like in compiler.
 607 // For others we can use a normal (native) entry.
 608 
 609 inline bool math_entry_available(AbstractInterpreter::MethodKind kind) {
 610   // Provide math entry with debugging on demand.
 611   // Note: Debugging changes which code will get executed:
 612   // Debugging or disabled InlineIntrinsics: java method will get interpreted and performs a native call.
 613   // Not debugging and enabled InlineIntrinics: processor instruction will get used.
 614   // Result might differ slightly due to rounding etc.
 615   if (!InlineIntrinsics && (!FLAG_IS_ERGO(InlineIntrinsics))) return false; // Generate a vanilla entry.
 616 
 617   return ((kind==Interpreter::java_lang_math_sqrt && VM_Version::has_fsqrt()) ||
 618           (kind==Interpreter::java_lang_math_abs));
 619 }
 620 
 621 address TemplateInterpreterGenerator::generate_math_entry(AbstractInterpreter::MethodKind kind) {
 622   if (!math_entry_available(kind)) {
 623     NOT_PRODUCT(__ should_not_reach_here();)
 624     return Interpreter::entry_for_kind(Interpreter::zerolocals);
 625   }
 626 
 627   Label Lslow_path;
 628   const Register Rjvmti_mode = R11_scratch1;
 629   address entry = __ pc();
 630 
 631   // Provide math entry with debugging on demand.
 632   __ lwz(Rjvmti_mode, thread_(interp_only_mode));
 633   __ cmpwi(CCR0, Rjvmti_mode, 0);
 634   __ bne(CCR0, Lslow_path); // jvmti_mode!=0
 635 
 636   __ lfd(F1_RET, Interpreter::stackElementSize, R15_esp);
 637 
 638   // Pop c2i arguments (if any) off when we return.
 639 #ifdef ASSERT
 640   __ ld(R9_ARG7, 0, R1_SP);
 641   __ ld(R10_ARG8, 0, R21_sender_SP);
 642   __ cmpd(CCR0, R9_ARG7, R10_ARG8);
 643   __ asm_assert_eq("backlink", 0x545);
 644 #endif // ASSERT
 645   __ mr(R1_SP, R21_sender_SP); // Cut the stack back to where the caller started.
 646 
 647   if (kind == Interpreter::java_lang_math_sqrt) {
 648     __ fsqrt(F1_RET, F1_RET);
 649   } else if (kind == Interpreter::java_lang_math_abs) {
 650     __ fabs(F1_RET, F1_RET);
 651   } else {
 652     ShouldNotReachHere();
 653   }
 654 
 655   // And we're done.
 656   __ blr();
 657 
 658   // Provide slow path for JVMTI case.
 659   __ bind(Lslow_path);
 660   __ branch_to_entry(Interpreter::entry_for_kind(Interpreter::zerolocals), R12_scratch2);
 661   __ flush();
 662 
 663   return entry;
 664 }
 665 
 666 // Interpreter stub for calling a native method. (asm interpreter)
 667 // This sets up a somewhat different looking stack for calling the
 668 // native method than the typical interpreter frame setup.
 669 //
 670 // On entry:
 671 //   R19_method    - method
 672 //   R16_thread    - JavaThread*
 673 //   R15_esp       - intptr_t* sender tos
 674 //
 675 //   abstract stack (grows up)
 676 //     [  IJava (caller of JNI callee)  ]  <-- ASP
 677 //        ...
 678 address TemplateInterpreterGenerator::generate_native_entry(bool synchronized) {
 679 
 680   address entry = __ pc();
 681 
 682   const bool inc_counter = UseCompiler || CountCompiledCalls;
 683 
 684   // -----------------------------------------------------------------------------
 685   // Allocate a new frame that represents the native callee (i2n frame).
 686   // This is not a full-blown interpreter frame, but in particular, the
 687   // following registers are valid after this:
 688   // - R19_method
 689   // - R18_local (points to start of argumuments to native function)
 690   //
 691   //   abstract stack (grows up)
 692   //     [  IJava (caller of JNI callee)  ]  <-- ASP
 693   //        ...
 694 
 695   const Register signature_handler_fd = R11_scratch1;
 696   const Register pending_exception    = R0;
 697   const Register result_handler_addr  = R31;
 698   const Register native_method_fd     = R11_scratch1;
 699   const Register access_flags         = R22_tmp2;
 700   const Register active_handles       = R11_scratch1; // R26_monitor saved to state.
 701   const Register sync_state           = R12_scratch2;
 702   const Register sync_state_addr      = sync_state;   // Address is dead after use.
 703   const Register suspend_flags        = R11_scratch1;
 704 
 705   //=============================================================================
 706   // Allocate new frame and initialize interpreter state.
 707 
 708   Label exception_return;
 709   Label exception_return_sync_check;
 710   Label stack_overflow_return;
 711 
 712   // Generate new interpreter state and jump to stack_overflow_return in case of
 713   // a stack overflow.
 714   //generate_compute_interpreter_state(stack_overflow_return);
 715 
 716   Register size_of_parameters = R22_tmp2;
 717 
 718   generate_fixed_frame(true, size_of_parameters, noreg /* unused */);
 719 
 720   //=============================================================================
 721   // Increment invocation counter. On overflow, entry to JNI method
 722   // will be compiled.
 723   Label invocation_counter_overflow, continue_after_compile;
 724   if (inc_counter) {
 725     if (synchronized) {
 726       // Since at this point in the method invocation the exception handler
 727       // would try to exit the monitor of synchronized methods which hasn't
 728       // been entered yet, we set the thread local variable
 729       // _do_not_unlock_if_synchronized to true. If any exception was thrown by
 730       // runtime, exception handling i.e. unlock_if_synchronized_method will
 731       // check this thread local flag.
 732       // This flag has two effects, one is to force an unwind in the topmost
 733       // interpreter frame and not perform an unlock while doing so.
 734       __ li(R0, 1);
 735       __ stb(R0, in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()), R16_thread);
 736     }
 737     generate_counter_incr(&invocation_counter_overflow, NULL, NULL);
 738 
 739     __ BIND(continue_after_compile);
 740     // Reset the _do_not_unlock_if_synchronized flag.
 741     if (synchronized) {
 742       __ li(R0, 0);
 743       __ stb(R0, in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()), R16_thread);
 744     }
 745   }
 746 
 747   // access_flags = method->access_flags();
 748   // Load access flags.
 749   assert(access_flags->is_nonvolatile(),
 750          "access_flags must be in a non-volatile register");
 751   // Type check.
 752   assert(4 == sizeof(AccessFlags), "unexpected field size");
 753   __ lwz(access_flags, method_(access_flags));
 754 
 755   // We don't want to reload R19_method and access_flags after calls
 756   // to some helper functions.
 757   assert(R19_method->is_nonvolatile(),
 758          "R19_method must be a non-volatile register");
 759 
 760   // Check for synchronized methods. Must happen AFTER invocation counter
 761   // check, so method is not locked if counter overflows.
 762 
 763   if (synchronized) {
 764     lock_method(access_flags, R11_scratch1, R12_scratch2, true);
 765 
 766     // Update monitor in state.
 767     __ ld(R11_scratch1, 0, R1_SP);
 768     __ std(R26_monitor, _ijava_state_neg(monitors), R11_scratch1);
 769   }
 770 
 771   // jvmti/jvmpi support
 772   __ notify_method_entry();
 773 
 774   //=============================================================================
 775   // Get and call the signature handler.
 776 
 777   __ ld(signature_handler_fd, method_(signature_handler));
 778   Label call_signature_handler;
 779 
 780   __ cmpdi(CCR0, signature_handler_fd, 0);
 781   __ bne(CCR0, call_signature_handler);
 782 
 783   // Method has never been called. Either generate a specialized
 784   // handler or point to the slow one.
 785   //
 786   // Pass parameter 'false' to avoid exception check in call_VM.
 787   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::prepare_native_call), R19_method, false);
 788 
 789   // Check for an exception while looking up the target method. If we
 790   // incurred one, bail.
 791   __ ld(pending_exception, thread_(pending_exception));
 792   __ cmpdi(CCR0, pending_exception, 0);
 793   __ bne(CCR0, exception_return_sync_check); // Has pending exception.
 794 
 795   // Reload signature handler, it may have been created/assigned in the meanwhile.
 796   __ ld(signature_handler_fd, method_(signature_handler));
 797   __ twi_0(signature_handler_fd); // Order wrt. load of klass mirror and entry point (isync is below).
 798 
 799   __ BIND(call_signature_handler);
 800 
 801   // Before we call the signature handler we push a new frame to
 802   // protect the interpreter frame volatile registers when we return
 803   // from jni but before we can get back to Java.
 804 
 805   // First set the frame anchor while the SP/FP registers are
 806   // convenient and the slow signature handler can use this same frame
 807   // anchor.
 808 
 809   // We have a TOP_IJAVA_FRAME here, which belongs to us.
 810   __ set_top_ijava_frame_at_SP_as_last_Java_frame(R1_SP, R12_scratch2/*tmp*/);
 811 
 812   // Now the interpreter frame (and its call chain) have been
 813   // invalidated and flushed. We are now protected against eager
 814   // being enabled in native code. Even if it goes eager the
 815   // registers will be reloaded as clean and we will invalidate after
 816   // the call so no spurious flush should be possible.
 817 
 818   // Call signature handler and pass locals address.
 819   //
 820   // Our signature handlers copy required arguments to the C stack
 821   // (outgoing C args), R3_ARG1 to R10_ARG8, and FARG1 to FARG13.
 822   __ mr(R3_ARG1, R18_locals);
 823 #if !defined(ABI_ELFv2)
 824   __ ld(signature_handler_fd, 0, signature_handler_fd);
 825 #endif
 826 
 827   __ call_stub(signature_handler_fd);
 828 
 829   // Remove the register parameter varargs slots we allocated in
 830   // compute_interpreter_state. SP+16 ends up pointing to the ABI
 831   // outgoing argument area.
 832   //
 833   // Not needed on PPC64.
 834   //__ add(SP, SP, Argument::n_register_parameters*BytesPerWord);
 835 
 836   assert(result_handler_addr->is_nonvolatile(), "result_handler_addr must be in a non-volatile register");
 837   // Save across call to native method.
 838   __ mr(result_handler_addr, R3_RET);
 839 
 840   __ isync(); // Acquire signature handler before trying to fetch the native entry point and klass mirror.
 841 
 842   // Set up fixed parameters and call the native method.
 843   // If the method is static, get mirror into R4_ARG2.
 844   {
 845     Label method_is_not_static;
 846     // Access_flags is non-volatile and still, no need to restore it.
 847 
 848     // Restore access flags.
 849     __ testbitdi(CCR0, R0, access_flags, JVM_ACC_STATIC_BIT);
 850     __ bfalse(CCR0, method_is_not_static);
 851 
 852     // constants = method->constants();
 853     __ ld(R11_scratch1, in_bytes(Method::const_offset()), R19_method);
 854     __ ld(R11_scratch1, in_bytes(ConstMethod::constants_offset()), R11_scratch1);
 855     // pool_holder = method->constants()->pool_holder();
 856     __ ld(R11_scratch1/*pool_holder*/, ConstantPool::pool_holder_offset_in_bytes(),
 857           R11_scratch1/*constants*/);
 858 
 859     const int mirror_offset = in_bytes(Klass::java_mirror_offset());
 860 
 861     // mirror = pool_holder->klass_part()->java_mirror();
 862     __ ld(R0/*mirror*/, mirror_offset, R11_scratch1/*pool_holder*/);
 863     // state->_native_mirror = mirror;
 864 
 865     __ ld(R11_scratch1, 0, R1_SP);
 866     __ std(R0/*mirror*/, _ijava_state_neg(oop_tmp), R11_scratch1);
 867     // R4_ARG2 = &state->_oop_temp;
 868     __ addi(R4_ARG2, R11_scratch1, _ijava_state_neg(oop_tmp));
 869     __ BIND(method_is_not_static);
 870   }
 871 
 872   // At this point, arguments have been copied off the stack into
 873   // their JNI positions. Oops are boxed in-place on the stack, with
 874   // handles copied to arguments. The result handler address is in a
 875   // register.
 876 
 877   // Pass JNIEnv address as first parameter.
 878   __ addir(R3_ARG1, thread_(jni_environment));
 879 
 880   // Load the native_method entry before we change the thread state.
 881   __ ld(native_method_fd, method_(native_function));
 882 
 883   //=============================================================================
 884   // Transition from _thread_in_Java to _thread_in_native. As soon as
 885   // we make this change the safepoint code needs to be certain that
 886   // the last Java frame we established is good. The pc in that frame
 887   // just needs to be near here not an actual return address.
 888 
 889   // We use release_store_fence to update values like the thread state, where
 890   // we don't want the current thread to continue until all our prior memory
 891   // accesses (including the new thread state) are visible to other threads.
 892   __ li(R0, _thread_in_native);
 893   __ release();
 894 
 895   // TODO PPC port assert(4 == JavaThread::sz_thread_state(), "unexpected field size");
 896   __ stw(R0, thread_(thread_state));
 897 
 898   if (UseMembar) {
 899     __ fence();
 900   }
 901 
 902   //=============================================================================
 903   // Call the native method. Argument registers must not have been
 904   // overwritten since "__ call_stub(signature_handler);" (except for
 905   // ARG1 and ARG2 for static methods).
 906   __ call_c(native_method_fd);
 907 
 908   __ li(R0, 0);
 909   __ ld(R11_scratch1, 0, R1_SP);
 910   __ std(R3_RET, _ijava_state_neg(lresult), R11_scratch1);
 911   __ stfd(F1_RET, _ijava_state_neg(fresult), R11_scratch1);
 912   __ std(R0/*mirror*/, _ijava_state_neg(oop_tmp), R11_scratch1); // reset
 913 
 914   // Note: C++ interpreter needs the following here:
 915   // The frame_manager_lr field, which we use for setting the last
 916   // java frame, gets overwritten by the signature handler. Restore
 917   // it now.
 918   //__ get_PC_trash_LR(R11_scratch1);
 919   //__ std(R11_scratch1, _top_ijava_frame_abi(frame_manager_lr), R1_SP);
 920 
 921   // Because of GC R19_method may no longer be valid.
 922 
 923   // Block, if necessary, before resuming in _thread_in_Java state.
 924   // In order for GC to work, don't clear the last_Java_sp until after
 925   // blocking.
 926 
 927   //=============================================================================
 928   // Switch thread to "native transition" state before reading the
 929   // synchronization state. This additional state is necessary
 930   // because reading and testing the synchronization state is not
 931   // atomic w.r.t. GC, as this scenario demonstrates: Java thread A,
 932   // in _thread_in_native state, loads _not_synchronized and is
 933   // preempted. VM thread changes sync state to synchronizing and
 934   // suspends threads for GC. Thread A is resumed to finish this
 935   // native method, but doesn't block here since it didn't see any
 936   // synchronization in progress, and escapes.
 937 
 938   // We use release_store_fence to update values like the thread state, where
 939   // we don't want the current thread to continue until all our prior memory
 940   // accesses (including the new thread state) are visible to other threads.
 941   __ li(R0/*thread_state*/, _thread_in_native_trans);
 942   __ release();
 943   __ stw(R0/*thread_state*/, thread_(thread_state));
 944   if (UseMembar) {
 945     __ fence();
 946   }
 947   // Write serialization page so that the VM thread can do a pseudo remote
 948   // membar. We use the current thread pointer to calculate a thread
 949   // specific offset to write to within the page. This minimizes bus
 950   // traffic due to cache line collision.
 951   else {
 952     __ serialize_memory(R16_thread, R11_scratch1, R12_scratch2);
 953   }
 954 
 955   // Now before we return to java we must look for a current safepoint
 956   // (a new safepoint can not start since we entered native_trans).
 957   // We must check here because a current safepoint could be modifying
 958   // the callers registers right this moment.
 959 
 960   // Acquire isn't strictly necessary here because of the fence, but
 961   // sync_state is declared to be volatile, so we do it anyway
 962   // (cmp-br-isync on one path, release (same as acquire on PPC64) on the other path).
 963   int sync_state_offs = __ load_const_optimized(sync_state_addr, SafepointSynchronize::address_of_state(), /*temp*/R0, true);
 964 
 965   // TODO PPC port assert(4 == SafepointSynchronize::sz_state(), "unexpected field size");
 966   __ lwz(sync_state, sync_state_offs, sync_state_addr);
 967 
 968   // TODO PPC port assert(4 == Thread::sz_suspend_flags(), "unexpected field size");
 969   __ lwz(suspend_flags, thread_(suspend_flags));
 970 
 971   Label sync_check_done;
 972   Label do_safepoint;
 973   // No synchronization in progress nor yet synchronized.
 974   __ cmpwi(CCR0, sync_state, SafepointSynchronize::_not_synchronized);
 975   // Not suspended.
 976   __ cmpwi(CCR1, suspend_flags, 0);
 977 
 978   __ bne(CCR0, do_safepoint);
 979   __ beq(CCR1, sync_check_done);
 980   __ bind(do_safepoint);
 981   __ isync();
 982   // Block. We do the call directly and leave the current
 983   // last_Java_frame setup undisturbed. We must save any possible
 984   // native result across the call. No oop is present.
 985 
 986   __ mr(R3_ARG1, R16_thread);
 987 #if defined(ABI_ELFv2)
 988   __ call_c(CAST_FROM_FN_PTR(address, JavaThread::check_special_condition_for_native_trans),
 989             relocInfo::none);
 990 #else
 991   __ call_c(CAST_FROM_FN_PTR(FunctionDescriptor*, JavaThread::check_special_condition_for_native_trans),
 992             relocInfo::none);
 993 #endif
 994 
 995   __ bind(sync_check_done);
 996 
 997   //=============================================================================
 998   // <<<<<< Back in Interpreter Frame >>>>>
 999 
1000   // We are in thread_in_native_trans here and back in the normal
1001   // interpreter frame. We don't have to do anything special about
1002   // safepoints and we can switch to Java mode anytime we are ready.
1003 
1004   // Note: frame::interpreter_frame_result has a dependency on how the
1005   // method result is saved across the call to post_method_exit. For
1006   // native methods it assumes that the non-FPU/non-void result is
1007   // saved in _native_lresult and a FPU result in _native_fresult. If
1008   // this changes then the interpreter_frame_result implementation
1009   // will need to be updated too.
1010 
1011   // On PPC64, we have stored the result directly after the native call.
1012 
1013   //=============================================================================
1014   // Back in Java
1015 
1016   // We use release_store_fence to update values like the thread state, where
1017   // we don't want the current thread to continue until all our prior memory
1018   // accesses (including the new thread state) are visible to other threads.
1019   __ li(R0/*thread_state*/, _thread_in_Java);
1020   __ release();
1021   __ stw(R0/*thread_state*/, thread_(thread_state));
1022   if (UseMembar) {
1023     __ fence();
1024   }
1025 
1026   __ reset_last_Java_frame();
1027 
1028   // Jvmdi/jvmpi support. Whether we've got an exception pending or
1029   // not, and whether unlocking throws an exception or not, we notify
1030   // on native method exit. If we do have an exception, we'll end up
1031   // in the caller's context to handle it, so if we don't do the
1032   // notify here, we'll drop it on the floor.
1033   __ notify_method_exit(true/*native method*/,
1034                         ilgl /*illegal state (not used for native methods)*/,
1035                         InterpreterMacroAssembler::NotifyJVMTI,
1036                         false /*check_exceptions*/);
1037 
1038   //=============================================================================
1039   // Handle exceptions
1040 
1041   if (synchronized) {
1042     // Don't check for exceptions since we're still in the i2n frame. Do that
1043     // manually afterwards.
1044     unlock_method(false);
1045   }
1046 
1047   // Reset active handles after returning from native.
1048   // thread->active_handles()->clear();
1049   __ ld(active_handles, thread_(active_handles));
1050   // TODO PPC port assert(4 == JNIHandleBlock::top_size_in_bytes(), "unexpected field size");
1051   __ li(R0, 0);
1052   __ stw(R0, JNIHandleBlock::top_offset_in_bytes(), active_handles);
1053 
1054   Label exception_return_sync_check_already_unlocked;
1055   __ ld(R0/*pending_exception*/, thread_(pending_exception));
1056   __ cmpdi(CCR0, R0/*pending_exception*/, 0);
1057   __ bne(CCR0, exception_return_sync_check_already_unlocked);
1058 
1059   //-----------------------------------------------------------------------------
1060   // No exception pending.
1061 
1062   // Move native method result back into proper registers and return.
1063   // Invoke result handler (may unbox/promote).
1064   __ ld(R11_scratch1, 0, R1_SP);
1065   __ ld(R3_RET, _ijava_state_neg(lresult), R11_scratch1);
1066   __ lfd(F1_RET, _ijava_state_neg(fresult), R11_scratch1);
1067   __ call_stub(result_handler_addr);
1068 
1069   __ merge_frames(/*top_frame_sp*/ R21_sender_SP, /*return_pc*/ R0, R11_scratch1, R12_scratch2);
1070 
1071   // Must use the return pc which was loaded from the caller's frame
1072   // as the VM uses return-pc-patching for deoptimization.
1073   __ mtlr(R0);
1074   __ blr();
1075 
1076   //-----------------------------------------------------------------------------
1077   // An exception is pending. We call into the runtime only if the
1078   // caller was not interpreted. If it was interpreted the
1079   // interpreter will do the correct thing. If it isn't interpreted
1080   // (call stub/compiled code) we will change our return and continue.
1081 
1082   __ BIND(exception_return_sync_check);
1083 
1084   if (synchronized) {
1085     // Don't check for exceptions since we're still in the i2n frame. Do that
1086     // manually afterwards.
1087     unlock_method(false);
1088   }
1089   __ BIND(exception_return_sync_check_already_unlocked);
1090 
1091   const Register return_pc = R31;
1092 
1093   __ ld(return_pc, 0, R1_SP);
1094   __ ld(return_pc, _abi(lr), return_pc);
1095 
1096   // Get the address of the exception handler.
1097   __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address),
1098                   R16_thread,
1099                   return_pc /* return pc */);
1100   __ merge_frames(/*top_frame_sp*/ R21_sender_SP, noreg, R11_scratch1, R12_scratch2);
1101 
1102   // Load the PC of the the exception handler into LR.
1103   __ mtlr(R3_RET);
1104 
1105   // Load exception into R3_ARG1 and clear pending exception in thread.
1106   __ ld(R3_ARG1/*exception*/, thread_(pending_exception));
1107   __ li(R4_ARG2, 0);
1108   __ std(R4_ARG2, thread_(pending_exception));
1109 
1110   // Load the original return pc into R4_ARG2.
1111   __ mr(R4_ARG2/*issuing_pc*/, return_pc);
1112 
1113   // Return to exception handler.
1114   __ blr();
1115 
1116   //=============================================================================
1117   // Counter overflow.
1118 
1119   if (inc_counter) {
1120     // Handle invocation counter overflow.
1121     __ bind(invocation_counter_overflow);
1122 
1123     generate_counter_overflow(continue_after_compile);
1124   }
1125 
1126   return entry;
1127 }
1128 
1129 // Generic interpreted method entry to (asm) interpreter.
1130 //
1131 address TemplateInterpreterGenerator::generate_normal_entry(bool synchronized) {
1132   bool inc_counter = UseCompiler || CountCompiledCalls;
1133   address entry = __ pc();
1134   // Generate the code to allocate the interpreter stack frame.
1135   Register Rsize_of_parameters = R4_ARG2, // Written by generate_fixed_frame.
1136            Rsize_of_locals     = R5_ARG3; // Written by generate_fixed_frame.
1137 
1138   generate_fixed_frame(false, Rsize_of_parameters, Rsize_of_locals);
1139 
1140 #ifdef FAST_DISPATCH
1141   __ unimplemented("Fast dispatch in generate_normal_entry");
1142 #if 0
1143   __ set((intptr_t)Interpreter::dispatch_table(), IdispatchTables);
1144   // Set bytecode dispatch table base.
1145 #endif
1146 #endif
1147 
1148   // --------------------------------------------------------------------------
1149   // Zero out non-parameter locals.
1150   // Note: *Always* zero out non-parameter locals as Sparc does. It's not
1151   // worth to ask the flag, just do it.
1152   Register Rslot_addr = R6_ARG4,
1153            Rnum       = R7_ARG5;
1154   Label Lno_locals, Lzero_loop;
1155 
1156   // Set up the zeroing loop.
1157   __ subf(Rnum, Rsize_of_parameters, Rsize_of_locals);
1158   __ subf(Rslot_addr, Rsize_of_parameters, R18_locals);
1159   __ srdi_(Rnum, Rnum, Interpreter::logStackElementSize);
1160   __ beq(CCR0, Lno_locals);
1161   __ li(R0, 0);
1162   __ mtctr(Rnum);
1163 
1164   // The zero locals loop.
1165   __ bind(Lzero_loop);
1166   __ std(R0, 0, Rslot_addr);
1167   __ addi(Rslot_addr, Rslot_addr, -Interpreter::stackElementSize);
1168   __ bdnz(Lzero_loop);
1169 
1170   __ bind(Lno_locals);
1171 
1172   // --------------------------------------------------------------------------
1173   // Counter increment and overflow check.
1174   Label invocation_counter_overflow,
1175         profile_method,
1176         profile_method_continue;
1177   if (inc_counter || ProfileInterpreter) {
1178 
1179     Register Rdo_not_unlock_if_synchronized_addr = R11_scratch1;
1180     if (synchronized) {
1181       // Since at this point in the method invocation the exception handler
1182       // would try to exit the monitor of synchronized methods which hasn't
1183       // been entered yet, we set the thread local variable
1184       // _do_not_unlock_if_synchronized to true. If any exception was thrown by
1185       // runtime, exception handling i.e. unlock_if_synchronized_method will
1186       // check this thread local flag.
1187       // This flag has two effects, one is to force an unwind in the topmost
1188       // interpreter frame and not perform an unlock while doing so.
1189       __ li(R0, 1);
1190       __ stb(R0, in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()), R16_thread);
1191     }
1192     // Increment invocation counter and check for overflow.
1193     if (inc_counter) {
1194       generate_counter_incr(&invocation_counter_overflow, &profile_method, &profile_method_continue);
1195     }
1196 
1197     __ bind(profile_method_continue);
1198 
1199     // Reset the _do_not_unlock_if_synchronized flag.
1200     if (synchronized) {
1201       __ li(R0, 0);
1202       __ stb(R0, in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()), R16_thread);
1203     }
1204   }
1205 
1206   // --------------------------------------------------------------------------
1207   // Locking of synchronized methods. Must happen AFTER invocation_counter
1208   // check and stack overflow check, so method is not locked if overflows.
1209   if (synchronized) {
1210     lock_method(R3_ARG1, R4_ARG2, R5_ARG3);
1211   }
1212 #ifdef ASSERT
1213   else {
1214     Label Lok;
1215     __ lwz(R0, in_bytes(Method::access_flags_offset()), R19_method);
1216     __ andi_(R0, R0, JVM_ACC_SYNCHRONIZED);
1217     __ asm_assert_eq("method needs synchronization", 0x8521);
1218     __ bind(Lok);
1219   }
1220 #endif // ASSERT
1221 
1222   __ verify_thread();
1223 
1224   // --------------------------------------------------------------------------
1225   // JVMTI support
1226   __ notify_method_entry();
1227 
1228   // --------------------------------------------------------------------------
1229   // Start executing instructions.
1230   __ dispatch_next(vtos);
1231 
1232   // --------------------------------------------------------------------------
1233   // Out of line counter overflow and MDO creation code.
1234   if (ProfileInterpreter) {
1235     // We have decided to profile this method in the interpreter.
1236     __ bind(profile_method);
1237     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::profile_method));
1238     __ set_method_data_pointer_for_bcp();
1239     __ b(profile_method_continue);
1240   }
1241 
1242   if (inc_counter) {
1243     // Handle invocation counter overflow.
1244     __ bind(invocation_counter_overflow);
1245     generate_counter_overflow(profile_method_continue);
1246   }
1247   return entry;
1248 }
1249 
1250 // These should never be compiled since the interpreter will prefer
1251 // the compiled version to the intrinsic version.
1252 bool AbstractInterpreter::can_be_compiled(methodHandle m) {
1253   return !math_entry_available(method_kind(m));
1254 }
1255 
1256 // How much stack a method activation needs in stack slots.
1257 // We must calc this exactly like in generate_fixed_frame.
1258 // Note: This returns the conservative size assuming maximum alignment.
1259 int AbstractInterpreter::size_top_interpreter_activation(Method* method) {
1260   const int max_alignment_size = 2;
1261   const int abi_scratch = frame::abi_reg_args_size;
1262   return method->max_locals() + method->max_stack() +
1263          frame::interpreter_frame_monitor_size() + max_alignment_size + abi_scratch;
1264 }
1265 
1266 // Returns number of stackElementWords needed for the interpreter frame with the
1267 // given sections.
1268 // This overestimates the stack by one slot in case of alignments.
1269 int AbstractInterpreter::size_activation(int max_stack,
1270                                          int temps,
1271                                          int extra_args,
1272                                          int monitors,
1273                                          int callee_params,
1274                                          int callee_locals,
1275                                          bool is_top_frame) {
1276   // Note: This calculation must exactly parallel the frame setup
1277   // in InterpreterGenerator::generate_fixed_frame.
1278   assert(Interpreter::stackElementWords == 1, "sanity");
1279   const int max_alignment_space = StackAlignmentInBytes / Interpreter::stackElementSize;
1280   const int abi_scratch = is_top_frame ? (frame::abi_reg_args_size / Interpreter::stackElementSize) :
1281                                          (frame::abi_minframe_size / Interpreter::stackElementSize);
1282   const int size =
1283     max_stack                                                +
1284     (callee_locals - callee_params)                          +
1285     monitors * frame::interpreter_frame_monitor_size()       +
1286     max_alignment_space                                      +
1287     abi_scratch                                              +
1288     frame::ijava_state_size / Interpreter::stackElementSize;
1289 
1290   // Fixed size of an interpreter frame, align to 16-byte.
1291   return (size & -2);
1292 }
1293 
1294 // Fills a sceletal interpreter frame generated during deoptimizations.
1295 //
1296 // Parameters:
1297 //
1298 // interpreter_frame != NULL:
1299 //   set up the method, locals, and monitors.
1300 //   The frame interpreter_frame, if not NULL, is guaranteed to be the
1301 //   right size, as determined by a previous call to this method.
1302 //   It is also guaranteed to be walkable even though it is in a skeletal state
1303 //
1304 // is_top_frame == true:
1305 //   We're processing the *oldest* interpreter frame!
1306 //
1307 // pop_frame_extra_args:
1308 //   If this is != 0 we are returning to a deoptimized frame by popping
1309 //   off the callee frame. We want to re-execute the call that called the
1310 //   callee interpreted, but since the return to the interpreter would pop
1311 //   the arguments off advance the esp by dummy popframe_extra_args slots.
1312 //   Popping off those will establish the stack layout as it was before the call.
1313 //
1314 void AbstractInterpreter::layout_activation(Method* method,
1315                                             int tempcount,
1316                                             int popframe_extra_args,
1317                                             int moncount,
1318                                             int caller_actual_parameters,
1319                                             int callee_param_count,
1320                                             int callee_locals_count,
1321                                             frame* caller,
1322                                             frame* interpreter_frame,
1323                                             bool is_top_frame,
1324                                             bool is_bottom_frame) {
1325 
1326   const int abi_scratch = is_top_frame ? (frame::abi_reg_args_size / Interpreter::stackElementSize) :
1327                                          (frame::abi_minframe_size / Interpreter::stackElementSize);
1328 
1329   intptr_t* locals_base  = (caller->is_interpreted_frame()) ?
1330     caller->interpreter_frame_esp() + caller_actual_parameters :
1331     caller->sp() + method->max_locals() - 1 + (frame::abi_minframe_size / Interpreter::stackElementSize) ;
1332 
1333   intptr_t* monitor_base = caller->sp() - frame::ijava_state_size / Interpreter::stackElementSize ;
1334   intptr_t* monitor      = monitor_base - (moncount * frame::interpreter_frame_monitor_size());
1335   intptr_t* esp_base     = monitor - 1;
1336   intptr_t* esp          = esp_base - tempcount - popframe_extra_args;
1337   intptr_t* sp           = (intptr_t *) (((intptr_t) (esp_base - callee_locals_count + callee_param_count - method->max_stack()- abi_scratch)) & -StackAlignmentInBytes);
1338   intptr_t* sender_sp    = caller->sp() + (frame::abi_minframe_size - frame::abi_reg_args_size) / Interpreter::stackElementSize;
1339   intptr_t* top_frame_sp = is_top_frame ? sp : sp + (frame::abi_minframe_size - frame::abi_reg_args_size) / Interpreter::stackElementSize;
1340 
1341   interpreter_frame->interpreter_frame_set_method(method);
1342   interpreter_frame->interpreter_frame_set_locals(locals_base);
1343   interpreter_frame->interpreter_frame_set_cpcache(method->constants()->cache());
1344   interpreter_frame->interpreter_frame_set_esp(esp);
1345   interpreter_frame->interpreter_frame_set_monitor_end((BasicObjectLock *)monitor);
1346   interpreter_frame->interpreter_frame_set_top_frame_sp(top_frame_sp);
1347   if (!is_bottom_frame) {
1348     interpreter_frame->interpreter_frame_set_sender_sp(sender_sp);
1349   }
1350 }
1351 
1352 // =============================================================================
1353 // Exceptions
1354 
1355 void TemplateInterpreterGenerator::generate_throw_exception() {
1356   Register Rexception    = R17_tos,
1357            Rcontinuation = R3_RET;
1358 
1359   // --------------------------------------------------------------------------
1360   // Entry point if an method returns with a pending exception (rethrow).
1361   Interpreter::_rethrow_exception_entry = __ pc();
1362   {
1363     __ restore_interpreter_state(R11_scratch1); // Sets R11_scratch1 = fp.
1364     __ ld(R12_scratch2, _ijava_state_neg(top_frame_sp), R11_scratch1);
1365     __ resize_frame_absolute(R12_scratch2, R11_scratch1, R0);
1366 
1367     // Compiled code destroys templateTableBase, reload.
1368     __ load_const_optimized(R25_templateTableBase, (address)Interpreter::dispatch_table((TosState)0), R11_scratch1);
1369   }
1370 
1371   // Entry point if a interpreted method throws an exception (throw).
1372   Interpreter::_throw_exception_entry = __ pc();
1373   {
1374     __ mr(Rexception, R3_RET);
1375 
1376     __ verify_thread();
1377     __ verify_oop(Rexception);
1378 
1379     // Expression stack must be empty before entering the VM in case of an exception.
1380     __ empty_expression_stack();
1381     // Find exception handler address and preserve exception oop.
1382     // Call C routine to find handler and jump to it.
1383     __ call_VM(Rexception, CAST_FROM_FN_PTR(address, InterpreterRuntime::exception_handler_for_exception), Rexception);
1384     __ mtctr(Rcontinuation);
1385     // Push exception for exception handler bytecodes.
1386     __ push_ptr(Rexception);
1387 
1388     // Jump to exception handler (may be remove activation entry!).
1389     __ bctr();
1390   }
1391 
1392   // If the exception is not handled in the current frame the frame is
1393   // removed and the exception is rethrown (i.e. exception
1394   // continuation is _rethrow_exception).
1395   //
1396   // Note: At this point the bci is still the bxi for the instruction
1397   // which caused the exception and the expression stack is
1398   // empty. Thus, for any VM calls at this point, GC will find a legal
1399   // oop map (with empty expression stack).
1400 
1401   // In current activation
1402   // tos: exception
1403   // bcp: exception bcp
1404 
1405   // --------------------------------------------------------------------------
1406   // JVMTI PopFrame support
1407 
1408   Interpreter::_remove_activation_preserving_args_entry = __ pc();
1409   {
1410     // Set the popframe_processing bit in popframe_condition indicating that we are
1411     // currently handling popframe, so that call_VMs that may happen later do not
1412     // trigger new popframe handling cycles.
1413     __ lwz(R11_scratch1, in_bytes(JavaThread::popframe_condition_offset()), R16_thread);
1414     __ ori(R11_scratch1, R11_scratch1, JavaThread::popframe_processing_bit);
1415     __ stw(R11_scratch1, in_bytes(JavaThread::popframe_condition_offset()), R16_thread);
1416 
1417     // Empty the expression stack, as in normal exception handling.
1418     __ empty_expression_stack();
1419     __ unlock_if_synchronized_method(vtos, /* throw_monitor_exception */ false, /* install_monitor_exception */ false);
1420 
1421     // Check to see whether we are returning to a deoptimized frame.
1422     // (The PopFrame call ensures that the caller of the popped frame is
1423     // either interpreted or compiled and deoptimizes it if compiled.)
1424     // Note that we don't compare the return PC against the
1425     // deoptimization blob's unpack entry because of the presence of
1426     // adapter frames in C2.
1427     Label Lcaller_not_deoptimized;
1428     Register return_pc = R3_ARG1;
1429     __ ld(return_pc, 0, R1_SP);
1430     __ ld(return_pc, _abi(lr), return_pc);
1431     __ call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::interpreter_contains), return_pc);
1432     __ cmpdi(CCR0, R3_RET, 0);
1433     __ bne(CCR0, Lcaller_not_deoptimized);
1434 
1435     // The deoptimized case.
1436     // In this case, we can't call dispatch_next() after the frame is
1437     // popped, but instead must save the incoming arguments and restore
1438     // them after deoptimization has occurred.
1439     __ ld(R4_ARG2, in_bytes(Method::const_offset()), R19_method);
1440     __ lhz(R4_ARG2 /* number of params */, in_bytes(ConstMethod::size_of_parameters_offset()), R4_ARG2);
1441     __ slwi(R4_ARG2, R4_ARG2, Interpreter::logStackElementSize);
1442     __ addi(R5_ARG3, R18_locals, Interpreter::stackElementSize);
1443     __ subf(R5_ARG3, R4_ARG2, R5_ARG3);
1444     // Save these arguments.
1445     __ call_VM_leaf(CAST_FROM_FN_PTR(address, Deoptimization::popframe_preserve_args), R16_thread, R4_ARG2, R5_ARG3);
1446 
1447     // Inform deoptimization that it is responsible for restoring these arguments.
1448     __ load_const_optimized(R11_scratch1, JavaThread::popframe_force_deopt_reexecution_bit);
1449     __ stw(R11_scratch1, in_bytes(JavaThread::popframe_condition_offset()), R16_thread);
1450 
1451     // Return from the current method into the deoptimization blob. Will eventually
1452     // end up in the deopt interpeter entry, deoptimization prepared everything that
1453     // we will reexecute the call that called us.
1454     __ merge_frames(/*top_frame_sp*/ R21_sender_SP, /*reload return_pc*/ return_pc, R11_scratch1, R12_scratch2);
1455     __ mtlr(return_pc);
1456     __ blr();
1457 
1458     // The non-deoptimized case.
1459     __ bind(Lcaller_not_deoptimized);
1460 
1461     // Clear the popframe condition flag.
1462     __ li(R0, 0);
1463     __ stw(R0, in_bytes(JavaThread::popframe_condition_offset()), R16_thread);
1464 
1465     // Get out of the current method and re-execute the call that called us.
1466     __ merge_frames(/*top_frame_sp*/ R21_sender_SP, /*return_pc*/ noreg, R11_scratch1, R12_scratch2);
1467     __ restore_interpreter_state(R11_scratch1);
1468     __ ld(R12_scratch2, _ijava_state_neg(top_frame_sp), R11_scratch1);
1469     __ resize_frame_absolute(R12_scratch2, R11_scratch1, R0);
1470     if (ProfileInterpreter) {
1471       __ set_method_data_pointer_for_bcp();
1472     }
1473 #if INCLUDE_JVMTI
1474     Label L_done;
1475 
1476     __ lbz(R11_scratch1, 0, R14_bcp);
1477     __ cmpwi(CCR0, R11_scratch1, Bytecodes::_invokestatic);
1478     __ bne(CCR0, L_done);
1479 
1480     // The member name argument must be restored if _invokestatic is re-executed after a PopFrame call.
1481     // Detect such a case in the InterpreterRuntime function and return the member name argument, or NULL.
1482     __ ld(R4_ARG2, 0, R18_locals);
1483     __ call_VM(R11_scratch1, CAST_FROM_FN_PTR(address, InterpreterRuntime::member_name_arg_or_null),
1484                R4_ARG2, R19_method, R14_bcp);
1485 
1486     __ cmpdi(CCR0, R11_scratch1, 0);
1487     __ beq(CCR0, L_done);
1488 
1489     __ std(R11_scratch1, wordSize, R15_esp);
1490     __ bind(L_done);
1491 #endif // INCLUDE_JVMTI
1492     __ dispatch_next(vtos);
1493   }
1494   // end of JVMTI PopFrame support
1495 
1496   // --------------------------------------------------------------------------
1497   // Remove activation exception entry.
1498   // This is jumped to if an interpreted method can't handle an exception itself
1499   // (we come from the throw/rethrow exception entry above). We're going to call
1500   // into the VM to find the exception handler in the caller, pop the current
1501   // frame and return the handler we calculated.
1502   Interpreter::_remove_activation_entry = __ pc();
1503   {
1504     __ pop_ptr(Rexception);
1505     __ verify_thread();
1506     __ verify_oop(Rexception);
1507     __ std(Rexception, in_bytes(JavaThread::vm_result_offset()), R16_thread);
1508 
1509     __ unlock_if_synchronized_method(vtos, /* throw_monitor_exception */ false, true);
1510     __ notify_method_exit(false, vtos, InterpreterMacroAssembler::SkipNotifyJVMTI, false);
1511 
1512     __ get_vm_result(Rexception);
1513 
1514     // We are done with this activation frame; find out where to go next.
1515     // The continuation point will be an exception handler, which expects
1516     // the following registers set up:
1517     //
1518     // RET:  exception oop
1519     // ARG2: Issuing PC (see generate_exception_blob()), only used if the caller is compiled.
1520 
1521     Register return_pc = R31; // Needs to survive the runtime call.
1522     __ ld(return_pc, 0, R1_SP);
1523     __ ld(return_pc, _abi(lr), return_pc);
1524     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address), R16_thread, return_pc);
1525 
1526     // Remove the current activation.
1527     __ merge_frames(/*top_frame_sp*/ R21_sender_SP, /*return_pc*/ noreg, R11_scratch1, R12_scratch2);
1528 
1529     __ mr(R4_ARG2, return_pc);
1530     __ mtlr(R3_RET);
1531     __ mr(R3_RET, Rexception);
1532     __ blr();
1533   }
1534 }
1535 
1536 // JVMTI ForceEarlyReturn support.
1537 // Returns "in the middle" of a method with a "fake" return value.
1538 address TemplateInterpreterGenerator::generate_earlyret_entry_for(TosState state) {
1539 
1540   Register Rscratch1 = R11_scratch1,
1541            Rscratch2 = R12_scratch2;
1542 
1543   address entry = __ pc();
1544   __ empty_expression_stack();
1545 
1546   __ load_earlyret_value(state, Rscratch1);
1547 
1548   __ ld(Rscratch1, in_bytes(JavaThread::jvmti_thread_state_offset()), R16_thread);
1549   // Clear the earlyret state.
1550   __ li(R0, 0);
1551   __ stw(R0, in_bytes(JvmtiThreadState::earlyret_state_offset()), Rscratch1);
1552 
1553   __ remove_activation(state, false, false);
1554   // Copied from TemplateTable::_return.
1555   // Restoration of lr done by remove_activation.
1556   switch (state) {
1557     case ltos:
1558     case btos:
1559     case ctos:
1560     case stos:
1561     case atos:
1562     case itos: __ mr(R3_RET, R17_tos); break;
1563     case ftos:
1564     case dtos: __ fmr(F1_RET, F15_ftos); break;
1565     case vtos: // This might be a constructor. Final fields (and volatile fields on PPC64) need
1566                // to get visible before the reference to the object gets stored anywhere.
1567                __ membar(Assembler::StoreStore); break;
1568     default  : ShouldNotReachHere();
1569   }
1570   __ blr();
1571 
1572   return entry;
1573 } // end of ForceEarlyReturn support
1574 
1575 //-----------------------------------------------------------------------------
1576 // Helper for vtos entry point generation
1577 
1578 void TemplateInterpreterGenerator::set_vtos_entry_points(Template* t,
1579                                                          address& bep,
1580                                                          address& cep,
1581                                                          address& sep,
1582                                                          address& aep,
1583                                                          address& iep,
1584                                                          address& lep,
1585                                                          address& fep,
1586                                                          address& dep,
1587                                                          address& vep) {
1588   assert(t->is_valid() && t->tos_in() == vtos, "illegal template");
1589   Label L;
1590 
1591   aep = __ pc();  __ push_ptr();  __ b(L);
1592   fep = __ pc();  __ push_f();    __ b(L);
1593   dep = __ pc();  __ push_d();    __ b(L);
1594   lep = __ pc();  __ push_l();    __ b(L);
1595   __ align(32, 12, 24); // align L
1596   bep = cep = sep =
1597   iep = __ pc();  __ push_i();
1598   vep = __ pc();
1599   __ bind(L);
1600   generate_and_dispatch(t);
1601 }
1602 
1603 //-----------------------------------------------------------------------------
1604 // Generation of individual instructions
1605 
1606 // helpers for generate_and_dispatch
1607 
1608 InterpreterGenerator::InterpreterGenerator(StubQueue* code)
1609   : TemplateInterpreterGenerator(code) {
1610   generate_all(); // Down here so it can be "virtual".
1611 }
1612 
1613 //-----------------------------------------------------------------------------
1614 
1615 // Non-product code
1616 #ifndef PRODUCT
1617 address TemplateInterpreterGenerator::generate_trace_code(TosState state) {
1618   //__ flush_bundle();
1619   address entry = __ pc();
1620 
1621   const char *bname = NULL;
1622   uint tsize = 0;
1623   switch(state) {
1624   case ftos:
1625     bname = "trace_code_ftos {";
1626     tsize = 2;
1627     break;
1628   case btos:
1629     bname = "trace_code_btos {";
1630     tsize = 2;
1631     break;
1632   case ctos:
1633     bname = "trace_code_ctos {";
1634     tsize = 2;
1635     break;
1636   case stos:
1637     bname = "trace_code_stos {";
1638     tsize = 2;
1639     break;
1640   case itos:
1641     bname = "trace_code_itos {";
1642     tsize = 2;
1643     break;
1644   case ltos:
1645     bname = "trace_code_ltos {";
1646     tsize = 3;
1647     break;
1648   case atos:
1649     bname = "trace_code_atos {";
1650     tsize = 2;
1651     break;
1652   case vtos:
1653     // Note: In case of vtos, the topmost of stack value could be a int or doubl
1654     // In case of a double (2 slots) we won't see the 2nd stack value.
1655     // Maybe we simply should print the topmost 3 stack slots to cope with the problem.
1656     bname = "trace_code_vtos {";
1657     tsize = 2;
1658 
1659     break;
1660   case dtos:
1661     bname = "trace_code_dtos {";
1662     tsize = 3;
1663     break;
1664   default:
1665     ShouldNotReachHere();
1666   }
1667   BLOCK_COMMENT(bname);
1668 
1669   // Support short-cut for TraceBytecodesAt.
1670   // Don't call into the VM if we don't want to trace to speed up things.
1671   Label Lskip_vm_call;
1672   if (TraceBytecodesAt > 0 && TraceBytecodesAt < max_intx) {
1673     int offs1 = __ load_const_optimized(R11_scratch1, (address) &TraceBytecodesAt, R0, true);
1674     int offs2 = __ load_const_optimized(R12_scratch2, (address) &BytecodeCounter::_counter_value, R0, true);
1675     __ ld(R11_scratch1, offs1, R11_scratch1);
1676     __ lwa(R12_scratch2, offs2, R12_scratch2);
1677     __ cmpd(CCR0, R12_scratch2, R11_scratch1);
1678     __ blt(CCR0, Lskip_vm_call);
1679   }
1680 
1681   __ push(state);
1682   // Load 2 topmost expression stack values.
1683   __ ld(R6_ARG4, tsize*Interpreter::stackElementSize, R15_esp);
1684   __ ld(R5_ARG3, Interpreter::stackElementSize, R15_esp);
1685   __ mflr(R31);
1686   __ call_VM(noreg, CAST_FROM_FN_PTR(address, SharedRuntime::trace_bytecode), /* unused */ R4_ARG2, R5_ARG3, R6_ARG4, false);
1687   __ mtlr(R31);
1688   __ pop(state);
1689 
1690   if (TraceBytecodesAt > 0 && TraceBytecodesAt < max_intx) {
1691     __ bind(Lskip_vm_call);
1692   }
1693   __ blr();
1694   BLOCK_COMMENT("} trace_code");
1695   return entry;
1696 }
1697 
1698 void TemplateInterpreterGenerator::count_bytecode() {
1699   int offs = __ load_const_optimized(R11_scratch1, (address) &BytecodeCounter::_counter_value, R12_scratch2, true);
1700   __ lwz(R12_scratch2, offs, R11_scratch1);
1701   __ addi(R12_scratch2, R12_scratch2, 1);
1702   __ stw(R12_scratch2, offs, R11_scratch1);
1703 }
1704 
1705 void TemplateInterpreterGenerator::histogram_bytecode(Template* t) {
1706   int offs = __ load_const_optimized(R11_scratch1, (address) &BytecodeHistogram::_counters[t->bytecode()], R12_scratch2, true);
1707   __ lwz(R12_scratch2, offs, R11_scratch1);
1708   __ addi(R12_scratch2, R12_scratch2, 1);
1709   __ stw(R12_scratch2, offs, R11_scratch1);
1710 }
1711 
1712 void TemplateInterpreterGenerator::histogram_bytecode_pair(Template* t) {
1713   const Register addr = R11_scratch1,
1714                  tmp  = R12_scratch2;
1715   // Get index, shift out old bytecode, bring in new bytecode, and store it.
1716   // _index = (_index >> log2_number_of_codes) |
1717   //          (bytecode << log2_number_of_codes);
1718   int offs1 = __ load_const_optimized(addr, (address)&BytecodePairHistogram::_index, tmp, true);
1719   __ lwz(tmp, offs1, addr);
1720   __ srwi(tmp, tmp, BytecodePairHistogram::log2_number_of_codes);
1721   __ ori(tmp, tmp, ((int) t->bytecode()) << BytecodePairHistogram::log2_number_of_codes);
1722   __ stw(tmp, offs1, addr);
1723 
1724   // Bump bucket contents.
1725   // _counters[_index] ++;
1726   int offs2 = __ load_const_optimized(addr, (address)&BytecodePairHistogram::_counters, R0, true);
1727   __ sldi(tmp, tmp, LogBytesPerInt);
1728   __ add(addr, tmp, addr);
1729   __ lwz(tmp, offs2, addr);
1730   __ addi(tmp, tmp, 1);
1731   __ stw(tmp, offs2, addr);
1732 }
1733 
1734 void TemplateInterpreterGenerator::trace_bytecode(Template* t) {
1735   // Call a little run-time stub to avoid blow-up for each bytecode.
1736   // The run-time runtime saves the right registers, depending on
1737   // the tosca in-state for the given template.
1738 
1739   assert(Interpreter::trace_code(t->tos_in()) != NULL,
1740          "entry must have been generated");
1741 
1742   // Note: we destroy LR here.
1743   __ bl(Interpreter::trace_code(t->tos_in()));
1744 }
1745 
1746 void TemplateInterpreterGenerator::stop_interpreter_at() {
1747   Label L;
1748   int offs1 = __ load_const_optimized(R11_scratch1, (address) &StopInterpreterAt, R0, true);
1749   int offs2 = __ load_const_optimized(R12_scratch2, (address) &BytecodeCounter::_counter_value, R0, true);
1750   __ ld(R11_scratch1, offs1, R11_scratch1);
1751   __ lwa(R12_scratch2, offs2, R12_scratch2);
1752   __ cmpd(CCR0, R12_scratch2, R11_scratch1);
1753   __ bne(CCR0, L);
1754   __ illtrap();
1755   __ bind(L);
1756 }
1757 
1758 #endif // !PRODUCT
1759 #endif // !CC_INTERP