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