src/share/vm/oops/method.cpp

Fri, 29 Apr 2016 00:06:10 +0800

author
aoqi
date
Fri, 29 Apr 2016 00:06:10 +0800
changeset 1
2d8a650513c2
parent 0
f90c822e73f8
child 26
ed5b982c0b0e
permissions
-rw-r--r--

Added MIPS 64-bit port.

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

mercurial