src/share/vm/oops/method.cpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 7365
600c44255e5f
parent 6876
710a3c8b516e
child 7994
04ff2f6cd0eb
permissions
-rw-r--r--

merge

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

mercurial