cppInterpreter_zero.cpp 48.9 KB
Newer Older
1
/*
2
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
T
twisti 已提交
3
 * Copyright 2007, 2008, 2009, 2010, 2011 Red Hat, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 *
20 21 22
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
23 24 25
 *
 */

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include "precompiled.hpp"
#include "asm/assembler.hpp"
#include "interpreter/bytecodeHistogram.hpp"
#include "interpreter/cppInterpreter.hpp"
#include "interpreter/interpreter.hpp"
#include "interpreter/interpreterGenerator.hpp"
#include "interpreter/interpreterRuntime.hpp"
#include "oops/arrayOop.hpp"
#include "oops/methodDataOop.hpp"
#include "oops/methodOop.hpp"
#include "oops/oop.inline.hpp"
#include "prims/jvmtiExport.hpp"
#include "prims/jvmtiThreadState.hpp"
#include "runtime/arguments.hpp"
#include "runtime/deoptimization.hpp"
#include "runtime/frame.inline.hpp"
#include "runtime/interfaceSupport.hpp"
#include "runtime/sharedRuntime.hpp"
#include "runtime/stubRoutines.hpp"
#include "runtime/synchronizer.hpp"
#include "runtime/timer.hpp"
#include "runtime/vframeArray.hpp"
#include "stack_zero.inline.hpp"
#include "utilities/debug.hpp"
#ifdef SHARK
#include "shark/shark_globals.hpp"
#endif
53 54 55 56 57 58

#ifdef CC_INTERP

#define fixup_after_potential_safepoint()       \
  method = istate->method()

T
twisti 已提交
59
#define CALL_VM_NOCHECK_NOFIX(func)             \
60 61
  thread->set_last_Java_frame();                \
  func;                                         \
T
twisti 已提交
62 63 64 65
  thread->reset_last_Java_frame();

#define CALL_VM_NOCHECK(func)                   \
  CALL_VM_NOCHECK_NOFIX(func)                   \
66 67
  fixup_after_potential_safepoint()

T
twisti 已提交
68
int CppInterpreter::normal_entry(methodOop method, intptr_t UNUSED, TRAPS) {
69 70 71
  JavaThread *thread = (JavaThread *) THREAD;

  // Allocate and initialize our frame.
T
twisti 已提交
72
  InterpreterFrame *frame = InterpreterFrame::build(method, CHECK_0);
73 74 75 76
  thread->push_zero_frame(frame);

  // Execute those bytecodes!
  main_loop(0, THREAD);
T
twisti 已提交
77 78 79

  // No deoptimized frames on the stack
  return 0;
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
}

void CppInterpreter::main_loop(int recurse, TRAPS) {
  JavaThread *thread = (JavaThread *) THREAD;
  ZeroStack *stack = thread->zero_stack();

  // If we are entering from a deopt we may need to call
  // ourself a few times in order to get to our frame.
  if (recurse)
    main_loop(recurse - 1, THREAD);

  InterpreterFrame *frame = thread->top_zero_frame()->as_interpreter_frame();
  interpreterState istate = frame->interpreter_state();
  methodOop method = istate->method();

  intptr_t *result = NULL;
  int result_slots = 0;

  while (true) {
    // We can set up 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.
    thread->set_last_Java_frame();

    // Call the interpreter
    if (JvmtiExport::can_post_interpreter_events())
      BytecodeInterpreter::runWithChecks(istate);
    else
      BytecodeInterpreter::run(istate);
    fixup_after_potential_safepoint();

    // Clear the frame anchor
    thread->reset_last_Java_frame();

    // Examine the message from the interpreter to decide what to do
    if (istate->msg() == BytecodeInterpreter::call_method) {
      methodOop callee = istate->callee();

      // Trim back the stack to put the parameters at the top
      stack->set_sp(istate->stack() + 1);

      // Make the call
      Interpreter::invoke_method(callee, istate->callee_entry_point(), THREAD);
      fixup_after_potential_safepoint();

      // Convert the result
      istate->set_stack(stack->sp() - 1);

      // Restore the stack
      stack->set_sp(istate->stack_limit() + 1);

      // Resume the interpreter
      istate->set_msg(BytecodeInterpreter::method_resume);
    }
    else if (istate->msg() == BytecodeInterpreter::more_monitors) {
      int monitor_words = frame::interpreter_frame_monitor_size();

      // Allocate the space
139 140 141
      stack->overflow_check(monitor_words, THREAD);
      if (HAS_PENDING_EXCEPTION)
        break;
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
      stack->alloc(monitor_words * wordSize);

      // Move the expression stack contents
      for (intptr_t *p = istate->stack() + 1; p < istate->stack_base(); p++)
        *(p - monitor_words) = *p;

      // Move the expression stack pointers
      istate->set_stack_limit(istate->stack_limit() - monitor_words);
      istate->set_stack(istate->stack() - monitor_words);
      istate->set_stack_base(istate->stack_base() - monitor_words);

      // Zero the new monitor so the interpreter can find it.
      ((BasicObjectLock *) istate->stack_base())->set_obj(NULL);

      // Resume the interpreter
      istate->set_msg(BytecodeInterpreter::got_monitors);
    }
    else if (istate->msg() == BytecodeInterpreter::return_from_method) {
      // Copy the result into the caller's frame
K
kvn 已提交
161
      result_slots = type2size[result_type_of(method)];
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
      assert(result_slots >= 0 && result_slots <= 2, "what?");
      result = istate->stack() + result_slots;
      break;
    }
    else if (istate->msg() == BytecodeInterpreter::throwing_exception) {
      assert(HAS_PENDING_EXCEPTION, "should do");
      break;
    }
    else if (istate->msg() == BytecodeInterpreter::do_osr) {
      // Unwind the current frame
      thread->pop_zero_frame();

      // Remove any extension of the previous frame
      int extra_locals = method->max_locals() - method->size_of_parameters();
      stack->set_sp(stack->sp() + extra_locals);

      // Jump into the OSR method
      Interpreter::invoke_osr(
        method, istate->osr_entry(), istate->osr_buf(), THREAD);
      return;
    }
T
twisti 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    else if (istate->msg() == BytecodeInterpreter::call_method_handle) {
      oop method_handle = istate->callee();

      // Trim back the stack to put the parameters at the top
      stack->set_sp(istate->stack() + 1);

      // Make the call
      process_method_handle(method_handle, THREAD);
      fixup_after_potential_safepoint();

      // Convert the result
      istate->set_stack(stack->sp() - 1);

      // Restore the stack
      stack->set_sp(istate->stack_limit() + 1);

      // Resume the interpreter
      istate->set_msg(BytecodeInterpreter::method_resume);
    }
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    else {
      ShouldNotReachHere();
    }
  }

  // Unwind the current frame
  thread->pop_zero_frame();

  // Pop our local variables
  stack->set_sp(stack->sp() + method->max_locals());

  // Push our result
  for (int i = 0; i < result_slots; i++)
    stack->push(result[-i]);
}

