annotate src/share/vm/runtime/sharedRuntime.cpp @ 1201:24128c2ffa87

6921339: backout 6917766 Reviewed-by: mr
author twisti
date Fri, 29 Jan 2010 08:33:24 -0800
parents ba263cfb7611
children 5f24d0319e54
rev   line source
duke@0 1 /*
never@1187 2 * Copyright 1997-2010 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/_sharedRuntime.cpp.incl"
duke@0 27 #include <math.h>
duke@0 28
duke@0 29 HS_DTRACE_PROBE_DECL4(hotspot, object__alloc, Thread*, char*, int, size_t);
duke@0 30 HS_DTRACE_PROBE_DECL7(hotspot, method__entry, int,
duke@0 31 char*, int, char*, int, char*, int);
duke@0 32 HS_DTRACE_PROBE_DECL7(hotspot, method__return, int,
duke@0 33 char*, int, char*, int, char*, int);
duke@0 34
duke@0 35 // Implementation of SharedRuntime
duke@0 36
duke@0 37 #ifndef PRODUCT
duke@0 38 // For statistics
duke@0 39 int SharedRuntime::_ic_miss_ctr = 0;
duke@0 40 int SharedRuntime::_wrong_method_ctr = 0;
duke@0 41 int SharedRuntime::_resolve_static_ctr = 0;
duke@0 42 int SharedRuntime::_resolve_virtual_ctr = 0;
duke@0 43 int SharedRuntime::_resolve_opt_virtual_ctr = 0;
duke@0 44 int SharedRuntime::_implicit_null_throws = 0;
duke@0 45 int SharedRuntime::_implicit_div0_throws = 0;
duke@0 46 int SharedRuntime::_throw_null_ctr = 0;
duke@0 47
duke@0 48 int SharedRuntime::_nof_normal_calls = 0;
duke@0 49 int SharedRuntime::_nof_optimized_calls = 0;
duke@0 50 int SharedRuntime::_nof_inlined_calls = 0;
duke@0 51 int SharedRuntime::_nof_megamorphic_calls = 0;
duke@0 52 int SharedRuntime::_nof_static_calls = 0;
duke@0 53 int SharedRuntime::_nof_inlined_static_calls = 0;
duke@0 54 int SharedRuntime::_nof_interface_calls = 0;
duke@0 55 int SharedRuntime::_nof_optimized_interface_calls = 0;
duke@0 56 int SharedRuntime::_nof_inlined_interface_calls = 0;
duke@0 57 int SharedRuntime::_nof_megamorphic_interface_calls = 0;
duke@0 58 int SharedRuntime::_nof_removable_exceptions = 0;
duke@0 59
duke@0 60 int SharedRuntime::_new_instance_ctr=0;
duke@0 61 int SharedRuntime::_new_array_ctr=0;
duke@0 62 int SharedRuntime::_multi1_ctr=0;
duke@0 63 int SharedRuntime::_multi2_ctr=0;
duke@0 64 int SharedRuntime::_multi3_ctr=0;
duke@0 65 int SharedRuntime::_multi4_ctr=0;
duke@0 66 int SharedRuntime::_multi5_ctr=0;
duke@0 67 int SharedRuntime::_mon_enter_stub_ctr=0;
duke@0 68 int SharedRuntime::_mon_exit_stub_ctr=0;
duke@0 69 int SharedRuntime::_mon_enter_ctr=0;
duke@0 70 int SharedRuntime::_mon_exit_ctr=0;
duke@0 71 int SharedRuntime::_partial_subtype_ctr=0;
duke@0 72 int SharedRuntime::_jbyte_array_copy_ctr=0;
duke@0 73 int SharedRuntime::_jshort_array_copy_ctr=0;
duke@0 74 int SharedRuntime::_jint_array_copy_ctr=0;
duke@0 75 int SharedRuntime::_jlong_array_copy_ctr=0;
duke@0 76 int SharedRuntime::_oop_array_copy_ctr=0;
duke@0 77 int SharedRuntime::_checkcast_array_copy_ctr=0;
duke@0 78 int SharedRuntime::_unsafe_array_copy_ctr=0;
duke@0 79 int SharedRuntime::_generic_array_copy_ctr=0;
duke@0 80 int SharedRuntime::_slow_array_copy_ctr=0;
duke@0 81 int SharedRuntime::_find_handler_ctr=0;
duke@0 82 int SharedRuntime::_rethrow_ctr=0;
duke@0 83
duke@0 84 int SharedRuntime::_ICmiss_index = 0;
duke@0 85 int SharedRuntime::_ICmiss_count[SharedRuntime::maxICmiss_count];
duke@0 86 address SharedRuntime::_ICmiss_at[SharedRuntime::maxICmiss_count];
duke@0 87
duke@0 88 void SharedRuntime::trace_ic_miss(address at) {
duke@0 89 for (int i = 0; i < _ICmiss_index; i++) {
duke@0 90 if (_ICmiss_at[i] == at) {
duke@0 91 _ICmiss_count[i]++;
duke@0 92 return;
duke@0 93 }
duke@0 94 }
duke@0 95 int index = _ICmiss_index++;
duke@0 96 if (_ICmiss_index >= maxICmiss_count) _ICmiss_index = maxICmiss_count - 1;
duke@0 97 _ICmiss_at[index] = at;
duke@0 98 _ICmiss_count[index] = 1;
duke@0 99 }
duke@0 100
duke@0 101 void SharedRuntime::print_ic_miss_histogram() {
duke@0 102 if (ICMissHistogram) {
duke@0 103 tty->print_cr ("IC Miss Histogram:");
duke@0 104 int tot_misses = 0;
duke@0 105 for (int i = 0; i < _ICmiss_index; i++) {
duke@0 106 tty->print_cr(" at: " INTPTR_FORMAT " nof: %d", _ICmiss_at[i], _ICmiss_count[i]);
duke@0 107 tot_misses += _ICmiss_count[i];
duke@0 108 }
duke@0 109 tty->print_cr ("Total IC misses: %7d", tot_misses);
duke@0 110 }
duke@0 111 }
duke@0 112 #endif // PRODUCT
duke@0 113
ysr@342 114 #ifndef SERIALGC
ysr@342 115
ysr@342 116 // G1 write-barrier pre: executed before a pointer store.
ysr@342 117 JRT_LEAF(void, SharedRuntime::g1_wb_pre(oopDesc* orig, JavaThread *thread))
ysr@342 118 if (orig == NULL) {
ysr@342 119 assert(false, "should be optimized out");
ysr@342 120 return;
ysr@342 121 }
ysr@845 122 assert(orig->is_oop(true /* ignore mark word */), "Error");
ysr@342 123 // store the original value that was in the field reference
ysr@342 124 thread->satb_mark_queue().enqueue(orig);
ysr@342 125 JRT_END
ysr@342 126
ysr@342 127 // G1 write-barrier post: executed after a pointer store.
ysr@342 128 JRT_LEAF(void, SharedRuntime::g1_wb_post(void* card_addr, JavaThread* thread))
ysr@342 129 thread->dirty_card_queue().enqueue(card_addr);
ysr@342 130 JRT_END
ysr@342 131
ysr@342 132 #endif // !SERIALGC
ysr@342 133
duke@0 134
duke@0 135 JRT_LEAF(jlong, SharedRuntime::lmul(jlong y, jlong x))
duke@0 136 return x * y;
duke@0 137 JRT_END
duke@0 138
duke@0 139
duke@0 140 JRT_LEAF(jlong, SharedRuntime::ldiv(jlong y, jlong x))
duke@0 141 if (x == min_jlong && y == CONST64(-1)) {
duke@0 142 return x;
duke@0 143 } else {
duke@0 144 return x / y;
duke@0 145 }
duke@0 146 JRT_END
duke@0 147
duke@0 148
duke@0 149 JRT_LEAF(jlong, SharedRuntime::lrem(jlong y, jlong x))
duke@0 150 if (x == min_jlong && y == CONST64(-1)) {
duke@0 151 return 0;
duke@0 152 } else {
duke@0 153 return x % y;
duke@0 154 }
duke@0 155 JRT_END
duke@0 156
duke@0 157
duke@0 158 const juint float_sign_mask = 0x7FFFFFFF;
duke@0 159 const juint float_infinity = 0x7F800000;
duke@0 160 const julong double_sign_mask = CONST64(0x7FFFFFFFFFFFFFFF);
duke@0 161 const julong double_infinity = CONST64(0x7FF0000000000000);
duke@0 162
duke@0 163 JRT_LEAF(jfloat, SharedRuntime::frem(jfloat x, jfloat y))
duke@0 164 #ifdef _WIN64
duke@0 165 // 64-bit Windows on amd64 returns the wrong values for
duke@0 166 // infinity operands.
duke@0 167 union { jfloat f; juint i; } xbits, ybits;
duke@0 168 xbits.f = x;
duke@0 169 ybits.f = y;
duke@0 170 // x Mod Infinity == x unless x is infinity
duke@0 171 if ( ((xbits.i & float_sign_mask) != float_infinity) &&
duke@0 172 ((ybits.i & float_sign_mask) == float_infinity) ) {
duke@0 173 return x;
duke@0 174 }
duke@0 175 #endif
duke@0 176 return ((jfloat)fmod((double)x,(double)y));
duke@0 177 JRT_END
duke@0 178
duke@0 179
duke@0 180 JRT_LEAF(jdouble, SharedRuntime::drem(jdouble x, jdouble y))
duke@0 181 #ifdef _WIN64
duke@0 182 union { jdouble d; julong l; } xbits, ybits;
duke@0 183 xbits.d = x;
duke@0 184 ybits.d = y;
duke@0 185 // x Mod Infinity == x unless x is infinity
duke@0 186 if ( ((xbits.l & double_sign_mask) != double_infinity) &&
duke@0 187 ((ybits.l & double_sign_mask) == double_infinity) ) {
duke@0 188 return x;
duke@0 189 }
duke@0 190 #endif
duke@0 191 return ((jdouble)fmod((double)x,(double)y));
duke@0 192 JRT_END
duke@0 193
duke@0 194
duke@0 195 JRT_LEAF(jint, SharedRuntime::f2i(jfloat x))
kvn@508 196 if (g_isnan(x))
kvn@508 197 return 0;
kvn@508 198 if (x >= (jfloat) max_jint)
kvn@508 199 return max_jint;
kvn@508 200 if (x <= (jfloat) min_jint)
kvn@508 201 return min_jint;
kvn@508 202 return (jint) x;
duke@0 203 JRT_END
duke@0 204
duke@0 205
duke@0 206 JRT_LEAF(jlong, SharedRuntime::f2l(jfloat x))
kvn@508 207 if (g_isnan(x))
kvn@508 208 return 0;
kvn@508 209 if (x >= (jfloat) max_jlong)
kvn@508 210 return max_jlong;
kvn@508 211 if (x <= (jfloat) min_jlong)
kvn@508 212 return min_jlong;
kvn@508 213 return (jlong) x;
duke@0 214 JRT_END
duke@0 215
duke@0 216
duke@0 217 JRT_LEAF(jint, SharedRuntime::d2i(jdouble x))
kvn@508 218 if (g_isnan(x))
kvn@508 219 return 0;
kvn@508 220 if (x >= (jdouble) max_jint)
kvn@508 221 return max_jint;
kvn@508 222 if (x <= (jdouble) min_jint)
kvn@508 223 return min_jint;
kvn@508 224 return (jint) x;
duke@0 225 JRT_END
duke@0 226
duke@0 227
duke@0 228 JRT_LEAF(jlong, SharedRuntime::d2l(jdouble x))
kvn@508 229 if (g_isnan(x))
kvn@508 230 return 0;
kvn@508 231 if (x >= (jdouble) max_jlong)
kvn@508 232 return max_jlong;
kvn@508 233 if (x <= (jdouble) min_jlong)
kvn@508 234 return min_jlong;
kvn@508 235 return (jlong) x;
duke@0 236 JRT_END
duke@0 237
duke@0 238
duke@0 239 JRT_LEAF(jfloat, SharedRuntime::d2f(jdouble x))
duke@0 240 return (jfloat)x;
duke@0 241 JRT_END
duke@0 242
duke@0 243
duke@0 244 JRT_LEAF(jfloat, SharedRuntime::l2f(jlong x))
duke@0 245 return (jfloat)x;
duke@0 246 JRT_END
duke@0 247
duke@0 248
duke@0 249 JRT_LEAF(jdouble, SharedRuntime::l2d(jlong x))
duke@0 250 return (jdouble)x;
duke@0 251 JRT_END
duke@0 252
duke@0 253 // Exception handling accross interpreter/compiler boundaries
duke@0 254 //
duke@0 255 // exception_handler_for_return_address(...) returns the continuation address.
duke@0 256 // The continuation address is the entry point of the exception handler of the
duke@0 257 // previous frame depending on the return address.
duke@0 258
duke@0 259 address SharedRuntime::raw_exception_handler_for_return_address(address return_address) {
duke@0 260 assert(frame::verify_return_pc(return_address), "must be a return pc");
duke@0 261
duke@0 262 // the fastest case first
duke@0 263 CodeBlob* blob = CodeCache::find_blob(return_address);
duke@0 264 if (blob != NULL && blob->is_nmethod()) {
duke@0 265 nmethod* code = (nmethod*)blob;
duke@0 266 assert(code != NULL, "nmethod must be present");
duke@0 267 // native nmethods don't have exception handlers
duke@0 268 assert(!code->is_native_method(), "no exception handler");
duke@0 269 assert(code->header_begin() != code->exception_begin(), "no exception handler");
duke@0 270 if (code->is_deopt_pc(return_address)) {
duke@0 271 return SharedRuntime::deopt_blob()->unpack_with_exception();
duke@0 272 } else {
duke@0 273 return code->exception_begin();
duke@0 274 }
duke@0 275 }
duke@0 276
duke@0 277 // Entry code
duke@0 278 if (StubRoutines::returns_to_call_stub(return_address)) {
duke@0 279 return StubRoutines::catch_exception_entry();
duke@0 280 }
duke@0 281 // Interpreted code
duke@0 282 if (Interpreter::contains(return_address)) {
duke@0 283 return Interpreter::rethrow_exception_entry();
duke@0 284 }
duke@0 285
duke@0 286 // Compiled code
duke@0 287 if (CodeCache::contains(return_address)) {
duke@0 288 CodeBlob* blob = CodeCache::find_blob(return_address);
duke@0 289 if (blob->is_nmethod()) {
duke@0 290 nmethod* code = (nmethod*)blob;
duke@0 291 assert(code != NULL, "nmethod must be present");
duke@0 292 assert(code->header_begin() != code->exception_begin(), "no exception handler");
duke@0 293 return code->exception_begin();
duke@0 294 }
duke@0 295 if (blob->is_runtime_stub()) {
duke@0 296 ShouldNotReachHere(); // callers are responsible for skipping runtime stub frames
duke@0 297 }
duke@0 298 }
duke@0 299 guarantee(!VtableStubs::contains(return_address), "NULL exceptions in vtables should have been handled already!");
duke@0 300 #ifndef PRODUCT
duke@0 301 { ResourceMark rm;
duke@0 302 tty->print_cr("No exception handler found for exception at " INTPTR_FORMAT " - potential problems:", return_address);
duke@0 303 tty->print_cr("a) exception happened in (new?) code stubs/buffers that is not handled here");
duke@0 304 tty->print_cr("b) other problem");
duke@0 305 }
duke@0 306 #endif // PRODUCT
duke@0 307 ShouldNotReachHere();
duke@0 308 return NULL;
duke@0 309 }
duke@0 310
duke@0 311
duke@0 312 JRT_LEAF(address, SharedRuntime::exception_handler_for_return_address(address return_address))
duke@0 313 return raw_exception_handler_for_return_address(return_address);
duke@0 314 JRT_END
duke@0 315
duke@0 316 address SharedRuntime::get_poll_stub(address pc) {
duke@0 317 address stub;
duke@0 318 // Look up the code blob
duke@0 319 CodeBlob *cb = CodeCache::find_blob(pc);
duke@0 320
duke@0 321 // Should be an nmethod
duke@0 322 assert( cb && cb->is_nmethod(), "safepoint polling: pc must refer to an nmethod" );
duke@0 323
duke@0 324 // Look up the relocation information
duke@0 325 assert( ((nmethod*)cb)->is_at_poll_or_poll_return(pc),
duke@0 326 "safepoint polling: type must be poll" );
duke@0 327
duke@0 328 assert( ((NativeInstruction*)pc)->is_safepoint_poll(),
duke@0 329 "Only polling locations are used for safepoint");
duke@0 330
duke@0 331 bool at_poll_return = ((nmethod*)cb)->is_at_poll_return(pc);
duke@0 332 if (at_poll_return) {
duke@0 333 assert(SharedRuntime::polling_page_return_handler_blob() != NULL,
duke@0 334 "polling page return stub not created yet");
duke@0 335 stub = SharedRuntime::polling_page_return_handler_blob()->instructions_begin();
duke@0 336 } else {
duke@0 337 assert(SharedRuntime::polling_page_safepoint_handler_blob() != NULL,
duke@0 338 "polling page safepoint stub not created yet");
duke@0 339 stub = SharedRuntime::polling_page_safepoint_handler_blob()->instructions_begin();
duke@0 340 }
duke@0 341 #ifndef PRODUCT
duke@0 342 if( TraceSafepoint ) {
duke@0 343 char buf[256];
duke@0 344 jio_snprintf(buf, sizeof(buf),
duke@0 345 "... found polling page %s exception at pc = "
duke@0 346 INTPTR_FORMAT ", stub =" INTPTR_FORMAT,
duke@0 347 at_poll_return ? "return" : "loop",
duke@0 348 (intptr_t)pc, (intptr_t)stub);
duke@0 349 tty->print_raw_cr(buf);
duke@0 350 }
duke@0 351 #endif // PRODUCT
duke@0 352 return stub;
duke@0 353 }
duke@0 354
duke@0 355
duke@0 356 oop SharedRuntime::retrieve_receiver( symbolHandle sig, frame caller ) {
duke@0 357 assert(caller.is_interpreted_frame(), "");
duke@0 358 int args_size = ArgumentSizeComputer(sig).size() + 1;
duke@0 359 assert(args_size <= caller.interpreter_frame_expression_stack_size(), "receiver must be on interpreter stack");
duke@0 360 oop result = (oop) *caller.interpreter_frame_tos_at(args_size - 1);
duke@0 361 assert(Universe::heap()->is_in(result) && result->is_oop(), "receiver must be an oop");
duke@0 362 return result;
duke@0 363 }
duke@0 364
duke@0 365
duke@0 366 void SharedRuntime::throw_and_post_jvmti_exception(JavaThread *thread, Handle h_exception) {
duke@0 367 if (JvmtiExport::can_post_exceptions()) {
duke@0 368 vframeStream vfst(thread, true);
duke@0 369 methodHandle method = methodHandle(thread, vfst.method());
duke@0 370 address bcp = method()->bcp_from(vfst.bci());
duke@0 371 JvmtiExport::post_exception_throw(thread, method(), bcp, h_exception());
duke@0 372 }
duke@0 373 Exceptions::_throw(thread, __FILE__, __LINE__, h_exception);
duke@0 374 }
duke@0 375
duke@0 376 void SharedRuntime::throw_and_post_jvmti_exception(JavaThread *thread, symbolOop name, const char *message) {
duke@0 377 Handle h_exception = Exceptions::new_exception(thread, name, message);
duke@0 378 throw_and_post_jvmti_exception(thread, h_exception);
duke@0 379 }
duke@0 380
dcubed@610 381 // The interpreter code to call this tracing function is only
dcubed@610 382 // called/generated when TraceRedefineClasses has the right bits
dcubed@610 383 // set. Since obsolete methods are never compiled, we don't have
dcubed@610 384 // to modify the compilers to generate calls to this function.
dcubed@610 385 //
dcubed@610 386 JRT_LEAF(int, SharedRuntime::rc_trace_method_entry(
dcubed@610 387 JavaThread* thread, methodOopDesc* method))
dcubed@610 388 assert(RC_TRACE_IN_RANGE(0x00001000, 0x00002000), "wrong call");
dcubed@610 389
dcubed@610 390 if (method->is_obsolete()) {
dcubed@610 391 // We are calling an obsolete method, but this is not necessarily
dcubed@610 392 // an error. Our method could have been redefined just after we
dcubed@610 393 // fetched the methodOop from the constant pool.
dcubed@610 394
dcubed@610 395 // RC_TRACE macro has an embedded ResourceMark
dcubed@610 396 RC_TRACE_WITH_THREAD(0x00001000, thread,
dcubed@610 397 ("calling obsolete method '%s'",
dcubed@610 398 method->name_and_sig_as_C_string()));
dcubed@610 399 if (RC_TRACE_ENABLED(0x00002000)) {
dcubed@610 400 // this option is provided to debug calls to obsolete methods
dcubed@610 401 guarantee(false, "faulting at call to an obsolete method.");
dcubed@610 402 }
dcubed@610 403 }
dcubed@610 404 return 0;
dcubed@610 405 JRT_END
dcubed@610 406
duke@0 407 // ret_pc points into caller; we are returning caller's exception handler
duke@0 408 // for given exception
duke@0 409 address SharedRuntime::compute_compiled_exc_handler(nmethod* nm, address ret_pc, Handle& exception,
duke@0 410 bool force_unwind, bool top_frame_only) {
duke@0 411 assert(nm != NULL, "must exist");
duke@0 412 ResourceMark rm;
duke@0 413
duke@0 414 ScopeDesc* sd = nm->scope_desc_at(ret_pc);
duke@0 415 // determine handler bci, if any
duke@0 416 EXCEPTION_MARK;
duke@0 417
duke@0 418 int handler_bci = -1;
duke@0 419 int scope_depth = 0;
duke@0 420 if (!force_unwind) {
duke@0 421 int bci = sd->bci();
duke@0 422 do {
duke@0 423 bool skip_scope_increment = false;
duke@0 424 // exception handler lookup
duke@0 425 KlassHandle ek (THREAD, exception->klass());
duke@0 426 handler_bci = sd->method()->fast_exception_handler_bci_for(ek, bci, THREAD);
duke@0 427 if (HAS_PENDING_EXCEPTION) {
duke@0 428 // We threw an exception while trying to find the exception handler.
duke@0 429 // Transfer the new exception to the exception handle which will
duke@0 430 // be set into thread local storage, and do another lookup for an
duke@0 431 // exception handler for this exception, this time starting at the
duke@0 432 // BCI of the exception handler which caused the exception to be
duke@0 433 // thrown (bugs 4307310 and 4546590). Set "exception" reference
duke@0 434 // argument to ensure that the correct exception is thrown (4870175).
duke@0 435 exception = Handle(THREAD, PENDING_EXCEPTION);
duke@0 436 CLEAR_PENDING_EXCEPTION;
duke@0 437 if (handler_bci >= 0) {
duke@0 438 bci = handler_bci;
duke@0 439 handler_bci = -1;
duke@0 440 skip_scope_increment = true;
duke@0 441 }
duke@0 442 }
duke@0 443 if (!top_frame_only && handler_bci < 0 && !skip_scope_increment) {
duke@0 444 sd = sd->sender();
duke@0 445 if (sd != NULL) {
duke@0 446 bci = sd->bci();
duke@0 447 }
duke@0 448 ++scope_depth;
duke@0 449 }
duke@0 450 } while (!top_frame_only && handler_bci < 0 && sd != NULL);
duke@0 451 }
duke@0 452
duke@0 453 // found handling method => lookup exception handler
duke@0 454 int catch_pco = ret_pc - nm->instructions_begin();
duke@0 455
duke@0 456 ExceptionHandlerTable table(nm);
duke@0 457 HandlerTableEntry *t = table.entry_for(catch_pco, handler_bci, scope_depth);
duke@0 458 if (t == NULL && (nm->is_compiled_by_c1() || handler_bci != -1)) {
duke@0 459 // Allow abbreviated catch tables. The idea is to allow a method
duke@0 460 // to materialize its exceptions without committing to the exact
duke@0 461 // routing of exceptions. In particular this is needed for adding
duke@0 462 // a synthethic handler to unlock monitors when inlining
duke@0 463 // synchonized methods since the unlock path isn't represented in
duke@0 464 // the bytecodes.
duke@0 465 t = table.entry_for(catch_pco, -1, 0);
duke@0 466 }
duke@0 467
duke@0 468 #ifdef COMPILER1
duke@0 469 if (nm->is_compiled_by_c1() && t == NULL && handler_bci == -1) {
duke@0 470 // Exception is not handled by this frame so unwind. Note that
duke@0 471 // this is not the same as how C2 does this. C2 emits a table
duke@0 472 // entry that dispatches to the unwind code in the nmethod.
duke@0 473 return NULL;
duke@0 474 }
duke@0 475 #endif /* COMPILER1 */
duke@0 476
duke@0 477
duke@0 478 if (t == NULL) {
duke@0 479 tty->print_cr("MISSING EXCEPTION HANDLER for pc " INTPTR_FORMAT " and handler bci %d", ret_pc, handler_bci);
duke@0 480 tty->print_cr(" Exception:");
duke@0 481 exception->print();
duke@0 482 tty->cr();
duke@0 483 tty->print_cr(" Compiled exception table :");
duke@0 484 table.print();
duke@0 485 nm->print_code();
duke@0 486 guarantee(false, "missing exception handler");
duke@0 487 return NULL;
duke@0 488 }
duke@0 489
duke@0 490 return nm->instructions_begin() + t->pco();
duke@0 491 }
duke@0 492
duke@0 493 JRT_ENTRY(void, SharedRuntime::throw_AbstractMethodError(JavaThread* thread))
duke@0 494 // These errors occur only at call sites
duke@0 495 throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_AbstractMethodError());
duke@0 496 JRT_END
duke@0 497
dcubed@16 498 JRT_ENTRY(void, SharedRuntime::throw_IncompatibleClassChangeError(JavaThread* thread))
dcubed@16 499 // These errors occur only at call sites
dcubed@16 500 throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_IncompatibleClassChangeError(), "vtable stub");
dcubed@16 501 JRT_END
dcubed@16 502
duke@0 503 JRT_ENTRY(void, SharedRuntime::throw_ArithmeticException(JavaThread* thread))
duke@0 504 throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_ArithmeticException(), "/ by zero");
duke@0 505 JRT_END
duke@0 506
duke@0 507 JRT_ENTRY(void, SharedRuntime::throw_NullPointerException(JavaThread* thread))
duke@0 508 throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_NullPointerException());
duke@0 509 JRT_END
duke@0 510
duke@0 511 JRT_ENTRY(void, SharedRuntime::throw_NullPointerException_at_call(JavaThread* thread))
duke@0 512 // This entry point is effectively only used for NullPointerExceptions which occur at inline
duke@0 513 // cache sites (when the callee activation is not yet set up) so we are at a call site
duke@0 514 throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_NullPointerException());
duke@0 515 JRT_END
duke@0 516
duke@0 517 JRT_ENTRY(void, SharedRuntime::throw_StackOverflowError(JavaThread* thread))
duke@0 518 // We avoid using the normal exception construction in this case because
duke@0 519 // it performs an upcall to Java, and we're already out of stack space.
duke@0 520 klassOop k = SystemDictionary::StackOverflowError_klass();
duke@0 521 oop exception_oop = instanceKlass::cast(k)->allocate_instance(CHECK);
duke@0 522 Handle exception (thread, exception_oop);
duke@0 523 if (StackTraceInThrowable) {
duke@0 524 java_lang_Throwable::fill_in_stack_trace(exception);
duke@0 525 }
duke@0 526 throw_and_post_jvmti_exception(thread, exception);
duke@0 527 JRT_END
duke@0 528
duke@0 529 address SharedRuntime::continuation_for_implicit_exception(JavaThread* thread,
duke@0 530 address pc,
duke@0 531 SharedRuntime::ImplicitExceptionKind exception_kind)
duke@0 532 {
duke@0 533 address target_pc = NULL;
duke@0 534
duke@0 535 if (Interpreter::contains(pc)) {
duke@0 536 #ifdef CC_INTERP
duke@0 537 // C++ interpreter doesn't throw implicit exceptions
duke@0 538 ShouldNotReachHere();
duke@0 539 #else
duke@0 540 switch (exception_kind) {
duke@0 541 case IMPLICIT_NULL: return Interpreter::throw_NullPointerException_entry();
duke@0 542 case IMPLICIT_DIVIDE_BY_ZERO: return Interpreter::throw_ArithmeticException_entry();
duke@0 543 case STACK_OVERFLOW: return Interpreter::throw_StackOverflowError_entry();
duke@0 544 default: ShouldNotReachHere();
duke@0 545 }
duke@0 546 #endif // !CC_INTERP
duke@0 547 } else {
duke@0 548 switch (exception_kind) {
duke@0 549 case STACK_OVERFLOW: {
duke@0 550 // Stack overflow only occurs upon frame setup; the callee is
duke@0 551 // going to be unwound. Dispatch to a shared runtime stub
duke@0 552 // which will cause the StackOverflowError to be fabricated
duke@0 553 // and processed.
duke@0 554 // For stack overflow in deoptimization blob, cleanup thread.
duke@0 555 if (thread->deopt_mark() != NULL) {
duke@0 556 Deoptimization::cleanup_deopt_info(thread, NULL);
duke@0 557 }
duke@0 558 return StubRoutines::throw_StackOverflowError_entry();
duke@0 559 }
duke@0 560
duke@0 561 case IMPLICIT_NULL: {
duke@0 562 if (VtableStubs::contains(pc)) {
duke@0 563 // We haven't yet entered the callee frame. Fabricate an
duke@0 564 // exception and begin dispatching it in the caller. Since
duke@0 565 // the caller was at a call site, it's safe to destroy all
duke@0 566 // caller-saved registers, as these entry points do.
duke@0 567 VtableStub* vt_stub = VtableStubs::stub_containing(pc);
poonam@465 568
poonam@465 569 // If vt_stub is NULL, then return NULL to signal handler to report the SEGV error.
poonam@465 570 if (vt_stub == NULL) return NULL;
poonam@465 571
duke@0 572 if (vt_stub->is_abstract_method_error(pc)) {
duke@0 573 assert(!vt_stub->is_vtable_stub(), "should never see AbstractMethodErrors from vtable-type VtableStubs");
duke@0 574 return StubRoutines::throw_AbstractMethodError_entry();
duke@0 575 } else {
duke@0 576 return StubRoutines::throw_NullPointerException_at_call_entry();
duke@0 577 }
duke@0 578 } else {
duke@0 579 CodeBlob* cb = CodeCache::find_blob(pc);
poonam@465 580
poonam@465 581 // If code blob is NULL, then return NULL to signal handler to report the SEGV error.
poonam@465 582 if (cb == NULL) return NULL;
duke@0 583
duke@0 584 // Exception happened in CodeCache. Must be either:
duke@0 585 // 1. Inline-cache check in C2I handler blob,
duke@0 586 // 2. Inline-cache check in nmethod, or
duke@0 587 // 3. Implict null exception in nmethod
duke@0 588
duke@0 589 if (!cb->is_nmethod()) {
duke@0 590 guarantee(cb->is_adapter_blob(),
poonam@465 591 "exception happened outside interpreter, nmethods and vtable stubs (1)");
duke@0 592 // There is no handler here, so we will simply unwind.
duke@0 593 return StubRoutines::throw_NullPointerException_at_call_entry();
duke@0 594 }
duke@0 595
duke@0 596 // Otherwise, it's an nmethod. Consult its exception handlers.
duke@0 597 nmethod* nm = (nmethod*)cb;
duke@0 598 if (nm->inlinecache_check_contains(pc)) {
duke@0 599 // exception happened inside inline-cache check code
duke@0 600 // => the nmethod is not yet active (i.e., the frame
duke@0 601 // is not set up yet) => use return address pushed by
duke@0 602 // caller => don't push another return address
duke@0 603 return StubRoutines::throw_NullPointerException_at_call_entry();
duke@0 604 }
duke@0 605
duke@0 606 #ifndef PRODUCT
duke@0 607 _implicit_null_throws++;
duke@0 608 #endif
duke@0 609 target_pc = nm->continuation_for_implicit_exception(pc);
duke@0 610 guarantee(target_pc != 0, "must have a continuation point");
duke@0 611 }
duke@0 612
duke@0 613 break; // fall through
duke@0 614 }
duke@0 615
duke@0 616
duke@0 617 case IMPLICIT_DIVIDE_BY_ZERO: {
duke@0 618 nmethod* nm = CodeCache::find_nmethod(pc);
duke@0 619 guarantee(nm != NULL, "must have containing nmethod for implicit division-by-zero exceptions");
duke@0 620 #ifndef PRODUCT
duke@0 621 _implicit_div0_throws++;
duke@0 622 #endif
duke@0 623 target_pc = nm->continuation_for_implicit_exception(pc);
duke@0 624 guarantee(target_pc != 0, "must have a continuation point");
duke@0 625 break; // fall through
duke@0 626 }
duke@0 627
duke@0 628 default: ShouldNotReachHere();
duke@0 629 }
duke@0 630
duke@0 631 guarantee(target_pc != NULL, "must have computed destination PC for implicit exception");
duke@0 632 assert(exception_kind == IMPLICIT_NULL || exception_kind == IMPLICIT_DIVIDE_BY_ZERO, "wrong implicit exception kind");
duke@0 633
duke@0 634 // for AbortVMOnException flag
duke@0 635 NOT_PRODUCT(Exceptions::debug_check_abort("java.lang.NullPointerException"));
duke@0 636 if (exception_kind == IMPLICIT_NULL) {
duke@0 637 Events::log("Implicit null exception at " INTPTR_FORMAT " to " INTPTR_FORMAT, pc, target_pc);
duke@0 638 } else {
duke@0 639 Events::log("Implicit division by zero exception at " INTPTR_FORMAT " to " INTPTR_FORMAT, pc, target_pc);
duke@0 640 }
duke@0 641 return target_pc;
duke@0 642 }
duke@0 643
duke@0 644 ShouldNotReachHere();
duke@0 645 return NULL;
duke@0 646 }
duke@0 647
duke@0 648
duke@0 649 JNI_ENTRY(void, throw_unsatisfied_link_error(JNIEnv* env, ...))
duke@0 650 {
duke@0 651 THROW(vmSymbols::java_lang_UnsatisfiedLinkError());
duke@0 652 }
duke@0 653 JNI_END
duke@0 654
duke@0 655
duke@0 656 address SharedRuntime::native_method_throw_unsatisfied_link_error_entry() {
duke@0 657 return CAST_FROM_FN_PTR(address, &throw_unsatisfied_link_error);
duke@0 658 }
duke@0 659
duke@0 660
duke@0 661 #ifndef PRODUCT
duke@0 662 JRT_ENTRY(intptr_t, SharedRuntime::trace_bytecode(JavaThread* thread, intptr_t preserve_this_value, intptr_t tos, intptr_t tos2))
duke@0 663 const frame f = thread->last_frame();
duke@0 664 assert(f.is_interpreted_frame(), "must be an interpreted frame");
duke@0 665 #ifndef PRODUCT
duke@0 666 methodHandle mh(THREAD, f.interpreter_frame_method());
duke@0 667 BytecodeTracer::trace(mh, f.interpreter_frame_bcp(), tos, tos2);
duke@0 668 #endif // !PRODUCT
duke@0 669 return preserve_this_value;
duke@0 670 JRT_END
duke@0 671 #endif // !PRODUCT
duke@0 672
duke@0 673
duke@0 674 JRT_ENTRY(void, SharedRuntime::yield_all(JavaThread* thread, int attempts))
duke@0 675 os::yield_all(attempts);
duke@0 676 JRT_END
duke@0 677
duke@0 678
duke@0 679 JRT_ENTRY_NO_ASYNC(void, SharedRuntime::register_finalizer(JavaThread* thread, oopDesc* obj))
duke@0 680 assert(obj->is_oop(), "must be a valid oop");
duke@0 681 assert(obj->klass()->klass_part()->has_finalizer(), "shouldn't be here otherwise");
duke@0 682 instanceKlass::register_finalizer(instanceOop(obj), CHECK);
duke@0 683 JRT_END
duke@0 684
duke@0 685
duke@0 686 jlong SharedRuntime::get_java_tid(Thread* thread) {
duke@0 687 if (thread != NULL) {
duke@0 688 if (thread->is_Java_thread()) {
duke@0 689 oop obj = ((JavaThread*)thread)->threadObj();
duke@0 690 return (obj == NULL) ? 0 : java_lang_Thread::thread_id(obj);
duke@0 691 }
duke@0 692 }
duke@0 693 return 0;
duke@0 694 }
duke@0 695
duke@0 696 /**
duke@0 697 * This function ought to be a void function, but cannot be because
duke@0 698 * it gets turned into a tail-call on sparc, which runs into dtrace bug
duke@0 699 * 6254741. Once that is fixed we can remove the dummy return value.
duke@0 700 */
duke@0 701 int SharedRuntime::dtrace_object_alloc(oopDesc* o) {
duke@0 702 return dtrace_object_alloc_base(Thread::current(), o);
duke@0 703 }
duke@0 704
duke@0 705 int SharedRuntime::dtrace_object_alloc_base(Thread* thread, oopDesc* o) {
duke@0 706 assert(DTraceAllocProbes, "wrong call");
duke@0 707 Klass* klass = o->blueprint();
duke@0 708 int size = o->size();
duke@0 709 symbolOop name = klass->name();
duke@0 710 HS_DTRACE_PROBE4(hotspot, object__alloc, get_java_tid(thread),
duke@0 711 name->bytes(), name->utf8_length(), size * HeapWordSize);
duke@0 712 return 0;
duke@0 713 }
duke@0 714
duke@0 715 JRT_LEAF(int, SharedRuntime::dtrace_method_entry(
duke@0 716 JavaThread* thread, methodOopDesc* method))
duke@0 717 assert(DTraceMethodProbes, "wrong call");
duke@0 718 symbolOop kname = method->klass_name();
duke@0 719 symbolOop name = method->name();
duke@0 720 symbolOop sig = method->signature();
duke@0 721 HS_DTRACE_PROBE7(hotspot, method__entry, get_java_tid(thread),
duke@0 722 kname->bytes(), kname->utf8_length(),
duke@0 723 name->bytes(), name->utf8_length(),
duke@0 724 sig->bytes(), sig->utf8_length());
duke@0 725 return 0;
duke@0 726 JRT_END
duke@0 727
duke@0 728 JRT_LEAF(int, SharedRuntime::dtrace_method_exit(
duke@0 729 JavaThread* thread, methodOopDesc* method))
duke@0 730 assert(DTraceMethodProbes, "wrong call");
duke@0 731 symbolOop kname = method->klass_name();
duke@0 732 symbolOop name = method->name();
duke@0 733 symbolOop sig = method->signature();
duke@0 734 HS_DTRACE_PROBE7(hotspot, method__return, get_java_tid(thread),
duke@0 735 kname->bytes(), kname->utf8_length(),
duke@0 736 name->bytes(), name->utf8_length(),
duke@0 737 sig->bytes(), sig->utf8_length());
duke@0 738 return 0;
duke@0 739 JRT_END
duke@0 740
duke@0 741
duke@0 742 // Finds receiver, CallInfo (i.e. receiver method), and calling bytecode)
duke@0 743 // for a call current in progress, i.e., arguments has been pushed on stack
duke@0 744 // put callee has not been invoked yet. Used by: resolve virtual/static,
duke@0 745 // vtable updates, etc. Caller frame must be compiled.
duke@0 746 Handle SharedRuntime::find_callee_info(JavaThread* thread, Bytecodes::Code& bc, CallInfo& callinfo, TRAPS) {
duke@0 747 ResourceMark rm(THREAD);
duke@0 748
duke@0 749 // last java frame on stack (which includes native call frames)
duke@0 750 vframeStream vfst(thread, true); // Do not skip and javaCalls
duke@0 751
duke@0 752 return find_callee_info_helper(thread, vfst, bc, callinfo, CHECK_(Handle()));
duke@0 753 }
duke@0 754
duke@0 755
duke@0 756 // Finds receiver, CallInfo (i.e. receiver method), and calling bytecode
duke@0 757 // for a call current in progress, i.e., arguments has been pushed on stack
duke@0 758 // but callee has not been invoked yet. Caller frame must be compiled.
duke@0 759 Handle SharedRuntime::find_callee_info_helper(JavaThread* thread,
duke@0 760 vframeStream& vfst,
duke@0 761 Bytecodes::Code& bc,
duke@0 762 CallInfo& callinfo, TRAPS) {
duke@0 763 Handle receiver;
duke@0 764 Handle nullHandle; //create a handy null handle for exception returns
duke@0 765
duke@0 766 assert(!vfst.at_end(), "Java frame must exist");
duke@0 767
duke@0 768 // Find caller and bci from vframe
duke@0 769 methodHandle caller (THREAD, vfst.method());
duke@0 770 int bci = vfst.bci();
duke@0 771
duke@0 772 // Find bytecode
duke@0 773 Bytecode_invoke* bytecode = Bytecode_invoke_at(caller, bci);
duke@0 774 bc = bytecode->adjusted_invoke_code();
duke@0 775 int bytecode_index = bytecode->index();
duke@0 776
duke@0 777 // Find receiver for non-static call
duke@0 778 if (bc != Bytecodes::_invokestatic) {
duke@0 779 // This register map must be update since we need to find the receiver for
duke@0 780 // compiled frames. The receiver might be in a register.
duke@0 781 RegisterMap reg_map2(thread);
duke@0 782 frame stubFrame = thread->last_frame();
duke@0 783 // Caller-frame is a compiled frame
duke@0 784 frame callerFrame = stubFrame.sender(&reg_map2);
duke@0 785
duke@0 786 methodHandle callee = bytecode->static_target(CHECK_(nullHandle));
duke@0 787 if (callee.is_null()) {
duke@0 788 THROW_(vmSymbols::java_lang_NoSuchMethodException(), nullHandle);
duke@0 789 }
duke@0 790 // Retrieve from a compiled argument list
duke@0 791 receiver = Handle(THREAD, callerFrame.retrieve_receiver(&reg_map2));
duke@0 792
duke@0 793 if (receiver.is_null()) {
duke@0 794 THROW_(vmSymbols::java_lang_NullPointerException(), nullHandle);
duke@0 795 }
duke@0 796 }
duke@0 797
duke@0 798 // Resolve method. This is parameterized by bytecode.
duke@0 799 constantPoolHandle constants (THREAD, caller->constants());
duke@0 800 assert (receiver.is_null() || receiver->is_oop(), "wrong receiver");
duke@0 801 LinkResolver::resolve_invoke(callinfo, receiver, constants, bytecode_index, bc, CHECK_(nullHandle));
duke@0 802
duke@0 803 #ifdef ASSERT
duke@0 804 // Check that the receiver klass is of the right subtype and that it is initialized for virtual calls
twisti@1135 805 if (bc != Bytecodes::_invokestatic && bc != Bytecodes::_invokedynamic) {
duke@0 806 assert(receiver.not_null(), "should have thrown exception");
duke@0 807 KlassHandle receiver_klass (THREAD, receiver->klass());
duke@0 808 klassOop rk = constants->klass_ref_at(bytecode_index, CHECK_(nullHandle));
duke@0 809 // klass is already loaded
duke@0 810 KlassHandle static_receiver_klass (THREAD, rk);
duke@0 811 assert(receiver_klass->is_subtype_of(static_receiver_klass()), "actual receiver must be subclass of static receiver klass");
duke@0 812 if (receiver_klass->oop_is_instance()) {
duke@0 813 if (instanceKlass::cast(receiver_klass())->is_not_initialized()) {
duke@0 814 tty->print_cr("ERROR: Klass not yet initialized!!");
duke@0 815 receiver_klass.print();
duke@0 816 }
duke@0 817 assert (!instanceKlass::cast(receiver_klass())->is_not_initialized(), "receiver_klass must be initialized");
duke@0 818 }
duke@0 819 }
duke@0 820 #endif
duke@0 821
duke@0 822 return receiver;
duke@0 823 }
duke@0 824
duke@0 825 methodHandle SharedRuntime::find_callee_method(JavaThread* thread, TRAPS) {
duke@0 826 ResourceMark rm(THREAD);
duke@0 827 // We need first to check if any Java activations (compiled, interpreted)
duke@0 828 // exist on the stack since last JavaCall. If not, we need
duke@0 829 // to get the target method from the JavaCall wrapper.
duke@0 830 vframeStream vfst(thread, true); // Do not skip any javaCalls
duke@0 831 methodHandle callee_method;
duke@0 832 if (vfst.at_end()) {
duke@0 833 // No Java frames were found on stack since we did the JavaCall.
duke@0 834 // Hence the stack can only contain an entry_frame. We need to
duke@0 835 // find the target method from the stub frame.
duke@0 836 RegisterMap reg_map(thread, false);
duke@0 837 frame fr = thread->last_frame();
duke@0 838 assert(fr.is_runtime_frame(), "must be a runtimeStub");
duke@0 839 fr = fr.sender(&reg_map);
duke@0 840 assert(fr.is_entry_frame(), "must be");
duke@0 841 // fr is now pointing to the entry frame.
duke@0 842 callee_method = methodHandle(THREAD, fr.entry_frame_call_wrapper()->callee_method());
duke@0 843 assert(fr.entry_frame_call_wrapper()->receiver() == NULL || !callee_method->is_static(), "non-null receiver for static call??");
duke@0 844 } else {
duke@0 845 Bytecodes::Code bc;
duke@0 846 CallInfo callinfo;
duke@0 847 find_callee_info_helper(thread, vfst, bc, callinfo, CHECK_(methodHandle()));
duke@0 848 callee_method = callinfo.selected_method();
duke@0 849 }
duke@0 850 assert(callee_method()->is_method(), "must be");
duke@0 851 return callee_method;
duke@0 852 }
duke@0 853
duke@0 854 // Resolves a call.
duke@0 855 methodHandle SharedRuntime::resolve_helper(JavaThread *thread,
duke@0 856 bool is_virtual,
duke@0 857 bool is_optimized, TRAPS) {
duke@0 858 methodHandle callee_method;
duke@0 859 callee_method = resolve_sub_helper(thread, is_virtual, is_optimized, THREAD);
duke@0 860 if (JvmtiExport::can_hotswap_or_post_breakpoint()) {
duke@0 861 int retry_count = 0;
duke@0 862 while (!HAS_PENDING_EXCEPTION && callee_method->is_old() &&
never@1142 863 callee_method->method_holder() != SystemDictionary::Object_klass()) {
duke@0 864 // If has a pending exception then there is no need to re-try to
duke@0 865 // resolve this method.
duke@0 866 // If the method has been redefined, we need to try again.
duke@0 867 // Hack: we have no way to update the vtables of arrays, so don't
duke@0 868 // require that java.lang.Object has been updated.
duke@0 869
duke@0 870 // It is very unlikely that method is redefined more than 100 times
duke@0 871 // in the middle of resolve. If it is looping here more than 100 times
duke@0 872 // means then there could be a bug here.
duke@0 873 guarantee((retry_count++ < 100),
duke@0 874 "Could not resolve to latest version of redefined method");
duke@0 875 // method is redefined in the middle of resolve so re-try.
duke@0 876 callee_method = resolve_sub_helper(thread, is_virtual, is_optimized, THREAD);
duke@0 877 }
duke@0 878 }
duke@0 879 return callee_method;
duke@0 880 }
duke@0 881
duke@0 882 // Resolves a call. The compilers generate code for calls that go here
duke@0 883 // and are patched with the real destination of the call.
duke@0 884 methodHandle SharedRuntime::resolve_sub_helper(JavaThread *thread,
duke@0 885 bool is_virtual,
duke@0 886 bool is_optimized, TRAPS) {
duke@0 887
duke@0 888 ResourceMark rm(thread);
duke@0 889 RegisterMap cbl_map(thread, false);
duke@0 890 frame caller_frame = thread->last_frame().sender(&cbl_map);
duke@0 891
duke@0 892 CodeBlob* cb = caller_frame.cb();
duke@0 893 guarantee(cb != NULL && cb->is_nmethod(), "must be called from nmethod");
duke@0 894 // make sure caller is not getting deoptimized
duke@0 895 // and removed before we are done with it.
duke@0 896 // CLEANUP - with lazy deopt shouldn't need this lock
duke@0 897 nmethodLocker caller_lock((nmethod*)cb);
duke@0 898
duke@0 899
duke@0 900 // determine call info & receiver
duke@0 901 // note: a) receiver is NULL for static calls
duke@0 902 // b) an exception is thrown if receiver is NULL for non-static calls
duke@0 903 CallInfo call_info;
duke@0 904 Bytecodes::Code invoke_code = Bytecodes::_illegal;
duke@0 905 Handle receiver = find_callee_info(thread, invoke_code,
duke@0 906 call_info, CHECK_(methodHandle()));
duke@0 907 methodHandle callee_method = call_info.selected_method();
duke@0 908
duke@0 909 assert((!is_virtual && invoke_code == Bytecodes::_invokestatic) ||
duke@0 910 ( is_virtual && invoke_code != Bytecodes::_invokestatic), "inconsistent bytecode");
duke@0 911
duke@0 912 #ifndef PRODUCT
duke@0 913 // tracing/debugging/statistics
duke@0 914 int *addr = (is_optimized) ? (&_resolve_opt_virtual_ctr) :
duke@0 915 (is_virtual) ? (&_resolve_virtual_ctr) :
duke@0 916 (&_resolve_static_ctr);
duke@0 917 Atomic::inc(addr);
duke@0 918
duke@0 919 if (TraceCallFixup) {
duke@0 920 ResourceMark rm(thread);
duke@0 921 tty->print("resolving %s%s (%s) call to",
duke@0 922 (is_optimized) ? "optimized " : "", (is_virtual) ? "virtual" : "static",
duke@0 923 Bytecodes::name(invoke_code));
duke@0 924 callee_method->print_short_name(tty);
duke@0 925 tty->print_cr(" code: " INTPTR_FORMAT, callee_method->code());
duke@0 926 }
duke@0 927 #endif
duke@0 928
duke@0 929 // Compute entry points. This might require generation of C2I converter
duke@0 930 // frames, so we cannot be holding any locks here. Furthermore, the
duke@0 931 // computation of the entry points is independent of patching the call. We
duke@0 932 // always return the entry-point, but we only patch the stub if the call has
duke@0 933 // not been deoptimized. Return values: For a virtual call this is an
duke@0 934 // (cached_oop, destination address) pair. For a static call/optimized
duke@0 935 // virtual this is just a destination address.
duke@0 936
duke@0 937 StaticCallInfo static_call_info;
duke@0 938 CompiledICInfo virtual_call_info;
duke@0 939
duke@0 940
duke@0 941 // Make sure the callee nmethod does not get deoptimized and removed before
duke@0 942 // we are done patching the code.
duke@0 943 nmethod* nm = callee_method->code();
duke@0 944 nmethodLocker nl_callee(nm);
duke@0 945 #ifdef ASSERT
duke@0 946 address dest_entry_point = nm == NULL ? 0 : nm->entry_point(); // used below
duke@0 947 #endif
duke@0 948
duke@0 949 if (is_virtual) {
duke@0 950 assert(receiver.not_null(), "sanity check");
duke@0 951 bool static_bound = call_info.resolved_method()->can_be_statically_bound();
duke@0 952 KlassHandle h_klass(THREAD, receiver->klass());
duke@0 953 CompiledIC::compute_monomorphic_entry(callee_method, h_klass,
duke@0 954 is_optimized, static_bound, virtual_call_info,
duke@0 955 CHECK_(methodHandle()));
duke@0 956 } else {
duke@0 957 // static call
duke@0 958 CompiledStaticCall::compute_entry(callee_method, static_call_info);
duke@0 959 }
duke@0 960
duke@0 961 // grab lock, check for deoptimization and potentially patch caller
duke@0 962 {
duke@0 963 MutexLocker ml_patch(CompiledIC_lock);
duke@0 964
duke@0 965 // Now that we are ready to patch if the methodOop was redefined then
duke@0 966 // don't update call site and let the caller retry.
duke@0 967
duke@0 968 if (!callee_method->is_old()) {
duke@0 969 #ifdef ASSERT
duke@0 970 // We must not try to patch to jump to an already unloaded method.
duke@0 971 if (dest_entry_point != 0) {
duke@0 972 assert(CodeCache::find_blob(dest_entry_point) != NULL,
duke@0 973 "should not unload nmethod while locked");
duke@0 974 }
duke@0 975 #endif
duke@0 976 if (is_virtual) {
duke@0 977 CompiledIC* inline_cache = CompiledIC_before(caller_frame.pc());
duke@0 978 if (inline_cache->is_clean()) {
duke@0 979 inline_cache->set_to_monomorphic(virtual_call_info);
duke@0 980 }
duke@0 981 } else {
duke@0 982 CompiledStaticCall* ssc = compiledStaticCall_before(caller_frame.pc());
duke@0 983 if (ssc->is_clean()) ssc->set(static_call_info);
duke@0 984 }
duke@0 985 }
duke@0 986
duke@0 987 } // unlock CompiledIC_lock
duke@0 988
duke@0 989 return callee_method;
duke@0 990 }
duke@0 991
duke@0 992
duke@0 993 // Inline caches exist only in compiled code
duke@0 994 JRT_BLOCK_ENTRY(address, SharedRuntime::handle_wrong_method_ic_miss(JavaThread* thread))
duke@0 995 #ifdef ASSERT
duke@0 996 RegisterMap reg_map(thread, false);
duke@0 997 frame stub_frame = thread->last_frame();
duke@0 998 assert(stub_frame.is_runtime_frame(), "sanity check");
duke@0 999 frame caller_frame = stub_frame.sender(&reg_map);
duke@0 1000 assert(!caller_frame.is_interpreted_frame() && !caller_frame.is_entry_frame(), "unexpected frame");
duke@0 1001 #endif /* ASSERT */
duke@0 1002
duke@0 1003 methodHandle callee_method;
duke@0 1004 JRT_BLOCK
duke@0 1005 callee_method = SharedRuntime::handle_ic_miss_helper(thread, CHECK_NULL);
duke@0 1006 // Return methodOop through TLS
duke@0 1007 thread->set_vm_result(callee_method());
duke@0 1008 JRT_BLOCK_END
duke@0 1009 // return compiled code entry point after potential safepoints
duke@0 1010 assert(callee_method->verified_code_entry() != NULL, " Jump to zero!");
duke@0 1011 return callee_method->verified_code_entry();
duke@0 1012 JRT_END
duke@0 1013
duke@0 1014
duke@0 1015 // Handle call site that has been made non-entrant
duke@0 1016 JRT_BLOCK_ENTRY(address, SharedRuntime::handle_wrong_method(JavaThread* thread))
duke@0 1017 // 6243940 We might end up in here if the callee is deoptimized
duke@0 1018 // as we race to call it. We don't want to take a safepoint if
duke@0 1019 // the caller was interpreted because the caller frame will look
duke@0 1020 // interpreted to the stack walkers and arguments are now
duke@0 1021 // "compiled" so it is much better to make this transition
duke@0 1022 // invisible to the stack walking code. The i2c path will
duke@0 1023 // place the callee method in the callee_target. It is stashed
duke@0 1024 // there because if we try and find the callee by normal means a
duke@0 1025 // safepoint is possible and have trouble gc'ing the compiled args.
duke@0 1026 RegisterMap reg_map(thread, false);
duke@0 1027 frame stub_frame = thread->last_frame();
duke@0 1028 assert(stub_frame.is_runtime_frame(), "sanity check");
duke@0 1029 frame caller_frame = stub_frame.sender(&reg_map);
twisti@1135 1030
twisti@1135 1031 // MethodHandle invokes don't have a CompiledIC and should always
twisti@1135 1032 // simply redispatch to the callee_target.
twisti@1135 1033 address sender_pc = caller_frame.pc();
twisti@1135 1034 CodeBlob* sender_cb = caller_frame.cb();
twisti@1135 1035 nmethod* sender_nm = sender_cb->as_nmethod_or_null();
twisti@1135 1036
twisti@1135 1037 if (caller_frame.is_interpreted_frame() ||
twisti@1201 1038 caller_frame.is_entry_frame() ||
twisti@1201 1039 (sender_nm != NULL && sender_nm->is_method_handle_return(sender_pc))) {
duke@0 1040 methodOop callee = thread->callee_target();
duke@0 1041 guarantee(callee != NULL && callee->is_method(), "bad handshake");
duke@0 1042 thread->set_vm_result(callee);
duke@0 1043 thread->set_callee_target(NULL);
duke@0 1044 return callee->get_c2i_entry();
duke@0 1045 }
duke@0 1046
duke@0 1047 // Must be compiled to compiled path which is safe to stackwalk
duke@0 1048 methodHandle callee_method;
duke@0 1049 JRT_BLOCK
duke@0 1050 // Force resolving of caller (if we called from compiled frame)
duke@0 1051 callee_method = SharedRuntime::reresolve_call_site(thread, CHECK_NULL);
duke@0 1052 thread->set_vm_result(callee_method());
duke@0 1053 JRT_BLOCK_END
duke@0 1054 // return compiled code entry point after potential safepoints
duke@0 1055 assert(callee_method->verified_code_entry() != NULL, " Jump to zero!");
duke@0 1056 return callee_method->verified_code_entry();
duke@0 1057 JRT_END
duke@0 1058
duke@0 1059
duke@0 1060 // resolve a static call and patch code
duke@0 1061 JRT_BLOCK_ENTRY(address, SharedRuntime::resolve_static_call_C(JavaThread *thread ))
duke@0 1062 methodHandle callee_method;
duke@0 1063 JRT_BLOCK
duke@0 1064 callee_method = SharedRuntime::resolve_helper(thread, false, false, CHECK_NULL);
duke@0 1065 thread->set_vm_result(callee_method());
duke@0 1066 JRT_BLOCK_END
duke@0 1067 // return compiled code entry point after potential safepoints
duke@0 1068 assert(callee_method->verified_code_entry() != NULL, " Jump to zero!");
duke@0 1069 return callee_method->verified_code_entry();
duke@0 1070 JRT_END
duke@0 1071
duke@0 1072
duke@0 1073 // resolve virtual call and update inline cache to monomorphic
duke@0 1074 JRT_BLOCK_ENTRY(address, SharedRuntime::resolve_virtual_call_C(JavaThread *thread ))
duke@0 1075 methodHandle callee_method;
duke@0 1076 JRT_BLOCK
duke@0 1077 callee_method = SharedRuntime::resolve_helper(thread, true, false, CHECK_NULL);
duke@0 1078 thread->set_vm_result(callee_method());
duke@0 1079 JRT_BLOCK_END
duke@0 1080 // return compiled code entry point after potential safepoints
duke@0 1081 assert(callee_method->verified_code_entry() != NULL, " Jump to zero!");
duke@0 1082 return callee_method->verified_code_entry();
duke@0 1083 JRT_END
duke@0 1084
duke@0 1085
duke@0 1086 // Resolve a virtual call that can be statically bound (e.g., always
duke@0 1087 // monomorphic, so it has no inline cache). Patch code to resolved target.
duke@0 1088 JRT_BLOCK_ENTRY(address, SharedRuntime::resolve_opt_virtual_call_C(JavaThread *thread))
duke@0 1089 methodHandle callee_method;
duke@0 1090 JRT_BLOCK
duke@0 1091 callee_method = SharedRuntime::resolve_helper(thread, true, true, CHECK_NULL);
duke@0 1092 thread->set_vm_result(callee_method());
duke@0 1093 JRT_BLOCK_END
duke@0 1094 // return compiled code entry point after potential safepoints
duke@0 1095 assert(callee_method->verified_code_entry() != NULL, " Jump to zero!");
duke@0 1096 return callee_method->verified_code_entry();
duke@0 1097 JRT_END
duke@0 1098
duke@0 1099
duke@0 1100
duke@0 1101
duke@0 1102
duke@0 1103 methodHandle SharedRuntime::handle_ic_miss_helper(JavaThread *thread, TRAPS) {
duke@0 1104 ResourceMark rm(thread);
duke@0 1105 CallInfo call_info;
duke@0 1106 Bytecodes::Code bc;
duke@0 1107
duke@0 1108 // receiver is NULL for static calls. An exception is thrown for NULL
duke@0 1109 // receivers for non-static calls
duke@0 1110 Handle receiver = find_callee_info(thread, bc, call_info,
duke@0 1111 CHECK_(methodHandle()));
duke@0 1112 // Compiler1 can produce virtual call sites that can actually be statically bound
duke@0 1113 // If we fell thru to below we would think that the site was going megamorphic
duke@0 1114 // when in fact the site can never miss. Worse because we'd think it was megamorphic
duke@0 1115 // we'd try and do a vtable dispatch however methods that can be statically bound
duke@0 1116 // don't have vtable entries (vtable_index < 0) and we'd blow up. So we force a
duke@0 1117 // reresolution of the call site (as if we did a handle_wrong_method and not an
duke@0 1118 // plain ic_miss) and the site will be converted to an optimized virtual call site
duke@0 1119 // never to miss again. I don't believe C2 will produce code like this but if it
duke@0 1120 // did this would still be the correct thing to do for it too, hence no ifdef.
duke@0 1121 //
duke@0 1122 if (call_info.resolved_method()->can_be_statically_bound()) {
duke@0 1123 methodHandle callee_method = SharedRuntime::reresolve_call_site(thread, CHECK_(methodHandle()));
duke@0 1124 if (TraceCallFixup) {
duke@0 1125 RegisterMap reg_map(thread, false);
duke@0 1126 frame caller_frame = thread->last_frame().sender(&reg_map);
duke@0 1127 ResourceMark rm(thread);
duke@0 1128 tty->print("converting IC miss to reresolve (%s) call to", Bytecodes::name(bc));
duke@0 1129 callee_method->print_short_name(tty);
duke@0 1130 tty->print_cr(" from pc: " INTPTR_FORMAT, caller_frame.pc());
duke@0 1131 tty->print_cr(" code: " INTPTR_FORMAT, callee_method->code());
duke@0 1132 }
duke@0 1133 return callee_method;
duke@0 1134 }
duke@0 1135
duke@0 1136 methodHandle callee_method = call_info.selected_method();
duke@0 1137
duke@0 1138 bool should_be_mono = false;
duke@0 1139
duke@0 1140 #ifndef PRODUCT
duke@0 1141 Atomic::inc(&_ic_miss_ctr);
duke@0 1142
duke@0 1143 // Statistics & Tracing
duke@0 1144 if (TraceCallFixup) {
duke@0 1145 ResourceMark rm(thread);
duke@0 1146 tty->print("IC miss (%s) call to", Bytecodes::name(bc));
duke@0 1147 callee_method->print_short_name(tty);
duke@0 1148 tty->print_cr(" code: " INTPTR_FORMAT, callee_method->code());
duke@0 1149 }
duke@0 1150
duke@0 1151 if (ICMissHistogram) {
duke@0 1152 MutexLocker m(VMStatistic_lock);
duke@0 1153 RegisterMap reg_map(thread, false);
duke@0 1154 frame f = thread->last_frame().real_sender(&reg_map);// skip runtime stub
duke@0 1155 // produce statistics under the lock
duke@0 1156 trace_ic_miss(f.pc());
duke@0 1157 }
duke@0 1158 #endif
duke@0 1159
duke@0 1160 // install an event collector so that when a vtable stub is created the
duke@0 1161 // profiler can be notified via a DYNAMIC_CODE_GENERATED event. The
duke@0 1162 // event can't be posted when the stub is created as locks are held
duke@0 1163 // - instead the event will be deferred until the event collector goes
duke@0 1164 // out of scope.
duke@0 1165 JvmtiDynamicCodeEventCollector event_collector;
duke@0 1166
duke@0 1167 // Update inline cache to megamorphic. Skip update if caller has been
duke@0 1168 // made non-entrant or we are called from interpreted.
duke@0 1169 { MutexLocker ml_patch (CompiledIC_lock);
duke@0 1170 RegisterMap reg_map(thread, false);
duke@0 1171 frame caller_frame = thread->last_frame().sender(&reg_map);
duke@0 1172 CodeBlob* cb = caller_frame.cb();
duke@0 1173 if (cb->is_nmethod() && ((nmethod*)cb)->is_in_use()) {
duke@0 1174 // Not a non-entrant nmethod, so find inline_cache
duke@0 1175 CompiledIC* inline_cache = CompiledIC_before(caller_frame.pc());
duke@0 1176 bool should_be_mono = false;
duke@0 1177 if (inline_cache->is_optimized()) {
duke@0 1178 if (TraceCallFixup) {
duke@0 1179 ResourceMark rm(thread);
duke@0 1180 tty->print("OPTIMIZED IC miss (%s) call to", Bytecodes::name(bc));
duke@0 1181 callee_method->print_short_name(tty);
duke@0 1182 tty->print_cr(" code: " INTPTR_FORMAT, callee_method->code());
duke@0 1183 }
duke@0 1184 should_be_mono = true;
duke@0 1185 } else {
duke@0 1186 compiledICHolderOop ic_oop = (compiledICHolderOop) inline_cache->cached_oop();
duke@0 1187 if ( ic_oop != NULL && ic_oop->is_compiledICHolder()) {
duke@0 1188
duke@0 1189 if (receiver()->klass() == ic_oop->holder_klass()) {
duke@0 1190 // This isn't a real miss. We must have seen that compiled code
duke@0 1191 // is now available and we want the call site converted to a
duke@0 1192 // monomorphic compiled call site.
duke@0 1193 // We can't assert for callee_method->code() != NULL because it
duke@0 1194 // could have been deoptimized in the meantime
duke@0 1195 if (TraceCallFixup) {
duke@0 1196 ResourceMark rm(thread);
duke@0 1197 tty->print("FALSE IC miss (%s) converting to compiled call to", Bytecodes::name(bc));
duke@0 1198 callee_method->print_short_name(tty);
duke@0 1199 tty->print_cr(" code: " INTPTR_FORMAT, callee_method->code());
duke@0 1200 }
duke@0 1201 should_be_mono = true;
duke@0 1202 }
duke@0 1203 }
duke@0 1204 }
duke@0 1205
duke@0 1206 if (should_be_mono) {
duke@0 1207
duke@0 1208 // We have a path that was monomorphic but was going interpreted
duke@0 1209 // and now we have (or had) a compiled entry. We correct the IC
duke@0 1210 // by using a new icBuffer.
duke@0 1211 CompiledICInfo info;
duke@0 1212 KlassHandle receiver_klass(THREAD, receiver()->klass());
duke@0 1213 inline_cache->compute_monomorphic_entry(callee_method,
duke@0 1214 receiver_klass,
duke@0 1215 inline_cache->is_optimized(),
duke@0 1216 false,
duke@0 1217 info, CHECK_(methodHandle()));
duke@0 1218 inline_cache->set_to_monomorphic(info);
duke@0 1219 } else if (!inline_cache->is_megamorphic() && !inline_cache->is_clean()) {
duke@0 1220 // Change to megamorphic
duke@0 1221 inline_cache->set_to_megamorphic(&call_info, bc, CHECK_(methodHandle()));
duke@0 1222 } else {
duke@0 1223 // Either clean or megamorphic
duke@0 1224 }
duke@0 1225 }
duke@0 1226 } // Release CompiledIC_lock
duke@0 1227
duke@0 1228 return callee_method;
duke@0 1229 }
duke@0 1230
duke@0 1231 //
duke@0 1232 // Resets a call-site in compiled code so it will get resolved again.
duke@0 1233 // This routines handles both virtual call sites, optimized virtual call
duke@0 1234 // sites, and static call sites. Typically used to change a call sites
duke@0 1235 // destination from compiled to interpreted.
duke@0 1236 //
duke@0 1237 methodHandle SharedRuntime::reresolve_call_site(JavaThread *thread, TRAPS) {
duke@0 1238 ResourceMark rm(thread);
duke@0 1239 RegisterMap reg_map(thread, false);
duke@0 1240 frame stub_frame = thread->last_frame();
duke@0 1241 assert(stub_frame.is_runtime_frame(), "must be a runtimeStub");
duke@0 1242 frame caller = stub_frame.sender(&reg_map);
duke@0 1243
duke@0 1244 // Do nothing if the frame isn't a live compiled frame.
duke@0 1245 // nmethod could be deoptimized by the time we get here
duke@0 1246 // so no update to the caller is needed.
duke@0 1247
duke@0 1248 if (caller.is_compiled_frame() && !caller.is_deoptimized_frame()) {
duke@0 1249
duke@0 1250 address pc = caller.pc();
duke@0 1251 Events::log("update call-site at pc " INTPTR_FORMAT, pc);
duke@0 1252
duke@0 1253 // Default call_addr is the location of the "basic" call.
duke@0 1254 // Determine the address of the call we a reresolving. With
duke@0 1255 // Inline Caches we will always find a recognizable call.
duke@0 1256 // With Inline Caches disabled we may or may not find a
duke@0 1257 // recognizable call. We will always find a call for static
duke@0 1258 // calls and for optimized virtual calls. For vanilla virtual
duke@0 1259 // calls it depends on the state of the UseInlineCaches switch.
duke@0 1260 //
duke@0 1261 // With Inline Caches disabled we can get here for a virtual call
duke@0 1262 // for two reasons:
duke@0 1263 // 1 - calling an abstract method. The vtable for abstract methods
duke@0 1264 // will run us thru handle_wrong_method and we will eventually
duke@0 1265 // end up in the interpreter to throw the ame.
duke@0 1266 // 2 - a racing deoptimization. We could be doing a vanilla vtable
duke@0 1267 // call and between the time we fetch the entry address and
duke@0 1268 // we jump to it the target gets deoptimized. Similar to 1
duke@0 1269 // we will wind up in the interprter (thru a c2i with c2).
duke@0 1270 //
duke@0 1271 address call_addr = NULL;
duke@0 1272 {
duke@0 1273 // Get call instruction under lock because another thread may be
duke@0 1274 // busy patching it.
duke@0 1275 MutexLockerEx ml_patch(Patching_lock, Mutex::_no_safepoint_check_flag);
duke@0 1276 // Location of call instruction
duke@0 1277 if (NativeCall::is_call_before(pc)) {
duke@0 1278 NativeCall *ncall = nativeCall_before(pc);
duke@0 1279 call_addr = ncall->instruction_address();
duke@0 1280 }
duke@0 1281 }
duke@0 1282
duke@0 1283 // Check for static or virtual call
duke@0 1284 bool is_static_call = false;
duke@0 1285 nmethod* caller_nm = CodeCache::find_nmethod(pc);
duke@0 1286 // Make sure nmethod doesn't get deoptimized and removed until
duke@0 1287 // this is done with it.
duke@0 1288 // CLEANUP - with lazy deopt shouldn't need this lock
duke@0 1289 nmethodLocker nmlock(caller_nm);
duke@0 1290
duke@0 1291 if (call_addr != NULL) {
duke@0 1292 RelocIterator iter(caller_nm, call_addr, call_addr+1);
duke@0 1293 int ret = iter.next(); // Get item
duke@0 1294 if (ret) {
duke@0 1295 assert(iter.addr() == call_addr, "must find call");
duke@0 1296 if (iter.type() == relocInfo::static_call_type) {
duke@0 1297 is_static_call = true;
duke@0 1298 } else {
duke@0 1299 assert(iter.type() == relocInfo::virtual_call_type ||
duke@0 1300 iter.type() == relocInfo::opt_virtual_call_type
duke@0 1301 , "unexpected relocInfo. type");
duke@0 1302 }
duke@0 1303 } else {
duke@0 1304 assert(!UseInlineCaches, "relocation info. must exist for this address");
duke@0 1305 }
duke@0 1306
duke@0 1307 // Cleaning the inline cache will force a new resolve. This is more robust
duke@0 1308 // than directly setting it to the new destination, since resolving of calls
duke@0 1309 // is always done through the same code path. (experience shows that it
duke@0 1310 // leads to very hard to track down bugs, if an inline cache gets updated
duke@0 1311 // to a wrong method). It should not be performance critical, since the
duke@0 1312 // resolve is only done once.
duke@0 1313
duke@0 1314 MutexLocker ml(CompiledIC_lock);
duke@0 1315 //
duke@0 1316 // We do not patch the call site if the nmethod has been made non-entrant
duke@0 1317 // as it is a waste of time
duke@0 1318 //
duke@0 1319 if (caller_nm->is_in_use()) {
duke@0 1320 if (is_static_call) {
duke@0 1321 CompiledStaticCall* ssc= compiledStaticCall_at(call_addr);
duke@0 1322 ssc->set_to_clean();
duke@0 1323 } else {
duke@0 1324 // compiled, dispatched call (which used to call an interpreted method)
duke@0 1325 CompiledIC* inline_cache = CompiledIC_at(call_addr);
duke@0 1326 inline_cache->set_to_clean();
duke@0 1327 }
duke@0 1328 }
duke@0 1329 }
duke@0 1330
duke@0 1331 }
duke@0 1332
duke@0 1333 methodHandle callee_method = find_callee_method(thread, CHECK_(methodHandle()));
duke@0 1334
duke@0 1335
duke@0 1336 #ifndef PRODUCT
duke@0 1337 Atomic::inc(&_wrong_method_ctr);
duke@0 1338
duke@0 1339 if (TraceCallFixup) {
duke@0 1340 ResourceMark rm(thread);
duke@0 1341 tty->print("handle_wrong_method reresolving call to");
duke@0 1342 callee_method->print_short_name(tty);
duke@0 1343 tty->print_cr(" code: " INTPTR_FORMAT, callee_method->code());
duke@0 1344 }
duke@0 1345 #endif
duke@0 1346
duke@0 1347 return callee_method;
duke@0 1348 }
duke@0 1349
duke@0 1350 // ---------------------------------------------------------------------------
duke@0 1351 // We are calling the interpreter via a c2i. Normally this would mean that
duke@0 1352 // we were called by a compiled method. However we could have lost a race
duke@0 1353 // where we went int -> i2c -> c2i and so the caller could in fact be
duke@0 1354 // interpreted. If the caller is compiled we attampt to patch the caller
duke@0 1355 // so he no longer calls into the interpreter.
duke@0 1356 IRT_LEAF(void, SharedRuntime::fixup_callers_callsite(methodOopDesc* method, address caller_pc))
duke@0 1357 methodOop moop(method);
duke@0 1358
duke@0 1359 address entry_point = moop->from_compiled_entry();
duke@0 1360
duke@0 1361 // It's possible that deoptimization can occur at a call site which hasn't
duke@0 1362 // been resolved yet, in which case this function will be called from
duke@0 1363 // an nmethod that has been patched for deopt and we can ignore the
duke@0 1364 // request for a fixup.
duke@0 1365 // Also it is possible that we lost a race in that from_compiled_entry
duke@0 1366 // is now back to the i2c in that case we don't need to patch and if
duke@0 1367 // we did we'd leap into space because the callsite needs to use
duke@0 1368 // "to interpreter" stub in order to load up the methodOop. Don't
duke@0 1369 // ask me how I know this...
duke@0 1370 //
duke@0 1371
duke@0 1372 CodeBlob* cb = CodeCache::find_blob(caller_pc);
duke@0 1373 if ( !cb->is_nmethod() || entry_point == moop->get_c2i_entry()) {
duke@0 1374 return;
duke@0 1375 }
duke@0 1376
duke@0 1377 // There is a benign race here. We could be attempting to patch to a compiled
duke@0 1378 // entry point at the same time the callee is being deoptimized. If that is
duke@0 1379 // the case then entry_point may in fact point to a c2i and we'd patch the
duke@0 1380 // call site with the same old data. clear_code will set code() to NULL
duke@0 1381 // at the end of it. If we happen to see that NULL then we can skip trying
duke@0 1382 // to patch. If we hit the window where the callee has a c2i in the
duke@0 1383 // from_compiled_entry and the NULL isn't present yet then we lose the race
duke@0 1384 // and patch the code with the same old data. Asi es la vida.
duke@0 1385
duke@0 1386 if (moop->code() == NULL) return;
duke@0 1387
duke@0 1388 if (((nmethod*)cb)->is_in_use()) {
duke@0 1389
duke@0 1390 // Expect to find a native call there (unless it was no-inline cache vtable dispatch)
duke@0 1391 MutexLockerEx ml_patch(Patching_lock, Mutex::_no_safepoint_check_flag);
duke@0 1392 if (NativeCall::is_call_before(caller_pc + frame::pc_return_offset)) {
duke@0 1393 NativeCall *call = nativeCall_before(caller_pc + frame::pc_return_offset);
duke@0 1394 //
duke@0 1395 // bug 6281185. We might get here after resolving a call site to a vanilla
duke@0 1396 // virtual call. Because the resolvee uses the verified entry it may then
duke@0 1397 // see compiled code and attempt to patch the site by calling us. This would
duke@0 1398 // then incorrectly convert the call site to optimized and its downhill from
duke@0 1399 // there. If you're lucky you'll get the assert in the bugid, if not you've
duke@0 1400 // just made a call site that could be megamorphic into a monomorphic site
duke@0 1401 // for the rest of its life! Just another racing bug in the life of
duke@0 1402 // fixup_callers_callsite ...
duke@0 1403 //
duke@0 1404 RelocIterator iter(cb, call->instruction_address(), call->next_instruction_address());
duke@0 1405 iter.next();
duke@0 1406 assert(iter.has_current(), "must have a reloc at java call site");
duke@0 1407 relocInfo::relocType typ = iter.reloc()->type();
duke@0 1408 if ( typ != relocInfo::static_call_type &&
duke@0 1409 typ != relocInfo::opt_virtual_call_type &&
duke@0 1410 typ != relocInfo::static_stub_type) {
duke@0 1411 return;
duke@0 1412 }
duke@0 1413 address destination = call->destination();
duke@0 1414 if (destination != entry_point) {
duke@0 1415 CodeBlob* callee = CodeCache::find_blob(destination);
duke@0 1416 // callee == cb seems weird. It means calling interpreter thru stub.
duke@0 1417 if (callee == cb || callee->is_adapter_blob()) {
duke@0 1418 // static call or optimized virtual
duke@0 1419 if (TraceCallFixup) {
twisti@1201 1420 tty->print("fixup callsite at " INTPTR_FORMAT " to compiled code for", caller_pc);
duke@0 1421 moop->print_short_name(tty);
duke@0 1422 tty->print_cr(" to " INTPTR_FORMAT, entry_point);
duke@0 1423 }
duke@0 1424 call->set_destination_mt_safe(entry_point);
duke@0 1425 } else {
duke@0 1426 if (TraceCallFixup) {
duke@0 1427 tty->print("failed to fixup callsite at " INTPTR_FORMAT " to compiled code for", caller_pc);
duke@0 1428 moop->print_short_name(tty);
duke@0 1429 tty->print_cr(" to " INTPTR_FORMAT, entry_point);
duke@0 1430 }
duke@0 1431 // assert is too strong could also be resolve destinations.
duke@0 1432 // assert(InlineCacheBuffer::contains(destination) || VtableStubs::contains(destination), "must be");
duke@0 1433 }
duke@0 1434 } else {
duke@0 1435 if (TraceCallFixup) {
twisti@1201 1436 tty->print("already patched callsite at " INTPTR_FORMAT " to compiled code for", caller_pc);
duke@0 1437 moop->print_short_name(tty);
duke@0 1438 tty->print_cr(" to " INTPTR_FORMAT, entry_point);
duke@0 1439 }
duke@0 1440 }
duke@0 1441 }
duke@0 1442 }
duke@0 1443
duke@0 1444 IRT_END
duke@0 1445
duke@0 1446
duke@0 1447 // same as JVM_Arraycopy, but called directly from compiled code
duke@0 1448 JRT_ENTRY(void, SharedRuntime::slow_arraycopy_C(oopDesc* src, jint src_pos,
duke@0 1449 oopDesc* dest, jint dest_pos,
duke@0 1450 jint length,
duke@0 1451 JavaThread* thread)) {
duke@0 1452 #ifndef PRODUCT
duke@0 1453 _slow_array_copy_ctr++;
duke@0 1454 #endif
duke@0 1455 // Check if we have null pointers
duke@0 1456 if (src == NULL || dest == NULL) {
duke@0 1457 THROW(vmSymbols::java_lang_NullPointerException());
duke@0 1458 }
duke@0 1459 // Do the copy. The casts to arrayOop are necessary to the copy_array API,
duke@0 1460 // even though the copy_array API also performs dynamic checks to ensure
duke@0 1461 // that src and dest are truly arrays (and are conformable).
duke@0 1462 // The copy_array mechanism is awkward and could be removed, but
duke@0 1463 // the compilers don't call this function except as a last resort,
duke@0 1464 // so it probably doesn't matter.
duke@0 1465 Klass::cast(src->klass())->copy_array((arrayOopDesc*)src, src_pos,
duke@0 1466 (arrayOopDesc*)dest, dest_pos,
duke@0 1467 length, thread);
duke@0 1468 }
duke@0 1469 JRT_END
duke@0 1470
duke@0 1471 char* SharedRuntime::generate_class_cast_message(
duke@0 1472 JavaThread* thread, const char* objName) {
duke@0 1473
duke@0 1474 // Get target class name from the checkcast instruction
duke@0 1475 vframeStream vfst(thread, true);
duke@0 1476 assert(!vfst.at_end(), "Java frame must exist");
duke@0 1477 Bytecode_checkcast* cc = Bytecode_checkcast_at(
duke@0 1478 vfst.method()->bcp_from(vfst.bci()));
duke@0 1479 Klass* targetKlass = Klass::cast(vfst.method()->constants()->klass_at(
duke@0 1480 cc->index(), thread));
duke@0 1481 return generate_class_cast_message(objName, targetKlass->external_name());
duke@0 1482 }
duke@0 1483
jrose@710 1484 char* SharedRuntime::generate_wrong_method_type_message(JavaThread* thread,
jrose@710 1485 oopDesc* required,
jrose@710 1486 oopDesc* actual) {
jrose@710 1487 assert(EnableMethodHandles, "");
jrose@710 1488 oop singleKlass = wrong_method_type_is_for_single_argument(thread, required);
jrose@710 1489 if (singleKlass != NULL) {
jrose@710 1490 const char* objName = "argument or return value";
jrose@710 1491 if (actual != NULL) {
jrose@710 1492 // be flexible about the junk passed in:
jrose@710 1493 klassOop ak = (actual->is_klass()
jrose@710 1494 ? (klassOop)actual
jrose@710 1495 : actual->klass());
jrose@710 1496 objName = Klass::cast(ak)->external_name();
jrose@710 1497 }
jrose@710 1498 Klass* targetKlass = Klass::cast(required->is_klass()
jrose@710 1499 ? (klassOop)required
jrose@710 1500 : java_lang_Class::as_klassOop(required));
jrose@710 1501 return generate_class_cast_message(objName, targetKlass->external_name());
jrose@710 1502 } else {
jrose@710 1503 // %%% need to get the MethodType string, without messing around too much
jrose@710 1504 // Get a signature from the invoke instruction
jrose@710 1505 const char* mhName = "method handle";
jrose@710 1506 const char* targetType = "the required signature";
jrose@710 1507 vframeStream vfst(thread, true);
jrose@710 1508 if (!vfst.at_end()) {
jrose@710 1509 Bytecode_invoke* call = Bytecode_invoke_at(vfst.method(), vfst.bci());
jrose@710 1510 methodHandle target;
jrose@710 1511 {
jrose@710 1512 EXCEPTION_MARK;
jrose@710 1513 target = call->static_target(THREAD);
jrose@710 1514 if (HAS_PENDING_EXCEPTION) { CLEAR_PENDING_EXCEPTION; }
jrose@710 1515 }
jrose@710 1516 if (target.not_null()
jrose@710 1517 && target->is_method_handle_invoke()
jrose@710 1518 && required == target->method_handle_type()) {
jrose@710 1519 targetType = target->signature()->as_C_string();
jrose@710 1520 }
jrose@710 1521 }
jrose@710 1522 klassOop kignore; int fignore;
jrose@710 1523 methodOop actual_method = MethodHandles::decode_method(actual,
jrose@710 1524 kignore, fignore);
jrose@710 1525 if (actual_method != NULL) {
jrose@710 1526 if (actual_method->name() == vmSymbols::invoke_name())
jrose@710 1527 mhName = "$";
jrose@710 1528 else
jrose@710 1529 mhName = actual_method->signature()->as_C_string();
jrose@710 1530 if (mhName[0] == '$')
jrose@710 1531 mhName = actual_method->signature()->as_C_string();
jrose@710 1532 }
jrose@710 1533 return generate_class_cast_message(mhName, targetType,
jrose@710 1534 " cannot be called as ");
jrose@710 1535 }
jrose@710 1536 }
jrose@710 1537
jrose@710 1538 oop SharedRuntime::wrong_method_type_is_for_single_argument(JavaThread* thr,
jrose@710 1539 oopDesc* required) {
jrose@710 1540 if (required == NULL) return NULL;
never@1142 1541 if (required->klass() == SystemDictionary::Class_klass())
jrose@710 1542 return required;
jrose@710 1543 if (required->is_klass())
jrose@710 1544 return Klass::cast(klassOop(required))->java_mirror();
jrose@710 1545 return NULL;
jrose@710 1546 }
jrose@710 1547
jrose@710 1548
duke@0 1549 char* SharedRuntime::generate_class_cast_message(
jrose@710 1550 const char* objName, const char* targetKlassName, const char* desc) {
duke@0 1551 size_t msglen = strlen(objName) + strlen(desc) + strlen(targetKlassName) + 1;
duke@0 1552
kamg@53 1553 char* message = NEW_RESOURCE_ARRAY(char, msglen);
duke@0 1554 if (NULL == message) {
kamg@53 1555 // Shouldn't happen, but don't cause even more problems if it does
duke@0 1556 message = const_cast<char*>(objName);
duke@0 1557 } else {
duke@0 1558 jio_snprintf(message, msglen, "%s%s%s", objName, desc, targetKlassName);
duke@0 1559 }
duke@0 1560 return message;
duke@0 1561 }
duke@0 1562
duke@0 1563 JRT_LEAF(void, SharedRuntime::reguard_yellow_pages())
duke@0 1564 (void) JavaThread::current()->reguard_stack();
duke@0 1565 JRT_END
duke@0 1566
duke@0 1567
duke@0 1568 // Handles the uncommon case in locking, i.e., contention or an inflated lock.
duke@0 1569 #ifndef PRODUCT
duke@0 1570 int SharedRuntime::_monitor_enter_ctr=0;
duke@0 1571 #endif
duke@0 1572 JRT_ENTRY_NO_ASYNC(void, SharedRuntime::complete_monitor_locking_C(oopDesc* _obj, BasicLock* lock, JavaThread* thread))
duke@0 1573 oop obj(_obj);
duke@0 1574 #ifndef PRODUCT
duke@0 1575 _monitor_enter_ctr++; // monitor enter slow
duke@0 1576 #endif
duke@0 1577 if (PrintBiasedLockingStatistics) {
duke@0 1578 Atomic::inc(BiasedLocking::slow_path_entry_count_addr());
duke@0 1579 }
duke@0 1580 Handle h_obj(THREAD, obj);
duke@0 1581 if (UseBiasedLocking) {
duke@0 1582 // Retry fast entry if bias is revoked to avoid unnecessary inflation
duke@0 1583 ObjectSynchronizer::fast_enter(h_obj, lock, true, CHECK);
duke@0 1584 } else {
duke@0 1585 ObjectSynchronizer::slow_enter(h_obj, lock, CHECK);
duke@0 1586 }
duke@0 1587 assert(!HAS_PENDING_EXCEPTION, "Should have no exception here");
duke@0 1588 JRT_END
duke@0 1589
duke@0 1590 #ifndef PRODUCT
duke@0 1591 int SharedRuntime::_monitor_exit_ctr=0;
duke@0 1592 #endif
duke@0 1593 // Handles the uncommon cases of monitor unlocking in compiled code
duke@0 1594 JRT_LEAF(void, SharedRuntime::complete_monitor_unlocking_C(oopDesc* _obj, BasicLock* lock))
duke@0 1595 oop obj(_obj);
duke@0 1596 #ifndef PRODUCT
duke@0 1597 _monitor_exit_ctr++; // monitor exit slow
duke@0 1598 #endif
duke@0 1599 Thread* THREAD = JavaThread::current();
duke@0 1600 // I'm not convinced we need the code contained by MIGHT_HAVE_PENDING anymore
duke@0 1601 // testing was unable to ever fire the assert that guarded it so I have removed it.
duke@0 1602 assert(!HAS_PENDING_EXCEPTION, "Do we need code below anymore?");
duke@0 1603 #undef MIGHT_HAVE_PENDING
duke@0 1604 #ifdef MIGHT_HAVE_PENDING
duke@0 1605 // Save and restore any pending_exception around the exception mark.
duke@0 1606 // While the slow_exit must not throw an exception, we could come into
duke@0 1607 // this routine with one set.
duke@0 1608 oop pending_excep = NULL;
duke@0 1609 const char* pending_file;
duke@0 1610 int pending_line;
duke@0 1611 if (HAS_PENDING_EXCEPTION) {
duke@0 1612 pending_excep = PENDING_EXCEPTION;
duke@0 1613 pending_file = THREAD->exception_file();
duke@0 1614 pending_line = THREAD->exception_line();
duke@0 1615 CLEAR_PENDING_EXCEPTION;
duke@0 1616 }
duke@0 1617 #endif /* MIGHT_HAVE_PENDING */
duke@0 1618
duke@0 1619 {
duke@0 1620 // Exit must be non-blocking, and therefore no exceptions can be thrown.
duke@0 1621 EXCEPTION_MARK;
duke@0 1622 ObjectSynchronizer::slow_exit(obj, lock, THREAD);
duke@0 1623 }
duke@0 1624
duke@0 1625 #ifdef MIGHT_HAVE_PENDING
duke@0 1626 if (pending_excep != NULL) {
duke@0 1627 THREAD->set_pending_exception(pending_excep, pending_file, pending_line);
duke@0 1628 }
duke@0 1629 #endif /* MIGHT_HAVE_PENDING */
duke@0 1630 JRT_END
duke@0 1631
duke@0 1632 #ifndef PRODUCT
duke@0 1633
duke@0 1634 void SharedRuntime::print_statistics() {
duke@0 1635 ttyLocker ttyl;
duke@0 1636 if (xtty != NULL) xtty->head("statistics type='SharedRuntime'");
duke@0 1637
duke@0 1638 if (_monitor_enter_ctr ) tty->print_cr("%5d monitor enter slow", _monitor_enter_ctr);
duke@0 1639 if (_monitor_exit_ctr ) tty->print_cr("%5d monitor exit slow", _monitor_exit_ctr);
duke@0 1640 if (_throw_null_ctr) tty->print_cr("%5d implicit null throw", _throw_null_ctr);
duke@0 1641
duke@0 1642 SharedRuntime::print_ic_miss_histogram();
duke@0 1643
duke@0 1644 if (CountRemovableExceptions) {
duke@0 1645 if (_nof_removable_exceptions > 0) {
duke@0 1646 Unimplemented(); // this counter is not yet incremented
duke@0 1647 tty->print_cr("Removable exceptions: %d", _nof_removable_exceptions);
duke@0 1648 }
duke@0 1649 }
duke@0 1650
duke@0 1651 // Dump the JRT_ENTRY counters
duke@0 1652 if( _new_instance_ctr ) tty->print_cr("%5d new instance requires GC", _new_instance_ctr);
duke@0 1653 if( _new_array_ctr ) tty->print_cr("%5d new array requires GC", _new_array_ctr);
duke@0 1654 if( _multi1_ctr ) tty->print_cr("%5d multianewarray 1 dim", _multi1_ctr);
duke@0 1655 if( _multi2_ctr ) tty->print_cr("%5d multianewarray 2 dim", _multi2_ctr);
duke@0 1656 if( _multi3_ctr ) tty->print_cr("%5d multianewarray 3 dim", _multi3_ctr);
duke@0 1657 if( _multi4_ctr ) tty->print_cr("%5d multianewarray 4 dim", _multi4_ctr);
duke@0 1658 if( _multi5_ctr ) tty->print_cr("%5d multianewarray 5 dim", _multi5_ctr);
duke@0 1659
duke@0 1660 tty->print_cr("%5d inline cache miss in compiled", _ic_miss_ctr );
duke@0 1661 tty->print_cr("%5d wrong method", _wrong_method_ctr );
duke@0 1662 tty->print_cr("%5d unresolved static call site", _resolve_static_ctr );
duke@0 1663 tty->print_cr("%5d unresolved virtual call site", _resolve_virtual_ctr );
duke@0 1664 tty->print_cr("%5d unresolved opt virtual call site", _resolve_opt_virtual_ctr );
duke@0 1665
duke@0 1666 if( _mon_enter_stub_ctr ) tty->print_cr("%5d monitor enter stub", _mon_enter_stub_ctr );
duke@0 1667 if( _mon_exit_stub_ctr ) tty->print_cr("%5d monitor exit stub", _mon_exit_stub_ctr );
duke@0 1668 if( _mon_enter_ctr ) tty->print_cr("%5d monitor enter slow", _mon_enter_ctr );
duke@0 1669 if( _mon_exit_ctr ) tty->print_cr("%5d monitor exit slow", _mon_exit_ctr );
duke@0 1670 if( _partial_subtype_ctr) tty->print_cr("%5d slow partial subtype", _partial_subtype_ctr );
duke@0 1671 if( _jbyte_array_copy_ctr ) tty->print_cr("%5d byte array copies", _jbyte_array_copy_ctr );
duke@0 1672 if( _jshort_array_copy_ctr ) tty->print_cr("%5d short array copies", _jshort_array_copy_ctr );
duke@0 1673 if( _jint_array_copy_ctr ) tty->print_cr("%5d int array copies", _jint_array_copy_ctr );
duke@0 1674 if( _jlong_array_copy_ctr ) tty->print_cr("%5d long array copies", _jlong_array_copy_ctr );
duke@0 1675 if( _oop_array_copy_ctr ) tty->print_cr("%5d oop array copies", _oop_array_copy_ctr );
duke@0 1676 if( _checkcast_array_copy_ctr ) tty->print_cr("%5d checkcast array copies", _checkcast_array_copy_ctr );
duke@0 1677 if( _unsafe_array_copy_ctr ) tty->print_cr("%5d unsafe array copies", _unsafe_array_copy_ctr );
duke@0 1678 if( _generic_array_copy_ctr ) tty->print_cr("%5d generic array copies", _generic_array_copy_ctr );
duke@0 1679 if( _slow_array_copy_ctr ) tty->print_cr("%5d slow array copies", _slow_array_copy_ctr );
duke@0 1680 if( _find_handler_ctr ) tty->print_cr("%5d find exception handler", _find_handler_ctr );
duke@0 1681 if( _rethrow_ctr ) tty->print_cr("%5d rethrow handler", _rethrow_ctr );
duke@0 1682
never@1187 1683 AdapterHandlerLibrary::print_statistics();
never@1187 1684
duke@0 1685 if (xtty != NULL) xtty->tail("statistics");
duke@0 1686 }
duke@0 1687
duke@0 1688 inline double percent(int x, int y) {
duke@0 1689 return 100.0 * x / MAX2(y, 1);
duke@0 1690 }
duke@0 1691
duke@0 1692 class MethodArityHistogram {
duke@0 1693 public:
duke@0 1694 enum { MAX_ARITY = 256 };
duke@0 1695 private:
duke@0 1696 static int _arity_histogram[MAX_ARITY]; // histogram of #args
duke@0 1697 static int _size_histogram[MAX_ARITY]; // histogram of arg size in words
duke@0 1698 static int _max_arity; // max. arity seen
duke@0 1699 static int _max_size; // max. arg size seen
duke@0 1700
duke@0 1701 static void add_method_to_histogram(nmethod* nm) {
duke@0 1702 methodOop m = nm->method();
duke@0 1703 ArgumentCount args(m->signature());
duke@0 1704 int arity = args.size() + (m->is_static() ? 0 : 1);
duke@0 1705 int argsize = m->size_of_parameters();
duke@0 1706 arity = MIN2(arity, MAX_ARITY-1);
duke@0 1707 argsize = MIN2(argsize, MAX_ARITY-1);
duke@0 1708 int count = nm->method()->compiled_invocation_count();
duke@0 1709 _arity_histogram[arity] += count;
duke@0 1710 _size_histogram[argsize] += count;
duke@0 1711 _max_arity = MAX2(_max_arity, arity);
duke@0 1712 _max_size = MAX2(_max_size, argsize);
duke@0 1713 }
duke@0 1714
duke@0 1715 void print_histogram_helper(int n, int* histo, const char* name) {
duke@0 1716 const int N = MIN2(5, n);
duke@0 1717 tty->print_cr("\nHistogram of call arity (incl. rcvr, calls to compiled methods only):");
duke@0 1718 double sum = 0;
duke@0 1719 double weighted_sum = 0;
duke@0 1720 int i;
duke@0 1721 for (i = 0; i <= n; i++) { sum += histo[i]; weighted_sum += i*histo[i]; }
duke@0 1722 double rest = sum;
duke@0 1723 double percent = sum / 100;
duke@0 1724 for (i = 0; i <= N; i++) {
duke@0 1725 rest -= histo[i];
duke@0 1726 tty->print_cr("%4d: %7d (%5.1f%%)", i, histo[i], histo[i] / percent);
duke@0 1727 }
duke@0 1728 tty->print_cr("rest: %7d (%5.1f%%))", (int)rest, rest / percent);
duke@0 1729 tty->print_cr("(avg. %s = %3.1f, max = %d)", name, weighted_sum / sum, n);
duke@0 1730 }
duke@0 1731
duke@0 1732 void print_histogram() {
duke@0 1733 tty->print_cr("\nHistogram of call arity (incl. rcvr, calls to compiled methods only):");
duke@0 1734 print_histogram_helper(_max_arity, _arity_histogram, "arity");
duke@0 1735 tty->print_cr("\nSame for parameter size (in words):");
duke@0 1736 print_histogram_helper(_max_size, _size_histogram, "size");
duke@0 1737 tty->cr();
duke@0 1738 }
duke@0 1739
duke@0 1740 public:
duke@0 1741 MethodArityHistogram() {
duke@0 1742 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
duke@0 1743 _max_arity = _max_size = 0;
duke@0 1744 for (int i = 0; i < MAX_ARITY; i++) _arity_histogram[i] = _size_histogram [i] = 0;
duke@0 1745 CodeCache::nmethods_do(add_method_to_histogram);
duke@0 1746 print_histogram();
duke@0 1747 }
duke@0 1748 };
duke@0 1749
duke@0 1750 int MethodArityHistogram::_arity_histogram[MethodArityHistogram::MAX_ARITY];
duke@0 1751 int MethodArityHistogram::_size_histogram[MethodArityHistogram::MAX_ARITY];
duke@0 1752 int MethodArityHistogram::_max_arity;
duke@0 1753 int MethodArityHistogram::_max_size;
duke@0 1754
duke@0 1755 void SharedRuntime::print_call_statistics(int comp_total) {
duke@0 1756 tty->print_cr("Calls from compiled code:");
duke@0 1757 int total = _nof_normal_calls + _nof_interface_calls + _nof_static_calls;
duke@0 1758 int mono_c = _nof_normal_calls - _nof_optimized_calls - _nof_megamorphic_calls;
duke@0 1759 int mono_i = _nof_interface_calls - _nof_optimized_interface_calls - _nof_megamorphic_interface_calls;
duke@0 1760 tty->print_cr("\t%9d (%4.1f%%) total non-inlined ", total, percent(total, total));
duke@0 1761 tty->print_cr("\t%9d (%4.1f%%) virtual calls ", _nof_normal_calls, percent(_nof_normal_calls, total));
duke@0 1762 tty->print_cr("\t %9d (%3.0f%%) inlined ", _nof_inlined_calls, percent(_nof_inlined_calls, _nof_normal_calls));
duke@0 1763 tty->print_cr("\t %9d (%3.0f%%) optimized ", _nof_optimized_calls, percent(_nof_optimized_calls, _nof_normal_calls));
duke@0 1764 tty->print_cr("\t %9d (%3.0f%%) monomorphic ", mono_c, percent(mono_c, _nof_normal_calls));
duke@0 1765 tty->print_cr("\t %9d (%3.0f%%) megamorphic ", _nof_megamorphic_calls, percent(_nof_megamorphic_calls, _nof_normal_calls));
duke@0 1766 tty->print_cr("\t%9d (%4.1f%%) interface calls ", _nof_interface_calls, percent(_nof_interface_calls, total));
duke@0 1767 tty->print_cr("\t %9d (%3.0f%%) inlined ", _nof_inlined_interface_calls, percent(_nof_inlined_interface_calls, _nof_interface_calls));
duke@0 1768 tty->print_cr("\t %9d (%3.0f%%) optimized ", _nof_optimized_interface_calls, percent(_nof_optimized_interface_calls, _nof_interface_calls));
duke@0 1769 tty->print_cr("\t %9d (%3.0f%%) monomorphic ", mono_i, percent(mono_i, _nof_interface_calls));
duke@0 1770 tty->print_cr("\t %9d (%3.0f%%) megamorphic ", _nof_megamorphic_interface_calls, percent(_nof_megamorphic_interface_calls, _nof_interface_calls));
duke@0 1771 tty->print_cr("\t%9d (%4.1f%%) static/special calls", _nof_static_calls, percent(_nof_static_calls, total));
duke@0 1772 tty->print_cr("\t %9d (%3.0f%%) inlined ", _nof_inlined_static_calls, percent(_nof_inlined_static_calls, _nof_static_calls));
duke@0 1773 tty->cr();
duke@0 1774 tty->print_cr("Note 1: counter updates are not MT-safe.");
duke@0 1775 tty->print_cr("Note 2: %% in major categories are relative to total non-inlined calls;");
duke@0 1776 tty->print_cr(" %% in nested categories are relative to their category");
duke@0 1777 tty->print_cr(" (and thus add up to more than 100%% with inlining)");
duke@0 1778 tty->cr();
duke@0 1779
duke@0 1780 MethodArityHistogram h;
duke@0 1781 }
duke@0 1782 #endif
duke@0 1783
duke@0 1784
never@1187 1785 // A simple wrapper class around the calling convention information
never@1187 1786 // that allows sharing of adapters for the same calling convention.
never@1187 1787 class AdapterFingerPrint : public CHeapObj {
never@1187 1788 private:
never@1187 1789 union {
never@1187 1790 signed char _compact[12];
never@1187 1791 int _compact_int[3];
never@1187 1792 intptr_t* _fingerprint;
never@1187 1793 } _value;
never@1187 1794 int _length; // A negative length indicates that _value._fingerprint is the array.
never@1187 1795 // Otherwise it's in the compact form.
never@1187 1796
never@1187 1797 public:
never@1187 1798 AdapterFingerPrint(int total_args_passed, VMRegPair* regs) {
never@1187 1799 assert(sizeof(_value._compact) == sizeof(_value._compact_int), "must match");
never@1187 1800 _length = total_args_passed * 2;
never@1187 1801 if (_length < (int)sizeof(_value._compact)) {
never@1187 1802 _value._compact_int[0] = _value._compact_int[1] = _value._compact_int[2] = 0;
never@1187 1803 // Storing the signature encoded as signed chars hits about 98%
never@1187 1804 // of the time.
never@1187 1805 signed char* ptr = _value._compact;
never@1187 1806 int o = 0;
never@1187 1807 for (int i = 0; i < total_args_passed; i++) {
never@1187 1808 VMRegPair pair = regs[i];
never@1187 1809 intptr_t v1 = pair.first()->value();
never@1187 1810 intptr_t v2 = pair.second()->value();
never@1187 1811 if (v1 == (signed char) v1 &&
never@1187 1812 v2 == (signed char) v2) {
never@1187 1813 _value._compact[o++] = v1;
never@1187 1814 _value._compact[o++] = v2;
never@1187 1815 } else {
never@1187 1816 goto big;
never@1187 1817 }
never@1187 1818 }
never@1187 1819 _length = -_length;
never@1187 1820 return;
never@1187 1821 }
never@1187 1822 big:
never@1187 1823 _value._fingerprint = NEW_C_HEAP_ARRAY(intptr_t, _length);
never@1187 1824 int o = 0;
never@1187 1825 for (int i = 0; i < total_args_passed; i++) {
never@1187 1826 VMRegPair pair = regs[i];
never@1187 1827 intptr_t v1 = pair.first()->value();
never@1187 1828 intptr_t v2 = pair.second()->value();
never@1187 1829 _value._fingerprint[o++] = v1;
never@1187 1830 _value._fingerprint[o++] = v2;
never@1187 1831 }
never@1187 1832 }
never@1187 1833
never@1187 1834 AdapterFingerPrint(AdapterFingerPrint* orig) {
never@1187 1835 _length = orig->_length;
never@1187 1836 _value = orig->_value;
never@1187 1837 // take ownership of any storage by destroying the length
never@1187 1838 orig->_length = 0;
never@1187 1839 }
never@1187 1840
never@1187 1841 ~AdapterFingerPrint() {
never@1187 1842 if (_length > 0) {
never@1187 1843 FREE_C_HEAP_ARRAY(int, _value._fingerprint);
never@1187 1844 }
never@1187 1845 }
never@1187 1846
never@1187 1847 AdapterFingerPrint* allocate() {
never@1187 1848 return new AdapterFingerPrint(this);
never@1187 1849 }
never@1187 1850
never@1187 1851 intptr_t value(int index) {
never@1187 1852 if (_length < 0) {
never@1187 1853 return _value._compact[index];
never@1187 1854 }
never@1187 1855 return _value._fingerprint[index];
never@1187 1856 }
never@1187 1857 int length() {
never@1187 1858 if (_length < 0) return -_length;
never@1187 1859 return _length;
never@1187 1860 }
never@1187 1861
never@1187 1862 bool is_compact() {
never@1187 1863 return _length <= 0;
never@1187 1864 }
never@1187 1865
never@1187 1866 unsigned int compute_hash() {
never@1187 1867 intptr_t hash = 0;
never@1187 1868 for (int i = 0; i < length(); i++) {
never@1187 1869 intptr_t v = value(i);
never@1187 1870 hash = (hash << 8) ^ v ^ (hash >> 5);
never@1187 1871 }
never@1187 1872 return (unsigned int)hash;
never@1187 1873 }
never@1187 1874
never@1187 1875 const char* as_string() {
never@1187 1876 stringStream st;
never@1187 1877 for (int i = 0; i < length(); i++) {
never@1187 1878 st.print(PTR_FORMAT, value(i));
never@1187 1879 }
never@1187 1880 return st.as_string();
never@1187 1881 }
never@1187 1882
never@1187 1883 bool equals(AdapterFingerPrint* other) {
never@1187 1884 if (other->_length != _length) {
never@1187 1885 return false;
never@1187 1886 }
never@1187 1887 if (_length < 0) {
never@1187 1888 return _value._compact_int[0] == other->_value._compact_int[0] &&
never@1187 1889 _value._compact_int[1] == other->_value._compact_int[1] &&
never@1187 1890 _value._compact_int[2] == other->_value._compact_int[2];
never@1187 1891 } else {
never@1187 1892 for (int i = 0; i < _length; i++) {
never@1187 1893 if (_value._fingerprint[i] != other->_value._fingerprint[i]) {
never@1187 1894 return false;
never@1187 1895 }
never@1187 1896 }
never@1187 1897 }
never@1187 1898 return true;
never@1187 1899 }
never@1187 1900 };
never@1187 1901
never@1187 1902
never@1187 1903 // A hashtable mapping from AdapterFingerPrints to AdapterHandlerEntries
never@1187 1904 class AdapterHandlerTable : public BasicHashtable {
never@1187 1905 friend class AdapterHandlerTableIterator;
never@1187 1906
never@1187 1907 private:
never@1187 1908
never@1187 1909 #ifdef ASSERT
never@1187 1910 static int _lookups; // number of calls to lookup
never@1187 1911 static int _buckets; // number of buckets checked
never@1187 1912 static int _equals; // number of buckets checked with matching hash
never@1187 1913 static int _hits; // number of successful lookups
never@1187 1914 static int _compact; // number of equals calls with compact signature
never@1187 1915 #endif
never@1187 1916
never@1187 1917 AdapterHandlerEntry* bucket(int i) {
never@1187 1918 return (AdapterHandlerEntry*)BasicHashtable::bucket(i);
never@1187 1919 }
never@1187 1920
never@1187 1921 public:
never@1187 1922 AdapterHandlerTable()
never@1187 1923 : BasicHashtable(293, sizeof(AdapterHandlerEntry)) { }
never@1187 1924
never@1187 1925 // Create a new entry suitable for insertion in the table
never@1187 1926 AdapterHandlerEntry* new_entry(AdapterFingerPrint* fingerprint, address i2c_entry, address c2i_entry, address c2i_unverified_entry) {
never@1187 1927 AdapterHandlerEntry* entry = (AdapterHandlerEntry*)BasicHashtable::new_entry(fingerprint->compute_hash());
never@1187 1928 entry->init(fingerprint, i2c_entry, c2i_entry, c2i_unverified_entry);
never@1187 1929 return entry;
never@1187 1930 }
never@1187 1931
never@1187 1932 // Insert an entry into the table
never@1187 1933 void add(AdapterHandlerEntry* entry) {
never@1187 1934 int index = hash_to_index(entry->hash());
never@1187 1935 add_entry(index, entry);
never@1187 1936 }
never@1187 1937
never@1187 1938 // Find a entry with the same fingerprint if it exists
never@1187 1939 AdapterHandlerEntry* lookup(int total_args_passed, VMRegPair* regs) {
never@1187 1940 debug_only(_lookups++);
never@1187 1941 AdapterFingerPrint fp(total_args_passed, regs);
never@1187 1942 unsigned int hash = fp.compute_hash();
never@1187 1943 int index = hash_to_index(hash);
never@1187 1944 for (AdapterHandlerEntry* e = bucket(index); e != NULL; e = e->next()) {
never@1187 1945 debug_only(_buckets++);
never@1187 1946 if (e->hash() == hash) {
never@1187 1947 debug_only(_equals++);
never@1187 1948 if (fp.equals(e->fingerprint())) {
never@1187 1949 #ifdef ASSERT
never@1187 1950 if (fp.is_compact()) _compact++;
never@1187 1951 _hits++;
never@1187 1952 #endif
never@1187 1953 return e;
never@1187 1954 }
never@1187 1955 }
never@1187 1956 }
never@1187 1957 return NULL;
never@1187 1958 }
never@1187 1959
never@1187 1960 void print_statistics() {
never@1187 1961 ResourceMark rm;
never@1187 1962 int longest = 0;
never@1187 1963 int empty = 0;
never@1187 1964 int total = 0;
never@1187 1965 int nonempty = 0;
never@1187 1966 for (int index = 0; index < table_size(); index++) {
never@1187 1967 int count = 0;
never@1187 1968 for (AdapterHandlerEntry* e = bucket(index); e != NULL; e = e->next()) {
never@1187 1969 count++;
never@1187 1970 }
never@1187 1971 if (count != 0) nonempty++;
never@1187 1972 if (count == 0) empty++;
never@1187 1973 if (count > longest) longest = count;
never@1187 1974 total += count;
never@1187 1975 }
never@1187 1976 tty->print_cr("AdapterHandlerTable: empty %d longest %d total %d average %f",
never@1187 1977 empty, longest, total, total / (double)nonempty);
never@1187 1978 #ifdef ASSERT
never@1187 1979 tty->print_cr("AdapterHandlerTable: lookups %d buckets %d equals %d hits %d compact %d",
never@1187 1980 _lookups, _buckets, _equals, _hits, _compact);
never@1187 1981 #endif
never@1187 1982 }
never@1187 1983 };
never@1187 1984
never@1187 1985
never@1187 1986 #ifdef ASSERT
never@1187 1987
never@1187 1988 int AdapterHandlerTable::_lookups;
never@1187 1989 int AdapterHandlerTable::_buckets;
never@1187 1990 int AdapterHandlerTable::_equals;
never@1187 1991 int AdapterHandlerTable::_hits;
never@1187 1992 int AdapterHandlerTable::_compact;
never@1187 1993
never@1187 1994 class AdapterHandlerTableIterator : public StackObj {
never@1187 1995 private:
never@1187 1996 AdapterHandlerTable* _table;
never@1187 1997 int _index;
never@1187 1998 AdapterHandlerEntry* _current;
never@1187 1999
never@1187 2000 void scan() {
never@1187 2001 while (_index < _table->table_size()) {
never@1187 2002 AdapterHandlerEntry* a = _table->bucket(_index);
never@1187 2003 if (a != NULL) {
never@1187 2004 _current = a;
never@1187 2005 return;
never@1187 2006 }
never@1187 2007 _index++;
never@1187 2008 }
never@1187 2009 }
never@1187 2010
never@1187 2011 public:
never@1187 2012 AdapterHandlerTableIterator(AdapterHandlerTable* table): _table(table), _index(0), _current(NULL) {
never@1187 2013 scan();
never@1187 2014 }
never@1187 2015 bool has_next() {
never@1187 2016 return _current != NULL;
never@1187 2017 }
never@1187 2018 AdapterHandlerEntry* next() {
never@1187 2019 if (_current != NULL) {
never@1187 2020 AdapterHandlerEntry* result = _current;
never@1187 2021 _current = _current->next();
never@1187 2022 if (_current == NULL) scan();
never@1187 2023 return result;
never@1187 2024 } else {
never@1187 2025 return NULL;
never@1187 2026 }
never@1187 2027 }
never@1187 2028 };
never@1187 2029 #endif
never@1187 2030
never@1187 2031
duke@0 2032 // ---------------------------------------------------------------------------
duke@0 2033 // Implementation of AdapterHandlerLibrary
duke@0 2034 const char* AdapterHandlerEntry::name = "I2C/C2I adapters";
never@1187 2035 AdapterHandlerTable* AdapterHandlerLibrary::_adapters = NULL;
never@1187 2036 AdapterHandlerEntry* AdapterHandlerLibrary::_abstract_method_handler = NULL;
duke@0 2037 const int AdapterHandlerLibrary_size = 16*K;
kvn@742 2038 BufferBlob* AdapterHandlerLibrary::_buffer = NULL;
kvn@742 2039
kvn@742 2040 BufferBlob* AdapterHandlerLibrary::buffer_blob() {
kvn@742 2041 // Should be called only when AdapterHandlerLibrary_lock is active.
kvn@742 2042 if (_buffer == NULL) // Initialize lazily
kvn@742 2043 _buffer = BufferBlob::create("adapters", AdapterHandlerLibrary_size);
kvn@742 2044 return _buffer;
kvn@742 2045 }
duke@0 2046
duke@0 2047 void AdapterHandlerLibrary::initialize() {
never@1187 2048 if (_adapters != NULL) return;
never@1187 2049 _adapters = new AdapterHandlerTable();
duke@0 2050
duke@0 2051 // Create a special handler for abstract methods. Abstract methods
duke@0 2052 // are never compiled so an i2c entry is somewhat meaningless, but
duke@0 2053 // fill it in with something appropriate just in case. Pass handle
duke@0 2054 // wrong method for the c2i transitions.
duke@0 2055 address wrong_method = SharedRuntime::get_handle_wrong_method_stub();
never@1187 2056 _abstract_method_handler = AdapterHandlerLibrary::new_entry(new AdapterFingerPrint(0, NULL),
never@1187 2057 StubRoutines::throw_AbstractMethodError_entry(),
never@1187 2058 wrong_method, wrong_method);
duke@0 2059 }
duke@0 2060
never@1187 2061 AdapterHandlerEntry* AdapterHandlerLibrary::new_entry(AdapterFingerPrint* fingerprint,
never@1187 2062 address i2c_entry,
never@1187 2063 address c2i_entry,
never@1187 2064 address c2i_unverified_entry) {
never@1187 2065 return _adapters->new_entry(fingerprint, i2c_entry, c2i_entry, c2i_unverified_entry);
never@1187 2066 }
never@1187 2067
never@1187 2068 AdapterHandlerEntry* AdapterHandlerLibrary::get_adapter(methodHandle method) {
never@1187 2069 // Use customized signature handler. Need to lock around updates to
never@1187 2070 // the AdapterHandlerTable (it is not safe for concurrent readers
never@1187 2071 // and a single writer: this could be fixed if it becomes a
never@1187 2072 // problem).
duke@0 2073
duke@0 2074 // Get the address of the ic_miss handlers before we grab the
duke@0 2075 // AdapterHandlerLibrary_lock. This fixes bug 6236259 which
duke@0 2076 // was caused by the initialization of the stubs happening
duke@0 2077 // while we held the lock and then notifying jvmti while
duke@0 2078 // holding it. This just forces the initialization to be a little
duke@0 2079 // earlier.
duke@0 2080 address ic_miss = SharedRuntime::get_ic_miss_stub();
duke@0 2081 assert(ic_miss != NULL, "must have handler");
duke@0 2082
never@1187 2083 ResourceMark rm;
never@1187 2084
kvn@742 2085 NOT_PRODUCT(int code_size);
duke@0 2086 BufferBlob *B = NULL;
kvn@742 2087 AdapterHandlerEntry* entry = NULL;
never@1187 2088 AdapterFingerPrint* fingerprint = NULL;
duke@0 2089 {
duke@0 2090 MutexLocker mu(AdapterHandlerLibrary_lock);
duke@0 2091 // make sure data structure is initialized
duke@0 2092 initialize();
duke@0 2093
duke@0 2094 if (method->is_abstract()) {
never@1187 2095 return _abstract_method_handler;
duke@0 2096 }
duke@0 2097
never@1187 2098 // Fill in the signature array, for the calling-convention call.
never@1187 2099 int total_args_passed = method->size_of_parameters(); // All args on stack
never@1187 2100
never@1187 2101 BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, total_args_passed);
never@1187 2102 VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair, total_args_passed);
never@1187 2103 int i = 0;
never@1187 2104 if (!method->is_static()) // Pass in receiver first
never@1187 2105 sig_bt[i++] = T_OBJECT;
never@1187 2106 for (SignatureStream ss(method->signature()); !ss.at_return_type(); ss.next()) {
never@1187 2107 sig_bt[i++] = ss.type(); // Collect remaining bits of signature
never@1187 2108 if (ss.type() == T_LONG || ss.type() == T_DOUBLE)
never@1187 2109 sig_bt[i++] = T_VOID; // Longs & doubles take 2 Java slots
never@1187 2110 }
never@1187 2111 assert(i == total_args_passed, "");
never@1187 2112
never@1187 2113 // Get a description of the compiled java calling convention and the largest used (VMReg) stack slot usage
never@1187 2114 int comp_args_on_stack = SharedRuntime::java_calling_convention(sig_bt, regs, total_args_passed, false);
never@1187 2115
duke@0 2116 // Lookup method signature's fingerprint
never@1187 2117 entry = _adapters->lookup(total_args_passed, regs);
never@1187 2118 if (entry != NULL) {
never@1187 2119 return entry;
duke@0 2120 }
duke@0 2121
never@1187 2122 // Make a C heap allocated version of the fingerprint to store in the adapter
never@1187 2123 fingerprint = new AdapterFingerPrint(total_args_passed, regs);
never@1187 2124
duke@0 2125 // Create I2C & C2I handlers
duke@0 2126
kvn@742 2127 BufferBlob* buf = buffer_blob(); // the temporary code buffer in CodeCache
kvn@742 2128 if (buf != NULL) {
kvn@742 2129 CodeBuffer buffer(buf->instructions_begin(), buf->instructions_size());
kvn@742 2130 short buffer_locs[20];
kvn@742 2131 buffer.insts()->initialize_shared_locs((relocInfo*)buffer_locs,
kvn@742 2132 sizeof(buffer_locs)/sizeof(relocInfo));
kvn@742 2133 MacroAssembler _masm(&buffer);
duke@0 2134
kvn@742 2135 entry = SharedRuntime::generate_i2c2i_adapters(&_masm,
kvn@742 2136 total_args_passed,
kvn@742 2137 comp_args_on_stack,
kvn@742 2138 sig_bt,
never@1187 2139 regs,
never@1187 2140 fingerprint);
kvn@742 2141
kvn@742 2142 B = BufferBlob::create(AdapterHandlerEntry::name, &buffer);
kvn@742 2143 NOT_PRODUCT(code_size = buffer.code_size());
duke@0 2144 }
kvn@28 2145 if (B == NULL) {
kvn@28 2146 // CodeCache is full, disable compilation
kvn@28 2147 // Ought to log this but compile log is only per compile thread
kvn@28 2148 // and we're some non descript Java thread.
kvn@28 2149 UseInterpreter = true;
kvn@28 2150 if (UseCompiler || AlwaysCompileLoopMethods ) {
kvn@28 2151 #ifndef PRODUCT
kvn@28 2152 warning("CodeCache is full. Compiler has been disabled");
kvn@28 2153 if (CompileTheWorld || ExitOnFullCodeCache) {
kvn@28 2154 before_exit(JavaThread::current());
kvn@28 2155 exit_globals(); // will delete tty
kvn@28 2156 vm_direct_exit(CompileTheWorld ? 0 : 1);
kvn@28 2157 }
kvn@28 2158 #endif
kvn@28 2159 UseCompiler = false;
kvn@28 2160 AlwaysCompileLoopMethods = false;
kvn@28 2161 }
never@1187 2162 return NULL; // Out of CodeCache space
kvn@28 2163 }
duke@0 2164 entry->relocate(B->instructions_begin());
duke@0 2165 #ifndef PRODUCT
duke@0 2166 // debugging suppport
duke@0 2167 if (PrintAdapterHandlers) {
duke@0 2168 tty->cr();
never@1187 2169 tty->print_cr("i2c argument handler #%d for: %s %s (fingerprint = %s, %d bytes generated)",
never@1187 2170 _adapters->number_of_entries(), (method->is_static() ? "static" : "receiver"),
never@1187 2171 method->signature()->as_C_string(), fingerprint->as_string(), code_size );
duke@0 2172 tty->print_cr("c2i argument handler starts at %p",entry->get_c2i_entry());
kvn@742 2173 Disassembler::decode(entry->get_i2c_entry(), entry->get_i2c_entry() + code_size);
duke@0 2174 }
duke@0 2175 #endif
duke@0 2176
never@1187 2177 _adapters->add(entry);
duke@0 2178 }
duke@0 2179 // Outside of the lock
duke@0 2180 if (B != NULL) {
duke@0 2181 char blob_id[256];
duke@0 2182 jio_snprintf(blob_id,
duke@0 2183 sizeof(blob_id),
never@1187 2184 "%s(%s)@" PTR_FORMAT,
duke@0 2185 AdapterHandlerEntry::name,
never@1187 2186 fingerprint->as_string(),
duke@0 2187 B->instructions_begin());
duke@0 2188 VTune::register_stub(blob_id, B->instructions_begin(), B->instructions_end());
duke@0 2189 Forte::register_stub(blob_id, B->instructions_begin(), B->instructions_end());
duke@0 2190
duke@0 2191 if (JvmtiExport::should_post_dynamic_code_generated()) {
duke@0 2192 JvmtiExport::post_dynamic_code_generated(blob_id,
duke@0 2193 B->instructions_begin(),
duke@0 2194 B->instructions_end());
duke@0 2195 }
duke@0 2196 }
never@1187 2197 return entry;
duke@0 2198 }
duke@0 2199
duke@0 2200 void AdapterHandlerEntry::relocate(address new_base) {
duke@0 2201 ptrdiff_t delta = new_base - _i2c_entry;
duke@0 2202 _i2c_entry += delta;
duke@0 2203 _c2i_entry += delta;
duke@0 2204 _c2i_unverified_entry += delta;
duke@0 2205 }
duke@0 2206
duke@0 2207 // Create a native wrapper for this native method. The wrapper converts the
duke@0 2208 // java compiled calling convention to the native convention, handlizes
duke@0 2209 // arguments, and transitions to native. On return from the native we transition
duke@0 2210 // back to java blocking if a safepoint is in progress.
duke@0 2211 nmethod *AdapterHandlerLibrary::create_native_wrapper(methodHandle method) {
duke@0 2212 ResourceMark rm;
duke@0 2213 nmethod* nm = NULL;
duke@0 2214
duke@0 2215 if (PrintCompilation) {
duke@0 2216 ttyLocker ttyl;
duke@0 2217 tty->print("--- n%s ", (method->is_synchronized() ? "s" : " "));
duke@0 2218 method->print_short_name(tty);
duke@0 2219 if (method->is_static()) {
duke@0 2220 tty->print(" (static)");
duke@0 2221 }
duke@0 2222 tty->cr();
duke@0 2223 }
duke@0 2224
duke@0 2225 assert(method->has_native_function(), "must have something valid to call!");
duke@0 2226
duke@0 2227 {
duke@0 2228 // perform the work while holding the lock, but perform any printing outside the lock
duke@0 2229 MutexLocker mu(AdapterHandlerLibrary_lock);
duke@0 2230 // See if somebody beat us to it
duke@0 2231 nm = method->code();
duke@0 2232 if (nm) {
duke@0 2233 return nm;
duke@0 2234 }
duke@0 2235
kvn@742 2236 ResourceMark rm;
duke@0 2237
kvn@742 2238 BufferBlob* buf = buffer_blob(); // the temporary code buffer in CodeCache
kvn@742 2239 if (buf != NULL) {
kvn@742 2240 CodeBuffer buffer(buf->instructions_begin(), buf->instructions_size());
kvn@742 2241 double locs_buf[20];
kvn@742 2242 buffer.insts()->initialize_shared_locs((relocInfo*)locs_buf, sizeof(locs_buf) / sizeof(relocInfo));
kvn@742 2243 MacroAssembler _masm(&buffer);
duke@0 2244
kvn@742 2245 // Fill in the signature array, for the calling-convention call.
kvn@742 2246 int total_args_passed = method->size_of_parameters();
kvn@742 2247
kvn@742 2248 BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType,total_args_passed);
kvn@742 2249 VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair,total_args_passed);
kvn@742 2250 int i=0;
kvn@742 2251 if( !method->is_static() ) // Pass in receiver first
kvn@742 2252 sig_bt[i++] = T_OBJECT;
kvn@742 2253 SignatureStream ss(method->signature());
kvn@742 2254 for( ; !ss.at_return_type(); ss.next()) {
kvn@742 2255 sig_bt[i++] = ss.type(); // Collect remaining bits of signature
kvn@742 2256 if( ss.type() == T_LONG || ss.type() == T_DOUBLE )
kvn@742 2257 sig_bt[i++] = T_VOID; // Longs & doubles take 2 Java slots
kvn@742 2258 }
kvn@742 2259 assert( i==total_args_passed, "" );
kvn@742 2260 BasicType ret_type = ss.type();
kvn@742 2261
kvn@742 2262 // Now get the compiled-Java layout as input arguments
kvn@742 2263 int comp_args_on_stack;
kvn@742 2264 comp_args_on_stack = SharedRuntime::java_calling_convention(sig_bt, regs, total_args_passed, false);
kvn@742 2265
kvn@742 2266 // Generate the compiled-to-native wrapper code
kvn@742 2267 nm = SharedRuntime::generate_native_wrapper(&_masm,
kvn@742 2268 method,
kvn@742 2269 total_args_passed,
kvn@742 2270 comp_args_on_stack,
kvn@742 2271 sig_bt,regs,
kvn@742 2272 ret_type);
duke@0 2273 }
duke@0 2274 }
duke@0 2275
duke@0 2276 // Must unlock before calling set_code
duke@0 2277 // Install the generated code.
duke@0 2278 if (nm != NULL) {
duke@0 2279 method->set_code(method, nm);
duke@0 2280 nm->post_compiled_method_load_event();
duke@0 2281 } else {
duke@0 2282 // CodeCache is full, disable compilation
duke@0 2283 // Ought to log this but compile log is only per compile thread
duke@0 2284 // and we're some non descript Java thread.
duke@0 2285 UseInterpreter = true;
duke@0 2286 if (UseCompiler || AlwaysCompileLoopMethods ) {
duke@0 2287 #ifndef PRODUCT
duke@0 2288 warning("CodeCache is full. Compiler has been disabled");
duke@0 2289 if (CompileTheWorld || ExitOnFullCodeCache) {
duke@0 2290 before_exit(JavaThread::current());
duke@0 2291 exit_globals(); // will delete tty
duke@0 2292 vm_direct_exit(CompileTheWorld ? 0 : 1);
duke@0 2293 }
duke@0 2294 #endif
duke@0 2295 UseCompiler = false;
duke@0 2296 AlwaysCompileLoopMethods = false;
duke@0 2297 }
duke@0 2298 }
duke@0 2299 return nm;
duke@0 2300 }
duke@0 2301
kamg@116 2302 #ifdef HAVE_DTRACE_H
kamg@116 2303 // Create a dtrace nmethod for this method. The wrapper converts the
kamg@116 2304 // java compiled calling convention to the native convention, makes a dummy call
kamg@116 2305 // (actually nops for the size of the call instruction, which become a trap if
kamg@116 2306 // probe is enabled). The returns to the caller. Since this all looks like a
kamg@116 2307 // leaf no thread transition is needed.
kamg@116 2308
kamg@116 2309 nmethod *AdapterHandlerLibrary::create_dtrace_nmethod(methodHandle method) {
kamg@116 2310 ResourceMark rm;
kamg@116 2311 nmethod* nm = NULL;
kamg@116 2312
kamg@116 2313 if (PrintCompilation) {
kamg@116 2314 ttyLocker ttyl;
kamg@116 2315 tty->print("--- n%s ");
kamg@116 2316 method->print_short_name(tty);
kamg@116 2317 if (method->is_static()) {
kamg@116 2318 tty->print(" (static)");
kamg@116 2319 }
kamg@116 2320 tty->cr();
kamg@116 2321 }
kamg@116 2322
kamg@116 2323 {
kamg@116 2324 // perform the work while holding the lock, but perform any printing
kamg@116 2325 // outside the lock
kamg@116 2326 MutexLocker mu(AdapterHandlerLibrary_lock);
kamg@116 2327 // See if somebody beat us to it
kamg@116 2328 nm = method->code();
kamg@116 2329 if (nm) {
kamg@116 2330 return nm;
kamg@116 2331 }
kamg@116 2332
kvn@742 2333 ResourceMark rm;
kvn@742 2334
kvn@742 2335 BufferBlob* buf = buffer_blob(); // the temporary code buffer in CodeCache
kvn@742 2336 if (buf != NULL) {
kvn@742 2337 CodeBuffer buffer(buf->instructions_begin(), buf->instructions_size());
kvn@742 2338 // Need a few relocation entries
kvn@742 2339 double locs_buf[20];
kvn@742 2340 buffer.insts()->initialize_shared_locs(
kamg@116 2341 (relocInfo*)locs_buf, sizeof(locs_buf) / sizeof(relocInfo));
kvn@742 2342 MacroAssembler _masm(&buffer);
kamg@116 2343
kvn@742 2344 // Generate the compiled-to-native wrapper code
kvn@742 2345 nm = SharedRuntime::generate_dtrace_nmethod(&_masm, method);
kvn@742 2346 }
kamg@116 2347 }
kamg@116 2348 return nm;
kamg@116 2349 }
kamg@116 2350
kamg@116 2351 // the dtrace method needs to convert java lang string to utf8 string.
kamg@116 2352 void SharedRuntime::get_utf(oopDesc* src, address dst) {
kamg@116 2353 typeArrayOop jlsValue = java_lang_String::value(src);
kamg@116 2354 int jlsOffset = java_lang_String::offset(src);
kamg@116 2355 int jlsLen = java_lang_String::length(src);
kamg@116 2356 jchar* jlsPos = (jlsLen == 0) ? NULL :
kamg@116 2357 jlsValue->char_at_addr(jlsOffset);
kamg@116 2358 (void) UNICODE::as_utf8(jlsPos, jlsLen, (char *)dst, max_dtrace_string_size);
kamg@116 2359 }
kamg@116 2360 #endif // ndef HAVE_DTRACE_H
kamg@116 2361
duke@0 2362 // -------------------------------------------------------------------------
duke@0 2363 // Java-Java calling convention
duke@0 2364 // (what you use when Java calls Java)
duke@0 2365
duke@0 2366 //------------------------------name_for_receiver----------------------------------
duke@0 2367 // For a given signature, return the VMReg for parameter 0.
duke@0 2368 VMReg SharedRuntime::name_for_receiver() {
duke@0 2369 VMRegPair regs;
duke@0 2370 BasicType sig_bt = T_OBJECT;
duke@0 2371 (void) java_calling_convention(&sig_bt, &regs, 1, true);
duke@0 2372 // Return argument 0 register. In the LP64 build pointers
duke@0 2373 // take 2 registers, but the VM wants only the 'main' name.
duke@0 2374 return regs.first();
duke@0 2375 }
duke@0 2376
twisti@1138 2377 VMRegPair *SharedRuntime::find_callee_arguments(symbolOop sig, bool has_receiver, int* arg_size) {
duke@0 2378 // This method is returning a data structure allocating as a
duke@0 2379 // ResourceObject, so do not put any ResourceMarks in here.
duke@0 2380 char *s = sig->as_C_string();
duke@0 2381 int len = (int)strlen(s);
duke@0 2382 *s++; len--; // Skip opening paren
duke@0 2383 char *t = s+len;
duke@0 2384 while( *(--t) != ')' ) ; // Find close paren
duke@0 2385
duke@0 2386 BasicType *sig_bt = NEW_RESOURCE_ARRAY( BasicType, 256 );
duke@0 2387 VMRegPair *regs = NEW_RESOURCE_ARRAY( VMRegPair, 256 );
duke@0 2388 int cnt = 0;
twisti@1138 2389 if (has_receiver) {
duke@0 2390 sig_bt[cnt++] = T_OBJECT; // Receiver is argument 0; not in signature
duke@0 2391 }
duke@0 2392
duke@0 2393 while( s < t ) {
duke@0 2394 switch( *s++ ) { // Switch on signature character
duke@0 2395 case 'B': sig_bt[cnt++] = T_BYTE; break;
duke@0 2396 case 'C': sig_bt[cnt++] = T_CHAR; break;
duke@0 2397 case 'D': sig_bt[cnt++] = T_DOUBLE; sig_bt[cnt++] = T_VOID; break;
duke@0 2398 case 'F': sig_bt[cnt++] = T_FLOAT; break;
duke@0 2399 case 'I': sig_bt[cnt++] = T_INT; break;
duke@0 2400 case 'J': sig_bt[cnt++] = T_LONG; sig_bt[cnt++] = T_VOID; break;
duke@0 2401 case 'S': sig_bt[cnt++] = T_SHORT; break;
duke@0 2402 case 'Z': sig_bt[cnt++] = T_BOOLEAN; break;
duke@0 2403 case 'V': sig_bt[cnt++] = T_VOID; break;
duke@0 2404 case 'L': // Oop
duke@0 2405 while( *s++ != ';' ) ; // Skip signature
duke@0 2406 sig_bt[cnt++] = T_OBJECT;
duke@0 2407 break;
duke@0 2408 case '[': { // Array
duke@0 2409 do { // Skip optional size
duke@0 2410 while( *s >= '0' && *s <= '9' ) s++;
duke@0 2411 } while( *s++ == '[' ); // Nested arrays?
duke@0 2412 // Skip element type
duke@0 2413 if( s[-1] == 'L' )
duke@0 2414 while( *s++ != ';' ) ; // Skip signature
duke@0 2415 sig_bt[cnt++] = T_ARRAY;
duke@0 2416 break;
duke@0 2417 }
duke@0 2418 default : ShouldNotReachHere();
duke@0 2419 }
duke@0 2420 }
duke@0 2421 assert( cnt < 256, "grow table size" );
duke@0 2422
duke@0 2423 int comp_args_on_stack;
duke@0 2424 comp_args_on_stack = java_calling_convention(sig_bt, regs, cnt, true);
duke@0 2425
duke@0 2426 // the calling convention doesn't count out_preserve_stack_slots so
duke@0 2427 // we must add that in to get "true" stack offsets.
duke@0 2428
duke@0 2429 if (comp_args_on_stack) {
duke@0 2430 for (int i = 0; i < cnt; i++) {
duke@0 2431 VMReg reg1 = regs[i].first();
duke@0 2432 if( reg1->is_stack()) {
duke@0 2433 // Yuck
duke@0 2434 reg1 = reg1->bias(out_preserve_stack_slots());
duke@0 2435 }
duke@0 2436 VMReg reg2 = regs[i].second();
duke@0 2437 if( reg2->is_stack()) {
duke@0 2438 // Yuck
duke@0 2439 reg2 = reg2->bias(out_preserve_stack_slots());
duke@0 2440 }
duke@0 2441 regs[i].set_pair(reg2, reg1);
duke@0 2442 }
duke@0 2443 }
duke@0 2444
duke@0 2445 // results
duke@0 2446 *arg_size = cnt;
duke@0 2447 return regs;
duke@0 2448 }
duke@0 2449
duke@0 2450 // OSR Migration Code
duke@0 2451 //
duke@0 2452 // This code is used convert interpreter frames into compiled frames. It is
duke@0 2453 // called from very start of a compiled OSR nmethod. A temp array is
duke@0 2454 // allocated to hold the interesting bits of the interpreter frame. All
duke@0 2455 // active locks are inflated to allow them to move. The displaced headers and
duke@0 2456 // active interpeter locals are copied into the temp buffer. Then we return
duke@0 2457 // back to the compiled code. The compiled code then pops the current
duke@0 2458 // interpreter frame off the stack and pushes a new compiled frame. Then it
duke@0 2459 // copies the interpreter locals and displaced headers where it wants.
duke@0 2460 // Finally it calls back to free the temp buffer.
duke@0 2461 //
duke@0 2462 // All of this is done NOT at any Safepoint, nor is any safepoint or GC allowed.
duke@0 2463
duke@0 2464 JRT_LEAF(intptr_t*, SharedRuntime::OSR_migration_begin( JavaThread *thread) )
duke@0 2465
duke@0 2466 #ifdef IA64
duke@0 2467 ShouldNotReachHere(); // NYI
duke@0 2468 #endif /* IA64 */
duke@0 2469
duke@0 2470 //
duke@0 2471 // This code is dependent on the memory layout of the interpreter local
duke@0 2472 // array and the monitors. On all of our platforms the layout is identical
duke@0 2473 // so this code is shared. If some platform lays the their arrays out
duke@0 2474 // differently then this code could move to platform specific code or
duke@0 2475 // the code here could be modified to copy items one at a time using
duke@0 2476 // frame accessor methods and be platform independent.
duke@0 2477
duke@0 2478 frame fr = thread->last_frame();
duke@0 2479 assert( fr.is_interpreted_frame(), "" );
duke@0 2480 assert( fr.interpreter_frame_expression_stack_size()==0, "only handle empty stacks" );
duke@0 2481
duke@0 2482 // Figure out how many monitors are active.
duke@0 2483 int active_monitor_count = 0;
duke@0 2484 for( BasicObjectLock *kptr = fr.interpreter_frame_monitor_end();
duke@0 2485 kptr < fr.interpreter_frame_monitor_begin();
duke@0 2486 kptr = fr.next_monitor_in_interpreter_frame(kptr) ) {
duke@0 2487 if( kptr->obj() != NULL ) active_monitor_count++;
duke@0 2488 }
duke@0 2489
duke@0 2490 // QQQ we could place number of active monitors in the array so that compiled code
duke@0 2491 // could double check it.
duke@0 2492
duke@0 2493 methodOop moop = fr.interpreter_frame_method();
duke@0 2494 int max_locals = moop->max_locals();
duke@0 2495 // Allocate temp buffer, 1 word per local & 2 per active monitor
duke@0 2496 int buf_size_words = max_locals + active_monitor_count*2;
duke@0 2497 intptr_t *buf = NEW_C_HEAP_ARRAY(intptr_t,buf_size_words);
duke@0 2498
duke@0 2499 // Copy the locals. Order is preserved so that loading of longs works.
duke@0 2500 // Since there's no GC I can copy the oops blindly.
duke@0 2501 assert( sizeof(HeapWord)==sizeof(intptr_t), "fix this code");
duke@0 2502 if (TaggedStackInterpreter) {
duke@0 2503 for (int i = 0; i < max_locals; i++) {
duke@0 2504 // copy only each local separately to the buffer avoiding the tag
duke@0 2505 buf[i] = *fr.interpreter_frame_local_at(max_locals-i-1);
duke@0 2506 }
duke@0 2507 } else {
duke@0 2508 Copy::disjoint_words(
duke@0 2509 (HeapWord*)fr.interpreter_frame_local_at(max_locals-1),
duke@0 2510 (HeapWord*)&buf[0],
duke@0 2511 max_locals);
duke@0 2512 }
duke@0 2513
duke@0 2514 // Inflate locks. Copy the displaced headers. Be careful, there can be holes.
duke@0 2515 int i = max_locals;
duke@0 2516 for( BasicObjectLock *kptr2 = fr.interpreter_frame_monitor_end();
duke@0 2517 kptr2 < fr.interpreter_frame_monitor_begin();
duke@0 2518 kptr2 = fr.next_monitor_in_interpreter_frame(kptr2) ) {
duke@0 2519 if( kptr2->obj() != NULL) { // Avoid 'holes' in the monitor array
duke@0 2520 BasicLock *lock = kptr2->lock();
duke@0 2521 // Inflate so the displaced header becomes position-independent
duke@0 2522 if (lock->displaced_header()->is_unlocked())
duke@0 2523 ObjectSynchronizer::inflate_helper(kptr2->obj());
duke@0 2524 // Now the displaced header is free to move
duke@0 2525 buf[i++] = (intptr_t)lock->displaced_header();
duke@0 2526 buf[i++] = (intptr_t)kptr2->obj();
duke@0 2527 }
duke@0 2528 }
duke@0 2529 assert( i - max_locals == active_monitor_count*2, "found the expected number of monitors" );
duke@0 2530
duke@0 2531 return buf;
duke@0 2532 JRT_END
duke@0 2533
duke@0 2534 JRT_LEAF(void, SharedRuntime::OSR_migration_end( intptr_t* buf) )
duke@0 2535 FREE_C_HEAP_ARRAY(intptr_t,buf);
duke@0 2536 JRT_END
duke@0 2537
duke@0 2538 #ifndef PRODUCT
duke@0 2539 bool AdapterHandlerLibrary::contains(CodeBlob* b) {
never@1187 2540 AdapterHandlerTableIterator iter(_adapters);
never@1187 2541 while (iter.has_next()) {
never@1187 2542 AdapterHandlerEntry* a = iter.next();
never@1187 2543 if ( b == CodeCache::find_blob(a->get_i2c_entry()) ) return true;
duke@0 2544 }
duke@0 2545 return false;
duke@0 2546 }
duke@0 2547
duke@0 2548 void AdapterHandlerLibrary::print_handler(CodeBlob* b) {
never@1187 2549 AdapterHandlerTableIterator iter(_adapters);
never@1187 2550 while (iter.has_next()) {
never@1187 2551 AdapterHandlerEntry* a = iter.next();
never@1187 2552 if ( b == CodeCache::find_blob(a->get_i2c_entry()) ) {
duke@0 2553 tty->print("Adapter for signature: ");
never@1187 2554 tty->print_cr("%s i2c: " INTPTR_FORMAT " c2i: " INTPTR_FORMAT " c2iUV: " INTPTR_FORMAT,
never@1187 2555 a->fingerprint()->as_string(),
duke@0 2556 a->get_i2c_entry(), a->get_c2i_entry(), a->get_c2i_unverified_entry());
duke@0 2557 return;
duke@0 2558 }
duke@0 2559 }
duke@0 2560 assert(false, "Should have found handler");
duke@0 2561 }
never@1187 2562
never@1187 2563 void AdapterHandlerLibrary::print_statistics() {
never@1187 2564 _adapters->print_statistics();
never@1187 2565 }
never@1187 2566
duke@0 2567 #endif /* PRODUCT */