1 /*
   2  * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "asm/assembler.hpp"
  27 #include "ci/ciMethod.hpp"
  28 #include "interpreter/bytecodeHistogram.hpp"
  29 #include "interpreter/cppInterpreter.hpp"
  30 #include "interpreter/interpreter.hpp"
  31 #include "interpreter/interpreterGenerator.hpp"
  32 #include "interpreter/interpreterRuntime.hpp"
  33 #include "oops/arrayOop.hpp"
  34 #include "oops/methodData.hpp"
  35 #include "oops/method.hpp"
  36 #include "oops/oop.inline.hpp"
  37 #include "prims/jvmtiExport.hpp"
  38 #include "prims/jvmtiThreadState.hpp"
  39 #include "runtime/arguments.hpp"
  40 #include "runtime/deoptimization.hpp"
  41 #include "runtime/frame.inline.hpp"
  42 #include "runtime/interfaceSupport.hpp"
  43 #include "runtime/sharedRuntime.hpp"
  44 #include "runtime/stubRoutines.hpp"
  45 #include "runtime/synchronizer.hpp"
  46 #include "runtime/timer.hpp"
  47 #include "runtime/vframeArray.hpp"
  48 #include "utilities/debug.hpp"
  49 #include "utilities/macros.hpp"
  50 #ifdef SHARK
  51 #include "shark/shark_globals.hpp"
  52 #endif
  53 
  54 #ifdef CC_INTERP
  55 
  56 // Routine exists to make tracebacks look decent in debugger
  57 // while "shadow" interpreter frames are on stack. It is also
  58 // used to distinguish interpreter frames.
  59 
  60 extern "C" void RecursiveInterpreterActivation(interpreterState istate) {
  61   ShouldNotReachHere();
  62 }
  63 
  64 bool CppInterpreter::contains(address pc) {
  65   return ( _code->contains(pc) ||
  66          ( pc == (CAST_FROM_FN_PTR(address, RecursiveInterpreterActivation) + frame::pc_return_offset)));
  67 }
  68 
  69 #define STATE(field_name) Lstate, in_bytes(byte_offset_of(BytecodeInterpreter, field_name))
  70 #define __ _masm->
  71 
  72 Label frame_manager_entry;
  73 Label fast_accessor_slow_entry_path;  // fast accessor methods need to be able to jmp to unsynchronized
  74                                       // c++ interpreter entry point this holds that entry point label.
  75 
  76 static address unctrap_frame_manager_entry  = NULL;
  77 
  78 static address interpreter_return_address  = NULL;
  79 static address deopt_frame_manager_return_atos  = NULL;
  80 static address deopt_frame_manager_return_btos  = NULL;
  81 static address deopt_frame_manager_return_itos  = NULL;
  82 static address deopt_frame_manager_return_ltos  = NULL;
  83 static address deopt_frame_manager_return_ftos  = NULL;
  84 static address deopt_frame_manager_return_dtos  = NULL;
  85 static address deopt_frame_manager_return_vtos  = NULL;
  86 
  87 const Register prevState = G1_scratch;
  88 
  89 void InterpreterGenerator::save_native_result(void) {
  90   // result potentially in O0/O1: save it across calls
  91   __ stf(FloatRegisterImpl::D, F0, STATE(_native_fresult));
  92 #ifdef _LP64
  93   __ stx(O0, STATE(_native_lresult));
  94 #else
  95   __ std(O0, STATE(_native_lresult));
  96 #endif
  97 }
  98 
  99 void InterpreterGenerator::restore_native_result(void) {
 100 
 101   // Restore any method result value
 102   __ ldf(FloatRegisterImpl::D, STATE(_native_fresult), F0);
 103 #ifdef _LP64
 104   __ ldx(STATE(_native_lresult), O0);
 105 #else
 106   __ ldd(STATE(_native_lresult), O0);
 107 #endif
 108 }
 109 
 110 // A result handler converts/unboxes a native call result into
 111 // a java interpreter/compiler result. The current frame is an
 112 // interpreter frame. The activation frame unwind code must be
 113 // consistent with that of TemplateTable::_return(...). In the
 114 // case of native methods, the caller's SP was not modified.
 115 address CppInterpreterGenerator::generate_result_handler_for(BasicType type) {
 116   address entry = __ pc();
 117   Register Itos_i  = Otos_i ->after_save();
 118   Register Itos_l  = Otos_l ->after_save();
 119   Register Itos_l1 = Otos_l1->after_save();
 120   Register Itos_l2 = Otos_l2->after_save();
 121   switch (type) {
 122     case T_BOOLEAN: __ subcc(G0, O0, G0); __ addc(G0, 0, Itos_i); break; // !0 => true; 0 => false
 123     case T_CHAR   : __ sll(O0, 16, O0); __ srl(O0, 16, Itos_i);   break; // cannot use and3, 0xFFFF too big as immediate value!
 124     case T_BYTE   : __ sll(O0, 24, O0); __ sra(O0, 24, Itos_i);   break;
 125     case T_SHORT  : __ sll(O0, 16, O0); __ sra(O0, 16, Itos_i);   break;
 126     case T_LONG   :
 127 #ifndef _LP64
 128                     __ mov(O1, Itos_l2);  // move other half of long
 129 #endif              // ifdef or no ifdef, fall through to the T_INT case
 130     case T_INT    : __ mov(O0, Itos_i);                         break;
 131     case T_VOID   : /* nothing to do */                         break;
 132     case T_FLOAT  : assert(F0 == Ftos_f, "fix this code" );     break;
 133     case T_DOUBLE : assert(F0 == Ftos_d, "fix this code" );     break;
 134     case T_OBJECT :
 135       __ ld_ptr(STATE(_oop_temp), Itos_i);
 136       __ verify_oop(Itos_i);
 137       break;
 138     default       : ShouldNotReachHere();
 139   }
 140   __ ret();                           // return from interpreter activation
 141   __ delayed()->restore(I5_savedSP, G0, SP);  // remove interpreter frame
 142   NOT_PRODUCT(__ emit_int32(0);)       // marker for disassembly
 143   return entry;
 144 }
 145 
 146 // tosca based result to c++ interpreter stack based result.
 147 // Result goes to address in L1_scratch
 148 
 149 address CppInterpreterGenerator::generate_tosca_to_stack_converter(BasicType type) {
 150   // A result is in the native abi result register from a native method call.
 151   // We need to return this result to the interpreter by pushing the result on the interpreter's
 152   // stack. This is relatively simple the destination is in L1_scratch
 153   // i.e. L1_scratch is the first free element on the stack. If we "push" a return value we must
 154   // adjust L1_scratch
 155   address entry = __ pc();
 156   switch (type) {
 157     case T_BOOLEAN:
 158       // !0 => true; 0 => false
 159       __ subcc(G0, O0, G0);
 160       __ addc(G0, 0, O0);
 161       __ st(O0, L1_scratch, 0);
 162       __ sub(L1_scratch, wordSize, L1_scratch);
 163       break;
 164 
 165     // cannot use and3, 0xFFFF too big as immediate value!
 166     case T_CHAR   :
 167       __ sll(O0, 16, O0);
 168       __ srl(O0, 16, O0);
 169       __ st(O0, L1_scratch, 0);
 170       __ sub(L1_scratch, wordSize, L1_scratch);
 171       break;
 172 
 173     case T_BYTE   :
 174       __ sll(O0, 24, O0);
 175       __ sra(O0, 24, O0);
 176       __ st(O0, L1_scratch, 0);
 177       __ sub(L1_scratch, wordSize, L1_scratch);
 178       break;
 179 
 180     case T_SHORT  :
 181       __ sll(O0, 16, O0);
 182       __ sra(O0, 16, O0);
 183       __ st(O0, L1_scratch, 0);
 184       __ sub(L1_scratch, wordSize, L1_scratch);
 185       break;
 186     case T_LONG   :
 187 #ifndef _LP64
 188 #if defined(COMPILER2)
 189   // All return values are where we want them, except for Longs.  C2 returns
 190   // longs in G1 in the 32-bit build whereas the interpreter wants them in O0/O1.
 191   // Since the interpreter will return longs in G1 and O0/O1 in the 32bit
 192   // build even if we are returning from interpreted we just do a little
 193   // stupid shuffing.
 194   // Note: I tried to make c2 return longs in O0/O1 and G1 so we wouldn't have to
 195   // do this here. Unfortunately if we did a rethrow we'd see an machepilog node
 196   // first which would move g1 -> O0/O1 and destroy the exception we were throwing.
 197       __ stx(G1, L1_scratch, -wordSize);
 198 #else
 199       // native result is in O0, O1
 200       __ st(O1, L1_scratch, 0);                      // Low order
 201       __ st(O0, L1_scratch, -wordSize);              // High order
 202 #endif /* COMPILER2 */
 203 #else
 204       __ stx(O0, L1_scratch, -wordSize);
 205 #endif
 206       __ sub(L1_scratch, 2*wordSize, L1_scratch);
 207       break;
 208 
 209     case T_INT    :
 210       __ st(O0, L1_scratch, 0);
 211       __ sub(L1_scratch, wordSize, L1_scratch);
 212       break;
 213 
 214     case T_VOID   : /* nothing to do */
 215       break;
 216 
 217     case T_FLOAT  :
 218       __ stf(FloatRegisterImpl::S, F0, L1_scratch, 0);
 219       __ sub(L1_scratch, wordSize, L1_scratch);
 220       break;
 221 
 222     case T_DOUBLE :
 223       // Every stack slot is aligned on 64 bit, However is this
 224       // the correct stack slot on 64bit?? QQQ
 225       __ stf(FloatRegisterImpl::D, F0, L1_scratch, -wordSize);
 226       __ sub(L1_scratch, 2*wordSize, L1_scratch);
 227       break;
 228     case T_OBJECT :
 229       __ verify_oop(O0);
 230       __ st_ptr(O0, L1_scratch, 0);
 231       __ sub(L1_scratch, wordSize, L1_scratch);
 232       break;
 233     default       : ShouldNotReachHere();
 234   }
 235   __ retl();                          // return from interpreter activation
 236   __ delayed()->nop();                // schedule this better
 237   NOT_PRODUCT(__ emit_int32(0);)       // marker for disassembly
 238   return entry;
 239 }
 240 
 241 address CppInterpreterGenerator::generate_stack_to_stack_converter(BasicType type) {
 242   // A result is in the java expression stack of the interpreted method that has just
 243   // returned. Place this result on the java expression stack of the caller.
 244   //
 245   // The current interpreter activation in Lstate is for the method just returning its
 246   // result. So we know that the result of this method is on the top of the current
 247   // execution stack (which is pre-pushed) and will be return to the top of the caller
 248   // stack. The top of the callers stack is the bottom of the locals of the current
 249   // activation.
 250   // Because of the way activation are managed by the frame manager the value of esp is
 251   // below both the stack top of the current activation and naturally the stack top
 252   // of the calling activation. This enable this routine to leave the return address
 253   // to the frame manager on the stack and do a vanilla return.
 254   //
 255   // On entry: O0 - points to source (callee stack top)
 256   //           O1 - points to destination (caller stack top [i.e. free location])
 257   // destroys O2, O3
 258   //
 259 
 260   address entry = __ pc();
 261   switch (type) {
 262     case T_VOID:  break;
 263       break;
 264     case T_FLOAT  :
 265     case T_BOOLEAN:
 266     case T_CHAR   :
 267     case T_BYTE   :
 268     case T_SHORT  :
 269     case T_INT    :
 270       // 1 word result
 271       __ ld(O0, 0, O2);
 272       __ st(O2, O1, 0);
 273       __ sub(O1, wordSize, O1);
 274       break;
 275     case T_DOUBLE  :
 276     case T_LONG    :
 277       // return top two words on current expression stack to caller's expression stack
 278       // The caller's expression stack is adjacent to the current frame manager's intepretState
 279       // except we allocated one extra word for this intepretState so we won't overwrite it
 280       // when we return a two word result.
 281 #ifdef _LP64
 282       __ ld_ptr(O0, 0, O2);
 283       __ st_ptr(O2, O1, -wordSize);
 284 #else
 285       __ ld(O0, 0, O2);
 286       __ ld(O0, wordSize, O3);
 287       __ st(O3, O1, 0);
 288       __ st(O2, O1, -wordSize);
 289 #endif
 290       __ sub(O1, 2*wordSize, O1);
 291       break;
 292     case T_OBJECT :
 293       __ ld_ptr(O0, 0, O2);
 294       __ verify_oop(O2);                                               // verify it
 295       __ st_ptr(O2, O1, 0);
 296       __ sub(O1, wordSize, O1);
 297       break;
 298     default       : ShouldNotReachHere();
 299   }
 300   __ retl();
 301   __ delayed()->nop(); // QQ schedule this better
 302   return entry;
 303 }
 304 
 305 address CppInterpreterGenerator::generate_stack_to_native_abi_converter(BasicType type) {
 306   // A result is in the java expression stack of the interpreted method that has just
 307   // returned. Place this result in the native abi that the caller expects.
 308   // We are in a new frame registers we set must be in caller (i.e. callstub) frame.
 309   //
 310   // Similar to generate_stack_to_stack_converter above. Called at a similar time from the
 311   // frame manager execept in this situation the caller is native code (c1/c2/call_stub)
 312   // and so rather than return result onto caller's java expression stack we return the
 313   // result in the expected location based on the native abi.
 314   // On entry: O0 - source (stack top)
 315   // On exit result in expected output register
 316   // QQQ schedule this better
 317 
 318   address entry = __ pc();
 319   switch (type) {
 320     case T_VOID:  break;
 321       break;
 322     case T_FLOAT  :
 323       __ ldf(FloatRegisterImpl::S, O0, 0, F0);
 324       break;
 325     case T_BOOLEAN:
 326     case T_CHAR   :
 327     case T_BYTE   :
 328     case T_SHORT  :
 329     case T_INT    :
 330       // 1 word result
 331       __ ld(O0, 0, O0->after_save());
 332       break;
 333     case T_DOUBLE  :
 334       __ ldf(FloatRegisterImpl::D, O0, 0, F0);
 335       break;
 336     case T_LONG    :
 337       // return top two words on current expression stack to caller's expression stack
 338       // The caller's expression stack is adjacent to the current frame manager's interpretState
 339       // except we allocated one extra word for this intepretState so we won't overwrite it
 340       // when we return a two word result.
 341 #ifdef _LP64
 342       __ ld_ptr(O0, 0, O0->after_save());
 343 #else
 344       __ ld(O0, wordSize, O1->after_save());
 345       __ ld(O0, 0, O0->after_save());
 346 #endif
 347 #if defined(COMPILER2) && !defined(_LP64)
 348       // C2 expects long results in G1 we can't tell if we're returning to interpreted
 349       // or compiled so just be safe use G1 and O0/O1
 350 
 351       // Shift bits into high (msb) of G1
 352       __ sllx(Otos_l1->after_save(), 32, G1);
 353       // Zero extend low bits
 354       __ srl (Otos_l2->after_save(), 0, Otos_l2->after_save());
 355       __ or3 (Otos_l2->after_save(), G1, G1);
 356 #endif /* COMPILER2 */
 357       break;
 358     case T_OBJECT :
 359       __ ld_ptr(O0, 0, O0->after_save());
 360       __ verify_oop(O0->after_save());                                               // verify it
 361       break;
 362     default       : ShouldNotReachHere();
 363   }
 364   __ retl();
 365   __ delayed()->nop();
 366   return entry;
 367 }
 368 
 369 address CppInterpreter::return_entry(TosState state, int length, Bytecodes::Code code) {
 370   // make it look good in the debugger
 371   return CAST_FROM_FN_PTR(address, RecursiveInterpreterActivation) + frame::pc_return_offset;
 372 }
 373 
 374 address CppInterpreter::deopt_entry(TosState state, int length) {
 375   address ret = NULL;
 376   if (length != 0) {
 377     switch (state) {
 378       case atos: ret = deopt_frame_manager_return_atos; break;
 379       case btos: ret = deopt_frame_manager_return_btos; break;
 380       case ctos:
 381       case stos:
 382       case itos: ret = deopt_frame_manager_return_itos; break;
 383       case ltos: ret = deopt_frame_manager_return_ltos; break;
 384       case ftos: ret = deopt_frame_manager_return_ftos; break;
 385       case dtos: ret = deopt_frame_manager_return_dtos; break;
 386       case vtos: ret = deopt_frame_manager_return_vtos; break;
 387     }
 388   } else {
 389     ret = unctrap_frame_manager_entry;  // re-execute the bytecode ( e.g. uncommon trap)
 390   }
 391   assert(ret != NULL, "Not initialized");
 392   return ret;
 393 }
 394 
 395 //
 396 // Helpers for commoning out cases in the various type of method entries.
 397 //
 398 
 399 // increment invocation count & check for overflow
 400 //
 401 // Note: checking for negative value instead of overflow
 402 //       so we have a 'sticky' overflow test
 403 //
 404 // Lmethod: method
 405 // ??: invocation counter
 406 //
 407 void InterpreterGenerator::generate_counter_incr(Label* overflow, Label* profile_method, Label* profile_method_continue) {
 408   Label done;
 409   const Register Rcounters = G3_scratch;
 410 
 411   __ ld_ptr(STATE(_method), G5_method);
 412   __ get_method_counters(G5_method, Rcounters, done);
 413 
 414   // Update standard invocation counters
 415   __ increment_invocation_counter(Rcounters, O0, G4_scratch);
 416   if (ProfileInterpreter) {
 417     Address interpreter_invocation_counter(Rcounters,
 418             in_bytes(MethodCounters::interpreter_invocation_counter_offset()));
 419     __ ld(interpreter_invocation_counter, G4_scratch);
 420     __ inc(G4_scratch);
 421     __ st(G4_scratch, interpreter_invocation_counter);
 422   }
 423 
 424   AddressLiteral invocation_limit((address)&InvocationCounter::InterpreterInvocationLimit);
 425   __ load_contents(invocation_limit, G3_scratch);
 426   __ cmp(O0, G3_scratch);
 427   __ br(Assembler::greaterEqualUnsigned, false, Assembler::pn, *overflow);
 428   __ delayed()->nop();
 429   __ bind(done);
 430 }
 431 
 432 address InterpreterGenerator::generate_empty_entry(void) {
 433 
 434   // A method that does nothing but return...
 435 
 436   address entry = __ pc();
 437   Label slow_path;
 438 
 439   // do nothing for empty methods (do not even increment invocation counter)
 440   if ( UseFastEmptyMethods) {
 441     // If we need a safepoint check, generate full interpreter entry.
 442     AddressLiteral sync_state(SafepointSynchronize::address_of_state());
 443     __ load_contents(sync_state, G3_scratch);
 444     __ cmp(G3_scratch, SafepointSynchronize::_not_synchronized);
 445     __ br(Assembler::notEqual, false, Assembler::pn, frame_manager_entry);
 446     __ delayed()->nop();
 447 
 448     // Code: _return
 449     __ retl();
 450     __ delayed()->mov(O5_savedSP, SP);
 451     return entry;
 452   }
 453   return NULL;
 454 }
 455 
 456 // Call an accessor method (assuming it is resolved, otherwise drop into
 457 // vanilla (slow path) entry
 458 
 459 // Generates code to elide accessor methods
 460 // Uses G3_scratch and G1_scratch as scratch
 461 address InterpreterGenerator::generate_accessor_entry(void) {
 462 
 463   // Code: _aload_0, _(i|a)getfield, _(i|a)return or any rewrites thereof;
 464   // parameter size = 1
 465   // Note: We can only use this code if the getfield has been resolved
 466   //       and if we don't have a null-pointer exception => check for
 467   //       these conditions first and use slow path if necessary.
 468   address entry = __ pc();
 469   Label slow_path;
 470 
 471   if ( UseFastAccessorMethods) {
 472     // Check if we need to reach a safepoint and generate full interpreter
 473     // frame if so.
 474     AddressLiteral sync_state(SafepointSynchronize::address_of_state());
 475     __ load_contents(sync_state, G3_scratch);
 476     __ cmp(G3_scratch, SafepointSynchronize::_not_synchronized);
 477     __ br(Assembler::notEqual, false, Assembler::pn, slow_path);
 478     __ delayed()->nop();
 479 
 480     // Check if local 0 != NULL
 481     __ ld_ptr(Gargs, G0, Otos_i ); // get local 0
 482     __ tst(Otos_i);  // check if local 0 == NULL and go the slow path
 483     __ brx(Assembler::zero, false, Assembler::pn, slow_path);
 484     __ delayed()->nop();
 485 
 486 
 487     // read first instruction word and extract bytecode @ 1 and index @ 2
 488     // get first 4 bytes of the bytecodes (big endian!)
 489     __ ld_ptr(Address(G5_method, in_bytes(Method::const_offset())), G1_scratch);
 490     __ ld(Address(G1_scratch, in_bytes(ConstMethod::codes_offset())), G1_scratch);
 491 
 492     // move index @ 2 far left then to the right most two bytes.
 493     __ sll(G1_scratch, 2*BitsPerByte, G1_scratch);
 494     __ srl(G1_scratch, 2*BitsPerByte - exact_log2(in_words(
 495                       ConstantPoolCacheEntry::size()) * BytesPerWord), G1_scratch);
 496 
 497     // get constant pool cache
 498     __ ld_ptr(G5_method, in_bytes(Method::const_offset()), G3_scratch);
 499     __ ld_ptr(G3_scratch, in_bytes(ConstMethod::constants_offset()), G3_scratch);
 500     __ ld_ptr(G3_scratch, ConstantPool::cache_offset_in_bytes(), G3_scratch);
 501 
 502     // get specific constant pool cache entry
 503     __ add(G3_scratch, G1_scratch, G3_scratch);
 504 
 505     // Check the constant Pool cache entry to see if it has been resolved.
 506     // If not, need the slow path.
 507     ByteSize cp_base_offset = ConstantPoolCache::base_offset();
 508     __ ld_ptr(G3_scratch, in_bytes(cp_base_offset + ConstantPoolCacheEntry::indices_offset()), G1_scratch);
 509     __ srl(G1_scratch, 2*BitsPerByte, G1_scratch);
 510     __ and3(G1_scratch, 0xFF, G1_scratch);
 511     __ cmp(G1_scratch, Bytecodes::_getfield);
 512     __ br(Assembler::notEqual, false, Assembler::pn, slow_path);
 513     __ delayed()->nop();
 514 
 515     // Get the type and return field offset from the constant pool cache
 516     __ ld_ptr(G3_scratch, in_bytes(cp_base_offset + ConstantPoolCacheEntry::flags_offset()), G1_scratch);
 517     __ ld_ptr(G3_scratch, in_bytes(cp_base_offset + ConstantPoolCacheEntry::f2_offset()), G3_scratch);
 518 
 519     Label xreturn_path;
 520     // Need to differentiate between igetfield, agetfield, bgetfield etc.
 521     // because they are different sizes.
 522     // Get the type from the constant pool cache
 523     __ srl(G1_scratch, ConstantPoolCacheEntry::tos_state_shift, G1_scratch);
 524     // Make sure we don't need to mask G1_scratch after the above shift
 525     ConstantPoolCacheEntry::verify_tos_state_shift();
 526     __ cmp(G1_scratch, atos );
 527     __ br(Assembler::equal, true, Assembler::pt, xreturn_path);
 528     __ delayed()->ld_ptr(Otos_i, G3_scratch, Otos_i);
 529     __ cmp(G1_scratch, itos);
 530     __ br(Assembler::equal, true, Assembler::pt, xreturn_path);
 531     __ delayed()->ld(Otos_i, G3_scratch, Otos_i);
 532     __ cmp(G1_scratch, stos);
 533     __ br(Assembler::equal, true, Assembler::pt, xreturn_path);
 534     __ delayed()->ldsh(Otos_i, G3_scratch, Otos_i);
 535     __ cmp(G1_scratch, ctos);
 536     __ br(Assembler::equal, true, Assembler::pt, xreturn_path);
 537     __ delayed()->lduh(Otos_i, G3_scratch, Otos_i);
 538 #ifdef ASSERT
 539     __ cmp(G1_scratch, btos);
 540     __ br(Assembler::equal, true, Assembler::pt, xreturn_path);
 541     __ delayed()->ldsb(Otos_i, G3_scratch, Otos_i);
 542     __ should_not_reach_here();
 543 #endif
 544     __ ldsb(Otos_i, G3_scratch, Otos_i);
 545     __ bind(xreturn_path);
 546 
 547     // _ireturn/_areturn
 548     __ retl();                      // return from leaf routine
 549     __ delayed()->mov(O5_savedSP, SP);
 550 
 551     // Generate regular method entry
 552     __ bind(slow_path);
 553     __ ba(fast_accessor_slow_entry_path);
 554     __ delayed()->nop();
 555     return entry;
 556   }
 557   return NULL;
 558 }
 559 
 560 address InterpreterGenerator::generate_Reference_get_entry(void) {
 561 #if INCLUDE_ALL_GCS
 562   if (UseG1GC) {
 563     // We need to generate have a routine that generates code to:
 564     //   * load the value in the referent field
 565     //   * passes that value to the pre-barrier.
 566     //
 567     // In the case of G1 this will record the value of the
 568     // referent in an SATB buffer if marking is active.
 569     // This will cause concurrent marking to mark the referent
 570     // field as live.
 571     Unimplemented();
 572   }
 573 #endif // INCLUDE_ALL_GCS
 574 
 575   // If G1 is not enabled then attempt to go through the accessor entry point
 576   // Reference.get is an accessor
 577   return generate_accessor_entry();
 578 }
 579 
 580 //
 581 // Interpreter stub for calling a native method. (C++ interpreter)
 582 // This sets up a somewhat different looking stack for calling the native method
 583 // than the typical interpreter frame setup.
 584 //
 585 
 586 address InterpreterGenerator::generate_native_entry(bool synchronized) {
 587   address entry = __ pc();
 588 
 589   // the following temporary registers are used during frame creation
 590   const Register Gtmp1 = G3_scratch ;
 591   const Register Gtmp2 = G1_scratch;
 592   const Register RconstMethod = Gtmp1;
 593   const Address constMethod(G5_method, in_bytes(Method::const_offset()));
 594   const Address size_of_parameters(RconstMethod, in_bytes(ConstMethod::size_of_parameters_offset()));
 595 
 596   bool inc_counter  = UseCompiler || CountCompiledCalls;
 597 
 598   // make sure registers are different!
 599   assert_different_registers(G2_thread, G5_method, Gargs, Gtmp1, Gtmp2);
 600 
 601   const Address access_flags      (G5_method, in_bytes(Method::access_flags_offset()));
 602 
 603   Label Lentry;
 604   __ bind(Lentry);
 605 
 606   const Register Glocals_size = G3;
 607   assert_different_registers(Glocals_size, G4_scratch, Gframe_size);
 608 
 609   // make sure method is native & not abstract
 610   // rethink these assertions - they can be simplified and shared (gri 2/25/2000)
 611 #ifdef ASSERT
 612   __ ld(access_flags, Gtmp1);
 613   {
 614     Label L;
 615     __ btst(JVM_ACC_NATIVE, Gtmp1);
 616     __ br(Assembler::notZero, false, Assembler::pt, L);
 617     __ delayed()->nop();
 618     __ stop("tried to execute non-native method as native");
 619     __ bind(L);
 620   }
 621   { Label L;
 622     __ btst(JVM_ACC_ABSTRACT, Gtmp1);
 623     __ br(Assembler::zero, false, Assembler::pt, L);
 624     __ delayed()->nop();
 625     __ stop("tried to execute abstract method as non-abstract");
 626     __ bind(L);
 627   }
 628 #endif // ASSERT
 629 
 630   __ ld_ptr(constMethod, RconstMethod);
 631   __ lduh(size_of_parameters, Gtmp1);
 632   __ sll(Gtmp1, LogBytesPerWord, Gtmp2);       // parameter size in bytes
 633   __ add(Gargs, Gtmp2, Gargs);                 // points to first local + BytesPerWord
 634   // NEW
 635   __ add(Gargs, -wordSize, Gargs);             // points to first local[0]
 636   // generate the code to allocate the interpreter stack frame
 637   // NEW FRAME ALLOCATED HERE
 638   // save callers original sp
 639   // __ mov(SP, I5_savedSP->after_restore());
 640 
 641   generate_compute_interpreter_state(Lstate, G0, true);
 642 
 643   // At this point Lstate points to new interpreter state
 644   //
 645 
 646   const Address do_not_unlock_if_synchronized(G2_thread,
 647       in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
 648   // Since at this point in the method invocation the exception handler
 649   // would try to exit the monitor of synchronized methods which hasn't
 650   // been entered yet, we set the thread local variable
 651   // _do_not_unlock_if_synchronized to true. If any exception was thrown by
 652   // runtime, exception handling i.e. unlock_if_synchronized_method will
 653   // check this thread local flag.
 654   // This flag has two effects, one is to force an unwind in the topmost
 655   // interpreter frame and not perform an unlock while doing so.
 656 
 657   __ movbool(true, G3_scratch);
 658   __ stbool(G3_scratch, do_not_unlock_if_synchronized);
 659 
 660 
 661   // increment invocation counter and check for overflow
 662   //
 663   // Note: checking for negative value instead of overflow
 664   //       so we have a 'sticky' overflow test (may be of
 665   //       importance as soon as we have true MT/MP)
 666   Label invocation_counter_overflow;
 667   if (inc_counter) {
 668     generate_counter_incr(&invocation_counter_overflow, NULL, NULL);
 669   }
 670   Label Lcontinue;
 671   __ bind(Lcontinue);
 672 
 673   bang_stack_shadow_pages(true);
 674   // reset the _do_not_unlock_if_synchronized flag
 675   __ stbool(G0, do_not_unlock_if_synchronized);
 676 
 677   // check for synchronized methods
 678   // Must happen AFTER invocation_counter check, so method is not locked
 679   // if counter overflows.
 680 
 681   if (synchronized) {
 682     lock_method();
 683     // Don't see how G2_thread is preserved here...
 684     // __ verify_thread(); QQQ destroys L0,L1 can't use
 685   } else {
 686 #ifdef ASSERT
 687     { Label ok;
 688       __ ld_ptr(STATE(_method), G5_method);
 689       __ ld(access_flags, O0);
 690       __ btst(JVM_ACC_SYNCHRONIZED, O0);
 691       __ br( Assembler::zero, false, Assembler::pt, ok);
 692       __ delayed()->nop();
 693       __ stop("method needs synchronization");
 694       __ bind(ok);
 695     }
 696 #endif // ASSERT
 697   }
 698 
 699   // start execution
 700 
 701 //   __ verify_thread(); kills L1,L2 can't  use at the moment
 702 
 703   // jvmti/jvmpi support
 704   __ notify_method_entry();
 705 
 706   // native call
 707 
 708   // (note that O0 is never an oop--at most it is a handle)
 709   // It is important not to smash any handles created by this call,
 710   // until any oop handle in O0 is dereferenced.
 711 
 712   // (note that the space for outgoing params is preallocated)
 713 
 714   // get signature handler
 715 
 716   Label pending_exception_present;
 717 
 718   { Label L;
 719     __ ld_ptr(STATE(_method), G5_method);
 720     __ ld_ptr(Address(G5_method, in_bytes(Method::signature_handler_offset())), G3_scratch);
 721     __ tst(G3_scratch);
 722     __ brx(Assembler::notZero, false, Assembler::pt, L);
 723     __ delayed()->nop();
 724     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::prepare_native_call), G5_method, false);
 725     __ ld_ptr(STATE(_method), G5_method);
 726 
 727     Address exception_addr(G2_thread, in_bytes(Thread::pending_exception_offset()));
 728     __ ld_ptr(exception_addr, G3_scratch);
 729     __ br_notnull_short(G3_scratch, Assembler::pn, pending_exception_present);
 730     __ ld_ptr(Address(G5_method, in_bytes(Method::signature_handler_offset())), G3_scratch);
 731     __ bind(L);
 732   }
 733 
 734   // Push a new frame so that the args will really be stored in
 735   // Copy a few locals across so the new frame has the variables
 736   // we need but these values will be dead at the jni call and
 737   // therefore not gc volatile like the values in the current
 738   // frame (Lstate in particular)
 739 
 740   // Flush the state pointer to the register save area
 741   // Which is the only register we need for a stack walk.
 742   __ st_ptr(Lstate, SP, (Lstate->sp_offset_in_saved_window() * wordSize) + STACK_BIAS);
 743 
 744   __ mov(Lstate, O1);         // Need to pass the state pointer across the frame
 745 
 746   // Calculate current frame size
 747   __ sub(SP, FP, O3);         // Calculate negative of current frame size
 748   __ save(SP, O3, SP);        // Allocate an identical sized frame
 749 
 750   __ mov(I1, Lstate);          // In the "natural" register.
 751 
 752   // Note I7 has leftover trash. Slow signature handler will fill it in
 753   // should we get there. Normal jni call will set reasonable last_Java_pc
 754   // below (and fix I7 so the stack trace doesn't have a meaningless frame
 755   // in it).
 756 
 757 
 758   // call signature handler
 759   __ ld_ptr(STATE(_method), Lmethod);
 760   __ ld_ptr(STATE(_locals), Llocals);
 761 
 762   __ callr(G3_scratch, 0);
 763   __ delayed()->nop();
 764   __ ld_ptr(STATE(_thread), G2_thread);        // restore thread (shouldn't be needed)
 765 
 766   { Label not_static;
 767 
 768     __ ld_ptr(STATE(_method), G5_method);
 769     __ ld(access_flags, O0);
 770     __ btst(JVM_ACC_STATIC, O0);
 771     __ br( Assembler::zero, false, Assembler::pt, not_static);
 772     __ delayed()->
 773       // get native function entry point(O0 is a good temp until the very end)
 774        ld_ptr(Address(G5_method, in_bytes(Method::native_function_offset())), O0);
 775     // for static methods insert the mirror argument
 776     const int mirror_offset = in_bytes(Klass::java_mirror_offset());
 777 
 778     __ ld_ptr(Address(G5_method, in_bytes(Method:: const_offset())), O1);
 779     __ ld_ptr(Address(O1, in_bytes(ConstMethod::constants_offset())), O1);
 780     __ ld_ptr(Address(O1, ConstantPool::pool_holder_offset_in_bytes()), O1);
 781     __ ld_ptr(O1, mirror_offset, O1);
 782     // where the mirror handle body is allocated:
 783 #ifdef ASSERT
 784     if (!PrintSignatureHandlers)  // do not dirty the output with this
 785     { Label L;
 786       __ tst(O1);
 787       __ brx(Assembler::notZero, false, Assembler::pt, L);
 788       __ delayed()->nop();
 789       __ stop("mirror is missing");
 790       __ bind(L);
 791     }
 792 #endif // ASSERT
 793     __ st_ptr(O1, STATE(_oop_temp));
 794     __ add(STATE(_oop_temp), O1);            // this is really an LEA not an add
 795     __ bind(not_static);
 796   }
 797 
 798   // At this point, arguments have been copied off of stack into
 799   // their JNI positions, which are O1..O5 and SP[68..].
 800   // Oops are boxed in-place on the stack, with handles copied to arguments.
 801   // The result handler is in Lscratch.  O0 will shortly hold the JNIEnv*.
 802 
 803 #ifdef ASSERT
 804   { Label L;
 805     __ tst(O0);
 806     __ brx(Assembler::notZero, false, Assembler::pt, L);
 807     __ delayed()->nop();
 808     __ stop("native entry point is missing");
 809     __ bind(L);
 810   }
 811 #endif // ASSERT
 812 
 813   //
 814   // setup the java frame anchor
 815   //
 816   // The scavenge function only needs to know that the PC of this frame is
 817   // in the interpreter method entry code, it doesn't need to know the exact
 818   // PC and hence we can use O7 which points to the return address from the
 819   // previous call in the code stream (signature handler function)
 820   //
 821   // The other trick is we set last_Java_sp to FP instead of the usual SP because
 822   // we have pushed the extra frame in order to protect the volatile register(s)
 823   // in that frame when we return from the jni call
 824   //
 825 
 826 
 827   __ set_last_Java_frame(FP, O7);
 828   __ mov(O7, I7);  // make dummy interpreter frame look like one above,
 829                    // not meaningless information that'll confuse me.
 830 
 831   // flush the windows now. We don't care about the current (protection) frame
 832   // only the outer frames
 833 
 834   __ flushw();
 835 
 836   // mark windows as flushed
 837   Address flags(G2_thread,
 838                 in_bytes(JavaThread::frame_anchor_offset()) + in_bytes(JavaFrameAnchor::flags_offset()));
 839   __ set(JavaFrameAnchor::flushed, G3_scratch);
 840   __ st(G3_scratch, flags);
 841 
 842   // Transition from _thread_in_Java to _thread_in_native. We are already safepoint ready.
 843 
 844   Address thread_state(G2_thread, in_bytes(JavaThread::thread_state_offset()));
 845 #ifdef ASSERT
 846   { Label L;
 847     __ ld(thread_state, G3_scratch);
 848     __ cmp(G3_scratch, _thread_in_Java);
 849     __ br(Assembler::equal, false, Assembler::pt, L);
 850     __ delayed()->nop();
 851     __ stop("Wrong thread state in native stub");
 852     __ bind(L);
 853   }
 854 #endif // ASSERT
 855   __ set(_thread_in_native, G3_scratch);
 856   __ st(G3_scratch, thread_state);
 857 
 858   // Call the jni method, using the delay slot to set the JNIEnv* argument.
 859   __ callr(O0, 0);
 860   __ delayed()->
 861      add(G2_thread, in_bytes(JavaThread::jni_environment_offset()), O0);
 862   __ ld_ptr(STATE(_thread), G2_thread);  // restore thread
 863 
 864   // must we block?
 865 
 866   // Block, if necessary, before resuming in _thread_in_Java state.
 867   // In order for GC to work, don't clear the last_Java_sp until after blocking.
 868   { Label no_block;
 869     AddressLiteral sync_state(SafepointSynchronize::address_of_state());
 870 
 871     // Switch thread to "native transition" state before reading the synchronization state.
 872     // This additional state is necessary because reading and testing the synchronization
 873     // state is not atomic w.r.t. GC, as this scenario demonstrates:
 874     //     Java thread A, in _thread_in_native state, loads _not_synchronized and is preempted.
 875     //     VM thread changes sync state to synchronizing and suspends threads for GC.
 876     //     Thread A is resumed to finish this native method, but doesn't block here since it
 877     //     didn't see any synchronization is progress, and escapes.
 878     __ set(_thread_in_native_trans, G3_scratch);
 879     __ st(G3_scratch, thread_state);
 880     if(os::is_MP()) {
 881       // Write serialization page so VM thread can do a pseudo remote membar.
 882       // We use the current thread pointer to calculate a thread specific
 883       // offset to write to within the page. This minimizes bus traffic
 884       // due to cache line collision.
 885       __ serialize_memory(G2_thread, G1_scratch, G3_scratch);
 886     }
 887     __ load_contents(sync_state, G3_scratch);
 888     __ cmp(G3_scratch, SafepointSynchronize::_not_synchronized);
 889 
 890 
 891     Label L;
 892     Address suspend_state(G2_thread, in_bytes(JavaThread::suspend_flags_offset()));
 893     __ br(Assembler::notEqual, false, Assembler::pn, L);
 894     __ delayed()->
 895       ld(suspend_state, G3_scratch);
 896     __ cmp(G3_scratch, 0);
 897     __ br(Assembler::equal, false, Assembler::pt, no_block);
 898     __ delayed()->nop();
 899     __ bind(L);
 900 
 901     // Block.  Save any potential method result value before the operation and
 902     // use a leaf call to leave the last_Java_frame setup undisturbed.
 903     save_native_result();
 904     __ call_VM_leaf(noreg,
 905                     CAST_FROM_FN_PTR(address, JavaThread::check_safepoint_and_suspend_for_native_trans),
 906                     G2_thread);
 907     __ ld_ptr(STATE(_thread), G2_thread);  // restore thread
 908     // Restore any method result value
 909     restore_native_result();
 910     __ bind(no_block);
 911   }
 912 
 913   // Clear the frame anchor now
 914 
 915   __ reset_last_Java_frame();
 916 
 917   // Move the result handler address
 918   __ mov(Lscratch, G3_scratch);
 919   // return possible result to the outer frame
 920 #ifndef __LP64
 921   __ mov(O0, I0);
 922   __ restore(O1, G0, O1);
 923 #else
 924   __ restore(O0, G0, O0);
 925 #endif /* __LP64 */
 926 
 927   // Move result handler to expected register
 928   __ mov(G3_scratch, Lscratch);
 929 
 930 
 931   // thread state is thread_in_native_trans. Any safepoint blocking has
 932   // happened in the trampoline we are ready to switch to thread_in_Java.
 933 
 934   __ set(_thread_in_Java, G3_scratch);
 935   __ st(G3_scratch, thread_state);
 936 
 937   // If we have an oop result store it where it will be safe for any further gc
 938   // until we return now that we've released the handle it might be protected by
 939 
 940   {
 941     Label no_oop, store_result;
 942 
 943     __ set((intptr_t)AbstractInterpreter::result_handler(T_OBJECT), G3_scratch);
 944     __ cmp(G3_scratch, Lscratch);
 945     __ brx(Assembler::notEqual, false, Assembler::pt, no_oop);
 946     __ delayed()->nop();
 947     __ addcc(G0, O0, O0);
 948     __ brx(Assembler::notZero, true, Assembler::pt, store_result);     // if result is not NULL:
 949     __ delayed()->ld_ptr(O0, 0, O0);                                   // unbox it
 950     __ mov(G0, O0);
 951 
 952     __ bind(store_result);
 953     // Store it where gc will look for it and result handler expects it.
 954     __ st_ptr(O0, STATE(_oop_temp));
 955 
 956     __ bind(no_oop);
 957 
 958   }
 959 
 960   // reset handle block
 961   __ ld_ptr(G2_thread, in_bytes(JavaThread::active_handles_offset()), G3_scratch);
 962   __ st_ptr(G0, G3_scratch, JNIHandleBlock::top_offset_in_bytes());
 963 
 964 
 965   // handle exceptions (exception handling will handle unlocking!)
 966   { Label L;
 967     Address exception_addr (G2_thread, in_bytes(Thread::pending_exception_offset()));
 968 
 969     __ ld_ptr(exception_addr, Gtemp);
 970     __ tst(Gtemp);
 971     __ brx(Assembler::equal, false, Assembler::pt, L);
 972     __ delayed()->nop();
 973     __ bind(pending_exception_present);
 974     // With c++ interpreter we just leave it pending caller will do the correct thing. However...
 975     // Like x86 we ignore the result of the native call and leave the method locked. This
 976     // seems wrong to leave things locked.
 977 
 978     __ br(Assembler::always, false, Assembler::pt, StubRoutines::forward_exception_entry(), relocInfo::runtime_call_type);
 979     __ delayed()->restore(I5_savedSP, G0, SP);  // remove interpreter frame
 980 
 981     __ bind(L);
 982   }
 983 
 984   // jvmdi/jvmpi support (preserves thread register)
 985   __ notify_method_exit(true, ilgl, InterpreterMacroAssembler::NotifyJVMTI);
 986 
 987   if (synchronized) {
 988     // save and restore any potential method result value around the unlocking operation
 989     save_native_result();
 990 
 991     const int entry_size            = frame::interpreter_frame_monitor_size() * wordSize;
 992     // Get the initial monitor we allocated
 993     __ sub(Lstate, entry_size, O1);                        // initial monitor
 994     __ unlock_object(O1);
 995     restore_native_result();
 996   }
 997 
 998 #if defined(COMPILER2) && !defined(_LP64)
 999 
