1 /* 2 * Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_OOPS_METHOD_HPP 26 #define SHARE_OOPS_METHOD_HPP 27 28 #include "code/compressedStream.hpp" 29 #include "compiler/compilerDefinitions.hpp" 30 #include "interpreter/invocationCounter.hpp" 31 #include "oops/annotations.hpp" 32 #include "oops/constantPool.hpp" 33 #include "oops/methodCounters.hpp" 34 #include "oops/instanceKlass.hpp" 35 #include "oops/oop.hpp" 36 #include "oops/typeArrayOop.hpp" 37 #include "utilities/accessFlags.hpp" 38 #include "utilities/align.hpp" 39 #include "utilities/growableArray.hpp" 40 #include "utilities/macros.hpp" 41 #include "utilities/vmEnums.hpp" 42 #if INCLUDE_JFR 43 #include "jfr/support/jfrTraceIdExtension.hpp" 44 #endif 45 46 47 // A Method represents a Java method. 48 // 49 // Note that most applications load thousands of methods, so keeping the size of this 50 // class small has a big impact on footprint. 51 // 52 // Note that native_function and signature_handler have to be at fixed offsets 53 // (required by the interpreter) 54 // 55 // Method embedded field layout (after declared fields): 56 // [EMBEDDED native_function (present only if native) ] 57 // [EMBEDDED signature_handler (present only if native) ] 58 59 class CheckedExceptionElement; 60 class LocalVariableTableElement; 61 class AdapterHandlerEntry; 62 class MethodData; 63 class MethodCounters; 64 class ConstMethod; 65 class InlineTableSizes; 66 class CompiledMethod; 67 class InterpreterOopMap; 68 69 class Method : public Metadata { 70 friend class VMStructs; 71 friend class JVMCIVMStructs; 72 friend class MethodTest; 73 private: 74 // If you add a new field that points to any metaspace object, you 75 // must add this field to Method::metaspace_pointers_do(). 76 ConstMethod* _constMethod; // Method read-only data. 77 MethodData* _method_data; 78 MethodCounters* _method_counters; 79 AdapterHandlerEntry* _adapter; 80 AccessFlags _access_flags; // Access flags 81 int _vtable_index; // vtable index of this method (see VtableIndexFlag) 82 // note: can have vtables with >2**16 elements (because of inheritance) 83 u2 _intrinsic_id; // vmSymbols::intrinsic_id (0 == _none) 84 85 // Flags 86 enum Flags { 87 _caller_sensitive = 1 << 0, 88 _force_inline = 1 << 1, 89 _dont_inline = 1 << 2, 90 _hidden = 1 << 3, 91 _has_injected_profile = 1 << 4, 92 _intrinsic_candidate = 1 << 5, 93 _reserved_stack_access = 1 << 6, 94 _scalarized_args = 1 << 7, 95 _c1_needs_stack_repair = 1 << 8, 96 _c2_needs_stack_repair = 1 << 9, 97 _scoped = 1 << 10 98 }; 99 mutable u2 _flags; 100 101 JFR_ONLY(DEFINE_TRACE_FLAG;) 102 103 #ifndef PRODUCT 104 int64_t _compiled_invocation_count; 105 106 Symbol* _name; 107 #endif 108 // Entry point for calling both from and to the interpreter. 109 address _i2i_entry; // All-args-on-stack calling convention 110 // Entry point for calling from compiled code, to compiled code if it exists 111 // or else the interpreter. 112 volatile address _from_compiled_entry; // Cache of: _code ? _code->verified_entry_point() : _adapter->c2i_entry() 113 volatile address _from_compiled_inline_ro_entry; // Cache of: _code ? _code->verified_inline_ro_entry_point() : _adapter->c2i_inline_ro_entry() 114 volatile address _from_compiled_inline_entry; // Cache of: _code ? _code->verified_inline_entry_point() : _adapter->c2i_inline_entry() 115 // The entry point for calling both from and to compiled code is 116 // "_code->entry_point()". Because of tiered compilation and de-opt, this 117 // field can come and go. It can transition from NULL to not-null at any 118 // time (whenever a compile completes). It can transition from not-null to 119 // NULL only at safepoints (because of a de-opt). 120 CompiledMethod* volatile _code; // Points to the corresponding piece of native code 121 volatile address _from_interpreted_entry; // Cache of _code ? _adapter->i2c_entry() : _i2i_entry 122 123 // Constructor 124 Method(ConstMethod* xconst, AccessFlags access_flags, Symbol* name); 125 public: 126 127 static Method* allocate(ClassLoaderData* loader_data, 128 int byte_code_size, 129 AccessFlags access_flags, 130 InlineTableSizes* sizes, 131 ConstMethod::MethodType method_type, 132 Symbol* name, 133 TRAPS); 134 135 // CDS and vtbl checking can create an empty Method to get vtbl pointer. 136 Method(){} 137 138 virtual bool is_method() const { return true; } 139 140 void restore_unshareable_info(TRAPS); 141 142 // accessors for instance variables 143 144 ConstMethod* constMethod() const { return _constMethod; } 145 void set_constMethod(ConstMethod* xconst) { _constMethod = xconst; } 146 147 148 static address make_adapters(const methodHandle& mh, TRAPS); 149 address from_compiled_entry() const; 150 address from_compiled_inline_ro_entry() const; 151 address from_compiled_inline_entry() const; 152 address from_compiled_entry_no_trampoline(bool caller_is_c1) const; 153 address from_interpreted_entry() const; 154 155 // access flag 156 AccessFlags access_flags() const { return _access_flags; } 157 void set_access_flags(AccessFlags flags) { _access_flags = flags; } 158 159 // name 160 Symbol* name() const { return constants()->symbol_at(name_index()); } 161 int name_index() const { return constMethod()->name_index(); } 162 void set_name_index(int index) { constMethod()->set_name_index(index); } 163 164 // signature 165 Symbol* signature() const { return constants()->symbol_at(signature_index()); } 166 int signature_index() const { return constMethod()->signature_index(); } 167 void set_signature_index(int index) { constMethod()->set_signature_index(index); } 168 169 // generics support 170 Symbol* generic_signature() const { int idx = generic_signature_index(); return ((idx != 0) ? constants()->symbol_at(idx) : (Symbol*)NULL); } 171 int generic_signature_index() const { return constMethod()->generic_signature_index(); } 172 void set_generic_signature_index(int index) { constMethod()->set_generic_signature_index(index); } 173 174 // annotations support 175 AnnotationArray* annotations() const { 176 return constMethod()->method_annotations(); 177 } 178 AnnotationArray* parameter_annotations() const { 179 return constMethod()->parameter_annotations(); 180 } 181 AnnotationArray* annotation_default() const { 182 return constMethod()->default_annotations(); 183 } 184 AnnotationArray* type_annotations() const { 185 return constMethod()->type_annotations(); 186 } 187 188 // Helper routine: get klass name + "." + method name + signature as 189 // C string, for the purpose of providing more useful 190 // fatal error handling. The string is allocated in resource 191 // area if a buffer is not provided by the caller. 192 char* name_and_sig_as_C_string() const; 193 char* name_and_sig_as_C_string(char* buf, int size) const; 194 195 // Static routine in the situations we don't have a Method* 196 static char* name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature); 197 static char* name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature, char* buf, int size); 198 199 // Get return type + klass name + "." + method name + ( parameters types ) 200 // as a C string or print it to an outputStream. 201 // This is to be used to assemble strings passed to Java, so that 202 // the text more resembles Java code. Used in exception messages. 203 // Memory is allocated in the resource area; the caller needs 204 // a ResourceMark. 205 const char* external_name() const; 206 void print_external_name(outputStream *os) const; 207 208 static const char* external_name( Klass* klass, Symbol* method_name, Symbol* signature); 209 static void print_external_name(outputStream *os, Klass* klass, Symbol* method_name, Symbol* signature); 210 211 Bytecodes::Code java_code_at(int bci) const { 212 return Bytecodes::java_code_at(this, bcp_from(bci)); 213 } 214 Bytecodes::Code code_at(int bci) const { 215 return Bytecodes::code_at(this, bcp_from(bci)); 216 } 217 218 // JVMTI breakpoints 219 #if !INCLUDE_JVMTI 220 Bytecodes::Code orig_bytecode_at(int bci) const { 221 ShouldNotReachHere(); 222 return Bytecodes::_shouldnotreachhere; 223 } 224 void set_orig_bytecode_at(int bci, Bytecodes::Code code) { 225 ShouldNotReachHere(); 226 }; 227 u2 number_of_breakpoints() const {return 0;} 228 #else // !INCLUDE_JVMTI 229 Bytecodes::Code orig_bytecode_at(int bci) const; 230 void set_orig_bytecode_at(int bci, Bytecodes::Code code); 231 void set_breakpoint(int bci); 232 void clear_breakpoint(int bci); 233 void clear_all_breakpoints(); 234 // Tracking number of breakpoints, for fullspeed debugging. 235 // Only mutated by VM thread. 236 u2 number_of_breakpoints() const { 237 MethodCounters* mcs = method_counters(); 238 if (mcs == NULL) { 239 return 0; 240 } else { 241 return mcs->number_of_breakpoints(); 242 } 243 } 244 void incr_number_of_breakpoints(Thread* current) { 245 MethodCounters* mcs = get_method_counters(current); 246 if (mcs != NULL) { 247 mcs->incr_number_of_breakpoints(); 248 } 249 } 250 void decr_number_of_breakpoints(Thread* current) { 251 MethodCounters* mcs = get_method_counters(current); 252 if (mcs != NULL) { 253 mcs->decr_number_of_breakpoints(); 254 } 255 } 256 // Initialization only 257 void clear_number_of_breakpoints() { 258 MethodCounters* mcs = method_counters(); 259 if (mcs != NULL) { 260 mcs->clear_number_of_breakpoints(); 261 } 262 } 263 #endif // !INCLUDE_JVMTI 264 265 // index into InstanceKlass methods() array 266 // note: also used by jfr 267 u2 method_idnum() const { return constMethod()->method_idnum(); } 268 void set_method_idnum(u2 idnum) { constMethod()->set_method_idnum(idnum); } 269 270 u2 orig_method_idnum() const { return constMethod()->orig_method_idnum(); } 271 void set_orig_method_idnum(u2 idnum) { constMethod()->set_orig_method_idnum(idnum); } 272 273 // code size 274 int code_size() const { return constMethod()->code_size(); } 275 276 // method size in words 277 int method_size() const { return sizeof(Method)/wordSize + ( is_native() ? 2 : 0 ); } 278 279 // constant pool for Klass* holding this method 280 ConstantPool* constants() const { return constMethod()->constants(); } 281 void set_constants(ConstantPool* c) { constMethod()->set_constants(c); } 282 283 // max stack 284 // return original max stack size for method verification 285 int verifier_max_stack() const { return constMethod()->max_stack(); } 286 int max_stack() const { return constMethod()->max_stack() + extra_stack_entries(); } 287 void set_max_stack(int size) { constMethod()->set_max_stack(size); } 288 289 // max locals 290 int max_locals() const { return constMethod()->max_locals(); } 291 void set_max_locals(int size) { constMethod()->set_max_locals(size); } 292 293 int highest_comp_level() const; 294 void set_highest_comp_level(int level); 295 int highest_osr_comp_level() const; 296 void set_highest_osr_comp_level(int level); 297 298 #if COMPILER2_OR_JVMCI 299 // Count of times method was exited via exception while interpreting 300 void interpreter_throwout_increment(Thread* current) { 301 MethodCounters* mcs = get_method_counters(current); 302 if (mcs != NULL) { 303 mcs->interpreter_throwout_increment(); 304 } 305 } 306 #endif 307 308 int interpreter_throwout_count() const { 309 MethodCounters* mcs = method_counters(); 310 if (mcs == NULL) { 311 return 0; 312 } else { 313 return mcs->interpreter_throwout_count(); 314 } 315 } 316 317 // Derive stuff from the signature at load time. 318 void compute_from_signature(Symbol* sig); 319 320 // size of parameters (receiver if any + arguments) 321 int size_of_parameters() const { return constMethod()->size_of_parameters(); } 322 void set_size_of_parameters(int size) { constMethod()->set_size_of_parameters(size); } 323 324 bool has_stackmap_table() const { 325 return constMethod()->has_stackmap_table(); 326 } 327 328 Array<u1>* stackmap_data() const { 329 return constMethod()->stackmap_data(); 330 } 331 332 void set_stackmap_data(Array<u1>* sd) { 333 constMethod()->set_stackmap_data(sd); 334 } 335 336 // exception handler table 337 bool has_exception_handler() const 338 { return constMethod()->has_exception_handler(); } 339 int exception_table_length() const 340 { return constMethod()->exception_table_length(); } 341 ExceptionTableElement* exception_table_start() const 342 { return constMethod()->exception_table_start(); } 343 344 // Finds the first entry point bci of an exception handler for an 345 // exception of klass ex_klass thrown at throw_bci. A value of NULL 346 // for ex_klass indicates that the exception klass is not known; in 347 // this case it matches any constraint class. Returns -1 if the 348 // exception cannot be handled in this method. The handler 349 // constraint classes are loaded if necessary. Note that this may 350 // throw an exception if loading of the constraint classes causes 351 // an IllegalAccessError (bugid 4307310) or an OutOfMemoryError. 352 // If an exception is thrown, returns the bci of the 353 // exception handler which caused the exception to be thrown, which 354 // is needed for proper retries. See, for example, 355 // InterpreterRuntime::exception_handler_for_exception. 356 static int fast_exception_handler_bci_for(const methodHandle& mh, Klass* ex_klass, int throw_bci, TRAPS); 357 358 static bool register_native(Klass* k, 359 Symbol* name, 360 Symbol* signature, 361 address entry, 362 TRAPS); 363 364 // method data access 365 MethodData* method_data() const { 366 return _method_data; 367 } 368 369 void set_method_data(MethodData* data); 370 371 MethodCounters* method_counters() const { 372 return _method_counters; 373 } 374 375 void clear_method_counters() { 376 _method_counters = NULL; 377 } 378 379 bool init_method_counters(MethodCounters* counters); 380 381 int prev_event_count() const { 382 MethodCounters* mcs = method_counters(); 383 return mcs == NULL ? 0 : mcs->prev_event_count(); 384 } 385 void set_prev_event_count(int count) { 386 MethodCounters* mcs = method_counters(); 387 if (mcs != NULL) { 388 mcs->set_prev_event_count(count); 389 } 390 } 391 jlong prev_time() const { 392 MethodCounters* mcs = method_counters(); 393 return mcs == NULL ? 0 : mcs->prev_time(); 394 } 395 void set_prev_time(jlong time) { 396 MethodCounters* mcs = method_counters(); 397 if (mcs != NULL) { 398 mcs->set_prev_time(time); 399 } 400 } 401 float rate() const { 402 MethodCounters* mcs = method_counters(); 403 return mcs == NULL ? 0 : mcs->rate(); 404 } 405 void set_rate(float rate) { 406 MethodCounters* mcs = method_counters(); 407 if (mcs != NULL) { 408 mcs->set_rate(rate); 409 } 410 } 411 412 int nmethod_age() const { 413 if (method_counters() == NULL) { 414 return INT_MAX; 415 } else { 416 return method_counters()->nmethod_age(); 417 } 418 } 419 420 int invocation_count() const; 421 int backedge_count() const; 422 423 bool was_executed_more_than(int n); 424 bool was_never_executed() { return !was_executed_more_than(0); } 425 426 static void build_interpreter_method_data(const methodHandle& method, TRAPS); 427 428 static MethodCounters* build_method_counters(Thread* current, Method* m); 429 430 int interpreter_invocation_count() { return invocation_count(); } 431 432 #ifndef PRODUCT 433 int64_t compiled_invocation_count() const { return _compiled_invocation_count;} 434 void set_compiled_invocation_count(int count) { _compiled_invocation_count = (int64_t)count; } 435 #else 436 // for PrintMethodData in a product build 437 int64_t compiled_invocation_count() const { return 0; } 438 #endif // not PRODUCT 439 440 // Clear (non-shared space) pointers which could not be relevant 441 // if this (shared) method were mapped into another JVM. 442 void remove_unshareable_info(); 443 444 // nmethod/verified compiler entry 445 address verified_code_entry(); 446 address verified_inline_code_entry(); 447 address verified_inline_ro_code_entry(); 448 bool check_code() const; // Not inline to avoid circular ref 449 CompiledMethod* volatile code() const; 450 451 // Locks CompiledMethod_lock if not held. 452 void unlink_code(CompiledMethod *compare); 453 // Locks CompiledMethod_lock if not held. 454 void unlink_code(); 455 456 private: 457 // Either called with CompiledMethod_lock held or from constructor. 458 void clear_code(); 459 460 public: 461 static void set_code(const methodHandle& mh, CompiledMethod* code); 462 void set_adapter_entry(AdapterHandlerEntry* adapter) { 463 _adapter = adapter; 464 } 465 void set_from_compiled_entry(address entry) { 466 _from_compiled_entry = entry; 467 } 468 void set_from_compiled_inline_ro_entry(address entry) { 469 _from_compiled_inline_ro_entry = entry; 470 } 471 void set_from_compiled_inline_entry(address entry) { 472 _from_compiled_inline_entry = entry; 473 } 474 475 address get_i2c_entry(); 476 address get_c2i_entry(); 477 address get_c2i_inline_entry(); 478 address get_c2i_unverified_entry(); 479 address get_c2i_unverified_inline_entry(); 480 address get_c2i_no_clinit_check_entry(); 481 AdapterHandlerEntry* adapter() const { 482 return _adapter; 483 } 484 // setup entry points 485 void link_method(const methodHandle& method, TRAPS); 486 // clear entry points. Used by sharing code during dump time 487 void unlink_method() NOT_CDS_RETURN; 488 489 virtual void metaspace_pointers_do(MetaspaceClosure* iter); 490 virtual MetaspaceObj::Type type() const { return MethodType; } 491 492 // vtable index 493 enum VtableIndexFlag { 494 // Valid vtable indexes are non-negative (>= 0). 495 // These few negative values are used as sentinels. 496 itable_index_max = -10, // first itable index, growing downward 497 pending_itable_index = -9, // itable index will be assigned 498 invalid_vtable_index = -4, // distinct from any valid vtable index 499 garbage_vtable_index = -3, // not yet linked; no vtable layout yet 500 nonvirtual_vtable_index = -2 // there is no need for vtable dispatch 501 // 6330203 Note: Do not use -1, which was overloaded with many meanings. 502 }; 503 DEBUG_ONLY(bool valid_vtable_index() const { return _vtable_index >= nonvirtual_vtable_index; }) 504 bool has_vtable_index() const { return _vtable_index >= 0; } 505 int vtable_index() const { return _vtable_index; } 506 void set_vtable_index(int index); 507 DEBUG_ONLY(bool valid_itable_index() const { return _vtable_index <= pending_itable_index; }) 508 bool has_itable_index() const { return _vtable_index <= itable_index_max; } 509 int itable_index() const { assert(valid_itable_index(), ""); 510 return itable_index_max - _vtable_index; } 511 void set_itable_index(int index); 512 513 // interpreter entry 514 address interpreter_entry() const { return _i2i_entry; } 515 // Only used when first initialize so we can set _i2i_entry and _from_interpreted_entry 516 void set_interpreter_entry(address entry) { 517 if (_i2i_entry != entry) { 518 _i2i_entry = entry; 519 } 520 if (_from_interpreted_entry != entry) { 521 _from_interpreted_entry = entry; 522 } 523 } 524 525 // native function (used for native methods only) 526 enum { 527 native_bind_event_is_interesting = true 528 }; 529 address native_function() const { return *(native_function_addr()); } 530 531 // Must specify a real function (not NULL). 532 // Use clear_native_function() to unregister. 533 void set_native_function(address function, bool post_event_flag); 534 bool has_native_function() const; 535 void clear_native_function(); 536 537 // signature handler (used for native methods only) 538 address signature_handler() const { return *(signature_handler_addr()); } 539 void set_signature_handler(address handler); 540 541 // Interpreter oopmap support 542 void mask_for(int bci, InterpreterOopMap* mask); 543 544 // operations on invocation counter 545 void print_invocation_count(); 546 547 // byte codes 548 void set_code(address code) { return constMethod()->set_code(code); } 549 address code_base() const { return constMethod()->code_base(); } 550 bool contains(address bcp) const { return constMethod()->contains(bcp); } 551 552 // prints byte codes 553 void print_codes() const { print_codes_on(tty); } 554 void print_codes_on(outputStream* st) const; 555 void print_codes_on(int from, int to, outputStream* st) const; 556 557 // method parameters 558 bool has_method_parameters() const 559 { return constMethod()->has_method_parameters(); } 560 int method_parameters_length() const 561 { return constMethod()->method_parameters_length(); } 562 MethodParametersElement* method_parameters_start() const 563 { return constMethod()->method_parameters_start(); } 564 565 // checked exceptions 566 int checked_exceptions_length() const 567 { return constMethod()->checked_exceptions_length(); } 568 CheckedExceptionElement* checked_exceptions_start() const 569 { return constMethod()->checked_exceptions_start(); } 570 571 // localvariable table 572 bool has_localvariable_table() const 573 { return constMethod()->has_localvariable_table(); } 574 int localvariable_table_length() const 575 { return constMethod()->localvariable_table_length(); } 576 LocalVariableTableElement* localvariable_table_start() const 577 { return constMethod()->localvariable_table_start(); } 578 579 bool has_linenumber_table() const 580 { return constMethod()->has_linenumber_table(); } 581 u_char* compressed_linenumber_table() const 582 { return constMethod()->compressed_linenumber_table(); } 583 584 // method holder (the Klass* holding this method) 585 InstanceKlass* method_holder() const { return constants()->pool_holder(); } 586 587 Symbol* klass_name() const; // returns the name of the method holder 588 BasicType result_type() const { return constMethod()->result_type(); } 589 bool is_returning_oop() const { BasicType r = result_type(); return is_reference_type(r); } 590 InlineKlass* returns_inline_type(Thread* thread) const; 591 592 // Checked exceptions thrown by this method (resolved to mirrors) 593 objArrayHandle resolved_checked_exceptions(TRAPS) { return resolved_checked_exceptions_impl(this, THREAD); } 594 595 // Access flags 596 bool is_public() const { return access_flags().is_public(); } 597 bool is_private() const { return access_flags().is_private(); } 598 bool is_protected() const { return access_flags().is_protected(); } 599 bool is_package_private() const { return !is_public() && !is_private() && !is_protected(); } 600 bool is_static() const { return access_flags().is_static(); } 601 bool is_final() const { return access_flags().is_final(); } 602 bool is_synchronized() const { return access_flags().is_synchronized();} 603 bool is_native() const { return access_flags().is_native(); } 604 bool is_abstract() const { return access_flags().is_abstract(); } 605 bool is_synthetic() const { return access_flags().is_synthetic(); } 606 607 // returns true if contains only return operation 608 bool is_empty_method() const; 609 610 // returns true if this is a vanilla constructor 611 bool is_vanilla_constructor() const; 612 613 // checks method and its method holder 614 bool is_final_method() const; 615 bool is_final_method(AccessFlags class_access_flags) const; 616 // interface method declared with 'default' - excludes private interface methods 617 bool is_default_method() const; 618 619 // true if method needs no dynamic dispatch (final and/or no vtable entry) 620 bool can_be_statically_bound() const; 621 bool can_be_statically_bound(InstanceKlass* context) const; 622 bool can_be_statically_bound(AccessFlags class_access_flags) const; 623 624 // true if method can omit stack trace in throw in compiled code. 625 bool can_omit_stack_trace(); 626 627 // returns true if the method has any backward branches. 628 bool has_loops() { 629 return access_flags().loops_flag_init() ? access_flags().has_loops() : compute_has_loops_flag(); 630 }; 631 632 bool compute_has_loops_flag(); 633 634 bool has_jsrs() { 635 return access_flags().has_jsrs(); 636 }; 637 void set_has_jsrs() { 638 _access_flags.set_has_jsrs(); 639 } 640 641 // returns true if the method has any monitors. 642 bool has_monitors() const { return is_synchronized() || access_flags().has_monitor_bytecodes(); } 643 bool has_monitor_bytecodes() const { return access_flags().has_monitor_bytecodes(); } 644 645 void set_has_monitor_bytecodes() { _access_flags.set_has_monitor_bytecodes(); } 646 647 // monitor matching. This returns a conservative estimate of whether the monitorenter/monitorexit bytecodes 648 // propererly nest in the method. It might return false, even though they actually nest properly, since the info. 649 // has not been computed yet. 650 bool guaranteed_monitor_matching() const { return access_flags().is_monitor_matching(); } 651 void set_guaranteed_monitor_matching() { _access_flags.set_monitor_matching(); } 652 653 // returns true if the method is an accessor function (setter/getter). 654 bool is_accessor() const; 655 656 // returns true if the method is a getter 657 bool is_getter() const; 658 659 // returns true if the method is a setter 660 bool is_setter() const; 661 662 // returns true if the method does nothing but return a constant of primitive type 663 bool is_constant_getter() const; 664 665 // returns true if the method name is <clinit> and the method has 666 // valid static initializer flags. 667 bool is_class_initializer() const; 668 669 // returns true if the method name is <init> and the method is not a static factory 670 bool is_object_constructor() const; 671 672 // returns true if the method is an object constructor or class initializer 673 // (non-static <init> or <clinit>), but false for factories (static <init>). 674 bool is_object_constructor_or_class_initializer() const; 675 676 // returns true if the method name is <init> and the method is static 677 bool is_static_init_factory() const; 678 679 // compiled code support 680 // NOTE: code() is inherently racy as deopt can be clearing code 681 // simultaneously. Use with caution. 682 bool has_compiled_code() const; 683 684 bool needs_clinit_barrier() const; 685 686 // sizing 687 static int header_size() { 688 return align_up((int)sizeof(Method), wordSize) / wordSize; 689 } 690 static int size(bool is_native); 691 int size() const { return method_size(); } 692 void log_touched(Thread* current); 693 static void print_touched_methods(outputStream* out); 694 695 // interpreter support 696 static ByteSize const_offset() { return byte_offset_of(Method, _constMethod ); } 697 static ByteSize access_flags_offset() { return byte_offset_of(Method, _access_flags ); } 698 static ByteSize from_compiled_offset() { return byte_offset_of(Method, _from_compiled_entry); } 699 static ByteSize from_compiled_inline_offset() { return byte_offset_of(Method, _from_compiled_inline_entry); } 700 static ByteSize from_compiled_inline_ro_offset(){ return byte_offset_of(Method, _from_compiled_inline_ro_entry); } 701 static ByteSize code_offset() { return byte_offset_of(Method, _code); } 702 static ByteSize flags_offset() { return byte_offset_of(Method, _flags); } 703 static ByteSize method_data_offset() { 704 return byte_offset_of(Method, _method_data); 705 } 706 static ByteSize method_counters_offset() { 707 return byte_offset_of(Method, _method_counters); 708 } 709 #ifndef PRODUCT 710 static ByteSize compiled_invocation_counter_offset() { return byte_offset_of(Method, _compiled_invocation_count); } 711 #endif // not PRODUCT 712 static ByteSize native_function_offset() { return in_ByteSize(sizeof(Method)); } 713 static ByteSize from_interpreted_offset() { return byte_offset_of(Method, _from_interpreted_entry ); } 714 static ByteSize interpreter_entry_offset() { return byte_offset_of(Method, _i2i_entry ); } 715 static ByteSize signature_handler_offset() { return in_ByteSize(sizeof(Method) + wordSize); } 716 static ByteSize itable_index_offset() { return byte_offset_of(Method, _vtable_index ); } 717 718 // for code generation 719 static int method_data_offset_in_bytes() { return offset_of(Method, _method_data); } 720 static int intrinsic_id_offset_in_bytes() { return offset_of(Method, _intrinsic_id); } 721 static int intrinsic_id_size_in_bytes() { return sizeof(u2); } 722 723 // Static methods that are used to implement member methods where an exposed this pointer 724 // is needed due to possible GCs 725 static objArrayHandle resolved_checked_exceptions_impl(Method* method, TRAPS); 726 727 // Returns the byte code index from the byte code pointer 728 int bci_from(address bcp) const; 729 address bcp_from(int bci) const; 730 address bcp_from(address bcp) const; 731 int validate_bci_from_bcp(address bcp) const; 732 int validate_bci(int bci) const; 733 734 // Returns the line number for a bci if debugging information for the method is prowided, 735 // -1 is returned otherwise. 736 int line_number_from_bci(int bci) const; 737 738 // Reflection support 739 bool is_overridden_in(Klass* k) const; 740 741 // Stack walking support 742 bool is_ignored_by_security_stack_walk() const; 743 744 // JSR 292 support 745 bool is_method_handle_intrinsic() const; // MethodHandles::is_signature_polymorphic_intrinsic(intrinsic_id) 746 bool is_compiled_lambda_form() const; // intrinsic_id() == vmIntrinsics::_compiledLambdaForm 747 bool has_member_arg() const; // intrinsic_id() == vmIntrinsics::_linkToSpecial, etc. 748 static methodHandle make_method_handle_intrinsic(vmIntrinsicID iid, // _invokeBasic, _linkToVirtual 749 Symbol* signature, //anything at all 750 TRAPS); 751 static Klass* check_non_bcp_klass(Klass* klass); 752 753 enum { 754 // How many extra stack entries for invokedynamic 755 extra_stack_entries_for_jsr292 = 1 756 }; 757 758 // this operates only on invoke methods: 759 // presize interpreter frames for extra interpreter stack entries, if needed 760 // Account for the extra appendix argument for invokehandle/invokedynamic 761 static int extra_stack_entries() { return extra_stack_entries_for_jsr292; } 762 static int extra_stack_words(); // = extra_stack_entries() * Interpreter::stackElementSize 763 764 // RedefineClasses() support: 765 bool is_old() const { return access_flags().is_old(); } 766 void set_is_old() { _access_flags.set_is_old(); } 767 bool is_obsolete() const { return access_flags().is_obsolete(); } 768 void set_is_obsolete() { _access_flags.set_is_obsolete(); } 769 bool is_deleted() const { return access_flags().is_deleted(); } 770 void set_is_deleted() { _access_flags.set_is_deleted(); } 771 772 bool on_stack() const { return access_flags().on_stack(); } 773 void set_on_stack(const bool value); 774 775 // see the definition in Method*.cpp for the gory details 776 bool should_not_be_cached() const; 777 778 // JVMTI Native method prefixing support: 779 bool is_prefixed_native() const { return access_flags().is_prefixed_native(); } 780 void set_is_prefixed_native() { _access_flags.set_is_prefixed_native(); } 781 782 // Rewriting support 783 static methodHandle clone_with_new_data(const methodHandle& m, u_char* new_code, int new_code_length, 784 u_char* new_compressed_linenumber_table, int new_compressed_linenumber_size, TRAPS); 785 786 // jmethodID handling 787 // Because the useful life-span of a jmethodID cannot be determined, 788 // once created they are never reclaimed. The methods to which they refer, 789 // however, can be GC'ed away if the class is unloaded or if the method is 790 // made obsolete or deleted -- in these cases, the jmethodID 791 // refers to NULL (as is the case for any weak reference). 792 static jmethodID make_jmethod_id(ClassLoaderData* loader_data, Method* mh); 793 static void destroy_jmethod_id(ClassLoaderData* loader_data, jmethodID mid); 794 795 // Ensure there is enough capacity in the internal tracking data 796 // structures to hold the number of jmethodIDs you plan to generate. 797 // This saves substantial time doing allocations. 798 static void ensure_jmethod_ids(ClassLoaderData* loader_data, int capacity); 799 800 // Use resolve_jmethod_id() in situations where the caller is expected 801 // to provide a valid jmethodID; the only sanity checks are in asserts; 802 // result guaranteed not to be NULL. 803 inline static Method* resolve_jmethod_id(jmethodID mid) { 804 assert(mid != NULL, "JNI method id should not be null"); 805 return *((Method**)mid); 806 } 807 808 // Use checked_resolve_jmethod_id() in situations where the caller 809 // should provide a valid jmethodID, but might not. NULL is returned 810 // when the jmethodID does not refer to a valid method. 811 static Method* checked_resolve_jmethod_id(jmethodID mid); 812 813 static void change_method_associated_with_jmethod_id(jmethodID old_jmid_ptr, Method* new_method); 814 static bool is_method_id(jmethodID mid); 815 816 // Clear methods 817 static void clear_jmethod_ids(ClassLoaderData* loader_data); 818 static void print_jmethod_ids_count(const ClassLoaderData* loader_data, outputStream* out) PRODUCT_RETURN; 819 820 // Get this method's jmethodID -- allocate if it doesn't exist 821 jmethodID jmethod_id(); 822 823 // Lookup the jmethodID for this method. Return NULL if not found. 824 // NOTE that this function can be called from a signal handler 825 // (see AsyncGetCallTrace support for Forte Analyzer) and this 826 // needs to be async-safe. No allocation should be done and 827 // so handles are not used to avoid deadlock. 828 jmethodID find_jmethod_id_or_null() { return method_holder()->jmethod_id_or_null(this); } 829 830 // Support for inlining of intrinsic methods 831 vmIntrinsicID intrinsic_id() const { return (vmIntrinsicID) _intrinsic_id; } 832 void set_intrinsic_id(vmIntrinsicID id) { _intrinsic_id = (u2) id; } 833 834 // Helper routines for intrinsic_id() and vmIntrinsics::method(). 835 void init_intrinsic_id(vmSymbolID klass_id); // updates from _none if a match 836 static vmSymbolID klass_id_for_intrinsics(const Klass* holder); 837 838 bool caller_sensitive() { 839 return (_flags & _caller_sensitive) != 0; 840 } 841 void set_caller_sensitive(bool x) { 842 _flags = x ? (_flags | _caller_sensitive) : (_flags & ~_caller_sensitive); 843 } 844 845 bool force_inline() { 846 return (_flags & _force_inline) != 0; 847 } 848 void set_force_inline(bool x) { 849 _flags = x ? (_flags | _force_inline) : (_flags & ~_force_inline); 850 } 851 852 bool dont_inline() { 853 return (_flags & _dont_inline) != 0; 854 } 855 void set_dont_inline(bool x) { 856 _flags = x ? (_flags | _dont_inline) : (_flags & ~_dont_inline); 857 } 858 859 bool is_hidden() const { 860 return (_flags & _hidden) != 0; 861 } 862 863 void set_hidden(bool x) { 864 _flags = x ? (_flags | _hidden) : (_flags & ~_hidden); 865 } 866 867 bool is_scoped() const { 868 return (_flags & _scoped) != 0; 869 } 870 871 void set_scoped(bool x) { 872 _flags = x ? (_flags | _scoped) : (_flags & ~_scoped); 873 } 874 875 bool intrinsic_candidate() { 876 return (_flags & _intrinsic_candidate) != 0; 877 } 878 void set_intrinsic_candidate(bool x) { 879 _flags = x ? (_flags | _intrinsic_candidate) : (_flags & ~_intrinsic_candidate); 880 } 881 882 bool has_injected_profile() { 883 return (_flags & _has_injected_profile) != 0; 884 } 885 void set_has_injected_profile(bool x) { 886 _flags = x ? (_flags | _has_injected_profile) : (_flags & ~_has_injected_profile); 887 } 888 889 bool has_reserved_stack_access() { 890 return (_flags & _reserved_stack_access) != 0; 891 } 892 893 void set_has_reserved_stack_access(bool x) { 894 _flags = x ? (_flags | _reserved_stack_access) : (_flags & ~_reserved_stack_access); 895 } 896 897 bool has_scalarized_args() const { 898 return (_flags & _scalarized_args) != 0; 899 } 900 901 void set_has_scalarized_args(bool x) { 902 _flags = x ? (_flags | _scalarized_args) : (_flags & ~_scalarized_args); 903 } 904 905 bool is_scalarized_arg(int idx) const; 906 907 bool c1_needs_stack_repair() { 908 return (_flags & _c1_needs_stack_repair) != 0; 909 } 910 911 bool c2_needs_stack_repair() { 912 return (_flags & _c2_needs_stack_repair) != 0; 913 } 914 915 void set_c1_needs_stack_repair(bool x) { 916 _flags = x ? (_flags | _c1_needs_stack_repair) : (_flags & ~_c1_needs_stack_repair); 917 } 918 919 void set_c2_needs_stack_repair(bool x) { 920 _flags = x ? (_flags | _c2_needs_stack_repair) : (_flags & ~_c2_needs_stack_repair); 921 } 922 923 JFR_ONLY(DEFINE_TRACE_FLAG_ACCESSOR;) 924 925 ConstMethod::MethodType method_type() const { 926 return _constMethod->method_type(); 927 } 928 bool is_overpass() const { return method_type() == ConstMethod::OVERPASS; } 929 930 // On-stack replacement support 931 bool has_osr_nmethod(int level, bool match_level) { 932 return method_holder()->lookup_osr_nmethod(this, InvocationEntryBci, level, match_level) != NULL; 933 } 934 935 int mark_osr_nmethods() { 936 return method_holder()->mark_osr_nmethods(this); 937 } 938 939 nmethod* lookup_osr_nmethod_for(int bci, int level, bool match_level) { 940 return method_holder()->lookup_osr_nmethod(this, bci, level, match_level); 941 } 942 943 // Find if klass for method is loaded 944 bool is_klass_loaded_by_klass_index(int klass_index) const; 945 bool is_klass_loaded(int refinfo_index, bool must_be_resolved = false) const; 946 947 // Indicates whether compilation failed earlier for this method, or 948 // whether it is not compilable for another reason like having a 949 // breakpoint set in it. 950 bool is_not_compilable(int comp_level = CompLevel_any) const; 951 void set_not_compilable(const char* reason, int comp_level = CompLevel_all, bool report = true); 952 void set_not_compilable_quietly(const char* reason, int comp_level = CompLevel_all) { 953 set_not_compilable(reason, comp_level, false); 954 } 955 bool is_not_osr_compilable(int comp_level = CompLevel_any) const; 956 void set_not_osr_compilable(const char* reason, int comp_level = CompLevel_all, bool report = true); 957 void set_not_osr_compilable_quietly(const char* reason, int comp_level = CompLevel_all) { 958 set_not_osr_compilable(reason, comp_level, false); 959 } 960 bool is_always_compilable() const; 961 962 private: 963 void print_made_not_compilable(int comp_level, bool is_osr, bool report, const char* reason); 964 965 public: 966 MethodCounters* get_method_counters(Thread* current) { 967 if (_method_counters == NULL) { 968 build_method_counters(current, this); 969 } 970 return _method_counters; 971 } 972 973 bool is_not_c1_compilable() const { return access_flags().is_not_c1_compilable(); } 974 void set_not_c1_compilable() { _access_flags.set_not_c1_compilable(); } 975 void clear_not_c1_compilable() { _access_flags.clear_not_c1_compilable(); } 976 bool is_not_c2_compilable() const { return access_flags().is_not_c2_compilable(); } 977 void set_not_c2_compilable() { _access_flags.set_not_c2_compilable(); } 978 void clear_not_c2_compilable() { _access_flags.clear_not_c2_compilable(); } 979 980 bool is_not_c1_osr_compilable() const { return is_not_c1_compilable(); } // don't waste an accessFlags bit 981 void set_not_c1_osr_compilable() { set_not_c1_compilable(); } // don't waste an accessFlags bit 982 void clear_not_c1_osr_compilable() { clear_not_c1_compilable(); } // don't waste an accessFlags bit 983 bool is_not_c2_osr_compilable() const { return access_flags().is_not_c2_osr_compilable(); } 984 void set_not_c2_osr_compilable() { _access_flags.set_not_c2_osr_compilable(); } 985 void clear_not_c2_osr_compilable() { _access_flags.clear_not_c2_osr_compilable(); } 986 987 // Background compilation support 988 bool queued_for_compilation() const { return access_flags().queued_for_compilation(); } 989 void set_queued_for_compilation() { _access_flags.set_queued_for_compilation(); } 990 void clear_queued_for_compilation() { _access_flags.clear_queued_for_compilation(); } 991 992 // Resolve all classes in signature, return 'true' if successful 993 static bool load_signature_classes(const methodHandle& m, TRAPS); 994 995 // Return if true if not all classes references in signature, including return type, has been loaded 996 static bool has_unloaded_classes_in_signature(const methodHandle& m, TRAPS); 997 998 // Printing 999 void print_short_name(outputStream* st = tty); // prints as klassname::methodname; Exposed so field engineers can debug VM 1000 #if INCLUDE_JVMTI 1001 void print_name(outputStream* st = tty); // prints as "virtual void foo(int)"; exposed for -Xlog:redefine+class 1002 #else 1003 void print_name(outputStream* st = tty) PRODUCT_RETURN; // prints as "virtual void foo(int)" 1004 #endif 1005 1006 typedef int (*method_comparator_func)(Method* a, Method* b); 1007 1008 // Helper routine used for method sorting 1009 static void sort_methods(Array<Method*>* methods, bool set_idnums = true, method_comparator_func func = NULL); 1010 1011 // Deallocation function for redefine classes or if an error occurs 1012 void deallocate_contents(ClassLoaderData* loader_data); 1013 1014 void release_C_heap_structures(); 1015 1016 Method* get_new_method() const { 1017 InstanceKlass* holder = method_holder(); 1018 Method* new_method = holder->method_with_idnum(orig_method_idnum()); 1019 1020 assert(new_method != NULL, "method_with_idnum() should not be NULL"); 1021 assert(this != new_method, "sanity check"); 1022 return new_method; 1023 } 1024 1025 // Printing 1026 #ifndef PRODUCT 1027 void print_on(outputStream* st) const; 1028 #endif 1029 void print_value_on(outputStream* st) const; 1030 void print_linkage_flags(outputStream* st) PRODUCT_RETURN; 1031 1032 const char* internal_name() const { return "{method}"; } 1033 1034 // Check for valid method pointer 1035 static bool has_method_vptr(const void* ptr); 1036 static bool is_valid_method(const Method* m); 1037 1038 // Verify 1039 void verify() { verify_on(tty); } 1040 void verify_on(outputStream* st); 1041 1042 private: 1043 1044 // Inlined elements 1045 address* native_function_addr() const { assert(is_native(), "must be native"); return (address*) (this+1); } 1046 address* signature_handler_addr() const { return native_function_addr() + 1; } 1047 }; 1048 1049 1050 // Utility class for compressing line number tables 1051 1052 class CompressedLineNumberWriteStream: public CompressedWriteStream { 1053 private: 1054 int _bci; 1055 int _line; 1056 public: 1057 // Constructor 1058 CompressedLineNumberWriteStream(int initial_size) : CompressedWriteStream(initial_size), _bci(0), _line(0) {} 1059 CompressedLineNumberWriteStream(u_char* buffer, int initial_size) : CompressedWriteStream(buffer, initial_size), _bci(0), _line(0) {} 1060 1061 // Write (bci, line number) pair to stream 1062 void write_pair_regular(int bci_delta, int line_delta); 1063 1064 // If (bci delta, line delta) fits in (5-bit unsigned, 3-bit unsigned) 1065 // we save it as one byte, otherwise we write a 0xFF escape character 1066 // and use regular compression. 0x0 is used as end-of-stream terminator. 1067 void write_pair_inline(int bci, int line); 1068 1069 void write_pair(int bci, int line); 1070 1071 // Write end-of-stream marker 1072 void write_terminator() { write_byte(0); } 1073 }; 1074 1075 1076 // Utility class for decompressing line number tables 1077 1078 class CompressedLineNumberReadStream: public CompressedReadStream { 1079 private: 1080 int _bci; 1081 int _line; 1082 public: 1083 // Constructor 1084 CompressedLineNumberReadStream(u_char* buffer); 1085 // Read (bci, line number) pair from stream. Returns false at end-of-stream. 1086 bool read_pair(); 1087 // Accessing bci and line number (after calling read_pair) 1088 int bci() const { return _bci; } 1089 int line() const { return _line; } 1090 }; 1091 1092 1093 #if INCLUDE_JVMTI 1094 1095 /// Fast Breakpoints. 1096 1097 // If this structure gets more complicated (because bpts get numerous), 1098 // move it into its own header. 1099 1100 // There is presently no provision for concurrent access 1101 // to breakpoint lists, which is only OK for JVMTI because 1102 // breakpoints are written only at safepoints, and are read 1103 // concurrently only outside of safepoints. 1104 1105 class BreakpointInfo : public CHeapObj<mtClass> { 1106 friend class VMStructs; 1107 private: 1108 Bytecodes::Code _orig_bytecode; 1109 int _bci; 1110 u2 _name_index; // of method 1111 u2 _signature_index; // of method 1112 BreakpointInfo* _next; // simple storage allocation 1113 1114 public: 1115 BreakpointInfo(Method* m, int bci); 1116 1117 // accessors 1118 Bytecodes::Code orig_bytecode() { return _orig_bytecode; } 1119 void set_orig_bytecode(Bytecodes::Code code) { _orig_bytecode = code; } 1120 int bci() { return _bci; } 1121 1122 BreakpointInfo* next() const { return _next; } 1123 void set_next(BreakpointInfo* n) { _next = n; } 1124 1125 // helps for searchers 1126 bool match(const Method* m, int bci) { 1127 return bci == _bci && match(m); 1128 } 1129 1130 bool match(const Method* m) { 1131 return _name_index == m->name_index() && 1132 _signature_index == m->signature_index(); 1133 } 1134 1135 void set(Method* method); 1136 void clear(Method* method); 1137 }; 1138 1139 #endif // INCLUDE_JVMTI 1140 1141 // Utility class for access exception handlers 1142 class ExceptionTable : public StackObj { 1143 private: 1144 ExceptionTableElement* _table; 1145 u2 _length; 1146 1147 public: 1148 ExceptionTable(const Method* m) { 1149 if (m->has_exception_handler()) { 1150 _table = m->exception_table_start(); 1151 _length = m->exception_table_length(); 1152 } else { 1153 _table = NULL; 1154 _length = 0; 1155 } 1156 } 1157 1158 int length() const { 1159 return _length; 1160 } 1161 1162 u2 start_pc(int idx) const { 1163 assert(idx < _length, "out of bounds"); 1164 return _table[idx].start_pc; 1165 } 1166 1167 void set_start_pc(int idx, u2 value) { 1168 assert(idx < _length, "out of bounds"); 1169 _table[idx].start_pc = value; 1170 } 1171 1172 u2 end_pc(int idx) const { 1173 assert(idx < _length, "out of bounds"); 1174 return _table[idx].end_pc; 1175 } 1176 1177 void set_end_pc(int idx, u2 value) { 1178 assert(idx < _length, "out of bounds"); 1179 _table[idx].end_pc = value; 1180 } 1181 1182 u2 handler_pc(int idx) const { 1183 assert(idx < _length, "out of bounds"); 1184 return _table[idx].handler_pc; 1185 } 1186 1187 void set_handler_pc(int idx, u2 value) { 1188 assert(idx < _length, "out of bounds"); 1189 _table[idx].handler_pc = value; 1190 } 1191 1192 u2 catch_type_index(int idx) const { 1193 assert(idx < _length, "out of bounds"); 1194 return _table[idx].catch_type_index; 1195 } 1196 1197 void set_catch_type_index(int idx, u2 value) { 1198 assert(idx < _length, "out of bounds"); 1199 _table[idx].catch_type_index = value; 1200 } 1201 }; 1202 1203 #endif // SHARE_OOPS_METHOD_HPP