hotspot/src/cpu/x86/vm/cppInterpreter_x86.cpp

Print this page
rev 611 : Merge
   1 #ifdef USE_PRAGMA_IDENT_HDR
   2 #pragma ident "@(#)cppInterpreter_x86.cpp       1.2 07/09/17 09:58:27 JVM"
   3 #endif
   4 /*
   5  * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  23  * CA 95054 USA or visit www.sun.com if you need additional information or
  24  * have any questions.
  25  *  


  30 
  31 #ifdef CC_INTERP
  32 
  33 // Routine exists to make tracebacks look decent in debugger
  34 // while we are recursed in the frame manager/c++ interpreter.
  35 // We could use an address in the frame manager but having
  36 // frames look natural in the debugger is a plus.
  37 extern "C" void RecursiveInterpreterActivation(interpreterState istate )
  38 {
  39   // 
  40   ShouldNotReachHere(); 
  41 }
  42 
  43 
  44 #define __ _masm->
  45 #define STATE(field_name) (Address(state, byte_offset_of(BytecodeInterpreter, field_name)))
  46 
  47 Label fast_accessor_slow_entry_path;  // fast accessor methods need to be able to jmp to unsynchronized
  48                                       // c++ interpreter entry point this holds that entry point label.
  49 








  50 // NEEDED for JVMTI?
  51 // address AbstractInterpreter::_remove_activation_preserving_args_entry;
  52 
  53 static address unctrap_frame_manager_entry  = NULL;
  54 
  55 static address deopt_frame_manager_return_atos  = NULL;
  56 static address deopt_frame_manager_return_btos  = NULL;
  57 static address deopt_frame_manager_return_itos  = NULL;
  58 static address deopt_frame_manager_return_ltos  = NULL;
  59 static address deopt_frame_manager_return_ftos  = NULL;
  60 static address deopt_frame_manager_return_dtos  = NULL;
  61 static address deopt_frame_manager_return_vtos  = NULL;
  62 
  63 int AbstractInterpreter::BasicType_as_index(BasicType type) {
  64   int i = 0;
  65   switch (type) {
  66     case T_BOOLEAN: i = 0; break;
  67     case T_CHAR   : i = 1; break;
  68     case T_BYTE   : i = 2; break;
  69     case T_SHORT  : i = 3; break;


  74     case T_DOUBLE : i = 6; break;
  75     case T_OBJECT : // fall through
  76     case T_ARRAY  : i = 7; break;
  77     default       : ShouldNotReachHere();
  78   }
  79   assert(0 <= i && i < AbstractInterpreter::number_of_result_handlers, "index out of bounds");
  80   return i;
  81 }
  82 
  83 // Is this pc anywhere within code owned by the interpreter?
  84 // This only works for pc that might possibly be exposed to frame
  85 // walkers. It clearly misses all of the actual c++ interpreter
  86 // implementation
  87 bool CppInterpreter::contains(address pc)            {
  88     return (_code->contains(pc) ||
  89             pc == CAST_FROM_FN_PTR(address, RecursiveInterpreterActivation));
  90 }
  91 
  92 
  93 address CppInterpreterGenerator::generate_result_handler_for(BasicType type) {
  94   const Register state   = rsi;                                    // current activation object, valid on entry
  95   address entry = __ pc();
  96   switch (type) {
  97     case T_BOOLEAN: __ c2bool(rax);            break;
  98     case T_CHAR   : __ andl(rax, 0xFFFF);      break;
  99     case T_BYTE   : __ sign_extend_byte (rax); break;
 100     case T_SHORT  : __ sign_extend_short(rax); break;
 101     case T_VOID   : // fall thru
 102     case T_LONG   : // fall thru
 103     case T_INT    : /* nothing to do */        break;

 104     case T_DOUBLE :
 105     case T_FLOAT  :
 106       { const Register t = InterpreterRuntime::SignatureHandlerGenerator::temp();
 107         __ popl(t);                            // remove return address first
 108         __ pop_dtos_to_rsp();
 109         // Must return a result for interpreter or compiler. In SSE
 110         // mode, results are returned in xmm0 and the FPU stack must
 111         // be empty.
 112         if (type == T_FLOAT && UseSSE >= 1) {

 113           // Load ST0
 114           __ fld_d(Address(rsp, 0));
 115           // Store as float and empty fpu stack
 116           __ fstp_s(Address(rsp, 0));

 117           // and reload
 118           __ movflt(xmm0, Address(rsp, 0));
 119         } else if (type == T_DOUBLE && UseSSE >= 2 ) {
 120           __ movdbl(xmm0, Address(rsp, 0));
 121         } else {
 122           // restore ST0
 123           __ fld_d(Address(rsp, 0));
 124         }
 125         // and pop the temp
 126         __ addl(rsp, 2 * wordSize);
 127         __ pushl(t);                           // restore return address
 128       }
 129       break;
 130     case T_OBJECT :
 131       // retrieve result from frame
 132       __ movl(rax, STATE(_oop_temp));
 133       // and verify it
 134       __ verify_oop(rax);
 135       break;
 136     default       : ShouldNotReachHere();
 137   }
 138   __ ret(0);                                   // return from result handler
 139   return entry;
 140 }
 141 
 142 // tosca based result to c++ interpreter stack based result.
 143 // Result goes to top of native stack.
 144 
 145 #undef EXTEND  // SHOULD NOT BE NEEDED
 146 address CppInterpreterGenerator::generate_tosca_to_stack_converter(BasicType type) {
 147   // A result is in the tosca (abi result) from either a native method call or compiled
 148   // code. Place this result on the java expression stack so C++ interpreter can use it.
 149   address entry = __ pc();
 150 
 151   const Register t = InterpreterRuntime::SignatureHandlerGenerator::temp();
 152   __ popl(t);                            // remove return address first
 153   switch (type) {
 154     case T_VOID:  
 155        break;
 156     case T_BOOLEAN: 
 157 #ifdef EXTEND
 158       __ c2bool(rax);            
 159 #endif
 160       __ pushl(rax);
 161       break;
 162     case T_CHAR   : 
 163 #ifdef EXTEND
 164       __ andl(rax, 0xFFFF);      
 165 #endif
 166       __ pushl(rax);
 167       break;
 168     case T_BYTE   : 
 169 #ifdef EXTEND
 170       __ sign_extend_byte (rax); 
 171 #endif
 172       __ pushl(rax);
 173       break;
 174     case T_SHORT  : 
 175 #ifdef EXTEND
 176       __ sign_extend_short(rax); 
 177 #endif
 178       __ pushl(rax);
 179       break;
 180     case T_LONG    : 
 181       __ pushl(rdx);
 182       __ pushl(rax);
 183       break;
 184     case T_INT    : 
 185       __ pushl(rax);
 186       break;
 187     case T_FLOAT  :
 188       // Result is in ST(0)

 189       if ( UseSSE < 1) {
 190         __ push(ftos);                           // and save it
 191       } else {
 192         __ subl(rsp, wordSize);
 193         __ movflt(Address(rsp, 0), xmm0);
 194       }
 195       break;
 196     case T_DOUBLE  :

 197       if ( UseSSE < 2 ) {
 198         __ push(dtos);                           // put ST0 on java stack
 199       } else {
 200         __ subl(rsp, 2*wordSize);
 201         __ movdbl(Address(rsp, 0), xmm0);
 202       }
 203       break;
 204     case T_OBJECT :
 205       __ verify_oop(rax);                      // verify it
 206       __ pushl(rax);
 207       break;
 208     default       : ShouldNotReachHere();
 209   }
 210   __ jmp(t);                                   // return from result handler
 211   return entry;
 212 }
 213 
 214 address CppInterpreterGenerator::generate_stack_to_stack_converter(BasicType type) {
 215   // A result is in the java expression stack of the interpreted method that has just
 216   // returned. Place this result on the java expression stack of the caller.
 217   //
 218   // The current interpreter activation in rsi is for the method just returning its
 219   // result. So we know that the result of this method is on the top of the current
 220   // execution stack (which is pre-pushed) and will be return to the top of the caller
 221   // stack. The top of the callers stack is the bottom of the locals of the current
 222   // activation.
 223   // Because of the way activation are managed by the frame manager the value of rsp is
 224   // below both the stack top of the current activation and naturally the stack top
 225   // of the calling activation. This enable this routine to leave the return address
 226   // to the frame manager on the stack and do a vanilla return.
 227   //
 228   // On entry: rsi - interpreter state of activation returning a (potential) result
 229   // On Return: rsi - unchanged
 230   //            rax - new stack top for caller activation (i.e. activation in _prev_link)
 231   //
 232   // Can destroy rdx, rcx.
 233   //
 234 
 235   address entry = __ pc();
 236   const Register state   = rsi;                                    // current activation object, valid on entry
 237   const Register t = InterpreterRuntime::SignatureHandlerGenerator::temp();
 238   switch (type) {
 239     case T_VOID:  
 240       __ movl(rax, STATE(_locals));                                     // pop parameters get new stack value
 241       __ addl(rax, wordSize);                                           // account for prepush before we return
 242       break;
 243     case T_FLOAT  :
 244     case T_BOOLEAN: 
 245     case T_CHAR   : 
 246     case T_BYTE   : 
 247     case T_SHORT  : 
 248     case T_INT    : 
 249       // 1 word result
 250       __ movl(rdx, STATE(_stack));
 251       __ movl(rax, STATE(_locals));                                     // address for result
 252       __ movl(rdx, Address(rdx, wordSize));                             // get result
 253       __ movl(Address(rax, 0), rdx);                                    // and store it
 254       break;
 255     case T_LONG    : 
 256     case T_DOUBLE  :
 257       // return top two words on current expression stack to caller's expression stack
 258       // The caller's expression stack is adjacent to the current frame manager's intepretState
 259       // except we allocated one extra word for this intepretState so we won't overwrite it
 260       // when we return a two word result.
 261 
 262       __ movl(rax, STATE(_locals));                                     // address for result
 263       __ movl(rcx, STATE(_stack));
 264       __ subl(rax, wordSize);                                           // need addition word besides locals[0]
 265       __ movl(rdx, Address(rcx, 2*wordSize));                           // get result word 
 266       __ movl(Address(rax, wordSize), rdx);                             // and store it
 267       __ movl(rdx, Address(rcx, wordSize));                             // get result word
 268       __ movl(Address(rax, 0), rdx);                                    // and store it
 269       break;
 270     case T_OBJECT :
 271       __ movl(rdx, STATE(_stack));
 272       __ movl(rax, STATE(_locals));                                     // address for result
 273       __ movl(rdx, Address(rdx, wordSize));                             // get result
 274       __ verify_oop(rdx);                                               // verify it
 275       __ movl(Address(rax, 0), rdx);                                    // and store it
 276       break;
 277     default       : ShouldNotReachHere();
 278   }
 279   __ ret(0);
 280   return entry;
 281 }
 282 
 283 address CppInterpreterGenerator::generate_stack_to_native_abi_converter(BasicType type) {
 284   // A result is in the java expression stack of the interpreted method that has just
 285   // returned. Place this result in the native abi that the caller expects.
 286   //
 287   // Similar to generate_stack_to_stack_converter above. Called at a similar time from the
 288   // frame manager execept in this situation the caller is native code (c1/c2/call_stub)
 289   // and so rather than return result onto caller's java expression stack we return the
 290   // result in the expected location based on the native abi.
 291   // On entry: rsi - interpreter state of activation returning a (potential) result
 292   // On Return: rsi - unchanged
 293   // Other registers changed [rax/rdx/ST(0) as needed for the result returned]
 294 
 295   address entry = __ pc();
 296   const Register state   = rsi;                                    // current activation object, valid on entry
 297   switch (type) {
 298     case T_VOID:  
 299        break;
 300     case T_BOOLEAN: 
 301     case T_CHAR   : 
 302     case T_BYTE   : 
 303     case T_SHORT  : 
 304     case T_INT    : 
 305       __ movl(rdx, STATE(_stack));                                      // get top of stack
 306       __ movl(rax, Address(rdx, wordSize));                             // get result word 1
 307       break;
 308     case T_LONG    : 
 309       __ movl(rdx, STATE(_stack));                                      // get top of stack
 310       __ movl(rax, Address(rdx, wordSize));                             // get result low word 
 311       __ movl(rdx, Address(rdx, 2*wordSize));                           // get result high word
 312       break;
 313       break;
 314     case T_FLOAT  :
 315       __ movl(rdx, STATE(_stack));                                      // get top of stack
 316       if ( UseSSE >= 1) {
 317         __ movflt(xmm0, Address(rdx, wordSize));
 318       } else {
 319         __ fld_s(Address(rdx, wordSize));                               // pushd float result
 320       }
 321       break;
 322     case T_DOUBLE  :
 323       __ movl(rdx, STATE(_stack));                                      // get top of stack
 324       if ( UseSSE > 1) {
 325         __ movdbl(xmm0, Address(rdx, wordSize));
 326       } else {
 327         __ fld_d(Address(rdx, wordSize));                               // push double result
 328       }
 329       break;
 330     case T_OBJECT :
 331       __ movl(rdx, STATE(_stack));                                      // get top of stack
 332       __ movl(rax, Address(rdx, wordSize));                             // get result word 1
 333       __ verify_oop(rax);                                               // verify it
 334       break;
 335     default       : ShouldNotReachHere();
 336   }
 337   __ ret(0);
 338   return entry;
 339 }
 340 
 341 address CppInterpreter::return_entry(TosState state, int length) {
 342   // make it look good in the debugger
 343   return CAST_FROM_FN_PTR(address, RecursiveInterpreterActivation);
 344 }
 345 
 346 address CppInterpreter::deopt_entry(TosState state, int length) {
 347   address ret = NULL;
 348   if (length != 0) {
 349     switch (state) {
 350       case atos: ret = deopt_frame_manager_return_atos; break;
 351       case btos: ret = deopt_frame_manager_return_btos; break;
 352       case ctos:


 394   // rsp == bottom of method's expression stack.
 395 
 396   const Address const_offset      (rbx, methodOopDesc::const_offset());
 397 
 398 
 399   // On entry sp is the sender's sp. This includes the space for the arguments
 400   // that the sender pushed. If the sender pushed no args (a static) and the
 401   // caller returns a long then we need two words on the sender's stack which
 402   // are not present (although when we return a restore full size stack the
 403   // space will be present). If we didn't allocate two words here then when
 404   // we "push" the result of the caller's stack we would overwrite the return
 405   // address and the saved rbp. Not good. So simply allocate 2 words now
 406   // just to be safe. This is the "static long no_params() method" issue.
 407   // See Lo.java for a testcase.
 408   // We don't need this for native calls because they return result in
 409   // register and the stack is expanded in the caller before we store
 410   // the results on the stack.
 411 
 412   if (!native) {
 413 #ifdef PRODUCT
 414     __ subl(rsp, 2*wordSize);
 415 #else /* PRODUCT */
 416     __ pushl((int)NULL);
 417     __ pushl(state);                         // make it look like a real argument
 418 #endif /* PRODUCT */
 419   }
 420 
 421   // Now that we are assure of space for stack result, setup typical linkage
 422 
 423   __ pushl(rax);
 424   __ enter();
 425 
 426   __ movl(rax, state);                                 // save current state
 427 
 428   __ leal(rsp, Address(rsp, -(int)sizeof(BytecodeInterpreter)));
 429   __ movl(state, rsp);
 430 
 431   // rsi == state/locals rax == prevstate 
 432 
 433   // initialize the "shadow" frame so that use since C++ interpreter not directly
 434   // recursive. Simpler to recurse but we can't trim expression stack as we call
 435   // new methods.
 436   __ movl(STATE(_locals), locals);                      // state->_locals = locals()
 437   __ movl(STATE(_self_link), state);                    // point to self
 438   __ movl(STATE(_prev_link), rax);                      // state->_link = state on entry (NULL or previous state)
 439   __ movl(STATE(_sender_sp), sender_sp);                // state->_sender_sp = sender_sp



 440   __ get_thread(rax);                                   // get vm's javathread*
 441   __ movl(STATE(_thread), rax);                         // state->_bcp = codes()
 442   __ movl(rdx, Address(rbx, methodOopDesc::const_offset())); // get constantMethodOop
 443   __ leal(rdx, Address(rdx, constMethodOopDesc::codes_offset())); // get code base

 444   if (native) {
 445     __ movl(STATE(_bcp), (intptr_t)NULL);               // state->_bcp = NULL
 446   } else {
 447     __ movl(STATE(_bcp), rdx);                          // state->_bcp = codes()
 448   }
 449   __ xorl(rdx, rdx);
 450   __ movl(STATE(_oop_temp), rdx);                       // state->_oop_temp = NULL (only really needed for native)
 451   __ movl(STATE(_mdx), rdx);                            // state->_mdx = NULL
 452   __ movl(rdx, Address(rbx, methodOopDesc::constants_offset()));
 453   __ movl(rdx, Address(rdx, constantPoolOopDesc::cache_offset_in_bytes()));
 454   __ movl(STATE(_constants), rdx);                      // state->_constants = constants()
 455 
 456   __ movl(STATE(_method), rbx);                         // state->_method = method()
 457   __ movl(STATE(_msg), (int) BytecodeInterpreter::method_entry);   // state->_msg = initial method entry
 458   __ movl(STATE(_result._to_call._callee), (int) NULL); // state->_result._to_call._callee_callee = NULL
 459 
 460 
 461   __ movl(STATE(_monitor_base), rsp);                   // set monitor block bottom (grows down) this would point to entry [0]
 462                                                         // entries run from -1..x where &monitor[x] == 
 463 
 464   { 
 465     // Must not attempt to lock method until we enter interpreter as gc won't be able to find the
 466     // initial frame. However we allocate a free monitor so we don't have to shuffle the expression stack
 467     // immediately.
 468 
 469     // synchronize method
 470     const Address access_flags      (rbx, methodOopDesc::access_flags_offset());
 471     const int entry_size            = frame::interpreter_frame_monitor_size() * wordSize;
 472     Label not_synced;
 473 
 474     __ movl(rax, access_flags);
 475     __ testl(rax, JVM_ACC_SYNCHRONIZED);
 476     __ jcc(Assembler::zero, not_synced);
 477 
 478     // Allocate initial monitor and pre initialize it
 479     // get synchronization object
 480 
 481     Label done;
 482     const int mirror_offset = klassOopDesc::klass_part_offset_in_bytes() + Klass::java_mirror_offset_in_bytes();
 483     __ movl(rax, access_flags);
 484     __ testl(rax, JVM_ACC_STATIC);
 485     __ movl(rax, Address(locals, 0));                     // get receiver (assume this is frequent case)
 486     __ jcc(Assembler::zero, done);
 487     __ movl(rax, Address(rbx, methodOopDesc::constants_offset()));
 488     __ movl(rax, Address(rax, constantPoolOopDesc::pool_holder_offset_in_bytes()));
 489     __ movl(rax, Address(rax, mirror_offset));
 490     __ bind(done);
 491     // add space for monitor & lock
 492     __ subl(rsp, entry_size);                                             // add space for a monitor entry
 493     __ movl(Address(rsp, BasicObjectLock::obj_offset_in_bytes()), rax);   // store object
 494     __ bind(not_synced);
 495   }
 496 
 497   __ movl(STATE(_stack_base), rsp);                                     // set expression stack base ( == &monitors[-count])
 498   if (native) {
 499     __ movl(STATE(_stack), rsp);                                          // set current expression stack tos
 500     __ movl(STATE(_stack_limit), rsp);
 501   } else {
 502     __ subl(rsp, wordSize);                                               // pre-push stack
 503     __ movl(STATE(_stack), rsp);                                          // set current expression stack tos
 504 
 505     // compute full expression stack limit
 506 
 507     const Address size_of_stack    (rbx, methodOopDesc::max_stack_offset());
 508     __ load_unsigned_word(rdx, size_of_stack);                            // get size of expression stack in words
 509     __ negl(rdx);                                                         // so we can subtract in next step
 510     // Allocate expression stack
 511     __ leal(rsp, Address(rsp, rdx, Address::times_4));
 512     __ movl(STATE(_stack_limit), rsp);
 513   }
 514 








 515 }
 516 
 517 // Helpers for commoning out cases in the various type of method entries.
 518 //
 519 
 520 // increment invocation count & check for overflow
 521 //
 522 // Note: checking for negative value instead of overflow
 523 //       so we have a 'sticky' overflow test
 524 //
 525 // rbx,: method
 526 // rcx: invocation counter
 527 //
 528 void InterpreterGenerator::generate_counter_incr(Label* overflow, Label* profile_method, Label* profile_method_continue) {
 529 
 530   const Address invocation_counter(rbx, methodOopDesc::invocation_counter_offset() + InvocationCounter::counter_offset());
 531   const Address backedge_counter  (rbx, methodOopDesc::backedge_counter_offset() + InvocationCounter::counter_offset());
 532 
 533   if (ProfileInterpreter) { // %%% Merge this into methodDataOop
 534     __ increment(Address(rbx,methodOopDesc::interpreter_invocation_counter_offset()));
 535   }
 536   // Update standard invocation counters
 537   __ movl(rax, backedge_counter);               // load backedge counter
 538 
 539   __ increment(rcx, InvocationCounter::count_increment);
 540   __ andl(rax, InvocationCounter::count_mask_value);  // mask out the status bits
 541 
 542   __ movl(invocation_counter, rcx);             // save invocation count
 543   __ addl(rcx, rax);                            // add both counters
 544 
 545   // profile_method is non-null only for interpreted method so
 546   // profile_method != NULL == !native_call
 547   // BytecodeInterpreter only calls for native so code is elided.
 548 
 549   __ cmp32(rcx,
 550            ExternalAddress((address)&InvocationCounter::InterpreterInvocationLimit));
 551   __ jcc(Assembler::aboveEqual, *overflow);
 552 
 553 }
 554 
 555 void InterpreterGenerator::generate_counter_overflow(Label* do_continue) {
 556 
 557   // C++ interpreter on entry
 558   // rsi - new interpreter state pointer
 559   // rbp - interpreter frame pointer
 560   // rbx - method
 561 
 562   // On return (i.e. jump to entry_point) [ back to invocation of interpreter ]
 563   // rbx, - method
 564   // rcx - rcvr (assuming there is one)
 565   // top of stack return address of interpreter caller
 566   // rsp - sender_sp
 567 
 568   // C++ interpreter only
 569   // rsi - previous interpreter state pointer
 570 
 571   const Address size_of_parameters(rbx, methodOopDesc::size_of_parameters_offset());
 572 
 573   // InterpreterRuntime::frequency_counter_overflow takes one argument
 574   // indicating if the counter overflow occurs at a backwards branch (non-NULL bcp).
 575   // The call returns the address of the verified entry point for the method or NULL
 576   // if the compilation did not complete (either went background or bailed out).
 577   __ movl(rax, (int)false);
 578   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::frequency_counter_overflow), rax);
 579 
 580   // for c++ interpreter can rsi really be munged? 
 581   __ leal(rsi, Address(rbp, -sizeof(BytecodeInterpreter)));                                // restore state
 582   __ movl(rbx, Address(rsi, byte_offset_of(BytecodeInterpreter, _method)));                // restore method
 583   __ movl(rdi, Address(rsi, byte_offset_of(BytecodeInterpreter, _locals)));                // get locals pointer
 584 
 585   // Preserve invariant that rsi/rdi contain bcp/locals of sender frame  
 586   // and jump to the interpreted entry. 
 587   __ jmp(*do_continue, relocInfo::none);
 588 
 589 }
 590 
 591 void InterpreterGenerator::generate_stack_overflow_check(void) {
 592   // see if we've got enough room on the stack for locals plus overhead.
 593   // the expression stack grows down incrementally, so the normal guard
 594   // page mechanism will work for that.
 595   //
 596   // Registers live on entry:
 597   //
 598   // Asm interpreter
 599   // rdx: number of additional locals this frame needs (what we must check)
 600   // rbx,: methodOop
 601 
 602   // C++ Interpreter
 603   // rsi: previous interpreter frame state object
 604   // rdi: &locals[0]
 605   // rcx: # of locals
 606   // rdx: number of additional locals this frame needs (what we must check)
 607   // rbx: methodOop
 608 
 609   // destroyed on exit
 610   // rax,
 611 
 612   // NOTE:  since the additional locals are also always pushed (wasn't obvious in
 613   // generate_method_entry) so the guard should work for them too. 
 614   //
 615 
 616   // monitor entry size: see picture of stack set (generate_method_entry) and frame_i486.hpp
 617   const int entry_size    = frame::interpreter_frame_monitor_size() * wordSize;
 618 
 619   // total overhead size: entry_size + (saved rbp, thru expr stack bottom).
 620   // be sure to change this if you add/subtract anything to/from the overhead area
 621   const int overhead_size = (int)sizeof(BytecodeInterpreter);
 622 
 623   const int page_size = os::vm_page_size();
 624 
 625   Label after_frame_check;
 626 
 627   // compute rsp as if this were going to be the last frame on
 628   // the stack before the red zone
 629 
 630   Label after_frame_check_pop;
 631 
 632   // save rsi == caller's bytecode ptr (c++ previous interp. state)
 633   // QQQ problem here?? rsi overload????
 634   __ pushl(rsi);
 635 
 636   const Register thread = rsi;
 637 
 638   __ get_thread(thread);
 639 
 640   const Address stack_base(thread, Thread::stack_base_offset());
 641   const Address stack_size(thread, Thread::stack_size_offset());
 642 
 643   // locals + overhead, in bytes
 644     const Address size_of_stack    (rbx, methodOopDesc::max_stack_offset());
 645     // Always give one monitor to allow us to start interp if sync method.
 646     // Any additional monitors need a check when moving the expression stack
 647     const one_monitor = frame::interpreter_frame_monitor_size() * wordSize;
 648   __ load_unsigned_word(rax, size_of_stack);                            // get size of expression stack in words
 649   __ leal(rax, Address(noreg, rax, Interpreter::stackElementScale(), one_monitor));
 650   __ leal(rax, Address(rax, rdx, Interpreter::stackElementScale(), overhead_size));
 651 
 652 #ifdef ASSERT
 653   Label stack_base_okay, stack_size_okay;
 654   // verify that thread stack base is non-zero
 655   __ cmpl(stack_base, 0);
 656   __ jcc(Assembler::notEqual, stack_base_okay);
 657   __ stop("stack base is zero");
 658   __ bind(stack_base_okay);
 659   // verify that thread stack size is non-zero
 660   __ cmpl(stack_size, 0);
 661   __ jcc(Assembler::notEqual, stack_size_okay);
 662   __ stop("stack size is zero");
 663   __ bind(stack_size_okay);
 664 #endif
 665 
 666   // Add stack base to locals and subtract stack size
 667   __ addl(rax, stack_base);
 668   __ subl(rax, stack_size);
 669 
 670   // We should have a magic number here for the size of the c++ interpreter frame.
 671   // We can't actually tell this ahead of time. The debug version size is around 3k
 672   // product is 1k and fastdebug is 4k
 673   const int slop = 6 * K;
 674 
 675   // Use the maximum number of pages we might bang.
 676   const int max_pages = StackShadowPages > (StackRedPages+StackYellowPages) ? StackShadowPages :
 677                                                                               (StackRedPages+StackYellowPages);
 678   // Only need this if we are stack banging which is temporary while
 679   // we're debugging.
 680   __ addl(rax, slop + 2*max_pages * page_size);
 681 
 682   // check against the current stack bottom
 683   __ cmpl(rsp, rax);
 684   __ jcc(Assembler::above, after_frame_check_pop);
 685 
 686   __ popl(rsi);  // get saved bcp / (c++ prev state ).
 687 
 688      // throw exception return address becomes throwing pc
 689   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_StackOverflowError));
 690 
 691   // all done with frame size check
 692   __ bind(after_frame_check_pop);
 693   __ popl(rsi);
 694 
 695   __ bind(after_frame_check);
 696 }
 697 
 698 // Find preallocated  monitor and lock method (C++ interpreter)
 699 // rbx - methodOop
 700 // 
 701 void InterpreterGenerator::lock_method(void) {
 702   // assumes state == rsi == pointer to current interpreterState
 703   // minimally destroys rax, rdx, rdi
 704   //
 705   // synchronize method
 706   const Register state = rsi;
 707   const int entry_size            = frame::interpreter_frame_monitor_size() * wordSize;
 708   const Address access_flags      (rbx, methodOopDesc::access_flags_offset());
 709 


 710   // find initial monitor i.e. monitors[-1]
 711   __ movl(rdx, STATE(_monitor_base));                                   // get monitor bottom limit
 712   __ subl(rdx, entry_size);                                             // point to initial monitor
 713 
 714 #ifdef ASSERT
 715   { Label L;
 716     __ movl(rax, access_flags);
 717     __ testl(rax, JVM_ACC_SYNCHRONIZED);
 718     __ jcc(Assembler::notZero, L);
 719     __ stop("method doesn't need synchronization");
 720     __ bind(L);
 721   }
 722 #endif // ASSERT
 723   // get synchronization object
 724   { Label done;
 725     const int mirror_offset = klassOopDesc::klass_part_offset_in_bytes() + Klass::java_mirror_offset_in_bytes();
 726     __ movl(rax, access_flags);
 727     __ movl(rdi, STATE(_locals));                                       // prepare to get receiver (assume common case)
 728     __ testl(rax, JVM_ACC_STATIC);
 729     __ movl(rax, Address(rdi, 0));                                      // get receiver (assume this is frequent case)
 730     __ jcc(Assembler::zero, done);
 731     __ movl(rax, Address(rbx, methodOopDesc::constants_offset()));
 732     __ movl(rax, Address(rax, constantPoolOopDesc::pool_holder_offset_in_bytes()));
 733     __ movl(rax, Address(rax, mirror_offset));
 734     __ bind(done);
 735   }
 736 #ifdef ASSERT
 737   { Label L;
 738     __ cmpl(rax, Address(rdx, BasicObjectLock::obj_offset_in_bytes()));   // correct object?
 739     __ jcc(Assembler::equal, L);
 740     __ stop("wrong synchronization lobject");
 741     __ bind(L);
 742   }
 743 #endif // ASSERT
 744   // can destroy rax, rdx, rcx, and (via call_VM) rdi!
 745   __ lock_object(rdx);          
 746 }
 747 
 748 // Call an accessor method (assuming it is resolved, otherwise drop into vanilla (slow path) entry
 749 
 750 address InterpreterGenerator::generate_accessor_entry(void) {
 751 
 752   // rbx,: methodOop
 753   // rcx: receiver (preserve for slow entry into asm interpreter)
 754 
 755   // rsi: senderSP must preserved for slow path, set SP to it on fast path
 756 
 757   Label xreturn_path;
 758 
 759   // do fastpath for resolved accessor methods
 760   if (UseFastAccessorMethods) {
 761 
 762     address entry_point = __ pc();
 763 
 764     Label slow_path;
 765     // If we need a safepoint check, generate full interpreter entry.
 766     ExternalAddress state(SafepointSynchronize::address_of_state());
 767     __ cmp32(ExternalAddress(SafepointSynchronize::address_of_state()),
 768              SafepointSynchronize::_not_synchronized);
 769 
 770     __ jcc(Assembler::notEqual, slow_path);
 771     // ASM/C++ Interpreter
 772     // Code: _aload_0, _(i|a)getfield, _(i|a)return or any rewrites thereof; parameter size = 1
 773     // Note: We can only use this code if the getfield has been resolved
 774     //       and if we don't have a null-pointer exception => check for
 775     //       these conditions first and use slow path if necessary.
 776     // rbx,: method
 777     // rcx: receiver
 778     __ movl(rax, Address(rsp, wordSize));
 779 
 780     // check if local 0 != NULL and read field
 781     __ testl(rax, rax);
 782     __ jcc(Assembler::zero, slow_path);
 783 
 784     __ movl(rdi, Address(rbx, methodOopDesc::constants_offset()));
 785     // read first instruction word and extract bytecode @ 1 and index @ 2
 786     __ movl(rdx, Address(rbx, methodOopDesc::const_offset()));
 787     __ movl(rdx, Address(rdx, constMethodOopDesc::codes_offset()));
 788     // Shift codes right to get the index on the right.
 789     // The bytecode fetched looks like <index><0xb4><0x2a>
 790     __ shrl(rdx, 2*BitsPerByte);
 791     __ shll(rdx, exact_log2(in_words(ConstantPoolCacheEntry::size())));
 792     __ movl(rdi, Address(rdi, constantPoolOopDesc::cache_offset_in_bytes()));
 793 
 794     // rax,: local 0
 795     // rbx,: method
 796     // rcx: receiver - do not destroy since it is needed for slow path!
 797     // rcx: scratch
 798     // rdx: constant pool cache index
 799     // rdi: constant pool cache
 800     // rsi: sender sp
 801 
 802     // check if getfield has been resolved and read constant pool cache entry
 803     // check the validity of the cache entry by testing whether _indices field
 804     // contains Bytecode::_getfield in b1 byte.
 805     assert(in_words(ConstantPoolCacheEntry::size()) == 4, "adjust shift below");
 806     __ movl(rcx, 
 807             Address(rdi, 
 808                     rdx, 
 809                     Address::times_4, constantPoolCacheOopDesc::base_offset() + ConstantPoolCacheEntry::indices_offset()));
 810     __ shrl(rcx, 2*BitsPerByte);
 811     __ andl(rcx, 0xFF);
 812     __ cmpl(rcx, Bytecodes::_getfield);
 813     __ jcc(Assembler::notEqual, slow_path);
 814 
 815     // Note: constant pool entry is not valid before bytecode is resolved
 816     __ movl(rcx, 
 817             Address(rdi, 
 818                     rdx, 
 819                     Address::times_4, constantPoolCacheOopDesc::base_offset() + ConstantPoolCacheEntry::f2_offset()));
 820     __ movl(rdx, 
 821             Address(rdi, 
 822                     rdx, 
 823                     Address::times_4, constantPoolCacheOopDesc::base_offset() + ConstantPoolCacheEntry::flags_offset()));
 824 
 825     Label notByte, notShort, notChar;
 826     const Address field_address (rax, rcx, Address::times_1);
 827 
 828     // Need to differentiate between igetfield, agetfield, bgetfield etc.
 829     // because they are different sizes.
 830     // Use the type from the constant pool cache
 831     __ shrl(rdx, ConstantPoolCacheEntry::tosBits);
 832     // Make sure we don't need to mask rdx for tosBits after the above shift
 833     ConstantPoolCacheEntry::verify_tosBits();










 834     __ cmpl(rdx, btos);
 835     __ jcc(Assembler::notEqual, notByte);
 836     __ load_signed_byte(rax, field_address);
 837     __ jmp(xreturn_path);
 838 
 839     __ bind(notByte);
 840     __ cmpl(rdx, stos);
 841     __ jcc(Assembler::notEqual, notShort);
 842     __ load_signed_word(rax, field_address);
 843     __ jmp(xreturn_path);
 844 
 845     __ bind(notShort);
 846     __ cmpl(rdx, ctos);
 847     __ jcc(Assembler::notEqual, notChar);
 848     __ load_unsigned_word(rax, field_address);
 849     __ jmp(xreturn_path);
 850 
 851     __ bind(notChar);
 852 #ifdef ASSERT
 853     Label okay;

 854     __ cmpl(rdx, atos);
 855     __ jcc(Assembler::equal, okay);

 856     __ cmpl(rdx, itos);
 857     __ jcc(Assembler::equal, okay);
 858     __ stop("what type is this?");
 859     __ bind(okay);
 860 #endif // ASSERT
 861     // All the rest are a 32 bit wordsize
 862     __ movl(rax, field_address);
 863 
 864     __ bind(xreturn_path);
 865 
 866     // _ireturn/_areturn
 867     __ popl(rdi);                              // get return address
 868     __ movl(rsp, rsi);                         // set sp to sender sp
 869     __ jmp(rdi);
 870 
 871     // generate a vanilla interpreter entry as the slow path
 872     __ bind(slow_path);
 873     // We will enter c++ interpreter looking like it was
 874     // called by the call_stub this will cause it to return
 875     // a tosca result to the invoker which might have been
 876     // the c++ interpreter itself.
 877 
 878     __ jmp(fast_accessor_slow_entry_path);
 879     return entry_point;
 880 
 881   } else {
 882     return NULL;
 883   }
 884 
 885 }
 886 
 887 //
 888 // C++ Interpreter stub for calling a native method.
 889 // This sets up a somewhat different looking stack for calling the native method
 890 // than the typical interpreter frame setup but still has the pointer to
 891 // an interpreter state.
 892 //
 893 
 894 address InterpreterGenerator::generate_native_entry(bool synchronized) {
 895   // determine code generation flags
 896   bool inc_counter  = UseCompiler || CountCompiledCalls;
 897 
 898   // rbx: methodOop
 899   // rcx: receiver (unused)
 900   // rsi: previous interpreter state (if called from C++ interpreter) must preserve
 901   //      in any case. If called via c1/c2/call_stub rsi is junk (to use) but harmless
 902   //      to save/restore.
 903   address entry_point = __ pc();
 904 
 905   const Address size_of_parameters(rbx, methodOopDesc::size_of_parameters_offset());
 906   const Address size_of_locals    (rbx, methodOopDesc::size_of_locals_offset());
 907   const Address invocation_counter(rbx, methodOopDesc::invocation_counter_offset() + InvocationCounter::counter_offset());
 908   const Address access_flags      (rbx, methodOopDesc::access_flags_offset());
 909 
 910   // rsi == state/locals rdi == prevstate 
 911   const Register state = rsi;
 912   const Register locals = rdi;
 913 
 914   // get parameter size (always needed)
 915   __ load_unsigned_word(rcx, size_of_parameters);
 916 
 917   // rbx: methodOop
 918   // rcx: size of parameters
 919   __ popl(rax);                                       // get return address
 920   // for natives the size of locals is zero
 921 
 922   // compute beginning of parameters /locals 
 923   __ leal(locals, Address(rsp, rcx, Address::times_4, -wordSize));
 924 
 925   // initialize fixed part of activation frame
 926 
 927   // Assumes rax = return address
 928 
 929   // allocate and initialize new interpreterState and method expression stack
 930   // IN(locals) ->  locals 
 931   // IN(state) -> previous frame manager state (NULL from stub/c1/c2)
 932   // destroys rax, rcx, rdx
 933   // OUT (state) -> new interpreterState
 934   // OUT(rsp) -> bottom of methods expression stack
 935 
 936   // save sender_sp
 937   __ movl(rcx, rsi); 
 938   // start with NULL previous state
 939   __ movl(state, 0);
 940   generate_compute_interpreter_state(state, locals, rcx, true);
 941 
 942 #ifdef ASSERT
 943   { Label L;
 944     __ movl(rax, STATE(_stack_base));
 945     __ cmpl(rax, rsp);





 946     __ jcc(Assembler::equal, L);
 947     __ stop("broken stack frame setup in interpreter");
 948     __ bind(L);
 949   }
 950 #endif
 951 
 952   if (inc_counter) __ movl(rcx, invocation_counter);  // (pre-)fetch invocation count
 953 
 954   __ movl(rax, STATE(_thread));                       // get thread

 955   // Since at this point in the method invocation the exception handler
 956   // would try to exit the monitor of synchronized methods which hasn't
 957   // been entered yet, we set the thread local variable
 958   // _do_not_unlock_if_synchronized to true. The remove_activation will
 959   // check this flag.
 960 
 961   const Address do_not_unlock_if_synchronized(rax,
 962         in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
 963   __ movbool(do_not_unlock_if_synchronized, true);
 964 
 965   // make sure method is native & not abstract
 966 #ifdef ASSERT
 967   __ movl(rax, access_flags);
 968   {
 969     Label L;
 970     __ testl(rax, JVM_ACC_NATIVE);
 971     __ jcc(Assembler::notZero, L);
 972     __ stop("tried to execute non-native method as native");
 973     __ bind(L);
 974   }
 975   { Label L;
 976     __ testl(rax, JVM_ACC_ABSTRACT);
 977     __ jcc(Assembler::zero, L);
 978     __ stop("tried to execute abstract method in interpreter");
 979     __ bind(L);
 980   }
 981 #endif
 982 
 983 
 984   // increment invocation count & check for overflow
 985   Label invocation_counter_overflow;
 986   if (inc_counter) {
 987     generate_counter_incr(&invocation_counter_overflow, NULL, NULL);
 988   }
 989 
 990   Label continue_after_compile;
 991 
 992   __ bind(continue_after_compile);
 993 
 994   bang_stack_shadow_pages(true);
 995 
 996   // reset the _do_not_unlock_if_synchronized flag
 997   __ movl(rax, STATE(_thread));                       // get thread
 998   __ movbool(do_not_unlock_if_synchronized, false);
 999 
1000 
1001   // check for synchronized native methods
1002   //
1003   // Note: This must happen *after* invocation counter check, since
1004   //       when overflow happens, the method should not be locked.
1005   if (synchronized) {
1006     // potentially kills rax, rcx, rdx, rdi
1007     lock_method();
1008   } else {
1009     // no synchronization necessary
1010 #ifdef ASSERT
1011       { Label L;
1012         __ movl(rax, access_flags);
1013         __ testl(rax, JVM_ACC_SYNCHRONIZED);
1014         __ jcc(Assembler::zero, L);
1015         __ stop("method needs synchronization");
1016         __ bind(L);
1017       }
1018 #endif
1019   }
1020 
1021   // start execution
1022 
1023   // jvmti support
1024   __ notify_method_entry();
1025 
1026   // work registers
1027   const Register method = rbx;
1028   const Register thread = rdi;
1029   const Register t      = rcx;    
1030 
1031   // allocate space for parameters
1032   __ movl(method, STATE(_method));
1033   __ verify_oop(method);
1034   __ load_unsigned_word(t, Address(method, methodOopDesc::size_of_parameters_offset()));
1035   __ shll(t, 2);
1036   __ addl(t, 2*wordSize);     // allocate two more slots for JNIEnv and possible mirror
1037   __ subl(rsp, t);
1038   __ andl(rsp, -(StackAlignmentInBytes)); // gcc needs 16 byte aligned stacks to do XMM intrinsics






1039 
1040   // get signature handler
1041     Label pending_exception_present;
1042 
1043   { Label L;
1044     __ movl(t, Address(method, methodOopDesc::signature_handler_offset()));
1045     __ testl(t, t);
1046     __ jcc(Assembler::notZero, L);
1047     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::prepare_native_call), method, false);
1048     __ movl(method, STATE(_method));
1049     __ cmpl(Address(thread, Thread::pending_exception_offset()), NULL_WORD);
1050     __ jcc(Assembler::notEqual, pending_exception_present);
1051     __ verify_oop(method);
1052     __ movl(t, Address(method, methodOopDesc::signature_handler_offset()));
1053     __ bind(L);
1054   }
1055 #ifdef ASSERT
1056   {
1057     Label L;
1058     __ pushl(t);
1059     __ get_thread(t);                                   // get vm's javathread*
1060     __ cmpl(t, STATE(_thread));
1061     __ jcc(Assembler::equal, L);
1062     __ int3();
1063     __ bind(L);
1064     __ popl(t);
1065   }
1066 #endif // 
1067 

