cppInterpreter_x86.cpp 93.8 KB
Newer Older
D
duke 已提交
1
/*
X
xdono 已提交
2
 * Copyright 2007-2008 Sun Microsystems, Inc.  All Rights Reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 *
 */

#include "incls/_precompiled.incl"
#include "incls/_cppInterpreter_x86.cpp.incl"

#ifdef CC_INTERP

// Routine exists to make tracebacks look decent in debugger
// while we are recursed in the frame manager/c++ interpreter.
// We could use an address in the frame manager but having
// frames look natural in the debugger is a plus.
extern "C" void RecursiveInterpreterActivation(interpreterState istate )
{
  //
  ShouldNotReachHere();
}


#define __ _masm->
#define STATE(field_name) (Address(state, byte_offset_of(BytecodeInterpreter, field_name)))

Label fast_accessor_slow_entry_path;  // fast accessor methods need to be able to jmp to unsynchronized
                                      // c++ interpreter entry point this holds that entry point label.

47 48 49 50 51 52 53 54
// default registers for state and sender_sp
// state and sender_sp are the same on 32bit because we have no choice.
// state could be rsi on 64bit but it is an arg reg and not callee save
// so r13 is better choice.

const Register state = NOT_LP64(rsi) LP64_ONLY(r13);
const Register sender_sp_on_entry = NOT_LP64(rsi) LP64_ONLY(r13);

D
duke 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
// NEEDED for JVMTI?
// address AbstractInterpreter::_remove_activation_preserving_args_entry;

static address unctrap_frame_manager_entry  = NULL;

static address deopt_frame_manager_return_atos  = NULL;
static address deopt_frame_manager_return_btos  = NULL;
static address deopt_frame_manager_return_itos  = NULL;
static address deopt_frame_manager_return_ltos  = NULL;
static address deopt_frame_manager_return_ftos  = NULL;
static address deopt_frame_manager_return_dtos  = NULL;
static address deopt_frame_manager_return_vtos  = NULL;

int AbstractInterpreter::BasicType_as_index(BasicType type) {
  int i = 0;
  switch (type) {
    case T_BOOLEAN: i = 0; break;
    case T_CHAR   : i = 1; break;
    case T_BYTE   : i = 2; break;
    case T_SHORT  : i = 3; break;
    case T_INT    : i = 4; break;
    case T_VOID   : i = 5; break;
    case T_FLOAT  : i = 8; break;
    case T_LONG   : i = 9; break;
    case T_DOUBLE : i = 6; break;
    case T_OBJECT : // fall through
    case T_ARRAY  : i = 7; break;
    default       : ShouldNotReachHere();
  }
  assert(0 <= i && i < AbstractInterpreter::number_of_result_handlers, "index out of bounds");
  return i;
}

// Is this pc anywhere within code owned by the interpreter?
// This only works for pc that might possibly be exposed to frame
// walkers. It clearly misses all of the actual c++ interpreter
// implementation
bool CppInterpreter::contains(address pc)            {
    return (_code->contains(pc) ||
            pc == CAST_FROM_FN_PTR(address, RecursiveInterpreterActivation));
}


address CppInterpreterGenerator::generate_result_handler_for(BasicType type) {
  address entry = __ pc();
  switch (type) {
    case T_BOOLEAN: __ c2bool(rax);            break;
    case T_CHAR   : __ andl(rax, 0xFFFF);      break;
    case T_BYTE   : __ sign_extend_byte (rax); break;
    case T_SHORT  : __ sign_extend_short(rax); break;
    case T_VOID   : // fall thru
    case T_LONG   : // fall thru
    case T_INT    : /* nothing to do */        break;
108

D
duke 已提交
109 110
    case T_DOUBLE :
    case T_FLOAT  :
111 112 113
      {
        const Register t = InterpreterRuntime::SignatureHandlerGenerator::temp();
        __ pop(t);                            // remove return address first
D
duke 已提交
114 115 116 117
        // Must return a result for interpreter or compiler. In SSE
        // mode, results are returned in xmm0 and the FPU stack must
        // be empty.
        if (type == T_FLOAT && UseSSE >= 1) {
118
#ifndef _LP64
D
duke 已提交
119 120 121 122
          // Load ST0
          __ fld_d(Address(rsp, 0));
          // Store as float and empty fpu stack
          __ fstp_s(Address(rsp, 0));
123
#endif // !_LP64
D
duke 已提交
124 125 126 127 128 129 130 131 132
          // and reload
          __ movflt(xmm0, Address(rsp, 0));
        } else if (type == T_DOUBLE && UseSSE >= 2 ) {
          __ movdbl(xmm0, Address(rsp, 0));
        } else {
          // restore ST0
          __ fld_d(Address(rsp, 0));
        }
        // and pop the temp
133 134
        __ addptr(rsp, 2 * wordSize);
        __ push(t);                            // restore return address
D
duke 已提交
135 136 137 138
      }
      break;
    case T_OBJECT :
      // retrieve result from frame
139
      __ movptr(rax, STATE(_oop_temp));
D
duke 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
      // and verify it
      __ verify_oop(rax);
      break;
    default       : ShouldNotReachHere();
  }
  __ ret(0);                                   // return from result handler
  return entry;
}

// tosca based result to c++ interpreter stack based result.
// Result goes to top of native stack.

#undef EXTEND  // SHOULD NOT BE NEEDED
address CppInterpreterGenerator::generate_tosca_to_stack_converter(BasicType type) {
  // A result is in the tosca (abi result) from either a native method call or compiled
  // code. Place this result on the java expression stack so C++ interpreter can use it.
  address entry = __ pc();

  const Register t = InterpreterRuntime::SignatureHandlerGenerator::temp();
159
  __ pop(t);                            // remove return address first
D
duke 已提交
160 161 162 163 164 165 166
  switch (type) {
    case T_VOID:
       break;
    case T_BOOLEAN:
#ifdef EXTEND
      __ c2bool(rax);
#endif
167
      __ push(rax);
D
duke 已提交
168 169 170 171 172
      break;
    case T_CHAR   :
#ifdef EXTEND
      __ andl(rax, 0xFFFF);
#endif
173
      __ push(rax);
D
duke 已提交
174 175 176 177 178
      break;
    case T_BYTE   :
#ifdef EXTEND
      __ sign_extend_byte (rax);
#endif
179
      __ push(rax);
D
duke 已提交
180 181 182 183 184
      break;
    case T_SHORT  :
#ifdef EXTEND
      __ sign_extend_short(rax);
#endif
185
      __ push(rax);
D
duke 已提交
186 187
      break;
    case T_LONG    :
188 189
      __ push(rdx);                             // pushes useless junk on 64bit
      __ push(rax);
D
duke 已提交
190 191
      break;
    case T_INT    :
192
      __ push(rax);
D
duke 已提交
193 194
      break;
    case T_FLOAT  :
195 196
      // Result is in ST(0)/xmm0
      __ subptr(rsp, wordSize);
D
duke 已提交
197
      if ( UseSSE < 1) {
198
        __ fstp_s(Address(rsp, 0));
D
duke 已提交
199 200 201 202 203
      } else {
        __ movflt(Address(rsp, 0), xmm0);
      }
      break;
    case T_DOUBLE  :
204
      __ subptr(rsp, 2*wordSize);
D
duke 已提交
205
      if ( UseSSE < 2 ) {
206
        __ fstp_d(Address(rsp, 0));
D
duke 已提交
207 208 209 210 211 212
      } else {
        __ movdbl(Address(rsp, 0), xmm0);
      }
      break;
    case T_OBJECT :
      __ verify_oop(rax);                      // verify it
213
      __ push(rax);
D
duke 已提交
214 215 216 217 218 219 220 221 222 223 224
      break;
    default       : ShouldNotReachHere();
  }
  __ jmp(t);                                   // return from result handler
  return entry;
}

address CppInterpreterGenerator::generate_stack_to_stack_converter(BasicType type) {
  // A result is in the java expression stack of the interpreted method that has just
  // returned. Place this result on the java expression stack of the caller.
  //
225
  // The current interpreter activation in rsi/r13 is for the method just returning its
D
duke 已提交
226 227 228 229 230 231 232 233 234
  // result. So we know that the result of this method is on the top of the current
  // execution stack (which is pre-pushed) and will be return to the top of the caller
  // stack. The top of the callers stack is the bottom of the locals of the current
  // activation.
  // Because of the way activation are managed by the frame manager the value of rsp is
  // below both the stack top of the current activation and naturally the stack top
  // of the calling activation. This enable this routine to leave the return address
  // to the frame manager on the stack and do a vanilla return.
  //
235 236
  // On entry: rsi/r13 - interpreter state of activation returning a (potential) result
  // On Return: rsi/r13 - unchanged
D
duke 已提交
237 238 239 240 241 242 243 244 245
  //            rax - new stack top for caller activation (i.e. activation in _prev_link)
  //
  // Can destroy rdx, rcx.
  //

  address entry = __ pc();
  const Register t = InterpreterRuntime::SignatureHandlerGenerator::temp();
  switch (type) {
    case T_VOID:
246 247
      __ movptr(rax, STATE(_locals));                                   // pop parameters get new stack value
      __ addptr(rax, wordSize);                                         // account for prepush before we return
D
duke 已提交
248 249 250 251 252 253 254 255
      break;
    case T_FLOAT  :
    case T_BOOLEAN:
    case T_CHAR   :
    case T_BYTE   :
    case T_SHORT  :
    case T_INT    :
      // 1 word result
256 257
      __ movptr(rdx, STATE(_stack));
      __ movptr(rax, STATE(_locals));                                   // address for result
D
duke 已提交
258
      __ movl(rdx, Address(rdx, wordSize));                             // get result
259
      __ movptr(Address(rax, 0), rdx);                                  // and store it
D
duke 已提交
260 261 262 263 264 265 266 267
      break;
    case T_LONG    :
    case T_DOUBLE  :
      // return top two words on current expression stack to caller's expression stack
      // The caller's expression stack is adjacent to the current frame manager's intepretState
      // except we allocated one extra word for this intepretState so we won't overwrite it
      // when we return a two word result.

268 269 270 271 272 273 274
      __ movptr(rax, STATE(_locals));                                   // address for result
      __ movptr(rcx, STATE(_stack));
      __ subptr(rax, wordSize);                                         // need addition word besides locals[0]
      __ movptr(rdx, Address(rcx, 2*wordSize));                         // get result word (junk in 64bit)
      __ movptr(Address(rax, wordSize), rdx);                           // and store it
      __ movptr(rdx, Address(rcx, wordSize));                           // get result word
      __ movptr(Address(rax, 0), rdx);                                  // and store it
D
duke 已提交
275 276
      break;
    case T_OBJECT :
277 278 279
      __ movptr(rdx, STATE(_stack));
      __ movptr(rax, STATE(_locals));                                   // address for result
      __ movptr(rdx, Address(rdx, wordSize));                           // get result
D
duke 已提交
280
      __ verify_oop(rdx);                                               // verify it
281
      __ movptr(Address(rax, 0), rdx);                                  // and store it
D
duke 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
      break;
    default       : ShouldNotReachHere();
  }
  __ ret(0);
  return entry;
}

address CppInterpreterGenerator::generate_stack_to_native_abi_converter(BasicType type) {
  // A result is in the java expression stack of the interpreted method that has just
  // returned. Place this result in the native abi that the caller expects.
  //
  // Similar to generate_stack_to_stack_converter above. Called at a similar time from the
  // frame manager execept in this situation the caller is native code (c1/c2/call_stub)
  // and so rather than return result onto caller's java expression stack we return the
  // result in the expected location based on the native abi.
297 298
  // On entry: rsi/r13 - interpreter state of activation returning a (potential) result
  // On Return: rsi/r13 - unchanged
D
duke 已提交
299 300 301 302 303 304 305 306 307 308 309
  // Other registers changed [rax/rdx/ST(0) as needed for the result returned]

  address entry = __ pc();
  switch (type) {
    case T_VOID:
       break;
    case T_BOOLEAN:
    case T_CHAR   :
    case T_BYTE   :
    case T_SHORT  :
    case T_INT    :
310
      __ movptr(rdx, STATE(_stack));                                    // get top of stack
D
duke 已提交
311 312 313
      __ movl(rax, Address(rdx, wordSize));                             // get result word 1
      break;
    case T_LONG    :
314 315 316
      __ movptr(rdx, STATE(_stack));                                    // get top of stack
      __ movptr(rax, Address(rdx, wordSize));                           // get result low word
      NOT_LP64(__ movl(rdx, Address(rdx, 2*wordSize));)                 // get result high word
D
duke 已提交
317 318
      break;
    case T_FLOAT  :
319
      __ movptr(rdx, STATE(_stack));                                    // get top of stack
D
duke 已提交
320 321 322 323 324 325 326
      if ( UseSSE >= 1) {
        __ movflt(xmm0, Address(rdx, wordSize));
      } else {
        __ fld_s(Address(rdx, wordSize));                               // pushd float result
      }
      break;
    case T_DOUBLE  :
327
      __ movptr(rdx, STATE(_stack));                                    // get top of stack
D
duke 已提交
328 329 330 331 332 333 334
      if ( UseSSE > 1) {
        __ movdbl(xmm0, Address(rdx, wordSize));
      } else {
        __ fld_d(Address(rdx, wordSize));                               // push double result
      }
      break;
    case T_OBJECT :
335 336
      __ movptr(rdx, STATE(_stack));                                    // get top of stack
      __ movptr(rax, Address(rdx, wordSize));                           // get result word 1
D
duke 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
      __ verify_oop(rax);                                               // verify it
      break;
    default       : ShouldNotReachHere();
  }
  __ ret(0);
  return entry;
}

