annotate src/share/vm/c1/c1_Runtime1.cpp @ 196:d1605aabd0a1

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