1068   // call signature handler
1069   assert(InterpreterRuntime::SignatureHandlerGenerator::from() == rdi, "adjust this code");
1070   assert(InterpreterRuntime::SignatureHandlerGenerator::to  () == rsp, "adjust this code");
1071   assert(InterpreterRuntime::SignatureHandlerGenerator::temp() == t  , "adjust this code");
1072   // The generated handlers do not touch RBX (the method oop).
1073   // However, large signatures cannot be cached and are generated 
1074   // each time here.  The slow-path generator will blow RBX
1075   // sometime, so we must reload it after the call.
1076   __ movl(rdi, STATE(_locals));  // get the from pointer
1077   __ call(t);
1078   __ movl(method, STATE(_method));
1079   __ verify_oop(method);
1080 
1081   // result handler is in rax
1082   // set result handler
1083   __ movl(STATE(_result_handler), rax);













1084 
1085   // pass mirror handle if static call
1086   { Label L;
1087     const int mirror_offset = klassOopDesc::klass_part_offset_in_bytes() + Klass::java_mirror_offset_in_bytes();
1088     __ movl(t, Address(method, methodOopDesc::access_flags_offset()));
1089     __ testl(t, JVM_ACC_STATIC);
1090     __ jcc(Assembler::zero, L);
1091     // get mirror
1092     __ movl(t, Address(method, methodOopDesc:: constants_offset()));
1093     __ movl(t, Address(t, constantPoolOopDesc::pool_holder_offset_in_bytes()));
1094     __ movl(t, Address(t, mirror_offset));
1095     // copy mirror into activation object
1096     __ movl(STATE(_oop_temp), t);
1097     // pass handle to mirror
1098     __ leal(t, STATE(_oop_temp));
1099     __ movl(Address(rsp, wordSize), t);




1100     __ bind(L);
1101   }
1102 #ifdef ASSERT
1103   {
1104     Label L;
1105     __ pushl(t);
1106     __ get_thread(t);                                   // get vm's javathread*
1107     __ cmpl(t, STATE(_thread));
1108     __ jcc(Assembler::equal, L);
1109     __ int3();
1110     __ bind(L);
1111     __ popl(t);
1112   }
1113 #endif // 
1114 
1115   // get native function entry point
1116   { Label L;
1117     __ movl(rax, Address(method, methodOopDesc::native_function_offset()));
1118     __ testl(rax, rax);
1119     __ jcc(Assembler::notZero, L);
1120     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::prepare_native_call), method);
1121     __ movl(method, STATE(_method));
1122     __ verify_oop(method);
1123     __ movl(rax, Address(method, methodOopDesc::native_function_offset()));
1124     __ bind(L);
1125   }
1126 
1127   // pass JNIEnv
1128   __ movl(thread, STATE(_thread));                       // get thread
1129   __ leal(t, Address(thread, JavaThread::jni_environment_offset()));
1130   __ movl(Address(rsp, 0), t);