address CppInterpreter::return_entry(TosState state, int length) {
  // make it look good in the debugger
  return CAST_FROM_FN_PTR(address, RecursiveInterpreterActivation);
}

address CppInterpreter::deopt_entry(TosState state, int length) {
  address ret = NULL;
  if (length != 0) {
    switch (state) {
      case atos: ret = deopt_frame_manager_return_atos; break;
      case btos: ret = deopt_frame_manager_return_btos; break;
      case ctos:
      case stos:
      case itos: ret = deopt_frame_manager_return_itos; break;
      case ltos: ret = deopt_frame_manager_return_ltos; break;
      case ftos: ret = deopt_frame_manager_return_ftos; break;
      case dtos: ret = deopt_frame_manager_return_dtos; break;
      case vtos: ret = deopt_frame_manager_return_vtos; break;
    }
  } else {
    ret = unctrap_frame_manager_entry;  // re-execute the bytecode ( e.g. uncommon trap)
  }
  assert(ret != NULL, "Not initialized");
  return ret;
}

// C++ Interpreter
void CppInterpreterGenerator::generate_compute_interpreter_state(const Register state,
                                                                 const Register locals,
                                                                 const Register sender_sp,
                                                                 bool native) {

  // On entry the "locals" argument points to locals[0] (or where it would be in case no locals in
  // a static method). "state" contains any previous frame manager state which we must save a link
  // to in the newly generated state object. On return "state" is a pointer to the newly allocated
  // state object. We must allocate and initialize a new interpretState object and the method
  // expression stack. Because the returned result (if any) of the method will be placed on the caller's
  // expression stack and this will overlap with locals[0] (and locals[1] if double/long) we must
  // be sure to leave space on the caller's stack so that this result will not overwrite values when
  // locals[0] and locals[1] do not exist (and in fact are return address and saved rbp). So when
  // we are non-native we in essence ensure that locals[0-1] exist. We play an extra trick in
  // non-product builds and initialize this last local with the previous interpreterState as
  // this makes things look real nice in the debugger.

  // State on entry
  // Assumes locals == &locals[0]
  // Assumes state == any previous frame manager state (assuming call path from c++ interpreter)
  // Assumes rax = return address
  // rcx == senders_sp
  // rbx == method
  // Modifies rcx, rdx, rax
  // Returns:
  // state == address of new interpreterState
  // rsp == bottom of method's expression stack.

  const Address const_offset      (rbx, methodOopDesc::const_offset());


  // On entry sp is the sender's sp. This includes the space for the arguments
  // that the sender pushed. If the sender pushed no args (a static) and the
  // caller returns a long then we need two words on the sender's stack which
  // are not present (although when we return a restore full size stack the
  // space will be present). If we didn't allocate two words here then when
  // we "push" the result of the caller's stack we would overwrite the return
  // address and the saved rbp. Not good. So simply allocate 2 words now
  // just to be safe. This is the "static long no_params() method" issue.
  // See Lo.java for a testcase.
  // We don't need this for native calls because they return result in
  // register and the stack is expanded in the caller before we store
  // the results on the stack.

  if (!native) {
#ifdef PRODUCT
418
    __ subptr(rsp, 2*wordSize);
D
duke 已提交
419
#else /* PRODUCT */
420 421
    __ push((int32_t)NULL_WORD);
    __ push(state);                         // make it look like a real argument
D
duke 已提交
422 423 424 425 426
#endif /* PRODUCT */
  }

  // Now that we are assure of space for stack result, setup typical linkage

427
  __ push(rax);
D
duke 已提交
428 429
  __ enter();

430
  __ mov(rax, state);                                  // save current state
D
duke 已提交
431

432 433
  __ lea(rsp, Address(rsp, -(int)sizeof(BytecodeInterpreter)));
  __ mov(state, rsp);
D
duke 已提交
434

435
  // rsi/r13 == state/locals rax == prevstate
D
duke 已提交
436 437 438 439

  // initialize the "shadow" frame so that use since C++ interpreter not directly
  // recursive. Simpler to recurse but we can't trim expression stack as we call
  // new methods.
440 441 442 443 444 445 446
  __ movptr(STATE(_locals), locals);                    // state->_locals = locals()
  __ movptr(STATE(_self_link), state);                  // point to self
  __ movptr(STATE(_prev_link), rax);                    // state->_link = state on entry (NULL or previous state)
  __ movptr(STATE(_sender_sp), sender_sp);              // state->_sender_sp = sender_sp
#ifdef _LP64
  __ movptr(STATE(_thread), r15_thread);                // state->_bcp = codes()
#else
D
duke 已提交
447
  __ get_thread(rax);                                   // get vm's javathread*
448 449 450 451
  __ movptr(STATE(_thread), rax);                       // state->_bcp = codes()
#endif // _LP64
  __ movptr(rdx, Address(rbx, methodOopDesc::const_offset())); // get constantMethodOop
  __ lea(rdx, Address(rdx, constMethodOopDesc::codes_offset())); // get code base
D
duke 已提交
452
  if (native) {
453
    __ movptr(STATE(_bcp), (int32_t)NULL_WORD);         // state->_bcp = NULL
D
duke 已提交
454
  } else {
455
    __ movptr(STATE(_bcp), rdx);                        // state->_bcp = codes()
D
duke 已提交
456
  }
457 458 459 460 461 462
  __ xorptr(rdx, rdx);
  __ movptr(STATE(_oop_temp), rdx);                     // state->_oop_temp = NULL (only really needed for native)
  __ movptr(STATE(_mdx), rdx);                          // state->_mdx = NULL
  __ movptr(rdx, Address(rbx, methodOopDesc::constants_offset()));
  __ movptr(rdx, Address(rdx, constantPoolOopDesc::cache_offset_in_bytes()));
  __ movptr(STATE(_constants), rdx);                    // state->_constants = constants()
D
duke 已提交
463

464 465 466
  __ movptr(STATE(_method), rbx);                       // state->_method = method()
  __ movl(STATE(_msg), (int32_t) BytecodeInterpreter::method_entry);   // state->_msg = initial method entry
  __ movptr(STATE(_result._to_call._callee), (int32_t) NULL_WORD); // state->_result._to_call._callee_callee = NULL
D
duke 已提交
467 468


469
  __ movptr(STATE(_monitor_base), rsp);                 // set monitor block bottom (grows down) this would point to entry [0]
D
duke 已提交
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
                                                        // entries run from -1..x where &monitor[x] ==

  {
    // Must not attempt to lock method until we enter interpreter as gc won't be able to find the
    // initial frame. However we allocate a free monitor so we don't have to shuffle the expression stack
    // immediately.

    // synchronize method
    const Address access_flags      (rbx, methodOopDesc::access_flags_offset());
    const int entry_size            = frame::interpreter_frame_monitor_size() * wordSize;
    Label not_synced;

    __ movl(rax, access_flags);
    __ testl(rax, JVM_ACC_SYNCHRONIZED);
    __ jcc(Assembler::zero, not_synced);

    // Allocate initial monitor and pre initialize it
    // get synchronization object

    Label done;
    const int mirror_offset = klassOopDesc::klass_part_offset_in_bytes() + Klass::java_mirror_offset_in_bytes();
    __ movl(rax, access_flags);
    __ testl(rax, JVM_ACC_STATIC);
493
    __ movptr(rax, Address(locals, 0));                   // get receiver (assume this is frequent case)
D
duke 已提交
494
    __ jcc(Assembler::zero, done);
495 496 497
    __ movptr(rax, Address(rbx, methodOopDesc::constants_offset()));
    __ movptr(rax, Address(rax, constantPoolOopDesc::pool_holder_offset_in_bytes()));
    __ movptr(rax, Address(rax, mirror_offset));
D
duke 已提交
498 499
    __ bind(done);
    // add space for monitor & lock
500 501
    __ subptr(rsp, entry_size);                                           // add space for a monitor entry
    __ movptr(Address(rsp, BasicObjectLock::obj_offset_in_bytes()), rax); // store object
D
duke 已提交
502 503 504
    __ bind(not_synced);
  }

505
  __ movptr(STATE(_stack_base), rsp);                                     // set expression stack base ( == &monitors[-count])
D
duke 已提交
506
  if (native) {
507 508
    __ movptr(STATE(_stack), rsp);                                        // set current expression stack tos
    __ movptr(STATE(_stack_limit), rsp);
D
duke 已提交
509
  } else {
510 511
    __ subptr(rsp, wordSize);                                             // pre-push stack
    __ movptr(STATE(_stack), rsp);                                        // set current expression stack tos
D
duke 已提交
512 513 514 515 516

    // compute full expression stack limit

    const Address size_of_stack    (rbx, methodOopDesc::max_stack_offset());
    __ load_unsigned_word(rdx, size_of_stack);                            // get size of expression stack in words
517
    __ negptr(rdx);                                                       // so we can subtract in next step
D
duke 已提交
518
    // Allocate expression stack
519 520
    __ lea(rsp, Address(rsp, rdx, Address::times_ptr));
    __ movptr(STATE(_stack_limit), rsp);
D
duke 已提交
521 522
  }

523 524 525 526 527 528 529 530
#ifdef _LP64
  // Make sure stack is properly aligned and sized for the abi
  __ subptr(rsp, frame::arg_reg_save_area_bytes); // windows
  __ andptr(rsp, -16); // must be 16 byte boundry (see amd64 ABI)
#endif // _LP64



D
duke 已提交
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
}

// Helpers for commoning out cases in the various type of method entries.
//

// increment invocation count & check for overflow
//
// Note: checking for negative value instead of overflow
//       so we have a 'sticky' overflow test
//
// rbx,: method
// rcx: invocation counter
//
void InterpreterGenerator::generate_counter_incr(Label* overflow, Label* profile_method, Label* profile_method_continue) {

  const Address invocation_counter(rbx, methodOopDesc::invocation_counter_offset() + InvocationCounter::counter_offset());
  const Address backedge_counter  (rbx, methodOopDesc::backedge_counter_offset() + InvocationCounter::counter_offset());

  if (ProfileInterpreter) { // %%% Merge this into methodDataOop
550
    __ incrementl(Address(rbx,methodOopDesc::interpreter_invocation_counter_offset()));
D
duke 已提交
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
  }
  // Update standard invocation counters
  __ movl(rax, backedge_counter);               // load backedge counter

  __ increment(rcx, InvocationCounter::count_increment);
  __ andl(rax, InvocationCounter::count_mask_value);  // mask out the status bits

  __ movl(invocation_counter, rcx);             // save invocation count
  __ addl(rcx, rax);                            // add both counters

  // profile_method is non-null only for interpreted method so
  // profile_method != NULL == !native_call
  // BytecodeInterpreter only calls for native so code is elided.

  __ cmp32(rcx,
           ExternalAddress((address)&InvocationCounter::InterpreterInvocationLimit));
  __ jcc(Assembler::aboveEqual, *overflow);

}

void InterpreterGenerator::generate_counter_overflow(Label* do_continue) {

  // C++ interpreter on entry
574
  // rsi/r13 - new interpreter state pointer
D
duke 已提交
575 576 577 578 579 580 581 582 583 584
  // rbp - interpreter frame pointer
  // rbx - method

  // On return (i.e. jump to entry_point) [ back to invocation of interpreter ]
  // rbx, - method
  // rcx - rcvr (assuming there is one)
  // top of stack return address of interpreter caller
  // rsp - sender_sp

  // C++ interpreter only
585
  // rsi/r13 - previous interpreter state pointer
D
duke 已提交
586 587 588 589 590 591 592

  const Address size_of_parameters(rbx, methodOopDesc::size_of_parameters_offset());

  // InterpreterRuntime::frequency_counter_overflow takes one argument
  // indicating if the counter overflow occurs at a backwards branch (non-NULL bcp).
  // The call returns the address of the verified entry point for the method or NULL
  // if the compilation did not complete (either went background or bailed out).
593
  __ movptr(rax, (int32_t)false);
D
duke 已提交
594 595 596
  __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::frequency_counter_overflow), rax);

  // for c++ interpreter can rsi really be munged?
597
  __ lea(state, Address(rbp, -(int)sizeof(BytecodeInterpreter)));                               // restore state
598 599
  __ movptr(rbx, Address(state, byte_offset_of(BytecodeInterpreter, _method)));            // restore method
  __ movptr(rdi, Address(state, byte_offset_of(BytecodeInterpreter, _locals)));            // get locals pointer
