src/share/vm/oops/method.cpp

Tue, 17 Oct 2017 12:58:25 +0800

author
aoqi
date
Tue, 17 Oct 2017 12:58:25 +0800
changeset 7994
04ff2f6cd0eb
parent 7890
bf41eee321e5
parent 7535
7ae4e26cb1e0
child 8604
04d83ba48607
permissions
-rw-r--r--

merge

aoqi@0 1 /*
sspitsyn@7636 2 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "classfile/metadataOnStackMark.hpp"
aoqi@0 27 #include "classfile/systemDictionary.hpp"
aoqi@0 28 #include "code/debugInfoRec.hpp"
aoqi@0 29 #include "gc_interface/collectedHeap.inline.hpp"
aoqi@0 30 #include "interpreter/bytecodeStream.hpp"
aoqi@0 31 #include "interpreter/bytecodeTracer.hpp"
aoqi@0 32 #include "interpreter/bytecodes.hpp"
aoqi@0 33 #include "interpreter/interpreter.hpp"
aoqi@0 34 #include "interpreter/oopMapCache.hpp"
aoqi@0 35 #include "memory/gcLocker.hpp"
aoqi@0 36 #include "memory/generation.hpp"
aoqi@0 37 #include "memory/heapInspection.hpp"
aoqi@0 38 #include "memory/metadataFactory.hpp"
aoqi@0 39 #include "memory/oopFactory.hpp"
aoqi@0 40 #include "oops/constMethod.hpp"
aoqi@0 41 #include "oops/methodData.hpp"
aoqi@0 42 #include "oops/method.hpp"
aoqi@0 43 #include "oops/oop.inline.hpp"
aoqi@0 44 #include "oops/symbol.hpp"
aoqi@0 45 #include "prims/jvmtiExport.hpp"
aoqi@0 46 #include "prims/methodHandles.hpp"
aoqi@0 47 #include "prims/nativeLookup.hpp"
aoqi@0 48 #include "runtime/arguments.hpp"
aoqi@0 49 #include "runtime/compilationPolicy.hpp"
aoqi@0 50 #include "runtime/frame.inline.hpp"
aoqi@0 51 #include "runtime/handles.inline.hpp"
goetz@6911 52 #include "runtime/orderAccess.inline.hpp"
aoqi@0 53 #include "runtime/relocator.hpp"
aoqi@0 54 #include "runtime/sharedRuntime.hpp"
aoqi@0 55 #include "runtime/signature.hpp"
aoqi@0 56 #include "utilities/quickSort.hpp"
aoqi@0 57 #include "utilities/xmlstream.hpp"
aoqi@0 58
aoqi@0 59 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
aoqi@0 60
aoqi@0 61 // Implementation of Method
aoqi@0 62
aoqi@0 63 Method* Method::allocate(ClassLoaderData* loader_data,
aoqi@0 64 int byte_code_size,
aoqi@0 65 AccessFlags access_flags,
aoqi@0 66 InlineTableSizes* sizes,
aoqi@0 67 ConstMethod::MethodType method_type,
aoqi@0 68 TRAPS) {
aoqi@0 69 assert(!access_flags.is_native() || byte_code_size == 0,
aoqi@0 70 "native methods should not contain byte codes");
aoqi@0 71 ConstMethod* cm = ConstMethod::allocate(loader_data,
aoqi@0 72 byte_code_size,
aoqi@0 73 sizes,
aoqi@0 74 method_type,
aoqi@0 75 CHECK_NULL);
aoqi@0 76
aoqi@0 77 int size = Method::size(access_flags.is_native());
aoqi@0 78
aoqi@0 79 return new (loader_data, size, false, MetaspaceObj::MethodType, THREAD) Method(cm, access_flags, size);
aoqi@0 80 }
aoqi@0 81
aoqi@0 82 Method::Method(ConstMethod* xconst, AccessFlags access_flags, int size) {
aoqi@0 83 No_Safepoint_Verifier no_safepoint;
aoqi@0 84 set_constMethod(xconst);
aoqi@0 85 set_access_flags(access_flags);
aoqi@0 86 set_method_size(size);
aoqi@0 87 #ifdef CC_INTERP
aoqi@0 88 set_result_index(T_VOID);
aoqi@0 89 #endif
aoqi@0 90 set_intrinsic_id(vmIntrinsics::_none);
aoqi@0 91 set_jfr_towrite(false);
aoqi@0 92 set_force_inline(false);
aoqi@0 93 set_hidden(false);
aoqi@0 94 set_dont_inline(false);
vlivanov@7890 95 set_has_injected_profile(false);
aoqi@0 96 set_method_data(NULL);
iveresov@7203 97 clear_method_counters();
aoqi@0 98 set_vtable_index(Method::garbage_vtable_index);
aoqi@0 99
aoqi@0 100 // Fix and bury in Method*
aoqi@0 101 set_interpreter_entry(NULL); // sets i2i entry and from_int
aoqi@0 102 set_adapter_entry(NULL);
aoqi@0 103 clear_code(); // from_c/from_i get set to c2i/i2i
aoqi@0 104
aoqi@0 105 if (access_flags.is_native()) {
aoqi@0 106 clear_native_function();
aoqi@0 107 set_signature_handler(NULL);
aoqi@0 108 }
aoqi@26 109
aoqi@0 110 NOT_PRODUCT(set_compiled_invocation_count(0);)
aoqi@0 111 }
aoqi@0 112
aoqi@0 113 // Release Method*. The nmethod will be gone when we get here because
aoqi@0 114 // we've walked the code cache.
aoqi@0 115 void Method::deallocate_contents(ClassLoaderData* loader_data) {
aoqi@0 116 MetadataFactory::free_metadata(loader_data, constMethod());
aoqi@0 117 set_constMethod(NULL);
aoqi@0 118 MetadataFactory::free_metadata(loader_data, method_data());
aoqi@0 119 set_method_data(NULL);
aoqi@0 120 MetadataFactory::free_metadata(loader_data, method_counters());
iveresov@7203 121 clear_method_counters();
aoqi@0 122 // The nmethod will be gone when we get here.
aoqi@0 123 if (code() != NULL) _code = NULL;
aoqi@0 124 }
aoqi@0 125
aoqi@0 126 address Method::get_i2c_entry() {
aoqi@0 127 assert(_adapter != NULL, "must have");
aoqi@0 128 return _adapter->get_i2c_entry();
aoqi@0 129 }
aoqi@0 130
aoqi@0 131 address Method::get_c2i_entry() {
aoqi@0 132 assert(_adapter != NULL, "must have");
aoqi@0 133 return _adapter->get_c2i_entry();
aoqi@0 134 }
aoqi@0 135
aoqi@0 136 address Method::get_c2i_unverified_entry() {
aoqi@0 137 assert(_adapter != NULL, "must have");
aoqi@0 138 return _adapter->get_c2i_unverified_entry();
aoqi@0 139 }
aoqi@0 140
aoqi@0 141 char* Method::name_and_sig_as_C_string() const {
aoqi@0 142 return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature());
aoqi@0 143 }
aoqi@0 144
aoqi@0 145 char* Method::name_and_sig_as_C_string(char* buf, int size) const {
aoqi@0 146 return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature(), buf, size);
aoqi@0 147 }
aoqi@0 148
aoqi@0 149 char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature) {
aoqi@0 150 const char* klass_name = klass->external_name();
aoqi@0 151 int klass_name_len = (int)strlen(klass_name);
aoqi@0 152 int method_name_len = method_name->utf8_length();
aoqi@0 153 int len = klass_name_len + 1 + method_name_len + signature->utf8_length();
aoqi@0 154 char* dest = NEW_RESOURCE_ARRAY(char, len + 1);
aoqi@0 155 strcpy(dest, klass_name);
aoqi@0 156 dest[klass_name_len] = '.';
aoqi@0 157 strcpy(&dest[klass_name_len + 1], method_name->as_C_string());
aoqi@0 158 strcpy(&dest[klass_name_len + 1 + method_name_len], signature->as_C_string());
aoqi@0 159 dest[len] = 0;
aoqi@0 160 return dest;
aoqi@0 161 }
aoqi@0 162
aoqi@0 163 char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature, char* buf, int size) {
aoqi@0 164 Symbol* klass_name = klass->name();
aoqi@0 165 klass_name->as_klass_external_name(buf, size);
aoqi@0 166 int len = (int)strlen(buf);
aoqi@0 167
aoqi@0 168 if (len < size - 1) {
aoqi@0 169 buf[len++] = '.';
aoqi@0 170
aoqi@0 171 method_name->as_C_string(&(buf[len]), size - len);
aoqi@0 172 len = (int)strlen(buf);
aoqi@0 173
aoqi@0 174 signature->as_C_string(&(buf[len]), size - len);
aoqi@0 175 }
aoqi@0 176
aoqi@0 177 return buf;
aoqi@0 178 }
aoqi@0 179
aoqi@0 180 int Method::fast_exception_handler_bci_for(methodHandle mh, KlassHandle ex_klass, int throw_bci, TRAPS) {
aoqi@0 181 // exception table holds quadruple entries of the form (beg_bci, end_bci, handler_bci, klass_index)
aoqi@0 182 // access exception table
aoqi@0 183 ExceptionTable table(mh());
aoqi@0 184 int length = table.length();
aoqi@0 185 // iterate through all entries sequentially
aoqi@0 186 constantPoolHandle pool(THREAD, mh->constants());
aoqi@0 187 for (int i = 0; i < length; i ++) {
aoqi@0 188 //reacquire the table in case a GC happened
aoqi@0 189 ExceptionTable table(mh());
aoqi@0 190 int beg_bci = table.start_pc(i);
aoqi@0 191 int end_bci = table.end_pc(i);
aoqi@0 192 assert(beg_bci <= end_bci, "inconsistent exception table");
aoqi@0 193 if (beg_bci <= throw_bci && throw_bci < end_bci) {
aoqi@0 194 // exception handler bci range covers throw_bci => investigate further
aoqi@0 195 int handler_bci = table.handler_pc(i);
aoqi@0 196 int klass_index = table.catch_type_index(i);
aoqi@0 197 if (klass_index == 0) {
aoqi@0 198 return handler_bci;
aoqi@0 199 } else if (ex_klass.is_null()) {
aoqi@0 200 return handler_bci;
aoqi@0 201 } else {
aoqi@0 202 // we know the exception class => get the constraint class
aoqi@0 203 // this may require loading of the constraint class; if verification
aoqi@0 204 // fails or some other exception occurs, return handler_bci
aoqi@0 205 Klass* k = pool->klass_at(klass_index, CHECK_(handler_bci));
aoqi@0 206 KlassHandle klass = KlassHandle(THREAD, k);
aoqi@0 207 assert(klass.not_null(), "klass not loaded");
aoqi@0 208 if (ex_klass->is_subtype_of(klass())) {
aoqi@0 209 return handler_bci;
aoqi@0 210 }
aoqi@0 211 }
aoqi@0 212 }
aoqi@0 213 }
aoqi@0 214
aoqi@0 215 return -1;
aoqi@0 216 }
aoqi@0 217
aoqi@0 218 void Method::mask_for(int bci, InterpreterOopMap* mask) {
aoqi@0 219
aoqi@0 220 Thread* myThread = Thread::current();
aoqi@0 221 methodHandle h_this(myThread, this);
aoqi@0 222 #ifdef ASSERT
aoqi@0 223 bool has_capability = myThread->is_VM_thread() ||
aoqi@0 224 myThread->is_ConcurrentGC_thread() ||
aoqi@0 225 myThread->is_GC_task_thread();
aoqi@0 226
aoqi@0 227 if (!has_capability) {
aoqi@0 228 if (!VerifyStack && !VerifyLastFrame) {
aoqi@0 229 // verify stack calls this outside VM thread
aoqi@0 230 warning("oopmap should only be accessed by the "
aoqi@0 231 "VM, GC task or CMS threads (or during debugging)");
aoqi@0 232 InterpreterOopMap local_mask;
aoqi@0 233 method_holder()->mask_for(h_this, bci, &local_mask);
aoqi@0 234 local_mask.print();
aoqi@0 235 }
aoqi@0 236 }
aoqi@0 237 #endif
aoqi@0 238 method_holder()->mask_for(h_this, bci, mask);
aoqi@0 239 return;
aoqi@0 240 }
aoqi@0 241
aoqi@0 242
aoqi@0 243 int Method::bci_from(address bcp) const {
aoqi@0 244 #ifdef ASSERT
aoqi@0 245 { ResourceMark rm;
aoqi@0 246 assert(is_native() && bcp == code_base() || contains(bcp) || is_error_reported(),
aoqi@0 247 err_msg("bcp doesn't belong to this method: bcp: " INTPTR_FORMAT ", method: %s", bcp, name_and_sig_as_C_string()));
aoqi@0 248 }
aoqi@0 249 #endif
aoqi@0 250 return bcp - code_base();
aoqi@0 251 }
aoqi@0 252
aoqi@0 253
aoqi@0 254 // Return (int)bcx if it appears to be a valid BCI.
aoqi@0 255 // Return bci_from((address)bcx) if it appears to be a valid BCP.
aoqi@0 256 // Return -1 otherwise.
aoqi@0 257 // Used by profiling code, when invalid data is a possibility.
aoqi@0 258 // The caller is responsible for validating the Method* itself.
aoqi@0 259 int Method::validate_bci_from_bcx(intptr_t bcx) const {
aoqi@0 260 // keep bci as -1 if not a valid bci
aoqi@0 261 int bci = -1;
aoqi@0 262 if (bcx == 0 || (address)bcx == code_base()) {
aoqi@0 263 // code_size() may return 0 and we allow 0 here
aoqi@0 264 // the method may be native
aoqi@0 265 bci = 0;
aoqi@0 266 } else if (frame::is_bci(bcx)) {
aoqi@0 267 if (bcx < code_size()) {
aoqi@0 268 bci = (int)bcx;
aoqi@0 269 }
aoqi@0 270 } else if (contains((address)bcx)) {
aoqi@0 271 bci = (address)bcx - code_base();
aoqi@0 272 }
aoqi@0 273 // Assert that if we have dodged any asserts, bci is negative.
aoqi@0 274 assert(bci == -1 || bci == bci_from(bcp_from(bci)), "sane bci if >=0");
aoqi@0 275 return bci;
aoqi@0 276 }
aoqi@0 277
aoqi@0 278 address Method::bcp_from(int bci) const {
aoqi@0 279 assert((is_native() && bci == 0) || (!is_native() && 0 <= bci && bci < code_size()), err_msg("illegal bci: %d", bci));
aoqi@0 280 address bcp = code_base() + bci;
aoqi@0 281 assert(is_native() && bcp == code_base() || contains(bcp), "bcp doesn't belong to this method");
aoqi@0 282 return bcp;
aoqi@0 283 }
aoqi@0 284
aoqi@0 285
aoqi@0 286 int Method::size(bool is_native) {
aoqi@0 287 // If native, then include pointers for native_function and signature_handler
aoqi@0 288 int extra_bytes = (is_native) ? 2*sizeof(address*) : 0;
aoqi@0 289 int extra_words = align_size_up(extra_bytes, BytesPerWord) / BytesPerWord;
aoqi@0 290 return align_object_size(header_size() + extra_words);
aoqi@0 291 }
aoqi@0 292
aoqi@0 293
aoqi@0 294 Symbol* Method::klass_name() const {
aoqi@0 295 Klass* k = method_holder();
aoqi@0 296 assert(k->is_klass(), "must be klass");
aoqi@0 297 InstanceKlass* ik = (InstanceKlass*) k;
aoqi@0 298 return ik->name();
aoqi@0 299 }
aoqi@0 300
aoqi@0 301
aoqi@0 302 // Attempt to return method oop to original state. Clear any pointers
aoqi@0 303 // (to objects outside the shared spaces). We won't be able to predict
aoqi@0 304 // where they should point in a new JVM. Further initialize some
aoqi@0 305 // entries now in order allow them to be write protected later.
aoqi@0 306
aoqi@0 307 void Method::remove_unshareable_info() {
aoqi@0 308 unlink_method();
aoqi@0 309 }
aoqi@0 310
aoqi@0 311
aoqi@0 312 bool Method::was_executed_more_than(int n) {
aoqi@0 313 // Invocation counter is reset when the Method* is compiled.
aoqi@0 314 // If the method has compiled code we therefore assume it has
aoqi@0 315 // be excuted more than n times.
aoqi@0 316 if (is_accessor() || is_empty_method() || (code() != NULL)) {
aoqi@0 317 // interpreter doesn't bump invocation counter of trivial methods
aoqi@0 318 // compiler does not bump invocation counter of compiled methods
aoqi@0 319 return true;
aoqi@0 320 }
aoqi@0 321 else if ((method_counters() != NULL &&
aoqi@0 322 method_counters()->invocation_counter()->carry()) ||
aoqi@0 323 (method_data() != NULL &&
aoqi@0 324 method_data()->invocation_counter()->carry())) {
aoqi@0 325 // The carry bit is set when the counter overflows and causes
aoqi@0 326 // a compilation to occur. We don't know how many times
aoqi@0 327 // the counter has been reset, so we simply assume it has
aoqi@0 328 // been executed more than n times.
aoqi@0 329 return true;
aoqi@0 330 } else {
aoqi@0 331 return invocation_count() > n;
aoqi@0 332 }
aoqi@0 333 }
aoqi@0 334
aoqi@0 335 #ifndef PRODUCT
aoqi@0 336 void Method::print_invocation_count() {
aoqi@0 337 if (is_static()) tty->print("static ");
aoqi@0 338 if (is_final()) tty->print("final ");
aoqi@0 339 if (is_synchronized()) tty->print("synchronized ");
aoqi@0 340 if (is_native()) tty->print("native ");
aoqi@0 341 method_holder()->name()->print_symbol_on(tty);
aoqi@0 342 tty->print(".");
aoqi@0 343 name()->print_symbol_on(tty);
aoqi@0 344 signature()->print_symbol_on(tty);
aoqi@0 345
aoqi@0 346 if (WizardMode) {
aoqi@0 347 // dump the size of the byte codes
aoqi@0 348 tty->print(" {%d}", code_size());
aoqi@0 349 }
aoqi@0 350 tty->cr();
aoqi@0 351
aoqi@0 352 tty->print_cr (" interpreter_invocation_count: %8d ", interpreter_invocation_count());
aoqi@0 353 tty->print_cr (" invocation_counter: %8d ", invocation_count());
aoqi@0 354 tty->print_cr (" backedge_counter: %8d ", backedge_count());
aoqi@0 355 if (CountCompiledCalls) {
aoqi@0 356 tty->print_cr (" compiled_invocation_count: %8d ", compiled_invocation_count());
aoqi@0 357 }
aoqi@0 358
aoqi@0 359 }
aoqi@0 360 #endif
aoqi@0 361
aoqi@0 362 // Build a MethodData* object to hold information about this method
aoqi@0 363 // collected in the interpreter.
aoqi@0 364 void Method::build_interpreter_method_data(methodHandle method, TRAPS) {
aoqi@0 365 // Do not profile method if current thread holds the pending list lock,
aoqi@0 366 // which avoids deadlock for acquiring the MethodData_lock.
aoqi@0 367 if (InstanceRefKlass::owns_pending_list_lock((JavaThread*)THREAD)) {
aoqi@0 368 return;
aoqi@0 369 }
aoqi@0 370
aoqi@0 371 // Grab a lock here to prevent multiple
aoqi@0 372 // MethodData*s from being created.
aoqi@0 373 MutexLocker ml(MethodData_lock, THREAD);
aoqi@0 374 if (method->method_data() == NULL) {
aoqi@0 375 ClassLoaderData* loader_data = method->method_holder()->class_loader_data();
aoqi@0 376 MethodData* method_data = MethodData::allocate(loader_data, method, CHECK);
aoqi@0 377 method->set_method_data(method_data);
aoqi@0 378 if (PrintMethodData && (Verbose || WizardMode)) {
aoqi@0 379 ResourceMark rm(THREAD);
aoqi@0 380 tty->print("build_interpreter_method_data for ");
aoqi@0 381 method->print_name(tty);
aoqi@0 382 tty->cr();
aoqi@0 383 // At the end of the run, the MDO, full of data, will be dumped.
aoqi@0 384 }
aoqi@0 385 }
aoqi@0 386 }
aoqi@0 387
aoqi@0 388 MethodCounters* Method::build_method_counters(Method* m, TRAPS) {
aoqi@0 389 methodHandle mh(m);
aoqi@0 390 ClassLoaderData* loader_data = mh->method_holder()->class_loader_data();
aoqi@0 391 MethodCounters* counters = MethodCounters::allocate(loader_data, CHECK_NULL);
iveresov@7203 392 if (!mh->init_method_counters(counters)) {
aoqi@0 393 MetadataFactory::free_metadata(loader_data, counters);
aoqi@0 394 }
aoqi@0 395 return mh->method_counters();
aoqi@0 396 }
aoqi@0 397
aoqi@0 398 void Method::cleanup_inline_caches() {
aoqi@0 399 // The current system doesn't use inline caches in the interpreter
aoqi@0 400 // => nothing to do (keep this method around for future use)
aoqi@0 401 }
aoqi@0 402
aoqi@0 403
aoqi@0 404 int Method::extra_stack_words() {
aoqi@0 405 // not an inline function, to avoid a header dependency on Interpreter
aoqi@0 406 return extra_stack_entries() * Interpreter::stackElementSize;
aoqi@0 407 }
aoqi@0 408
aoqi@0 409
aoqi@0 410 void Method::compute_size_of_parameters(Thread *thread) {
aoqi@0 411 ArgumentSizeComputer asc(signature());
aoqi@0 412 set_size_of_parameters(asc.size() + (is_static() ? 0 : 1));
aoqi@0 413 }
aoqi@0 414
aoqi@0 415 #ifdef CC_INTERP
aoqi@0 416 void Method::set_result_index(BasicType type) {
aoqi@0 417 _result_index = Interpreter::BasicType_as_index(type);
aoqi@0 418 }
aoqi@0 419 #endif
aoqi@0 420
aoqi@0 421 BasicType Method::result_type() const {
aoqi@0 422 ResultTypeFinder rtf(signature());
aoqi@0 423 return rtf.type();
aoqi@0 424 }
aoqi@0 425
aoqi@0 426
aoqi@0 427 bool Method::is_empty_method() const {
aoqi@0 428 return code_size() == 1
aoqi@0 429 && *code_base() == Bytecodes::_return;
aoqi@0 430 }
aoqi@0 431
aoqi@0 432
aoqi@0 433 bool Method::is_vanilla_constructor() const {
aoqi@0 434 // Returns true if this method is a vanilla constructor, i.e. an "<init>" "()V" method
aoqi@0 435 // which only calls the superclass vanilla constructor and possibly does stores of
aoqi@0 436 // zero constants to local fields:
aoqi@0 437 //
aoqi@0 438 // aload_0
aoqi@0 439 // invokespecial
aoqi@0 440 // indexbyte1
aoqi@0 441 // indexbyte2
aoqi@0 442 //
aoqi@0 443 // followed by an (optional) sequence of:
aoqi@0 444 //
aoqi@0 445 // aload_0
aoqi@0 446 // aconst_null / iconst_0 / fconst_0 / dconst_0
aoqi@0 447 // putfield
aoqi@0 448 // indexbyte1
aoqi@0 449 // indexbyte2
aoqi@0 450 //
aoqi@0 451 // followed by:
aoqi@0 452 //
aoqi@0 453 // return
aoqi@0 454
aoqi@0 455 assert(name() == vmSymbols::object_initializer_name(), "Should only be called for default constructors");
aoqi@0 456 assert(signature() == vmSymbols::void_method_signature(), "Should only be called for default constructors");
aoqi@0 457 int size = code_size();
aoqi@0 458 // Check if size match
aoqi@0 459 if (size == 0 || size % 5 != 0) return false;
aoqi@0 460 address cb = code_base();
aoqi@0 461 int last = size - 1;
aoqi@0 462 if (cb[0] != Bytecodes::_aload_0 || cb[1] != Bytecodes::_invokespecial || cb[last] != Bytecodes::_return) {
aoqi@0 463 // Does not call superclass default constructor
aoqi@0 464 return false;
aoqi@0 465 }
aoqi@0 466 // Check optional sequence
aoqi@0 467 for (int i = 4; i < last; i += 5) {
aoqi@0 468 if (cb[i] != Bytecodes::_aload_0) return false;
aoqi@0 469 if (!Bytecodes::is_zero_const(Bytecodes::cast(cb[i+1]))) return false;
aoqi@0 470 if (cb[i+2] != Bytecodes::_putfield) return false;
aoqi@0 471 }
aoqi@0 472 return true;
aoqi@0 473 }
aoqi@0 474
aoqi@0 475
aoqi@0 476 bool Method::compute_has_loops_flag() {
aoqi@0 477 BytecodeStream bcs(this);
aoqi@0 478 Bytecodes::Code bc;
aoqi@0 479
aoqi@0 480 while ((bc = bcs.next()) >= 0) {
aoqi@0 481 switch( bc ) {
aoqi@0 482 case Bytecodes::_ifeq:
aoqi@0 483 case Bytecodes::_ifnull:
aoqi@0 484 case Bytecodes::_iflt:
aoqi@0 485 case Bytecodes::_ifle:
aoqi@0 486 case Bytecodes::_ifne:
aoqi@0 487 case Bytecodes::_ifnonnull:
aoqi@0 488 case Bytecodes::_ifgt:
aoqi@0 489 case Bytecodes::_ifge:
aoqi@0 490 case Bytecodes::_if_icmpeq:
aoqi@0 491 case Bytecodes::_if_icmpne:
aoqi@0 492 case Bytecodes::_if_icmplt:
aoqi@0 493 case Bytecodes::_if_icmpgt:
aoqi@0 494 case Bytecodes::_if_icmple:
aoqi@0 495 case Bytecodes::_if_icmpge:
aoqi@0 496 case Bytecodes::_if_acmpeq:
aoqi@0 497 case Bytecodes::_if_acmpne:
aoqi@0 498 case Bytecodes::_goto:
aoqi@0 499 case Bytecodes::_jsr:
aoqi@0 500 if( bcs.dest() < bcs.next_bci() ) _access_flags.set_has_loops();
aoqi@0 501 break;
aoqi@0 502
aoqi@0 503 case Bytecodes::_goto_w:
aoqi@0 504 case Bytecodes::_jsr_w:
aoqi@0 505 if( bcs.dest_w() < bcs.next_bci() ) _access_flags.set_has_loops();
aoqi@0 506 break;
aoqi@0 507 }
aoqi@0 508 }
aoqi@0 509 _access_flags.set_loops_flag_init();
aoqi@0 510 return _access_flags.has_loops();
aoqi@0 511 }
aoqi@0 512
aoqi@0 513 bool Method::is_final_method(AccessFlags class_access_flags) const {
aoqi@0 514 // or "does_not_require_vtable_entry"
aoqi@0 515 // default method or overpass can occur, is not final (reuses vtable entry)
aoqi@0 516 // private methods get vtable entries for backward class compatibility.
aoqi@0 517 if (is_overpass() || is_default_method()) return false;
aoqi@0 518 return is_final() || class_access_flags.is_final();
aoqi@0 519 }
aoqi@0 520
aoqi@0 521 bool Method::is_final_method() const {
aoqi@0 522 return is_final_method(method_holder()->access_flags());
aoqi@0 523 }
aoqi@0 524
aoqi@0 525 bool Method::is_default_method() const {
aoqi@0 526 if (method_holder() != NULL &&
aoqi@0 527 method_holder()->is_interface() &&
aoqi@0 528 !is_abstract()) {
aoqi@0 529 return true;
aoqi@0 530 } else {
aoqi@0 531 return false;
aoqi@0 532 }
aoqi@0 533 }
aoqi@0 534
aoqi@0 535 bool Method::can_be_statically_bound(AccessFlags class_access_flags) const {
aoqi@0 536 if (is_final_method(class_access_flags)) return true;
aoqi@0 537 #ifdef ASSERT
aoqi@0 538 ResourceMark rm;
aoqi@0 539 bool is_nonv = (vtable_index() == nonvirtual_vtable_index);
aoqi@0 540 if (class_access_flags.is_interface()) {
aoqi@0 541 assert(is_nonv == is_static(), err_msg("is_nonv=%s", name_and_sig_as_C_string()));
aoqi@0 542 }
aoqi@0 543 #endif
aoqi@0 544 assert(valid_vtable_index() || valid_itable_index(), "method must be linked before we ask this question");
aoqi@0 545 return vtable_index() == nonvirtual_vtable_index;
aoqi@0 546 }
aoqi@0 547
aoqi@0 548 bool Method::can_be_statically_bound() const {
aoqi@0 549 return can_be_statically_bound(method_holder()->access_flags());
aoqi@0 550 }
aoqi@0 551
aoqi@0 552 bool Method::is_accessor() const {
aoqi@0 553 if (code_size() != 5) return false;
aoqi@0 554 if (size_of_parameters() != 1) return false;
aoqi@0 555 if (java_code_at(0) != Bytecodes::_aload_0 ) return false;
aoqi@0 556 if (java_code_at(1) != Bytecodes::_getfield) return false;
aoqi@0 557 if (java_code_at(4) != Bytecodes::_areturn &&
aoqi@0 558 java_code_at(4) != Bytecodes::_ireturn ) return false;
aoqi@0 559 return true;
aoqi@0 560 }
aoqi@0 561
thartmann@7365 562 bool Method::is_constant_getter() const {
thartmann@7365 563 int last_index = code_size() - 1;
thartmann@7365 564 // Check if the first 1-3 bytecodes are a constant push
thartmann@7365 565 // and the last bytecode is a return.
thartmann@7365 566 return (2 <= code_size() && code_size() <= 4 &&
thartmann@7365 567 Bytecodes::is_const(java_code_at(0)) &&
thartmann@7365 568 Bytecodes::length_for(java_code_at(0)) == last_index &&
thartmann@7365 569 Bytecodes::is_return(java_code_at(last_index)));
thartmann@7365 570 }
aoqi@0 571
aoqi@0 572 bool Method::is_initializer() const {
aoqi@0 573 return name() == vmSymbols::object_initializer_name() || is_static_initializer();
aoqi@0 574 }
aoqi@0 575
aoqi@0 576 bool Method::has_valid_initializer_flags() const {
aoqi@0 577 return (is_static() ||
aoqi@0 578 method_holder()->major_version() < 51);
aoqi@0 579 }
aoqi@0 580
aoqi@0 581 bool Method::is_static_initializer() const {
aoqi@0 582 // For classfiles version 51 or greater, ensure that the clinit method is
aoqi@0 583 // static. Non-static methods with the name "<clinit>" are not static
aoqi@0 584 // initializers. (older classfiles exempted for backward compatibility)
aoqi@0 585 return name() == vmSymbols::class_initializer_name() &&
aoqi@0 586 has_valid_initializer_flags();
aoqi@0 587 }
aoqi@0 588
aoqi@0 589
aoqi@0 590 objArrayHandle Method::resolved_checked_exceptions_impl(Method* this_oop, TRAPS) {
aoqi@0 591 int length = this_oop->checked_exceptions_length();
aoqi@0 592 if (length == 0) { // common case
aoqi@0 593 return objArrayHandle(THREAD, Universe::the_empty_class_klass_array());
aoqi@0 594 } else {
aoqi@0 595 methodHandle h_this(THREAD, this_oop);
aoqi@0 596 objArrayOop m_oop = oopFactory::new_objArray(SystemDictionary::Class_klass(), length, CHECK_(objArrayHandle()));
aoqi@0 597 objArrayHandle mirrors (THREAD, m_oop);
aoqi@0 598 for (int i = 0; i < length; i++) {
aoqi@0 599 CheckedExceptionElement* table = h_this->checked_exceptions_start(); // recompute on each iteration, not gc safe
aoqi@0 600 Klass* k = h_this->constants()->klass_at(table[i].class_cp_index, CHECK_(objArrayHandle()));
aoqi@0 601 assert(k->is_subclass_of(SystemDictionary::Throwable_klass()), "invalid exception class");
aoqi@0 602 mirrors->obj_at_put(i, k->java_mirror());
aoqi@0 603 }
aoqi@0 604 return mirrors;
aoqi@0 605 }
aoqi@0 606 };
aoqi@0 607
aoqi@0 608
aoqi@0 609 int Method::line_number_from_bci(int bci) const {
aoqi@0 610 if (bci == SynchronizationEntryBCI) bci = 0;
aoqi@0 611 assert(bci == 0 || 0 <= bci && bci < code_size(), "illegal bci");
aoqi@0 612 int best_bci = 0;
aoqi@0 613 int best_line = -1;
aoqi@0 614
aoqi@0 615 if (has_linenumber_table()) {
aoqi@0 616 // The line numbers are a short array of 2-tuples [start_pc, line_number].
aoqi@0 617 // Not necessarily sorted and not necessarily one-to-one.
aoqi@0 618 CompressedLineNumberReadStream stream(compressed_linenumber_table());
aoqi@0 619 while (stream.read_pair()) {
aoqi@0 620 if (stream.bci() == bci) {
aoqi@0 621 // perfect match
aoqi@0 622 return stream.line();
aoqi@0 623 } else {
aoqi@0 624 // update best_bci/line
aoqi@0 625 if (stream.bci() < bci && stream.bci() >= best_bci) {
aoqi@0 626 best_bci = stream.bci();
aoqi@0 627 best_line = stream.line();
aoqi@0 628 }
aoqi@0 629 }
aoqi@0 630 }
aoqi@0 631 }
aoqi@0 632 return best_line;
aoqi@0 633 }
aoqi@0 634
aoqi@0 635
aoqi@0 636 bool Method::is_klass_loaded_by_klass_index(int klass_index) const {
aoqi@0 637 if( constants()->tag_at(klass_index).is_unresolved_klass() ) {
aoqi@0 638 Thread *thread = Thread::current();
aoqi@0 639 Symbol* klass_name = constants()->klass_name_at(klass_index);
aoqi@0 640 Handle loader(thread, method_holder()->class_loader());
aoqi@0 641 Handle prot (thread, method_holder()->protection_domain());
aoqi@0 642 return SystemDictionary::find(klass_name, loader, prot, thread) != NULL;
aoqi@0 643 } else {
aoqi@0 644 return true;
aoqi@0 645 }
aoqi@0 646 }
aoqi@0 647
aoqi@0 648
aoqi@0 649 bool Method::is_klass_loaded(int refinfo_index, bool must_be_resolved) const {
aoqi@0 650 int klass_index = constants()->klass_ref_index_at(refinfo_index);
aoqi@0 651 if (must_be_resolved) {
aoqi@0 652 // Make sure klass is resolved in constantpool.
aoqi@0 653 if (constants()->tag_at(klass_index).is_unresolved_klass()) return false;
aoqi@0 654 }
aoqi@0 655 return is_klass_loaded_by_klass_index(klass_index);
aoqi@0 656 }
aoqi@0 657
aoqi@0 658
aoqi@0 659 void Method::set_native_function(address function, bool post_event_flag) {
aoqi@0 660 assert(function != NULL, "use clear_native_function to unregister natives");
aoqi@0 661 assert(!is_method_handle_intrinsic() || function == SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), "");
aoqi@0 662 address* native_function = native_function_addr();
aoqi@0 663
aoqi@0 664 // We can see racers trying to place the same native function into place. Once
aoqi@0 665 // is plenty.
aoqi@0 666 address current = *native_function;
aoqi@0 667 if (current == function) return;
aoqi@0 668 if (post_event_flag && JvmtiExport::should_post_native_method_bind() &&
aoqi@0 669 function != NULL) {
aoqi@0 670 // native_method_throw_unsatisfied_link_error_entry() should only
aoqi@0 671 // be passed when post_event_flag is false.
aoqi@0 672 assert(function !=
aoqi@0 673 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
aoqi@0 674 "post_event_flag mis-match");
aoqi@0 675
aoqi@0 676 // post the bind event, and possible change the bind function
aoqi@0 677 JvmtiExport::post_native_method_bind(this, &function);
aoqi@0 678 }
aoqi@0 679 *native_function = function;
aoqi@0 680 // This function can be called more than once. We must make sure that we always
aoqi@0 681 // use the latest registered method -> check if a stub already has been generated.
aoqi@0 682 // If so, we have to make it not_entrant.
aoqi@0 683 nmethod* nm = code(); // Put it into local variable to guard against concurrent updates
aoqi@0 684 if (nm != NULL) {
aoqi@0 685 nm->make_not_entrant();
aoqi@0 686 }
aoqi@0 687 }
aoqi@0 688
aoqi@0 689
aoqi@0 690 bool Method::has_native_function() const {
aoqi@0 691 if (is_method_handle_intrinsic())
aoqi@0 692 return false; // special-cased in SharedRuntime::generate_native_wrapper
aoqi@0 693 address func = native_function();
aoqi@0 694 return (func != NULL && func != SharedRuntime::native_method_throw_unsatisfied_link_error_entry());
aoqi@0 695 }
aoqi@0 696
aoqi@0 697
aoqi@0 698 void Method::clear_native_function() {
aoqi@0 699 // Note: is_method_handle_intrinsic() is allowed here.
aoqi@0 700 set_native_function(
aoqi@0 701 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
aoqi@0 702 !native_bind_event_is_interesting);
aoqi@0 703 clear_code();
aoqi@0 704 }
aoqi@0 705
aoqi@0 706 address Method::critical_native_function() {
aoqi@0 707 methodHandle mh(this);
aoqi@0 708 return NativeLookup::lookup_critical_entry(mh);
aoqi@0 709 }
aoqi@0 710
aoqi@0 711
aoqi@0 712 void Method::set_signature_handler(address handler) {
aoqi@0 713 address* signature_handler = signature_handler_addr();
aoqi@0 714 *signature_handler = handler;
aoqi@0 715 }
aoqi@0 716
aoqi@0 717
aoqi@0 718 void Method::print_made_not_compilable(int comp_level, bool is_osr, bool report, const char* reason) {
aoqi@0 719 if (PrintCompilation && report) {
aoqi@0 720 ttyLocker ttyl;
aoqi@0 721 tty->print("made not %scompilable on ", is_osr ? "OSR " : "");
aoqi@0 722 if (comp_level == CompLevel_all) {
aoqi@0 723 tty->print("all levels ");
aoqi@0 724 } else {
aoqi@0 725 tty->print("levels ");
aoqi@0 726 for (int i = (int)CompLevel_none; i <= comp_level; i++) {
aoqi@0 727 tty->print("%d ", i);
aoqi@0 728 }
aoqi@0 729 }
aoqi@0 730 this->print_short_name(tty);
aoqi@0 731 int size = this->code_size();
aoqi@0 732 if (size > 0) {
aoqi@0 733 tty->print(" (%d bytes)", size);
aoqi@0 734 }
aoqi@0 735 if (reason != NULL) {
aoqi@0 736 tty->print(" %s", reason);
aoqi@0 737 }
aoqi@0 738 tty->cr();
aoqi@0 739 }
aoqi@0 740 if ((TraceDeoptimization || LogCompilation) && (xtty != NULL)) {
aoqi@0 741 ttyLocker ttyl;
vlivanov@7184 742 xtty->begin_elem("make_not_compilable thread='" UINTX_FORMAT "' osr='%d' level='%d'",
vlivanov@7184 743 os::current_thread_id(), is_osr, comp_level);
aoqi@0 744 if (reason != NULL) {
aoqi@0 745 xtty->print(" reason=\'%s\'", reason);
aoqi@0 746 }
aoqi@0 747 xtty->method(this);
aoqi@0 748 xtty->stamp();
aoqi@0 749 xtty->end_elem();
aoqi@0 750 }
aoqi@0 751 }
aoqi@0 752
aoqi@0 753 bool Method::is_always_compilable() const {
aoqi@0 754 // Generated adapters must be compiled
aoqi@0 755 if (is_method_handle_intrinsic() && is_synthetic()) {
aoqi@0 756 assert(!is_not_c1_compilable(), "sanity check");
aoqi@0 757 assert(!is_not_c2_compilable(), "sanity check");
aoqi@0 758 return true;
aoqi@0 759 }
aoqi@0 760
aoqi@0 761 return false;
aoqi@0 762 }
aoqi@0 763
aoqi@0 764 bool Method::is_not_compilable(int comp_level) const {
aoqi@0 765 if (number_of_breakpoints() > 0)
aoqi@0 766 return true;
aoqi@0 767 if (is_always_compilable())
aoqi@0 768 return false;
aoqi@0 769 if (comp_level == CompLevel_any)
aoqi@0 770 return is_not_c1_compilable() || is_not_c2_compilable();
aoqi@0 771 if (is_c1_compile(comp_level))
aoqi@0 772 return is_not_c1_compilable();
aoqi@0 773 if (is_c2_compile(comp_level))
aoqi@0 774 return is_not_c2_compilable();
aoqi@0 775 return false;
aoqi@0 776 }
aoqi@0 777
aoqi@0 778 // call this when compiler finds that this method is not compilable
aoqi@0 779 void Method::set_not_compilable(int comp_level, bool report, const char* reason) {
aoqi@0 780 if (is_always_compilable()) {
aoqi@0 781 // Don't mark a method which should be always compilable
aoqi@0 782 return;
aoqi@0 783 }
aoqi@0 784 print_made_not_compilable(comp_level, /*is_osr*/ false, report, reason);
aoqi@0 785 if (comp_level == CompLevel_all) {
aoqi@0 786 set_not_c1_compilable();
aoqi@0 787 set_not_c2_compilable();
aoqi@0 788 } else {
aoqi@0 789 if (is_c1_compile(comp_level))
aoqi@0 790 set_not_c1_compilable();
aoqi@0 791 if (is_c2_compile(comp_level))
aoqi@0 792 set_not_c2_compilable();
aoqi@0 793 }
aoqi@0 794 CompilationPolicy::policy()->disable_compilation(this);
aoqi@0 795 assert(!CompilationPolicy::can_be_compiled(this, comp_level), "sanity check");
aoqi@0 796 }
aoqi@0 797
aoqi@0 798 bool Method::is_not_osr_compilable(int comp_level) const {
aoqi@0 799 if (is_not_compilable(comp_level))
aoqi@0 800 return true;
aoqi@0 801 if (comp_level == CompLevel_any)
aoqi@0 802 return is_not_c1_osr_compilable() || is_not_c2_osr_compilable();
aoqi@0 803 if (is_c1_compile(comp_level))
aoqi@0 804 return is_not_c1_osr_compilable();
aoqi@0 805 if (is_c2_compile(comp_level))
aoqi@0 806 return is_not_c2_osr_compilable();
aoqi@0 807 return false;
aoqi@0 808 }
aoqi@0 809
aoqi@0 810 void Method::set_not_osr_compilable(int comp_level, bool report, const char* reason) {
aoqi@0 811 print_made_not_compilable(comp_level, /*is_osr*/ true, report, reason);
aoqi@0 812 if (comp_level == CompLevel_all) {
aoqi@0 813 set_not_c1_osr_compilable();
aoqi@0 814 set_not_c2_osr_compilable();
aoqi@0 815 } else {
aoqi@0 816 if (is_c1_compile(comp_level))
aoqi@0 817 set_not_c1_osr_compilable();
aoqi@0 818 if (is_c2_compile(comp_level))
aoqi@0 819 set_not_c2_osr_compilable();
aoqi@0 820 }
aoqi@0 821 CompilationPolicy::policy()->disable_compilation(this);
aoqi@0 822 assert(!CompilationPolicy::can_be_osr_compiled(this, comp_level), "sanity check");
aoqi@0 823 }
aoqi@0 824
aoqi@0 825 // Revert to using the interpreter and clear out the nmethod
aoqi@0 826 void Method::clear_code() {
aoqi@0 827
aoqi@0 828 // this may be NULL if c2i adapters have not been made yet
aoqi@0 829 // Only should happen at allocate time.
aoqi@0 830 if (_adapter == NULL) {
aoqi@0 831 _from_compiled_entry = NULL;
aoqi@0 832 } else {
aoqi@0 833 _from_compiled_entry = _adapter->get_c2i_entry();
aoqi@0 834 }
aoqi@0 835 OrderAccess::storestore();
aoqi@0 836 _from_interpreted_entry = _i2i_entry;
aoqi@0 837 OrderAccess::storestore();
aoqi@0 838 _code = NULL;
aoqi@0 839 }
aoqi@0 840
aoqi@0 841 // Called by class data sharing to remove any entry points (which are not shared)
aoqi@0 842 void Method::unlink_method() {
aoqi@0 843 _code = NULL;
aoqi@0 844 _i2i_entry = NULL;
aoqi@0 845 _from_interpreted_entry = NULL;
aoqi@0 846 if (is_native()) {
aoqi@0 847 *native_function_addr() = NULL;
aoqi@0 848 set_signature_handler(NULL);
aoqi@0 849 }
aoqi@0 850 NOT_PRODUCT(set_compiled_invocation_count(0);)
aoqi@0 851 _adapter = NULL;
aoqi@0 852 _from_compiled_entry = NULL;
aoqi@0 853
aoqi@0 854 // In case of DumpSharedSpaces, _method_data should always be NULL.
aoqi@0 855 //
aoqi@0 856 // During runtime (!DumpSharedSpaces), when we are cleaning a
aoqi@0 857 // shared class that failed to load, this->link_method() may
aoqi@0 858 // have already been called (before an exception happened), so
aoqi@0 859 // this->_method_data may not be NULL.
aoqi@0 860 assert(!DumpSharedSpaces || _method_data == NULL, "unexpected method data?");
aoqi@0 861
aoqi@0 862 set_method_data(NULL);
iveresov@7203 863 clear_method_counters();
aoqi@0 864 }
aoqi@0 865
aoqi@0 866 // Called when the method_holder is getting linked. Setup entrypoints so the method
aoqi@0 867 // is ready to be called from interpreter, compiler, and vtables.
aoqi@0 868 void Method::link_method(methodHandle h_method, TRAPS) {
aoqi@0 869 // If the code cache is full, we may reenter this function for the
aoqi@0 870 // leftover methods that weren't linked.
aoqi@0 871 if (_i2i_entry != NULL) return;
aoqi@0 872
aoqi@0 873 assert(_adapter == NULL, "init'd to NULL" );
aoqi@0 874 assert( _code == NULL, "nothing compiled yet" );
aoqi@0 875
aoqi@0 876 // Setup interpreter entrypoint
aoqi@0 877 assert(this == h_method(), "wrong h_method()" );
aoqi@0 878 address entry = Interpreter::entry_for_method(h_method);
aoqi@0 879 assert(entry != NULL, "interpreter entry must be non-null");
aoqi@0 880 // Sets both _i2i_entry and _from_interpreted_entry
aoqi@0 881 set_interpreter_entry(entry);
aoqi@0 882
aoqi@0 883 // Don't overwrite already registered native entries.
aoqi@0 884 if (is_native() && !has_native_function()) {
aoqi@0 885 set_native_function(
aoqi@0 886 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
aoqi@0 887 !native_bind_event_is_interesting);
aoqi@0 888 }
aoqi@0 889
aoqi@0 890 // Setup compiler entrypoint. This is made eagerly, so we do not need
aoqi@0 891 // special handling of vtables. An alternative is to make adapters more
aoqi@0 892 // lazily by calling make_adapter() from from_compiled_entry() for the
aoqi@0 893 // normal calls. For vtable calls life gets more complicated. When a
aoqi@0 894 // call-site goes mega-morphic we need adapters in all methods which can be
aoqi@0 895 // called from the vtable. We need adapters on such methods that get loaded
aoqi@0 896 // later. Ditto for mega-morphic itable calls. If this proves to be a
aoqi@0 897 // problem we'll make these lazily later.
aoqi@0 898 (void) make_adapters(h_method, CHECK);
aoqi@0 899
aoqi@0 900 // ONLY USE the h_method now as make_adapter may have blocked
aoqi@0 901
aoqi@0 902 }
aoqi@0 903
aoqi@0 904 address Method::make_adapters(methodHandle mh, TRAPS) {
aoqi@0 905 // Adapters for compiled code are made eagerly here. They are fairly
aoqi@0 906 // small (generally < 100 bytes) and quick to make (and cached and shared)
aoqi@0 907 // so making them eagerly shouldn't be too expensive.
aoqi@0 908 AdapterHandlerEntry* adapter = AdapterHandlerLibrary::get_adapter(mh);
aoqi@0 909 if (adapter == NULL ) {
aoqi@0 910 THROW_MSG_NULL(vmSymbols::java_lang_VirtualMachineError(), "out of space in CodeCache for adapters");
aoqi@0 911 }
aoqi@0 912
aoqi@0 913 mh->set_adapter_entry(adapter);
aoqi@0 914 mh->_from_compiled_entry = adapter->get_c2i_entry();
aoqi@0 915 return adapter->get_c2i_entry();
aoqi@0 916 }
aoqi@0 917
aoqi@0 918 void Method::restore_unshareable_info(TRAPS) {
aoqi@0 919 // Since restore_unshareable_info can be called more than once for a method, don't
aoqi@0 920 // redo any work. If this field is restored, there is nothing to do.
aoqi@0 921 if (_from_compiled_entry == NULL) {
aoqi@0 922 // restore method's vtable by calling a virtual function
aoqi@0 923 restore_vtable();
aoqi@0 924
aoqi@0 925 methodHandle mh(THREAD, this);
aoqi@0 926 link_method(mh, CHECK);
aoqi@0 927 }
aoqi@0 928 }
aoqi@0 929
aoqi@0 930
aoqi@0 931 // The verified_code_entry() must be called when a invoke is resolved
aoqi@0 932 // on this method.
aoqi@0 933
aoqi@0 934 // It returns the compiled code entry point, after asserting not null.
aoqi@0 935 // This function is called after potential safepoints so that nmethod
aoqi@0 936 // or adapter that it points to is still live and valid.
aoqi@0 937 // This function must not hit a safepoint!
aoqi@0 938 address Method::verified_code_entry() {
aoqi@0 939 debug_only(No_Safepoint_Verifier nsv;)
aoqi@0 940 assert(_from_compiled_entry != NULL, "must be set");
aoqi@0 941 return _from_compiled_entry;
aoqi@0 942 }
aoqi@0 943
aoqi@0 944 // Check that if an nmethod ref exists, it has a backlink to this or no backlink at all
aoqi@0 945 // (could be racing a deopt).
aoqi@0 946 // Not inline to avoid circular ref.
aoqi@0 947 bool Method::check_code() const {
aoqi@0 948 // cached in a register or local. There's a race on the value of the field.
aoqi@0 949 nmethod *code = (nmethod *)OrderAccess::load_ptr_acquire(&_code);
aoqi@0 950 return code == NULL || (code->method() == NULL) || (code->method() == (Method*)this && !code->is_osr_method());
aoqi@0 951 }
aoqi@0 952
aoqi@0 953 // Install compiled code. Instantly it can execute.
aoqi@0 954 void Method::set_code(methodHandle mh, nmethod *code) {
aoqi@0 955 assert( code, "use clear_code to remove code" );
aoqi@0 956 assert( mh->check_code(), "" );
aoqi@0 957
aoqi@0 958 guarantee(mh->adapter() != NULL, "Adapter blob must already exist!");
aoqi@0 959
aoqi@0 960 // These writes must happen in this order, because the interpreter will
aoqi@0 961 // directly jump to from_interpreted_entry which jumps to an i2c adapter
aoqi@0 962 // which jumps to _from_compiled_entry.
aoqi@0 963 mh->_code = code; // Assign before allowing compiled code to exec
aoqi@0 964
aoqi@0 965 int comp_level = code->comp_level();
aoqi@0 966 // In theory there could be a race here. In practice it is unlikely
aoqi@0 967 // and not worth worrying about.
aoqi@0 968 if (comp_level > mh->highest_comp_level()) {
aoqi@0 969 mh->set_highest_comp_level(comp_level);
aoqi@0 970 }
aoqi@0 971
aoqi@0 972 OrderAccess::storestore();
aoqi@0 973 #ifdef SHARK
aoqi@0 974 mh->_from_interpreted_entry = code->insts_begin();
aoqi@0 975 #else //!SHARK
aoqi@0 976 mh->_from_compiled_entry = code->verified_entry_point();
aoqi@0 977 OrderAccess::storestore();
aoqi@0 978 // Instantly compiled code can execute.
aoqi@0 979 if (!mh->is_method_handle_intrinsic())
aoqi@0 980 mh->_from_interpreted_entry = mh->get_i2c_entry();
aoqi@0 981 #endif //!SHARK
aoqi@0 982 }
aoqi@0 983
aoqi@0 984
aoqi@0 985 bool Method::is_overridden_in(Klass* k) const {
aoqi@0 986 InstanceKlass* ik = InstanceKlass::cast(k);
aoqi@0 987
aoqi@0 988 if (ik->is_interface()) return false;
aoqi@0 989
aoqi@0 990 // If method is an interface, we skip it - except if it
aoqi@0 991 // is a miranda method
aoqi@0 992 if (method_holder()->is_interface()) {
aoqi@0 993 // Check that method is not a miranda method
aoqi@0 994 if (ik->lookup_method(name(), signature()) == NULL) {
aoqi@0 995 // No implementation exist - so miranda method
aoqi@0 996 return false;
aoqi@0 997 }
aoqi@0 998 return true;
aoqi@0 999 }
aoqi@0 1000
aoqi@0 1001 assert(ik->is_subclass_of(method_holder()), "should be subklass");
aoqi@0 1002 assert(ik->vtable() != NULL, "vtable should exist");
aoqi@0 1003 if (!has_vtable_index()) {
aoqi@0 1004 return false;
aoqi@0 1005 } else {
aoqi@0 1006 Method* vt_m = ik->method_at_vtable(vtable_index());
aoqi@0 1007 return vt_m != this;
aoqi@0 1008 }
aoqi@0 1009 }
aoqi@0 1010
aoqi@0 1011
aoqi@0 1012 // give advice about whether this Method* should be cached or not
aoqi@0 1013 bool Method::should_not_be_cached() const {
aoqi@0 1014 if (is_old()) {
aoqi@0 1015 // This method has been redefined. It is either EMCP or obsolete
aoqi@0 1016 // and we don't want to cache it because that would pin the method
aoqi@0 1017 // down and prevent it from being collectible if and when it
aoqi@0 1018 // finishes executing.
aoqi@0 1019 return true;
aoqi@0 1020 }
aoqi@0 1021
aoqi@0 1022 // caching this method should be just fine
aoqi@0 1023 return false;
aoqi@0 1024 }
aoqi@0 1025
aoqi@0 1026
aoqi@0 1027 /**
aoqi@0 1028 * Returns true if this is one of the specially treated methods for
aoqi@0 1029 * security related stack walks (like Reflection.getCallerClass).
aoqi@0 1030 */
aoqi@0 1031 bool Method::is_ignored_by_security_stack_walk() const {
aoqi@0 1032 const bool use_new_reflection = JDK_Version::is_gte_jdk14x_version() && UseNewReflection;
aoqi@0 1033
aoqi@0 1034 if (intrinsic_id() == vmIntrinsics::_invoke) {
aoqi@0 1035 // This is Method.invoke() -- ignore it
aoqi@0 1036 return true;
aoqi@0 1037 }
aoqi@0 1038 if (use_new_reflection &&
aoqi@0 1039 method_holder()->is_subclass_of(SystemDictionary::reflect_MethodAccessorImpl_klass())) {
aoqi@0 1040 // This is an auxilary frame -- ignore it
aoqi@0 1041 return true;
aoqi@0 1042 }
aoqi@0 1043 if (is_method_handle_intrinsic() || is_compiled_lambda_form()) {
aoqi@0 1044 // This is an internal adapter frame for method handles -- ignore it
aoqi@0 1045 return true;
aoqi@0 1046 }
aoqi@0 1047 return false;
aoqi@0 1048 }
aoqi@0 1049
aoqi@0 1050
aoqi@0 1051 // Constant pool structure for invoke methods:
aoqi@0 1052 enum {
aoqi@0 1053 _imcp_invoke_name = 1, // utf8: 'invokeExact', etc.
aoqi@0 1054 _imcp_invoke_signature, // utf8: (variable Symbol*)
aoqi@0 1055 _imcp_limit
aoqi@0 1056 };
aoqi@0 1057
aoqi@0 1058 // Test if this method is an MH adapter frame generated by Java code.
aoqi@0 1059 // Cf. java/lang/invoke/InvokerBytecodeGenerator
aoqi@0 1060 bool Method::is_compiled_lambda_form() const {
aoqi@0 1061 return intrinsic_id() == vmIntrinsics::_compiledLambdaForm;
aoqi@0 1062 }
aoqi@0 1063
aoqi@0 1064 // Test if this method is an internal MH primitive method.
aoqi@0 1065 bool Method::is_method_handle_intrinsic() const {
aoqi@0 1066 vmIntrinsics::ID iid = intrinsic_id();
aoqi@0 1067 return (MethodHandles::is_signature_polymorphic(iid) &&
aoqi@0 1068 MethodHandles::is_signature_polymorphic_intrinsic(iid));
aoqi@0 1069 }
aoqi@0 1070
aoqi@0 1071 bool Method::has_member_arg() const {
aoqi@0 1072 vmIntrinsics::ID iid = intrinsic_id();
aoqi@0 1073 return (MethodHandles::is_signature_polymorphic(iid) &&
aoqi@0 1074 MethodHandles::has_member_arg(iid));
aoqi@0 1075 }
aoqi@0 1076
aoqi@0 1077 // Make an instance of a signature-polymorphic internal MH primitive.
aoqi@0 1078 methodHandle Method::make_method_handle_intrinsic(vmIntrinsics::ID iid,
aoqi@0 1079 Symbol* signature,
aoqi@0 1080 TRAPS) {
aoqi@0 1081 ResourceMark rm;
aoqi@0 1082 methodHandle empty;
aoqi@0 1083
aoqi@0 1084 KlassHandle holder = SystemDictionary::MethodHandle_klass();
aoqi@0 1085 Symbol* name = MethodHandles::signature_polymorphic_intrinsic_name(iid);
aoqi@0 1086 assert(iid == MethodHandles::signature_polymorphic_name_id(name), "");
aoqi@0 1087 if (TraceMethodHandles) {
aoqi@0 1088 tty->print_cr("make_method_handle_intrinsic MH.%s%s", name->as_C_string(), signature->as_C_string());
aoqi@0 1089 }
aoqi@0 1090
aoqi@0 1091 // invariant: cp->symbol_at_put is preceded by a refcount increment (more usually a lookup)
aoqi@0 1092 name->increment_refcount();
aoqi@0 1093 signature->increment_refcount();
aoqi@0 1094
aoqi@0 1095 int cp_length = _imcp_limit;
aoqi@0 1096 ClassLoaderData* loader_data = holder->class_loader_data();
aoqi@0 1097 constantPoolHandle cp;
aoqi@0 1098 {
aoqi@0 1099 ConstantPool* cp_oop = ConstantPool::allocate(loader_data, cp_length, CHECK_(empty));
aoqi@0 1100 cp = constantPoolHandle(THREAD, cp_oop);
aoqi@0 1101 }
aoqi@0 1102 cp->set_pool_holder(InstanceKlass::cast(holder()));
aoqi@0 1103 cp->symbol_at_put(_imcp_invoke_name, name);
aoqi@0 1104 cp->symbol_at_put(_imcp_invoke_signature, signature);
aoqi@0 1105 cp->set_has_preresolution();
aoqi@0 1106
aoqi@0 1107 // decide on access bits: public or not?
aoqi@0 1108 int flags_bits = (JVM_ACC_NATIVE | JVM_ACC_SYNTHETIC | JVM_ACC_FINAL);
aoqi@0 1109 bool must_be_static = MethodHandles::is_signature_polymorphic_static(iid);
aoqi@0 1110 if (must_be_static) flags_bits |= JVM_ACC_STATIC;
aoqi@0 1111 assert((flags_bits & JVM_ACC_PUBLIC) == 0, "do not expose these methods");
aoqi@0 1112
aoqi@0 1113 methodHandle m;
aoqi@0 1114 {
aoqi@0 1115 InlineTableSizes sizes;
aoqi@0 1116 Method* m_oop = Method::allocate(loader_data, 0,
aoqi@0 1117 accessFlags_from(flags_bits), &sizes,
aoqi@0 1118 ConstMethod::NORMAL, CHECK_(empty));
aoqi@0 1119 m = methodHandle(THREAD, m_oop);
aoqi@0 1120 }
aoqi@0 1121 m->set_constants(cp());
aoqi@0 1122 m->set_name_index(_imcp_invoke_name);
aoqi@0 1123 m->set_signature_index(_imcp_invoke_signature);
aoqi@0 1124 assert(MethodHandles::is_signature_polymorphic_name(m->name()), "");
aoqi@0 1125 assert(m->signature() == signature, "");
aoqi@0 1126 #ifdef CC_INTERP
aoqi@0 1127 ResultTypeFinder rtf(signature);
aoqi@0 1128 m->set_result_index(rtf.type());
aoqi@0 1129 #endif
aoqi@0 1130 m->compute_size_of_parameters(THREAD);
aoqi@0 1131 m->init_intrinsic_id();
aoqi@0 1132 assert(m->is_method_handle_intrinsic(), "");
aoqi@0 1133 #ifdef ASSERT
aoqi@0 1134 if (!MethodHandles::is_signature_polymorphic(m->intrinsic_id())) m->print();
aoqi@0 1135 assert(MethodHandles::is_signature_polymorphic(m->intrinsic_id()), "must be an invoker");
aoqi@0 1136 assert(m->intrinsic_id() == iid, "correctly predicted iid");
aoqi@0 1137 #endif //ASSERT
aoqi@0 1138
aoqi@0 1139 // Finally, set up its entry points.
aoqi@0 1140 assert(m->can_be_statically_bound(), "");
aoqi@0 1141 m->set_vtable_index(Method::nonvirtual_vtable_index);
aoqi@0 1142 m->link_method(m, CHECK_(empty));
aoqi@0 1143
aoqi@0 1144 if (TraceMethodHandles && (Verbose || WizardMode))
aoqi@0 1145 m->print_on(tty);
aoqi@0 1146
aoqi@0 1147 return m;
aoqi@0 1148 }
aoqi@0 1149
aoqi@0 1150 Klass* Method::check_non_bcp_klass(Klass* klass) {
aoqi@0 1151 if (klass != NULL && klass->class_loader() != NULL) {
aoqi@0 1152 if (klass->oop_is_objArray())
aoqi@0 1153 klass = ObjArrayKlass::cast(klass)->bottom_klass();
aoqi@0 1154 return klass;
aoqi@0 1155 }
aoqi@0 1156 return NULL;
aoqi@0 1157 }
aoqi@0 1158
aoqi@0 1159
aoqi@0 1160 methodHandle Method::clone_with_new_data(methodHandle m, u_char* new_code, int new_code_length,
aoqi@0 1161 u_char* new_compressed_linenumber_table, int new_compressed_linenumber_size, TRAPS) {
aoqi@0 1162 // Code below does not work for native methods - they should never get rewritten anyway
aoqi@0 1163 assert(!m->is_native(), "cannot rewrite native methods");
aoqi@0 1164 // Allocate new Method*
aoqi@0 1165 AccessFlags flags = m->access_flags();
aoqi@0 1166
aoqi@0 1167 ConstMethod* cm = m->constMethod();
aoqi@0 1168 int checked_exceptions_len = cm->checked_exceptions_length();
aoqi@0 1169 int localvariable_len = cm->localvariable_table_length();
aoqi@0 1170 int exception_table_len = cm->exception_table_length();
aoqi@0 1171 int method_parameters_len = cm->method_parameters_length();
aoqi@0 1172 int method_annotations_len = cm->method_annotations_length();
aoqi@0 1173 int parameter_annotations_len = cm->parameter_annotations_length();
aoqi@0 1174 int type_annotations_len = cm->type_annotations_length();
aoqi@0 1175 int default_annotations_len = cm->default_annotations_length();
aoqi@0 1176
aoqi@0 1177 InlineTableSizes sizes(
aoqi@0 1178 localvariable_len,
aoqi@0 1179 new_compressed_linenumber_size,
aoqi@0 1180 exception_table_len,
aoqi@0 1181 checked_exceptions_len,
aoqi@0 1182 method_parameters_len,
aoqi@0 1183 cm->generic_signature_index(),
aoqi@0 1184 method_annotations_len,
aoqi@0 1185 parameter_annotations_len,
aoqi@0 1186 type_annotations_len,
aoqi@0 1187 default_annotations_len,
aoqi@0 1188 0);
aoqi@0 1189
aoqi@0 1190 ClassLoaderData* loader_data = m->method_holder()->class_loader_data();
aoqi@0 1191 Method* newm_oop = Method::allocate(loader_data,
aoqi@0 1192 new_code_length,
aoqi@0 1193 flags,
aoqi@0 1194 &sizes,
aoqi@0 1195 m->method_type(),
aoqi@0 1196 CHECK_(methodHandle()));
aoqi@0 1197 methodHandle newm (THREAD, newm_oop);
aoqi@0 1198 int new_method_size = newm->method_size();
aoqi@0 1199
aoqi@0 1200 // Create a shallow copy of Method part, but be careful to preserve the new ConstMethod*
aoqi@0 1201 ConstMethod* newcm = newm->constMethod();
aoqi@0 1202 int new_const_method_size = newm->constMethod()->size();
aoqi@0 1203
aoqi@0 1204 memcpy(newm(), m(), sizeof(Method));
aoqi@0 1205
aoqi@0 1206 // Create shallow copy of ConstMethod.
aoqi@0 1207 memcpy(newcm, m->constMethod(), sizeof(ConstMethod));
aoqi@0 1208
aoqi@0 1209 // Reset correct method/const method, method size, and parameter info
aoqi@0 1210 newm->set_constMethod(newcm);
aoqi@0 1211 newm->constMethod()->set_code_size(new_code_length);
aoqi@0 1212 newm->constMethod()->set_constMethod_size(new_const_method_size);
aoqi@0 1213 newm->set_method_size(new_method_size);
aoqi@0 1214 assert(newm->code_size() == new_code_length, "check");
aoqi@0 1215 assert(newm->method_parameters_length() == method_parameters_len, "check");
aoqi@0 1216 assert(newm->checked_exceptions_length() == checked_exceptions_len, "check");
aoqi@0 1217 assert(newm->exception_table_length() == exception_table_len, "check");
aoqi@0 1218 assert(newm->localvariable_table_length() == localvariable_len, "check");
aoqi@0 1219 // Copy new byte codes
aoqi@0 1220 memcpy(newm->code_base(), new_code, new_code_length);
aoqi@0 1221 // Copy line number table
aoqi@0 1222 if (new_compressed_linenumber_size > 0) {
aoqi@0 1223 memcpy(newm->compressed_linenumber_table(),
aoqi@0 1224 new_compressed_linenumber_table,
aoqi@0 1225 new_compressed_linenumber_size);
aoqi@0 1226 }
aoqi@0 1227 // Copy method_parameters
aoqi@0 1228 if (method_parameters_len > 0) {
aoqi@0 1229 memcpy(newm->method_parameters_start(),
aoqi@0 1230 m->method_parameters_start(),
aoqi@0 1231 method_parameters_len * sizeof(MethodParametersElement));
aoqi@0 1232 }
aoqi@0 1233 // Copy checked_exceptions
aoqi@0 1234 if (checked_exceptions_len > 0) {
aoqi@0 1235 memcpy(newm->checked_exceptions_start(),
aoqi@0 1236 m->checked_exceptions_start(),
aoqi@0 1237 checked_exceptions_len * sizeof(CheckedExceptionElement));
aoqi@0 1238 }
aoqi@0 1239 // Copy exception table
aoqi@0 1240 if (exception_table_len > 0) {
aoqi@0 1241 memcpy(newm->exception_table_start(),
aoqi@0 1242 m->exception_table_start(),
aoqi@0 1243 exception_table_len * sizeof(ExceptionTableElement));
aoqi@0 1244 }
aoqi@0 1245 // Copy local variable number table
aoqi@0 1246 if (localvariable_len > 0) {
aoqi@0 1247 memcpy(newm->localvariable_table_start(),
aoqi@0 1248 m->localvariable_table_start(),
aoqi@0 1249 localvariable_len * sizeof(LocalVariableTableElement));
aoqi@0 1250 }
aoqi@0 1251 // Copy stackmap table
aoqi@0 1252 if (m->has_stackmap_table()) {
aoqi@0 1253 int code_attribute_length = m->stackmap_data()->length();
aoqi@0 1254 Array<u1>* stackmap_data =
aoqi@0 1255 MetadataFactory::new_array<u1>(loader_data, code_attribute_length, 0, CHECK_NULL);
aoqi@0 1256 memcpy((void*)stackmap_data->adr_at(0),
aoqi@0 1257 (void*)m->stackmap_data()->adr_at(0), code_attribute_length);
aoqi@0 1258 newm->set_stackmap_data(stackmap_data);
aoqi@0 1259 }
aoqi@0 1260
aoqi@0 1261 // copy annotations over to new method
aoqi@0 1262 newcm->copy_annotations_from(cm);
aoqi@0 1263 return newm;
aoqi@0 1264 }
aoqi@0 1265
aoqi@0 1266 vmSymbols::SID Method::klass_id_for_intrinsics(Klass* holder) {
aoqi@0 1267 // if loader is not the default loader (i.e., != NULL), we can't know the intrinsics
aoqi@0 1268 // because we are not loading from core libraries
aoqi@0 1269 // exception: the AES intrinsics come from lib/ext/sunjce_provider.jar
aoqi@0 1270 // which does not use the class default class loader so we check for its loader here
aoqi@0 1271 InstanceKlass* ik = InstanceKlass::cast(holder);
aoqi@0 1272 if ((ik->class_loader() != NULL) && !SystemDictionary::is_ext_class_loader(ik->class_loader())) {
aoqi@0 1273 return vmSymbols::NO_SID; // regardless of name, no intrinsics here
aoqi@0 1274 }
aoqi@0 1275
aoqi@0 1276 // see if the klass name is well-known:
aoqi@0 1277 Symbol* klass_name = ik->name();
aoqi@0 1278 return vmSymbols::find_sid(klass_name);
aoqi@0 1279 }
aoqi@0 1280
aoqi@0 1281 void Method::init_intrinsic_id() {
aoqi@0 1282 assert(_intrinsic_id == vmIntrinsics::_none, "do this just once");
aoqi@0 1283 const uintptr_t max_id_uint = right_n_bits((int)(sizeof(_intrinsic_id) * BitsPerByte));
aoqi@0 1284 assert((uintptr_t)vmIntrinsics::ID_LIMIT <= max_id_uint, "else fix size");
aoqi@0 1285 assert(intrinsic_id_size_in_bytes() == sizeof(_intrinsic_id), "");
aoqi@0 1286
aoqi@0 1287 // the klass name is well-known:
aoqi@0 1288 vmSymbols::SID klass_id = klass_id_for_intrinsics(method_holder());
aoqi@0 1289 assert(klass_id != vmSymbols::NO_SID, "caller responsibility");
aoqi@0 1290
aoqi@0 1291 // ditto for method and signature:
aoqi@0 1292 vmSymbols::SID name_id = vmSymbols::find_sid(name());
aoqi@0 1293 if (klass_id != vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_MethodHandle)
aoqi@0 1294 && name_id == vmSymbols::NO_SID)
aoqi@0 1295 return;
aoqi@0 1296 vmSymbols::SID sig_id = vmSymbols::find_sid(signature());
aoqi@0 1297 if (klass_id != vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_MethodHandle)
aoqi@0 1298 && sig_id == vmSymbols::NO_SID) return;
aoqi@0 1299 jshort flags = access_flags().as_short();
aoqi@0 1300
aoqi@0 1301 vmIntrinsics::ID id = vmIntrinsics::find_id(klass_id, name_id, sig_id, flags);
aoqi@0 1302 if (id != vmIntrinsics::_none) {
aoqi@0 1303 set_intrinsic_id(id);
aoqi@0 1304 return;
aoqi@0 1305 }
aoqi@0 1306
aoqi@0 1307 // A few slightly irregular cases:
aoqi@0 1308 switch (klass_id) {
aoqi@0 1309 case vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_StrictMath):
aoqi@0 1310 // Second chance: check in regular Math.
aoqi@0 1311 switch (name_id) {
aoqi@0 1312 case vmSymbols::VM_SYMBOL_ENUM_NAME(min_name):
aoqi@0 1313 case vmSymbols::VM_SYMBOL_ENUM_NAME(max_name):
aoqi@0 1314 case vmSymbols::VM_SYMBOL_ENUM_NAME(sqrt_name):
aoqi@0 1315 // pretend it is the corresponding method in the non-strict class:
aoqi@0 1316 klass_id = vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_Math);
aoqi@0 1317 id = vmIntrinsics::find_id(klass_id, name_id, sig_id, flags);
aoqi@0 1318 break;
aoqi@0 1319 }
aoqi@0 1320 break;
aoqi@0 1321
aoqi@0 1322 // Signature-polymorphic methods: MethodHandle.invoke*, InvokeDynamic.*.
aoqi@0 1323 case vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_MethodHandle):
aoqi@0 1324 if (!is_native()) break;
aoqi@0 1325 id = MethodHandles::signature_polymorphic_name_id(method_holder(), name());
aoqi@0 1326 if (is_static() != MethodHandles::is_signature_polymorphic_static(id))
aoqi@0 1327 id = vmIntrinsics::_none;
aoqi@0 1328 break;
aoqi@0 1329 }
aoqi@0 1330
aoqi@0 1331 if (id != vmIntrinsics::_none) {
aoqi@0 1332 // Set up its iid. It is an alias method.
aoqi@0 1333 set_intrinsic_id(id);
aoqi@0 1334 return;
aoqi@0 1335 }
aoqi@0 1336 }
aoqi@0 1337
aoqi@0 1338 // These two methods are static since a GC may move the Method
aoqi@0 1339 bool Method::load_signature_classes(methodHandle m, TRAPS) {
aoqi@0 1340 if (THREAD->is_Compiler_thread()) {
aoqi@0 1341 // There is nothing useful this routine can do from within the Compile thread.
aoqi@0 1342 // Hopefully, the signature contains only well-known classes.
aoqi@0 1343 // We could scan for this and return true/false, but the caller won't care.
aoqi@0 1344 return false;
aoqi@0 1345 }
aoqi@0 1346 bool sig_is_loaded = true;
aoqi@0 1347 Handle class_loader(THREAD, m->method_holder()->class_loader());
aoqi@0 1348 Handle protection_domain(THREAD, m->method_holder()->protection_domain());
aoqi@0 1349 ResourceMark rm(THREAD);
aoqi@0 1350 Symbol* signature = m->signature();
aoqi@0 1351 for(SignatureStream ss(signature); !ss.is_done(); ss.next()) {
aoqi@0 1352 if (ss.is_object()) {
aoqi@0 1353 Symbol* sym = ss.as_symbol(CHECK_(false));
aoqi@0 1354 Symbol* name = sym;
aoqi@0 1355 Klass* klass = SystemDictionary::resolve_or_null(name, class_loader,
aoqi@0 1356 protection_domain, THREAD);
aoqi@0 1357 // We are loading classes eagerly. If a ClassNotFoundException or
aoqi@0 1358 // a LinkageError was generated, be sure to ignore it.
aoqi@0 1359 if (HAS_PENDING_EXCEPTION) {
aoqi@0 1360 if (PENDING_EXCEPTION->is_a(SystemDictionary::ClassNotFoundException_klass()) ||
aoqi@0 1361 PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) {
aoqi@0 1362 CLEAR_PENDING_EXCEPTION;
aoqi@0 1363 } else {
aoqi@0 1364 return false;
aoqi@0 1365 }
aoqi@0 1366 }
aoqi@0 1367 if( klass == NULL) { sig_is_loaded = false; }
aoqi@0 1368 }
aoqi@0 1369 }
aoqi@0 1370 return sig_is_loaded;
aoqi@0 1371 }
aoqi@0 1372
aoqi@0 1373 bool Method::has_unloaded_classes_in_signature(methodHandle m, TRAPS) {
aoqi@0 1374 Handle class_loader(THREAD, m->method_holder()->class_loader());
aoqi@0 1375 Handle protection_domain(THREAD, m->method_holder()->protection_domain());
aoqi@0 1376 ResourceMark rm(THREAD);
aoqi@0 1377 Symbol* signature = m->signature();
aoqi@0 1378 for(SignatureStream ss(signature); !ss.is_done(); ss.next()) {
aoqi@0 1379 if (ss.type() == T_OBJECT) {
aoqi@0 1380 Symbol* name = ss.as_symbol_or_null();
aoqi@0 1381 if (name == NULL) return true;
aoqi@0 1382 Klass* klass = SystemDictionary::find(name, class_loader, protection_domain, THREAD);
aoqi@0 1383 if (klass == NULL) return true;
aoqi@0 1384 }
aoqi@0 1385 }
aoqi@0 1386 return false;
aoqi@0 1387 }
aoqi@0 1388
aoqi@0 1389 // Exposed so field engineers can debug VM
aoqi@0 1390 void Method::print_short_name(outputStream* st) {
aoqi@0 1391 ResourceMark rm;
aoqi@0 1392 #ifdef PRODUCT
aoqi@0 1393 st->print(" %s::", method_holder()->external_name());
aoqi@0 1394 #else
aoqi@0 1395 st->print(" %s::", method_holder()->internal_name());
aoqi@0 1396 #endif
aoqi@0 1397 name()->print_symbol_on(st);
aoqi@0 1398 if (WizardMode) signature()->print_symbol_on(st);
aoqi@0 1399 else if (MethodHandles::is_signature_polymorphic(intrinsic_id()))
aoqi@0 1400 MethodHandles::print_as_basic_type_signature_on(st, signature(), true);
aoqi@0 1401 }
aoqi@0 1402
aoqi@0 1403 // Comparer for sorting an object array containing
aoqi@0 1404 // Method*s.
aoqi@0 1405 static int method_comparator(Method* a, Method* b) {
aoqi@0 1406 return a->name()->fast_compare(b->name());
aoqi@0 1407 }
aoqi@0 1408
aoqi@0 1409 // This is only done during class loading, so it is OK to assume method_idnum matches the methods() array
aoqi@0 1410 // default_methods also uses this without the ordering for fast find_method
aoqi@0 1411 void Method::sort_methods(Array<Method*>* methods, bool idempotent, bool set_idnums) {
aoqi@0 1412 int length = methods->length();
aoqi@0 1413 if (length > 1) {
aoqi@0 1414 {
aoqi@0 1415 No_Safepoint_Verifier nsv;
aoqi@0 1416 QuickSort::sort<Method*>(methods->data(), length, method_comparator, idempotent);
aoqi@0 1417 }
aoqi@0 1418 // Reset method ordering
aoqi@0 1419 if (set_idnums) {
aoqi@0 1420 for (int i = 0; i < length; i++) {
aoqi@0 1421 Method* m = methods->at(i);
aoqi@0 1422 m->set_method_idnum(i);
sspitsyn@7636 1423 m->set_orig_method_idnum(i);
aoqi@0 1424 }
aoqi@0 1425 }
aoqi@0 1426 }
aoqi@0 1427 }
aoqi@0 1428
aoqi@0 1429 //-----------------------------------------------------------------------------------
aoqi@0 1430 // Non-product code unless JVM/TI needs it
aoqi@0 1431
aoqi@0 1432 #if !defined(PRODUCT) || INCLUDE_JVMTI
aoqi@0 1433 class SignatureTypePrinter : public SignatureTypeNames {
aoqi@0 1434 private:
aoqi@0 1435 outputStream* _st;
aoqi@0 1436 bool _use_separator;
aoqi@0 1437
aoqi@0 1438 void type_name(const char* name) {
aoqi@0 1439 if (_use_separator) _st->print(", ");
aoqi@0 1440 _st->print("%s", name);
aoqi@0 1441 _use_separator = true;
aoqi@0 1442 }
aoqi@0 1443
aoqi@0 1444 public:
aoqi@0 1445 SignatureTypePrinter(Symbol* signature, outputStream* st) : SignatureTypeNames(signature) {
aoqi@0 1446 _st = st;
aoqi@0 1447 _use_separator = false;
aoqi@0 1448 }
aoqi@0 1449
aoqi@0 1450 void print_parameters() { _use_separator = false; iterate_parameters(); }
aoqi@0 1451 void print_returntype() { _use_separator = false; iterate_returntype(); }
aoqi@0 1452 };
aoqi@0 1453
aoqi@0 1454
aoqi@0 1455 void Method::print_name(outputStream* st) {
aoqi@0 1456 Thread *thread = Thread::current();
aoqi@0 1457 ResourceMark rm(thread);
aoqi@0 1458 SignatureTypePrinter sig(signature(), st);
aoqi@0 1459 st->print("%s ", is_static() ? "static" : "virtual");
aoqi@0 1460 sig.print_returntype();
aoqi@0 1461 st->print(" %s.", method_holder()->internal_name());
aoqi@0 1462 name()->print_symbol_on(st);
aoqi@0 1463 st->print("(");
aoqi@0 1464 sig.print_parameters();
aoqi@0 1465 st->print(")");
aoqi@0 1466 }
aoqi@0 1467 #endif // !PRODUCT || INCLUDE_JVMTI
aoqi@0 1468
aoqi@0 1469
aoqi@0 1470 //-----------------------------------------------------------------------------------
aoqi@0 1471 // Non-product code
aoqi@0 1472
aoqi@0 1473 #ifndef PRODUCT
aoqi@0 1474 void Method::print_codes_on(outputStream* st) const {
aoqi@0 1475 print_codes_on(0, code_size(), st);
aoqi@0 1476 }
aoqi@0 1477
aoqi@0 1478 void Method::print_codes_on(int from, int to, outputStream* st) const {
aoqi@0 1479 Thread *thread = Thread::current();
aoqi@0 1480 ResourceMark rm(thread);
aoqi@0 1481 methodHandle mh (thread, (Method*)this);
aoqi@0 1482 BytecodeStream s(mh);
aoqi@0 1483 s.set_interval(from, to);
aoqi@0 1484 BytecodeTracer::set_closure(BytecodeTracer::std_closure());
aoqi@0 1485 while (s.next() >= 0) BytecodeTracer::trace(mh, s.bcp(), st);
aoqi@0 1486 }
aoqi@0 1487 #endif // not PRODUCT
aoqi@0 1488
aoqi@0 1489
aoqi@0 1490 // Simple compression of line number tables. We use a regular compressed stream, except that we compress deltas
aoqi@0 1491 // between (bci,line) pairs since they are smaller. If (bci delta, line delta) fits in (5-bit unsigned, 3-bit unsigned)
aoqi@0 1492 // we save it as one byte, otherwise we write a 0xFF escape character and use regular compression. 0x0 is used
aoqi@0 1493 // as end-of-stream terminator.
aoqi@0 1494
aoqi@0 1495 void CompressedLineNumberWriteStream::write_pair_regular(int bci_delta, int line_delta) {
aoqi@0 1496 // bci and line number does not compress into single byte.
aoqi@0 1497 // Write out escape character and use regular compression for bci and line number.
aoqi@0 1498 write_byte((jubyte)0xFF);
aoqi@0 1499 write_signed_int(bci_delta);
aoqi@0 1500 write_signed_int(line_delta);
aoqi@0 1501 }
aoqi@0 1502
aoqi@0 1503 // See comment in method.hpp which explains why this exists.
aoqi@0 1504 #if defined(_M_AMD64) && _MSC_VER >= 1400
aoqi@0 1505 #pragma optimize("", off)
aoqi@0 1506 void CompressedLineNumberWriteStream::write_pair(int bci, int line) {
aoqi@0 1507 write_pair_inline(bci, line);
aoqi@0 1508 }
aoqi@0 1509 #pragma optimize("", on)
aoqi@0 1510 #endif
aoqi@0 1511
aoqi@0 1512 CompressedLineNumberReadStream::CompressedLineNumberReadStream(u_char* buffer) : CompressedReadStream(buffer) {
aoqi@0 1513 _bci = 0;
aoqi@0 1514 _line = 0;
aoqi@0 1515 };
aoqi@0 1516
aoqi@0 1517
aoqi@0 1518 bool CompressedLineNumberReadStream::read_pair() {
aoqi@0 1519 jubyte next = read_byte();
aoqi@0 1520 // Check for terminator
aoqi@0 1521 if (next == 0) return false;
aoqi@0 1522 if (next == 0xFF) {
aoqi@0 1523 // Escape character, regular compression used
aoqi@0 1524 _bci += read_signed_int();
aoqi@0 1525 _line += read_signed_int();
aoqi@0 1526 } else {
aoqi@0 1527 // Single byte compression used
aoqi@0 1528 _bci += next >> 3;
aoqi@0 1529 _line += next & 0x7;
aoqi@0 1530 }
aoqi@0 1531 return true;
aoqi@0 1532 }
aoqi@0 1533
aoqi@0 1534
aoqi@0 1535 Bytecodes::Code Method::orig_bytecode_at(int bci) const {
aoqi@0 1536 BreakpointInfo* bp = method_holder()->breakpoints();
aoqi@0 1537 for (; bp != NULL; bp = bp->next()) {
aoqi@0 1538 if (bp->match(this, bci)) {
aoqi@0 1539 return bp->orig_bytecode();
aoqi@0 1540 }
aoqi@0 1541 }
aoqi@0 1542 {
aoqi@0 1543 ResourceMark rm;
aoqi@0 1544 fatal(err_msg("no original bytecode found in %s at bci %d", name_and_sig_as_C_string(), bci));
aoqi@0 1545 }
aoqi@0 1546 return Bytecodes::_shouldnotreachhere;
aoqi@0 1547 }
aoqi@0 1548
aoqi@0 1549 void Method::set_orig_bytecode_at(int bci, Bytecodes::Code code) {
aoqi@0 1550 assert(code != Bytecodes::_breakpoint, "cannot patch breakpoints this way");
aoqi@0 1551 BreakpointInfo* bp = method_holder()->breakpoints();
aoqi@0 1552 for (; bp != NULL; bp = bp->next()) {
aoqi@0 1553 if (bp->match(this, bci)) {
aoqi@0 1554 bp->set_orig_bytecode(code);
aoqi@0 1555 // and continue, in case there is more than one
aoqi@0 1556 }
aoqi@0 1557 }
aoqi@0 1558 }
aoqi@0 1559
aoqi@0 1560 void Method::set_breakpoint(int bci) {
aoqi@0 1561 InstanceKlass* ik = method_holder();
aoqi@0 1562 BreakpointInfo *bp = new BreakpointInfo(this, bci);
aoqi@0 1563 bp->set_next(ik->breakpoints());
aoqi@0 1564 ik->set_breakpoints(bp);
aoqi@0 1565 // do this last:
aoqi@0 1566 bp->set(this);
aoqi@0 1567 }
aoqi@0 1568
aoqi@0 1569 static void clear_matches(Method* m, int bci) {
aoqi@0 1570 InstanceKlass* ik = m->method_holder();
aoqi@0 1571 BreakpointInfo* prev_bp = NULL;
aoqi@0 1572 BreakpointInfo* next_bp;
aoqi@0 1573 for (BreakpointInfo* bp = ik->breakpoints(); bp != NULL; bp = next_bp) {
aoqi@0 1574 next_bp = bp->next();
aoqi@0 1575 // bci value of -1 is used to delete all breakpoints in method m (ex: clear_all_breakpoint).
aoqi@0 1576 if (bci >= 0 ? bp->match(m, bci) : bp->match(m)) {
aoqi@0 1577 // do this first:
aoqi@0 1578 bp->clear(m);
aoqi@0 1579 // unhook it
aoqi@0 1580 if (prev_bp != NULL)
aoqi@0 1581 prev_bp->set_next(next_bp);
aoqi@0 1582 else
aoqi@0 1583 ik->set_breakpoints(next_bp);
aoqi@0 1584 delete bp;
aoqi@0 1585 // When class is redefined JVMTI sets breakpoint in all versions of EMCP methods
aoqi@0 1586 // at same location. So we have multiple matching (method_index and bci)
aoqi@0 1587 // BreakpointInfo nodes in BreakpointInfo list. We should just delete one
aoqi@0 1588 // breakpoint for clear_breakpoint request and keep all other method versions
aoqi@0 1589 // BreakpointInfo for future clear_breakpoint request.
aoqi@0 1590 // bcivalue of -1 is used to clear all breakpoints (see clear_all_breakpoints)
aoqi@0 1591 // which is being called when class is unloaded. We delete all the Breakpoint
aoqi@0 1592 // information for all versions of method. We may not correctly restore the original
aoqi@0 1593 // bytecode in all method versions, but that is ok. Because the class is being unloaded
aoqi@0 1594 // so these methods won't be used anymore.
aoqi@0 1595 if (bci >= 0) {
aoqi@0 1596 break;
aoqi@0 1597 }
aoqi@0 1598 } else {
aoqi@0 1599 // This one is a keeper.
aoqi@0 1600 prev_bp = bp;
aoqi@0 1601 }
aoqi@0 1602 }
aoqi@0 1603 }
aoqi@0 1604
aoqi@0 1605 void Method::clear_breakpoint(int bci) {
aoqi@0 1606 assert(bci >= 0, "");
aoqi@0 1607 clear_matches(this, bci);
aoqi@0 1608 }
aoqi@0 1609
aoqi@0 1610 void Method::clear_all_breakpoints() {
aoqi@0 1611 clear_matches(this, -1);
aoqi@0 1612 }
aoqi@0 1613
aoqi@0 1614
aoqi@0 1615 int Method::invocation_count() {
aoqi@0 1616 MethodCounters *mcs = method_counters();
aoqi@0 1617 if (TieredCompilation) {
aoqi@0 1618 MethodData* const mdo = method_data();
aoqi@0 1619 if (((mcs != NULL) ? mcs->invocation_counter()->carry() : false) ||
aoqi@0 1620 ((mdo != NULL) ? mdo->invocation_counter()->carry() : false)) {
aoqi@0 1621 return InvocationCounter::count_limit;
aoqi@0 1622 } else {
aoqi@0 1623 return ((mcs != NULL) ? mcs->invocation_counter()->count() : 0) +
aoqi@0 1624 ((mdo != NULL) ? mdo->invocation_counter()->count() : 0);
aoqi@0 1625 }
aoqi@0 1626 } else {
aoqi@0 1627 return (mcs == NULL) ? 0 : mcs->invocation_counter()->count();
aoqi@0 1628 }
aoqi@0 1629 }
aoqi@0 1630
aoqi@0 1631 int Method::backedge_count() {
aoqi@0 1632 MethodCounters *mcs = method_counters();
aoqi@0 1633 if (TieredCompilation) {
aoqi@0 1634 MethodData* const mdo = method_data();
aoqi@0 1635 if (((mcs != NULL) ? mcs->backedge_counter()->carry() : false) ||
aoqi@0 1636 ((mdo != NULL) ? mdo->backedge_counter()->carry() : false)) {
aoqi@0 1637 return InvocationCounter::count_limit;
aoqi@0 1638 } else {
aoqi@0 1639 return ((mcs != NULL) ? mcs->backedge_counter()->count() : 0) +
aoqi@0 1640 ((mdo != NULL) ? mdo->backedge_counter()->count() : 0);
aoqi@0 1641 }
aoqi@0 1642 } else {
aoqi@0 1643 return (mcs == NULL) ? 0 : mcs->backedge_counter()->count();
aoqi@0 1644 }
aoqi@0 1645 }
aoqi@0 1646
aoqi@0 1647 int Method::highest_comp_level() const {
iveresov@7171 1648 const MethodCounters* mcs = method_counters();
iveresov@7171 1649 if (mcs != NULL) {
iveresov@7171 1650 return mcs->highest_comp_level();
aoqi@0 1651 } else {
aoqi@0 1652 return CompLevel_none;
aoqi@0 1653 }
aoqi@0 1654 }
aoqi@0 1655
aoqi@0 1656 int Method::highest_osr_comp_level() const {
iveresov@7171 1657 const MethodCounters* mcs = method_counters();
iveresov@7171 1658 if (mcs != NULL) {
iveresov@7171 1659 return mcs->highest_osr_comp_level();
aoqi@0 1660 } else {
aoqi@0 1661 return CompLevel_none;
aoqi@0 1662 }
aoqi@0 1663 }
aoqi@0 1664
aoqi@0 1665 void Method::set_highest_comp_level(int level) {
iveresov@7171 1666 MethodCounters* mcs = method_counters();
iveresov@7171 1667 if (mcs != NULL) {
iveresov@7171 1668 mcs->set_highest_comp_level(level);
aoqi@0 1669 }
aoqi@0 1670 }
aoqi@0 1671
aoqi@0 1672 void Method::set_highest_osr_comp_level(int level) {
iveresov@7171 1673 MethodCounters* mcs = method_counters();
iveresov@7171 1674 if (mcs != NULL) {
iveresov@7171 1675 mcs->set_highest_osr_comp_level(level);
aoqi@0 1676 }
aoqi@0 1677 }
aoqi@0 1678
aoqi@0 1679 BreakpointInfo::BreakpointInfo(Method* m, int bci) {
aoqi@0 1680 _bci = bci;
aoqi@0 1681 _name_index = m->name_index();
aoqi@0 1682 _signature_index = m->signature_index();
aoqi@0 1683 _orig_bytecode = (Bytecodes::Code) *m->bcp_from(_bci);
aoqi@0 1684 if (_orig_bytecode == Bytecodes::_breakpoint)
aoqi@0 1685 _orig_bytecode = m->orig_bytecode_at(_bci);
aoqi@0 1686 _next = NULL;
aoqi@0 1687 }
aoqi@0 1688
aoqi@0 1689 void BreakpointInfo::set(Method* method) {
aoqi@0 1690 #ifdef ASSERT
aoqi@0 1691 {
aoqi@0 1692 Bytecodes::Code code = (Bytecodes::Code) *method->bcp_from(_bci);
aoqi@0 1693 if (code == Bytecodes::_breakpoint)
aoqi@0 1694 code = method->orig_bytecode_at(_bci);
aoqi@0 1695 assert(orig_bytecode() == code, "original bytecode must be the same");
aoqi@0 1696 }
aoqi@0 1697 #endif
aoqi@0 1698 Thread *thread = Thread::current();
aoqi@0 1699 *method->bcp_from(_bci) = Bytecodes::_breakpoint;
aoqi@0 1700 method->incr_number_of_breakpoints(thread);
aoqi@0 1701 SystemDictionary::notice_modification();
aoqi@0 1702 {
aoqi@0 1703 // Deoptimize all dependents on this method
aoqi@0 1704 HandleMark hm(thread);
aoqi@0 1705 methodHandle mh(thread, method);
aoqi@0 1706 Universe::flush_dependents_on_method(mh);
aoqi@0 1707 }
aoqi@0 1708 }
aoqi@0 1709
aoqi@0 1710 void BreakpointInfo::clear(Method* method) {
aoqi@0 1711 *method->bcp_from(_bci) = orig_bytecode();
aoqi@0 1712 assert(method->number_of_breakpoints() > 0, "must not go negative");
aoqi@0 1713 method->decr_number_of_breakpoints(Thread::current());
aoqi@0 1714 }
aoqi@0 1715
aoqi@0 1716 // jmethodID handling
aoqi@0 1717
aoqi@0 1718 // This is a block allocating object, sort of like JNIHandleBlock, only a
aoqi@0 1719 // lot simpler. There aren't many of these, they aren't long, they are rarely
aoqi@0 1720 // deleted and so we can do some suboptimal things.
aoqi@0 1721 // It's allocated on the CHeap because once we allocate a jmethodID, we can
aoqi@0 1722 // never get rid of it.
aoqi@0 1723 // It would be nice to be able to parameterize the number of methods for
aoqi@0 1724 // the null_class_loader but then we'd have to turn this and ClassLoaderData
aoqi@0 1725 // into templates.
aoqi@0 1726
aoqi@0 1727 // I feel like this brain dead class should exist somewhere in the STL
aoqi@0 1728
aoqi@0 1729 class JNIMethodBlock : public CHeapObj<mtClass> {
aoqi@0 1730 enum { number_of_methods = 8 };
aoqi@0 1731
aoqi@0 1732 Method* _methods[number_of_methods];
aoqi@0 1733 int _top;
aoqi@0 1734 JNIMethodBlock* _next;
aoqi@0 1735 public:
aoqi@0 1736 static Method* const _free_method;
aoqi@0 1737
aoqi@0 1738 JNIMethodBlock() : _next(NULL), _top(0) {
aoqi@0 1739 for (int i = 0; i< number_of_methods; i++) _methods[i] = _free_method;
aoqi@0 1740 }
aoqi@0 1741
aoqi@0 1742 Method** add_method(Method* m) {
aoqi@0 1743 if (_top < number_of_methods) {
aoqi@0 1744 // top points to the next free entry.
aoqi@0 1745 int i = _top;
aoqi@0 1746 _methods[i] = m;
aoqi@0 1747 _top++;
aoqi@0 1748 return &_methods[i];
aoqi@0 1749 } else if (_top == number_of_methods) {
aoqi@0 1750 // if the next free entry ran off the block see if there's a free entry
aoqi@0 1751 for (int i = 0; i< number_of_methods; i++) {
aoqi@0 1752 if (_methods[i] == _free_method) {
aoqi@0 1753 _methods[i] = m;
aoqi@0 1754 return &_methods[i];
aoqi@0 1755 }
aoqi@0 1756 }
aoqi@0 1757 // Only check each block once for frees. They're very unlikely.
aoqi@0 1758 // Increment top past the end of the block.
aoqi@0 1759 _top++;
aoqi@0 1760 }
aoqi@0 1761 // need to allocate a next block.
aoqi@0 1762 if (_next == NULL) {
aoqi@0 1763 _next = new JNIMethodBlock();
aoqi@0 1764 }
aoqi@0 1765 return _next->add_method(m);
aoqi@0 1766 }
aoqi@0 1767
aoqi@0 1768 bool contains(Method** m) {
aoqi@0 1769 for (JNIMethodBlock* b = this; b != NULL; b = b->_next) {
aoqi@0 1770 for (int i = 0; i< number_of_methods; i++) {
aoqi@0 1771 if (&(b->_methods[i]) == m) {
aoqi@0 1772 return true;
aoqi@0 1773 }
aoqi@0 1774 }
aoqi@0 1775 }
aoqi@0 1776 return false; // not found
aoqi@0 1777 }
aoqi@0 1778
aoqi@0 1779 // Doesn't really destroy it, just marks it as free so it can be reused.
aoqi@0 1780 void destroy_method(Method** m) {
aoqi@0 1781 #ifdef ASSERT
aoqi@0 1782 assert(contains(m), "should be a methodID");
aoqi@0 1783 #endif // ASSERT
aoqi@0 1784 *m = _free_method;
aoqi@0 1785 }
aoqi@0 1786
aoqi@0 1787 // During class unloading the methods are cleared, which is different
aoqi@0 1788 // than freed.
aoqi@0 1789 void clear_all_methods() {
aoqi@0 1790 for (JNIMethodBlock* b = this; b != NULL; b = b->_next) {
aoqi@0 1791 for (int i = 0; i< number_of_methods; i++) {
aoqi@0 1792 _methods[i] = NULL;
aoqi@0 1793 }
aoqi@0 1794 }
aoqi@0 1795 }
aoqi@0 1796 #ifndef PRODUCT
aoqi@0 1797 int count_methods() {
aoqi@0 1798 // count all allocated methods
aoqi@0 1799 int count = 0;
aoqi@0 1800 for (JNIMethodBlock* b = this; b != NULL; b = b->_next) {
aoqi@0 1801 for (int i = 0; i< number_of_methods; i++) {
aoqi@0 1802 if (_methods[i] != _free_method) count++;
aoqi@0 1803 }
aoqi@0 1804 }
aoqi@0 1805 return count;
aoqi@0 1806 }
aoqi@0 1807 #endif // PRODUCT
aoqi@0 1808 };
aoqi@0 1809
aoqi@0 1810 // Something that can't be mistaken for an address or a markOop
aoqi@0 1811 Method* const JNIMethodBlock::_free_method = (Method*)55;
aoqi@0 1812
aoqi@0 1813 // Add a method id to the jmethod_ids
aoqi@0 1814 jmethodID Method::make_jmethod_id(ClassLoaderData* loader_data, Method* m) {
aoqi@0 1815 ClassLoaderData* cld = loader_data;
aoqi@0 1816
aoqi@0 1817 if (!SafepointSynchronize::is_at_safepoint()) {
aoqi@0 1818 // Have to add jmethod_ids() to class loader data thread-safely.
aoqi@0 1819 // Also have to add the method to the list safely, which the cld lock
aoqi@0 1820 // protects as well.
aoqi@0 1821 MutexLockerEx ml(cld->metaspace_lock(), Mutex::_no_safepoint_check_flag);
aoqi@0 1822 if (cld->jmethod_ids() == NULL) {
aoqi@0 1823 cld->set_jmethod_ids(new JNIMethodBlock());
aoqi@0 1824 }
aoqi@0 1825 // jmethodID is a pointer to Method*
aoqi@0 1826 return (jmethodID)cld->jmethod_ids()->add_method(m);
aoqi@0 1827 } else {
aoqi@0 1828 // At safepoint, we are single threaded and can set this.
aoqi@0 1829 if (cld->jmethod_ids() == NULL) {
aoqi@0 1830 cld->set_jmethod_ids(new JNIMethodBlock());
aoqi@0 1831 }
aoqi@0 1832 // jmethodID is a pointer to Method*
aoqi@0 1833 return (jmethodID)cld->jmethod_ids()->add_method(m);
aoqi@0 1834 }
aoqi@0 1835 }
aoqi@0 1836
aoqi@0 1837 // Mark a jmethodID as free. This is called when there is a data race in
aoqi@0 1838 // InstanceKlass while creating the jmethodID cache.
aoqi@0 1839 void Method::destroy_jmethod_id(ClassLoaderData* loader_data, jmethodID m) {
aoqi@0 1840 ClassLoaderData* cld = loader_data;
aoqi@0 1841 Method** ptr = (Method**)m;
aoqi@0 1842 assert(cld->jmethod_ids() != NULL, "should have method handles");
aoqi@0 1843 cld->jmethod_ids()->destroy_method(ptr);
aoqi@0 1844 }
aoqi@0 1845
aoqi@0 1846 void Method::change_method_associated_with_jmethod_id(jmethodID jmid, Method* new_method) {
aoqi@0 1847 // Can't assert the method_holder is the same because the new method has the
aoqi@0 1848 // scratch method holder.
aoqi@0 1849 assert(resolve_jmethod_id(jmid)->method_holder()->class_loader()
aoqi@0 1850 == new_method->method_holder()->class_loader(),
aoqi@0 1851 "changing to a different class loader");
aoqi@0 1852 // Just change the method in place, jmethodID pointer doesn't change.
aoqi@0 1853 *((Method**)jmid) = new_method;
aoqi@0 1854 }
aoqi@0 1855
aoqi@0 1856 bool Method::is_method_id(jmethodID mid) {
aoqi@0 1857 Method* m = resolve_jmethod_id(mid);
aoqi@0 1858 assert(m != NULL, "should be called with non-null method");
aoqi@0 1859 InstanceKlass* ik = m->method_holder();
aoqi@0 1860 ClassLoaderData* cld = ik->class_loader_data();
aoqi@0 1861 if (cld->jmethod_ids() == NULL) return false;
aoqi@0 1862 return (cld->jmethod_ids()->contains((Method**)mid));
aoqi@0 1863 }
aoqi@0 1864
aoqi@0 1865 Method* Method::checked_resolve_jmethod_id(jmethodID mid) {
aoqi@0 1866 if (mid == NULL) return NULL;
aoqi@0 1867 Method* o = resolve_jmethod_id(mid);
aoqi@0 1868 if (o == NULL || o == JNIMethodBlock::_free_method || !((Metadata*)o)->is_method()) {
aoqi@0 1869 return NULL;
aoqi@0 1870 }
aoqi@0 1871 return o;
aoqi@0 1872 };
aoqi@0 1873
aoqi@0 1874 void Method::set_on_stack(const bool value) {
aoqi@0 1875 // Set both the method itself and its constant pool. The constant pool
aoqi@0 1876 // on stack means some method referring to it is also on the stack.
aoqi@0 1877 constants()->set_on_stack(value);
stefank@7333 1878
stefank@7333 1879 bool succeeded = _access_flags.set_on_stack(value);
stefank@7333 1880 if (value && succeeded) {
stefank@7333 1881 MetadataOnStackMark::record(this, Thread::current());
stefank@7333 1882 }
aoqi@0 1883 }
aoqi@0 1884
aoqi@0 1885 // Called when the class loader is unloaded to make all methods weak.
aoqi@0 1886 void Method::clear_jmethod_ids(ClassLoaderData* loader_data) {
aoqi@0 1887 loader_data->jmethod_ids()->clear_all_methods();
aoqi@0 1888 }
aoqi@0 1889
aoqi@0 1890 bool Method::has_method_vptr(const void* ptr) {
aoqi@0 1891 Method m;
aoqi@0 1892 // This assumes that the vtbl pointer is the first word of a C++ object.
aoqi@0 1893 // This assumption is also in universe.cpp patch_klass_vtble
aoqi@0 1894 void* vtbl2 = dereference_vptr((const void*)&m);
aoqi@0 1895 void* this_vtbl = dereference_vptr(ptr);
aoqi@0 1896 return vtbl2 == this_vtbl;
aoqi@0 1897 }
aoqi@0 1898
aoqi@0 1899 // Check that this pointer is valid by checking that the vtbl pointer matches
aoqi@0 1900 bool Method::is_valid_method() const {
aoqi@0 1901 if (this == NULL) {
aoqi@0 1902 return false;
aoqi@0 1903 } else if (!is_metaspace_object()) {
aoqi@0 1904 return false;
aoqi@0 1905 } else {
aoqi@0 1906 return has_method_vptr((const void*)this);
aoqi@0 1907 }
aoqi@0 1908 }
aoqi@0 1909
aoqi@0 1910 #ifndef PRODUCT
aoqi@0 1911 void Method::print_jmethod_ids(ClassLoaderData* loader_data, outputStream* out) {
aoqi@0 1912 out->print_cr("jni_method_id count = %d", loader_data->jmethod_ids()->count_methods());
aoqi@0 1913 }
aoqi@0 1914 #endif // PRODUCT
aoqi@0 1915
aoqi@0 1916
aoqi@0 1917 // Printing
aoqi@0 1918
aoqi@0 1919 #ifndef PRODUCT
aoqi@0 1920
aoqi@0 1921 void Method::print_on(outputStream* st) const {
aoqi@0 1922 ResourceMark rm;
aoqi@0 1923 assert(is_method(), "must be method");
aoqi@0 1924 st->print_cr("%s", internal_name());
aoqi@0 1925 // get the effect of PrintOopAddress, always, for methods:
aoqi@0 1926 st->print_cr(" - this oop: "INTPTR_FORMAT, (intptr_t)this);
aoqi@0 1927 st->print (" - method holder: "); method_holder()->print_value_on(st); st->cr();
aoqi@0 1928 st->print (" - constants: "INTPTR_FORMAT" ", (address)constants());
aoqi@0 1929 constants()->print_value_on(st); st->cr();
aoqi@0 1930 st->print (" - access: 0x%x ", access_flags().as_int()); access_flags().print_on(st); st->cr();
aoqi@0 1931 st->print (" - name: "); name()->print_value_on(st); st->cr();
aoqi@0 1932 st->print (" - signature: "); signature()->print_value_on(st); st->cr();
aoqi@0 1933 st->print_cr(" - max stack: %d", max_stack());
aoqi@0 1934 st->print_cr(" - max locals: %d", max_locals());
aoqi@0 1935 st->print_cr(" - size of params: %d", size_of_parameters());
aoqi@0 1936 st->print_cr(" - method size: %d", method_size());
aoqi@0 1937 if (intrinsic_id() != vmIntrinsics::_none)
aoqi@0 1938 st->print_cr(" - intrinsic id: %d %s", intrinsic_id(), vmIntrinsics::name_at(intrinsic_id()));
aoqi@0 1939 if (highest_comp_level() != CompLevel_none)
aoqi@0 1940 st->print_cr(" - highest level: %d", highest_comp_level());
aoqi@0 1941 st->print_cr(" - vtable index: %d", _vtable_index);
aoqi@0 1942 st->print_cr(" - i2i entry: " INTPTR_FORMAT, interpreter_entry());
aoqi@0 1943 st->print( " - adapters: ");
aoqi@0 1944 AdapterHandlerEntry* a = ((Method*)this)->adapter();
aoqi@0 1945 if (a == NULL)
aoqi@0 1946 st->print_cr(INTPTR_FORMAT, a);
aoqi@0 1947 else
aoqi@0 1948 a->print_adapter_on(st);
aoqi@0 1949 st->print_cr(" - compiled entry " INTPTR_FORMAT, from_compiled_entry());
aoqi@0 1950 st->print_cr(" - code size: %d", code_size());
aoqi@0 1951 if (code_size() != 0) {
aoqi@0 1952 st->print_cr(" - code start: " INTPTR_FORMAT, code_base());
aoqi@0 1953 st->print_cr(" - code end (excl): " INTPTR_FORMAT, code_base() + code_size());
aoqi@0 1954 }
aoqi@0 1955 if (method_data() != NULL) {
aoqi@0 1956 st->print_cr(" - method data: " INTPTR_FORMAT, (address)method_data());
aoqi@0 1957 }
aoqi@0 1958 st->print_cr(" - checked ex length: %d", checked_exceptions_length());
aoqi@0 1959 if (checked_exceptions_length() > 0) {
aoqi@0 1960 CheckedExceptionElement* table = checked_exceptions_start();
aoqi@0 1961 st->print_cr(" - checked ex start: " INTPTR_FORMAT, table);
aoqi@0 1962 if (Verbose) {
aoqi@0 1963 for (int i = 0; i < checked_exceptions_length(); i++) {
aoqi@0 1964 st->print_cr(" - throws %s", constants()->printable_name_at(table[i].class_cp_index));
aoqi@0 1965 }
aoqi@0 1966 }
aoqi@0 1967 }
aoqi@0 1968 if (has_linenumber_table()) {
aoqi@0 1969 u_char* table = compressed_linenumber_table();
aoqi@0 1970 st->print_cr(" - linenumber start: " INTPTR_FORMAT, table);
aoqi@0 1971 if (Verbose) {
aoqi@0 1972 CompressedLineNumberReadStream stream(table);
aoqi@0 1973 while (stream.read_pair()) {
aoqi@0 1974 st->print_cr(" - line %d: %d", stream.line(), stream.bci());
aoqi@0 1975 }
aoqi@0 1976 }
aoqi@0 1977 }
aoqi@0 1978 st->print_cr(" - localvar length: %d", localvariable_table_length());
aoqi@0 1979 if (localvariable_table_length() > 0) {
aoqi@0 1980 LocalVariableTableElement* table = localvariable_table_start();
aoqi@0 1981 st->print_cr(" - localvar start: " INTPTR_FORMAT, table);
aoqi@0 1982 if (Verbose) {
aoqi@0 1983 for (int i = 0; i < localvariable_table_length(); i++) {
aoqi@0 1984 int bci = table[i].start_bci;
aoqi@0 1985 int len = table[i].length;
aoqi@0 1986 const char* name = constants()->printable_name_at(table[i].name_cp_index);
aoqi@0 1987 const char* desc = constants()->printable_name_at(table[i].descriptor_cp_index);
aoqi@0 1988 int slot = table[i].slot;
aoqi@0 1989 st->print_cr(" - %s %s bci=%d len=%d slot=%d", desc, name, bci, len, slot);
aoqi@0 1990 }
aoqi@0 1991 }
aoqi@0 1992 }
aoqi@0 1993 if (code() != NULL) {
aoqi@0 1994 st->print (" - compiled code: ");
aoqi@0 1995 code()->print_value_on(st);
aoqi@0 1996 }
aoqi@0 1997 if (is_native()) {
aoqi@0 1998 st->print_cr(" - native function: " INTPTR_FORMAT, native_function());
aoqi@0 1999 st->print_cr(" - signature handler: " INTPTR_FORMAT, signature_handler());
aoqi@0 2000 }
aoqi@0 2001 }
aoqi@0 2002
aoqi@0 2003 #endif //PRODUCT
aoqi@0 2004
aoqi@0 2005 void Method::print_value_on(outputStream* st) const {
aoqi@0 2006 assert(is_method(), "must be method");
aoqi@0 2007 st->print("%s", internal_name());
aoqi@0 2008 print_address_on(st);
aoqi@0 2009 st->print(" ");
aoqi@0 2010 name()->print_value_on(st);
aoqi@0 2011 st->print(" ");
aoqi@0 2012 signature()->print_value_on(st);
aoqi@0 2013 st->print(" in ");
aoqi@0 2014 method_holder()->print_value_on(st);
aoqi@0 2015 if (WizardMode) st->print("#%d", _vtable_index);
aoqi@0 2016 if (WizardMode) st->print("[%d,%d]", size_of_parameters(), max_locals());
aoqi@0 2017 if (WizardMode && code() != NULL) st->print(" ((nmethod*)%p)", code());
aoqi@0 2018 }
aoqi@0 2019
aoqi@0 2020 #if INCLUDE_SERVICES
aoqi@0 2021 // Size Statistics
aoqi@0 2022 void Method::collect_statistics(KlassSizeStats *sz) const {
aoqi@0 2023 int mysize = sz->count(this);
aoqi@0 2024 sz->_method_bytes += mysize;
aoqi@0 2025 sz->_method_all_bytes += mysize;
aoqi@0 2026 sz->_rw_bytes += mysize;
aoqi@0 2027
aoqi@0 2028 if (constMethod()) {
aoqi@0 2029 constMethod()->collect_statistics(sz);
aoqi@0 2030 }
aoqi@0 2031 if (method_data()) {
aoqi@0 2032 method_data()->collect_statistics(sz);
aoqi@0 2033 }
aoqi@0 2034 }
aoqi@0 2035 #endif // INCLUDE_SERVICES
aoqi@0 2036
aoqi@0 2037 // Verification
aoqi@0 2038
aoqi@0 2039 void Method::verify_on(outputStream* st) {
aoqi@0 2040 guarantee(is_method(), "object must be method");
aoqi@0 2041 guarantee(constants()->is_constantPool(), "should be constant pool");
aoqi@0 2042 guarantee(constMethod()->is_constMethod(), "should be ConstMethod*");
aoqi@0 2043 MethodData* md = method_data();
aoqi@0 2044 guarantee(md == NULL ||
aoqi@0 2045 md->is_methodData(), "should be method data");
aoqi@0 2046 }

mercurial