1131 #ifdef ASSERT
1132   {
1133     Label L;
1134     __ pushl(t);
1135     __ get_thread(t);                                   // get vm's javathread*
1136     __ cmpl(t, STATE(_thread));
1137     __ jcc(Assembler::equal, L);
1138     __ int3();
1139     __ bind(L);
1140     __ popl(t);
1141   }
1142 #endif // 
1143 
1144 #ifdef ASSERT
1145   { Label L;
1146     __ movl(t, Address(thread, JavaThread::thread_state_offset()));
1147     __ cmpl(t, _thread_in_Java);
1148     __ jcc(Assembler::equal, L);
1149     __ stop("Wrong thread state in native stub");
1150     __ bind(L);
1151   }
1152 #endif
1153 
1154   // Change state to native (we save the return address in the thread, since it might not
1155   // be pushed on the stack when we do a a stack traversal). It is enough that the pc()
1156   // points into the right code segment. It does not have to be the correct return pc.
1157 
1158   __ set_last_Java_frame(thread, noreg, rbp, __ pc());
1159 
1160   __ movl(Address(thread, JavaThread::thread_state_offset()), _thread_in_native);    
1161 
1162   __ call(rax);
1163 
1164   // result potentially in rdx:rax or ST0
1165   __ movl(method, STATE(_method));
1166   __ movl(thread, STATE(_thread));                       // get thread
1167 
1168   // The potential result is in ST(0) & rdx:rax
1169   // With C++ interpreter we leave any possible result in ST(0) until we are in result handler and then
1170   // we do the appropriate stuff for returning the result. rdx:rax must always be saved because just about
1171   // anything we do here will destroy it, st(0) is only saved if we re-enter the vm where it would
1172   // be destroyed.
1173   // It is safe to do these pushes because state is _thread_in_native and return address will be found
1174   // via _last_native_pc and not via _last_jave_sp
1175 
1176     // Must save the value of ST(0) since it could be destroyed before we get to result handler
1177     { Label Lpush, Lskip;
1178       ExternalAddress float_handler(AbstractInterpreter::result_handler(T_FLOAT));
1179       ExternalAddress double_handler(AbstractInterpreter::result_handler(T_DOUBLE));
1180       __ cmpptr(STATE(_result_handler), float_handler.addr());
1181       __ jcc(Assembler::equal, Lpush);
1182       __ cmpptr(STATE(_result_handler), double_handler.addr());
1183       __ jcc(Assembler::notEqual, Lskip);
1184       __ bind(Lpush);
1185       __ push(dtos);      





1186       __ bind(Lskip);
1187     }
1188 
1189   __ push(ltos);           // save rax:rdx for potential use by result handler.




1190 
1191   // Either restore the MXCSR register after returning from the JNI Call
1192   // or verify that it wasn't changed.
1193   if (VM_Version::supports_sse()) {
1194     if (RestoreMXCSROnJNICalls) {
1195       __ ldmxcsr(ExternalAddress(StubRoutines::addr_mxcsr_std()));
1196     }
1197     else if (CheckJNICalls ) {
1198       __ call(RuntimeAddress(StubRoutines::i486::verify_mxcsr_entry()));
1199     }
1200   }
1201 

1202   // Either restore the x87 floating pointer control word after returning
1203   // from the JNI call or verify that it wasn't changed.
1204   if (CheckJNICalls) {
1205     __ call(RuntimeAddress(StubRoutines::i486::verify_fpu_cntrl_wrd_entry()));
1206   }

1207 
1208 
1209   // change thread state
1210   __ movl(Address(thread, JavaThread::thread_state_offset()), _thread_in_native_trans);
1211   if(os::is_MP()) { 
1212     // Write serialization page so VM thread can do a pseudo remote membar.
1213     // We use the current thread pointer to calculate a thread specific
1214     // offset to write to within the page. This minimizes bus traffic
1215     // due to cache line collision.
1216     __ serialize_memory(thread, rcx);
1217   }
1218 
1219   // check for safepoint operation in progress and/or pending suspend requests
1220   { Label Continue;
1221 
1222     __ cmp32(ExternalAddress(SafepointSynchronize::address_of_state()),
1223              SafepointSynchronize::_not_synchronized);
1224 
1225     // threads running native code and they are expected to self-suspend
1226     // when leaving the _thread_in_native state. We need to check for 
1227     // pending suspend requests here.
1228     Label L;
1229     __ jcc(Assembler::notEqual, L);
1230     __ cmpl(Address(thread, JavaThread::suspend_flags_offset()), 0);
1231     __ jcc(Assembler::equal, Continue);
1232     __ bind(L);
1233 
1234     // Don't use call_VM as it will see a possible pending exception and forward it
1235     // and never return here preventing us from clearing _last_native_pc down below.
1236     // Also can't use call_VM_leaf either as it will check to see if rsi & rdi are
1237     // preserved and correspond to the bcp/locals pointers. So we do a runtime call
1238     // by hand.
1239     //
1240     __ pushl(thread);
1241     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address,
1242                                             JavaThread::check_special_condition_for_native_trans)));
1243     __ increment(rsp, wordSize);
1244 
1245     __ movl(method, STATE(_method));
1246     __ verify_oop(method);
1247     __ movl(thread, STATE(_thread));                       // get thread
1248 
1249     __ bind(Continue);
1250   }
1251 
1252   // change thread state
1253   __ movl(Address(thread, JavaThread::thread_state_offset()), _thread_in_Java);
1254 
1255   __ reset_last_Java_frame(thread, true, true);
1256 
1257   // reset handle block
1258   __ movl(t, Address(thread, JavaThread::active_handles_offset()));
1259   __ movl(Address(t, JNIHandleBlock::top_offset_in_bytes()), NULL_WORD);
1260 
1261   // If result was an oop then unbox and save it in the frame
1262   { Label L;
1263     Label no_oop, store_result;
1264       ExternalAddress oop_handler(AbstractInterpreter::result_handler(T_OBJECT));
1265     __ cmpptr(STATE(_result_handler), oop_handler.addr());
1266     __ jcc(Assembler::notEqual, no_oop);
1267     __ pop(ltos);
1268     __ testl(rax, rax);



1269     __ jcc(Assembler::zero, store_result);
1270     // unbox
1271     __ movl(rax, Address(rax, 0));
1272     __ bind(store_result);
1273     __ movl(STATE(_oop_temp), rax);
1274     // keep stack depth as expected by pushing oop which will eventually be discarded
1275     __ push(ltos);



1276     __ bind(no_oop);
1277   }
1278 
1279   {
1280      Label no_reguard;
1281      __ cmpl(Address(thread, JavaThread::stack_guard_state_offset()), JavaThread::stack_guard_yellow_disabled);
1282      __ jcc(Assembler::notEqual, no_reguard);
1283 
1284      __ pushad();
1285      __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages)));
1286      __ popad();
1287 
1288      __ bind(no_reguard);
1289    }
1290 
1291 
1292   // QQQ Seems like for native methods we simply return and the caller will see the pending
1293   // exception and do the right thing. Certainly the interpreter will, don't know about
1294   // compiled methods.
1295   // Seems that the answer to above is no this is wrong. The old code would see the exception
1296   // and forward it before doing the unlocking and notifying jvmdi that method has exited.
1297   // This seems wrong need to investigate the spec.
1298   
1299   // handle exceptions (exception handling will handle unlocking!)
1300   { Label L;
1301     __ cmpl(Address(thread, Thread::pending_exception_offset()), NULL_WORD);
1302     __ jcc(Assembler::zero, L);
1303     __ bind(pending_exception_present);
1304 
1305     // There are potential results on the stack (rax/rdx, ST(0)) we ignore these and simply
1306     // return and let caller deal with exception. This skips the unlocking here which 
1307     // seems wrong but seems to be what asm interpreter did. Can't find this in the spec.
1308     // Note: must preverve method in rbx
1309     //
1310 
1311     // remove activation
1312 
1313     __ movl(t, STATE(_sender_sp));                
1314     __ leave();                                  // remove frame anchor
1315     __ popl(rdi);                                // get return address
1316     __ movl(state, STATE(_prev_link));           // get previous state for return
1317     __ movl(rsp, t);                             // set sp to sender sp
1318     __ pushl(rdi);                               // [ush throwing pc
1319     // The skips unlocking!! This seems to be what asm interpreter does but seems
1320     // very wrong. Not clear if this violates the spec.
1321     __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
1322     __ bind(L);
1323   }
1324 
1325   // do unlocking if necessary
1326   { Label L;
1327     __ movl(t, Address(method, methodOopDesc::access_flags_offset()));
1328     __ testl(t, JVM_ACC_SYNCHRONIZED);
1329     __ jcc(Assembler::zero, L);
1330     // the code below should be shared with interpreter macro assembler implementation
1331     { Label unlock;

1332       // BasicObjectLock will be first in list, since this is a synchronized method. However, need
1333       // to check that the object has not been unlocked by an explicit monitorexit bytecode.        
1334       __ movl(rdx, STATE(_monitor_base));
1335       __ subl(rdx, frame::interpreter_frame_monitor_size() * wordSize);  // address of initial monitor
1336 
1337       __ movl(t, Address(rdx, BasicObjectLock::obj_offset_in_bytes()));
1338       __ testl(t, t);
1339       __ jcc(Assembler::notZero, unlock);
1340                                 
1341       // Entry already unlocked, need to throw exception
1342       __ MacroAssembler::call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_illegal_monitor_state_exception));
1343       __ should_not_reach_here();
1344   
1345       __ bind(unlock);        
1346       __ unlock_object(rdx);             
1347       // unlock can blow rbx so restore it for path that needs it below
1348       __ movl(method, STATE(_method));
1349     }
1350     __ bind(L);
1351   }    
1352 
1353   // jvmti support
1354   // Note: This must happen _after_ handling/throwing any exceptions since
1355   //       the exception handler code notifies the runtime of method exits
1356   //       too. If this happens before, method entry/exit notifications are
1357   //       not properly paired (was bug - gri 11/22/99).
1358   __ notify_method_exit(vtos, InterpreterMacroAssembler::NotifyJVMTI);
1359 
1360   // restore potential result in rdx:rax, call result handler to restore potential result in ST0 & handle result
1361   __ pop(ltos);                               // restore rax/rdx floating result if present still on stack
1362   __ movl(t, STATE(_result_handler));         // get result handler