D
duke 已提交
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616

  __ jmp(*do_continue, relocInfo::none);

}

void InterpreterGenerator::generate_stack_overflow_check(void) {
  // see if we've got enough room on the stack for locals plus overhead.
  // the expression stack grows down incrementally, so the normal guard
  // page mechanism will work for that.
  //
  // Registers live on entry:
  //
  // Asm interpreter
  // rdx: number of additional locals this frame needs (what we must check)
  // rbx,: methodOop

  // C++ Interpreter
617
  // rsi/r13: previous interpreter frame state object
D
duke 已提交
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
  // rdi: &locals[0]
  // rcx: # of locals
  // rdx: number of additional locals this frame needs (what we must check)
  // rbx: methodOop

  // destroyed on exit
  // rax,

  // NOTE:  since the additional locals are also always pushed (wasn't obvious in
  // generate_method_entry) so the guard should work for them too.
  //

  // monitor entry size: see picture of stack set (generate_method_entry) and frame_i486.hpp
  const int entry_size    = frame::interpreter_frame_monitor_size() * wordSize;

  // total overhead size: entry_size + (saved rbp, thru expr stack bottom).
  // be sure to change this if you add/subtract anything to/from the overhead area
  const int overhead_size = (int)sizeof(BytecodeInterpreter);

  const int page_size = os::vm_page_size();

  Label after_frame_check;

  // compute rsp as if this were going to be the last frame on
  // the stack before the red zone

  Label after_frame_check_pop;

  // save rsi == caller's bytecode ptr (c++ previous interp. state)
  // QQQ problem here?? rsi overload????
648
  __ push(state);
D
duke 已提交
649

650
  const Register thread = LP64_ONLY(r15_thread) NOT_LP64(rsi);
D
duke 已提交
651

652
  NOT_LP64(__ get_thread(thread));
D
duke 已提交
653 654 655 656 657 658 659 660

  const Address stack_base(thread, Thread::stack_base_offset());
  const Address stack_size(thread, Thread::stack_size_offset());

  // locals + overhead, in bytes
    const Address size_of_stack    (rbx, methodOopDesc::max_stack_offset());
    // Always give one monitor to allow us to start interp if sync method.
    // Any additional monitors need a check when moving the expression stack
661
    const int one_monitor = frame::interpreter_frame_monitor_size() * wordSize;
D
duke 已提交
662
  __ load_unsigned_word(rax, size_of_stack);                            // get size of expression stack in words
663 664
  __ lea(rax, Address(noreg, rax, Interpreter::stackElementScale(), one_monitor));
  __ lea(rax, Address(rax, rdx, Interpreter::stackElementScale(), overhead_size));
D
duke 已提交
665 666 667 668

#ifdef ASSERT
  Label stack_base_okay, stack_size_okay;
  // verify that thread stack base is non-zero
669
  __ cmpptr(stack_base, (int32_t)0);
D
duke 已提交
670 671 672 673
  __ jcc(Assembler::notEqual, stack_base_okay);
  __ stop("stack base is zero");
  __ bind(stack_base_okay);
  // verify that thread stack size is non-zero
674
  __ cmpptr(stack_size, (int32_t)0);
D
duke 已提交
675 676 677 678 679 680
  __ jcc(Assembler::notEqual, stack_size_okay);
  __ stop("stack size is zero");
  __ bind(stack_size_okay);
#endif

  // Add stack base to locals and subtract stack size
681 682
  __ addptr(rax, stack_base);
  __ subptr(rax, stack_size);
D
duke 已提交
683 684 685 686 687 688 689 690 691 692 693

  // We should have a magic number here for the size of the c++ interpreter frame.
  // We can't actually tell this ahead of time. The debug version size is around 3k
  // product is 1k and fastdebug is 4k
  const int slop = 6 * K;

  // Use the maximum number of pages we might bang.
  const int max_pages = StackShadowPages > (StackRedPages+StackYellowPages) ? StackShadowPages :
                                                                              (StackRedPages+StackYellowPages);
  // Only need this if we are stack banging which is temporary while
  // we're debugging.
694
  __ addptr(rax, slop + 2*max_pages * page_size);
D
duke 已提交
695 696

  // check against the current stack bottom
697
  __ cmpptr(rsp, rax);
D
duke 已提交
698 699
  __ jcc(Assembler::above, after_frame_check_pop);

700
  __ pop(state);  //  get c++ prev state.
D
duke 已提交
701 702 703 704 705 706

     // throw exception return address becomes throwing pc
  __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_StackOverflowError));

  // all done with frame size check
  __ bind(after_frame_check_pop);
707
  __ pop(state);
D
duke 已提交
708 709 710 711 712 713 714 715

  __ bind(after_frame_check);
}

// Find preallocated  monitor and lock method (C++ interpreter)
// rbx - methodOop
//
void InterpreterGenerator::lock_method(void) {
716 717
  // assumes state == rsi/r13 == pointer to current interpreterState
  // minimally destroys rax, rdx|c_rarg1, rdi
D
duke 已提交
718 719 720 721 722
  //
  // synchronize method
  const int entry_size            = frame::interpreter_frame_monitor_size() * wordSize;
  const Address access_flags      (rbx, methodOopDesc::access_flags_offset());

723 724
  const Register monitor  = NOT_LP64(rdx) LP64_ONLY(c_rarg1);

D
duke 已提交
725
  // find initial monitor i.e. monitors[-1]
726 727
  __ movptr(monitor, STATE(_monitor_base));                                   // get monitor bottom limit
  __ subptr(monitor, entry_size);                                             // point to initial monitor
D
duke 已提交
728 729 730 731 732 733 734 735 736 737 738 739 740 741

#ifdef ASSERT
  { Label L;
    __ movl(rax, access_flags);
    __ testl(rax, JVM_ACC_SYNCHRONIZED);
    __ jcc(Assembler::notZero, L);
    __ stop("method doesn't need synchronization");
    __ bind(L);
  }
#endif // ASSERT
  // get synchronization object
  { Label done;
    const int mirror_offset = klassOopDesc::klass_part_offset_in_bytes() + Klass::java_mirror_offset_in_bytes();
    __ movl(rax, access_flags);
742
    __ movptr(rdi, STATE(_locals));                                     // prepare to get receiver (assume common case)
D
duke 已提交
743
    __ testl(rax, JVM_ACC_STATIC);
744
    __ movptr(rax, Address(rdi, 0));                                    // get receiver (assume this is frequent case)
D
duke 已提交
745
    __ jcc(Assembler::zero, done);
746 747 748
    __ movptr(rax, Address(rbx, methodOopDesc::constants_offset()));
    __ movptr(rax, Address(rax, constantPoolOopDesc::pool_holder_offset_in_bytes()));
    __ movptr(rax, Address(rax, mirror_offset));
D
duke 已提交
749 750 751 752
    __ bind(done);
  }
#ifdef ASSERT
  { Label L;
753
    __ cmpptr(rax, Address(monitor, BasicObjectLock::obj_offset_in_bytes()));   // correct object?
D
duke 已提交
754 755 756 757 758
    __ jcc(Assembler::equal, L);
    __ stop("wrong synchronization lobject");
    __ bind(L);
  }
#endif // ASSERT
759 760
  // can destroy rax, rdx|c_rarg1, rcx, and (via call_VM) rdi!
  __ lock_object(monitor);
D
duke 已提交
761 762 763 764 765 766
}

// Call an accessor method (assuming it is resolved, otherwise drop into vanilla (slow path) entry

address InterpreterGenerator::generate_accessor_entry(void) {

767
  // rbx: methodOop
D
duke 已提交
768

769
  // rsi/r13: senderSP must preserved for slow path, set SP to it on fast path
D
duke 已提交
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791

  Label xreturn_path;

  // do fastpath for resolved accessor methods
  if (UseFastAccessorMethods) {

    address entry_point = __ pc();

    Label slow_path;
    // If we need a safepoint check, generate full interpreter entry.
    ExternalAddress state(SafepointSynchronize::address_of_state());
    __ cmp32(ExternalAddress(SafepointSynchronize::address_of_state()),
             SafepointSynchronize::_not_synchronized);

    __ jcc(Assembler::notEqual, slow_path);
    // ASM/C++ Interpreter
    // Code: _aload_0, _(i|a)getfield, _(i|a)return or any rewrites thereof; parameter size = 1
    // Note: We can only use this code if the getfield has been resolved
    //       and if we don't have a null-pointer exception => check for
    //       these conditions first and use slow path if necessary.
    // rbx,: method
    // rcx: receiver
792
    __ movptr(rax, Address(rsp, wordSize));
D
duke 已提交
793 794

    // check if local 0 != NULL and read field
795
    __ testptr(rax, rax);
D
duke 已提交
796 797
    __ jcc(Assembler::zero, slow_path);

798
    __ movptr(rdi, Address(rbx, methodOopDesc::constants_offset()));
D
duke 已提交
799
    // read first instruction word and extract bytecode @ 1 and index @ 2
800
    __ movptr(rdx, Address(rbx, methodOopDesc::const_offset()));
D
duke 已提交
801 802 803 804 805
    __ movl(rdx, Address(rdx, constMethodOopDesc::codes_offset()));
    // Shift codes right to get the index on the right.
    // The bytecode fetched looks like <index><0xb4><0x2a>
    __ shrl(rdx, 2*BitsPerByte);
    __ shll(rdx, exact_log2(in_words(ConstantPoolCacheEntry::size())));
806
    __ movptr(rdi, Address(rdi, constantPoolOopDesc::cache_offset_in_bytes()));
D
duke 已提交
807 808 809 810 811 812 813

    // rax,: local 0
    // rbx,: method
    // rcx: receiver - do not destroy since it is needed for slow path!
    // rcx: scratch
    // rdx: constant pool cache index
    // rdi: constant pool cache
814
    // rsi/r13: sender sp
D
duke 已提交
815 816 817 818 819 820 821 822

    // check if getfield has been resolved and read constant pool cache entry
    // check the validity of the cache entry by testing whether _indices field
    // contains Bytecode::_getfield in b1 byte.
    assert(in_words(ConstantPoolCacheEntry::size()) == 4, "adjust shift below");
    __ movl(rcx,
            Address(rdi,
                    rdx,
823
                    Address::times_ptr, constantPoolCacheOopDesc::base_offset() + ConstantPoolCacheEntry::indices_offset()));
D
duke 已提交
824 825 826 827 828 829
    __ shrl(rcx, 2*BitsPerByte);
    __ andl(rcx, 0xFF);
    __ cmpl(rcx, Bytecodes::_getfield);
    __ jcc(Assembler::notEqual, slow_path);

    // Note: constant pool entry is not valid before bytecode is resolved
830
    __ movptr(rcx,
D
duke 已提交
831 832
            Address(rdi,
                    rdx,
833
                    Address::times_ptr, constantPoolCacheOopDesc::base_offset() + ConstantPoolCacheEntry::f2_offset()));
D
duke 已提交
834 835 836
    __ movl(rdx,
            Address(rdi,
                    rdx,
837
                    Address::times_ptr, constantPoolCacheOopDesc::base_offset() + ConstantPoolCacheEntry::flags_offset()));
D
duke 已提交
838 839 840 841 842 843 844 845 846 847

    Label notByte, notShort, notChar;
    const Address field_address (rax, rcx, Address::times_1);

    // Need to differentiate between igetfield, agetfield, bgetfield etc.
    // because they are different sizes.
    // Use the type from the constant pool cache
    __ shrl(rdx, ConstantPoolCacheEntry::tosBits);
    // Make sure we don't need to mask rdx for tosBits after the above shift
    ConstantPoolCacheEntry::verify_tosBits();
848 849 850 851 852 853 854 855 856 857
#ifdef _LP64
    Label notObj;
    __ cmpl(rdx, atos);
    __ jcc(Assembler::notEqual, notObj);
    // atos
    __ movptr(rax, field_address);
    __ jmp(xreturn_path);

    __ bind(notObj);
#endif // _LP64
D
duke 已提交
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
    __ cmpl(rdx, btos);
    __ jcc(Assembler::notEqual, notByte);
    __ load_signed_byte(rax, field_address);
    __ jmp(xreturn_path);

    __ bind(notByte);
    __ cmpl(rdx, stos);
    __ jcc(Assembler::notEqual, notShort);
    __ load_signed_word(rax, field_address);
    __ jmp(xreturn_path);

    __ bind(notShort);
    __ cmpl(rdx, ctos);
    __ jcc(Assembler::notEqual, notChar);
    __ load_unsigned_word(rax, field_address);
    __ jmp(xreturn_path);

    __ bind(notChar);
#ifdef ASSERT
    Label okay;
878
#ifndef _LP64
D
duke 已提交
879 880
    __ cmpl(rdx, atos);
    __ jcc(Assembler::equal, okay);
881
#endif // _LP64
D
duke 已提交
882 883 884 885 886 887 888 889 890 891 892
    __ cmpl(rdx, itos);
    __ jcc(Assembler::equal, okay);
    __ stop("what type is this?");
    __ bind(okay);
#endif // ASSERT
    // All the rest are a 32 bit wordsize
    __ movl(rax, field_address);

    __ bind(xreturn_path);

    // _ireturn/_areturn
893 894
    __ pop(rdi);                               // get return address
    __ mov(rsp, sender_sp_on_entry);           // set sp to sender sp
D
duke 已提交
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
    __ jmp(rdi);

    // generate a vanilla interpreter entry as the slow path
    __ bind(slow_path);
    // We will enter c++ interpreter looking like it was
    // called by the call_stub this will cause it to return
    // a tosca result to the invoker which might have been
    // the c++ interpreter itself.

    __ jmp(fast_accessor_slow_entry_path);
    return entry_point;

  } else {
    return NULL;
  }

}

