Wed, 09 Jun 2010 18:50:45 -0700
6939203: JSR 292 needs method handle constants
Summary: Add new CP types CONSTANT_MethodHandle, CONSTANT_MethodType; extend 'ldc' bytecode.
Reviewed-by: twisti, never
1 /*
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
25 #include "incls/_precompiled.incl"
26 #include "incls/_interpreterRuntime.cpp.incl"
28 class UnlockFlagSaver {
29 private:
30 JavaThread* _thread;
31 bool _do_not_unlock;
32 public:
33 UnlockFlagSaver(JavaThread* t) {
34 _thread = t;
35 _do_not_unlock = t->do_not_unlock_if_synchronized();
36 t->set_do_not_unlock_if_synchronized(false);
37 }
38 ~UnlockFlagSaver() {
39 _thread->set_do_not_unlock_if_synchronized(_do_not_unlock);
40 }
41 };
43 //------------------------------------------------------------------------------------------------------------------------
44 // State accessors
46 void InterpreterRuntime::set_bcp_and_mdp(address bcp, JavaThread *thread) {
47 last_frame(thread).interpreter_frame_set_bcp(bcp);
48 if (ProfileInterpreter) {
49 // ProfileTraps uses MDOs independently of ProfileInterpreter.
50 // That is why we must check both ProfileInterpreter and mdo != NULL.
51 methodDataOop mdo = last_frame(thread).interpreter_frame_method()->method_data();
52 if (mdo != NULL) {
53 NEEDS_CLEANUP;
54 last_frame(thread).interpreter_frame_set_mdp(mdo->bci_to_dp(last_frame(thread).interpreter_frame_bci()));
55 }
56 }
57 }
59 //------------------------------------------------------------------------------------------------------------------------
60 // Constants
63 IRT_ENTRY(void, InterpreterRuntime::ldc(JavaThread* thread, bool wide))
64 // access constant pool
65 constantPoolOop pool = method(thread)->constants();
66 int index = wide ? get_index_u2(thread, Bytecodes::_ldc_w) : get_index_u1(thread, Bytecodes::_ldc);
67 constantTag tag = pool->tag_at(index);
69 if (tag.is_unresolved_klass() || tag.is_klass()) {
70 klassOop klass = pool->klass_at(index, CHECK);
71 oop java_class = klass->klass_part()->java_mirror();
72 thread->set_vm_result(java_class);
73 } else {
74 #ifdef ASSERT
75 // If we entered this runtime routine, we believed the tag contained
76 // an unresolved string, an unresolved class or a resolved class.
77 // However, another thread could have resolved the unresolved string
78 // or class by the time we go there.
79 assert(tag.is_unresolved_string()|| tag.is_string(), "expected string");
80 #endif
81 oop s_oop = pool->string_at(index, CHECK);
82 thread->set_vm_result(s_oop);
83 }
84 IRT_END
86 IRT_ENTRY(void, InterpreterRuntime::resolve_ldc(JavaThread* thread, Bytecodes::Code bytecode)) {
87 assert(bytecode == Bytecodes::_fast_aldc ||
88 bytecode == Bytecodes::_fast_aldc_w, "wrong bc");
89 ResourceMark rm(thread);
90 methodHandle m (thread, method(thread));
91 Bytecode_loadconstant* ldc = Bytecode_loadconstant_at(m, bci(thread));
92 oop result = ldc->resolve_constant(THREAD);
93 DEBUG_ONLY(ConstantPoolCacheEntry* cpce = m->constants()->cache()->entry_at(ldc->cache_index()));
94 assert(result == cpce->f1(), "expected result for assembly code");
95 }
96 IRT_END
99 //------------------------------------------------------------------------------------------------------------------------
100 // Allocation
102 IRT_ENTRY(void, InterpreterRuntime::_new(JavaThread* thread, constantPoolOopDesc* pool, int index))
103 klassOop k_oop = pool->klass_at(index, CHECK);
104 instanceKlassHandle klass (THREAD, k_oop);
106 // Make sure we are not instantiating an abstract klass
107 klass->check_valid_for_instantiation(true, CHECK);
109 // Make sure klass is initialized
110 klass->initialize(CHECK);
112 // At this point the class may not be fully initialized
113 // because of recursive initialization. If it is fully
114 // initialized & has_finalized is not set, we rewrite
115 // it into its fast version (Note: no locking is needed
116 // here since this is an atomic byte write and can be
117 // done more than once).
118 //
119 // Note: In case of classes with has_finalized we don't
120 // rewrite since that saves us an extra check in
121 // the fast version which then would call the
122 // slow version anyway (and do a call back into
123 // Java).
124 // If we have a breakpoint, then we don't rewrite
125 // because the _breakpoint bytecode would be lost.
126 oop obj = klass->allocate_instance(CHECK);
127 thread->set_vm_result(obj);
128 IRT_END
131 IRT_ENTRY(void, InterpreterRuntime::newarray(JavaThread* thread, BasicType type, jint size))
132 oop obj = oopFactory::new_typeArray(type, size, CHECK);
133 thread->set_vm_result(obj);
134 IRT_END
137 IRT_ENTRY(void, InterpreterRuntime::anewarray(JavaThread* thread, constantPoolOopDesc* pool, int index, jint size))
138 // Note: no oopHandle for pool & klass needed since they are not used
139 // anymore after new_objArray() and no GC can happen before.
140 // (This may have to change if this code changes!)
141 klassOop klass = pool->klass_at(index, CHECK);
142 objArrayOop obj = oopFactory::new_objArray(klass, size, CHECK);
143 thread->set_vm_result(obj);
144 IRT_END
147 IRT_ENTRY(void, InterpreterRuntime::multianewarray(JavaThread* thread, jint* first_size_address))
148 // We may want to pass in more arguments - could make this slightly faster
149 constantPoolOop constants = method(thread)->constants();
150 int i = get_index_u2(thread, Bytecodes::_multianewarray);
151 klassOop klass = constants->klass_at(i, CHECK);
152 int nof_dims = number_of_dimensions(thread);
153 assert(oop(klass)->is_klass(), "not a class");
154 assert(nof_dims >= 1, "multianewarray rank must be nonzero");
156 // We must create an array of jints to pass to multi_allocate.
157 ResourceMark rm(thread);
158 const int small_dims = 10;
159 jint dim_array[small_dims];
160 jint *dims = &dim_array[0];
161 if (nof_dims > small_dims) {
162 dims = (jint*) NEW_RESOURCE_ARRAY(jint, nof_dims);
163 }
164 for (int index = 0; index < nof_dims; index++) {
165 // offset from first_size_address is addressed as local[index]
166 int n = Interpreter::local_offset_in_bytes(index)/jintSize;
167 dims[index] = first_size_address[n];
168 }
169 oop obj = arrayKlass::cast(klass)->multi_allocate(nof_dims, dims, CHECK);
170 thread->set_vm_result(obj);
171 IRT_END
174 IRT_ENTRY(void, InterpreterRuntime::register_finalizer(JavaThread* thread, oopDesc* obj))
175 assert(obj->is_oop(), "must be a valid oop");
176 assert(obj->klass()->klass_part()->has_finalizer(), "shouldn't be here otherwise");
177 instanceKlass::register_finalizer(instanceOop(obj), CHECK);
178 IRT_END
181 // Quicken instance-of and check-cast bytecodes
182 IRT_ENTRY(void, InterpreterRuntime::quicken_io_cc(JavaThread* thread))
183 // Force resolving; quicken the bytecode
184 int which = get_index_u2(thread, Bytecodes::_checkcast);
185 constantPoolOop cpool = method(thread)->constants();
186 // We'd expect to assert that we're only here to quicken bytecodes, but in a multithreaded
187 // program we might have seen an unquick'd bytecode in the interpreter but have another
188 // thread quicken the bytecode before we get here.
189 // assert( cpool->tag_at(which).is_unresolved_klass(), "should only come here to quicken bytecodes" );
190 klassOop klass = cpool->klass_at(which, CHECK);
191 thread->set_vm_result(klass);
192 IRT_END
195 //------------------------------------------------------------------------------------------------------------------------
196 // Exceptions
198 // Assume the compiler is (or will be) interested in this event.
199 // If necessary, create an MDO to hold the information, and record it.
200 void InterpreterRuntime::note_trap(JavaThread* thread, int reason, TRAPS) {
201 assert(ProfileTraps, "call me only if profiling");
202 methodHandle trap_method(thread, method(thread));
203 if (trap_method.not_null()) {
204 methodDataHandle trap_mdo(thread, trap_method->method_data());
205 if (trap_mdo.is_null()) {
206 methodOopDesc::build_interpreter_method_data(trap_method, THREAD);
207 if (HAS_PENDING_EXCEPTION) {
208 assert((PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())), "we expect only an OOM error here");
209 CLEAR_PENDING_EXCEPTION;
210 }
211 trap_mdo = methodDataHandle(thread, trap_method->method_data());
212 // and fall through...
213 }
214 if (trap_mdo.not_null()) {
215 // Update per-method count of trap events. The interpreter
216 // is updating the MDO to simulate the effect of compiler traps.
217 int trap_bci = trap_method->bci_from(bcp(thread));
218 Deoptimization::update_method_data_from_interpreter(trap_mdo, trap_bci, reason);
219 }
220 }
221 }
223 static Handle get_preinitialized_exception(klassOop k, TRAPS) {
224 // get klass
225 instanceKlass* klass = instanceKlass::cast(k);
226 assert(klass->is_initialized(),
227 "this klass should have been initialized during VM initialization");
228 // create instance - do not call constructor since we may have no
229 // (java) stack space left (should assert constructor is empty)
230 Handle exception;
231 oop exception_oop = klass->allocate_instance(CHECK_(exception));
232 exception = Handle(THREAD, exception_oop);
233 if (StackTraceInThrowable) {
234 java_lang_Throwable::fill_in_stack_trace(exception);
235 }
236 return exception;
237 }
239 // Special handling for stack overflow: since we don't have any (java) stack
240 // space left we use the pre-allocated & pre-initialized StackOverflowError
241 // klass to create an stack overflow error instance. We do not call its
242 // constructor for the same reason (it is empty, anyway).
243 IRT_ENTRY(void, InterpreterRuntime::throw_StackOverflowError(JavaThread* thread))
244 Handle exception = get_preinitialized_exception(
245 SystemDictionary::StackOverflowError_klass(),
246 CHECK);
247 THROW_HANDLE(exception);
248 IRT_END
251 IRT_ENTRY(void, InterpreterRuntime::create_exception(JavaThread* thread, char* name, char* message))
252 // lookup exception klass
253 symbolHandle s = oopFactory::new_symbol_handle(name, CHECK);
254 if (ProfileTraps) {
255 if (s == vmSymbols::java_lang_ArithmeticException()) {
256 note_trap(thread, Deoptimization::Reason_div0_check, CHECK);
257 } else if (s == vmSymbols::java_lang_NullPointerException()) {
258 note_trap(thread, Deoptimization::Reason_null_check, CHECK);
259 }
260 }
261 // create exception
262 Handle exception = Exceptions::new_exception(thread, s(), message);
263 thread->set_vm_result(exception());
264 IRT_END
267 IRT_ENTRY(void, InterpreterRuntime::create_klass_exception(JavaThread* thread, char* name, oopDesc* obj))
268 ResourceMark rm(thread);
269 const char* klass_name = Klass::cast(obj->klass())->external_name();
270 // lookup exception klass
271 symbolHandle s = oopFactory::new_symbol_handle(name, CHECK);
272 if (ProfileTraps) {
273 note_trap(thread, Deoptimization::Reason_class_check, CHECK);
274 }
275 // create exception, with klass name as detail message
276 Handle exception = Exceptions::new_exception(thread, s(), klass_name);
277 thread->set_vm_result(exception());
278 IRT_END
281 IRT_ENTRY(void, InterpreterRuntime::throw_ArrayIndexOutOfBoundsException(JavaThread* thread, char* name, jint index))
282 char message[jintAsStringSize];
283 // lookup exception klass
284 symbolHandle s = oopFactory::new_symbol_handle(name, CHECK);
285 if (ProfileTraps) {
286 note_trap(thread, Deoptimization::Reason_range_check, CHECK);
287 }
288 // create exception
289 sprintf(message, "%d", index);
290 THROW_MSG(s(), message);
291 IRT_END
293 IRT_ENTRY(void, InterpreterRuntime::throw_ClassCastException(
294 JavaThread* thread, oopDesc* obj))
296 ResourceMark rm(thread);
297 char* message = SharedRuntime::generate_class_cast_message(
298 thread, Klass::cast(obj->klass())->external_name());
300 if (ProfileTraps) {
301 note_trap(thread, Deoptimization::Reason_class_check, CHECK);
302 }
304 // create exception
305 THROW_MSG(vmSymbols::java_lang_ClassCastException(), message);
306 IRT_END
308 // required can be either a MethodType, or a Class (for a single argument)
309 // actual (if not null) can be either a MethodHandle, or an arbitrary value (for a single argument)
310 IRT_ENTRY(void, InterpreterRuntime::throw_WrongMethodTypeException(JavaThread* thread,
311 oopDesc* required,
312 oopDesc* actual)) {
313 ResourceMark rm(thread);
314 char* message = SharedRuntime::generate_wrong_method_type_message(thread, required, actual);
316 if (ProfileTraps) {
317 note_trap(thread, Deoptimization::Reason_constraint, CHECK);
318 }
320 // create exception
321 THROW_MSG(vmSymbols::java_dyn_WrongMethodTypeException(), message);
322 }
323 IRT_END
327 // exception_handler_for_exception(...) returns the continuation address,
328 // the exception oop (via TLS) and sets the bci/bcp for the continuation.
329 // The exception oop is returned to make sure it is preserved over GC (it
330 // is only on the stack if the exception was thrown explicitly via athrow).
331 // During this operation, the expression stack contains the values for the
332 // bci where the exception happened. If the exception was propagated back
333 // from a call, the expression stack contains the values for the bci at the
334 // invoke w/o arguments (i.e., as if one were inside the call).
335 IRT_ENTRY(address, InterpreterRuntime::exception_handler_for_exception(JavaThread* thread, oopDesc* exception))
337 Handle h_exception(thread, exception);
338 methodHandle h_method (thread, method(thread));
339 constantPoolHandle h_constants(thread, h_method->constants());
340 typeArrayHandle h_extable (thread, h_method->exception_table());
341 bool should_repeat;
342 int handler_bci;
343 int current_bci = bci(thread);
345 // Need to do this check first since when _do_not_unlock_if_synchronized
346 // is set, we don't want to trigger any classloading which may make calls
347 // into java, or surprisingly find a matching exception handler for bci 0
348 // since at this moment the method hasn't been "officially" entered yet.
349 if (thread->do_not_unlock_if_synchronized()) {
350 ResourceMark rm;
351 assert(current_bci == 0, "bci isn't zero for do_not_unlock_if_synchronized");
352 thread->set_vm_result(exception);
353 #ifdef CC_INTERP
354 return (address) -1;
355 #else
356 return Interpreter::remove_activation_entry();
357 #endif
358 }
360 do {
361 should_repeat = false;
363 // assertions
364 #ifdef ASSERT
365 assert(h_exception.not_null(), "NULL exceptions should be handled by athrow");
366 assert(h_exception->is_oop(), "just checking");
367 // Check that exception is a subclass of Throwable, otherwise we have a VerifyError
368 if (!(h_exception->is_a(SystemDictionary::Throwable_klass()))) {
369 if (ExitVMOnVerifyError) vm_exit(-1);
370 ShouldNotReachHere();
371 }
372 #endif
374 // tracing
375 if (TraceExceptions) {
376 ttyLocker ttyl;
377 ResourceMark rm(thread);
378 tty->print_cr("Exception <%s> (" INTPTR_FORMAT ")", h_exception->print_value_string(), (address)h_exception());
379 tty->print_cr(" thrown in interpreter method <%s>", h_method->print_value_string());
380 tty->print_cr(" at bci %d for thread " INTPTR_FORMAT, current_bci, thread);
381 }
382 // Don't go paging in something which won't be used.
383 // else if (h_extable->length() == 0) {
384 // // disabled for now - interpreter is not using shortcut yet
385 // // (shortcut is not to call runtime if we have no exception handlers)
386 // // warning("performance bug: should not call runtime if method has no exception handlers");
387 // }
388 // for AbortVMOnException flag
389 NOT_PRODUCT(Exceptions::debug_check_abort(h_exception));
391 // exception handler lookup
392 KlassHandle h_klass(THREAD, h_exception->klass());
393 handler_bci = h_method->fast_exception_handler_bci_for(h_klass, current_bci, THREAD);
394 if (HAS_PENDING_EXCEPTION) {
395 // We threw an exception while trying to find the exception handler.
396 // Transfer the new exception to the exception handle which will
397 // be set into thread local storage, and do another lookup for an
398 // exception handler for this exception, this time starting at the
399 // BCI of the exception handler which caused the exception to be
400 // thrown (bug 4307310).
401 h_exception = Handle(THREAD, PENDING_EXCEPTION);
402 CLEAR_PENDING_EXCEPTION;
403 if (handler_bci >= 0) {
404 current_bci = handler_bci;
405 should_repeat = true;
406 }
407 }
408 } while (should_repeat == true);
410 // notify JVMTI of an exception throw; JVMTI will detect if this is a first
411 // time throw or a stack unwinding throw and accordingly notify the debugger
412 if (JvmtiExport::can_post_on_exceptions()) {
413 JvmtiExport::post_exception_throw(thread, h_method(), bcp(thread), h_exception());
414 }
416 #ifdef CC_INTERP
417 address continuation = (address)(intptr_t) handler_bci;
418 #else
419 address continuation = NULL;
420 #endif
421 address handler_pc = NULL;
422 if (handler_bci < 0 || !thread->reguard_stack((address) &continuation)) {
423 // Forward exception to callee (leaving bci/bcp untouched) because (a) no
424 // handler in this method, or (b) after a stack overflow there is not yet
425 // enough stack space available to reprotect the stack.
426 #ifndef CC_INTERP
427 continuation = Interpreter::remove_activation_entry();
428 #endif
429 // Count this for compilation purposes
430 h_method->interpreter_throwout_increment();
431 } else {
432 // handler in this method => change bci/bcp to handler bci/bcp and continue there
433 handler_pc = h_method->code_base() + handler_bci;
434 #ifndef CC_INTERP
435 set_bcp_and_mdp(handler_pc, thread);
436 continuation = Interpreter::dispatch_table(vtos)[*handler_pc];
437 #endif
438 }
439 // notify debugger of an exception catch
440 // (this is good for exceptions caught in native methods as well)
441 if (JvmtiExport::can_post_on_exceptions()) {
442 JvmtiExport::notice_unwind_due_to_exception(thread, h_method(), handler_pc, h_exception(), (handler_pc != NULL));
443 }
445 thread->set_vm_result(h_exception());
446 return continuation;
447 IRT_END
450 IRT_ENTRY(void, InterpreterRuntime::throw_pending_exception(JavaThread* thread))
451 assert(thread->has_pending_exception(), "must only ne called if there's an exception pending");
452 // nothing to do - eventually we should remove this code entirely (see comments @ call sites)
453 IRT_END
456 IRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodError(JavaThread* thread))
457 THROW(vmSymbols::java_lang_AbstractMethodError());
458 IRT_END
461 IRT_ENTRY(void, InterpreterRuntime::throw_IncompatibleClassChangeError(JavaThread* thread))
462 THROW(vmSymbols::java_lang_IncompatibleClassChangeError());
463 IRT_END
466 //------------------------------------------------------------------------------------------------------------------------
467 // Fields
468 //
470 IRT_ENTRY(void, InterpreterRuntime::resolve_get_put(JavaThread* thread, Bytecodes::Code bytecode))
471 // resolve field
472 FieldAccessInfo info;
473 constantPoolHandle pool(thread, method(thread)->constants());
474 bool is_static = (bytecode == Bytecodes::_getstatic || bytecode == Bytecodes::_putstatic);
476 {
477 JvmtiHideSingleStepping jhss(thread);
478 LinkResolver::resolve_field(info, pool, get_index_u2_cpcache(thread, bytecode),
479 bytecode, false, CHECK);
480 } // end JvmtiHideSingleStepping
482 // check if link resolution caused cpCache to be updated
483 if (already_resolved(thread)) return;
485 // compute auxiliary field attributes
486 TosState state = as_TosState(info.field_type());
488 // We need to delay resolving put instructions on final fields
489 // until we actually invoke one. This is required so we throw
490 // exceptions at the correct place. If we do not resolve completely
491 // in the current pass, leaving the put_code set to zero will
492 // cause the next put instruction to reresolve.
493 bool is_put = (bytecode == Bytecodes::_putfield ||
494 bytecode == Bytecodes::_putstatic);
495 Bytecodes::Code put_code = (Bytecodes::Code)0;
497 // We also need to delay resolving getstatic instructions until the
498 // class is intitialized. This is required so that access to the static
499 // field will call the initialization function every time until the class
500 // is completely initialized ala. in 2.17.5 in JVM Specification.
501 instanceKlass *klass = instanceKlass::cast(info.klass()->as_klassOop());
502 bool uninitialized_static = ((bytecode == Bytecodes::_getstatic || bytecode == Bytecodes::_putstatic) &&
503 !klass->is_initialized());
504 Bytecodes::Code get_code = (Bytecodes::Code)0;
507 if (!uninitialized_static) {
508 get_code = ((is_static) ? Bytecodes::_getstatic : Bytecodes::_getfield);
509 if (is_put || !info.access_flags().is_final()) {
510 put_code = ((is_static) ? Bytecodes::_putstatic : Bytecodes::_putfield);
511 }
512 }
514 cache_entry(thread)->set_field(
515 get_code,
516 put_code,
517 info.klass(),
518 info.field_index(),
519 info.field_offset(),
520 state,
521 info.access_flags().is_final(),
522 info.access_flags().is_volatile()
523 );
524 IRT_END
527 //------------------------------------------------------------------------------------------------------------------------
528 // Synchronization
529 //
530 // The interpreter's synchronization code is factored out so that it can
531 // be shared by method invocation and synchronized blocks.
532 //%note synchronization_3
534 static void trace_locking(Handle& h_locking_obj, bool is_locking) {
535 ObjectSynchronizer::trace_locking(h_locking_obj, false, true, is_locking);
536 }
539 //%note monitor_1
540 IRT_ENTRY_NO_ASYNC(void, InterpreterRuntime::monitorenter(JavaThread* thread, BasicObjectLock* elem))
541 #ifdef ASSERT
542 thread->last_frame().interpreter_frame_verify_monitor(elem);
543 #endif
544 if (PrintBiasedLockingStatistics) {
545 Atomic::inc(BiasedLocking::slow_path_entry_count_addr());
546 }
547 Handle h_obj(thread, elem->obj());
548 assert(Universe::heap()->is_in_reserved_or_null(h_obj()),
549 "must be NULL or an object");
550 if (UseBiasedLocking) {
551 // Retry fast entry if bias is revoked to avoid unnecessary inflation
552 ObjectSynchronizer::fast_enter(h_obj, elem->lock(), true, CHECK);
553 } else {
554 ObjectSynchronizer::slow_enter(h_obj, elem->lock(), CHECK);
555 }
556 assert(Universe::heap()->is_in_reserved_or_null(elem->obj()),
557 "must be NULL or an object");
558 #ifdef ASSERT
559 thread->last_frame().interpreter_frame_verify_monitor(elem);
560 #endif
561 IRT_END
564 //%note monitor_1
565 IRT_ENTRY_NO_ASYNC(void, InterpreterRuntime::monitorexit(JavaThread* thread, BasicObjectLock* elem))
566 #ifdef ASSERT
567 thread->last_frame().interpreter_frame_verify_monitor(elem);
568 #endif
569 Handle h_obj(thread, elem->obj());
570 assert(Universe::heap()->is_in_reserved_or_null(h_obj()),
571 "must be NULL or an object");
572 if (elem == NULL || h_obj()->is_unlocked()) {
573 THROW(vmSymbols::java_lang_IllegalMonitorStateException());
574 }
575 ObjectSynchronizer::slow_exit(h_obj(), elem->lock(), thread);
576 // Free entry. This must be done here, since a pending exception might be installed on
577 // exit. If it is not cleared, the exception handling code will try to unlock the monitor again.
578 elem->set_obj(NULL);
579 #ifdef ASSERT
580 thread->last_frame().interpreter_frame_verify_monitor(elem);
581 #endif
582 IRT_END
585 IRT_ENTRY(void, InterpreterRuntime::throw_illegal_monitor_state_exception(JavaThread* thread))
586 THROW(vmSymbols::java_lang_IllegalMonitorStateException());
587 IRT_END
590 IRT_ENTRY(void, InterpreterRuntime::new_illegal_monitor_state_exception(JavaThread* thread))
591 // Returns an illegal exception to install into the current thread. The
592 // pending_exception flag is cleared so normal exception handling does not
593 // trigger. Any current installed exception will be overwritten. This
594 // method will be called during an exception unwind.
596 assert(!HAS_PENDING_EXCEPTION, "no pending exception");
597 Handle exception(thread, thread->vm_result());
598 assert(exception() != NULL, "vm result should be set");
599 thread->set_vm_result(NULL); // clear vm result before continuing (may cause memory leaks and assert failures)
600 if (!exception->is_a(SystemDictionary::ThreadDeath_klass())) {
601 exception = get_preinitialized_exception(
602 SystemDictionary::IllegalMonitorStateException_klass(),
603 CATCH);
604 }
605 thread->set_vm_result(exception());
606 IRT_END
609 //------------------------------------------------------------------------------------------------------------------------
610 // Invokes
612 IRT_ENTRY(Bytecodes::Code, InterpreterRuntime::get_original_bytecode_at(JavaThread* thread, methodOopDesc* method, address bcp))
613 return method->orig_bytecode_at(method->bci_from(bcp));
614 IRT_END
616 IRT_ENTRY(void, InterpreterRuntime::set_original_bytecode_at(JavaThread* thread, methodOopDesc* method, address bcp, Bytecodes::Code new_code))
617 method->set_orig_bytecode_at(method->bci_from(bcp), new_code);
618 IRT_END
620 IRT_ENTRY(void, InterpreterRuntime::_breakpoint(JavaThread* thread, methodOopDesc* method, address bcp))
621 JvmtiExport::post_raw_breakpoint(thread, method, bcp);
622 IRT_END
624 IRT_ENTRY(void, InterpreterRuntime::resolve_invoke(JavaThread* thread, Bytecodes::Code bytecode))
625 // extract receiver from the outgoing argument list if necessary
626 Handle receiver(thread, NULL);
627 if (bytecode == Bytecodes::_invokevirtual || bytecode == Bytecodes::_invokeinterface) {
628 ResourceMark rm(thread);
629 methodHandle m (thread, method(thread));
630 Bytecode_invoke* call = Bytecode_invoke_at(m, bci(thread));
631 symbolHandle signature (thread, call->signature());
632 receiver = Handle(thread,
633 thread->last_frame().interpreter_callee_receiver(signature));
634 assert(Universe::heap()->is_in_reserved_or_null(receiver()),
635 "sanity check");
636 assert(receiver.is_null() ||
637 Universe::heap()->is_in_reserved(receiver->klass()),
638 "sanity check");
639 }
641 // resolve method
642 CallInfo info;
643 constantPoolHandle pool(thread, method(thread)->constants());
645 {
646 JvmtiHideSingleStepping jhss(thread);
647 LinkResolver::resolve_invoke(info, receiver, pool,
648 get_index_u2_cpcache(thread, bytecode), bytecode, CHECK);
649 if (JvmtiExport::can_hotswap_or_post_breakpoint()) {
650 int retry_count = 0;
651 while (info.resolved_method()->is_old()) {
652 // It is very unlikely that method is redefined more than 100 times
653 // in the middle of resolve. If it is looping here more than 100 times
654 // means then there could be a bug here.
655 guarantee((retry_count++ < 100),
656 "Could not resolve to latest version of redefined method");
657 // method is redefined in the middle of resolve so re-try.
658 LinkResolver::resolve_invoke(info, receiver, pool,
659 get_index_u2_cpcache(thread, bytecode), bytecode, CHECK);
660 }
661 }
662 } // end JvmtiHideSingleStepping
664 // check if link resolution caused cpCache to be updated
665 if (already_resolved(thread)) return;
667 if (bytecode == Bytecodes::_invokeinterface) {
669 if (TraceItables && Verbose) {
670 ResourceMark rm(thread);
671 tty->print_cr("Resolving: klass: %s to method: %s", info.resolved_klass()->name()->as_C_string(), info.resolved_method()->name()->as_C_string());
672 }
673 if (info.resolved_method()->method_holder() ==
674 SystemDictionary::Object_klass()) {
675 // NOTE: THIS IS A FIX FOR A CORNER CASE in the JVM spec
676 // (see also cpCacheOop.cpp for details)
677 methodHandle rm = info.resolved_method();
678 assert(rm->is_final() || info.has_vtable_index(),
679 "should have been set already");
680 cache_entry(thread)->set_method(bytecode, rm, info.vtable_index());
681 } else {
682 // Setup itable entry
683 int index = klassItable::compute_itable_index(info.resolved_method()());
684 cache_entry(thread)->set_interface_call(info.resolved_method(), index);
685 }
686 } else {
687 cache_entry(thread)->set_method(
688 bytecode,
689 info.resolved_method(),
690 info.vtable_index());
691 }
692 IRT_END
695 // First time execution: Resolve symbols, create a permanent CallSite object.
696 IRT_ENTRY(void, InterpreterRuntime::resolve_invokedynamic(JavaThread* thread)) {
697 ResourceMark rm(thread);
699 assert(EnableInvokeDynamic, "");
701 const Bytecodes::Code bytecode = Bytecodes::_invokedynamic;
703 methodHandle caller_method(thread, method(thread));
705 // first find the bootstrap method
706 KlassHandle caller_klass(thread, caller_method->method_holder());
707 Handle bootm = SystemDictionary::find_bootstrap_method(caller_klass, CHECK);
709 constantPoolHandle pool(thread, caller_method->constants());
710 pool->set_invokedynamic(); // mark header to flag active call sites
712 int caller_bci = 0;
713 int site_index = 0;
714 { address caller_bcp = bcp(thread);
715 caller_bci = caller_method->bci_from(caller_bcp);
716 site_index = Bytes::get_native_u4(caller_bcp+1);
717 }
718 assert(site_index == InterpreterRuntime::bytecode(thread)->get_index_u4(bytecode), "");
719 assert(constantPoolCacheOopDesc::is_secondary_index(site_index), "proper format");
720 // there is a second CPC entries that is of interest; it caches signature info:
721 int main_index = pool->cache()->secondary_entry_at(site_index)->main_entry_index();
723 // first resolve the signature to a MH.invoke methodOop
724 if (!pool->cache()->entry_at(main_index)->is_resolved(bytecode)) {
725 JvmtiHideSingleStepping jhss(thread);
726 CallInfo info;
727 LinkResolver::resolve_invoke(info, Handle(), pool,
728 site_index, bytecode, CHECK);
729 // The main entry corresponds to a JVM_CONSTANT_NameAndType, and serves
730 // as a common reference point for all invokedynamic call sites with
731 // that exact call descriptor. We will link it in the CP cache exactly
732 // as if it were an invokevirtual of MethodHandle.invoke.
733 pool->cache()->entry_at(main_index)->set_method(
734 bytecode,
735 info.resolved_method(),
736 info.vtable_index());
737 assert(pool->cache()->entry_at(main_index)->is_vfinal(), "f2 must be a methodOop");
738 }
740 // The method (f2 entry) of the main entry is the MH.invoke for the
741 // invokedynamic target call signature.
742 intptr_t f2_value = pool->cache()->entry_at(main_index)->f2();
743 methodHandle signature_invoker(THREAD, (methodOop) f2_value);
744 assert(signature_invoker.not_null() && signature_invoker->is_method() && signature_invoker->is_method_handle_invoke(),
745 "correct result from LinkResolver::resolve_invokedynamic");
747 symbolHandle call_site_name(THREAD, pool->name_ref_at(site_index));
749 Handle info; // NYI: Other metadata from a new kind of CP entry. (Annotations?)
751 // this is the index which gets stored on the CallSite object (as "callerPosition"):
752 int call_site_position = constantPoolCacheOopDesc::decode_secondary_index(site_index);
754 Handle call_site
755 = SystemDictionary::make_dynamic_call_site(bootm,
756 // Callee information:
757 call_site_name,
758 signature_invoker,
759 info,
760 // Caller information:
761 caller_method,
762 caller_bci,
763 CHECK);
765 // In the secondary entry, the f1 field is the call site, and the f2 (index)
766 // field is some data about the invoke site. Currently, it is just the BCI.
767 // Later, it might be changed to help manage inlining dependencies.
768 pool->cache()->secondary_entry_at(site_index)->set_dynamic_call(call_site, signature_invoker);
769 }
770 IRT_END
773 //------------------------------------------------------------------------------------------------------------------------
774 // Miscellaneous
777 #ifndef PRODUCT
778 static void trace_frequency_counter_overflow(methodHandle m, int branch_bci, int bci, address branch_bcp) {
779 if (TraceInvocationCounterOverflow) {
780 InvocationCounter* ic = m->invocation_counter();
781 InvocationCounter* bc = m->backedge_counter();
782 ResourceMark rm;
783 const char* msg =
784 branch_bcp == NULL
785 ? "comp-policy cntr ovfl @ %d in entry of "
786 : "comp-policy cntr ovfl @ %d in loop of ";
787 tty->print(msg, bci);
788 m->print_value();
789 tty->cr();
790 ic->print();
791 bc->print();
792 if (ProfileInterpreter) {
793 if (branch_bcp != NULL) {
794 methodDataOop mdo = m->method_data();
795 if (mdo != NULL) {
796 int count = mdo->bci_to_data(branch_bci)->as_JumpData()->taken();
797 tty->print_cr("back branch count = %d", count);
798 }
799 }
800 }
801 }
802 }
804 static void trace_osr_request(methodHandle method, nmethod* osr, int bci) {
805 if (TraceOnStackReplacement) {
806 ResourceMark rm;
807 tty->print(osr != NULL ? "Reused OSR entry for " : "Requesting OSR entry for ");
808 method->print_short_name(tty);
809 tty->print_cr(" at bci %d", bci);
810 }
811 }
812 #endif // !PRODUCT
814 nmethod* InterpreterRuntime::frequency_counter_overflow(JavaThread* thread, address branch_bcp) {
815 nmethod* nm = frequency_counter_overflow_inner(thread, branch_bcp);
816 assert(branch_bcp != NULL || nm == NULL, "always returns null for non OSR requests");
817 if (branch_bcp != NULL && nm != NULL) {
818 // This was a successful request for an OSR nmethod. Because
819 // frequency_counter_overflow_inner ends with a safepoint check,
820 // nm could have been unloaded so look it up again. It's unsafe
821 // to examine nm directly since it might have been freed and used
822 // for something else.
823 frame fr = thread->last_frame();
824 methodOop method = fr.interpreter_frame_method();
825 int bci = method->bci_from(fr.interpreter_frame_bcp());
826 nm = method->lookup_osr_nmethod_for(bci);
827 }
828 return nm;
829 }
831 IRT_ENTRY(nmethod*,
832 InterpreterRuntime::frequency_counter_overflow_inner(JavaThread* thread, address branch_bcp))
833 // use UnlockFlagSaver to clear and restore the _do_not_unlock_if_synchronized
834 // flag, in case this method triggers classloading which will call into Java.
835 UnlockFlagSaver fs(thread);
837 frame fr = thread->last_frame();
838 assert(fr.is_interpreted_frame(), "must come from interpreter");
839 methodHandle method(thread, fr.interpreter_frame_method());
840 const int branch_bci = branch_bcp != NULL ? method->bci_from(branch_bcp) : 0;
841 const int bci = method->bci_from(fr.interpreter_frame_bcp());
842 NOT_PRODUCT(trace_frequency_counter_overflow(method, branch_bci, bci, branch_bcp);)
844 if (JvmtiExport::can_post_interpreter_events()) {
845 if (thread->is_interp_only_mode()) {
846 // If certain JVMTI events (e.g. frame pop event) are requested then the
847 // thread is forced to remain in interpreted code. This is
848 // implemented partly by a check in the run_compiled_code
849 // section of the interpreter whether we should skip running
850 // compiled code, and partly by skipping OSR compiles for
851 // interpreted-only threads.
852 if (branch_bcp != NULL) {
853 CompilationPolicy::policy()->reset_counter_for_back_branch_event(method);
854 return NULL;
855 }
856 }
857 }
859 if (branch_bcp == NULL) {
860 // when code cache is full, compilation gets switched off, UseCompiler
861 // is set to false
862 if (!method->has_compiled_code() && UseCompiler) {
863 CompilationPolicy::policy()->method_invocation_event(method, CHECK_NULL);
864 } else {
865 // Force counter overflow on method entry, even if no compilation
866 // happened. (The method_invocation_event call does this also.)
867 CompilationPolicy::policy()->reset_counter_for_invocation_event(method);
868 }
869 // compilation at an invocation overflow no longer goes and retries test for
870 // compiled method. We always run the loser of the race as interpreted.
871 // so return NULL
872 return NULL;
873 } else {
874 // counter overflow in a loop => try to do on-stack-replacement
875 nmethod* osr_nm = method->lookup_osr_nmethod_for(bci);
876 NOT_PRODUCT(trace_osr_request(method, osr_nm, bci);)
877 // when code cache is full, we should not compile any more...
878 if (osr_nm == NULL && UseCompiler) {
879 const int branch_bci = method->bci_from(branch_bcp);
880 CompilationPolicy::policy()->method_back_branch_event(method, branch_bci, bci, CHECK_NULL);
881 osr_nm = method->lookup_osr_nmethod_for(bci);
882 }
883 if (osr_nm == NULL) {
884 CompilationPolicy::policy()->reset_counter_for_back_branch_event(method);
885 return NULL;
886 } else {
887 // We may need to do on-stack replacement which requires that no
888 // monitors in the activation are biased because their
889 // BasicObjectLocks will need to migrate during OSR. Force
890 // unbiasing of all monitors in the activation now (even though
891 // the OSR nmethod might be invalidated) because we don't have a
892 // safepoint opportunity later once the migration begins.
893 if (UseBiasedLocking) {
894 ResourceMark rm;
895 GrowableArray<Handle>* objects_to_revoke = new GrowableArray<Handle>();
896 for( BasicObjectLock *kptr = fr.interpreter_frame_monitor_end();
897 kptr < fr.interpreter_frame_monitor_begin();
898 kptr = fr.next_monitor_in_interpreter_frame(kptr) ) {
899 if( kptr->obj() != NULL ) {
900 objects_to_revoke->append(Handle(THREAD, kptr->obj()));
901 }
902 }
903 BiasedLocking::revoke(objects_to_revoke);
904 }
905 return osr_nm;
906 }
907 }
908 IRT_END
910 IRT_LEAF(jint, InterpreterRuntime::bcp_to_di(methodOopDesc* method, address cur_bcp))
911 assert(ProfileInterpreter, "must be profiling interpreter");
912 int bci = method->bci_from(cur_bcp);
913 methodDataOop mdo = method->method_data();
914 if (mdo == NULL) return 0;
915 return mdo->bci_to_di(bci);
916 IRT_END
918 IRT_ENTRY(jint, InterpreterRuntime::profile_method(JavaThread* thread, address cur_bcp))
919 // use UnlockFlagSaver to clear and restore the _do_not_unlock_if_synchronized
920 // flag, in case this method triggers classloading which will call into Java.
921 UnlockFlagSaver fs(thread);
923 assert(ProfileInterpreter, "must be profiling interpreter");
924 frame fr = thread->last_frame();
925 assert(fr.is_interpreted_frame(), "must come from interpreter");
926 methodHandle method(thread, fr.interpreter_frame_method());
927 int bci = method->bci_from(cur_bcp);
928 methodOopDesc::build_interpreter_method_data(method, THREAD);
929 if (HAS_PENDING_EXCEPTION) {
930 assert((PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())), "we expect only an OOM error here");
931 CLEAR_PENDING_EXCEPTION;
932 // and fall through...
933 }
934 methodDataOop mdo = method->method_data();
935 if (mdo == NULL) return 0;
936 return mdo->bci_to_di(bci);
937 IRT_END
940 #ifdef ASSERT
941 IRT_LEAF(void, InterpreterRuntime::verify_mdp(methodOopDesc* method, address bcp, address mdp))
942 assert(ProfileInterpreter, "must be profiling interpreter");
944 methodDataOop mdo = method->method_data();
945 assert(mdo != NULL, "must not be null");
947 int bci = method->bci_from(bcp);
949 address mdp2 = mdo->bci_to_dp(bci);
950 if (mdp != mdp2) {
951 ResourceMark rm;
952 ResetNoHandleMark rnm; // In a LEAF entry.
953 HandleMark hm;
954 tty->print_cr("FAILED verify : actual mdp %p expected mdp %p @ bci %d", mdp, mdp2, bci);
955 int current_di = mdo->dp_to_di(mdp);
956 int expected_di = mdo->dp_to_di(mdp2);
957 tty->print_cr(" actual di %d expected di %d", current_di, expected_di);
958 int expected_approx_bci = mdo->data_at(expected_di)->bci();
959 int approx_bci = -1;
960 if (current_di >= 0) {
961 approx_bci = mdo->data_at(current_di)->bci();
962 }
963 tty->print_cr(" actual bci is %d expected bci %d", approx_bci, expected_approx_bci);
964 mdo->print_on(tty);
965 method->print_codes();
966 }
967 assert(mdp == mdp2, "wrong mdp");
968 IRT_END
969 #endif // ASSERT
971 IRT_ENTRY(void, InterpreterRuntime::update_mdp_for_ret(JavaThread* thread, int return_bci))
972 assert(ProfileInterpreter, "must be profiling interpreter");
973 ResourceMark rm(thread);
974 HandleMark hm(thread);
975 frame fr = thread->last_frame();
976 assert(fr.is_interpreted_frame(), "must come from interpreter");
977 methodDataHandle h_mdo(thread, fr.interpreter_frame_method()->method_data());
979 // Grab a lock to ensure atomic access to setting the return bci and
980 // the displacement. This can block and GC, invalidating all naked oops.
981 MutexLocker ml(RetData_lock);
983 // ProfileData is essentially a wrapper around a derived oop, so we
984 // need to take the lock before making any ProfileData structures.
985 ProfileData* data = h_mdo->data_at(h_mdo->dp_to_di(fr.interpreter_frame_mdp()));
986 RetData* rdata = data->as_RetData();
987 address new_mdp = rdata->fixup_ret(return_bci, h_mdo);
988 fr.interpreter_frame_set_mdp(new_mdp);
989 IRT_END
992 IRT_ENTRY(void, InterpreterRuntime::at_safepoint(JavaThread* thread))
993 // We used to need an explict preserve_arguments here for invoke bytecodes. However,
994 // stack traversal automatically takes care of preserving arguments for invoke, so
995 // this is no longer needed.
997 // IRT_END does an implicit safepoint check, hence we are guaranteed to block
998 // if this is called during a safepoint
1000 if (JvmtiExport::should_post_single_step()) {
1001 // We are called during regular safepoints and when the VM is
1002 // single stepping. If any thread is marked for single stepping,
1003 // then we may have JVMTI work to do.
1004 JvmtiExport::at_single_stepping_point(thread, method(thread), bcp(thread));
1005 }
1006 IRT_END
1008 IRT_ENTRY(void, InterpreterRuntime::post_field_access(JavaThread *thread, oopDesc* obj,
1009 ConstantPoolCacheEntry *cp_entry))
1011 // check the access_flags for the field in the klass
1012 instanceKlass* ik = instanceKlass::cast((klassOop)cp_entry->f1());
1013 typeArrayOop fields = ik->fields();
1014 int index = cp_entry->field_index();
1015 assert(index < fields->length(), "holders field index is out of range");
1016 // bail out if field accesses are not watched
1017 if ((fields->ushort_at(index) & JVM_ACC_FIELD_ACCESS_WATCHED) == 0) return;
1019 switch(cp_entry->flag_state()) {
1020 case btos: // fall through
1021 case ctos: // fall through
1022 case stos: // fall through
1023 case itos: // fall through
1024 case ftos: // fall through
1025 case ltos: // fall through
1026 case dtos: // fall through
1027 case atos: break;
1028 default: ShouldNotReachHere(); return;
1029 }
1030 bool is_static = (obj == NULL);
1031 HandleMark hm(thread);
1033 Handle h_obj;
1034 if (!is_static) {
1035 // non-static field accessors have an object, but we need a handle
1036 h_obj = Handle(thread, obj);
1037 }
1038 instanceKlassHandle h_cp_entry_f1(thread, (klassOop)cp_entry->f1());
1039 jfieldID fid = jfieldIDWorkaround::to_jfieldID(h_cp_entry_f1, cp_entry->f2(), is_static);
1040 JvmtiExport::post_field_access(thread, method(thread), bcp(thread), h_cp_entry_f1, h_obj, fid);
1041 IRT_END
1043 IRT_ENTRY(void, InterpreterRuntime::post_field_modification(JavaThread *thread,
1044 oopDesc* obj, ConstantPoolCacheEntry *cp_entry, jvalue *value))
1046 klassOop k = (klassOop)cp_entry->f1();
1048 // check the access_flags for the field in the klass
1049 instanceKlass* ik = instanceKlass::cast(k);
1050 typeArrayOop fields = ik->fields();
1051 int index = cp_entry->field_index();
1052 assert(index < fields->length(), "holders field index is out of range");
1053 // bail out if field modifications are not watched
1054 if ((fields->ushort_at(index) & JVM_ACC_FIELD_MODIFICATION_WATCHED) == 0) return;
1056 char sig_type = '\0';
1058 switch(cp_entry->flag_state()) {
1059 case btos: sig_type = 'Z'; break;
1060 case ctos: sig_type = 'C'; break;
1061 case stos: sig_type = 'S'; break;
1062 case itos: sig_type = 'I'; break;
1063 case ftos: sig_type = 'F'; break;
1064 case atos: sig_type = 'L'; break;
1065 case ltos: sig_type = 'J'; break;
1066 case dtos: sig_type = 'D'; break;
1067 default: ShouldNotReachHere(); return;
1068 }
1069 bool is_static = (obj == NULL);
1071 HandleMark hm(thread);
1072 instanceKlassHandle h_klass(thread, k);
1073 jfieldID fid = jfieldIDWorkaround::to_jfieldID(h_klass, cp_entry->f2(), is_static);
1074 jvalue fvalue;
1075 #ifdef _LP64
1076 fvalue = *value;
1077 #else
1078 // Long/double values are stored unaligned and also noncontiguously with
1079 // tagged stacks. We can't just do a simple assignment even in the non-
1080 // J/D cases because a C++ compiler is allowed to assume that a jvalue is
1081 // 8-byte aligned, and interpreter stack slots are only 4-byte aligned.
1082 // We assume that the two halves of longs/doubles are stored in interpreter
1083 // stack slots in platform-endian order.
1084 jlong_accessor u;
1085 jint* newval = (jint*)value;
1086 u.words[0] = newval[0];
1087 u.words[1] = newval[Interpreter::stackElementWords]; // skip if tag
1088 fvalue.j = u.long_value;
1089 #endif // _LP64
1091 Handle h_obj;
1092 if (!is_static) {
1093 // non-static field accessors have an object, but we need a handle
1094 h_obj = Handle(thread, obj);
1095 }
1097 JvmtiExport::post_raw_field_modification(thread, method(thread), bcp(thread), h_klass, h_obj,
1098 fid, sig_type, &fvalue);
1099 IRT_END
1101 IRT_ENTRY(void, InterpreterRuntime::post_method_entry(JavaThread *thread))
1102 JvmtiExport::post_method_entry(thread, InterpreterRuntime::method(thread), InterpreterRuntime::last_frame(thread));
1103 IRT_END
1106 IRT_ENTRY(void, InterpreterRuntime::post_method_exit(JavaThread *thread))
1107 JvmtiExport::post_method_exit(thread, InterpreterRuntime::method(thread), InterpreterRuntime::last_frame(thread));
1108 IRT_END
1110 IRT_LEAF(int, InterpreterRuntime::interpreter_contains(address pc))
1111 {
1112 return (Interpreter::contains(pc) ? 1 : 0);
1113 }
1114 IRT_END
1117 // Implementation of SignatureHandlerLibrary
1119 address SignatureHandlerLibrary::set_handler_blob() {
1120 BufferBlob* handler_blob = BufferBlob::create("native signature handlers", blob_size);
1121 if (handler_blob == NULL) {
1122 return NULL;
1123 }
1124 address handler = handler_blob->instructions_begin();
1125 _handler_blob = handler_blob;
1126 _handler = handler;
1127 return handler;
1128 }
1130 void SignatureHandlerLibrary::initialize() {
1131 if (_fingerprints != NULL) {
1132 return;
1133 }
1134 if (set_handler_blob() == NULL) {
1135 vm_exit_out_of_memory(blob_size, "native signature handlers");
1136 }
1138 BufferBlob* bb = BufferBlob::create("Signature Handler Temp Buffer",
1139 SignatureHandlerLibrary::buffer_size);
1140 _buffer = bb->instructions_begin();
1142 _fingerprints = new(ResourceObj::C_HEAP)GrowableArray<uint64_t>(32, true);
1143 _handlers = new(ResourceObj::C_HEAP)GrowableArray<address>(32, true);
1144 }
1146 address SignatureHandlerLibrary::set_handler(CodeBuffer* buffer) {
1147 address handler = _handler;
1148 int code_size = buffer->pure_code_size();
1149 if (handler + code_size > _handler_blob->instructions_end()) {
1150 // get a new handler blob
1151 handler = set_handler_blob();
1152 }
1153 if (handler != NULL) {
1154 memcpy(handler, buffer->code_begin(), code_size);
1155 pd_set_handler(handler);
1156 ICache::invalidate_range(handler, code_size);
1157 _handler = handler + code_size;
1158 }
1159 return handler;
1160 }
1162 void SignatureHandlerLibrary::add(methodHandle method) {
1163 if (method->signature_handler() == NULL) {
1164 // use slow signature handler if we can't do better
1165 int handler_index = -1;
1166 // check if we can use customized (fast) signature handler
1167 if (UseFastSignatureHandlers && method->size_of_parameters() <= Fingerprinter::max_size_of_parameters) {
1168 // use customized signature handler
1169 MutexLocker mu(SignatureHandlerLibrary_lock);
1170 // make sure data structure is initialized
1171 initialize();
1172 // lookup method signature's fingerprint
1173 uint64_t fingerprint = Fingerprinter(method).fingerprint();
1174 handler_index = _fingerprints->find(fingerprint);
1175 // create handler if necessary
1176 if (handler_index < 0) {
1177 ResourceMark rm;
1178 ptrdiff_t align_offset = (address)
1179 round_to((intptr_t)_buffer, CodeEntryAlignment) - (address)_buffer;
1180 CodeBuffer buffer((address)(_buffer + align_offset),
1181 SignatureHandlerLibrary::buffer_size - align_offset);
1182 InterpreterRuntime::SignatureHandlerGenerator(method, &buffer).generate(fingerprint);
1183 // copy into code heap
1184 address handler = set_handler(&buffer);
1185 if (handler == NULL) {
1186 // use slow signature handler
1187 } else {
1188 // debugging suppport
1189 if (PrintSignatureHandlers) {
1190 tty->cr();
1191 tty->print_cr("argument handler #%d for: %s %s (fingerprint = " UINT64_FORMAT ", %d bytes generated)",
1192 _handlers->length(),
1193 (method->is_static() ? "static" : "receiver"),
1194 method->name_and_sig_as_C_string(),
1195 fingerprint,
1196 buffer.code_size());
1197 Disassembler::decode(handler, handler + buffer.code_size());
1198 #ifndef PRODUCT
1199 tty->print_cr(" --- associated result handler ---");
1200 address rh_begin = Interpreter::result_handler(method()->result_type());
1201 address rh_end = rh_begin;
1202 while (*(int*)rh_end != 0) {
1203 rh_end += sizeof(int);
1204 }
1205 Disassembler::decode(rh_begin, rh_end);
1206 #endif
1207 }
1208 // add handler to library
1209 _fingerprints->append(fingerprint);
1210 _handlers->append(handler);
1211 // set handler index
1212 assert(_fingerprints->length() == _handlers->length(), "sanity check");
1213 handler_index = _fingerprints->length() - 1;
1214 }
1215 }
1216 } else {
1217 CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
1218 }
1219 if (handler_index < 0) {
1220 // use generic signature handler
1221 method->set_signature_handler(Interpreter::slow_signature_handler());
1222 } else {
1223 // set handler
1224 method->set_signature_handler(_handlers->at(handler_index));
1225 }
1226 }
1227 assert(method->signature_handler() == Interpreter::slow_signature_handler() ||
1228 _handlers->find(method->signature_handler()) == _fingerprints->find(Fingerprinter(method).fingerprint()),
1229 "sanity check");
1230 }
1233 BufferBlob* SignatureHandlerLibrary::_handler_blob = NULL;
1234 address SignatureHandlerLibrary::_handler = NULL;
1235 GrowableArray<uint64_t>* SignatureHandlerLibrary::_fingerprints = NULL;
1236 GrowableArray<address>* SignatureHandlerLibrary::_handlers = NULL;
1237 address SignatureHandlerLibrary::_buffer = NULL;
1240 IRT_ENTRY(void, InterpreterRuntime::prepare_native_call(JavaThread* thread, methodOopDesc* method))
1241 methodHandle m(thread, method);
1242 assert(m->is_native(), "sanity check");
1243 // lookup native function entry point if it doesn't exist
1244 bool in_base_library;
1245 if (!m->has_native_function()) {
1246 NativeLookup::lookup(m, in_base_library, CHECK);
1247 }
1248 // make sure signature handler is installed
1249 SignatureHandlerLibrary::add(m);
1250 // The interpreter entry point checks the signature handler first,
1251 // before trying to fetch the native entry point and klass mirror.
1252 // We must set the signature handler last, so that multiple processors
1253 // preparing the same method will be sure to see non-null entry & mirror.
1254 IRT_END
1256 #if defined(IA32) || defined(AMD64)
1257 IRT_LEAF(void, InterpreterRuntime::popframe_move_outgoing_args(JavaThread* thread, void* src_address, void* dest_address))
1258 if (src_address == dest_address) {
1259 return;
1260 }
1261 ResetNoHandleMark rnm; // In a LEAF entry.
1262 HandleMark hm;
1263 ResourceMark rm;
1264 frame fr = thread->last_frame();
1265 assert(fr.is_interpreted_frame(), "");
1266 jint bci = fr.interpreter_frame_bci();
1267 methodHandle mh(thread, fr.interpreter_frame_method());
1268 Bytecode_invoke* invoke = Bytecode_invoke_at(mh, bci);
1269 ArgumentSizeComputer asc(invoke->signature());
1270 int size_of_arguments = (asc.size() + (invoke->has_receiver() ? 1 : 0)); // receiver
1271 Copy::conjoint_bytes(src_address, dest_address,
1272 size_of_arguments * Interpreter::stackElementSize);
1273 IRT_END
1274 #endif