1363   __ call(t);                                 // call result handler to convert to tosca form
1364 
1365   // remove activation
1366 
1367   __ movl(t, STATE(_sender_sp));                
1368 
1369   __ leave();                                  // remove frame anchor
1370   __ popl(rdi);                                // get return address
1371   __ movl(state, STATE(_prev_link));           // get previous state for return (if c++ interpreter was caller)
1372   __ movl(rsp, t);                             // set sp to sender sp
1373   __ jmp(rdi);
1374 
1375   // invocation counter overflow
1376   if (inc_counter) {
1377     // Handle overflow of counter and compile method
1378     __ bind(invocation_counter_overflow);
1379     generate_counter_overflow(&continue_after_compile);
1380   }
1381 
1382   return entry_point;
1383 }
1384 
1385 // Generate entries that will put a result type index into rcx
1386 void CppInterpreterGenerator::generate_deopt_handling() {
1387 
1388   const Register state = rsi;
1389   Label return_from_deopt_common;
1390 
1391   // Generate entries that will put a result type index into rcx
1392   // deopt needs to jump to here to enter the interpreter (return a result)
1393   deopt_frame_manager_return_atos  = __ pc();
1394 
1395   // rax is live here
1396   __ movl(rcx, AbstractInterpreter::BasicType_as_index(T_OBJECT));    // Result stub address array index
1397   __ jmp(return_from_deopt_common);
1398 
1399 
1400   // deopt needs to jump to here to enter the interpreter (return a result)
1401   deopt_frame_manager_return_btos  = __ pc();
1402 
1403   // rax is live here
1404   __ movl(rcx, AbstractInterpreter::BasicType_as_index(T_BOOLEAN));    // Result stub address array index
1405   __ jmp(return_from_deopt_common);
1406 
1407   // deopt needs to jump to here to enter the interpreter (return a result)
1408   deopt_frame_manager_return_itos  = __ pc();


1435   // deopt needs to jump to here to enter the interpreter (return a result)
1436   deopt_frame_manager_return_vtos  = __ pc();
1437 
1438   __ movl(rcx, AbstractInterpreter::BasicType_as_index(T_VOID));
1439 
1440   // Deopt return common 
1441   // an index is present in rcx that lets us move any possible result being
1442   // return to the interpreter's stack
1443   //
1444   // Because we have a full sized interpreter frame on the youngest
1445   // activation the stack is pushed too deep to share the tosca to
1446   // stack converters directly. We shrink the stack to the desired
1447   // amount and then push result and then re-extend the stack.
1448   // We could have the code in size_activation layout a short
1449   // frame for the top activation but that would look different
1450   // than say sparc (which needs a full size activation because
1451   // the windows are in the way. Really it could be short? QQQ
1452   //
1453   __ bind(return_from_deopt_common);
1454 
1455   __ leal(state, Address(rbp, -(int)sizeof(BytecodeInterpreter)));
1456 
1457   // setup rsp so we can push the "result" as needed.
1458   __ movl(rsp, STATE(_stack));                                     // trim stack (is prepushed)
1459   __ addl(rsp, wordSize);                                          // undo prepush
1460 
1461   ExternalAddress tosca_to_stack((address)CppInterpreter::_tosca_to_stack);
1462   // Address index(noreg, rcx, Address::times_4);
1463   __ movptr(rcx, ArrayAddress(tosca_to_stack, Address(noreg, rcx, Address::times_4)));
1464   // __ movl(rcx, Address(noreg, rcx, Address::times_4, int(AbstractInterpreter::_tosca_to_stack)));
1465   __ call(rcx);                                                   // call result converter
1466 
1467   __ movl(STATE(_msg), (int)BytecodeInterpreter::deopt_resume);
1468   __ leal(rsp, Address(rsp, -wordSize));                           // prepush stack (result if any already present)
1469   __ movl(STATE(_stack), rsp);                                     // inform interpreter of new stack depth (parameters removed,
1470                                                                    // result if any on stack already )
1471   __ movl(rsp, STATE(_stack_limit));                               // restore expression stack to full depth
1472 }
1473 
1474 // Generate the code to handle a more_monitors message from the c++ interpreter
1475 void CppInterpreterGenerator::generate_more_monitors() {
1476 
1477   const Register state = rsi;
1478 
1479   Label entry, loop;
1480   const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
1481   // 1. compute new pointers                   // rsp: old expression stack top
1482   __ movl(rdx, STATE(_stack_base));            // rdx: old expression stack bottom
1483   __ subl(rsp, entry_size);                    // move expression stack top limit
1484   __ subl(STATE(_stack), entry_size);          // update interpreter stack top
1485   __ movl(STATE(_stack_limit), rsp);           // inform interpreter
1486   __ subl(rdx, entry_size);                    // move expression stack bottom
1487   __ movl(STATE(_stack_base), rdx);            // inform interpreter
1488   __ movl(rcx, STATE(_stack));                 // set start value for copy loop
1489   __ jmp(entry);
1490   // 2. move expression stack contents
1491   __ bind(loop);
1492   __ movl(rbx, Address(rcx, entry_size));      // load expression stack word from old location
1493   __ movl(Address(rcx, 0), rbx);               // and store it at new location
1494   __ addl(rcx, wordSize);                      // advance to next word
1495   __ bind(entry);
1496   __ cmpl(rcx, rdx);                           // check if bottom reached
1497   __ jcc(Assembler::notEqual, loop);           // if not at bottom then copy next word
1498   // now zero the slot so we can find it.
1499   __ movl(Address(rdx, BasicObjectLock::obj_offset_in_bytes()), (int) NULL);
1500   __ movl(STATE(_msg), (int)BytecodeInterpreter::got_monitors);
1501 }
1502 
1503 
1504 // Initial entry to C++ interpreter from the call_stub.
1505 // This entry point is called the frame manager since it handles the generation
1506 // of interpreter activation frames via requests directly from the vm (via call_stub)
1507 // and via requests from the interpreter. The requests from the call_stub happen
1508 // directly thru the entry point. Requests from the interpreter happen via returning
1509 // from the interpreter and examining the message the interpreter has returned to
1510 // the frame manager. The frame manager can take the following requests:
1511 
1512 // NO_REQUEST - error, should never happen.
1513 // MORE_MONITORS - need a new monitor. Shuffle the expression stack on down and
1514 //                 allocate a new monitor.
1515 // CALL_METHOD - setup a new activation to call a new method. Very similar to what
1516 //               happens during entry during the entry via the call stub.
1517 // RETURN_FROM_METHOD - remove an activation. Return to interpreter or call stub.
1518 //
1519 // Arguments:
1520 //
1521 // rbx: methodOop
1522 // rcx: receiver - unused (retrieved from stack as needed)
1523 // rsi: previous frame manager state (NULL from the call_stub/c1/c2)
1524 //
1525 //
1526 // Stack layout at entry
1527 //
1528 // [ return address     ] <--- rsp
1529 // [ parameter n        ]
1530 //   ...
1531 // [ parameter 1        ]
1532 // [ expression stack   ]
1533 //
1534 //
1535 // We are free to blow any registers we like because the call_stub which brought us here
1536 // initially has preserved the callee save registers already.
1537 //
1538 //
1539 
1540 static address interpreter_frame_manager = NULL;
1541 
1542 address InterpreterGenerator::generate_normal_entry(bool synchronized) {
1543 
1544   // rbx: methodOop
1545   // rsi: sender sp
1546 
1547   // Because we redispatch "recursive" interpreter entries thru this same entry point
1548   // the "input" register usage is a little strange and not what you expect coming
1549   // from the call_stub. From the call stub rsi/rdi (current/previous) interpreter
1550   // state are NULL but on "recursive" dispatches they are what you'd expect.
1551   // rsi: current interpreter state (C++ interpreter) must preserve (null from call_stub/c1/c2)
1552 
1553 
1554   // A single frame manager is plenty as we don't specialize for synchronized. We could and
1555   // the code is pretty much ready. Would need to change the test below and for good measure
1556   // modify generate_interpreter_state to only do the (pre) sync stuff stuff for synchronized
1557   // routines. Not clear this is worth it yet.
1558 
1559   if (interpreter_frame_manager) return interpreter_frame_manager;
1560 
1561   address entry_point = __ pc();
1562 
1563   // Fast accessor methods share this entry point. 
1564   // This works because frame manager is in the same codelet
1565   if (UseFastAccessorMethods && !synchronized) __ bind(fast_accessor_slow_entry_path);
1566 
1567   Label dispatch_entry_2;
1568   __ movl(rcx, rsi);
1569   __ movl(rsi, 0);                                                 // no current activation
1570 
1571   __ jmp(dispatch_entry_2);
1572 
1573   const Register state   = rsi;                                    // current activation object, valid on entry
1574   const Register locals  = rdi; 
1575 
1576   Label re_dispatch;
1577 
1578   __ bind(re_dispatch);
1579 
1580   // save sender sp (doesn't include return address
1581   __ leal(rcx, Address(rsp, wordSize));
1582 
1583   __ bind(dispatch_entry_2);
1584 
1585   // save sender sp
1586   __ pushl(rcx);
1587 
1588   const Address size_of_parameters(rbx, methodOopDesc::size_of_parameters_offset());
1589   const Address size_of_locals    (rbx, methodOopDesc::size_of_locals_offset());
1590   const Address access_flags      (rbx, methodOopDesc::access_flags_offset());
1591 
1592   // const Address monitor_block_top (rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
1593   // const Address monitor_block_bot (rbp, frame::interpreter_frame_initial_sp_offset        * wordSize);
1594   // const Address monitor(rbp, frame::interpreter_frame_initial_sp_offset * wordSize - (int)sizeof(BasicObjectLock));
1595 
1596   // get parameter size (always needed)
1597   __ load_unsigned_word(rcx, size_of_parameters);
1598 
1599   // rbx: methodOop
1600   // rcx: size of parameters
1601   __ load_unsigned_word(rdx, size_of_locals);                      // get size of locals in words
1602 
1603   __ subl(rdx, rcx);                                               // rdx = no. of additional locals
1604 
1605   // see if we've got enough room on the stack for locals plus overhead.
1606   generate_stack_overflow_check();                                 // C++
1607 
1608   // c++ interpreter does not use stack banging or any implicit exceptions
1609   // leave for now to verify that check is proper.
1610   bang_stack_shadow_pages(false);
1611 
1612 
1613 
1614   // compute beginning of parameters (rdi)
1615   __ leal(locals, Address(rsp, rcx, Address::times_4, wordSize));
1616 
1617   // save sender's sp
1618   // __ movl(rcx, rsp);
1619 
1620   // get sender's sp
1621   __ popl(rcx);                                       
1622 
1623   // get return address
1624   __ popl(rax);                                       
1625 
1626   // rdx - # of additional locals
1627   // allocate space for locals
1628   // explicitly initialize locals
1629   {
1630     Label exit, loop;
1631     __ testl(rdx, rdx);
1632     __ jcc(Assembler::lessEqual, exit);               // do nothing if rdx <= 0
1633     __ bind(loop);
1634     __ pushl((int)NULL);                              // initialize local variables
1635     __ decrement(rdx);                                // until everything initialized
1636     __ jcc(Assembler::greater, loop);
1637     __ bind(exit);
1638   }
1639 
1640   
1641   // Assumes rax = return address
1642 
1643   // allocate and initialize new interpreterState and method expression stack
1644   // IN(locals) ->  locals
1645   // IN(state) -> any current interpreter activation
1646   // destroys rax, rcx, rdx, rdi
1647   // OUT (state) -> new interpreterState
1648   // OUT(rsp) -> bottom of methods expression stack
1649 
1650   generate_compute_interpreter_state(state, locals, rcx, false);
1651 
1652   // Call interpreter
1653 
1654   Label call_interpreter;
1655   __ bind(call_interpreter);
1656 
1657   // c++ interpreter does not use stack banging or any implicit exceptions
1658   // leave for now to verify that check is proper.
1659   bang_stack_shadow_pages(false);
1660 
1661 
1662   // Call interpreter enter here if message is
1663   // set and we know stack size is valid
1664 
1665   Label call_interpreter_2;
1666 
1667   __ bind(call_interpreter_2);
1668 
1669   {
1670     const Register thread  = rcx; 
1671 
1672     __ pushl(state);                                                 // push arg to interpreter
1673     __ movl(thread, STATE(_thread));




1674 
1675     // We can setup the frame anchor with everything we want at this point
1676     // as we are thread_in_Java and no safepoints can occur until we go to
1677     // vm mode. We do have to clear flags on return from vm but that is it
1678     //
1679     __ movl(Address(thread, JavaThread::last_Java_fp_offset()), rbp);
1680     __ movl(Address(thread, JavaThread::last_Java_sp_offset()), rsp);
1681 
1682     // Call the interpreter
1683 
1684     RuntimeAddress normal(CAST_FROM_FN_PTR(address, BytecodeInterpreter::run));
1685     RuntimeAddress checking(CAST_FROM_FN_PTR(address, BytecodeInterpreter::runWithChecks));
1686 
1687     __ call(JvmtiExport::can_post_interpreter_events() ? checking : normal);
1688     __ popl(rax);                                                  // discard parameter to run
1689     //
1690     // state is preserved since it is callee saved
1691     //
1692 
1693     // reset_last_Java_frame
1694 
1695     __ movl(thread, STATE(_thread));
1696     __ reset_last_Java_frame(thread, true, true);
1697   }
1698 
1699   // examine msg from interpreter to determine next action
1700   
1701   __ movl(rdx, STATE(_msg));                                       // Get new message
1702 
1703   Label call_method;
1704   Label return_from_interpreted_method;
1705   Label throw_exception;
1706   Label bad_msg;
1707   Label do_OSR;
1708 
1709   __ cmpl(rdx, (int)BytecodeInterpreter::call_method);
1710   __ jcc(Assembler::equal, call_method);
1711   __ cmpl(rdx, (int)BytecodeInterpreter::return_from_method);
1712   __ jcc(Assembler::equal, return_from_interpreted_method);
1713   __ cmpl(rdx, (int)BytecodeInterpreter::do_osr);
1714   __ jcc(Assembler::equal, do_OSR);
1715   __ cmpl(rdx, (int)BytecodeInterpreter::throwing_exception);
1716   __ jcc(Assembler::equal, throw_exception);
1717   __ cmpl(rdx, (int)BytecodeInterpreter::more_monitors);
1718   __ jcc(Assembler::notEqual, bad_msg);
1719 
1720   // Allocate more monitor space, shuffle expression stack....
1721 
1722   generate_more_monitors();
1723 
1724   __ jmp(call_interpreter);
1725 
1726   // uncommon trap needs to jump to here to enter the interpreter (re-execute current bytecode)
1727   unctrap_frame_manager_entry  = __ pc();
1728   //
1729   // Load the registers we need.
1730   __ leal(state, Address(rbp, -(int)sizeof(BytecodeInterpreter)));
1731   __ movl(rsp, STATE(_stack_limit));                               // restore expression stack to full depth
1732   __ jmp(call_interpreter_2);
1733 
1734 
1735 
1736   //=============================================================================
1737   // Returning from a compiled method into a deopted method. The bytecode at the
1738   // bcp has completed. The result of the bytecode is in the native abi (the tosca
1739   // for the template based interpreter). Any stack space that was used by the
1740   // bytecode that has completed has been removed (e.g. parameters for an invoke)
1741   // so all that we have to do is place any pending result on the expression stack
1742   // and resume execution on the next bytecode.
1743 
1744 
1745   generate_deopt_handling();
1746   __ jmp(call_interpreter);
1747 
1748 
1749   // Current frame has caught an exception we need to dispatch to the
1750   // handler. We can get here because a native interpreter frame caught
1751   // an exception in which case there is no handler and we must rethrow
1752   // If it is a vanilla interpreted frame the we simply drop into the
1753   // interpreter and let it do the lookup.
1754 
1755   Interpreter::_rethrow_exception_entry = __ pc();
1756   // rax: exception
1757   // rdx: return address/pc that threw exception
1758 
1759   Label return_with_exception;
1760   Label unwind_and_forward;
1761 
1762   // restore state pointer.
1763   __ leal(state, Address(rbp,  -sizeof(BytecodeInterpreter)));
1764 
1765   __ movl(rbx, STATE(_method));                       // get method



1766   __ movl(rcx, STATE(_thread));                       // get thread
1767 
1768   // Store exception with interpreter will expect it
1769   __ movl(Address(rcx, Thread::pending_exception_offset()), rax);

1770 
1771   // is current frame vanilla or native?
1772 
1773   __ movl(rdx, access_flags);
1774   __ testl(rdx, JVM_ACC_NATIVE);
1775   __ jcc(Assembler::zero, return_with_exception);     // vanilla interpreted frame, handle directly
1776 
1777   // We drop thru to unwind a native interpreted frame with a pending exception
1778   // We jump here for the initial interpreter frame with exception pending 
1779   // We unwind the current acivation and forward it to our caller.
1780 
1781   __ bind(unwind_and_forward);
1782   
1783   // unwind rbp, return stack to unextended value and re-push return address
1784 
1785   __ movl(rcx, STATE(_sender_sp));
1786   __ leave();
1787   __ popl(rdx);
1788   __ movl(rsp, rcx);
1789   __ pushl(rdx);
1790   __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
1791 
1792   // Return point from a call which returns a result in the native abi
1793   // (c1/c2/jni-native). This result must be processed onto the java
1794   // expression stack. 
1795   //
1796   // A pending exception may be present in which case there is no result present
1797 
1798   Label resume_interpreter;
1799   Label do_float;
1800   Label do_double;
1801   Label done_conv;
1802 
1803   address compiled_entry = __ pc();
1804 
1805   // The FPU stack is clean if UseSSE >= 2 but must be cleaned in other cases
1806   if (UseSSE < 2) {
1807     __ leal(state, Address(rbp,  -sizeof(BytecodeInterpreter)));
1808     __ movl(rbx, STATE(_result._to_call._callee));                     // get method just executed 
1809     __ movl(rcx, Address(rbx, methodOopDesc::result_index_offset())); 
1810     __ cmpl(rcx, AbstractInterpreter::BasicType_as_index(T_FLOAT));    // Result stub address array index
1811     __ jcc(Assembler::equal, do_float);
1812     __ cmpl(rcx, AbstractInterpreter::BasicType_as_index(T_DOUBLE));    // Result stub address array index
1813     __ jcc(Assembler::equal, do_double);
1814 #ifdef COMPILER2
1815     __ empty_FPU_stack();
1816 #endif // COMPILER2
1817     __ jmp(done_conv);
1818 
1819     __ bind(do_float);
1820 #ifdef COMPILER2
1821     for (int i = 1; i < 8; i++) {
1822       __ ffree(i);
1823     }
1824 #endif // COMPILER2
1825     __ jmp(done_conv);
1826     __ bind(do_double);
1827 #ifdef COMPILER2
1828     for (int i = 1; i < 8; i++) {
1829       __ ffree(i);
1830     }
1831 #endif // COMPILER2
1832     __ jmp(done_conv);
1833   } else {
1834     __ MacroAssembler::verify_FPU(0, "generate_return_entry_for compiled");
1835     __ jmp(done_conv);
1836   }
1837 

1838   // emit a sentinel we can test for when converting an interpreter
1839   // entry point to a compiled entry point.
1840   __ a_long(Interpreter::return_sentinel);
1841   __ a_long((int)compiled_entry);

1842 
1843   // Return point to interpreter from compiled/native method
1844 
1845   InternalAddress return_from_native_method(__ pc());
1846 
1847   __ bind(done_conv);
1848 
1849 
1850   // Result if any is in tosca. The java expression stack is in the state that the
1851   // calling convention left it (i.e. params may or may not be present)
1852   // Copy the result from tosca and place it on java expression stack.
1853 
1854   // Restore rsi as compiled code may not preserve it
1855 
1856   __ leal(state, Address(rbp,  -sizeof(BytecodeInterpreter)));
1857 
1858   // restore stack to what we had when we left (in case i2c extended it)
1859 
1860   __ movl(rsp, STATE(_stack));
1861   __ leal(rsp, Address(rsp, wordSize));
1862 
1863   // If there is a pending exception then we don't really have a result to process
1864 
1865   __ movl(rcx, STATE(_thread));                       // get thread
1866   __ cmpl(Address(rcx, Thread::pending_exception_offset()), (int)NULL);




1867   __ jcc(Assembler::notZero, return_with_exception);    
1868 
1869   // get method just executed 
1870   __ movl(rbx, STATE(_result._to_call._callee));
1871 
1872   // callee left args on top of expression stack, remove them
1873   __ load_unsigned_word(rcx, Address(rbx, methodOopDesc::size_of_parameters_offset()));
1874   __ leal(rsp, Address(rsp, rcx, Address::times_4));
1875 
1876   __ movl(rcx, Address(rbx, methodOopDesc::result_index_offset())); 
1877   ExternalAddress tosca_to_stack((address)CppInterpreter::_tosca_to_stack);
1878   // Address index(noreg, rax, Address::times_4);
1879   __ movptr(rcx, ArrayAddress(tosca_to_stack, Address(noreg, rcx, Address::times_4)));
1880   // __ movl(rcx, Address(noreg, rcx, Address::times_4, int(AbstractInterpreter::_tosca_to_stack)));
1881   __ call(rcx);                                               // call result converter
1882   __ jmp(resume_interpreter);
1883 
1884   // An exception is being caught on return to a vanilla interpreter frame.
1885   // Empty the stack and resume interpreter
1886 
1887   __ bind(return_with_exception);
1888 
1889   // Exception present, empty stack
1890   __ movl(rsp, STATE(_stack_base));
1891   __ jmp(resume_interpreter);
1892 
1893   // Return from interpreted method we return result appropriate to the caller (i.e. "recursive"
1894   // interpreter call, or native) and unwind this interpreter activation.
1895   // All monitors should be unlocked.
1896 
1897   __ bind(return_from_interpreted_method); 
1898 
1899   Label return_to_initial_caller;
1900 
1901   __ movl(rbx, STATE(_method));                                     // get method just executed
1902   __ cmpl(STATE(_prev_link), (int)NULL);                            // returning from "recursive" interpreter call?
1903   __ movl(rax, Address(rbx, methodOopDesc::result_index_offset())); // get result type index
1904   __ jcc(Assembler::equal, return_to_initial_caller);               // back to native code (call_stub/c1/c2)
1905 
1906   // Copy result to callers java stack
1907   ExternalAddress stack_to_stack((address)CppInterpreter::_stack_to_stack);
1908   // Address index(noreg, rax, Address::times_4);
1909 
1910   __ movptr(rax, ArrayAddress(stack_to_stack, Address(noreg, rax, Address::times_4)));
1911   // __ movl(rax, Address(noreg, rax, Address::times_4, int(AbstractInterpreter::_stack_to_stack)));
1912   __ call(rax);                                                     // call result converter
1913 
1914   Label unwind_recursive_activation;
1915   __ bind(unwind_recursive_activation);
1916 
1917   // returning to interpreter method from "recursive" interpreter call
1918   // result converter left rax pointing to top of the java stack for method we are returning
1919   // to. Now all we must do is unwind the state from the completed call
1920 
1921   __ movl(state, STATE(_prev_link));                                // unwind state
1922   __ leave();                                                       // pop the frame
1923   __ movl(rsp, rax);                                                // unwind stack to remove args
1924 
1925   // Resume the interpreter. The current frame contains the current interpreter
1926   // state object. 
1927   //
1928 
1929   __ bind(resume_interpreter);
1930 
1931   // state == interpreterState object for method we are resuming
1932 
1933   __ movl(STATE(_msg), (int)BytecodeInterpreter::method_resume);
1934   __ leal(rsp, Address(rsp, -wordSize));                           // prepush stack (result if any already present)
1935   __ movl(STATE(_stack), rsp);                                     // inform interpreter of new stack depth (parameters removed,
1936                                                                    // result if any on stack already )
1937   __ movl(rsp, STATE(_stack_limit));                               // restore expression stack to full depth
1938   __ jmp(call_interpreter_2);                                      // No need to bang 
1939 
1940   // interpreter returning to native code (call_stub/c1/c2) 
1941   // convert result and unwind initial activation
1942   // rax - result index
1943 
1944   __ bind(return_to_initial_caller);
1945   ExternalAddress stack_to_native((address)CppInterpreter::_stack_to_native_abi);
1946   // Address index(noreg, rax, Address::times_4);
1947 
1948   __ movptr(rax, ArrayAddress(stack_to_native, Address(noreg, rax, Address::times_4)));
1949   __ call(rax);                                                    // call result converter
1950 
1951   Label unwind_initial_activation;
1952   __ bind(unwind_initial_activation); 
1953 
1954   // RETURN TO CALL_STUB/C1/C2 code (result if any in rax/rdx ST(0))
1955 
1956   /* Current stack picture
1957 
1958         [ incoming parameters ]
1959         [ extra locals ]
1960         [ return address to CALL_STUB/C1/C2]
1961   fp -> [ CALL_STUB/C1/C2 fp ]
1962         BytecodeInterpreter object
1963         expression stack
1964   sp ->
1965 
1966   */
1967 
1968   // return restoring the stack to the original sender_sp value
1969 
1970   __ movl(rcx, STATE(_sender_sp));
1971   __ leave();
1972   __ popl(rdi);                                                     // get return address
1973   // set stack to sender's sp
1974   __ movl(rsp, rcx);
1975   __ jmp(rdi);                                                        // return to call_stub
1976 
1977   // OSR request, adjust return address to make current frame into adapter frame
1978   // and enter OSR nmethod
1979 
1980   __ bind(do_OSR);
1981 
1982   Label remove_initial_frame;
1983 
1984   // We are going to pop this frame. Is there another interpreter frame underneath
1985   // it or is it callstub/compiled?
1986 
1987   // Move buffer to the expected parameter location
1988   __ movl(rcx, STATE(_result._osr._osr_buf));
1989 
1990   __ movl(rax, STATE(_result._osr._osr_entry));
1991 
1992   __ cmpl(STATE(_prev_link), (int)NULL);                       // returning from "recursive" interpreter call?
1993   __ jcc(Assembler::equal, remove_initial_frame);              // back to native code (call_stub/c1/c2)
1994 
1995   // __ movl(state, STATE(_prev_link));                           // unwind state
1996   __ movl(rsi, STATE(_sender_sp));                             // get sender's sp in expected register
1997   __ leave();                                                  // pop the frame
1998   __ movl(rsp, rsi);                                           // trim any stack expansion
1999 
2000 
2001   // We know we are calling compiled so push specialized return
2002   // method uses specialized entry, push a return so we look like call stub setup
2003   // this path will handle fact that result is returned in registers and not
2004   // on the java stack.
2005 
2006   __ pushptr(return_from_native_method.addr());
2007 
2008   __ jmp(rax);
2009 
2010   __ bind(remove_initial_frame);
2011 
2012   __ movl(rdx, STATE(_sender_sp));
2013   __ leave();
2014   // get real return
2015   __ popl(rsi);
2016   // set stack to sender's sp
2017   __ movl(rsp, rdx);
2018   // repush real return
2019   __ pushl(rsi);
2020   // Enter OSR nmethod
2021   __ jmp(rax);
2022 
2023 
2024 
2025 
2026   // Call a new method. All we do is (temporarily) trim the expression stack
2027   // push a return address to bring us back to here and leap to the new entry.
2028 
2029   __ bind(call_method);
2030 
2031   // stack points to next free location and not top element on expression stack
2032   // method expects sp to be pointing to topmost element
2033 
2034   __ movl(rsp, STATE(_stack));                                       // pop args to c++ interpreter, set sp to java stack top
2035   __ leal(rsp, Address(rsp, wordSize));
2036 
2037   __ movl(rbx, STATE(_result._to_call._callee));                     // get method to execute
2038 
2039   // don't need a return address if reinvoking interpreter
2040 
2041   // Make it look like call_stub calling conventions
2042 
2043   // Get (potential) receiver
2044   __ load_unsigned_word(rcx, size_of_parameters);                     // get size of parameters in words
2045 
2046   ExternalAddress recursive(CAST_FROM_FN_PTR(address, RecursiveInterpreterActivation));
2047   __ pushptr(recursive.addr());                                      // make it look good in the debugger
2048 
2049   InternalAddress entry(entry_point);
2050   __ cmpptr(STATE(_result._to_call._callee_entry_point), entry.addr()); // returning to interpreter?
2051   __ jcc(Assembler::equal, re_dispatch);                             // yes
2052 
2053   __ popl(rax);                                                      // pop dummy address 
2054 
2055 
2056   // get specialized entry
2057   __ movl(rax, STATE(_result._to_call._callee_entry_point));
2058   // set sender SP
2059   __ movl(rsi, rsp);
2060 
2061   // method uses specialized entry, push a return so we look like call stub setup
2062   // this path will handle fact that result is returned in registers and not
2063   // on the java stack.
2064 
2065   __ pushptr(return_from_native_method.addr());
2066 
2067   __ jmp(rax);
2068 
2069   __ bind(bad_msg);
2070   __ stop("Bad message from interpreter");
2071 
2072   // Interpreted method "returned" with an exception pass it on...
2073   // Pass result, unwind activation and continue/return to interpreter/call_stub
2074   // We handle result (if any) differently based on return to interpreter or call_stub
2075 
2076   Label unwind_initial_with_pending_exception;
2077 
2078   __ bind(throw_exception);
2079   __ cmpl(STATE(_prev_link), (int)NULL);                            // returning from recursive interpreter call?
2080   __ jcc(Assembler::equal, unwind_initial_with_pending_exception);  // no, back to native code (call_stub/c1/c2)
2081   __ movl(rax, STATE(_locals));                                     // pop parameters get new stack value
2082   __ addl(rax, wordSize);                                           // account for prepush before we return
2083   __ jmp(unwind_recursive_activation);
2084 
2085   __ bind(unwind_initial_with_pending_exception);
2086   
2087   // We will unwind the current (initial) interpreter frame and forward
2088   // the exception to the caller. We must put the exception in the
2089   // expected register and clear pending exception and then forward.
2090 
2091   __ jmp(unwind_and_forward);
2092 
2093   interpreter_frame_manager = entry_point;
2094   return entry_point;
2095 }
2096 
2097 address AbstractInterpreterGenerator::generate_method_entry(AbstractInterpreter::MethodKind kind) {
2098   // determine code generation flags
2099   bool synchronized = false;
2100   address entry_point = NULL;
2101 
2102   switch (kind) {    





   1 /*
   2  * Copyright 2007-2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *


  27 
  28 #ifdef CC_INTERP
  29 
  30 // Routine exists to make tracebacks look decent in debugger
  31 // while we are recursed in the frame manager/c++ interpreter.
  32 // We could use an address in the frame manager but having
  33 // frames look natural in the debugger is a plus.
  34 extern "C" void RecursiveInterpreterActivation(interpreterState istate )
  35 {
  36   //
  37   ShouldNotReachHere();
  38 }
  39 
  40 
  41 #define __ _masm->
  42 #define STATE(field_name) (Address(state, byte_offset_of(BytecodeInterpreter, field_name)))
  43 
  44 Label fast_accessor_slow_entry_path;  // fast accessor methods need to be able to jmp to unsynchronized
  45                                       // c++ interpreter entry point this holds that entry point label.
  46 
  47 // default registers for state and sender_sp
  48 // state and sender_sp are the same on 32bit because we have no choice.
  49 // state could be rsi on 64bit but it is an arg reg and not callee save
  50 // so r13 is better choice.
  51 
  52 const Register state = NOT_LP64(rsi) LP64_ONLY(r13);
  53 const Register sender_sp_on_entry = NOT_LP64(rsi) LP64_ONLY(r13);
  54 
  55 // NEEDED for JVMTI?
  56 // address AbstractInterpreter::_remove_activation_preserving_args_entry;
  57 
  58 static address unctrap_frame_manager_entry  = NULL;
  59 
  60 static address deopt_frame_manager_return_atos  = NULL;
  61 static address deopt_frame_manager_return_btos  = NULL;
  62 static address deopt_frame_manager_return_itos  = NULL;
  63 static address deopt_frame_manager_return_ltos  = NULL;
  64 static address deopt_frame_manager_return_ftos  = NULL;
  65 static address deopt_frame_manager_return_dtos  = NULL;
  66 static address deopt_frame_manager_return_vtos  = NULL;
  67 
  68 int AbstractInterpreter::BasicType_as_index(BasicType type) {
  69   int i = 0;
  70   switch (type) {
  71     case T_BOOLEAN: i = 0; break;
  72     case T_CHAR   : i = 1; break;
  73     case T_BYTE   : i = 2; break;
  74     case T_SHORT  : i = 3; break;


  79     case T_DOUBLE : i = 6; break;
  80     case T_OBJECT : // fall through
  81     case T_ARRAY  : i = 7; break;
  82     default       : ShouldNotReachHere();
  83   }
  84   assert(0 <= i && i < AbstractInterpreter::number_of_result_handlers, "index out of bounds");
  85   return i;
  86 }
  87 
  88 // Is this pc anywhere within code owned by the interpreter?
  89 // This only works for pc that might possibly be exposed to frame
  90 // walkers. It clearly misses all of the actual c++ interpreter
  91 // implementation
  92 bool CppInterpreter::contains(address pc)            {
  93     return (_code->contains(pc) ||
  94             pc == CAST_FROM_FN_PTR(address, RecursiveInterpreterActivation));
  95 }
  96 
  97 
  98 address CppInterpreterGenerator::generate_result_handler_for(BasicType type) {

  99   address entry = __ pc();
 100   switch (type) {
 101     case T_BOOLEAN: __ c2bool(rax);            break;
 102     case T_CHAR   : __ andl(rax, 0xFFFF);      break;
 103     case T_BYTE   : __ sign_extend_byte (rax); break;
 104     case T_SHORT  : __ sign_extend_short(rax); break;
 105     case T_VOID   : // fall thru
 106     case T_LONG   : // fall thru
 107     case T_INT    : /* nothing to do */        break;
 108 
 109     case T_DOUBLE :
 110     case T_FLOAT  :
 111       {
 112         const Register t = InterpreterRuntime::SignatureHandlerGenerator::temp();
 113         __ pop(t);                            // remove return address first
 114         // Must return a result for interpreter or compiler. In SSE
 115         // mode, results are returned in xmm0 and the FPU stack must
 116         // be empty.
 117         if (type == T_FLOAT && UseSSE >= 1) {
 118 #ifndef _LP64
 119           // Load ST0
 120           __ fld_d(Address(rsp, 0));
 121           // Store as float and empty fpu stack
 122           __ fstp_s(Address(rsp, 0));
 123 #endif // !_LP64
 124           // and reload
 125           __ movflt(xmm0, Address(rsp, 0));
 126         } else if (type == T_DOUBLE && UseSSE >= 2 ) {
 127           __ movdbl(xmm0, Address(rsp, 0));
 128         } else {
 129           // restore ST0
 130           __ fld_d(Address(rsp, 0));
 131         }
 132         // and pop the temp
 133         __ addptr(rsp, 2 * wordSize);
 134         __ push(t);                            // restore return address
 135       }
 136       break;
 137     case T_OBJECT :
 138       // retrieve result from frame
 139       __ movptr(rax, STATE(_oop_temp));
 140       // and verify it
 141       __ verify_oop(rax);
 142       break;
 143     default       : ShouldNotReachHere();
 144   }
 145   __ ret(0);                                   // return from result handler
 146   return entry;
 147 }
 148 
 149 // tosca based result to c++ interpreter stack based result.
 150 // Result goes to top of native stack.
 151 
 152 #undef EXTEND  // SHOULD NOT BE NEEDED
 153 address CppInterpreterGenerator::generate_tosca_to_stack_converter(BasicType type) {
 154   // A result is in the tosca (abi result) from either a native method call or compiled
 155   // code. Place this result on the java expression stack so C++ interpreter can use it.
 156   address entry = __ pc();
 157 
 158   const Register t = InterpreterRuntime::SignatureHandlerGenerator::temp();
 159   __ pop(t);                            // remove return address first
 160   switch (type) {
 161     case T_VOID:
 162        break;
 163     case T_BOOLEAN:
 164 #ifdef EXTEND
 165       __ c2bool(rax);
 166 #endif
 167       __ push(rax);
 168       break;
 169     case T_CHAR   :
 170 #ifdef EXTEND
 171       __ andl(rax, 0xFFFF);
 172 #endif
 173       __ push(rax);
 174       break;
 175     case T_BYTE   :
 176 #ifdef EXTEND
 177       __ sign_extend_byte (rax);
 178 #endif
 179       __ push(rax);
 180       break;
 181     case T_SHORT  :
 182 #ifdef EXTEND
 183       __ sign_extend_short(rax);
 184 #endif
 185       __ push(rax);
 186       break;
 187     case T_LONG    :
 188       __ push(rdx);                             // pushes useless junk on 64bit
 189       __ push(rax);
 190       break;
 191     case T_INT    :
 192       __ push(rax);
 193       break;
 194     case T_FLOAT  :
 195       // Result is in ST(0)/xmm0
 196       __ subptr(rsp, wordSize);
 197       if ( UseSSE < 1) {
 198         __ fstp_s(Address(rsp, 0));
 199       } else {

 200         __ movflt(Address(rsp, 0), xmm0);
 201       }
 202       break;
 203     case T_DOUBLE  :
 204       __ subptr(rsp, 2*wordSize);
 205       if ( UseSSE < 2 ) {
 206         __ fstp_d(Address(rsp, 0));
 207       } else {

 208         __ movdbl(Address(rsp, 0), xmm0);
 209       }
 210       break;
 211     case T_OBJECT :
 212       __ verify_oop(rax);                      // verify it
 213       __ push(rax);
 214       break;
 215     default       : ShouldNotReachHere();
 216   }
 217   __ jmp(t);                                   // return from result handler
 218   return entry;
 219 }
 220 
 221 address CppInterpreterGenerator::generate_stack_to_stack_converter(BasicType type) {
 222   // A result is in the java expression stack of the interpreted method that has just
 223   // returned. Place this result on the java expression stack of the caller.
 224   //
 225   // The current interpreter activation in rsi/r13 is for the method just returning its
 226   // result. So we know that the result of this method is on the top of the current
 227   // execution stack (which is pre-pushed) and will be return to the top of the caller
 228   // stack. The top of the callers stack is the bottom of the locals of the current
 229   // activation.
 230   // Because of the way activation are managed by the frame manager the value of rsp is
 231   // below both the stack top of the current activation and naturally the stack top
 232   // of the calling activation. This enable this routine to leave the return address
 233   // to the frame manager on the stack and do a vanilla return.
 234   //
 235   // On entry: rsi/r13 - interpreter state of activation returning a (potential) result
 236   // On Return: rsi/r13 - unchanged
 237   //            rax - new stack top for caller activation (i.e. activation in _prev_link)
 238   //
 239   // Can destroy rdx, rcx.
 240   //
 241 
 242   address entry = __ pc();

 243   const Register t = InterpreterRuntime::SignatureHandlerGenerator::temp();
 244   switch (type) {
 245     case T_VOID:
 246       __ movptr(rax, STATE(_locals));                                   // pop parameters get new stack value
 247       __ addptr(rax, wordSize);                                         // account for prepush before we return
 248       break;
 249     case T_FLOAT  :
 250     case T_BOOLEAN:
 251     case T_CHAR   :
 252     case T_BYTE   :
 253     case T_SHORT  :
 254     case T_INT    :
 255       // 1 word result
 256       __ movptr(rdx, STATE(_stack));
 257       __ movptr(rax, STATE(_locals));                                   // address for result
 258       __ movl(rdx, Address(rdx, wordSize));                             // get result
 259       __ movptr(Address(rax, 0), rdx);                                  // and store it
 260       break;
 261     case T_LONG    :
 262     case T_DOUBLE  :
 263       // return top two words on current expression stack to caller's expression stack
 264       // The caller's expression stack is adjacent to the current frame manager's intepretState
 265       // except we allocated one extra word for this intepretState so we won't overwrite it
 266       // when we return a two word result.
 267 
 268       __ movptr(rax, STATE(_locals));                                   // address for result
 269       __ movptr(rcx, STATE(_stack));
 270       __ subptr(rax, wordSize);                                         // need addition word besides locals[0]
 271       __ movptr(rdx, Address(rcx, 2*wordSize));                         // get result word (junk in 64bit)
 272       __ movptr(Address(rax, wordSize), rdx);                           // and store it
 273       __ movptr(rdx, Address(rcx, wordSize));                           // get result word
 274       __ movptr(Address(rax, 0), rdx);                                  // and store it
 275       break;
 276     case T_OBJECT :
 277       __ movptr(rdx, STATE(_stack));
 278       __ movptr(rax, STATE(_locals));                                   // address for result
 279       __ movptr(rdx, Address(rdx, wordSize));                           // get result
 280       __ verify_oop(rdx);                                               // verify it
 281       __ movptr(Address(rax, 0), rdx);                                  // and store it
 282       break;
 283     default       : ShouldNotReachHere();
 284   }
 285   __ ret(0);
 286   return entry;
 287 }
 288 
 289 address CppInterpreterGenerator::generate_stack_to_native_abi_converter(BasicType type) {
 290   // A result is in the java expression stack of the interpreted method that has just
 291   // returned. Place this result in the native abi that the caller expects.
 292   //
 293   // Similar to generate_stack_to_stack_converter above. Called at a similar time from the
 294   // frame manager execept in this situation the caller is native code (c1/c2/call_stub)
 295   // and so rather than return result onto caller's java expression stack we return the
 296   // result in the expected location based on the native abi.
 297   // On entry: rsi/r13 - interpreter state of activation returning a (potential) result
 298   // On Return: rsi/r13 - unchanged
 299   // Other registers changed [rax/rdx/ST(0) as needed for the result returned]
 300 
 301   address entry = __ pc();

 302   switch (type) {
 303     case T_VOID:
 304        break;
 305     case T_BOOLEAN:
 306     case T_CHAR   :
 307     case T_BYTE   :
 308     case T_SHORT  :
 309     case T_INT    :
 310       __ movptr(rdx, STATE(_stack));                                    // get top of stack
 311       __ movl(rax, Address(rdx, wordSize));                             // get result word 1
 312       break;
 313     case T_LONG    :
 314       __ movptr(rdx, STATE(_stack));                                    // get top of stack
 315       __ movptr(rax, Address(rdx, wordSize));                           // get result low word
 316       NOT_LP64(__ movl(rdx, Address(rdx, 2*wordSize));)                 // get result high word

 317       break;
 318     case T_FLOAT  :
 319       __ movptr(rdx, STATE(_stack));                                    // get top of stack
 320       if ( UseSSE >= 1) {
 321         __ movflt(xmm0, Address(rdx, wordSize));
 322       } else {
 323         __ fld_s(Address(rdx, wordSize));                               // pushd float result
 324       }
 325       break;
 326     case T_DOUBLE  :
 327       __ movptr(rdx, STATE(_stack));                                    // get top of stack
 328       if ( UseSSE > 1) {
 329         __ movdbl(xmm0, Address(rdx, wordSize));
 330       } else {
 331         __ fld_d(Address(rdx, wordSize));                               // push double result
 332       }
 333       break;
 334     case T_OBJECT :
 335       __ movptr(rdx, STATE(_stack));                                    // get top of stack
 336       __ movptr(rax, Address(rdx, wordSize));                           // get result word 1
 337       __ verify_oop(rax);                                               // verify it
 338       break;
 339     default       : ShouldNotReachHere();
 340   }
 341   __ ret(0);
 342   return entry;
 343 }
 344 
 345 address CppInterpreter::return_entry(TosState state, int length) {
 346   // make it look good in the debugger
 347   return CAST_FROM_FN_PTR(address, RecursiveInterpreterActivation);
 348 }
 349 
 350 address CppInterpreter::deopt_entry(TosState state, int length) {
 351   address ret = NULL;
 352   if (length != 0) {
 353     switch (state) {
 354       case atos: ret = deopt_frame_manager_return_atos; break;
 355       case btos: ret = deopt_frame_manager_return_btos; break;
 356       case ctos:


 398   // rsp == bottom of method's expression stack.
 399 
 400   const Address const_offset      (rbx, methodOopDesc::const_offset());
 401 
 402 
 403   // On entry sp is the sender's sp. This includes the space for the arguments
 404   // that the sender pushed. If the sender pushed no args (a static) and the
 405   // caller returns a long then we need two words on the sender's stack which
 406   // are not present (although when we return a restore full size stack the
 407   // space will be present). If we didn't allocate two words here then when
 408   // we "push" the result of the caller's stack we would overwrite the return
 409   // address and the saved rbp. Not good. So simply allocate 2 words now
 410   // just to be safe. This is the "static long no_params() method" issue.
 411   // See Lo.java for a testcase.
 412   // We don't need this for native calls because they return result in
 413   // register and the stack is expanded in the caller before we store
 414   // the results on the stack.
 415 
 416   if (!native) {
 417 #ifdef PRODUCT
 418     __ subptr(rsp, 2*wordSize);
 419 #else /* PRODUCT */
 420     __ push((int32_t)NULL_WORD);
 421     __ push(state);                         // make it look like a real argument
 422 #endif /* PRODUCT */
 423   }
 424 
 425   // Now that we are assure of space for stack result, setup typical linkage
 426 
 427   __ push(rax);
 428   __ enter();
 429 
 430   __ mov(rax, state);                                  // save current state
 431 
 432   __ lea(rsp, Address(rsp, -(int)sizeof(BytecodeInterpreter)));
 433   __ mov(state, rsp);
 434 
 435   // rsi/r13 == state/locals rax == prevstate
 436 
 437   // initialize the "shadow" frame so that use since C++ interpreter not directly
 438   // recursive. Simpler to recurse but we can't trim expression stack as we call
 439   // new methods.
 440   __ movptr(STATE(_locals), locals);                    // state->_locals = locals()
 441   __ movptr(STATE(_self_link), state);                  // point to self
 442   __ movptr(STATE(_prev_link), rax);                    // state->_link = state on entry (NULL or previous state)
 443   __ movptr(STATE(_sender_sp), sender_sp);              // state->_sender_sp = sender_sp
 444 #ifdef _LP64
 445   __ movptr(STATE(_thread), r15_thread);                // state->_bcp = codes()
 446 #else
 447   __ get_thread(rax);                                   // get vm's javathread*
 448   __ movptr(STATE(_thread), rax);                       // state->_bcp = codes()
 449 #endif // _LP64
 450   __ movptr(rdx, Address(rbx, methodOopDesc::const_offset())); // get constantMethodOop
 451   __ lea(rdx, Address(rdx, constMethodOopDesc::codes_offset())); // get code base
 452   if (native) {
 453     __ movptr(STATE(_bcp), (int32_t)NULL_WORD);         // state->_bcp = NULL
 454   } else {
 455     __ movptr(STATE(_bcp), rdx);                        // state->_bcp = codes()
 456   }
 457   __ xorptr(rdx, rdx);
 458   __ movptr(STATE(_oop_temp), rdx);                     // state->_oop_temp = NULL (only really needed for native)
 459   __ movptr(STATE(_mdx), rdx);                          // state->_mdx = NULL
 460   __ movptr(rdx, Address(rbx, methodOopDesc::constants_offset()));
 461   __ movptr(rdx, Address(rdx, constantPoolOopDesc::cache_offset_in_bytes()));
 462   __ movptr(STATE(_constants), rdx);                    // state->_constants = constants()
 463 
 464   __ movptr(STATE(_method), rbx);                       // state->_method = method()
 465   __ movl(STATE(_msg), (int32_t) BytecodeInterpreter::method_entry);   // state->_msg = initial method entry
 466   __ movptr(STATE(_result._to_call._callee), (int32_t) NULL_WORD); // state->_result._to_call._callee_callee = NULL
 467 
 468 
 469   __ movptr(STATE(_monitor_base), rsp);                 // set monitor block bottom (grows down) this would point to entry [0]
 470                                                         // entries run from -1..x where &monitor[x] ==
 471 
 472   {
 473     // Must not attempt to lock method until we enter interpreter as gc won't be able to find the
 474     // initial frame. However we allocate a free monitor so we don't have to shuffle the expression stack
 475     // immediately.
 476 
 477     // synchronize method
 478     const Address access_flags      (rbx, methodOopDesc::access_flags_offset());
 479     const int entry_size            = frame::interpreter_frame_monitor_size() * wordSize;
 480     Label not_synced;
 481 
 482     __ movl(rax, access_flags);
 483     __ testl(rax, JVM_ACC_SYNCHRONIZED);
 484     __ jcc(Assembler::zero, not_synced);
 485 
 486     // Allocate initial monitor and pre initialize it
 487     // get synchronization object
 488 
 489     Label done;
 490     const int mirror_offset = klassOopDesc::klass_part_offset_in_bytes() + Klass::java_mirror_offset_in_bytes();
 491     __ movl(rax, access_flags);
 492     __ testl(rax, JVM_ACC_STATIC);
 493     __ movptr(rax, Address(locals, 0));                   // get receiver (assume this is frequent case)
 494     __ jcc(Assembler::zero, done);
 495     __ movptr(rax, Address(rbx, methodOopDesc::constants_offset()));
 496     __ movptr(rax, Address(rax, constantPoolOopDesc::pool_holder_offset_in_bytes()));
 497     __ movptr(rax, Address(rax, mirror_offset));
 498     __ bind(done);
 499     // add space for monitor & lock
 500     __ subptr(rsp, entry_size);                                           // add space for a monitor entry
 501     __ movptr(Address(rsp, BasicObjectLock::obj_offset_in_bytes()), rax); // store object
 502     __ bind(not_synced);
 503   }
 504 
 505   __ movptr(STATE(_stack_base), rsp);                                     // set expression stack base ( == &monitors[-count])
 506   if (native) {
 507     __ movptr(STATE(_stack), rsp);                                        // set current expression stack tos
 508     __ movptr(STATE(_stack_limit), rsp);
 509   } else {
 510     __ subptr(rsp, wordSize);                                             // pre-push stack
 511     __ movptr(STATE(_stack), rsp);                                        // set current expression stack tos
 512 
 513     // compute full expression stack limit
 514 
 515     const Address size_of_stack    (rbx, methodOopDesc::max_stack_offset());
 516     __ load_unsigned_word(rdx, size_of_stack);                            // get size of expression stack in words
 517     __ negptr(rdx);                                                       // so we can subtract in next step
 518     // Allocate expression stack
 519     __ lea(rsp, Address(rsp, rdx, Address::times_ptr));
 520     __ movptr(STATE(_stack_limit), rsp);
 521   }
 522 
 523 #ifdef _LP64
 524   // Make sure stack is properly aligned and sized for the abi
 525   __ subptr(rsp, frame::arg_reg_save_area_bytes); // windows
 526   __ andptr(rsp, -16); // must be 16 byte boundry (see amd64 ABI)
 527 #endif // _LP64
 528 
 529 
 530 
 531 }
 532 
 533 // Helpers for commoning out cases in the various type of method entries.
 534 //
 535 
 536 // increment invocation count & check for overflow
 537 //
 538 // Note: checking for negative value instead of overflow
 539 //       so we have a 'sticky' overflow test
 540 //
 541 // rbx,: method
 542 // rcx: invocation counter
 543 //
 544 void InterpreterGenerator::generate_counter_incr(Label* overflow, Label* profile_method, Label* profile_method_continue) {
 545 
 546   const Address invocation_counter(rbx, methodOopDesc::invocation_counter_offset() + InvocationCounter::counter_offset());
 547   const Address backedge_counter  (rbx, methodOopDesc::backedge_counter_offset() + InvocationCounter::counter_offset());
 548 
 549   if (ProfileInterpreter) { // %%% Merge this into methodDataOop
 550     __ incrementl(Address(rbx,methodOopDesc::interpreter_invocation_counter_offset()));
 551   }
 552   // Update standard invocation counters
 553   __ movl(rax, backedge_counter);               // load backedge counter
 554 
 555   __ increment(rcx, InvocationCounter::count_increment);
 556   __ andl(rax, InvocationCounter::count_mask_value);  // mask out the status bits
 557 
 558   __ movl(invocation_counter, rcx);             // save invocation count
 559   __ addl(rcx, rax);                            // add both counters
 560 
 561   // profile_method is non-null only for interpreted method so
 562   // profile_method != NULL == !native_call
 563   // BytecodeInterpreter only calls for native so code is elided.
 564 
 565   __ cmp32(rcx,
 566            ExternalAddress((address)&InvocationCounter::InterpreterInvocationLimit));
 567   __ jcc(Assembler::aboveEqual, *overflow);
 568 
 569 }
 570 
 571 void InterpreterGenerator::generate_counter_overflow(Label* do_continue) {
 572 
 573   // C++ interpreter on entry
 574   // rsi/r13 - new interpreter state pointer
 575   // rbp - interpreter frame pointer
 576   // rbx - method
 577 
 578   // On return (i.e. jump to entry_point) [ back to invocation of interpreter ]
 579   // rbx, - method
 580   // rcx - rcvr (assuming there is one)
 581   // top of stack return address of interpreter caller
 582   // rsp - sender_sp
 583 
 584   // C++ interpreter only
 585   // rsi/r13 - previous interpreter state pointer
 586 
 587   const Address size_of_parameters(rbx, methodOopDesc::size_of_parameters_offset());
 588 
 589   // InterpreterRuntime::frequency_counter_overflow takes one argument
 590   // indicating if the counter overflow occurs at a backwards branch (non-NULL bcp).
 591   // The call returns the address of the verified entry point for the method or NULL
 592   // if the compilation did not complete (either went background or bailed out).
 593   __ movptr(rax, (int32_t)false);
 594   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::frequency_counter_overflow), rax);
 595 
 596   // for c++ interpreter can rsi really be munged?
 597   __ lea(state, Address(rbp, -sizeof(BytecodeInterpreter)));                               // restore state
 598   __ movptr(rbx, Address(state, byte_offset_of(BytecodeInterpreter, _method)));            // restore method
 599   __ movptr(rdi, Address(state, byte_offset_of(BytecodeInterpreter, _locals)));            // get locals pointer
 600 


 601   __ jmp(*do_continue, relocInfo::none);
 602 
 603 }
 604 
 605 void InterpreterGenerator::generate_stack_overflow_check(void) {
 606   // see if we've got enough room on the stack for locals plus overhead.
 607   // the expression stack grows down incrementally, so the normal guard
 608   // page mechanism will work for that.
 609   //
 610   // Registers live on entry:
 611   //
 612   // Asm interpreter
 613   // rdx: number of additional locals this frame needs (what we must check)
 614   // rbx,: methodOop
 615 
 616   // C++ Interpreter
 617   // rsi/r13: previous interpreter frame state object
 618   // rdi: &locals[0]
 619   // rcx: # of locals
 620   // rdx: number of additional locals this frame needs (what we must check)
 621   // rbx: methodOop
 622 
 623   // destroyed on exit
 624   // rax,
 625 
 626   // NOTE:  since the additional locals are also always pushed (wasn't obvious in
 627   // generate_method_entry) so the guard should work for them too.
 628   //
 629 
 630   // monitor entry size: see picture of stack set (generate_method_entry) and frame_i486.hpp
 631   const int entry_size    = frame::interpreter_frame_monitor_size() * wordSize;
 632 
 633   // total overhead size: entry_size + (saved rbp, thru expr stack bottom).
 634   // be sure to change this if you add/subtract anything to/from the overhead area
 635   const int overhead_size = (int)sizeof(BytecodeInterpreter);
 636 
 637   const int page_size = os::vm_page_size();
 638 
 639   Label after_frame_check;
 640 
 641   // compute rsp as if this were going to be the last frame on
 642   // the stack before the red zone
 643 
 644   Label after_frame_check_pop;
 645 
 646   // save rsi == caller's bytecode ptr (c++ previous interp. state)
 647   // QQQ problem here?? rsi overload????
 648   __ push(state);
 649 
 650   const Register thread = LP64_ONLY(r15_thread) NOT_LP64(rsi);
 651 
 652   NOT_LP64(__ get_thread(thread));
 653 
 654   const Address stack_base(thread, Thread::stack_base_offset());
 655   const Address stack_size(thread, Thread::stack_size_offset());
 656 
 657   // locals + overhead, in bytes
 658     const Address size_of_stack    (rbx, methodOopDesc::max_stack_offset());
 659     // Always give one monitor to allow us to start interp if sync method.
 660     // Any additional monitors need a check when moving the expression stack
 661     const one_monitor = frame::interpreter_frame_monitor_size() * wordSize;
 662   __ load_unsigned_word(rax, size_of_stack);                            // get size of expression stack in words
 663   __ lea(rax, Address(noreg, rax, Interpreter::stackElementScale(), one_monitor));
 664   __ lea(rax, Address(rax, rdx, Interpreter::stackElementScale(), overhead_size));
 665 
 666 #ifdef ASSERT
 667   Label stack_base_okay, stack_size_okay;
 668   // verify that thread stack base is non-zero
 669   __ cmpptr(stack_base, (int32_t)0);
 670   __ jcc(Assembler::notEqual, stack_base_okay);
 671   __ stop("stack base is zero");
 672   __ bind(stack_base_okay);
 673   // verify that thread stack size is non-zero
 674   __ cmpptr(stack_size, (int32_t)0);
 675   __ jcc(Assembler::notEqual, stack_size_okay);
 676   __ stop("stack size is zero");
 677   __ bind(stack_size_okay);
 678 #endif
 679 
 680   // Add stack base to locals and subtract stack size
 681   __ addptr(rax, stack_base);
 682   __ subptr(rax, stack_size);
 683 
 684   // We should have a magic number here for the size of the c++ interpreter frame.
 685   // We can't actually tell this ahead of time. The debug version size is around 3k
 686   // product is 1k and fastdebug is 4k
 687   const int slop = 6 * K;
 688 
 689   // Use the maximum number of pages we might bang.
 690   const int max_pages = StackShadowPages > (StackRedPages+StackYellowPages) ? StackShadowPages :
 691                                                                               (StackRedPages+StackYellowPages);
 692   // Only need this if we are stack banging which is temporary while
 693   // we're debugging.
 694   __ addptr(rax, slop + 2*max_pages * page_size);
 695 
 696   // check against the current stack bottom
 697   __ cmpptr(rsp, rax);
 698   __ jcc(Assembler::above, after_frame_check_pop);
 699 
 700   __ pop(state);  //  get c++ prev state.
 701 
 702      // throw exception return address becomes throwing pc
 703   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_StackOverflowError));
 704 
 705   // all done with frame size check
 706   __ bind(after_frame_check_pop);
 707   __ pop(state);
 708 
 709   __ bind(after_frame_check);
 710 }
 711 
 712 // Find preallocated  monitor and lock method (C++ interpreter)
 713 // rbx - methodOop
 714 //
 715 void InterpreterGenerator::lock_method(void) {
 716   // assumes state == rsi/r13 == pointer to current interpreterState
 717   // minimally destroys rax, rdx|c_rarg1, rdi
 718   //
 719   // synchronize method

 720   const int entry_size            = frame::interpreter_frame_monitor_size() * wordSize;
 721   const Address access_flags      (rbx, methodOopDesc::access_flags_offset());
 722 
 723   const Register monitor  = NOT_LP64(rdx) LP64_ONLY(c_rarg1);
 724 
 725   // find initial monitor i.e. monitors[-1]
 726   __ movptr(monitor, STATE(_monitor_base));                                   // get monitor bottom limit
 727   __ subptr(monitor, entry_size);                                             // point to initial monitor
 728 
 729 #ifdef ASSERT
 730   { Label L;
 731     __ movl(rax, access_flags);
 732     __ testl(rax, JVM_ACC_SYNCHRONIZED);
 733     __ jcc(Assembler::notZero, L);
 734     __ stop("method doesn't need synchronization");
 735     __ bind(L);
 736   }
 737 #endif // ASSERT
 738   // get synchronization object
 739   { Label done;
 740     const int mirror_offset = klassOopDesc::klass_part_offset_in_bytes() + Klass::java_mirror_offset_in_bytes();
 741     __ movl(rax, access_flags);
 742     __ movptr(rdi, STATE(_locals));                                     // prepare to get receiver (assume common case)
 743     __ testl(rax, JVM_ACC_STATIC);
 744     __ movptr(rax, Address(rdi, 0));                                    // get receiver (assume this is frequent case)
 745     __ jcc(Assembler::zero, done);
 746     __ movptr(rax, Address(rbx, methodOopDesc::constants_offset()));
 747     __ movptr(rax, Address(rax, constantPoolOopDesc::pool_holder_offset_in_bytes()));
 748     __ movptr(rax, Address(rax, mirror_offset));
 749     __ bind(done);
 750   }
 751 #ifdef ASSERT
 752   { Label L;
 753     __ cmpptr(rax, Address(monitor, BasicObjectLock::obj_offset_in_bytes()));   // correct object?
 754     __ jcc(Assembler::equal, L);
 755     __ stop("wrong synchronization lobject");
 756     __ bind(L);
 757   }
 758 #endif // ASSERT
 759   // can destroy rax, rdx|c_rarg1, rcx, and (via call_VM) rdi!
 760   __ lock_object(monitor);
 761 }
 762 
 763 // Call an accessor method (assuming it is resolved, otherwise drop into vanilla (slow path) entry
 764 
 765 address InterpreterGenerator::generate_accessor_entry(void) {
 766 
 767   // rbx: methodOop

 768 
 769   // rsi/r13: senderSP must preserved for slow path, set SP to it on fast path
 770 
 771   Label xreturn_path;
 772 
 773   // do fastpath for resolved accessor methods
 774   if (UseFastAccessorMethods) {
 775 
 776     address entry_point = __ pc();
 777 
 778     Label slow_path;
 779     // If we need a safepoint check, generate full interpreter entry.
 780     ExternalAddress state(SafepointSynchronize::address_of_state());
 781     __ cmp32(ExternalAddress(SafepointSynchronize::address_of_state()),
 782              SafepointSynchronize::_not_synchronized);
 783 
 784     __ jcc(Assembler::notEqual, slow_path);
 785     // ASM/C++ Interpreter
 786     // Code: _aload_0, _(i|a)getfield, _(i|a)return or any rewrites thereof; parameter size = 1
 787     // Note: We can only use this code if the getfield has been resolved
 788     //       and if we don't have a null-pointer exception => check for
 789     //       these conditions first and use slow path if necessary.
 790     // rbx,: method
 791     // rcx: receiver
 792     __ movptr(rax, Address(rsp, wordSize));
 793 
 794     // check if local 0 != NULL and read field
 795     __ testptr(rax, rax);
 796     __ jcc(Assembler::zero, slow_path);
 797 
 798     __ movptr(rdi, Address(rbx, methodOopDesc::constants_offset()));
 799     // read first instruction word and extract bytecode @ 1 and index @ 2
 800     __ movptr(rdx, Address(rbx, methodOopDesc::const_offset()));
 801     __ movl(rdx, Address(rdx, constMethodOopDesc::codes_offset()));
 802     // Shift codes right to get the index on the right.
 803     // The bytecode fetched looks like <index><0xb4><0x2a>
 804     __ shrl(rdx, 2*BitsPerByte);
 805     __ shll(rdx, exact_log2(in_words(ConstantPoolCacheEntry::size())));
 806     __ movptr(rdi, Address(rdi, constantPoolOopDesc::cache_offset_in_bytes()));
 807 
 808     // rax,: local 0
 809     // rbx,: method
 810     // rcx: receiver - do not destroy since it is needed for slow path!
 811     // rcx: scratch
 812     // rdx: constant pool cache index
 813     // rdi: constant pool cache
 814     // rsi/r13: sender sp
 815 
 816     // check if getfield has been resolved and read constant pool cache entry
 817     // check the validity of the cache entry by testing whether _indices field
 818     // contains Bytecode::_getfield in b1 byte.
 819     assert(in_words(ConstantPoolCacheEntry::size()) == 4, "adjust shift below");
 820     __ movl(rcx,
 821             Address(rdi,
 822                     rdx,
 823                     Address::times_ptr, constantPoolCacheOopDesc::base_offset() + ConstantPoolCacheEntry::indices_offset()));
 824     __ shrl(rcx, 2*BitsPerByte);
 825     __ andl(rcx, 0xFF);
 826     __ cmpl(rcx, Bytecodes::_getfield);
 827     __ jcc(Assembler::notEqual, slow_path);
 828 
 829     // Note: constant pool entry is not valid before bytecode is resolved
 830     __ movptr(rcx,
 831             Address(rdi,
 832                     rdx,
 833                     Address::times_ptr, constantPoolCacheOopDesc::base_offset() + ConstantPoolCacheEntry::f2_offset()));
 834     __ movl(rdx,
 835             Address(rdi,
 836                     rdx,
 837                     Address::times_ptr, constantPoolCacheOopDesc::base_offset() + ConstantPoolCacheEntry::flags_offset()));
 838 
 839     Label notByte, notShort, notChar;
 840     const Address field_address (rax, rcx, Address::times_1);
 841 
 842     // Need to differentiate between igetfield, agetfield, bgetfield etc.
 843     // because they are different sizes.
 844     // Use the type from the constant pool cache
 845     __ shrl(rdx, ConstantPoolCacheEntry::tosBits);
 846     // Make sure we don't need to mask rdx for tosBits after the above shift
 847     ConstantPoolCacheEntry::verify_tosBits();
 848 #ifdef _LP64
 849     Label notObj;
 850     __ cmpl(rdx, atos);
 851     __ jcc(Assembler::notEqual, notObj);
 852     // atos
 853     __ movptr(rax, field_address);
 854     __ jmp(xreturn_path);
 855 
 856     __ bind(notObj);
 857 #endif // _LP64
 858     __ cmpl(rdx, btos);
 859     __ jcc(Assembler::notEqual, notByte);
 860     __ load_signed_byte(rax, field_address);
 861     __ jmp(xreturn_path);
 862 
 863     __ bind(notByte);
 864     __ cmpl(rdx, stos);
 865     __ jcc(Assembler::notEqual, notShort);
 866     __ load_signed_word(rax, field_address);
 867     __ jmp(xreturn_path);
 868 
 869     __ bind(notShort);
 870     __ cmpl(rdx, ctos);
 871     __ jcc(Assembler::notEqual, notChar);
 872     __ load_unsigned_word(rax, field_address);
 873     __ jmp(xreturn_path);
 874 
 875     __ bind(notChar);
 876 #ifdef ASSERT
 877     Label okay;
 878 #ifndef _LP64
 879     __ cmpl(rdx, atos);
 880     __ jcc(Assembler::equal, okay);
 881 #endif // _LP64
 882     __ cmpl(rdx, itos);
 883     __ jcc(Assembler::equal, okay);
 884     __ stop("what type is this?");
 885     __ bind(okay);
 886 #endif // ASSERT
 887     // All the rest are a 32 bit wordsize
 888     __ movl(rax, field_address);
 889 
 890     __ bind(xreturn_path);
 891 
 892     // _ireturn/_areturn
 893     __ pop(rdi);                               // get return address
 894     __ mov(rsp, sender_sp_on_entry);           // set sp to sender sp
 895     __ jmp(rdi);
 896 
 897     // generate a vanilla interpreter entry as the slow path
 898     __ bind(slow_path);
 899     // We will enter c++ interpreter looking like it was
 900     // called by the call_stub this will cause it to return
 901     // a tosca result to the invoker which might have been
 902     // the c++ interpreter itself.
 903 
 904     __ jmp(fast_accessor_slow_entry_path);
 905     return entry_point;
 906 
 907   } else {
 908     return NULL;
 909   }
 910 
 911 }
 912 
 913 //
 914 // C++ Interpreter stub for calling a native method.
 915 // This sets up a somewhat different looking stack for calling the native method
 916 // than the typical interpreter frame setup but still has the pointer to
 917 // an interpreter state.
 918 //
 919 
 920 address InterpreterGenerator::generate_native_entry(bool synchronized) {
 921   // determine code generation flags
 922   bool inc_counter  = UseCompiler || CountCompiledCalls;
 923 
 924   // rbx: methodOop
 925   // rcx: receiver (unused)
 926   // rsi/r13: previous interpreter state (if called from C++ interpreter) must preserve
 927   //      in any case. If called via c1/c2/call_stub rsi/r13 is junk (to use) but harmless
 928   //      to save/restore.
 929   address entry_point = __ pc();
 930 
 931   const Address size_of_parameters(rbx, methodOopDesc::size_of_parameters_offset());
 932   const Address size_of_locals    (rbx, methodOopDesc::size_of_locals_offset());
 933   const Address invocation_counter(rbx, methodOopDesc::invocation_counter_offset() + InvocationCounter::counter_offset());
 934   const Address access_flags      (rbx, methodOopDesc::access_flags_offset());
 935 
 936   // rsi/r13 == state/locals rdi == prevstate

 937   const Register locals = rdi;
 938 
 939   // get parameter size (always needed)
 940   __ load_unsigned_word(rcx, size_of_parameters);
 941 
 942   // rbx: methodOop
 943   // rcx: size of parameters
 944   __ pop(rax);                                       // get return address
 945   // for natives the size of locals is zero
 946 
 947   // compute beginning of parameters /locals
 948   __ lea(locals, Address(rsp, rcx, Address::times_ptr, -wordSize));
 949 
 950   // initialize fixed part of activation frame
 951 
 952   // Assumes rax = return address
 953 
 954   // allocate and initialize new interpreterState and method expression stack
 955   // IN(locals) ->  locals
 956   // IN(state) -> previous frame manager state (NULL from stub/c1/c2)
 957   // destroys rax, rcx, rdx
 958   // OUT (state) -> new interpreterState
 959   // OUT(rsp) -> bottom of methods expression stack
 960 
 961   // save sender_sp
 962   __ mov(rcx, sender_sp_on_entry);
 963   // start with NULL previous state
 964   __ movptr(state, (int32_t)NULL_WORD);
 965   generate_compute_interpreter_state(state, locals, rcx, true);
 966 
 967 #ifdef ASSERT
 968   { Label L;
 969     __ movptr(rax, STATE(_stack_base));
 970 #ifdef _LP64
 971     // duplicate the alignment rsp got after setting stack_base
 972     __ subptr(rax, frame::arg_reg_save_area_bytes); // windows
 973     __ andptr(rax, -16); // must be 16 byte boundry (see amd64 ABI)
 974 #endif // _LP64
 975     __ cmpptr(rax, rsp);
 976     __ jcc(Assembler::equal, L);
 977     __ stop("broken stack frame setup in interpreter");
 978     __ bind(L);
 979   }
 980 #endif
 981 
 982   if (inc_counter) __ movl(rcx, invocation_counter);  // (pre-)fetch invocation count
 983 
 984   const Register unlock_thread = LP64_ONLY(r15_thread) NOT_LP64(rax);
 985   NOT_LP64(__ movptr(unlock_thread, STATE(_thread));) // get thread
 986   // Since at this point in the method invocation the exception handler
 987   // would try to exit the monitor of synchronized methods which hasn't
 988   // been entered yet, we set the thread local variable
 989   // _do_not_unlock_if_synchronized to true. The remove_activation will
 990   // check this flag.
 991 
 992   const Address do_not_unlock_if_synchronized(unlock_thread,
 993         in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
 994   __ movbool(do_not_unlock_if_synchronized, true);
 995 
 996   // make sure method is native & not abstract
 997 #ifdef ASSERT
 998   __ movl(rax, access_flags);
 999   {
1000     Label L;
1001     __ testl(rax, JVM_ACC_NATIVE);
1002     __ jcc(Assembler::notZero, L);
1003     __ stop("tried to execute non-native method as native");
1004     __ bind(L);
1005   }
1006   { Label L;
1007     __ testl(rax, JVM_ACC_ABSTRACT);
1008     __ jcc(Assembler::zero, L);
1009     __ stop("tried to execute abstract method in interpreter");
1010     __ bind(L);
1011   }
1012 #endif
1013 
1014 
1015   // increment invocation count & check for overflow
1016   Label invocation_counter_overflow;
1017   if (inc_counter) {
1018     generate_counter_incr(&invocation_counter_overflow, NULL, NULL);
1019   }
1020 
1021   Label continue_after_compile;
1022 
1023   __ bind(continue_after_compile);
1024 
1025   bang_stack_shadow_pages(true);
1026 
1027   // reset the _do_not_unlock_if_synchronized flag
1028   NOT_LP64(__ movl(rax, STATE(_thread));)                       // get thread
1029   __ movbool(do_not_unlock_if_synchronized, false);
1030 
1031 
1032   // check for synchronized native methods
1033   //
1034   // Note: This must happen *after* invocation counter check, since
1035   //       when overflow happens, the method should not be locked.
1036   if (synchronized) {
1037     // potentially kills rax, rcx, rdx, rdi
1038     lock_method();
1039   } else {
1040     // no synchronization necessary
1041 #ifdef ASSERT
1042       { Label L;
1043         __ movl(rax, access_flags);
1044         __ testl(rax, JVM_ACC_SYNCHRONIZED);
1045         __ jcc(Assembler::zero, L);
1046         __ stop("method needs synchronization");
1047         __ bind(L);
1048       }
1049 #endif
1050   }
1051 
1052   // start execution
1053 
1054   // jvmti support
1055   __ notify_method_entry();
1056 
1057   // work registers
1058   const Register method = rbx;
1059   const Register thread = LP64_ONLY(r15_thread) NOT_LP64(rdi);
1060   const Register t      = InterpreterRuntime::SignatureHandlerGenerator::temp();    // rcx|rscratch1
1061 
1062   // allocate space for parameters
1063   __ movptr(method, STATE(_method));
1064   __ verify_oop(method);
1065   __ load_unsigned_word(t, Address(method, methodOopDesc::size_of_parameters_offset()));
1066   __ shll(t, 2);
1067 #ifdef _LP64
1068   __ subptr(rsp, t);
1069   __ subptr(rsp, frame::arg_reg_save_area_bytes); // windows
1070   __ andptr(rsp, -16); // must be 16 byte boundry (see amd64 ABI)
1071 #else
1072   __ addptr(t, 2*wordSize);     // allocate two more slots for JNIEnv and possible mirror
1073   __ subptr(rsp, t);
1074   __ andptr(rsp, -(StackAlignmentInBytes)); // gcc needs 16 byte aligned stacks to do XMM intrinsics
1075 #endif // _LP64
1076 
1077   // get signature handler
1078     Label pending_exception_present;
1079 
1080   { Label L;
1081     __ movptr(t, Address(method, methodOopDesc::signature_handler_offset()));
1082     __ testptr(t, t);
1083     __ jcc(Assembler::notZero, L);
1084     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::prepare_native_call), method, false);
1085     __ movptr(method, STATE(_method));
1086     __ cmpptr(Address(thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
1087     __ jcc(Assembler::notEqual, pending_exception_present);
1088     __ verify_oop(method);
1089     __ movptr(t, Address(method, methodOopDesc::signature_handler_offset()));
1090     __ bind(L);
1091   }
1092 #ifdef ASSERT
1093   {
1094     Label L;
1095     __ push(t);
1096     __ get_thread(t);                                   // get vm's javathread*
1097     __ cmpptr(t, STATE(_thread));
1098     __ jcc(Assembler::equal, L);
1099     __ int3();
1100     __ bind(L);
1101     __ pop(t);
1102   }
1103 #endif //
1104 
1105   const Register from_ptr = InterpreterRuntime::SignatureHandlerGenerator::from();
1106   // call signature handler

1107   assert(InterpreterRuntime::SignatureHandlerGenerator::to  () == rsp, "adjust this code");
1108 
1109   // The generated handlers do not touch RBX (the method oop).
1110   // However, large signatures cannot be cached and are generated
1111   // each time here.  The slow-path generator will blow RBX
1112   // sometime, so we must reload it after the call.
1113   __ movptr(from_ptr, STATE(_locals));  // get the from pointer
1114   __ call(t);
1115   __ movptr(method, STATE(_method));
1116   __ verify_oop(method);
1117 
1118   // result handler is in rax
1119   // set result handler
1120   __ movptr(STATE(_result_handler), rax);
1121 
1122 
1123   // get native function entry point
1124   { Label L;
1125     __ movptr(rax, Address(method, methodOopDesc::native_function_offset()));
1126     __ testptr(rax, rax);
1127     __ jcc(Assembler::notZero, L);
1128     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::prepare_native_call), method);
1129     __ movptr(method, STATE(_method));
1130     __ verify_oop(method);
1131     __ movptr(rax, Address(method, methodOopDesc::native_function_offset()));
1132     __ bind(L);
1133   }
1134 
1135   // pass mirror handle if static call
1136   { Label L;
1137     const int mirror_offset = klassOopDesc::klass_part_offset_in_bytes() + Klass::java_mirror_offset_in_bytes();
1138     __ movl(t, Address(method, methodOopDesc::access_flags_offset()));
1139     __ testl(t, JVM_ACC_STATIC);
1140     __ jcc(Assembler::zero, L);
1141     // get mirror
1142     __ movptr(t, Address(method, methodOopDesc:: constants_offset()));
1143     __ movptr(t, Address(t, constantPoolOopDesc::pool_holder_offset_in_bytes()));
1144     __ movptr(t, Address(t, mirror_offset));
1145     // copy mirror into activation object
1146     __ movptr(STATE(_oop_temp), t);
1147     // pass handle to mirror
1148 #ifdef _LP64
1149     __ lea(c_rarg1, STATE(_oop_temp));
1150 #else
1151     __ lea(t, STATE(_oop_temp));
1152     __ movptr(Address(rsp, wordSize), t);
1153 #endif // _LP64
1154     __ bind(L);
1155   }
1156 #ifdef ASSERT
1157   {
1158     Label L;
1159     __ push(t);
1160     __ get_thread(t);                                   // get vm's javathread*
1161     __ cmpptr(t, STATE(_thread));
1162     __ jcc(Assembler::equal, L);
1163     __ int3();
1164     __ bind(L);
1165     __ pop(t);
1166   }
1167 #endif //
1168 












