Path: blob/master/src/hotspot/share/oops/constantPool.hpp
40951 views
/*1* Copyright (c) 1997, 2021, 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_CONSTANTPOOL_HPP25#define SHARE_OOPS_CONSTANTPOOL_HPP2627#include "memory/allocation.hpp"28#include "oops/arrayOop.hpp"29#include "oops/cpCache.hpp"30#include "oops/objArrayOop.hpp"31#include "oops/oopHandle.hpp"32#include "oops/symbol.hpp"33#include "oops/typeArrayOop.hpp"34#include "runtime/handles.hpp"35#include "runtime/thread.hpp"36#include "utilities/align.hpp"37#include "utilities/bytes.hpp"38#include "utilities/constantTag.hpp"3940// A ConstantPool is an array containing class constants as described in the41// class file.42//43// Most of the constant pool entries are written during class parsing, which44// is safe. For klass types, the constant pool entry is45// modified when the entry is resolved. If a klass constant pool46// entry is read without a lock, only the resolved state guarantees that47// the entry in the constant pool is a klass object and not a Symbol*.4849class SymbolHashMap;5051class CPSlot {52friend class ConstantPool;53intptr_t _ptr;54enum TagBits {_pseudo_bit = 1};55public:5657CPSlot(intptr_t ptr): _ptr(ptr) {}58CPSlot(Symbol* ptr, int tag_bits = 0): _ptr((intptr_t)ptr | tag_bits) {}5960intptr_t value() { return _ptr; }6162Symbol* get_symbol() {63return (Symbol*)(_ptr & ~_pseudo_bit);64}65};6667// This represents a JVM_CONSTANT_Class, JVM_CONSTANT_UnresolvedClass, or68// JVM_CONSTANT_UnresolvedClassInError slot in the constant pool.69class CPKlassSlot {70// cp->symbol_at(_name_index) gives the name of the class.71int _name_index;7273// cp->_resolved_klasses->at(_resolved_klass_index) gives the Klass* for the class.74int _resolved_klass_index;75public:76enum {77// This is used during constant pool merging where the resolved klass index is78// not yet known, and will be computed at a later stage (during a call to79// initialize_unresolved_klasses()).80_temp_resolved_klass_index = 0xffff81};82CPKlassSlot(int n, int rk) {83_name_index = n;84_resolved_klass_index = rk;85}86int name_index() const {87return _name_index;88}89int resolved_klass_index() const {90assert(_resolved_klass_index != _temp_resolved_klass_index, "constant pool merging was incomplete");91return _resolved_klass_index;92}93};9495class ConstantPool : public Metadata {96friend class VMStructs;97friend class JVMCIVMStructs;98friend class BytecodeInterpreter; // Directly extracts a klass in the pool for fast instanceof/checkcast99friend class Universe; // For null constructor100private:101// If you add a new field that points to any metaspace object, you102// must add this field to ConstantPool::metaspace_pointers_do().103Array<u1>* _tags; // the tag array describing the constant pool's contents104ConstantPoolCache* _cache; // the cache holding interpreter runtime information105InstanceKlass* _pool_holder; // the corresponding class106Array<u2>* _operands; // for variable-sized (InvokeDynamic) nodes, usually empty107108// Consider using an array of compressed klass pointers to109// save space on 64-bit platforms.110Array<Klass*>* _resolved_klasses;111112u2 _major_version; // major version number of class file113u2 _minor_version; // minor version number of class file114115// Constant pool index to the utf8 entry of the Generic signature,116// or 0 if none.117u2 _generic_signature_index;118// Constant pool index to the utf8 entry for the name of source file119// containing this klass, 0 if not specified.120u2 _source_file_name_index;121122enum {123_has_preresolution = 1, // Flags124_on_stack = 2,125_is_shared = 4,126_has_dynamic_constant = 8127};128129u2 _flags; // old fashioned bit twiddling130131int _length; // number of elements in the array132133union {134// set for CDS to restore resolved references135int _resolved_reference_length;136// keeps version number for redefined classes (used in backtrace)137int _version;138} _saved;139140void set_tags(Array<u1>* tags) { _tags = tags; }141void tag_at_put(int which, jbyte t) { tags()->at_put(which, t); }142void release_tag_at_put(int which, jbyte t) { tags()->release_at_put(which, t); }143144u1* tag_addr_at(int which) const { return tags()->adr_at(which); }145146void set_operands(Array<u2>* operands) { _operands = operands; }147148u2 flags() const { return _flags; }149void set_flags(u2 f) { _flags = f; }150151private:152intptr_t* base() const { return (intptr_t*) (((char*) this) + sizeof(ConstantPool)); }153154CPSlot slot_at(int which) const;155156void slot_at_put(int which, CPSlot s) const {157assert(is_within_bounds(which), "index out of bounds");158assert(s.value() != 0, "Caught something");159*(intptr_t*)&base()[which] = s.value();160}161intptr_t* obj_at_addr(int which) const {162assert(is_within_bounds(which), "index out of bounds");163return (intptr_t*) &base()[which];164}165166jint* int_at_addr(int which) const {167assert(is_within_bounds(which), "index out of bounds");168return (jint*) &base()[which];169}170171jlong* long_at_addr(int which) const {172assert(is_within_bounds(which), "index out of bounds");173return (jlong*) &base()[which];174}175176jfloat* float_at_addr(int which) const {177assert(is_within_bounds(which), "index out of bounds");178return (jfloat*) &base()[which];179}180181jdouble* double_at_addr(int which) const {182assert(is_within_bounds(which), "index out of bounds");183return (jdouble*) &base()[which];184}185186ConstantPool(Array<u1>* tags);187ConstantPool() { assert(DumpSharedSpaces || UseSharedSpaces, "only for CDS"); }188public:189static ConstantPool* allocate(ClassLoaderData* loader_data, int length, TRAPS);190191virtual bool is_constantPool() const { return true; }192193Array<u1>* tags() const { return _tags; }194Array<u2>* operands() const { return _operands; }195196bool has_preresolution() const { return (_flags & _has_preresolution) != 0; }197void set_has_preresolution() {198assert(!is_shared(), "should never be called on shared ConstantPools");199_flags |= _has_preresolution;200}201202// minor and major version numbers of class file203u2 major_version() const { return _major_version; }204void set_major_version(u2 major_version) { _major_version = major_version; }205u2 minor_version() const { return _minor_version; }206void set_minor_version(u2 minor_version) { _minor_version = minor_version; }207208// generics support209Symbol* generic_signature() const {210return (_generic_signature_index == 0) ?211(Symbol*)NULL : symbol_at(_generic_signature_index);212}213u2 generic_signature_index() const { return _generic_signature_index; }214void set_generic_signature_index(u2 sig_index) { _generic_signature_index = sig_index; }215216// source file name217Symbol* source_file_name() const {218return (_source_file_name_index == 0) ?219(Symbol*)NULL : symbol_at(_source_file_name_index);220}221u2 source_file_name_index() const { return _source_file_name_index; }222void set_source_file_name_index(u2 sourcefile_index) { _source_file_name_index = sourcefile_index; }223224void copy_fields(const ConstantPool* orig);225226// Redefine classes support. If a method refering to this constant pool227// is on the executing stack, or as a handle in vm code, this constant pool228// can't be removed from the set of previous versions saved in the instance229// class.230bool on_stack() const { return (_flags &_on_stack) != 0; }231void set_on_stack(const bool value);232233// Faster than MetaspaceObj::is_shared() - used by set_on_stack()234bool is_shared() const { return (_flags & _is_shared) != 0; }235236bool has_dynamic_constant() const { return (_flags & _has_dynamic_constant) != 0; }237void set_has_dynamic_constant() { _flags |= _has_dynamic_constant; }238239// Klass holding pool240InstanceKlass* pool_holder() const { return _pool_holder; }241void set_pool_holder(InstanceKlass* k) { _pool_holder = k; }242InstanceKlass** pool_holder_addr() { return &_pool_holder; }243244// Interpreter runtime support245ConstantPoolCache* cache() const { return _cache; }246void set_cache(ConstantPoolCache* cache){ _cache = cache; }247248virtual void metaspace_pointers_do(MetaspaceClosure* iter);249virtual MetaspaceObj::Type type() const { return ConstantPoolType; }250251// Create object cache in the constant pool252void initialize_resolved_references(ClassLoaderData* loader_data,253const intStack& reference_map,254int constant_pool_map_length,255TRAPS);256257// resolved strings, methodHandles and callsite objects from the constant pool258objArrayOop resolved_references() const;259objArrayOop resolved_references_or_null() const;260// mapping resolved object array indexes to cp indexes and back.261int object_to_cp_index(int index) { return reference_map()->at(index); }262int cp_to_object_index(int index);263264void set_resolved_klasses(Array<Klass*>* rk) { _resolved_klasses = rk; }265Array<Klass*>* resolved_klasses() const { return _resolved_klasses; }266void allocate_resolved_klasses(ClassLoaderData* loader_data, int num_klasses, TRAPS);267void initialize_unresolved_klasses(ClassLoaderData* loader_data, TRAPS);268269// Invokedynamic indexes.270// They must look completely different from normal indexes.271// The main reason is that byte swapping is sometimes done on normal indexes.272// Finally, it is helpful for debugging to tell the two apart.273static bool is_invokedynamic_index(int i) { return (i < 0); }274static int decode_invokedynamic_index(int i) { assert(is_invokedynamic_index(i), ""); return ~i; }275static int encode_invokedynamic_index(int i) { assert(!is_invokedynamic_index(i), ""); return ~i; }276277278// The invokedynamic points at a CP cache entry. This entry points back279// at the original CP entry (CONSTANT_InvokeDynamic) and also (via f2) at an entry280// in the resolved_references array (which provides the appendix argument).281int invokedynamic_cp_cache_index(int indy_index) const {282assert(is_invokedynamic_index(indy_index), "should be a invokedynamic index");283int cache_index = decode_invokedynamic_index(indy_index);284return cache_index;285}286ConstantPoolCacheEntry* invokedynamic_cp_cache_entry_at(int indy_index) const {287// decode index that invokedynamic points to.288int cp_cache_index = invokedynamic_cp_cache_index(indy_index);289return cache()->entry_at(cp_cache_index);290}291// Given the per-instruction index of an indy instruction, report the292// main constant pool entry for its bootstrap specifier.293// From there, uncached_name/signature_ref_at will get the name/type.294int invokedynamic_bootstrap_ref_index_at(int indy_index) const {295return invokedynamic_cp_cache_entry_at(indy_index)->constant_pool_index();296}297298// Assembly code support299static int tags_offset_in_bytes() { return offset_of(ConstantPool, _tags); }300static int cache_offset_in_bytes() { return offset_of(ConstantPool, _cache); }301static int pool_holder_offset_in_bytes() { return offset_of(ConstantPool, _pool_holder); }302static int resolved_klasses_offset_in_bytes() { return offset_of(ConstantPool, _resolved_klasses); }303304// Storing constants305306// For temporary use while constructing constant pool307void klass_index_at_put(int which, int name_index) {308tag_at_put(which, JVM_CONSTANT_ClassIndex);309*int_at_addr(which) = name_index;310}311312// Hidden class support:313void klass_at_put(int class_index, Klass* k);314315void unresolved_klass_at_put(int which, int name_index, int resolved_klass_index) {316release_tag_at_put(which, JVM_CONSTANT_UnresolvedClass);317318assert((name_index & 0xffff0000) == 0, "must be");319assert((resolved_klass_index & 0xffff0000) == 0, "must be");320*int_at_addr(which) =321build_int_from_shorts((jushort)resolved_klass_index, (jushort)name_index);322}323324void method_handle_index_at_put(int which, int ref_kind, int ref_index) {325tag_at_put(which, JVM_CONSTANT_MethodHandle);326*int_at_addr(which) = ((jint) ref_index<<16) | ref_kind;327}328329void method_type_index_at_put(int which, int ref_index) {330tag_at_put(which, JVM_CONSTANT_MethodType);331*int_at_addr(which) = ref_index;332}333334void dynamic_constant_at_put(int which, int bsms_attribute_index, int name_and_type_index) {335tag_at_put(which, JVM_CONSTANT_Dynamic);336*int_at_addr(which) = ((jint) name_and_type_index<<16) | bsms_attribute_index;337}338339void invoke_dynamic_at_put(int which, int bsms_attribute_index, int name_and_type_index) {340tag_at_put(which, JVM_CONSTANT_InvokeDynamic);341*int_at_addr(which) = ((jint) name_and_type_index<<16) | bsms_attribute_index;342}343344void unresolved_string_at_put(int which, Symbol* s) {345release_tag_at_put(which, JVM_CONSTANT_String);346slot_at_put(which, CPSlot(s));347}348349void int_at_put(int which, jint i) {350tag_at_put(which, JVM_CONSTANT_Integer);351*int_at_addr(which) = i;352}353354void long_at_put(int which, jlong l) {355tag_at_put(which, JVM_CONSTANT_Long);356// *long_at_addr(which) = l;357Bytes::put_native_u8((address)long_at_addr(which), *((u8*) &l));358}359360void float_at_put(int which, jfloat f) {361tag_at_put(which, JVM_CONSTANT_Float);362*float_at_addr(which) = f;363}364365void double_at_put(int which, jdouble d) {366tag_at_put(which, JVM_CONSTANT_Double);367// *double_at_addr(which) = d;368// u8 temp = *(u8*) &d;369Bytes::put_native_u8((address) double_at_addr(which), *((u8*) &d));370}371372Symbol** symbol_at_addr(int which) const {373assert(is_within_bounds(which), "index out of bounds");374return (Symbol**) &base()[which];375}376377void symbol_at_put(int which, Symbol* s) {378assert(s->refcount() != 0, "should have nonzero refcount");379tag_at_put(which, JVM_CONSTANT_Utf8);380*symbol_at_addr(which) = s;381}382383void string_at_put(int which, int obj_index, oop str);384385// For temporary use while constructing constant pool386void string_index_at_put(int which, int string_index) {387tag_at_put(which, JVM_CONSTANT_StringIndex);388*int_at_addr(which) = string_index;389}390391void field_at_put(int which, int class_index, int name_and_type_index) {392tag_at_put(which, JVM_CONSTANT_Fieldref);393*int_at_addr(which) = ((jint) name_and_type_index<<16) | class_index;394}395396void method_at_put(int which, int class_index, int name_and_type_index) {397tag_at_put(which, JVM_CONSTANT_Methodref);398*int_at_addr(which) = ((jint) name_and_type_index<<16) | class_index;399}400401void interface_method_at_put(int which, int class_index, int name_and_type_index) {402tag_at_put(which, JVM_CONSTANT_InterfaceMethodref);403*int_at_addr(which) = ((jint) name_and_type_index<<16) | class_index; // Not so nice404}405406void name_and_type_at_put(int which, int name_index, int signature_index) {407tag_at_put(which, JVM_CONSTANT_NameAndType);408*int_at_addr(which) = ((jint) signature_index<<16) | name_index; // Not so nice409}410411// Tag query412413constantTag tag_at(int which) const { return (constantTag)tags()->at_acquire(which); }414415// Fetching constants416417Klass* klass_at(int which, TRAPS) {418constantPoolHandle h_this(THREAD, this);419return klass_at_impl(h_this, which, THREAD);420}421422CPKlassSlot klass_slot_at(int which) const {423assert(tag_at(which).is_unresolved_klass() || tag_at(which).is_klass(),424"Corrupted constant pool");425int value = *int_at_addr(which);426int name_index = extract_high_short_from_int(value);427int resolved_klass_index = extract_low_short_from_int(value);428return CPKlassSlot(name_index, resolved_klass_index);429}430431Symbol* klass_name_at(int which) const; // Returns the name, w/o resolving.432int klass_name_index_at(int which) const {433return klass_slot_at(which).name_index();434}435436Klass* resolved_klass_at(int which) const; // Used by Compiler437438// RedefineClasses() API support:439Symbol* klass_at_noresolve(int which) { return klass_name_at(which); }440void temp_unresolved_klass_at_put(int which, int name_index) {441// Used only during constant pool merging for class redefinition. The resolved klass index442// will be initialized later by a call to initialize_unresolved_klasses().443unresolved_klass_at_put(which, name_index, CPKlassSlot::_temp_resolved_klass_index);444}445446jint int_at(int which) {447assert(tag_at(which).is_int(), "Corrupted constant pool");448return *int_at_addr(which);449}450451jlong long_at(int which) {452assert(tag_at(which).is_long(), "Corrupted constant pool");453// return *long_at_addr(which);454u8 tmp = Bytes::get_native_u8((address)&base()[which]);455return *((jlong*)&tmp);456}457458jfloat float_at(int which) {459assert(tag_at(which).is_float(), "Corrupted constant pool");460return *float_at_addr(which);461}462463jdouble double_at(int which) {464assert(tag_at(which).is_double(), "Corrupted constant pool");465u8 tmp = Bytes::get_native_u8((address)&base()[which]);466return *((jdouble*)&tmp);467}468469Symbol* symbol_at(int which) const {470assert(tag_at(which).is_utf8(), "Corrupted constant pool");471return *symbol_at_addr(which);472}473474oop string_at(int which, int obj_index, TRAPS) {475constantPoolHandle h_this(THREAD, this);476return string_at_impl(h_this, which, obj_index, THREAD);477}478oop string_at(int which, TRAPS) {479int obj_index = cp_to_object_index(which);480return string_at(which, obj_index, THREAD);481}482483// Version that can be used before string oop array is created.484oop uncached_string_at(int which, TRAPS);485486// only called when we are sure a string entry is already resolved (via an487// earlier string_at call.488oop resolved_string_at(int which) {489assert(tag_at(which).is_string(), "Corrupted constant pool");490// Must do an acquire here in case another thread resolved the klass491// behind our back, lest we later load stale values thru the oop.492// we might want a volatile_obj_at in ObjArrayKlass.493int obj_index = cp_to_object_index(which);494return resolved_references()->obj_at(obj_index);495}496497Symbol* unresolved_string_at(int which) {498assert(tag_at(which).is_string(), "Corrupted constant pool");499Symbol* sym = slot_at(which).get_symbol();500return sym;501}502503// Returns an UTF8 for a CONSTANT_String entry at a given index.504// UTF8 char* representation was chosen to avoid conversion of505// java_lang_Strings at resolved entries into Symbol*s506// or vice versa.507char* string_at_noresolve(int which);508509jint name_and_type_at(int which) {510assert(tag_at(which).is_name_and_type(), "Corrupted constant pool");511return *int_at_addr(which);512}513514int method_handle_ref_kind_at(int which) {515assert(tag_at(which).is_method_handle() ||516tag_at(which).is_method_handle_in_error(), "Corrupted constant pool");517return extract_low_short_from_int(*int_at_addr(which)); // mask out unwanted ref_index bits518}519int method_handle_index_at(int which) {520assert(tag_at(which).is_method_handle() ||521tag_at(which).is_method_handle_in_error(), "Corrupted constant pool");522return extract_high_short_from_int(*int_at_addr(which)); // shift out unwanted ref_kind bits523}524int method_type_index_at(int which) {525assert(tag_at(which).is_method_type() ||526tag_at(which).is_method_type_in_error(), "Corrupted constant pool");527return *int_at_addr(which);528}529530// Derived queries:531Symbol* method_handle_name_ref_at(int which) {532int member = method_handle_index_at(which);533return impl_name_ref_at(member, true);534}535Symbol* method_handle_signature_ref_at(int which) {536int member = method_handle_index_at(which);537return impl_signature_ref_at(member, true);538}539int method_handle_klass_index_at(int which) {540int member = method_handle_index_at(which);541return impl_klass_ref_index_at(member, true);542}543Symbol* method_type_signature_at(int which) {544int sym = method_type_index_at(which);545return symbol_at(sym);546}547548int bootstrap_name_and_type_ref_index_at(int which) {549assert(tag_at(which).has_bootstrap(), "Corrupted constant pool");550return extract_high_short_from_int(*int_at_addr(which));551}552int bootstrap_methods_attribute_index(int which) {553assert(tag_at(which).has_bootstrap(), "Corrupted constant pool");554return extract_low_short_from_int(*int_at_addr(which));555}556int bootstrap_operand_base(int which) {557int bsms_attribute_index = bootstrap_methods_attribute_index(which);558return operand_offset_at(operands(), bsms_attribute_index);559}560// The first part of the operands array consists of an index into the second part.561// Extract a 32-bit index value from the first part.562static int operand_offset_at(Array<u2>* operands, int bsms_attribute_index) {563int n = (bsms_attribute_index * 2);564assert(n >= 0 && n+2 <= operands->length(), "oob");565// The first 32-bit index points to the beginning of the second part566// of the operands array. Make sure this index is in the first part.567DEBUG_ONLY(int second_part = build_int_from_shorts(operands->at(0),568operands->at(1)));569assert(second_part == 0 || n+2 <= second_part, "oob (2)");570int offset = build_int_from_shorts(operands->at(n+0),571operands->at(n+1));572// The offset itself must point into the second part of the array.573assert(offset == 0 || offset >= second_part && offset <= operands->length(), "oob (3)");574return offset;575}576static void operand_offset_at_put(Array<u2>* operands, int bsms_attribute_index, int offset) {577int n = bsms_attribute_index * 2;578assert(n >= 0 && n+2 <= operands->length(), "oob");579operands->at_put(n+0, extract_low_short_from_int(offset));580operands->at_put(n+1, extract_high_short_from_int(offset));581}582static int operand_array_length(Array<u2>* operands) {583if (operands == NULL || operands->length() == 0) return 0;584int second_part = operand_offset_at(operands, 0);585return (second_part / 2);586}587588#ifdef ASSERT589// operand tuples fit together exactly, end to end590static int operand_limit_at(Array<u2>* operands, int bsms_attribute_index) {591int nextidx = bsms_attribute_index + 1;592if (nextidx == operand_array_length(operands))593return operands->length();594else595return operand_offset_at(operands, nextidx);596}597int bootstrap_operand_limit(int which) {598int bsms_attribute_index = bootstrap_methods_attribute_index(which);599return operand_limit_at(operands(), bsms_attribute_index);600}601#endif //ASSERT602603// Layout of InvokeDynamic and Dynamic bootstrap method specifier604// data in second part of operands array. This encodes one record in605// the BootstrapMethods attribute. The whole specifier also includes606// the name and type information from the main constant pool entry.607enum {608_indy_bsm_offset = 0, // CONSTANT_MethodHandle bsm609_indy_argc_offset = 1, // u2 argc610_indy_argv_offset = 2 // u2 argv[argc]611};612613// These functions are used in RedefineClasses for CP merge614615int operand_offset_at(int bsms_attribute_index) {616assert(0 <= bsms_attribute_index &&617bsms_attribute_index < operand_array_length(operands()),618"Corrupted CP operands");619return operand_offset_at(operands(), bsms_attribute_index);620}621int operand_bootstrap_method_ref_index_at(int bsms_attribute_index) {622int offset = operand_offset_at(bsms_attribute_index);623return operands()->at(offset + _indy_bsm_offset);624}625int operand_argument_count_at(int bsms_attribute_index) {626int offset = operand_offset_at(bsms_attribute_index);627int argc = operands()->at(offset + _indy_argc_offset);628return argc;629}630int operand_argument_index_at(int bsms_attribute_index, int j) {631int offset = operand_offset_at(bsms_attribute_index);632return operands()->at(offset + _indy_argv_offset + j);633}634int operand_next_offset_at(int bsms_attribute_index) {635int offset = operand_offset_at(bsms_attribute_index) + _indy_argv_offset636+ operand_argument_count_at(bsms_attribute_index);637return offset;638}639// Compare a bootstrap specifier data in the operands arrays640bool compare_operand_to(int bsms_attribute_index1, const constantPoolHandle& cp2,641int bsms_attribute_index2);642// Find a bootstrap specifier data in the operands array643int find_matching_operand(int bsms_attribute_index, const constantPoolHandle& search_cp,644int operands_cur_len);645// Resize the operands array with delta_len and delta_size646void resize_operands(int delta_len, int delta_size, TRAPS);647// Extend the operands array with the length and size of the ext_cp operands648void extend_operands(const constantPoolHandle& ext_cp, TRAPS);649// Shrink the operands array to a smaller array with new_len length650void shrink_operands(int new_len, TRAPS);651652int bootstrap_method_ref_index_at(int which) {653assert(tag_at(which).has_bootstrap(), "Corrupted constant pool");654int op_base = bootstrap_operand_base(which);655return operands()->at(op_base + _indy_bsm_offset);656}657int bootstrap_argument_count_at(int which) {658assert(tag_at(which).has_bootstrap(), "Corrupted constant pool");659int op_base = bootstrap_operand_base(which);660int argc = operands()->at(op_base + _indy_argc_offset);661DEBUG_ONLY(int end_offset = op_base + _indy_argv_offset + argc;662int next_offset = bootstrap_operand_limit(which));663assert(end_offset == next_offset, "matched ending");664return argc;665}666int bootstrap_argument_index_at(int which, int j) {667int op_base = bootstrap_operand_base(which);668DEBUG_ONLY(int argc = operands()->at(op_base + _indy_argc_offset));669assert((uint)j < (uint)argc, "oob");670return operands()->at(op_base + _indy_argv_offset + j);671}672673// The following methods (name/signature/klass_ref_at, klass_ref_at_noresolve,674// name_and_type_ref_index_at) all expect to be passed indices obtained675// directly from the bytecode.676// If the indices are meant to refer to fields or methods, they are677// actually rewritten constant pool cache indices.678// The routine remap_instruction_operand_from_cache manages the adjustment679// of these values back to constant pool indices.680681// There are also "uncached" versions which do not adjust the operand index; see below.682683// FIXME: Consider renaming these with a prefix "cached_" to make the distinction clear.684// In a few cases (the verifier) there are uses before a cpcache has been built,685// which are handled by a dynamic check in remap_instruction_operand_from_cache.686// FIXME: Remove the dynamic check, and adjust all callers to specify the correct mode.687688// Lookup for entries consisting of (klass_index, name_and_type index)689Klass* klass_ref_at(int which, TRAPS);690Symbol* klass_ref_at_noresolve(int which);691Symbol* name_ref_at(int which) { return impl_name_ref_at(which, false); }692Symbol* signature_ref_at(int which) { return impl_signature_ref_at(which, false); }693694int klass_ref_index_at(int which) { return impl_klass_ref_index_at(which, false); }695int name_and_type_ref_index_at(int which) { return impl_name_and_type_ref_index_at(which, false); }696697int remap_instruction_operand_from_cache(int operand); // operand must be biased by CPCACHE_INDEX_TAG698699constantTag tag_ref_at(int cp_cache_index) { return impl_tag_ref_at(cp_cache_index, false); }700701// Lookup for entries consisting of (name_index, signature_index)702int name_ref_index_at(int which_nt); // == low-order jshort of name_and_type_at(which_nt)703int signature_ref_index_at(int which_nt); // == high-order jshort of name_and_type_at(which_nt)704705BasicType basic_type_for_signature_at(int which) const;706707// Resolve string constants (to prevent allocation during compilation)708void resolve_string_constants(TRAPS) {709constantPoolHandle h_this(THREAD, this);710resolve_string_constants_impl(h_this, CHECK);711}712713// CDS support714void archive_resolved_references() NOT_CDS_JAVA_HEAP_RETURN;715void add_dumped_interned_strings() NOT_CDS_JAVA_HEAP_RETURN;716void resolve_class_constants(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;717void remove_unshareable_info();718void restore_unshareable_info(TRAPS);719// The ConstantPool vtable is restored by this call when the ConstantPool is720// in the shared archive. See patch_klass_vtables() in metaspaceShared.cpp for721// all the gory details. SA, dtrace and pstack helpers distinguish metadata722// by their vtable.723void restore_vtable() { guarantee(is_constantPool(), "vtable restored by this call"); }724725private:726enum { _no_index_sentinel = -1, _possible_index_sentinel = -2 };727public:728729// Get the tag for a constant, which may involve a constant dynamic730constantTag constant_tag_at(int which);731// Get the basic type for a constant, which may involve a constant dynamic732BasicType basic_type_for_constant_at(int which);733734// Resolve late bound constants.735oop resolve_constant_at(int index, TRAPS) {736constantPoolHandle h_this(THREAD, this);737return resolve_constant_at_impl(h_this, index, _no_index_sentinel, NULL, THREAD);738}739740oop resolve_cached_constant_at(int cache_index, TRAPS) {741constantPoolHandle h_this(THREAD, this);742return resolve_constant_at_impl(h_this, _no_index_sentinel, cache_index, NULL, THREAD);743}744745oop resolve_possibly_cached_constant_at(int pool_index, TRAPS) {746constantPoolHandle h_this(THREAD, this);747return resolve_constant_at_impl(h_this, pool_index, _possible_index_sentinel, NULL, THREAD);748}749750oop find_cached_constant_at(int pool_index, bool& found_it, TRAPS) {751constantPoolHandle h_this(THREAD, this);752return resolve_constant_at_impl(h_this, pool_index, _possible_index_sentinel, &found_it, THREAD);753}754755void copy_bootstrap_arguments_at(int index,756int start_arg, int end_arg,757objArrayHandle info, int pos,758bool must_resolve, Handle if_not_available, TRAPS) {759constantPoolHandle h_this(THREAD, this);760copy_bootstrap_arguments_at_impl(h_this, index, start_arg, end_arg,761info, pos, must_resolve, if_not_available, THREAD);762}763764// Klass name matches name at offset765bool klass_name_at_matches(const InstanceKlass* k, int which);766767// Sizing768int length() const { return _length; }769void set_length(int length) { _length = length; }770771// Tells whether index is within bounds.772bool is_within_bounds(int index) const {773return 0 <= index && index < length();774}775776// Sizing (in words)777static int header_size() {778return align_up((int)sizeof(ConstantPool), wordSize) / wordSize;779}780static int size(int length) { return align_metadata_size(header_size() + length); }781int size() const { return size(length()); }782783// ConstantPools should be stored in the read-only region of CDS archive.784static bool is_read_only_by_default() { return true; }785786friend class ClassFileParser;787friend class SystemDictionary;788789// Used by CDS. These classes need to access the private ConstantPool() constructor.790template <class T> friend class CppVtableTesterA;791template <class T> friend class CppVtableTesterB;792template <class T> friend class CppVtableCloner;793794// Used by compiler to prevent classloading.795static Method* method_at_if_loaded (const constantPoolHandle& this_cp, int which);796static bool has_appendix_at_if_loaded (const constantPoolHandle& this_cp, int which);797static oop appendix_at_if_loaded (const constantPoolHandle& this_cp, int which);798static bool has_local_signature_at_if_loaded (const constantPoolHandle& this_cp, int which);799static Klass* klass_at_if_loaded (const constantPoolHandle& this_cp, int which);800801// Routines currently used for annotations (only called by jvm.cpp) but which might be used in the802// future by other Java code. These take constant pool indices rather than803// constant pool cache indices as do the peer methods above.804Symbol* uncached_klass_ref_at_noresolve(int which);805Symbol* uncached_name_ref_at(int which) { return impl_name_ref_at(which, true); }806Symbol* uncached_signature_ref_at(int which) { return impl_signature_ref_at(which, true); }807int uncached_klass_ref_index_at(int which) { return impl_klass_ref_index_at(which, true); }808int uncached_name_and_type_ref_index_at(int which) { return impl_name_and_type_ref_index_at(which, true); }809810// Sharing811int pre_resolve_shared_klasses(TRAPS);812813// Debugging814const char* printable_name_at(int which) PRODUCT_RETURN0;815816#ifdef ASSERT817enum { CPCACHE_INDEX_TAG = 0x10000 }; // helps keep CP cache indices distinct from CP indices818#else819enum { CPCACHE_INDEX_TAG = 0 }; // in product mode, this zero value is a no-op820#endif //ASSERT821822static int decode_cpcache_index(int raw_index, bool invokedynamic_ok = false) {823if (invokedynamic_ok && is_invokedynamic_index(raw_index))824return decode_invokedynamic_index(raw_index);825else826return raw_index - CPCACHE_INDEX_TAG;827}828829private:830831void set_resolved_references(OopHandle s) { _cache->set_resolved_references(s); }832Array<u2>* reference_map() const { return (_cache == NULL) ? NULL : _cache->reference_map(); }833void set_reference_map(Array<u2>* o) { _cache->set_reference_map(o); }834835Symbol* impl_name_ref_at(int which, bool uncached);836Symbol* impl_signature_ref_at(int which, bool uncached);837838int impl_klass_ref_index_at(int which, bool uncached);839int impl_name_and_type_ref_index_at(int which, bool uncached);840constantTag impl_tag_ref_at(int which, bool uncached);841842// Used while constructing constant pool (only by ClassFileParser)843jint klass_index_at(int which) {844assert(tag_at(which).is_klass_index(), "Corrupted constant pool");845return *int_at_addr(which);846}847848jint string_index_at(int which) {849assert(tag_at(which).is_string_index(), "Corrupted constant pool");850return *int_at_addr(which);851}852853// Performs the LinkResolver checks854static void verify_constant_pool_resolve(const constantPoolHandle& this_cp, Klass* klass, TRAPS);855856// Implementation of methods that needs an exposed 'this' pointer, in order to857// handle GC while executing the method858static Klass* klass_at_impl(const constantPoolHandle& this_cp, int which, TRAPS);859static oop string_at_impl(const constantPoolHandle& this_cp, int which, int obj_index, TRAPS);860861static void trace_class_resolution(const constantPoolHandle& this_cp, Klass* k);862863// Resolve string constants (to prevent allocation during compilation)864static void resolve_string_constants_impl(const constantPoolHandle& this_cp, TRAPS);865866static oop resolve_constant_at_impl(const constantPoolHandle& this_cp, int index, int cache_index,867bool* status_return, TRAPS);868static void copy_bootstrap_arguments_at_impl(const constantPoolHandle& this_cp, int index,869int start_arg, int end_arg,870objArrayHandle info, int pos,871bool must_resolve, Handle if_not_available, TRAPS);872873// Exception handling874static void save_and_throw_exception(const constantPoolHandle& this_cp, int which, constantTag tag, TRAPS);875876public:877// Exception handling878static void throw_resolution_error(const constantPoolHandle& this_cp, int which, TRAPS);879880// Merging ConstantPool* support:881bool compare_entry_to(int index1, const constantPoolHandle& cp2, int index2);882void copy_cp_to(int start_i, int end_i, const constantPoolHandle& to_cp, int to_i, TRAPS) {883constantPoolHandle h_this(THREAD, this);884copy_cp_to_impl(h_this, start_i, end_i, to_cp, to_i, THREAD);885}886static void copy_cp_to_impl(const constantPoolHandle& from_cp, int start_i, int end_i, const constantPoolHandle& to_cp, int to_i, TRAPS);887static void copy_entry_to(const constantPoolHandle& from_cp, int from_i, const constantPoolHandle& to_cp, int to_i);888static void copy_operands(const constantPoolHandle& from_cp, const constantPoolHandle& to_cp, TRAPS);889int find_matching_entry(int pattern_i, const constantPoolHandle& search_cp);890int version() const { return _saved._version; }891void set_version(int version) { _saved._version = version; }892void increment_and_save_version(int version) {893_saved._version = version >= 0 ? (version + 1) : version; // keep overflow894}895896void set_resolved_reference_length(int length) { _saved._resolved_reference_length = length; }897int resolved_reference_length() const { return _saved._resolved_reference_length; }898899// Decrease ref counts of symbols that are in the constant pool900// when the holder class is unloaded901void unreference_symbols();902903// Deallocate constant pool for RedefineClasses904void deallocate_contents(ClassLoaderData* loader_data);905void release_C_heap_structures();906907// JVMTI accesss - GetConstantPool, RetransformClasses, ...908friend class JvmtiConstantPoolReconstituter;909910private:911jint cpool_entry_size(jint idx);912jint hash_entries_to(SymbolHashMap *symmap, SymbolHashMap *classmap);913914// Copy cpool bytes into byte array.915// Returns:916// int > 0, count of the raw cpool bytes that have been copied917// 0, OutOfMemory error918// -1, Internal error919int copy_cpool_bytes(int cpool_size,920SymbolHashMap* tbl,921unsigned char *bytes);922923public:924// Verify925void verify_on(outputStream* st);926927// Printing928void print_on(outputStream* st) const;929void print_value_on(outputStream* st) const;930void print_entry_on(int index, outputStream* st);931932const char* internal_name() const { return "{constant pool}"; }933};934935class SymbolHashMapEntry : public CHeapObj<mtSymbol> {936private:937SymbolHashMapEntry* _next; // Next element in the linked list for this bucket938Symbol* _symbol; // 1-st part of the mapping: symbol => value939unsigned int _hash; // 32-bit hash for item940u2 _value; // 2-nd part of the mapping: symbol => value941942public:943unsigned int hash() const { return _hash; }944void set_hash(unsigned int hash) { _hash = hash; }945946SymbolHashMapEntry* next() const { return _next; }947void set_next(SymbolHashMapEntry* next) { _next = next; }948949Symbol* symbol() const { return _symbol; }950void set_symbol(Symbol* sym) { _symbol = sym; }951952u2 value() const { return _value; }953void set_value(u2 value) { _value = value; }954955SymbolHashMapEntry(unsigned int hash, Symbol* symbol, u2 value)956: _next(NULL), _symbol(symbol), _hash(hash), _value(value) {}957958}; // End SymbolHashMapEntry class959960961class SymbolHashMapBucket : public CHeapObj<mtSymbol> {962963private:964SymbolHashMapEntry* _entry;965966public:967SymbolHashMapEntry* entry() const { return _entry; }968void set_entry(SymbolHashMapEntry* entry) { _entry = entry; }969void clear() { _entry = NULL; }970971}; // End SymbolHashMapBucket class972973974class SymbolHashMap: public CHeapObj<mtSymbol> {975976private:977// Default number of entries in the table978enum SymbolHashMap_Constants {979_Def_HashMap_Size = 256980};981982int _table_size;983SymbolHashMapBucket* _buckets;984985void initialize_table(int table_size);986987public:988989int table_size() const { return _table_size; }990991SymbolHashMap() { initialize_table(_Def_HashMap_Size); }992SymbolHashMap(int table_size) { initialize_table(table_size); }993994// hash P(31) from Kernighan & Ritchie995static unsigned int compute_hash(const char* str, int len) {996unsigned int hash = 0;997while (len-- > 0) {998hash = 31*hash + (unsigned) *str;999str++;1000}1001return hash;1002}10031004SymbolHashMapEntry* bucket(int i) {1005return _buckets[i].entry();1006}10071008void add_entry(Symbol* sym, u2 value);1009SymbolHashMapEntry* find_entry(Symbol* sym);10101011u2 symbol_to_value(Symbol* sym) {1012SymbolHashMapEntry *entry = find_entry(sym);1013return (entry == NULL) ? 0 : entry->value();1014}10151016~SymbolHashMap();1017}; // End SymbolHashMap class10181019#endif // SHARE_OOPS_CONSTANTPOOL_HPP102010211022