T
twisti 已提交
218
int CppInterpreter::native_entry(methodOop method, intptr_t UNUSED, TRAPS) {
219 220 221 222 223 224 225
  // Make sure method is native and not abstract
  assert(method->is_native() && !method->is_abstract(), "should be");

  JavaThread *thread = (JavaThread *) THREAD;
  ZeroStack *stack = thread->zero_stack();

  // Allocate and initialize our frame
T
twisti 已提交
226
  InterpreterFrame *frame = InterpreterFrame::build(method, CHECK_0);
227 228 229 230
  thread->push_zero_frame(frame);
  interpreterState istate = frame->interpreter_state();
  intptr_t *locals = istate->locals();

T
twisti 已提交
231 232 233 234 235 236 237 238 239 240 241 242
  // Update the invocation counter
  if ((UseCompiler || CountCompiledCalls) && !method->is_synchronized()) {
    InvocationCounter *counter = method->invocation_counter();
    counter->increment();
    if (counter->reached_InvocationLimit()) {
      CALL_VM_NOCHECK(
        InterpreterRuntime::frequency_counter_overflow(thread, NULL));
      if (HAS_PENDING_EXCEPTION)
        goto unwind_and_return;
    }
  }

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
  // Lock if necessary
  BasicObjectLock *monitor;
  monitor = NULL;
  if (method->is_synchronized()) {
    monitor = (BasicObjectLock*) istate->stack_base();
    oop lockee = monitor->obj();
    markOop disp = lockee->mark()->set_unlocked();

    monitor->lock()->set_displaced_header(disp);
    if (Atomic::cmpxchg_ptr(monitor, lockee->mark_addr(), disp) != disp) {
      if (thread->is_lock_owned((address) disp->clear_lock_bits())) {
        monitor->lock()->set_displaced_header(NULL);
      }
      else {
        CALL_VM_NOCHECK(InterpreterRuntime::monitorenter(thread, monitor));
        if (HAS_PENDING_EXCEPTION)
          goto unwind_and_return;
      }
    }
  }

  // Get the signature handler
  InterpreterRuntime::SignatureHandler *handler; {
    address handlerAddr = method->signature_handler();
    if (handlerAddr == NULL) {
      CALL_VM_NOCHECK(InterpreterRuntime::prepare_native_call(thread, method));
      if (HAS_PENDING_EXCEPTION)
270
        goto unlock_unwind_and_return;
271 272 273 274 275 276 277 278

      handlerAddr = method->signature_handler();
      assert(handlerAddr != NULL, "eh?");
    }
    if (handlerAddr == (address) InterpreterRuntime::slow_signature_handler) {
      CALL_VM_NOCHECK(handlerAddr =
        InterpreterRuntime::slow_signature_handler(thread, method, NULL,NULL));
      if (HAS_PENDING_EXCEPTION)
279
        goto unlock_unwind_and_return;
280 281 282 283 284 285 286 287 288 289 290
    }
    handler = \
      InterpreterRuntime::SignatureHandler::from_handlerAddr(handlerAddr);
  }

  // Get the native function entry point
  address function;
  function = method->native_function();
  assert(function != NULL, "should be set if signature handler is");

  // Build the argument list
291 292 293 294
  stack->overflow_check(handler->argument_count() * 2, THREAD);
  if (HAS_PENDING_EXCEPTION)
    goto unlock_unwind_and_return;

295 296 297 298 299 300 301 302 303 304 305
  void **arguments;
  void *mirror; {
    arguments =
      (void **) stack->alloc(handler->argument_count() * sizeof(void **));
    void **dst = arguments;

    void *env = thread->jni_environment();
    *(dst++) = &env;

    if (method->is_static()) {
      istate->set_oop_temp(
306
        method->constants()->pool_holder()->java_mirror());
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 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
      mirror = istate->oop_temp_addr();
      *(dst++) = &mirror;
    }

    intptr_t *src = locals;
    for (int i = dst - arguments; i < handler->argument_count(); i++) {
      ffi_type *type = handler->argument_type(i);
      if (type == &ffi_type_pointer) {
        if (*src) {
          stack->push((intptr_t) src);
          *(dst++) = stack->sp();
        }
        else {
          *(dst++) = src;
        }
        src--;
      }
      else if (type->size == 4) {
        *(dst++) = src--;
      }
      else if (type->size == 8) {
        src--;
        *(dst++) = src--;
      }
      else {
        ShouldNotReachHere();
      }
    }
  }

  // Set up the Java frame anchor
  thread->set_last_Java_frame();

  // Change the thread state to _thread_in_native
  ThreadStateTransition::transition_from_java(thread, _thread_in_native);

  // Make the call
  intptr_t result[4 - LogBytesPerWord];
  ffi_call(handler->cif(), (void (*)()) function, result, arguments);

  // Change the thread state back to _thread_in_Java.
  // ThreadStateTransition::transition_from_native() cannot be used
  // here because it does not check for asynchronous exceptions.
  // We have to manage the transition ourself.
  thread->set_thread_state(_thread_in_native_trans);

  // Make sure new state is visible in the GC thread
  if (os::is_MP()) {
    if (UseMembar) {
      OrderAccess::fence();
    }
    else {
      InterfaceSupport::serialize_memory(thread);
    }
  }

  // Handle safepoint operations, pending suspend requests,
  // and pending asynchronous exceptions.
  if (SafepointSynchronize::do_call_back() ||
      thread->has_special_condition_for_native_trans()) {
    JavaThread::check_special_condition_for_native_trans(thread);
    CHECK_UNHANDLED_OOPS_ONLY(thread->clear_unhandled_oops());
  }

  // Finally we can change the thread state to _thread_in_Java.
  thread->set_thread_state(_thread_in_Java);
  fixup_after_potential_safepoint();

  // Clear the frame anchor
  thread->reset_last_Java_frame();

  // If the result was an oop then unbox it and store it in
  // oop_temp where the garbage collector can see it before
  // we release the handle it might be protected by.
  if (handler->result_type() == &ffi_type_pointer) {
    if (result[0])
      istate->set_oop_temp(*(oop *) result[0]);
    else
      istate->set_oop_temp(NULL);
  }

  // Reset handle block
  thread->active_handles()->clear();

391 392 393 394
 unlock_unwind_and_return:

  // Unlock if necessary
  if (monitor) {
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
    BasicLock *lock = monitor->lock();
    markOop header = lock->displaced_header();
    oop rcvr = monitor->obj();
    monitor->set_obj(NULL);

    if (header != NULL) {
      if (Atomic::cmpxchg_ptr(header, rcvr->mark_addr(), lock) != lock) {
        monitor->set_obj(rcvr); {
          HandleMark hm(thread);
          CALL_VM_NOCHECK(InterpreterRuntime::monitorexit(thread, monitor));
        }
      }
    }
  }

 unwind_and_return:

  // Unwind the current activation
  thread->pop_zero_frame();

  // Pop our parameters
  stack->set_sp(stack->sp() + method->size_of_parameters());

  // Push our result
  if (!HAS_PENDING_EXCEPTION) {
K
kvn 已提交
420 421
    BasicType type = result_type_of(method);
    stack->set_sp(stack->sp() - type2size[type]);
422

K
kvn 已提交
423
    switch (type) {
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
    case T_VOID:
      break;

    case T_BOOLEAN:
#ifndef VM_LITTLE_ENDIAN
      result[0] <<= (BitsPerWord - BitsPerByte);
#endif
      SET_LOCALS_INT(*(jboolean *) result != 0, 0);
      break;

    case T_CHAR:
#ifndef VM_LITTLE_ENDIAN
      result[0] <<= (BitsPerWord - BitsPerShort);
#endif
      SET_LOCALS_INT(*(jchar *) result, 0);
      break;

    case T_BYTE:
#ifndef VM_LITTLE_ENDIAN
      result[0] <<= (BitsPerWord - BitsPerByte);
#endif
      SET_LOCALS_INT(*(jbyte *) result, 0);
      break;

    case T_SHORT:
#ifndef VM_LITTLE_ENDIAN
      result[0] <<= (BitsPerWord - BitsPerShort);
#endif
      SET_LOCALS_INT(*(jshort *) result, 0);
      break;

    case T_INT:
#ifndef VM_LITTLE_ENDIAN
      result[0] <<= (BitsPerWord - BitsPerInt);
#endif
      SET_LOCALS_INT(*(jint *) result, 0);
      break;

    case T_LONG:
      SET_LOCALS_LONG(*(jlong *) result, 0);
      break;

    case T_FLOAT:
      SET_LOCALS_FLOAT(*(jfloat *) result, 0);
      break;

    case T_DOUBLE:
      SET_LOCALS_DOUBLE(*(jdouble *) result, 0);
      break;

    case T_OBJECT:
    case T_ARRAY:
      SET_LOCALS_OBJECT(istate->oop_temp(), 0);
      break;

    default:
      ShouldNotReachHere();
    }
  }
T
twisti 已提交
483 484 485

  // No deoptimized frames on the stack
  return 0;
486 487
}

