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