//
// C++ Interpreter stub for calling a native method.
// This sets up a somewhat different looking stack for calling the native method
// than the typical interpreter frame setup but still has the pointer to
// an interpreter state.
//

address InterpreterGenerator::generate_native_entry(bool synchronized) {
  // determine code generation flags
  bool inc_counter  = UseCompiler || CountCompiledCalls;

  // rbx: methodOop
  // rcx: receiver (unused)
926 927
  // rsi/r13: previous interpreter state (if called from C++ interpreter) must preserve
  //      in any case. If called via c1/c2/call_stub rsi/r13 is junk (to use) but harmless
D
duke 已提交
928 929 930 931 932 933 934 935
  //      to save/restore.
  address entry_point = __ pc();

  const Address size_of_parameters(rbx, methodOopDesc::size_of_parameters_offset());
  const Address size_of_locals    (rbx, methodOopDesc::size_of_locals_offset());
  const Address invocation_counter(rbx, methodOopDesc::invocation_counter_offset() + InvocationCounter::counter_offset());
  const Address access_flags      (rbx, methodOopDesc::access_flags_offset());

936
  // rsi/r13 == state/locals rdi == prevstate
D
duke 已提交
937 938 939 940 941 942 943
  const Register locals = rdi;

  // get parameter size (always needed)
  __ load_unsigned_word(rcx, size_of_parameters);

  // rbx: methodOop
  // rcx: size of parameters
944
  __ pop(rax);                                       // get return address
D
duke 已提交
945 946 947
  // for natives the size of locals is zero

  // compute beginning of parameters /locals
948
  __ lea(locals, Address(rsp, rcx, Address::times_ptr, -wordSize));
D
duke 已提交
949 950 951 952 953 954 955 956 957 958 959 960 961

  // initialize fixed part of activation frame

  // Assumes rax = return address

  // allocate and initialize new interpreterState and method expression stack
  // IN(locals) ->  locals
  // IN(state) -> previous frame manager state (NULL from stub/c1/c2)
  // destroys rax, rcx, rdx
  // OUT (state) -> new interpreterState
  // OUT(rsp) -> bottom of methods expression stack

  // save sender_sp
962
  __ mov(rcx, sender_sp_on_entry);
D
duke 已提交
963
  // start with NULL previous state
964
  __ movptr(state, (int32_t)NULL_WORD);
D
duke 已提交
965 966 967 968
  generate_compute_interpreter_state(state, locals, rcx, true);

#ifdef ASSERT
  { Label L;
969 970 971 972 973 974 975
    __ movptr(rax, STATE(_stack_base));
#ifdef _LP64
    // duplicate the alignment rsp got after setting stack_base
    __ subptr(rax, frame::arg_reg_save_area_bytes); // windows
    __ andptr(rax, -16); // must be 16 byte boundry (see amd64 ABI)
#endif // _LP64
    __ cmpptr(rax, rsp);
D
duke 已提交
976 977 978 979 980 981 982 983
    __ jcc(Assembler::equal, L);
    __ stop("broken stack frame setup in interpreter");
    __ bind(L);
  }
#endif

  if (inc_counter) __ movl(rcx, invocation_counter);  // (pre-)fetch invocation count

984 985
  const Register unlock_thread = LP64_ONLY(r15_thread) NOT_LP64(rax);
  NOT_LP64(__ movptr(unlock_thread, STATE(_thread));) // get thread
D
duke 已提交
986 987 988 989 990 991
  // Since at this point in the method invocation the exception handler
  // would try to exit the monitor of synchronized methods which hasn't
  // been entered yet, we set the thread local variable
  // _do_not_unlock_if_synchronized to true. The remove_activation will
  // check this flag.

992
  const Address do_not_unlock_if_synchronized(unlock_thread,
D
duke 已提交
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
        in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()));
  __ movbool(do_not_unlock_if_synchronized, true);

  // make sure method is native & not abstract
#ifdef ASSERT
  __ movl(rax, access_flags);
  {
    Label L;
    __ testl(rax, JVM_ACC_NATIVE);
    __ jcc(Assembler::notZero, L);
    __ stop("tried to execute non-native method as native");
    __ bind(L);
  }
  { Label L;
    __ testl(rax, JVM_ACC_ABSTRACT);
    __ jcc(Assembler::zero, L);
    __ stop("tried to execute abstract method in interpreter");
    __ bind(L);
  }
#endif


  // increment invocation count & check for overflow
  Label invocation_counter_overflow;
  if (inc_counter) {
    generate_counter_incr(&invocation_counter_overflow, NULL, NULL);
  }

  Label continue_after_compile;

  __ bind(continue_after_compile);

  bang_stack_shadow_pages(true);

  // reset the _do_not_unlock_if_synchronized flag
1028
  NOT_LP64(__ movl(rax, STATE(_thread));)                       // get thread
D
duke 已提交
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
  __ movbool(do_not_unlock_if_synchronized, false);


  // check for synchronized native methods
  //
  // Note: This must happen *after* invocation counter check, since
  //       when overflow happens, the method should not be locked.
  if (synchronized) {
    // potentially kills rax, rcx, rdx, rdi
    lock_method();
  } else {
    // no synchronization necessary
#ifdef ASSERT
      { Label L;
        __ movl(rax, access_flags);
        __ testl(rax, JVM_ACC_SYNCHRONIZED);
        __ jcc(Assembler::zero, L);
        __ stop("method needs synchronization");
        __ bind(L);
      }
#endif
  }

  // start execution

  // jvmti support
  __ notify_method_entry();

  // work registers
  const Register method = rbx;
1059 1060
  const Register thread = LP64_ONLY(r15_thread) NOT_LP64(rdi);
  const Register t      = InterpreterRuntime::SignatureHandlerGenerator::temp();    // rcx|rscratch1
D
duke 已提交
1061 1062

  // allocate space for parameters
1063
  __ movptr(method, STATE(_method));
D
duke 已提交
1064 1065 1066
  __ verify_oop(method);
  __ load_unsigned_word(t, Address(method, methodOopDesc::size_of_parameters_offset()));
  __ shll(t, 2);
1067 1068 1069 1070 1071 1072 1073 1074 1075
#ifdef _LP64
  __ subptr(rsp, t);
  __ subptr(rsp, frame::arg_reg_save_area_bytes); // windows
  __ andptr(rsp, -16); // must be 16 byte boundry (see amd64 ABI)
#else
  __ addptr(t, 2*wordSize);     // allocate two more slots for JNIEnv and possible mirror
  __ subptr(rsp, t);
  __ andptr(rsp, -(StackAlignmentInBytes)); // gcc needs 16 byte aligned stacks to do XMM intrinsics
#endif // _LP64
D
duke 已提交
1076 1077 1078 1079 1080

  // get signature handler
    Label pending_exception_present;

  { Label L;
1081 1082
    __ movptr(t, Address(method, methodOopDesc::signature_handler_offset()));
    __ testptr(t, t);
D
duke 已提交
1083 1084
    __ jcc(Assembler::notZero, L);
    __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::prepare_native_call), method, false);
1085 1086
    __ movptr(method, STATE(_method));
    __ cmpptr(Address(thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
D
duke 已提交
1087 1088
    __ jcc(Assembler::notEqual, pending_exception_present);
    __ verify_oop(method);
1089
    __ movptr(t, Address(method, methodOopDesc::signature_handler_offset()));
D
duke 已提交
1090 1091 1092 1093 1094
    __ bind(L);
  }
#ifdef ASSERT
  {
    Label L;
1095
    __ push(t);
D
duke 已提交
1096
    __ get_thread(t);                                   // get vm's javathread*
1097
    __ cmpptr(t, STATE(_thread));
D
duke 已提交
1098 1099 1100
    __ jcc(Assembler::equal, L);
    __ int3();
    __ bind(L);
1101
    __ pop(t);
D
duke 已提交
1102 1103 1104
  }
#endif //

1105
  const Register from_ptr = InterpreterRuntime::SignatureHandlerGenerator::from();
D
duke 已提交
1106 1107
  // call signature handler
  assert(InterpreterRuntime::SignatureHandlerGenerator::to  () == rsp, "adjust this code");
1108

D
duke 已提交
1109 1110 1111 1112
  // The generated handlers do not touch RBX (the method oop).
  // However, large signatures cannot be cached and are generated
  // each time here.  The slow-path generator will blow RBX
  // sometime, so we must reload it after the call.
1113
  __ movptr(from_ptr, STATE(_locals));  // get the from pointer
D
duke 已提交
1114
  __ call(t);
1115
  __ movptr(method, STATE(_method));
D
duke 已提交
1116 1117 1118 1119
  __ verify_oop(method);

  // result handler is in rax
  // set result handler
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
  __ movptr(STATE(_result_handler), rax);


  // get native function entry point
  { Label L;
    __ movptr(rax, Address(method, methodOopDesc::native_function_offset()));
    __ testptr(rax, rax);
    __ jcc(Assembler::notZero, L);
    __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::prepare_native_call), method);
    __ movptr(method, STATE(_method));
    __ verify_oop(method);
    __ movptr(rax, Address(method, methodOopDesc::native_function_offset()));
    __ bind(L);
  }
D
duke 已提交
1134 1135 1136 1137 1138 1139 1140 1141

  // pass mirror handle if static call
  { Label L;
    const int mirror_offset = klassOopDesc::klass_part_offset_in_bytes() + Klass::java_mirror_offset_in_bytes();
    __ movl(t, Address(method, methodOopDesc::access_flags_offset()));
    __ testl(t, JVM_ACC_STATIC);
    __ jcc(Assembler::zero, L);
    // get mirror
1142 1143 1144
    __ movptr(t, Address(method, methodOopDesc:: constants_offset()));
    __ movptr(t, Address(t, constantPoolOopDesc::pool_holder_offset_in_bytes()));
    __ movptr(t, Address(t, mirror_offset));
D
duke 已提交
1145
    // copy mirror into activation object
1146
    __ movptr(STATE(_oop_temp), t);
D
duke 已提交
1147
    // pass handle to mirror
1148 1149 1150 1151 1152 1153
#ifdef _LP64
    __ lea(c_rarg1, STATE(_oop_temp));
#else
    __ lea(t, STATE(_oop_temp));
    __ movptr(Address(rsp, wordSize), t);
#endif // _LP64
D
duke 已提交
1154 1155 1156 1157 1158
    __ bind(L);
  }
#ifdef ASSERT
  {
    Label L;
1159
    __ push(t);
D
duke 已提交
1160
    __ get_thread(t);                                   // get vm's javathread*
1161
    __ cmpptr(t, STATE(_thread));
D
duke 已提交
1162 1163 1164
    __ jcc(Assembler::equal, L);
    __ int3();
    __ bind(L);
1165
    __ pop(t);
D
duke 已提交
1166 1167 1168 1169
  }
#endif //

  // pass JNIEnv
1170 1171 1172 1173 1174 1175 1176 1177 1178
#ifdef _LP64
  __ lea(c_rarg0, Address(thread, JavaThread::jni_environment_offset()));
#else
  __ movptr(thread, STATE(_thread));          // get thread
  __ lea(t, Address(thread, JavaThread::jni_environment_offset()));

  __ movptr(Address(rsp, 0), t);
#endif // _LP64

D
duke 已提交
1179 1180 1181
#ifdef ASSERT
  {
    Label L;
1182
    __ push(t);
D
duke 已提交
1183
    __ get_thread(t);                                   // get vm's javathread*
1184
    __ cmpptr(t, STATE(_thread));
D
duke 已提交
1185 1186 1187
    __ jcc(Assembler::equal, L);
    __ int3();
    __ bind(L);
1188
    __ pop(t);
D
duke 已提交
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
  }
#endif //

#ifdef ASSERT
  { Label L;
    __ movl(t, Address(thread, JavaThread::thread_state_offset()));
    __ cmpl(t, _thread_in_Java);
    __ jcc(Assembler::equal, L);
    __ stop("Wrong thread state in native stub");
    __ bind(L);
  }
#endif

  // Change state to native (we save the return address in the thread, since it might not
  // be pushed on the stack when we do a a stack traversal). It is enough that the pc()
  // points into the right code segment. It does not have to be the correct return pc.

  __ set_last_Java_frame(thread, noreg, rbp, __ pc());

  __ movl(Address(thread, JavaThread::thread_state_offset()), _thread_in_native);

  __ call(rax);

  // result potentially in rdx:rax or ST0