1169   // pass JNIEnv
1170 #ifdef _LP64
1171   __ lea(c_rarg0, Address(thread, JavaThread::jni_environment_offset()));
1172 #else
1173   __ movptr(thread, STATE(_thread));          // get thread
1174   __ lea(t, Address(thread, JavaThread::jni_environment_offset()));
1175 
1176   __ movptr(Address(rsp, 0), t);
1177 #endif // _LP64
1178 
1179 #ifdef ASSERT
1180   {
1181     Label L;
1182     __ push(t);
1183     __ get_thread(t);                                   // get vm's javathread*
1184     __ cmpptr(t, STATE(_thread));
1185     __ jcc(Assembler::equal, L);
1186     __ int3();
1187     __ bind(L);
1188     __ pop(t);
1189   }
1190 #endif //
1191 
1192 #ifdef ASSERT
1193   { Label L;
1194     __ movl(t, Address(thread, JavaThread::thread_state_offset()));
1195     __ cmpl(t, _thread_in_Java);
1196     __ jcc(Assembler::equal, L);
1197     __ stop("Wrong thread state in native stub");
1198     __ bind(L);
1199   }
1200 #endif
1201 
1202   // Change state to native (we save the return address in the thread, since it might not
1203   // be pushed on the stack when we do a a stack traversal). It is enough that the pc()
1204   // points into the right code segment. It does not have to be the correct return pc.
1205 
1206   __ set_last_Java_frame(thread, noreg, rbp, __ pc());
1207 
1208   __ movl(Address(thread, JavaThread::thread_state_offset()), _thread_in_native);
1209 
1210   __ call(rax);
1211 
1212   // result potentially in rdx:rax or ST0
1213   __ movptr(method, STATE(_method));
1214   NOT_LP64(__ movptr(thread, STATE(_thread));)                  // get thread
1215 
1216   // The potential result is in ST(0) & rdx:rax
1217   // With C++ interpreter we leave any possible result in ST(0) until we are in result handler and then
1218   // we do the appropriate stuff for returning the result. rdx:rax must always be saved because just about
1219   // anything we do here will destroy it, st(0) is only saved if we re-enter the vm where it would
1220   // be destroyed.
1221   // It is safe to do these pushes because state is _thread_in_native and return address will be found
1222   // via _last_native_pc and not via _last_jave_sp
1223 
1224     // Must save the value of ST(0)/xmm0 since it could be destroyed before we get to result handler
1225     { Label Lpush, Lskip;
1226       ExternalAddress float_handler(AbstractInterpreter::result_handler(T_FLOAT));
1227       ExternalAddress double_handler(AbstractInterpreter::result_handler(T_DOUBLE));
1228       __ cmpptr(STATE(_result_handler), float_handler.addr());
1229       __ jcc(Assembler::equal, Lpush);
1230       __ cmpptr(STATE(_result_handler), double_handler.addr());
1231       __ jcc(Assembler::notEqual, Lskip);
1232       __ bind(Lpush);
1233       __ subptr(rsp, 2*wordSize);
1234       if ( UseSSE < 2 ) {
1235         __ fstp_d(Address(rsp, 0));
1236       } else {
1237         __ movdbl(Address(rsp, 0), xmm0);
1238       }
1239       __ bind(Lskip);
1240     }
1241 
1242   // save rax:rdx for potential use by result handler.
1243   __ push(rax);
1244 #ifndef _LP64
1245   __ push(rdx);
1246 #endif // _LP64
1247 
1248   // Either restore the MXCSR register after returning from the JNI Call
1249   // or verify that it wasn't changed.
1250   if (VM_Version::supports_sse()) {
1251     if (RestoreMXCSROnJNICalls) {
1252       __ ldmxcsr(ExternalAddress(StubRoutines::addr_mxcsr_std()));
1253     }
1254     else if (CheckJNICalls ) {
1255       __ call(RuntimeAddress(StubRoutines::x86::verify_mxcsr_entry()));
1256     }
1257   }
1258 
1259 #ifndef _LP64
1260   // Either restore the x87 floating pointer control word after returning
1261   // from the JNI call or verify that it wasn't changed.
1262   if (CheckJNICalls) {
1263     __ call(RuntimeAddress(StubRoutines::x86::verify_fpu_cntrl_wrd_entry()));
1264   }
1265 #endif // _LP64
1266 
1267 
1268   // change thread state
1269   __ movl(Address(thread, JavaThread::thread_state_offset()), _thread_in_native_trans);
1270   if(os::is_MP()) {
1271     // Write serialization page so VM thread can do a pseudo remote membar.
1272     // We use the current thread pointer to calculate a thread specific
1273     // offset to write to within the page. This minimizes bus traffic
1274     // due to cache line collision.
1275     __ serialize_memory(thread, rcx);
1276   }
1277 
1278   // check for safepoint operation in progress and/or pending suspend requests
1279   { Label Continue;
1280 
1281     __ cmp32(ExternalAddress(SafepointSynchronize::address_of_state()),
1282              SafepointSynchronize::_not_synchronized);
1283 
1284     // threads running native code and they are expected to self-suspend
1285     // when leaving the _thread_in_native state. We need to check for
1286     // pending suspend requests here.
1287     Label L;
1288     __ jcc(Assembler::notEqual, L);
1289     __ cmpl(Address(thread, JavaThread::suspend_flags_offset()), 0);
1290     __ jcc(Assembler::equal, Continue);
1291     __ bind(L);
1292 
1293     // Don't use call_VM as it will see a possible pending exception and forward it
1294     // and never return here preventing us from clearing _last_native_pc down below.
1295     // Also can't use call_VM_leaf either as it will check to see if rsi & rdi are
1296     // preserved and correspond to the bcp/locals pointers.

1297     //
1298 
1299     ((MacroAssembler*)_masm)->call_VM_leaf(CAST_FROM_FN_PTR(address, JavaThread::check_special_condition_for_native_trans),
1300                           thread);
1301     __ increment(rsp, wordSize);
1302 
1303     __ movptr(method, STATE(_method));
1304     __ verify_oop(method);
1305     __ movptr(thread, STATE(_thread));                       // get thread
1306 
1307     __ bind(Continue);
1308   }
1309 
1310   // change thread state
1311   __ movl(Address(thread, JavaThread::thread_state_offset()), _thread_in_Java);
1312 
1313   __ reset_last_Java_frame(thread, true, true);
1314 
1315   // reset handle block
1316   __ movptr(t, Address(thread, JavaThread::active_handles_offset()));
1317   __ movptr(Address(t, JNIHandleBlock::top_offset_in_bytes()), (int32_t)NULL_WORD);
1318 
1319   // If result was an oop then unbox and save it in the frame
1320   { Label L;
1321     Label no_oop, store_result;
1322       ExternalAddress oop_handler(AbstractInterpreter::result_handler(T_OBJECT));
1323     __ cmpptr(STATE(_result_handler), oop_handler.addr());
1324     __ jcc(Assembler::notEqual, no_oop);
1325 #ifndef _LP64
1326     __ pop(rdx);
1327 #endif // _LP64
1328     __ pop(rax);
1329     __ testptr(rax, rax);
1330     __ jcc(Assembler::zero, store_result);
1331     // unbox
1332     __ movptr(rax, Address(rax, 0));
1333     __ bind(store_result);
1334     __ movptr(STATE(_oop_temp), rax);
1335     // keep stack depth as expected by pushing oop which will eventually be discarded
1336     __ push(rax);
1337 #ifndef _LP64
1338     __ push(rdx);
1339 #endif // _LP64
1340     __ bind(no_oop);
1341   }
1342 
1343   {
1344      Label no_reguard;
1345      __ cmpl(Address(thread, JavaThread::stack_guard_state_offset()), JavaThread::stack_guard_yellow_disabled);
1346      __ jcc(Assembler::notEqual, no_reguard);
1347 
1348      __ pusha();
1349      __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages)));
1350      __ popa();
1351 
1352      __ bind(no_reguard);
1353    }
1354 
1355 
1356   // QQQ Seems like for native methods we simply return and the caller will see the pending
1357   // exception and do the right thing. Certainly the interpreter will, don't know about
1358   // compiled methods.
1359   // Seems that the answer to above is no this is wrong. The old code would see the exception
1360   // and forward it before doing the unlocking and notifying jvmdi that method has exited.
1361   // This seems wrong need to investigate the spec.
1362 
1363   // handle exceptions (exception handling will handle unlocking!)
1364   { Label L;
1365     __ cmpptr(Address(thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
1366     __ jcc(Assembler::zero, L);
1367     __ bind(pending_exception_present);
1368 
1369     // There are potential results on the stack (rax/rdx, ST(0)) we ignore these and simply
1370     // return and let caller deal with exception. This skips the unlocking here which
1371     // seems wrong but seems to be what asm interpreter did. Can't find this in the spec.
1372     // Note: must preverve method in rbx
1373     //
1374 
1375     // remove activation
1376 
1377     __ movptr(t, STATE(_sender_sp));
1378     __ leave();                                  // remove frame anchor
1379     __ pop(rdi);                                 // get return address
1380     __ movptr(state, STATE(_prev_link));         // get previous state for return
1381     __ mov(rsp, t);                              // set sp to sender sp
1382     __ push(rdi);                                // push throwing pc
1383     // The skips unlocking!! This seems to be what asm interpreter does but seems
1384     // very wrong. Not clear if this violates the spec.
1385     __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
1386     __ bind(L);
1387   }
1388 
1389   // do unlocking if necessary
1390   { Label L;
1391     __ movl(t, Address(method, methodOopDesc::access_flags_offset()));
1392     __ testl(t, JVM_ACC_SYNCHRONIZED);
1393     __ jcc(Assembler::zero, L);
1394     // the code below should be shared with interpreter macro assembler implementation
1395     { Label unlock;
1396     const Register monitor = NOT_LP64(rdx) LP64_ONLY(c_rarg1);
1397       // BasicObjectLock will be first in list, since this is a synchronized method. However, need
1398       // to check that the object has not been unlocked by an explicit monitorexit bytecode.
1399       __ movptr(monitor, STATE(_monitor_base));
1400       __ subptr(monitor, frame::interpreter_frame_monitor_size() * wordSize);  // address of initial monitor
1401 
1402       __ movptr(t, Address(monitor, BasicObjectLock::obj_offset_in_bytes()));
1403       __ testptr(t, t);
1404       __ jcc(Assembler::notZero, unlock);
1405 
1406       // Entry already unlocked, need to throw exception
1407       __ MacroAssembler::call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_illegal_monitor_state_exception));
1408       __ should_not_reach_here();
1409 
1410       __ bind(unlock);
1411       __ unlock_object(monitor);
1412       // unlock can blow rbx so restore it for path that needs it below
1413       __ movptr(method, STATE(_method));
1414     }
1415     __ bind(L);
1416   }
1417 
1418   // jvmti support
1419   // Note: This must happen _after_ handling/throwing any exceptions since
1420   //       the exception handler code notifies the runtime of method exits
1421   //       too. If this happens before, method entry/exit notifications are
1422   //       not properly paired (was bug - gri 11/22/99).
1423   __ notify_method_exit(vtos, InterpreterMacroAssembler::NotifyJVMTI);
1424 
1425   // restore potential result in rdx:rax, call result handler to restore potential result in ST0 & handle result
1426 #ifndef _LP64
1427   __ pop(rdx);
1428 #endif // _LP64
1429   __ pop(rax);
1430   __ movptr(t, STATE(_result_handler));       // get result handler
1431   __ call(t);                                 // call result handler to convert to tosca form
1432 
1433   // remove activation
1434 
1435   __ movptr(t, STATE(_sender_sp));
1436 
1437   __ leave();                                  // remove frame anchor
1438   __ pop(rdi);                                 // get return address
1439   __ movptr(state, STATE(_prev_link));         // get previous state for return (if c++ interpreter was caller)
1440   __ mov(rsp, t);                              // set sp to sender sp
1441   __ jmp(rdi);
1442 
1443   // invocation counter overflow
1444   if (inc_counter) {
1445     // Handle overflow of counter and compile method
1446     __ bind(invocation_counter_overflow);
1447     generate_counter_overflow(&continue_after_compile);
1448   }
1449 
1450   return entry_point;
1451 }
1452 
1453 // Generate entries that will put a result type index into rcx
1454 void CppInterpreterGenerator::generate_deopt_handling() {
1455 

1456   Label return_from_deopt_common;
1457 
1458   // Generate entries that will put a result type index into rcx
1459   // deopt needs to jump to here to enter the interpreter (return a result)
1460   deopt_frame_manager_return_atos  = __ pc();
1461 
1462   // rax is live here
1463   __ movl(rcx, AbstractInterpreter::BasicType_as_index(T_OBJECT));    // Result stub address array index
1464   __ jmp(return_from_deopt_common);
1465 
1466 
1467   // deopt needs to jump to here to enter the interpreter (return a result)
1468   deopt_frame_manager_return_btos  = __ pc();
1469 
1470   // rax is live here
1471   __ movl(rcx, AbstractInterpreter::BasicType_as_index(T_BOOLEAN));    // Result stub address array index
1472   __ jmp(return_from_deopt_common);
1473 
1474   // deopt needs to jump to here to enter the interpreter (return a result)
1475   deopt_frame_manager_return_itos  = __ pc();


1502   // deopt needs to jump to here to enter the interpreter (return a result)
1503   deopt_frame_manager_return_vtos  = __ pc();
1504 
1505   __ movl(rcx, AbstractInterpreter::BasicType_as_index(T_VOID));
1506 
1507   // Deopt return common
1508   // an index is present in rcx that lets us move any possible result being
1509   // return to the interpreter's stack
1510   //
1511   // Because we have a full sized interpreter frame on the youngest
1512   // activation the stack is pushed too deep to share the tosca to
1513   // stack converters directly. We shrink the stack to the desired
1514   // amount and then push result and then re-extend the stack.
1515   // We could have the code in size_activation layout a short
1516   // frame for the top activation but that would look different
1517   // than say sparc (which needs a full size activation because
1518   // the windows are in the way. Really it could be short? QQQ
1519   //
1520   __ bind(return_from_deopt_common);
1521 
1522   __ lea(state, Address(rbp, -(int)sizeof(BytecodeInterpreter)));
1523 
1524   // setup rsp so we can push the "result" as needed.
1525   __ movptr(rsp, STATE(_stack));                                   // trim stack (is prepushed)
1526   __ addptr(rsp, wordSize);                                        // undo prepush
1527 
1528   ExternalAddress tosca_to_stack((address)CppInterpreter::_tosca_to_stack);
1529   // Address index(noreg, rcx, Address::times_ptr);
1530   __ movptr(rcx, ArrayAddress(tosca_to_stack, Address(noreg, rcx, Address::times_ptr)));
1531   // __ movl(rcx, Address(noreg, rcx, Address::times_ptr, int(AbstractInterpreter::_tosca_to_stack)));
1532   __ call(rcx);                                                   // call result converter
1533 
1534   __ movl(STATE(_msg), (int)BytecodeInterpreter::deopt_resume);
1535   __ lea(rsp, Address(rsp, -wordSize));                            // prepush stack (result if any already present)
1536   __ movptr(STATE(_stack), rsp);                                   // inform interpreter of new stack depth (parameters removed,
1537                                                                    // result if any on stack already )
1538   __ movptr(rsp, STATE(_stack_limit));                             // restore expression stack to full depth
1539 }
1540 
1541 // Generate the code to handle a more_monitors message from the c++ interpreter
1542 void CppInterpreterGenerator::generate_more_monitors() {
1543 

1544 
1545   Label entry, loop;
1546   const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
1547   // 1. compute new pointers                     // rsp: old expression stack top
1548   __ movptr(rdx, STATE(_stack_base));            // rdx: old expression stack bottom
1549   __ subptr(rsp, entry_size);                    // move expression stack top limit
1550   __ subptr(STATE(_stack), entry_size);          // update interpreter stack top
1551   __ subptr(STATE(_stack_limit), entry_size);    // inform interpreter
1552   __ subptr(rdx, entry_size);                    // move expression stack bottom
1553   __ movptr(STATE(_stack_base), rdx);            // inform interpreter
1554   __ movptr(rcx, STATE(_stack));                 // set start value for copy loop
1555   __ jmp(entry);
1556   // 2. move expression stack contents
1557   __ bind(loop);
1558   __ movptr(rbx, Address(rcx, entry_size));      // load expression stack word from old location
1559   __ movptr(Address(rcx, 0), rbx);               // and store it at new location
1560   __ addptr(rcx, wordSize);                      // advance to next word
1561   __ bind(entry);
1562   __ cmpptr(rcx, rdx);                           // check if bottom reached
1563   __ jcc(Assembler::notEqual, loop);             // if not at bottom then copy next word
1564   // now zero the slot so we can find it.
1565   __ movptr(Address(rdx, BasicObjectLock::obj_offset_in_bytes()), (int32_t) NULL_WORD);
1566   __ movl(STATE(_msg), (int)BytecodeInterpreter::got_monitors);
1567 }
1568 
1569 
1570 // Initial entry to C++ interpreter from the call_stub.
1571 // This entry point is called the frame manager since it handles the generation
1572 // of interpreter activation frames via requests directly from the vm (via call_stub)
1573 // and via requests from the interpreter. The requests from the call_stub happen
1574 // directly thru the entry point. Requests from the interpreter happen via returning
1575 // from the interpreter and examining the message the interpreter has returned to
1576 // the frame manager. The frame manager can take the following requests:
1577 
1578 // NO_REQUEST - error, should never happen.
1579 // MORE_MONITORS - need a new monitor. Shuffle the expression stack on down and
1580 //                 allocate a new monitor.
1581 // CALL_METHOD - setup a new activation to call a new method. Very similar to what
1582 //               happens during entry during the entry via the call stub.
1583 // RETURN_FROM_METHOD - remove an activation. Return to interpreter or call stub.
1584 //
1585 // Arguments:
1586 //
1587 // rbx: methodOop
1588 // rcx: receiver - unused (retrieved from stack as needed)
1589 // rsi/r13: previous frame manager state (NULL from the call_stub/c1/c2)
1590 //
1591 //
1592 // Stack layout at entry
1593 //
1594 // [ return address     ] <--- rsp
1595 // [ parameter n        ]
1596 //   ...
1597 // [ parameter 1        ]
1598 // [ expression stack   ]
1599 //
1600 //
1601 // We are free to blow any registers we like because the call_stub which brought us here
1602 // initially has preserved the callee save registers already.
1603 //
1604 //
1605 
1606 static address interpreter_frame_manager = NULL;
1607 
1608 address InterpreterGenerator::generate_normal_entry(bool synchronized) {
1609 
1610   // rbx: methodOop
1611   // rsi/r13: sender sp
1612 
1613   // Because we redispatch "recursive" interpreter entries thru this same entry point
1614   // the "input" register usage is a little strange and not what you expect coming
1615   // from the call_stub. From the call stub rsi/rdi (current/previous) interpreter
1616   // state are NULL but on "recursive" dispatches they are what you'd expect.
1617   // rsi: current interpreter state (C++ interpreter) must preserve (null from call_stub/c1/c2)
1618 
1619 
1620   // A single frame manager is plenty as we don't specialize for synchronized. We could and
1621   // the code is pretty much ready. Would need to change the test below and for good measure
1622   // modify generate_interpreter_state to only do the (pre) sync stuff stuff for synchronized
1623   // routines. Not clear this is worth it yet.
1624 
1625   if (interpreter_frame_manager) return interpreter_frame_manager;
1626 
1627   address entry_point = __ pc();
1628 
1629   // Fast accessor methods share this entry point.
1630   // This works because frame manager is in the same codelet
1631   if (UseFastAccessorMethods && !synchronized) __ bind(fast_accessor_slow_entry_path);
1632 
1633   Label dispatch_entry_2;
1634   __ movptr(rcx, sender_sp_on_entry);
1635   __ movptr(state, (int32_t)NULL_WORD);                              // no current activation
1636 
1637   __ jmp(dispatch_entry_2);
1638 

1639   const Register locals  = rdi;
1640 
1641   Label re_dispatch;
1642 
1643   __ bind(re_dispatch);
1644 
1645   // save sender sp (doesn't include return address
1646   __ lea(rcx, Address(rsp, wordSize));
1647 
1648   __ bind(dispatch_entry_2);
1649 
1650   // save sender sp
1651   __ push(rcx);
1652 
1653   const Address size_of_parameters(rbx, methodOopDesc::size_of_parameters_offset());
1654   const Address size_of_locals    (rbx, methodOopDesc::size_of_locals_offset());
1655   const Address access_flags      (rbx, methodOopDesc::access_flags_offset());
1656 
1657   // const Address monitor_block_top (rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
1658   // const Address monitor_block_bot (rbp, frame::interpreter_frame_initial_sp_offset        * wordSize);
1659   // const Address monitor(rbp, frame::interpreter_frame_initial_sp_offset * wordSize - (int)sizeof(BasicObjectLock));
1660 
1661   // get parameter size (always needed)
1662   __ load_unsigned_word(rcx, size_of_parameters);
1663 
1664   // rbx: methodOop
1665   // rcx: size of parameters
1666   __ load_unsigned_word(rdx, size_of_locals);                      // get size of locals in words
1667 
1668   __ subptr(rdx, rcx);                                             // rdx = no. of additional locals
1669 
1670   // see if we've got enough room on the stack for locals plus overhead.
1671   generate_stack_overflow_check();                                 // C++
1672 
1673   // c++ interpreter does not use stack banging or any implicit exceptions
1674   // leave for now to verify that check is proper.
1675   bang_stack_shadow_pages(false);
1676 
1677 
1678 
1679   // compute beginning of parameters (rdi)
1680   __ lea(locals, Address(rsp, rcx, Address::times_ptr, wordSize));
1681 
1682   // save sender's sp
1683   // __ movl(rcx, rsp);
1684 
1685   // get sender's sp
1686   __ pop(rcx);
1687 
1688   // get return address
1689   __ pop(rax);
1690 
1691   // rdx - # of additional locals
1692   // allocate space for locals
1693   // explicitly initialize locals
1694   {
1695     Label exit, loop;
1696     __ testl(rdx, rdx);                               // (32bit ok)
1697     __ jcc(Assembler::lessEqual, exit);               // do nothing if rdx <= 0
1698     __ bind(loop);
1699     __ push((int32_t)NULL_WORD);                      // initialize local variables
1700     __ decrement(rdx);                                // until everything initialized
1701     __ jcc(Assembler::greater, loop);
1702     __ bind(exit);
1703   }
1704 
1705 
1706   // Assumes rax = return address
1707 
1708   // allocate and initialize new interpreterState and method expression stack
1709   // IN(locals) ->  locals
1710   // IN(state) -> any current interpreter activation
1711   // destroys rax, rcx, rdx, rdi
1712   // OUT (state) -> new interpreterState
1713   // OUT(rsp) -> bottom of methods expression stack
1714 
1715   generate_compute_interpreter_state(state, locals, rcx, false);
1716 
1717   // Call interpreter
1718 
1719   Label call_interpreter;
1720   __ bind(call_interpreter);
1721 
1722   // c++ interpreter does not use stack banging or any implicit exceptions
1723   // leave for now to verify that check is proper.
1724   bang_stack_shadow_pages(false);
1725 
1726 
1727   // Call interpreter enter here if message is
1728   // set and we know stack size is valid
1729 
1730   Label call_interpreter_2;
1731 
1732   __ bind(call_interpreter_2);
1733 
1734   {
1735     const Register thread  = NOT_LP64(rcx) LP64_ONLY(r15_thread);
1736 
1737 #ifdef _LP64
1738     __ mov(c_rarg0, state);
1739 #else
1740     __ push(state);                                                 // push arg to interpreter
1741     __ movptr(thread, STATE(_thread));
1742 #endif // _LP64
1743 
1744     // We can setup the frame anchor with everything we want at this point
1745     // as we are thread_in_Java and no safepoints can occur until we go to
1746     // vm mode. We do have to clear flags on return from vm but that is it
1747     //
1748     __ movptr(Address(thread, JavaThread::last_Java_fp_offset()), rbp);
1749     __ movptr(Address(thread, JavaThread::last_Java_sp_offset()), rsp);
1750 
1751     // Call the interpreter
1752 
1753     RuntimeAddress normal(CAST_FROM_FN_PTR(address, BytecodeInterpreter::run));
1754     RuntimeAddress checking(CAST_FROM_FN_PTR(address, BytecodeInterpreter::runWithChecks));
1755 
1756     __ call(JvmtiExport::can_post_interpreter_events() ? checking : normal);
1757     NOT_LP64(__ pop(rax);)                                          // discard parameter to run
1758     //
1759     // state is preserved since it is callee saved
1760     //
1761 
1762     // reset_last_Java_frame
1763 
1764     NOT_LP64(__ movl(thread, STATE(_thread));)
1765     __ reset_last_Java_frame(thread, true, true);
1766   }
1767 
1768   // examine msg from interpreter to determine next action
1769 
1770   __ movl(rdx, STATE(_msg));                                       // Get new message
1771 
1772   Label call_method;
1773   Label return_from_interpreted_method;
1774   Label throw_exception;
1775   Label bad_msg;
1776   Label do_OSR;
1777 
1778   __ cmpl(rdx, (int32_t)BytecodeInterpreter::call_method);
1779   __ jcc(Assembler::equal, call_method);
1780   __ cmpl(rdx, (int32_t)BytecodeInterpreter::return_from_method);
1781   __ jcc(Assembler::equal, return_from_interpreted_method);
1782   __ cmpl(rdx, (int32_t)BytecodeInterpreter::do_osr);
1783   __ jcc(Assembler::equal, do_OSR);
1784   __ cmpl(rdx, (int32_t)BytecodeInterpreter::throwing_exception);
1785   __ jcc(Assembler::equal, throw_exception);
1786   __ cmpl(rdx, (int32_t)BytecodeInterpreter::more_monitors);
1787   __ jcc(Assembler::notEqual, bad_msg);
1788 
1789   // Allocate more monitor space, shuffle expression stack....
1790 
1791   generate_more_monitors();
1792 
1793   __ jmp(call_interpreter);
1794 
1795   // uncommon trap needs to jump to here to enter the interpreter (re-execute current bytecode)
1796   unctrap_frame_manager_entry  = __ pc();
1797   //
1798   // Load the registers we need.
1799   __ lea(state, Address(rbp, -(int)sizeof(BytecodeInterpreter)));
1800   __ movptr(rsp, STATE(_stack_limit));                             // restore expression stack to full depth
1801   __ jmp(call_interpreter_2);
1802 
1803 
1804 
1805   //=============================================================================
1806   // Returning from a compiled method into a deopted method. The bytecode at the
1807   // bcp has completed. The result of the bytecode is in the native abi (the tosca
1808   // for the template based interpreter). Any stack space that was used by the
1809   // bytecode that has completed has been removed (e.g. parameters for an invoke)
1810   // so all that we have to do is place any pending result on the expression stack
1811   // and resume execution on the next bytecode.
1812 
1813 
1814   generate_deopt_handling();
1815   __ jmp(call_interpreter);
1816 
1817 
1818   // Current frame has caught an exception we need to dispatch to the
1819   // handler. We can get here because a native interpreter frame caught
1820   // an exception in which case there is no handler and we must rethrow
1821   // If it is a vanilla interpreted frame the we simply drop into the
1822   // interpreter and let it do the lookup.
1823 
1824   Interpreter::_rethrow_exception_entry = __ pc();
1825   // rax: exception
1826   // rdx: return address/pc that threw exception
1827 
1828   Label return_with_exception;
1829   Label unwind_and_forward;
1830 
1831   // restore state pointer.
1832   __ lea(state, Address(rbp,  -sizeof(BytecodeInterpreter)));
1833 
1834   __ movptr(rbx, STATE(_method));                       // get method
1835 #ifdef _LP64
1836   __ movptr(Address(r15_thread, Thread::pending_exception_offset()), rax);
1837 #else
1838   __ movl(rcx, STATE(_thread));                       // get thread
1839 
1840   // Store exception with interpreter will expect it
1841   __ movptr(Address(rcx, Thread::pending_exception_offset()), rax);
1842 #endif // _LP64
1843 
1844   // is current frame vanilla or native?
1845 
1846   __ movl(rdx, access_flags);
1847   __ testl(rdx, JVM_ACC_NATIVE);
1848   __ jcc(Assembler::zero, return_with_exception);     // vanilla interpreted frame, handle directly
1849 
1850   // We drop thru to unwind a native interpreted frame with a pending exception
1851   // We jump here for the initial interpreter frame with exception pending
1852   // We unwind the current acivation and forward it to our caller.
1853 
1854   __ bind(unwind_and_forward);
1855 
1856   // unwind rbp, return stack to unextended value and re-push return address
1857 
1858   __ movptr(rcx, STATE(_sender_sp));
1859   __ leave();
1860   __ pop(rdx);
1861   __ mov(rsp, rcx);
1862   __ push(rdx);
1863   __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
1864 
1865   // Return point from a call which returns a result in the native abi
1866   // (c1/c2/jni-native). This result must be processed onto the java
1867   // expression stack.
1868   //
1869   // A pending exception may be present in which case there is no result present
1870 
1871   Label resume_interpreter;
1872   Label do_float;
1873   Label do_double;
1874   Label done_conv;
1875 
1876   address compiled_entry = __ pc();
1877 
1878   // The FPU stack is clean if UseSSE >= 2 but must be cleaned in other cases
1879   if (UseSSE < 2) {
1880     __ lea(state, Address(rbp,  -sizeof(BytecodeInterpreter)));
1881     __ movptr(rbx, STATE(_result._to_call._callee));                   // get method just executed
1882     __ movl(rcx, Address(rbx, methodOopDesc::result_index_offset()));
1883     __ cmpl(rcx, AbstractInterpreter::BasicType_as_index(T_FLOAT));    // Result stub address array index
1884     __ jcc(Assembler::equal, do_float);
1885     __ cmpl(rcx, AbstractInterpreter::BasicType_as_index(T_DOUBLE));    // Result stub address array index
1886     __ jcc(Assembler::equal, do_double);
1887 #ifdef COMPILER2
1888     __ empty_FPU_stack();
1889 #endif // COMPILER2
1890     __ jmp(done_conv);
1891 
1892     __ bind(do_float);
1893 #ifdef COMPILER2
1894     for (int i = 1; i < 8; i++) {
1895       __ ffree(i);
1896     }
1897 #endif // COMPILER2
1898     __ jmp(done_conv);
1899     __ bind(do_double);
1900 #ifdef COMPILER2
1901     for (int i = 1; i < 8; i++) {
1902       __ ffree(i);
1903     }
1904 #endif // COMPILER2
1905     __ jmp(done_conv);
1906   } else {
1907     __ MacroAssembler::verify_FPU(0, "generate_return_entry_for compiled");
1908     __ jmp(done_conv);
1909   }
1910 
1911 #if 0
1912   // emit a sentinel we can test for when converting an interpreter
1913   // entry point to a compiled entry point.
1914   __ a_long(Interpreter::return_sentinel);
1915   __ a_long((int)compiled_entry);
1916 #endif
1917 
1918   // Return point to interpreter from compiled/native method
1919 
1920   InternalAddress return_from_native_method(__ pc());
1921 
1922   __ bind(done_conv);
1923 
1924 
1925   // Result if any is in tosca. The java expression stack is in the state that the
1926   // calling convention left it (i.e. params may or may not be present)
1927   // Copy the result from tosca and place it on java expression stack.
1928 
1929   // Restore rsi/r13 as compiled code may not preserve it
1930 
1931   __ lea(state, Address(rbp,  -sizeof(BytecodeInterpreter)));
1932 
1933   // restore stack to what we had when we left (in case i2c extended it)
1934 
1935   __ movptr(rsp, STATE(_stack));
1936   __ lea(rsp, Address(rsp, wordSize));
1937 
1938   // If there is a pending exception then we don't really have a result to process
1939 
1940 #ifdef _LP64
1941   __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
1942 #else
1943   __ movptr(rcx, STATE(_thread));                       // get thread
1944   __ cmpptr(Address(rcx, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
1945 #endif / __LP64
1946   __ jcc(Assembler::notZero, return_with_exception);
1947 
1948   // get method just executed
1949   __ movptr(rbx, STATE(_result._to_call._callee));
1950 
1951   // callee left args on top of expression stack, remove them
1952   __ load_unsigned_word(rcx, Address(rbx, methodOopDesc::size_of_parameters_offset()));
1953   __ lea(rsp, Address(rsp, rcx, Address::times_ptr));
1954 
1955   __ movl(rcx, Address(rbx, methodOopDesc::result_index_offset()));
1956   ExternalAddress tosca_to_stack((address)CppInterpreter::_tosca_to_stack);
1957   // Address index(noreg, rax, Address::times_ptr);
1958   __ movptr(rcx, ArrayAddress(tosca_to_stack, Address(noreg, rcx, Address::times_ptr)));
1959   // __ movl(rcx, Address(noreg, rcx, Address::times_ptr, int(AbstractInterpreter::_tosca_to_stack)));
1960   __ call(rcx);                                               // call result converter
1961   __ jmp(resume_interpreter);
1962 
1963   // An exception is being caught on return to a vanilla interpreter frame.
1964   // Empty the stack and resume interpreter
1965 
1966   __ bind(return_with_exception);
1967 
1968   // Exception present, empty stack
1969   __ movptr(rsp, STATE(_stack_base));
1970   __ jmp(resume_interpreter);
1971 
1972   // Return from interpreted method we return result appropriate to the caller (i.e. "recursive"
1973   // interpreter call, or native) and unwind this interpreter activation.
1974   // All monitors should be unlocked.
1975 
1976   __ bind(return_from_interpreted_method);
1977 
1978   Label return_to_initial_caller;
1979 
1980   __ movptr(rbx, STATE(_method));                                   // get method just executed
1981   __ cmpptr(STATE(_prev_link), (int32_t)NULL_WORD);                 // returning from "recursive" interpreter call?
1982   __ movl(rax, Address(rbx, methodOopDesc::result_index_offset())); // get result type index
1983   __ jcc(Assembler::equal, return_to_initial_caller);               // back to native code (call_stub/c1/c2)
1984 
1985   // Copy result to callers java stack
1986   ExternalAddress stack_to_stack((address)CppInterpreter::_stack_to_stack);
1987   // Address index(noreg, rax, Address::times_ptr);
1988 
1989   __ movptr(rax, ArrayAddress(stack_to_stack, Address(noreg, rax, Address::times_ptr)));
1990   // __ movl(rax, Address(noreg, rax, Address::times_ptr, int(AbstractInterpreter::_stack_to_stack)));
1991   __ call(rax);                                                     // call result converter
1992 
1993   Label unwind_recursive_activation;
1994   __ bind(unwind_recursive_activation);
1995 
1996   // returning to interpreter method from "recursive" interpreter call
1997   // result converter left rax pointing to top of the java stack for method we are returning
1998   // to. Now all we must do is unwind the state from the completed call
1999 
2000   __ movptr(state, STATE(_prev_link));                              // unwind state
2001   __ leave();                                                       // pop the frame
2002   __ mov(rsp, rax);                                                 // unwind stack to remove args
2003 
2004   // Resume the interpreter. The current frame contains the current interpreter
2005   // state object.
2006   //
2007 
2008   __ bind(resume_interpreter);
2009 
2010   // state == interpreterState object for method we are resuming
2011 
2012   __ movl(STATE(_msg), (int)BytecodeInterpreter::method_resume);
2013   __ lea(rsp, Address(rsp, -wordSize));                            // prepush stack (result if any already present)
2014   __ movptr(STATE(_stack), rsp);                                   // inform interpreter of new stack depth (parameters removed,
2015                                                                    // result if any on stack already )
2016   __ movptr(rsp, STATE(_stack_limit));                             // restore expression stack to full depth
2017   __ jmp(call_interpreter_2);                                      // No need to bang
2018 
2019   // interpreter returning to native code (call_stub/c1/c2)
2020   // convert result and unwind initial activation
2021   // rax - result index
2022 
2023   __ bind(return_to_initial_caller);
2024   ExternalAddress stack_to_native((address)CppInterpreter::_stack_to_native_abi);
2025   // Address index(noreg, rax, Address::times_ptr);
2026 
2027   __ movptr(rax, ArrayAddress(stack_to_native, Address(noreg, rax, Address::times_ptr)));
2028   __ call(rax);                                                    // call result converter
2029 
2030   Label unwind_initial_activation;
2031   __ bind(unwind_initial_activation);
2032 
2033   // RETURN TO CALL_STUB/C1/C2 code (result if any in rax/rdx ST(0))
2034 
2035   /* Current stack picture
2036 
2037         [ incoming parameters ]
2038         [ extra locals ]
2039         [ return address to CALL_STUB/C1/C2]
2040   fp -> [ CALL_STUB/C1/C2 fp ]
2041         BytecodeInterpreter object
2042         expression stack
2043   sp ->
2044 
2045   */
2046 
2047   // return restoring the stack to the original sender_sp value
2048 
2049   __ movptr(rcx, STATE(_sender_sp));
2050   __ leave();
2051   __ pop(rdi);                                                        // get return address
2052   // set stack to sender's sp
2053   __ mov(rsp, rcx);
2054   __ jmp(rdi);                                                        // return to call_stub
2055 
2056   // OSR request, adjust return address to make current frame into adapter frame
2057   // and enter OSR nmethod
2058 
2059   __ bind(do_OSR);
2060 
2061   Label remove_initial_frame;
2062 
2063   // We are going to pop this frame. Is there another interpreter frame underneath
2064   // it or is it callstub/compiled?
2065 
2066   // Move buffer to the expected parameter location
2067   __ movptr(rcx, STATE(_result._osr._osr_buf));
2068 
2069   __ movptr(rax, STATE(_result._osr._osr_entry));
2070 
2071   __ cmpptr(STATE(_prev_link), (int32_t)NULL_WORD);            // returning from "recursive" interpreter call?
2072   __ jcc(Assembler::equal, remove_initial_frame);              // back to native code (call_stub/c1/c2)
2073 
2074   __ movptr(sender_sp_on_entry, STATE(_sender_sp));            // get sender's sp in expected register

2075   __ leave();                                                  // pop the frame
2076   __ mov(rsp, sender_sp_on_entry);                             // trim any stack expansion
2077 
2078 
2079   // We know we are calling compiled so push specialized return
2080   // method uses specialized entry, push a return so we look like call stub setup
2081   // this path will handle fact that result is returned in registers and not
2082   // on the java stack.
2083 
2084   __ pushptr(return_from_native_method.addr());
2085 
2086   __ jmp(rax);
2087 
2088   __ bind(remove_initial_frame);
2089 
2090   __ movptr(rdx, STATE(_sender_sp));
2091   __ leave();
2092   // get real return
2093   __ pop(rsi);
2094   // set stack to sender's sp
2095   __ mov(rsp, rdx);
2096   // repush real return
2097   __ push(rsi);
2098   // Enter OSR nmethod
2099   __ jmp(rax);
2100 
2101 
2102 
2103 
2104   // Call a new method. All we do is (temporarily) trim the expression stack
2105   // push a return address to bring us back to here and leap to the new entry.
2106 
2107   __ bind(call_method);
2108 
2109   // stack points to next free location and not top element on expression stack
2110   // method expects sp to be pointing to topmost element
2111 
2112   __ movptr(rsp, STATE(_stack));                                     // pop args to c++ interpreter, set sp to java stack top
2113   __ lea(rsp, Address(rsp, wordSize));
2114 
2115   __ movptr(rbx, STATE(_result._to_call._callee));                   // get method to execute
2116 
2117   // don't need a return address if reinvoking interpreter
2118 
2119   // Make it look like call_stub calling conventions
2120 
2121   // Get (potential) receiver
2122   __ load_unsigned_word(rcx, size_of_parameters);                     // get size of parameters in words
2123 
2124   ExternalAddress recursive(CAST_FROM_FN_PTR(address, RecursiveInterpreterActivation));
2125   __ pushptr(recursive.addr());                                      // make it look good in the debugger
2126 
2127   InternalAddress entry(entry_point);
2128   __ cmpptr(STATE(_result._to_call._callee_entry_point), entry.addr()); // returning to interpreter?
2129   __ jcc(Assembler::equal, re_dispatch);                             // yes
2130 
2131   __ pop(rax);                                                       // pop dummy address
2132 
2133 
2134   // get specialized entry
2135   __ movptr(rax, STATE(_result._to_call._callee_entry_point));
2136   // set sender SP
2137   __ mov(sender_sp_on_entry, rsp);
2138 
2139   // method uses specialized entry, push a return so we look like call stub setup
2140   // this path will handle fact that result is returned in registers and not
2141   // on the java stack.
2142 
2143   __ pushptr(return_from_native_method.addr());
2144 
2145   __ jmp(rax);
2146 
2147   __ bind(bad_msg);
2148   __ stop("Bad message from interpreter");
2149 
2150   // Interpreted method "returned" with an exception pass it on...
2151   // Pass result, unwind activation and continue/return to interpreter/call_stub
2152   // We handle result (if any) differently based on return to interpreter or call_stub
2153 
2154   Label unwind_initial_with_pending_exception;
2155 
2156   __ bind(throw_exception);
2157   __ cmpptr(STATE(_prev_link), (int32_t)NULL_WORD);                 // returning from recursive interpreter call?
2158   __ jcc(Assembler::equal, unwind_initial_with_pending_exception);  // no, back to native code (call_stub/c1/c2)
2159   __ movptr(rax, STATE(_locals));                                   // pop parameters get new stack value
2160   __ addptr(rax, wordSize);                                         // account for prepush before we return
2161   __ jmp(unwind_recursive_activation);
2162 
2163   __ bind(unwind_initial_with_pending_exception);
2164 
2165   // We will unwind the current (initial) interpreter frame and forward
2166   // the exception to the caller. We must put the exception in the
2167   // expected register and clear pending exception and then forward.
2168 
2169   __ jmp(unwind_and_forward);
2170 
2171   interpreter_frame_manager = entry_point;
2172   return entry_point;
2173 }
2174 
2175 address AbstractInterpreterGenerator::generate_method_entry(AbstractInterpreter::MethodKind kind) {
2176   // determine code generation flags
2177   bool synchronized = false;
2178   address entry_point = NULL;
2179 
2180   switch (kind) {