1 /*
   2  * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "asm/assembler.hpp"
  27 #include "interpreter/bytecodeHistogram.hpp"
  28 #include "interpreter/interpreter.hpp"
  29 #include "interpreter/interpreterGenerator.hpp"
  30 #include "interpreter/interpreterRuntime.hpp"
  31 #include "interpreter/templateTable.hpp"
  32 #include "oops/arrayOop.hpp"
  33 #include "oops/methodDataOop.hpp"
  34 #include "oops/methodOop.hpp"
  35 #include "oops/oop.inline.hpp"
  36 #include "prims/jvmtiExport.hpp"
  37 #include "prims/jvmtiThreadState.hpp"
  38 #include "runtime/arguments.hpp"
  39 #include "runtime/deoptimization.hpp"
  40 #include "runtime/frame.inline.hpp"
  41 #include "runtime/sharedRuntime.hpp"
  42 #include "runtime/stubRoutines.hpp"
  43 #include "runtime/synchronizer.hpp"
  44 #include "runtime/timer.hpp"
  45 #include "runtime/vframeArray.hpp"
  46 #include "utilities/debug.hpp"
  47 
  48 #define __ _masm->
  49 
  50 
  51 #ifndef CC_INTERP
  52 const int method_offset = frame::interpreter_frame_method_offset * wordSize;
  53 const int bci_offset    = frame::interpreter_frame_bcx_offset    * wordSize;
  54 const int locals_offset = frame::interpreter_frame_locals_offset * wordSize;
  55 
  56 //------------------------------------------------------------------------------------------------------------------------
  57 
  58 address TemplateInterpreterGenerator::generate_StackOverflowError_handler() {
  59   address entry = __ pc();
  60 
  61   // Note: There should be a minimal interpreter frame set up when stack
  62   // overflow occurs since we check explicitly for it now.
  63   //
  64 #ifdef ASSERT
  65   { Label L;
  66     __ lea(rax, Address(rbp,
  67                 frame::interpreter_frame_monitor_block_top_offset * wordSize));
  68     __ cmpptr(rax, rsp);  // rax, = maximal rsp for current rbp,
  69                         //  (stack grows negative)
  70     __ jcc(Assembler::aboveEqual, L); // check if frame is complete
  71     __ stop ("interpreter frame not set up");
  72     __ bind(L);
  73   }
  74 #endif // ASSERT
  75   // Restore bcp under the assumption that the current frame is still
  76   // interpreted
  77   __ restore_bcp();
  78 
  79   // expression stack must be empty before entering the VM if an exception
  80   // happened
  81   __ empty_expression_stack();
  82   __ empty_FPU_stack();
  83   // throw exception
  84   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_StackOverflowError));
  85   return entry;
  86 }
  87 
  88 address TemplateInterpreterGenerator::generate_ArrayIndexOutOfBounds_handler(const char* name) {
  89   address entry = __ pc();
  90   // expression stack must be empty before entering the VM if an exception happened
  91   __ empty_expression_stack();
  92   __ empty_FPU_stack();
  93   // setup parameters
  94   // ??? convention: expect aberrant index in register rbx,
  95   __ lea(rax, ExternalAddress((address)name));
  96   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_ArrayIndexOutOfBoundsException), rax, rbx);
  97   return entry;
  98 }
  99 
 100 address TemplateInterpreterGenerator::generate_ClassCastException_handler() {
 101   address entry = __ pc();
 102   // object is at TOS
 103   __ pop(rax);
 104   // expression stack must be empty before entering the VM if an exception
 105   // happened
 106   __ empty_expression_stack();
 107   __ empty_FPU_stack();
 108   __ call_VM(noreg,
 109              CAST_FROM_FN_PTR(address,
 110                               InterpreterRuntime::throw_ClassCastException),
 111              rax);
 112   return entry;
 113 }
 114 
 115 // Arguments are: required type at TOS+4, failing object (or NULL) at TOS.
 116 address TemplateInterpreterGenerator::generate_WrongMethodType_handler() {
 117   address entry = __ pc();
 118 
 119   __ pop(rbx);                  // actual failing object is at TOS
 120   __ pop(rax);                  // required type is at TOS+4
 121 
 122   __ verify_oop(rbx);
 123   __ verify_oop(rax);
 124 
 125   // Various method handle types use interpreter registers as temps.
 126   __ restore_bcp();
 127   __ restore_locals();
 128 
 129   // Expression stack must be empty before entering the VM for an exception.
 130   __ empty_expression_stack();
 131   __ empty_FPU_stack();
 132   __ call_VM(noreg,
 133              CAST_FROM_FN_PTR(address,
 134                               InterpreterRuntime::throw_WrongMethodTypeException),
 135              // pass required type, failing object (or NULL)
 136              rax, rbx);
 137   return entry;
 138 }
 139 
 140 
 141 address TemplateInterpreterGenerator::generate_exception_handler_common(const char* name, const char* message, bool pass_oop) {
 142   assert(!pass_oop || message == NULL, "either oop or message but not both");
 143   address entry = __ pc();
 144   if (pass_oop) {
 145     // object is at TOS
 146     __ pop(rbx);
 147   }
 148   // expression stack must be empty before entering the VM if an exception happened
 149   __ empty_expression_stack();
 150   __ empty_FPU_stack();
 151   // setup parameters
 152   __ lea(rax, ExternalAddress((address)name));
 153   if (pass_oop) {
 154     __ call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::create_klass_exception), rax, rbx);
 155   } else {
 156     if (message != NULL) {
 157       __ lea(rbx, ExternalAddress((address)message));
 158     } else {
 159       __ movptr(rbx, NULL_WORD);
 160     }
 161     __ call_VM(rax, CAST_FROM_FN_PTR(address, InterpreterRuntime::create_exception), rax, rbx);
 162   }
 163   // throw exception
 164   __ jump(ExternalAddress(Interpreter::throw_exception_entry()));
 165   return entry;
 166 }
 167 
 168 
 169 address TemplateInterpreterGenerator::generate_continuation_for(TosState state) {
 170   address entry = __ pc();
 171   // NULL last_sp until next java call
 172   __ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), NULL_WORD);
 173   __ dispatch_next(state);
 174   return entry;
 175 }
 176 
 177 
 178 address TemplateInterpreterGenerator::generate_return_entry_for(TosState state, int step) {
 179   TosState incoming_state = state;
 180 
 181   Label interpreter_entry;
 182   address compiled_entry = __ pc();
 183 
 184 #ifdef COMPILER2
 185   // The FPU stack is clean if UseSSE >= 2 but must be cleaned in other cases
 186   if ((incoming_state == ftos && UseSSE < 1) || (incoming_state == dtos && UseSSE < 2)) {
 187     for (int i = 1; i < 8; i++) {
 188         __ ffree(i);
 189     }
 190   } else if (UseSSE < 2) {
 191     __ empty_FPU_stack();
 192   }
 193 #endif
 194   if ((incoming_state == ftos && UseSSE < 1) || (incoming_state == dtos && UseSSE < 2)) {
 195     __ MacroAssembler::verify_FPU(1, "generate_return_entry_for compiled");
 196   } else {
 197     __ MacroAssembler::verify_FPU(0, "generate_return_entry_for compiled");
 198   }
 199 
 200   __ jmp(interpreter_entry, relocInfo::none);
 201   // emit a sentinel we can test for when converting an interpreter
 202   // entry point to a compiled entry point.
 203   __ a_long(Interpreter::return_sentinel);
 204   __ a_long((int)compiled_entry);
 205   address entry = __ pc();
 206   __ bind(interpreter_entry);
 207 
 208   // In SSE mode, interpreter returns FP results in xmm0 but they need
 209   // to end up back on the FPU so it can operate on them.
 210   if (incoming_state == ftos && UseSSE >= 1) {
 211     __ subptr(rsp, wordSize);
 212     __ movflt(Address(rsp, 0), xmm0);
 213     __ fld_s(Address(rsp, 0));
 214     __ addptr(rsp, wordSize);
 215   } else if (incoming_state == dtos && UseSSE >= 2) {
 216     __ subptr(rsp, 2*wordSize);
 217     __ movdbl(Address(rsp, 0), xmm0);
 218     __ fld_d(Address(rsp, 0));
 219     __ addptr(rsp, 2*wordSize);
 220   }
 221 
 222   __ MacroAssembler::verify_FPU(state == ftos || state == dtos ? 1 : 0, "generate_return_entry_for in interpreter");
 223 
 224   // Restore stack bottom in case i2c adjusted stack
 225   __ movptr(rsp, Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize));
 226   // and NULL it as marker that rsp is now tos until next java call
 227   __ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), NULL_WORD);
 228 
 229   __ restore_bcp();
 230   __ restore_locals();
 231 
 232   Label L_got_cache, L_giant_index;
 233   if (EnableInvokeDynamic) {
 234     __ cmpb(Address(rsi, 0), Bytecodes::_invokedynamic);
 235     __ jcc(Assembler::equal, L_giant_index);
 236   }
 237   __ get_cache_and_index_at_bcp(rbx, rcx, 1, sizeof(u2));
 238   __ bind(L_got_cache);
 239   __ movl(rbx, Address(rbx, rcx,
 240                     Address::times_ptr, constantPoolCacheOopDesc::base_offset() +
 241                     ConstantPoolCacheEntry::flags_offset()));
 242   __ andptr(rbx, 0xFF);
 243   __ lea(rsp, Address(rsp, rbx, Interpreter::stackElementScale()));
 244   __ dispatch_next(state, step);
 245 
 246   // out of the main line of code...
 247   if (EnableInvokeDynamic) {
 248     __ bind(L_giant_index);
 249     __ get_cache_and_index_at_bcp(rbx, rcx, 1, sizeof(u4));
 250     __ jmp(L_got_cache);
 251   }
 252 
 253   return entry;
 254 }
 255 
 256 
 257 address TemplateInterpreterGenerator::generate_deopt_entry_for(TosState state, int step) {
 258   address entry = __ pc();
 259 
 260   // In SSE mode, FP results are in xmm0
 261   if (state == ftos && UseSSE > 0) {
 262     __ subptr(rsp, wordSize);
 263     __ movflt(Address(rsp, 0), xmm0);
 264     __ fld_s(Address(rsp, 0));
 265     __ addptr(rsp, wordSize);
 266   } else if (state == dtos && UseSSE >= 2) {
 267     __ subptr(rsp, 2*wordSize);
 268     __ movdbl(Address(rsp, 0), xmm0);
 269     __ fld_d(Address(rsp, 0));
 270     __ addptr(rsp, 2*wordSize);
 271   }
 272 
 273   __ MacroAssembler::verify_FPU(state == ftos || state == dtos ? 1 : 0, "generate_deopt_entry_for in interpreter");
 274 
 275   // The stack is not extended by deopt but we must NULL last_sp as this
 276   // entry is like a "return".
 277   __ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), NULL_WORD);
 278   __ restore_bcp();
 279   __ restore_locals();
 280   // handle exceptions
 281   { Label L;
 282     const Register thread = rcx;
 283     __ get_thread(thread);
 284     __ cmpptr(Address(thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
 285     __ jcc(Assembler::zero, L);
 286     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_pending_exception));
 287     __ should_not_reach_here();
 288     __ bind(L);
 289   }
 290   __ dispatch_next(state, step);
 291   return entry;
 292 }
 293 
 294 
 295 int AbstractInterpreter::BasicType_as_index(BasicType type) {
 296   int i = 0;
 297   switch (type) {
 298     case T_BOOLEAN: i = 0; break;
 299     case T_CHAR   : i = 1; break;
 300     case T_BYTE   : i = 2; break;
 301     case T_SHORT  : i = 3; break;
 302     case T_INT    : // fall through
 303     case T_LONG   : // fall through
 304     case T_VOID   : i = 4; break;
 305     case T_FLOAT  : i = 5; break;  // have to treat float and double separately for SSE
 306     case T_DOUBLE : i = 6; break;
 307     case T_OBJECT : // fall through
 308     case T_ARRAY  : i = 7; break;
 309     default       : ShouldNotReachHere();
 310   }
 311   assert(0 <= i && i < AbstractInterpreter::number_of_result_handlers, "index out of bounds");
 312   return i;
 313 }
 314 
 315 
 316 address TemplateInterpreterGenerator::generate_result_handler_for(BasicType type) {
 317   address entry = __ pc();
 318   switch (type) {
 319     case T_BOOLEAN: __ c2bool(rax);            break;
 320     case T_CHAR   : __ andptr(rax, 0xFFFF);    break;
 321     case T_BYTE   : __ sign_extend_byte (rax); break;
 322     case T_SHORT  : __ sign_extend_short(rax); break;
 323     case T_INT    : /* nothing to do */        break;
 324     case T_DOUBLE :
 325     case T_FLOAT  :
 326       { const Register t = InterpreterRuntime::SignatureHandlerGenerator::temp();
 327         __ pop(t);                            // remove return address first
 328         // Must return a result for interpreter or compiler. In SSE
 329         // mode, results are returned in xmm0 and the FPU stack must
 330         // be empty.
 331         if (type == T_FLOAT && UseSSE >= 1) {
 332           // Load ST0
 333           __ fld_d(Address(rsp, 0));
 334           // Store as float and empty fpu stack
 335           __ fstp_s(Address(rsp, 0));
 336           // and reload
 337           __ movflt(xmm0, Address(rsp, 0));
 338         } else if (type == T_DOUBLE && UseSSE >= 2 ) {
 339           __ movdbl(xmm0, Address(rsp, 0));
 340         } else {
 341           // restore ST0
 342           __ fld_d(Address(rsp, 0));
 343         }
 344         // and pop the temp
 345         __ addptr(rsp, 2 * wordSize);
 346         __ push(t);                           // restore return address
 347       }
 348       break;
 349     case T_OBJECT :
 350       // retrieve result from frame
 351       __ movptr(rax, Address(rbp, frame::interpreter_frame_oop_temp_offset*wordSize));
 352       // and verify it
 353       __ verify_oop(rax);
 354       break;
 355     default       : ShouldNotReachHere();
 356   }
 357   __ ret(0);                                   // return from result handler
 358   return entry;
 359 }
 360 
 361 address TemplateInterpreterGenerator::generate_safept_entry_for(TosState state, address runtime_entry) {
 362   address entry = __ pc();
 363   __ push(state);
 364   __ call_VM(noreg, runtime_entry);
 365   __ dispatch_via(vtos, Interpreter::_normal_table.table_for(vtos));
 366   return entry;
 367 }
 368 
 369 
 370 // Helpers for commoning out cases in the various type of method entries.
 371 //
 372 
 373 // increment invocation count & check for overflow
 374 //
 375 // Note: checking for negative value instead of overflow
 376 //       so we have a 'sticky' overflow test
 377 //
 378 // rbx,: method
 379 // rcx: invocation counter
 380 //
 381 void InterpreterGenerator::generate_counter_incr(Label* overflow, Label* profile_method, Label* profile_method_continue) {
 382   const Address invocation_counter(rbx, in_bytes(methodOopDesc::invocation_counter_offset()) +
 383                                         in_bytes(InvocationCounter::counter_offset()));
 384   // Note: In tiered we increment either counters in methodOop or in MDO depending if we're profiling or not.
 385   if (TieredCompilation) {
 386     int increment = InvocationCounter::count_increment;
 387     int mask = ((1 << Tier0InvokeNotifyFreqLog)  - 1) << InvocationCounter::count_shift;
 388     Label no_mdo, done;
 389     if (ProfileInterpreter) {
 390       // Are we profiling?
 391       __ movptr(rax, Address(rbx, methodOopDesc::method_data_offset()));
 392       __ testptr(rax, rax);
 393       __ jccb(Assembler::zero, no_mdo);
 394       // Increment counter in the MDO
 395       const Address mdo_invocation_counter(rax, in_bytes(methodDataOopDesc::invocation_counter_offset()) +
 396                                                 in_bytes(InvocationCounter::counter_offset()));
 397       __ increment_mask_and_jump(mdo_invocation_counter, increment, mask, rcx, false, Assembler::zero, overflow);
 398       __ jmpb(done);
 399     }
 400     __ bind(no_mdo);
 401     // Increment counter in methodOop (we don't need to load it, it's in rcx).
 402     __ increment_mask_and_jump(invocation_counter, increment, mask, rcx, true, Assembler::zero, overflow);
 403     __ bind(done);
 404   } else {
 405     const Address backedge_counter  (rbx, methodOopDesc::backedge_counter_offset() +
 406                                           InvocationCounter::counter_offset());
 407 
 408     if (ProfileInterpreter) { // %%% Merge this into methodDataOop
 409       __ incrementl(Address(rbx,methodOopDesc::interpreter_invocation_counter_offset()));
 410     }
 411     // Update standard invocation counters
 412     __ movl(rax, backedge_counter);               // load backedge counter
 413 
 414     __ incrementl(rcx, InvocationCounter::count_increment);
 415     __ andl(rax, InvocationCounter::count_mask_value);  // mask out the status bits
 416 
 417     __ movl(invocation_counter, rcx);             // save invocation count
 418     __ addl(rcx, rax);                            // add both counters
 419 
 420     // profile_method is non-null only for interpreted method so
 421     // profile_method != NULL == !native_call
 422     // BytecodeInterpreter only calls for native so code is elided.
 423 
 424     if (ProfileInterpreter && profile_method != NULL) {
 425       // Test to see if we should create a method data oop
 426       __ cmp32(rcx,
 427                ExternalAddress((address)&InvocationCounter::InterpreterProfileLimit));
 428       __ jcc(Assembler::less, *profile_method_continue);
 429 
 430       // if no method data exists, go to profile_method
 431       __ test_method_data_pointer(rax, *profile_method);
 432     }
 433 
 434     __ cmp32(rcx,
 435              ExternalAddress((address)&InvocationCounter::InterpreterInvocationLimit));
 436     __ jcc(Assembler::aboveEqual, *overflow);
 437   }
 438 }
 439 
 440 void InterpreterGenerator::generate_counter_overflow(Label* do_continue) {
 441 
 442   // Asm interpreter on entry
 443   // rdi - locals
 444   // rsi - bcp
 445   // rbx, - method
 446   // rdx - cpool
 447   // rbp, - interpreter frame
 448 
 449   // C++ interpreter on entry
 450   // rsi - new interpreter state pointer
 451   // rbp - interpreter frame pointer
 452   // rbx - method
 453 
 454   // On return (i.e. jump to entry_point) [ back to invocation of interpreter ]
 455   // rbx, - method
 456   // rcx - rcvr (assuming there is one)
 457   // top of stack return address of interpreter caller
 458   // rsp - sender_sp
 459 
 460   // C++ interpreter only
 461   // rsi - previous interpreter state pointer
 462 
 463   const Address size_of_parameters(rbx, methodOopDesc::size_of_parameters_offset());
 464 
 465   // InterpreterRuntime::frequency_counter_overflow takes one argument
 466   // indicating if the counter overflow occurs at a backwards branch (non-NULL bcp).
 467   // The call returns the address of the verified entry point for the method or NULL
 468   // if the compilation did not complete (either went background or bailed out).
 469   __ movptr(rax, (intptr_t)false);
 470   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::frequency_counter_overflow), rax);
 471 
 472   __ movptr(rbx, Address(rbp, method_offset));   // restore methodOop
 473 
 474   // Preserve invariant that rsi/rdi contain bcp/locals of sender frame
 475   // and jump to the interpreted entry.
 476   __ jmp(*do_continue, relocInfo::none);
 477 
 478 }
 479 
 480 void InterpreterGenerator::generate_stack_overflow_check(void) {
 481   // see if we've got enough room on the stack for locals plus overhead.
 482   // the expression stack grows down incrementally, so the normal guard
 483   // page mechanism will work for that.
 484   //
 485   // Registers live on entry:
 486   //
 487   // Asm interpreter
 488   // rdx: number of additional locals this frame needs (what we must check)
 489   // rbx,: methodOop
 490 
 491   // destroyed on exit
 492   // rax,
 493 
 494   // NOTE:  since the additional locals are also always pushed (wasn't obvious in
 495   // generate_method_entry) so the guard should work for them too.
 496   //
 497 
 498   // monitor entry size: see picture of stack set (generate_method_entry) and frame_x86.hpp
 499   const int entry_size    = frame::interpreter_frame_monitor_size() * wordSize;
 500 
 501   // total overhead size: entry_size + (saved rbp, thru expr stack bottom).
 502   // be sure to change this if you add/subtract anything to/from the overhead area
 503   const int overhead_size = -(frame::interpreter_frame_initial_sp_offset*wordSize) + entry_size;
 504 
 505   const int page_size = os::vm_page_size();
 506 
 507   Label after_frame_check;
 508 
 509   // see if the frame is greater than one page in size. If so,
 510   // then we need to verify there is enough stack space remaining
 511   // for the additional locals.
 512   __ cmpl(rdx, (page_size - overhead_size)/Interpreter::stackElementSize);
 513   __ jcc(Assembler::belowEqual, after_frame_check);
 514 
 515   // compute rsp as if this were going to be the last frame on
 516   // the stack before the red zone
 517 
 518   Label after_frame_check_pop;
 519 
 520   __ push(rsi);
 521 
 522   const Register thread = rsi;
 523 
 524   __ get_thread(thread);
 525 
 526   const Address stack_base(thread, Thread::stack_base_offset());
 527   const Address stack_size(thread, Thread::stack_size_offset());
 528 
 529   // locals + overhead, in bytes
 530   __ lea(rax, Address(noreg, rdx, Interpreter::stackElementScale(), overhead_size));
 531 
 532 #ifdef ASSERT
 533   Label stack_base_okay, stack_size_okay;
 534   // verify that thread stack base is non-zero
 535   __ cmpptr(stack_base, (int32_t)NULL_WORD);
 536   __ jcc(Assembler::notEqual, stack_base_okay);
 537   __ stop("stack base is zero");
 538   __ bind(stack_base_okay);
 539   // verify that thread stack size is non-zero
 540   __ cmpptr(stack_size, 0);
 541   __ jcc(Assembler::notEqual, stack_size_okay);
 542   __ stop("stack size is zero");
 543   __ bind(stack_size_okay);
 544 #endif
 545 
 546   // Add stack base to locals and subtract stack size
 547   __ addptr(rax, stack_base);
 548   __ subptr(rax, stack_size);
 549 
 550   // Use the maximum number of pages we might bang.
 551   const int max_pages = StackShadowPages > (StackRedPages+StackYellowPages) ? StackShadowPages :
 552                                                                               (StackRedPages+StackYellowPages);
 553   __ addptr(rax, max_pages * page_size);
 554 
 555   // check against the current stack bottom
 556   __ cmpptr(rsp, rax);
 557   __ jcc(Assembler::above, after_frame_check_pop);
 558 
 559   __ pop(rsi);  // get saved bcp / (c++ prev state ).
 560 
 561   __ pop(rax);  // get return address
 562   __ jump(ExternalAddress(Interpreter::throw_StackOverflowError_entry()));
 563 
 564   // all done with frame size check
 565   __ bind(after_frame_check_pop);
 566   __ pop(rsi);
 567 
 568   __ bind(after_frame_check);
 569 }
 570 
 571 // Allocate monitor and lock method (asm interpreter)
 572 // rbx, - methodOop
 573 //
 574 void InterpreterGenerator::lock_method(void) {
 575   // synchronize method
 576   const Address access_flags      (rbx, methodOopDesc::access_flags_offset());
 577   const Address monitor_block_top (rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
 578   const int entry_size            = frame::interpreter_frame_monitor_size() * wordSize;
 579 
 580   #ifdef ASSERT
 581     { Label L;
 582       __ movl(rax, access_flags);
 583       __ testl(rax, JVM_ACC_SYNCHRONIZED);
 584       __ jcc(Assembler::notZero, L);
 585       __ stop("method doesn't need synchronization");
 586       __ bind(L);
 587     }
 588   #endif // ASSERT
 589   // get synchronization object
 590   { Label done;
 591     const int mirror_offset = klassOopDesc::klass_part_offset_in_bytes() + Klass::java_mirror_offset_in_bytes();
 592     __ movl(rax, access_flags);
 593     __ testl(rax, JVM_ACC_STATIC);
 594     __ movptr(rax, Address(rdi, Interpreter::local_offset_in_bytes(0)));  // get receiver (assume this is frequent case)
 595     __ jcc(Assembler::zero, done);
 596     __ movptr(rax, Address(rbx, methodOopDesc::constants_offset()));
 597     __ movptr(rax, Address(rax, constantPoolOopDesc::pool_holder_offset_in_bytes()));
 598     __ movptr(rax, Address(rax, mirror_offset));
 599     __ bind(done);
 600   }
 601   // add space for monitor & lock
 602   __ subptr(rsp, entry_size);                                           // add space for a monitor entry
 603   __ movptr(monitor_block_top, rsp);                                    // set new monitor block top
 604   __ movptr(Address(rsp, BasicObjectLock::obj_offset_in_bytes()), rax); // store object
 605   __ mov(rdx, rsp);                                                    // object address
 606   __ lock_object(rdx);
 607 }
 608 
 609 //
 610 // Generate a fixed interpreter frame. This is identical setup for interpreted methods
 611 // and for native methods hence the shared code.
 612 
 613 void TemplateInterpreterGenerator::generate_fixed_frame(bool native_call) {
 614   // initialize fixed part of activation frame
 615   __ push(rax);                                       // save return address
 616   __ enter();                                         // save old & set new rbp,
 617 
 618 
 619   __ push(rsi);                                       // set sender sp
 620   __ push((int32_t)NULL_WORD);                        // leave last_sp as null
 621   __ movptr(rsi, Address(rbx,methodOopDesc::const_offset())); // get constMethodOop
 622   __ lea(rsi, Address(rsi,constMethodOopDesc::codes_offset())); // get codebase
 623   __ push(rbx);                                      // save methodOop
 624   if (ProfileInterpreter) {
 625     Label method_data_continue;
 626     __ movptr(rdx, Address(rbx, in_bytes(methodOopDesc::method_data_offset())));
 627     __ testptr(rdx, rdx);
 628     __ jcc(Assembler::zero, method_data_continue);
 629     __ addptr(rdx, in_bytes(methodDataOopDesc::data_offset()));
 630     __ bind(method_data_continue);
 631     __ push(rdx);                                       // set the mdp (method data pointer)
 632   } else {
 633     __ push(0);
 634   }
 635 
 636   __ movptr(rdx, Address(rbx, methodOopDesc::constants_offset()));
 637   __ movptr(rdx, Address(rdx, constantPoolOopDesc::cache_offset_in_bytes()));
 638   __ push(rdx);                                       // set constant pool cache
 639   __ push(rdi);                                       // set locals pointer
 640   if (native_call) {
 641     __ push(0);                                       // no bcp
 642   } else {
 643     __ push(rsi);                                     // set bcp
 644     }
 645   __ push(0);                                         // reserve word for pointer to expression stack bottom
 646   __ movptr(Address(rsp, 0), rsp);                    // set expression stack bottom
 647 }
 648 
 649 // End of helpers
 650 
 651 //
 652 // Various method entries
 653 //------------------------------------------------------------------------------------------------------------------------
 654 //
 655 //
 656 
 657 // Call an accessor method (assuming it is resolved, otherwise drop into vanilla (slow path) entry
 658 
 659 address InterpreterGenerator::generate_accessor_entry(void) {
 660 
 661   // rbx,: methodOop
 662   // rcx: receiver (preserve for slow entry into asm interpreter)
 663 
 664   // rsi: senderSP must preserved for slow path, set SP to it on fast path
 665 
 666   address entry_point = __ pc();
 667   Label xreturn_path;
 668 
 669   // do fastpath for resolved accessor methods
 670   if (UseFastAccessorMethods) {
 671     Label slow_path;
 672     // If we need a safepoint check, generate full interpreter entry.
 673     ExternalAddress state(SafepointSynchronize::address_of_state());
 674     __ cmp32(ExternalAddress(SafepointSynchronize::address_of_state()),
 675              SafepointSynchronize::_not_synchronized);
 676 
 677     __ jcc(Assembler::notEqual, slow_path);
 678     // ASM/C++ Interpreter
 679     // Code: _aload_0, _(i|a)getfield, _(i|a)return or any rewrites thereof; parameter size = 1
 680     // Note: We can only use this code if the getfield has been resolved
 681     //       and if we don't have a null-pointer exception => check for
 682     //       these conditions first and use slow path if necessary.
 683     // rbx,: method
 684     // rcx: receiver
 685     __ movptr(rax, Address(rsp, wordSize));
 686 
 687     // check if local 0 != NULL and read field
 688     __ testptr(rax, rax);
 689     __ jcc(Assembler::zero, slow_path);
 690 
 691     __ movptr(rdi, Address(rbx, methodOopDesc::constants_offset()));
 692     // read first instruction word and extract bytecode @ 1 and index @ 2
 693     __ movptr(rdx, Address(rbx, methodOopDesc::const_offset()));
 694     __ movl(rdx, Address(rdx, constMethodOopDesc::codes_offset()));
 695     // Shift codes right to get the index on the right.
 696     // The bytecode fetched looks like <index><0xb4><0x2a>
 697     __ shrl(rdx, 2*BitsPerByte);
 698     __ shll(rdx, exact_log2(in_words(ConstantPoolCacheEntry::size())));
 699     __ movptr(rdi, Address(rdi, constantPoolOopDesc::cache_offset_in_bytes()));
 700 
 701     // rax,: local 0
 702     // rbx,: method
 703     // rcx: receiver - do not destroy since it is needed for slow path!
 704     // rcx: scratch
 705     // rdx: constant pool cache index
 706     // rdi: constant pool cache
 707     // rsi: sender sp
 708 
 709     // check if getfield has been resolved and read constant pool cache entry
 710     // check the validity of the cache entry by testing whether _indices field
 711     // contains Bytecode::_getfield in b1 byte.
 712     assert(in_words(ConstantPoolCacheEntry::size()) == 4, "adjust shift below");
 713     __ movl(rcx,
 714             Address(rdi,
 715                     rdx,
 716                     Address::times_ptr, constantPoolCacheOopDesc::base_offset() + ConstantPoolCacheEntry::indices_offset()));
 717     __ shrl(rcx, 2*BitsPerByte);
 718     __ andl(rcx, 0xFF);
 719     __ cmpl(rcx, Bytecodes::_getfield);
 720     __ jcc(Assembler::notEqual, slow_path);
 721 
 722     // Note: constant pool entry is not valid before bytecode is resolved
 723     __ movptr(rcx,
 724               Address(rdi,
 725                       rdx,
 726                       Address::times_ptr, constantPoolCacheOopDesc::base_offset() + ConstantPoolCacheEntry::f2_offset()));
 727     __ movl(rdx,
 728             Address(rdi,
 729                     rdx,
 730                     Address::times_ptr, constantPoolCacheOopDesc::base_offset() + ConstantPoolCacheEntry::flags_offset()));
 731 
 732     Label notByte, notShort, notChar;
 733     const Address field_address (rax, rcx, Address::times_1);
 734 
 735     // Need to differentiate between igetfield, agetfield, bgetfield etc.
 736     // because they are different sizes.
 737     // Use the type from the constant pool cache
 738     __ shrl(rdx, ConstantPoolCacheEntry::tosBits);
 739     // Make sure we don't need to mask rdx for tosBits after the above shift
 740     ConstantPoolCacheEntry::verify_tosBits();
 741     __ cmpl(rdx, btos);
 742     __ jcc(Assembler::notEqual, notByte);
 743     __ load_signed_byte(rax, field_address);
 744     __ jmp(xreturn_path);
 745 
 746     __ bind(notByte);
 747     __ cmpl(rdx, stos);
 748     __ jcc(Assembler::notEqual, notShort);
 749     __ load_signed_short(rax, field_address);
 750     __ jmp(xreturn_path);
 751 
 752     __ bind(notShort);
 753     __ cmpl(rdx, ctos);
 754     __ jcc(Assembler::notEqual, notChar);
 755     __ load_unsigned_short(rax, field_address);
 756     __ jmp(xreturn_path);
 757 
 758     __ bind(notChar);
 759 #ifdef ASSERT
 760     Label okay;
 761     __ cmpl(rdx, atos);
 762     __ jcc(Assembler::equal, okay);
 763     __ cmpl(rdx, itos);
 764     __ jcc(Assembler::equal, okay);
 765     __ stop("what type is this?");
 766     __ bind(okay);
 767 #endif // ASSERT
 768     // All the rest are a 32 bit wordsize
 769     // This is ok for now. Since fast accessors should be going away
 770     __ movptr(rax, field_address);
 771 
 772     __ bind(xreturn_path);
 773 
 774     // _ireturn/_areturn
 775     __ pop(rdi);                               // get return address
 776     __ mov(rsp, rsi);                          // set sp to sender sp
 777     __ jmp(rdi);
 778 
 779     // generate a vanilla interpreter entry as the slow path
 780     __ bind(slow_path);
 781 
 782     (void) generate_normal_entry(false);
 783     return entry_point;
 784   }
 785   return NULL;
 786 
 787 }
 788 
 789 //
 790 // Interpreter stub for calling a native method. (asm interpreter)
 791 // This sets up a somewhat different looking stack for calling the native method
 792 // than the typical interpreter frame setup.
 793 //
 794 
 795 address InterpreterGenerator::generate_native_entry(bool synchronized) {
 796   // determine code generation flags
 797   bool inc_counter  = UseCompiler || CountCompiledCalls;
 798 
 799   // rbx,: methodOop
 800   // rsi: sender sp
 801   // rsi: previous interpreter state (C++ interpreter) must preserve
 802   address entry_point = __ pc();
 803 
 804 
 805   const Address size_of_parameters(rbx, methodOopDesc::size_of_parameters_offset());
 806   const Address invocation_counter(rbx, methodOopDesc::invocation_counter_offset() + InvocationCounter::counter_offset());
 807   const Address access_flags      (rbx, methodOopDesc::access_flags_offset());
 808 
 809   // get parameter size (always needed)
 810   __ load_unsigned_short(rcx, size_of_parameters);
 811 
 812   // native calls don't need the stack size check since they have no expression stack
 813   // and the arguments are already on the stack and we only add a handful of words
 814   // to the stack
 815 
 816   // rbx,: methodOop
 817   // rcx: size of parameters
 818   // rsi: sender sp
 819 
 820   __ pop(rax);                                       // get return address
 821   // for natives the size of locals is zero
 822 
 823   // compute beginning of parameters (rdi)
 824   __ lea(rdi, Address(rsp, rcx, Interpreter::stackElementScale(), -wordSize));
 825 
 826 
 827   // add 2 zero-initialized slots for native calls
 828   // NULL result handler
 829   __ push((int32_t)NULL_WORD);
 830   // NULL oop temp (mirror or jni oop result)
 831   __ push((int32_t)NULL_WORD);
 832 
 833   if (inc_counter) __ movl(rcx, invocation_counter);  // (pre-)fetch invocation count
 834   // initialize fixed part of activation frame
 835 
 836   generate_fixed_frame(true);
 837 
 838   // make sure method is native & not abstract
 839 #ifdef ASSERT
 840   __ movl(rax, access_flags);
 841   {
 842     Label L;
 843     __ testl(rax, JVM_ACC_NATIVE);
 844     __ jcc(Assembler::notZero, L);
 845     __ stop("tried to execute non-native method as native");
 846     __ bind(L);
 847   }
 848   { Label L;
 849     __ testl(rax, JVM_ACC_ABSTRACT);
 850     __ jcc(Assembler::zero, L);
 851     __ stop("tried to execute abstract method in interpreter");
 852     __ bind(L);
 853   }
 854 #endif
 855 
 856   // Since at this point in the method invocation the exception handler
 857   // would try to exit the monitor of synchronized methods which hasn't
 858   // been entered yet, we set the thread local variable
 859   // _do_not_unlock_if_synchronized to true. The remove_activation will
 860   // check this flag.
 861 
 862   __ get_thread(rax);
 863   const Address do_not_unlock_if_synchronized(rax,
 864         in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
 865   __ movbool(do_not_unlock_if_synchronized, true);
 866 
 867   // increment invocation count & check for overflow
 868   Label invocation_counter_overflow;
 869   if (inc_counter) {
 870     generate_counter_incr(&invocation_counter_overflow, NULL, NULL);
 871   }
 872 
 873   Label continue_after_compile;
 874   __ bind(continue_after_compile);
 875 
 876   bang_stack_shadow_pages(true);
 877 
 878   // reset the _do_not_unlock_if_synchronized flag
 879   __ get_thread(rax);
 880   __ movbool(do_not_unlock_if_synchronized, false);
 881 
 882   // check for synchronized methods
 883   // Must happen AFTER invocation_counter check and stack overflow check,
 884   // so method is not locked if overflows.
 885   //
 886   if (synchronized) {
 887     lock_method();
 888   } else {
 889     // no synchronization necessary
 890 #ifdef ASSERT
 891       { Label L;
 892         __ movl(rax, access_flags);
 893         __ testl(rax, JVM_ACC_SYNCHRONIZED);
 894         __ jcc(Assembler::zero, L);
 895         __ stop("method needs synchronization");
 896         __ bind(L);
 897       }
 898 #endif
 899   }
 900 
 901   // start execution
 902 #ifdef ASSERT
 903   { Label L;
 904     const Address monitor_block_top (rbp,
 905                  frame::interpreter_frame_monitor_block_top_offset * wordSize);
 906     __ movptr(rax, monitor_block_top);
 907     __ cmpptr(rax, rsp);
 908     __ jcc(Assembler::equal, L);
 909     __ stop("broken stack frame setup in interpreter");
 910     __ bind(L);
 911   }
 912 #endif
 913 
 914   // jvmti/dtrace support
 915   __ notify_method_entry();
 916 
 917   // work registers
 918   const Register method = rbx;
 919   const Register thread = rdi;
 920   const Register t      = rcx;
 921 
 922   // allocate space for parameters
 923   __ get_method(method);
 924   __ verify_oop(method);
 925   __ load_unsigned_short(t, Address(method, methodOopDesc::size_of_parameters_offset()));
 926   __ shlptr(t, Interpreter::logStackElementSize);
 927   __ addptr(t, 2*wordSize);     // allocate two more slots for JNIEnv and possible mirror
 928   __ subptr(rsp, t);
 929   __ andptr(rsp, -(StackAlignmentInBytes)); // gcc needs 16 byte aligned stacks to do XMM intrinsics
 930 
 931   // get signature handler
 932   { Label L;
 933     __ movptr(t, Address(method, methodOopDesc::signature_handler_offset()));
 934     __ testptr(t, t);
 935     __ jcc(Assembler::notZero, L);
 936     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::prepare_native_call), method);
 937     __ get_method(method);
 938     __ movptr(t, Address(method, methodOopDesc::signature_handler_offset()));
 939     __ bind(L);
 940   }
 941 
 942   // call signature handler
 943   assert(InterpreterRuntime::SignatureHandlerGenerator::from() == rdi, "adjust this code");
 944   assert(InterpreterRuntime::SignatureHandlerGenerator::to  () == rsp, "adjust this code");
 945   assert(InterpreterRuntime::SignatureHandlerGenerator::temp() == t  , "adjust this code");
 946   // The generated handlers do not touch RBX (the method oop).
 947   // However, large signatures cannot be cached and are generated
 948   // each time here.  The slow-path generator will blow RBX
 949   // sometime, so we must reload it after the call.
 950   __ call(t);
 951   __ get_method(method);        // slow path call blows RBX on DevStudio 5.0
 952 
 953   // result handler is in rax,
 954   // set result handler
 955   __ movptr(Address(rbp, frame::interpreter_frame_result_handler_offset*wordSize), rax);
 956 
 957   // pass mirror handle if static call
 958   { Label L;
 959     const int mirror_offset = klassOopDesc::klass_part_offset_in_bytes() + Klass::java_mirror_offset_in_bytes();
 960     __ movl(t, Address(method, methodOopDesc::access_flags_offset()));
 961     __ testl(t, JVM_ACC_STATIC);
 962     __ jcc(Assembler::zero, L);
 963     // get mirror
 964     __ movptr(t, Address(method, methodOopDesc:: constants_offset()));
 965     __ movptr(t, Address(t, constantPoolOopDesc::pool_holder_offset_in_bytes()));
 966     __ movptr(t, Address(t, mirror_offset));
 967     // copy mirror into activation frame
 968     __ movptr(Address(rbp, frame::interpreter_frame_oop_temp_offset * wordSize), t);
 969     // pass handle to mirror
 970     __ lea(t, Address(rbp, frame::interpreter_frame_oop_temp_offset * wordSize));
 971     __ movptr(Address(rsp, wordSize), t);
 972     __ bind(L);
 973   }
 974 
 975   // get native function entry point
 976   { Label L;
 977     __ movptr(rax, Address(method, methodOopDesc::native_function_offset()));
 978     ExternalAddress unsatisfied(SharedRuntime::native_method_throw_unsatisfied_link_error_entry());
 979     __ cmpptr(rax, unsatisfied.addr());
 980     __ jcc(Assembler::notEqual, L);
 981     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::prepare_native_call), method);
 982     __ get_method(method);
 983     __ verify_oop(method);
 984     __ movptr(rax, Address(method, methodOopDesc::native_function_offset()));
 985     __ bind(L);
 986   }
 987 
 988   // pass JNIEnv
 989   __ get_thread(thread);
 990   __ lea(t, Address(thread, JavaThread::jni_environment_offset()));
 991   __ movptr(Address(rsp, 0), t);
 992 
 993   // set_last_Java_frame_before_call
 994   // It is enough that the pc()
 995   // points into the right code segment. It does not have to be the correct return pc.
 996   __ set_last_Java_frame(thread, noreg, rbp, __ pc());
 997 
 998   // change thread state
 999 #ifdef ASSERT