T
twisti 已提交
488
int CppInterpreter::accessor_entry(methodOop method, intptr_t UNUSED, TRAPS) {
489 490 491 492 493 494
  JavaThread *thread = (JavaThread *) THREAD;
  ZeroStack *stack = thread->zero_stack();
  intptr_t *locals = stack->sp();

  // Drop into the slow path if we need a safepoint check
  if (SafepointSynchronize::do_call_back()) {
T
twisti 已提交
495
    return normal_entry(method, 0, THREAD);
496 497 498 499 500 501
  }

  // Load the object pointer and drop into the slow path
  // if we have a NullPointerException
  oop object = LOCALS_OBJECT(0);
  if (object == NULL) {
T
twisti 已提交
502
    return normal_entry(method, 0, THREAD);
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
  }

  // Read the field index from the bytecode, which looks like this:
  //  0:  aload_0
  //  1:  getfield
  //  2:    index
  //  3:    index
  //  4:  ireturn/areturn
  // NB this is not raw bytecode: index is in machine order
  u1 *code = method->code_base();
  assert(code[0] == Bytecodes::_aload_0 &&
         code[1] == Bytecodes::_getfield &&
         (code[4] == Bytecodes::_ireturn ||
          code[4] == Bytecodes::_areturn), "should do");
  u2 index = Bytes::get_native_u2(&code[2]);

  // Get the entry from the constant pool cache, and drop into
  // the slow path if it has not been resolved
  constantPoolCacheOop cache = method->constants()->cache();
  ConstantPoolCacheEntry* entry = cache->entry_at(index);
  if (!entry->is_resolved(Bytecodes::_getfield)) {
T
twisti 已提交
524
    return normal_entry(method, 0, THREAD);
525 526 527 528 529 530
  }

  // Get the result and push it onto the stack
  switch (entry->flag_state()) {
  case ltos:
  case dtos:
T
twisti 已提交
531
    stack->overflow_check(1, CHECK_0);
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
    stack->alloc(wordSize);
    break;
  }
  if (entry->is_volatile()) {
    switch (entry->flag_state()) {
    case ctos:
      SET_LOCALS_INT(object->char_field_acquire(entry->f2()), 0);
      break;

    case btos:
      SET_LOCALS_INT(object->byte_field_acquire(entry->f2()), 0);
      break;

    case stos:
      SET_LOCALS_INT(object->short_field_acquire(entry->f2()), 0);
      break;

    case itos:
      SET_LOCALS_INT(object->int_field_acquire(entry->f2()), 0);
      break;

    case ltos:
      SET_LOCALS_LONG(object->long_field_acquire(entry->f2()), 0);
      break;

    case ftos:
      SET_LOCALS_FLOAT(object->float_field_acquire(entry->f2()), 0);
      break;

    case dtos:
      SET_LOCALS_DOUBLE(object->double_field_acquire(entry->f2()), 0);
      break;

    case atos:
      SET_LOCALS_OBJECT(object->obj_field_acquire(entry->f2()), 0);
      break;

    default:
      ShouldNotReachHere();
    }
  }
  else {
    switch (entry->flag_state()) {
    case ctos:
      SET_LOCALS_INT(object->char_field(entry->f2()), 0);
      break;

    case btos:
      SET_LOCALS_INT(object->byte_field(entry->f2()), 0);
      break;

    case stos:
      SET_LOCALS_INT(object->short_field(entry->f2()), 0);
      break;

    case itos:
      SET_LOCALS_INT(object->int_field(entry->f2()), 0);
      break;

    case ltos:
      SET_LOCALS_LONG(object->long_field(entry->f2()), 0);
      break;

    case ftos:
      SET_LOCALS_FLOAT(object->float_field(entry->f2()), 0);
      break;

    case dtos:
      SET_LOCALS_DOUBLE(object->double_field(entry->f2()), 0);
      break;

    case atos:
      SET_LOCALS_OBJECT(object->obj_field(entry->f2()), 0);
      break;

    default:
      ShouldNotReachHere();
    }
  }
T
twisti 已提交
611 612 613

  // No deoptimized frames on the stack
  return 0;
614 615
}