1213 1214
  __ movptr(method, STATE(_method));
  NOT_LP64(__ movptr(thread, STATE(_thread));)                  // get thread
D
duke 已提交
1215 1216 1217 1218 1219 1220 1221 1222 1223

  // The potential result is in ST(0) & rdx:rax
  // With C++ interpreter we leave any possible result in ST(0) until we are in result handler and then
  // we do the appropriate stuff for returning the result. rdx:rax must always be saved because just about
  // anything we do here will destroy it, st(0) is only saved if we re-enter the vm where it would
  // be destroyed.
  // It is safe to do these pushes because state is _thread_in_native and return address will be found
  // via _last_native_pc and not via _last_jave_sp

1224
    // Must save the value of ST(0)/xmm0 since it could be destroyed before we get to result handler
D
duke 已提交
1225 1226 1227 1228 1229 1230 1231 1232
    { Label Lpush, Lskip;
      ExternalAddress float_handler(AbstractInterpreter::result_handler(T_FLOAT));
      ExternalAddress double_handler(AbstractInterpreter::result_handler(T_DOUBLE));
      __ cmpptr(STATE(_result_handler), float_handler.addr());
      __ jcc(Assembler::equal, Lpush);
      __ cmpptr(STATE(_result_handler), double_handler.addr());
      __ jcc(Assembler::notEqual, Lskip);
      __ bind(Lpush);
1233 1234 1235 1236 1237 1238
      __ subptr(rsp, 2*wordSize);
      if ( UseSSE < 2 ) {
        __ fstp_d(Address(rsp, 0));
      } else {
        __ movdbl(Address(rsp, 0), xmm0);
      }
D
duke 已提交
1239 1240 1241
      __ bind(Lskip);
    }

1242 1243 1244 1245 1246
  // save rax:rdx for potential use by result handler.
  __ push(rax);
#ifndef _LP64
  __ push(rdx);
#endif // _LP64
D
duke 已提交
1247 1248 1249 1250 1251 1252 1253 1254

  // Either restore the MXCSR register after returning from the JNI Call
  // or verify that it wasn't changed.
  if (VM_Version::supports_sse()) {
    if (RestoreMXCSROnJNICalls) {
      __ ldmxcsr(ExternalAddress(StubRoutines::addr_mxcsr_std()));
    }
    else if (CheckJNICalls ) {
1255
      __ call(RuntimeAddress(StubRoutines::x86::verify_mxcsr_entry()));
D
duke 已提交
1256 1257 1258
    }
  }

1259
#ifndef _LP64
D
duke 已提交
1260 1261 1262
  // Either restore the x87 floating pointer control word after returning
  // from the JNI call or verify that it wasn't changed.
  if (CheckJNICalls) {
1263
    __ call(RuntimeAddress(StubRoutines::x86::verify_fpu_cntrl_wrd_entry()));
D
duke 已提交
1264
  }
1265
#endif // _LP64
D
duke 已提交
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295


  // change thread state
  __ movl(Address(thread, JavaThread::thread_state_offset()), _thread_in_native_trans);
  if(os::is_MP()) {
    // Write serialization page so VM thread can do a pseudo remote membar.
    // We use the current thread pointer to calculate a thread specific
    // offset to write to within the page. This minimizes bus traffic
    // due to cache line collision.
    __ serialize_memory(thread, rcx);
  }

  // check for safepoint operation in progress and/or pending suspend requests
  { Label Continue;

    __ cmp32(ExternalAddress(SafepointSynchronize::address_of_state()),
             SafepointSynchronize::_not_synchronized);

    // threads running native code and they are expected to self-suspend
    // when leaving the _thread_in_native state. We need to check for
    // pending suspend requests here.
    Label L;
    __ jcc(Assembler::notEqual, L);
    __ cmpl(Address(thread, JavaThread::suspend_flags_offset()), 0);
    __ jcc(Assembler::equal, Continue);
    __ bind(L);

    // Don't use call_VM as it will see a possible pending exception and forward it
    // and never return here preventing us from clearing _last_native_pc down below.
    // Also can't use call_VM_leaf either as it will check to see if rsi & rdi are
1296
    // preserved and correspond to the bcp/locals pointers.
D
duke 已提交
1297
    //
1298 1299 1300

    ((MacroAssembler*)_masm)->call_VM_leaf(CAST_FROM_FN_PTR(address, JavaThread::check_special_condition_for_native_trans),
                          thread);
D
duke 已提交
1301 1302
    __ increment(rsp, wordSize);

1303
    __ movptr(method, STATE(_method));
D
duke 已提交
1304
    __ verify_oop(method);
1305
    __ movptr(thread, STATE(_thread));                       // get thread
D
duke 已提交
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315

    __ bind(Continue);
  }

  // change thread state
  __ movl(Address(thread, JavaThread::thread_state_offset()), _thread_in_Java);

  __ reset_last_Java_frame(thread, true, true);

  // reset handle block
1316 1317
  __ movptr(t, Address(thread, JavaThread::active_handles_offset()));
  __ movptr(Address(t, JNIHandleBlock::top_offset_in_bytes()), (int32_t)NULL_WORD);
D
duke 已提交
1318 1319 1320 1321 1322 1323 1324

  // If result was an oop then unbox and save it in the frame
  { Label L;
    Label no_oop, store_result;
      ExternalAddress oop_handler(AbstractInterpreter::result_handler(T_OBJECT));
    __ cmpptr(STATE(_result_handler), oop_handler.addr());
    __ jcc(Assembler::notEqual, no_oop);
1325 1326 1327 1328 1329
#ifndef _LP64
    __ pop(rdx);
#endif // _LP64
    __ pop(rax);
    __ testptr(rax, rax);
D
duke 已提交
1330 1331
    __ jcc(Assembler::zero, store_result);
    // unbox
1332
    __ movptr(rax, Address(rax, 0));
D
duke 已提交
1333
    __ bind(store_result);
1334
    __ movptr(STATE(_oop_temp), rax);
D
duke 已提交
1335
    // keep stack depth as expected by pushing oop which will eventually be discarded
1336 1337 1338 1339
    __ push(rax);
#ifndef _LP64
    __ push(rdx);
#endif // _LP64
D
duke 已提交
1340 1341 1342 1343 1344 1345 1346 1347
    __ bind(no_oop);
  }

  {
     Label no_reguard;
     __ cmpl(Address(thread, JavaThread::stack_guard_state_offset()), JavaThread::stack_guard_yellow_disabled);
     __ jcc(Assembler::notEqual, no_reguard);

1348
     __ pusha();
D
duke 已提交
1349
     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages)));
1350
     __ popa();
D
duke 已提交
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364

     __ bind(no_reguard);
   }


  // QQQ Seems like for native methods we simply return and the caller will see the pending
  // exception and do the right thing. Certainly the interpreter will, don't know about
  // compiled methods.
  // Seems that the answer to above is no this is wrong. The old code would see the exception
  // and forward it before doing the unlocking and notifying jvmdi that method has exited.
  // This seems wrong need to investigate the spec.

  // handle exceptions (exception handling will handle unlocking!)
  { Label L;
1365
    __ cmpptr(Address(thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
D
duke 已提交
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
    __ jcc(Assembler::zero, L);
    __ bind(pending_exception_present);

    // There are potential results on the stack (rax/rdx, ST(0)) we ignore these and simply
    // return and let caller deal with exception. This skips the unlocking here which
    // seems wrong but seems to be what asm interpreter did. Can't find this in the spec.
    // Note: must preverve method in rbx
    //

    // remove activation

1377
    __ movptr(t, STATE(_sender_sp));
D
duke 已提交
1378
    __ leave();                                  // remove frame anchor
1379 1380 1381 1382
    __ pop(rdi);                                 // get return address
    __ movptr(state, STATE(_prev_link));         // get previous state for return
    __ mov(rsp, t);                              // set sp to sender sp
    __ push(rdi);                                // push throwing pc
D
duke 已提交
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
    // The skips unlocking!! This seems to be what asm interpreter does but seems
    // very wrong. Not clear if this violates the spec.
    __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
    __ bind(L);
  }

  // do unlocking if necessary
  { Label L;
    __ movl(t, Address(method, methodOopDesc::access_flags_offset()));
    __ testl(t, JVM_ACC_SYNCHRONIZED);
    __ jcc(Assembler::zero, L);
    // the code below should be shared with interpreter macro assembler implementation
    { Label unlock;
1396
    const Register monitor = NOT_LP64(rdx) LP64_ONLY(c_rarg1);
D
duke 已提交
1397 1398
      // BasicObjectLock will be first in list, since this is a synchronized method. However, need
      // to check that the object has not been unlocked by an explicit monitorexit bytecode.
1399 1400
      __ movptr(monitor, STATE(_monitor_base));
      __ subptr(monitor, frame::interpreter_frame_monitor_size() * wordSize);  // address of initial monitor
D
duke 已提交
1401

1402 1403
      __ movptr(t, Address(monitor, BasicObjectLock::obj_offset_in_bytes()));
      __ testptr(t, t);
D
duke 已提交
1404 1405 1406 1407 1408 1409 1410
      __ jcc(Assembler::notZero, unlock);

      // Entry already unlocked, need to throw exception
      __ MacroAssembler::call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_illegal_monitor_state_exception));
      __ should_not_reach_here();

      __ bind(unlock);
1411
      __ unlock_object(monitor);
D
duke 已提交
1412
      // unlock can blow rbx so restore it for path that needs it below
1413
      __ movptr(method, STATE(_method));
D
duke 已提交
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
    }
    __ bind(L);
  }

  // jvmti support
  // Note: This must happen _after_ handling/throwing any exceptions since
  //       the exception handler code notifies the runtime of method exits
  //       too. If this happens before, method entry/exit notifications are
  //       not properly paired (was bug - gri 11/22/99).
  __ notify_method_exit(vtos, InterpreterMacroAssembler::NotifyJVMTI);

  // restore potential result in rdx:rax, call result handler to restore potential result in ST0 & handle result
1426 1427 1428 1429 1430
#ifndef _LP64
  __ pop(rdx);
#endif // _LP64
  __ pop(rax);
  __ movptr(t, STATE(_result_handler));       // get result handler
D
duke 已提交
1431 1432 1433 1434
  __ call(t);                                 // call result handler to convert to tosca form

  // remove activation

1435
  __ movptr(t, STATE(_sender_sp));
D
duke 已提交
1436 1437

  __ leave();                                  // remove frame anchor
1438 1439 1440
  __ pop(rdi);                                 // get return address
  __ movptr(state, STATE(_prev_link));         // get previous state for return (if c++ interpreter was caller)
  __ mov(rsp, t);                              // set sp to sender sp
D
duke 已提交
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
  __ jmp(rdi);

  // invocation counter overflow
  if (inc_counter) {
    // Handle overflow of counter and compile method
    __ bind(invocation_counter_overflow);
    generate_counter_overflow(&continue_after_compile);
  }

  return entry_point;
}

