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