1000   // C2 expects long results in G1 we can't tell if we're returning to interpreted
1001   // or compiled so just be safe.
1002 
1003   __ sllx(O0, 32, G1);          // Shift bits into high G1
1004   __ srl (O1, 0, O1);           // Zero extend O1
1005   __ or3 (O1, G1, G1);          // OR 64 bits into G1
1006 
1007 #endif /* COMPILER2 && !_LP64 */
1008 
1009 #ifdef ASSERT
1010   {
1011     Label ok;
1012     __ cmp(I5_savedSP, FP);
1013     __ brx(Assembler::greaterEqualUnsigned, false, Assembler::pt, ok);
1014     __ delayed()->nop();
1015     __ stop("bad I5_savedSP value");
1016     __ should_not_reach_here();
1017     __ bind(ok);
1018   }
1019 #endif
1020   // Calls result handler which POPS FRAME
1021   if (TraceJumps) {
1022     // Move target to register that is recordable
1023     __ mov(Lscratch, G3_scratch);
1024     __ JMP(G3_scratch, 0);
1025   } else {
1026     __ jmp(Lscratch, 0);
1027   }
1028   __ delayed()->nop();
1029 
1030   if (inc_counter) {
1031     // handle invocation counter overflow
1032     __ bind(invocation_counter_overflow);
1033     generate_counter_overflow(Lcontinue);
1034   }
1035 
1036 
1037   return entry;
1038 }
1039 
1040 void CppInterpreterGenerator::generate_compute_interpreter_state(const Register state,
1041                                                               const Register prev_state,
1042                                                               bool native) {
1043 
1044   // On entry
1045   // G5_method - caller's method
1046   // Gargs - points to initial parameters (i.e. locals[0])
1047   // G2_thread - valid? (C1 only??)
1048   // "prev_state" - contains any previous frame manager state which we must save a link
1049   //
1050   // On return
1051   // "state" is a pointer to the newly allocated  state object. We must allocate and initialize
1052   // a new interpretState object and the method expression stack.
1053 
1054   assert_different_registers(state, prev_state);
1055   assert_different_registers(prev_state, G3_scratch);
1056   const Register Gtmp = G3_scratch;
1057   const Address constMethod       (G5_method, in_bytes(Method::const_offset()));
1058   const Address access_flags      (G5_method, in_bytes(Method::access_flags_offset()));
1059 
1060   // slop factor is two extra slots on the expression stack so that
1061   // we always have room to store a result when returning from a call without parameters
1062   // that returns a result.
1063 
1064   const int slop_factor = 2*wordSize;
1065 
1066   const int fixed_size = ((sizeof(BytecodeInterpreter) + slop_factor) >> LogBytesPerWord) + // what is the slop factor?
1067                          Method::extra_stack_entries() + // extra stack for jsr 292
1068                          frame::memory_parameter_word_sp_offset +  // register save area + param window
1069                          (native ?  frame::interpreter_frame_extra_outgoing_argument_words : 0); // JNI, class
1070 
1071   // XXX G5_method valid
1072 
1073   // Now compute new frame size
1074 
1075   if (native) {
1076     const Register RconstMethod = Gtmp;
1077     const Address size_of_parameters(RconstMethod, in_bytes(ConstMethod::size_of_parameters_offset()));
1078     __ ld_ptr(constMethod, RconstMethod);
1079     __ lduh( size_of_parameters, Gtmp );
1080     __ calc_mem_param_words(Gtmp, Gtmp);     // space for native call parameters passed on the stack in words
1081   } else {
1082     // Full size expression stack
1083     __ ld_ptr(constMethod, Gtmp);
1084     __ lduh(Gtmp, in_bytes(ConstMethod::max_stack_offset()), Gtmp);
1085   }
1086   __ add(Gtmp, fixed_size, Gtmp);           // plus the fixed portion
1087 
1088   __ neg(Gtmp);                               // negative space for stack/parameters in words
1089   __ and3(Gtmp, -WordsPerLong, Gtmp);        // make multiple of 2 (SP must be 2-word aligned)
1090   __ sll(Gtmp, LogBytesPerWord, Gtmp);       // negative space for frame in bytes
1091 
1092   // Need to do stack size check here before we fault on large frames
1093 
1094   Label stack_ok;
1095 
1096   const int max_pages = StackShadowPages > (StackRedPages+StackYellowPages) ? StackShadowPages :
1097                                                                               (StackRedPages+StackYellowPages);
1098 
1099 
1100   __ ld_ptr(G2_thread, in_bytes(Thread::stack_base_offset()), O0);
1101   __ ld_ptr(G2_thread, in_bytes(Thread::stack_size_offset()), O1);
1102   // compute stack bottom
1103   __ sub(O0, O1, O0);
1104 
1105   // Avoid touching the guard pages
1106   // Also a fudge for frame size of BytecodeInterpreter::run
1107   // It varies from 1k->4k depending on build type
1108   const int fudge = 6 * K;
1109 
1110   __ set(fudge + (max_pages * os::vm_page_size()), O1);
1111 
1112   __ add(O0, O1, O0);
1113   __ sub(O0, Gtmp, O0);
1114   __ cmp(SP, O0);
1115   __ brx(Assembler::greaterUnsigned, false, Assembler::pt, stack_ok);
1116   __ delayed()->nop();
1117 
1118      // throw exception return address becomes throwing pc
1119 
1120   __ call_VM(Oexception, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_StackOverflowError));
1121   __ stop("never reached");
1122 
1123   __ bind(stack_ok);
1124 
1125   __ save(SP, Gtmp, SP);                      // setup new frame and register window
1126 
1127   // New window I7 call_stub or previous activation
1128   // O6 - register save area, BytecodeInterpreter just below it, args/locals just above that
1129   //
1130   __ sub(FP, sizeof(BytecodeInterpreter), state);        // Point to new Interpreter state
1131   __ add(state, STACK_BIAS, state );         // Account for 64bit bias
1132 
1133 #define XXX_STATE(field_name) state, in_bytes(byte_offset_of(BytecodeInterpreter, field_name))
1134 
1135   // Initialize a new Interpreter state
1136   // orig_sp - caller's original sp
1137   // G2_thread - thread
1138   // Gargs - &locals[0] (unbiased?)
1139   // G5_method - method
1140   // SP (biased) - accounts for full size java stack, BytecodeInterpreter object, register save area, and register parameter save window
1141 
1142 
1143   __ set(0xdead0004, O1);
1144 
1145 
1146   __ st_ptr(Gargs, XXX_STATE(_locals));
1147   __ st_ptr(G0, XXX_STATE(_oop_temp));
1148 
1149   __ st_ptr(state, XXX_STATE(_self_link));                // point to self
1150   __ st_ptr(prev_state->after_save(), XXX_STATE(_prev_link)); // Chain interpreter states
1151   __ st_ptr(G2_thread, XXX_STATE(_thread));               // Store javathread
1152 
1153   if (native) {
1154     __ st_ptr(G0, XXX_STATE(_bcp));
1155   } else {
1156     __ ld_ptr(G5_method, in_bytes(Method::const_offset()), O2); // get ConstMethod*
1157     __ add(O2, in_bytes(ConstMethod::codes_offset()), O2);        // get bcp
1158     __ st_ptr(O2, XXX_STATE(_bcp));
1159   }
1160 
1161   __ st_ptr(G0, XXX_STATE(_mdx));
1162   __ st_ptr(G5_method, XXX_STATE(_method));
1163 
1164   __ set((int) BytecodeInterpreter::method_entry, O1);
1165   __ st(O1, XXX_STATE(_msg));
1166 
1167   __ ld_ptr(constMethod, O3);
1168   __ ld_ptr(O3, in_bytes(ConstMethod::constants_offset()), O3);
1169   __ ld_ptr(O3, ConstantPool::cache_offset_in_bytes(), O2);
1170   __ st_ptr(O2, XXX_STATE(_constants));
1171 
1172   __ st_ptr(G0, XXX_STATE(_result._to_call._callee));
1173 
1174   // Monitor base is just start of BytecodeInterpreter object;
1175   __ mov(state, O2);
1176   __ st_ptr(O2, XXX_STATE(_monitor_base));
1177 
1178   // Do we need a monitor for synchonized method?
1179   {
1180     __ ld(access_flags, O1);
1181     Label done;
1182     Label got_obj;
1183     __ btst(JVM_ACC_SYNCHRONIZED, O1);
1184     __ br( Assembler::zero, false, Assembler::pt, done);
1185 
1186     const int mirror_offset = in_bytes(Klass::java_mirror_offset());
1187     __ delayed()->btst(JVM_ACC_STATIC, O1);
1188     __ ld_ptr(XXX_STATE(_locals), O1);
1189     __ br( Assembler::zero, true, Assembler::pt, got_obj);
1190     __ delayed()->ld_ptr(O1, 0, O1);                  // get receiver for not-static case
1191     __ ld_ptr(constMethod, O1);
1192     __ ld_ptr( O1, in_bytes(ConstMethod::constants_offset()), O1);
1193     __ ld_ptr( O1, ConstantPool::pool_holder_offset_in_bytes(), O1);
1194     // lock the mirror, not the Klass*
1195     __ ld_ptr( O1, mirror_offset, O1);
1196 
1197     __ bind(got_obj);
1198 
1199   #ifdef ASSERT
1200     __ tst(O1);
1201     __ breakpoint_trap(Assembler::zero, Assembler::ptr_cc);
1202   #endif // ASSERT
1203 
1204     const int entry_size            = frame::interpreter_frame_monitor_size() * wordSize;
1205     __ sub(SP, entry_size, SP);                         // account for initial monitor
1206     __ sub(O2, entry_size, O2);                        // initial monitor
1207     __ st_ptr(O1, O2, BasicObjectLock::obj_offset_in_bytes()); // and allocate it for interpreter use
1208     __ bind(done);
1209   }
1210 
1211   // Remember initial frame bottom
1212 
1213   __ st_ptr(SP, XXX_STATE(_frame_bottom));
1214 
1215   __ st_ptr(O2, XXX_STATE(_stack_base));
1216 
1217   __ sub(O2, wordSize, O2);                    // prepush
1218   __ st_ptr(O2, XXX_STATE(_stack));                // PREPUSH
1219 
1220   // Full size expression stack
1221   __ ld_ptr(constMethod, O3);
1222   __ lduh(O3, in_bytes(ConstMethod::max_stack_offset()), O3);
1223   __ inc(O3, Method::extra_stack_entries());
1224   __ sll(O3, LogBytesPerWord, O3);
1225   __ sub(O2, O3, O3);
1226 //  __ sub(O3, wordSize, O3);                    // so prepush doesn't look out of bounds
1227   __ st_ptr(O3, XXX_STATE(_stack_limit));
1228 
1229   if (!native) {
1230     //
1231     // Code to initialize locals
1232     //
1233     Register init_value = noreg;    // will be G0 if we must clear locals
1234     // Now zero locals
1235     if (true /* zerolocals */ || ClearInterpreterLocals) {
1236       // explicitly initialize locals
1237       init_value = G0;
1238     } else {
1239     #ifdef ASSERT
1240       // initialize locals to a garbage pattern for better debugging
1241       init_value = O3;
1242       __ set( 0x0F0F0F0F, init_value );
1243     #endif // ASSERT
1244     }
1245     if (init_value != noreg) {
1246       Label clear_loop;
1247       const Register RconstMethod = O1;
1248       const Address size_of_parameters(RconstMethod, in_bytes(ConstMethod::size_of_parameters_offset()));
1249       const Address size_of_locals    (RconstMethod, in_bytes(ConstMethod::size_of_locals_offset()));
1250 
1251       // NOTE: If you change the frame layout, this code will need to
1252       // be updated!
1253       __ ld_ptr( constMethod, RconstMethod );
1254       __ lduh( size_of_locals, O2 );
1255       __ lduh( size_of_parameters, O1 );
1256       __ sll( O2, LogBytesPerWord, O2);
1257       __ sll( O1, LogBytesPerWord, O1 );
1258       __ ld_ptr(XXX_STATE(_locals), L2_scratch);
1259       __ sub( L2_scratch, O2, O2 );
1260       __ sub( L2_scratch, O1, O1 );
1261 
1262       __ bind( clear_loop );
1263       __ inc( O2, wordSize );
1264 
1265       __ cmp( O2, O1 );
1266       __ br( Assembler::lessEqualUnsigned, true, Assembler::pt, clear_loop );
1267       __ delayed()->st_ptr( init_value, O2, 0 );
1268     }
1269   }
1270 }
1271 // Find preallocated  monitor and lock method (C++ interpreter)
1272 //
1273 void InterpreterGenerator::lock_method(void) {
1274 // Lock the current method.
1275 // Destroys registers L2_scratch, L3_scratch, O0
1276 //
1277 // Find everything relative to Lstate
1278 
1279 #ifdef ASSERT
1280   __ ld_ptr(STATE(_method), L2_scratch);
1281   __ ld(L2_scratch, in_bytes(Method::access_flags_offset()), O0);
1282 
1283  { Label ok;
1284    __ btst(JVM_ACC_SYNCHRONIZED, O0);
1285    __ br( Assembler::notZero, false, Assembler::pt, ok);
1286    __ delayed()->nop();
1287    __ stop("method doesn't need synchronization");
1288    __ bind(ok);
1289   }
1290 #endif // ASSERT
1291 
1292   // monitor is already allocated at stack base
1293   // and the lockee is already present
1294   __ ld_ptr(STATE(_stack_base), L2_scratch);
1295   __ ld_ptr(L2_scratch, BasicObjectLock::obj_offset_in_bytes(), O0);   // get object
1296   __ lock_object(L2_scratch, O0);
1297 
1298 }
1299 
1300 //  Generate code for handling resuming a deopted method
1301 void CppInterpreterGenerator::generate_deopt_handling() {
1302 
1303   Label return_from_deopt_common;
1304 
1305   // deopt needs to jump to here to enter the interpreter (return a result)
1306   deopt_frame_manager_return_atos  = __ pc();
1307 
1308   // O0/O1 live
1309   __ ba(return_from_deopt_common);
1310   __ delayed()->set(AbstractInterpreter::BasicType_as_index(T_OBJECT), L3_scratch);    // Result stub address array index
1311 
1312 
1313   // deopt needs to jump to here to enter the interpreter (return a result)
1314   deopt_frame_manager_return_btos  = __ pc();
1315 
1316   // O0/O1 live
1317   __ ba(return_from_deopt_common);
1318   __ delayed()->set(AbstractInterpreter::BasicType_as_index(T_BOOLEAN), L3_scratch);    // Result stub address array index
1319 
1320   // deopt needs to jump to here to enter the interpreter (return a result)
1321   deopt_frame_manager_return_itos  = __ pc();
1322 
1323   // O0/O1 live
1324   __ ba(return_from_deopt_common);
1325   __ delayed()->set(AbstractInterpreter::BasicType_as_index(T_INT), L3_scratch);    // Result stub address array index
1326 
1327   // deopt needs to jump to here to enter the interpreter (return a result)
1328 
1329   deopt_frame_manager_return_ltos  = __ pc();
1330 #if !defined(_LP64) && defined(COMPILER2)
1331   // All return values are where we want them, except for Longs.  C2 returns
1332   // longs in G1 in the 32-bit build whereas the interpreter wants them in O0/O1.
1333   // Since the interpreter will return longs in G1 and O0/O1 in the 32bit
1334   // build even if we are returning from interpreted we just do a little
1335   // stupid shuffing.
1336   // Note: I tried to make c2 return longs in O0/O1 and G1 so we wouldn't have to
1337   // do this here. Unfortunately if we did a rethrow we'd see an machepilog node
1338   // first which would move g1 -> O0/O1 and destroy the exception we were throwing.
1339 
1340   __ srl (G1, 0,O1);
1341   __ srlx(G1,32,O0);
1342 #endif /* !_LP64 && COMPILER2 */
1343   // O0/O1 live
1344   __ ba(return_from_deopt_common);
1345   __ delayed()->set(AbstractInterpreter::BasicType_as_index(T_LONG), L3_scratch);    // Result stub address array index
1346 
1347   // deopt needs to jump to here to enter the interpreter (return a result)
1348 
1349   deopt_frame_manager_return_ftos  = __ pc();
1350   // O0/O1 live
1351   __ ba(return_from_deopt_common);
1352   __ delayed()->set(AbstractInterpreter::BasicType_as_index(T_FLOAT), L3_scratch);    // Result stub address array index
1353 
1354   // deopt needs to jump to here to enter the interpreter (return a result)
1355   deopt_frame_manager_return_dtos  = __ pc();
1356 
1357   // O0/O1 live
1358   __ ba(return_from_deopt_common);
1359   __ delayed()->set(AbstractInterpreter::BasicType_as_index(T_DOUBLE), L3_scratch);    // Result stub address array index
1360 
1361   // deopt needs to jump to here to enter the interpreter (return a result)
1362   deopt_frame_manager_return_vtos  = __ pc();
1363 
1364   // O0/O1 live
1365   __ set(AbstractInterpreter::BasicType_as_index(T_VOID), L3_scratch);
1366 
1367   // Deopt return common
1368   // an index is present that lets us move any possible result being
1369   // return to the interpreter's stack
1370   //
1371   __ bind(return_from_deopt_common);
1372 
1373   // Result if any is in native abi result (O0..O1/F0..F1). The java expression
1374   // stack is in the state that the  calling convention left it.
1375   // Copy the result from native abi result and place it on java expression stack.
1376 
1377   // Current interpreter state is present in Lstate
1378 
1379   // Get current pre-pushed top of interpreter stack
1380   // Any result (if any) is in native abi
1381   // result type index is in L3_scratch
1382 
1383   __ ld_ptr(STATE(_stack), L1_scratch);                                          // get top of java expr stack
1384 
1385   __ set((intptr_t)CppInterpreter::_tosca_to_stack, L4_scratch);
1386   __ sll(L3_scratch, LogBytesPerWord, L3_scratch);
1387   __ ld_ptr(L4_scratch, L3_scratch, Lscratch);                                       // get typed result converter address
1388   __ jmpl(Lscratch, G0, O7);                                         // and convert it
1389   __ delayed()->nop();
1390 
1391   // L1_scratch points to top of stack (prepushed)
1392   __ st_ptr(L1_scratch, STATE(_stack));
1393 }
1394 
1395 // Generate the code to handle a more_monitors message from the c++ interpreter
1396 void CppInterpreterGenerator::generate_more_monitors() {
1397 
1398   Label entry, loop;
1399   const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
1400   // 1. compute new pointers                                // esp: old expression stack top
1401   __ delayed()->ld_ptr(STATE(_stack_base), L4_scratch);            // current expression stack bottom
1402   __ sub(L4_scratch, entry_size, L4_scratch);
1403   __ st_ptr(L4_scratch, STATE(_stack_base));
1404 
1405   __ sub(SP, entry_size, SP);                  // Grow stack
1406   __ st_ptr(SP, STATE(_frame_bottom));
1407 
1408   __ ld_ptr(STATE(_stack_limit), L2_scratch);
1409   __ sub(L2_scratch, entry_size, L2_scratch);
1410   __ st_ptr(L2_scratch, STATE(_stack_limit));
1411 
1412   __ ld_ptr(STATE(_stack), L1_scratch);                // Get current stack top
1413   __ sub(L1_scratch, entry_size, L1_scratch);
1414   __ st_ptr(L1_scratch, STATE(_stack));
1415   __ ba(entry);
1416   __ delayed()->add(L1_scratch, wordSize, L1_scratch);        // first real entry (undo prepush)
1417 
1418   // 2. move expression stack
1419 
1420   __ bind(loop);
1421   __ st_ptr(L3_scratch, Address(L1_scratch, 0));
1422   __ add(L1_scratch, wordSize, L1_scratch);
1423   __ bind(entry);
1424   __ cmp(L1_scratch, L4_scratch);
1425   __ br(Assembler::notEqual, false, Assembler::pt, loop);
1426   __ delayed()->ld_ptr(L1_scratch, entry_size, L3_scratch);
1427 
1428   // now zero the slot so we can find it.
1429   __ st_ptr(G0, L4_scratch, BasicObjectLock::obj_offset_in_bytes());
1430 
1431 }
1432 
1433 // Initial entry to C++ interpreter from the call_stub.
1434 // This entry point is called the frame manager since it handles the generation
1435 // of interpreter activation frames via requests directly from the vm (via call_stub)
1436 // and via requests from the interpreter. The requests from the call_stub happen
1437 // directly thru the entry point. Requests from the interpreter happen via returning
1438 // from the interpreter and examining the message the interpreter has returned to
1439 // the frame manager. The frame manager can take the following requests:
1440 
1441 // NO_REQUEST - error, should never happen.
1442 // MORE_MONITORS - need a new monitor. Shuffle the expression stack on down and
1443 //                 allocate a new monitor.
1444 // CALL_METHOD - setup a new activation to call a new method. Very similar to what
1445 //               happens during entry during the entry via the call stub.
1446 // RETURN_FROM_METHOD - remove an activation. Return to interpreter or call stub.
1447 //
1448 // Arguments:
1449 //
1450 // ebx: Method*
1451 // ecx: receiver - unused (retrieved from stack as needed)
1452 // esi: previous frame manager state (NULL from the call_stub/c1/c2)
1453 //
1454 //
1455 // Stack layout at entry
1456 //
1457 // [ return address     ] <--- esp
1458 // [ parameter n        ]
1459 //   ...
1460 // [ parameter 1        ]
1461 // [ expression stack   ]
1462 //
1463 //
1464 // We are free to blow any registers we like because the call_stub which brought us here
1465 // initially has preserved the callee save registers already.
1466 //
1467 //
1468 
1469 static address interpreter_frame_manager = NULL;
1470 
1471 #ifdef ASSERT
1472   #define VALIDATE_STATE(scratch, marker)                         \
1473   {                                                               \
1474     Label skip;                                                   \
1475     __ ld_ptr(STATE(_self_link), scratch);                        \
1476     __ cmp(Lstate, scratch);                                      \
1477     __ brx(Assembler::equal, false, Assembler::pt, skip);         \
1478     __ delayed()->nop();                                          \
1479     __ breakpoint_trap();                                         \
1480     __ emit_int32(marker);                                         \
1481     __ bind(skip);                                                \
1482   }
1483 #else
1484   #define VALIDATE_STATE(scratch, marker)
1485 #endif /* ASSERT */
1486 
1487 void CppInterpreterGenerator::adjust_callers_stack(Register args) {
1488 //
1489 // Adjust caller's stack so that all the locals can be contiguous with
1490 // the parameters.
1491 // Worries about stack overflow make this a pain.
1492 //
1493 // Destroys args, G3_scratch, G3_scratch
1494 // In/Out O5_savedSP (sender's original SP)
1495 //
1496 //  assert_different_registers(state, prev_state);
1497   const Register Gtmp = G3_scratch;
1498   const Register RconstMethod = G3_scratch;
1499   const Register tmp = O2;
1500   const Address constMethod(G5_method, in_bytes(Method::const_offset()));
1501   const Address size_of_parameters(RconstMethod, in_bytes(ConstMethod::size_of_parameters_offset()));
1502   const Address size_of_locals    (RconstMethod, in_bytes(ConstMethod::size_of_locals_offset()));
1503 
1504   __ ld_ptr(constMethod, RconstMethod);
1505   __ lduh(size_of_parameters, tmp);
1506   __ sll(tmp, LogBytesPerWord, Gargs);       // parameter size in bytes
1507   __ add(args, Gargs, Gargs);                // points to first local + BytesPerWord
1508   // NEW
1509   __ add(Gargs, -wordSize, Gargs);             // points to first local[0]
1510   // determine extra space for non-argument locals & adjust caller's SP
1511   // Gtmp1: parameter size in words
1512   __ lduh(size_of_locals, Gtmp);
1513   __ compute_extra_locals_size_in_bytes(tmp, Gtmp, Gtmp);
1514 
1515 #if 1
1516   // c2i adapters place the final interpreter argument in the register save area for O0/I0
1517   // the call_stub will place the final interpreter argument at
1518   // frame::memory_parameter_word_sp_offset. This is mostly not noticable for either asm
1519   // or c++ interpreter. However with the c++ interpreter when we do a recursive call
1520   // and try to make it look good in the debugger we will store the argument to
1521   // RecursiveInterpreterActivation in the register argument save area. Without allocating
1522   // extra space for the compiler this will overwrite locals in the local array of the
1523   // interpreter.
1524   // QQQ still needed with frameless adapters???
1525 
1526   const int c2i_adjust_words = frame::memory_parameter_word_sp_offset - frame::callee_register_argument_save_area_sp_offset;
1527 
1528   __ add(Gtmp, c2i_adjust_words*wordSize, Gtmp);
1529 #endif // 1
1530 
1531 
1532   __ sub(SP, Gtmp, SP);                      // just caller's frame for the additional space we need.
1533 }
1534 
1535 address InterpreterGenerator::generate_normal_entry(bool synchronized) {
1536 
1537   // G5_method: Method*
1538   // G2_thread: thread (unused)
1539   // Gargs:   bottom of args (sender_sp)
1540   // O5: sender's sp
1541 
1542   // A single frame manager is plenty as we don't specialize for synchronized. We could and
1543   // the code is pretty much ready. Would need to change the test below and for good measure
1544   // modify generate_interpreter_state to only do the (pre) sync stuff stuff for synchronized
1545   // routines. Not clear this is worth it yet.
1546 
1547   if (interpreter_frame_manager) {
1548     return interpreter_frame_manager;
1549   }
1550 
1551   __ bind(frame_manager_entry);
1552 
1553   // the following temporary registers are used during frame creation
1554   const Register Gtmp1 = G3_scratch;
1555   // const Register Lmirror = L1;     // native mirror (native calls only)
1556 
1557   const Address constMethod       (G5_method, in_bytes(Method::const_offset()));
1558   const Address access_flags      (G5_method, in_bytes(Method::access_flags_offset()));
1559 
1560   address entry_point = __ pc();
1561   __ mov(G0, prevState);                                                 // no current activation
1562 
1563 
1564   Label re_dispatch;
1565 
1566   __ bind(re_dispatch);
1567 
1568   // Interpreter needs to have locals completely contiguous. In order to do that
1569   // We must adjust the caller's stack pointer for any locals beyond just the
1570   // parameters
1571   adjust_callers_stack(Gargs);
1572 
1573   // O5_savedSP still contains sender's sp
1574 
1575   // NEW FRAME
1576 
1577   generate_compute_interpreter_state(Lstate, prevState, false);
1578 
1579   // At this point a new interpreter frame and state object are created and initialized
1580   // Lstate has the pointer to the new activation
1581   // Any stack banging or limit check should already be done.
1582 
1583   Label call_interpreter;
1584 
1585   __ bind(call_interpreter);
1586 
1587 
1588 #if 1
1589   __ set(0xdead002, Lmirror);
1590   __ set(0xdead002, L2_scratch);
1591   __ set(0xdead003, L3_scratch);
1592   __ set(0xdead004, L4_scratch);
1593   __ set(0xdead005, Lscratch);
1594   __ set(0xdead006, Lscratch2);
1595   __ set(0xdead007, L7_scratch);
1596 
1597   __ set(0xdeaf002, O2);
1598   __ set(0xdeaf003, O3);
1599   __ set(0xdeaf004, O4);
1600   __ set(0xdeaf005, O5);
1601 #endif
1602 
1603   // Call interpreter (stack bang complete) enter here if message is
1604   // set and we know stack size is valid
1605 
1606   Label call_interpreter_2;
1607 
1608   __ bind(call_interpreter_2);
1609 
1610 #ifdef ASSERT
1611   {
1612     Label skip;
1613     __ ld_ptr(STATE(_frame_bottom), G3_scratch);
1614     __ cmp(G3_scratch, SP);
1615     __ brx(Assembler::equal, false, Assembler::pt, skip);
1616     __ delayed()->nop();
1617     __ stop("SP not restored to frame bottom");
1618     __ bind(skip);
1619   }
1620 #endif
1621 
1622   VALIDATE_STATE(G3_scratch, 4);
1623   __ set_last_Java_frame(SP, noreg);
1624   __ mov(Lstate, O0);                 // (arg) pointer to current state
1625 
1626   __ call(CAST_FROM_FN_PTR(address,
1627                            JvmtiExport::can_post_interpreter_events() ?
1628                                                                   BytecodeInterpreter::runWithChecks
1629                                                                 : BytecodeInterpreter::run),
1630          relocInfo::runtime_call_type);
1631 
1632   __ delayed()->nop();
1633 
1634   __ ld_ptr(STATE(_thread), G2_thread);
1635   __ reset_last_Java_frame();
1636 
1637   // examine msg from interpreter to determine next action
1638   __ ld_ptr(STATE(_thread), G2_thread);                                  // restore G2_thread
1639 
1640   __ ld(STATE(_msg), L1_scratch);                                       // Get new message
1641 
1642   Label call_method;
1643   Label return_from_interpreted_method;
1644   Label throw_exception;
1645   Label do_OSR;
1646   Label bad_msg;
1647   Label resume_interpreter;
1648 
1649   __ cmp(L1_scratch, (int)BytecodeInterpreter::call_method);
1650   __ br(Assembler::equal, false, Assembler::pt, call_method);
1651   __ delayed()->cmp(L1_scratch, (int)BytecodeInterpreter::return_from_method);
1652   __ br(Assembler::equal, false, Assembler::pt, return_from_interpreted_method);
1653   __ delayed()->cmp(L1_scratch, (int)BytecodeInterpreter::throwing_exception);
1654   __ br(Assembler::equal, false, Assembler::pt, throw_exception);
1655   __ delayed()->cmp(L1_scratch, (int)BytecodeInterpreter::do_osr);
1656   __ br(Assembler::equal, false, Assembler::pt, do_OSR);
1657   __ delayed()->cmp(L1_scratch, (int)BytecodeInterpreter::more_monitors);
1658   __ br(Assembler::notEqual, false, Assembler::pt, bad_msg);
1659 
1660   // Allocate more monitor space, shuffle expression stack....
1661 
1662   generate_more_monitors();
1663 
1664   // new monitor slot allocated, resume the interpreter.
1665 
1666   __ set((int)BytecodeInterpreter::got_monitors, L1_scratch);
1667   VALIDATE_STATE(G3_scratch, 5);
1668   __ ba(call_interpreter);
1669   __ delayed()->st(L1_scratch, STATE(_msg));
1670 
1671   // uncommon trap needs to jump to here to enter the interpreter (re-execute current bytecode)
1672   unctrap_frame_manager_entry  = __ pc();
1673 
1674   // QQQ what message do we send
1675 
1676   __ ba(call_interpreter);
1677   __ delayed()->ld_ptr(STATE(_frame_bottom), SP);                  // restore to full stack frame
1678 
1679   //=============================================================================
1680   // Returning from a compiled method into a deopted method. The bytecode at the
1681   // bcp has completed. The result of the bytecode is in the native abi (the tosca
1682   // for the template based interpreter). Any stack space that was used by the
1683   // bytecode that has completed has been removed (e.g. parameters for an invoke)
1684   // so all that we have to do is place any pending result on the expression stack
1685   // and resume execution on the next bytecode.
1686 
1687   generate_deopt_handling();
1688 
1689   // ready to resume the interpreter
1690 
1691   __ set((int)BytecodeInterpreter::deopt_resume, L1_scratch);
1692   __ ba(call_interpreter);
1693   __ delayed()->st(L1_scratch, STATE(_msg));
1694 
1695   // Current frame has caught an exception we need to dispatch to the
1696   // handler. We can get here because a native interpreter frame caught
1697   // an exception in which case there is no handler and we must rethrow
1698   // If it is a vanilla interpreted frame the we simply drop into the
1699   // interpreter and let it do the lookup.
1700 
1701   Interpreter::_rethrow_exception_entry = __ pc();
1702 
1703   Label return_with_exception;
1704   Label unwind_and_forward;
1705 
1706   // O0: exception
1707   // O7: throwing pc
1708 
1709   // We want exception in the thread no matter what we ultimately decide about frame type.
1710 
1711   Address exception_addr (G2_thread, in_bytes(Thread::pending_exception_offset()));
1712   __ verify_thread();
1713   __ st_ptr(O0, exception_addr);
1714 
1715   // get the Method*
1716   __ ld_ptr(STATE(_method), G5_method);
1717 
1718   // if this current frame vanilla or native?
1719 
1720   __ ld(access_flags, Gtmp1);
1721   __ btst(JVM_ACC_NATIVE, Gtmp1);
1722   __ br(Assembler::zero, false, Assembler::pt, return_with_exception);  // vanilla interpreted frame handle directly
1723   __ delayed()->nop();
1724 
1725   // We drop thru to unwind a native interpreted frame with a pending exception
1726   // We jump here for the initial interpreter frame with exception pending
1727   // We unwind the current acivation and forward it to our caller.
1728 
1729   __ bind(unwind_and_forward);
1730 
1731   // Unwind frame and jump to forward exception. unwinding will place throwing pc in O7
1732   // as expected by forward_exception.
1733 
1734   __ restore(FP, G0, SP);                  // unwind interpreter state frame
1735   __ br(Assembler::always, false, Assembler::pt, StubRoutines::forward_exception_entry(), relocInfo::runtime_call_type);
1736   __ delayed()->mov(I5_savedSP->after_restore(), SP);
1737 
1738   // Return point from a call which returns a result in the native abi
1739   // (c1/c2/jni-native). This result must be processed onto the java
1740   // expression stack.
1741   //
1742   // A pending exception may be present in which case there is no result present
1743 
1744   address return_from_native_method = __ pc();
1745 
1746   VALIDATE_STATE(G3_scratch, 6);
1747 
1748   // Result if any is in native abi result (O0..O1/F0..F1). The java expression
1749   // stack is in the state that the  calling convention left it.
1750   // Copy the result from native abi result and place it on java expression stack.
1751 
1752   // Current interpreter state is present in Lstate
1753 
1754   // Exception pending?
1755 
1756   __ ld_ptr(STATE(_frame_bottom), SP);                             // restore to full stack frame
1757   __ ld_ptr(exception_addr, Lscratch);                                         // get any pending exception
1758   __ tst(Lscratch);                                                            // exception pending?
1759   __ brx(Assembler::notZero, false, Assembler::pt, return_with_exception);
1760   __ delayed()->nop();
1761 
1762   // Process the native abi result to java expression stack
1763 
1764   __ ld_ptr(STATE(_result._to_call._callee), L4_scratch);                        // called method
1765   __ ld_ptr(STATE(_stack), L1_scratch);                                          // get top of java expr stack
1766   // get parameter size
1767   __ ld_ptr(L4_scratch, in_bytes(Method::const_offset()), L2_scratch);
1768   __ lduh(L2_scratch, in_bytes(ConstMethod::size_of_parameters_offset()), L2_scratch);
1769   __ sll(L2_scratch, LogBytesPerWord, L2_scratch     );                           // parameter size in bytes
1770   __ add(L1_scratch, L2_scratch, L1_scratch);                                      // stack destination for result
1771   __ ld(L4_scratch, in_bytes(Method::result_index_offset()), L3_scratch); // called method result type index
1772 
1773   // tosca is really just native abi
1774   __ set((intptr_t)CppInterpreter::_tosca_to_stack, L4_scratch);
1775   __ sll(L3_scratch, LogBytesPerWord, L3_scratch);
1776   __ ld_ptr(L4_scratch, L3_scratch, Lscratch);                                       // get typed result converter address
1777   __ jmpl(Lscratch, G0, O7);                                                   // and convert it
1778   __ delayed()->nop();
1779 
1780   // L1_scratch points to top of stack (prepushed)
1781 
1782   __ ba(resume_interpreter);
1783   __ delayed()->mov(L1_scratch, O1);
1784 
1785   // An exception is being caught on return to a vanilla interpreter frame.
1786   // Empty the stack and resume interpreter
1787 
1788   __ bind(return_with_exception);
1789 
1790   __ ld_ptr(STATE(_frame_bottom), SP);                             // restore to full stack frame
1791   __ ld_ptr(STATE(_stack_base), O1);                               // empty java expression stack
1792   __ ba(resume_interpreter);
1793   __ delayed()->sub(O1, wordSize, O1);                             // account for prepush
1794 
1795   // Return from interpreted method we return result appropriate to the caller (i.e. "recursive"
1796   // interpreter call, or native) and unwind this interpreter activation.
1797   // All monitors should be unlocked.
1798 
1799   __ bind(return_from_interpreted_method);
1800 
1801   VALIDATE_STATE(G3_scratch, 7);
1802 
1803   Label return_to_initial_caller;
1804 
1805   // Interpreted result is on the top of the completed activation expression stack.
1806   // We must return it to the top of the callers stack if caller was interpreted
1807   // otherwise we convert to native abi result and return to call_stub/c1/c2
1808   // The caller's expression stack was truncated by the call however the current activation
1809   // has enough stuff on the stack that we have usable space there no matter what. The
1810   // other thing that makes it easy is that the top of the caller's stack is stored in STATE(_locals)
1811   // for the current activation
1812 
1813   __ ld_ptr(STATE(_prev_link), L1_scratch);
1814   __ ld_ptr(STATE(_method), L2_scratch);                               // get method just executed
1815   __ ld(L2_scratch, in_bytes(Method::result_index_offset()), L2_scratch);
1816   __ tst(L1_scratch);
1817   __ brx(Assembler::zero, false, Assembler::pt, return_to_initial_caller);
1818   __ delayed()->sll(L2_scratch, LogBytesPerWord, L2_scratch);
1819 
1820   // Copy result to callers java stack
1821 
1822   __ set((intptr_t)CppInterpreter::_stack_to_stack, L4_scratch);
1823   __ ld_ptr(L4_scratch, L2_scratch, Lscratch);                          // get typed result converter address
1824   __ ld_ptr(STATE(_stack), O0);                                       // current top (prepushed)
1825   __ ld_ptr(STATE(_locals), O1);                                      // stack destination
1826 
1827   // O0 - will be source, O1 - will be destination (preserved)
1828   __ jmpl(Lscratch, G0, O7);                                          // and convert it
1829   __ delayed()->add(O0, wordSize, O0);                                // get source (top of current expr stack)
1830 
1831   // O1 == &locals[0]
1832 
1833   // Result is now on caller's stack. Just unwind current activation and resume
1834 
1835   Label unwind_recursive_activation;
1836 
1837 
1838   __ bind(unwind_recursive_activation);
1839 
1840   // O1 == &locals[0] (really callers stacktop) for activation now returning
1841   // returning to interpreter method from "recursive" interpreter call
1842   // result converter left O1 pointing to top of the( prepushed) java stack for method we are returning
1843   // to. Now all we must do is unwind the state from the completed call
1844 
1845   // Must restore stack
1846   VALIDATE_STATE(G3_scratch, 8);
1847 
1848   // Return to interpreter method after a method call (interpreted/native/c1/c2) has completed.
1849   // Result if any is already on the caller's stack. All we must do now is remove the now dead
1850   // frame and tell interpreter to resume.
1851 
1852 
1853   __ mov(O1, I1);                                                     // pass back new stack top across activation
1854   // POP FRAME HERE ==================================
1855   __ restore(FP, G0, SP);                                             // unwind interpreter state frame
1856   __ ld_ptr(STATE(_frame_bottom), SP);                                // restore to full stack frame
1857 
1858 
1859   // Resume the interpreter. The current frame contains the current interpreter
1860   // state object.
1861   //
1862   // O1 == new java stack pointer
1863 
1864   __ bind(resume_interpreter);
1865   VALIDATE_STATE(G3_scratch, 10);
1866 
1867   // A frame we have already used before so no need to bang stack so use call_interpreter_2 entry
1868 
1869   __ set((int)BytecodeInterpreter::method_resume, L1_scratch);
1870   __ st(L1_scratch, STATE(_msg));
1871   __ ba(call_interpreter_2);
1872   __ delayed()->st_ptr(O1, STATE(_stack));
1873 
1874 
1875   // Fast accessor methods share this entry point.
1876   // This works because frame manager is in the same codelet
1877   // This can either be an entry via call_stub/c1/c2 or a recursive interpreter call
1878   // we need to do a little register fixup here once we distinguish the two of them
1879   if (UseFastAccessorMethods && !synchronized) {
1880   // Call stub_return address still in O7
1881     __ bind(fast_accessor_slow_entry_path);
1882     __ set((intptr_t)return_from_native_method - 8, Gtmp1);
1883     __ cmp(Gtmp1, O7);                                                // returning to interpreter?
1884     __ brx(Assembler::equal, true, Assembler::pt, re_dispatch);       // yep
1885     __ delayed()->nop();
1886     __ ba(re_dispatch);
1887     __ delayed()->mov(G0, prevState);                                 // initial entry
1888 
1889   }
1890 
1891   // interpreter returning to native code (call_stub/c1/c2)
1892   // convert result and unwind initial activation
1893   // L2_scratch - scaled result type index
1894 
1895   __ bind(return_to_initial_caller);
1896 
1897   __ set((intptr_t)CppInterpreter::_stack_to_native_abi, L4_scratch);
1898   __ ld_ptr(L4_scratch, L2_scratch, Lscratch);                           // get typed result converter address
1899   __ ld_ptr(STATE(_stack), O0);                                        // current top (prepushed)
1900   __ jmpl(Lscratch, G0, O7);                                           // and convert it
1901   __ delayed()->add(O0, wordSize, O0);                                 // get source (top of current expr stack)
1902 
1903   Label unwind_initial_activation;
1904   __ bind(unwind_initial_activation);
1905 
1906   // RETURN TO CALL_STUB/C1/C2 code (result if any in I0..I1/(F0/..F1)
1907   // we can return here with an exception that wasn't handled by interpreted code
1908   // how does c1/c2 see it on return?
1909 
1910   // compute resulting sp before/after args popped depending upon calling convention
1911   // __ ld_ptr(STATE(_saved_sp), Gtmp1);
1912   //
1913   // POP FRAME HERE ==================================
1914   __ restore(FP, G0, SP);
1915   __ retl();
1916   __ delayed()->mov(I5_savedSP->after_restore(), SP);
1917 
1918   // OSR request, unwind the current frame and transfer to the OSR entry
1919   // and enter OSR nmethod
1920 
1921   __ bind(do_OSR);
1922   Label remove_initial_frame;
1923   __ ld_ptr(STATE(_prev_link), L1_scratch);
1924   __ ld_ptr(STATE(_result._osr._osr_buf), G1_scratch);
1925 
1926   // We are going to pop this frame. Is there another interpreter frame underneath
1927   // it or is it callstub/compiled?
1928 
1929   __ tst(L1_scratch);
1930   __ brx(Assembler::zero, false, Assembler::pt, remove_initial_frame);
1931   __ delayed()->ld_ptr(STATE(_result._osr._osr_entry), G3_scratch);
1932 
1933   // Frame underneath is an interpreter frame simply unwind
1934   // POP FRAME HERE ==================================
1935   __ restore(FP, G0, SP);                                             // unwind interpreter state frame
1936   __ mov(I5_savedSP->after_restore(), SP);
1937 
1938   // Since we are now calling native need to change our "return address" from the
1939   // dummy RecursiveInterpreterActivation to a return from native
1940 
1941   __ set((intptr_t)return_from_native_method - 8, O7);
1942 
1943   __ jmpl(G3_scratch, G0, G0);
1944   __ delayed()->mov(G1_scratch, O0);
1945 
1946   __ bind(remove_initial_frame);
1947 
1948   // POP FRAME HERE ==================================
1949   __ restore(FP, G0, SP);
1950   __ mov(I5_savedSP->after_restore(), SP);
1951   __ jmpl(G3_scratch, G0, G0);
1952   __ delayed()->mov(G1_scratch, O0);
1953 
1954   // Call a new method. All we do is (temporarily) trim the expression stack
1955   // push a return address to bring us back to here and leap to the new entry.
1956   // At this point we have a topmost frame that was allocated by the frame manager
1957   // which contains the current method interpreted state. We trim this frame
1958   // of excess java expression stack entries and then recurse.
1959 
1960   __ bind(call_method);
1961 
1962   // stack points to next free location and not top element on expression stack
1963   // method expects sp to be pointing to topmost element
1964 
1965   __ ld_ptr(STATE(_thread), G2_thread);
1966   __ ld_ptr(STATE(_result._to_call._callee), G5_method);
1967 
1968 
1969   // SP already takes in to account the 2 extra words we use for slop
1970   // when we call a "static long no_params()" method. So if
1971   // we trim back sp by the amount of unused java expression stack
1972   // there will be automagically the 2 extra words we need.
1973   // We also have to worry about keeping SP aligned.
1974 
1975   __ ld_ptr(STATE(_stack), Gargs);
1976   __ ld_ptr(STATE(_stack_limit), L1_scratch);
1977 
1978   // compute the unused java stack size
1979   __ sub(Gargs, L1_scratch, L2_scratch);                       // compute unused space
1980 
1981   // Round down the unused space to that stack is always 16-byte aligned
1982   // by making the unused space a multiple of the size of two longs.
1983 
1984   __ and3(L2_scratch, -2*BytesPerLong, L2_scratch);
1985 
1986   // Now trim the stack
1987   __ add(SP, L2_scratch, SP);
1988 
1989 
1990   // Now point to the final argument (account for prepush)
1991   __ add(Gargs, wordSize, Gargs);
1992 #ifdef ASSERT
1993   // Make sure we have space for the window
1994   __ sub(Gargs, SP, L1_scratch);
1995   __ cmp(L1_scratch, 16*wordSize);
1996   {
1997     Label skip;
1998     __ brx(Assembler::greaterEqual, false, Assembler::pt, skip);
1999     __ delayed()->nop();
2000     __ stop("killed stack");
2001     __ bind(skip);
2002   }
2003 #endif // ASSERT
2004 
2005   // Create a new frame where we can store values that make it look like the interpreter
2006   // really recursed.
2007 
2008   // prepare to recurse or call specialized entry
2009 
2010   // First link the registers we need
2011 
2012   // make the pc look good in debugger
2013   __ set(CAST_FROM_FN_PTR(intptr_t, RecursiveInterpreterActivation), O7);
2014   // argument too
2015   __ mov(Lstate, I0);
2016 
2017   // Record our sending SP
2018   __ mov(SP, O5_savedSP);
2019 
2020   __ ld_ptr(STATE(_result._to_call._callee_entry_point), L2_scratch);
2021   __ set((intptr_t) entry_point, L1_scratch);
2022   __ cmp(L1_scratch, L2_scratch);
2023   __ brx(Assembler::equal, false, Assembler::pt, re_dispatch);
2024   __ delayed()->mov(Lstate, prevState);                                // link activations
2025 
2026   // method uses specialized entry, push a return so we look like call stub setup
2027   // this path will handle fact that result is returned in registers and not
2028   // on the java stack.
2029 
2030   __ set((intptr_t)return_from_native_method - 8, O7);
2031   __ jmpl(L2_scratch, G0, G0);                               // Do specialized entry
2032   __ delayed()->nop();
2033 
2034   //
2035   // Bad Message from interpreter
2036   //
2037   __ bind(bad_msg);
2038   __ stop("Bad message from interpreter");
2039 
2040   // Interpreted method "returned" with an exception pass it on...
2041   // Pass result, unwind activation and continue/return to interpreter/call_stub
2042   // We handle result (if any) differently based on return to interpreter or call_stub
2043 
2044   __ bind(throw_exception);
2045   __ ld_ptr(STATE(_prev_link), L1_scratch);
2046   __ tst(L1_scratch);
2047   __ brx(Assembler::zero, false, Assembler::pt, unwind_and_forward);
2048   __ delayed()->nop();
2049 
2050   __ ld_ptr(STATE(_locals), O1); // get result of popping callee's args
2051   __ ba(unwind_recursive_activation);
2052   __ delayed()->nop();
2053 
2054   interpreter_frame_manager = entry_point;
2055   return entry_point;
2056 }
2057 
2058 InterpreterGenerator::InterpreterGenerator(StubQueue* code)
2059  : CppInterpreterGenerator(code) {
2060    generate_all(); // down here so it can be "virtual"
2061 }
2062 
2063 
2064 template<class M> static int size_activation_helper(int callee_extra_locals, int max_stack, int monitor_size) {
2065 
2066   // Figure out the size of an interpreter frame (in words) given that we have a fully allocated
2067   // expression stack, the callee will have callee_extra_locals (so we can account for
2068   // frame extension) and monitor_size for monitors. Basically we need to calculate
2069   // this exactly like generate_fixed_frame/generate_compute_interpreter_state.
2070   //
2071   //
2072   // The big complicating thing here is that we must ensure that the stack stays properly
2073   // aligned. This would be even uglier if monitor size wasn't modulo what the stack
2074   // needs to be aligned for). We are given that the sp (fp) is already aligned by
2075   // the caller so we must ensure that it is properly aligned for our callee.
2076   //
2077   // Ths c++ interpreter always makes sure that we have a enough extra space on the
2078   // stack at all times to deal with the "stack long no_params()" method issue. This
2079   // is "slop_factor" here.
2080   const int slop_factor = 2;
2081 
2082   const int fixed_size = sizeof(BytecodeInterpreter)/wordSize +           // interpreter state object
2083                          frame::memory_parameter_word_sp_offset;   // register save area + param window
2084   return (round_to(max_stack +
2085                    slop_factor +
2086                    fixed_size +
2087                    monitor_size +
2088                    (callee_extra_locals * Interpreter::stackElementWords), WordsPerLong));
2089 
2090 }
2091 
2092 int AbstractInterpreter::size_top_interpreter_activation(Method* method) {
2093 
2094   // See call_stub code
2095   int call_stub_size  = round_to(7 + frame::memory_parameter_word_sp_offset,
2096                                  WordsPerLong);    // 7 + register save area
2097 
2098   // Save space for one monitor to get into the interpreted method in case
2099   // the method is synchronized
2100   int monitor_size    = method->is_synchronized() ?
2101                                 1*frame::interpreter_frame_monitor_size() : 0;
2102   return size_activation_helper<Method>(method->max_locals(), method->max_stack(),
2103                                         monitor_size) + call_stub_size;
2104 }
2105 
2106 void BytecodeInterpreter::layout_interpreterState(interpreterState to_fill,
2107                                            frame* caller,
2108                                            frame* current,
2109                                            Method* method,
2110                                            intptr_t* locals,
2111                                            intptr_t* stack,
2112                                            intptr_t* stack_base,
2113                                            intptr_t* monitor_base,
2114                                            intptr_t* frame_bottom,
2115                                            bool is_top_frame
2116                                            )
2117 {
2118   // What about any vtable?
2119   //
2120   to_fill->_thread = JavaThread::current();
2121   // This gets filled in later but make it something recognizable for now
2122   to_fill->_bcp = method->code_base();
2123   to_fill->_locals = locals;
2124   to_fill->_constants = method->constants()->cache();
2125   to_fill->_method = method;
2126   to_fill->_mdx = NULL;
2127   to_fill->_stack = stack;
2128   if (is_top_frame && JavaThread::current()->popframe_forcing_deopt_reexecution() ) {
2129     to_fill->_msg = deopt_resume2;
2130   } else {
2131     to_fill->_msg = method_resume;
2132   }
2133   to_fill->_result._to_call._bcp_advance = 0;
2134   to_fill->_result._to_call._callee_entry_point = NULL; // doesn't matter to anyone
2135   to_fill->_result._to_call._callee = NULL; // doesn't matter to anyone
2136   to_fill->_prev_link = NULL;
2137 
2138   // Fill in the registers for the frame
2139 
2140   // Need to install _sender_sp. Actually not too hard in C++!
2141   // When the skeletal frames are layed out we fill in a value
2142   // for _sender_sp. That value is only correct for the oldest
2143   // skeletal frame constructed (because there is only a single
2144   // entry for "caller_adjustment". While the skeletal frames
2145   // exist that is good enough. We correct that calculation
2146   // here and get all the frames correct.
2147 
2148   // to_fill->_sender_sp = locals - (method->size_of_parameters() - 1);
2149 
2150   *current->register_addr(Lstate) = (intptr_t) to_fill;
2151   // skeletal already places a useful value here and this doesn't account
2152   // for alignment so don't bother.
2153   // *current->register_addr(I5_savedSP) =     (intptr_t) locals - (method->size_of_parameters() - 1);
2154 
2155   if (caller->is_interpreted_frame()) {
2156     interpreterState prev  = caller->get_interpreterState();
2157     to_fill->_prev_link = prev;
2158     // Make the prev callee look proper
2159     prev->_result._to_call._callee = method;
2160     if (*prev->_bcp == Bytecodes::_invokeinterface) {
2161       prev->_result._to_call._bcp_advance = 5;
2162     } else {
2163       prev->_result._to_call._bcp_advance = 3;
2164     }
2165   }
2166   to_fill->_oop_temp = NULL;
2167   to_fill->_stack_base = stack_base;
2168   // Need +1 here because stack_base points to the word just above the first expr stack entry
2169   // and stack_limit is supposed to point to the word just below the last expr stack entry.
2170   // See generate_compute_interpreter_state.
2171   to_fill->_stack_limit = stack_base - (method->max_stack() + 1);
2172   to_fill->_monitor_base = (BasicObjectLock*) monitor_base;
2173 
2174   // sparc specific
2175   to_fill->_frame_bottom = frame_bottom;
2176   to_fill->_self_link = to_fill;
2177 #ifdef ASSERT
2178   to_fill->_native_fresult = 123456.789;
2179   to_fill->_native_lresult = CONST64(0xdeadcafedeafcafe);
2180 #endif
2181 }
2182 
2183 void BytecodeInterpreter::pd_layout_interpreterState(interpreterState istate, address last_Java_pc, intptr_t* last_Java_fp) {
2184   istate->_last_Java_pc = (intptr_t*) last_Java_pc;
2185 }
2186 
2187 template<class M> static int frame_size_helper(M* method,
2188                                                int moncount,
2189                                                int callee_param_size,
2190                                                int callee_locals_size,
2191                                                bool is_top_frame,
2192                                                int& monitor_size,
2193                                                int& full_frame_words) {
2194   int extra_locals_size = callee_locals_size - callee_param_size;
2195   monitor_size = (sizeof(BasicObjectLock) * moncount) / wordSize;
2196   full_frame_words = size_activation_helper<M>(extra_locals_size, method->max_stack(), monitor_size);
2197   int short_frame_words = size_activation_helper<M>(extra_locals_size, method->max_stack(), monitor_size);
2198   int frame_words = is_top_frame ? full_frame_words : short_frame_words;
2199 
2200   return frame_words;
2201 }
2202 
2203 template<class M> int AbstractInterpreter::size_activation(M* method,
2204                                                            int tempcount,
2205                                                            int popframe_extra_args,
2206                                                            int moncount,
2207                                                            int callee_param_size,
2208                                                            int callee_locals_size,
2209                                                            bool is_top_frame) {
2210   assert(popframe_extra_args == 0, "NEED TO FIX");
2211   // NOTE: return size is in words not bytes
2212   // Calculate the amount our frame will be adjust by the callee. For top frame
2213   // this is zero.
2214 
2215   // NOTE: ia64 seems to do this wrong (or at least backwards) in that it
2216   // calculates the extra locals based on itself. Not what the callee does
2217   // to it. So it ignores last_frame_adjust value. Seems suspicious as far
2218   // as getting sender_sp correct.
2219 
2220   int unused_monitor_size = 0;
2221   int unused_full_frame_words = 0;
2222   return frame_size_helper(method, moncount, callee_param_size, callee_locals_size, is_top_frame,
2223                            unused_monitor_size, unused_full_frame_words);
2224 }
2225 
2226 template int AbstractInterpreter::size_activation<Method>(Method* method,
2227                                                           int temps,
2228                                                           int popframe_args,
2229                                                           int monitors,
2230                                                           int callee_params,
2231                                                           int callee_locals,
2232                                                           bool is_top_frame);
2233 
2234 template int AbstractInterpreter::size_activation<ciMethod>(ciMethod* method,
2235                                                             int temps,
2236                                                             int popframe_args,
2237                                                             int monitors,
2238                                                             int callee_params,
2239                                                             int callee_locals,
2240                                                             bool is_top_frame);
2241 
2242 void AbstractInterpreter::layout_activation(Method* method,
2243                                             int tempcount, // Number of slots on java expression stack in use
2244                                             int popframe_extra_args,
2245                                             int moncount,  // Number of active monitors
2246                                             int caller_actual_parameters,
2247                                             int callee_param_size,
2248                                             int callee_locals_size,
2249                                             frame* caller,
2250                                             frame* interpreter_frame,
2251                                             bool is_top_frame,
2252                                             bool is_bottom_frame) {
2253   assert(popframe_extra_args == 0, "NEED TO FIX");
2254   // NOTE this code must exactly mimic what InterpreterGenerator::generate_compute_interpreter_state()
2255   // does as far as allocating an interpreter frame.
2256   // Set up the method, locals, and monitors.
2257   // The frame interpreter_frame is guaranteed to be the right size,
2258   // as determined by a previous call to the size_activation() method.
2259   // It is also guaranteed to be walkable even though it is in a skeletal state
2260   // NOTE: tempcount is the current size of the java expression stack. For top most
2261   //       frames we will allocate a full sized expression stack and not the curback
2262   //       version that non-top frames have.
2263 
2264   int monitor_size = 0;
2265   int full_frame_words = 0;
2266   int frame_words = frame_size_helper<Method>(method, moncount, callee_param_size, callee_locals_size,
2267                                               is_top_frame, monitor_size, full_frame_words);
2268 
2269   /*
2270     We must now fill in all the pieces of the frame. This means both
2271     the interpreterState and the registers.
2272   */
2273 
2274   // MUCHO HACK
2275 
2276   intptr_t* frame_bottom = interpreter_frame->sp() - (full_frame_words - frame_words);
2277   // 'interpreter_frame->sp()' is unbiased while 'frame_bottom' must be a biased value in 64bit mode.
2278   assert(((intptr_t)frame_bottom & 0xf) == 0, "SP biased in layout_activation");
2279   frame_bottom = (intptr_t*)((intptr_t)frame_bottom - STACK_BIAS);
2280 
2281   /* Now fillin the interpreterState object */
2282 
2283   interpreterState cur_state = (interpreterState) ((intptr_t)interpreter_frame->fp() -  sizeof(BytecodeInterpreter));
2284 
2285 
2286   intptr_t* locals;
2287 
2288   // Calculate the postion of locals[0]. This is painful because of
2289   // stack alignment (same as ia64). The problem is that we can
2290   // not compute the location of locals from fp(). fp() will account
2291   // for the extra locals but it also accounts for aligning the stack
2292   // and we can't determine if the locals[0] was misaligned but max_locals
2293   // was enough to have the
2294   // calculate postion of locals. fp already accounts for extra locals.
2295   // +2 for the static long no_params() issue.
2296 
2297   if (caller->is_interpreted_frame()) {
2298     // locals must agree with the caller because it will be used to set the
2299     // caller's tos when we return.
2300     interpreterState prev  = caller->get_interpreterState();
2301     // stack() is prepushed.
2302     locals = prev->stack() + method->size_of_parameters();
2303   } else {
2304     // Lay out locals block in the caller adjacent to the register window save area.
2305     //
2306     // Compiled frames do not allocate a varargs area which is why this if
2307     // statement is needed.
2308     //
2309     intptr_t* fp = interpreter_frame->fp();
2310     int local_words = method->max_locals() * Interpreter::stackElementWords;
2311 
2312     if (caller->is_compiled_frame()) {
2313       locals = fp + frame::register_save_words + local_words - 1;
2314     } else {
2315       locals = fp + frame::memory_parameter_word_sp_offset + local_words - 1;
2316     }
2317 
2318   }
2319   // END MUCHO HACK
2320 
2321   intptr_t* monitor_base = (intptr_t*) cur_state;
2322   intptr_t* stack_base =  monitor_base - monitor_size;
2323   /* +1 because stack is always prepushed */
2324   intptr_t* stack = stack_base - (tempcount + 1);
2325 
2326 
2327   BytecodeInterpreter::layout_interpreterState(cur_state,
2328                                                caller,
2329                                                interpreter_frame,
2330                                                method,
2331                                                locals,
2332                                                stack,
2333                                                stack_base,
2334                                                monitor_base,
2335                                                frame_bottom,
2336                                                is_top_frame);
2337 
2338   BytecodeInterpreter::pd_layout_interpreterState(cur_state, interpreter_return_address, interpreter_frame->fp());
2339 }
2340 
2341 #endif // CC_INTERP