Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/oops/constantPool.hpp
32285 views
/*1* Copyright (c) 1997, 2018, 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_VM_OOPS_CONSTANTPOOLOOP_HPP25#define SHARE_VM_OOPS_CONSTANTPOOLOOP_HPP2627#include "oops/arrayOop.hpp"28#include "oops/cpCache.hpp"29#include "oops/objArrayOop.hpp"30#include "oops/symbol.hpp"31#include "oops/typeArrayOop.hpp"32#include "runtime/handles.hpp"33#include "utilities/constantTag.hpp"34#ifdef TARGET_ARCH_x8635# include "bytes_x86.hpp"36#endif37#ifdef TARGET_ARCH_aarch3238# include "bytes_aarch32.hpp"39#endif40#ifdef TARGET_ARCH_aarch6441# include "bytes_aarch64.hpp"42#endif43#ifdef TARGET_ARCH_sparc44# include "bytes_sparc.hpp"45#endif46#ifdef TARGET_ARCH_zero47# include "bytes_zero.hpp"48#endif49#ifdef TARGET_ARCH_arm50# include "bytes_arm.hpp"51#endif52#ifdef TARGET_ARCH_ppc53# include "bytes_ppc.hpp"54#endif5556// A constantPool is an array containing class constants as described in the57// class file.58//59// Most of the constant pool entries are written during class parsing, which60// is safe. For klass types, the constant pool entry is61// modified when the entry is resolved. If a klass constant pool62// entry is read without a lock, only the resolved state guarantees that63// the entry in the constant pool is a klass object and not a Symbol*.6465class SymbolHashMap;6667class CPSlot VALUE_OBJ_CLASS_SPEC {68intptr_t _ptr;69public:70CPSlot(intptr_t ptr): _ptr(ptr) {}71CPSlot(Klass* ptr): _ptr((intptr_t)ptr) {}72CPSlot(Symbol* ptr): _ptr((intptr_t)ptr | 1) {}7374intptr_t value() { return _ptr; }75bool is_resolved() { return (_ptr & 1) == 0; }76bool is_unresolved() { return (_ptr & 1) == 1; }7778Symbol* get_symbol() {79assert(is_unresolved(), "bad call");80return (Symbol*)(_ptr & ~1);81}82Klass* get_klass() {83assert(is_resolved(), "bad call");84return (Klass*)_ptr;85}86};8788class KlassSizeStats;89class ConstantPool : public Metadata {90friend class VMStructs;91friend class BytecodeInterpreter; // Directly extracts an oop in the pool for fast instanceof/checkcast92friend class Universe; // For null constructor93private:94Array<u1>* _tags; // the tag array describing the constant pool's contents95ConstantPoolCache* _cache; // the cache holding interpreter runtime information96InstanceKlass* _pool_holder; // the corresponding class97Array<u2>* _operands; // for variable-sized (InvokeDynamic) nodes, usually empty9899// Array of resolved objects from the constant pool and map from resolved100// object index to original constant pool index101jobject _resolved_references;102Array<u2>* _reference_map;103104enum {105_has_preresolution = 1, // Flags106_on_stack = 2107};108109int _flags; // old fashioned bit twiddling110int _length; // number of elements in the array111112union {113// set for CDS to restore resolved references114int _resolved_reference_length;115// keeps version number for redefined classes (used in backtrace)116int _version;117} _saved;118119Monitor* _lock;120121void set_tags(Array<u1>* tags) { _tags = tags; }122void tag_at_put(int which, jbyte t) { tags()->at_put(which, t); }123void release_tag_at_put(int which, jbyte t) { tags()->release_at_put(which, t); }124125void set_operands(Array<u2>* operands) { _operands = operands; }126127int flags() const { return _flags; }128void set_flags(int f) { _flags = f; }129130private:131intptr_t* base() const { return (intptr_t*) (((char*) this) + sizeof(ConstantPool)); }132133CPSlot slot_at(int which) const {134assert(is_within_bounds(which), "index out of bounds");135// Uses volatile because the klass slot changes without a lock.136volatile intptr_t adr = (intptr_t)OrderAccess::load_ptr_acquire(obj_at_addr_raw(which));137assert(adr != 0 || which == 0, "cp entry for klass should not be zero");138return CPSlot(adr);139}140141void slot_at_put(int which, CPSlot s) const {142assert(is_within_bounds(which), "index out of bounds");143assert(s.value() != 0, "Caught something");144*(intptr_t*)&base()[which] = s.value();145}146intptr_t* obj_at_addr_raw(int which) const {147assert(is_within_bounds(which), "index out of bounds");148return (intptr_t*) &base()[which];149}150151jint* int_at_addr(int which) const {152assert(is_within_bounds(which), "index out of bounds");153return (jint*) &base()[which];154}155156jlong* long_at_addr(int which) const {157assert(is_within_bounds(which), "index out of bounds");158return (jlong*) &base()[which];159}160161jfloat* float_at_addr(int which) const {162assert(is_within_bounds(which), "index out of bounds");163return (jfloat*) &base()[which];164}165166jdouble* double_at_addr(int which) const {167assert(is_within_bounds(which), "index out of bounds");168return (jdouble*) &base()[which];169}170171ConstantPool(Array<u1>* tags);172ConstantPool() { assert(DumpSharedSpaces || UseSharedSpaces, "only for CDS"); }173public:174static ConstantPool* allocate(ClassLoaderData* loader_data, int length, TRAPS);175176bool is_constantPool() const volatile { return true; }177178Array<u1>* tags() const { return _tags; }179Array<u2>* operands() const { return _operands; }180181bool has_preresolution() const { return (_flags & _has_preresolution) != 0; }182void set_has_preresolution() { _flags |= _has_preresolution; }183184// Redefine classes support. If a method refering to this constant pool185// is on the executing stack, or as a handle in vm code, this constant pool186// can't be removed from the set of previous versions saved in the instance187// class.188bool on_stack() const { return (_flags &_on_stack) != 0; }189void set_on_stack(const bool value);190191// Klass holding pool192InstanceKlass* pool_holder() const { return _pool_holder; }193void set_pool_holder(InstanceKlass* k) { _pool_holder = k; }194InstanceKlass** pool_holder_addr() { return &_pool_holder; }195196// Interpreter runtime support197ConstantPoolCache* cache() const { return _cache; }198void set_cache(ConstantPoolCache* cache){ _cache = cache; }199200// Create object cache in the constant pool201void initialize_resolved_references(ClassLoaderData* loader_data,202intStack reference_map,203int constant_pool_map_length,204TRAPS);205206// resolved strings, methodHandles and callsite objects from the constant pool207objArrayOop resolved_references() const;208objArrayOop resolved_references_or_null() const;209// mapping resolved object array indexes to cp indexes and back.210int object_to_cp_index(int index) { return _reference_map->at(index); }211int cp_to_object_index(int index);212213// Invokedynamic indexes.214// They must look completely different from normal indexes.215// The main reason is that byte swapping is sometimes done on normal indexes.216// Finally, it is helpful for debugging to tell the two apart.217static bool is_invokedynamic_index(int i) { return (i < 0); }218static int decode_invokedynamic_index(int i) { assert(is_invokedynamic_index(i), ""); return ~i; }219static int encode_invokedynamic_index(int i) { assert(!is_invokedynamic_index(i), ""); return ~i; }220221222// The invokedynamic points at a CP cache entry. This entry points back223// at the original CP entry (CONSTANT_InvokeDynamic) and also (via f2) at an entry224// in the resolved_references array (which provides the appendix argument).225int invokedynamic_cp_cache_index(int index) const {226assert (is_invokedynamic_index(index), "should be a invokedynamic index");227int cache_index = decode_invokedynamic_index(index);228return cache_index;229}230ConstantPoolCacheEntry* invokedynamic_cp_cache_entry_at(int index) const {231// decode index that invokedynamic points to.232int cp_cache_index = invokedynamic_cp_cache_index(index);233return cache()->entry_at(cp_cache_index);234}235236// Assembly code support237static int tags_offset_in_bytes() { return offset_of(ConstantPool, _tags); }238static int cache_offset_in_bytes() { return offset_of(ConstantPool, _cache); }239static int pool_holder_offset_in_bytes() { return offset_of(ConstantPool, _pool_holder); }240static int resolved_references_offset_in_bytes() { return offset_of(ConstantPool, _resolved_references); }241242// Storing constants243244void klass_at_put(int which, Klass* k) {245assert(k != NULL, "resolved class shouldn't be null");246assert(is_within_bounds(which), "index out of bounds");247OrderAccess::release_store_ptr((Klass* volatile *)obj_at_addr_raw(which), k);248// The interpreter assumes when the tag is stored, the klass is resolved249// and the Klass* is a klass rather than a Symbol*, so we need250// hardware store ordering here.251release_tag_at_put(which, JVM_CONSTANT_Class);252}253254// For temporary use while constructing constant pool255void klass_index_at_put(int which, int name_index) {256tag_at_put(which, JVM_CONSTANT_ClassIndex);257*int_at_addr(which) = name_index;258}259260// Temporary until actual use261void unresolved_klass_at_put(int which, Symbol* s) {262release_tag_at_put(which, JVM_CONSTANT_UnresolvedClass);263slot_at_put(which, s);264}265266void method_handle_index_at_put(int which, int ref_kind, int ref_index) {267tag_at_put(which, JVM_CONSTANT_MethodHandle);268*int_at_addr(which) = ((jint) ref_index<<16) | ref_kind;269}270271void method_type_index_at_put(int which, int ref_index) {272tag_at_put(which, JVM_CONSTANT_MethodType);273*int_at_addr(which) = ref_index;274}275276void invoke_dynamic_at_put(int which, int bootstrap_specifier_index, int name_and_type_index) {277tag_at_put(which, JVM_CONSTANT_InvokeDynamic);278*int_at_addr(which) = ((jint) name_and_type_index<<16) | bootstrap_specifier_index;279}280281void unresolved_string_at_put(int which, Symbol* s) {282release_tag_at_put(which, JVM_CONSTANT_String);283*symbol_at_addr(which) = s;284}285286void int_at_put(int which, jint i) {287tag_at_put(which, JVM_CONSTANT_Integer);288*int_at_addr(which) = i;289}290291void long_at_put(int which, jlong l) {292tag_at_put(which, JVM_CONSTANT_Long);293// *long_at_addr(which) = l;294Bytes::put_native_u8((address)long_at_addr(which), *((u8*) &l));295}296297void float_at_put(int which, jfloat f) {298tag_at_put(which, JVM_CONSTANT_Float);299*float_at_addr(which) = f;300}301302void double_at_put(int which, jdouble d) {303tag_at_put(which, JVM_CONSTANT_Double);304// *double_at_addr(which) = d;305// u8 temp = *(u8*) &d;306Bytes::put_native_u8((address) double_at_addr(which), *((u8*) &d));307}308309Symbol** symbol_at_addr(int which) const {310assert(is_within_bounds(which), "index out of bounds");311return (Symbol**) &base()[which];312}313314void symbol_at_put(int which, Symbol* s) {315assert(s->refcount() != 0, "should have nonzero refcount");316tag_at_put(which, JVM_CONSTANT_Utf8);317*symbol_at_addr(which) = s;318}319320void string_at_put(int which, int obj_index, oop str) {321resolved_references()->obj_at_put(obj_index, str);322}323324// For temporary use while constructing constant pool325void string_index_at_put(int which, int string_index) {326tag_at_put(which, JVM_CONSTANT_StringIndex);327*int_at_addr(which) = string_index;328}329330void field_at_put(int which, int class_index, int name_and_type_index) {331tag_at_put(which, JVM_CONSTANT_Fieldref);332*int_at_addr(which) = ((jint) name_and_type_index<<16) | class_index;333}334335void method_at_put(int which, int class_index, int name_and_type_index) {336tag_at_put(which, JVM_CONSTANT_Methodref);337*int_at_addr(which) = ((jint) name_and_type_index<<16) | class_index;338}339340void interface_method_at_put(int which, int class_index, int name_and_type_index) {341tag_at_put(which, JVM_CONSTANT_InterfaceMethodref);342*int_at_addr(which) = ((jint) name_and_type_index<<16) | class_index; // Not so nice343}344345void name_and_type_at_put(int which, int name_index, int signature_index) {346tag_at_put(which, JVM_CONSTANT_NameAndType);347*int_at_addr(which) = ((jint) signature_index<<16) | name_index; // Not so nice348}349350// Tag query351352constantTag tag_at(int which) const { return (constantTag)tags()->at_acquire(which); }353354// Fetching constants355356Klass* klass_at(int which, TRAPS) {357constantPoolHandle h_this(THREAD, this);358return klass_at_impl(h_this, which, THREAD);359}360361Symbol* klass_name_at(int which) const; // Returns the name, w/o resolving.362363Klass* resolved_klass_at(int which) const { // Used by Compiler364guarantee(tag_at(which).is_klass(), "Corrupted constant pool");365// Must do an acquire here in case another thread resolved the klass366// behind our back, lest we later load stale values thru the oop.367return CPSlot((Klass*)OrderAccess::load_ptr_acquire(obj_at_addr_raw(which))).get_klass();368}369370// This method should only be used with a cpool lock or during parsing or gc371Symbol* unresolved_klass_at(int which) { // Temporary until actual use372Symbol* s = CPSlot((Symbol*)OrderAccess::load_ptr_acquire(obj_at_addr_raw(which))).get_symbol();373// check that the klass is still unresolved.374assert(tag_at(which).is_unresolved_klass(), "Corrupted constant pool");375return s;376}377378// RedefineClasses() API support:379Symbol* klass_at_noresolve(int which) { return klass_name_at(which); }380381jint int_at(int which) {382assert(tag_at(which).is_int(), "Corrupted constant pool");383return *int_at_addr(which);384}385386jlong long_at(int which) {387assert(tag_at(which).is_long(), "Corrupted constant pool");388// return *long_at_addr(which);389u8 tmp = Bytes::get_native_u8((address)&base()[which]);390return *((jlong*)&tmp);391}392393jfloat float_at(int which) {394assert(tag_at(which).is_float(), "Corrupted constant pool");395return *float_at_addr(which);396}397398jdouble double_at(int which) {399assert(tag_at(which).is_double(), "Corrupted constant pool");400u8 tmp = Bytes::get_native_u8((address)&base()[which]);401return *((jdouble*)&tmp);402}403404Symbol* symbol_at(int which) {405assert(tag_at(which).is_utf8(), "Corrupted constant pool");406return *symbol_at_addr(which);407}408409oop string_at(int which, int obj_index, TRAPS) {410constantPoolHandle h_this(THREAD, this);411return string_at_impl(h_this, which, obj_index, THREAD);412}413oop string_at(int which, TRAPS) {414int obj_index = cp_to_object_index(which);415return string_at(which, obj_index, THREAD);416}417418// Version that can be used before string oop array is created.419oop uncached_string_at(int which, TRAPS);420421// A "pseudo-string" is an non-string oop that has found is way into422// a String entry.423// Under EnableInvokeDynamic this can happen if the user patches a live424// object into a CONSTANT_String entry of an anonymous class.425// Method oops internally created for method handles may also426// use pseudo-strings to link themselves to related metaobjects.427428bool is_pseudo_string_at(int which) {429// A pseudo string is a string that doesn't have a symbol in the cpSlot430return unresolved_string_at(which) == NULL;431}432433oop pseudo_string_at(int which, int obj_index) {434assert(tag_at(which).is_string(), "Corrupted constant pool");435assert(unresolved_string_at(which) == NULL, "shouldn't have symbol");436oop s = resolved_references()->obj_at(obj_index);437return s;438}439440oop pseudo_string_at(int which) {441assert(tag_at(which).is_string(), "Corrupted constant pool");442assert(unresolved_string_at(which) == NULL, "shouldn't have symbol");443int obj_index = cp_to_object_index(which);444oop s = resolved_references()->obj_at(obj_index);445return s;446}447448void pseudo_string_at_put(int which, int obj_index, oop x) {449assert(EnableInvokeDynamic, "");450assert(tag_at(which).is_string(), "Corrupted constant pool");451unresolved_string_at_put(which, NULL); // indicates patched string452string_at_put(which, obj_index, x); // this works just fine453}454455// only called when we are sure a string entry is already resolved (via an456// earlier string_at call.457oop resolved_string_at(int which) {458assert(tag_at(which).is_string(), "Corrupted constant pool");459// Must do an acquire here in case another thread resolved the klass460// behind our back, lest we later load stale values thru the oop.461// we might want a volatile_obj_at in ObjArrayKlass.462int obj_index = cp_to_object_index(which);463return resolved_references()->obj_at(obj_index);464}465466Symbol* unresolved_string_at(int which) {467assert(tag_at(which).is_string(), "Corrupted constant pool");468Symbol* s = *symbol_at_addr(which);469return s;470}471472// Returns an UTF8 for a CONSTANT_String entry at a given index.473// UTF8 char* representation was chosen to avoid conversion of474// java_lang_Strings at resolved entries into Symbol*s475// or vice versa.476// Caller is responsible for checking for pseudo-strings.477char* string_at_noresolve(int which);478479jint name_and_type_at(int which) {480assert(tag_at(which).is_name_and_type(), "Corrupted constant pool");481return *int_at_addr(which);482}483484private:485int method_handle_ref_kind_at(int which, bool error_ok) {486assert(tag_at(which).is_method_handle() ||487(error_ok && tag_at(which).is_method_handle_in_error()), "Corrupted constant pool");488return extract_low_short_from_int(*int_at_addr(which)); // mask out unwanted ref_index bits489}490int method_handle_index_at(int which, bool error_ok) {491assert(tag_at(which).is_method_handle() ||492(error_ok && tag_at(which).is_method_handle_in_error()), "Corrupted constant pool");493return extract_high_short_from_int(*int_at_addr(which)); // shift out unwanted ref_kind bits494}495int method_type_index_at(int which, bool error_ok) {496assert(tag_at(which).is_method_type() ||497(error_ok && tag_at(which).is_method_type_in_error()), "Corrupted constant pool");498return *int_at_addr(which);499}500public:501int method_handle_ref_kind_at(int which) {502return method_handle_ref_kind_at(which, false);503}504int method_handle_ref_kind_at_error_ok(int which) {505return method_handle_ref_kind_at(which, true);506}507int method_handle_index_at(int which) {508return method_handle_index_at(which, false);509}510int method_handle_index_at_error_ok(int which) {511return method_handle_index_at(which, true);512}513int method_type_index_at(int which) {514return method_type_index_at(which, false);515}516int method_type_index_at_error_ok(int which) {517return method_type_index_at(which, true);518}519520// Derived queries:521Symbol* method_handle_name_ref_at(int which) {522int member = method_handle_index_at(which);523return impl_name_ref_at(member, true);524}525Symbol* method_handle_signature_ref_at(int which) {526int member = method_handle_index_at(which);527return impl_signature_ref_at(member, true);528}529int method_handle_klass_index_at(int which) {530int member = method_handle_index_at(which);531return impl_klass_ref_index_at(member, true);532}533Symbol* method_type_signature_at(int which) {534int sym = method_type_index_at(which);535return symbol_at(sym);536}537538int invoke_dynamic_name_and_type_ref_index_at(int which) {539assert(tag_at(which).is_invoke_dynamic(), "Corrupted constant pool");540return extract_high_short_from_int(*int_at_addr(which));541}542int invoke_dynamic_bootstrap_specifier_index(int which) {543assert(tag_at(which).value() == JVM_CONSTANT_InvokeDynamic, "Corrupted constant pool");544return extract_low_short_from_int(*int_at_addr(which));545}546int invoke_dynamic_operand_base(int which) {547int bootstrap_specifier_index = invoke_dynamic_bootstrap_specifier_index(which);548return operand_offset_at(operands(), bootstrap_specifier_index);549}550// The first part of the operands array consists of an index into the second part.551// Extract a 32-bit index value from the first part.552static int operand_offset_at(Array<u2>* operands, int bootstrap_specifier_index) {553int n = (bootstrap_specifier_index * 2);554assert(n >= 0 && n+2 <= operands->length(), "oob");555// The first 32-bit index points to the beginning of the second part556// of the operands array. Make sure this index is in the first part.557DEBUG_ONLY(int second_part = build_int_from_shorts(operands->at(0),558operands->at(1)));559assert(second_part == 0 || n+2 <= second_part, "oob (2)");560int offset = build_int_from_shorts(operands->at(n+0),561operands->at(n+1));562// The offset itself must point into the second part of the array.563assert(offset == 0 || offset >= second_part && offset <= operands->length(), "oob (3)");564return offset;565}566static void operand_offset_at_put(Array<u2>* operands, int bootstrap_specifier_index, int offset) {567int n = bootstrap_specifier_index * 2;568assert(n >= 0 && n+2 <= operands->length(), "oob");569operands->at_put(n+0, extract_low_short_from_int(offset));570operands->at_put(n+1, extract_high_short_from_int(offset));571}572static int operand_array_length(Array<u2>* operands) {573if (operands == NULL || operands->length() == 0) return 0;574int second_part = operand_offset_at(operands, 0);575return (second_part / 2);576}577578#ifdef ASSERT579// operand tuples fit together exactly, end to end580static int operand_limit_at(Array<u2>* operands, int bootstrap_specifier_index) {581int nextidx = bootstrap_specifier_index + 1;582if (nextidx == operand_array_length(operands))583return operands->length();584else585return operand_offset_at(operands, nextidx);586}587int invoke_dynamic_operand_limit(int which) {588int bootstrap_specifier_index = invoke_dynamic_bootstrap_specifier_index(which);589return operand_limit_at(operands(), bootstrap_specifier_index);590}591#endif //ASSERT592593// layout of InvokeDynamic bootstrap method specifier (in second part of operands array):594enum {595_indy_bsm_offset = 0, // CONSTANT_MethodHandle bsm596_indy_argc_offset = 1, // u2 argc597_indy_argv_offset = 2 // u2 argv[argc]598};599600// These functions are used in RedefineClasses for CP merge601602int operand_offset_at(int bootstrap_specifier_index) {603assert(0 <= bootstrap_specifier_index &&604bootstrap_specifier_index < operand_array_length(operands()),605"Corrupted CP operands");606return operand_offset_at(operands(), bootstrap_specifier_index);607}608int operand_bootstrap_method_ref_index_at(int bootstrap_specifier_index) {609int offset = operand_offset_at(bootstrap_specifier_index);610return operands()->at(offset + _indy_bsm_offset);611}612int operand_argument_count_at(int bootstrap_specifier_index) {613int offset = operand_offset_at(bootstrap_specifier_index);614int argc = operands()->at(offset + _indy_argc_offset);615return argc;616}617int operand_argument_index_at(int bootstrap_specifier_index, int j) {618int offset = operand_offset_at(bootstrap_specifier_index);619return operands()->at(offset + _indy_argv_offset + j);620}621int operand_next_offset_at(int bootstrap_specifier_index) {622int offset = operand_offset_at(bootstrap_specifier_index) + _indy_argv_offset623+ operand_argument_count_at(bootstrap_specifier_index);624return offset;625}626// Compare a bootsrap specifier in the operands arrays627bool compare_operand_to(int bootstrap_specifier_index1, constantPoolHandle cp2,628int bootstrap_specifier_index2, TRAPS);629// Find a bootsrap specifier in the operands array630int find_matching_operand(int bootstrap_specifier_index, constantPoolHandle search_cp,631int operands_cur_len, TRAPS);632// Resize the operands array with delta_len and delta_size633void resize_operands(int delta_len, int delta_size, TRAPS);634// Extend the operands array with the length and size of the ext_cp operands635void extend_operands(constantPoolHandle ext_cp, TRAPS);636// Shrink the operands array to a smaller array with new_len length637void shrink_operands(int new_len, TRAPS);638639640int invoke_dynamic_bootstrap_method_ref_index_at(int which) {641assert(tag_at(which).is_invoke_dynamic(), "Corrupted constant pool");642int op_base = invoke_dynamic_operand_base(which);643return operands()->at(op_base + _indy_bsm_offset);644}645int invoke_dynamic_argument_count_at(int which) {646assert(tag_at(which).is_invoke_dynamic(), "Corrupted constant pool");647int op_base = invoke_dynamic_operand_base(which);648int argc = operands()->at(op_base + _indy_argc_offset);649DEBUG_ONLY(int end_offset = op_base + _indy_argv_offset + argc;650int next_offset = invoke_dynamic_operand_limit(which));651assert(end_offset == next_offset, "matched ending");652return argc;653}654int invoke_dynamic_argument_index_at(int which, int j) {655int op_base = invoke_dynamic_operand_base(which);656DEBUG_ONLY(int argc = operands()->at(op_base + _indy_argc_offset));657assert((uint)j < (uint)argc, "oob");658return operands()->at(op_base + _indy_argv_offset + j);659}660661// The following methods (name/signature/klass_ref_at, klass_ref_at_noresolve,662// name_and_type_ref_index_at) all expect to be passed indices obtained663// directly from the bytecode.664// If the indices are meant to refer to fields or methods, they are665// actually rewritten constant pool cache indices.666// The routine remap_instruction_operand_from_cache manages the adjustment667// of these values back to constant pool indices.668669// There are also "uncached" versions which do not adjust the operand index; see below.670671// FIXME: Consider renaming these with a prefix "cached_" to make the distinction clear.672// In a few cases (the verifier) there are uses before a cpcache has been built,673// which are handled by a dynamic check in remap_instruction_operand_from_cache.674// FIXME: Remove the dynamic check, and adjust all callers to specify the correct mode.675676// Lookup for entries consisting of (klass_index, name_and_type index)677Klass* klass_ref_at(int which, TRAPS);678Symbol* klass_ref_at_noresolve(int which);679Symbol* name_ref_at(int which) { return impl_name_ref_at(which, false); }680Symbol* signature_ref_at(int which) { return impl_signature_ref_at(which, false); }681682int klass_ref_index_at(int which) { return impl_klass_ref_index_at(which, false); }683int name_and_type_ref_index_at(int which) { return impl_name_and_type_ref_index_at(which, false); }684685// Lookup for entries consisting of (name_index, signature_index)686int name_ref_index_at(int which_nt); // == low-order jshort of name_and_type_at(which_nt)687int signature_ref_index_at(int which_nt); // == high-order jshort of name_and_type_at(which_nt)688689BasicType basic_type_for_signature_at(int which);690691// Resolve string constants (to prevent allocation during compilation)692void resolve_string_constants(TRAPS) {693constantPoolHandle h_this(THREAD, this);694resolve_string_constants_impl(h_this, CHECK);695}696697// CDS support698void remove_unshareable_info();699void restore_unshareable_info(TRAPS);700bool resolve_class_constants(TRAPS);701// The ConstantPool vtable is restored by this call when the ConstantPool is702// in the shared archive. See patch_klass_vtables() in metaspaceShared.cpp for703// all the gory details. SA, dtrace and pstack helpers distinguish metadata704// by their vtable.705void restore_vtable() { guarantee(is_constantPool(), "vtable restored by this call"); }706707private:708enum { _no_index_sentinel = -1, _possible_index_sentinel = -2 };709public:710711// Resolve late bound constants.712oop resolve_constant_at(int index, TRAPS) {713constantPoolHandle h_this(THREAD, this);714return resolve_constant_at_impl(h_this, index, _no_index_sentinel, THREAD);715}716717oop resolve_cached_constant_at(int cache_index, TRAPS) {718constantPoolHandle h_this(THREAD, this);719return resolve_constant_at_impl(h_this, _no_index_sentinel, cache_index, THREAD);720}721722oop resolve_possibly_cached_constant_at(int pool_index, TRAPS) {723constantPoolHandle h_this(THREAD, this);724return resolve_constant_at_impl(h_this, pool_index, _possible_index_sentinel, THREAD);725}726727oop resolve_bootstrap_specifier_at(int index, TRAPS) {728constantPoolHandle h_this(THREAD, this);729return resolve_bootstrap_specifier_at_impl(h_this, index, THREAD);730}731732// Klass name matches name at offset733bool klass_name_at_matches(instanceKlassHandle k, int which);734735// Sizing736int length() const { return _length; }737void set_length(int length) { _length = length; }738739// Tells whether index is within bounds.740bool is_within_bounds(int index) const {741return 0 <= index && index < length();742}743744// Sizing (in words)745static int header_size() { return sizeof(ConstantPool)/HeapWordSize; }746static int size(int length) { return align_object_size(header_size() + length); }747int size() const { return size(length()); }748#if INCLUDE_SERVICES749void collect_statistics(KlassSizeStats *sz) const;750#endif751752friend class ClassFileParser;753friend class SystemDictionary;754755// Used by compiler to prevent classloading.756static Method* method_at_if_loaded (constantPoolHandle this_oop, int which);757static bool has_appendix_at_if_loaded (constantPoolHandle this_oop, int which);758static oop appendix_at_if_loaded (constantPoolHandle this_oop, int which);759static bool has_method_type_at_if_loaded (constantPoolHandle this_oop, int which);760static oop method_type_at_if_loaded (constantPoolHandle this_oop, int which);761static Klass* klass_at_if_loaded (constantPoolHandle this_oop, int which);762static Klass* klass_ref_at_if_loaded (constantPoolHandle this_oop, int which);763764// Routines currently used for annotations (only called by jvm.cpp) but which might be used in the765// future by other Java code. These take constant pool indices rather than766// constant pool cache indices as do the peer methods above.767Symbol* uncached_klass_ref_at_noresolve(int which);768Symbol* uncached_name_ref_at(int which) { return impl_name_ref_at(which, true); }769Symbol* uncached_signature_ref_at(int which) { return impl_signature_ref_at(which, true); }770int uncached_klass_ref_index_at(int which) { return impl_klass_ref_index_at(which, true); }771int uncached_name_and_type_ref_index_at(int which) { return impl_name_and_type_ref_index_at(which, true); }772773// Sharing774int pre_resolve_shared_klasses(TRAPS);775776// Debugging777const char* printable_name_at(int which) PRODUCT_RETURN0;778779#ifdef ASSERT780enum { CPCACHE_INDEX_TAG = 0x10000 }; // helps keep CP cache indices distinct from CP indices781#else782enum { CPCACHE_INDEX_TAG = 0 }; // in product mode, this zero value is a no-op783#endif //ASSERT784785static int decode_cpcache_index(int raw_index, bool invokedynamic_ok = false) {786if (invokedynamic_ok && is_invokedynamic_index(raw_index))787return decode_invokedynamic_index(raw_index);788else789return raw_index - CPCACHE_INDEX_TAG;790}791792private:793794void set_resolved_references(jobject s) { _resolved_references = s; }795Array<u2>* reference_map() const { return _reference_map; }796void set_reference_map(Array<u2>* o) { _reference_map = o; }797798// patch JSR 292 resolved references after the class is linked.799void patch_resolved_references(GrowableArray<Handle>* cp_patches);800801Symbol* impl_name_ref_at(int which, bool uncached);802Symbol* impl_signature_ref_at(int which, bool uncached);803int impl_klass_ref_index_at(int which, bool uncached);804int impl_name_and_type_ref_index_at(int which, bool uncached);805806int remap_instruction_operand_from_cache(int operand); // operand must be biased by CPCACHE_INDEX_TAG807808// Used while constructing constant pool (only by ClassFileParser)809jint klass_index_at(int which) {810assert(tag_at(which).is_klass_index(), "Corrupted constant pool");811return *int_at_addr(which);812}813814jint string_index_at(int which) {815assert(tag_at(which).is_string_index(), "Corrupted constant pool");816return *int_at_addr(which);817}818819// Performs the LinkResolver checks820static void verify_constant_pool_resolve(constantPoolHandle this_oop, KlassHandle klass, TRAPS);821822// Implementation of methods that needs an exposed 'this' pointer, in order to823// handle GC while executing the method824static Klass* klass_at_impl(constantPoolHandle this_oop, int which, TRAPS);825static oop string_at_impl(constantPoolHandle this_oop, int which, int obj_index, TRAPS);826827// Resolve string constants (to prevent allocation during compilation)828static void resolve_string_constants_impl(constantPoolHandle this_oop, TRAPS);829830static oop resolve_constant_at_impl(constantPoolHandle this_oop, int index, int cache_index, TRAPS);831static oop resolve_bootstrap_specifier_at_impl(constantPoolHandle this_oop, int index, TRAPS);832833// Exception handling834static void throw_resolution_error(constantPoolHandle this_oop, int which, TRAPS);835static Symbol* exception_message(constantPoolHandle this_oop, int which, constantTag tag, oop pending_exception);836static void save_and_throw_exception(constantPoolHandle this_oop, int which, constantTag tag, TRAPS);837838public:839// Merging ConstantPool* support:840bool compare_entry_to(int index1, constantPoolHandle cp2, int index2, TRAPS);841void copy_cp_to(int start_i, int end_i, constantPoolHandle to_cp, int to_i, TRAPS) {842constantPoolHandle h_this(THREAD, this);843copy_cp_to_impl(h_this, start_i, end_i, to_cp, to_i, THREAD);844}845static void copy_cp_to_impl(constantPoolHandle from_cp, int start_i, int end_i, constantPoolHandle to_cp, int to_i, TRAPS);846static void copy_entry_to(constantPoolHandle from_cp, int from_i, constantPoolHandle to_cp, int to_i, TRAPS);847static void copy_operands(constantPoolHandle from_cp, constantPoolHandle to_cp, TRAPS);848int find_matching_entry(int pattern_i, constantPoolHandle search_cp, TRAPS);849int version() const { return _saved._version; }850void set_version(int version) { _saved._version = version; }851void increment_and_save_version(int version) {852_saved._version = version >= 0 ? (version + 1) : version; // keep overflow853}854855void set_resolved_reference_length(int length) { _saved._resolved_reference_length = length; }856int resolved_reference_length() const { return _saved._resolved_reference_length; }857void set_lock(Monitor* lock) { _lock = lock; }858Monitor* lock() { return _lock; }859860// Decrease ref counts of symbols that are in the constant pool861// when the holder class is unloaded862void unreference_symbols();863864// Deallocate constant pool for RedefineClasses865void deallocate_contents(ClassLoaderData* loader_data);866void release_C_heap_structures();867868// JVMTI accesss - GetConstantPool, RetransformClasses, ...869friend class JvmtiConstantPoolReconstituter;870871private:872jint cpool_entry_size(jint idx);873jint hash_entries_to(SymbolHashMap *symmap, SymbolHashMap *classmap);874875// Copy cpool bytes into byte array.876// Returns:877// int > 0, count of the raw cpool bytes that have been copied878// 0, OutOfMemory error879// -1, Internal error880int copy_cpool_bytes(int cpool_size,881SymbolHashMap* tbl,882unsigned char *bytes);883884public:885// Verify886void verify_on(outputStream* st);887888// Printing889void print_on(outputStream* st) const;890void print_value_on(outputStream* st) const;891void print_entry_on(int index, outputStream* st);892893const char* internal_name() const { return "{constant pool}"; }894895#ifndef PRODUCT896// Compile the world support897static void preload_and_initialize_all_classes(ConstantPool* constant_pool, TRAPS);898#endif899};900901class SymbolHashMapEntry : public CHeapObj<mtSymbol> {902private:903unsigned int _hash; // 32-bit hash for item904SymbolHashMapEntry* _next; // Next element in the linked list for this bucket905Symbol* _symbol; // 1-st part of the mapping: symbol => value906u2 _value; // 2-nd part of the mapping: symbol => value907908public:909unsigned int hash() const { return _hash; }910void set_hash(unsigned int hash) { _hash = hash; }911912SymbolHashMapEntry* next() const { return _next; }913void set_next(SymbolHashMapEntry* next) { _next = next; }914915Symbol* symbol() const { return _symbol; }916void set_symbol(Symbol* sym) { _symbol = sym; }917918u2 value() const { return _value; }919void set_value(u2 value) { _value = value; }920921SymbolHashMapEntry(unsigned int hash, Symbol* symbol, u2 value)922: _hash(hash), _symbol(symbol), _value(value), _next(NULL) {}923924}; // End SymbolHashMapEntry class925926927class SymbolHashMapBucket : public CHeapObj<mtSymbol> {928929private:930SymbolHashMapEntry* _entry;931932public:933SymbolHashMapEntry* entry() const { return _entry; }934void set_entry(SymbolHashMapEntry* entry) { _entry = entry; }935void clear() { _entry = NULL; }936937}; // End SymbolHashMapBucket class938939940class SymbolHashMap: public CHeapObj<mtSymbol> {941942private:943// Default number of entries in the table944enum SymbolHashMap_Constants {945_Def_HashMap_Size = 256946};947948int _table_size;949SymbolHashMapBucket* _buckets;950951void initialize_table(int table_size) {952_table_size = table_size;953_buckets = NEW_C_HEAP_ARRAY(SymbolHashMapBucket, table_size, mtSymbol);954for (int index = 0; index < table_size; index++) {955_buckets[index].clear();956}957}958959public:960961int table_size() const { return _table_size; }962963SymbolHashMap() { initialize_table(_Def_HashMap_Size); }964SymbolHashMap(int table_size) { initialize_table(table_size); }965966// hash P(31) from Kernighan & Ritchie967static unsigned int compute_hash(const char* str, int len) {968unsigned int hash = 0;969while (len-- > 0) {970hash = 31*hash + (unsigned) *str;971str++;972}973return hash;974}975976SymbolHashMapEntry* bucket(int i) {977return _buckets[i].entry();978}979980void add_entry(Symbol* sym, u2 value);981SymbolHashMapEntry* find_entry(Symbol* sym);982983u2 symbol_to_value(Symbol* sym) {984SymbolHashMapEntry *entry = find_entry(sym);985return (entry == NULL) ? 0 : entry->value();986}987988~SymbolHashMap() {989SymbolHashMapEntry* next;990for (int i = 0; i < _table_size; i++) {991for (SymbolHashMapEntry* cur = bucket(i); cur != NULL; cur = next) {992next = cur->next();993delete(cur);994}995}996FREE_C_HEAP_ARRAY(SymbolHashMapBucket, _buckets, mtSymbol);997}998}; // End SymbolHashMap class9991000#endif // SHARE_VM_OOPS_CONSTANTPOOLOOP_HPP100110021003