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