1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2012, 2013 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 #include "asm/assembler.hpp"
  28 #include "asm/macroAssembler.inline.hpp"
  29 #include "interpreter/bytecodeHistogram.hpp"
  30 #include "interpreter/cppInterpreter.hpp"
  31 #include "interpreter/interpreter.hpp"
  32 #include "interpreter/interpreterGenerator.hpp"
  33 #include "interpreter/interpreterRuntime.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/interfaceSupport.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 #ifdef SHARK
  51 #include "shark/shark_globals.hpp"
  52 #endif
  53 
  54 #ifdef CC_INTERP
  55 
  56 #define __ _masm->
  57 
  58 // Contains is used for identifying interpreter frames during a stack-walk.
  59 // A frame with a PC in InterpretMethod must be identified as a normal C frame.
  60 bool CppInterpreter::contains(address pc) {
  61   return _code->contains(pc);
  62 }
  63 
  64 #ifdef PRODUCT
  65 #define BLOCK_COMMENT(str) // nothing
  66 #else
  67 #define BLOCK_COMMENT(str) __ block_comment(str)
  68 #endif
  69 
  70 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
  71 
  72 static address interpreter_frame_manager        = NULL;
  73 static address frame_manager_specialized_return = NULL;
  74 static address native_entry                     = NULL;
  75 
  76 static address interpreter_return_address       = NULL;
  77 
  78 static address unctrap_frame_manager_entry      = NULL;
  79 
  80 static address deopt_frame_manager_return_atos  = NULL;
  81 static address deopt_frame_manager_return_btos  = NULL;
  82 static address deopt_frame_manager_return_itos  = NULL;
  83 static address deopt_frame_manager_return_ltos  = NULL;
  84 static address deopt_frame_manager_return_ftos  = NULL;
  85 static address deopt_frame_manager_return_dtos  = NULL;
  86 static address deopt_frame_manager_return_vtos  = NULL;
  87 
  88 // A result handler converts/unboxes a native call result into
  89 // a java interpreter/compiler result. The current frame is an
  90 // interpreter frame.
  91 address CppInterpreterGenerator::generate_result_handler_for(BasicType type) {
  92   return AbstractInterpreterGenerator::generate_result_handler_for(type);
  93 }
  94 
  95 // tosca based result to c++ interpreter stack based result.
  96 address CppInterpreterGenerator::generate_tosca_to_stack_converter(BasicType type) {
  97   //
  98   // A result is in the native abi result register from a native
  99   // method call. We need to return this result to the interpreter by
 100   // pushing the result on the interpreter's stack.
 101   //
 102   // Registers alive:
 103   //   R3_ARG1(R3_RET)/F1_ARG1(F1_RET) - result to move
 104   //   R4_ARG2                         - address of tos
 105   //   LR
 106   //
 107   // Registers updated:
 108   //   R3_RET(R3_ARG1)   - address of new tos (== R17_tos for T_VOID)
 109   //
 110 
 111   int number_of_used_slots = 1;
 112 
 113   const Register tos = R4_ARG2;
 114   Label done;
 115   Label is_false;
 116 
 117   address entry = __ pc();
 118 
 119   switch (type) {
 120   case T_BOOLEAN:
 121     __ cmpwi(CCR0, R3_RET, 0);
 122     __ beq(CCR0, is_false);
 123     __ li(R3_RET, 1);
 124     __ stw(R3_RET, 0, tos);
 125     __ b(done);
 126     __ bind(is_false);
 127     __ li(R3_RET, 0);
 128     __ stw(R3_RET, 0, tos);
 129     break;
 130   case T_BYTE:
 131   case T_CHAR:
 132   case T_SHORT:
 133   case T_INT:
 134     __ stw(R3_RET, 0, tos);
 135     break;
 136   case T_LONG:
 137     number_of_used_slots = 2;
 138     // mark unused slot for debugging
 139     // long goes to topmost slot
 140     __ std(R3_RET, -BytesPerWord, tos);
 141     __ li(R3_RET, 0);
 142     __ std(R3_RET, 0, tos);
 143     break;
 144   case T_OBJECT:
 145     __ verify_oop(R3_RET);
 146     __ std(R3_RET, 0, tos);
 147     break;
 148   case T_FLOAT:
 149     __ stfs(F1_RET, 0, tos);
 150     break;
 151   case T_DOUBLE:
 152     number_of_used_slots = 2;
 153     // mark unused slot for debugging
 154     __ li(R3_RET, 0);
 155     __ std(R3_RET, 0, tos);
 156     // double goes to topmost slot
 157     __ stfd(F1_RET, -BytesPerWord, tos);
 158     break;
 159   case T_VOID:
 160     number_of_used_slots = 0;
 161     break;
 162   default:
 163     ShouldNotReachHere();
 164   }
 165 
 166   __ BIND(done);
 167 
 168   // new expression stack top
 169   __ addi(R3_RET, tos, -BytesPerWord * number_of_used_slots);
 170 
 171   __ blr();
 172 
 173   return entry;
 174 }
 175 
 176 address CppInterpreterGenerator::generate_stack_to_stack_converter(BasicType type) {
 177   //
 178   // Copy the result from the callee's stack to the caller's stack,
 179   // caller and callee both being interpreted.
 180   //
 181   // Registers alive
 182   //   R3_ARG1        - address of callee's tos + BytesPerWord
 183   //   R4_ARG2        - address of caller's tos [i.e. free location]
 184   //   LR
 185   //
 186   //   stack grows upwards, memory grows downwards.
 187   //
 188   //   [      free         ]  <-- callee's tos
 189   //   [  optional result  ]  <-- R3_ARG1
 190   //   [  optional dummy   ]
 191   //          ...
 192   //   [      free         ]  <-- caller's tos, R4_ARG2
 193   //          ...
 194   // Registers updated
 195   //   R3_RET(R3_ARG1) - address of caller's new tos
 196   //
 197   //   stack grows upwards, memory grows downwards.
 198   //
 199   //   [      free         ]  <-- current tos, R3_RET
 200   //   [  optional result  ]
 201   //   [  optional dummy   ]
 202   //          ...
 203   //
 204 
 205   const Register from = R3_ARG1;
 206   const Register ret  = R3_ARG1;
 207   const Register tos  = R4_ARG2;
 208   const Register tmp1 = R21_tmp1;
 209   const Register tmp2 = R22_tmp2;
 210 
 211   address entry = __ pc();
 212 
 213   switch (type) {
 214   case T_BOOLEAN:
 215   case T_BYTE:
 216   case T_CHAR:
 217   case T_SHORT:
 218   case T_INT:
 219   case T_FLOAT:
 220     __ lwz(tmp1, 0, from);
 221     __ stw(tmp1, 0, tos);
 222     // New expression stack top.
 223     __ addi(ret, tos, - BytesPerWord);
 224     break;
 225   case T_LONG:
 226   case T_DOUBLE:
 227     // Move both entries for debug purposes even though only one is live.
 228     __ ld(tmp1, BytesPerWord, from);
 229     __ ld(tmp2, 0, from);
 230     __ std(tmp1, 0, tos);
 231     __ std(tmp2, -BytesPerWord, tos);
 232     // New expression stack top.
 233     __ addi(ret, tos, - 2 * BytesPerWord); // two slots
 234     break;
 235   case T_OBJECT:
 236     __ ld(tmp1, 0, from);
 237     __ verify_oop(tmp1);
 238     __ std(tmp1, 0, tos);
 239     // New expression stack top.
 240     __ addi(ret, tos, - BytesPerWord);
 241     break;
 242   case T_VOID:
 243     // New expression stack top.
 244     __ mr(ret, tos);
 245     break;
 246   default:
 247     ShouldNotReachHere();
 248   }
 249 
 250   __ blr();
 251 
 252   return entry;
 253 }
 254 
 255 address CppInterpreterGenerator::generate_stack_to_native_abi_converter(BasicType type) {
 256   //
 257   // Load a result from the callee's stack into the caller's expecting
 258   // return register, callee being interpreted, caller being call stub
 259   // or jit code.
 260   //
 261   // Registers alive
 262   //   R3_ARG1   - callee expression tos + BytesPerWord
 263   //   LR
 264   //
 265   //   stack grows upwards, memory grows downwards.
 266   //
 267   //   [      free         ]  <-- callee's tos
 268   //   [  optional result  ]  <-- R3_ARG1
 269   //   [  optional dummy   ]
 270   //          ...
 271   //
 272   // Registers updated
 273   //   R3_RET(R3_ARG1)/F1_RET - result
 274   //
 275 
 276   const Register from = R3_ARG1;
 277   const Register ret = R3_ARG1;
 278   const FloatRegister fret = F1_ARG1;
 279 
 280   address entry = __ pc();
 281 
 282   // Implemented uniformly for both kinds of endianness. The interpreter
 283   // implements boolean, byte, char, and short as jint (4 bytes).
 284   switch (type) {
 285   case T_BOOLEAN:
 286   case T_CHAR:
 287     // zero extension
 288     __ lwz(ret, 0, from);
 289     break;
 290   case T_BYTE:
 291   case T_SHORT:
 292   case T_INT:
 293     // sign extension
 294     __ lwa(ret, 0, from);
 295     break;
 296   case T_LONG:
 297     __ ld(ret, 0, from);
 298     break;
 299   case T_OBJECT:
 300     __ ld(ret, 0, from);
 301     __ verify_oop(ret);
 302     break;
 303   case T_FLOAT:
 304     __ lfs(fret, 0, from);
 305     break;
 306   case T_DOUBLE:
 307     __ lfd(fret, 0, from);
 308     break;
 309   case T_VOID:
 310     break;
 311   default:
 312     ShouldNotReachHere();
 313   }
 314 
 315   __ blr();
 316 
 317   return entry;
 318 }
 319 
 320 address CppInterpreter::return_entry(TosState state, int length, Bytecodes::Code code) {
 321   assert(interpreter_return_address != NULL, "Not initialized");
 322   return interpreter_return_address;
 323 }
 324 
 325 address CppInterpreter::deopt_entry(TosState state, int length) {
 326   address ret = NULL;
 327   if (length != 0) {
 328     switch (state) {
 329       case atos: ret = deopt_frame_manager_return_atos; break;
 330       case btos: ret = deopt_frame_manager_return_itos; break;
 331       case ctos:
 332       case stos:
 333       case itos: ret = deopt_frame_manager_return_itos; break;
 334       case ltos: ret = deopt_frame_manager_return_ltos; break;
 335       case ftos: ret = deopt_frame_manager_return_ftos; break;
 336       case dtos: ret = deopt_frame_manager_return_dtos; break;
 337       case vtos: ret = deopt_frame_manager_return_vtos; break;
 338       default: ShouldNotReachHere();
 339     }
 340   } else {
 341     ret = unctrap_frame_manager_entry;  // re-execute the bytecode (e.g. uncommon trap, popframe)
 342   }
 343   assert(ret != NULL, "Not initialized");
 344   return ret;
 345 }
 346 
 347 //
 348 // Helpers for commoning out cases in the various type of method entries.
 349 //
 350 
 351 //
 352 // Registers alive
 353 //   R16_thread      - JavaThread*
 354 //   R1_SP           - old stack pointer
 355 //   R19_method      - callee's Method
 356 //   R17_tos         - address of caller's tos (prepushed)
 357 //   R15_prev_state  - address of caller's BytecodeInterpreter or 0
 358 //   return_pc in R21_tmp15 (only when called within generate_native_entry)
 359 //
 360 // Registers updated
 361 //   R14_state       - address of callee's interpreter state
 362 //   R1_SP           - new stack pointer
 363 //   CCR4_is_synced  - current method is synchronized
 364 //
 365 void CppInterpreterGenerator::generate_compute_interpreter_state(Label& stack_overflow_return) {
 366   //
 367   // Stack layout at this point:
 368   //
 369   //   F1      [TOP_IJAVA_FRAME_ABI]              <-- R1_SP
 370   //           alignment (optional)
 371   //           [F1's outgoing Java arguments]     <-- R17_tos
 372   //           ...
 373   //   F2      [PARENT_IJAVA_FRAME_ABI]
 374   //            ...
 375 
 376   //=============================================================================
 377   // Allocate space for locals other than the parameters, the
 378   // interpreter state, monitors, and the expression stack.
 379 
 380   const Register local_count        = R21_tmp1;
 381   const Register parameter_count    = R22_tmp2;
 382   const Register max_stack          = R23_tmp3;
 383   // Must not be overwritten within this method!
 384   // const Register return_pc         = R29_tmp9;
 385 
 386   const ConditionRegister is_synced = CCR4_is_synced;
 387   const ConditionRegister is_native = CCR6;
 388   const ConditionRegister is_static = CCR7;
 389 
 390   assert(is_synced != is_native, "condition code registers must be distinct");
 391   assert(is_synced != is_static, "condition code registers must be distinct");
 392   assert(is_native != is_static, "condition code registers must be distinct");
 393 
 394   {
 395 
 396   // Local registers
 397   const Register top_frame_size     = R24_tmp4;
 398   const Register access_flags       = R25_tmp5;
 399   const Register state_offset       = R26_tmp6;
 400   Register mem_stack_limit          = R27_tmp7;
 401   const Register page_size          = R28_tmp8;
 402 
 403   BLOCK_COMMENT("compute_interpreter_state {");
 404 
 405   // access_flags = method->access_flags();
 406   // TODO: PPC port: assert(4 == methodOopDesc::sz_access_flags(), "unexpected field size");
 407   __ lwa(access_flags, method_(access_flags));
 408 
 409   // parameter_count = method->constMethod->size_of_parameters();
 410   // TODO: PPC port: assert(2 == ConstMethod::sz_size_of_parameters(), "unexpected field size");
 411   __ ld(max_stack, in_bytes(Method::const_offset()), R19_method);   // Max_stack holds constMethod for a while.
 412   __ lhz(parameter_count, in_bytes(ConstMethod::size_of_parameters_offset()), max_stack);
 413 
 414   // local_count = method->constMethod()->max_locals();
 415   // TODO: PPC port: assert(2 == ConstMethod::sz_max_locals(), "unexpected field size");
 416   __ lhz(local_count, in_bytes(ConstMethod::size_of_locals_offset()), max_stack);
 417 
 418   // max_stack = method->constMethod()->max_stack();
 419   // TODO: PPC port: assert(2 == ConstMethod::sz_max_stack(), "unexpected field size");
 420   __ lhz(max_stack, in_bytes(ConstMethod::max_stack_offset()), max_stack);
 421 
 422   if (EnableInvokeDynamic) {
 423     // Take into account 'extra_stack_entries' needed by method handles (see method.hpp).
 424     __ addi(max_stack, max_stack, Method::extra_stack_entries());
 425   }
 426 
 427   // mem_stack_limit = thread->stack_limit();
 428   __ ld(mem_stack_limit, thread_(stack_overflow_limit));
 429 
 430   // Point locals at the first argument. Method's locals are the
 431   // parameters on top of caller's expression stack.
 432 
 433   // tos points past last Java argument
 434   __ sldi(R18_locals, parameter_count, Interpreter::logStackElementSize);
 435   __ add(R18_locals, R17_tos, R18_locals);
 436 
 437   // R18_locals - i*BytesPerWord points to i-th Java local (i starts at 0)
 438 
 439   // Set is_native, is_synced, is_static - will be used later.
 440   __ testbitdi(is_native, R0, access_flags, JVM_ACC_NATIVE_BIT);
 441   __ testbitdi(is_synced, R0, access_flags, JVM_ACC_SYNCHRONIZED_BIT);
 442   assert(is_synced->is_nonvolatile(), "is_synced must be non-volatile");
 443   __ testbitdi(is_static, R0, access_flags, JVM_ACC_STATIC_BIT);
 444 
 445   // PARENT_IJAVA_FRAME_ABI
 446   //
 447   // frame_size =
 448   //   round_to((local_count - parameter_count)*BytesPerWord +
 449   //              2*BytesPerWord +
 450   //              alignment +
 451   //              frame::interpreter_frame_cinterpreterstate_size_in_bytes()
 452   //              sizeof(PARENT_IJAVA_FRAME_ABI)
 453   //              method->is_synchronized() ? sizeof(BasicObjectLock) : 0 +
 454   //              max_stack*BytesPerWord,
 455   //            16)
 456   //
 457   // Note that this calculation is exactly mirrored by
 458   // AbstractInterpreter::layout_activation_impl() [ and
 459   // AbstractInterpreter::size_activation() ]. Which is used by
 460   // deoptimization so that it can allocate the proper sized
 461   // frame. This only happens for interpreted frames so the extra
 462   // notes below about max_stack below are not important. The other
 463   // thing to note is that for interpreter frames other than the
 464   // current activation the size of the stack is the size of the live
 465   // portion of the stack at the particular bcp and NOT the maximum
 466   // stack that the method might use.
 467   //
 468   // If we're calling a native method, we replace max_stack (which is
 469   // zero) with space for the worst-case signature handler varargs
 470   // vector, which is:
 471   //
 472   //   max_stack = max(Argument::n_register_parameters, parameter_count+2);
 473   //
 474   // We add two slots to the parameter_count, one for the jni
 475   // environment and one for a possible native mirror.  We allocate
 476   // space for at least the number of ABI registers, even though
 477   // InterpreterRuntime::slow_signature_handler won't write more than
 478   // parameter_count+2 words when it creates the varargs vector at the
 479   // top of the stack.  The generated slow signature handler will just
 480   // load trash into registers beyond the necessary number.  We're
 481   // still going to cut the stack back by the ABI register parameter
 482   // count so as to get SP+16 pointing at the ABI outgoing parameter
 483   // area, so we need to allocate at least that much even though we're
 484   // going to throw it away.
 485   //
 486 
 487   // Adjust max_stack for native methods:
 488   Label skip_native_calculate_max_stack;
 489   __ bfalse(is_native, skip_native_calculate_max_stack);
 490   // if (is_native) {
 491   //  max_stack = max(Argument::n_register_parameters, parameter_count+2);
 492   __ addi(max_stack, parameter_count, 2*Interpreter::stackElementWords);
 493   __ cmpwi(CCR0, max_stack, Argument::n_register_parameters);
 494   __ bge(CCR0, skip_native_calculate_max_stack);
 495   __ li(max_stack,  Argument::n_register_parameters);
 496   // }
 497   __ bind(skip_native_calculate_max_stack);
 498   // max_stack is now in bytes
 499   __ slwi(max_stack, max_stack, Interpreter::logStackElementSize);
 500 
 501   // Calculate number of non-parameter locals (in slots):
 502   Label not_java;
 503   __ btrue(is_native, not_java);
 504   // if (!is_native) {
 505   //   local_count = non-parameter local count
 506   __ sub(local_count, local_count, parameter_count);
 507   // } else {
 508   //   // nothing to do: method->max_locals() == 0 for native methods
 509   // }
 510   __ bind(not_java);
 511 
 512 
 513   // Calculate top_frame_size and parent_frame_resize.
 514   {
 515   const Register parent_frame_resize = R12_scratch2;
 516 
 517   BLOCK_COMMENT("Compute top_frame_size.");
 518   // top_frame_size = TOP_IJAVA_FRAME_ABI
 519   //                  + size of interpreter state
 520   __ li(top_frame_size, frame::top_ijava_frame_abi_size
 521                         + frame::interpreter_frame_cinterpreterstate_size_in_bytes());
 522   //                  + max_stack
 523   __ add(top_frame_size, top_frame_size, max_stack);
 524   //                  + stack slots for a BasicObjectLock for synchronized methods
 525   {
 526     Label not_synced;
 527     __ bfalse(is_synced, not_synced);
 528     __ addi(top_frame_size, top_frame_size, frame::interpreter_frame_monitor_size_in_bytes());
 529     __ bind(not_synced);
 530   }
 531   // align
 532   __ round_to(top_frame_size, frame::alignment_in_bytes);
 533 
 534 
 535   BLOCK_COMMENT("Compute parent_frame_resize.");
 536   // parent_frame_resize = R1_SP - R17_tos
 537   __ sub(parent_frame_resize, R1_SP, R17_tos);
 538   //__ li(parent_frame_resize, 0);
 539   //                       + PARENT_IJAVA_FRAME_ABI
 540   //                       + extra two slots for the no-parameter/no-locals
 541   //                         method result
 542   __ addi(parent_frame_resize, parent_frame_resize,
 543                                       frame::parent_ijava_frame_abi_size
 544                                     + 2*Interpreter::stackElementSize);
 545   //                       + (locals_count - params_count)
 546   __ sldi(R0, local_count, Interpreter::logStackElementSize);
 547   __ add(parent_frame_resize, parent_frame_resize, R0);
 548   // align
 549   __ round_to(parent_frame_resize, frame::alignment_in_bytes);
 550 
 551   //
 552   // Stack layout at this point:
 553   //
 554   // The new frame F0 hasn't yet been pushed, F1 is still the top frame.
 555   //
 556   //   F0      [TOP_IJAVA_FRAME_ABI]
 557   //           alignment (optional)
 558   //           [F0's full operand stack]
 559   //           [F0's monitors] (optional)
 560   //           [F0's BytecodeInterpreter object]
 561   //   F1      [PARENT_IJAVA_FRAME_ABI]
 562   //           alignment (optional)
 563   //           [F0's Java result]
 564   //           [F0's non-arg Java locals]
 565   //           [F1's outgoing Java arguments]     <-- R17_tos
 566   //           ...
 567   //   F2      [PARENT_IJAVA_FRAME_ABI]
 568   //            ...
 569 
 570 
 571   // Calculate new R14_state
 572   // and
 573   // test that the new memory stack pointer is above the limit,
 574   // throw a StackOverflowError otherwise.
 575   __ sub(R11_scratch1/*F1's SP*/,  R1_SP, parent_frame_resize);
 576   __ addi(R14_state, R11_scratch1/*F1's SP*/,
 577               -frame::interpreter_frame_cinterpreterstate_size_in_bytes());
 578   __ sub(R11_scratch1/*F0's SP*/,
 579              R11_scratch1/*F1's SP*/, top_frame_size);
 580 
 581   BLOCK_COMMENT("Test for stack overflow:");
 582   __ cmpld(CCR0/*is_stack_overflow*/, R11_scratch1, mem_stack_limit);
 583   __ blt(CCR0/*is_stack_overflow*/, stack_overflow_return);
 584 
 585 
 586   //=============================================================================
 587   // Frame_size doesn't overflow the stack. Allocate new frame and
 588   // initialize interpreter state.
 589 
 590   // Register state
 591   //
 592   //   R15            - local_count
 593   //   R16            - parameter_count
 594   //   R17            - max_stack
 595   //
 596   //   R18            - frame_size
 597   //   R19            - access_flags
 598   //   CCR4_is_synced - is_synced
 599   //
 600   //   GR_Lstate      - pointer to the uninitialized new BytecodeInterpreter.
 601 
 602   // _last_Java_pc just needs to be close enough that we can identify
 603   // the frame as an interpreted frame. It does not need to be the
 604   // exact return address from either calling
 605   // BytecodeInterpreter::InterpretMethod or the call to a jni native method.
 606   // So we can initialize it here with a value of a bundle in this
 607   // code fragment. We only do this initialization for java frames
 608   // where InterpretMethod needs a a way to get a good pc value to
 609   // store in the thread state. For interpreter frames used to call
 610   // jni native code we just zero the value in the state and move an
 611   // ip as needed in the native entry code.
 612   //
 613   // const Register last_Java_pc_addr     = GR24_SCRATCH;  // QQQ 27
 614   // const Register last_Java_pc          = GR26_SCRATCH;
 615 
 616   // Must reference stack before setting new SP since Windows
 617   // will not be able to deliver the exception on a bad SP.
 618   // Windows also insists that we bang each page one at a time in order
 619   // for the OS to map in the reserved pages. If we bang only
 620   // the final page, Windows stops delivering exceptions to our
 621   // VectoredExceptionHandler and terminates our program.
 622   // Linux only requires a single bang but it's rare to have
 623   // to bang more than 1 page so the code is enabled for both OS's.
 624 
 625   // BANG THE STACK
 626   //
 627   // Nothing to do for PPC, because updating the SP will automatically
 628   // bang the page.
 629 
 630   // Up to here we have calculated the delta for the new C-frame and
 631   // checked for a stack-overflow. Now we can savely update SP and
 632   // resize the C-frame.
 633 
 634   // R14_state has already been calculated.
 635   __ push_interpreter_frame(top_frame_size, parent_frame_resize,
 636                             R25_tmp5, R26_tmp6, R27_tmp7, R28_tmp8);
 637 
 638   }
 639 
 640   //
 641   // Stack layout at this point:
 642   //
 643   //   F0 has been been pushed!
 644   //
 645   //   F0      [TOP_IJAVA_FRAME_ABI]              <-- R1_SP
 646   //           alignment (optional)               (now it's here, if required)
 647   //           [F0's full operand stack]
 648   //           [F0's monitors] (optional)
 649   //           [F0's BytecodeInterpreter object]
 650   //   F1      [PARENT_IJAVA_FRAME_ABI]
 651   //           alignment (optional)               (now it's here, if required)
 652   //           [F0's Java result]
 653   //           [F0's non-arg Java locals]
 654   //           [F1's outgoing Java arguments]
 655   //           ...
 656   //   F2      [PARENT_IJAVA_FRAME_ABI]
 657   //           ...
 658   //
 659   // R14_state points to F0's BytecodeInterpreter object.
 660   //
 661 
 662   }
 663 
 664   //=============================================================================
 665   // new BytecodeInterpreter-object is save, let's initialize it:
 666   BLOCK_COMMENT("New BytecodeInterpreter-object is save.");
 667 
 668   {
 669   // Locals
 670   const Register bytecode_addr = R24_tmp4;
 671   const Register constants     = R25_tmp5;
 672   const Register tos           = R26_tmp6;
 673   const Register stack_base    = R27_tmp7;
 674   const Register local_addr    = R28_tmp8;
 675   {
 676     Label L;
 677     __ btrue(is_native, L);
 678     // if (!is_native) {
 679       // bytecode_addr = constMethod->codes();
 680       __ ld(bytecode_addr, method_(const));
 681       __ addi(bytecode_addr, bytecode_addr, in_bytes(ConstMethod::codes_offset()));
 682     // }
 683     __ bind(L);
 684   }
 685 
 686   __ ld(constants, in_bytes(Method::const_offset()), R19_method);
 687   __ ld(constants, in_bytes(ConstMethod::constants_offset()), constants);
 688 
 689   // state->_prev_link = prev_state;
 690   __ std(R15_prev_state, state_(_prev_link));
 691 
 692   // For assertions only.
 693   // TODO: not needed anyway because it coincides with `_monitor_base'. remove!
 694   // state->_self_link = state;
 695   DEBUG_ONLY(__ std(R14_state, state_(_self_link));)
 696 
 697   // state->_thread = thread;
 698   __ std(R16_thread, state_(_thread));
 699 
 700   // state->_method = method;
 701   __ std(R19_method, state_(_method));
 702 
 703   // state->_locals = locals;
 704   __ std(R18_locals, state_(_locals));
 705 
 706   // state->_oop_temp = NULL;
 707   __ li(R0, 0);
 708   __ std(R0, state_(_oop_temp));
 709 
 710   // state->_last_Java_fp = *R1_SP // Use *R1_SP as fp
 711   __ ld(R0, _abi(callers_sp), R1_SP);
 712   __ std(R0, state_(_last_Java_fp));
 713 
 714   BLOCK_COMMENT("load Stack base:");
 715   {
 716     // Stack_base.
 717     // if (!method->synchronized()) {
 718     //   stack_base = state;
 719     // } else {
 720     //   stack_base = (uintptr_t)state - sizeof(BasicObjectLock);
 721     // }
 722     Label L;
 723     __ mr(stack_base, R14_state);
 724     __ bfalse(is_synced, L);
 725     __ addi(stack_base, stack_base, -frame::interpreter_frame_monitor_size_in_bytes());
 726     __ bind(L);
 727   }
 728 
 729   // state->_mdx = NULL;
 730   __ li(R0, 0);
 731   __ std(R0, state_(_mdx));
 732 
 733   {
 734     // if (method->is_native()) state->_bcp = NULL;
 735     // else state->_bcp = bytecode_addr;
 736     Label label1, label2;
 737     __ bfalse(is_native, label1);
 738     __ std(R0, state_(_bcp));
 739     __ b(label2);
 740     __ bind(label1);
 741     __ std(bytecode_addr, state_(_bcp));
 742     __ bind(label2);
 743   }
 744 
 745 
 746   // state->_result._to_call._callee = NULL;
 747   __ std(R0, state_(_result._to_call._callee));
 748 
 749   // state->_monitor_base = state;
 750   __ std(R14_state, state_(_monitor_base));
 751 
 752   // state->_msg = BytecodeInterpreter::method_entry;
 753   __ li(R0, BytecodeInterpreter::method_entry);
 754   __ stw(R0, state_(_msg));
 755 
 756   // state->_last_Java_sp = R1_SP;
 757   __ std(R1_SP, state_(_last_Java_sp));
 758 
 759   // state->_stack_base = stack_base;
 760   __ std(stack_base, state_(_stack_base));
 761 
 762   // tos = stack_base - 1 slot (prepushed);
 763   // state->_stack.Tos(tos);
 764   __ addi(tos, stack_base, - Interpreter::stackElementSize);
 765   __ std(tos,  state_(_stack));
 766 
 767 
 768   {
 769     BLOCK_COMMENT("get last_Java_pc:");
 770     // if (!is_native) state->_last_Java_pc = <some_ip_in_this_code_buffer>;
 771     // else state->_last_Java_pc = NULL; (just for neatness)
 772     Label label1, label2;
 773     __ btrue(is_native, label1);
 774     __ get_PC_trash_LR(R0);
 775     __ std(R0, state_(_last_Java_pc));
 776     __ b(label2);
 777     __ bind(label1);
 778     __ li(R0, 0);
 779     __ std(R0, state_(_last_Java_pc));
 780     __ bind(label2);
 781   }
 782 
 783 
 784   // stack_limit = tos - max_stack;
 785   __ sub(R0, tos, max_stack);
 786   // state->_stack_limit = stack_limit;
 787   __ std(R0, state_(_stack_limit));
 788 
 789 
 790   // cache = method->constants()->cache();
 791    __ ld(R0, ConstantPool::cache_offset_in_bytes(), constants);
 792   // state->_constants = method->constants()->cache();
 793   __ std(R0, state_(_constants));
 794 
 795 
 796 
 797   //=============================================================================
 798   // synchronized method, allocate and initialize method object lock.
 799   // if (!method->is_synchronized()) goto fill_locals_with_0x0s;
 800   Label fill_locals_with_0x0s;
 801   __ bfalse(is_synced, fill_locals_with_0x0s);
 802 
 803   //   pool_holder = method->constants()->pool_holder();
 804   const int mirror_offset = in_bytes(Klass::java_mirror_offset());
 805   {
 806     Label label1, label2;
 807     // lockee = NULL; for java methods, correct value will be inserted in BytecodeInterpretMethod.hpp
 808     __ li(R0,0);
 809     __ bfalse(is_native, label2);
 810 
 811     __ bfalse(is_static, label1);
 812     // if (method->is_static()) lockee =
 813     // pool_holder->klass_part()->java_mirror();
 814     __ ld(R11_scratch1/*pool_holder*/, ConstantPool::pool_holder_offset_in_bytes(), constants);
 815     __ ld(R0/*lockee*/, mirror_offset, R11_scratch1/*pool_holder*/);
 816     __ b(label2);
 817 
 818     __ bind(label1);
 819     // else lockee = *(oop*)locals;
 820     __ ld(R0/*lockee*/, 0, R18_locals);
 821     __ bind(label2);
 822 
 823     // monitor->set_obj(lockee);
 824     __ std(R0/*lockee*/, BasicObjectLock::obj_offset_in_bytes(), stack_base);
 825   }
 826 
 827   // See if we need to zero the locals
 828   __ BIND(fill_locals_with_0x0s);
 829 
 830 
 831   //=============================================================================
 832   // fill locals with 0x0s
 833   Label locals_zeroed;
 834   __ btrue(is_native, locals_zeroed);
 835 
 836   if (true /* zerolocals */ || ClearInterpreterLocals) {
 837     // local_count is already num_locals_slots - num_param_slots
 838     __ sldi(R0, parameter_count, Interpreter::logStackElementSize);
 839     __ sub(local_addr, R18_locals, R0);
 840     __ cmpdi(CCR0, local_count, 0);
 841     __ ble(CCR0, locals_zeroed);
 842 
 843     __ mtctr(local_count);
 844     //__ ld_const_addr(R0, (address) 0xcafe0000babe);
 845     __ li(R0, 0);
 846 
 847     Label zero_slot;
 848     __ bind(zero_slot);
 849 
 850     // first local is at local_addr
 851     __ std(R0, 0, local_addr);
 852     __ addi(local_addr, local_addr, -BytesPerWord);
 853     __ bdnz(zero_slot);
 854   }
 855 
 856    __ BIND(locals_zeroed);
 857 
 858   }
 859   BLOCK_COMMENT("} compute_interpreter_state");
 860 }
 861 
 862 // Generate code to initiate compilation on invocation counter overflow.
 863 void CppInterpreterGenerator::generate_counter_overflow(Label& continue_entry) {
 864   // Registers alive
 865   //   R14_state
 866   //   R16_thread
 867   //
 868   // Registers updated
 869   //   R14_state
 870   //   R3_ARG1 (=R3_RET)
 871   //   R4_ARG2
 872 
 873   // After entering the vm we remove the activation and retry the
 874   // entry point in case the compilation is complete.
 875 
 876   // InterpreterRuntime::frequency_counter_overflow takes one argument
 877   // that indicates if the counter overflow occurs at a backwards
 878   // branch (NULL bcp). We pass zero. The call returns the address
 879   // of the verified entry point for the method or NULL if the
 880   // compilation did not complete (either went background or bailed
 881   // out).
 882   __ li(R4_ARG2, 0);
 883 
 884   // Pass false to call_VM so it doesn't check for pending exceptions,
 885   // since at this point in the method invocation the exception
 886   // handler would try to exit the monitor of synchronized methods
 887   // which haven't been entered yet.
 888   //
 889   // Returns verified_entry_point or NULL, we don't care which.
 890   //
 891   // Do not use the variant `frequency_counter_overflow' that returns
 892   // a structure, because this will change the argument list by a
 893   // hidden parameter (gcc 4.1).
 894 
 895   __ call_VM(noreg,
 896              CAST_FROM_FN_PTR(address, InterpreterRuntime::frequency_counter_overflow),
 897              R4_ARG2,
 898              false);
 899   // Returns verified_entry_point or NULL, we don't care which as we ignore it
 900   // and run interpreted.
 901 
 902   // Reload method, it may have moved.
 903   __ ld(R19_method, state_(_method));
 904 
 905   // We jump now to the label "continue_after_compile".
 906   __ b(continue_entry);
 907 }
 908 
 909 // Increment invocation count and check for overflow.
 910 //
 911 // R19_method must contain Method* of method to profile.
 912 void CppInterpreterGenerator::generate_counter_incr(Label& overflow) {
 913   Label done;
 914   const Register Rcounters             = R12_scratch2;
 915   const Register iv_be_count           = R11_scratch1;
 916   const Register invocation_limit      = R12_scratch2;
 917   const Register invocation_limit_addr = invocation_limit;
 918 
 919   // Load and ev. allocate MethodCounters object.
 920   __ get_method_counters(R19_method, Rcounters, done);
 921 
 922   // Update standard invocation counters.
 923   __ increment_invocation_counter(Rcounters, iv_be_count, R0);
 924 
 925   // Compare against limit.
 926   BLOCK_COMMENT("Compare counter against limit:");
 927   assert(4 == sizeof(InvocationCounter::InterpreterInvocationLimit),
 928          "must be 4 bytes");
 929   __ load_const(invocation_limit_addr, (address)&InvocationCounter::InterpreterInvocationLimit);
 930   __ lwa(invocation_limit, 0, invocation_limit_addr);
 931   __ cmpw(CCR0, iv_be_count, invocation_limit);
 932   __ bge(CCR0, overflow);
 933   __ bind(done);
 934 }
 935 
 936 //
 937 // Call a JNI method.
 938 //
 939 // Interpreter stub for calling a native method. (C++ interpreter)
 940 // This sets up a somewhat different looking stack for calling the native method
 941 // than the typical interpreter frame setup.
 942 //
 943 address CppInterpreterGenerator::generate_native_entry(void) {
 944   if (native_entry != NULL) return native_entry;
 945   address entry = __ pc();
 946 
 947   // Read
 948   //   R16_thread
 949   //   R15_prev_state  - address of caller's BytecodeInterpreter, if this snippet
 950   //                     gets called by the frame manager.
 951   //   R19_method      - callee's Method
 952   //   R17_tos         - address of caller's tos
 953   //   R1_SP           - caller's stack pointer
 954   //   R21_sender_SP   - initial caller sp
 955   //
 956   // Update
 957   //   R14_state       - address of caller's BytecodeInterpreter
 958   //   R3_RET          - integer result, if any.
 959   //   F1_RET          - float result, if any.
 960   //
 961   //
 962   // Stack layout at this point:
 963   //
 964   //    0       [TOP_IJAVA_FRAME_ABI]         <-- R1_SP
 965   //            alignment (optional)
 966   //            [outgoing Java arguments]     <-- R17_tos
 967   //            ...
 968   //    PARENT  [PARENT_IJAVA_FRAME_ABI]
 969   //            ...
 970   //
 971 
 972   const bool inc_counter = UseCompiler || CountCompiledCalls;
 973 
 974   const Register signature_handler_fd   = R21_tmp1;
 975   const Register pending_exception      = R22_tmp2;
 976   const Register result_handler_addr    = R23_tmp3;
 977   const Register native_method_fd       = R24_tmp4;
 978   const Register access_flags           = R25_tmp5;
 979   const Register active_handles         = R26_tmp6;
 980   const Register sync_state             = R27_tmp7;
 981   const Register sync_state_addr        = sync_state;     // Address is dead after use.
 982   const Register suspend_flags          = R24_tmp4;
 983 
 984   const Register return_pc              = R28_tmp8;       // Register will be locked for some time.
 985 
 986   const ConditionRegister is_synced     = CCR4_is_synced; // Live-on-exit from compute_interpreter_state.
 987 
 988 
 989   // R1_SP still points to caller's SP at this point.
 990 
 991   // Save initial_caller_sp to caller's abi. The caller frame must be
 992   // resized before returning to get rid of the c2i arguments (if
 993   // any).
 994   // Override the saved SP with the senderSP so we can pop c2i
 995   // arguments (if any) off when we return
 996   __ std(R21_sender_SP, _top_ijava_frame_abi(initial_caller_sp), R1_SP);
 997 
 998   // Save LR to caller's frame. We don't use _abi(lr) here, because it is not safe.
 999   __ mflr(return_pc);
1000   __ std(return_pc, _top_ijava_frame_abi(frame_manager_lr), R1_SP);
1001 
1002   assert(return_pc->is_nonvolatile(), "return_pc must be a non-volatile register");
1003 
1004   __ verify_method_ptr(R19_method);
1005 
1006   //=============================================================================
1007 
1008   // If this snippet gets called by the frame manager (at label
1009   // `call_special'), then R15_prev_state is valid. If this snippet
1010   // is not called by the frame manager, but e.g. by the call stub or
1011   // by compiled code, then R15_prev_state is invalid.
1012   {
1013     // Set R15_prev_state to 0 if we don't return to the frame
1014     // manager; we will return to the call_stub or to compiled code
1015     // instead. If R15_prev_state is 0 there will be only one
1016     // interpreter frame (we will set this up later) in this C frame!
1017     // So we must take care about retrieving prev_state_(_prev_link)
1018     // and restoring R1_SP when popping that interpreter.
1019     Label prev_state_is_valid;
1020 
1021     __ load_const(R11_scratch1/*frame_manager_returnpc_addr*/, (address)&frame_manager_specialized_return);
1022     __ ld(R12_scratch2/*frame_manager_returnpc*/, 0, R11_scratch1/*frame_manager_returnpc_addr*/);
1023     __ cmpd(CCR0, return_pc, R12_scratch2/*frame_manager_returnpc*/);
1024     __ beq(CCR0, prev_state_is_valid);
1025 
1026     __ li(R15_prev_state, 0);
1027 
1028     __ BIND(prev_state_is_valid);
1029   }
1030 
1031   //=============================================================================
1032   // Allocate new frame and initialize interpreter state.
1033 
1034   Label exception_return;
1035   Label exception_return_sync_check;
1036   Label stack_overflow_return;
1037 
1038   // Generate new interpreter state and jump to stack_overflow_return in case of
1039   // a stack overflow.
1040   generate_compute_interpreter_state(stack_overflow_return);
1041 
1042   //=============================================================================
1043   // Increment invocation counter. On overflow, entry to JNI method
1044   // will be compiled.
1045   Label invocation_counter_overflow;
1046   if (inc_counter) {
1047     generate_counter_incr(invocation_counter_overflow);
1048   }
1049 
1050   Label continue_after_compile;
1051   __ BIND(continue_after_compile);
1052 
1053   // access_flags = method->access_flags();
1054   // Load access flags.
1055   assert(access_flags->is_nonvolatile(),
1056          "access_flags must be in a non-volatile register");
1057   // Type check.
1058   // TODO: PPC port: assert(4 == methodOopDesc::sz_access_flags(), "unexpected field size");
1059   __ lwz(access_flags, method_(access_flags));
1060 
1061   // We don't want to reload R19_method and access_flags after calls
1062   // to some helper functions.
1063   assert(R19_method->is_nonvolatile(), "R19_method must be a non-volatile register");
1064 
1065   // Check for synchronized methods. Must happen AFTER invocation counter
1066   // check, so method is not locked if counter overflows.
1067 
1068   {
1069     Label method_is_not_synced;
1070     // Is_synced is still alive.
1071     assert(is_synced->is_nonvolatile(), "is_synced must be non-volatile");
1072     __ bfalse(is_synced, method_is_not_synced);
1073 
1074     lock_method();
1075     // Reload method, it may have moved.
1076     __ ld(R19_method, state_(_method));
1077 
1078     __ BIND(method_is_not_synced);
1079   }
1080 
1081   // jvmti/jvmpi support
1082   __ notify_method_entry();
1083 
1084   // Reload method, it may have moved.
1085   __ ld(R19_method, state_(_method));
1086 
1087   //=============================================================================
1088   // Get and call the signature handler
1089 
1090   __ ld(signature_handler_fd, method_(signature_handler));
1091   Label call_signature_handler;
1092 
1093   __ cmpdi(CCR0, signature_handler_fd, 0);
1094   __ bne(CCR0, call_signature_handler);
1095 
1096   // Method has never been called. Either generate a specialized
1097   // handler or point to the slow one.
1098   //
1099   // Pass parameter 'false' to avoid exception check in call_VM.
1100   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::prepare_native_call), R19_method, false);
1101 
1102   // Check for an exception while looking up the target method. If we
1103   // incurred one, bail.
1104   __ ld(pending_exception, thread_(pending_exception));
1105   __ cmpdi(CCR0, pending_exception, 0);
1106   __ bne(CCR0, exception_return_sync_check); // has pending exception
1107 
1108   // reload method
1109   __ ld(R19_method, state_(_method));
1110 
1111   // Reload signature handler, it may have been created/assigned in the meanwhile
1112   __ ld(signature_handler_fd, method_(signature_handler));
1113 
1114   __ BIND(call_signature_handler);
1115 
1116   // Before we call the signature handler we push a new frame to
1117   // protect the interpreter frame volatile registers when we return
1118   // from jni but before we can get back to Java.
1119 
1120   // First set the frame anchor while the SP/FP registers are
1121   // convenient and the slow signature handler can use this same frame
1122   // anchor.
1123 
1124   // We have a TOP_IJAVA_FRAME here, which belongs to us.
1125   __ set_top_ijava_frame_at_SP_as_last_Java_frame(R1_SP, R12_scratch2/*tmp*/);
1126 
1127   // Now the interpreter frame (and its call chain) have been
1128   // invalidated and flushed. We are now protected against eager
1129   // being enabled in native code. Even if it goes eager the
1130   // registers will be reloaded as clean and we will invalidate after
1131   // the call so no spurious flush should be possible.
1132 
1133   // Call signature handler and pass locals address.
1134   //
1135   // Our signature handlers copy required arguments to the C stack
1136   // (outgoing C args), R3_ARG1 to R10_ARG8, and F1_ARG1 to
1137   // F13_ARG13.
1138   __ mr(R3_ARG1, R18_locals);
1139   __ ld(signature_handler_fd, 0, signature_handler_fd);
1140   __ call_stub(signature_handler_fd);
1141   // reload method
1142   __ ld(R19_method, state_(_method));
1143 
1144   // Remove the register parameter varargs slots we allocated in
1145   // compute_interpreter_state. SP+16 ends up pointing to the ABI
1146   // outgoing argument area.
1147   //
1148   // Not needed on PPC64.
1149   //__ add(SP, SP, Argument::n_register_parameters*BytesPerWord);
1150 
1151   assert(result_handler_addr->is_nonvolatile(), "result_handler_addr must be in a non-volatile register");
1152   // Save across call to native method.
1153   __ mr(result_handler_addr, R3_RET);
1154 
1155   // Set up fixed parameters and call the native method.
1156   // If the method is static, get mirror into R4_ARG2.
1157 
1158   {
1159     Label method_is_not_static;
1160     // access_flags is non-volatile and still, no need to restore it
1161 
1162     // restore access flags
1163     __ testbitdi(CCR0, R0, access_flags, JVM_ACC_STATIC_BIT);
1164     __ bfalse(CCR0, method_is_not_static);
1165 
1166     // constants = method->constants();
1167     __ ld(R11_scratch1, in_bytes(Method::const_offset()), R19_method);
1168     __ ld(R11_scratch1/*constants*/, in_bytes(ConstMethod::constants_offset()), R11_scratch1);
1169     // pool_holder = method->constants()->pool_holder();
1170     __ ld(R11_scratch1/*pool_holder*/, ConstantPool::pool_holder_offset_in_bytes(),
1171           R11_scratch1/*constants*/);
1172 
1173     const int mirror_offset = in_bytes(Klass::java_mirror_offset());
1174 
1175     // mirror = pool_holder->klass_part()->java_mirror();
1176     __ ld(R0/*mirror*/, mirror_offset, R11_scratch1/*pool_holder*/);
1177     // state->_native_mirror = mirror;
1178     __ std(R0/*mirror*/, state_(_oop_temp));
1179     // R4_ARG2 = &state->_oop_temp;
1180     __ addir(R4_ARG2, state_(_oop_temp));
1181 
1182     __ BIND(method_is_not_static);
1183   }
1184 
1185   // At this point, arguments have been copied off the stack into
1186   // their JNI positions. Oops are boxed in-place on the stack, with
1187   // handles copied to arguments. The result handler address is in a
1188   // register.
1189 
1190   // pass JNIEnv address as first parameter
1191   __ addir(R3_ARG1, thread_(jni_environment));
1192 
1193   // Load the native_method entry before we change the thread state.
1194   __ ld(native_method_fd, method_(native_function));
1195 
1196   //=============================================================================
1197   // Transition from _thread_in_Java to _thread_in_native. As soon as
1198   // we make this change the safepoint code needs to be certain that
1199   // the last Java frame we established is good. The pc in that frame
1200   // just needs to be near here not an actual return address.
1201 
1202   // We use release_store_fence to update values like the thread state, where
1203   // we don't want the current thread to continue until all our prior memory
1204   // accesses (including the new thread state) are visible to other threads.
1205   __ li(R0, _thread_in_native);
1206   __ release();
1207 
1208   // TODO: PPC port: assert(4 == JavaThread::sz_thread_state(), "unexpected field size");
1209   __ stw(R0, thread_(thread_state));
1210 
1211   if (UseMembar) {
1212     __ fence();
1213   }
1214 
1215   //=============================================================================
1216   // Call the native method. Argument registers must not have been
1217   // overwritten since "__ call_stub(signature_handler);" (except for
1218   // ARG1 and ARG2 for static methods)
1219   __ call_c(native_method_fd);
1220 
1221   __ std(R3_RET, state_(_native_lresult));
1222   __ stfd(F1_RET, state_(_native_fresult));
1223 
1224   // The frame_manager_lr field, which we use for setting the last
1225   // java frame, gets overwritten by the signature handler. Restore
1226   // it now.
1227   __ get_PC_trash_LR(R11_scratch1);
1228   __ std(R11_scratch1, _top_ijava_frame_abi(frame_manager_lr), R1_SP);
1229 
1230   // Because of GC R19_method may no longer be valid.
1231 
1232   // Block, if necessary, before resuming in _thread_in_Java state.
1233   // In order for GC to work, don't clear the last_Java_sp until after
1234   // blocking.
1235 
1236 
1237 
1238   //=============================================================================
1239   // Switch thread to "native transition" state before reading the
1240   // synchronization state.  This additional state is necessary
1241   // because reading and testing the synchronization state is not
1242   // atomic w.r.t. GC, as this scenario demonstrates: Java thread A,
1243   // in _thread_in_native state, loads _not_synchronized and is
1244   // preempted.  VM thread changes sync state to synchronizing and
1245   // suspends threads for GC. Thread A is resumed to finish this
1246   // native method, but doesn't block here since it didn't see any
1247   // synchronization in progress, and escapes.
1248 
1249   // We use release_store_fence to update values like the thread state, where
1250   // we don't want the current thread to continue until all our prior memory
1251   // accesses (including the new thread state) are visible to other threads.
1252   __ li(R0/*thread_state*/, _thread_in_native_trans);
1253   __ release();
1254   __ stw(R0/*thread_state*/, thread_(thread_state));
1255   if (UseMembar) {
1256     __ fence();
1257   }
1258   // Write serialization page so that the VM thread can do a pseudo remote
1259   // membar. We use the current thread pointer to calculate a thread
1260   // specific offset to write to within the page. This minimizes bus
1261   // traffic due to cache line collision.
1262   else {
1263     __ serialize_memory(R16_thread, R11_scratch1, R12_scratch2);
1264   }
1265 
1266   // Now before we return to java we must look for a current safepoint
1267   // (a new safepoint can not start since we entered native_trans).
1268   // We must check here because a current safepoint could be modifying
1269   // the callers registers right this moment.
1270 
1271   // Acquire isn't strictly necessary here because of the fence, but
1272   // sync_state is declared to be volatile, so we do it anyway.
1273   __ load_const(sync_state_addr, SafepointSynchronize::address_of_state());
1274 
1275   // TODO: PPC port: assert(4 == SafepointSynchronize::sz_state(), "unexpected field size");
1276   __ lwz(sync_state, 0, sync_state_addr);
1277 
1278   // TODO: PPC port: assert(4 == Thread::sz_suspend_flags(), "unexpected field size");
1279   __ lwz(suspend_flags, thread_(suspend_flags));
1280 
1281   __ acquire();
1282 
1283   Label sync_check_done;
1284   Label do_safepoint;
1285   // No synchronization in progress nor yet synchronized
1286   __ cmpwi(CCR0, sync_state, SafepointSynchronize::_not_synchronized);
1287   // not suspended
1288   __ cmpwi(CCR1, suspend_flags, 0);
1289 
1290   __ bne(CCR0, do_safepoint);
1291   __ beq(CCR1, sync_check_done);
1292   __ bind(do_safepoint);
1293   // Block.  We do the call directly and leave the current
1294   // last_Java_frame setup undisturbed.  We must save any possible
1295   // native result acrosss the call. No oop is present
1296 
1297   __ mr(R3_ARG1, R16_thread);
1298   __ call_c(CAST_FROM_FN_PTR(FunctionDescriptor*, JavaThread::check_special_condition_for_native_trans),
1299             relocInfo::none);
1300   __ bind(sync_check_done);
1301 
1302   //=============================================================================
1303   // <<<<<< Back in Interpreter Frame >>>>>
1304 
1305   // We are in thread_in_native_trans here and back in the normal
1306   // interpreter frame. We don't have to do anything special about
1307   // safepoints and we can switch to Java mode anytime we are ready.
1308 
1309   // Note: frame::interpreter_frame_result has a dependency on how the
1310   // method result is saved across the call to post_method_exit. For
1311   // native methods it assumes that the non-FPU/non-void result is
1312   // saved in _native_lresult and a FPU result in _native_fresult. If
1313   // this changes then the interpreter_frame_result implementation
1314   // will need to be updated too.
1315 
1316   // On PPC64, we have stored the result directly after the native call.
1317 
1318   //=============================================================================
1319   // back in Java
1320 
1321   // We use release_store_fence to update values like the thread state, where
1322   // we don't want the current thread to continue until all our prior memory
1323   // accesses (including the new thread state) are visible to other threads.
1324   __ li(R0/*thread_state*/, _thread_in_Java);
1325   __ release();
1326   __ stw(R0/*thread_state*/, thread_(thread_state));
1327   if (UseMembar) {
1328     __ fence();
1329   }
1330 
1331   __ reset_last_Java_frame();
1332 
1333   // Reload GR27_method, call killed it. We can't look at
1334   // state->_method until we're back in java state because in java
1335   // state gc can't happen until we get to a safepoint.
1336   //
1337   // We've set thread_state to _thread_in_Java already, so restoring
1338   // R19_method from R14_state works; R19_method is invalid, because
1339   // GC may have happened.
1340   __ ld(R19_method, state_(_method)); // reload method, may have moved
1341 
1342   // jvmdi/jvmpi support. Whether we've got an exception pending or
1343   // not, and whether unlocking throws an exception or not, we notify
1344   // on native method exit. If we do have an exception, we'll end up
1345   // in the caller's context to handle it, so if we don't do the
1346   // notify here, we'll drop it on the floor.
1347 
1348   __ notify_method_exit(true/*native method*/,
1349                         ilgl /*illegal state (not used for native methods)*/);
1350 
1351 
1352 
1353   //=============================================================================
1354   // Handle exceptions
1355 
1356   // See if we must unlock.
1357   //
1358   {
1359     Label method_is_not_synced;
1360     // is_synced is still alive
1361     assert(is_synced->is_nonvolatile(), "is_synced must be non-volatile");
1362     __ bfalse(is_synced, method_is_not_synced);
1363 
1364     unlock_method();
1365 
1366     __ bind(method_is_not_synced);
1367   }
1368 
1369   // Reset active handles after returning from native.
1370   // thread->active_handles()->clear();
1371   __ ld(active_handles, thread_(active_handles));
1372   // JNIHandleBlock::_top is an int.
1373   // TODO:  PPC port: assert(4 == JNIHandleBlock::top_size_in_bytes(), "unexpected field size");
1374   __ li(R0, 0);
1375   __ stw(R0, JNIHandleBlock::top_offset_in_bytes(), active_handles);
1376 
1377   Label no_pending_exception_from_native_method;
1378   __ ld(R0/*pending_exception*/, thread_(pending_exception));
1379   __ cmpdi(CCR0, R0/*pending_exception*/, 0);
1380   __ beq(CCR0, no_pending_exception_from_native_method);
1381 
1382 
1383   //-----------------------------------------------------------------------------
1384   // An exception is pending. We call into the runtime only if the
1385   // caller was not interpreted. If it was interpreted the
1386   // interpreter will do the correct thing. If it isn't interpreted
1387   // (call stub/compiled code) we will change our return and continue.
1388   __ BIND(exception_return);
1389 
1390   Label return_to_initial_caller_with_pending_exception;
1391   __ cmpdi(CCR0, R15_prev_state, 0);
1392   __ beq(CCR0, return_to_initial_caller_with_pending_exception);
1393 
1394   // We are returning to an interpreter activation, just pop the state,
1395   // pop our frame, leave the exception pending, and return.
1396   __ pop_interpreter_state(/*prev_state_may_be_0=*/false);
1397   __ pop_interpreter_frame(R11_scratch1, R12_scratch2, R21_tmp1 /* set to return pc */, R22_tmp2);
1398   __ mtlr(R21_tmp1);
1399   __ blr();
1400 
1401   __ BIND(exception_return_sync_check);
1402 
1403   assert(is_synced->is_nonvolatile(), "is_synced must be non-volatile");
1404   __ bfalse(is_synced, exception_return);
1405   unlock_method();
1406   __ b(exception_return);
1407 
1408 
1409   __ BIND(return_to_initial_caller_with_pending_exception);
1410   // We are returning to a c2i-adapter / call-stub, get the address of the
1411   // exception handler, pop the frame and return to the handler.
1412 
1413   // First, pop to caller's frame.
1414   __ pop_interpreter_frame(R11_scratch1, R12_scratch2, R21_tmp1  /* set to return pc */, R22_tmp2);
1415 
1416   __ push_frame_abi112(0, R11_scratch1);
1417   // Get the address of the exception handler.
1418   __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address),
1419                   R16_thread,
1420                   R21_tmp1 /* return pc */);
1421   __ pop_frame();
1422 
1423   // Load the PC of the the exception handler into LR.
1424   __ mtlr(R3_RET);
1425 
1426   // Load exception into R3_ARG1 and clear pending exception in thread.
1427   __ ld(R3_ARG1/*exception*/, thread_(pending_exception));
1428   __ li(R4_ARG2, 0);
1429   __ std(R4_ARG2, thread_(pending_exception));
1430 
1431   // Load the original return pc into R4_ARG2.
1432   __ mr(R4_ARG2/*issuing_pc*/, R21_tmp1);
1433 
1434   // Resize frame to get rid of a potential extension.
1435   __ resize_frame_to_initial_caller(R11_scratch1, R12_scratch2);
1436 
1437   // Return to exception handler.
1438   __ blr();
1439 
1440 
1441   //-----------------------------------------------------------------------------
1442   // No exception pending.
1443   __ BIND(no_pending_exception_from_native_method);
1444 
1445   // Move native method result back into proper registers and return.
1446   // Invoke result handler (may unbox/promote).
1447   __ ld(R3_RET, state_(_native_lresult));
1448   __ lfd(F1_RET, state_(_native_fresult));
1449   __ call_stub(result_handler_addr);
1450 
1451   // We have created a new BytecodeInterpreter object, now we must destroy it.
1452   //
1453   // Restore previous R14_state and caller's SP.  R15_prev_state may
1454   // be 0 here, because our caller may be the call_stub or compiled
1455   // code.
1456   __ pop_interpreter_state(/*prev_state_may_be_0=*/true);
1457   __ pop_interpreter_frame(R11_scratch1, R12_scratch2, R21_tmp1 /* set to return pc */, R22_tmp2);
1458   // Resize frame to get rid of a potential extension.
1459   __ resize_frame_to_initial_caller(R11_scratch1, R12_scratch2);
1460 
1461   // Must use the return pc which was loaded from the caller's frame
1462   // as the VM uses return-pc-patching for deoptimization.
1463   __ mtlr(R21_tmp1);
1464   __ blr();
1465 
1466 
1467 
1468   //=============================================================================
1469   // We encountered an exception while computing the interpreter
1470   // state, so R14_state isn't valid. Act as if we just returned from
1471   // the callee method with a pending exception.
1472   __ BIND(stack_overflow_return);
1473 
1474   //
1475   // Register state:
1476   //   R14_state         invalid; trashed by compute_interpreter_state
1477   //   R15_prev_state    valid, but may be 0
1478   //
1479   //   R1_SP             valid, points to caller's SP; wasn't yet updated by
1480   //                     compute_interpreter_state
1481   //
1482 
1483   // Create exception oop and make it pending.
1484 
1485   // Throw the exception via RuntimeStub "throw_StackOverflowError_entry".
1486   //
1487   // Previously, we called C-Code directly. As a consequence, a
1488   // possible GC tried to process the argument oops of the top frame
1489   // (see RegisterMap::clear, which sets the corresponding flag to
1490   // true). This lead to crashes because:
1491   //   1. The top register map did not contain locations for the argument registers
1492   //   2. The arguments are dead anyway, could be already overwritten in the worst case
1493   // Solution: Call via special runtime stub that pushes it's own
1494   // frame. This runtime stub has the flag "CodeBlob::caller_must_gc_arguments()"
1495   // set to "false", what prevents the dead arguments getting GC'd.
1496   //
1497   // 2 cases exist:
1498   // 1. We were called by the c2i adapter / call stub
1499   // 2. We were called by the frame manager
1500   //
1501   // Both cases are handled by this code:
1502   // 1. - initial_caller_sp was saved in both cases on entry, so it's safe to load it back even if it was not changed.
1503   //    - control flow will be:
1504   //      throw_stackoverflow_stub->VM->throw_stackoverflow_stub->forward_excep->excp_blob of caller method
1505   // 2. - control flow will be:
1506   //      throw_stackoverflow_stub->VM->throw_stackoverflow_stub->forward_excep->rethrow_excp_entry of frame manager->resume_method
1507   //      Since we restored the caller SP above, the rethrow_excp_entry can restore the original interpreter state
1508   //      registers using the stack and resume the calling method with a pending excp.
1509 
1510   // Pop any c2i extension from the stack, restore LR just to be sure
1511   __ ld(R0, _top_ijava_frame_abi(frame_manager_lr), R1_SP);
1512   __ mtlr(R0);
1513   // Resize frame to get rid of a potential extension.
1514   __ resize_frame_to_initial_caller(R11_scratch1, R12_scratch2);
1515 
1516   // Load target address of the runtime stub.
1517   __ load_const(R12_scratch2, (StubRoutines::throw_StackOverflowError_entry()));
1518   __ mtctr(R12_scratch2);
1519   __ bctr();
1520 
1521 
1522   //=============================================================================
1523   // Counter overflow.
1524 
1525   if (inc_counter) {
1526     // Handle invocation counter overflow
1527     __ bind(invocation_counter_overflow);
1528 
1529     generate_counter_overflow(continue_after_compile);
1530   }
1531 
1532   native_entry = entry;
1533   return entry;
1534 }
1535 
1536 bool AbstractInterpreter::can_be_compiled(methodHandle m) {
1537   // No special entry points that preclude compilation.
1538   return true;
1539 }
1540 
1541 // Unlock the current method.
1542 //
1543 void CppInterpreterGenerator::unlock_method(void) {
1544   // Find preallocated monitor and unlock method. Method monitor is
1545   // the first one.
1546 
1547   // Registers alive
1548   //   R14_state
1549   //
1550   // Registers updated
1551   //   volatiles
1552   //
1553   const Register monitor = R4_ARG2;
1554 
1555   // Pass address of initial monitor we allocated.
1556   //
1557   // First monitor.
1558   __ addi(monitor, R14_state, -frame::interpreter_frame_monitor_size_in_bytes());
1559 
1560   // Unlock method
1561   __ unlock_object(monitor);
1562 }
1563 
1564 // Lock the current method.
1565 //
1566 void CppInterpreterGenerator::lock_method(void) {
1567   // Find preallocated monitor and lock method. Method monitor is the
1568   // first one.
1569 
1570   //
1571   // Registers alive
1572   //   R14_state
1573   //
1574   // Registers updated
1575   //   volatiles
1576   //
1577 
1578   const Register monitor = R4_ARG2;
1579   const Register object  = R5_ARG3;
1580 
1581   // Pass address of initial monitor we allocated.
1582   __ addi(monitor, R14_state, -frame::interpreter_frame_monitor_size_in_bytes());
1583 
1584   // Pass object address.
1585   __ ld(object, BasicObjectLock::obj_offset_in_bytes(), monitor);
1586 
1587   // Lock method.
1588   __ lock_object(monitor, object);
1589 }
1590 
1591 // Generate code for handling resuming a deopted method.
1592 void CppInterpreterGenerator::generate_deopt_handling(Register result_index) {
1593 
1594   //=============================================================================
1595   // Returning from a compiled method into a deopted method. The
1596   // bytecode at the bcp has completed. The result of the bytecode is
1597   // in the native abi (the tosca for the template based
1598   // interpreter). Any stack space that was used by the bytecode that
1599   // has completed has been removed (e.g. parameters for an invoke) so
1600   // all that we have to do is place any pending result on the
1601   // expression stack and resume execution on the next bytecode.
1602 
1603   Label return_from_deopt_common;
1604 
1605   // R3_RET and F1_RET are live here! Load the array index of the
1606   // required result stub address and continue at return_from_deopt_common.
1607 
1608   // Deopt needs to jump to here to enter the interpreter (return a result).
1609   deopt_frame_manager_return_atos = __ pc();
1610   __ li(result_index, AbstractInterpreter::BasicType_as_index(T_OBJECT));
1611   __ b(return_from_deopt_common);
1612 
1613   deopt_frame_manager_return_btos = __ pc();
1614   __ li(result_index, AbstractInterpreter::BasicType_as_index(T_BOOLEAN));
1615   __ b(return_from_deopt_common);
1616 
1617   deopt_frame_manager_return_itos = __ pc();
1618   __ li(result_index, AbstractInterpreter::BasicType_as_index(T_INT));
1619   __ b(return_from_deopt_common);
1620 
1621   deopt_frame_manager_return_ltos = __ pc();
1622   __ li(result_index, AbstractInterpreter::BasicType_as_index(T_LONG));
1623   __ b(return_from_deopt_common);
1624 
1625   deopt_frame_manager_return_ftos = __ pc();
1626   __ li(result_index, AbstractInterpreter::BasicType_as_index(T_FLOAT));
1627   __ b(return_from_deopt_common);
1628 
1629   deopt_frame_manager_return_dtos = __ pc();
1630   __ li(result_index, AbstractInterpreter::BasicType_as_index(T_DOUBLE));
1631   __ b(return_from_deopt_common);
1632 
1633   deopt_frame_manager_return_vtos = __ pc();
1634   __ li(result_index, AbstractInterpreter::BasicType_as_index(T_VOID));
1635   // Last one, fall-through to return_from_deopt_common.
1636 
1637   // Deopt return common. An index is present that lets us move any
1638   // possible result being return to the interpreter's stack.
1639   //
1640   __ BIND(return_from_deopt_common);
1641 
1642 }
1643 
1644 // Generate the code to handle a more_monitors message from the c++ interpreter.
1645 void CppInterpreterGenerator::generate_more_monitors() {
1646 
1647   //
1648   // Registers alive
1649   //   R16_thread      - JavaThread*
1650   //   R15_prev_state  - previous BytecodeInterpreter or 0
1651   //   R14_state       - BytecodeInterpreter* address of receiver's interpreter state
1652   //   R1_SP           - old stack pointer
1653   //
1654   // Registers updated
1655   //   R1_SP          - new stack pointer
1656   //
1657 
1658   // Very-local scratch registers.
1659   const Register old_tos         = R21_tmp1;
1660   const Register new_tos         = R22_tmp2;
1661   const Register stack_base      = R23_tmp3;
1662   const Register stack_limit     = R24_tmp4;
1663   const Register slot            = R25_tmp5;
1664   const Register n_slots         = R25_tmp5;
1665 
1666   // Interpreter state fields.
1667   const Register msg             = R24_tmp4;
1668 
1669   // Load up relevant interpreter state.
1670 
1671   __ ld(stack_base, state_(_stack_base));                // Old stack_base
1672   __ ld(old_tos, state_(_stack));                        // Old tos
1673   __ ld(stack_limit, state_(_stack_limit));              // Old stack_limit
1674 
1675   // extracted monitor_size
1676   int monitor_size = frame::interpreter_frame_monitor_size_in_bytes();
1677   assert(Assembler::is_aligned((unsigned int)monitor_size,
1678                                (unsigned int)frame::alignment_in_bytes),
1679          "size of a monitor must respect alignment of SP");
1680 
1681   // Save and restore top LR
1682   __ ld(R12_scratch2, _top_ijava_frame_abi(frame_manager_lr), R1_SP);
1683   __ resize_frame(-monitor_size, R11_scratch1);// Allocate space for new monitor
1684   __ std(R12_scratch2, _top_ijava_frame_abi(frame_manager_lr), R1_SP);
1685     // Initial_caller_sp is used as unextended_sp for non initial callers.
1686   __ std(R1_SP, _top_ijava_frame_abi(initial_caller_sp), R1_SP);
1687   __ addi(stack_base, stack_base, -monitor_size);        // New stack_base
1688   __ addi(new_tos, old_tos, -monitor_size);              // New tos
1689   __ addi(stack_limit, stack_limit, -monitor_size);      // New stack_limit
1690 
1691   __ std(R1_SP, state_(_last_Java_sp));                  // Update frame_bottom
1692 
1693   __ std(stack_base, state_(_stack_base));               // Update stack_base
1694   __ std(new_tos, state_(_stack));                       // Update tos
1695   __ std(stack_limit, state_(_stack_limit));             // Update stack_limit
1696 
1697   __ li(msg, BytecodeInterpreter::got_monitors);         // Tell interpreter we allocated the lock
1698   __ stw(msg, state_(_msg));
1699 
1700   // Shuffle expression stack down. Recall that stack_base points
1701   // just above the new expression stack bottom. Old_tos and new_tos
1702   // are used to scan thru the old and new expression stacks.
1703 
1704   Label copy_slot, copy_slot_finished;
1705   __ sub(n_slots, stack_base, new_tos);
1706   __ srdi_(n_slots, n_slots, LogBytesPerWord);           // compute number of slots to copy
1707   assert(LogBytesPerWord == 3, "conflicts assembler instructions");
1708   __ beq(CCR0, copy_slot_finished);                       // nothing to copy
1709 
1710   __ mtctr(n_slots);
1711 
1712   // loop
1713   __ bind(copy_slot);
1714   __ ldu(slot, BytesPerWord, old_tos);                   // slot = *++old_tos;
1715   __ stdu(slot, BytesPerWord, new_tos);                  // *++new_tos = slot;
1716   __ bdnz(copy_slot);
1717 
1718   __ bind(copy_slot_finished);
1719 
1720   // Restart interpreter
1721   __ li(R0, 0);
1722   __ std(R0, BasicObjectLock::obj_offset_in_bytes(), stack_base);  // Mark lock as unused
1723 }
1724 
1725 address CppInterpreterGenerator::generate_normal_entry(void) {
1726   if (interpreter_frame_manager != NULL) return interpreter_frame_manager;
1727 
1728   address entry = __ pc();
1729 
1730   address return_from_native_pc = (address) NULL;
1731 
1732   // Initial entry to frame manager (from call_stub or c2i_adapter)
1733 
1734   //
1735   // Registers alive
1736   //   R16_thread               - JavaThread*
1737   //   R19_method               - callee's Method (method to be invoked)
1738   //   R17_tos                  - address of sender tos (prepushed)
1739   //   R1_SP                    - SP prepared by call stub such that caller's outgoing args are near top
1740   //   LR                       - return address to caller (call_stub or c2i_adapter)
1741   //   R21_sender_SP            - initial caller sp
1742   //
1743   // Registers updated
1744   //   R15_prev_state           - 0
1745   //
1746   // Stack layout at this point:
1747   //
1748   //   0       [TOP_IJAVA_FRAME_ABI]         <-- R1_SP
1749   //           alignment (optional)
1750   //           [outgoing Java arguments]     <-- R17_tos
1751   //           ...
1752   //   PARENT  [PARENT_IJAVA_FRAME_ABI]
1753   //           ...
1754   //
1755 
1756   // Save initial_caller_sp to caller's abi.
1757   // The caller frame must be resized before returning to get rid of
1758   // the c2i part on top of the calling compiled frame (if any).
1759   // R21_tmp1 must match sender_sp in gen_c2i_adapter.
1760   // Now override the saved SP with the senderSP so we can pop c2i
1761   // arguments (if any) off when we return.
1762   __ std(R21_sender_SP, _top_ijava_frame_abi(initial_caller_sp), R1_SP);
1763 
1764   // Save LR to caller's frame. We don't use _abi(lr) here,
1765   // because it is not safe.
1766   __ mflr(R0);
1767   __ std(R0, _top_ijava_frame_abi(frame_manager_lr), R1_SP);
1768 
1769   // If we come here, it is the first invocation of the frame manager.
1770   // So there is no previous interpreter state.
1771   __ li(R15_prev_state, 0);
1772 
1773 
1774   // Fall through to where "recursive" invocations go.
1775 
1776   //=============================================================================
1777   // Dispatch an instance of the interpreter. Recursive activations
1778   // come here.
1779 
1780   Label re_dispatch;
1781   __ BIND(re_dispatch);
1782 
1783   //
1784   // Registers alive
1785   //    R16_thread        - JavaThread*
1786   //    R19_method        - callee's Method
1787   //    R17_tos           - address of caller's tos (prepushed)
1788   //    R15_prev_state    - address of caller's BytecodeInterpreter or 0
1789   //    R1_SP             - caller's SP trimmed such that caller's outgoing args are near top.
1790   //
1791   // Stack layout at this point:
1792   //
1793   //   0       [TOP_IJAVA_FRAME_ABI]
1794   //           alignment (optional)
1795   //           [outgoing Java arguments]
1796   //           ...
1797   //   PARENT  [PARENT_IJAVA_FRAME_ABI]
1798   //           ...
1799 
1800   // fall through to interpreted execution
1801 
1802   //=============================================================================
1803   // Allocate a new Java frame and initialize the new interpreter state.
1804 
1805   Label stack_overflow_return;
1806 
1807   // Create a suitable new Java frame plus a new BytecodeInterpreter instance
1808   // in the current (frame manager's) C frame.
1809   generate_compute_interpreter_state(stack_overflow_return);
1810 
1811   // fall through
1812 
1813   //=============================================================================
1814   // Interpreter dispatch.
1815 
1816   Label call_interpreter;
1817   __ BIND(call_interpreter);
1818 
1819   //
1820   // Registers alive
1821   //   R16_thread       - JavaThread*
1822   //   R15_prev_state   - previous BytecodeInterpreter or 0
1823   //   R14_state        - address of receiver's BytecodeInterpreter
1824   //   R1_SP            - receiver's stack pointer
1825   //
1826 
1827   // Thread fields.
1828   const Register pending_exception = R21_tmp1;
1829 
1830   // Interpreter state fields.
1831   const Register msg               = R24_tmp4;
1832 
1833   // MethodOop fields.
1834   const Register parameter_count   = R25_tmp5;
1835   const Register result_index      = R26_tmp6;
1836 
1837   const Register dummy             = R28_tmp8;
1838 
1839   // Address of various interpreter stubs.
1840   // R29_tmp9 is reserved.
1841   const Register stub_addr         = R27_tmp7;
1842 
1843   // Uncommon trap needs to jump to here to enter the interpreter
1844   // (re-execute current bytecode).
1845   unctrap_frame_manager_entry  = __ pc();
1846 
1847   // If we are profiling, store our fp (BSP) in the thread so we can
1848   // find it during a tick.
1849   if (Arguments::has_profile()) {
1850     // On PPC64 we store the pointer to the current BytecodeInterpreter,
1851     // instead of the bsp of ia64. This should suffice to be able to
1852     // find all interesting information.
1853     __ std(R14_state, thread_(last_interpreter_fp));
1854   }
1855 
1856   // R16_thread, R14_state and R15_prev_state are nonvolatile
1857   // registers. There is no need to save these. If we needed to save
1858   // some state in the current Java frame, this could be a place to do
1859   // so.
1860 
1861   // Call Java bytecode dispatcher passing "BytecodeInterpreter* istate".
1862   __ call_VM_leaf(CAST_FROM_FN_PTR(address,
1863                                    JvmtiExport::can_post_interpreter_events()
1864                                    ? BytecodeInterpreter::runWithChecks
1865                                    : BytecodeInterpreter::run),
1866                   R14_state);
1867 
1868   interpreter_return_address  = __ last_calls_return_pc();
1869 
1870   // R16_thread, R14_state and R15_prev_state have their values preserved.
1871 
1872   // If we are profiling, clear the fp in the thread to tell
1873   // the profiler that we are no longer in the interpreter.
1874   if (Arguments::has_profile()) {
1875     __ li(R11_scratch1, 0);
1876     __ std(R11_scratch1, thread_(last_interpreter_fp));
1877   }
1878 
1879   // Load message from bytecode dispatcher.
1880   // TODO: PPC port: guarantee(4 == BytecodeInterpreter::sz_msg(), "unexpected field size");
1881   __ lwz(msg, state_(_msg));
1882 
1883 
1884   Label more_monitors;
1885   Label return_from_native;
1886   Label return_from_native_common;
1887   Label return_from_native_no_exception;
1888   Label return_from_interpreted_method;
1889   Label return_from_recursive_activation;
1890   Label unwind_recursive_activation;
1891   Label resume_interpreter;
1892   Label return_to_initial_caller;
1893   Label unwind_initial_activation;
1894   Label unwind_initial_activation_pending_exception;
1895   Label call_method;
1896   Label call_special;
1897   Label retry_method;
1898   Label retry_method_osr;
1899   Label popping_frame;
1900   Label throwing_exception;
1901 
1902   // Branch according to the received message
1903 
1904   __ cmpwi(CCR1, msg, BytecodeInterpreter::call_method);
1905   __ cmpwi(CCR2, msg, BytecodeInterpreter::return_from_method);
1906 
1907   __ beq(CCR1, call_method);
1908   __ beq(CCR2, return_from_interpreted_method);
1909 
1910   __ cmpwi(CCR3, msg, BytecodeInterpreter::more_monitors);
1911   __ cmpwi(CCR4, msg, BytecodeInterpreter::throwing_exception);
1912 
1913   __ beq(CCR3, more_monitors);
1914   __ beq(CCR4, throwing_exception);
1915 
1916   __ cmpwi(CCR5, msg, BytecodeInterpreter::popping_frame);
1917   __ cmpwi(CCR6, msg, BytecodeInterpreter::do_osr);
1918 
1919   __ beq(CCR5, popping_frame);
1920   __ beq(CCR6, retry_method_osr);
1921 
1922   __ stop("bad message from interpreter");
1923 
1924 
1925   //=============================================================================
1926   // Add a monitor just below the existing one(s). State->_stack_base
1927   // points to the lowest existing one, so we insert the new one just
1928   // below it and shuffle the expression stack down. Ref. the above
1929   // stack layout picture, we must update _stack_base, _stack, _stack_limit
1930   // and _last_Java_sp in the interpreter state.
1931 
1932   __ BIND(more_monitors);
1933 
1934   generate_more_monitors();
1935   __ b(call_interpreter);
1936 
1937   generate_deopt_handling(result_index);
1938 
1939   // Restoring the R14_state is already done by the deopt_blob.
1940 
1941   // Current tos includes no parameter slots.
1942   __ ld(R17_tos, state_(_stack));
1943   __ li(msg, BytecodeInterpreter::deopt_resume);
1944   __ b(return_from_native_common);
1945 
1946   // We are sent here when we are unwinding from a native method or
1947   // adapter with an exception pending. We need to notify the interpreter
1948   // that there is an exception to process.
1949   // We arrive here also if the frame manager called an (interpreted) target
1950   // which returns with a StackOverflow exception.
1951   // The control flow is in this case is:
1952   // frame_manager->throw_excp_stub->forward_excp->rethrow_excp_entry
1953 
1954   AbstractInterpreter::_rethrow_exception_entry = __ pc();
1955 
1956   // Restore R14_state.
1957   __ ld(R14_state, 0, R1_SP);
1958   __ addi(R14_state, R14_state,
1959               -frame::interpreter_frame_cinterpreterstate_size_in_bytes());
1960 
1961   // Store exception oop into thread object.
1962   __ std(R3_RET, thread_(pending_exception));
1963   __ li(msg, BytecodeInterpreter::method_resume /*rethrow_exception*/);
1964   //
1965   // NOTE: the interpreter frame as setup be deopt does NOT include
1966   // any parameter slots (good thing since we have no callee here
1967   // and couldn't remove them) so we don't have to do any calculations
1968   // here to figure it out.
1969   //
1970   __ ld(R17_tos, state_(_stack));
1971   __ b(return_from_native_common);
1972 
1973 
1974   //=============================================================================
1975   // Returning from a native method.  Result is in the native abi
1976   // location so we must move it to the java expression stack.
1977 
1978   __ BIND(return_from_native);
1979   guarantee(return_from_native_pc == (address) NULL, "precondition");
1980   return_from_native_pc = __ pc();
1981 
1982   // Restore R14_state.
1983   __ ld(R14_state, 0, R1_SP);
1984   __ addi(R14_state, R14_state,
1985               -frame::interpreter_frame_cinterpreterstate_size_in_bytes());
1986 
1987   //
1988   // Registers alive
1989   //   R16_thread
1990   //   R14_state    - address of caller's BytecodeInterpreter.
1991   //   R3_RET       - integer result, if any.
1992   //   F1_RET       - float result, if any.
1993   //
1994   // Registers updated
1995   //   R19_method   - callee's Method
1996   //   R17_tos      - caller's tos, with outgoing args popped
1997   //   result_index - index of result handler.
1998   //   msg          - message for resuming interpreter.
1999   //
2000 
2001   // Very-local scratch registers.
2002 
2003   const ConditionRegister have_pending_exception = CCR0;
2004 
2005   // Load callee Method, gc may have moved it.
2006   __ ld(R19_method, state_(_result._to_call._callee));
2007 
2008   // Load address of caller's tos. includes parameter slots.
2009   __ ld(R17_tos, state_(_stack));
2010 
2011   // Pop callee's parameters.
2012 
2013   __ ld(parameter_count, in_bytes(Method::const_offset()), R19_method);
2014   __ lhz(parameter_count, in_bytes(ConstMethod::size_of_parameters_offset()), parameter_count);
2015   __ sldi(parameter_count, parameter_count, Interpreter::logStackElementSize);
2016   __ add(R17_tos, R17_tos, parameter_count);
2017 
2018   // Result stub address array index
2019   // TODO: PPC port: assert(4 == methodOopDesc::sz_result_index(), "unexpected field size");
2020   __ lwa(result_index, method_(result_index));
2021 
2022   __ li(msg, BytecodeInterpreter::method_resume);
2023 
2024   //
2025   // Registers alive
2026   //   R16_thread
2027   //   R14_state    - address of caller's BytecodeInterpreter.
2028   //   R17_tos      - address of caller's tos with outgoing args already popped
2029   //   R3_RET       - integer return value, if any.
2030   //   F1_RET       - float return value, if any.
2031   //   result_index - index of result handler.
2032   //   msg          - message for resuming interpreter.
2033   //
2034   // Registers updated
2035   //   R3_RET       - new address of caller's tos, including result, if any
2036   //
2037 
2038   __ BIND(return_from_native_common);
2039 
2040   // Check for pending exception
2041   __ ld(pending_exception, thread_(pending_exception));
2042   __ cmpdi(CCR0, pending_exception, 0);
2043   __ beq(CCR0, return_from_native_no_exception);
2044 
2045   // If there's a pending exception, we really have no result, so
2046   // R3_RET is dead. Resume_interpreter assumes the new tos is in
2047   // R3_RET.
2048   __ mr(R3_RET, R17_tos);
2049   // `resume_interpreter' expects R15_prev_state to be alive.
2050   __ ld(R15_prev_state, state_(_prev_link));
2051   __ b(resume_interpreter);
2052 
2053   __ BIND(return_from_native_no_exception);
2054 
2055   // No pending exception, copy method result from native ABI register
2056   // to tos.
2057 
2058   // Address of stub descriptor address array.
2059   __ load_const(stub_addr, CppInterpreter::tosca_result_to_stack());
2060 
2061   // Pass address of tos to stub.
2062   __ mr(R4_ARG2, R17_tos);
2063 
2064   // Address of stub descriptor address.
2065   __ sldi(result_index, result_index, LogBytesPerWord);
2066   __ add(stub_addr, stub_addr, result_index);
2067 
2068   // Stub descriptor address.
2069   __ ld(stub_addr, 0, stub_addr);
2070 
2071   // TODO: don't do this via a call, do it in place!
2072   //
2073   // call stub via descriptor
2074   // in R3_ARG1/F1_ARG1: result value (R3_RET or F1_RET)
2075   __ call_stub(stub_addr);
2076 
2077   // new tos = result of call in R3_RET
2078 
2079   // `resume_interpreter' expects R15_prev_state to be alive.
2080   __ ld(R15_prev_state, state_(_prev_link));
2081   __ b(resume_interpreter);
2082 
2083   //=============================================================================
2084   // We encountered an exception while computing the interpreter
2085   // state, so R14_state isn't valid. Act as if we just returned from
2086   // the callee method with a pending exception.
2087   __ BIND(stack_overflow_return);
2088 
2089   //
2090   // Registers alive
2091   //   R16_thread        - JavaThread*
2092   //   R1_SP             - old stack pointer
2093   //   R19_method        - callee's Method
2094   //   R17_tos           - address of caller's tos (prepushed)
2095   //   R15_prev_state    - address of caller's BytecodeInterpreter or 0
2096   //   R18_locals        - address of callee's locals array
2097   //
2098   // Registers updated
2099   //   R3_RET           - address of resuming tos, if recursive unwind
2100 
2101   Label Lskip_unextend_SP;
2102 
2103   {
2104   const ConditionRegister is_initial_call = CCR0;
2105   const Register tos_save = R21_tmp1;
2106   const Register tmp = R22_tmp2;
2107 
2108   assert(tos_save->is_nonvolatile(), "need a nonvolatile");
2109 
2110   // Is the exception thrown in the initial Java frame of this frame
2111   // manager frame?
2112   __ cmpdi(is_initial_call, R15_prev_state, 0);
2113   __ bne(is_initial_call, Lskip_unextend_SP);
2114 
2115   // Pop any c2i extension from the stack. This is necessary in the
2116   // non-recursive case (that is we were called by the c2i adapter,
2117   // meaning we have to prev state). In this case we entered the frame
2118   // manager through a special entry which pushes the orignal
2119   // unextended SP to the stack. Here we load it back.
2120   __ ld(R0, _top_ijava_frame_abi(frame_manager_lr), R1_SP);
2121   __ mtlr(R0);
2122   // Resize frame to get rid of a potential extension.
2123   __ resize_frame_to_initial_caller(R11_scratch1, R12_scratch2);
2124 
2125   // Fall through
2126 
2127   __ bind(Lskip_unextend_SP);
2128 
2129   // Throw the exception via RuntimeStub "throw_StackOverflowError_entry".
2130   //
2131   // Previously, we called C-Code directly. As a consequence, a
2132   // possible GC tried to process the argument oops of the top frame
2133   // (see RegisterMap::clear, which sets the corresponding flag to
2134   // true). This lead to crashes because:
2135   // 1. The top register map did not contain locations for the argument registers
2136   // 2. The arguments are dead anyway, could be already overwritten in the worst case
2137   // Solution: Call via special runtime stub that pushes it's own frame. This runtime stub has the flag
2138   // "CodeBlob::caller_must_gc_arguments()" set to "false", what prevents the dead arguments getting GC'd.
2139   //
2140   // 2 cases exist:
2141   // 1. We were called by the c2i adapter / call stub
2142   // 2. We were called by the frame manager
2143   //
2144   // Both cases are handled by this code:
2145   // 1. - initial_caller_sp was saved on stack => Load it back and we're ok
2146   //    - control flow will be:
2147   //      throw_stackoverflow_stub->VM->throw_stackoverflow_stub->forward_excep->excp_blob of calling method
2148   // 2. - control flow will be:
2149   //      throw_stackoverflow_stub->VM->throw_stackoverflow_stub->forward_excep->
2150   //        ->rethrow_excp_entry of frame manager->resume_method
2151   //      Since we restored the caller SP above, the rethrow_excp_entry can restore the original interpreter state
2152   //      registers using the stack and resume the calling method with a pending excp.
2153 
2154   __ load_const(R3_ARG1, (StubRoutines::throw_StackOverflowError_entry()));
2155   __ mtctr(R3_ARG1);
2156   __ bctr();
2157   }
2158   //=============================================================================
2159   // We have popped a frame from an interpreted call. We are assured
2160   // of returning to an interpreted call by the popframe abi. We have
2161   // no return value all we have to do is pop the current frame and
2162   // then make sure that the top of stack (of the caller) gets set to
2163   // where it was when we entered the callee (i.e. the args are still
2164   // in place).  Or we are returning to the interpreter. In the first
2165   // case we must extract result (if any) from the java expression
2166   // stack and store it in the location the native abi would expect
2167   // for a call returning this type. In the second case we must simply
2168   // do a stack to stack move as we unwind.
2169 
2170   __ BIND(popping_frame);
2171 
2172   // Registers alive
2173   //   R14_state
2174   //   R15_prev_state
2175   //   R17_tos
2176   //
2177   // Registers updated
2178   //   R19_method
2179   //   R3_RET
2180   //   msg
2181   {
2182     Label L;
2183 
2184     // Reload callee method, gc may have moved it.
2185     __ ld(R19_method, state_(_method));
2186 
2187     // We may be returning to a deoptimized frame in which case the
2188     // usual assumption of a recursive return is not true.
2189 
2190     // not equal = is recursive call
2191     __ cmpdi(CCR0, R15_prev_state, 0);
2192 
2193     __ bne(CCR0, L);
2194 
2195     // Pop_frame capability.
2196     // The pop_frame api says that the underlying frame is a Java frame, in this case
2197     // (prev_state==null) it must be a compiled frame:
2198     //
2199     // Stack at this point: I, C2I + C, ...
2200     //
2201     // The outgoing arguments of the call have just been copied (popframe_preserve_args).
2202     // By the pop_frame api, we must end up in an interpreted frame. So the compiled frame
2203     // will be deoptimized. Deoptimization will restore the outgoing arguments from
2204     // popframe_preserve_args, adjust the tos such that it includes the popframe_preserve_args,
2205     // and adjust the bci such that the call will be executed again.
2206     // We have no results, just pop the interpreter frame, resize the compiled frame to get rid
2207     // of the c2i extension and return to the deopt_handler.
2208     __ b(unwind_initial_activation);
2209 
2210     // is recursive call
2211     __ bind(L);
2212 
2213     // Resume_interpreter expects the original tos in R3_RET.
2214     __ ld(R3_RET, prev_state_(_stack));
2215 
2216     // We're done.
2217     __ li(msg, BytecodeInterpreter::popping_frame);
2218 
2219     __ b(unwind_recursive_activation);
2220   }
2221 
2222 
2223   //=============================================================================
2224 
2225   // We have finished an interpreted call. We are either returning to
2226   // native (call_stub/c2) or we are returning to the interpreter.
2227   // When returning to native, we must extract the result (if any)
2228   // from the java expression stack and store it in the location the
2229   // native abi expects. When returning to the interpreter we must
2230   // simply do a stack to stack move as we unwind.
2231 
2232   __ BIND(return_from_interpreted_method);
2233 
2234   //
2235   // Registers alive
2236   //   R16_thread     - JavaThread*
2237   //   R15_prev_state - address of caller's BytecodeInterpreter or 0
2238   //   R14_state      - address of callee's interpreter state
2239   //   R1_SP          - callee's stack pointer
2240   //
2241   // Registers updated
2242   //   R19_method     - callee's method
2243   //   R3_RET         - address of result (new caller's tos),
2244   //
2245   // if returning to interpreted
2246   //   msg  - message for interpreter,
2247   // if returning to interpreted
2248   //
2249 
2250   // Check if this is the initial invocation of the frame manager.
2251   // If so, R15_prev_state will be null.
2252   __ cmpdi(CCR0, R15_prev_state, 0);
2253 
2254   // Reload callee method, gc may have moved it.
2255   __ ld(R19_method, state_(_method));
2256 
2257   // Load the method's result type.
2258   __ lwz(result_index, method_(result_index));
2259 
2260   // Go to return_to_initial_caller if R15_prev_state is null.
2261   __ beq(CCR0, return_to_initial_caller);
2262 
2263   // Copy callee's result to caller's expression stack via inline stack-to-stack
2264   // converters.
2265   {
2266     Register new_tos   = R3_RET;
2267     Register from_temp = R4_ARG2;
2268     Register from      = R5_ARG3;
2269     Register tos       = R6_ARG4;
2270     Register tmp1      = R7_ARG5;
2271     Register tmp2      = R8_ARG6;
2272 
2273     ConditionRegister result_type_is_void   = CCR1;
2274     ConditionRegister result_type_is_long   = CCR2;
2275     ConditionRegister result_type_is_double = CCR3;
2276 
2277     Label stack_to_stack_void;
2278     Label stack_to_stack_double_slot; // T_LONG, T_DOUBLE
2279     Label stack_to_stack_single_slot; // T_BOOLEAN, T_BYTE, T_CHAR, T_SHORT, T_INT, T_FLOAT, T_OBJECT
2280     Label stack_to_stack_done;
2281 
2282     // Pass callee's address of tos + BytesPerWord
2283     __ ld(from_temp, state_(_stack));
2284 
2285     // result type: void
2286     __ cmpwi(result_type_is_void, result_index, AbstractInterpreter::BasicType_as_index(T_VOID));
2287 
2288     // Pass caller's tos == callee's locals address
2289     __ ld(tos, state_(_locals));
2290 
2291     // result type: long
2292     __ cmpwi(result_type_is_long, result_index, AbstractInterpreter::BasicType_as_index(T_LONG));
2293 
2294     __ addi(from, from_temp, Interpreter::stackElementSize);
2295 
2296     // !! don't branch above this line !!
2297 
2298     // handle void
2299     __ beq(result_type_is_void,   stack_to_stack_void);
2300 
2301     // result type: double
2302     __ cmpwi(result_type_is_double, result_index, AbstractInterpreter::BasicType_as_index(T_DOUBLE));
2303 
2304     // handle long or double
2305     __ beq(result_type_is_long, stack_to_stack_double_slot);
2306     __ beq(result_type_is_double, stack_to_stack_double_slot);
2307 
2308     // fall through to single slot types (incl. object)
2309 
2310     {
2311       __ BIND(stack_to_stack_single_slot);
2312       // T_BOOLEAN, T_BYTE, T_CHAR, T_SHORT, T_INT, T_FLOAT, T_OBJECT
2313 
2314       __ ld(tmp1, 0, from);
2315       __ std(tmp1, 0, tos);
2316       // New expression stack top
2317       __ addi(new_tos, tos, - BytesPerWord);
2318 
2319       __ b(stack_to_stack_done);
2320     }
2321 
2322     {
2323       __ BIND(stack_to_stack_double_slot);
2324       // T_LONG, T_DOUBLE
2325 
2326       // Move both entries for debug purposes even though only one is live
2327       __ ld(tmp1, BytesPerWord, from);
2328       __ ld(tmp2, 0, from);
2329       __ std(tmp1, 0, tos);
2330       __ std(tmp2, -BytesPerWord, tos);
2331 
2332       // new expression stack top
2333       __ addi(new_tos, tos, - 2 * BytesPerWord); // two slots
2334       __ b(stack_to_stack_done);
2335     }
2336 
2337     {
2338       __ BIND(stack_to_stack_void);
2339       // T_VOID
2340 
2341       // new expression stack top
2342       __ mr(new_tos, tos);
2343       // fall through to stack_to_stack_done
2344     }
2345 
2346     __ BIND(stack_to_stack_done);
2347   }
2348 
2349   // new tos = R3_RET
2350 
2351   // Get the message for the interpreter
2352   __ li(msg, BytecodeInterpreter::method_resume);
2353 
2354   // And fall thru
2355 
2356 
2357   //=============================================================================
2358   // Restore caller's interpreter state and pass pointer to caller's
2359   // new tos to caller.
2360 
2361   __ BIND(unwind_recursive_activation);
2362 
2363   //
2364   // Registers alive
2365   //   R15_prev_state   - address of caller's BytecodeInterpreter
2366   //   R3_RET           - address of caller's tos
2367   //   msg              - message for caller's BytecodeInterpreter
2368   //   R1_SP            - callee's stack pointer
2369   //
2370   // Registers updated
2371   //   R14_state        - address of caller's BytecodeInterpreter
2372   //   R15_prev_state   - address of its parent or 0
2373   //
2374 
2375   // Pop callee's interpreter and set R14_state to caller's interpreter.
2376   __ pop_interpreter_state(/*prev_state_may_be_0=*/false);
2377 
2378   // And fall thru
2379 
2380 
2381   //=============================================================================
2382   // Resume the (calling) interpreter after a call.
2383 
2384   __ BIND(resume_interpreter);
2385 
2386   //
2387   // Registers alive
2388   //   R14_state        - address of resuming BytecodeInterpreter
2389   //   R15_prev_state   - address of its parent or 0
2390   //   R3_RET           - address of resuming tos
2391   //   msg              - message for resuming interpreter
2392   //   R1_SP            - callee's stack pointer
2393   //
2394   // Registers updated
2395   //   R1_SP            - caller's stack pointer
2396   //
2397 
2398   // Restore C stack pointer of caller (resuming interpreter),
2399   // R14_state already points to the resuming BytecodeInterpreter.
2400   __ pop_interpreter_frame_to_state(R14_state, R21_tmp1, R11_scratch1, R12_scratch2);
2401 
2402   // Store new address of tos (holding return value) in interpreter state.
2403   __ std(R3_RET, state_(_stack));
2404 
2405   // Store message for interpreter.
2406   __ stw(msg, state_(_msg));
2407 
2408   __ b(call_interpreter);
2409 
2410   //=============================================================================
2411   // Interpreter returning to native code (call_stub/c1/c2) from
2412   // initial activation. Convert stack result and unwind activation.
2413 
2414   __ BIND(return_to_initial_caller);
2415 
2416   //
2417   // Registers alive
2418   //   R19_method       - callee's Method
2419   //   R14_state        - address of callee's interpreter state
2420   //   R16_thread       - JavaThread
2421   //   R1_SP            - callee's stack pointer
2422   //
2423   // Registers updated
2424   //   R3_RET/F1_RET - result in expected output register
2425   //
2426 
2427   // If we have an exception pending we have no result and we
2428   // must figure out where to really return to.
2429   //
2430   __ ld(pending_exception, thread_(pending_exception));
2431   __ cmpdi(CCR0, pending_exception, 0);
2432   __ bne(CCR0, unwind_initial_activation_pending_exception);
2433 
2434   __ lwa(result_index, method_(result_index));
2435 
2436   // Address of stub descriptor address array.
2437   __ load_const(stub_addr, CppInterpreter::stack_result_to_native());
2438 
2439   // Pass address of callee's tos + BytesPerWord.
2440   // Will then point directly to result.
2441   __ ld(R3_ARG1, state_(_stack));
2442   __ addi(R3_ARG1, R3_ARG1, Interpreter::stackElementSize);
2443 
2444   // Address of stub descriptor address
2445   __ sldi(result_index, result_index, LogBytesPerWord);
2446   __ add(stub_addr, stub_addr, result_index);
2447 
2448   // Stub descriptor address
2449   __ ld(stub_addr, 0, stub_addr);
2450 
2451   // TODO: don't do this via a call, do it in place!
2452   //
2453   // call stub via descriptor
2454   __ call_stub(stub_addr);
2455 
2456   __ BIND(unwind_initial_activation);
2457 
2458   // Unwind from initial activation. No exception is pending.
2459 
2460   //
2461   // Stack layout at this point:
2462   //
2463   //    0       [TOP_IJAVA_FRAME_ABI]         <-- R1_SP
2464   //            ...
2465   //    CALLER  [PARENT_IJAVA_FRAME_ABI]
2466   //            ...
2467   //    CALLER  [unextended ABI]
2468   //            ...
2469   //
2470   //  The CALLER frame has a C2I adapter or is an entry-frame.
2471   //
2472 
2473   // An interpreter frame exists, we may pop the TOP_IJAVA_FRAME and
2474   // turn the caller's PARENT_IJAVA_FRAME back into a TOP_IJAVA_FRAME.
2475   // But, we simply restore the return pc from the caller's frame and
2476   // use the caller's initial_caller_sp as the new SP which pops the
2477   // interpreter frame and "resizes" the caller's frame to its "unextended"
2478   // size.
2479 
2480   // get rid of top frame
2481   __ pop_frame();
2482 
2483   // Load return PC from parent frame.
2484   __ ld(R21_tmp1, _parent_ijava_frame_abi(lr), R1_SP);
2485 
2486   // Resize frame to get rid of a potential extension.
2487   __ resize_frame_to_initial_caller(R11_scratch1, R12_scratch2);
2488 
2489   // update LR
2490   __ mtlr(R21_tmp1);
2491 
2492   // return
2493   __ blr();
2494 
2495   //=============================================================================
2496   // Unwind from initial activation. An exception is pending
2497 
2498   __ BIND(unwind_initial_activation_pending_exception);
2499 
2500   //
2501   // Stack layout at this point:
2502   //
2503   //   0       [TOP_IJAVA_FRAME_ABI]         <-- R1_SP
2504   //           ...
2505   //   CALLER  [PARENT_IJAVA_FRAME_ABI]
2506   //           ...
2507   //   CALLER  [unextended ABI]
2508   //           ...
2509   //
2510   // The CALLER frame has a C2I adapter or is an entry-frame.
2511   //
2512 
2513   // An interpreter frame exists, we may pop the TOP_IJAVA_FRAME and
2514   // turn the caller's PARENT_IJAVA_FRAME back into a TOP_IJAVA_FRAME.
2515   // But, we just pop the current TOP_IJAVA_FRAME and fall through
2516 
2517   __ pop_frame();
2518   __ ld(R3_ARG1, _top_ijava_frame_abi(lr), R1_SP);
2519 
2520   //
2521   // Stack layout at this point:
2522   //
2523   //   CALLER  [PARENT_IJAVA_FRAME_ABI]      <-- R1_SP
2524   //           ...
2525   //   CALLER  [unextended ABI]
2526   //           ...
2527   //
2528   // The CALLER frame has a C2I adapter or is an entry-frame.
2529   //
2530   // Registers alive
2531   //   R16_thread
2532   //   R3_ARG1 - return address to caller
2533   //
2534   // Registers updated
2535   //   R3_ARG1 - address of pending exception
2536   //   R4_ARG2 - issuing pc = return address to caller
2537   //   LR      - address of exception handler stub
2538   //
2539 
2540   // Resize frame to get rid of a potential extension.
2541   __ resize_frame_to_initial_caller(R11_scratch1, R12_scratch2);
2542 
2543   __ mr(R14, R3_ARG1);   // R14 := ARG1
2544   __ mr(R4_ARG2, R3_ARG1);  // ARG2 := ARG1
2545 
2546   // Find the address of the "catch_exception" stub.
2547   __ push_frame_abi112(0, R11_scratch1);
2548   __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address),
2549                   R16_thread,
2550                   R4_ARG2);
2551   __ pop_frame();
2552 
2553   // Load continuation address into LR.
2554   __ mtlr(R3_RET);
2555 
2556   // Load address of pending exception and clear it in thread object.
2557   __ ld(R3_ARG1/*R3_RET*/, thread_(pending_exception));
2558   __ li(R4_ARG2, 0);
2559   __ std(R4_ARG2, thread_(pending_exception));
2560 
2561   // re-load issuing pc
2562   __ mr(R4_ARG2, R14);
2563 
2564   // Branch to found exception handler.
2565   __ blr();
2566 
2567   //=============================================================================
2568   // Call a new method. Compute new args and trim the expression stack
2569   // to only what we are currently using and then recurse.
2570 
2571   __ BIND(call_method);
2572 
2573   //
2574   //  Registers alive
2575   //    R16_thread
2576   //    R14_state      - address of caller's BytecodeInterpreter
2577   //    R1_SP          - caller's stack pointer
2578   //
2579   //  Registers updated
2580   //    R15_prev_state - address of caller's BytecodeInterpreter
2581   //    R17_tos        - address of caller's tos
2582   //    R19_method     - callee's Method
2583   //    R1_SP          - trimmed back
2584   //
2585 
2586   // Very-local scratch registers.
2587 
2588   const Register offset = R21_tmp1;
2589   const Register tmp    = R22_tmp2;
2590   const Register self_entry  = R23_tmp3;
2591   const Register stub_entry  = R24_tmp4;
2592 
2593   const ConditionRegister cr = CCR0;
2594 
2595   // Load the address of the frame manager.
2596   __ load_const(self_entry, &interpreter_frame_manager);
2597   __ ld(self_entry, 0, self_entry);
2598 
2599   // Load BytecodeInterpreter._result._to_call._callee (callee's Method).
2600   __ ld(R19_method, state_(_result._to_call._callee));
2601   // Load BytecodeInterpreter._stack (outgoing tos).
2602   __ ld(R17_tos, state_(_stack));
2603 
2604   // Save address of caller's BytecodeInterpreter.
2605   __ mr(R15_prev_state, R14_state);
2606 
2607   // Load the callee's entry point.
2608   // Load BytecodeInterpreter._result._to_call._callee_entry_point.
2609   __ ld(stub_entry, state_(_result._to_call._callee_entry_point));
2610 
2611   // Check whether stub_entry is equal to self_entry.
2612   __ cmpd(cr, self_entry, stub_entry);
2613   // if (self_entry == stub_entry)
2614   //   do a re-dispatch
2615   __ beq(cr, re_dispatch);
2616   // else
2617   //   call the specialized entry (adapter for jni or compiled code)
2618   __ BIND(call_special);
2619 
2620   //
2621   // Call the entry generated by `InterpreterGenerator::generate_native_entry'.
2622   //
2623   // Registers alive
2624   //   R16_thread
2625   //   R15_prev_state    - address of caller's BytecodeInterpreter
2626   //   R19_method        - callee's Method
2627   //   R17_tos           - address of caller's tos
2628   //   R1_SP             - caller's stack pointer
2629   //
2630 
2631   // Mark return from specialized entry for generate_native_entry.
2632   guarantee(return_from_native_pc != (address) NULL, "precondition");
2633   frame_manager_specialized_return = return_from_native_pc;
2634 
2635   // Set sender_SP in case we call interpreter native wrapper which
2636   // will expect it. Compiled code should not care.
2637   __ mr(R21_sender_SP, R1_SP);
2638 
2639   // Do a tail call here, and let the link register point to
2640   // frame_manager_specialized_return which is return_from_native_pc.
2641   __ load_const(tmp, frame_manager_specialized_return);
2642   __ call_stub_and_return_to(stub_entry,  tmp /* return_pc=tmp */);
2643 
2644 
2645   //=============================================================================
2646   //
2647   // InterpretMethod triggered OSR compilation of some Java method M
2648   // and now asks to run the compiled code.  We call this code the
2649   // `callee'.
2650   //
2651   // This is our current idea on how OSR should look like on PPC64:
2652   //
2653   // While interpreting a Java method M the stack is:
2654   //
2655   //  (InterpretMethod (M), IJAVA_FRAME (M), ANY_FRAME, ...).
2656   //
2657   // After having OSR compiled M, `InterpretMethod' returns to the
2658   // frame manager, sending the message `retry_method_osr'.  The stack
2659   // is:
2660   //
2661   //  (IJAVA_FRAME (M), ANY_FRAME, ...).
2662   //
2663   // The compiler will have generated an `nmethod' suitable for
2664   // continuing execution of M at the bytecode index at which OSR took
2665   // place.  So now the frame manager calls the OSR entry.  The OSR
2666   // entry sets up a JIT_FRAME for M and continues execution of M with
2667   // initial state determined by the IJAVA_FRAME.
2668   //
2669   //  (JIT_FRAME (M), IJAVA_FRAME (M), ANY_FRAME, ...).
2670   //
2671 
2672   __ BIND(retry_method_osr);
2673   {
2674   //
2675   // Registers alive
2676   //   R16_thread
2677   //   R15_prev_state     - address of caller's BytecodeInterpreter
2678   //   R14_state          - address of callee's BytecodeInterpreter
2679   //   R1_SP              - callee's SP before call to InterpretMethod
2680   //
2681   // Registers updated
2682   //   R17                - pointer to callee's locals array
2683   //                       (declared via `interpreter_arg_ptr_reg' in the AD file)
2684   //   R19_method         - callee's Method
2685   //   R1_SP              - callee's SP (will become SP of OSR adapter frame)
2686   //
2687 
2688   // Provide a debugger breakpoint in the frame manager if breakpoints
2689   // in osr'd methods are requested.
2690 #ifdef COMPILER2
2691   NOT_PRODUCT( if (OptoBreakpointOSR) { __ illtrap(); } )
2692 #endif
2693 
2694   // Load callee's pointer to locals array from callee's state.
2695   //  __ ld(R17, state_(_locals));
2696 
2697   // Load osr entry.
2698   __ ld(R12_scratch2, state_(_result._osr._osr_entry));
2699 
2700   // Load address of temporary osr buffer to arg1.
2701   __ ld(R3_ARG1, state_(_result._osr._osr_buf));
2702   __ mtctr(R12_scratch2);
2703 
2704   // Load method oop, gc may move it during execution of osr'd method.
2705   __ ld(R22_tmp2, state_(_method));
2706   // Load message 'call_method'.
2707   __ li(R23_tmp3, BytecodeInterpreter::call_method);
2708 
2709   {
2710     // Pop the IJAVA frame of the method which we are going to call osr'd.
2711     Label no_state, skip_no_state;
2712     __ pop_interpreter_state(/*prev_state_may_be_0=*/true);
2713     __ cmpdi(CCR0, R14_state,0);
2714     __ beq(CCR0, no_state);
2715     // return to interpreter
2716     __ pop_interpreter_frame_to_state(R14_state, R11_scratch1, R12_scratch2, R21_tmp1);
2717 
2718     // Init _result._to_call._callee and tell gc that it contains a valid oop
2719     // by setting _msg to 'call_method'.
2720     __ std(R22_tmp2, state_(_result._to_call._callee));
2721     // TODO: PPC port: assert(4 == BytecodeInterpreter::sz_msg(), "unexpected field size");
2722     __ stw(R23_tmp3, state_(_msg));
2723 
2724     __ load_const(R21_tmp1, frame_manager_specialized_return);
2725     __ b(skip_no_state);
2726     __ bind(no_state);
2727 
2728     // Return to initial caller.
2729 
2730     // Get rid of top frame.
2731     __ pop_frame();
2732 
2733     // Load return PC from parent frame.
2734     __ ld(R21_tmp1, _parent_ijava_frame_abi(lr), R1_SP);
2735 
2736     // Resize frame to get rid of a potential extension.
2737     __ resize_frame_to_initial_caller(R11_scratch1, R12_scratch2);
2738 
2739     __ bind(skip_no_state);
2740 
2741     // Update LR with return pc.
2742     __ mtlr(R21_tmp1);
2743   }
2744   // Jump to the osr entry point.
2745   __ bctr();
2746 
2747   }
2748 
2749   //=============================================================================
2750   // Interpreted method "returned" with an exception, pass it on.
2751   // Pass no result, unwind activation and continue/return to
2752   // interpreter/call_stub/c2.
2753 
2754   __ BIND(throwing_exception);
2755 
2756   // Check if this is the initial invocation of the frame manager.  If
2757   // so, previous interpreter state in R15_prev_state will be null.
2758 
2759   // New tos of caller is callee's first parameter address, that is
2760   // callee's incoming arguments are popped.
2761   __ ld(R3_RET, state_(_locals));
2762 
2763   // Check whether this is an initial call.
2764   __ cmpdi(CCR0, R15_prev_state, 0);
2765   // Yes, called from the call stub or from generated code via a c2i frame.
2766   __ beq(CCR0, unwind_initial_activation_pending_exception);
2767 
2768   // Send resume message, interpreter will see the exception first.
2769 
2770   __ li(msg, BytecodeInterpreter::method_resume);
2771   __ b(unwind_recursive_activation);
2772 
2773 
2774   //=============================================================================
2775   // Push the last instruction out to the code buffer.
2776 
2777   {
2778     __ unimplemented("end of InterpreterGenerator::generate_normal_entry", 128);
2779   }
2780 
2781   interpreter_frame_manager = entry;
2782   return interpreter_frame_manager;
2783 }
2784 
2785 // Generate code for various sorts of method entries
2786 //
2787 address AbstractInterpreterGenerator::generate_method_entry(AbstractInterpreter::MethodKind kind) {
2788   address entry_point = NULL;
2789 
2790   switch (kind) {
2791     case Interpreter::zerolocals                 :                                                                              break;
2792     case Interpreter::zerolocals_synchronized    :                                                                              break;
2793     case Interpreter::native                     : // Fall thru
2794     case Interpreter::native_synchronized        : entry_point = ((CppInterpreterGenerator*)this)->generate_native_entry();     break;
2795     case Interpreter::empty                      :                                                                              break;
2796     case Interpreter::accessor                   : entry_point = ((InterpreterGenerator*)this)->generate_accessor_entry();      break;
2797     case Interpreter::abstract                   : entry_point = ((InterpreterGenerator*)this)->generate_abstract_entry();      break;
2798     // These are special interpreter intrinsics which we don't support so far.
2799     case Interpreter::java_lang_math_sin         :                                                                              break;
2800     case Interpreter::java_lang_math_cos         :                                                                              break;
2801     case Interpreter::java_lang_math_tan         :                                                                              break;
2802     case Interpreter::java_lang_math_abs         :                                                                              break;
2803     case Interpreter::java_lang_math_log         :                                                                              break;
2804     case Interpreter::java_lang_math_log10       :                                                                              break;
2805     case Interpreter::java_lang_math_sqrt        :                                                                              break;
2806     case Interpreter::java_lang_math_pow         :                                                                              break;
2807     case Interpreter::java_lang_math_exp         :                                                                              break;
2808     case Interpreter::java_lang_ref_reference_get: entry_point = ((InterpreterGenerator*)this)->generate_Reference_get_entry(); break;
2809     default                                      : ShouldNotReachHere();                                                        break;
2810   }
2811 
2812   if (entry_point) {
2813     return entry_point;
2814   }
2815   return ((InterpreterGenerator*)this)->generate_normal_entry();
2816 }
2817 
2818 InterpreterGenerator::InterpreterGenerator(StubQueue* code)
2819  : CppInterpreterGenerator(code) {
2820    generate_all(); // down here so it can be "virtual"
2821 }
2822 
2823 // How much stack a topmost interpreter method activation needs in words.
2824 int AbstractInterpreter::size_top_interpreter_activation(Method* method) {
2825   // Computation is in bytes not words to match layout_activation_impl
2826   // below, but the return is in words.
2827 
2828   //
2829   //  0       [TOP_IJAVA_FRAME_ABI]                                                    \
2830   //          alignment (optional)                                             \       |
2831   //          [operand stack / Java parameters] > stack                        |       |
2832   //          [monitors] (optional)             > monitors                     |       |
2833   //          [PARENT_IJAVA_FRAME_ABI]                                \        |       |
2834   //          [BytecodeInterpreter object]      > interpreter \       |        |       |
2835   //          alignment (optional)                            | round | parent | round | top
2836   //          [Java result] (2 slots)           > result      |       |        |       |
2837   //          [Java non-arg locals]             \ locals      |       |        |       |
2838   //          [arg locals]                      /             /       /        /       /
2839   //
2840 
2841   int locals = method->max_locals() * BytesPerWord;
2842   int interpreter = frame::interpreter_frame_cinterpreterstate_size_in_bytes();
2843   int result = 2 * BytesPerWord;
2844 
2845   int parent = round_to(interpreter + result + locals, 16) + frame::parent_ijava_frame_abi_size;
2846 
2847   int stack = method->max_stack() * BytesPerWord;
2848   int monitors = method->is_synchronized() ? frame::interpreter_frame_monitor_size_in_bytes() : 0;
2849   int top = round_to(parent + monitors + stack, 16) + frame::top_ijava_frame_abi_size;
2850 
2851   return (top / BytesPerWord);
2852 }
2853 
2854 void BytecodeInterpreter::layout_interpreterState(interpreterState to_fill,
2855                                                   frame* caller,
2856                                                   frame* current,
2857                                                   Method* method,
2858                                                   intptr_t* locals,
2859                                                   intptr_t* stack,
2860                                                   intptr_t* stack_base,
2861                                                   intptr_t* monitor_base,
2862                                                   intptr_t* frame_sp,
2863                                                   bool is_top_frame) {
2864   // What about any vtable?
2865   //
2866   to_fill->_thread = JavaThread::current();
2867   // This gets filled in later but make it something recognizable for now.
2868   to_fill->_bcp = method->code_base();
2869   to_fill->_locals = locals;
2870   to_fill->_constants = method->constants()->cache();
2871   to_fill->_method = method;
2872   to_fill->_mdx = NULL;
2873   to_fill->_stack = stack;
2874 
2875   if (is_top_frame && JavaThread::current()->popframe_forcing_deopt_reexecution()) {
2876     to_fill->_msg = deopt_resume2;
2877   } else {
2878     to_fill->_msg = method_resume;
2879   }
2880   to_fill->_result._to_call._bcp_advance = 0;
2881   to_fill->_result._to_call._callee_entry_point = NULL; // doesn't matter to anyone
2882   to_fill->_result._to_call._callee = NULL; // doesn't matter to anyone
2883   to_fill->_prev_link = NULL;
2884 
2885   if (caller->is_interpreted_frame()) {
2886     interpreterState prev  = caller->get_interpreterState();
2887 
2888     // Support MH calls. Make sure the interpreter will return the right address:
2889     // 1. Caller did ordinary interpreted->compiled call call: Set a prev_state
2890     //    which makes the CPP interpreter return to frame manager "return_from_interpreted_method"
2891     //    entry after finishing execution.
2892     // 2. Caller did a MH call: If the caller has a MethodHandleInvoke in it's
2893     //    state (invariant: must be the caller of the bottom vframe) we used the
2894     //    "call_special" entry to do the call, meaning the arguments have not been
2895     //    popped from the stack. Therefore, don't enter a prev state in this case
2896     //    in order to return to "return_from_native" frame manager entry which takes
2897     //    care of popping arguments. Also, don't overwrite the MH.invoke Method in
2898     //    the prev_state in order to be able to figure out the number of arguments to
2899     //     pop.
2900     // The parameter method can represent MethodHandle.invokeExact(...).
2901     // The MethodHandleCompiler generates these synthetic Methods,
2902     // including bytecodes, if an invokedynamic call gets inlined. In
2903     // this case we want to return like from any other interpreted
2904     // Java call, so we set _prev_link.
2905     to_fill->_prev_link = prev;
2906 
2907     if (*prev->_bcp == Bytecodes::_invokeinterface || *prev->_bcp == Bytecodes::_invokedynamic) {
2908       prev->_result._to_call._bcp_advance = 5;
2909     } else {
2910       prev->_result._to_call._bcp_advance = 3;
2911     }
2912   }
2913   to_fill->_oop_temp = NULL;
2914   to_fill->_stack_base = stack_base;
2915   // Need +1 here because stack_base points to the word just above the
2916   // first expr stack entry and stack_limit is supposed to point to
2917   // the word just below the last expr stack entry. See
2918   // generate_compute_interpreter_state.
2919   to_fill->_stack_limit = stack_base - (method->max_stack() + 1);
2920   to_fill->_monitor_base = (BasicObjectLock*) monitor_base;
2921 
2922   to_fill->_frame_bottom = frame_sp;
2923 
2924   // PPC64 specific
2925   to_fill->_last_Java_pc = NULL;
2926   to_fill->_last_Java_fp = NULL;
2927   to_fill->_last_Java_sp = frame_sp;
2928 #ifdef ASSERT
2929   to_fill->_self_link = to_fill;
2930   to_fill->_native_fresult = 123456.789;
2931   to_fill->_native_lresult = CONST64(0xdeafcafedeadc0de);
2932 #endif
2933 }
2934 
2935 void BytecodeInterpreter::pd_layout_interpreterState(interpreterState istate,
2936                                                      address last_Java_pc,
2937                                                      intptr_t* last_Java_fp) {
2938   istate->_last_Java_pc = last_Java_pc;
2939   istate->_last_Java_fp = last_Java_fp;
2940 }
2941 
2942 int AbstractInterpreter::layout_activation(Method* method,
2943                                            int temps,        // Number of slots on java expression stack in use.
2944                                            int popframe_args,
2945                                            int monitors,     // Number of active monitors.
2946                                            int caller_actual_parameters,
2947                                            int callee_params,// Number of slots for callee parameters.
2948                                            int callee_locals,// Number of slots for locals.
2949                                            frame* caller,
2950                                            frame* interpreter_frame,
2951                                            bool is_top_frame,
2952                                            bool is_bottom_frame) {
2953 
2954   // NOTE this code must exactly mimic what
2955   // InterpreterGenerator::generate_compute_interpreter_state() does
2956   // as far as allocating an interpreter frame. However there is an
2957   // exception. With the C++ based interpreter only the top most frame
2958   // has a full sized expression stack.  The 16 byte slop factor is
2959   // both the abi scratch area and a place to hold a result from a
2960   // callee on its way to the callers stack.
2961 
2962   int monitor_size = frame::interpreter_frame_monitor_size_in_bytes() * monitors;
2963   int frame_size;
2964   int top_frame_size = round_to(frame::interpreter_frame_cinterpreterstate_size_in_bytes()
2965                                 + monitor_size
2966                                 + (method->max_stack() *Interpreter::stackElementWords * BytesPerWord)
2967                                 + 2*BytesPerWord,
2968                                 frame::alignment_in_bytes)
2969                       + frame::top_ijava_frame_abi_size;
2970   if (is_top_frame) {
2971     frame_size = top_frame_size;
2972   } else {
2973     frame_size = round_to(frame::interpreter_frame_cinterpreterstate_size_in_bytes()
2974                           + monitor_size
2975                           + ((temps - callee_params + callee_locals) *
2976                              Interpreter::stackElementWords * BytesPerWord)
2977                           + 2*BytesPerWord,
2978                           frame::alignment_in_bytes)
2979                  + frame::parent_ijava_frame_abi_size;
2980     assert(popframe_args==0, "non-zero for top_frame only");
2981   }
2982 
2983   // If we actually have a frame to layout we must now fill in all the pieces.
2984   if (interpreter_frame != NULL) {
2985 
2986     intptr_t sp = (intptr_t)interpreter_frame->sp();
2987     intptr_t fp = *(intptr_t *)sp;
2988     assert(fp == (intptr_t)caller->sp(), "fp must match");
2989     interpreterState cur_state =
2990       (interpreterState)(fp - frame::interpreter_frame_cinterpreterstate_size_in_bytes());
2991 
2992     // Now fill in the interpreterState object.
2993 
2994     intptr_t* locals;
2995     if (caller->is_interpreted_frame()) {
2996       // Locals must agree with the caller because it will be used to set the
2997       // caller's tos when we return.
2998       interpreterState prev  = caller->get_interpreterState();
2999       // Calculate start of "locals" for MH calls.  For MH calls, the
3000       // current method() (= MH target) and prev->callee() (=
3001       // MH.invoke*()) are different and especially have different
3002       // signatures. To pop the argumentsof the caller, we must use
3003       // the prev->callee()->size_of_arguments() because that's what
3004       // the caller actually pushed.  Currently, for synthetic MH
3005       // calls (deoptimized from inlined MH calls), detected by
3006       // is_method_handle_invoke(), we use the callee's arguments
3007       // because here, the caller's and callee's signature match.
3008       if (true /*!caller->is_at_mh_callsite()*/) {
3009         locals = prev->stack() + method->size_of_parameters();
3010       } else {
3011         // Normal MH call.
3012         locals = prev->stack() + prev->callee()->size_of_parameters();
3013       }
3014     } else {
3015       bool is_deopted;
3016       locals = (intptr_t*) (fp + ((method->max_locals() - 1) * BytesPerWord) +
3017                             frame::parent_ijava_frame_abi_size);
3018     }
3019 
3020     intptr_t* monitor_base = (intptr_t*) cur_state;
3021     intptr_t* stack_base   = (intptr_t*) ((intptr_t) monitor_base - monitor_size);
3022 
3023     // Provide pop_frame capability on PPC64, add popframe_args.
3024     // +1 because stack is always prepushed.
3025     intptr_t* stack = (intptr_t*) ((intptr_t) stack_base - (temps + popframe_args + 1) * BytesPerWord);
3026 
3027     BytecodeInterpreter::layout_interpreterState(cur_state,
3028                                                  caller,
3029                                                  interpreter_frame,
3030                                                  method,
3031                                                  locals,
3032                                                  stack,
3033                                                  stack_base,
3034                                                  monitor_base,
3035                                                  (intptr_t*)(((intptr_t)fp)-top_frame_size),
3036                                                  is_top_frame);
3037 
3038     BytecodeInterpreter::pd_layout_interpreterState(cur_state, interpreter_return_address,
3039                                                     interpreter_frame->fp());
3040   }
3041   return frame_size/BytesPerWord;
3042 }
3043 
3044 #endif // CC_INTERP