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