// Generate entries that will put a result type index into rcx
void CppInterpreterGenerator::generate_deopt_handling() {

  Label return_from_deopt_common;

  // Generate entries that will put a result type index into rcx
  // deopt needs to jump to here to enter the interpreter (return a result)
  deopt_frame_manager_return_atos  = __ pc();

  // rax is live here
  __ movl(rcx, AbstractInterpreter::BasicType_as_index(T_OBJECT));    // Result stub address array index
  __ jmp(return_from_deopt_common);


  // deopt needs to jump to here to enter the interpreter (return a result)
  deopt_frame_manager_return_btos  = __ pc();

  // rax is live here
  __ movl(rcx, AbstractInterpreter::BasicType_as_index(T_BOOLEAN));    // Result stub address array index
  __ jmp(return_from_deopt_common);

  // deopt needs to jump to here to enter the interpreter (return a result)
  deopt_frame_manager_return_itos  = __ pc();

  // rax is live here
  __ movl(rcx, AbstractInterpreter::BasicType_as_index(T_INT));    // Result stub address array index
  __ jmp(return_from_deopt_common);

  // deopt needs to jump to here to enter the interpreter (return a result)

  deopt_frame_manager_return_ltos  = __ pc();
  // rax,rdx are live here
  __ movl(rcx, AbstractInterpreter::BasicType_as_index(T_LONG));    // Result stub address array index
  __ jmp(return_from_deopt_common);

  // deopt needs to jump to here to enter the interpreter (return a result)

  deopt_frame_manager_return_ftos  = __ pc();
  // st(0) is live here
  __ movl(rcx, AbstractInterpreter::BasicType_as_index(T_FLOAT));    // Result stub address array index
  __ jmp(return_from_deopt_common);

  // deopt needs to jump to here to enter the interpreter (return a result)
  deopt_frame_manager_return_dtos  = __ pc();

  // st(0) is live here
  __ movl(rcx, AbstractInterpreter::BasicType_as_index(T_DOUBLE));    // Result stub address array index
  __ jmp(return_from_deopt_common);

  // deopt needs to jump to here to enter the interpreter (return a result)
  deopt_frame_manager_return_vtos  = __ pc();

  __ movl(rcx, AbstractInterpreter::BasicType_as_index(T_VOID));

  // Deopt return common
  // an index is present in rcx that lets us move any possible result being
  // return to the interpreter's stack
  //
  // Because we have a full sized interpreter frame on the youngest
  // activation the stack is pushed too deep to share the tosca to
  // stack converters directly. We shrink the stack to the desired
  // amount and then push result and then re-extend the stack.
  // We could have the code in size_activation layout a short
  // frame for the top activation but that would look different
  // than say sparc (which needs a full size activation because
  // the windows are in the way. Really it could be short? QQQ
  //
  __ bind(return_from_deopt_common);

1522
  __ lea(state, Address(rbp, -(int)sizeof(BytecodeInterpreter)));
D
duke 已提交
1523 1524

  // setup rsp so we can push the "result" as needed.
1525 1526
  __ movptr(rsp, STATE(_stack));                                   // trim stack (is prepushed)
  __ addptr(rsp, wordSize);                                        // undo prepush
D
duke 已提交
1527 1528

  ExternalAddress tosca_to_stack((address)CppInterpreter::_tosca_to_stack);
1529 1530 1531
  // Address index(noreg, rcx, Address::times_ptr);
  __ movptr(rcx, ArrayAddress(tosca_to_stack, Address(noreg, rcx, Address::times_ptr)));
  // __ movl(rcx, Address(noreg, rcx, Address::times_ptr, int(AbstractInterpreter::_tosca_to_stack)));
D
duke 已提交
1532 1533 1534
  __ call(rcx);                                                   // call result converter

  __ movl(STATE(_msg), (int)BytecodeInterpreter::deopt_resume);
1535 1536
  __ lea(rsp, Address(rsp, -wordSize));                            // prepush stack (result if any already present)
  __ movptr(STATE(_stack), rsp);                                   // inform interpreter of new stack depth (parameters removed,
D
duke 已提交
1537
                                                                   // result if any on stack already )
1538
  __ movptr(rsp, STATE(_stack_limit));                             // restore expression stack to full depth
D
duke 已提交
1539 1540 1541 1542 1543 1544 1545 1546
}

// Generate the code to handle a more_monitors message from the c++ interpreter
void CppInterpreterGenerator::generate_more_monitors() {


  Label entry, loop;
  const int entry_size = frame::interpreter_frame_monitor_size() * wordSize;
1547 1548 1549 1550 1551 1552 1553 1554
  // 1. compute new pointers                     // rsp: old expression stack top
  __ movptr(rdx, STATE(_stack_base));            // rdx: old expression stack bottom
  __ subptr(rsp, entry_size);                    // move expression stack top limit
  __ subptr(STATE(_stack), entry_size);          // update interpreter stack top
  __ subptr(STATE(_stack_limit), entry_size);    // inform interpreter
  __ subptr(rdx, entry_size);                    // move expression stack bottom
  __ movptr(STATE(_stack_base), rdx);            // inform interpreter
  __ movptr(rcx, STATE(_stack));                 // set start value for copy loop
D
duke 已提交
1555 1556 1557
  __ jmp(entry);
  // 2. move expression stack contents
  __ bind(loop);
1558 1559 1560
  __ movptr(rbx, Address(rcx, entry_size));      // load expression stack word from old location
  __ movptr(Address(rcx, 0), rbx);               // and store it at new location
  __ addptr(rcx, wordSize);                      // advance to next word
D
duke 已提交
1561
  __ bind(entry);
1562 1563
  __ cmpptr(rcx, rdx);                           // check if bottom reached
  __ jcc(Assembler::notEqual, loop);             // if not at bottom then copy next word
D
duke 已提交
1564
  // now zero the slot so we can find it.
1565
  __ movptr(Address(rdx, BasicObjectLock::obj_offset_in_bytes()), (int32_t) NULL_WORD);
D
duke 已提交
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
  __ movl(STATE(_msg), (int)BytecodeInterpreter::got_monitors);
}


// Initial entry to C++ interpreter from the call_stub.
// This entry point is called the frame manager since it handles the generation
// of interpreter activation frames via requests directly from the vm (via call_stub)
// and via requests from the interpreter. The requests from the call_stub happen
// directly thru the entry point. Requests from the interpreter happen via returning
// from the interpreter and examining the message the interpreter has returned to
// the frame manager. The frame manager can take the following requests:

// NO_REQUEST - error, should never happen.
// MORE_MONITORS - need a new monitor. Shuffle the expression stack on down and
//                 allocate a new monitor.
// CALL_METHOD - setup a new activation to call a new method. Very similar to what
//               happens during entry during the entry via the call stub.
// RETURN_FROM_METHOD - remove an activation. Return to interpreter or call stub.
//
// Arguments:
//
// rbx: methodOop
// rcx: receiver - unused (retrieved from stack as needed)
1589
// rsi/r13: previous frame manager state (NULL from the call_stub/c1/c2)
D
duke 已提交
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
//
//
// Stack layout at entry
//
// [ return address     ] <--- rsp
// [ parameter n        ]
//   ...
// [ parameter 1        ]
// [ expression stack   ]
//
//
// We are free to blow any registers we like because the call_stub which brought us here
// initially has preserved the callee save registers already.
//
//

static address interpreter_frame_manager = NULL;

address InterpreterGenerator::generate_normal_entry(bool synchronized) {

  // rbx: methodOop
1611
  // rsi/r13: sender sp
D
duke 已提交
1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633

  // Because we redispatch "recursive" interpreter entries thru this same entry point
  // the "input" register usage is a little strange and not what you expect coming
  // from the call_stub. From the call stub rsi/rdi (current/previous) interpreter
  // state are NULL but on "recursive" dispatches they are what you'd expect.
  // rsi: current interpreter state (C++ interpreter) must preserve (null from call_stub/c1/c2)


  // A single frame manager is plenty as we don't specialize for synchronized. We could and
  // the code is pretty much ready. Would need to change the test below and for good measure
  // modify generate_interpreter_state to only do the (pre) sync stuff stuff for synchronized
  // routines. Not clear this is worth it yet.

  if (interpreter_frame_manager) return interpreter_frame_manager;

  address entry_point = __ pc();

  // Fast accessor methods share this entry point.
  // This works because frame manager is in the same codelet
  if (UseFastAccessorMethods && !synchronized) __ bind(fast_accessor_slow_entry_path);

  Label dispatch_entry_2;
1634 1635
  __ movptr(rcx, sender_sp_on_entry);
  __ movptr(state, (int32_t)NULL_WORD);                              // no current activation
D
duke 已提交
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645

  __ jmp(dispatch_entry_2);

  const Register locals  = rdi;

  Label re_dispatch;

  __ bind(re_dispatch);

  // save sender sp (doesn't include return address
1646
  __ lea(rcx, Address(rsp, wordSize));
D
duke 已提交
1647 1648 1649 1650

  __ bind(dispatch_entry_2);

  // save sender sp
1651
  __ push(rcx);
D
duke 已提交
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667

  const Address size_of_parameters(rbx, methodOopDesc::size_of_parameters_offset());
  const Address size_of_locals    (rbx, methodOopDesc::size_of_locals_offset());
  const Address access_flags      (rbx, methodOopDesc::access_flags_offset());

  // const Address monitor_block_top (rbp, frame::interpreter_frame_monitor_block_top_offset * wordSize);
  // const Address monitor_block_bot (rbp, frame::interpreter_frame_initial_sp_offset        * wordSize);
  // const Address monitor(rbp, frame::interpreter_frame_initial_sp_offset * wordSize - (int)sizeof(BasicObjectLock));

  // get parameter size (always needed)
  __ load_unsigned_word(rcx, size_of_parameters);

  // rbx: methodOop
  // rcx: size of parameters
  __ load_unsigned_word(rdx, size_of_locals);                      // get size of locals in words

1668
  __ subptr(rdx, rcx);                                             // rdx = no. of additional locals
D
duke 已提交
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679

  // see if we've got enough room on the stack for locals plus overhead.
  generate_stack_overflow_check();                                 // C++

  // c++ interpreter does not use stack banging or any implicit exceptions
  // leave for now to verify that check is proper.
  bang_stack_shadow_pages(false);



  // compute beginning of parameters (rdi)
1680
  __ lea(locals, Address(rsp, rcx, Address::times_ptr, wordSize));
D
duke 已提交
1681 1682 1683 1684 1685

  // save sender's sp
  // __ movl(rcx, rsp);

  // get sender's sp
1686
  __ pop(rcx);
D
duke 已提交
1687 1688

  // get return address
1689
  __ pop(rax);
D
duke 已提交
1690 1691 1692 1693 1694 1695

  // rdx - # of additional locals
  // allocate space for locals
  // explicitly initialize locals
  {
    Label exit, loop;
1696
    __ testl(rdx, rdx);                               // (32bit ok)
D
duke 已提交
1697 1698
    __ jcc(Assembler::lessEqual, exit);               // do nothing if rdx <= 0
    __ bind(loop);
1699
    __ push((int32_t)NULL_WORD);                      // initialize local variables
D
duke 已提交
1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734
    __ decrement(rdx);                                // until everything initialized
    __ jcc(Assembler::greater, loop);
    __ bind(exit);
  }


  // Assumes rax = return address

  // allocate and initialize new interpreterState and method expression stack
  // IN(locals) ->  locals
  // IN(state) -> any current interpreter activation
  // destroys rax, rcx, rdx, rdi
  // OUT (state) -> new interpreterState
  // OUT(rsp) -> bottom of methods expression stack

  generate_compute_interpreter_state(state, locals, rcx, false);

  // Call interpreter

  Label call_interpreter;
  __ bind(call_interpreter);

  // c++ interpreter does not use stack banging or any implicit exceptions
  // leave for now to verify that check is proper.
  bang_stack_shadow_pages(false);


  // Call interpreter enter here if message is
  // set and we know stack size is valid

  Label call_interpreter_2;

  __ bind(call_interpreter_2);

  {
1735
    const Register thread  = NOT_LP64(rcx) LP64_ONLY(r15_thread);
D
duke 已提交
1736

1737 1738 1739 1740 1741 1742
#ifdef _LP64
    __ mov(c_rarg0, state);
#else
    __ push(state);                                                 // push arg to interpreter
    __ movptr(thread, STATE(_thread));
#endif // _LP64
D
duke 已提交
1743 1744 1745 1746 1747

    // We can setup the frame anchor with everything we want at this point
    // as we are thread_in_Java and no safepoints can occur until we go to
    // vm mode. We do have to clear flags on return from vm but that is it
    //
1748 1749
    __ movptr(Address(thread, JavaThread::last_Java_fp_offset()), rbp);
    __ movptr(Address(thread, JavaThread::last_Java_sp_offset()), rsp);
D
duke 已提交
1750 1751 1752 1753 1754 1755 1756

    // Call the interpreter

    RuntimeAddress normal(CAST_FROM_FN_PTR(address, BytecodeInterpreter::run));
    RuntimeAddress checking(CAST_FROM_FN_PTR(address, BytecodeInterpreter::runWithChecks));

    __ call(JvmtiExport::can_post_interpreter_events() ? checking : normal);
1757
    NOT_LP64(__ pop(rax);)                                          // discard parameter to run
D
duke 已提交
1758 1759 1760 1761 1762 1763
    //
    // state is preserved since it is callee saved
    //

    // reset_last_Java_frame

1764
    NOT_LP64(__ movl(thread, STATE(_thread));)
D
duke 已提交
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777
    __ reset_last_Java_frame(thread, true, true);
  }

  // examine msg from interpreter to determine next action

  __ movl(rdx, STATE(_msg));                                       // Get new message

  Label call_method;
  Label return_from_interpreted_method;
  Label throw_exception;
  Label bad_msg;
  Label do_OSR;

1778
  __ cmpl(rdx, (int32_t)BytecodeInterpreter::call_method);
D
duke 已提交
1779
  __ jcc(Assembler::equal, call_method);
1780
  __ cmpl(rdx, (int32_t)BytecodeInterpreter::return_from_method);
D
duke 已提交
1781
  __ jcc(Assembler::equal, return_from_interpreted_method);
1782
  __ cmpl(rdx, (int32_t)BytecodeInterpreter::do_osr);
D
duke 已提交
1783
  __ jcc(Assembler::equal, do_OSR);
1784
  __ cmpl(rdx, (int32_t)BytecodeInterpreter::throwing_exception);
D
duke 已提交
1785
  __ jcc(Assembler::equal, throw_exception);
1786
  __ cmpl(rdx, (int32_t)BytecodeInterpreter::more_monitors);
D
duke 已提交
1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798
  __ jcc(Assembler::notEqual, bad_msg);

  // Allocate more monitor space, shuffle expression stack....

  generate_more_monitors();

  __ jmp(call_interpreter);

  // uncommon trap needs to jump to here to enter the interpreter (re-execute current bytecode)
  unctrap_frame_manager_entry  = __ pc();
  //
  // Load the registers we need.
1799 1800
  __ lea(state, Address(rbp, -(int)sizeof(BytecodeInterpreter)));
  __ movptr(rsp, STATE(_stack_limit));                             // restore expression stack to full depth
D
duke 已提交
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831
  __ jmp(call_interpreter_2);



  //=============================================================================
  // Returning from a compiled method into a deopted method. The bytecode at the
  // bcp has completed. The result of the bytecode is in the native abi (the tosca
  // for the template based interpreter). Any stack space that was used by the
  // bytecode that has completed has been removed (e.g. parameters for an invoke)
  // so all that we have to do is place any pending result on the expression stack
  // and resume execution on the next bytecode.


  generate_deopt_handling();
  __ jmp(call_interpreter);


  // Current frame has caught an exception we need to dispatch to the
  // handler. We can get here because a native interpreter frame caught
  // an exception in which case there is no handler and we must rethrow
  // If it is a vanilla interpreted frame the we simply drop into the
  // interpreter and let it do the lookup.

  Interpreter::_rethrow_exception_entry = __ pc();
  // rax: exception
  // rdx: return address/pc that threw exception

  Label return_with_exception;
  Label unwind_and_forward;

  // restore state pointer.
1832
  __ lea(state, Address(rbp,  -(int)sizeof(BytecodeInterpreter)));
D
duke 已提交
1833

1834 1835 1836 1837
  __ movptr(rbx, STATE(_method));                       // get method
#ifdef _LP64
  __ movptr(Address(r15_thread, Thread::pending_exception_offset()), rax);
#else
D
duke 已提交
1838 1839 1840
  __ movl(rcx, STATE(_thread));                       // get thread

  // Store exception with interpreter will expect it
1841 1842
  __ movptr(Address(rcx, Thread::pending_exception_offset()), rax);
#endif // _LP64
D
duke 已提交
1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857

  // is current frame vanilla or native?

  __ movl(rdx, access_flags);
  __ testl(rdx, JVM_ACC_NATIVE);
  __ jcc(Assembler::zero, return_with_exception);     // vanilla interpreted frame, handle directly

  // We drop thru to unwind a native interpreted frame with a pending exception
  // We jump here for the initial interpreter frame with exception pending
  // We unwind the current acivation and forward it to our caller.

  __ bind(unwind_and_forward);

  // unwind rbp, return stack to unextended value and re-push return address

1858
  __ movptr(rcx, STATE(_sender_sp));
D
duke 已提交
1859
  __ leave();
1860 1861 1862
  __ pop(rdx);
  __ mov(rsp, rcx);
  __ push(rdx);
D
duke 已提交
1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879
  __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));

  // Return point from a call which returns a result in the native abi
  // (c1/c2/jni-native). This result must be processed onto the java
  // expression stack.
  //
  // A pending exception may be present in which case there is no result present

  Label resume_interpreter;
  Label do_float;
  Label do_double;
  Label done_conv;

  address compiled_entry = __ pc();

  // The FPU stack is clean if UseSSE >= 2 but must be cleaned in other cases
  if (UseSSE < 2) {
1880
    __ lea(state, Address(rbp,  -(int)sizeof(BytecodeInterpreter)));
1881
    __ movptr(rbx, STATE(_result._to_call._callee));                   // get method just executed
D
duke 已提交
1882 1883 1884 1885 1886
    __ movl(rcx, Address(rbx, methodOopDesc::result_index_offset()));
    __ cmpl(rcx, AbstractInterpreter::BasicType_as_index(T_FLOAT));    // Result stub address array index
    __ jcc(Assembler::equal, do_float);
    __ cmpl(rcx, AbstractInterpreter::BasicType_as_index(T_DOUBLE));    // Result stub address array index
    __ jcc(Assembler::equal, do_double);
1887
#if !defined(_LP64) || defined(COMPILER1) || !defined(COMPILER2)
D
duke 已提交
1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910
    __ empty_FPU_stack();
#endif // COMPILER2
    __ jmp(done_conv);

    __ bind(do_float);
#ifdef COMPILER2
    for (int i = 1; i < 8; i++) {
      __ ffree(i);
    }
#endif // COMPILER2
    __ jmp(done_conv);
    __ bind(do_double);
#ifdef COMPILER2
    for (int i = 1; i < 8; i++) {
      __ ffree(i);
    }
#endif // COMPILER2
    __ jmp(done_conv);
  } else {
    __ MacroAssembler::verify_FPU(0, "generate_return_entry_for compiled");
    __ jmp(done_conv);
  }