T
twisti 已提交
616
int CppInterpreter::empty_entry(methodOop method, intptr_t UNUSED, TRAPS) {
617 618 619 620 621
  JavaThread *thread = (JavaThread *) THREAD;
  ZeroStack *stack = thread->zero_stack();

  // Drop into the slow path if we need a safepoint check
  if (SafepointSynchronize::do_call_back()) {
T
twisti 已提交
622
    return normal_entry(method, 0, THREAD);
623 624 625 626
  }

  // Pop our parameters
  stack->set_sp(stack->sp() + method->size_of_parameters());
T
twisti 已提交
627 628 629

  // No deoptimized frames on the stack
  return 0;
630 631
}

T
twisti 已提交
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 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 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 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 1028 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 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
int CppInterpreter::method_handle_entry(methodOop method,
                                        intptr_t UNUSED, TRAPS) {
  JavaThread *thread = (JavaThread *) THREAD;
  ZeroStack *stack = thread->zero_stack();
  int argument_slots = method->size_of_parameters();
  int result_slots = type2size[result_type_of(method)];
  intptr_t *vmslots = stack->sp();
  intptr_t *unwind_sp = vmslots + argument_slots;

  // Find the MethodType
  address p = (address) method;
  for (jint* pc = method->method_type_offsets_chain(); (*pc) != -1; pc++) {
    p = *(address*)(p + (*pc));
  }
  oop method_type = (oop) p;

  // The MethodHandle is in the slot after the arguments
  oop form = java_lang_invoke_MethodType::form(method_type);
  int num_vmslots = java_lang_invoke_MethodTypeForm::vmslots(form);
  assert(argument_slots == num_vmslots + 1, "should be");
  oop method_handle = VMSLOTS_OBJECT(num_vmslots);

  // InvokeGeneric requires some extra shuffling
  oop mhtype = java_lang_invoke_MethodHandle::type(method_handle);
  bool is_exact = mhtype == method_type;
  if (!is_exact) {
    if (method->intrinsic_id() == vmIntrinsics::_invokeExact) {
      CALL_VM_NOCHECK_NOFIX(
        InterpreterRuntime::throw_WrongMethodTypeException(
          thread, method_type, mhtype));
      // NB all oops trashed!
      assert(HAS_PENDING_EXCEPTION, "should do");
      stack->set_sp(unwind_sp);
      return 0;
    }
    assert(method->intrinsic_id() == vmIntrinsics::_invokeGeneric, "should be");

    // Load up an adapter from the calling type
    // NB the x86 code for this (in methodHandles_x86.cpp, search for
    // "genericInvoker") is really really odd.  I'm hoping it's trying
    // to accomodate odd VM/class library combinations I can ignore.
    oop adapter = java_lang_invoke_MethodTypeForm::genericInvoker(form);
    if (adapter == NULL) {
      CALL_VM_NOCHECK_NOFIX(
        InterpreterRuntime::throw_WrongMethodTypeException(
          thread, method_type, mhtype));
      // NB all oops trashed!
      assert(HAS_PENDING_EXCEPTION, "should do");
      stack->set_sp(unwind_sp);
      return 0;
    }

    // Adapters are shared among form-families of method-type.  The
    // type being called is passed as a trusted first argument so that
    // the adapter knows the actual types of its arguments and return
    // values.
    insert_vmslots(num_vmslots + 1, 1, THREAD);
    if (HAS_PENDING_EXCEPTION) {
      // NB all oops trashed!
      stack->set_sp(unwind_sp);
      return 0;
    }

    vmslots = stack->sp();
    num_vmslots++;
    SET_VMSLOTS_OBJECT(method_type, num_vmslots);

    method_handle = adapter;
  }

  // Start processing
  process_method_handle(method_handle, THREAD);
  if (HAS_PENDING_EXCEPTION)
    result_slots = 0;

  // If this is an invokeExact then the eventual callee will not
  // have unwound the method handle argument so we have to do it.
  // If a result is being returned the it will be above the method
  // handle argument we're unwinding.
  if (is_exact) {
    intptr_t result[2];
    for (int i = 0; i < result_slots; i++)
      result[i] = stack->pop();
    stack->pop();
    for (int i = result_slots - 1; i >= 0; i--)
      stack->push(result[i]);
  }

  // Check
  assert(stack->sp() == unwind_sp - result_slots, "should be");

  // No deoptimized frames on the stack
  return 0;
}

