1 /*
2 * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
228
229 // Klass holding pool
230 InstanceKlass* pool_holder() const { return _pool_holder; }
231 void set_pool_holder(InstanceKlass* k) { _pool_holder = k; }
232 InstanceKlass** pool_holder_addr() { return &_pool_holder; }
233
234 // Interpreter runtime support
235 ConstantPoolCache* cache() const { return _cache; }
236 void set_cache(ConstantPoolCache* cache){ _cache = cache; }
237
238 virtual void metaspace_pointers_do(MetaspaceClosure* iter);
239 virtual MetaspaceObj::Type type() const { return ConstantPoolType; }
240
241 // Create object cache in the constant pool
242 void initialize_resolved_references(ClassLoaderData* loader_data,
243 const intStack& reference_map,
244 int constant_pool_map_length,
245 TRAPS);
246
247 // resolved strings, methodHandles and callsite objects from the constant pool
248 objArrayOop resolved_references() const;
249 objArrayOop resolved_references_or_null() const;
250 oop resolved_reference_at(int obj_index) const;
251 oop set_resolved_reference_at(int index, oop new_value);
252
253 // mapping resolved object array indexes to cp indexes and back.
254 int object_to_cp_index(int index) { return reference_map()->at(index); }
255 int cp_to_object_index(int index);
256
257 void set_resolved_klasses(Array<Klass*>* rk) { _resolved_klasses = rk; }
258 Array<Klass*>* resolved_klasses() const { return _resolved_klasses; }
259 void allocate_resolved_klasses(ClassLoaderData* loader_data, int num_klasses, TRAPS);
260 void initialize_unresolved_klasses(ClassLoaderData* loader_data, TRAPS);
261
262 // Given the per-instruction index of an indy instruction, report the
263 // main constant pool entry for its bootstrap specifier.
264 // From there, uncached_name/signature_ref_at will get the name/type.
265 inline u2 invokedynamic_bootstrap_ref_index_at(int indy_index) const;
266
267 // Assembly code support
268 static ByteSize tags_offset() { return byte_offset_of(ConstantPool, _tags); }
269 static ByteSize cache_offset() { return byte_offset_of(ConstantPool, _cache); }
270 static ByteSize pool_holder_offset() { return byte_offset_of(ConstantPool, _pool_holder); }
271 static ByteSize resolved_klasses_offset() { return byte_offset_of(ConstantPool, _resolved_klasses); }
272
273 // Storing constants
274
275 // For temporary use while constructing constant pool
276 void klass_index_at_put(int cp_index, int name_index) {
277 tag_at_put(cp_index, JVM_CONSTANT_ClassIndex);
278 *int_at_addr(cp_index) = name_index;
279 }
280
281 // Hidden class support:
282 void klass_at_put(int class_index, Klass* k);
283
284 void unresolved_klass_at_put(int cp_index, int name_index, int resolved_klass_index) {
285 release_tag_at_put(cp_index, JVM_CONSTANT_UnresolvedClass);
286
287 assert((name_index & 0xffff0000) == 0, "must be");
288 assert((resolved_klass_index & 0xffff0000) == 0, "must be");
289 *int_at_addr(cp_index) =
290 build_int_from_shorts((jushort)resolved_klass_index, (jushort)name_index);
291 }
292
293 void method_handle_index_at_put(int cp_index, int ref_kind, int ref_index) {
294 tag_at_put(cp_index, JVM_CONSTANT_MethodHandle);
295 *int_at_addr(cp_index) = ((jint) ref_index<<16) | ref_kind;
587 constantTag tag_ref_at(int cp_cache_index, Bytecodes::Code code);
588
589 int to_cp_index(int which, Bytecodes::Code code);
590
591 bool is_resolved(int which, Bytecodes::Code code);
592
593 // Lookup for entries consisting of (name_index, signature_index)
594 u2 name_ref_index_at(int cp_index); // == low-order jshort of name_and_type_at(cp_index)
595 u2 signature_ref_index_at(int cp_index); // == high-order jshort of name_and_type_at(cp_index)
596
597 BasicType basic_type_for_signature_at(int cp_index) const;
598
599 // Resolve string constants (to prevent allocation during compilation)
600 void resolve_string_constants(TRAPS) {
601 constantPoolHandle h_this(THREAD, this);
602 resolve_string_constants_impl(h_this, CHECK);
603 }
604
605 #if INCLUDE_CDS
606 // CDS support
607 objArrayOop prepare_resolved_references_for_archiving() NOT_CDS_JAVA_HEAP_RETURN_(nullptr);
608 void remove_unshareable_info();
609 void restore_unshareable_info(TRAPS);
610 private:
611 void remove_unshareable_entries();
612 void remove_resolved_klass_if_non_deterministic(int cp_index);
613 template <typename Function> void iterate_archivable_resolved_references(Function function);
614 #endif
615
616 private:
617 enum { _no_index_sentinel = -1, _possible_index_sentinel = -2 };
618 public:
619
620 // Get the tag for a constant, which may involve a constant dynamic
621 constantTag constant_tag_at(int cp_index);
622 // Get the basic type for a constant, which may involve a constant dynamic
623 BasicType basic_type_for_constant_at(int cp_index);
624
625 // Resolve late bound constants.
626 oop resolve_constant_at(int cp_index, TRAPS) {
627 constantPoolHandle h_this(THREAD, this);
628 return resolve_constant_at_impl(h_this, cp_index, _no_index_sentinel, nullptr, THREAD);
629 }
630
631 oop resolve_cached_constant_at(int cache_index, TRAPS) {
632 constantPoolHandle h_this(THREAD, this);
633 return resolve_constant_at_impl(h_this, _no_index_sentinel, cache_index, nullptr, THREAD);
634 }
635
636 oop resolve_possibly_cached_constant_at(int cp_index, TRAPS) {
637 constantPoolHandle h_this(THREAD, this);
638 return resolve_constant_at_impl(h_this, cp_index, _possible_index_sentinel, nullptr, THREAD);
639 }
640
641 oop find_cached_constant_at(int cp_index, bool& found_it, TRAPS) {
642 constantPoolHandle h_this(THREAD, this);
643 return resolve_constant_at_impl(h_this, cp_index, _possible_index_sentinel, &found_it, THREAD);
644 }
645
646 void copy_bootstrap_arguments_at(int cp_index,
647 int start_arg, int end_arg,
648 objArrayHandle info, int pos,
649 bool must_resolve, Handle if_not_available, TRAPS) {
650 constantPoolHandle h_this(THREAD, this);
651 copy_bootstrap_arguments_at_impl(h_this, cp_index, start_arg, end_arg,
652 info, pos, must_resolve, if_not_available, THREAD);
653 }
654
655 // Klass name matches name at offset
656 bool klass_name_at_matches(const InstanceKlass* k, int cp_index);
657
658 // Sizing
659 int length() const { return _length; }
660 void set_length(int length) { _length = length; }
661
662 // Tells whether index is within bounds.
663 bool is_within_bounds(int index) const {
664 return 0 <= index && index < length();
665 }
666
667 // Sizing (in words)
668 static int header_size() {
727 return *int_at_addr(cp_index);
728 }
729
730 // Performs the LinkResolver checks
731 static void verify_constant_pool_resolve(const constantPoolHandle& this_cp, Klass* klass, TRAPS);
732
733 // Implementation of methods that needs an exposed 'this' pointer, in order to
734 // handle GC while executing the method
735 static Klass* klass_at_impl(const constantPoolHandle& this_cp, int cp_index, TRAPS);
736 static oop string_at_impl(const constantPoolHandle& this_cp, int cp_index, int obj_index, TRAPS);
737
738 static void trace_class_resolution(const constantPoolHandle& this_cp, Klass* k);
739
740 // Resolve string constants (to prevent allocation during compilation)
741 static void resolve_string_constants_impl(const constantPoolHandle& this_cp, TRAPS);
742
743 static oop resolve_constant_at_impl(const constantPoolHandle& this_cp, int cp_index, int cache_index,
744 bool* status_return, TRAPS);
745 static void copy_bootstrap_arguments_at_impl(const constantPoolHandle& this_cp, int cp_index,
746 int start_arg, int end_arg,
747 objArrayHandle info, int pos,
748 bool must_resolve, Handle if_not_available, TRAPS);
749
750 // Exception handling
751 static void save_and_throw_exception(const constantPoolHandle& this_cp, int cp_index, constantTag tag, TRAPS);
752
753 public:
754 // Exception handling
755 static void throw_resolution_error(const constantPoolHandle& this_cp, int which, TRAPS);
756
757 // Merging ConstantPool* support:
758 bool compare_entry_to(int index1, const constantPoolHandle& cp2, int index2);
759 void copy_cp_to(int start_cpi, int end_cpi, const constantPoolHandle& to_cp, int to_cpi, TRAPS) {
760 constantPoolHandle h_this(THREAD, this);
761 copy_cp_to_impl(h_this, start_cpi, end_cpi, to_cp, to_cpi, THREAD);
762 }
763 static void copy_cp_to_impl(const constantPoolHandle& from_cp, int start_cpi, int end_cpi, const constantPoolHandle& to_cp, int to_cpi, TRAPS);
764 static void copy_entry_to(const constantPoolHandle& from_cp, int from_cpi, const constantPoolHandle& to_cp, int to_cpi);
765 static void copy_bsm_entries(const constantPoolHandle& from_cp, const constantPoolHandle& to_cp, TRAPS);
766 int find_matching_entry(int pattern_i, const constantPoolHandle& search_cp);
767 int version() const { return _saved._version; }
|
1 /*
2 * Copyright (c) 1997, 2026, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
228
229 // Klass holding pool
230 InstanceKlass* pool_holder() const { return _pool_holder; }
231 void set_pool_holder(InstanceKlass* k) { _pool_holder = k; }
232 InstanceKlass** pool_holder_addr() { return &_pool_holder; }
233
234 // Interpreter runtime support
235 ConstantPoolCache* cache() const { return _cache; }
236 void set_cache(ConstantPoolCache* cache){ _cache = cache; }
237
238 virtual void metaspace_pointers_do(MetaspaceClosure* iter);
239 virtual MetaspaceObj::Type type() const { return ConstantPoolType; }
240
241 // Create object cache in the constant pool
242 void initialize_resolved_references(ClassLoaderData* loader_data,
243 const intStack& reference_map,
244 int constant_pool_map_length,
245 TRAPS);
246
247 // resolved strings, methodHandles and callsite objects from the constant pool
248 refArrayOop resolved_references() const;
249 refArrayOop resolved_references_or_null() const;
250 oop resolved_reference_at(int obj_index) const;
251 oop set_resolved_reference_at(int index, oop new_value);
252
253 // mapping resolved object array indexes to cp indexes and back.
254 int object_to_cp_index(int index) { return reference_map()->at(index); }
255 int cp_to_object_index(int index);
256
257 void set_resolved_klasses(Array<Klass*>* rk) { _resolved_klasses = rk; }
258 Array<Klass*>* resolved_klasses() const { return _resolved_klasses; }
259 void allocate_resolved_klasses(ClassLoaderData* loader_data, int num_klasses, TRAPS);
260 void initialize_unresolved_klasses(ClassLoaderData* loader_data, TRAPS);
261
262 // Given the per-instruction index of an indy instruction, report the
263 // main constant pool entry for its bootstrap specifier.
264 // From there, uncached_name/signature_ref_at will get the name/type.
265 inline u2 invokedynamic_bootstrap_ref_index_at(int indy_index) const;
266
267 // Assembly code support
268 static ByteSize tags_offset() { return byte_offset_of(ConstantPool, _tags); }
269 static ByteSize cache_offset() { return byte_offset_of(ConstantPool, _cache); }
270 static ByteSize pool_holder_offset() { return byte_offset_of(ConstantPool, _pool_holder); }
271 static ByteSize resolved_klasses_offset() { return byte_offset_of(ConstantPool, _resolved_klasses); }
272
273 // Storing constants
274
275 // For temporary use while constructing constant pool. Used during a retransform/class redefinition as well.
276 void klass_index_at_put(int cp_index, int name_index) {
277 tag_at_put(cp_index, JVM_CONSTANT_ClassIndex);
278 *int_at_addr(cp_index) = name_index;
279 }
280
281 // Hidden class support:
282 void klass_at_put(int class_index, Klass* k);
283
284 void unresolved_klass_at_put(int cp_index, int name_index, int resolved_klass_index) {
285 release_tag_at_put(cp_index, JVM_CONSTANT_UnresolvedClass);
286
287 assert((name_index & 0xffff0000) == 0, "must be");
288 assert((resolved_klass_index & 0xffff0000) == 0, "must be");
289 *int_at_addr(cp_index) =
290 build_int_from_shorts((jushort)resolved_klass_index, (jushort)name_index);
291 }
292
293 void method_handle_index_at_put(int cp_index, int ref_kind, int ref_index) {
294 tag_at_put(cp_index, JVM_CONSTANT_MethodHandle);
295 *int_at_addr(cp_index) = ((jint) ref_index<<16) | ref_kind;
587 constantTag tag_ref_at(int cp_cache_index, Bytecodes::Code code);
588
589 int to_cp_index(int which, Bytecodes::Code code);
590
591 bool is_resolved(int which, Bytecodes::Code code);
592
593 // Lookup for entries consisting of (name_index, signature_index)
594 u2 name_ref_index_at(int cp_index); // == low-order jshort of name_and_type_at(cp_index)
595 u2 signature_ref_index_at(int cp_index); // == high-order jshort of name_and_type_at(cp_index)
596
597 BasicType basic_type_for_signature_at(int cp_index) const;
598
599 // Resolve string constants (to prevent allocation during compilation)
600 void resolve_string_constants(TRAPS) {
601 constantPoolHandle h_this(THREAD, this);
602 resolve_string_constants_impl(h_this, CHECK);
603 }
604
605 #if INCLUDE_CDS
606 // CDS support
607 refArrayOop prepare_resolved_references_for_archiving() NOT_CDS_JAVA_HEAP_RETURN_(nullptr);
608 void remove_unshareable_info();
609 void restore_unshareable_info(TRAPS);
610 private:
611 void remove_unshareable_entries();
612 void remove_resolved_klass_if_non_deterministic(int cp_index);
613 template <typename Function> void iterate_archivable_resolved_references(Function function);
614 #endif
615
616 private:
617 enum { _no_index_sentinel = -1, _possible_index_sentinel = -2 };
618 public:
619
620 // Get the tag for a constant, which may involve a constant dynamic
621 constantTag constant_tag_at(int cp_index);
622 // Get the basic type for a constant, which may involve a constant dynamic
623 BasicType basic_type_for_constant_at(int cp_index);
624
625 // Resolve late bound constants.
626 oop resolve_constant_at(int cp_index, TRAPS) {
627 constantPoolHandle h_this(THREAD, this);
628 return resolve_constant_at_impl(h_this, cp_index, _no_index_sentinel, nullptr, THREAD);
629 }
630
631 oop resolve_cached_constant_at(int cache_index, TRAPS) {
632 constantPoolHandle h_this(THREAD, this);
633 return resolve_constant_at_impl(h_this, _no_index_sentinel, cache_index, nullptr, THREAD);
634 }
635
636 oop resolve_possibly_cached_constant_at(int cp_index, TRAPS) {
637 constantPoolHandle h_this(THREAD, this);
638 return resolve_constant_at_impl(h_this, cp_index, _possible_index_sentinel, nullptr, THREAD);
639 }
640
641 oop find_cached_constant_at(int cp_index, bool& found_it, TRAPS) {
642 constantPoolHandle h_this(THREAD, this);
643 return resolve_constant_at_impl(h_this, cp_index, _possible_index_sentinel, &found_it, THREAD);
644 }
645
646 void copy_bootstrap_arguments_at(int cp_index,
647 int start_arg, int end_arg,
648 refArrayHandle info, int pos,
649 bool must_resolve, Handle if_not_available, TRAPS) {
650 constantPoolHandle h_this(THREAD, this);
651 copy_bootstrap_arguments_at_impl(h_this, cp_index, start_arg, end_arg,
652 info, pos, must_resolve, if_not_available, THREAD);
653 }
654
655 // Klass name matches name at offset
656 bool klass_name_at_matches(const InstanceKlass* k, int cp_index);
657
658 // Sizing
659 int length() const { return _length; }
660 void set_length(int length) { _length = length; }
661
662 // Tells whether index is within bounds.
663 bool is_within_bounds(int index) const {
664 return 0 <= index && index < length();
665 }
666
667 // Sizing (in words)
668 static int header_size() {
727 return *int_at_addr(cp_index);
728 }
729
730 // Performs the LinkResolver checks
731 static void verify_constant_pool_resolve(const constantPoolHandle& this_cp, Klass* klass, TRAPS);
732
733 // Implementation of methods that needs an exposed 'this' pointer, in order to
734 // handle GC while executing the method
735 static Klass* klass_at_impl(const constantPoolHandle& this_cp, int cp_index, TRAPS);
736 static oop string_at_impl(const constantPoolHandle& this_cp, int cp_index, int obj_index, TRAPS);
737
738 static void trace_class_resolution(const constantPoolHandle& this_cp, Klass* k);
739
740 // Resolve string constants (to prevent allocation during compilation)
741 static void resolve_string_constants_impl(const constantPoolHandle& this_cp, TRAPS);
742
743 static oop resolve_constant_at_impl(const constantPoolHandle& this_cp, int cp_index, int cache_index,
744 bool* status_return, TRAPS);
745 static void copy_bootstrap_arguments_at_impl(const constantPoolHandle& this_cp, int cp_index,
746 int start_arg, int end_arg,
747 refArrayHandle info, int pos,
748 bool must_resolve, Handle if_not_available, TRAPS);
749
750 // Exception handling
751 static void save_and_throw_exception(const constantPoolHandle& this_cp, int cp_index, constantTag tag, TRAPS);
752
753 public:
754 // Exception handling
755 static void throw_resolution_error(const constantPoolHandle& this_cp, int which, TRAPS);
756
757 // Merging ConstantPool* support:
758 bool compare_entry_to(int index1, const constantPoolHandle& cp2, int index2);
759 void copy_cp_to(int start_cpi, int end_cpi, const constantPoolHandle& to_cp, int to_cpi, TRAPS) {
760 constantPoolHandle h_this(THREAD, this);
761 copy_cp_to_impl(h_this, start_cpi, end_cpi, to_cp, to_cpi, THREAD);
762 }
763 static void copy_cp_to_impl(const constantPoolHandle& from_cp, int start_cpi, int end_cpi, const constantPoolHandle& to_cp, int to_cpi, TRAPS);
764 static void copy_entry_to(const constantPoolHandle& from_cp, int from_cpi, const constantPoolHandle& to_cp, int to_cpi);
765 static void copy_bsm_entries(const constantPoolHandle& from_cp, const constantPoolHandle& to_cp, TRAPS);
766 int find_matching_entry(int pattern_i, const constantPoolHandle& search_cp);
767 int version() const { return _saved._version; }
|