Thu, 24 May 2018 17:06:56 +0800
Merge
1 /*
2 * Copyright (c) 1997, 2016, 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 /*
26 * This file has been modified by Loongson Technology in 2015. These
27 * modifications are Copyright (c) 2015 Loongson Technology, and are made
28 * available on the same license terms set forth above.
29 */
31 #include "precompiled.hpp"
32 #include "classfile/systemDictionary.hpp"
33 #include "classfile/vmSymbols.hpp"
34 #include "compiler/compileBroker.hpp"
35 #include "compiler/disassembler.hpp"
36 #include "gc_interface/collectedHeap.hpp"
37 #include "interpreter/interpreter.hpp"
38 #include "interpreter/interpreterRuntime.hpp"
39 #include "interpreter/linkResolver.hpp"
40 #include "interpreter/templateTable.hpp"
41 #include "memory/oopFactory.hpp"
42 #include "memory/universe.inline.hpp"
43 #include "oops/constantPool.hpp"
44 #include "oops/instanceKlass.hpp"
45 #include "oops/methodData.hpp"
46 #include "oops/objArrayKlass.hpp"
47 #include "oops/oop.inline.hpp"
48 #include "oops/symbol.hpp"
49 #include "prims/jvmtiExport.hpp"
50 #include "prims/nativeLookup.hpp"
51 #include "runtime/biasedLocking.hpp"
52 #include "runtime/compilationPolicy.hpp"
53 #include "runtime/deoptimization.hpp"
54 #include "runtime/fieldDescriptor.hpp"
55 #include "runtime/handles.inline.hpp"
56 #include "runtime/interfaceSupport.hpp"
57 #include "runtime/java.hpp"
58 #include "runtime/jfieldIDWorkaround.hpp"
59 #include "runtime/osThread.hpp"
60 #include "runtime/sharedRuntime.hpp"
61 #include "runtime/stubRoutines.hpp"
62 #include "runtime/synchronizer.hpp"
63 #include "runtime/threadCritical.hpp"
64 #include "utilities/events.hpp"
65 #ifdef TARGET_ARCH_x86
66 # include "vm_version_x86.hpp"
67 #endif
68 #ifdef TARGET_ARCH_mips
69 # include "vm_version_mips.hpp"
70 #endif
71 #ifdef TARGET_ARCH_sparc
72 # include "vm_version_sparc.hpp"
73 #endif
74 #ifdef TARGET_ARCH_zero
75 # include "vm_version_zero.hpp"
76 #endif
77 #ifdef TARGET_ARCH_arm
78 # include "vm_version_arm.hpp"
79 #endif
80 #ifdef TARGET_ARCH_ppc
81 # include "vm_version_ppc.hpp"
82 #endif
83 #ifdef COMPILER2
84 #include "opto/runtime.hpp"
85 #endif
87 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
89 class UnlockFlagSaver {
90 private:
91 JavaThread* _thread;
92 bool _do_not_unlock;
93 public:
94 UnlockFlagSaver(JavaThread* t) {
95 _thread = t;
96 _do_not_unlock = t->do_not_unlock_if_synchronized();
97 t->set_do_not_unlock_if_synchronized(false);
98 }
99 ~UnlockFlagSaver() {
100 _thread->set_do_not_unlock_if_synchronized(_do_not_unlock);
101 }
102 };
104 //------------------------------------------------------------------------------------------------------------------------
105 // State accessors
107 void InterpreterRuntime::set_bcp_and_mdp(address bcp, JavaThread *thread) {
108 last_frame(thread).interpreter_frame_set_bcp(bcp);
109 if (ProfileInterpreter) {
110 // ProfileTraps uses MDOs independently of ProfileInterpreter.
111 // That is why we must check both ProfileInterpreter and mdo != NULL.
112 MethodData* mdo = last_frame(thread).interpreter_frame_method()->method_data();
113 if (mdo != NULL) {
114 NEEDS_CLEANUP;
115 last_frame(thread).interpreter_frame_set_mdp(mdo->bci_to_dp(last_frame(thread).interpreter_frame_bci()));
116 }
117 }
118 }
120 //------------------------------------------------------------------------------------------------------------------------
121 // Constants
124 IRT_ENTRY(void, InterpreterRuntime::ldc(JavaThread* thread, bool wide))
125 // access constant pool
126 ConstantPool* pool = method(thread)->constants();
127 int index = wide ? get_index_u2(thread, Bytecodes::_ldc_w) : get_index_u1(thread, Bytecodes::_ldc);
128 constantTag tag = pool->tag_at(index);
130 assert (tag.is_unresolved_klass() || tag.is_klass(), "wrong ldc call");
131 Klass* klass = pool->klass_at(index, CHECK);
132 oop java_class = klass->java_mirror();
133 thread->set_vm_result(java_class);
134 IRT_END
136 IRT_ENTRY(void, InterpreterRuntime::resolve_ldc(JavaThread* thread, Bytecodes::Code bytecode)) {
137 assert(bytecode == Bytecodes::_fast_aldc ||
138 bytecode == Bytecodes::_fast_aldc_w, "wrong bc");
139 ResourceMark rm(thread);
140 methodHandle m (thread, method(thread));
141 Bytecode_loadconstant ldc(m, bci(thread));
142 oop result = ldc.resolve_constant(CHECK);
143 #ifdef ASSERT
144 {
145 // The bytecode wrappers aren't GC-safe so construct a new one
146 Bytecode_loadconstant ldc2(m, bci(thread));
147 oop coop = m->constants()->resolved_references()->obj_at(ldc2.cache_index());
148 assert(result == coop, "expected result for assembly code");
149 }
150 #endif
151 thread->set_vm_result(result);
152 }
153 IRT_END
156 //------------------------------------------------------------------------------------------------------------------------
157 // Allocation
159 IRT_ENTRY(void, InterpreterRuntime::_new(JavaThread* thread, ConstantPool* pool, int index))
160 Klass* k_oop = pool->klass_at(index, CHECK);
161 instanceKlassHandle klass (THREAD, k_oop);
163 // Make sure we are not instantiating an abstract klass
164 klass->check_valid_for_instantiation(true, CHECK);
166 // Make sure klass is initialized
167 klass->initialize(CHECK);
169 // At this point the class may not be fully initialized
170 // because of recursive initialization. If it is fully
171 // initialized & has_finalized is not set, we rewrite
172 // it into its fast version (Note: no locking is needed
173 // here since this is an atomic byte write and can be
174 // done more than once).
175 //
176 // Note: In case of classes with has_finalized we don't
177 // rewrite since that saves us an extra check in
178 // the fast version which then would call the
179 // slow version anyway (and do a call back into
180 // Java).
181 // If we have a breakpoint, then we don't rewrite
182 // because the _breakpoint bytecode would be lost.
183 oop obj = klass->allocate_instance(CHECK);
184 thread->set_vm_result(obj);
185 IRT_END
188 IRT_ENTRY(void, InterpreterRuntime::newarray(JavaThread* thread, BasicType type, jint size))
189 oop obj = oopFactory::new_typeArray(type, size, CHECK);
190 thread->set_vm_result(obj);
191 IRT_END
194 IRT_ENTRY(void, InterpreterRuntime::anewarray(JavaThread* thread, ConstantPool* pool, int index, jint size))
195 // Note: no oopHandle for pool & klass needed since they are not used
196 // anymore after new_objArray() and no GC can happen before.
197 // (This may have to change if this code changes!)
198 Klass* klass = pool->klass_at(index, CHECK);
199 objArrayOop obj = oopFactory::new_objArray(klass, size, CHECK);
200 thread->set_vm_result(obj);
201 IRT_END
204 IRT_ENTRY(void, InterpreterRuntime::multianewarray(JavaThread* thread, jint* first_size_address))
205 // We may want to pass in more arguments - could make this slightly faster
206 ConstantPool* constants = method(thread)->constants();
207 int i = get_index_u2(thread, Bytecodes::_multianewarray);
208 Klass* klass = constants->klass_at(i, CHECK);
209 int nof_dims = number_of_dimensions(thread);
210 assert(klass->is_klass(), "not a class");
211 assert(nof_dims >= 1, "multianewarray rank must be nonzero");
213 // We must create an array of jints to pass to multi_allocate.
214 ResourceMark rm(thread);
215 const int small_dims = 10;
216 jint dim_array[small_dims];
217 jint *dims = &dim_array[0];
218 if (nof_dims > small_dims) {
219 dims = (jint*) NEW_RESOURCE_ARRAY(jint, nof_dims);
220 }
221 for (int index = 0; index < nof_dims; index++) {
222 // offset from first_size_address is addressed as local[index]
223 int n = Interpreter::local_offset_in_bytes(index)/jintSize;
224 dims[index] = first_size_address[n];
225 }
226 oop obj = ArrayKlass::cast(klass)->multi_allocate(nof_dims, dims, CHECK);
227 thread->set_vm_result(obj);
228 IRT_END
231 IRT_ENTRY(void, InterpreterRuntime::register_finalizer(JavaThread* thread, oopDesc* obj))
232 assert(obj->is_oop(), "must be a valid oop");
233 assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
234 InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
235 IRT_END
238 // Quicken instance-of and check-cast bytecodes
239 IRT_ENTRY(void, InterpreterRuntime::quicken_io_cc(JavaThread* thread))
240 // Force resolving; quicken the bytecode
241 int which = get_index_u2(thread, Bytecodes::_checkcast);
242 ConstantPool* cpool = method(thread)->constants();
243 // We'd expect to assert that we're only here to quicken bytecodes, but in a multithreaded
244 // program we might have seen an unquick'd bytecode in the interpreter but have another
245 // thread quicken the bytecode before we get here.
246 // assert( cpool->tag_at(which).is_unresolved_klass(), "should only come here to quicken bytecodes" );
247 Klass* klass = cpool->klass_at(which, CHECK);
248 thread->set_vm_result_2(klass);
249 IRT_END
252 //------------------------------------------------------------------------------------------------------------------------
253 // Exceptions
255 void InterpreterRuntime::note_trap_inner(JavaThread* thread, int reason,
256 methodHandle trap_method, int trap_bci, TRAPS) {
257 if (trap_method.not_null()) {
258 MethodData* trap_mdo = trap_method->method_data();
259 if (trap_mdo == NULL) {
260 Method::build_interpreter_method_data(trap_method, THREAD);
261 if (HAS_PENDING_EXCEPTION) {
262 assert((PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())),
263 "we expect only an OOM error here");
264 CLEAR_PENDING_EXCEPTION;
265 }
266 trap_mdo = trap_method->method_data();
267 // and fall through...
268 }
269 if (trap_mdo != NULL) {
270 // Update per-method count of trap events. The interpreter
271 // is updating the MDO to simulate the effect of compiler traps.
272 Deoptimization::update_method_data_from_interpreter(trap_mdo, trap_bci, reason);
273 }
274 }
275 }
277 // Assume the compiler is (or will be) interested in this event.
278 // If necessary, create an MDO to hold the information, and record it.
279 void InterpreterRuntime::note_trap(JavaThread* thread, int reason, TRAPS) {
280 assert(ProfileTraps, "call me only if profiling");
281 methodHandle trap_method(thread, method(thread));
282 int trap_bci = trap_method->bci_from(bcp(thread));
283 note_trap_inner(thread, reason, trap_method, trap_bci, THREAD);
284 }
286 #ifdef CC_INTERP
287 // As legacy note_trap, but we have more arguments.
288 IRT_ENTRY(void, InterpreterRuntime::note_trap(JavaThread* thread, int reason, Method *method, int trap_bci))
289 methodHandle trap_method(method);
290 note_trap_inner(thread, reason, trap_method, trap_bci, THREAD);
291 IRT_END
293 // Class Deoptimization is not visible in BytecodeInterpreter, so we need a wrapper
294 // for each exception.
295 void InterpreterRuntime::note_nullCheck_trap(JavaThread* thread, Method *method, int trap_bci)
296 { if (ProfileTraps) note_trap(thread, Deoptimization::Reason_null_check, method, trap_bci); }
297 void InterpreterRuntime::note_div0Check_trap(JavaThread* thread, Method *method, int trap_bci)
298 { if (ProfileTraps) note_trap(thread, Deoptimization::Reason_div0_check, method, trap_bci); }
299 void InterpreterRuntime::note_rangeCheck_trap(JavaThread* thread, Method *method, int trap_bci)
300 { if (ProfileTraps) note_trap(thread, Deoptimization::Reason_range_check, method, trap_bci); }
301 void InterpreterRuntime::note_classCheck_trap(JavaThread* thread, Method *method, int trap_bci)
302 { if (ProfileTraps) note_trap(thread, Deoptimization::Reason_class_check, method, trap_bci); }
303 void InterpreterRuntime::note_arrayCheck_trap(JavaThread* thread, Method *method, int trap_bci)
304 { if (ProfileTraps) note_trap(thread, Deoptimization::Reason_array_check, method, trap_bci); }
305 #endif // CC_INTERP
308 static Handle get_preinitialized_exception(Klass* k, TRAPS) {
309 // get klass
310 InstanceKlass* klass = InstanceKlass::cast(k);
311 assert(klass->is_initialized(),
312 "this klass should have been initialized during VM initialization");
313 // create instance - do not call constructor since we may have no
314 // (java) stack space left (should assert constructor is empty)
315 Handle exception;
316 oop exception_oop = klass->allocate_instance(CHECK_(exception));
317 exception = Handle(THREAD, exception_oop);
318 if (StackTraceInThrowable) {
319 java_lang_Throwable::fill_in_stack_trace(exception);
320 }
321 return exception;
322 }
324 // Special handling for stack overflow: since we don't have any (java) stack
325 // space left we use the pre-allocated & pre-initialized StackOverflowError
326 // klass to create an stack overflow error instance. We do not call its
327 // constructor for the same reason (it is empty, anyway).
328 IRT_ENTRY(void, InterpreterRuntime::throw_StackOverflowError(JavaThread* thread))
329 Handle exception = get_preinitialized_exception(
330 SystemDictionary::StackOverflowError_klass(),
331 CHECK);
332 THROW_HANDLE(exception);
333 IRT_END
336 IRT_ENTRY(void, InterpreterRuntime::create_exception(JavaThread* thread, char* name, char* message))
337 // lookup exception klass
338 TempNewSymbol s = SymbolTable::new_symbol(name, CHECK);
339 if (ProfileTraps) {
340 if (s == vmSymbols::java_lang_ArithmeticException()) {
341 note_trap(thread, Deoptimization::Reason_div0_check, CHECK);
342 } else if (s == vmSymbols::java_lang_NullPointerException()) {
343 note_trap(thread, Deoptimization::Reason_null_check, CHECK);
344 }
345 }
346 // create exception
347 Handle exception = Exceptions::new_exception(thread, s, message);
348 thread->set_vm_result(exception());
349 IRT_END
352 IRT_ENTRY(void, InterpreterRuntime::create_klass_exception(JavaThread* thread, char* name, oopDesc* obj))
353 ResourceMark rm(thread);
354 const char* klass_name = obj->klass()->external_name();
355 // lookup exception klass
356 TempNewSymbol s = SymbolTable::new_symbol(name, CHECK);
357 if (ProfileTraps) {
358 note_trap(thread, Deoptimization::Reason_class_check, CHECK);
359 }
360 // create exception, with klass name as detail message
361 Handle exception = Exceptions::new_exception(thread, s, klass_name);
362 thread->set_vm_result(exception());
363 IRT_END
366 IRT_ENTRY(void, InterpreterRuntime::throw_ArrayIndexOutOfBoundsException(JavaThread* thread, char* name, jint index))
367 char message[jintAsStringSize];
368 // lookup exception klass
369 TempNewSymbol s = SymbolTable::new_symbol(name, CHECK);
370 if (ProfileTraps) {
371 note_trap(thread, Deoptimization::Reason_range_check, CHECK);
372 }
373 // create exception
374 sprintf(message, "%d", index);
375 THROW_MSG(s, message);
376 IRT_END
378 IRT_ENTRY(void, InterpreterRuntime::throw_ClassCastException(
379 JavaThread* thread, oopDesc* obj))
381 ResourceMark rm(thread);
382 char* message = SharedRuntime::generate_class_cast_message(
383 thread, obj->klass()->external_name());
385 if (ProfileTraps) {
386 note_trap(thread, Deoptimization::Reason_class_check, CHECK);
387 }
389 // create exception
390 THROW_MSG(vmSymbols::java_lang_ClassCastException(), message);
391 IRT_END
393 // exception_handler_for_exception(...) returns the continuation address,
394 // the exception oop (via TLS) and sets the bci/bcp for the continuation.
395 // The exception oop is returned to make sure it is preserved over GC (it
396 // is only on the stack if the exception was thrown explicitly via athrow).
397 // During this operation, the expression stack contains the values for the
398 // bci where the exception happened. If the exception was propagated back
399 // from a call, the expression stack contains the values for the bci at the
400 // invoke w/o arguments (i.e., as if one were inside the call).
401 IRT_ENTRY(address, InterpreterRuntime::exception_handler_for_exception(JavaThread* thread, oopDesc* exception))
403 Handle h_exception(thread, exception);
404 methodHandle h_method (thread, method(thread));
405 constantPoolHandle h_constants(thread, h_method->constants());
406 bool should_repeat;
407 int handler_bci;
408 int current_bci = bci(thread);
410 if (thread->frames_to_pop_failed_realloc() > 0) {
411 // Allocation of scalar replaced object used in this frame
412 // failed. Unconditionally pop the frame.
413 thread->dec_frames_to_pop_failed_realloc();
414 thread->set_vm_result(h_exception());
415 // If the method is synchronized we already unlocked the monitor
416 // during deoptimization so the interpreter needs to skip it when
417 // the frame is popped.
418 thread->set_do_not_unlock_if_synchronized(true);
419 #ifdef CC_INTERP
420 return (address) -1;
421 #else
422 return Interpreter::remove_activation_entry();
423 #endif
424 }
426 // Need to do this check first since when _do_not_unlock_if_synchronized
427 // is set, we don't want to trigger any classloading which may make calls
428 // into java, or surprisingly find a matching exception handler for bci 0
429 // since at this moment the method hasn't been "officially" entered yet.
430 if (thread->do_not_unlock_if_synchronized()) {
431 ResourceMark rm;
432 assert(current_bci == 0, "bci isn't zero for do_not_unlock_if_synchronized");
433 thread->set_vm_result(exception);
434 #ifdef CC_INTERP
435 return (address) -1;
436 #else
437 return Interpreter::remove_activation_entry();
438 #endif
439 }
441 do {
442 should_repeat = false;
444 // assertions
445 #ifdef ASSERT
446 assert(h_exception.not_null(), "NULL exceptions should be handled by athrow");
447 assert(h_exception->is_oop(), "just checking");
448 // Check that exception is a subclass of Throwable, otherwise we have a VerifyError
449 if (!(h_exception->is_a(SystemDictionary::Throwable_klass()))) {
450 if (ExitVMOnVerifyError) vm_exit(-1);
451 ShouldNotReachHere();
452 }
453 #endif
455 // tracing
456 if (TraceExceptions) {
457 ttyLocker ttyl;
458 ResourceMark rm(thread);
459 tty->print_cr("Exception <%s> (" INTPTR_FORMAT ")", h_exception->print_value_string(), (address)h_exception());
460 tty->print_cr(" thrown in interpreter method <%s>", h_method->print_value_string());
461 tty->print_cr(" at bci %d for thread " INTPTR_FORMAT, current_bci, thread);
462 }
463 // Don't go paging in something which won't be used.
464 // else if (extable->length() == 0) {
465 // // disabled for now - interpreter is not using shortcut yet
466 // // (shortcut is not to call runtime if we have no exception handlers)
467 // // warning("performance bug: should not call runtime if method has no exception handlers");
468 // }
469 // for AbortVMOnException flag
470 NOT_PRODUCT(Exceptions::debug_check_abort(h_exception));
472 // exception handler lookup
473 KlassHandle h_klass(THREAD, h_exception->klass());
474 handler_bci = Method::fast_exception_handler_bci_for(h_method, h_klass, current_bci, THREAD);
475 if (HAS_PENDING_EXCEPTION) {
476 // We threw an exception while trying to find the exception handler.
477 // Transfer the new exception to the exception handle which will
478 // be set into thread local storage, and do another lookup for an
479 // exception handler for this exception, this time starting at the
480 // BCI of the exception handler which caused the exception to be
481 // thrown (bug 4307310).
482 h_exception = Handle(THREAD, PENDING_EXCEPTION);
483 CLEAR_PENDING_EXCEPTION;
484 if (handler_bci >= 0) {
485 current_bci = handler_bci;
486 should_repeat = true;
487 }
488 }
489 } while (should_repeat == true);
491 // notify JVMTI of an exception throw; JVMTI will detect if this is a first
492 // time throw or a stack unwinding throw and accordingly notify the debugger
493 if (JvmtiExport::can_post_on_exceptions()) {
494 JvmtiExport::post_exception_throw(thread, h_method(), bcp(thread), h_exception());
495 }
497 #ifdef CC_INTERP
498 address continuation = (address)(intptr_t) handler_bci;
499 #else
500 address continuation = NULL;
501 #endif
502 address handler_pc = NULL;
503 if (handler_bci < 0 || !thread->reguard_stack((address) &continuation)) {
504 // Forward exception to callee (leaving bci/bcp untouched) because (a) no
505 // handler in this method, or (b) after a stack overflow there is not yet
506 // enough stack space available to reprotect the stack.
507 #ifndef CC_INTERP
508 continuation = Interpreter::remove_activation_entry();
509 #endif
510 // Count this for compilation purposes
511 h_method->interpreter_throwout_increment(THREAD);
512 } else {
513 // handler in this method => change bci/bcp to handler bci/bcp and continue there
514 handler_pc = h_method->code_base() + handler_bci;
515 #ifndef CC_INTERP
516 set_bcp_and_mdp(handler_pc, thread);
517 continuation = Interpreter::dispatch_table(vtos)[*handler_pc];
518 #endif
519 }
520 // notify debugger of an exception catch
521 // (this is good for exceptions caught in native methods as well)
522 if (JvmtiExport::can_post_on_exceptions()) {
523 JvmtiExport::notice_unwind_due_to_exception(thread, h_method(), handler_pc, h_exception(), (handler_pc != NULL));
524 }
526 thread->set_vm_result(h_exception());
527 return continuation;
528 IRT_END
531 IRT_ENTRY(void, InterpreterRuntime::throw_pending_exception(JavaThread* thread))
532 assert(thread->has_pending_exception(), "must only ne called if there's an exception pending");
533 // nothing to do - eventually we should remove this code entirely (see comments @ call sites)
534 IRT_END
537 IRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodError(JavaThread* thread))
538 THROW(vmSymbols::java_lang_AbstractMethodError());
539 IRT_END
542 IRT_ENTRY(void, InterpreterRuntime::throw_IncompatibleClassChangeError(JavaThread* thread))
543 THROW(vmSymbols::java_lang_IncompatibleClassChangeError());
544 IRT_END
547 //------------------------------------------------------------------------------------------------------------------------
548 // Fields
549 //
551 IRT_ENTRY(void, InterpreterRuntime::resolve_get_put(JavaThread* thread, Bytecodes::Code bytecode))
552 // resolve field
553 fieldDescriptor info;
554 constantPoolHandle pool(thread, method(thread)->constants());
555 bool is_put = (bytecode == Bytecodes::_putfield || bytecode == Bytecodes::_putstatic);
556 bool is_static = (bytecode == Bytecodes::_getstatic || bytecode == Bytecodes::_putstatic);
558 {
559 JvmtiHideSingleStepping jhss(thread);
560 LinkResolver::resolve_field_access(info, pool, get_index_u2_cpcache(thread, bytecode),
561 bytecode, CHECK);
562 } // end JvmtiHideSingleStepping
564 // check if link resolution caused cpCache to be updated
565 if (already_resolved(thread)) return;
567 // compute auxiliary field attributes
568 TosState state = as_TosState(info.field_type());
570 // We need to delay resolving put instructions on final fields
571 // until we actually invoke one. This is required so we throw
572 // exceptions at the correct place. If we do not resolve completely
573 // in the current pass, leaving the put_code set to zero will
574 // cause the next put instruction to reresolve.
575 Bytecodes::Code put_code = (Bytecodes::Code)0;
577 // We also need to delay resolving getstatic instructions until the
578 // class is intitialized. This is required so that access to the static
579 // field will call the initialization function every time until the class
580 // is completely initialized ala. in 2.17.5 in JVM Specification.
581 InstanceKlass* klass = InstanceKlass::cast(info.field_holder());
582 bool uninitialized_static = ((bytecode == Bytecodes::_getstatic || bytecode == Bytecodes::_putstatic) &&
583 !klass->is_initialized());
584 Bytecodes::Code get_code = (Bytecodes::Code)0;
586 if (!uninitialized_static) {
587 get_code = ((is_static) ? Bytecodes::_getstatic : Bytecodes::_getfield);
588 if (is_put || !info.access_flags().is_final()) {
589 put_code = ((is_static) ? Bytecodes::_putstatic : Bytecodes::_putfield);
590 }
591 }
593 cache_entry(thread)->set_field(
594 get_code,
595 put_code,
596 info.field_holder(),
597 info.index(),
598 info.offset(),
599 state,
600 info.access_flags().is_final(),
601 info.access_flags().is_volatile(),
602 pool->pool_holder()
603 );
604 IRT_END
607 //------------------------------------------------------------------------------------------------------------------------
608 // Synchronization
609 //
610 // The interpreter's synchronization code is factored out so that it can
611 // be shared by method invocation and synchronized blocks.
612 //%note synchronization_3
614 //%note monitor_1
615 IRT_ENTRY_NO_ASYNC(void, InterpreterRuntime::monitorenter(JavaThread* thread, BasicObjectLock* elem))
616 #ifdef ASSERT
617 thread->last_frame().interpreter_frame_verify_monitor(elem);
618 #endif
619 if (PrintBiasedLockingStatistics) {
620 Atomic::inc(BiasedLocking::slow_path_entry_count_addr());
621 }
622 Handle h_obj(thread, elem->obj());
623 assert(Universe::heap()->is_in_reserved_or_null(h_obj()),
624 "must be NULL or an object");
625 if (UseBiasedLocking) {
626 // Retry fast entry if bias is revoked to avoid unnecessary inflation
627 ObjectSynchronizer::fast_enter(h_obj, elem->lock(), true, CHECK);
628 } else {
629 ObjectSynchronizer::slow_enter(h_obj, elem->lock(), CHECK);
630 }
631 assert(Universe::heap()->is_in_reserved_or_null(elem->obj()),
632 "must be NULL or an object");
633 #ifdef ASSERT
634 thread->last_frame().interpreter_frame_verify_monitor(elem);
635 #endif
636 IRT_END
639 //%note monitor_1
640 IRT_ENTRY_NO_ASYNC(void, InterpreterRuntime::monitorexit(JavaThread* thread, BasicObjectLock* elem))
641 #ifdef ASSERT
642 thread->last_frame().interpreter_frame_verify_monitor(elem);
643 #endif
644 Handle h_obj(thread, elem->obj());
645 assert(Universe::heap()->is_in_reserved_or_null(h_obj()),
646 "must be NULL or an object");
647 if (elem == NULL || h_obj()->is_unlocked()) {
648 THROW(vmSymbols::java_lang_IllegalMonitorStateException());
649 }
650 ObjectSynchronizer::slow_exit(h_obj(), elem->lock(), thread);
651 // Free entry. This must be done here, since a pending exception might be installed on
652 // exit. If it is not cleared, the exception handling code will try to unlock the monitor again.
653 elem->set_obj(NULL);
654 #ifdef ASSERT
655 thread->last_frame().interpreter_frame_verify_monitor(elem);
656 #endif
657 IRT_END
660 IRT_ENTRY(void, InterpreterRuntime::throw_illegal_monitor_state_exception(JavaThread* thread))
661 THROW(vmSymbols::java_lang_IllegalMonitorStateException());
662 IRT_END
665 IRT_ENTRY(void, InterpreterRuntime::new_illegal_monitor_state_exception(JavaThread* thread))
666 // Returns an illegal exception to install into the current thread. The
667 // pending_exception flag is cleared so normal exception handling does not
668 // trigger. Any current installed exception will be overwritten. This
669 // method will be called during an exception unwind.
671 assert(!HAS_PENDING_EXCEPTION, "no pending exception");
672 Handle exception(thread, thread->vm_result());
673 assert(exception() != NULL, "vm result should be set");
674 thread->set_vm_result(NULL); // clear vm result before continuing (may cause memory leaks and assert failures)
675 if (!exception->is_a(SystemDictionary::ThreadDeath_klass())) {
676 exception = get_preinitialized_exception(
677 SystemDictionary::IllegalMonitorStateException_klass(),
678 CATCH);
679 }
680 thread->set_vm_result(exception());
681 IRT_END
684 //------------------------------------------------------------------------------------------------------------------------
685 // Invokes
687 IRT_ENTRY(Bytecodes::Code, InterpreterRuntime::get_original_bytecode_at(JavaThread* thread, Method* method, address bcp))
688 return method->orig_bytecode_at(method->bci_from(bcp));
689 IRT_END
691 IRT_ENTRY(void, InterpreterRuntime::set_original_bytecode_at(JavaThread* thread, Method* method, address bcp, Bytecodes::Code new_code))
692 method->set_orig_bytecode_at(method->bci_from(bcp), new_code);
693 IRT_END
695 IRT_ENTRY(void, InterpreterRuntime::_breakpoint(JavaThread* thread, Method* method, address bcp))
696 JvmtiExport::post_raw_breakpoint(thread, method, bcp);
697 IRT_END
699 IRT_ENTRY(void, InterpreterRuntime::resolve_invoke(JavaThread* thread, Bytecodes::Code bytecode)) {
700 // extract receiver from the outgoing argument list if necessary
701 Handle receiver(thread, NULL);
702 if (bytecode == Bytecodes::_invokevirtual || bytecode == Bytecodes::_invokeinterface) {
703 ResourceMark rm(thread);
704 methodHandle m (thread, method(thread));
705 Bytecode_invoke call(m, bci(thread));
706 Symbol* signature = call.signature();
707 receiver = Handle(thread,
708 thread->last_frame().interpreter_callee_receiver(signature));
709 assert(Universe::heap()->is_in_reserved_or_null(receiver()),
710 "sanity check");
711 assert(receiver.is_null() ||
712 !Universe::heap()->is_in_reserved(receiver->klass()),
713 "sanity check");
714 }
716 // resolve method
717 CallInfo info;
718 constantPoolHandle pool(thread, method(thread)->constants());
720 {
721 JvmtiHideSingleStepping jhss(thread);
722 LinkResolver::resolve_invoke(info, receiver, pool,
723 get_index_u2_cpcache(thread, bytecode), bytecode, CHECK);
724 if (JvmtiExport::can_hotswap_or_post_breakpoint()) {
725 int retry_count = 0;
726 while (info.resolved_method()->is_old()) {
727 // It is very unlikely that method is redefined more than 100 times
728 // in the middle of resolve. If it is looping here more than 100 times
729 // means then there could be a bug here.
730 guarantee((retry_count++ < 100),
731 "Could not resolve to latest version of redefined method");
732 // method is redefined in the middle of resolve so re-try.
733 LinkResolver::resolve_invoke(info, receiver, pool,
734 get_index_u2_cpcache(thread, bytecode), bytecode, CHECK);
735 }
736 }
737 } // end JvmtiHideSingleStepping
739 // check if link resolution caused cpCache to be updated
740 if (already_resolved(thread)) return;
742 if (bytecode == Bytecodes::_invokeinterface) {
743 if (TraceItables && Verbose) {
744 ResourceMark rm(thread);
745 tty->print_cr("Resolving: klass: %s to method: %s", info.resolved_klass()->name()->as_C_string(), info.resolved_method()->name()->as_C_string());
746 }
747 }
748 #ifdef ASSERT
749 if (bytecode == Bytecodes::_invokeinterface) {
750 if (info.resolved_method()->method_holder() ==
751 SystemDictionary::Object_klass()) {
752 // NOTE: THIS IS A FIX FOR A CORNER CASE in the JVM spec
753 // (see also CallInfo::set_interface for details)
754 assert(info.call_kind() == CallInfo::vtable_call ||
755 info.call_kind() == CallInfo::direct_call, "");
756 methodHandle rm = info.resolved_method();
757 assert(rm->is_final() || info.has_vtable_index(),
758 "should have been set already");
759 } else if (!info.resolved_method()->has_itable_index()) {
760 // Resolved something like CharSequence.toString. Use vtable not itable.
761 assert(info.call_kind() != CallInfo::itable_call, "");
762 } else {
763 // Setup itable entry
764 assert(info.call_kind() == CallInfo::itable_call, "");
765 int index = info.resolved_method()->itable_index();
766 assert(info.itable_index() == index, "");
767 }
768 } else {
769 assert(info.call_kind() == CallInfo::direct_call ||
770 info.call_kind() == CallInfo::vtable_call, "");
771 }
772 #endif
773 switch (info.call_kind()) {
774 case CallInfo::direct_call:
775 cache_entry(thread)->set_direct_call(
776 bytecode,
777 info.resolved_method());
778 break;
779 case CallInfo::vtable_call:
780 cache_entry(thread)->set_vtable_call(
781 bytecode,
782 info.resolved_method(),
783 info.vtable_index());
784 break;
785 case CallInfo::itable_call:
786 cache_entry(thread)->set_itable_call(
787 bytecode,
788 info.resolved_method(),
789 info.itable_index());
790 break;
791 default: ShouldNotReachHere();
792 }
793 }
794 IRT_END
797 // First time execution: Resolve symbols, create a permanent MethodType object.
798 IRT_ENTRY(void, InterpreterRuntime::resolve_invokehandle(JavaThread* thread)) {
799 assert(EnableInvokeDynamic, "");
800 const Bytecodes::Code bytecode = Bytecodes::_invokehandle;
802 // resolve method
803 CallInfo info;
804 constantPoolHandle pool(thread, method(thread)->constants());
806 {
807 JvmtiHideSingleStepping jhss(thread);
808 LinkResolver::resolve_invoke(info, Handle(), pool,
809 get_index_u2_cpcache(thread, bytecode), bytecode, CHECK);
810 } // end JvmtiHideSingleStepping
812 cache_entry(thread)->set_method_handle(pool, info);
813 }
814 IRT_END
817 // First time execution: Resolve symbols, create a permanent CallSite object.
818 IRT_ENTRY(void, InterpreterRuntime::resolve_invokedynamic(JavaThread* thread)) {
819 assert(EnableInvokeDynamic, "");
820 const Bytecodes::Code bytecode = Bytecodes::_invokedynamic;
822 //TO DO: consider passing BCI to Java.
823 // int caller_bci = method(thread)->bci_from(bcp(thread));
825 // resolve method
826 CallInfo info;
827 constantPoolHandle pool(thread, method(thread)->constants());
828 int index = get_index_u4(thread, bytecode);
829 {
830 JvmtiHideSingleStepping jhss(thread);
831 LinkResolver::resolve_invoke(info, Handle(), pool,
832 index, bytecode, CHECK);
833 } // end JvmtiHideSingleStepping
835 ConstantPoolCacheEntry* cp_cache_entry = pool->invokedynamic_cp_cache_entry_at(index);
836 cp_cache_entry->set_dynamic_call(pool, info);
837 }
838 IRT_END
841 //------------------------------------------------------------------------------------------------------------------------
842 // Miscellaneous
845 nmethod* InterpreterRuntime::frequency_counter_overflow(JavaThread* thread, address branch_bcp) {
846 nmethod* nm = frequency_counter_overflow_inner(thread, branch_bcp);
847 assert(branch_bcp != NULL || nm == NULL, "always returns null for non OSR requests");
848 if (branch_bcp != NULL && nm != NULL) {
849 // This was a successful request for an OSR nmethod. Because
850 // frequency_counter_overflow_inner ends with a safepoint check,
851 // nm could have been unloaded so look it up again. It's unsafe
852 // to examine nm directly since it might have been freed and used
853 // for something else.
854 frame fr = thread->last_frame();
855 Method* method = fr.interpreter_frame_method();
856 int bci = method->bci_from(fr.interpreter_frame_bcp());
857 nm = method->lookup_osr_nmethod_for(bci, CompLevel_none, false);
858 }
859 #ifndef PRODUCT
860 if (TraceOnStackReplacement) {
861 if (nm != NULL) {
862 tty->print("OSR entry @ pc: " INTPTR_FORMAT ": ", nm->osr_entry());
863 nm->print();
864 }
865 }
866 #endif
867 return nm;
868 }
870 IRT_ENTRY(nmethod*,
871 InterpreterRuntime::frequency_counter_overflow_inner(JavaThread* thread, address branch_bcp))
872 // use UnlockFlagSaver to clear and restore the _do_not_unlock_if_synchronized
873 // flag, in case this method triggers classloading which will call into Java.
874 UnlockFlagSaver fs(thread);
876 frame fr = thread->last_frame();
877 assert(fr.is_interpreted_frame(), "must come from interpreter");
878 methodHandle method(thread, fr.interpreter_frame_method());
879 const int branch_bci = branch_bcp != NULL ? method->bci_from(branch_bcp) : InvocationEntryBci;
880 const int bci = branch_bcp != NULL ? method->bci_from(fr.interpreter_frame_bcp()) : InvocationEntryBci;
882 assert(!HAS_PENDING_EXCEPTION, "Should not have any exceptions pending");
883 nmethod* osr_nm = CompilationPolicy::policy()->event(method, method, branch_bci, bci, CompLevel_none, NULL, thread);
884 assert(!HAS_PENDING_EXCEPTION, "Event handler should not throw any exceptions");
886 if (osr_nm != NULL) {
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 }
906 return osr_nm;
907 IRT_END
909 IRT_LEAF(jint, InterpreterRuntime::bcp_to_di(Method* method, address cur_bcp))
910 assert(ProfileInterpreter, "must be profiling interpreter");
911 int bci = method->bci_from(cur_bcp);
912 MethodData* mdo = method->method_data();
913 if (mdo == NULL) return 0;
914 return mdo->bci_to_di(bci);
915 IRT_END
917 IRT_ENTRY(void, InterpreterRuntime::profile_method(JavaThread* thread))
918 // use UnlockFlagSaver to clear and restore the _do_not_unlock_if_synchronized
919 // flag, in case this method triggers classloading which will call into Java.
920 UnlockFlagSaver fs(thread);
922 assert(ProfileInterpreter, "must be profiling interpreter");
923 frame fr = thread->last_frame();
924 assert(fr.is_interpreted_frame(), "must come from interpreter");
925 methodHandle method(thread, fr.interpreter_frame_method());
926 Method::build_interpreter_method_data(method, THREAD);
927 if (HAS_PENDING_EXCEPTION) {
928 assert((PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())), "we expect only an OOM error here");
929 CLEAR_PENDING_EXCEPTION;
930 // and fall through...
931 }
932 IRT_END
935 #ifdef ASSERT
936 IRT_LEAF(void, InterpreterRuntime::verify_mdp(Method* method, address bcp, address mdp))
937 assert(ProfileInterpreter, "must be profiling interpreter");
939 MethodData* mdo = method->method_data();
940 assert(mdo != NULL, "must not be null");
942 int bci = method->bci_from(bcp);
944 address mdp2 = mdo->bci_to_dp(bci);
945 if (mdp != mdp2) {
946 ResourceMark rm;
947 ResetNoHandleMark rnm; // In a LEAF entry.
948 HandleMark hm;
949 tty->print_cr("FAILED verify : actual mdp %p expected mdp %p @ bci %d", mdp, mdp2, bci);
950 int current_di = mdo->dp_to_di(mdp);
951 int expected_di = mdo->dp_to_di(mdp2);
952 tty->print_cr(" actual di %d expected di %d", current_di, expected_di);
953 int expected_approx_bci = mdo->data_at(expected_di)->bci();
954 int approx_bci = -1;
955 if (current_di >= 0) {
956 approx_bci = mdo->data_at(current_di)->bci();
957 }
958 tty->print_cr(" actual bci is %d expected bci %d", approx_bci, expected_approx_bci);
959 mdo->print_on(tty);
960 method->print_codes();
961 }
962 assert(mdp == mdp2, "wrong mdp");
963 IRT_END
964 #endif // ASSERT
966 IRT_ENTRY(void, InterpreterRuntime::update_mdp_for_ret(JavaThread* thread, int return_bci))
967 assert(ProfileInterpreter, "must be profiling interpreter");
968 ResourceMark rm(thread);
969 HandleMark hm(thread);
970 frame fr = thread->last_frame();
971 assert(fr.is_interpreted_frame(), "must come from interpreter");
972 MethodData* h_mdo = fr.interpreter_frame_method()->method_data();
974 // Grab a lock to ensure atomic access to setting the return bci and
975 // the displacement. This can block and GC, invalidating all naked oops.
976 MutexLocker ml(RetData_lock);
978 // ProfileData is essentially a wrapper around a derived oop, so we
979 // need to take the lock before making any ProfileData structures.
980 ProfileData* data = h_mdo->data_at(h_mdo->dp_to_di(fr.interpreter_frame_mdp()));
981 RetData* rdata = data->as_RetData();
982 address new_mdp = rdata->fixup_ret(return_bci, h_mdo);
983 fr.interpreter_frame_set_mdp(new_mdp);
984 IRT_END
986 IRT_ENTRY(MethodCounters*, InterpreterRuntime::build_method_counters(JavaThread* thread, Method* m))
987 MethodCounters* mcs = Method::build_method_counters(m, thread);
988 if (HAS_PENDING_EXCEPTION) {
989 assert((PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())), "we expect only an OOM error here");
990 CLEAR_PENDING_EXCEPTION;
991 }
992 return mcs;
993 IRT_END
996 IRT_ENTRY(void, InterpreterRuntime::at_safepoint(JavaThread* thread))
997 // We used to need an explict preserve_arguments here for invoke bytecodes. However,
998 // stack traversal automatically takes care of preserving arguments for invoke, so
999 // this is no longer needed.
1001 // IRT_END does an implicit safepoint check, hence we are guaranteed to block
1002 // if this is called during a safepoint
1004 if (JvmtiExport::should_post_single_step()) {
1005 // We are called during regular safepoints and when the VM is
1006 // single stepping. If any thread is marked for single stepping,
1007 // then we may have JVMTI work to do.
1008 JvmtiExport::at_single_stepping_point(thread, method(thread), bcp(thread));
1009 }
1010 IRT_END
1012 IRT_ENTRY(void, InterpreterRuntime::post_field_access(JavaThread *thread, oopDesc* obj,
1013 ConstantPoolCacheEntry *cp_entry))
1015 // check the access_flags for the field in the klass
1017 InstanceKlass* ik = InstanceKlass::cast(cp_entry->f1_as_klass());
1018 int index = cp_entry->field_index();
1019 if ((ik->field_access_flags(index) & JVM_ACC_FIELD_ACCESS_WATCHED) == 0) return;
1021 switch(cp_entry->flag_state()) {
1022 case btos: // fall through
1023 case ztos: // fall through
1024 case ctos: // fall through
1025 case stos: // fall through
1026 case itos: // fall through
1027 case ftos: // fall through
1028 case ltos: // fall through
1029 case dtos: // fall through
1030 case atos: break;
1031 default: ShouldNotReachHere(); return;
1032 }
1033 bool is_static = (obj == NULL);
1034 HandleMark hm(thread);
1036 Handle h_obj;
1037 if (!is_static) {
1038 // non-static field accessors have an object, but we need a handle
1039 h_obj = Handle(thread, obj);
1040 }
1041 instanceKlassHandle h_cp_entry_f1(thread, (Klass*)cp_entry->f1_as_klass());
1042 jfieldID fid = jfieldIDWorkaround::to_jfieldID(h_cp_entry_f1, cp_entry->f2_as_index(), is_static);
1043 JvmtiExport::post_field_access(thread, method(thread), bcp(thread), h_cp_entry_f1, h_obj, fid);
1044 IRT_END
1046 IRT_ENTRY(void, InterpreterRuntime::post_field_modification(JavaThread *thread,
1047 oopDesc* obj, ConstantPoolCacheEntry *cp_entry, jvalue *value))
1049 Klass* k = (Klass*)cp_entry->f1_as_klass();
1051 // check the access_flags for the field in the klass
1052 InstanceKlass* ik = InstanceKlass::cast(k);
1053 int index = cp_entry->field_index();
1054 // bail out if field modifications are not watched
1055 if ((ik->field_access_flags(index) & JVM_ACC_FIELD_MODIFICATION_WATCHED) == 0) return;
1057 char sig_type = '\0';
1059 switch(cp_entry->flag_state()) {
1060 case btos: sig_type = 'B'; break;
1061 case ztos: sig_type = 'Z'; break;
1062 case ctos: sig_type = 'C'; break;
1063 case stos: sig_type = 'S'; break;
1064 case itos: sig_type = 'I'; break;
1065 case ftos: sig_type = 'F'; break;
1066 case atos: sig_type = 'L'; break;
1067 case ltos: sig_type = 'J'; break;
1068 case dtos: sig_type = 'D'; break;
1069 default: ShouldNotReachHere(); return;
1070 }
1071 bool is_static = (obj == NULL);
1073 HandleMark hm(thread);
1074 instanceKlassHandle h_klass(thread, k);
1075 jfieldID fid = jfieldIDWorkaround::to_jfieldID(h_klass, cp_entry->f2_as_index(), is_static);
1076 jvalue fvalue;
1077 #ifdef _LP64
1078 fvalue = *value;
1079 #else
1080 // Long/double values are stored unaligned and also noncontiguously with
1081 // tagged stacks. We can't just do a simple assignment even in the non-
1082 // J/D cases because a C++ compiler is allowed to assume that a jvalue is
1083 // 8-byte aligned, and interpreter stack slots are only 4-byte aligned.
1084 // We assume that the two halves of longs/doubles are stored in interpreter
1085 // stack slots in platform-endian order.
1086 jlong_accessor u;
1087 jint* newval = (jint*)value;
1088 u.words[0] = newval[0];
1089 u.words[1] = newval[Interpreter::stackElementWords]; // skip if tag
1090 fvalue.j = u.long_value;
1091 #endif // _LP64
1093 Handle h_obj;
1094 if (!is_static) {
1095 // non-static field accessors have an object, but we need a handle
1096 h_obj = Handle(thread, obj);
1097 }
1099 JvmtiExport::post_raw_field_modification(thread, method(thread), bcp(thread), h_klass, h_obj,
1100 fid, sig_type, &fvalue);
1101 IRT_END
1103 IRT_ENTRY(void, InterpreterRuntime::post_method_entry(JavaThread *thread))
1104 JvmtiExport::post_method_entry(thread, InterpreterRuntime::method(thread), InterpreterRuntime::last_frame(thread));
1105 IRT_END
1108 IRT_ENTRY(void, InterpreterRuntime::post_method_exit(JavaThread *thread))
1109 JvmtiExport::post_method_exit(thread, InterpreterRuntime::method(thread), InterpreterRuntime::last_frame(thread));
1110 IRT_END
1112 IRT_LEAF(int, InterpreterRuntime::interpreter_contains(address pc))
1113 {
1114 return (Interpreter::contains(pc) ? 1 : 0);
1115 }
1116 IRT_END
1119 // Implementation of SignatureHandlerLibrary
1121 address SignatureHandlerLibrary::set_handler_blob() {
1122 BufferBlob* handler_blob = BufferBlob::create("native signature handlers", blob_size);
1123 if (handler_blob == NULL) {
1124 return NULL;
1125 }
1126 address handler = handler_blob->code_begin();
1127 _handler_blob = handler_blob;
1128 _handler = handler;
1129 return handler;
1130 }
1132 void SignatureHandlerLibrary::initialize() {
1133 if (_fingerprints != NULL) {
1134 return;
1135 }
1136 if (set_handler_blob() == NULL) {
1137 vm_exit_out_of_memory(blob_size, OOM_MALLOC_ERROR, "native signature handlers");
1138 }
1140 BufferBlob* bb = BufferBlob::create("Signature Handler Temp Buffer",
1141 SignatureHandlerLibrary::buffer_size);
1142 _buffer = bb->code_begin();
1144 _fingerprints = new(ResourceObj::C_HEAP, mtCode)GrowableArray<uint64_t>(32, true);
1145 _handlers = new(ResourceObj::C_HEAP, mtCode)GrowableArray<address>(32, true);
1146 }
1148 address SignatureHandlerLibrary::set_handler(CodeBuffer* buffer) {
1149 address handler = _handler;
1150 int insts_size = buffer->pure_insts_size();
1151 if (handler + insts_size > _handler_blob->code_end()) {
1152 // get a new handler blob
1153 handler = set_handler_blob();
1154 }
1155 if (handler != NULL) {
1156 memcpy(handler, buffer->insts_begin(), insts_size);
1157 pd_set_handler(handler);
1158 ICache::invalidate_range(handler, insts_size);
1159 _handler = handler + insts_size;
1160 }
1161 return handler;
1162 }
1164 void SignatureHandlerLibrary::add(methodHandle method) {
1165 if (method->signature_handler() == NULL) {
1166 // use slow signature handler if we can't do better
1167 int handler_index = -1;
1168 // check if we can use customized (fast) signature handler
1169 if (UseFastSignatureHandlers && method->size_of_parameters() <= Fingerprinter::max_size_of_parameters) {
1170 // use customized signature handler
1171 MutexLocker mu(SignatureHandlerLibrary_lock);
1172 // make sure data structure is initialized
1173 initialize();
1174 // lookup method signature's fingerprint
1175 uint64_t fingerprint = Fingerprinter(method).fingerprint();
1176 handler_index = _fingerprints->find(fingerprint);
1177 // create handler if necessary
1178 if (handler_index < 0) {
1179 ResourceMark rm;
1180 ptrdiff_t align_offset = (address)
1181 round_to((intptr_t)_buffer, CodeEntryAlignment) - (address)_buffer;
1182 CodeBuffer buffer((address)(_buffer + align_offset),
1183 SignatureHandlerLibrary::buffer_size - align_offset);
1184 InterpreterRuntime::SignatureHandlerGenerator(method, &buffer).generate(fingerprint);
1185 // copy into code heap
1186 address handler = set_handler(&buffer);
1187 if (handler == NULL) {
1188 // use slow signature handler
1189 } else {
1190 // debugging suppport
1191 if (PrintSignatureHandlers) {
1192 tty->cr();
1193 tty->print_cr("argument handler #%d for: %s %s (fingerprint = " UINT64_FORMAT ", %d bytes generated)",
1194 _handlers->length(),
1195 (method->is_static() ? "static" : "receiver"),
1196 method->name_and_sig_as_C_string(),
1197 fingerprint,
1198 buffer.insts_size());
1199 Disassembler::decode(handler, handler + buffer.insts_size());
1200 #ifndef PRODUCT
1201 tty->print_cr(" --- associated result handler ---");
1202 address rh_begin = Interpreter::result_handler(method()->result_type());
1203 address rh_end = rh_begin;
1204 while (*(int*)rh_end != 0) {
1205 rh_end += sizeof(int);
1206 }
1207 Disassembler::decode(rh_begin, rh_end);
1208 #endif
1209 }
1210 // add handler to library
1211 _fingerprints->append(fingerprint);
1212 _handlers->append(handler);
1213 // set handler index
1214 assert(_fingerprints->length() == _handlers->length(), "sanity check");
1215 handler_index = _fingerprints->length() - 1;
1216 }
1217 }
1218 // Set handler under SignatureHandlerLibrary_lock
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 } else {
1227 CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
1228 // use generic signature handler
1229 method->set_signature_handler(Interpreter::slow_signature_handler());
1230 }
1231 }
1232 #ifdef ASSERT
1233 int handler_index = -1;
1234 int fingerprint_index = -2;
1235 {
1236 // '_handlers' and '_fingerprints' are 'GrowableArray's and are NOT synchronized
1237 // in any way if accessed from multiple threads. To avoid races with another
1238 // thread which may change the arrays in the above, mutex protected block, we
1239 // have to protect this read access here with the same mutex as well!
1240 MutexLocker mu(SignatureHandlerLibrary_lock);
1241 if (_handlers != NULL) {
1242 handler_index = _handlers->find(method->signature_handler());
1243 fingerprint_index = _fingerprints->find(Fingerprinter(method).fingerprint());
1244 }
1245 }
1246 assert(method->signature_handler() == Interpreter::slow_signature_handler() ||
1247 handler_index == fingerprint_index, "sanity check");
1248 #endif // ASSERT
1249 }
1252 BufferBlob* SignatureHandlerLibrary::_handler_blob = NULL;
1253 address SignatureHandlerLibrary::_handler = NULL;
1254 GrowableArray<uint64_t>* SignatureHandlerLibrary::_fingerprints = NULL;
1255 GrowableArray<address>* SignatureHandlerLibrary::_handlers = NULL;
1256 address SignatureHandlerLibrary::_buffer = NULL;
1259 IRT_ENTRY(void, InterpreterRuntime::prepare_native_call(JavaThread* thread, Method* method))
1260 methodHandle m(thread, method);
1261 assert(m->is_native(), "sanity check");
1262 // lookup native function entry point if it doesn't exist
1263 bool in_base_library;
1264 if (!m->has_native_function()) {
1265 NativeLookup::lookup(m, in_base_library, CHECK);
1266 }
1267 // make sure signature handler is installed
1268 SignatureHandlerLibrary::add(m);
1269 // The interpreter entry point checks the signature handler first,
1270 // before trying to fetch the native entry point and klass mirror.
1271 // We must set the signature handler last, so that multiple processors
1272 // preparing the same method will be sure to see non-null entry & mirror.
1273 IRT_END
1275 #if defined(IA32) || defined(AMD64) || defined(ARM) || defined(MIPS64)
1276 IRT_LEAF(void, InterpreterRuntime::popframe_move_outgoing_args(JavaThread* thread, void* src_address, void* dest_address))
1277 if (src_address == dest_address) {
1278 return;
1279 }
1280 ResetNoHandleMark rnm; // In a LEAF entry.
1281 HandleMark hm;
1282 ResourceMark rm;
1283 frame fr = thread->last_frame();
1284 assert(fr.is_interpreted_frame(), "");
1285 jint bci = fr.interpreter_frame_bci();
1286 methodHandle mh(thread, fr.interpreter_frame_method());
1287 Bytecode_invoke invoke(mh, bci);
1288 ArgumentSizeComputer asc(invoke.signature());
1289 int size_of_arguments = (asc.size() + (invoke.has_receiver() ? 1 : 0)); // receiver
1290 Copy::conjoint_jbytes(src_address, dest_address,
1291 size_of_arguments * Interpreter::stackElementSize);
1292 IRT_END
1293 #endif
1295 #if INCLUDE_JVMTI
1296 // This is a support of the JVMTI PopFrame interface.
1297 // Make sure it is an invokestatic of a polymorphic intrinsic that has a member_name argument
1298 // and return it as a vm_result so that it can be reloaded in the list of invokestatic parameters.
1299 // The member_name argument is a saved reference (in local#0) to the member_name.
1300 // For backward compatibility with some JDK versions (7, 8) it can also be a direct method handle.
1301 // FIXME: remove DMH case after j.l.i.InvokerBytecodeGenerator code shape is updated.
1302 IRT_ENTRY(void, InterpreterRuntime::member_name_arg_or_null(JavaThread* thread, address member_name,
1303 Method* method, address bcp))
1304 Bytecodes::Code code = Bytecodes::code_at(method, bcp);
1305 if (code != Bytecodes::_invokestatic) {
1306 return;
1307 }
1308 ConstantPool* cpool = method->constants();
1309 int cp_index = Bytes::get_native_u2(bcp + 1) + ConstantPool::CPCACHE_INDEX_TAG;
1310 Symbol* cname = cpool->klass_name_at(cpool->klass_ref_index_at(cp_index));
1311 Symbol* mname = cpool->name_ref_at(cp_index);
1313 if (MethodHandles::has_member_arg(cname, mname)) {
1314 oop member_name_oop = (oop) member_name;
1315 if (java_lang_invoke_DirectMethodHandle::is_instance(member_name_oop)) {
1316 // FIXME: remove after j.l.i.InvokerBytecodeGenerator code shape is updated.
1317 member_name_oop = java_lang_invoke_DirectMethodHandle::member(member_name_oop);
1318 }
1319 thread->set_vm_result(member_name_oop);
1320 } else {
1321 thread->set_vm_result(NULL);
1322 }
1323 IRT_END
1324 #endif // INCLUDE_JVMTI