void CppInterpreter::process_method_handle(oop method_handle, TRAPS) {
  JavaThread *thread = (JavaThread *) THREAD;
  ZeroStack *stack = thread->zero_stack();
  intptr_t *vmslots = stack->sp();

  bool direct_to_method = false;
  BasicType src_rtype = T_ILLEGAL;
  BasicType dst_rtype = T_ILLEGAL;

  MethodHandleEntry *entry =
    java_lang_invoke_MethodHandle::vmentry(method_handle);
  MethodHandles::EntryKind entry_kind =
    (MethodHandles::EntryKind) (((intptr_t) entry) & 0xffffffff);

  methodOop method = NULL;
  switch (entry_kind) {
  case MethodHandles::_invokestatic_mh:
    direct_to_method = true;
    break;

  case MethodHandles::_invokespecial_mh:
  case MethodHandles::_invokevirtual_mh:
  case MethodHandles::_invokeinterface_mh:
    {
      oop receiver =
        VMSLOTS_OBJECT(
          java_lang_invoke_MethodHandle::vmslots(method_handle) - 1);
      if (receiver == NULL) {
          stack->set_sp(calculate_unwind_sp(stack, method_handle));
          CALL_VM_NOCHECK_NOFIX(
            throw_exception(
              thread, vmSymbols::java_lang_NullPointerException()));
          // NB all oops trashed!
          assert(HAS_PENDING_EXCEPTION, "should do");
          return;
      }
      if (entry_kind != MethodHandles::_invokespecial_mh) {
        int index = java_lang_invoke_DirectMethodHandle::vmindex(method_handle);
        instanceKlass* rcvrKlass =
          (instanceKlass *) receiver->klass()->klass_part();
        if (entry_kind == MethodHandles::_invokevirtual_mh) {
          method = (methodOop) rcvrKlass->start_of_vtable()[index];
        }
        else {
          oop iclass = java_lang_invoke_MethodHandle::vmtarget(method_handle);
          itableOffsetEntry* ki =
            (itableOffsetEntry *) rcvrKlass->start_of_itable();
          int i, length = rcvrKlass->itable_length();
          for (i = 0; i < length; i++, ki++ ) {
            if (ki->interface_klass() == iclass)
              break;
          }
          if (i == length) {
            stack->set_sp(calculate_unwind_sp(stack, method_handle));
            CALL_VM_NOCHECK_NOFIX(
              throw_exception(
                thread, vmSymbols::java_lang_IncompatibleClassChangeError()));
            // NB all oops trashed!
            assert(HAS_PENDING_EXCEPTION, "should do");
            return;
          }
          itableMethodEntry* im = ki->first_method_entry(receiver->klass());
          method = im[index].method();
          if (method == NULL) {
            stack->set_sp(calculate_unwind_sp(stack, method_handle));
            CALL_VM_NOCHECK_NOFIX(
              throw_exception(
                thread, vmSymbols::java_lang_AbstractMethodError()));
            // NB all oops trashed!
            assert(HAS_PENDING_EXCEPTION, "should do");
            return;
          }
        }
      }
    }
    direct_to_method = true;
    break;

  case MethodHandles::_bound_ref_direct_mh:
  case MethodHandles::_bound_int_direct_mh:
  case MethodHandles::_bound_long_direct_mh:
    direct_to_method = true;
    // fall through
  case MethodHandles::_bound_ref_mh:
  case MethodHandles::_bound_int_mh:
  case MethodHandles::_bound_long_mh:
    {
      BasicType arg_type  = T_ILLEGAL;
      int       arg_mask  = -1;
      int       arg_slots = -1;
      MethodHandles::get_ek_bound_mh_info(
        entry_kind, arg_type, arg_mask, arg_slots);
      int arg_slot =
        java_lang_invoke_BoundMethodHandle::vmargslot(method_handle);

      // Create the new slot(s)
      intptr_t *unwind_sp = calculate_unwind_sp(stack, method_handle);
      insert_vmslots(arg_slot, arg_slots, THREAD);
      if (HAS_PENDING_EXCEPTION) {
        // all oops trashed
        stack->set_sp(unwind_sp);
        return;
      }
      vmslots = stack->sp();

      // Store bound argument into new stack slot
      oop arg = java_lang_invoke_BoundMethodHandle::argument(method_handle);
      if (arg_type == T_OBJECT) {
        assert(arg_slots == 1, "should be");
        SET_VMSLOTS_OBJECT(arg, arg_slot);
      }
      else {
        jvalue arg_value;
        arg_type = java_lang_boxing_object::get_value(arg, &arg_value);
        switch (arg_type) {
        case T_BOOLEAN:
          SET_VMSLOTS_INT(arg_value.z, arg_slot);
          break;
        case T_CHAR:
          SET_VMSLOTS_INT(arg_value.c, arg_slot);
          break;
        case T_BYTE:
          SET_VMSLOTS_INT(arg_value.b, arg_slot);
          break;
        case T_SHORT:
          SET_VMSLOTS_INT(arg_value.s, arg_slot);
          break;
        case T_INT:
          SET_VMSLOTS_INT(arg_value.i, arg_slot);
          break;
        case T_FLOAT:
          SET_VMSLOTS_FLOAT(arg_value.f, arg_slot);
          break;
        case T_LONG:
          SET_VMSLOTS_LONG(arg_value.j, arg_slot + 1);
          break;
        case T_DOUBLE:
          SET_VMSLOTS_DOUBLE(arg_value.d, arg_slot + 1);
          break;
        default:
          tty->print_cr("unhandled type %s", type2name(arg_type));
          ShouldNotReachHere();
        }
      }
    }
    break;

  case MethodHandles::_adapter_retype_only:
  case MethodHandles::_adapter_retype_raw:
    src_rtype = result_type_of_handle(
      java_lang_invoke_MethodHandle::vmtarget(method_handle));
    dst_rtype = result_type_of_handle(method_handle);
    break;

  case MethodHandles::_adapter_check_cast:
    {
      int arg_slot =
        java_lang_invoke_AdapterMethodHandle::vmargslot(method_handle);
      oop arg = VMSLOTS_OBJECT(arg_slot);
      if (arg != NULL) {
        klassOop objKlassOop = arg->klass();
        klassOop klassOf = java_lang_Class::as_klassOop(
          java_lang_invoke_AdapterMethodHandle::argument(method_handle));

        if (objKlassOop != klassOf &&
            !objKlassOop->klass_part()->is_subtype_of(klassOf)) {
          ResourceMark rm(THREAD);
          const char* objName = Klass::cast(objKlassOop)->external_name();
          const char* klassName = Klass::cast(klassOf)->external_name();
          char* message = SharedRuntime::generate_class_cast_message(
            objName, klassName);

          stack->set_sp(calculate_unwind_sp(stack, method_handle));
          CALL_VM_NOCHECK_NOFIX(
            throw_exception(
              thread, vmSymbols::java_lang_ClassCastException(), message));
          // NB all oops trashed!
          assert(HAS_PENDING_EXCEPTION, "should do");
          return;
        }
      }
    }
    break;

  case MethodHandles::_adapter_dup_args:
    {
      int arg_slot =
        java_lang_invoke_AdapterMethodHandle::vmargslot(method_handle);
      int conv =
        java_lang_invoke_AdapterMethodHandle::conversion(method_handle);
      int num_slots = -MethodHandles::adapter_conversion_stack_move(conv);
      assert(num_slots > 0, "should be");

      // Create the new slot(s)
      intptr_t *unwind_sp = calculate_unwind_sp(stack, method_handle);
      stack->overflow_check(num_slots, THREAD);
      if (HAS_PENDING_EXCEPTION) {
        // all oops trashed
        stack->set_sp(unwind_sp);
        return;
      }

      // Duplicate the arguments
      for (int i = num_slots - 1; i >= 0; i--)
        stack->push(*VMSLOTS_SLOT(arg_slot + i));

      vmslots = stack->sp(); // unused, but let the compiler figure that out
    }
    break;

  case MethodHandles::_adapter_drop_args:
    {
      int arg_slot =
        java_lang_invoke_AdapterMethodHandle::vmargslot(method_handle);
      int conv =
        java_lang_invoke_AdapterMethodHandle::conversion(method_handle);
      int num_slots = MethodHandles::adapter_conversion_stack_move(conv);
      assert(num_slots > 0, "should be");

      remove_vmslots(arg_slot, num_slots, THREAD); // doesn't trap
      vmslots = stack->sp(); // unused, but let the compiler figure that out
    }
    break;

  case MethodHandles::_adapter_opt_swap_1:
  case MethodHandles::_adapter_opt_swap_2:
  case MethodHandles::_adapter_opt_rot_1_up:
  case MethodHandles::_adapter_opt_rot_1_down:
  case MethodHandles::_adapter_opt_rot_2_up:
  case MethodHandles::_adapter_opt_rot_2_down:
    {
      int arg1 =
        java_lang_invoke_AdapterMethodHandle::vmargslot(method_handle);
      int conv =
        java_lang_invoke_AdapterMethodHandle::conversion(method_handle);
      int arg2 = MethodHandles::adapter_conversion_vminfo(conv);

      int swap_bytes = 0, rotate = 0;
      MethodHandles::get_ek_adapter_opt_swap_rot_info(
        entry_kind, swap_bytes, rotate);
      int swap_slots = swap_bytes >> LogBytesPerWord;

      intptr_t tmp;
      switch (rotate) {
      case 0: // swap
        for (int i = 0; i < swap_slots; i++) {
          tmp = *VMSLOTS_SLOT(arg1 + i);
          SET_VMSLOTS_SLOT(VMSLOTS_SLOT(arg2 + i), arg1 + i);
          SET_VMSLOTS_SLOT(&tmp, arg2 + i);
        }
        break;

      case 1: // up
        assert(arg1 - swap_slots > arg2, "should be");

        tmp = *VMSLOTS_SLOT(arg1);
        for (int i = arg1 - swap_slots; i >= arg2; i--)
          SET_VMSLOTS_SLOT(VMSLOTS_SLOT(i), i + swap_slots);
        SET_VMSLOTS_SLOT(&tmp, arg2);

        break;

      case -1: // down
        assert(arg2 - swap_slots > arg1, "should be");

        tmp = *VMSLOTS_SLOT(arg1);
        for (int i = arg1 + swap_slots; i <= arg2; i++)
          SET_VMSLOTS_SLOT(VMSLOTS_SLOT(i), i - swap_slots);
        SET_VMSLOTS_SLOT(&tmp, arg2);
        break;

      default:
        ShouldNotReachHere();
      }
    }
    break;

  case MethodHandles::_adapter_opt_i2l:
    {
      int arg_slot =
        java_lang_invoke_AdapterMethodHandle::vmargslot(method_handle);
      int arg = VMSLOTS_INT(arg_slot);
      intptr_t *unwind_sp = calculate_unwind_sp(stack, method_handle);
      insert_vmslots(arg_slot, 1, THREAD);
      if (HAS_PENDING_EXCEPTION) {
        // all oops trashed
        stack->set_sp(unwind_sp);
        return;
      }
      vmslots = stack->sp();
      arg_slot++;
      SET_VMSLOTS_LONG(arg, arg_slot);
    }
    break;

  case MethodHandles::_adapter_opt_unboxi:
  case MethodHandles::_adapter_opt_unboxl:
    {
      int arg_slot =
        java_lang_invoke_AdapterMethodHandle::vmargslot(method_handle);
      oop arg = VMSLOTS_OBJECT(arg_slot);
      jvalue arg_value;
      BasicType arg_type = java_lang_boxing_object::get_value(arg, &arg_value);
      if (arg_type == T_LONG || arg_type == T_DOUBLE) {
        intptr_t *unwind_sp = calculate_unwind_sp(stack, method_handle);
        insert_vmslots(arg_slot, 1, THREAD);
        if (HAS_PENDING_EXCEPTION) {
          // all oops trashed
          stack->set_sp(unwind_sp);
          return;
        }
        vmslots = stack->sp();
        arg_slot++;
      }
      switch (arg_type) {
      case T_BOOLEAN:
        SET_VMSLOTS_INT(arg_value.z, arg_slot);
        break;
      case T_CHAR:
        SET_VMSLOTS_INT(arg_value.c, arg_slot);
        break;
      case T_BYTE:
        SET_VMSLOTS_INT(arg_value.b, arg_slot);
        break;
      case T_SHORT:
        SET_VMSLOTS_INT(arg_value.s, arg_slot);
        break;
      case T_INT:
        SET_VMSLOTS_INT(arg_value.i, arg_slot);
        break;
      case T_FLOAT:
        SET_VMSLOTS_FLOAT(arg_value.f, arg_slot);
        break;
      case T_LONG:
        SET_VMSLOTS_LONG(arg_value.j, arg_slot);
        break;
      case T_DOUBLE:
        SET_VMSLOTS_DOUBLE(arg_value.d, arg_slot);
        break;
      default:
        tty->print_cr("unhandled type %s", type2name(arg_type));
        ShouldNotReachHere();
      }
    }
    break;

  default:
    tty->print_cr("unhandled entry_kind %s",
                  MethodHandles::entry_name(entry_kind));
    ShouldNotReachHere();
  }

  // Continue along the chain
  if (direct_to_method) {
    if (method == NULL) {
      method =
        (methodOop) java_lang_invoke_MethodHandle::vmtarget(method_handle);
    }
    address entry_point = method->from_interpreted_entry();
    Interpreter::invoke_method(method, entry_point, THREAD);
  }
  else {
    process_method_handle(
      java_lang_invoke_MethodHandle::vmtarget(method_handle), THREAD);
  }
  // NB all oops now trashed

  // Adapt the result type, if necessary
  if (src_rtype != dst_rtype && !HAS_PENDING_EXCEPTION) {
    switch (dst_rtype) {
    case T_VOID:
      for (int i = 0; i < type2size[src_rtype]; i++)
        stack->pop();
      return;

    case T_INT:
      switch (src_rtype) {
      case T_VOID:
        stack->overflow_check(1, CHECK);
        stack->push(0);
        return;

      case T_BOOLEAN:
      case T_CHAR:
      case T_BYTE:
      case T_SHORT:
        return;
      }
    }

    tty->print_cr("unhandled conversion:");
    tty->print_cr("src_rtype = %s", type2name(src_rtype));
    tty->print_cr("dst_rtype = %s", type2name(dst_rtype));
    ShouldNotReachHere();
  }
}