1000   { Label L;
1001     __ movl(t, Address(thread, JavaThread::thread_state_offset()));
1002     __ cmpl(t, _thread_in_Java);
1003     __ jcc(Assembler::equal, L);
1004     __ stop("Wrong thread state in native stub");
1005     __ bind(L);
1006   }
1007 #endif
1008 
1009   // Change state to native
1010   __ movl(Address(thread, JavaThread::thread_state_offset()), _thread_in_native);
1011   __ call(rax);
1012 
1013   // result potentially in rdx:rax or ST0
1014 
1015   // Either restore the MXCSR register after returning from the JNI Call
1016   // or verify that it wasn't changed.
1017   if (VM_Version::supports_sse()) {
1018     if (RestoreMXCSROnJNICalls) {
1019       __ ldmxcsr(ExternalAddress(StubRoutines::addr_mxcsr_std()));
1020     }
1021     else if (CheckJNICalls ) {
1022       __ call(RuntimeAddress(StubRoutines::x86::verify_mxcsr_entry()));
1023     }
1024   }
1025 
1026   // Either restore the x87 floating pointer control word after returning
1027   // from the JNI call or verify that it wasn't changed.
1028   if (CheckJNICalls) {
1029     __ call(RuntimeAddress(StubRoutines::x86::verify_fpu_cntrl_wrd_entry()));
1030   }
1031 
1032   // save potential result in ST(0) & rdx:rax
1033   // (if result handler is the T_FLOAT or T_DOUBLE handler, result must be in ST0 -
1034   // the check is necessary to avoid potential Intel FPU overflow problems by saving/restoring 'empty' FPU registers)
1035   // It is safe to do this push because state is _thread_in_native and return address will be found
1036   // via _last_native_pc and not via _last_jave_sp
1037 
1038   // NOTE: the order of theses push(es) is known to frame::interpreter_frame_result.
1039   // If the order changes or anything else is added to the stack the code in
1040   // interpreter_frame_result will have to be changed.
1041 
1042   { Label L;
1043     Label push_double;
1044     ExternalAddress float_handler(AbstractInterpreter::result_handler(T_FLOAT));
1045     ExternalAddress double_handler(AbstractInterpreter::result_handler(T_DOUBLE));
1046     __ cmpptr(Address(rbp, (frame::interpreter_frame_oop_temp_offset + 1)*wordSize),
1047               float_handler.addr());
1048     __ jcc(Assembler::equal, push_double);
1049     __ cmpptr(Address(rbp, (frame::interpreter_frame_oop_temp_offset + 1)*wordSize),
1050               double_handler.addr());
1051     __ jcc(Assembler::notEqual, L);
1052     __ bind(push_double);
1053     __ push(dtos);
1054     __ bind(L);
1055   }
1056   __ push(ltos);
1057 
1058   // change thread state
1059   __ get_thread(thread);
1060   __ movl(Address(thread, JavaThread::thread_state_offset()), _thread_in_native_trans);
1061   if(os::is_MP()) {
1062     if (UseMembar) {
1063       // Force this write out before the read below
1064       __ membar(Assembler::Membar_mask_bits(
1065            Assembler::LoadLoad | Assembler::LoadStore |
1066            Assembler::StoreLoad | Assembler::StoreStore));
1067     } else {
1068       // Write serialization page so VM thread can do a pseudo remote membar.
1069       // We use the current thread pointer to calculate a thread specific
1070       // offset to write to within the page. This minimizes bus traffic
1071       // due to cache line collision.
1072       __ serialize_memory(thread, rcx);
1073     }
1074   }
1075 
1076   if (AlwaysRestoreFPU) {
1077     //  Make sure the control word is correct.
1078     __ fldcw(ExternalAddress(StubRoutines::addr_fpu_cntrl_wrd_std()));
1079   }
1080 
1081   // check for safepoint operation in progress and/or pending suspend requests
1082   { Label Continue;
1083 
1084     __ cmp32(ExternalAddress(SafepointSynchronize::address_of_state()),
1085              SafepointSynchronize::_not_synchronized);
1086 
1087     Label L;
1088     __ jcc(Assembler::notEqual, L);
1089     __ cmpl(Address(thread, JavaThread::suspend_flags_offset()), 0);
1090     __ jcc(Assembler::equal, Continue);
1091     __ bind(L);
1092 
1093     // Don't use call_VM as it will see a possible pending exception and forward it
1094     // and never return here preventing us from clearing _last_native_pc down below.
1095     // Also can't use call_VM_leaf either as it will check to see if rsi & rdi are
1096     // preserved and correspond to the bcp/locals pointers. So we do a runtime call
1097     // by hand.
1098     //
1099     __ push(thread);
1100     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address,
1101                                             JavaThread::check_special_condition_for_native_trans)));
1102     __ increment(rsp, wordSize);
1103     __ get_thread(thread);
1104 
1105     __ bind(Continue);
1106   }
1107 
1108   // change thread state
1109   __ movl(Address(thread, JavaThread::thread_state_offset()), _thread_in_Java);
1110 
1111   __ reset_last_Java_frame(thread, true, true);
1112 
1113   // reset handle block
1114   __ movptr(t, Address(thread, JavaThread::active_handles_offset()));
1115   __ movptr(Address(t, JNIHandleBlock::top_offset_in_bytes()), NULL_WORD);
1116 
1117   // If result was an oop then unbox and save it in the frame
1118   { Label L;
1119     Label no_oop, store_result;
1120     ExternalAddress handler(AbstractInterpreter::result_handler(T_OBJECT));
1121     __ cmpptr(Address(rbp, frame::interpreter_frame_result_handler_offset*wordSize),
1122               handler.addr());
1123     __ jcc(Assembler::notEqual, no_oop);
1124     __ cmpptr(Address(rsp, 0), (int32_t)NULL_WORD);
1125     __ pop(ltos);
1126     __ testptr(rax, rax);
1127     __ jcc(Assembler::zero, store_result);
1128     // unbox
1129     __ movptr(rax, Address(rax, 0));
1130     __ bind(store_result);
1131     __ movptr(Address(rbp, (frame::interpreter_frame_oop_temp_offset)*wordSize), rax);
1132     // keep stack depth as expected by pushing oop which will eventually be discarded
1133     __ push(ltos);
1134     __ bind(no_oop);
1135   }
1136 
1137   {
1138      Label no_reguard;
1139      __ cmpl(Address(thread, JavaThread::stack_guard_state_offset()), JavaThread::stack_guard_yellow_disabled);
1140      __ jcc(Assembler::notEqual, no_reguard);
1141 
1142      __ pusha();
1143      __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages)));
1144      __ popa();
1145 
1146      __ bind(no_reguard);
1147    }
1148 
1149   // restore rsi to have legal interpreter frame,
1150   // i.e., bci == 0 <=> rsi == code_base()
1151   // Can't call_VM until bcp is within reasonable.
1152   __ get_method(method);      // method is junk from thread_in_native to now.
1153   __ verify_oop(method);
1154   __ movptr(rsi, Address(method,methodOopDesc::const_offset()));   // get constMethodOop
1155   __ lea(rsi, Address(rsi,constMethodOopDesc::codes_offset()));    // get codebase
1156 
1157   // handle exceptions (exception handling will handle unlocking!)
1158   { Label L;
1159     __ cmpptr(Address(thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
1160     __ jcc(Assembler::zero, L);
1161     // Note: At some point we may want to unify this with the code used in call_VM_base();
1162     //       i.e., we should use the StubRoutines::forward_exception code. For now this
1163     //       doesn't work here because the rsp is not correctly set at this point.
1164     __ MacroAssembler::call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_pending_exception));
1165     __ should_not_reach_here();
1166     __ bind(L);
1167   }
1168 
1169   // do unlocking if necessary
1170   { Label L;
1171     __ movl(t, Address(method, methodOopDesc::access_flags_offset()));
1172     __ testl(t, JVM_ACC_SYNCHRONIZED);
1173     __ jcc(Assembler::zero, L);
1174     // the code below should be shared with interpreter macro assembler implementation
1175     { Label unlock;
1176       // BasicObjectLock will be first in list, since this is a synchronized method. However, need
1177       // to check that the object has not been unlocked by an explicit monitorexit bytecode.
1178       const Address monitor(rbp, frame::interpreter_frame_initial_sp_offset * wordSize - (int)sizeof(BasicObjectLock));
1179 
1180       __ lea(rdx, monitor);                   // address of first monitor
1181 
1182       __ movptr(t, Address(rdx, BasicObjectLock::obj_offset_in_bytes()));
1183       __ testptr(t, t);
1184       __ jcc(Assembler::notZero, unlock);
1185 
1186       // Entry already unlocked, need to throw exception
1187       __ MacroAssembler::call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_illegal_monitor_state_exception));
1188       __ should_not_reach_here();
1189 
1190       __ bind(unlock);
1191       __ unlock_object(rdx);
1192     }
1193     __ bind(L);
1194   }
1195 
1196   // jvmti/dtrace support
1197   // Note: This must happen _after_ handling/throwing any exceptions since
1198   //       the exception handler code notifies the runtime of method exits
1199   //       too. If this happens before, method entry/exit notifications are
1200   //       not properly paired (was bug - gri 11/22/99).
1201   __ notify_method_exit(vtos, InterpreterMacroAssembler::NotifyJVMTI);
1202 
1203   // restore potential result in rdx:rax, call result handler to restore potential result in ST0 & handle result
1204   __ pop(ltos);
1205   __ movptr(t, Address(rbp, frame::interpreter_frame_result_handler_offset*wordSize));
1206   __ call(t);
1207 
1208   // remove activation
1209   __ movptr(t, Address(rbp, frame::interpreter_frame_sender_sp_offset * wordSize)); // get sender sp
1210   __ leave();                                // remove frame anchor
1211   __ pop(rdi);                               // get return address
1212   __ mov(rsp, t);                            // set sp to sender sp
1213   __ jmp(rdi);
1214 
1215   if (inc_counter) {
1216     // Handle overflow of counter and compile method
1217     __ bind(invocation_counter_overflow);
1218     generate_counter_overflow(&continue_after_compile);
1219   }
1220 
1221   return entry_point;
1222 }
1223 
1224 //
1225 // Generic interpreted method entry to (asm) interpreter
1226 //
1227 address InterpreterGenerator::generate_normal_entry(bool synchronized) {
1228   // determine code generation flags
1229   bool inc_counter  = UseCompiler || CountCompiledCalls;
1230 
1231   // rbx,: methodOop
1232   // rsi: sender sp
1233   address entry_point = __ pc();
1234 
1235 
1236   const Address size_of_parameters(rbx, methodOopDesc::size_of_parameters_offset());
1237   const Address size_of_locals    (rbx, methodOopDesc::size_of_locals_offset());
1238   const Address invocation_counter(rbx, methodOopDesc::invocation_counter_offset() + InvocationCounter::counter_offset());
1239   const Address access_flags      (rbx, methodOopDesc::access_flags_offset());
1240 
1241   // get parameter size (always needed)
1242   __ load_unsigned_short(rcx, size_of_parameters);
1243 
1244   // rbx,: methodOop
1245   // rcx: size of parameters
1246 
1247   // rsi: sender_sp (could differ from sp+wordSize if we were called via c2i )
1248 
1249   __ load_unsigned_short(rdx, size_of_locals);       // get size of locals in words
1250   __ subl(rdx, rcx);                                // rdx = no. of additional locals
1251 
1252   // see if we've got enough room on the stack for locals plus overhead.
1253   generate_stack_overflow_check();
1254 
1255   // get return address
1256   __ pop(rax);
1257 
1258   // compute beginning of parameters (rdi)
1259   __ lea(rdi, Address(rsp, rcx, Interpreter::stackElementScale(), -wordSize));
1260 
1261   // rdx - # of additional locals
1262   // allocate space for locals
1263   // explicitly initialize locals
1264   {
1265     Label exit, loop;
1266     __ testl(rdx, rdx);
1267     __ jcc(Assembler::lessEqual, exit);               // do nothing if rdx <= 0
1268     __ bind(loop);
1269     __ push((int32_t)NULL_WORD);                      // initialize local variables
1270     __ decrement(rdx);                                // until everything initialized
1271     __ jcc(Assembler::greater, loop);
1272     __ bind(exit);
1273   }
1274 
1275   if (inc_counter) __ movl(rcx, invocation_counter);  // (pre-)fetch invocation count
1276   // initialize fixed part of activation frame
1277   generate_fixed_frame(false);
1278 
1279   // make sure method is not native & not abstract
1280 #ifdef ASSERT
1281   __ movl(rax, access_flags);
1282   {
1283     Label L;
1284     __ testl(rax, JVM_ACC_NATIVE);
1285     __ jcc(Assembler::zero, L);
1286     __ stop("tried to execute native method as non-native");
1287     __ bind(L);
1288   }
1289   { Label L;
1290     __ testl(rax, JVM_ACC_ABSTRACT);
1291     __ jcc(Assembler::zero, L);
1292     __ stop("tried to execute abstract method in interpreter");
1293     __ bind(L);
1294   }
1295 #endif
1296 
1297   // Since at this point in the method invocation the exception handler
1298   // would try to exit the monitor of synchronized methods which hasn't
1299   // been entered yet, we set the thread local variable
1300   // _do_not_unlock_if_synchronized to true. The remove_activation will
1301   // check this flag.
1302 
1303   __ get_thread(rax);
1304   const Address do_not_unlock_if_synchronized(rax,
1305         in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
1306   __ movbool(do_not_unlock_if_synchronized, true);
1307 
1308   // increment invocation count & check for overflow
1309   Label invocation_counter_overflow;
1310   Label profile_method;
1311   Label profile_method_continue;
1312   if (inc_counter) {
1313     generate_counter_incr(&invocation_counter_overflow, &profile_method, &profile_method_continue);
1314     if (ProfileInterpreter) {
1315       __ bind(profile_method_continue);
1316     }
1317   }
1318   Label continue_after_compile;
1319   __ bind(continue_after_compile);
1320 
1321   bang_stack_shadow_pages(false);
1322 
1323   // reset the _do_not_unlock_if_synchronized flag
1324   __ get_thread(rax);
1325   __ movbool(do_not_unlock_if_synchronized, false);
1326 
1327   // check for synchronized methods
1328   // Must happen AFTER invocation_counter check and stack overflow check,
1329   // so method is not locked if overflows.
1330   //
1331   if (synchronized) {
1332     // Allocate monitor and lock method
1333     lock_method();
1334   } else {
1335     // no synchronization necessary
1336 #ifdef ASSERT
1337       { Label L;
1338         __ movl(rax, access_flags);
1339         __ testl(rax, JVM_ACC_SYNCHRONIZED);
1340         __ jcc(Assembler::zero, L);
1341         __ stop("method needs synchronization");
1342         __ bind(L);
1343       }
1344 #endif
1345   }
1346 
1347   // start execution
1348 #ifdef ASSERT
1349   { Label L;
1350      const Address monitor_block_top (rbp,
1351                  frame::interpreter_frame_monitor_block_top_offset * wordSize);
1352     __ movptr(rax, monitor_block_top);
1353     __ cmpptr(rax, rsp);
1354     __ jcc(Assembler::equal, L);
1355     __ stop("broken stack frame setup in interpreter");
1356     __ bind(L);
1357   }
1358 #endif
1359 
1360   // jvmti support
1361   __ notify_method_entry();
1362 
1363   __ dispatch_next(vtos);
1364 
1365   // invocation counter overflow
1366   if (inc_counter) {
1367     if (ProfileInterpreter) {
1368       // We have decided to profile this method in the interpreter
1369       __ bind(profile_method);
1370 
1371       __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::profile_method), rsi, true);
1372 
1373       __ movptr(rbx, Address(rbp, method_offset));   // restore methodOop
1374       __ movptr(rax, Address(rbx, in_bytes(methodOopDesc::method_data_offset())));
1375       __ movptr(Address(rbp, frame::interpreter_frame_mdx_offset * wordSize), rax);
1376       __ test_method_data_pointer(rax, profile_method_continue);
1377       __ addptr(rax, in_bytes(methodDataOopDesc::data_offset()));
1378       __ movptr(Address(rbp, frame::interpreter_frame_mdx_offset * wordSize), rax);
1379       __ jmp(profile_method_continue);
1380     }
1381     // Handle overflow of counter and compile method
1382     __ bind(invocation_counter_overflow);
1383     generate_counter_overflow(&continue_after_compile);
1384   }
1385 
1386   return entry_point;
1387 }
1388 
1389 //------------------------------------------------------------------------------------------------------------------------
1390 // Entry points
1391 //
1392 // Here we generate the various kind of entries into the interpreter.
1393 // The two main entry type are generic bytecode methods and native call method.
1394 // These both come in synchronized and non-synchronized versions but the
1395 // frame layout they create is very similar. The other method entry
1396 // types are really just special purpose entries that are really entry
1397 // and interpretation all in one. These are for trivial methods like
1398 // accessor, empty, or special math methods.
1399 //
1400 // When control flow reaches any of the entry types for the interpreter
1401 // the following holds ->
1402 //
1403 // Arguments:
1404 //
1405 // rbx,: methodOop
1406 // rcx: receiver
1407 //
1408 //
1409 // Stack layout immediately at entry
1410 //
1411 // [ return address     ] <--- rsp
1412 // [ parameter n        ]
1413 //   ...
1414 // [ parameter 1        ]
1415 // [ expression stack   ] (caller's java expression stack)
1416 
1417 // Assuming that we don't go to one of the trivial specialized
1418 // entries the stack will look like below when we are ready to execute
1419 // the first bytecode (or call the native routine). The register usage
1420 // will be as the template based interpreter expects (see interpreter_x86.hpp).
1421 //
1422 // local variables follow incoming parameters immediately; i.e.
1423 // the return address is moved to the end of the locals).
1424 //
1425 // [ monitor entry      ] <--- rsp
1426 //   ...
1427 // [ monitor entry      ]
1428 // [ expr. stack bottom ]
1429 // [ saved rsi          ]
1430 // [ current rdi        ]
1431 // [ methodOop          ]
1432 // [ saved rbp,          ] <--- rbp,
1433 // [ return address     ]
1434 // [ local variable m   ]
1435 //   ...
1436 // [ local variable 1   ]
1437 // [ parameter n        ]
1438 //   ...
1439 // [ parameter 1        ] <--- rdi
1440 
1441 address AbstractInterpreterGenerator::generate_method_entry(AbstractInterpreter::MethodKind kind) {
1442   // determine code generation flags
1443   bool synchronized = false;
1444   address entry_point = NULL;
1445 
1446   switch (kind) {
1447     case Interpreter::zerolocals             :                                                                             break;
1448     case Interpreter::zerolocals_synchronized: synchronized = true;                                                        break;
1449     case Interpreter::native                 : entry_point = ((InterpreterGenerator*)this)->generate_native_entry(false);  break;
1450     case Interpreter::native_synchronized    : entry_point = ((InterpreterGenerator*)this)->generate_native_entry(true);   break;
1451     case Interpreter::empty                  : entry_point = ((InterpreterGenerator*)this)->generate_empty_entry();        break;
1452     case Interpreter::accessor               : entry_point = ((InterpreterGenerator*)this)->generate_accessor_entry();     break;
1453     case Interpreter::abstract               : entry_point = ((InterpreterGenerator*)this)->generate_abstract_entry();     break;
1454     case Interpreter::method_handle          : entry_point = ((InterpreterGenerator*)this)->generate_method_handle_entry(); break;
1455 
1456     case Interpreter::java_lang_math_sin     : // fall thru
1457     case Interpreter::java_lang_math_cos     : // fall thru
1458     case Interpreter::java_lang_math_tan     : // fall thru
1459     case Interpreter::java_lang_math_abs     : // fall thru
1460     case Interpreter::java_lang_math_log     : // fall thru
1461     case Interpreter::java_lang_math_log10   : // fall thru
1462     case Interpreter::java_lang_math_sqrt    : entry_point = ((InterpreterGenerator*)this)->generate_math_entry(kind);     break;
1463     default                                  : ShouldNotReachHere();                                                       break;
1464   }
1465 
1466   if (entry_point) return entry_point;
1467 
1468   return ((InterpreterGenerator*)this)->generate_normal_entry(synchronized);
1469 
1470 }
1471 
1472 // These should never be compiled since the interpreter will prefer
1473 // the compiled version to the intrinsic version.
1474 bool AbstractInterpreter::can_be_compiled(methodHandle m) {
1475   switch (method_kind(m)) {
1476     case Interpreter::java_lang_math_sin     : // fall thru
1477     case Interpreter::java_lang_math_cos     : // fall thru
1478     case Interpreter::java_lang_math_tan     : // fall thru
1479     case Interpreter::java_lang_math_abs     : // fall thru
1480     case Interpreter::java_lang_math_log     : // fall thru
1481     case Interpreter::java_lang_math_log10   : // fall thru
1482     case Interpreter::java_lang_math_sqrt    :
1483       return false;
1484     default:
1485       return true;
1486   }
1487 }
1488 
1489 // How much stack a method activation needs in words.
1490 int AbstractInterpreter::size_top_interpreter_activation(methodOop method) {
1491 
1492   const int stub_code = 4;  // see generate_call_stub
1493   // Save space for one monitor to get into the interpreted method in case
1494   // the method is synchronized
1495   int monitor_size    = method->is_synchronized() ?
1496                                 1*frame::interpreter_frame_monitor_size() : 0;
1497 
1498   // total overhead size: entry_size + (saved rbp, thru expr stack bottom).
1499   // be sure to change this if you add/subtract anything to/from the overhead area
1500   const int overhead_size = -frame::interpreter_frame_initial_sp_offset;
1501 
1502   const int extra_stack = methodOopDesc::extra_stack_entries();
1503   const int method_stack = (method->max_locals() + method->max_stack() + extra_stack) *
1504                            Interpreter::stackElementWords;
1505   return overhead_size + method_stack + stub_code;
1506 }
1507 
1508 // asm based interpreter deoptimization helpers
1509 
1510 int AbstractInterpreter::layout_activation(methodOop method,
1511                                            int tempcount,
1512                                            int popframe_extra_args,
1513                                            int moncount,
1514                                            int callee_param_count,
1515                                            int callee_locals,
1516                                            frame* caller,
1517                                            frame* interpreter_frame,
1518                                            bool is_top_frame) {
1519   // Note: This calculation must exactly parallel the frame setup
1520   // in AbstractInterpreterGenerator::generate_method_entry.
1521   // If interpreter_frame!=NULL, set up the method, locals, and monitors.
1522   // The frame interpreter_frame, if not NULL, is guaranteed to be the right size,
1523   // as determined by a previous call to this method.
1524   // It is also guaranteed to be walkable even though it is in a skeletal state
1525   // NOTE: return size is in words not bytes
1526 
1527   // fixed size of an interpreter frame:
1528   int max_locals = method->max_locals() * Interpreter::stackElementWords;
1529   int extra_locals = (method->max_locals() - method->size_of_parameters()) *
1530                      Interpreter::stackElementWords;
1531 
1532   int overhead = frame::sender_sp_offset - frame::interpreter_frame_initial_sp_offset;
1533 
1534   // Our locals were accounted for by the caller (or last_frame_adjust on the transistion)
1535   // Since the callee parameters already account for the callee's params we only need to account for
1536   // the extra locals.
1537 
1538 
1539   int size = overhead +
1540          ((callee_locals - callee_param_count)*Interpreter::stackElementWords) +
1541          (moncount*frame::interpreter_frame_monitor_size()) +
1542          tempcount*Interpreter::stackElementWords + popframe_extra_args;
1543 
1544   if (interpreter_frame != NULL) {
1545 #ifdef ASSERT
1546     if (!EnableMethodHandles)
1547       // @@@ FIXME: Should we correct interpreter_frame_sender_sp in the calling sequences?
1548       // Probably, since deoptimization doesn't work yet.
1549       assert(caller->unextended_sp() == interpreter_frame->interpreter_frame_sender_sp(), "Frame not properly walkable");
1550     assert(caller->sp() == interpreter_frame->sender_sp(), "Frame not properly walkable(2)");
1551 #endif
1552 
1553     interpreter_frame->interpreter_frame_set_method(method);
1554     // NOTE the difference in using sender_sp and interpreter_frame_sender_sp
1555     // interpreter_frame_sender_sp is the original sp of the caller (the unextended_sp)
1556     // and sender_sp is fp+8
1557     intptr_t* locals = interpreter_frame->sender_sp() + max_locals - 1;
1558 
1559     interpreter_frame->interpreter_frame_set_locals(locals);
1560     BasicObjectLock* montop = interpreter_frame->interpreter_frame_monitor_begin();
1561     BasicObjectLock* monbot = montop - moncount;
1562     interpreter_frame->interpreter_frame_set_monitor_end(monbot);
1563 
1564     // Set last_sp
1565     intptr_t*  rsp = (intptr_t*) monbot  -
1566                      tempcount*Interpreter::stackElementWords -
1567                      popframe_extra_args;
1568     interpreter_frame->interpreter_frame_set_last_sp(rsp);
1569 
1570     // All frames but the initial (oldest) interpreter frame we fill in have a
1571     // value for sender_sp that allows walking the stack but isn't
1572     // truly correct. Correct the value here.
1573 
1574     if (extra_locals != 0 &&
1575         interpreter_frame->sender_sp() == interpreter_frame->interpreter_frame_sender_sp() ) {
1576       interpreter_frame->set_interpreter_frame_sender_sp(caller->sp() + extra_locals);
1577     }
1578     *interpreter_frame->interpreter_frame_cache_addr() =
1579       method->constants()->cache();
1580   }
1581   return size;
1582 }
1583 
1584 
1585 //------------------------------------------------------------------------------------------------------------------------
1586 // Exceptions
1587 
1588 void TemplateInterpreterGenerator::generate_throw_exception() {
1589   // Entry point in previous activation (i.e., if the caller was interpreted)
1590   Interpreter::_rethrow_exception_entry = __ pc();
1591   const Register thread = rcx;
1592 
1593   // Restore sp to interpreter_frame_last_sp even though we are going
1594   // to empty the expression stack for the exception processing.
1595   __ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), NULL_WORD);
1596   // rax,: exception
1597   // rdx: return address/pc that threw exception
1598   __ restore_bcp();                              // rsi points to call/send
1599   __ restore_locals();
1600 
1601   // Entry point for exceptions thrown within interpreter code
1602   Interpreter::_throw_exception_entry = __ pc();
1603   // expression stack is undefined here
1604   // rax,: exception
1605   // rsi: exception bcp
1606   __ verify_oop(rax);
1607 
1608   // expression stack must be empty before entering the VM in case of an exception
1609   __ empty_expression_stack();
1610   __ empty_FPU_stack();
1611   // find exception handler address and preserve exception oop
1612   __ call_VM(rdx, CAST_FROM_FN_PTR(address, InterpreterRuntime::exception_handler_for_exception), rax);
1613   // rax,: exception handler entry point
1614   // rdx: preserved exception oop
1615   // rsi: bcp for exception handler
1616   __ push_ptr(rdx);                              // push exception which is now the only value on the stack
1617   __ jmp(rax);                                   // jump to exception handler (may be _remove_activation_entry!)
1618 
1619   // If the exception is not handled in the current frame the frame is removed and
1620   // the exception is rethrown (i.e. exception continuation is _rethrow_exception).
1621   //
1622   // Note: At this point the bci is still the bxi for the instruction which caused
1623   //       the exception and the expression stack is empty. Thus, for any VM calls
1624   //       at this point, GC will find a legal oop map (with empty expression stack).
1625 
1626   // In current activation
1627   // tos: exception
1628   // rsi: exception bcp
1629 
1630   //
1631   // JVMTI PopFrame support
1632   //
1633 
1634    Interpreter::_remove_activation_preserving_args_entry = __ pc();
1635   __ empty_expression_stack();
1636   __ empty_FPU_stack();
1637   // Set the popframe_processing bit in pending_popframe_condition indicating that we are
1638   // currently handling popframe, so that call_VMs that may happen later do not trigger new
1639   // popframe handling cycles.
1640   __ get_thread(thread);
1641   __ movl(rdx, Address(thread, JavaThread::popframe_condition_offset()));
1642   __ orl(rdx, JavaThread::popframe_processing_bit);
1643   __ movl(Address(thread, JavaThread::popframe_condition_offset()), rdx);
1644 
1645   {
1646     // Check to see whether we are returning to a deoptimized frame.
1647     // (The PopFrame call ensures that the caller of the popped frame is
1648     // either interpreted or compiled and deoptimizes it if compiled.)
1649     // In this case, we can't call dispatch_next() after the frame is
1650     // popped, but instead must save the incoming arguments and restore
1651     // them after deoptimization has occurred.
1652     //
1653     // Note that we don't compare the return PC against the
1654     // deoptimization blob's unpack entry because of the presence of
1655     // adapter frames in C2.
1656     Label caller_not_deoptimized;
1657     __ movptr(rdx, Address(rbp, frame::return_addr_offset * wordSize));
1658     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::interpreter_contains), rdx);
1659     __ testl(rax, rax);
1660     __ jcc(Assembler::notZero, caller_not_deoptimized);
1661 
1662     // Compute size of arguments for saving when returning to deoptimized caller
1663     __ get_method(rax);
1664     __ verify_oop(rax);
1665     __ load_unsigned_short(rax, Address(rax, in_bytes(methodOopDesc::size_of_parameters_offset())));
1666     __ shlptr(rax, Interpreter::logStackElementSize);
1667     __ restore_locals();
1668     __ subptr(rdi, rax);
1669     __ addptr(rdi, wordSize);
1670     // Save these arguments
1671     __ get_thread(thread);
1672     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, Deoptimization::popframe_preserve_args), thread, rax, rdi);
1673 
1674     __ remove_activation(vtos, rdx,
1675                          /* throw_monitor_exception */ false,
1676                          /* install_monitor_exception */ false,
1677                          /* notify_jvmdi */ false);
1678 
1679     // Inform deoptimization that it is responsible for restoring these arguments
1680     __ get_thread(thread);
1681     __ movl(Address(thread, JavaThread::popframe_condition_offset()), JavaThread::popframe_force_deopt_reexecution_bit);
1682 
1683     // Continue in deoptimization handler
1684     __ jmp(rdx);
1685 
1686     __ bind(caller_not_deoptimized);
1687   }
1688 
1689   __ remove_activation(vtos, rdx,
1690                        /* throw_monitor_exception */ false,
1691                        /* install_monitor_exception */ false,
1692                        /* notify_jvmdi */ false);
1693 
1694   // Finish with popframe handling
1695   // A previous I2C followed by a deoptimization might have moved the
1696   // outgoing arguments further up the stack. PopFrame expects the
1697   // mutations to those outgoing arguments to be preserved and other
1698   // constraints basically require this frame to look exactly as
1699   // though it had previously invoked an interpreted activation with
1700   // no space between the top of the expression stack (current
1701   // last_sp) and the top of stack. Rather than force deopt to
1702   // maintain this kind of invariant all the time we call a small
1703   // fixup routine to move the mutated arguments onto the top of our
1704   // expression stack if necessary.
1705   __ mov(rax, rsp);
1706   __ movptr(rbx, Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize));
1707   __ get_thread(thread);
1708   // PC must point into interpreter here
1709   __ set_last_Java_frame(thread, noreg, rbp, __ pc());
1710   __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::popframe_move_outgoing_args), thread, rax, rbx);
1711   __ get_thread(thread);
1712   __ reset_last_Java_frame(thread, true, true);
1713   // Restore the last_sp and null it out
1714   __ movptr(rsp, Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize));
1715   __ movptr(Address(rbp, frame::interpreter_frame_last_sp_offset * wordSize), NULL_WORD);
1716 
1717   __ restore_bcp();
1718   __ restore_locals();
1719   // The method data pointer was incremented already during
1720   // call profiling. We have to restore the mdp for the current bcp.
1721   if (ProfileInterpreter) {
1722     __ set_method_data_pointer_for_bcp();
1723   }
1724 
1725   // Clear the popframe condition flag
1726   __ get_thread(thread);
1727   __ movl(Address(thread, JavaThread::popframe_condition_offset()), JavaThread::popframe_inactive);
1728 
1729   __ dispatch_next(vtos);
1730   // end of PopFrame support
1731 
1732   Interpreter::_remove_activation_entry = __ pc();
1733 
1734   // preserve exception over this code sequence
1735   __ pop_ptr(rax);
1736   __ get_thread(thread);
1737   __ movptr(Address(thread, JavaThread::vm_result_offset()), rax);
1738   // remove the activation (without doing throws on illegalMonitorExceptions)
1739   __ remove_activation(vtos, rdx, false, true, false);
1740   // restore exception
1741   __ get_thread(thread);
1742   __ movptr(rax, Address(thread, JavaThread::vm_result_offset()));
1743   __ movptr(Address(thread, JavaThread::vm_result_offset()), NULL_WORD);
1744   __ verify_oop(rax);
1745 
1746   // Inbetween activations - previous activation type unknown yet
1747   // compute continuation point - the continuation point expects
1748   // the following registers set up:
1749   //
1750   // rax: exception
1751   // rdx: return address/pc that threw exception
1752   // rsp: expression stack of caller
1753   // rbp: rbp, of caller
1754   __ push(rax);                                  // save exception
1755   __ push(rdx);                                  // save return address
1756   __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address), thread, rdx);
1757   __ mov(rbx, rax);                              // save exception handler
1758   __ pop(rdx);                                   // restore return address
1759   __ pop(rax);                                   // restore exception
1760   // Note that an "issuing PC" is actually the next PC after the call
1761   __ jmp(rbx);                                   // jump to exception handler of caller
1762 }
1763 
1764 
1765 //
1766 // JVMTI ForceEarlyReturn support
1767 //
1768 address TemplateInterpreterGenerator::generate_earlyret_entry_for(TosState state) {
1769   address entry = __ pc();
1770   const Register thread = rcx;
1771 
1772   __ restore_bcp();
1773   __ restore_locals();
1774   __ empty_expression_stack();
1775   __ empty_FPU_stack();
1776   __ load_earlyret_value(state);
1777 
1778   __ get_thread(thread);
1779   __ movptr(rcx, Address(thread, JavaThread::jvmti_thread_state_offset()));
1780   const Address cond_addr(rcx, JvmtiThreadState::earlyret_state_offset());
1781 
1782   // Clear the earlyret state
1783   __ movl(cond_addr, JvmtiThreadState::earlyret_inactive);
1784 
1785   __ remove_activation(state, rsi,
1786                        false, /* throw_monitor_exception */
1787                        false, /* install_monitor_exception */
1788                        true); /* notify_jvmdi */
1789   __ jmp(rsi);
1790   return entry;
1791 } // end of ForceEarlyReturn support
1792 
1793 
1794 //------------------------------------------------------------------------------------------------------------------------
1795 // Helper for vtos entry point generation
1796 
1797 void TemplateInterpreterGenerator::set_vtos_entry_points (Template* t, address& bep, address& cep, address& sep, address& aep, address& iep, address& lep, address& fep, address& dep, address& vep) {
1798   assert(t->is_valid() && t->tos_in() == vtos, "illegal template");
1799   Label L;
1800   fep = __ pc(); __ push(ftos); __ jmp(L);
1801   dep = __ pc(); __ push(dtos); __ jmp(L);
1802   lep = __ pc(); __ push(ltos); __ jmp(L);
1803   aep = __ pc(); __ push(atos); __ jmp(L);
1804   bep = cep = sep =             // fall through
1805   iep = __ pc(); __ push(itos); // fall through
1806   vep = __ pc(); __ bind(L);    // fall through
1807   generate_and_dispatch(t);
1808 }
1809 
1810 //------------------------------------------------------------------------------------------------------------------------
1811 // Generation of individual instructions
1812 
1813 // helpers for generate_and_dispatch
1814 
1815 
1816 
1817 InterpreterGenerator::InterpreterGenerator(StubQueue* code)
1818  : TemplateInterpreterGenerator(code) {
1819    generate_all(); // down here so it can be "virtual"
1820 }
1821 
1822 //------------------------------------------------------------------------------------------------------------------------
1823 
1824 // Non-product code
1825 #ifndef PRODUCT
1826 address TemplateInterpreterGenerator::generate_trace_code(TosState state) {
1827   address entry = __ pc();
1828 
1829   // prepare expression stack
1830   __ pop(rcx);          // pop return address so expression stack is 'pure'
1831   __ push(state);       // save tosca
1832 
1833   // pass tosca registers as arguments & call tracer
1834   __ call_VM(noreg, CAST_FROM_FN_PTR(address, SharedRuntime::trace_bytecode), rcx, rax, rdx);
1835   __ mov(rcx, rax);     // make sure return address is not destroyed by pop(state)
1836   __ pop(state);        // restore tosca
1837 
1838   // return
1839   __ jmp(rcx);
1840 
1841   return entry;
1842 }
1843 
1844 
1845 void TemplateInterpreterGenerator::count_bytecode() {
1846   __ incrementl(ExternalAddress((address) &BytecodeCounter::_counter_value));
1847 }
1848 
1849 
1850 void TemplateInterpreterGenerator::histogram_bytecode(Template* t) {
1851   __ incrementl(ExternalAddress((address) &BytecodeHistogram::_counters[t->bytecode()]));
1852 }
1853 
1854 
1855 void TemplateInterpreterGenerator::histogram_bytecode_pair(Template* t) {
1856   __ mov32(ExternalAddress((address) &BytecodePairHistogram::_index), rbx);
1857   __ shrl(rbx, BytecodePairHistogram::log2_number_of_codes);
1858   __ orl(rbx, ((int)t->bytecode()) << BytecodePairHistogram::log2_number_of_codes);
1859   ExternalAddress table((address) BytecodePairHistogram::_counters);
1860   Address index(noreg, rbx, Address::times_4);
1861   __ incrementl(ArrayAddress(table, index));
1862 }
1863 
1864 
1865 void TemplateInterpreterGenerator::trace_bytecode(Template* t) {
1866   // Call a little run-time stub to avoid blow-up for each bytecode.
1867   // The run-time runtime saves the right registers, depending on
1868   // the tosca in-state for the given template.
1869   assert(Interpreter::trace_code(t->tos_in()) != NULL,
1870          "entry must have been generated");
1871   __ call(RuntimeAddress(Interpreter::trace_code(t->tos_in())));
1872 }
1873 
1874 
1875 void TemplateInterpreterGenerator::stop_interpreter_at() {
1876   Label L;
1877   __ cmp32(ExternalAddress((address) &BytecodeCounter::_counter_value),
1878            StopInterpreterAt);
1879   __ jcc(Assembler::notEqual, L);
1880   __ int3();
1881   __ bind(L);
1882 }
1883 #endif // !PRODUCT
1884 #endif // CC_INTERP