Path: blob/master/src/hotspot/share/oops/cpCache.hpp
40951 views
/*1* Copyright (c) 1998, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_OOPS_CPCACHE_HPP25#define SHARE_OOPS_CPCACHE_HPP2627#include "interpreter/bytecodes.hpp"28#include "memory/allocation.hpp"29#include "oops/array.hpp"30#include "oops/oopHandle.hpp"31#include "runtime/handles.hpp"32#include "utilities/align.hpp"33#include "utilities/constantTag.hpp"34#include "utilities/growableArray.hpp"3536class PSPromotionManager;3738// The ConstantPoolCache is not a cache! It is the resolution table that the39// interpreter uses to avoid going into the runtime and a way to access resolved40// values.4142// A ConstantPoolCacheEntry describes an individual entry of the constant43// pool cache. There's 2 principal kinds of entries: field entries for in-44// stance & static field access, and method entries for invokes. Some of45// the entry layout is shared and looks as follows:46//47// bit number |31 0|48// bit length |-8--|-8--|---16----|49// --------------------------------50// _indices [ b2 | b1 | index ] index = constant_pool_index51// _f1 [ entry specific ] metadata ptr (method or klass)52// _f2 [ entry specific ] vtable or res_ref index, or vfinal method ptr53// _flags [tos|0|F=1|0|0|0|f|v|0 |0000|field_index] (for field entries)54// bit length [ 4 |1| 1 |1|1|1|1|1|1 |1 |-3-|----16-----]55// _flags [tos|0|F=0|S|A|I|f|0|vf|indy_rf|000|00000|psize] (for method entries)56// bit length [ 4 |1| 1 |1|1|1|1|1|1 |-4--|--8--|--8--]5758// --------------------------------59//60// with:61// index = original constant pool index62// b1 = bytecode 163// b2 = bytecode 264// psize = parameters size (method entries only)65// field_index = index into field information in holder InstanceKlass66// The index max is 0xffff (max number of fields in constant pool)67// and is multiplied by (InstanceKlass::next_offset) when accessing.68// tos = TosState69// F = the entry is for a field (or F=0 for a method)70// A = call site has an appendix argument (loaded from resolved references)71// I = interface call is forced virtual (must use a vtable index or vfinal)72// f = field or method is final73// v = field is volatile74// vf = virtual but final (method entries only: is_vfinal())75// indy_rf = call site specifier method resolution failed76//77// The flags after TosState have the following interpretation:78// bit 27: 0 for fields, 1 for methods79// f flag true if field is marked final80// v flag true if field is volatile (only for fields)81// f2 flag true if f2 contains an oop (e.g., virtual final method)82// fv flag true if invokeinterface used for method in class Object83//84// The flags 31, 30, 29, 28 together build a 4 bit number 0 to 16 with the85// following mapping to the TosState states:86//87// btos: 088// ztos: 189// ctos: 290// stos: 391// itos: 492// ltos: 593// ftos: 694// dtos: 795// atos: 896// vtos: 997//98// Entry specific: field entries:99// _indices = get (b1 section) and put (b2 section) bytecodes, original constant pool index100// _f1 = field holder (as a java.lang.Class, not a Klass*)101// _f2 = field offset in bytes102// _flags = field type information, original FieldInfo index in field holder103// (field_index section)104//105// Entry specific: method entries:106// _indices = invoke code for f1 (b1 section), invoke code for f2 (b2 section),107// original constant pool index108// _f1 = Method* for non-virtual calls, unused by virtual calls.109// for interface calls, which are essentially virtual but need a klass,110// contains Klass* for the corresponding interface.111// for invokedynamic and invokehandle, f1 contains the adapter method which112// manages the actual call. The appendix is stored in the ConstantPool113// resolved_references array.114// (upcoming metadata changes will move the appendix to a separate array)115// _f2 = vtable/itable index (or final Method*) for virtual calls only,116// unused by non-virtual. The is_vfinal flag indicates this is a117// method pointer for a final method, not an index.118// _flags = has local signature (MHs and indy),119// virtual final bit (vfinal),120// parameter size (psize section)121//122// Note: invokevirtual & invokespecial bytecodes can share the same constant123// pool entry and thus the same constant pool cache entry. All invoke124// bytecodes but invokevirtual use only _f1 and the corresponding b1125// bytecode, while invokevirtual uses only _f2 and the corresponding126// b2 bytecode. The value of _flags is shared for both types of entries.127//128// The fields are volatile so that they are stored in the order written in the129// source code. The _indices field with the bytecode must be written last.130131class CallInfo;132133class ConstantPoolCacheEntry {134friend class VMStructs;135friend class constantPoolCacheKlass;136friend class ConstantPool;137friend class InterpreterRuntime;138139private:140volatile intx _indices; // constant pool index & rewrite bytecodes141Metadata* volatile _f1; // entry specific metadata field142volatile intx _f2; // entry specific int/metadata field143volatile intx _flags; // flags144145146void set_bytecode_1(Bytecodes::Code code);147void set_bytecode_2(Bytecodes::Code code);148void set_f1(Metadata* f1) {149Metadata* existing_f1 = _f1; // read once150assert(existing_f1 == NULL || existing_f1 == f1, "illegal field change");151_f1 = f1;152}153void release_set_f1(Metadata* f1);154void set_f2(intx f2) {155intx existing_f2 = _f2; // read once156assert(existing_f2 == 0 || existing_f2 == f2, "illegal field change");157_f2 = f2;158}159void set_f2_as_vfinal_method(Method* f2) {160assert(is_vfinal(), "flags must be set");161set_f2((intx)f2);162}163int make_flags(TosState state, int option_bits, int field_index_or_method_params);164void set_flags(intx flags) { _flags = flags; }165void set_field_flags(TosState field_type, int option_bits, int field_index) {166assert((field_index & field_index_mask) == field_index, "field_index in range");167set_flags(make_flags(field_type, option_bits | (1 << is_field_entry_shift), field_index));168}169void set_method_flags(TosState return_type, int option_bits, int method_params) {170assert((method_params & parameter_size_mask) == method_params, "method_params in range");171set_flags(make_flags(return_type, option_bits, method_params));172}173174public:175// specific bit definitions for the flags field:176// (Note: the interpreter must use these definitions to access the CP cache.)177enum {178// high order bits are the TosState corresponding to field type or method return type179tos_state_bits = 4,180tos_state_mask = right_n_bits(tos_state_bits),181tos_state_shift = BitsPerInt - tos_state_bits, // see verify_tos_state_shift below182// misc. option bits; can be any bit position in [16..27]183is_field_entry_shift = 26, // (F) is it a field or a method?184has_local_signature_shift = 25, // (S) does the call site have a per-site signature (sig-poly methods)?185has_appendix_shift = 24, // (A) does the call site have an appendix argument?186is_forced_virtual_shift = 23, // (I) is the interface reference forced to virtual mode?187is_final_shift = 22, // (f) is the field or method final?188is_volatile_shift = 21, // (v) is the field volatile?189is_vfinal_shift = 20, // (vf) did the call resolve to a final method?190indy_resolution_failed_shift= 19, // (indy_rf) did call site specifier resolution fail ?191// low order bits give field index (for FieldInfo) or method parameter size:192field_index_bits = 16,193field_index_mask = right_n_bits(field_index_bits),194parameter_size_bits = 8, // subset of field_index_mask, range is 0..255195parameter_size_mask = right_n_bits(parameter_size_bits),196option_bits_mask = ~(((~0u) << tos_state_shift) | (field_index_mask | parameter_size_mask))197};198199// specific bit definitions for the indices field:200enum {201cp_index_bits = 2*BitsPerByte,202cp_index_mask = right_n_bits(cp_index_bits),203bytecode_1_shift = cp_index_bits,204bytecode_1_mask = right_n_bits(BitsPerByte), // == (u1)0xFF205bytecode_2_shift = cp_index_bits + BitsPerByte,206bytecode_2_mask = right_n_bits(BitsPerByte) // == (u1)0xFF207};208209210// Initialization211void initialize_entry(int original_index); // initialize primary entry212void initialize_resolved_reference_index(int ref_index) {213assert(_f2 == 0, "set once"); // note: ref_index might be zero also214_f2 = ref_index;215}216217void set_field( // sets entry to resolved field state218Bytecodes::Code get_code, // the bytecode used for reading the field219Bytecodes::Code put_code, // the bytecode used for writing the field220Klass* field_holder, // the object/klass holding the field221int orig_field_index, // the original field index in the field holder222int field_offset, // the field offset in words in the field holder223TosState field_type, // the (machine) field type224bool is_final, // the field is final225bool is_volatile // the field is volatile226);227228private:229void set_direct_or_vtable_call(230Bytecodes::Code invoke_code, // the bytecode used for invoking the method231const methodHandle& method, // the method/prototype if any (NULL, otherwise)232int vtable_index, // the vtable index if any, else negative233bool sender_is_interface234);235236public:237void set_direct_call( // sets entry to exact concrete method entry238Bytecodes::Code invoke_code, // the bytecode used for invoking the method239const methodHandle& method, // the method to call240bool sender_is_interface241);242243void set_vtable_call( // sets entry to vtable index244Bytecodes::Code invoke_code, // the bytecode used for invoking the method245const methodHandle& method, // resolved method which declares the vtable index246int vtable_index // the vtable index247);248249void set_itable_call(250Bytecodes::Code invoke_code, // the bytecode used; must be invokeinterface251Klass* referenced_klass, // the referenced klass in the InterfaceMethodref252const methodHandle& method, // the resolved interface method253int itable_index // index into itable for the method254);255256void set_method_handle(257const constantPoolHandle& cpool, // holding constant pool (required for locking)258const CallInfo &call_info // Call link information259);260261void set_dynamic_call(262const constantPoolHandle& cpool, // holding constant pool (required for locking)263const CallInfo &call_info // Call link information264);265266// Common code for invokedynamic and MH invocations.267268// The "appendix" is an optional call-site-specific parameter which is269// pushed by the JVM at the end of the argument list. This argument may270// be a MethodType for the MH.invokes and a CallSite for an invokedynamic271// instruction. However, its exact type and use depends on the Java upcall,272// which simply returns a compiled LambdaForm along with any reference273// that LambdaForm needs to complete the call. If the upcall returns a274// null appendix, the argument is not passed at all.275//276// The appendix is *not* represented in the signature of the symbolic277// reference for the call site, but (if present) it *is* represented in278// the Method* bound to the site. This means that static and dynamic279// resolution logic needs to make slightly different assessments about the280// number and types of arguments.281void set_method_handle_common(282const constantPoolHandle& cpool, // holding constant pool (required for locking)283Bytecodes::Code invoke_code, // _invokehandle or _invokedynamic284const CallInfo &call_info // Call link information285);286287// Return TRUE if resolution failed and this thread got to record the failure288// status. Return FALSE if another thread succeeded or failed in resolving289// the method and recorded the success or failure before this thread had a290// chance to record its failure.291bool save_and_throw_indy_exc(const constantPoolHandle& cpool, int cpool_index,292int index, constantTag tag, TRAPS);293294// invokedynamic and invokehandle call sites have an "appendix" item in the295// resolved references array.296Method* method_if_resolved(const constantPoolHandle& cpool);297oop appendix_if_resolved(const constantPoolHandle& cpool);298299void set_parameter_size(int value);300301// Which bytecode number (1 or 2) in the index field is valid for this bytecode?302// Returns -1 if neither is valid.303static int bytecode_number(Bytecodes::Code code) {304switch (code) {305case Bytecodes::_getstatic : // fall through306case Bytecodes::_getfield : // fall through307case Bytecodes::_invokespecial : // fall through308case Bytecodes::_invokestatic : // fall through309case Bytecodes::_invokehandle : // fall through310case Bytecodes::_invokedynamic : // fall through311case Bytecodes::_invokeinterface : return 1;312case Bytecodes::_putstatic : // fall through313case Bytecodes::_putfield : // fall through314case Bytecodes::_invokevirtual : return 2;315default : break;316}317return -1;318}319320// Has this bytecode been resolved? Only valid for invokes and get/put field/static.321bool is_resolved(Bytecodes::Code code) const;322323// Accessors324int indices() const { return _indices; }325int indices_ord() const;326int constant_pool_index() const { return (indices() & cp_index_mask); }327Bytecodes::Code bytecode_1() const;328Bytecodes::Code bytecode_2() const;329Metadata* f1_ord() const;330Method* f1_as_method() const;331Klass* f1_as_klass() const;332// Use the accessor f1() to acquire _f1's value. This is needed for333// example in BytecodeInterpreter::run(), where is_f1_null() is334// called to check if an invokedynamic call is resolved. This load335// of _f1 must be ordered with the loads performed by336// cache->main_entry_index().337bool is_f1_null() const; // classifies a CPC entry as unbound338int f2_as_index() const { assert(!is_vfinal(), ""); return (int) _f2; }339Method* f2_as_vfinal_method() const { assert(is_vfinal(), ""); return (Method*)_f2; }340Method* f2_as_interface_method() const;341intx flags_ord() const;342int field_index() const { assert(is_field_entry(), ""); return (_flags & field_index_mask); }343int parameter_size() const { assert(is_method_entry(), ""); return (_flags & parameter_size_mask); }344bool is_volatile() const { return (_flags & (1 << is_volatile_shift)) != 0; }345bool is_final() const { return (_flags & (1 << is_final_shift)) != 0; }346bool is_forced_virtual() const { return (_flags & (1 << is_forced_virtual_shift)) != 0; }347bool is_vfinal() const { return (_flags & (1 << is_vfinal_shift)) != 0; }348bool indy_resolution_failed() const;349bool has_appendix() const;350bool has_local_signature() const;351bool is_method_entry() const { return (_flags & (1 << is_field_entry_shift)) == 0; }352bool is_field_entry() const { return (_flags & (1 << is_field_entry_shift)) != 0; }353bool is_long() const { return flag_state() == ltos; }354bool is_double() const { return flag_state() == dtos; }355TosState flag_state() const { assert((uint)number_of_states <= (uint)tos_state_mask+1, "");356return (TosState)((_flags >> tos_state_shift) & tos_state_mask); }357void set_indy_resolution_failed();358359// Code generation support360static WordSize size() {361return in_WordSize(align_up((int)sizeof(ConstantPoolCacheEntry), wordSize) / wordSize);362}363static ByteSize size_in_bytes() { return in_ByteSize(sizeof(ConstantPoolCacheEntry)); }364static ByteSize indices_offset() { return byte_offset_of(ConstantPoolCacheEntry, _indices); }365static ByteSize f1_offset() { return byte_offset_of(ConstantPoolCacheEntry, _f1); }366static ByteSize f2_offset() { return byte_offset_of(ConstantPoolCacheEntry, _f2); }367static ByteSize flags_offset() { return byte_offset_of(ConstantPoolCacheEntry, _flags); }368369#if INCLUDE_JVMTI370// RedefineClasses() API support:371// If this ConstantPoolCacheEntry refers to old_method then update it372// to refer to new_method.373// trace_name_printed is set to true if the current call has374// printed the klass name so that other routines in the adjust_*375// group don't print the klass name.376void adjust_method_entry(Method* old_method, Method* new_method,377bool* trace_name_printed);378bool check_no_old_or_obsolete_entries();379Method* get_interesting_method_entry();380#endif // INCLUDE_JVMTI381382// Debugging & Printing383void print (outputStream* st, int index) const;384void verify(outputStream* st) const;385386static void verify_tos_state_shift() {387// When shifting flags as a 32-bit int, make sure we don't need an extra mask for tos_state:388assert((((u4)-1 >> tos_state_shift) & ~tos_state_mask) == 0, "no need for tos_state mask");389}390391void verify_just_initialized(bool f2_used);392void reinitialize(bool f2_used);393};394395396// A constant pool cache is a runtime data structure set aside to a constant pool. The cache397// holds interpreter runtime information for all field access and invoke bytecodes. The cache398// is created and initialized before a class is actively used (i.e., initialized), the indivi-399// dual cache entries are filled at resolution (i.e., "link") time (see also: rewriter.*).400401class ConstantPoolCache: public MetaspaceObj {402friend class VMStructs;403friend class MetadataFactory;404private:405// If you add a new field that points to any metaspace object, you406// must add this field to ConstantPoolCache::metaspace_pointers_do().407int _length;408ConstantPool* _constant_pool; // the corresponding constant pool409410// The following fields need to be modified at runtime, so they cannot be411// stored in the ConstantPool, which is read-only.412// Array of resolved objects from the constant pool and map from resolved413// object index to original constant pool index414OopHandle _resolved_references;415Array<u2>* _reference_map;416// The narrowOop pointer to the archived resolved_references. Set at CDS dump417// time when caching java heap object is supported.418CDS_JAVA_HEAP_ONLY(int _archived_references_index;)419420// Sizing421debug_only(friend class ClassVerifier;)422423// Constructor424ConstantPoolCache(int length,425const intStack& inverse_index_map,426const intStack& invokedynamic_inverse_index_map,427const intStack& invokedynamic_references_map);428429// Initialization430void initialize(const intArray& inverse_index_map,431const intArray& invokedynamic_inverse_index_map,432const intArray& invokedynamic_references_map);433public:434static ConstantPoolCache* allocate(ClassLoaderData* loader_data,435const intStack& cp_cache_map,436const intStack& invokedynamic_cp_cache_map,437const intStack& invokedynamic_references_map, TRAPS);438439int length() const { return _length; }440void metaspace_pointers_do(MetaspaceClosure* it);441MetaspaceObj::Type type() const { return ConstantPoolCacheType; }442443oop archived_references() NOT_CDS_JAVA_HEAP_RETURN_(NULL);444void set_archived_references(oop o) NOT_CDS_JAVA_HEAP_RETURN;445void clear_archived_references() NOT_CDS_JAVA_HEAP_RETURN;446447inline oop resolved_references();448void set_resolved_references(OopHandle s) { _resolved_references = s; }449Array<u2>* reference_map() const { return _reference_map; }450void set_reference_map(Array<u2>* o) { _reference_map = o; }451452// Assembly code support453static int resolved_references_offset_in_bytes() { return offset_of(ConstantPoolCache, _resolved_references); }454455// CDS support456void remove_unshareable_info();457void verify_just_initialized();458private:459void walk_entries_for_initialization(bool check_only);460void set_length(int length) { _length = length; }461462static int header_size() { return sizeof(ConstantPoolCache) / wordSize; }463static int size(int length) { return align_metadata_size(header_size() + length * in_words(ConstantPoolCacheEntry::size())); }464public:465int size() const { return size(length()); }466private:467468// Helpers469ConstantPool** constant_pool_addr() { return &_constant_pool; }470ConstantPoolCacheEntry* base() const { return (ConstantPoolCacheEntry*)((address)this + in_bytes(base_offset())); }471472friend class constantPoolCacheKlass;473friend class ConstantPoolCacheEntry;474475public:476// Accessors477void set_constant_pool(ConstantPool* pool) { _constant_pool = pool; }478ConstantPool* constant_pool() const { return _constant_pool; }479// Fetches the entry at the given index.480// In either case the index must not be encoded or byte-swapped in any way.481ConstantPoolCacheEntry* entry_at(int i) const {482assert(0 <= i && i < length(), "index out of bounds");483return base() + i;484}485486// Code generation487static ByteSize base_offset() { return in_ByteSize(sizeof(ConstantPoolCache)); }488static ByteSize entry_offset(int raw_index) {489int index = raw_index;490return (base_offset() + ConstantPoolCacheEntry::size_in_bytes() * index);491}492493#if INCLUDE_JVMTI494// RedefineClasses() API support:495// If any entry of this ConstantPoolCache points to any of496// old_methods, replace it with the corresponding new_method.497// trace_name_printed is set to true if the current call has498// printed the klass name so that other routines in the adjust_*499// group don't print the klass name.500void adjust_method_entries(bool* trace_name_printed);501bool check_no_old_or_obsolete_entries();502void dump_cache();503#endif // INCLUDE_JVMTI504505// RedefineClasses support506DEBUG_ONLY(bool on_stack() { return false; })507void deallocate_contents(ClassLoaderData* data);508bool is_klass() const { return false; }509510// Printing511void print_on(outputStream* st) const;512void print_value_on(outputStream* st) const;513514const char* internal_name() const { return "{constant pool cache}"; }515516// Verify517void verify_on(outputStream* st);518};519520#endif // SHARE_OOPS_CPCACHE_HPP521522523