Mon, 28 May 2018 10:33:52 +0800
Merge
1 /*
2 * Copyright (c) 1997, 2017, 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 bytecode == Bytecodes::_invokespecial) {
704 ResourceMark rm(thread);
705 methodHandle m (thread, method(thread));
706 Bytecode_invoke call(m, bci(thread));
707 Symbol* signature = call.signature();
708 receiver = Handle(thread,
709 thread->last_frame().interpreter_callee_receiver(signature));
710 assert(Universe::heap()->is_in_reserved_or_null(receiver()),
711 "sanity check");
712 assert(receiver.is_null() ||
713 !Universe::heap()->is_in_reserved(receiver->klass()),
714 "sanity check");
715 }
717 // resolve method
718 CallInfo info;
719 constantPoolHandle pool(thread, method(thread)->constants());
721 {
722 JvmtiHideSingleStepping jhss(thread);
723 LinkResolver::resolve_invoke(info, receiver, pool,
724 get_index_u2_cpcache(thread, bytecode), bytecode, CHECK);
725 if (JvmtiExport::can_hotswap_or_post_breakpoint()) {
726 int retry_count = 0;
727 while (info.resolved_method()->is_old()) {
728 // It is very unlikely that method is redefined more than 100 times
729 // in the middle of resolve. If it is looping here more than 100 times
730 // means then there could be a bug here.
731 guarantee((retry_count++ < 100),
732 "Could not resolve to latest version of redefined method");
733 // method is redefined in the middle of resolve so re-try.
734 LinkResolver::resolve_invoke(info, receiver, pool,
735 get_index_u2_cpcache(thread, bytecode), bytecode, CHECK);
736 }
737 }
738 } // end JvmtiHideSingleStepping
740 // check if link resolution caused cpCache to be updated
741 if (already_resolved(thread)) return;
743 if (bytecode == Bytecodes::_invokeinterface) {
744 if (TraceItables && Verbose) {
745 ResourceMark rm(thread);
746 tty->print_cr("Resolving: klass: %s to method: %s", info.resolved_klass()->name()->as_C_string(), info.resolved_method()->name()->as_C_string());
747 }
748 }
749 #ifdef ASSERT
750 if (bytecode == Bytecodes::_invokeinterface) {
751 if (info.resolved_method()->method_holder() ==
752 SystemDictionary::Object_klass()) {
753 // NOTE: THIS IS A FIX FOR A CORNER CASE in the JVM spec
754 // (see also CallInfo::set_interface for details)
755 assert(info.call_kind() == CallInfo::vtable_call ||
756 info.call_kind() == CallInfo::direct_call, "");
757 methodHandle rm = info.resolved_method();
758 assert(rm->is_final() || info.has_vtable_index(),
759 "should have been set already");
760 } else if (!info.resolved_method()->has_itable_index()) {
761 // Resolved something like CharSequence.toString. Use vtable not itable.
762 assert(info.call_kind() != CallInfo::itable_call, "");
763 } else {
764 // Setup itable entry
765 assert(info.call_kind() == CallInfo::itable_call, "");
766 int index = info.resolved_method()->itable_index();
767 assert(info.itable_index() == index, "");
768 }
769 } else if (bytecode == Bytecodes::_invokespecial) {
770 assert(info.call_kind() == CallInfo::direct_call, "must be direct call");
771 } else {
772 assert(info.call_kind() == CallInfo::direct_call ||
773 info.call_kind() == CallInfo::vtable_call, "");
774 }
775 #endif
776 // Get sender or sender's host_klass, and only set cpCache entry to resolved if
777 // it is not an interface. The receiver for invokespecial calls within interface
778 // methods must be checked for every call.
779 InstanceKlass* sender = pool->pool_holder();
780 sender = sender->has_host_klass() ? InstanceKlass::cast(sender->host_klass()) : sender;
782 switch (info.call_kind()) {
783 case CallInfo::direct_call:
784 cache_entry(thread)->set_direct_call(
785 bytecode,
786 info.resolved_method(),
787 sender->is_interface());
788 break;
789 case CallInfo::vtable_call:
790 cache_entry(thread)->set_vtable_call(
791 bytecode,
792 info.resolved_method(),
793 info.vtable_index());
794 break;
795 case CallInfo::itable_call:
796 cache_entry(thread)->set_itable_call(
797 bytecode,
798 info.resolved_klass(),
799 info.resolved_method(),
800 info.itable_index());
801 break;
802 default: ShouldNotReachHere();
803 }
804 }
805 IRT_END
808 // First time execution: Resolve symbols, create a permanent MethodType object.
809 IRT_ENTRY(void, InterpreterRuntime::resolve_invokehandle(JavaThread* thread)) {
810 assert(EnableInvokeDynamic, "");
811 const Bytecodes::Code bytecode = Bytecodes::_invokehandle;
813 // resolve method
814 CallInfo info;
815 constantPoolHandle pool(thread, method(thread)->constants());
817 {
818 JvmtiHideSingleStepping jhss(thread);
819 LinkResolver::resolve_invoke(info, Handle(), pool,
820 get_index_u2_cpcache(thread, bytecode), bytecode, CHECK);
821 } // end JvmtiHideSingleStepping
823 cache_entry(thread)->set_method_handle(pool, info);
824 }
825 IRT_END
828 // First time execution: Resolve symbols, create a permanent CallSite object.
829 IRT_ENTRY(void, InterpreterRuntime::resolve_invokedynamic(JavaThread* thread)) {
830 assert(EnableInvokeDynamic, "");
831 const Bytecodes::Code bytecode = Bytecodes::_invokedynamic;
833 //TO DO: consider passing BCI to Java.
834 // int caller_bci = method(thread)->bci_from(bcp(thread));
836 // resolve method
837 CallInfo info;
838 constantPoolHandle pool(thread, method(thread)->constants());
839 int index = get_index_u4(thread, bytecode);
840 {
841 JvmtiHideSingleStepping jhss(thread);
842 LinkResolver::resolve_invoke(info, Handle(), pool,
843 index, bytecode, CHECK);
844 } // end JvmtiHideSingleStepping
846 ConstantPoolCacheEntry* cp_cache_entry = pool->invokedynamic_cp_cache_entry_at(index);
847 cp_cache_entry->set_dynamic_call(pool, info);
848 }
849 IRT_END
852 //------------------------------------------------------------------------------------------------------------------------
853 // Miscellaneous
856 nmethod* InterpreterRuntime::frequency_counter_overflow(JavaThread* thread, address branch_bcp) {
857 nmethod* nm = frequency_counter_overflow_inner(thread, branch_bcp);
858 assert(branch_bcp != NULL || nm == NULL, "always returns null for non OSR requests");
859 if (branch_bcp != NULL && nm != NULL) {
860 // This was a successful request for an OSR nmethod. Because
861 // frequency_counter_overflow_inner ends with a safepoint check,
862 // nm could have been unloaded so look it up again. It's unsafe
863 // to examine nm directly since it might have been freed and used
864 // for something else.
865 frame fr = thread->last_frame();
866 Method* method = fr.interpreter_frame_method();
867 int bci = method->bci_from(fr.interpreter_frame_bcp());
868 nm = method->lookup_osr_nmethod_for(bci, CompLevel_none, false);
869 }
870 #ifndef PRODUCT
871 if (TraceOnStackReplacement) {
872 if (nm != NULL) {
873 tty->print("OSR entry @ pc: " INTPTR_FORMAT ": ", nm->osr_entry());
874 nm->print();
875 }
876 }
877 #endif
878 return nm;
879 }
881 IRT_ENTRY(nmethod*,
882 InterpreterRuntime::frequency_counter_overflow_inner(JavaThread* thread, address branch_bcp))
883 // use UnlockFlagSaver to clear and restore the _do_not_unlock_if_synchronized
884 // flag, in case this method triggers classloading which will call into Java.
885 UnlockFlagSaver fs(thread);
887 frame fr = thread->last_frame();
888 assert(fr.is_interpreted_frame(), "must come from interpreter");
889 methodHandle method(thread, fr.interpreter_frame_method());
890 const int branch_bci = branch_bcp != NULL ? method->bci_from(branch_bcp) : InvocationEntryBci;
891 const int bci = branch_bcp != NULL ? method->bci_from(fr.interpreter_frame_bcp()) : InvocationEntryBci;
893 assert(!HAS_PENDING_EXCEPTION, "Should not have any exceptions pending");
894 nmethod* osr_nm = CompilationPolicy::policy()->event(method, method, branch_bci, bci, CompLevel_none, NULL, thread);
895 assert(!HAS_PENDING_EXCEPTION, "Event handler should not throw any exceptions");
897 if (osr_nm != NULL) {
898 // We may need to do on-stack replacement which requires that no
899 // monitors in the activation are biased because their
900 // BasicObjectLocks will need to migrate during OSR. Force
901 // unbiasing of all monitors in the activation now (even though
902 // the OSR nmethod might be invalidated) because we don't have a
903 // safepoint opportunity later once the migration begins.
904 if (UseBiasedLocking) {
905 ResourceMark rm;
906 GrowableArray<Handle>* objects_to_revoke = new GrowableArray<Handle>();
907 for( BasicObjectLock *kptr = fr.interpreter_frame_monitor_end();
908 kptr < fr.interpreter_frame_monitor_begin();
909 kptr = fr.next_monitor_in_interpreter_frame(kptr) ) {
910 if( kptr->obj() != NULL ) {
911 objects_to_revoke->append(Handle(THREAD, kptr->obj()));
912 }
913 }
914 BiasedLocking::revoke(objects_to_revoke);
915 }
916 }
917 return osr_nm;
918 IRT_END
920 IRT_LEAF(jint, InterpreterRuntime::bcp_to_di(Method* method, address cur_bcp))
921 assert(ProfileInterpreter, "must be profiling interpreter");
922 int bci = method->bci_from(cur_bcp);
923 MethodData* mdo = method->method_data();
924 if (mdo == NULL) return 0;
925 return mdo->bci_to_di(bci);
926 IRT_END
928 IRT_ENTRY(void, InterpreterRuntime::profile_method(JavaThread* thread))
929 // use UnlockFlagSaver to clear and restore the _do_not_unlock_if_synchronized
930 // flag, in case this method triggers classloading which will call into Java.
931 UnlockFlagSaver fs(thread);
933 assert(ProfileInterpreter, "must be profiling interpreter");
934 frame fr = thread->last_frame();
935 assert(fr.is_interpreted_frame(), "must come from interpreter");
936 methodHandle method(thread, fr.interpreter_frame_method());
937 Method::build_interpreter_method_data(method, THREAD);
938 if (HAS_PENDING_EXCEPTION) {
939 assert((PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())), "we expect only an OOM error here");
940 CLEAR_PENDING_EXCEPTION;
941 // and fall through...
942 }
943 IRT_END
946 #ifdef ASSERT
947 IRT_LEAF(void, InterpreterRuntime::verify_mdp(Method* method, address bcp, address mdp))
948 assert(ProfileInterpreter, "must be profiling interpreter");
950 MethodData* mdo = method->method_data();
951 assert(mdo != NULL, "must not be null");
953 int bci = method->bci_from(bcp);
955 address mdp2 = mdo->bci_to_dp(bci);
956 if (mdp != mdp2) {
957 ResourceMark rm;
958 ResetNoHandleMark rnm; // In a LEAF entry.
959 HandleMark hm;
960 tty->print_cr("FAILED verify : actual mdp %p expected mdp %p @ bci %d", mdp, mdp2, bci);
961 int current_di = mdo->dp_to_di(mdp);
962 int expected_di = mdo->dp_to_di(mdp2);
963 tty->print_cr(" actual di %d expected di %d", current_di, expected_di);
964 int expected_approx_bci = mdo->data_at(expected_di)->bci();
965 int approx_bci = -1;
966 if (current_di >= 0) {
967 approx_bci = mdo->data_at(current_di)->bci();
968 }
969 tty->print_cr(" actual bci is %d expected bci %d", approx_bci, expected_approx_bci);
970 mdo->print_on(tty);
971 method->print_codes();
972 }
973 assert(mdp == mdp2, "wrong mdp");
974 IRT_END
975 #endif // ASSERT
977 IRT_ENTRY(void, InterpreterRuntime::update_mdp_for_ret(JavaThread* thread, int return_bci))
978 assert(ProfileInterpreter, "must be profiling interpreter");
979 ResourceMark rm(thread);
980 HandleMark hm(thread);
981 frame fr = thread->last_frame();
982 assert(fr.is_interpreted_frame(), "must come from interpreter");
983 MethodData* h_mdo = fr.interpreter_frame_method()->method_data();
985 // Grab a lock to ensure atomic access to setting the return bci and
986 // the displacement. This can block and GC, invalidating all naked oops.
987 MutexLocker ml(RetData_lock);
989 // ProfileData is essentially a wrapper around a derived oop, so we
990 // need to take the lock before making any ProfileData structures.
991 ProfileData* data = h_mdo->data_at(h_mdo->dp_to_di(fr.interpreter_frame_mdp()));
992 guarantee(data != NULL, "profile data must be valid");
993 RetData* rdata = data->as_RetData();
994 address new_mdp = rdata->fixup_ret(return_bci, h_mdo);
995 fr.interpreter_frame_set_mdp(new_mdp);
996 IRT_END
998 IRT_ENTRY(MethodCounters*, InterpreterRuntime::build_method_counters(JavaThread* thread, Method* m))
999 MethodCounters* mcs = Method::build_method_counters(m, thread);
1000 if (HAS_PENDING_EXCEPTION) {
1001 assert((PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())), "we expect only an OOM error here");
1002 CLEAR_PENDING_EXCEPTION;
1003 }
1004 return mcs;
1005 IRT_END
1008 IRT_ENTRY(void, InterpreterRuntime::at_safepoint(JavaThread* thread))
1009 // We used to need an explict preserve_arguments here for invoke bytecodes. However,
1010 // stack traversal automatically takes care of preserving arguments for invoke, so
1011 // this is no longer needed.
1013 // IRT_END does an implicit safepoint check, hence we are guaranteed to block
1014 // if this is called during a safepoint
1016 if (JvmtiExport::should_post_single_step()) {
1017 // We are called during regular safepoints and when the VM is
1018 // single stepping. If any thread is marked for single stepping,
1019 // then we may have JVMTI work to do.
1020 JvmtiExport::at_single_stepping_point(thread, method(thread), bcp(thread));
1021 }
1022 IRT_END
1024 IRT_ENTRY(void, InterpreterRuntime::post_field_access(JavaThread *thread, oopDesc* obj,
1025 ConstantPoolCacheEntry *cp_entry))
1027 // check the access_flags for the field in the klass
1029 InstanceKlass* ik = InstanceKlass::cast(cp_entry->f1_as_klass());
1030 int index = cp_entry->field_index();
1031 if ((ik->field_access_flags(index) & JVM_ACC_FIELD_ACCESS_WATCHED) == 0) return;
1033 switch(cp_entry->flag_state()) {
1034 case btos: // fall through
1035 case ztos: // fall through
1036 case ctos: // fall through
1037 case stos: // fall through
1038 case itos: // fall through
1039 case ftos: // fall through
1040 case ltos: // fall through
1041 case dtos: // fall through
1042 case atos: break;
1043 default: ShouldNotReachHere(); return;
1044 }
1045 bool is_static = (obj == NULL);
1046 HandleMark hm(thread);
1048 Handle h_obj;
1049 if (!is_static) {
1050 // non-static field accessors have an object, but we need a handle
1051 h_obj = Handle(thread, obj);
1052 }
1053 instanceKlassHandle h_cp_entry_f1(thread, (Klass*)cp_entry->f1_as_klass());
1054 jfieldID fid = jfieldIDWorkaround::to_jfieldID(h_cp_entry_f1, cp_entry->f2_as_index(), is_static);
1055 JvmtiExport::post_field_access(thread, method(thread), bcp(thread), h_cp_entry_f1, h_obj, fid);
1056 IRT_END
1058 IRT_ENTRY(void, InterpreterRuntime::post_field_modification(JavaThread *thread,
1059 oopDesc* obj, ConstantPoolCacheEntry *cp_entry, jvalue *value))
1061 Klass* k = (Klass*)cp_entry->f1_as_klass();
1063 // check the access_flags for the field in the klass
1064 InstanceKlass* ik = InstanceKlass::cast(k);
1065 int index = cp_entry->field_index();
1066 // bail out if field modifications are not watched
1067 if ((ik->field_access_flags(index) & JVM_ACC_FIELD_MODIFICATION_WATCHED) == 0) return;
1069 char sig_type = '\0';
1071 switch(cp_entry->flag_state()) {
1072 case btos: sig_type = 'B'; break;
1073 case ztos: sig_type = 'Z'; break;
1074 case ctos: sig_type = 'C'; break;
1075 case stos: sig_type = 'S'; break;
1076 case itos: sig_type = 'I'; break;
1077 case ftos: sig_type = 'F'; break;
1078 case atos: sig_type = 'L'; break;
1079 case ltos: sig_type = 'J'; break;
1080 case dtos: sig_type = 'D'; break;
1081 default: ShouldNotReachHere(); return;
1082 }
1083 bool is_static = (obj == NULL);
1085 HandleMark hm(thread);
1086 instanceKlassHandle h_klass(thread, k);
1087 jfieldID fid = jfieldIDWorkaround::to_jfieldID(h_klass, cp_entry->f2_as_index(), is_static);
1088 jvalue fvalue;
1089 #ifdef _LP64
1090 fvalue = *value;
1091 #else
1092 // Long/double values are stored unaligned and also noncontiguously with
1093 // tagged stacks. We can't just do a simple assignment even in the non-
1094 // J/D cases because a C++ compiler is allowed to assume that a jvalue is
1095 // 8-byte aligned, and interpreter stack slots are only 4-byte aligned.
1096 // We assume that the two halves of longs/doubles are stored in interpreter
1097 // stack slots in platform-endian order.
1098 jlong_accessor u;
1099 jint* newval = (jint*)value;
1100 u.words[0] = newval[0];
1101 u.words[1] = newval[Interpreter::stackElementWords]; // skip if tag
1102 fvalue.j = u.long_value;
1103 #endif // _LP64
1105 Handle h_obj;
1106 if (!is_static) {
1107 // non-static field accessors have an object, but we need a handle
1108 h_obj = Handle(thread, obj);
1109 }
1111 JvmtiExport::post_raw_field_modification(thread, method(thread), bcp(thread), h_klass, h_obj,
1112 fid, sig_type, &fvalue);
1113 IRT_END
1115 IRT_ENTRY(void, InterpreterRuntime::post_method_entry(JavaThread *thread))
1116 JvmtiExport::post_method_entry(thread, InterpreterRuntime::method(thread), InterpreterRuntime::last_frame(thread));
1117 IRT_END
1120 IRT_ENTRY(void, InterpreterRuntime::post_method_exit(JavaThread *thread))
1121 JvmtiExport::post_method_exit(thread, InterpreterRuntime::method(thread), InterpreterRuntime::last_frame(thread));
1122 IRT_END
1124 IRT_LEAF(int, InterpreterRuntime::interpreter_contains(address pc))
1125 {
1126 return (Interpreter::contains(pc) ? 1 : 0);
1127 }
1128 IRT_END
1131 // Implementation of SignatureHandlerLibrary
1133 address SignatureHandlerLibrary::set_handler_blob() {
1134 BufferBlob* handler_blob = BufferBlob::create("native signature handlers", blob_size);
1135 if (handler_blob == NULL) {
1136 return NULL;
1137 }
1138 address handler = handler_blob->code_begin();
1139 _handler_blob = handler_blob;
1140 _handler = handler;
1141 return handler;
1142 }
1144 void SignatureHandlerLibrary::initialize() {
1145 if (_fingerprints != NULL) {
1146 return;
1147 }
1148 if (set_handler_blob() == NULL) {
1149 vm_exit_out_of_memory(blob_size, OOM_MALLOC_ERROR, "native signature handlers");
1150 }
1152 BufferBlob* bb = BufferBlob::create("Signature Handler Temp Buffer",
1153 SignatureHandlerLibrary::buffer_size);
1154 _buffer = bb->code_begin();
1156 _fingerprints = new(ResourceObj::C_HEAP, mtCode)GrowableArray<uint64_t>(32, true);
1157 _handlers = new(ResourceObj::C_HEAP, mtCode)GrowableArray<address>(32, true);
1158 }
1160 address SignatureHandlerLibrary::set_handler(CodeBuffer* buffer) {
1161 address handler = _handler;
1162 int insts_size = buffer->pure_insts_size();
1163 if (handler + insts_size > _handler_blob->code_end()) {
1164 // get a new handler blob
1165 handler = set_handler_blob();
1166 }
1167 if (handler != NULL) {
1168 memcpy(handler, buffer->insts_begin(), insts_size);
1169 pd_set_handler(handler);
1170 ICache::invalidate_range(handler, insts_size);
1171 _handler = handler + insts_size;
1172 }
1173 return handler;
1174 }
1176 void SignatureHandlerLibrary::add(methodHandle method) {
1177 if (method->signature_handler() == NULL) {
1178 // use slow signature handler if we can't do better
1179 int handler_index = -1;
1180 // check if we can use customized (fast) signature handler
1181 if (UseFastSignatureHandlers && method->size_of_parameters() <= Fingerprinter::max_size_of_parameters) {
1182 // use customized signature handler
1183 MutexLocker mu(SignatureHandlerLibrary_lock);
1184 // make sure data structure is initialized
1185 initialize();
1186 // lookup method signature's fingerprint
1187 uint64_t fingerprint = Fingerprinter(method).fingerprint();
1188 handler_index = _fingerprints->find(fingerprint);
1189 // create handler if necessary
1190 if (handler_index < 0) {
1191 ResourceMark rm;
1192 ptrdiff_t align_offset = (address)
1193 round_to((intptr_t)_buffer, CodeEntryAlignment) - (address)_buffer;
1194 CodeBuffer buffer((address)(_buffer + align_offset),
1195 SignatureHandlerLibrary::buffer_size - align_offset);
1196 InterpreterRuntime::SignatureHandlerGenerator(method, &buffer).generate(fingerprint);
1197 // copy into code heap
1198 address handler = set_handler(&buffer);
1199 if (handler == NULL) {
1200 // use slow signature handler
1201 } else {
1202 // debugging suppport
1203 if (PrintSignatureHandlers) {
1204 tty->cr();
1205 tty->print_cr("argument handler #%d for: %s %s (fingerprint = " UINT64_FORMAT ", %d bytes generated)",
1206 _handlers->length(),
1207 (method->is_static() ? "static" : "receiver"),
1208 method->name_and_sig_as_C_string(),
1209 fingerprint,
1210 buffer.insts_size());
1211 Disassembler::decode(handler, handler + buffer.insts_size());
1212 #ifndef PRODUCT
1213 tty->print_cr(" --- associated result handler ---");
1214 address rh_begin = Interpreter::result_handler(method()->result_type());
1215 address rh_end = rh_begin;
1216 while (*(int*)rh_end != 0) {
1217 rh_end += sizeof(int);
1218 }
1219 Disassembler::decode(rh_begin, rh_end);
1220 #endif
1221 }
1222 // add handler to library
1223 _fingerprints->append(fingerprint);
1224 _handlers->append(handler);
1225 // set handler index
1226 assert(_fingerprints->length() == _handlers->length(), "sanity check");
1227 handler_index = _fingerprints->length() - 1;
1228 }
1229 }
1230 // Set handler under SignatureHandlerLibrary_lock
1231 if (handler_index < 0) {
1232 // use generic signature handler
1233 method->set_signature_handler(Interpreter::slow_signature_handler());
1234 } else {
1235 // set handler
1236 method->set_signature_handler(_handlers->at(handler_index));
1237 }
1238 } else {
1239 CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
1240 // use generic signature handler
1241 method->set_signature_handler(Interpreter::slow_signature_handler());
1242 }
1243 }
1244 #ifdef ASSERT
1245 int handler_index = -1;
1246 int fingerprint_index = -2;
1247 {
1248 // '_handlers' and '_fingerprints' are 'GrowableArray's and are NOT synchronized
1249 // in any way if accessed from multiple threads. To avoid races with another
1250 // thread which may change the arrays in the above, mutex protected block, we
1251 // have to protect this read access here with the same mutex as well!
1252 MutexLocker mu(SignatureHandlerLibrary_lock);
1253 if (_handlers != NULL) {
1254 handler_index = _handlers->find(method->signature_handler());
1255 fingerprint_index = _fingerprints->find(Fingerprinter(method).fingerprint());
1256 }
1257 }
1258 assert(method->signature_handler() == Interpreter::slow_signature_handler() ||
1259 handler_index == fingerprint_index, "sanity check");
1260 #endif // ASSERT
1261 }
1264 BufferBlob* SignatureHandlerLibrary::_handler_blob = NULL;
1265 address SignatureHandlerLibrary::_handler = NULL;
1266 GrowableArray<uint64_t>* SignatureHandlerLibrary::_fingerprints = NULL;
1267 GrowableArray<address>* SignatureHandlerLibrary::_handlers = NULL;
1268 address SignatureHandlerLibrary::_buffer = NULL;
1271 IRT_ENTRY(void, InterpreterRuntime::prepare_native_call(JavaThread* thread, Method* method))
1272 methodHandle m(thread, method);
1273 assert(m->is_native(), "sanity check");
1274 // lookup native function entry point if it doesn't exist
1275 bool in_base_library;
1276 if (!m->has_native_function()) {
1277 NativeLookup::lookup(m, in_base_library, CHECK);
1278 }
1279 // make sure signature handler is installed
1280 SignatureHandlerLibrary::add(m);
1281 // The interpreter entry point checks the signature handler first,
1282 // before trying to fetch the native entry point and klass mirror.
1283 // We must set the signature handler last, so that multiple processors
1284 // preparing the same method will be sure to see non-null entry & mirror.
1285 IRT_END
1287 #if defined(IA32) || defined(AMD64) || defined(ARM) || defined(MIPS64)
1288 IRT_LEAF(void, InterpreterRuntime::popframe_move_outgoing_args(JavaThread* thread, void* src_address, void* dest_address))
1289 if (src_address == dest_address) {
1290 return;
1291 }
1292 ResetNoHandleMark rnm; // In a LEAF entry.
1293 HandleMark hm;
1294 ResourceMark rm;
1295 frame fr = thread->last_frame();
1296 assert(fr.is_interpreted_frame(), "");
1297 jint bci = fr.interpreter_frame_bci();
1298 methodHandle mh(thread, fr.interpreter_frame_method());
1299 Bytecode_invoke invoke(mh, bci);
1300 ArgumentSizeComputer asc(invoke.signature());
1301 int size_of_arguments = (asc.size() + (invoke.has_receiver() ? 1 : 0)); // receiver
1302 Copy::conjoint_jbytes(src_address, dest_address,
1303 size_of_arguments * Interpreter::stackElementSize);
1304 IRT_END
1305 #endif
1307 #if INCLUDE_JVMTI
1308 // This is a support of the JVMTI PopFrame interface.
1309 // Make sure it is an invokestatic of a polymorphic intrinsic that has a member_name argument
1310 // and return it as a vm_result so that it can be reloaded in the list of invokestatic parameters.
1311 // The member_name argument is a saved reference (in local#0) to the member_name.
1312 // For backward compatibility with some JDK versions (7, 8) it can also be a direct method handle.
1313 // FIXME: remove DMH case after j.l.i.InvokerBytecodeGenerator code shape is updated.
1314 IRT_ENTRY(void, InterpreterRuntime::member_name_arg_or_null(JavaThread* thread, address member_name,
1315 Method* method, address bcp))
1316 Bytecodes::Code code = Bytecodes::code_at(method, bcp);
1317 if (code != Bytecodes::_invokestatic) {
1318 return;
1319 }
1320 ConstantPool* cpool = method->constants();
1321 int cp_index = Bytes::get_native_u2(bcp + 1) + ConstantPool::CPCACHE_INDEX_TAG;
1322 Symbol* cname = cpool->klass_name_at(cpool->klass_ref_index_at(cp_index));
1323 Symbol* mname = cpool->name_ref_at(cp_index);
1325 if (MethodHandles::has_member_arg(cname, mname)) {
1326 oop member_name_oop = (oop) member_name;
1327 if (java_lang_invoke_DirectMethodHandle::is_instance(member_name_oop)) {
1328 // FIXME: remove after j.l.i.InvokerBytecodeGenerator code shape is updated.
1329 member_name_oop = java_lang_invoke_DirectMethodHandle::member(member_name_oop);
1330 }
1331 thread->set_vm_result(member_name_oop);
1332 } else {
1333 thread->set_vm_result(NULL);
1334 }
1335 IRT_END
1336 #endif // INCLUDE_JVMTI