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/macroAssembler.hpp"
  27 #include "interpreter/bytecodeHistogram.hpp"
  28 #include "interpreter/cppInterpreter.hpp"
  29 #include "interpreter/interpreter.hpp"
  30 #include "interpreter/interpreterGenerator.hpp"
  31 #include "interpreter/interpreterRuntime.hpp"
  32 #include "oops/arrayOop.hpp"
  33 #include "oops/methodData.hpp"
  34 #include "oops/method.hpp"
  35 #include "oops/oop.inline.hpp"
  36 #include "prims/jvmtiExport.hpp"
  37 #include "prims/jvmtiThreadState.hpp"
  38 #include "runtime/arguments.hpp"
  39 #include "runtime/deoptimization.hpp"
  40 #include "runtime/frame.inline.hpp"
  41 #include "runtime/interfaceSupport.hpp"
  42 #include "runtime/sharedRuntime.hpp"
  43 #include "runtime/stubRoutines.hpp"
  44 #include "runtime/synchronizer.hpp"
  45 #include "runtime/timer.hpp"
  46 #include "runtime/vframeArray.hpp"
  47 #include "utilities/debug.hpp"
  48 #include "utilities/macros.hpp"
  49 #ifdef SHARK
  50 #include "shark/shark_globals.hpp"
  51 #endif
  52 
  53 #ifdef CC_INTERP
  54 
  55 // Routine exists to make tracebacks look decent in debugger
  56 // while we are recursed in the frame manager/c++ interpreter.
  57 // We could use an address in the frame manager but having
  58 // frames look natural in the debugger is a plus.
  59 extern "C" void RecursiveInterpreterActivation(interpreterState istate )
  60 {
  61   //
  62   ShouldNotReachHere();
  63 }
  64 
  65 
  66 #define __ _masm->
  67 #define STATE(field_name) (Address(state, byte_offset_of(BytecodeInterpreter, field_name)))
  68 
  69 Label fast_accessor_slow_entry_path;  // fast accessor methods need to be able to jmp to unsynchronized
  70                                       // c++ interpreter entry point this holds that entry point label.
  71 
  72 // default registers for state and sender_sp
  73 // state and sender_sp are the same on 32bit because we have no choice.
  74 // state could be rsi on 64bit but it is an arg reg and not callee save
  75 // so r13 is better choice.
  76 
  77 const Register state = NOT_LP64(rsi) LP64_ONLY(r13);
  78 const Register sender_sp_on_entry = NOT_LP64(rsi) LP64_ONLY(r13);
  79 
  80 // NEEDED for JVMTI?
  81 // address AbstractInterpreter::_remove_activation_preserving_args_entry;
  82 
  83 static address unctrap_frame_manager_entry  = NULL;
  84 
  85 static address deopt_frame_manager_return_atos  = NULL;
  86 static address deopt_frame_manager_return_btos  = NULL;
  87 static address deopt_frame_manager_return_itos  = NULL;
  88 static address deopt_frame_manager_return_ltos  = NULL;
  89 static address deopt_frame_manager_return_ftos  = NULL;
  90 static address deopt_frame_manager_return_dtos  = NULL;
  91 static address deopt_frame_manager_return_vtos  = NULL;
  92 
  93 int AbstractInterpreter::BasicType_as_index(BasicType type) {
  94   int i = 0;
  95   switch (type) {
  96     case T_BOOLEAN: i = 0; break;
  97     case T_CHAR   : i = 1; break;
  98     case T_BYTE   : i = 2; break;
  99     case T_SHORT  : i = 3; break;
 100     case T_INT    : i = 4; break;
 101     case T_VOID   : i = 5; break;
 102     case T_FLOAT  : i = 8; break;
 103     case T_LONG   : i = 9; break;
 104     case T_DOUBLE : i = 6; break;
 105     case T_OBJECT : // fall through
 106     case T_ARRAY  : i = 7; break;
 107     default       : ShouldNotReachHere();
 108   }
 109   assert(0 <= i && i < AbstractInterpreter::number_of_result_handlers, "index out of bounds");
 110   return i;
 111 }
 112 
 113 // Is this pc anywhere within code owned by the interpreter?
 114 // This only works for pc that might possibly be exposed to frame
 115 // walkers. It clearly misses all of the actual c++ interpreter
 116 // implementation
 117 bool CppInterpreter::contains(address pc)            {
 118     return (_code->contains(pc) ||
 119             pc == CAST_FROM_FN_PTR(address, RecursiveInterpreterActivation));
 120 }
 121 
 122 
 123 address CppInterpreterGenerator::generate_result_handler_for(BasicType type) {
 124   address entry = __ pc();
 125   switch (type) {
 126     case T_BOOLEAN: __ c2bool(rax);            break;
 127     case T_CHAR   : __ andl(rax, 0xFFFF);      break;
 128     case T_BYTE   : __ sign_extend_byte (rax); break;
 129     case T_SHORT  : __ sign_extend_short(rax); break;
 130     case T_VOID   : // fall thru
 131     case T_LONG   : // fall thru
 132     case T_INT    : /* nothing to do */        break;
 133 
 134     case T_DOUBLE :
 135     case T_FLOAT  :
 136       {
 137         const Register t = InterpreterRuntime::SignatureHandlerGenerator::temp();
 138         __ pop(t);                            // remove return address first
 139         // Must return a result for interpreter or compiler. In SSE
 140         // mode, results are returned in xmm0 and the FPU stack must
 141         // be empty.
 142         if (type == T_FLOAT && UseSSE >= 1) {
 143 #ifndef _LP64
 144           // Load ST0
 145           __ fld_d(Address(rsp, 0));
 146           // Store as float and empty fpu stack
 147           __ fstp_s(Address(rsp, 0));
 148 #endif // !_LP64
 149           // and reload
 150           __ movflt(xmm0, Address(rsp, 0));
 151         } else if (type == T_DOUBLE && UseSSE >= 2 ) {
 152           __ movdbl(xmm0, Address(rsp, 0));
 153         } else {
 154           // restore ST0
 155           __ fld_d(Address(rsp, 0));
 156         }
 157         // and pop the temp
 158         __ addptr(rsp, 2 * wordSize);
 159         __ push(t);                            // restore return address
 160       }
 161       break;
 162     case T_OBJECT :
 163       // retrieve result from frame
 164       __ movptr(rax, STATE(_oop_temp));
 165       // and verify it
 166       __ verify_oop(rax);
 167       break;
 168     default       : ShouldNotReachHere();
 169   }
 170   __ ret(0);                                   // return from result handler
 171   return entry;
 172 }
 173 
 174 // tosca based result to c++ interpreter stack based result.
 175 // Result goes to top of native stack.
 176 
 177 #undef EXTEND  // SHOULD NOT BE NEEDED
 178 address CppInterpreterGenerator::generate_tosca_to_stack_converter(BasicType type) {
 179   // A result is in the tosca (abi result) from either a native method call or compiled
 180   // code. Place this result on the java expression stack so C++ interpreter can use it.
 181   address entry = __ pc();
 182 
 183   const Register t = InterpreterRuntime::SignatureHandlerGenerator::temp();
 184   __ pop(t);                            // remove return address first
 185   switch (type) {
 186     case T_VOID:
 187        break;
 188     case T_BOOLEAN:
 189 #ifdef EXTEND
 190       __ c2bool(rax);
 191 #endif
 192       __ push(rax);
 193       break;
 194     case T_CHAR   :
 195 #ifdef EXTEND
 196       __ andl(rax, 0xFFFF);
 197 #endif
 198       __ push(rax);
 199       break;
 200     case T_BYTE   :
 201 #ifdef EXTEND
 202       __ sign_extend_byte (rax);
 203 #endif
 204       __ push(rax);
 205       break;
 206     case T_SHORT  :
 207 #ifdef EXTEND
 208       __ sign_extend_short(rax);
 209 #endif
 210       __ push(rax);
 211       break;
 212     case T_LONG    :
 213       __ push(rdx);                             // pushes useless junk on 64bit
 214       __ push(rax);
 215       break;
 216     case T_INT    :
 217       __ push(rax);
 218       break;
 219     case T_FLOAT  :
 220       // Result is in ST(0)/xmm0
 221       __ subptr(rsp, wordSize);
 222       if ( UseSSE < 1) {
 223         __ fstp_s(Address(rsp, 0));
 224       } else {
 225         __ movflt(Address(rsp, 0), xmm0);
 226       }
 227       break;
 228     case T_DOUBLE  :
 229       __ subptr(rsp, 2*wordSize);
 230       if ( UseSSE < 2 ) {
 231         __ fstp_d(Address(rsp, 0));
 232       } else {
 233         __ movdbl(Address(rsp, 0), xmm0);
 234       }
 235       break;
 236     case T_OBJECT :
 237       __ verify_oop(rax);                      // verify it
 238       __ push(rax);
 239       break;
 240     default       : ShouldNotReachHere();
 241   }
 242   __ jmp(t);                                   // return from result handler
 243   return entry;
 244 }
 245 
 246 address CppInterpreterGenerator::generate_stack_to_stack_converter(BasicType type) {
 247   // A result is in the java expression stack of the interpreted method that has just
 248   // returned. Place this result on the java expression stack of the caller.
 249   //
 250   // The current interpreter activation in rsi/r13 is for the method just returning its
 251   // result. So we know that the result of this method is on the top of the current
 252   // execution stack (which is pre-pushed) and will be return to the top of the caller
 253   // stack. The top of the callers stack is the bottom of the locals of the current
 254   // activation.
 255   // Because of the way activation are managed by the frame manager the value of rsp is
 256   // below both the stack top of the current activation and naturally the stack top
 257   // of the calling activation. This enable this routine to leave the return address
 258   // to the frame manager on the stack and do a vanilla return.
 259   //
 260   // On entry: rsi/r13 - interpreter state of activation returning a (potential) result
 261   // On Return: rsi/r13 - unchanged
 262   //            rax - new stack top for caller activation (i.e. activation in _prev_link)
 263   //
 264   // Can destroy rdx, rcx.
 265   //
 266 
 267   address entry = __ pc();
 268   const Register t = InterpreterRuntime::SignatureHandlerGenerator::temp();
 269   switch (type) {
 270     case T_VOID:
 271       __ movptr(rax, STATE(_locals));                                   // pop parameters get new stack value
 272       __ addptr(rax, wordSize);                                         // account for prepush before we return
 273       break;
 274     case T_FLOAT  :
 275     case T_BOOLEAN:
 276     case T_CHAR   :
 277     case T_BYTE   :
 278     case T_SHORT  :
 279     case T_INT    :
 280       // 1 word result
 281       __ movptr(rdx, STATE(_stack));
 282       __ movptr(rax, STATE(_locals));                                   // address for result
 283       __ movl(rdx, Address(rdx, wordSize));                             // get result
 284       __ movptr(Address(rax, 0), rdx);                                  // and store it
 285       break;
 286     case T_LONG    :
 287     case T_DOUBLE  :
 288       // return top two words on current expression stack to caller's expression stack
 289       // The caller's expression stack is adjacent to the current frame manager's intepretState
 290       // except we allocated one extra word for this intepretState so we won't overwrite it
 291       // when we return a two word result.
 292 
 293       __ movptr(rax, STATE(_locals));                                   // address for result
 294       __ movptr(rcx, STATE(_stack));
 295       __ subptr(rax, wordSize);                                         // need addition word besides locals[0]
 296       __ movptr(rdx, Address(rcx, 2*wordSize));                         // get result word (junk in 64bit)
 297       __ movptr(Address(rax, wordSize), rdx);                           // and store it
 298       __ movptr(rdx, Address(rcx, wordSize));                           // get result word
 299       __ movptr(Address(rax, 0), rdx);                                  // and store it
 300       break;
 301     case T_OBJECT :
 302       __ movptr(rdx, STATE(_stack));
 303       __ movptr(rax, STATE(_locals));                                   // address for result
 304       __ movptr(rdx, Address(rdx, wordSize));                           // get result
 305       __ verify_oop(rdx);                                               // verify it
 306       __ movptr(Address(rax, 0), rdx);                                  // and store it
 307       break;
 308     default       : ShouldNotReachHere();
 309   }
 310   __ ret(0);
 311   return entry;
 312 }
 313 
 314 address CppInterpreterGenerator::generate_stack_to_native_abi_converter(BasicType type) {
 315   // A result is in the java expression stack of the interpreted method that has just
 316   // returned. Place this result in the native abi that the caller expects.
 317   //
 318   // Similar to generate_stack_to_stack_converter above. Called at a similar time from the
 319   // frame manager execept in this situation the caller is native code (c1/c2/call_stub)
 320   // and so rather than return result onto caller's java expression stack we return the
 321   // result in the expected location based on the native abi.
 322   // On entry: rsi/r13 - interpreter state of activation returning a (potential) result
 323   // On Return: rsi/r13 - unchanged
 324   // Other registers changed [rax/rdx/ST(0) as needed for the result returned]
 325 
 326   address entry = __ pc();
 327   switch (type) {
 328     case T_VOID:
 329        break;
 330     case T_BOOLEAN:
 331     case T_CHAR   :
 332     case T_BYTE   :
 333     case T_SHORT  :
 334     case T_INT    :
 335       __ movptr(rdx, STATE(_stack));                                    // get top of stack
 336       __ movl(rax, Address(rdx, wordSize));                             // get result word 1
 337       break;
 338     case T_LONG    :
 339       __ movptr(rdx, STATE(_stack));                                    // get top of stack
 340       __ movptr(rax, Address(rdx, wordSize));                           // get result low word
 341       NOT_LP64(__ movl(rdx, Address(rdx, 2*wordSize));)                 // get result high word
 342       break;
 343     case T_FLOAT  :
 344       __ movptr(rdx, STATE(_stack));                                    // get top of stack
 345       if ( UseSSE >= 1) {
 346         __ movflt(xmm0, Address(rdx, wordSize));
 347       } else {
 348         __ fld_s(Address(rdx, wordSize));                               // pushd float result
 349       }
 350       break;
 351     case T_DOUBLE  :
 352       __ movptr(rdx, STATE(_stack));                                    // get top of stack
 353       if ( UseSSE > 1) {
 354         __ movdbl(xmm0, Address(rdx, wordSize));
 355       } else {
 356         __ fld_d(Address(rdx, wordSize));                               // push double result
 357       }
 358       break;
 359     case T_OBJECT :
 360       __ movptr(rdx, STATE(_stack));                                    // get top of stack
 361       __ movptr(rax, Address(rdx, wordSize));                           // get result word 1
 362       __ verify_oop(rax);                                               // verify it
 363       break;
 364     default       : ShouldNotReachHere();
 365   }
 366   __ ret(0);
 367   return entry;
 368 }
 369 
 370 address CppInterpreter::return_entry(TosState state, int length) {
 371   // make it look good in the debugger
 372   return CAST_FROM_FN_PTR(address, RecursiveInterpreterActivation);
 373 }
 374 
 375 address CppInterpreter::deopt_entry(TosState state, int length) {
 376   address ret = NULL;
 377   if (length != 0) {
 378     switch (state) {
 379       case atos: ret = deopt_frame_manager_return_atos; break;
 380       case btos: ret = deopt_frame_manager_return_btos; break;
 381       case ctos:
 382       case stos:
 383       case itos: ret = deopt_frame_manager_return_itos; break;
 384       case ltos: ret = deopt_frame_manager_return_ltos; break;
 385       case ftos: ret = deopt_frame_manager_return_ftos; break;
 386       case dtos: ret = deopt_frame_manager_return_dtos; break;
 387       case vtos: ret = deopt_frame_manager_return_vtos; break;
 388     }
 389   } else {
 390     ret = unctrap_frame_manager_entry;  // re-execute the bytecode ( e.g. uncommon trap)
 391   }
 392   assert(ret != NULL, "Not initialized");
 393   return ret;
 394 }
 395 
 396 // C++ Interpreter
 397 void CppInterpreterGenerator::generate_compute_interpreter_state(const Register state,
 398                                                                  const Register locals,
 399                                                                  const Register sender_sp,
 400                                                                  bool native) {
 401 
 402   // On entry the "locals" argument points to locals[0] (or where it would be in case no locals in
 403   // a static method). "state" contains any previous frame manager state which we must save a link
 404   // to in the newly generated state object. On return "state" is a pointer to the newly allocated
 405   // state object. We must allocate and initialize a new interpretState object and the method
 406   // expression stack. Because the returned result (if any) of the method will be placed on the caller's
 407   // expression stack and this will overlap with locals[0] (and locals[1] if double/long) we must
 408   // be sure to leave space on the caller's stack so that this result will not overwrite values when
 409   // locals[0] and locals[1] do not exist (and in fact are return address and saved rbp). So when
 410   // we are non-native we in essence ensure that locals[0-1] exist. We play an extra trick in
 411   // non-product builds and initialize this last local with the previous interpreterState as
 412   // this makes things look real nice in the debugger.
 413 
 414   // State on entry
 415   // Assumes locals == &locals[0]
 416   // Assumes state == any previous frame manager state (assuming call path from c++ interpreter)
 417   // Assumes rax = return address
 418   // rcx == senders_sp
 419   // rbx == method
 420   // Modifies rcx, rdx, rax
 421   // Returns:
 422   // state == address of new interpreterState
 423   // rsp == bottom of method's expression stack.
 424 
 425   const Address const_offset      (rbx, Method::const_offset());
 426 
 427 
 428   // On entry sp is the sender's sp. This includes the space for the arguments
 429   // that the sender pushed. If the sender pushed no args (a static) and the
 430   // caller returns a long then we need two words on the sender's stack which
 431   // are not present (although when we return a restore full size stack the
 432   // space will be present). If we didn't allocate two words here then when
 433   // we "push" the result of the caller's stack we would overwrite the return
 434   // address and the saved rbp. Not good. So simply allocate 2 words now
 435   // just to be safe. This is the "static long no_params() method" issue.
 436   // See Lo.java for a testcase.
 437   // We don't need this for native calls because they return result in
 438   // register and the stack is expanded in the caller before we store
 439   // the results on the stack.
 440 
 441   if (!native) {
 442 #ifdef PRODUCT
 443     __ subptr(rsp, 2*wordSize);
 444 #else /* PRODUCT */
 445     __ push((int32_t)NULL_WORD);
 446     __ push(state);                         // make it look like a real argument
 447 #endif /* PRODUCT */
 448   }
 449 
 450   // Now that we are assure of space for stack result, setup typical linkage
 451 
 452   __ push(rax);
 453   __ enter();
 454 
 455   __ mov(rax, state);                                  // save current state
 456 
 457   __ lea(rsp, Address(rsp, -(int)sizeof(BytecodeInterpreter)));
 458   __ mov(state, rsp);
 459 
 460   // rsi/r13 == state/locals rax == prevstate
 461 
 462   // initialize the "shadow" frame so that use since C++ interpreter not directly
 463   // recursive. Simpler to recurse but we can't trim expression stack as we call
 464   // new methods.
 465   __ movptr(STATE(_locals), locals);                    // state->_locals = locals()
 466   __ movptr(STATE(_self_link), state);                  // point to self
 467   __ movptr(STATE(_prev_link), rax);                    // state->_link = state on entry (NULL or previous state)
 468   __ movptr(STATE(_sender_sp), sender_sp);              // state->_sender_sp = sender_sp
 469 #ifdef _LP64
 470   __ movptr(STATE(_thread), r15_thread);                // state->_bcp = codes()
 471 #else
 472   __ get_thread(rax);                                   // get vm's javathread*
 473   __ movptr(STATE(_thread), rax);                       // state->_bcp = codes()
 474 #endif // _LP64
 475   __ movptr(rdx, Address(rbx, Method::const_offset())); // get constantMethodOop
 476   __ lea(rdx, Address(rdx, ConstMethod::codes_offset())); // get code base
 477   if (native) {
 478     __ movptr(STATE(_bcp), (int32_t)NULL_WORD);         // state->_bcp = NULL
 479   } else {
 480     __ movptr(STATE(_bcp), rdx);                        // state->_bcp = codes()
 481   }
 482   __ xorptr(rdx, rdx);
 483   __ movptr(STATE(_oop_temp), rdx);                     // state->_oop_temp = NULL (only really needed for native)
 484   __ movptr(STATE(_mdx), rdx);                          // state->_mdx = NULL
 485   __ movptr(rdx, Address(rbx, Method::const_offset()));
 486   __ movptr(rdx, Address(rdx, ConstMethod::constants_offset()));
 487   __ movptr(rdx, Address(rdx, ConstantPool::cache_offset_in_bytes()));
 488   __ movptr(STATE(_constants), rdx);                    // state->_constants = constants()
 489 
 490   __ movptr(STATE(_method), rbx);                       // state->_method = method()
 491   __ movl(STATE(_msg), (int32_t) BytecodeInterpreter::method_entry);   // state->_msg = initial method entry
 492   __ movptr(STATE(_result._to_call._callee), (int32_t) NULL_WORD); // state->_result._to_call._callee_callee = NULL
 493 
 494 
 495   __ movptr(STATE(_monitor_base), rsp);                 // set monitor block bottom (grows down) this would point to entry [0]
 496                                                         // entries run from -1..x where &monitor[x] ==
 497 
 498   {
 499     // Must not attempt to lock method until we enter interpreter as gc won't be able to find the
 500     // initial frame. However we allocate a free monitor so we don't have to shuffle the expression stack
 501     // immediately.
 502 
 503     // synchronize method
 504     const Address access_flags      (rbx, Method::access_flags_offset());
 505     const int entry_size            = frame::interpreter_frame_monitor_size() * wordSize;
 506     Label not_synced;
 507 
 508     __ movl(rax, access_flags);
 509     __ testl(rax, JVM_ACC_SYNCHRONIZED);
 510     __ jcc(Assembler::zero, not_synced);
 511 
 512     // Allocate initial monitor and pre initialize it
 513     // get synchronization object
 514 
 515     Label done;
 516     const int mirror_offset = in_bytes(Klass::java_mirror_offset());
 517     __ movl(rax, access_flags);
 518     __ testl(rax, JVM_ACC_STATIC);
 519     __ movptr(rax, Address(locals, 0));                   // get receiver (assume this is frequent case)
 520     __ jcc(Assembler::zero, done);
 521     __ movptr(rax, Address(rbx, Method::const_offset()));
 522     __ movptr(rax, Address(rax, ConstMethod::constants_offset()));
 523     __ movptr(rax, Address(rax, ConstantPool::pool_holder_offset_in_bytes()));
 524     __ movptr(rax, Address(rax, mirror_offset));
 525     __ bind(done);
 526     // add space for monitor & lock
 527     __ subptr(rsp, entry_size);                                           // add space for a monitor entry
 528     __ movptr(Address(rsp, BasicObjectLock::obj_offset_in_bytes()), rax); // store object
 529     __ bind(not_synced);
 530   }
 531 
 532   __ movptr(STATE(_stack_base), rsp);                                     // set expression stack base ( == &monitors[-count])
 533   if (native) {
 534     __ movptr(STATE(_stack), rsp);                                        // set current expression stack tos
 535     __ movptr(STATE(_stack_limit), rsp);
 536   } else {
 537     __ subptr(rsp, wordSize);                                             // pre-push stack
 538     __ movptr(STATE(_stack), rsp);                                        // set current expression stack tos
 539 
 540     // compute full expression stack limit
 541 
 542     __ movptr(rdx, Address(rbx, Method::const_offset()));
 543     __ load_unsigned_short(rdx, Address(rdx, ConstMethod::max_stack_offset())); // get size of expression stack in words
 544     __ negptr(rdx);                                                       // so we can subtract in next step
 545     // Allocate expression stack
 546     __ lea(rsp, Address(rsp, rdx, Address::times_ptr, -Method::extra_stack_words()));
 547     __ movptr(STATE(_stack_limit), rsp);
 548   }
 549 
 550 #ifdef _LP64
 551   // Make sure stack is properly aligned and sized for the abi
 552   __ subptr(rsp, frame::arg_reg_save_area_bytes); // windows
 553   __ andptr(rsp, -16); // must be 16 byte boundary (see amd64 ABI)
 554 #endif // _LP64
 555 
 556 
 557 
 558 }
 559 
 560 // Helpers for commoning out cases in the various type of method entries.
 561 //
 562 
 563 // increment invocation count & check for overflow
 564 //
 565 // Note: checking for negative value instead of overflow
 566 //       so we have a 'sticky' overflow test
 567 //
 568 // rbx,: method
 569 // rcx: invocation counter
 570 //
 571 void InterpreterGenerator::generate_counter_incr(Label* overflow, Label* profile_method, Label* profile_method_continue) {
 572   Label done;
 573   const Address invocation_counter(rax,
 574                 MethodCounters::invocation_counter_offset() +
 575                 InvocationCounter::counter_offset());
 576   const Address backedge_counter  (rax,
 577                 MethodCounter::backedge_counter_offset() +
 578                 InvocationCounter::counter_offset());
 579 
 580   __ get_method_counters(rbx, rax, done);
 581 
 582   if (ProfileInterpreter) {
 583     __ incrementl(Address(rax,
 584             MethodCounters::interpreter_invocation_counter_offset()));
 585   }
 586   // Update standard invocation counters
 587   __ movl(rcx, invocation_counter);
 588   __ increment(rcx, InvocationCounter::count_increment);
 589   __ movl(invocation_counter, rcx);             // save invocation count
 590 
 591   __ movl(rax, backedge_counter);               // load backedge counter
 592   __ andl(rax, InvocationCounter::count_mask_value);  // mask out the status bits
 593 
 594   __ addl(rcx, rax);                            // add both counters
 595 
 596   // profile_method is non-null only for interpreted method so
 597   // profile_method != NULL == !native_call
 598   // BytecodeInterpreter only calls for native so code is elided.
 599 
 600   __ cmp32(rcx,
 601            ExternalAddress((address)&InvocationCounter::InterpreterInvocationLimit));
 602   __ jcc(Assembler::aboveEqual, *overflow);
 603   __ bind(done);
 604 }
 605 
 606 void InterpreterGenerator::generate_counter_overflow(Label* do_continue) {
 607 
 608   // C++ interpreter on entry
 609   // rsi/r13 - new interpreter state pointer
 610   // rbp - interpreter frame pointer
 611   // rbx - method
 612 
 613   // On return (i.e. jump to entry_point) [ back to invocation of interpreter ]
 614   // rbx, - method
 615   // rcx - rcvr (assuming there is one)
 616   // top of stack return address of interpreter caller
 617   // rsp - sender_sp
 618 
 619   // C++ interpreter only
 620   // rsi/r13 - previous interpreter state pointer
 621 
 622   // InterpreterRuntime::frequency_counter_overflow takes one argument
 623   // indicating if the counter overflow occurs at a backwards branch (non-NULL bcp).
 624   // The call returns the address of the verified entry point for the method or NULL
 625   // if the compilation did not complete (either went background or bailed out).
 626   __ movptr(rax, (int32_t)false);
 627   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::frequency_counter_overflow), rax);
 628 
 629   // for c++ interpreter can rsi really be munged?
 630   __ lea(state, Address(rbp, -(int)sizeof(BytecodeInterpreter)));                               // restore state
 631   __ movptr(rbx, Address(state, byte_offset_of(BytecodeInterpreter, _method)));            // restore method
 632   __ movptr(rdi, Address(state, byte_offset_of(BytecodeInterpreter, _locals)));            // get locals pointer
 633 
 634   __ jmp(*do_continue, relocInfo::none);
 635 
 636 }
 637 
 638 void InterpreterGenerator::generate_stack_overflow_check(void) {
 639   // see if we've got enough room on the stack for locals plus overhead.
 640   // the expression stack grows down incrementally, so the normal guard
 641   // page mechanism will work for that.
 642   //
 643   // Registers live on entry:
 644   //
 645   // Asm interpreter
 646   // rdx: number of additional locals this frame needs (what we must check)
 647   // rbx,: Method*
 648 
 649   // C++ Interpreter
 650   // rsi/r13: previous interpreter frame state object
 651   // rdi: &locals[0]
 652   // rcx: # of locals
 653   // rdx: number of additional locals this frame needs (what we must check)
 654   // rbx: Method*
 655 
 656   // destroyed on exit
 657   // rax,
 658 
 659   // NOTE:  since the additional locals are also always pushed (wasn't obvious in
 660   // generate_method_entry) so the guard should work for them too.
 661   //
 662 
 663   // monitor entry size: see picture of stack set (generate_method_entry) and frame_i486.hpp
 664   const int entry_size    = frame::interpreter_frame_monitor_size() * wordSize;
 665 
 666   // total overhead size: entry_size + (saved rbp, thru expr stack bottom).
 667   // be sure to change this if you add/subtract anything to/from the overhead area
 668   const int overhead_size = (int)sizeof(BytecodeInterpreter);
 669 
 670   const int page_size = os::vm_page_size();
 671 
 672   Label after_frame_check;
 673 
 674   // compute rsp as if this were going to be the last frame on
 675   // the stack before the red zone
 676 
 677   Label after_frame_check_pop;
 678 
 679   // save rsi == caller's bytecode ptr (c++ previous interp. state)
 680   // QQQ problem here?? rsi overload????
 681   __ push(state);
 682 
 683   const Register thread = LP64_ONLY(r15_thread) NOT_LP64(rsi);
 684 
 685   NOT_LP64(__ get_thread(thread));
 686 
 687   const Address stack_base(thread, Thread::stack_base_offset());
 688   const Address stack_size(thread, Thread::stack_size_offset());
 689 
 690   // locals + overhead, in bytes
 691   // Always give one monitor to allow us to start interp if sync method.
 692   // Any additional monitors need a check when moving the expression stack
 693   const int one_monitor = frame::interpreter_frame_monitor_size() * wordSize;
 694   __ movptr(rax, Address(rbx, Method::const_offset()));
 695   __ load_unsigned_short(rax, Address(rax, ConstMethod::max_stack_offset())); // get size of expression stack in words
 696   __ lea(rax, Address(noreg, rax, Interpreter::stackElementScale(), one_monitor+Method::extra_stack_words()));
 697   __ lea(rax, Address(rax, rdx, Interpreter::stackElementScale(), overhead_size));
 698 
 699 #ifdef ASSERT
 700   Label stack_base_okay, stack_size_okay;
 701   // verify that thread stack base is non-zero
 702   __ cmpptr(stack_base, (int32_t)0);
 703   __ jcc(Assembler::notEqual, stack_base_okay);
 704   __ stop("stack base is zero");
 705   __ bind(stack_base_okay);
 706   // verify that thread stack size is non-zero
 707   __ cmpptr(stack_size, (int32_t)0);
 708   __ jcc(Assembler::notEqual, stack_size_okay);
 709   __ stop("stack size is zero");
 710   __ bind(stack_size_okay);
 711 #endif
 712 
 713   // Add stack base to locals and subtract stack size
 714   __ addptr(rax, stack_base);
 715   __ subptr(rax, stack_size);
 716 
 717   // We should have a magic number here for the size of the c++ interpreter frame.
 718   // We can't actually tell this ahead of time. The debug version size is around 3k
 719   // product is 1k and fastdebug is 4k
 720   const int slop = 6 * K;
 721 
 722   // Use the maximum number of pages we might bang.
 723   const int max_pages = StackShadowPages > (StackRedPages+StackYellowPages) ? StackShadowPages :
 724                                                                               (StackRedPages+StackYellowPages);
 725   // Only need this if we are stack banging which is temporary while
 726   // we're debugging.
 727   __ addptr(rax, slop + 2*max_pages * page_size);
 728 
 729   // check against the current stack bottom
 730   __ cmpptr(rsp, rax);
 731   __ jcc(Assembler::above, after_frame_check_pop);
 732 
 733   __ pop(state);  //  get c++ prev state.
 734 
 735      // throw exception return address becomes throwing pc
 736   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_StackOverflowError));
 737 
 738   // all done with frame size check
 739   __ bind(after_frame_check_pop);
 740   __ pop(state);
 741 
 742   __ bind(after_frame_check);
 743 }
 744 
 745 // Find preallocated  monitor and lock method (C++ interpreter)
 746 // rbx - Method*
 747 //
 748 void InterpreterGenerator::lock_method(void) {
 749   // assumes state == rsi/r13 == pointer to current interpreterState
 750   // minimally destroys rax, rdx|c_rarg1, rdi
 751   //
 752   // synchronize method
 753   const int entry_size            = frame::interpreter_frame_monitor_size() * wordSize;
 754   const Address access_flags      (rbx, Method::access_flags_offset());
 755 
 756   const Register monitor  = NOT_LP64(rdx) LP64_ONLY(c_rarg1);
 757 
 758   // find initial monitor i.e. monitors[-1]
 759   __ movptr(monitor, STATE(_monitor_base));                                   // get monitor bottom limit
 760   __ subptr(monitor, entry_size);                                             // point to initial monitor
 761 
 762 #ifdef ASSERT
 763   { Label L;
 764     __ movl(rax, access_flags);
 765     __ testl(rax, JVM_ACC_SYNCHRONIZED);
 766     __ jcc(Assembler::notZero, L);
 767     __ stop("method doesn't need synchronization");
 768     __ bind(L);
 769   }
 770 #endif // ASSERT
 771   // get synchronization object
 772   { Label done;
 773     const int mirror_offset = in_bytes(Klass::java_mirror_offset());
 774     __ movl(rax, access_flags);
 775     __ movptr(rdi, STATE(_locals));                                     // prepare to get receiver (assume common case)
 776     __ testl(rax, JVM_ACC_STATIC);
 777     __ movptr(rax, Address(rdi, 0));                                    // get receiver (assume this is frequent case)
 778     __ jcc(Assembler::zero, done);
 779     __ movptr(rax, Address(rbx, Method::const_offset()));
 780     __ movptr(rax, Address(rax, ConstMethod::constants_offset()));
 781     __ movptr(rax, Address(rax, ConstantPool::pool_holder_offset_in_bytes()));
 782     __ movptr(rax, Address(rax, mirror_offset));
 783     __ bind(done);
 784   }
 785 #ifdef ASSERT
 786   { Label L;
 787     __ cmpptr(rax, Address(monitor, BasicObjectLock::obj_offset_in_bytes()));   // correct object?
 788     __ jcc(Assembler::equal, L);
 789     __ stop("wrong synchronization lobject");
 790     __ bind(L);
 791   }
 792 #endif // ASSERT
 793   // can destroy rax, rdx|c_rarg1, rcx, and (via call_VM) rdi!
 794   __ lock_object(monitor);
 795 }
 796 
 797 // Call an accessor method (assuming it is resolved, otherwise drop into vanilla (slow path) entry
 798 
 799 address InterpreterGenerator::generate_accessor_entry(void) {
 800 
 801   // rbx: Method*
 802 
 803   // rsi/r13: senderSP must preserved for slow path, set SP to it on fast path
 804 
 805   Label xreturn_path;
 806 
 807   // do fastpath for resolved accessor methods
 808   if (UseFastAccessorMethods) {
 809 
 810     address entry_point = __ pc();
 811 
 812     Label slow_path;
 813     // If we need a safepoint check, generate full interpreter entry.
 814     ExternalAddress state(SafepointSynchronize::address_of_state());
 815     __ cmp32(ExternalAddress(SafepointSynchronize::address_of_state()),
 816              SafepointSynchronize::_not_synchronized);
 817 
 818     __ jcc(Assembler::notEqual, slow_path);
 819     // ASM/C++ Interpreter
 820     // Code: _aload_0, _(i|a)getfield, _(i|a)return or any rewrites thereof; parameter size = 1
 821     // Note: We can only use this code if the getfield has been resolved
 822     //       and if we don't have a null-pointer exception => check for
 823     //       these conditions first and use slow path if necessary.
 824     // rbx,: method
 825     // rcx: receiver
 826     __ movptr(rax, Address(rsp, wordSize));
 827 
 828     // check if local 0 != NULL and read field
 829     __ testptr(rax, rax);
 830     __ jcc(Assembler::zero, slow_path);
 831 
 832     // read first instruction word and extract bytecode @ 1 and index @ 2
 833     __ movptr(rdx, Address(rbx, Method::const_offset()));
 834     __ movptr(rdi, Address(rdx, ConstMethod::constants_offset()));
 835     __ movl(rdx, Address(rdx, ConstMethod::codes_offset()));
 836     // Shift codes right to get the index on the right.
 837     // The bytecode fetched looks like <index><0xb4><0x2a>
 838     __ shrl(rdx, 2*BitsPerByte);
 839     __ shll(rdx, exact_log2(in_words(ConstantPoolCacheEntry::size())));
 840     __ movptr(rdi, Address(rdi, ConstantPool::cache_offset_in_bytes()));
 841 
 842     // rax,: local 0
 843     // rbx,: method
 844     // rcx: receiver - do not destroy since it is needed for slow path!
 845     // rcx: scratch
 846     // rdx: constant pool cache index
 847     // rdi: constant pool cache
 848     // rsi/r13: sender sp
 849 
 850     // check if getfield has been resolved and read constant pool cache entry
 851     // check the validity of the cache entry by testing whether _indices field
 852     // contains Bytecode::_getfield in b1 byte.
 853     assert(in_words(ConstantPoolCacheEntry::size()) == 4, "adjust shift below");
 854     __ movl(rcx,
 855             Address(rdi,
 856                     rdx,
 857                     Address::times_ptr, ConstantPoolCache::base_offset() + ConstantPoolCacheEntry::indices_offset()));
 858     __ shrl(rcx, 2*BitsPerByte);
 859     __ andl(rcx, 0xFF);
 860     __ cmpl(rcx, Bytecodes::_getfield);
 861     __ jcc(Assembler::notEqual, slow_path);
 862 
 863     // Note: constant pool entry is not valid before bytecode is resolved
 864     __ movptr(rcx,
 865             Address(rdi,
 866                     rdx,
 867                     Address::times_ptr, ConstantPoolCache::base_offset() + ConstantPoolCacheEntry::f2_offset()));
 868     __ movl(rdx,
 869             Address(rdi,
 870                     rdx,
 871                     Address::times_ptr, ConstantPoolCache::base_offset() + ConstantPoolCacheEntry::flags_offset()));
 872 
 873     Label notByte, notShort, notChar;
 874     const Address field_address (rax, rcx, Address::times_1);
 875 
 876     // Need to differentiate between igetfield, agetfield, bgetfield etc.
 877     // because they are different sizes.
 878     // Use the type from the constant pool cache
 879     __ shrl(rdx, ConstantPoolCacheEntry::tos_state_shift);
 880     // Make sure we don't need to mask rdx after the above shift
 881     ConstantPoolCacheEntry::verify_tos_state_shift();
 882 #ifdef _LP64
 883     Label notObj;
 884     __ cmpl(rdx, atos);
 885     __ jcc(Assembler::notEqual, notObj);
 886     // atos
 887     __ movptr(rax, field_address);
 888     __ jmp(xreturn_path);
 889 
 890     __ bind(notObj);
 891 #endif // _LP64
 892     __ cmpl(rdx, btos);
 893     __ jcc(Assembler::notEqual, notByte);
 894     __ load_signed_byte(rax, field_address);
 895     __ jmp(xreturn_path);
 896 
 897     __ bind(notByte);
 898     __ cmpl(rdx, stos);
 899     __ jcc(Assembler::notEqual, notShort);
 900     __ load_signed_short(rax, field_address);
 901     __ jmp(xreturn_path);
 902 
 903     __ bind(notShort);
 904     __ cmpl(rdx, ctos);
 905     __ jcc(Assembler::notEqual, notChar);
 906     __ load_unsigned_short(rax, field_address);
 907     __ jmp(xreturn_path);
 908 
 909     __ bind(notChar);
 910 #ifdef ASSERT
 911     Label okay;
 912 #ifndef _LP64
 913     __ cmpl(rdx, atos);
 914     __ jcc(Assembler::equal, okay);
 915 #endif // _LP64
 916     __ cmpl(rdx, itos);
 917     __ jcc(Assembler::equal, okay);
 918     __ stop("what type is this?");
 919     __ bind(okay);
 920 #endif // ASSERT
 921     // All the rest are a 32 bit wordsize
 922     __ movl(rax, field_address);
 923 
 924     __ bind(xreturn_path);
 925 
 926     // _ireturn/_areturn
 927     __ pop(rdi);                               // get return address
 928     __ mov(rsp, sender_sp_on_entry);           // set sp to sender sp
 929     __ jmp(rdi);
 930 
 931     // generate a vanilla interpreter entry as the slow path
 932     __ bind(slow_path);
 933     // We will enter c++ interpreter looking like it was
 934     // called by the call_stub this will cause it to return
 935     // a tosca result to the invoker which might have been
 936     // the c++ interpreter itself.
 937 
 938     __ jmp(fast_accessor_slow_entry_path);
 939     return entry_point;
 940 
 941   } else {
 942     return NULL;
 943   }
 944 
 945 }
 946 
 947 address InterpreterGenerator::generate_Reference_get_entry(void) {
 948 #if INCLUDE_ALL_GCS
 949   if (UseG1GC) {
 950     // We need to generate have a routine that generates code to:
 951     //   * load the value in the referent field
 952     //   * passes that value to the pre-barrier.
 953     //
 954     // In the case of G1 this will record the value of the
 955     // referent in an SATB buffer if marking is active.
 956     // This will cause concurrent marking to mark the referent
 957     // field as live.
 958     Unimplemented();
 959   }
 960 #endif // INCLUDE_ALL_GCS
 961 
 962   // If G1 is not enabled then attempt to go through the accessor entry point
 963   // Reference.get is an accessor
 964   return generate_accessor_entry();
 965 }
 966 
 967 //
 968 // C++ Interpreter stub for calling a native method.
 969 // This sets up a somewhat different looking stack for calling the native method
 970 // than the typical interpreter frame setup but still has the pointer to
 971 // an interpreter state.
 972 //
 973 
 974 address InterpreterGenerator::generate_native_entry(bool synchronized) {
 975   // determine code generation flags
 976   bool inc_counter  = UseCompiler || CountCompiledCalls;
 977 
 978   // rbx: Method*
 979   // rcx: receiver (unused)
 980   // rsi/r13: previous interpreter state (if called from C++ interpreter) must preserve
 981   //      in any case. If called via c1/c2/call_stub rsi/r13 is junk (to use) but harmless
 982   //      to save/restore.
 983   address entry_point = __ pc();
 984 
 985   const Address constMethod       (rbx, Method::const_offset());
 986   const Address access_flags      (rbx, Method::access_flags_offset());
 987   const Address size_of_parameters(rcx, ConstMethod::size_of_parameters_offset());
 988 
 989   // rsi/r13 == state/locals rdi == prevstate
 990   const Register locals = rdi;
 991 
 992   // get parameter size (always needed)
 993   __ movptr(rcx, constMethod);
 994   __ load_unsigned_short(rcx, size_of_parameters);
 995 
 996   // rbx: Method*
 997   // rcx: size of parameters
 998   __ pop(rax);                                       // get return address
 999   // for natives the size of locals is zero
1000 
1001   // compute beginning of parameters /locals
1002 
1003   __ lea(locals, Address(rsp, rcx, Address::times_ptr, -wordSize));
1004 
1005   // initialize fixed part of activation frame
1006 
1007   // Assumes rax = return address
1008 
1009   // allocate and initialize new interpreterState and method expression stack
1010   // IN(locals) ->  locals
1011   // IN(state) -> previous frame manager state (NULL from stub/c1/c2)
1012   // destroys rax, rcx, rdx
1013   // OUT (state) -> new interpreterState
1014   // OUT(rsp) -> bottom of methods expression stack
1015 
1016   // save sender_sp
1017   __ mov(rcx, sender_sp_on_entry);
1018   // start with NULL previous state
1019   __ movptr(state, (int32_t)NULL_WORD);
1020   generate_compute_interpreter_state(state, locals, rcx, true);
1021 
1022 #ifdef ASSERT
1023   { Label L;
1024     __ movptr(rax, STATE(_stack_base));
1025 #ifdef _LP64
1026     // duplicate the alignment rsp got after setting stack_base
1027     __ subptr(rax, frame::arg_reg_save_area_bytes); // windows
1028     __ andptr(rax, -16); // must be 16 byte boundary (see amd64 ABI)
1029 #endif // _LP64
1030     __ cmpptr(rax, rsp);
1031     __ jcc(Assembler::equal, L);
1032     __ stop("broken stack frame setup in interpreter");
1033     __ bind(L);
1034   }
1035 #endif
1036 
1037   const Register unlock_thread = LP64_ONLY(r15_thread) NOT_LP64(rax);
1038   NOT_LP64(__ movptr(unlock_thread, STATE(_thread));) // get thread
1039   // Since at this point in the method invocation the exception handler
1040   // would try to exit the monitor of synchronized methods which hasn't
1041   // been entered yet, we set the thread local variable
1042   // _do_not_unlock_if_synchronized to true. The remove_activation will
1043   // check this flag.
1044 
1045   const Address do_not_unlock_if_synchronized(unlock_thread,
1046         in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
1047   __ movbool(do_not_unlock_if_synchronized, true);
1048 
1049   // make sure method is native & not abstract
1050 #ifdef ASSERT
1051   __ movl(rax, access_flags);
1052   {
1053     Label L;
1054     __ testl(rax, JVM_ACC_NATIVE);
1055     __ jcc(Assembler::notZero, L);
1056     __ stop("tried to execute non-native method as native");
1057     __ bind(L);
1058   }
1059   { Label L;
1060     __ testl(rax, JVM_ACC_ABSTRACT);
1061     __ jcc(Assembler::zero, L);
1062     __ stop("tried to execute abstract method in interpreter");
1063     __ bind(L);
1064   }
1065 #endif
1066 
1067 
1068   // increment invocation count & check for overflow
1069   Label invocation_counter_overflow;
1070   if (inc_counter) {
1071     generate_counter_incr(&invocation_counter_overflow, NULL, NULL);
1072   }
1073 
1074   Label continue_after_compile;
1075 
1076   __ bind(continue_after_compile);
1077 
1078   bang_stack_shadow_pages(true);
1079 
1080   // reset the _do_not_unlock_if_synchronized flag
1081   NOT_LP64(__ movl(rax, STATE(_thread));)                       // get thread
1082   __ movbool(do_not_unlock_if_synchronized, false);
1083 
1084 
1085   // check for synchronized native methods
1086   //
1087   // Note: This must happen *after* invocation counter check, since
1088   //       when overflow happens, the method should not be locked.
1089   if (synchronized) {
1090     // potentially kills rax, rcx, rdx, rdi
1091     lock_method();
1092   } else {
1093     // no synchronization necessary
1094 #ifdef ASSERT
1095       { Label L;
1096         __ movl(rax, access_flags);
1097         __ testl(rax, JVM_ACC_SYNCHRONIZED);
1098         __ jcc(Assembler::zero, L);
1099         __ stop("method needs synchronization");
1100         __ bind(L);
1101       }
1102 #endif
1103   }
1104 
1105   // start execution
1106 
1107   // jvmti support
1108   __ notify_method_entry();
1109 
1110   // work registers
1111   const Register method = rbx;
1112   const Register thread = LP64_ONLY(r15_thread) NOT_LP64(rdi);
1113   const Register t      = InterpreterRuntime::SignatureHandlerGenerator::temp();    // rcx|rscratch1
1114   const Address constMethod       (method, Method::const_offset());
1115   const Address size_of_parameters(t, ConstMethod::size_of_parameters_offset());
1116 
1117   // allocate space for parameters
1118   __ movptr(method, STATE(_method));
1119   __ verify_method_ptr(method);
1120   __ movptr(t, constMethod);
1121   __ load_unsigned_short(t, size_of_parameters);
1122   __ shll(t, 2);
1123 #ifdef _LP64
1124   __ subptr(rsp, t);
1125   __ subptr(rsp, frame::arg_reg_save_area_bytes); // windows
1126   __ andptr(rsp, -16); // must be 16 byte boundary (see amd64 ABI)
1127 #else
1128   __ addptr(t, 2*wordSize);     // allocate two more slots for JNIEnv and possible mirror
1129   __ subptr(rsp, t);
1130   __ andptr(rsp, -(StackAlignmentInBytes)); // gcc needs 16 byte aligned stacks to do XMM intrinsics
1131 #endif // _LP64
1132 
1133   // get signature handler
1134     Label pending_exception_present;
1135 
1136   { Label L;
1137     __ movptr(t, Address(method, Method::signature_handler_offset()));
1138     __ testptr(t, t);
1139     __ jcc(Assembler::notZero, L);
1140     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::prepare_native_call), method, false);
1141     __ movptr(method, STATE(_method));
1142     __ cmpptr(Address(thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
1143     __ jcc(Assembler::notEqual, pending_exception_present);
1144     __ verify_method_ptr(method);
1145     __ movptr(t, Address(method, Method::signature_handler_offset()));
1146     __ bind(L);
1147   }
1148 #ifdef ASSERT
1149   {
1150     Label L;
1151     __ push(t);
1152     __ get_thread(t);                                   // get vm's javathread*
1153     __ cmpptr(t, STATE(_thread));
1154     __ jcc(Assembler::equal, L);
1155     __ int3();
1156     __ bind(L);
1157     __ pop(t);
1158   }
1159 #endif //
1160 
1161   const Register from_ptr = InterpreterRuntime::SignatureHandlerGenerator::from();
1162   // call signature handler
1163   assert(InterpreterRuntime::SignatureHandlerGenerator::to  () == rsp, "adjust this code");
1164 
1165   // The generated handlers do not touch RBX (the method oop).
1166   // However, large signatures cannot be cached and are generated
1167   // each time here.  The slow-path generator will blow RBX
1168   // sometime, so we must reload it after the call.
1169   __ movptr(from_ptr, STATE(_locals));  // get the from pointer
1170   __ call(t);
1171   __ movptr(method, STATE(_method));
1172   __ verify_method_ptr(method);
1173 
1174   // result handler is in rax
1175   // set result handler
1176   __ movptr(STATE(_result_handler), rax);
1177 
1178 
1179   // get native function entry point
1180   { Label L;
1181     __ movptr(rax, Address(method, Method::native_function_offset()));
1182     __ testptr(rax, rax);
1183     __ jcc(Assembler::notZero, L);
1184     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::prepare_native_call), method);
1185     __ movptr(method, STATE(_method));
1186     __ verify_method_ptr(method);
1187     __ movptr(rax, Address(method, Method::native_function_offset()));
1188     __ bind(L);
1189   }
1190 
1191   // pass mirror handle if static call
1192   { Label L;
1193     const int mirror_offset = in_bytes(Klass::java_mirror_offset());
1194     __ movl(t, Address(method, Method::access_flags_offset()));
1195     __ testl(t, JVM_ACC_STATIC);
1196     __ jcc(Assembler::zero, L);
1197     // get mirror
1198     __ movptr(t, Address(method, Method:: const_offset()));
1199     __ movptr(t, Address(t, ConstMethod::constants_offset()));
1200     __ movptr(t, Address(t, ConstantPool::pool_holder_offset_in_bytes()));
1201     __ movptr(t, Address(t, mirror_offset));
1202     // copy mirror into activation object
1203     __ movptr(STATE(_oop_temp), t);
1204     // pass handle to mirror
1205 #ifdef _LP64
1206     __ lea(c_rarg1, STATE(_oop_temp));
1207 #else
1208     __ lea(t, STATE(_oop_temp));
1209     __ movptr(Address(rsp, wordSize), t);
1210 #endif // _LP64
1211     __ bind(L);
1212   }
1213 #ifdef ASSERT
1214   {
1215     Label L;
1216     __ push(t);
1217     __ get_thread(t);                                   // get vm's javathread*
1218     __ cmpptr(t, STATE(_thread));
1219     __ jcc(Assembler::equal, L);
1220     __ int3();
1221     __ bind(L);
1222     __ pop(t);
1223   }
1224 #endif //
1225 
1226   // pass JNIEnv
1227 #ifdef _LP64
1228   __ lea(c_rarg0, Address(thread, JavaThread::jni_environment_offset()));
1229 #else
1230   __ movptr(thread, STATE(_thread));          // get thread
1231   __ lea(t, Address(thread, JavaThread::jni_environment_offset()));
1232 
1233   __ movptr(Address(rsp, 0), t);
1234 #endif // _LP64
1235 
1236 #ifdef ASSERT
1237   {
1238     Label L;
1239     __ push(t);
1240     __ get_thread(t);                                   // get vm's javathread*
1241     __ cmpptr(t, STATE(_thread));
1242     __ jcc(Assembler::equal, L);
1243     __ int3();
1244     __ bind(L);
1245     __ pop(t);
1246   }
1247 #endif //
1248 
1249 #ifdef ASSERT
1250   { Label L;
1251     __ movl(t, Address(thread, JavaThread::thread_state_offset()));
1252     __ cmpl(t, _thread_in_Java);
1253     __ jcc(Assembler::equal, L);
1254     __ stop("Wrong thread state in native stub");
1255     __ bind(L);
1256   }
1257 #endif
1258 
1259   // Change state to native (we save the return address in the thread, since it might not
1260   // be pushed on the stack when we do a a stack traversal). It is enough that the pc()
1261   // points into the right code segment. It does not have to be the correct return pc.
1262 
1263   __ set_last_Java_frame(thread, noreg, rbp, __ pc());
1264 
1265   __ movl(Address(thread, JavaThread::thread_state_offset()), _thread_in_native);
1266 
1267   __ call(rax);
1268 
1269   // result potentially in rdx:rax or ST0
1270   __ movptr(method, STATE(_method));
1271   NOT_LP64(__ movptr(thread, STATE(_thread));)                  // get thread
1272 
1273   // The potential result is in ST(0) & rdx:rax
1274   // With C++ interpreter we leave any possible result in ST(0) until we are in result handler and then
1275   // we do the appropriate stuff for returning the result. rdx:rax must always be saved because just about
1276   // anything we do here will destroy it, st(0) is only saved if we re-enter the vm where it would
1277   // be destroyed.
1278   // It is safe to do these pushes because state is _thread_in_native and return address will be found
1279   // via _last_native_pc and not via _last_jave_sp
1280 
1281     // Must save the value of ST(0)/xmm0 since it could be destroyed before we get to result handler
1282     { Label Lpush, Lskip;
1283       ExternalAddress float_handler(AbstractInterpreter::result_handler(T_FLOAT));
1284       ExternalAddress double_handler(AbstractInterpreter::result_handler(T_DOUBLE));
1285       __ cmpptr(STATE(_result_handler), float_handler.addr());
1286       __ jcc(Assembler::equal, Lpush);
1287       __ cmpptr(STATE(_result_handler), double_handler.addr());
1288       __ jcc(Assembler::notEqual, Lskip);
1289       __ bind(Lpush);
1290       __ subptr(rsp, 2*wordSize);
1291       if ( UseSSE < 2 ) {
1292         __ fstp_d(Address(rsp, 0));
1293       } else {
1294         __ movdbl(Address(rsp, 0), xmm0);
1295       }
1296       __ bind(Lskip);
1297     }
1298 
1299   // save rax:rdx for potential use by result handler.
1300   __ push(rax);
1301 #ifndef _LP64
1302   __ push(rdx);
1303 #endif // _LP64
1304 
1305   // Verify or restore cpu control state after JNI call
1306   __ restore_cpu_control_state_after_jni();
1307 
1308   // change thread state
1309   __ movl(Address(thread, JavaThread::thread_state_offset()), _thread_in_native_trans);
1310   if(os::is_MP()) {
1311     // Write serialization page so VM thread can do a pseudo remote membar.
1312     // We use the current thread pointer to calculate a thread specific
1313     // offset to write to within the page. This minimizes bus traffic
1314     // due to cache line collision.
1315     __ serialize_memory(thread, rcx);
1316   }
1317 
1318   // check for safepoint operation in progress and/or pending suspend requests
1319   { Label Continue;
1320 
1321     __ cmp32(ExternalAddress(SafepointSynchronize::address_of_state()),
1322              SafepointSynchronize::_not_synchronized);
1323 
1324     // threads running native code and they are expected to self-suspend
1325     // when leaving the _thread_in_native state. We need to check for
1326     // pending suspend requests here.
1327     Label L;
1328     __ jcc(Assembler::notEqual, L);
1329     __ cmpl(Address(thread, JavaThread::suspend_flags_offset()), 0);
1330     __ jcc(Assembler::equal, Continue);
1331     __ bind(L);
1332 
1333     // Don't use call_VM as it will see a possible pending exception and forward it
1334     // and never return here preventing us from clearing _last_native_pc down below.
1335     // Also can't use call_VM_leaf either as it will check to see if rsi & rdi are
1336     // preserved and correspond to the bcp/locals pointers.
1337     //
1338 
1339     ((MacroAssembler*)_masm)->call_VM_leaf(CAST_FROM_FN_PTR(address, JavaThread::check_special_condition_for_native_trans),
1340                           thread);
1341     __ increment(rsp, wordSize);
1342 
1343     __ movptr(method, STATE(_method));
1344     __ verify_method_ptr(method);
1345     __ movptr(thread, STATE(_thread));                       // get thread
1346 
1347     __ bind(Continue);
1348   }
1349 
1350   // change thread state
1351   __ movl(Address(thread, JavaThread::thread_state_offset()), _thread_in_Java);
1352 
1353   __ reset_last_Java_frame(thread, true, true);
1354 
1355   // reset handle block
1356   __ movptr(t, Address(thread, JavaThread::active_handles_offset()));
1357   __ movptr(Address(t, JNIHandleBlock::top_offset_in_bytes()), (int32_t)NULL_WORD);
1358 
1359   // If result was an oop then unbox and save it in the frame
1360   { Label L;
1361     Label no_oop, store_result;
1362       ExternalAddress oop_handler(AbstractInterpreter::result_handler(T_OBJECT));
1363     __ cmpptr(STATE(_result_handler), oop_handler.addr());
1364     __ jcc(Assembler::notEqual, no_oop);
1365 #ifndef _LP64
1366     __ pop(rdx);
1367 #endif // _LP64
1368     __ pop(rax);
1369     __ testptr(rax, rax);
1370     __ jcc(Assembler::zero, store_result);
1371     // unbox
1372     __ movptr(rax, Address(rax, 0));
1373     __ bind(store_result);
1374     __ movptr(STATE(_oop_temp), rax);
1375     // keep stack depth as expected by pushing oop which will eventually be discarded
1376     __ push(rax);
1377 #ifndef _LP64
1378     __ push(rdx);
1379 #endif // _LP64
1380     __ bind(no_oop);
1381   }
1382 
1383   {
1384      Label no_reguard;
1385      __ cmpl(Address(thread, JavaThread::stack_guard_state_offset()), JavaThread::stack_guard_yellow_disabled);
1386      __ jcc(Assembler::notEqual, no_reguard);
1387 
1388      __ pusha();
1389      __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages)));
1390      __ popa();
1391 
1392      __ bind(no_reguard);
1393    }
1394 
1395 
1396   // QQQ Seems like for native methods we simply return and the caller will see the pending
1397   // exception and do the right thing. Certainly the interpreter will, don't know about
1398   // compiled methods.
1399   // Seems that the answer to above is no this is wrong. The old code would see the exception
1400   // and forward it before doing the unlocking and notifying jvmdi that method has exited.
1401   // This seems wrong need to investigate the spec.
1402 
1403   // handle exceptions (exception handling will handle unlocking!)
1404   { Label L;
1405     __ cmpptr(Address(thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
1406     __ jcc(Assembler::zero, L);
1407     __ bind(pending_exception_present);
1408 
1409     // There are potential results on the stack (rax/rdx, ST(0)) we ignore these and simply
1410     // return and let caller deal with exception. This skips the unlocking here which
1411     // seems wrong but seems to be what asm interpreter did. Can't find this in the spec.
1412     // Note: must preverve method in rbx
1413     //
1414 
1415     // remove activation
1416 
1417     __ movptr(t, STATE(_sender_sp));
1418     __ leave();                                  // remove frame anchor
1419     __ pop(rdi);                                 // get return address
1420     __ movptr(state, STATE(_prev_link));         // get previous state for return
1421     __ mov(rsp, t);                              // set sp to sender sp
1422     __ push(rdi);                                // push throwing pc
1423     // The skips unlocking!! This seems to be what asm interpreter does but seems
1424     // very wrong. Not clear if this violates the spec.
1425     __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
1426     __ bind(L);
1427   }
1428 
1429   // do unlocking if necessary
1430   { Label L;
1431     __ movl(t, Address(method, Method::access_flags_offset()));
1432     __ testl(t, JVM_ACC_SYNCHRONIZED);
1433     __ jcc(Assembler::zero, L);
1434     // the code below should be shared with interpreter macro assembler implementation
1435     { Label unlock;
1436     const Register monitor = NOT_LP64(rdx) LP64_ONLY(c_rarg1);
1437       // BasicObjectLock will be first in list, since this is a synchronized method. However, need
1438       // to check that the object has not been unlocked by an explicit monitorexit bytecode.
1439       __ movptr(monitor, STATE(_monitor_base));
1440       __ subptr(monitor, frame::interpreter_frame_monitor_size() * wordSize);  // address of initial monitor
1441 
1442       __ movptr(t, Address(monitor, BasicObjectLock::obj_offset_in_bytes()));
1443       __ testptr(t, t);
1444       __ jcc(Assembler::notZero, unlock);
1445 
1446       // Entry already unlocked, need to throw exception
1447       __ MacroAssembler::call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_illegal_monitor_state_exception));
1448       __ should_not_reach_here();
1449 
1450       __ bind(unlock);
1451       __ unlock_object(monitor);
1452       // unlock can blow rbx so restore it for path that needs it below
1453       __ movptr(method, STATE(_method));
1454     }
1455     __ bind(L);
1456   }
1457 
1458   // jvmti support
1459   // Note: This must happen _after_ handling/throwing any exceptions since
1460   //       the exception handler code notifies the runtime of method exits
1461   //       too. If this happens before, method entry/exit notifications are
1462   //       not properly paired (was bug - gri 11/22/99).
1463   __ notify_method_exit(vtos, InterpreterMacroAssembler::NotifyJVMTI);
1464 
1465   // restore potential result in rdx:rax, call result handler to restore potential result in ST0 & handle result
1466 #ifndef _LP64
1467   __ pop(rdx);
1468 #endif // _LP64
1469   __ pop(rax);
1470   __ movptr(t, STATE(_result_handler));       // get result handler
1471   __ call(t);                                 // call result handler to convert to tosca form
1472 
1473   // remove activation
1474 
1475   __ movptr(t, STATE(_sender_sp));
1476 
1477   __ leave();                                  // remove frame anchor
1478   __ pop(rdi);                                 // get return address
1479   __ movptr(state, STATE(_prev_link));         // get previous state for return (if c++ interpreter was caller)
1480   __ mov(rsp, t);                              // set sp to sender sp
1481   __ jmp(rdi);
1482 
1483   // invocation counter overflow
1484   if (inc_counter) {
1485     // Handle overflow of counter and compile method
1486     __ bind(invocation_counter_overflow);
1487     generate_counter_overflow(&continue_after_compile);
1488   }
1489 
1490   return entry_point;
1491 }
1492 
1493 // Generate entries that will put a result type index into rcx
1494 void CppInterpreterGenerator::generate_deopt_handling() {
1495 
1496   Label return_from_deopt_common;
1497 
1498   // Generate entries that will put a result type index into rcx
1499   // deopt needs to jump to here to enter the interpreter (return a result)
1500   deopt_frame_manager_return_atos  = __ pc();
1501 
1502   // rax is live here
1503   __ movl(rcx, AbstractInterpreter::BasicType_as_index(T_OBJECT));    // Result stub address array index
1504   __ jmp(return_from_deopt_common);
1505 
1506 
1507   // deopt needs to jump to here to enter the interpreter (return a result)
1508   deopt_frame_manager_return_btos  = __ pc();
1509 
1510   // rax is live here
1511   __ movl(rcx, AbstractInterpreter::BasicType_as_index(T_BOOLEAN));    // Result stub address array index
1512   __ jmp(return_from_deopt_common);
1513 
1514   // deopt needs to jump to here to enter the interpreter (return a result)
1515   deopt_frame_manager_return_itos  = __ pc();
1516 
1517   // rax is live here
1518   __ movl(rcx, AbstractInterpreter::BasicType_as_index(T_INT));    // Result stub address array index
1519   __ jmp(return_from_deopt_common);
1520 
1521   // deopt needs to jump to here to enter the interpreter (return a result)
1522 
1523   deopt_frame_manager_return_ltos  = __ pc();
1524   // rax,rdx are live here
1525   __ movl(rcx, AbstractInterpreter::BasicType_as_index(T_LONG));    // Result stub address array index
1526   __ jmp(return_from_deopt_common);
1527 
1528   // deopt needs to jump to here to enter the interpreter (return a result)
1529 
1530   deopt_frame_manager_return_ftos  = __ pc();
1531   // st(0) is live here
1532   __ movl(rcx, AbstractInterpreter::BasicType_as_index(T_FLOAT));    // Result stub address array index
1533   __ jmp(return_from_deopt_common);
1534 
1535   // deopt needs to jump to here to enter the interpreter (return a result)
1536   deopt_frame_manager_return_dtos  = __ pc();
1537 
1538   // st(0) is live here
1539   __ movl(rcx, AbstractInterpreter::BasicType_as_index(T_DOUBLE));    // Result stub address array index
1540   __ jmp(return_from_deopt_common);
1541 
1542   // deopt needs to jump to here to enter the interpreter (return a result)
1543   deopt_frame_manager_return_vtos  = __ pc();
1544 
1545   __ movl(rcx, AbstractInterpreter::BasicType_as_index(T_VOID));
1546 
1547   // Deopt return common
1548   // an index is present in rcx that lets us move any possible result being
1549   // return to the interpreter's stack
1550   //
1551   // Because we have a full sized interpreter frame on the youngest
1552   // activation the stack is pushed too deep to share the tosca to
1553   // stack converters directly. We shrink the stack to the desired
1554   // amount and then push result and then re-extend the stack.
1555   // We could have the code in size_activation layout a short
1556   // frame for the top activation but that would look different
1557   // than say sparc (which needs a full size activation because
1558   // the windows are in the way. Really it could be short? QQQ
1559   //
1560   __ bind(return_from_deopt_common);
1561 
1562   __ lea(state, Address(rbp, -(int)sizeof(BytecodeInterpreter)));
1563 
1564   // setup rsp so we can push the "result" as needed.
1565   __ movptr(rsp, STATE(_stack));                                   // trim stack (is prepushed)
1566   __ addptr(rsp, wordSize);                                        // undo prepush
1567 
1568   ExternalAddress tosca_to_stack((address)CppInterpreter::_tosca_to_stack);
1569   // Address index(noreg, rcx, Address::times_ptr);
1570   __ movptr(rcx, ArrayAddress(tosca_to_stack, Address(noreg, rcx, Address::times_ptr)));
1571   // __ movl(rcx, Address(noreg, rcx, Address::times_ptr, int(AbstractInterpreter::_tosca_to_stack)));
1572   __ call(rcx);                                                   // call result converter
1573 
1574   __ movl(STATE(_msg), (int)BytecodeInterpreter::deopt_resume);
1575   __ lea(rsp, Address(rsp, -wordSize));                            // prepush stack (result if any already present)
1576   __ movptr(STATE(_stack), rsp);                                   // inform interpreter of new stack depth (parameters removed,
1577                                                                    // result if any on stack already )
1578   __ movptr(rsp, STATE(_stack_limit));                             // restore expression stack to full depth
1579 }
1580 
1581 // Generate the code to handle a more_monitors message from the c++ interpreter
1582 void CppInterpreterGenerator::generate_more_monitors() {
1583 
1584 
1585   Label entry, loop;
1586   const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
1587   // 1. compute new pointers                     // rsp: old expression stack top
1588   __ movptr(rdx, STATE(_stack_base));            // rdx: old expression stack bottom
1589   __ subptr(rsp, entry_size);                    // move expression stack top limit
1590   __ subptr(STATE(_stack), entry_size);          // update interpreter stack top
1591   __ subptr(STATE(_stack_limit), entry_size);    // inform interpreter
1592   __ subptr(rdx, entry_size);                    // move expression stack bottom
1593   __ movptr(STATE(_stack_base), rdx);            // inform interpreter
1594   __ movptr(rcx, STATE(_stack));                 // set start value for copy loop
1595   __ jmp(entry);
1596   // 2. move expression stack contents
1597   __ bind(loop);
1598   __ movptr(rbx, Address(rcx, entry_size));      // load expression stack word from old location
1599   __ movptr(Address(rcx, 0), rbx);               // and store it at new location
1600   __ addptr(rcx, wordSize);                      // advance to next word
1601   __ bind(entry);
1602   __ cmpptr(rcx, rdx);                           // check if bottom reached
1603   __ jcc(Assembler::notEqual, loop);             // if not at bottom then copy next word
1604   // now zero the slot so we can find it.
1605   __ movptr(Address(rdx, BasicObjectLock::obj_offset_in_bytes()), (int32_t) NULL_WORD);
1606   __ movl(STATE(_msg), (int)BytecodeInterpreter::got_monitors);
1607 }
1608 
1609 
1610 // Initial entry to C++ interpreter from the call_stub.
1611 // This entry point is called the frame manager since it handles the generation
1612 // of interpreter activation frames via requests directly from the vm (via call_stub)
1613 // and via requests from the interpreter. The requests from the call_stub happen
1614 // directly thru the entry point. Requests from the interpreter happen via returning
1615 // from the interpreter and examining the message the interpreter has returned to
1616 // the frame manager. The frame manager can take the following requests:
1617 
1618 // NO_REQUEST - error, should never happen.
1619 // MORE_MONITORS - need a new monitor. Shuffle the expression stack on down and
1620 //                 allocate a new monitor.
1621 // CALL_METHOD - setup a new activation to call a new method. Very similar to what
1622 //               happens during entry during the entry via the call stub.
1623 // RETURN_FROM_METHOD - remove an activation. Return to interpreter or call stub.
1624 //
1625 // Arguments:
1626 //
1627 // rbx: Method*
1628 // rcx: receiver - unused (retrieved from stack as needed)
1629 // rsi/r13: previous frame manager state (NULL from the call_stub/c1/c2)
1630 //
1631 //
1632 // Stack layout at entry
1633 //
1634 // [ return address     ] <--- rsp
1635 // [ parameter n        ]
1636 //   ...
1637 // [ parameter 1        ]
1638 // [ expression stack   ]
1639 //
1640 //
1641 // We are free to blow any registers we like because the call_stub which brought us here
1642 // initially has preserved the callee save registers already.
1643 //
1644 //
1645 
1646 static address interpreter_frame_manager = NULL;
1647 
1648 address InterpreterGenerator::generate_normal_entry(bool synchronized) {
1649 
1650   // rbx: Method*
1651   // rsi/r13: sender sp
1652 
1653   // Because we redispatch "recursive" interpreter entries thru this same entry point
1654   // the "input" register usage is a little strange and not what you expect coming
1655   // from the call_stub. From the call stub rsi/rdi (current/previous) interpreter
1656   // state are NULL but on "recursive" dispatches they are what you'd expect.
1657   // rsi: current interpreter state (C++ interpreter) must preserve (null from call_stub/c1/c2)
1658 
1659 
1660   // A single frame manager is plenty as we don't specialize for synchronized. We could and
1661   // the code is pretty much ready. Would need to change the test below and for good measure
1662   // modify generate_interpreter_state to only do the (pre) sync stuff stuff for synchronized
1663   // routines. Not clear this is worth it yet.
1664 
1665   if (interpreter_frame_manager) return interpreter_frame_manager;
1666 
1667   address entry_point = __ pc();
1668 
1669   // Fast accessor methods share this entry point.
1670   // This works because frame manager is in the same codelet
1671   if (UseFastAccessorMethods && !synchronized) __ bind(fast_accessor_slow_entry_path);
1672 
1673   Label dispatch_entry_2;
1674   __ movptr(rcx, sender_sp_on_entry);
1675   __ movptr(state, (int32_t)NULL_WORD);                              // no current activation
1676 
1677   __ jmp(dispatch_entry_2);
1678 
1679   const Register locals  = rdi;
1680 
1681   Label re_dispatch;
1682 
1683   __ bind(re_dispatch);
1684 
1685   // save sender sp (doesn't include return address
1686   __ lea(rcx, Address(rsp, wordSize));
1687 
1688   __ bind(dispatch_entry_2);
1689 
1690   // save sender sp
1691   __ push(rcx);
1692 
1693   const Address constMethod       (rbx, Method::const_offset());
1694   const Address access_flags      (rbx, Method::access_flags_offset());
1695   const Address size_of_parameters(rdx, ConstMethod::size_of_parameters_offset());
1696   const Address size_of_locals    (rdx, ConstMethod::size_of_locals_offset());
1697 
1698   // const Address monitor_block_top (rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
1699   // const Address monitor_block_bot (rbp, frame::interpreter_frame_initial_sp_offset        * wordSize);
1700   // const Address monitor(rbp, frame::interpreter_frame_initial_sp_offset * wordSize - (int)sizeof(BasicObjectLock));
1701 
1702   // get parameter size (always needed)
1703   __ movptr(rdx, constMethod);
1704   __ load_unsigned_short(rcx, size_of_parameters);
1705 
1706   // rbx: Method*
1707   // rcx: size of parameters
1708   __ load_unsigned_short(rdx, size_of_locals);                     // get size of locals in words
1709 
1710   __ subptr(rdx, rcx);                                             // rdx = no. of additional locals
1711 
1712   // see if we've got enough room on the stack for locals plus overhead.
1713   generate_stack_overflow_check();                                 // C++
1714 
1715   // c++ interpreter does not use stack banging or any implicit exceptions
1716   // leave for now to verify that check is proper.
1717   bang_stack_shadow_pages(false);
1718 
1719 
1720 
1721   // compute beginning of parameters (rdi)
1722   __ lea(locals, Address(rsp, rcx, Address::times_ptr, wordSize));
1723 
1724   // save sender's sp
1725   // __ movl(rcx, rsp);
1726 
1727   // get sender's sp
1728   __ pop(rcx);
1729 
1730   // get return address
1731   __ pop(rax);
1732 
1733   // rdx - # of additional locals
1734   // allocate space for locals
1735   // explicitly initialize locals
1736   {
1737     Label exit, loop;
1738     __ testl(rdx, rdx);                               // (32bit ok)
1739     __ jcc(Assembler::lessEqual, exit);               // do nothing if rdx <= 0
1740     __ bind(loop);
1741     __ push((int32_t)NULL_WORD);                      // initialize local variables
1742     __ decrement(rdx);                                // until everything initialized
1743     __ jcc(Assembler::greater, loop);
1744     __ bind(exit);
1745   }
1746 
1747 
1748   // Assumes rax = return address
1749 
1750   // allocate and initialize new interpreterState and method expression stack
1751   // IN(locals) ->  locals
1752   // IN(state) -> any current interpreter activation
1753   // destroys rax, rcx, rdx, rdi
1754   // OUT (state) -> new interpreterState
1755   // OUT(rsp) -> bottom of methods expression stack
1756 
1757   generate_compute_interpreter_state(state, locals, rcx, false);
1758 
1759   // Call interpreter
1760 
1761   Label call_interpreter;
1762   __ bind(call_interpreter);
1763 
1764   // c++ interpreter does not use stack banging or any implicit exceptions
1765   // leave for now to verify that check is proper.
1766   bang_stack_shadow_pages(false);
1767 
1768 
1769   // Call interpreter enter here if message is
1770   // set and we know stack size is valid
1771 
1772   Label call_interpreter_2;
1773 
1774   __ bind(call_interpreter_2);
1775 
1776   {
1777     const Register thread  = NOT_LP64(rcx) LP64_ONLY(r15_thread);
1778 
1779 #ifdef _LP64
1780     __ mov(c_rarg0, state);
1781 #else
1782     __ push(state);                                                 // push arg to interpreter
1783     __ movptr(thread, STATE(_thread));
1784 #endif // _LP64
1785 
1786     // We can setup the frame anchor with everything we want at this point
1787     // as we are thread_in_Java and no safepoints can occur until we go to
1788     // vm mode. We do have to clear flags on return from vm but that is it
1789     //
1790     __ movptr(Address(thread, JavaThread::last_Java_fp_offset()), rbp);
1791     __ movptr(Address(thread, JavaThread::last_Java_sp_offset()), rsp);
1792 
1793     // Call the interpreter
1794 
1795     RuntimeAddress normal(CAST_FROM_FN_PTR(address, BytecodeInterpreter::run));
1796     RuntimeAddress checking(CAST_FROM_FN_PTR(address, BytecodeInterpreter::runWithChecks));
1797 
1798     __ call(JvmtiExport::can_post_interpreter_events() ? checking : normal);
1799     NOT_LP64(__ pop(rax);)                                          // discard parameter to run
1800     //
1801     // state is preserved since it is callee saved
1802     //
1803 
1804     // reset_last_Java_frame
1805 
1806     NOT_LP64(__ movl(thread, STATE(_thread));)
1807     __ reset_last_Java_frame(thread, true, true);
1808   }
1809 
1810   // examine msg from interpreter to determine next action
1811 
1812   __ movl(rdx, STATE(_msg));                                       // Get new message
1813 
1814   Label call_method;
1815   Label return_from_interpreted_method;
1816   Label throw_exception;
1817   Label bad_msg;
1818   Label do_OSR;
1819 
1820   __ cmpl(rdx, (int32_t)BytecodeInterpreter::call_method);
1821   __ jcc(Assembler::equal, call_method);
1822   __ cmpl(rdx, (int32_t)BytecodeInterpreter::return_from_method);
1823   __ jcc(Assembler::equal, return_from_interpreted_method);
1824   __ cmpl(rdx, (int32_t)BytecodeInterpreter::do_osr);
1825   __ jcc(Assembler::equal, do_OSR);
1826   __ cmpl(rdx, (int32_t)BytecodeInterpreter::throwing_exception);
1827   __ jcc(Assembler::equal, throw_exception);
1828   __ cmpl(rdx, (int32_t)BytecodeInterpreter::more_monitors);
1829   __ jcc(Assembler::notEqual, bad_msg);
1830 
1831   // Allocate more monitor space, shuffle expression stack....
1832 
1833   generate_more_monitors();
1834 
1835   __ jmp(call_interpreter);
1836 
1837   // uncommon trap needs to jump to here to enter the interpreter (re-execute current bytecode)
1838   unctrap_frame_manager_entry  = __ pc();
1839   //
1840   // Load the registers we need.
1841   __ lea(state, Address(rbp, -(int)sizeof(BytecodeInterpreter)));
1842   __ movptr(rsp, STATE(_stack_limit));                             // restore expression stack to full depth
1843   __ jmp(call_interpreter_2);
1844 
1845 
1846 
1847   //=============================================================================
1848   // Returning from a compiled method into a deopted method. The bytecode at the
1849   // bcp has completed. The result of the bytecode is in the native abi (the tosca
1850   // for the template based interpreter). Any stack space that was used by the
1851   // bytecode that has completed has been removed (e.g. parameters for an invoke)
1852   // so all that we have to do is place any pending result on the expression stack
1853   // and resume execution on the next bytecode.
1854 
1855 
1856   generate_deopt_handling();
1857   __ jmp(call_interpreter);
1858 
1859 
1860   // Current frame has caught an exception we need to dispatch to the
1861   // handler. We can get here because a native interpreter frame caught
1862   // an exception in which case there is no handler and we must rethrow
1863   // If it is a vanilla interpreted frame the we simply drop into the
1864   // interpreter and let it do the lookup.
1865 
1866   Interpreter::_rethrow_exception_entry = __ pc();
1867   // rax: exception
1868   // rdx: return address/pc that threw exception
1869 
1870   Label return_with_exception;
1871   Label unwind_and_forward;
1872 
1873   // restore state pointer.
1874   __ lea(state, Address(rbp,  -(int)sizeof(BytecodeInterpreter)));
1875 
1876   __ movptr(rbx, STATE(_method));                       // get method
1877 #ifdef _LP64
1878   __ movptr(Address(r15_thread, Thread::pending_exception_offset()), rax);
1879 #else
1880   __ movl(rcx, STATE(_thread));                       // get thread
1881 
1882   // Store exception with interpreter will expect it
1883   __ movptr(Address(rcx, Thread::pending_exception_offset()), rax);
1884 #endif // _LP64
1885 
1886   // is current frame vanilla or native?
1887 
1888   __ movl(rdx, access_flags);
1889   __ testl(rdx, JVM_ACC_NATIVE);
1890   __ jcc(Assembler::zero, return_with_exception);     // vanilla interpreted frame, handle directly
1891 
1892   // We drop thru to unwind a native interpreted frame with a pending exception
1893   // We jump here for the initial interpreter frame with exception pending
1894   // We unwind the current acivation and forward it to our caller.
1895 
1896   __ bind(unwind_and_forward);
1897 
1898   // unwind rbp, return stack to unextended value and re-push return address
1899 
1900   __ movptr(rcx, STATE(_sender_sp));
1901   __ leave();
1902   __ pop(rdx);
1903   __ mov(rsp, rcx);
1904   __ push(rdx);
1905   __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
1906 
1907   // Return point from a call which returns a result in the native abi
1908   // (c1/c2/jni-native). This result must be processed onto the java
1909   // expression stack.
1910   //
1911   // A pending exception may be present in which case there is no result present
1912 
1913   Label resume_interpreter;
1914   Label do_float;
1915   Label do_double;
1916   Label done_conv;
1917 
1918   // The FPU stack is clean if UseSSE >= 2 but must be cleaned in other cases
1919   if (UseSSE < 2) {
1920     __ lea(state, Address(rbp,  -(int)sizeof(BytecodeInterpreter)));
1921     __ movptr(rbx, STATE(_result._to_call._callee));                   // get method just executed
1922     __ movl(rcx, Address(rbx, Method::result_index_offset()));
1923     __ cmpl(rcx, AbstractInterpreter::BasicType_as_index(T_FLOAT));    // Result stub address array index
1924     __ jcc(Assembler::equal, do_float);
1925     __ cmpl(rcx, AbstractInterpreter::BasicType_as_index(T_DOUBLE));    // Result stub address array index
1926     __ jcc(Assembler::equal, do_double);
1927 #if !defined(_LP64) || defined(COMPILER1) || !defined(COMPILER2)
1928     __ empty_FPU_stack();
1929 #endif // COMPILER2
1930     __ jmp(done_conv);
1931 
1932     __ bind(do_float);
1933 #ifdef COMPILER2
1934     for (int i = 1; i < 8; i++) {
1935       __ ffree(i);
1936     }
1937 #endif // COMPILER2
1938     __ jmp(done_conv);
1939     __ bind(do_double);
1940 #ifdef COMPILER2
1941     for (int i = 1; i < 8; i++) {
1942       __ ffree(i);
1943     }
1944 #endif // COMPILER2
1945     __ jmp(done_conv);
1946   } else {
1947     __ MacroAssembler::verify_FPU(0, "generate_return_entry_for compiled");
1948     __ jmp(done_conv);
1949   }
1950 
1951   // Return point to interpreter from compiled/native method
1952   InternalAddress return_from_native_method(__ pc());
1953 
1954   __ bind(done_conv);
1955 
1956 
1957   // Result if any is in tosca. The java expression stack is in the state that the
1958   // calling convention left it (i.e. params may or may not be present)
1959   // Copy the result from tosca and place it on java expression stack.
1960 
1961   // Restore rsi/r13 as compiled code may not preserve it
1962 
1963   __ lea(state, Address(rbp,  -(int)sizeof(BytecodeInterpreter)));
1964 
1965   // restore stack to what we had when we left (in case i2c extended it)
1966 
1967   __ movptr(rsp, STATE(_stack));
1968   __ lea(rsp, Address(rsp, wordSize));
1969 
1970   // If there is a pending exception then we don't really have a result to process
1971 
1972 #ifdef _LP64
1973   __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
1974 #else
1975   __ movptr(rcx, STATE(_thread));                       // get thread
1976   __ cmpptr(Address(rcx, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
1977 #endif // _LP64
1978   __ jcc(Assembler::notZero, return_with_exception);
1979 
1980   // get method just executed
1981   __ movptr(rbx, STATE(_result._to_call._callee));
1982 
1983   // callee left args on top of expression stack, remove them
1984   __ movptr(rcx, constMethod);
1985   __ load_unsigned_short(rcx, Address(rcx, ConstMethod::size_of_parameters_offset()));
1986 
1987   __ lea(rsp, Address(rsp, rcx, Address::times_ptr));
1988 
1989   __ movl(rcx, Address(rbx, Method::result_index_offset()));
1990   ExternalAddress tosca_to_stack((address)CppInterpreter::_tosca_to_stack);
1991   // Address index(noreg, rax, Address::times_ptr);
1992   __ movptr(rcx, ArrayAddress(tosca_to_stack, Address(noreg, rcx, Address::times_ptr)));
1993   // __ movl(rcx, Address(noreg, rcx, Address::times_ptr, int(AbstractInterpreter::_tosca_to_stack)));
1994   __ call(rcx);                                               // call result converter
1995   __ jmp(resume_interpreter);
1996 
1997   // An exception is being caught on return to a vanilla interpreter frame.
1998   // Empty the stack and resume interpreter
1999 
2000   __ bind(return_with_exception);
2001 
2002   // Exception present, empty stack
2003   __ movptr(rsp, STATE(_stack_base));
2004   __ jmp(resume_interpreter);
2005 
2006   // Return from interpreted method we return result appropriate to the caller (i.e. "recursive"
2007   // interpreter call, or native) and unwind this interpreter activation.
2008   // All monitors should be unlocked.
2009 
2010   __ bind(return_from_interpreted_method);
2011 
2012   Label return_to_initial_caller;
2013 
2014   __ movptr(rbx, STATE(_method));                                   // get method just executed
2015   __ cmpptr(STATE(_prev_link), (int32_t)NULL_WORD);                 // returning from "recursive" interpreter call?
2016   __ movl(rax, Address(rbx, Method::result_index_offset())); // get result type index
2017   __ jcc(Assembler::equal, return_to_initial_caller);               // back to native code (call_stub/c1/c2)
2018 
2019   // Copy result to callers java stack
2020   ExternalAddress stack_to_stack((address)CppInterpreter::_stack_to_stack);
2021   // Address index(noreg, rax, Address::times_ptr);
2022 
2023   __ movptr(rax, ArrayAddress(stack_to_stack, Address(noreg, rax, Address::times_ptr)));
2024   // __ movl(rax, Address(noreg, rax, Address::times_ptr, int(AbstractInterpreter::_stack_to_stack)));
2025   __ call(rax);                                                     // call result converter
2026 
2027   Label unwind_recursive_activation;
2028   __ bind(unwind_recursive_activation);
2029 
2030   // returning to interpreter method from "recursive" interpreter call
2031   // result converter left rax pointing to top of the java stack for method we are returning
2032   // to. Now all we must do is unwind the state from the completed call
2033 
2034   __ movptr(state, STATE(_prev_link));                              // unwind state
2035   __ leave();                                                       // pop the frame
2036   __ mov(rsp, rax);                                                 // unwind stack to remove args
2037 
2038   // Resume the interpreter. The current frame contains the current interpreter
2039   // state object.
2040   //
2041 
2042   __ bind(resume_interpreter);
2043 
2044   // state == interpreterState object for method we are resuming
2045 
2046   __ movl(STATE(_msg), (int)BytecodeInterpreter::method_resume);
2047   __ lea(rsp, Address(rsp, -wordSize));                            // prepush stack (result if any already present)
2048   __ movptr(STATE(_stack), rsp);                                   // inform interpreter of new stack depth (parameters removed,
2049                                                                    // result if any on stack already )
2050   __ movptr(rsp, STATE(_stack_limit));                             // restore expression stack to full depth
2051   __ jmp(call_interpreter_2);                                      // No need to bang
2052 
2053   // interpreter returning to native code (call_stub/c1/c2)
2054   // convert result and unwind initial activation
2055   // rax - result index
2056 
2057   __ bind(return_to_initial_caller);
2058   ExternalAddress stack_to_native((address)CppInterpreter::_stack_to_native_abi);
2059   // Address index(noreg, rax, Address::times_ptr);
2060 
2061   __ movptr(rax, ArrayAddress(stack_to_native, Address(noreg, rax, Address::times_ptr)));
2062   __ call(rax);                                                    // call result converter
2063 
2064   Label unwind_initial_activation;
2065   __ bind(unwind_initial_activation);
2066 
2067   // RETURN TO CALL_STUB/C1/C2 code (result if any in rax/rdx ST(0))
2068 
2069   /* Current stack picture
2070 
2071         [ incoming parameters ]
2072         [ extra locals ]
2073         [ return address to CALL_STUB/C1/C2]
2074   fp -> [ CALL_STUB/C1/C2 fp ]
2075         BytecodeInterpreter object
2076         expression stack
2077   sp ->
2078 
2079   */
2080 
2081   // return restoring the stack to the original sender_sp value
2082 
2083   __ movptr(rcx, STATE(_sender_sp));
2084   __ leave();
2085   __ pop(rdi);                                                        // get return address
2086   // set stack to sender's sp
2087   __ mov(rsp, rcx);
2088   __ jmp(rdi);                                                        // return to call_stub
2089 
2090   // OSR request, adjust return address to make current frame into adapter frame
2091   // and enter OSR nmethod
2092 
2093   __ bind(do_OSR);
2094 
2095   Label remove_initial_frame;
2096 
2097   // We are going to pop this frame. Is there another interpreter frame underneath
2098   // it or is it callstub/compiled?
2099 
2100   // Move buffer to the expected parameter location
2101   __ movptr(rcx, STATE(_result._osr._osr_buf));
2102 
2103   __ movptr(rax, STATE(_result._osr._osr_entry));
2104 
2105   __ cmpptr(STATE(_prev_link), (int32_t)NULL_WORD);            // returning from "recursive" interpreter call?
2106   __ jcc(Assembler::equal, remove_initial_frame);              // back to native code (call_stub/c1/c2)
2107 
2108   __ movptr(sender_sp_on_entry, STATE(_sender_sp));            // get sender's sp in expected register
2109   __ leave();                                                  // pop the frame
2110   __ mov(rsp, sender_sp_on_entry);                             // trim any stack expansion
2111 
2112 
2113   // We know we are calling compiled so push specialized return
2114   // method uses specialized entry, push a return so we look like call stub setup
2115   // this path will handle fact that result is returned in registers and not
2116   // on the java stack.
2117 
2118   __ pushptr(return_from_native_method.addr());
2119 
2120   __ jmp(rax);
2121 
2122   __ bind(remove_initial_frame);
2123 
2124   __ movptr(rdx, STATE(_sender_sp));
2125   __ leave();
2126   // get real return
2127   __ pop(rsi);
2128   // set stack to sender's sp
2129   __ mov(rsp, rdx);
2130   // repush real return
2131   __ push(rsi);
2132   // Enter OSR nmethod
2133   __ jmp(rax);
2134 
2135 
2136 
2137 
2138   // Call a new method. All we do is (temporarily) trim the expression stack
2139   // push a return address to bring us back to here and leap to the new entry.
2140 
2141   __ bind(call_method);
2142 
2143   // stack points to next free location and not top element on expression stack
2144   // method expects sp to be pointing to topmost element
2145 
2146   __ movptr(rsp, STATE(_stack));                                     // pop args to c++ interpreter, set sp to java stack top
2147   __ lea(rsp, Address(rsp, wordSize));
2148 
2149   __ movptr(rbx, STATE(_result._to_call._callee));                   // get method to execute
2150 
2151   // don't need a return address if reinvoking interpreter
2152 
2153   // Make it look like call_stub calling conventions
2154 
2155   // Get (potential) receiver
2156   // get size of parameters in words
2157   __ movptr(rcx, constMethod);
2158   __ load_unsigned_short(rcx, Address(rcx, ConstMethod::size_of_parameters_offset()));
2159 
2160   ExternalAddress recursive(CAST_FROM_FN_PTR(address, RecursiveInterpreterActivation));
2161   __ pushptr(recursive.addr());                                      // make it look good in the debugger
2162 
2163   InternalAddress entry(entry_point);
2164   __ cmpptr(STATE(_result._to_call._callee_entry_point), entry.addr()); // returning to interpreter?
2165   __ jcc(Assembler::equal, re_dispatch);                             // yes
2166 
2167   __ pop(rax);                                                       // pop dummy address
2168 
2169 
2170   // get specialized entry
2171   __ movptr(rax, STATE(_result._to_call._callee_entry_point));
2172   // set sender SP
2173   __ mov(sender_sp_on_entry, rsp);
2174 
2175   // method uses specialized entry, push a return so we look like call stub setup
2176   // this path will handle fact that result is returned in registers and not
2177   // on the java stack.
2178 
2179   __ pushptr(return_from_native_method.addr());
2180 
2181   __ jmp(rax);
2182 
2183   __ bind(bad_msg);
2184   __ stop("Bad message from interpreter");
2185 
2186   // Interpreted method "returned" with an exception pass it on...
2187   // Pass result, unwind activation and continue/return to interpreter/call_stub
2188   // We handle result (if any) differently based on return to interpreter or call_stub
2189 
2190   Label unwind_initial_with_pending_exception;
2191 
2192   __ bind(throw_exception);
2193   __ cmpptr(STATE(_prev_link), (int32_t)NULL_WORD);                 // returning from recursive interpreter call?
2194   __ jcc(Assembler::equal, unwind_initial_with_pending_exception);  // no, back to native code (call_stub/c1/c2)
2195   __ movptr(rax, STATE(_locals));                                   // pop parameters get new stack value
2196   __ addptr(rax, wordSize);                                         // account for prepush before we return
2197   __ jmp(unwind_recursive_activation);
2198 
2199   __ bind(unwind_initial_with_pending_exception);
2200 
2201   // We will unwind the current (initial) interpreter frame and forward
2202   // the exception to the caller. We must put the exception in the
2203   // expected register and clear pending exception and then forward.
2204 
2205   __ jmp(unwind_and_forward);
2206 
2207   interpreter_frame_manager = entry_point;
2208   return entry_point;
2209 }
2210 
2211 address AbstractInterpreterGenerator::generate_method_entry(AbstractInterpreter::MethodKind kind) {
2212   // determine code generation flags
2213   bool synchronized = false;
2214   address entry_point = NULL;
2215 
2216   switch (kind) {
2217     case Interpreter::zerolocals             :                                                                             break;
2218     case Interpreter::zerolocals_synchronized: synchronized = true;                                                        break;
2219     case Interpreter::native                 : entry_point = ((InterpreterGenerator*)this)->generate_native_entry(false);  break;
2220     case Interpreter::native_synchronized    : entry_point = ((InterpreterGenerator*)this)->generate_native_entry(true);   break;
2221     case Interpreter::empty                  : entry_point = ((InterpreterGenerator*)this)->generate_empty_entry();        break;
2222     case Interpreter::accessor               : entry_point = ((InterpreterGenerator*)this)->generate_accessor_entry();     break;
2223     case Interpreter::abstract               : entry_point = ((InterpreterGenerator*)this)->generate_abstract_entry();     break;
2224     case Interpreter::method_handle          : entry_point = ((InterpreterGenerator*)this)->generate_method_handle_entry(); break;
2225 
2226     case Interpreter::java_lang_math_sin     : // fall thru
2227     case Interpreter::java_lang_math_cos     : // fall thru
2228     case Interpreter::java_lang_math_tan     : // fall thru
2229     case Interpreter::java_lang_math_abs     : // fall thru
2230     case Interpreter::java_lang_math_log     : // fall thru
2231     case Interpreter::java_lang_math_log10   : // fall thru
2232     case Interpreter::java_lang_math_sqrt    : entry_point = ((InterpreterGenerator*)this)->generate_math_entry(kind);     break;
2233     case Interpreter::java_lang_ref_reference_get
2234                                              : entry_point = ((InterpreterGenerator*)this)->generate_Reference_get_entry(); break;
2235     default                                  : ShouldNotReachHere();                                                       break;
2236   }
2237 
2238   if (entry_point) return entry_point;
2239 
2240   return ((InterpreterGenerator*)this)->generate_normal_entry(synchronized);
2241 
2242 }
2243 
2244 InterpreterGenerator::InterpreterGenerator(StubQueue* code)
2245  : CppInterpreterGenerator(code) {
2246    generate_all(); // down here so it can be "virtual"
2247 }
2248 
2249 // Deoptimization helpers for C++ interpreter
2250 
2251 // How much stack a method activation needs in words.
2252 int AbstractInterpreter::size_top_interpreter_activation(Method* method) {
2253 
2254   const int stub_code = 4;  // see generate_call_stub
2255   // Save space for one monitor to get into the interpreted method in case
2256   // the method is synchronized
2257   int monitor_size    = method->is_synchronized() ?
2258                                 1*frame::interpreter_frame_monitor_size() : 0;
2259 
2260   // total static overhead size. Account for interpreter state object, return
2261   // address, saved rbp and 2 words for a "static long no_params() method" issue.
2262 
2263   const int overhead_size = sizeof(BytecodeInterpreter)/wordSize +
2264     ( frame::sender_sp_offset - frame::link_offset) + 2;
2265 
2266   const int method_stack = (method->max_locals() + method->max_stack()) *
2267                            Interpreter::stackElementWords;
2268   return overhead_size + method_stack + stub_code;
2269 }
2270 
2271 // returns the activation size.
2272 static int size_activation_helper(int extra_locals_size, int monitor_size) {
2273   return (extra_locals_size +                  // the addition space for locals
2274           2*BytesPerWord +                     // return address and saved rbp
2275           2*BytesPerWord +                     // "static long no_params() method" issue
2276           sizeof(BytecodeInterpreter) +               // interpreterState
2277           monitor_size);                       // monitors
2278 }
2279 
2280 void BytecodeInterpreter::layout_interpreterState(interpreterState to_fill,
2281                                            frame* caller,
2282                                            frame* current,
2283                                            Method* method,
2284                                            intptr_t* locals,
2285                                            intptr_t* stack,
2286                                            intptr_t* stack_base,
2287                                            intptr_t* monitor_base,
2288                                            intptr_t* frame_bottom,
2289                                            bool is_top_frame
2290                                            )
2291 {
2292   // What about any vtable?
2293   //
2294   to_fill->_thread = JavaThread::current();
2295   // This gets filled in later but make it something recognizable for now
2296   to_fill->_bcp = method->code_base();
2297   to_fill->_locals = locals;
2298   to_fill->_constants = method->constants()->cache();
2299   to_fill->_method = method;
2300   to_fill->_mdx = NULL;
2301   to_fill->_stack = stack;
2302   if (is_top_frame && JavaThread::current()->popframe_forcing_deopt_reexecution() ) {
2303     to_fill->_msg = deopt_resume2;
2304   } else {
2305     to_fill->_msg = method_resume;
2306   }
2307   to_fill->_result._to_call._bcp_advance = 0;
2308   to_fill->_result._to_call._callee_entry_point = NULL; // doesn't matter to anyone
2309   to_fill->_result._to_call._callee = NULL; // doesn't matter to anyone
2310   to_fill->_prev_link = NULL;
2311 
2312   to_fill->_sender_sp = caller->unextended_sp();
2313 
2314   if (caller->is_interpreted_frame()) {
2315     interpreterState prev  = caller->get_interpreterState();
2316     to_fill->_prev_link = prev;
2317     // *current->register_addr(GR_Iprev_state) = (intptr_t) prev;
2318     // Make the prev callee look proper
2319     prev->_result._to_call._callee = method;
2320     if (*prev->_bcp == Bytecodes::_invokeinterface) {
2321       prev->_result._to_call._bcp_advance = 5;
2322     } else {
2323       prev->_result._to_call._bcp_advance = 3;
2324     }
2325   }
2326   to_fill->_oop_temp = NULL;
2327   to_fill->_stack_base = stack_base;
2328   // Need +1 here because stack_base points to the word just above the first expr stack entry
2329   // and stack_limit is supposed to point to the word just below the last expr stack entry.
2330   // See generate_compute_interpreter_state.
2331   to_fill->_stack_limit = stack_base - (method->max_stack() + 1);
2332   to_fill->_monitor_base = (BasicObjectLock*) monitor_base;
2333 
2334   to_fill->_self_link = to_fill;
2335   assert(stack >= to_fill->_stack_limit && stack < to_fill->_stack_base,
2336          "Stack top out of range");
2337 }
2338 
2339 int AbstractInterpreter::layout_activation(Method* method,
2340                                            int tempcount,  //
2341                                            int popframe_extra_args,
2342                                            int moncount,
2343                                            int caller_actual_parameters,
2344                                            int callee_param_count,
2345                                            int callee_locals,
2346                                            frame* caller,
2347                                            frame* interpreter_frame,
2348                                            bool is_top_frame,
2349                                            bool is_bottom_frame) {
2350 
2351   assert(popframe_extra_args == 0, "FIX ME");
2352   // NOTE this code must exactly mimic what InterpreterGenerator::generate_compute_interpreter_state()
2353   // does as far as allocating an interpreter frame.
2354   // If interpreter_frame!=NULL, set up the method, locals, and monitors.
2355   // The frame interpreter_frame, if not NULL, is guaranteed to be the right size,
2356   // as determined by a previous call to this method.
2357   // It is also guaranteed to be walkable even though it is in a skeletal state
2358   // NOTE: return size is in words not bytes
2359   // NOTE: tempcount is the current size of the java expression stack. For top most
2360   //       frames we will allocate a full sized expression stack and not the curback
2361   //       version that non-top frames have.
2362 
2363   // Calculate the amount our frame will be adjust by the callee. For top frame
2364   // this is zero.
2365 
2366   // NOTE: ia64 seems to do this wrong (or at least backwards) in that it
2367   // calculates the extra locals based on itself. Not what the callee does
2368   // to it. So it ignores last_frame_adjust value. Seems suspicious as far
2369   // as getting sender_sp correct.
2370 
2371   int extra_locals_size = (callee_locals - callee_param_count) * BytesPerWord;
2372   int monitor_size = sizeof(BasicObjectLock) * moncount;
2373 
2374   // First calculate the frame size without any java expression stack
2375   int short_frame_size = size_activation_helper(extra_locals_size,
2376                                                 monitor_size);
2377 
2378   // Now with full size expression stack
2379   int full_frame_size = short_frame_size + method->max_stack() * BytesPerWord;
2380 
2381   // and now with only live portion of the expression stack
2382   short_frame_size = short_frame_size + tempcount * BytesPerWord;
2383 
2384   // the size the activation is right now. Only top frame is full size
2385   int frame_size = (is_top_frame ? full_frame_size : short_frame_size);
2386 
2387   if (interpreter_frame != NULL) {
2388 #ifdef ASSERT
2389     assert(caller->unextended_sp() == interpreter_frame->interpreter_frame_sender_sp(), "Frame not properly walkable");
2390 #endif
2391 
2392     // MUCHO HACK
2393 
2394     intptr_t* frame_bottom = (intptr_t*) ((intptr_t)interpreter_frame->sp() - (full_frame_size - frame_size));
2395 
2396     /* Now fillin the interpreterState object */
2397 
2398     // The state object is the first thing on the frame and easily located
2399 
2400     interpreterState cur_state = (interpreterState) ((intptr_t)interpreter_frame->fp() - sizeof(BytecodeInterpreter));
2401 
2402 
2403     // Find the locals pointer. This is rather simple on x86 because there is no
2404     // confusing rounding at the callee to account for. We can trivially locate
2405     // our locals based on the current fp().
2406     // Note: the + 2 is for handling the "static long no_params() method" issue.
2407     // (too bad I don't really remember that issue well...)
2408 
2409     intptr_t* locals;
2410     // If the caller is interpreted we need to make sure that locals points to the first
2411     // argument that the caller passed and not in an area where the stack might have been extended.
2412     // because the stack to stack to converter needs a proper locals value in order to remove the
2413     // arguments from the caller and place the result in the proper location. Hmm maybe it'd be
2414     // simpler if we simply stored the result in the BytecodeInterpreter object and let the c++ code
2415     // adjust the stack?? HMMM QQQ
2416     //
2417     if (caller->is_interpreted_frame()) {
2418       // locals must agree with the caller because it will be used to set the
2419       // caller's tos when we return.
2420       interpreterState prev  = caller->get_interpreterState();
2421       // stack() is prepushed.
2422       locals = prev->stack() + method->size_of_parameters();
2423       // locals = caller->unextended_sp() + (method->size_of_parameters() - 1);
2424       if (locals != interpreter_frame->fp() + frame::sender_sp_offset + (method->max_locals() - 1) + 2) {
2425         // os::breakpoint();
2426       }
2427     } else {
2428       // this is where a c2i would have placed locals (except for the +2)
2429       locals = interpreter_frame->fp() + frame::sender_sp_offset + (method->max_locals() - 1) + 2;
2430     }
2431 
2432     intptr_t* monitor_base = (intptr_t*) cur_state;
2433     intptr_t* stack_base = (intptr_t*) ((intptr_t) monitor_base - monitor_size);
2434     /* +1 because stack is always prepushed */
2435     intptr_t* stack = (intptr_t*) ((intptr_t) stack_base - (tempcount + 1) * BytesPerWord);
2436 
2437 
2438     BytecodeInterpreter::layout_interpreterState(cur_state,
2439                                           caller,
2440                                           interpreter_frame,
2441                                           method,
2442                                           locals,
2443                                           stack,
2444                                           stack_base,
2445                                           monitor_base,
2446                                           frame_bottom,
2447                                           is_top_frame);
2448 
2449     // BytecodeInterpreter::pd_layout_interpreterState(cur_state, interpreter_return_address, interpreter_frame->fp());
2450   }
2451   return frame_size/BytesPerWord;
2452 }
2453 
2454 #endif // CC_INTERP (all)