// The new slots will be inserted before slot insert_before.
// Slots < insert_before will have the same slot number after the insert.
// Slots >= insert_before will become old_slot + num_slots.
void CppInterpreter::insert_vmslots(int insert_before, int num_slots, TRAPS) {
  JavaThread *thread = (JavaThread *) THREAD;
  ZeroStack *stack = thread->zero_stack();

  // Allocate the space
  stack->overflow_check(num_slots, CHECK);
  stack->alloc(num_slots * wordSize);
  intptr_t *vmslots = stack->sp();

  // Shuffle everything up
  for (int i = 0; i < insert_before; i++)
    SET_VMSLOTS_SLOT(VMSLOTS_SLOT(i + num_slots), i);
}

void CppInterpreter::remove_vmslots(int first_slot, int num_slots, TRAPS) {
  JavaThread *thread = (JavaThread *) THREAD;
  ZeroStack *stack = thread->zero_stack();
  intptr_t *vmslots = stack->sp();

  // Move everything down
  for (int i = first_slot - 1; i >= 0; i--)
    SET_VMSLOTS_SLOT(VMSLOTS_SLOT(i), i + num_slots);

  // Deallocate the space
  stack->set_sp(stack->sp() + num_slots);
}

BasicType CppInterpreter::result_type_of_handle(oop method_handle) {
  oop method_type = java_lang_invoke_MethodHandle::type(method_handle);
  oop return_type = java_lang_invoke_MethodType::rtype(method_type);
  return java_lang_Class::as_BasicType(return_type, (klassOop *) NULL);
}

