1612N/A/*
1879N/A * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
1612N/A * Copyright 2008, 2009, 2010 Red Hat, Inc.
1612N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1612N/A *
1612N/A * This code is free software; you can redistribute it and/or modify it
1612N/A * under the terms of the GNU General Public License version 2 only, as
1612N/A * published by the Free Software Foundation.
1612N/A *
1612N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1612N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1612N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1612N/A * version 2 for more details (a copy is included in the LICENSE file that
1612N/A * accompanied this code).
1612N/A *
1612N/A * You should have received a copy of the GNU General Public License version
1612N/A * 2 along with this work; if not, write to the Free Software Foundation,
1612N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1612N/A *
1612N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1612N/A * or visit www.oracle.com if you need additional information or have any
1612N/A * questions.
1612N/A *
1612N/A */
1612N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "oops/klassOop.hpp"
1879N/A#include "runtime/biasedLocking.hpp"
1879N/A#include "runtime/deoptimization.hpp"
1879N/A#include "runtime/thread.hpp"
1879N/A#include "shark/llvmHeaders.hpp"
1879N/A#include "shark/sharkRuntime.hpp"
1879N/A#ifdef TARGET_ARCH_zero
1879N/A# include "stack_zero.inline.hpp"
1879N/A#endif
1612N/A
1612N/Ausing namespace llvm;
1612N/A
1612N/AJRT_ENTRY(int, SharkRuntime::find_exception_handler(JavaThread* thread,
1612N/A int* indexes,
1612N/A int num_indexes))
1612N/A constantPoolHandle pool(thread, method(thread)->constants());
1612N/A KlassHandle exc_klass(thread, ((oop) tos_at(thread, 0))->klass());
1612N/A
1612N/A for (int i = 0; i < num_indexes; i++) {
1612N/A klassOop tmp = pool->klass_at(indexes[i], CHECK_0);
1612N/A KlassHandle chk_klass(thread, tmp);
1612N/A
1612N/A if (exc_klass() == chk_klass())
1612N/A return i;
1612N/A
1612N/A if (exc_klass()->klass_part()->is_subtype_of(chk_klass()))
1612N/A return i;
1612N/A }
1612N/A
1612N/A return -1;
1612N/AJRT_END
1612N/A
1612N/AJRT_ENTRY(void, SharkRuntime::monitorenter(JavaThread* thread,
1612N/A BasicObjectLock* lock))
1612N/A if (PrintBiasedLockingStatistics)
1612N/A Atomic::inc(BiasedLocking::slow_path_entry_count_addr());
1612N/A
1612N/A Handle object(thread, lock->obj());
1612N/A assert(Universe::heap()->is_in_reserved_or_null(object()), "should be");
1612N/A if (UseBiasedLocking) {
1612N/A // Retry fast entry if bias is revoked to avoid unnecessary inflation
1612N/A ObjectSynchronizer::fast_enter(object, lock->lock(), true, CHECK);
1612N/A } else {
1612N/A ObjectSynchronizer::slow_enter(object, lock->lock(), CHECK);
1612N/A }
1612N/A assert(Universe::heap()->is_in_reserved_or_null(lock->obj()), "should be");
1612N/AJRT_END
1612N/A
1612N/AJRT_ENTRY(void, SharkRuntime::monitorexit(JavaThread* thread,
1612N/A BasicObjectLock* lock))
1612N/A Handle object(thread, lock->obj());
1612N/A assert(Universe::heap()->is_in_reserved_or_null(object()), "should be");
1612N/A if (lock == NULL || object()->is_unlocked()) {
1612N/A THROW(vmSymbols::java_lang_IllegalMonitorStateException());
1612N/A }
1612N/A ObjectSynchronizer::slow_exit(object(), lock->lock(), thread);
1612N/AJRT_END
1612N/A
1612N/AJRT_ENTRY(void, SharkRuntime::new_instance(JavaThread* thread, int index))
1612N/A klassOop k_oop = method(thread)->constants()->klass_at(index, CHECK);
1612N/A instanceKlassHandle klass(THREAD, k_oop);
1612N/A
1612N/A // Make sure we are not instantiating an abstract klass
1612N/A klass->check_valid_for_instantiation(true, CHECK);
1612N/A
1612N/A // Make sure klass is initialized
1612N/A klass->initialize(CHECK);
1612N/A
1612N/A // At this point the class may not be fully initialized
1612N/A // because of recursive initialization. If it is fully
1612N/A // initialized & has_finalized is not set, we rewrite
1612N/A // it into its fast version (Note: no locking is needed
1612N/A // here since this is an atomic byte write and can be
1612N/A // done more than once).
1612N/A //
1612N/A // Note: In case of classes with has_finalized we don't
1612N/A // rewrite since that saves us an extra check in
1612N/A // the fast version which then would call the
1612N/A // slow version anyway (and do a call back into
1612N/A // Java).
1612N/A // If we have a breakpoint, then we don't rewrite
1612N/A // because the _breakpoint bytecode would be lost.
1612N/A oop obj = klass->allocate_instance(CHECK);
1612N/A thread->set_vm_result(obj);
1612N/AJRT_END
1612N/A
1612N/AJRT_ENTRY(void, SharkRuntime::newarray(JavaThread* thread,
1612N/A BasicType type,
1612N/A int size))
1612N/A oop obj = oopFactory::new_typeArray(type, size, CHECK);
1612N/A thread->set_vm_result(obj);
1612N/AJRT_END
1612N/A
1612N/AJRT_ENTRY(void, SharkRuntime::anewarray(JavaThread* thread,
1612N/A int index,
1612N/A int size))
1612N/A klassOop klass = method(thread)->constants()->klass_at(index, CHECK);
1612N/A objArrayOop obj = oopFactory::new_objArray(klass, size, CHECK);
1612N/A thread->set_vm_result(obj);
1612N/AJRT_END
1612N/A
1612N/AJRT_ENTRY(void, SharkRuntime::multianewarray(JavaThread* thread,
1612N/A int index,
1612N/A int ndims,
1612N/A int* dims))
1612N/A klassOop klass = method(thread)->constants()->klass_at(index, CHECK);
1612N/A oop obj = arrayKlass::cast(klass)->multi_allocate(ndims, dims, CHECK);
1612N/A thread->set_vm_result(obj);
1612N/AJRT_END
1612N/A
1612N/AJRT_ENTRY(void, SharkRuntime::register_finalizer(JavaThread* thread,
1612N/A oop object))
1612N/A assert(object->is_oop(), "should be");
1612N/A assert(object->klass()->klass_part()->has_finalizer(), "should have");
1612N/A instanceKlass::register_finalizer(instanceOop(object), CHECK);
1612N/AJRT_END
1612N/A
1612N/AJRT_ENTRY(void, SharkRuntime::throw_ArithmeticException(JavaThread* thread,
1612N/A const char* file,
1612N/A int line))
1612N/A Exceptions::_throw_msg(
1612N/A thread, file, line,
1612N/A vmSymbols::java_lang_ArithmeticException(),
1612N/A "");
1612N/AJRT_END
1612N/A
1612N/AJRT_ENTRY(void, SharkRuntime::throw_ArrayIndexOutOfBoundsException(
1612N/A JavaThread* thread,
1612N/A const char* file,
1612N/A int line,
1612N/A int index))
1612N/A char msg[jintAsStringSize];
1612N/A snprintf(msg, sizeof(msg), "%d", index);
1612N/A Exceptions::_throw_msg(
1612N/A thread, file, line,
1612N/A vmSymbols::java_lang_ArrayIndexOutOfBoundsException(),
1612N/A msg);
1612N/AJRT_END
1612N/A
1612N/AJRT_ENTRY(void, SharkRuntime::throw_ClassCastException(JavaThread* thread,
1612N/A const char* file,
1612N/A int line))
1612N/A Exceptions::_throw_msg(
1612N/A thread, file, line,
1612N/A vmSymbols::java_lang_ClassCastException(),
1612N/A "");
1612N/AJRT_END
1612N/A
1612N/AJRT_ENTRY(void, SharkRuntime::throw_NullPointerException(JavaThread* thread,
1612N/A const char* file,
1612N/A int line))
1612N/A Exceptions::_throw_msg(
1612N/A thread, file, line,
1612N/A vmSymbols::java_lang_NullPointerException(),
1612N/A "");
1612N/AJRT_END
1612N/A
1612N/A// Non-VM calls
1612N/A// Nothing in these must ever GC!
1612N/A
1612N/Avoid SharkRuntime::dump(const char *name, intptr_t value) {
1612N/A oop valueOop = (oop) value;
1612N/A tty->print("%s = ", name);
1612N/A if (valueOop->is_oop(true))
1612N/A valueOop->print_on(tty);
1612N/A else if (value >= ' ' && value <= '~')
1612N/A tty->print("'%c' (%d)", value, value);
1612N/A else
1612N/A tty->print("%p", value);
1612N/A tty->print_cr("");
1612N/A}
1612N/A
1612N/Abool SharkRuntime::is_subtype_of(klassOop check_klass, klassOop object_klass) {
1612N/A return object_klass->klass_part()->is_subtype_of(check_klass);
1612N/A}
1612N/A
1612N/Aint SharkRuntime::uncommon_trap(JavaThread* thread, int trap_request) {
1612N/A Thread *THREAD = thread;
1612N/A
1612N/A // In C2, uncommon_trap_blob creates a frame, so all the various
1612N/A // deoptimization functions expect to find the frame of the method
1612N/A // being deopted one frame down on the stack. We create a dummy
1612N/A // frame to mirror this.
1612N/A FakeStubFrame *stubframe = FakeStubFrame::build(CHECK_0);
1612N/A thread->push_zero_frame(stubframe);
1612N/A
1612N/A // Initiate the trap
1612N/A thread->set_last_Java_frame();
1612N/A Deoptimization::UnrollBlock *urb =
1612N/A Deoptimization::uncommon_trap(thread, trap_request);
1612N/A thread->reset_last_Java_frame();
1612N/A
1612N/A // Pop our dummy frame and the frame being deoptimized
1612N/A thread->pop_zero_frame();
1612N/A thread->pop_zero_frame();
1612N/A
1612N/A // Push skeleton frames
1612N/A int number_of_frames = urb->number_of_frames();
1612N/A for (int i = 0; i < number_of_frames; i++) {
1612N/A intptr_t size = urb->frame_sizes()[i];
1612N/A InterpreterFrame *frame = InterpreterFrame::build(size, CHECK_0);
1612N/A thread->push_zero_frame(frame);
1612N/A }
1612N/A
1612N/A // Push another dummy frame
1612N/A stubframe = FakeStubFrame::build(CHECK_0);
1612N/A thread->push_zero_frame(stubframe);
1612N/A
1612N/A // Fill in the skeleton frames
1612N/A thread->set_last_Java_frame();
1612N/A Deoptimization::unpack_frames(thread, Deoptimization::Unpack_uncommon_trap);
1612N/A thread->reset_last_Java_frame();
1612N/A
1612N/A // Pop our dummy frame
1612N/A thread->pop_zero_frame();
1612N/A
1612N/A // Fall back into the interpreter
1612N/A return number_of_frames;
1612N/A}
1612N/A
1612N/AFakeStubFrame* FakeStubFrame::build(TRAPS) {
1612N/A ZeroStack *stack = ((JavaThread *) THREAD)->zero_stack();
1612N/A stack->overflow_check(header_words, CHECK_NULL);
1612N/A
1612N/A stack->push(0); // next_frame, filled in later
1612N/A intptr_t *fp = stack->sp();
1612N/A assert(fp - stack->sp() == next_frame_off, "should be");
1612N/A
1612N/A stack->push(FAKE_STUB_FRAME);
1612N/A assert(fp - stack->sp() == frame_type_off, "should be");
1612N/A
1612N/A return (FakeStubFrame *) fp;
1612N/A}