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