src/share/vm/oops/method.cpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 1
2d8a650513c2
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

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

mercurial