1911
#if 0
D
duke 已提交
1912 1913 1914 1915
  // emit a sentinel we can test for when converting an interpreter
  // entry point to a compiled entry point.
  __ a_long(Interpreter::return_sentinel);
  __ a_long((int)compiled_entry);
1916
#endif
D
duke 已提交
1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928

  // Return point to interpreter from compiled/native method

  InternalAddress return_from_native_method(__ pc());

  __ bind(done_conv);


  // Result if any is in tosca. The java expression stack is in the state that the
  // calling convention left it (i.e. params may or may not be present)
  // Copy the result from tosca and place it on java expression stack.

1929
  // Restore rsi/r13 as compiled code may not preserve it
D
duke 已提交
1930

1931
  __ lea(state, Address(rbp,  -(int)sizeof(BytecodeInterpreter)));
D
duke 已提交
1932 1933 1934

  // restore stack to what we had when we left (in case i2c extended it)

1935 1936
  __ movptr(rsp, STATE(_stack));
  __ lea(rsp, Address(rsp, wordSize));
D
duke 已提交
1937 1938 1939

  // If there is a pending exception then we don't really have a result to process

1940 1941 1942 1943 1944
#ifdef _LP64
  __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
#else
  __ movptr(rcx, STATE(_thread));                       // get thread
  __ cmpptr(Address(rcx, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
1945
#endif // _LP64
D
duke 已提交
1946 1947 1948
  __ jcc(Assembler::notZero, return_with_exception);

  // get method just executed
1949
  __ movptr(rbx, STATE(_result._to_call._callee));
D
duke 已提交
1950 1951 1952

  // callee left args on top of expression stack, remove them
  __ load_unsigned_word(rcx, Address(rbx, methodOopDesc::size_of_parameters_offset()));
1953
  __ lea(rsp, Address(rsp, rcx, Address::times_ptr));
D
duke 已提交
1954 1955 1956

  __ movl(rcx, Address(rbx, methodOopDesc::result_index_offset()));
  ExternalAddress tosca_to_stack((address)CppInterpreter::_tosca_to_stack);
1957 1958 1959
  // Address index(noreg, rax, Address::times_ptr);
  __ movptr(rcx, ArrayAddress(tosca_to_stack, Address(noreg, rcx, Address::times_ptr)));
  // __ movl(rcx, Address(noreg, rcx, Address::times_ptr, int(AbstractInterpreter::_tosca_to_stack)));
D
duke 已提交
1960 1961 1962 1963 1964 1965 1966 1967 1968
  __ call(rcx);                                               // call result converter
  __ jmp(resume_interpreter);

  // An exception is being caught on return to a vanilla interpreter frame.
  // Empty the stack and resume interpreter

  __ bind(return_with_exception);

  // Exception present, empty stack
1969
  __ movptr(rsp, STATE(_stack_base));
D
duke 已提交
1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
  __ jmp(resume_interpreter);

  // Return from interpreted method we return result appropriate to the caller (i.e. "recursive"
  // interpreter call, or native) and unwind this interpreter activation.
  // All monitors should be unlocked.

  __ bind(return_from_interpreted_method);

  Label return_to_initial_caller;

1980 1981
  __ movptr(rbx, STATE(_method));                                   // get method just executed
  __ cmpptr(STATE(_prev_link), (int32_t)NULL_WORD);                 // returning from "recursive" interpreter call?
D
duke 已提交
1982 1983 1984 1985 1986
  __ movl(rax, Address(rbx, methodOopDesc::result_index_offset())); // get result type index
  __ jcc(Assembler::equal, return_to_initial_caller);               // back to native code (call_stub/c1/c2)

  // Copy result to callers java stack
  ExternalAddress stack_to_stack((address)CppInterpreter::_stack_to_stack);
1987
  // Address index(noreg, rax, Address::times_ptr);
D
duke 已提交
1988

1989 1990
  __ movptr(rax, ArrayAddress(stack_to_stack, Address(noreg, rax, Address::times_ptr)));
  // __ movl(rax, Address(noreg, rax, Address::times_ptr, int(AbstractInterpreter::_stack_to_stack)));
D
duke 已提交
1991 1992 1993 1994 1995 1996 1997 1998 1999
  __ call(rax);                                                     // call result converter

  Label unwind_recursive_activation;
  __ bind(unwind_recursive_activation);

  // returning to interpreter method from "recursive" interpreter call
  // result converter left rax pointing to top of the java stack for method we are returning
  // to. Now all we must do is unwind the state from the completed call

2000
  __ movptr(state, STATE(_prev_link));                              // unwind state
D
duke 已提交
2001
  __ leave();                                                       // pop the frame
2002
  __ mov(rsp, rax);                                                 // unwind stack to remove args
D
duke 已提交
2003 2004 2005 2006 2007 2008 2009 2010 2011 2012

  // Resume the interpreter. The current frame contains the current interpreter
  // state object.
  //

  __ bind(resume_interpreter);

  // state == interpreterState object for method we are resuming

  __ movl(STATE(_msg), (int)BytecodeInterpreter::method_resume);
2013 2014
  __ lea(rsp, Address(rsp, -wordSize));                            // prepush stack (result if any already present)
  __ movptr(STATE(_stack), rsp);                                   // inform interpreter of new stack depth (parameters removed,
D
duke 已提交
2015
                                                                   // result if any on stack already )
2016
  __ movptr(rsp, STATE(_stack_limit));                             // restore expression stack to full depth
D
duke 已提交
2017 2018 2019 2020 2021 2022 2023 2024
  __ jmp(call_interpreter_2);                                      // No need to bang

  // interpreter returning to native code (call_stub/c1/c2)
  // convert result and unwind initial activation
  // rax - result index

  __ bind(return_to_initial_caller);
  ExternalAddress stack_to_native((address)CppInterpreter::_stack_to_native_abi);
2025
  // Address index(noreg, rax, Address::times_ptr);
D
duke 已提交
2026

2027
  __ movptr(rax, ArrayAddress(stack_to_native, Address(noreg, rax, Address::times_ptr)));
D
duke 已提交
2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048
  __ call(rax);                                                    // call result converter

  Label unwind_initial_activation;
  __ bind(unwind_initial_activation);

  // RETURN TO CALL_STUB/C1/C2 code (result if any in rax/rdx ST(0))

  /* Current stack picture

        [ incoming parameters ]
        [ extra locals ]
        [ return address to CALL_STUB/C1/C2]
  fp -> [ CALL_STUB/C1/C2 fp ]
        BytecodeInterpreter object
        expression stack
  sp ->

  */

  // return restoring the stack to the original sender_sp value

2049
  __ movptr(rcx, STATE(_sender_sp));
D
duke 已提交
2050
  __ leave();
2051
  __ pop(rdi);                                                        // get return address
D
duke 已提交
2052
  // set stack to sender's sp
2053
  __ mov(rsp, rcx);
D
duke 已提交
2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066
  __ jmp(rdi);                                                        // return to call_stub

  // OSR request, adjust return address to make current frame into adapter frame
  // and enter OSR nmethod

  __ bind(do_OSR);

  Label remove_initial_frame;

  // We are going to pop this frame. Is there another interpreter frame underneath
  // it or is it callstub/compiled?

  // Move buffer to the expected parameter location
2067
  __ movptr(rcx, STATE(_result._osr._osr_buf));
D
duke 已提交
2068

2069
  __ movptr(rax, STATE(_result._osr._osr_entry));
D
duke 已提交
2070

2071
  __ cmpptr(STATE(_prev_link), (int32_t)NULL_WORD);            // returning from "recursive" interpreter call?
D
duke 已提交
2072 2073
  __ jcc(Assembler::equal, remove_initial_frame);              // back to native code (call_stub/c1/c2)

2074
  __ movptr(sender_sp_on_entry, STATE(_sender_sp));            // get sender's sp in expected register
D
duke 已提交
2075
  __ leave();                                                  // pop the frame
2076
  __ mov(rsp, sender_sp_on_entry);                             // trim any stack expansion
D
duke 已提交
2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089


  // We know we are calling compiled so push specialized return
  // method uses specialized entry, push a return so we look like call stub setup
  // this path will handle fact that result is returned in registers and not
  // on the java stack.

  __ pushptr(return_from_native_method.addr());

  __ jmp(rax);

  __ bind(remove_initial_frame);

2090
  __ movptr(rdx, STATE(_sender_sp));
D
duke 已提交
2091 2092
  __ leave();
  // get real return
2093
  __ pop(rsi);
D
duke 已提交
2094
  // set stack to sender's sp
2095
  __ mov(rsp, rdx);
D
duke 已提交
2096
  // repush real return
2097
  __ push(rsi);
D
duke 已提交
2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111
  // Enter OSR nmethod
  __ jmp(rax);




  // Call a new method. All we do is (temporarily) trim the expression stack
  // push a return address to bring us back to here and leap to the new entry.

  __ bind(call_method);

  // stack points to next free location and not top element on expression stack
  // method expects sp to be pointing to topmost element

2112 2113
  __ movptr(rsp, STATE(_stack));                                     // pop args to c++ interpreter, set sp to java stack top
  __ lea(rsp, Address(rsp, wordSize));
D
duke 已提交
2114

2115
  __ movptr(rbx, STATE(_result._to_call._callee));                   // get method to execute
D
duke 已提交
2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130

  // don't need a return address if reinvoking interpreter

  // Make it look like call_stub calling conventions

  // Get (potential) receiver
  __ load_unsigned_word(rcx, size_of_parameters);                     // get size of parameters in words

  ExternalAddress recursive(CAST_FROM_FN_PTR(address, RecursiveInterpreterActivation));
  __ pushptr(recursive.addr());                                      // make it look good in the debugger

  InternalAddress entry(entry_point);
  __ cmpptr(STATE(_result._to_call._callee_entry_point), entry.addr()); // returning to interpreter?
  __ jcc(Assembler::equal, re_dispatch);                             // yes

2131
  __ pop(rax);                                                       // pop dummy address
D
duke 已提交
2132 2133 2134


  // get specialized entry
2135
  __ movptr(rax, STATE(_result._to_call._callee_entry_point));
D
duke 已提交
2136
  // set sender SP
2137
  __ mov(sender_sp_on_entry, rsp);
D
duke 已提交
2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156

  // method uses specialized entry, push a return so we look like call stub setup
  // this path will handle fact that result is returned in registers and not
  // on the java stack.

  __ pushptr(return_from_native_method.addr());

  __ jmp(rax);

  __ bind(bad_msg);
  __ stop("Bad message from interpreter");

  // Interpreted method "returned" with an exception pass it on...
  // Pass result, unwind activation and continue/return to interpreter/call_stub
  // We handle result (if any) differently based on return to interpreter or call_stub

  Label unwind_initial_with_pending_exception;

  __ bind(throw_exception);
2157
  __ cmpptr(STATE(_prev_link), (int32_t)NULL_WORD);                 // returning from recursive interpreter call?
D
duke 已提交
2158
  __ jcc(Assembler::equal, unwind_initial_with_pending_exception);  // no, back to native code (call_stub/c1/c2)
2159 2160
  __ movptr(rax, STATE(_locals));                                   // pop parameters get new stack value
  __ addptr(rax, wordSize);                                         // account for prepush before we return
D
duke 已提交
2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413
  __ jmp(unwind_recursive_activation);

  __ bind(unwind_initial_with_pending_exception);

  // We will unwind the current (initial) interpreter frame and forward
  // the exception to the caller. We must put the exception in the
  // expected register and clear pending exception and then forward.

  __ jmp(unwind_and_forward);

  interpreter_frame_manager = entry_point;
  return entry_point;
}

address AbstractInterpreterGenerator::generate_method_entry(AbstractInterpreter::MethodKind kind) {
  // determine code generation flags
  bool synchronized = false;
  address entry_point = NULL;

  switch (kind) {
    case Interpreter::zerolocals             :                                                                             break;
    case Interpreter::zerolocals_synchronized: synchronized = true;                                                        break;
    case Interpreter::native                 : entry_point = ((InterpreterGenerator*)this)->generate_native_entry(false);  break;
    case Interpreter::native_synchronized    : entry_point = ((InterpreterGenerator*)this)->generate_native_entry(true);   break;
    case Interpreter::empty                  : entry_point = ((InterpreterGenerator*)this)->generate_empty_entry();        break;
    case Interpreter::accessor               : entry_point = ((InterpreterGenerator*)this)->generate_accessor_entry();     break;
    case Interpreter::abstract               : entry_point = ((InterpreterGenerator*)this)->generate_abstract_entry();     break;

    case Interpreter::java_lang_math_sin     : // fall thru
    case Interpreter::java_lang_math_cos     : // fall thru
    case Interpreter::java_lang_math_tan     : // fall thru
    case Interpreter::java_lang_math_abs     : // fall thru
    case Interpreter::java_lang_math_log     : // fall thru
    case Interpreter::java_lang_math_log10   : // fall thru
    case Interpreter::java_lang_math_sqrt    : entry_point = ((InterpreterGenerator*)this)->generate_math_entry(kind);     break;
    default                                  : ShouldNotReachHere();                                                       break;
  }

  if (entry_point) return entry_point;

  return ((InterpreterGenerator*)this)->generate_normal_entry(synchronized);

}

InterpreterGenerator::InterpreterGenerator(StubQueue* code)
 : CppInterpreterGenerator(code) {
   generate_all(); // down here so it can be "virtual"
}

// Deoptimization helpers for C++ interpreter

// How much stack a method activation needs in words.
int AbstractInterpreter::size_top_interpreter_activation(methodOop method) {

  const int stub_code = 4;  // see generate_call_stub
  // Save space for one monitor to get into the interpreted method in case
  // the method is synchronized
  int monitor_size    = method->is_synchronized() ?
                                1*frame::interpreter_frame_monitor_size() : 0;

  // total static overhead size. Account for interpreter state object, return
  // address, saved rbp and 2 words for a "static long no_params() method" issue.

  const int overhead_size = sizeof(BytecodeInterpreter)/wordSize +
    ( frame::sender_sp_offset - frame::link_offset) + 2;

  const int method_stack = (method->max_locals() + method->max_stack()) *
                           Interpreter::stackElementWords();
  return overhead_size + method_stack + stub_code;
}

// returns the activation size.
static int size_activation_helper(int extra_locals_size, int monitor_size) {
  return (extra_locals_size +                  // the addition space for locals
          2*BytesPerWord +                     // return address and saved rbp
          2*BytesPerWord +                     // "static long no_params() method" issue
          sizeof(BytecodeInterpreter) +               // interpreterState
          monitor_size);                       // monitors
}

void BytecodeInterpreter::layout_interpreterState(interpreterState to_fill,
                                           frame* caller,
                                           frame* current,
                                           methodOop method,
                                           intptr_t* locals,
                                           intptr_t* stack,
                                           intptr_t* stack_base,
                                           intptr_t* monitor_base,
                                           intptr_t* frame_bottom,
                                           bool is_top_frame
                                           )
{
  // What about any vtable?
  //
  to_fill->_thread = JavaThread::current();
  // This gets filled in later but make it something recognizable for now
  to_fill->_bcp = method->code_base();
  to_fill->_locals = locals;
  to_fill->_constants = method->constants()->cache();
  to_fill->_method = method;
  to_fill->_mdx = NULL;
  to_fill->_stack = stack;
  if (is_top_frame && JavaThread::current()->popframe_forcing_deopt_reexecution() ) {
    to_fill->_msg = deopt_resume2;
  } else {
    to_fill->_msg = method_resume;
  }
  to_fill->_result._to_call._bcp_advance = 0;
  to_fill->_result._to_call._callee_entry_point = NULL; // doesn't matter to anyone
  to_fill->_result._to_call._callee = NULL; // doesn't matter to anyone
  to_fill->_prev_link = NULL;

  to_fill->_sender_sp = caller->unextended_sp();

  if (caller->is_interpreted_frame()) {
    interpreterState prev  = caller->get_interpreterState();
    to_fill->_prev_link = prev;
    // *current->register_addr(GR_Iprev_state) = (intptr_t) prev;
    // Make the prev callee look proper
    prev->_result._to_call._callee = method;
    if (*prev->_bcp == Bytecodes::_invokeinterface) {
      prev->_result._to_call._bcp_advance = 5;
    } else {
      prev->_result._to_call._bcp_advance = 3;
    }
  }
  to_fill->_oop_temp = NULL;
  to_fill->_stack_base = stack_base;
  // Need +1 here because stack_base points to the word just above the first expr stack entry
  // and stack_limit is supposed to point to the word just below the last expr stack entry.
  // See generate_compute_interpreter_state.
  to_fill->_stack_limit = stack_base - (method->max_stack() + 1);
  to_fill->_monitor_base = (BasicObjectLock*) monitor_base;

  to_fill->_self_link = to_fill;
  assert(stack >= to_fill->_stack_limit && stack < to_fill->_stack_base,
         "Stack top out of range");
}

int AbstractInterpreter::layout_activation(methodOop method,
                                                int tempcount,  //
                                                int popframe_extra_args,
                                                int moncount,
                                                int callee_param_count,
                                                int callee_locals,
                                                frame* caller,
                                                frame* interpreter_frame,
                                                bool is_top_frame) {

  assert(popframe_extra_args == 0, "FIX ME");
  // NOTE this code must exactly mimic what InterpreterGenerator::generate_compute_interpreter_state()
  // does as far as allocating an interpreter frame.
  // If interpreter_frame!=NULL, set up the method, locals, and monitors.
  // The frame interpreter_frame, if not NULL, is guaranteed to be the right size,
  // as determined by a previous call to this method.
  // It is also guaranteed to be walkable even though it is in a skeletal state
  // NOTE: return size is in words not bytes
  // NOTE: tempcount is the current size of the java expression stack. For top most
  //       frames we will allocate a full sized expression stack and not the curback
  //       version that non-top frames have.

  // Calculate the amount our frame will be adjust by the callee. For top frame
  // this is zero.

  // NOTE: ia64 seems to do this wrong (or at least backwards) in that it
  // calculates the extra locals based on itself. Not what the callee does
  // to it. So it ignores last_frame_adjust value. Seems suspicious as far
  // as getting sender_sp correct.

  int extra_locals_size = (callee_locals - callee_param_count) * BytesPerWord;
  int monitor_size = sizeof(BasicObjectLock) * moncount;

  // First calculate the frame size without any java expression stack
  int short_frame_size = size_activation_helper(extra_locals_size,
                                                monitor_size);

  // Now with full size expression stack
  int full_frame_size = short_frame_size + method->max_stack() * BytesPerWord;

  // and now with only live portion of the expression stack
  short_frame_size = short_frame_size + tempcount * BytesPerWord;

  // the size the activation is right now. Only top frame is full size
  int frame_size = (is_top_frame ? full_frame_size : short_frame_size);

  if (interpreter_frame != NULL) {
#ifdef ASSERT
    assert(caller->unextended_sp() == interpreter_frame->interpreter_frame_sender_sp(), "Frame not properly walkable");
#endif

    // MUCHO HACK

    intptr_t* frame_bottom = (intptr_t*) ((intptr_t)interpreter_frame->sp() - (full_frame_size - frame_size));

    /* Now fillin the interpreterState object */

    // The state object is the first thing on the frame and easily located

    interpreterState cur_state = (interpreterState) ((intptr_t)interpreter_frame->fp() - sizeof(BytecodeInterpreter));


    // Find the locals pointer. This is rather simple on x86 because there is no
    // confusing rounding at the callee to account for. We can trivially locate
    // our locals based on the current fp().
    // Note: the + 2 is for handling the "static long no_params() method" issue.
    // (too bad I don't really remember that issue well...)

    intptr_t* locals;
    // If the caller is interpreted we need to make sure that locals points to the first
    // argument that the caller passed and not in an area where the stack might have been extended.
    // because the stack to stack to converter needs a proper locals value in order to remove the
    // arguments from the caller and place the result in the proper location. Hmm maybe it'd be
    // simpler if we simply stored the result in the BytecodeInterpreter object and let the c++ code
    // adjust the stack?? HMMM QQQ
    //
    if (caller->is_interpreted_frame()) {
      // locals must agree with the caller because it will be used to set the
      // caller's tos when we return.
      interpreterState prev  = caller->get_interpreterState();
      // stack() is prepushed.
      locals = prev->stack() + method->size_of_parameters();
      // locals = caller->unextended_sp() + (method->size_of_parameters() - 1);
      if (locals != interpreter_frame->fp() + frame::sender_sp_offset + (method->max_locals() - 1) + 2) {
        // os::breakpoint();
      }
    } else {
      // this is where a c2i would have placed locals (except for the +2)
      locals = interpreter_frame->fp() + frame::sender_sp_offset + (method->max_locals() - 1) + 2;
    }

    intptr_t* monitor_base = (intptr_t*) cur_state;
    intptr_t* stack_base = (intptr_t*) ((intptr_t) monitor_base - monitor_size);
    /* +1 because stack is always prepushed */
    intptr_t* stack = (intptr_t*) ((intptr_t) stack_base - (tempcount + 1) * BytesPerWord);


    BytecodeInterpreter::layout_interpreterState(cur_state,
                                          caller,
                                          interpreter_frame,
                                          method,
                                          locals,
                                          stack,
                                          stack_base,
                                          monitor_base,
                                          frame_bottom,
                                          is_top_frame);

    // BytecodeInterpreter::pd_layout_interpreterState(cur_state, interpreter_return_address, interpreter_frame->fp());
  }
  return frame_size/BytesPerWord;
}

#endif // CC_INTERP (all)