intptr_t* CppInterpreter::calculate_unwind_sp(ZeroStack* stack,
                                              oop method_handle) {
  oop method_type = java_lang_invoke_MethodHandle::type(method_handle);
  oop form = java_lang_invoke_MethodType::form(method_type);
  int argument_slots = java_lang_invoke_MethodTypeForm::vmslots(form);

  return stack->sp() + argument_slots;
}

IRT_ENTRY(void, CppInterpreter::throw_exception(JavaThread* thread,
                                                Symbol*     name,
                                                char*       message))
  THROW_MSG(name, message);
IRT_END

1175 1176 1177
InterpreterFrame *InterpreterFrame::build(const methodOop method, TRAPS) {
  JavaThread *thread = (JavaThread *) THREAD;
  ZeroStack *stack = thread->zero_stack();
1178

1179 1180 1181 1182 1183
  // Calculate the size of the frame we'll build, including
  // any adjustments to the caller's frame that we'll make.
  int extra_locals  = 0;
  int monitor_words = 0;
  int stack_words   = 0;
1184

1185 1186 1187
  if (!method->is_native()) {
    extra_locals = method->max_locals() - method->size_of_parameters();
    stack_words  = method->max_stack();
1188
  }
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
  if (method->is_synchronized()) {
    monitor_words = frame::interpreter_frame_monitor_size();
  }
  stack->overflow_check(
    extra_locals + header_words + monitor_words + stack_words, CHECK_NULL);

  // Adjust the caller's stack frame to accomodate any additional
  // local variables we have contiguously with our parameters.
  for (int i = 0; i < extra_locals; i++)
    stack->push(0);
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234

  intptr_t *locals;
  if (method->is_native())
    locals = stack->sp() + (method->size_of_parameters() - 1);
  else
    locals = stack->sp() + (method->max_locals() - 1);

  stack->push(0); // next_frame, filled in later
  intptr_t *fp = stack->sp();
  assert(fp - stack->sp() == next_frame_off, "should be");

  stack->push(INTERPRETER_FRAME);
  assert(fp - stack->sp() == frame_type_off, "should be");

  interpreterState istate =
    (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
  assert(fp - stack->sp() == istate_off, "should be");

  istate->set_locals(locals);
  istate->set_method(method);
  istate->set_self_link(istate);
  istate->set_prev_link(NULL);
  istate->set_thread(thread);
  istate->set_bcp(method->is_native() ? NULL : method->code_base());
  istate->set_constants(method->constants()->cache());
  istate->set_msg(BytecodeInterpreter::method_entry);
  istate->set_oop_temp(NULL);
  istate->set_mdx(NULL);
  istate->set_callee(NULL);

  istate->set_monitor_base((BasicObjectLock *) stack->sp());
  if (method->is_synchronized()) {
    BasicObjectLock *monitor =
      (BasicObjectLock *) stack->alloc(monitor_words * wordSize);
    oop object;
    if (method->is_static())
1235
      object = method->constants()->pool_holder()->java_mirror();
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
    else
      object = (oop) locals[0];
    monitor->set_obj(object);
  }

  istate->set_stack_base(stack->sp());
  istate->set_stack(stack->sp() - 1);
  if (stack_words)
    stack->alloc(stack_words * wordSize);
  istate->set_stack_limit(stack->sp() - 1);

  return (InterpreterFrame *) fp;
}

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_LONG   : i = 5; break;
    case T_VOID   : i = 6; break;
    case T_FLOAT  : i = 7; break;
    case T_DOUBLE : i = 8; break;
    case T_OBJECT : i = 9; break;
    case T_ARRAY  : i = 9; break;
    default       : ShouldNotReachHere();
  }
  assert(0 <= i && i < AbstractInterpreter::number_of_result_handlers,
         "index out of bounds");
  return i;
}

K
kvn 已提交
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
BasicType CppInterpreter::result_type_of(methodOop method) {
  BasicType t;
  switch (method->result_index()) {
    case 0 : t = T_BOOLEAN; break;
    case 1 : t = T_CHAR;    break;
    case 2 : t = T_BYTE;    break;
    case 3 : t = T_SHORT;   break;
    case 4 : t = T_INT;     break;
    case 5 : t = T_LONG;    break;
    case 6 : t = T_VOID;    break;
    case 7 : t = T_FLOAT;   break;
    case 8 : t = T_DOUBLE;  break;
    case 9 : t = T_OBJECT;  break;
    default: ShouldNotReachHere();
  }
  assert(AbstractInterpreter::BasicType_as_index(t) == method->result_index(),
         "out of step with AbstractInterpreter::BasicType_as_index");
  return t;
}

1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
address InterpreterGenerator::generate_empty_entry() {
  if (!UseFastEmptyMethods)
    return NULL;

  return generate_entry((address) CppInterpreter::empty_entry);
}

address InterpreterGenerator::generate_accessor_entry() {
  if (!UseFastAccessorMethods)
    return NULL;

  return generate_entry((address) CppInterpreter::accessor_entry);
}

1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
address InterpreterGenerator::generate_Reference_get_entry(void) {
#ifndef SERIALGC
  if (UseG1GC) {
    // We need to generate have a routine that generates code to:
    //   * load the value in the referent field
    //   * passes that value to the pre-barrier.
    //
    // In the case of G1 this will record the value of the
    // referent in an SATB buffer if marking is active.
    // This will cause concurrent marking to mark the referent
    // field as live.
    Unimplemented();
  }
#endif // SERIALGC

  // If G1 is not enabled then attempt to go through the accessor entry point
  // Reference.get is an accessor
  return generate_accessor_entry();
}

1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
address InterpreterGenerator::generate_native_entry(bool synchronized) {
  assert(synchronized == false, "should be");

  return generate_entry((address) CppInterpreter::native_entry);
}

address InterpreterGenerator::generate_normal_entry(bool synchronized) {
  assert(synchronized == false, "should be");

  return generate_entry((address) CppInterpreter::normal_entry);
}

