Wed, 13 Apr 2011 17:56:43 -0700
7035117: G1: nsk/stress/jni/jnistress002 fails with assertion failure
Summary: Allow long type for offset in G1 code in compiler implementations of Unsafe.getObject
Reviewed-by: never, iveresov
1 /*
2 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
25 #include "precompiled.hpp"
26 #include "asm/codeBuffer.hpp"
27 #include "c1/c1_CodeStubs.hpp"
28 #include "c1/c1_Defs.hpp"
29 #include "c1/c1_FrameMap.hpp"
30 #include "c1/c1_LIRAssembler.hpp"
31 #include "c1/c1_MacroAssembler.hpp"
32 #include "c1/c1_Runtime1.hpp"
33 #include "classfile/systemDictionary.hpp"
34 #include "classfile/vmSymbols.hpp"
35 #include "code/codeBlob.hpp"
36 #include "code/compiledIC.hpp"
37 #include "code/pcDesc.hpp"
38 #include "code/scopeDesc.hpp"
39 #include "code/vtableStubs.hpp"
40 #include "compiler/disassembler.hpp"
41 #include "gc_interface/collectedHeap.hpp"
42 #include "interpreter/bytecode.hpp"
43 #include "interpreter/interpreter.hpp"
44 #include "memory/allocation.inline.hpp"
45 #include "memory/barrierSet.hpp"
46 #include "memory/oopFactory.hpp"
47 #include "memory/resourceArea.hpp"
48 #include "oops/objArrayKlass.hpp"
49 #include "oops/oop.inline.hpp"
50 #include "runtime/biasedLocking.hpp"
51 #include "runtime/compilationPolicy.hpp"
52 #include "runtime/interfaceSupport.hpp"
53 #include "runtime/javaCalls.hpp"
54 #include "runtime/sharedRuntime.hpp"
55 #include "runtime/threadCritical.hpp"
56 #include "runtime/vframe.hpp"
57 #include "runtime/vframeArray.hpp"
58 #include "utilities/copy.hpp"
59 #include "utilities/events.hpp"
62 // Implementation of StubAssembler
64 StubAssembler::StubAssembler(CodeBuffer* code, const char * name, int stub_id) : C1_MacroAssembler(code) {
65 _name = name;
66 _must_gc_arguments = false;
67 _frame_size = no_frame_size;
68 _num_rt_args = 0;
69 _stub_id = stub_id;
70 }
73 void StubAssembler::set_info(const char* name, bool must_gc_arguments) {
74 _name = name;
75 _must_gc_arguments = must_gc_arguments;
76 }
79 void StubAssembler::set_frame_size(int size) {
80 if (_frame_size == no_frame_size) {
81 _frame_size = size;
82 }
83 assert(_frame_size == size, "can't change the frame size");
84 }
87 void StubAssembler::set_num_rt_args(int args) {
88 if (_num_rt_args == 0) {
89 _num_rt_args = args;
90 }
91 assert(_num_rt_args == args, "can't change the number of args");
92 }
94 // Implementation of Runtime1
96 CodeBlob* Runtime1::_blobs[Runtime1::number_of_ids];
97 const char *Runtime1::_blob_names[] = {
98 RUNTIME1_STUBS(STUB_NAME, LAST_STUB_NAME)
99 };
101 #ifndef PRODUCT
102 // statistics
103 int Runtime1::_generic_arraycopy_cnt = 0;
104 int Runtime1::_primitive_arraycopy_cnt = 0;
105 int Runtime1::_oop_arraycopy_cnt = 0;
106 int Runtime1::_arraycopy_slowcase_cnt = 0;
107 int Runtime1::_new_type_array_slowcase_cnt = 0;
108 int Runtime1::_new_object_array_slowcase_cnt = 0;
109 int Runtime1::_new_instance_slowcase_cnt = 0;
110 int Runtime1::_new_multi_array_slowcase_cnt = 0;
111 int Runtime1::_monitorenter_slowcase_cnt = 0;
112 int Runtime1::_monitorexit_slowcase_cnt = 0;
113 int Runtime1::_patch_code_slowcase_cnt = 0;
114 int Runtime1::_throw_range_check_exception_count = 0;
115 int Runtime1::_throw_index_exception_count = 0;
116 int Runtime1::_throw_div0_exception_count = 0;
117 int Runtime1::_throw_null_pointer_exception_count = 0;
118 int Runtime1::_throw_class_cast_exception_count = 0;
119 int Runtime1::_throw_incompatible_class_change_error_count = 0;
120 int Runtime1::_throw_array_store_exception_count = 0;
121 int Runtime1::_throw_count = 0;
122 #endif
124 // Simple helper to see if the caller of a runtime stub which
125 // entered the VM has been deoptimized
127 static bool caller_is_deopted() {
128 JavaThread* thread = JavaThread::current();
129 RegisterMap reg_map(thread, false);
130 frame runtime_frame = thread->last_frame();
131 frame caller_frame = runtime_frame.sender(®_map);
132 assert(caller_frame.is_compiled_frame(), "must be compiled");
133 return caller_frame.is_deoptimized_frame();
134 }
136 // Stress deoptimization
137 static void deopt_caller() {
138 if ( !caller_is_deopted()) {
139 JavaThread* thread = JavaThread::current();
140 RegisterMap reg_map(thread, false);
141 frame runtime_frame = thread->last_frame();
142 frame caller_frame = runtime_frame.sender(®_map);
143 Deoptimization::deoptimize_frame(thread, caller_frame.id());
144 assert(caller_is_deopted(), "Must be deoptimized");
145 }
146 }
149 void Runtime1::generate_blob_for(BufferBlob* buffer_blob, StubID id) {
150 assert(0 <= id && id < number_of_ids, "illegal stub id");
151 ResourceMark rm;
152 // create code buffer for code storage
153 CodeBuffer code(buffer_blob);
155 Compilation::setup_code_buffer(&code, 0);
157 // create assembler for code generation
158 StubAssembler* sasm = new StubAssembler(&code, name_for(id), id);
159 // generate code for runtime stub
160 OopMapSet* oop_maps;
161 oop_maps = generate_code_for(id, sasm);
162 assert(oop_maps == NULL || sasm->frame_size() != no_frame_size,
163 "if stub has an oop map it must have a valid frame size");
165 #ifdef ASSERT
166 // Make sure that stubs that need oopmaps have them
167 switch (id) {
168 // These stubs don't need to have an oopmap
169 case dtrace_object_alloc_id:
170 case g1_pre_barrier_slow_id:
171 case g1_post_barrier_slow_id:
172 case slow_subtype_check_id:
173 case fpu2long_stub_id:
174 case unwind_exception_id:
175 case counter_overflow_id:
176 #if defined(SPARC) || defined(PPC)
177 case handle_exception_nofpu_id: // Unused on sparc
178 #endif
179 break;
181 // All other stubs should have oopmaps
182 default:
183 assert(oop_maps != NULL, "must have an oopmap");
184 }
185 #endif
187 // align so printing shows nop's instead of random code at the end (SimpleStubs are aligned)
188 sasm->align(BytesPerWord);
189 // make sure all code is in code buffer
190 sasm->flush();
191 // create blob - distinguish a few special cases
192 CodeBlob* blob = RuntimeStub::new_runtime_stub(name_for(id),
193 &code,
194 CodeOffsets::frame_never_safe,
195 sasm->frame_size(),
196 oop_maps,
197 sasm->must_gc_arguments());
198 // install blob
199 assert(blob != NULL, "blob must exist");
200 _blobs[id] = blob;
201 }
204 void Runtime1::initialize(BufferBlob* blob) {
205 // platform-dependent initialization
206 initialize_pd();
207 // generate stubs
208 for (int id = 0; id < number_of_ids; id++) generate_blob_for(blob, (StubID)id);
209 // printing
210 #ifndef PRODUCT
211 if (PrintSimpleStubs) {
212 ResourceMark rm;
213 for (int id = 0; id < number_of_ids; id++) {
214 _blobs[id]->print();
215 if (_blobs[id]->oop_maps() != NULL) {
216 _blobs[id]->oop_maps()->print();
217 }
218 }
219 }
220 #endif
221 }
224 CodeBlob* Runtime1::blob_for(StubID id) {
225 assert(0 <= id && id < number_of_ids, "illegal stub id");
226 return _blobs[id];
227 }
230 const char* Runtime1::name_for(StubID id) {
231 assert(0 <= id && id < number_of_ids, "illegal stub id");
232 return _blob_names[id];
233 }
235 const char* Runtime1::name_for_address(address entry) {
236 for (int id = 0; id < number_of_ids; id++) {
237 if (entry == entry_for((StubID)id)) return name_for((StubID)id);
238 }
240 #define FUNCTION_CASE(a, f) \
241 if ((intptr_t)a == CAST_FROM_FN_PTR(intptr_t, f)) return #f
243 FUNCTION_CASE(entry, os::javaTimeMillis);
244 FUNCTION_CASE(entry, os::javaTimeNanos);
245 FUNCTION_CASE(entry, SharedRuntime::OSR_migration_end);
246 FUNCTION_CASE(entry, SharedRuntime::d2f);
247 FUNCTION_CASE(entry, SharedRuntime::d2i);
248 FUNCTION_CASE(entry, SharedRuntime::d2l);
249 FUNCTION_CASE(entry, SharedRuntime::dcos);
250 FUNCTION_CASE(entry, SharedRuntime::dexp);
251 FUNCTION_CASE(entry, SharedRuntime::dlog);
252 FUNCTION_CASE(entry, SharedRuntime::dlog10);
253 FUNCTION_CASE(entry, SharedRuntime::dpow);
254 FUNCTION_CASE(entry, SharedRuntime::drem);
255 FUNCTION_CASE(entry, SharedRuntime::dsin);
256 FUNCTION_CASE(entry, SharedRuntime::dtan);
257 FUNCTION_CASE(entry, SharedRuntime::f2i);
258 FUNCTION_CASE(entry, SharedRuntime::f2l);
259 FUNCTION_CASE(entry, SharedRuntime::frem);
260 FUNCTION_CASE(entry, SharedRuntime::l2d);
261 FUNCTION_CASE(entry, SharedRuntime::l2f);
262 FUNCTION_CASE(entry, SharedRuntime::ldiv);
263 FUNCTION_CASE(entry, SharedRuntime::lmul);
264 FUNCTION_CASE(entry, SharedRuntime::lrem);
265 FUNCTION_CASE(entry, SharedRuntime::lrem);
266 FUNCTION_CASE(entry, SharedRuntime::dtrace_method_entry);
267 FUNCTION_CASE(entry, SharedRuntime::dtrace_method_exit);
268 FUNCTION_CASE(entry, trace_block_entry);
270 #undef FUNCTION_CASE
272 // Soft float adds more runtime names.
273 return pd_name_for_address(entry);
274 }
277 JRT_ENTRY(void, Runtime1::new_instance(JavaThread* thread, klassOopDesc* klass))
278 NOT_PRODUCT(_new_instance_slowcase_cnt++;)
280 assert(oop(klass)->is_klass(), "not a class");
281 instanceKlassHandle h(thread, klass);
282 h->check_valid_for_instantiation(true, CHECK);
283 // make sure klass is initialized
284 h->initialize(CHECK);
285 // allocate instance and return via TLS
286 oop obj = h->allocate_instance(CHECK);
287 thread->set_vm_result(obj);
288 JRT_END
291 JRT_ENTRY(void, Runtime1::new_type_array(JavaThread* thread, klassOopDesc* klass, jint length))
292 NOT_PRODUCT(_new_type_array_slowcase_cnt++;)
293 // Note: no handle for klass needed since they are not used
294 // anymore after new_typeArray() and no GC can happen before.
295 // (This may have to change if this code changes!)
296 assert(oop(klass)->is_klass(), "not a class");
297 BasicType elt_type = typeArrayKlass::cast(klass)->element_type();
298 oop obj = oopFactory::new_typeArray(elt_type, length, CHECK);
299 thread->set_vm_result(obj);
300 // This is pretty rare but this runtime patch is stressful to deoptimization
301 // if we deoptimize here so force a deopt to stress the path.
302 if (DeoptimizeALot) {
303 deopt_caller();
304 }
306 JRT_END
309 JRT_ENTRY(void, Runtime1::new_object_array(JavaThread* thread, klassOopDesc* array_klass, jint length))
310 NOT_PRODUCT(_new_object_array_slowcase_cnt++;)
312 // Note: no handle for klass needed since they are not used
313 // anymore after new_objArray() and no GC can happen before.
314 // (This may have to change if this code changes!)
315 assert(oop(array_klass)->is_klass(), "not a class");
316 klassOop elem_klass = objArrayKlass::cast(array_klass)->element_klass();
317 objArrayOop obj = oopFactory::new_objArray(elem_klass, length, CHECK);
318 thread->set_vm_result(obj);
319 // This is pretty rare but this runtime patch is stressful to deoptimization
320 // if we deoptimize here so force a deopt to stress the path.
321 if (DeoptimizeALot) {
322 deopt_caller();
323 }
324 JRT_END
327 JRT_ENTRY(void, Runtime1::new_multi_array(JavaThread* thread, klassOopDesc* klass, int rank, jint* dims))
328 NOT_PRODUCT(_new_multi_array_slowcase_cnt++;)
330 assert(oop(klass)->is_klass(), "not a class");
331 assert(rank >= 1, "rank must be nonzero");
332 oop obj = arrayKlass::cast(klass)->multi_allocate(rank, dims, CHECK);
333 thread->set_vm_result(obj);
334 JRT_END
337 JRT_ENTRY(void, Runtime1::unimplemented_entry(JavaThread* thread, StubID id))
338 tty->print_cr("Runtime1::entry_for(%d) returned unimplemented entry point", id);
339 JRT_END
342 JRT_ENTRY(void, Runtime1::throw_array_store_exception(JavaThread* thread, oopDesc* obj))
343 ResourceMark rm(thread);
344 const char* klass_name = Klass::cast(obj->klass())->external_name();
345 SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_ArrayStoreException(), klass_name);
346 JRT_END
349 JRT_ENTRY(void, Runtime1::post_jvmti_exception_throw(JavaThread* thread))
350 if (JvmtiExport::can_post_on_exceptions()) {
351 vframeStream vfst(thread, true);
352 address bcp = vfst.method()->bcp_from(vfst.bci());
353 JvmtiExport::post_exception_throw(thread, vfst.method(), bcp, thread->exception_oop());
354 }
355 JRT_END
357 // This is a helper to allow us to safepoint but allow the outer entry
358 // to be safepoint free if we need to do an osr
359 static nmethod* counter_overflow_helper(JavaThread* THREAD, int branch_bci, methodOopDesc* m) {
360 nmethod* osr_nm = NULL;
361 methodHandle method(THREAD, m);
363 RegisterMap map(THREAD, false);
364 frame fr = THREAD->last_frame().sender(&map);
365 nmethod* nm = (nmethod*) fr.cb();
366 assert(nm!= NULL && nm->is_nmethod(), "Sanity check");
367 methodHandle enclosing_method(THREAD, nm->method());
369 CompLevel level = (CompLevel)nm->comp_level();
370 int bci = InvocationEntryBci;
371 if (branch_bci != InvocationEntryBci) {
372 // Compute desination bci
373 address pc = method()->code_base() + branch_bci;
374 Bytecodes::Code branch = Bytecodes::code_at(method(), pc);
375 int offset = 0;
376 switch (branch) {
377 case Bytecodes::_if_icmplt: case Bytecodes::_iflt:
378 case Bytecodes::_if_icmpgt: case Bytecodes::_ifgt:
379 case Bytecodes::_if_icmple: case Bytecodes::_ifle:
380 case Bytecodes::_if_icmpge: case Bytecodes::_ifge:
381 case Bytecodes::_if_icmpeq: case Bytecodes::_if_acmpeq: case Bytecodes::_ifeq:
382 case Bytecodes::_if_icmpne: case Bytecodes::_if_acmpne: case Bytecodes::_ifne:
383 case Bytecodes::_ifnull: case Bytecodes::_ifnonnull: case Bytecodes::_goto:
384 offset = (int16_t)Bytes::get_Java_u2(pc + 1);
385 break;
386 case Bytecodes::_goto_w:
387 offset = Bytes::get_Java_u4(pc + 1);
388 break;
389 default: ;
390 }
391 bci = branch_bci + offset;
392 }
394 osr_nm = CompilationPolicy::policy()->event(enclosing_method, method, branch_bci, bci, level, THREAD);
395 return osr_nm;
396 }
398 JRT_BLOCK_ENTRY(address, Runtime1::counter_overflow(JavaThread* thread, int bci, methodOopDesc* method))
399 nmethod* osr_nm;
400 JRT_BLOCK
401 osr_nm = counter_overflow_helper(thread, bci, method);
402 if (osr_nm != NULL) {
403 RegisterMap map(thread, false);
404 frame fr = thread->last_frame().sender(&map);
405 Deoptimization::deoptimize_frame(thread, fr.id());
406 }
407 JRT_BLOCK_END
408 return NULL;
409 JRT_END
411 extern void vm_exit(int code);
413 // Enter this method from compiled code handler below. This is where we transition
414 // to VM mode. This is done as a helper routine so that the method called directly
415 // from compiled code does not have to transition to VM. This allows the entry
416 // method to see if the nmethod that we have just looked up a handler for has
417 // been deoptimized while we were in the vm. This simplifies the assembly code
418 // cpu directories.
419 //
420 // We are entering here from exception stub (via the entry method below)
421 // If there is a compiled exception handler in this method, we will continue there;
422 // otherwise we will unwind the stack and continue at the caller of top frame method
423 // Note: we enter in Java using a special JRT wrapper. This wrapper allows us to
424 // control the area where we can allow a safepoint. After we exit the safepoint area we can
425 // check to see if the handler we are going to return is now in a nmethod that has
426 // been deoptimized. If that is the case we return the deopt blob
427 // unpack_with_exception entry instead. This makes life for the exception blob easier
428 // because making that same check and diverting is painful from assembly language.
429 JRT_ENTRY_NO_ASYNC(static address, exception_handler_for_pc_helper(JavaThread* thread, oopDesc* ex, address pc, nmethod*& nm))
430 // Reset method handle flag.
431 thread->set_is_method_handle_return(false);
433 Handle exception(thread, ex);
434 nm = CodeCache::find_nmethod(pc);
435 assert(nm != NULL, "this is not an nmethod");
436 // Adjust the pc as needed/
437 if (nm->is_deopt_pc(pc)) {
438 RegisterMap map(thread, false);
439 frame exception_frame = thread->last_frame().sender(&map);
440 // if the frame isn't deopted then pc must not correspond to the caller of last_frame
441 assert(exception_frame.is_deoptimized_frame(), "must be deopted");
442 pc = exception_frame.pc();
443 }
444 #ifdef ASSERT
445 assert(exception.not_null(), "NULL exceptions should be handled by throw_exception");
446 assert(exception->is_oop(), "just checking");
447 // Check that exception is a subclass of Throwable, otherwise we have a VerifyError
448 if (!(exception->is_a(SystemDictionary::Throwable_klass()))) {
449 if (ExitVMOnVerifyError) vm_exit(-1);
450 ShouldNotReachHere();
451 }
452 #endif
454 // Check the stack guard pages and reenable them if necessary and there is
455 // enough space on the stack to do so. Use fast exceptions only if the guard
456 // pages are enabled.
457 bool guard_pages_enabled = thread->stack_yellow_zone_enabled();
458 if (!guard_pages_enabled) guard_pages_enabled = thread->reguard_stack();
460 if (JvmtiExport::can_post_on_exceptions()) {
461 // To ensure correct notification of exception catches and throws
462 // we have to deoptimize here. If we attempted to notify the
463 // catches and throws during this exception lookup it's possible
464 // we could deoptimize on the way out of the VM and end back in
465 // the interpreter at the throw site. This would result in double
466 // notifications since the interpreter would also notify about
467 // these same catches and throws as it unwound the frame.
469 RegisterMap reg_map(thread);
470 frame stub_frame = thread->last_frame();
471 frame caller_frame = stub_frame.sender(®_map);
473 // We don't really want to deoptimize the nmethod itself since we
474 // can actually continue in the exception handler ourselves but I
475 // don't see an easy way to have the desired effect.
476 Deoptimization::deoptimize_frame(thread, caller_frame.id());
477 assert(caller_is_deopted(), "Must be deoptimized");
479 return SharedRuntime::deopt_blob()->unpack_with_exception_in_tls();
480 }
482 // ExceptionCache is used only for exceptions at call sites and not for implicit exceptions
483 if (guard_pages_enabled) {
484 address fast_continuation = nm->handler_for_exception_and_pc(exception, pc);
485 if (fast_continuation != NULL) {
486 // Set flag if return address is a method handle call site.
487 thread->set_is_method_handle_return(nm->is_method_handle_return(pc));
488 return fast_continuation;
489 }
490 }
492 // If the stack guard pages are enabled, check whether there is a handler in
493 // the current method. Otherwise (guard pages disabled), force an unwind and
494 // skip the exception cache update (i.e., just leave continuation==NULL).
495 address continuation = NULL;
496 if (guard_pages_enabled) {
498 // New exception handling mechanism can support inlined methods
499 // with exception handlers since the mappings are from PC to PC
501 // debugging support
502 // tracing
503 if (TraceExceptions) {
504 ttyLocker ttyl;
505 ResourceMark rm;
506 tty->print_cr("Exception <%s> (0x%x) thrown in compiled method <%s> at PC " PTR_FORMAT " for thread 0x%x",
507 exception->print_value_string(), (address)exception(), nm->method()->print_value_string(), pc, thread);
508 }
509 // for AbortVMOnException flag
510 NOT_PRODUCT(Exceptions::debug_check_abort(exception));
512 // Clear out the exception oop and pc since looking up an
513 // exception handler can cause class loading, which might throw an
514 // exception and those fields are expected to be clear during
515 // normal bytecode execution.
516 thread->set_exception_oop(NULL);
517 thread->set_exception_pc(NULL);
519 continuation = SharedRuntime::compute_compiled_exc_handler(nm, pc, exception, false, false);
520 // If an exception was thrown during exception dispatch, the exception oop may have changed
521 thread->set_exception_oop(exception());
522 thread->set_exception_pc(pc);
524 // the exception cache is used only by non-implicit exceptions
525 if (continuation != NULL) {
526 nm->add_handler_for_exception_and_pc(exception, pc, continuation);
527 }
528 }
530 thread->set_vm_result(exception());
531 // Set flag if return address is a method handle call site.
532 thread->set_is_method_handle_return(nm->is_method_handle_return(pc));
534 if (TraceExceptions) {
535 ttyLocker ttyl;
536 ResourceMark rm;
537 tty->print_cr("Thread " PTR_FORMAT " continuing at PC " PTR_FORMAT " for exception thrown at PC " PTR_FORMAT,
538 thread, continuation, pc);
539 }
541 return continuation;
542 JRT_END
544 // Enter this method from compiled code only if there is a Java exception handler
545 // in the method handling the exception.
546 // We are entering here from exception stub. We don't do a normal VM transition here.
547 // We do it in a helper. This is so we can check to see if the nmethod we have just
548 // searched for an exception handler has been deoptimized in the meantime.
549 address Runtime1::exception_handler_for_pc(JavaThread* thread) {
550 oop exception = thread->exception_oop();
551 address pc = thread->exception_pc();
552 // Still in Java mode
553 DEBUG_ONLY(ResetNoHandleMark rnhm);
554 nmethod* nm = NULL;
555 address continuation = NULL;
556 {
557 // Enter VM mode by calling the helper
558 ResetNoHandleMark rnhm;
559 continuation = exception_handler_for_pc_helper(thread, exception, pc, nm);
560 }
561 // Back in JAVA, use no oops DON'T safepoint
563 // Now check to see if the nmethod we were called from is now deoptimized.
564 // If so we must return to the deopt blob and deoptimize the nmethod
565 if (nm != NULL && caller_is_deopted()) {
566 continuation = SharedRuntime::deopt_blob()->unpack_with_exception_in_tls();
567 }
569 assert(continuation != NULL, "no handler found");
570 return continuation;
571 }
574 JRT_ENTRY(void, Runtime1::throw_range_check_exception(JavaThread* thread, int index))
575 NOT_PRODUCT(_throw_range_check_exception_count++;)
576 Events::log("throw_range_check");
577 char message[jintAsStringSize];
578 sprintf(message, "%d", index);
579 SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), message);
580 JRT_END
583 JRT_ENTRY(void, Runtime1::throw_index_exception(JavaThread* thread, int index))
584 NOT_PRODUCT(_throw_index_exception_count++;)
585 Events::log("throw_index");
586 char message[16];
587 sprintf(message, "%d", index);
588 SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_IndexOutOfBoundsException(), message);
589 JRT_END
592 JRT_ENTRY(void, Runtime1::throw_div0_exception(JavaThread* thread))
593 NOT_PRODUCT(_throw_div0_exception_count++;)
594 SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_ArithmeticException(), "/ by zero");
595 JRT_END
598 JRT_ENTRY(void, Runtime1::throw_null_pointer_exception(JavaThread* thread))
599 NOT_PRODUCT(_throw_null_pointer_exception_count++;)
600 SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_NullPointerException());
601 JRT_END
604 JRT_ENTRY(void, Runtime1::throw_class_cast_exception(JavaThread* thread, oopDesc* object))
605 NOT_PRODUCT(_throw_class_cast_exception_count++;)
606 ResourceMark rm(thread);
607 char* message = SharedRuntime::generate_class_cast_message(
608 thread, Klass::cast(object->klass())->external_name());
609 SharedRuntime::throw_and_post_jvmti_exception(
610 thread, vmSymbols::java_lang_ClassCastException(), message);
611 JRT_END
614 JRT_ENTRY(void, Runtime1::throw_incompatible_class_change_error(JavaThread* thread))
615 NOT_PRODUCT(_throw_incompatible_class_change_error_count++;)
616 ResourceMark rm(thread);
617 SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_IncompatibleClassChangeError());
618 JRT_END
621 JRT_ENTRY_NO_ASYNC(void, Runtime1::monitorenter(JavaThread* thread, oopDesc* obj, BasicObjectLock* lock))
622 NOT_PRODUCT(_monitorenter_slowcase_cnt++;)
623 if (PrintBiasedLockingStatistics) {
624 Atomic::inc(BiasedLocking::slow_path_entry_count_addr());
625 }
626 Handle h_obj(thread, obj);
627 assert(h_obj()->is_oop(), "must be NULL or an object");
628 if (UseBiasedLocking) {
629 // Retry fast entry if bias is revoked to avoid unnecessary inflation
630 ObjectSynchronizer::fast_enter(h_obj, lock->lock(), true, CHECK);
631 } else {
632 if (UseFastLocking) {
633 // When using fast locking, the compiled code has already tried the fast case
634 assert(obj == lock->obj(), "must match");
635 ObjectSynchronizer::slow_enter(h_obj, lock->lock(), THREAD);
636 } else {
637 lock->set_obj(obj);
638 ObjectSynchronizer::fast_enter(h_obj, lock->lock(), false, THREAD);
639 }
640 }
641 JRT_END
644 JRT_LEAF(void, Runtime1::monitorexit(JavaThread* thread, BasicObjectLock* lock))
645 NOT_PRODUCT(_monitorexit_slowcase_cnt++;)
646 assert(thread == JavaThread::current(), "threads must correspond");
647 assert(thread->last_Java_sp(), "last_Java_sp must be set");
648 // monitorexit is non-blocking (leaf routine) => no exceptions can be thrown
649 EXCEPTION_MARK;
651 oop obj = lock->obj();
652 assert(obj->is_oop(), "must be NULL or an object");
653 if (UseFastLocking) {
654 // When using fast locking, the compiled code has already tried the fast case
655 ObjectSynchronizer::slow_exit(obj, lock->lock(), THREAD);
656 } else {
657 ObjectSynchronizer::fast_exit(obj, lock->lock(), THREAD);
658 }
659 JRT_END
662 static klassOop resolve_field_return_klass(methodHandle caller, int bci, TRAPS) {
663 Bytecode_field field_access(caller, bci);
664 // This can be static or non-static field access
665 Bytecodes::Code code = field_access.code();
667 // We must load class, initialize class and resolvethe field
668 FieldAccessInfo result; // initialize class if needed
669 constantPoolHandle constants(THREAD, caller->constants());
670 LinkResolver::resolve_field(result, constants, field_access.index(), Bytecodes::java_code(code), false, CHECK_NULL);
671 return result.klass()();
672 }
675 //
676 // This routine patches sites where a class wasn't loaded or
677 // initialized at the time the code was generated. It handles
678 // references to classes, fields and forcing of initialization. Most
679 // of the cases are straightforward and involving simply forcing
680 // resolution of a class, rewriting the instruction stream with the
681 // needed constant and replacing the call in this function with the
682 // patched code. The case for static field is more complicated since
683 // the thread which is in the process of initializing a class can
684 // access it's static fields but other threads can't so the code
685 // either has to deoptimize when this case is detected or execute a
686 // check that the current thread is the initializing thread. The
687 // current
688 //
689 // Patches basically look like this:
690 //
691 //
692 // patch_site: jmp patch stub ;; will be patched
693 // continue: ...
694 // ...
695 // ...
696 // ...
697 //
698 // They have a stub which looks like this:
699 //
700 // ;; patch body
701 // movl <const>, reg (for class constants)
702 // <or> movl [reg1 + <const>], reg (for field offsets)
703 // <or> movl reg, [reg1 + <const>] (for field offsets)
704 // <being_init offset> <bytes to copy> <bytes to skip>
705 // patch_stub: call Runtime1::patch_code (through a runtime stub)
706 // jmp patch_site
707 //
708 //
709 // A normal patch is done by rewriting the patch body, usually a move,
710 // and then copying it into place over top of the jmp instruction
711 // being careful to flush caches and doing it in an MP-safe way. The
712 // constants following the patch body are used to find various pieces
713 // of the patch relative to the call site for Runtime1::patch_code.
714 // The case for getstatic and putstatic is more complicated because
715 // getstatic and putstatic have special semantics when executing while
716 // the class is being initialized. getstatic/putstatic on a class
717 // which is being_initialized may be executed by the initializing
718 // thread but other threads have to block when they execute it. This
719 // is accomplished in compiled code by executing a test of the current
720 // thread against the initializing thread of the class. It's emitted
721 // as boilerplate in their stub which allows the patched code to be
722 // executed before it's copied back into the main body of the nmethod.
723 //
724 // being_init: get_thread(<tmp reg>
725 // cmpl [reg1 + <init_thread_offset>], <tmp reg>
726 // jne patch_stub
727 // movl [reg1 + <const>], reg (for field offsets) <or>
728 // movl reg, [reg1 + <const>] (for field offsets)
729 // jmp continue
730 // <being_init offset> <bytes to copy> <bytes to skip>
731 // patch_stub: jmp Runtim1::patch_code (through a runtime stub)
732 // jmp patch_site
733 //
734 // If the class is being initialized the patch body is rewritten and
735 // the patch site is rewritten to jump to being_init, instead of
736 // patch_stub. Whenever this code is executed it checks the current
737 // thread against the intializing thread so other threads will enter
738 // the runtime and end up blocked waiting the class to finish
739 // initializing inside the calls to resolve_field below. The
740 // initializing class will continue on it's way. Once the class is
741 // fully_initialized, the intializing_thread of the class becomes
742 // NULL, so the next thread to execute this code will fail the test,
743 // call into patch_code and complete the patching process by copying
744 // the patch body back into the main part of the nmethod and resume
745 // executing.
746 //
747 //
749 JRT_ENTRY(void, Runtime1::patch_code(JavaThread* thread, Runtime1::StubID stub_id ))
750 NOT_PRODUCT(_patch_code_slowcase_cnt++;)
752 ResourceMark rm(thread);
753 RegisterMap reg_map(thread, false);
754 frame runtime_frame = thread->last_frame();
755 frame caller_frame = runtime_frame.sender(®_map);
757 // last java frame on stack
758 vframeStream vfst(thread, true);
759 assert(!vfst.at_end(), "Java frame must exist");
761 methodHandle caller_method(THREAD, vfst.method());
762 // Note that caller_method->code() may not be same as caller_code because of OSR's
763 // Note also that in the presence of inlining it is not guaranteed
764 // that caller_method() == caller_code->method()
767 int bci = vfst.bci();
769 Events::log("patch_code @ " INTPTR_FORMAT , caller_frame.pc());
771 Bytecodes::Code code = caller_method()->java_code_at(bci);
773 #ifndef PRODUCT
774 // this is used by assertions in the access_field_patching_id
775 BasicType patch_field_type = T_ILLEGAL;
776 #endif // PRODUCT
777 bool deoptimize_for_volatile = false;
778 int patch_field_offset = -1;
779 KlassHandle init_klass(THREAD, klassOop(NULL)); // klass needed by access_field_patching code
780 Handle load_klass(THREAD, NULL); // oop needed by load_klass_patching code
781 if (stub_id == Runtime1::access_field_patching_id) {
783 Bytecode_field field_access(caller_method, bci);
784 FieldAccessInfo result; // initialize class if needed
785 Bytecodes::Code code = field_access.code();
786 constantPoolHandle constants(THREAD, caller_method->constants());
787 LinkResolver::resolve_field(result, constants, field_access.index(), Bytecodes::java_code(code), false, CHECK);
788 patch_field_offset = result.field_offset();
790 // If we're patching a field which is volatile then at compile it
791 // must not have been know to be volatile, so the generated code
792 // isn't correct for a volatile reference. The nmethod has to be
793 // deoptimized so that the code can be regenerated correctly.
794 // This check is only needed for access_field_patching since this
795 // is the path for patching field offsets. load_klass is only
796 // used for patching references to oops which don't need special
797 // handling in the volatile case.
798 deoptimize_for_volatile = result.access_flags().is_volatile();
800 #ifndef PRODUCT
801 patch_field_type = result.field_type();
802 #endif
803 } else if (stub_id == Runtime1::load_klass_patching_id) {
804 oop k;
805 switch (code) {
806 case Bytecodes::_putstatic:
807 case Bytecodes::_getstatic:
808 { klassOop klass = resolve_field_return_klass(caller_method, bci, CHECK);
809 // Save a reference to the class that has to be checked for initialization
810 init_klass = KlassHandle(THREAD, klass);
811 k = klass->java_mirror();
812 }
813 break;
814 case Bytecodes::_new:
815 { Bytecode_new bnew(caller_method(), caller_method->bcp_from(bci));
816 k = caller_method->constants()->klass_at(bnew.index(), CHECK);
817 }
818 break;
819 case Bytecodes::_multianewarray:
820 { Bytecode_multianewarray mna(caller_method(), caller_method->bcp_from(bci));
821 k = caller_method->constants()->klass_at(mna.index(), CHECK);
822 }
823 break;
824 case Bytecodes::_instanceof:
825 { Bytecode_instanceof io(caller_method(), caller_method->bcp_from(bci));
826 k = caller_method->constants()->klass_at(io.index(), CHECK);
827 }
828 break;
829 case Bytecodes::_checkcast:
830 { Bytecode_checkcast cc(caller_method(), caller_method->bcp_from(bci));
831 k = caller_method->constants()->klass_at(cc.index(), CHECK);
832 }
833 break;
834 case Bytecodes::_anewarray:
835 { Bytecode_anewarray anew(caller_method(), caller_method->bcp_from(bci));
836 klassOop ek = caller_method->constants()->klass_at(anew.index(), CHECK);
837 k = Klass::cast(ek)->array_klass(CHECK);
838 }
839 break;
840 case Bytecodes::_ldc:
841 case Bytecodes::_ldc_w:
842 {
843 Bytecode_loadconstant cc(caller_method, bci);
844 k = cc.resolve_constant(CHECK);
845 assert(k != NULL && !k->is_klass(), "must be class mirror or other Java constant");
846 }
847 break;
848 default: Unimplemented();
849 }
850 // convert to handle
851 load_klass = Handle(THREAD, k);
852 } else {
853 ShouldNotReachHere();
854 }
856 if (deoptimize_for_volatile) {
857 // At compile time we assumed the field wasn't volatile but after
858 // loading it turns out it was volatile so we have to throw the
859 // compiled code out and let it be regenerated.
860 if (TracePatching) {
861 tty->print_cr("Deoptimizing for patching volatile field reference");
862 }
863 // It's possible the nmethod was invalidated in the last
864 // safepoint, but if it's still alive then make it not_entrant.
865 nmethod* nm = CodeCache::find_nmethod(caller_frame.pc());
866 if (nm != NULL) {
867 nm->make_not_entrant();
868 }
870 Deoptimization::deoptimize_frame(thread, caller_frame.id());
872 // Return to the now deoptimized frame.
873 }
875 // If we are patching in a non-perm oop, make sure the nmethod
876 // is on the right list.
877 if (ScavengeRootsInCode && load_klass.not_null() && load_klass->is_scavengable()) {
878 MutexLockerEx ml_code (CodeCache_lock, Mutex::_no_safepoint_check_flag);
879 nmethod* nm = CodeCache::find_nmethod(caller_frame.pc());
880 guarantee(nm != NULL, "only nmethods can contain non-perm oops");
881 if (!nm->on_scavenge_root_list())
882 CodeCache::add_scavenge_root_nmethod(nm);
883 }
885 // Now copy code back
887 {
888 MutexLockerEx ml_patch (Patching_lock, Mutex::_no_safepoint_check_flag);
889 //
890 // Deoptimization may have happened while we waited for the lock.
891 // In that case we don't bother to do any patching we just return
892 // and let the deopt happen
893 if (!caller_is_deopted()) {
894 NativeGeneralJump* jump = nativeGeneralJump_at(caller_frame.pc());
895 address instr_pc = jump->jump_destination();
896 NativeInstruction* ni = nativeInstruction_at(instr_pc);
897 if (ni->is_jump() ) {
898 // the jump has not been patched yet
899 // The jump destination is slow case and therefore not part of the stubs
900 // (stubs are only for StaticCalls)
902 // format of buffer
903 // ....
904 // instr byte 0 <-- copy_buff
905 // instr byte 1
906 // ..
907 // instr byte n-1
908 // n
909 // .... <-- call destination
911 address stub_location = caller_frame.pc() + PatchingStub::patch_info_offset();
912 unsigned char* byte_count = (unsigned char*) (stub_location - 1);
913 unsigned char* byte_skip = (unsigned char*) (stub_location - 2);
914 unsigned char* being_initialized_entry_offset = (unsigned char*) (stub_location - 3);
915 address copy_buff = stub_location - *byte_skip - *byte_count;
916 address being_initialized_entry = stub_location - *being_initialized_entry_offset;
917 if (TracePatching) {
918 tty->print_cr(" Patching %s at bci %d at address 0x%x (%s)", Bytecodes::name(code), bci,
919 instr_pc, (stub_id == Runtime1::access_field_patching_id) ? "field" : "klass");
920 nmethod* caller_code = CodeCache::find_nmethod(caller_frame.pc());
921 assert(caller_code != NULL, "nmethod not found");
923 // NOTE we use pc() not original_pc() because we already know they are
924 // identical otherwise we'd have never entered this block of code
926 OopMap* map = caller_code->oop_map_for_return_address(caller_frame.pc());
927 assert(map != NULL, "null check");
928 map->print();
929 tty->cr();
931 Disassembler::decode(copy_buff, copy_buff + *byte_count, tty);
932 }
933 // depending on the code below, do_patch says whether to copy the patch body back into the nmethod
934 bool do_patch = true;
935 if (stub_id == Runtime1::access_field_patching_id) {
936 // The offset may not be correct if the class was not loaded at code generation time.
937 // Set it now.
938 NativeMovRegMem* n_move = nativeMovRegMem_at(copy_buff);
939 assert(n_move->offset() == 0 || (n_move->offset() == 4 && (patch_field_type == T_DOUBLE || patch_field_type == T_LONG)), "illegal offset for type");
940 assert(patch_field_offset >= 0, "illegal offset");
941 n_move->add_offset_in_bytes(patch_field_offset);
942 } else if (stub_id == Runtime1::load_klass_patching_id) {
943 // If a getstatic or putstatic is referencing a klass which
944 // isn't fully initialized, the patch body isn't copied into
945 // place until initialization is complete. In this case the
946 // patch site is setup so that any threads besides the
947 // initializing thread are forced to come into the VM and
948 // block.
949 do_patch = (code != Bytecodes::_getstatic && code != Bytecodes::_putstatic) ||
950 instanceKlass::cast(init_klass())->is_initialized();
951 NativeGeneralJump* jump = nativeGeneralJump_at(instr_pc);
952 if (jump->jump_destination() == being_initialized_entry) {
953 assert(do_patch == true, "initialization must be complete at this point");
954 } else {
955 // patch the instruction <move reg, klass>
956 NativeMovConstReg* n_copy = nativeMovConstReg_at(copy_buff);
958 assert(n_copy->data() == 0 ||
959 n_copy->data() == (intptr_t)Universe::non_oop_word(),
960 "illegal init value");
961 assert(load_klass() != NULL, "klass not set");
962 n_copy->set_data((intx) (load_klass()));
964 if (TracePatching) {
965 Disassembler::decode(copy_buff, copy_buff + *byte_count, tty);
966 }
968 #if defined(SPARC) || defined(PPC)
969 // Update the oop location in the nmethod with the proper
970 // oop. When the code was generated, a NULL was stuffed
971 // in the oop table and that table needs to be update to
972 // have the right value. On intel the value is kept
973 // directly in the instruction instead of in the oop
974 // table, so set_data above effectively updated the value.
975 nmethod* nm = CodeCache::find_nmethod(instr_pc);
976 assert(nm != NULL, "invalid nmethod_pc");
977 RelocIterator oops(nm, copy_buff, copy_buff + 1);
978 bool found = false;
979 while (oops.next() && !found) {
980 if (oops.type() == relocInfo::oop_type) {
981 oop_Relocation* r = oops.oop_reloc();
982 oop* oop_adr = r->oop_addr();
983 *oop_adr = load_klass();
984 r->fix_oop_relocation();
985 found = true;
986 }
987 }
988 assert(found, "the oop must exist!");
989 #endif
991 }
992 } else {
993 ShouldNotReachHere();
994 }
995 if (do_patch) {
996 // replace instructions
997 // first replace the tail, then the call
998 #ifdef ARM
999 if(stub_id == Runtime1::load_klass_patching_id && !VM_Version::supports_movw()) {
1000 copy_buff -= *byte_count;
1001 NativeMovConstReg* n_copy2 = nativeMovConstReg_at(copy_buff);
1002 n_copy2->set_data((intx) (load_klass()), instr_pc);
1003 }
1004 #endif
1006 for (int i = NativeCall::instruction_size; i < *byte_count; i++) {
1007 address ptr = copy_buff + i;
1008 int a_byte = (*ptr) & 0xFF;
1009 address dst = instr_pc + i;
1010 *(unsigned char*)dst = (unsigned char) a_byte;
1011 }
1012 ICache::invalidate_range(instr_pc, *byte_count);
1013 NativeGeneralJump::replace_mt_safe(instr_pc, copy_buff);
1015 if (stub_id == Runtime1::load_klass_patching_id) {
1016 // update relocInfo to oop
1017 nmethod* nm = CodeCache::find_nmethod(instr_pc);
1018 assert(nm != NULL, "invalid nmethod_pc");
1020 // The old patch site is now a move instruction so update
1021 // the reloc info so that it will get updated during
1022 // future GCs.
1023 RelocIterator iter(nm, (address)instr_pc, (address)(instr_pc + 1));
1024 relocInfo::change_reloc_info_for_address(&iter, (address) instr_pc,
1025 relocInfo::none, relocInfo::oop_type);
1026 #ifdef SPARC
1027 // Sparc takes two relocations for an oop so update the second one.
1028 address instr_pc2 = instr_pc + NativeMovConstReg::add_offset;
1029 RelocIterator iter2(nm, instr_pc2, instr_pc2 + 1);
1030 relocInfo::change_reloc_info_for_address(&iter2, (address) instr_pc2,
1031 relocInfo::none, relocInfo::oop_type);
1032 #endif
1033 #ifdef PPC
1034 { address instr_pc2 = instr_pc + NativeMovConstReg::lo_offset;
1035 RelocIterator iter2(nm, instr_pc2, instr_pc2 + 1);
1036 relocInfo::change_reloc_info_for_address(&iter2, (address) instr_pc2, relocInfo::none, relocInfo::oop_type);
1037 }
1038 #endif
1039 }
1041 } else {
1042 ICache::invalidate_range(copy_buff, *byte_count);
1043 NativeGeneralJump::insert_unconditional(instr_pc, being_initialized_entry);
1044 }
1045 }
1046 }
1047 }
1048 JRT_END
1050 //
1051 // Entry point for compiled code. We want to patch a nmethod.
1052 // We don't do a normal VM transition here because we want to
1053 // know after the patching is complete and any safepoint(s) are taken
1054 // if the calling nmethod was deoptimized. We do this by calling a
1055 // helper method which does the normal VM transition and when it
1056 // completes we can check for deoptimization. This simplifies the
1057 // assembly code in the cpu directories.
1058 //
1059 int Runtime1::move_klass_patching(JavaThread* thread) {
1060 //
1061 // NOTE: we are still in Java
1062 //
1063 Thread* THREAD = thread;
1064 debug_only(NoHandleMark nhm;)
1065 {
1066 // Enter VM mode
1068 ResetNoHandleMark rnhm;
1069 patch_code(thread, load_klass_patching_id);
1070 }
1071 // Back in JAVA, use no oops DON'T safepoint
1073 // Return true if calling code is deoptimized
1075 return caller_is_deopted();
1076 }
1078 //
1079 // Entry point for compiled code. We want to patch a nmethod.
1080 // We don't do a normal VM transition here because we want to
1081 // know after the patching is complete and any safepoint(s) are taken
1082 // if the calling nmethod was deoptimized. We do this by calling a
1083 // helper method which does the normal VM transition and when it
1084 // completes we can check for deoptimization. This simplifies the
1085 // assembly code in the cpu directories.
1086 //
1088 int Runtime1::access_field_patching(JavaThread* thread) {
1089 //
1090 // NOTE: we are still in Java
1091 //
1092 Thread* THREAD = thread;
1093 debug_only(NoHandleMark nhm;)
1094 {
1095 // Enter VM mode
1097 ResetNoHandleMark rnhm;
1098 patch_code(thread, access_field_patching_id);
1099 }
1100 // Back in JAVA, use no oops DON'T safepoint
1102 // Return true if calling code is deoptimized
1104 return caller_is_deopted();
1105 JRT_END
1108 JRT_LEAF(void, Runtime1::trace_block_entry(jint block_id))
1109 // for now we just print out the block id
1110 tty->print("%d ", block_id);
1111 JRT_END
1114 // Array copy return codes.
1115 enum {
1116 ac_failed = -1, // arraycopy failed
1117 ac_ok = 0 // arraycopy succeeded
1118 };
1121 // Below length is the # elements copied.
1122 template <class T> int obj_arraycopy_work(oopDesc* src, T* src_addr,
1123 oopDesc* dst, T* dst_addr,
1124 int length) {
1126 // For performance reasons, we assume we are using a card marking write
1127 // barrier. The assert will fail if this is not the case.
1128 // Note that we use the non-virtual inlineable variant of write_ref_array.
1129 BarrierSet* bs = Universe::heap()->barrier_set();
1130 assert(bs->has_write_ref_array_opt(), "Barrier set must have ref array opt");
1131 assert(bs->has_write_ref_array_pre_opt(), "For pre-barrier as well.");
1132 if (src == dst) {
1133 // same object, no check
1134 bs->write_ref_array_pre(dst_addr, length);
1135 Copy::conjoint_oops_atomic(src_addr, dst_addr, length);
1136 bs->write_ref_array((HeapWord*)dst_addr, length);
1137 return ac_ok;
1138 } else {
1139 klassOop bound = objArrayKlass::cast(dst->klass())->element_klass();
1140 klassOop stype = objArrayKlass::cast(src->klass())->element_klass();
1141 if (stype == bound || Klass::cast(stype)->is_subtype_of(bound)) {
1142 // Elements are guaranteed to be subtypes, so no check necessary
1143 bs->write_ref_array_pre(dst_addr, length);
1144 Copy::conjoint_oops_atomic(src_addr, dst_addr, length);
1145 bs->write_ref_array((HeapWord*)dst_addr, length);
1146 return ac_ok;
1147 }
1148 }
1149 return ac_failed;
1150 }
1152 // fast and direct copy of arrays; returning -1, means that an exception may be thrown
1153 // and we did not copy anything
1154 JRT_LEAF(int, Runtime1::arraycopy(oopDesc* src, int src_pos, oopDesc* dst, int dst_pos, int length))
1155 #ifndef PRODUCT
1156 _generic_arraycopy_cnt++; // Slow-path oop array copy
1157 #endif
1159 if (src == NULL || dst == NULL || src_pos < 0 || dst_pos < 0 || length < 0) return ac_failed;
1160 if (!dst->is_array() || !src->is_array()) return ac_failed;
1161 if ((unsigned int) arrayOop(src)->length() < (unsigned int)src_pos + (unsigned int)length) return ac_failed;
1162 if ((unsigned int) arrayOop(dst)->length() < (unsigned int)dst_pos + (unsigned int)length) return ac_failed;
1164 if (length == 0) return ac_ok;
1165 if (src->is_typeArray()) {
1166 const klassOop klass_oop = src->klass();
1167 if (klass_oop != dst->klass()) return ac_failed;
1168 typeArrayKlass* klass = typeArrayKlass::cast(klass_oop);
1169 const int l2es = klass->log2_element_size();
1170 const int ihs = klass->array_header_in_bytes() / wordSize;
1171 char* src_addr = (char*) ((oopDesc**)src + ihs) + (src_pos << l2es);
1172 char* dst_addr = (char*) ((oopDesc**)dst + ihs) + (dst_pos << l2es);
1173 // Potential problem: memmove is not guaranteed to be word atomic
1174 // Revisit in Merlin
1175 memmove(dst_addr, src_addr, length << l2es);
1176 return ac_ok;
1177 } else if (src->is_objArray() && dst->is_objArray()) {
1178 if (UseCompressedOops) {
1179 narrowOop *src_addr = objArrayOop(src)->obj_at_addr<narrowOop>(src_pos);
1180 narrowOop *dst_addr = objArrayOop(dst)->obj_at_addr<narrowOop>(dst_pos);
1181 return obj_arraycopy_work(src, src_addr, dst, dst_addr, length);
1182 } else {
1183 oop *src_addr = objArrayOop(src)->obj_at_addr<oop>(src_pos);
1184 oop *dst_addr = objArrayOop(dst)->obj_at_addr<oop>(dst_pos);
1185 return obj_arraycopy_work(src, src_addr, dst, dst_addr, length);
1186 }
1187 }
1188 return ac_failed;
1189 JRT_END
1192 JRT_LEAF(void, Runtime1::primitive_arraycopy(HeapWord* src, HeapWord* dst, int length))
1193 #ifndef PRODUCT
1194 _primitive_arraycopy_cnt++;
1195 #endif
1197 if (length == 0) return;
1198 // Not guaranteed to be word atomic, but that doesn't matter
1199 // for anything but an oop array, which is covered by oop_arraycopy.
1200 Copy::conjoint_jbytes(src, dst, length);
1201 JRT_END
1203 JRT_LEAF(void, Runtime1::oop_arraycopy(HeapWord* src, HeapWord* dst, int num))
1204 #ifndef PRODUCT
1205 _oop_arraycopy_cnt++;
1206 #endif
1208 if (num == 0) return;
1209 BarrierSet* bs = Universe::heap()->barrier_set();
1210 assert(bs->has_write_ref_array_opt(), "Barrier set must have ref array opt");
1211 assert(bs->has_write_ref_array_pre_opt(), "For pre-barrier as well.");
1212 if (UseCompressedOops) {
1213 bs->write_ref_array_pre((narrowOop*)dst, num);
1214 Copy::conjoint_oops_atomic((narrowOop*) src, (narrowOop*) dst, num);
1215 } else {
1216 bs->write_ref_array_pre((oop*)dst, num);
1217 Copy::conjoint_oops_atomic((oop*) src, (oop*) dst, num);
1218 }
1219 bs->write_ref_array(dst, num);
1220 JRT_END
1223 #ifndef PRODUCT
1224 void Runtime1::print_statistics() {
1225 tty->print_cr("C1 Runtime statistics:");
1226 tty->print_cr(" _resolve_invoke_virtual_cnt: %d", SharedRuntime::_resolve_virtual_ctr);
1227 tty->print_cr(" _resolve_invoke_opt_virtual_cnt: %d", SharedRuntime::_resolve_opt_virtual_ctr);
1228 tty->print_cr(" _resolve_invoke_static_cnt: %d", SharedRuntime::_resolve_static_ctr);
1229 tty->print_cr(" _handle_wrong_method_cnt: %d", SharedRuntime::_wrong_method_ctr);
1230 tty->print_cr(" _ic_miss_cnt: %d", SharedRuntime::_ic_miss_ctr);
1231 tty->print_cr(" _generic_arraycopy_cnt: %d", _generic_arraycopy_cnt);
1232 tty->print_cr(" _primitive_arraycopy_cnt: %d", _primitive_arraycopy_cnt);
1233 tty->print_cr(" _oop_arraycopy_cnt: %d", _oop_arraycopy_cnt);
1234 tty->print_cr(" _arraycopy_slowcase_cnt: %d", _arraycopy_slowcase_cnt);
1236 tty->print_cr(" _new_type_array_slowcase_cnt: %d", _new_type_array_slowcase_cnt);
1237 tty->print_cr(" _new_object_array_slowcase_cnt: %d", _new_object_array_slowcase_cnt);
1238 tty->print_cr(" _new_instance_slowcase_cnt: %d", _new_instance_slowcase_cnt);
1239 tty->print_cr(" _new_multi_array_slowcase_cnt: %d", _new_multi_array_slowcase_cnt);
1240 tty->print_cr(" _monitorenter_slowcase_cnt: %d", _monitorenter_slowcase_cnt);
1241 tty->print_cr(" _monitorexit_slowcase_cnt: %d", _monitorexit_slowcase_cnt);
1242 tty->print_cr(" _patch_code_slowcase_cnt: %d", _patch_code_slowcase_cnt);
1244 tty->print_cr(" _throw_range_check_exception_count: %d:", _throw_range_check_exception_count);
1245 tty->print_cr(" _throw_index_exception_count: %d:", _throw_index_exception_count);
1246 tty->print_cr(" _throw_div0_exception_count: %d:", _throw_div0_exception_count);
1247 tty->print_cr(" _throw_null_pointer_exception_count: %d:", _throw_null_pointer_exception_count);
1248 tty->print_cr(" _throw_class_cast_exception_count: %d:", _throw_class_cast_exception_count);
1249 tty->print_cr(" _throw_incompatible_class_change_error_count: %d:", _throw_incompatible_class_change_error_count);
1250 tty->print_cr(" _throw_array_store_exception_count: %d:", _throw_array_store_exception_count);
1251 tty->print_cr(" _throw_count: %d:", _throw_count);
1253 SharedRuntime::print_ic_miss_histogram();
1254 tty->cr();
1255 }
1256 #endif // PRODUCT