1 /* 2 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. 3 * Copyright 2008, 2009, 2010 Red Hat, Inc. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 * 24 */ 25 26 #include "precompiled.hpp" 27 #include "runtime/atomic.inline.hpp" 28 #include "runtime/biasedLocking.hpp" 29 #include "runtime/deoptimization.hpp" 30 #include "runtime/thread.hpp" 31 #include "shark/llvmHeaders.hpp" 32 #include "shark/sharkRuntime.hpp" 33 #ifdef TARGET_ARCH_zero 34 # include "stack_zero.inline.hpp" 35 #endif 36 37 using namespace llvm; 38 39 JRT_ENTRY(int, SharkRuntime::find_exception_handler(JavaThread* thread, 40 int* indexes, 41 int num_indexes)) 42 constantPoolHandle pool(thread, method(thread)->constants()); 43 KlassHandle exc_klass(thread, (oop((void*) tos_at(thread, 0)))->klass()); 44 45 for (int i = 0; i < num_indexes; i++) { 46 Klass* tmp = pool->klass_at(indexes[i], CHECK_0); 47 KlassHandle chk_klass(thread, tmp); 48 49 if (exc_klass() == chk_klass()) 50 return i; 51 52 if (exc_klass()->is_subtype_of(chk_klass())) 53 return i; 54 } 55 56 return -1; 57 JRT_END 58 59 JRT_ENTRY(void, SharkRuntime::monitorenter(JavaThread* thread, 60 BasicObjectLock* lock)) 61 if (PrintBiasedLockingStatistics) 62 Atomic::inc(BiasedLocking::slow_path_entry_count_addr()); 63 64 Handle object(thread, lock->obj()); 65 assert(Universe::heap()->is_in_reserved_or_null(object()), "should be"); 66 if (UseBiasedLocking) { 67 // Retry fast entry if bias is revoked to avoid unnecessary inflation 68 ObjectSynchronizer::fast_enter(object, lock->lock(), true, CHECK); 69 } else { 70 ObjectSynchronizer::slow_enter(object, lock->lock(), CHECK); 71 } 72 assert(Universe::heap()->is_in_reserved_or_null(lock->obj()), "should be"); 73 JRT_END 74 75 JRT_ENTRY(void, SharkRuntime::monitorexit(JavaThread* thread, 76 BasicObjectLock* lock)) 77 Handle object(thread, lock->obj()); 78 assert(Universe::heap()->is_in_reserved_or_null(object()), "should be"); 79 if (lock == NULL || object()->is_unlocked()) { 80 THROW(vmSymbols::java_lang_IllegalMonitorStateException()); 81 } 82 ObjectSynchronizer::slow_exit(object(), lock->lock(), thread); 83 JRT_END 84 85 JRT_ENTRY(void, SharkRuntime::new_instance(JavaThread* thread, int index)) 86 Klass* k_oop = method(thread)->constants()->klass_at(index, CHECK); 87 instanceKlassHandle klass(THREAD, k_oop); 88 89 // Make sure we are not instantiating an abstract klass 90 klass->check_valid_for_instantiation(true, CHECK); 91 92 // Make sure klass is initialized 93 klass->initialize(CHECK); 94 95 // At this point the class may not be fully initialized 96 // because of recursive initialization. If it is fully 97 // initialized & has_finalized is not set, we rewrite 98 // it into its fast version (Note: no locking is needed 99 // here since this is an atomic byte write and can be 100 // done more than once). 101 // 102 // Note: In case of classes with has_finalized we don't 103 // rewrite since that saves us an extra check in 104 // the fast version which then would call the 105 // slow version anyway (and do a call back into 106 // Java). 107 // If we have a breakpoint, then we don't rewrite 108 // because the _breakpoint bytecode would be lost. 109 oop obj = klass->allocate_instance(CHECK); 110 thread->set_vm_result(obj); 111 JRT_END 112 113 JRT_ENTRY(void, SharkRuntime::newarray(JavaThread* thread, 114 BasicType type, 115 int size)) 116 oop obj = oopFactory::new_typeArray(type, size, CHECK); 117 thread->set_vm_result(obj); 118 JRT_END 119 120 JRT_ENTRY(void, SharkRuntime::anewarray(JavaThread* thread, 121 int index, 122 int size)) 123 Klass* klass = method(thread)->constants()->klass_at(index, CHECK); 124 objArrayOop obj = oopFactory::new_objArray(klass, size, CHECK); 125 thread->set_vm_result(obj); 126 JRT_END 127 128 JRT_ENTRY(void, SharkRuntime::multianewarray(JavaThread* thread, 129 int index, 130 int ndims, 131 int* dims)) 132 Klass* klass = method(thread)->constants()->klass_at(index, CHECK); 133 oop obj = ArrayKlass::cast(klass)->multi_allocate(ndims, dims, CHECK); 134 thread->set_vm_result(obj); 135 JRT_END 136 137 JRT_ENTRY(void, SharkRuntime::register_finalizer(JavaThread* thread, 138 oop object)) 139 assert(object->is_oop(), "should be"); 140 assert(object->klass()->has_finalizer(), "should have"); 141 InstanceKlass::register_finalizer(instanceOop(object), CHECK); 142 JRT_END 143 144 JRT_ENTRY(void, SharkRuntime::throw_ArithmeticException(JavaThread* thread, 145 const char* file, 146 int line)) 147 Exceptions::_throw_msg( 148 thread, file, line, 149 vmSymbols::java_lang_ArithmeticException(), 150 ""); 151 JRT_END 152 153 JRT_ENTRY(void, SharkRuntime::throw_ArrayIndexOutOfBoundsException( 154 JavaThread* thread, 155 const char* file, 156 int line, 157 int index)) 158 char msg[jintAsStringSize]; 159 snprintf(msg, sizeof(msg), "%d", index); 160 Exceptions::_throw_msg( 161 thread, file, line, 162 vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), 163 msg); 164 JRT_END 165 166 JRT_ENTRY(void, SharkRuntime::throw_ClassCastException(JavaThread* thread, 167 const char* file, 168 int line)) 169 Exceptions::_throw_msg( 170 thread, file, line, 171 vmSymbols::java_lang_ClassCastException(), 172 ""); 173 JRT_END 174 175 JRT_ENTRY(void, SharkRuntime::throw_NullPointerException(JavaThread* thread, 176 const char* file, 177 int line)) 178 Exceptions::_throw_msg( 179 thread, file, line, 180 vmSymbols::java_lang_NullPointerException(), 181 ""); 182 JRT_END 183 184 // Non-VM calls 185 // Nothing in these must ever GC! 186 187 void SharkRuntime::dump(const char *name, intptr_t value) { 188 oop valueOop = oop((void*) value); 189 tty->print("%s = ", name); 190 if (valueOop->is_oop(true)) 191 valueOop->print_on(tty); 192 else if (value >= ' ' && value <= '~') 193 tty->print("'%c' (%d)", value, value); 194 else 195 tty->print("%p", value); 196 tty->print_cr(""); 197 } 198 199 bool SharkRuntime::is_subtype_of(Klass* check_klass, Klass* object_klass) { 200 return object_klass->is_subtype_of(check_klass); 201 } 202 203 int SharkRuntime::uncommon_trap(JavaThread* thread, int trap_request) { 204 Thread *THREAD = thread; 205 206 // In C2, uncommon_trap_blob creates a frame, so all the various 207 // deoptimization functions expect to find the frame of the method 208 // being deopted one frame down on the stack. We create a dummy 209 // frame to mirror this. 210 FakeStubFrame *stubframe = FakeStubFrame::build(CHECK_0); 211 thread->push_zero_frame(stubframe); 212 213 // Initiate the trap 214 thread->set_last_Java_frame(); 215 Deoptimization::UnrollBlock *urb = 216 Deoptimization::uncommon_trap(thread, trap_request); 217 thread->reset_last_Java_frame(); 218 219 // Pop our dummy frame and the frame being deoptimized 220 thread->pop_zero_frame(); 221 thread->pop_zero_frame(); 222 223 // Push skeleton frames 224 int number_of_frames = urb->number_of_frames(); 225 for (int i = 0; i < number_of_frames; i++) { 226 intptr_t size = urb->frame_sizes()[i]; 227 InterpreterFrame *frame = InterpreterFrame::build(size, CHECK_0); 228 thread->push_zero_frame(frame); 229 } 230 231 // Push another dummy frame 232 stubframe = FakeStubFrame::build(CHECK_0); 233 thread->push_zero_frame(stubframe); 234 235 // Fill in the skeleton frames 236 thread->set_last_Java_frame(); 237 Deoptimization::unpack_frames(thread, Deoptimization::Unpack_uncommon_trap); 238 thread->reset_last_Java_frame(); 239 240 // Pop our dummy frame 241 thread->pop_zero_frame(); 242 243 // Fall back into the interpreter 244 return number_of_frames; 245 } 246 247 FakeStubFrame* FakeStubFrame::build(TRAPS) { 248 ZeroStack *stack = ((JavaThread *) THREAD)->zero_stack(); 249 stack->overflow_check(header_words, CHECK_NULL); 250 251 stack->push(0); // next_frame, filled in later 252 intptr_t *fp = stack->sp(); 253 assert(fp - stack->sp() == next_frame_off, "should be"); 254 255 stack->push(FAKE_STUB_FRAME); 256 assert(fp - stack->sp() == frame_type_off, "should be"); 257 258 return (FakeStubFrame *) fp; 259 }