address AbstractInterpreterGenerator::generate_method_entry(
    AbstractInterpreter::MethodKind kind) {
  address entry_point = NULL;

  switch (kind) {
  case Interpreter::zerolocals:
  case Interpreter::zerolocals_synchronized:
    break;

  case Interpreter::native:
    entry_point = ((InterpreterGenerator*) this)->generate_native_entry(false);
    break;

  case Interpreter::native_synchronized:
    entry_point = ((InterpreterGenerator*) this)->generate_native_entry(false);
    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::method_handle:
    entry_point = ((InterpreterGenerator*) this)->generate_method_handle_entry();
    break;

  case Interpreter::java_lang_math_sin:
  case Interpreter::java_lang_math_cos:
  case Interpreter::java_lang_math_tan:
  case Interpreter::java_lang_math_abs:
  case Interpreter::java_lang_math_log:
  case Interpreter::java_lang_math_log10:
  case Interpreter::java_lang_math_sqrt:
    entry_point = ((InterpreterGenerator*) this)->generate_math_entry(kind);
    break;

1380 1381 1382 1383
  case Interpreter::java_lang_ref_reference_get:
    entry_point = ((InterpreterGenerator*)this)->generate_Reference_get_entry();
    break;

1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
  default:
    ShouldNotReachHere();
  }

  if (entry_point == NULL)
    entry_point = ((InterpreterGenerator*) this)->generate_normal_entry(false);

  return entry_point;
}

InterpreterGenerator::InterpreterGenerator(StubQueue* code)
 : CppInterpreterGenerator(code) {
   generate_all();
}

// Deoptimization helpers

1401 1402 1403
InterpreterFrame *InterpreterFrame::build(int size, TRAPS) {
  ZeroStack *stack = ((JavaThread *) THREAD)->zero_stack();

1404 1405 1406
  int size_in_words = size >> LogBytesPerWord;
  assert(size_in_words * wordSize == size, "unaligned");
  assert(size_in_words >= header_words, "too small");
1407
  stack->overflow_check(size_in_words, CHECK_NULL);
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429

  stack->push(0); // next_frame, filled in later
  intptr_t *fp = stack->sp();
  assert(fp - stack->sp() == next_frame_off, "should be");

  stack->push(INTERPRETER_FRAME);
  assert(fp - stack->sp() == frame_type_off, "should be");

  interpreterState istate =
    (interpreterState) stack->alloc(sizeof(BytecodeInterpreter));
  assert(fp - stack->sp() == istate_off, "should be");
  istate->set_self_link(NULL); // mark invalid

  stack->alloc((size_in_words - header_words) * wordSize);

  return (InterpreterFrame *) fp;
}

int AbstractInterpreter::layout_activation(methodOop method,
                                           int       tempcount,
                                           int       popframe_extra_args,
                                           int       moncount,
1430
                                           int       caller_actual_parameters,
1431 1432 1433 1434 1435 1436 1437
                                           int       callee_param_count,
                                           int       callee_locals,
                                           frame*    caller,
                                           frame*    interpreter_frame,
                                           bool      is_top_frame) {
  assert(popframe_extra_args == 0, "what to do?");
  assert(!is_top_frame || (!callee_locals && !callee_param_count),
T
twisti 已提交
1438
         "top frame should have no caller");
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458

  // This code must exactly match what InterpreterFrame::build
  // does (the full InterpreterFrame::build, that is, not the
  // one that creates empty frames for the deoptimizer).
  //
  // If interpreter_frame is not NULL then it will be filled in.
  // It's size is determined by a previous call to this method,
  // so it should be correct.
  //
  // Note that tempcount is the current size of the expression
  // stack.  For top most frames we will allocate a full sized
  // expression stack and not the trimmed version that non-top
  // frames have.

  int header_words        = InterpreterFrame::header_words;
  int monitor_words       = moncount * frame::interpreter_frame_monitor_size();
  int stack_words         = is_top_frame ? method->max_stack() : tempcount;
  int callee_extra_locals = callee_locals - callee_param_count;

  if (interpreter_frame) {
T
twisti 已提交
1459
    intptr_t *locals        = interpreter_frame->fp() + method->max_locals();
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 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565
    interpreterState istate = interpreter_frame->get_interpreterState();
    intptr_t *monitor_base  = (intptr_t*) istate;
    intptr_t *stack_base    = monitor_base - monitor_words;
    intptr_t *stack         = stack_base - tempcount - 1;

    BytecodeInterpreter::layout_interpreterState(istate,
                                                 caller,
                                                 NULL,
                                                 method,
                                                 locals,
                                                 stack,
                                                 stack_base,
                                                 monitor_base,
                                                 NULL,
                                                 is_top_frame);
  }
  return header_words + monitor_words + stack_words + callee_extra_locals;
}

void BytecodeInterpreter::layout_interpreterState(interpreterState istate,
                                                  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) {
  istate->set_locals(locals);
  istate->set_method(method);
  istate->set_self_link(istate);
  istate->set_prev_link(NULL);
  // thread will be set by a hacky repurposing of frame::patch_pc()
  // bcp will be set by vframeArrayElement::unpack_on_stack()
  istate->set_constants(method->constants()->cache());
  istate->set_msg(BytecodeInterpreter::method_resume);
  istate->set_bcp_advance(0);
  istate->set_oop_temp(NULL);
  istate->set_mdx(NULL);
  if (caller->is_interpreted_frame()) {
    interpreterState prev = caller->get_interpreterState();
    prev->set_callee(method);
    if (*prev->bcp() == Bytecodes::_invokeinterface)
      prev->set_bcp_advance(5);
    else
      prev->set_bcp_advance(3);
  }
  istate->set_callee(NULL);
  istate->set_monitor_base((BasicObjectLock *) monitor_base);
  istate->set_stack_base(stack_base);
  istate->set_stack(stack);
  istate->set_stack_limit(stack_base - method->max_stack() - 1);
}

address CppInterpreter::return_entry(TosState state, int length) {
  ShouldNotCallThis();
}

address CppInterpreter::deopt_entry(TosState state, int length) {
  return NULL;
}

// Helper for (runtime) stack overflow checks

int AbstractInterpreter::size_top_interpreter_activation(methodOop method) {
  return 0;
}

// Helper for figuring out if frames are interpreter frames

bool CppInterpreter::contains(address pc) {
#ifdef PRODUCT
  ShouldNotCallThis();
#else
  return false; // make frame::print_value_on work
#endif // !PRODUCT
}

// Result handlers and convertors

address CppInterpreterGenerator::generate_result_handler_for(
    BasicType type) {
  assembler()->advance(1);
  return ShouldNotCallThisStub();
}

address CppInterpreterGenerator::generate_tosca_to_stack_converter(
    BasicType type) {
  assembler()->advance(1);
  return ShouldNotCallThisStub();
}

address CppInterpreterGenerator::generate_stack_to_stack_converter(
    BasicType type) {
  assembler()->advance(1);
  return ShouldNotCallThisStub();
}

address CppInterpreterGenerator::generate_stack_to_native_abi_converter(
    BasicType type) {
  assembler()->advance(1);
  return ShouldNotCallThisStub();
}

#endif // CC_INTERP