229 int object_to_cp_index(int index) { return reference_map()->at(index); }
230 int cp_to_object_index(int index);
231
232 void set_resolved_klasses(Array<Klass*>* rk) { _resolved_klasses = rk; }
233 Array<Klass*>* resolved_klasses() const { return _resolved_klasses; }
234 void allocate_resolved_klasses(ClassLoaderData* loader_data, int num_klasses, TRAPS);
235 void initialize_unresolved_klasses(ClassLoaderData* loader_data, TRAPS);
236
237 // Invokedynamic indexes.
238 // They must look completely different from normal indexes.
239 // The main reason is that byte swapping is sometimes done on normal indexes.
240 // Finally, it is helpful for debugging to tell the two apart.
241 static bool is_invokedynamic_index(int i) { return (i < 0); }
242 static int decode_invokedynamic_index(int i) { assert(is_invokedynamic_index(i), ""); return ~i; }
243 static int encode_invokedynamic_index(int i) { assert(!is_invokedynamic_index(i), ""); return ~i; }
244
245
246 // The invokedynamic points at a CP cache entry. This entry points back
247 // at the original CP entry (CONSTANT_InvokeDynamic) and also (via f2) at an entry
248 // in the resolved_references array (which provides the appendix argument).
249 int invokedynamic_cp_cache_index(int index) const {
250 assert (is_invokedynamic_index(index), "should be a invokedynamic index");
251 int cache_index = decode_invokedynamic_index(index);
252 return cache_index;
253 }
254 ConstantPoolCacheEntry* invokedynamic_cp_cache_entry_at(int index) const {
255 // decode index that invokedynamic points to.
256 int cp_cache_index = invokedynamic_cp_cache_index(index);
257 return cache()->entry_at(cp_cache_index);
258 }
259
260 // Assembly code support
261 static int tags_offset_in_bytes() { return offset_of(ConstantPool, _tags); }
262 static int cache_offset_in_bytes() { return offset_of(ConstantPool, _cache); }
263 static int pool_holder_offset_in_bytes() { return offset_of(ConstantPool, _pool_holder); }
264 static int resolved_klasses_offset_in_bytes() { return offset_of(ConstantPool, _resolved_klasses); }
265
266 // Storing constants
267
268 // For temporary use while constructing constant pool
269 void klass_index_at_put(int which, int name_index) {
270 tag_at_put(which, JVM_CONSTANT_ClassIndex);
271 *int_at_addr(which) = name_index;
272 }
273
274 // Unsafe anonymous class support:
275 void klass_at_put(int class_index, int name_index, int resolved_klass_index, Klass* k, Symbol* name);
276 void klass_at_put(int class_index, Klass* k);
277
278 void unresolved_klass_at_put(int which, int name_index, int resolved_klass_index) {
279 release_tag_at_put(which, JVM_CONSTANT_UnresolvedClass);
280
281 assert((name_index & 0xffff0000) == 0, "must be");
282 assert((resolved_klass_index & 0xffff0000) == 0, "must be");
283 *int_at_addr(which) =
284 build_int_from_shorts((jushort)resolved_klass_index, (jushort)name_index);
285 }
286
287 void method_handle_index_at_put(int which, int ref_kind, int ref_index) {
288 tag_at_put(which, JVM_CONSTANT_MethodHandle);
289 *int_at_addr(which) = ((jint) ref_index<<16) | ref_kind;
290 }
291
292 void method_type_index_at_put(int which, int ref_index) {
293 tag_at_put(which, JVM_CONSTANT_MethodType);
294 *int_at_addr(which) = ref_index;
295 }
296
297 void dynamic_constant_at_put(int which, int bootstrap_specifier_index, int name_and_type_index) {
298 tag_at_put(which, JVM_CONSTANT_Dynamic);
299 *int_at_addr(which) = ((jint) name_and_type_index<<16) | bootstrap_specifier_index;
300 }
301
302 void invoke_dynamic_at_put(int which, int bootstrap_specifier_index, int name_and_type_index) {
303 tag_at_put(which, JVM_CONSTANT_InvokeDynamic);
304 *int_at_addr(which) = ((jint) name_and_type_index<<16) | bootstrap_specifier_index;
305 }
306
307 void unresolved_string_at_put(int which, Symbol* s) {
308 release_tag_at_put(which, JVM_CONSTANT_String);
309 slot_at_put(which, CPSlot(s));
310 }
311
312 void int_at_put(int which, jint i) {
313 tag_at_put(which, JVM_CONSTANT_Integer);
314 *int_at_addr(which) = i;
315 }
316
317 void long_at_put(int which, jlong l) {
318 tag_at_put(which, JVM_CONSTANT_Long);
319 // *long_at_addr(which) = l;
320 Bytes::put_native_u8((address)long_at_addr(which), *((u8*) &l));
321 }
322
323 void float_at_put(int which, jfloat f) {
324 tag_at_put(which, JVM_CONSTANT_Float);
517 }
518
519 // Derived queries:
520 Symbol* method_handle_name_ref_at(int which) {
521 int member = method_handle_index_at(which);
522 return impl_name_ref_at(member, true);
523 }
524 Symbol* method_handle_signature_ref_at(int which) {
525 int member = method_handle_index_at(which);
526 return impl_signature_ref_at(member, true);
527 }
528 int method_handle_klass_index_at(int which) {
529 int member = method_handle_index_at(which);
530 return impl_klass_ref_index_at(member, true);
531 }
532 Symbol* method_type_signature_at(int which) {
533 int sym = method_type_index_at(which);
534 return symbol_at(sym);
535 }
536
537 int invoke_dynamic_name_and_type_ref_index_at(int which) {
538 assert(tag_at(which).is_invoke_dynamic() ||
539 tag_at(which).is_dynamic_constant() ||
540 tag_at(which).is_dynamic_constant_in_error(), "Corrupted constant pool");
541 return extract_high_short_from_int(*int_at_addr(which));
542 }
543 int invoke_dynamic_bootstrap_specifier_index(int which) {
544 assert(tag_at(which).is_invoke_dynamic() ||
545 tag_at(which).is_dynamic_constant() ||
546 tag_at(which).is_dynamic_constant_in_error(), "Corrupted constant pool");
547 return extract_low_short_from_int(*int_at_addr(which));
548 }
549 int invoke_dynamic_operand_base(int which) {
550 int bootstrap_specifier_index = invoke_dynamic_bootstrap_specifier_index(which);
551 return operand_offset_at(operands(), bootstrap_specifier_index);
552 }
553 // The first part of the operands array consists of an index into the second part.
554 // Extract a 32-bit index value from the first part.
555 static int operand_offset_at(Array<u2>* operands, int bootstrap_specifier_index) {
556 int n = (bootstrap_specifier_index * 2);
557 assert(n >= 0 && n+2 <= operands->length(), "oob");
558 // The first 32-bit index points to the beginning of the second part
559 // of the operands array. Make sure this index is in the first part.
560 DEBUG_ONLY(int second_part = build_int_from_shorts(operands->at(0),
561 operands->at(1)));
562 assert(second_part == 0 || n+2 <= second_part, "oob (2)");
563 int offset = build_int_from_shorts(operands->at(n+0),
564 operands->at(n+1));
565 // The offset itself must point into the second part of the array.
566 assert(offset == 0 || offset >= second_part && offset <= operands->length(), "oob (3)");
567 return offset;
568 }
569 static void operand_offset_at_put(Array<u2>* operands, int bootstrap_specifier_index, int offset) {
570 int n = bootstrap_specifier_index * 2;
571 assert(n >= 0 && n+2 <= operands->length(), "oob");
572 operands->at_put(n+0, extract_low_short_from_int(offset));
573 operands->at_put(n+1, extract_high_short_from_int(offset));
574 }
575 static int operand_array_length(Array<u2>* operands) {
576 if (operands == NULL || operands->length() == 0) return 0;
577 int second_part = operand_offset_at(operands, 0);
578 return (second_part / 2);
579 }
580
581 #ifdef ASSERT
582 // operand tuples fit together exactly, end to end
583 static int operand_limit_at(Array<u2>* operands, int bootstrap_specifier_index) {
584 int nextidx = bootstrap_specifier_index + 1;
585 if (nextidx == operand_array_length(operands))
586 return operands->length();
587 else
588 return operand_offset_at(operands, nextidx);
589 }
590 int invoke_dynamic_operand_limit(int which) {
591 int bootstrap_specifier_index = invoke_dynamic_bootstrap_specifier_index(which);
592 return operand_limit_at(operands(), bootstrap_specifier_index);
593 }
594 #endif //ASSERT
595
596 // layout of InvokeDynamic and Dynamic bootstrap method specifier (in second part of operands array):
597 enum {
598 _indy_bsm_offset = 0, // CONSTANT_MethodHandle bsm
599 _indy_argc_offset = 1, // u2 argc
600 _indy_argv_offset = 2 // u2 argv[argc]
601 };
602
603 // These functions are used in RedefineClasses for CP merge
604
605 int operand_offset_at(int bootstrap_specifier_index) {
606 assert(0 <= bootstrap_specifier_index &&
607 bootstrap_specifier_index < operand_array_length(operands()),
608 "Corrupted CP operands");
609 return operand_offset_at(operands(), bootstrap_specifier_index);
610 }
611 int operand_bootstrap_method_ref_index_at(int bootstrap_specifier_index) {
612 int offset = operand_offset_at(bootstrap_specifier_index);
613 return operands()->at(offset + _indy_bsm_offset);
614 }
615 int operand_argument_count_at(int bootstrap_specifier_index) {
616 int offset = operand_offset_at(bootstrap_specifier_index);
617 int argc = operands()->at(offset + _indy_argc_offset);
618 return argc;
619 }
620 int operand_argument_index_at(int bootstrap_specifier_index, int j) {
621 int offset = operand_offset_at(bootstrap_specifier_index);
622 return operands()->at(offset + _indy_argv_offset + j);
623 }
624 int operand_next_offset_at(int bootstrap_specifier_index) {
625 int offset = operand_offset_at(bootstrap_specifier_index) + _indy_argv_offset
626 + operand_argument_count_at(bootstrap_specifier_index);
627 return offset;
628 }
629 // Compare a bootsrap specifier in the operands arrays
630 bool compare_operand_to(int bootstrap_specifier_index1, const constantPoolHandle& cp2,
631 int bootstrap_specifier_index2, TRAPS);
632 // Find a bootsrap specifier in the operands array
633 int find_matching_operand(int bootstrap_specifier_index, const constantPoolHandle& search_cp,
634 int operands_cur_len, TRAPS);
635 // Resize the operands array with delta_len and delta_size
636 void resize_operands(int delta_len, int delta_size, TRAPS);
637 // Extend the operands array with the length and size of the ext_cp operands
638 void extend_operands(const constantPoolHandle& ext_cp, TRAPS);
639 // Shrink the operands array to a smaller array with new_len length
640 void shrink_operands(int new_len, TRAPS);
641
642 int invoke_dynamic_bootstrap_method_ref_index_at(int which) {
643 assert(tag_at(which).is_invoke_dynamic() ||
644 tag_at(which).is_dynamic_constant() ||
645 tag_at(which).is_dynamic_constant_in_error(), "Corrupted constant pool");
646 int op_base = invoke_dynamic_operand_base(which);
647 return operands()->at(op_base + _indy_bsm_offset);
648 }
649 int invoke_dynamic_argument_count_at(int which) {
650 assert(tag_at(which).is_invoke_dynamic() ||
651 tag_at(which).is_dynamic_constant() ||
652 tag_at(which).is_dynamic_constant_in_error(), "Corrupted constant pool");
653 int op_base = invoke_dynamic_operand_base(which);
654 int argc = operands()->at(op_base + _indy_argc_offset);
655 DEBUG_ONLY(int end_offset = op_base + _indy_argv_offset + argc;
656 int next_offset = invoke_dynamic_operand_limit(which));
657 assert(end_offset == next_offset, "matched ending");
658 return argc;
659 }
660 int invoke_dynamic_argument_index_at(int which, int j) {
661 int op_base = invoke_dynamic_operand_base(which);
662 DEBUG_ONLY(int argc = operands()->at(op_base + _indy_argc_offset));
663 assert((uint)j < (uint)argc, "oob");
664 return operands()->at(op_base + _indy_argv_offset + j);
665 }
666
667 // The following methods (name/signature/klass_ref_at, klass_ref_at_noresolve,
668 // name_and_type_ref_index_at) all expect to be passed indices obtained
669 // directly from the bytecode.
670 // If the indices are meant to refer to fields or methods, they are
671 // actually rewritten constant pool cache indices.
672 // The routine remap_instruction_operand_from_cache manages the adjustment
673 // of these values back to constant pool indices.
674
675 // There are also "uncached" versions which do not adjust the operand index; see below.
676
677 // FIXME: Consider renaming these with a prefix "cached_" to make the distinction clear.
678 // In a few cases (the verifier) there are uses before a cpcache has been built,
679 // which are handled by a dynamic check in remap_instruction_operand_from_cache.
680 // FIXME: Remove the dynamic check, and adjust all callers to specify the correct mode.
681
728 oop resolve_constant_at(int index, TRAPS) {
729 constantPoolHandle h_this(THREAD, this);
730 return resolve_constant_at_impl(h_this, index, _no_index_sentinel, NULL, THREAD);
731 }
732
733 oop resolve_cached_constant_at(int cache_index, TRAPS) {
734 constantPoolHandle h_this(THREAD, this);
735 return resolve_constant_at_impl(h_this, _no_index_sentinel, cache_index, NULL, THREAD);
736 }
737
738 oop resolve_possibly_cached_constant_at(int pool_index, TRAPS) {
739 constantPoolHandle h_this(THREAD, this);
740 return resolve_constant_at_impl(h_this, pool_index, _possible_index_sentinel, NULL, THREAD);
741 }
742
743 oop find_cached_constant_at(int pool_index, bool& found_it, TRAPS) {
744 constantPoolHandle h_this(THREAD, this);
745 return resolve_constant_at_impl(h_this, pool_index, _possible_index_sentinel, &found_it, THREAD);
746 }
747
748 oop resolve_bootstrap_specifier_at(int index, TRAPS) {
749 constantPoolHandle h_this(THREAD, this);
750 return resolve_bootstrap_specifier_at_impl(h_this, index, THREAD);
751 }
752
753 void copy_bootstrap_arguments_at(int index,
754 int start_arg, int end_arg,
755 objArrayHandle info, int pos,
756 bool must_resolve, Handle if_not_available, TRAPS) {
757 constantPoolHandle h_this(THREAD, this);
758 copy_bootstrap_arguments_at_impl(h_this, index, start_arg, end_arg,
759 info, pos, must_resolve, if_not_available, THREAD);
760 }
761
762 // Klass name matches name at offset
763 bool klass_name_at_matches(const InstanceKlass* k, int which);
764
765 // Sizing
766 int length() const { return _length; }
767 void set_length(int length) { _length = length; }
768
769 // Tells whether index is within bounds.
770 bool is_within_bounds(int index) const {
771 return 0 <= index && index < length();
772 }
773
774 // Sizing (in words)
775 static int header_size() {
776 return align_up((int)sizeof(ConstantPool), wordSize) / wordSize;
777 }
778 static int size(int length) { return align_metadata_size(header_size() + length); }
779 int size() const { return size(length()); }
780 #if INCLUDE_SERVICES
781 void collect_statistics(KlassSizeStats *sz) const;
782 #endif
783
784 // ConstantPools should be stored in the read-only region of CDS archive.
785 static bool is_read_only_by_default() { return true; }
786
787 friend class ClassFileParser;
788 friend class SystemDictionary;
789
790 // Used by CDS. These classes need to access the private ConstantPool() constructor.
791 template <class T> friend class CppVtableTesterA;
792 template <class T> friend class CppVtableTesterB;
793 template <class T> friend class CppVtableCloner;
794
795 // Used by compiler to prevent classloading.
796 static Method* method_at_if_loaded (const constantPoolHandle& this_cp, int which);
797 static bool has_appendix_at_if_loaded (const constantPoolHandle& this_cp, int which);
798 static oop appendix_at_if_loaded (const constantPoolHandle& this_cp, int which);
799 static bool has_method_type_at_if_loaded (const constantPoolHandle& this_cp, int which);
800 static oop method_type_at_if_loaded (const constantPoolHandle& this_cp, int which);
801 static Klass* klass_at_if_loaded (const constantPoolHandle& this_cp, int which);
802
803 // Routines currently used for annotations (only called by jvm.cpp) but which might be used in the
804 // future by other Java code. These take constant pool indices rather than
805 // constant pool cache indices as do the peer methods above.
806 Symbol* uncached_klass_ref_at_noresolve(int which);
807 Symbol* uncached_name_ref_at(int which) { return impl_name_ref_at(which, true); }
808 Symbol* uncached_signature_ref_at(int which) { return impl_signature_ref_at(which, true); }
809 int uncached_klass_ref_index_at(int which) { return impl_klass_ref_index_at(which, true); }
810 int uncached_name_and_type_ref_index_at(int which) { return impl_name_and_type_ref_index_at(which, true); }
811
812 // Sharing
813 int pre_resolve_shared_klasses(TRAPS);
814
815 // Debugging
816 const char* printable_name_at(int which) PRODUCT_RETURN0;
817
818 #ifdef ASSERT
819 enum { CPCACHE_INDEX_TAG = 0x10000 }; // helps keep CP cache indices distinct from CP indices
820 #else
854 assert(tag_at(which).is_string_index(), "Corrupted constant pool");
855 return *int_at_addr(which);
856 }
857
858 // Performs the LinkResolver checks
859 static void verify_constant_pool_resolve(const constantPoolHandle& this_cp, Klass* klass, TRAPS);
860
861 // Implementation of methods that needs an exposed 'this' pointer, in order to
862 // handle GC while executing the method
863 static Klass* klass_at_impl(const constantPoolHandle& this_cp, int which,
864 bool save_resolution_error, TRAPS);
865 static oop string_at_impl(const constantPoolHandle& this_cp, int which, int obj_index, TRAPS);
866
867 static void trace_class_resolution(const constantPoolHandle& this_cp, Klass* k);
868
869 // Resolve string constants (to prevent allocation during compilation)
870 static void resolve_string_constants_impl(const constantPoolHandle& this_cp, TRAPS);
871
872 static oop resolve_constant_at_impl(const constantPoolHandle& this_cp, int index, int cache_index,
873 bool* status_return, TRAPS);
874 static oop resolve_bootstrap_specifier_at_impl(const constantPoolHandle& this_cp, int index, TRAPS);
875 static void copy_bootstrap_arguments_at_impl(const constantPoolHandle& this_cp, int index,
876 int start_arg, int end_arg,
877 objArrayHandle info, int pos,
878 bool must_resolve, Handle if_not_available, TRAPS);
879
880 // Exception handling
881 static Symbol* exception_message(const constantPoolHandle& this_cp, int which, constantTag tag, oop pending_exception);
882 static void save_and_throw_exception(const constantPoolHandle& this_cp, int which, constantTag tag, TRAPS);
883
884 public:
885 // Exception handling
886 static void throw_resolution_error(const constantPoolHandle& this_cp, int which, TRAPS);
887
888 // Merging ConstantPool* support:
889 bool compare_entry_to(int index1, const constantPoolHandle& cp2, int index2, TRAPS);
890 void copy_cp_to(int start_i, int end_i, const constantPoolHandle& to_cp, int to_i, TRAPS) {
891 constantPoolHandle h_this(THREAD, this);
892 copy_cp_to_impl(h_this, start_i, end_i, to_cp, to_i, THREAD);
893 }
894 static void copy_cp_to_impl(const constantPoolHandle& from_cp, int start_i, int end_i, const constantPoolHandle& to_cp, int to_i, TRAPS);
895 static void copy_entry_to(const constantPoolHandle& from_cp, int from_i, const constantPoolHandle& to_cp, int to_i, TRAPS);
896 static void copy_operands(const constantPoolHandle& from_cp, const constantPoolHandle& to_cp, TRAPS);
897 int find_matching_entry(int pattern_i, const constantPoolHandle& search_cp, TRAPS);
898 int version() const { return _saved._version; }
|
229 int object_to_cp_index(int index) { return reference_map()->at(index); }
230 int cp_to_object_index(int index);
231
232 void set_resolved_klasses(Array<Klass*>* rk) { _resolved_klasses = rk; }
233 Array<Klass*>* resolved_klasses() const { return _resolved_klasses; }
234 void allocate_resolved_klasses(ClassLoaderData* loader_data, int num_klasses, TRAPS);
235 void initialize_unresolved_klasses(ClassLoaderData* loader_data, TRAPS);
236
237 // Invokedynamic indexes.
238 // They must look completely different from normal indexes.
239 // The main reason is that byte swapping is sometimes done on normal indexes.
240 // Finally, it is helpful for debugging to tell the two apart.
241 static bool is_invokedynamic_index(int i) { return (i < 0); }
242 static int decode_invokedynamic_index(int i) { assert(is_invokedynamic_index(i), ""); return ~i; }
243 static int encode_invokedynamic_index(int i) { assert(!is_invokedynamic_index(i), ""); return ~i; }
244
245
246 // The invokedynamic points at a CP cache entry. This entry points back
247 // at the original CP entry (CONSTANT_InvokeDynamic) and also (via f2) at an entry
248 // in the resolved_references array (which provides the appendix argument).
249 int invokedynamic_cp_cache_index(int indy_index) const {
250 assert(is_invokedynamic_index(indy_index), "should be a invokedynamic index");
251 int cache_index = decode_invokedynamic_index(indy_index);
252 return cache_index;
253 }
254 ConstantPoolCacheEntry* invokedynamic_cp_cache_entry_at(int indy_index) const {
255 // decode index that invokedynamic points to.
256 int cp_cache_index = invokedynamic_cp_cache_index(indy_index);
257 return cache()->entry_at(cp_cache_index);
258 }
259 // Given the per-instruction index of an indy instruction, report the
260 // main constant pool entry for its bootstrap specifier.
261 // From there, uncached_name/signature_ref_at will get the name/type.
262 int invokedynamic_bootstrap_ref_index_at(int indy_index) const {
263 return invokedynamic_cp_cache_entry_at(indy_index)->constant_pool_index();
264 }
265
266 // Assembly code support
267 static int tags_offset_in_bytes() { return offset_of(ConstantPool, _tags); }
268 static int cache_offset_in_bytes() { return offset_of(ConstantPool, _cache); }
269 static int pool_holder_offset_in_bytes() { return offset_of(ConstantPool, _pool_holder); }
270 static int resolved_klasses_offset_in_bytes() { return offset_of(ConstantPool, _resolved_klasses); }
271
272 // Storing constants
273
274 // For temporary use while constructing constant pool
275 void klass_index_at_put(int which, int name_index) {
276 tag_at_put(which, JVM_CONSTANT_ClassIndex);
277 *int_at_addr(which) = name_index;
278 }
279
280 // Unsafe anonymous class support:
281 void klass_at_put(int class_index, int name_index, int resolved_klass_index, Klass* k, Symbol* name);
282 void klass_at_put(int class_index, Klass* k);
283
284 void unresolved_klass_at_put(int which, int name_index, int resolved_klass_index) {
285 release_tag_at_put(which, 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(which) =
290 build_int_from_shorts((jushort)resolved_klass_index, (jushort)name_index);
291 }
292
293 void method_handle_index_at_put(int which, int ref_kind, int ref_index) {
294 tag_at_put(which, JVM_CONSTANT_MethodHandle);
295 *int_at_addr(which) = ((jint) ref_index<<16) | ref_kind;
296 }
297
298 void method_type_index_at_put(int which, int ref_index) {
299 tag_at_put(which, JVM_CONSTANT_MethodType);
300 *int_at_addr(which) = ref_index;
301 }
302
303 void dynamic_constant_at_put(int which, int bsms_attribute_index, int name_and_type_index) {
304 tag_at_put(which, JVM_CONSTANT_Dynamic);
305 *int_at_addr(which) = ((jint) name_and_type_index<<16) | bsms_attribute_index;
306 }
307
308 void invoke_dynamic_at_put(int which, int bsms_attribute_index, int name_and_type_index) {
309 tag_at_put(which, JVM_CONSTANT_InvokeDynamic);
310 *int_at_addr(which) = ((jint) name_and_type_index<<16) | bsms_attribute_index;
311 }
312
313 void unresolved_string_at_put(int which, Symbol* s) {
314 release_tag_at_put(which, JVM_CONSTANT_String);
315 slot_at_put(which, CPSlot(s));
316 }
317
318 void int_at_put(int which, jint i) {
319 tag_at_put(which, JVM_CONSTANT_Integer);
320 *int_at_addr(which) = i;
321 }
322
323 void long_at_put(int which, jlong l) {
324 tag_at_put(which, JVM_CONSTANT_Long);
325 // *long_at_addr(which) = l;
326 Bytes::put_native_u8((address)long_at_addr(which), *((u8*) &l));
327 }
328
329 void float_at_put(int which, jfloat f) {
330 tag_at_put(which, JVM_CONSTANT_Float);
523 }
524
525 // Derived queries:
526 Symbol* method_handle_name_ref_at(int which) {
527 int member = method_handle_index_at(which);
528 return impl_name_ref_at(member, true);
529 }
530 Symbol* method_handle_signature_ref_at(int which) {
531 int member = method_handle_index_at(which);
532 return impl_signature_ref_at(member, true);
533 }
534 int method_handle_klass_index_at(int which) {
535 int member = method_handle_index_at(which);
536 return impl_klass_ref_index_at(member, true);
537 }
538 Symbol* method_type_signature_at(int which) {
539 int sym = method_type_index_at(which);
540 return symbol_at(sym);
541 }
542
543 int bootstrap_name_and_type_ref_index_at(int which) {
544 assert(tag_at(which).has_bootstrap(), "Corrupted constant pool");
545 return extract_high_short_from_int(*int_at_addr(which));
546 }
547 int bootstrap_methods_attribute_index(int which) {
548 assert(tag_at(which).has_bootstrap(), "Corrupted constant pool");
549 return extract_low_short_from_int(*int_at_addr(which));
550 }
551 int bootstrap_operand_base(int which) {
552 int bsms_attribute_index = bootstrap_methods_attribute_index(which);
553 return operand_offset_at(operands(), bsms_attribute_index);
554 }
555 // The first part of the operands array consists of an index into the second part.
556 // Extract a 32-bit index value from the first part.
557 static int operand_offset_at(Array<u2>* operands, int bsms_attribute_index) {
558 int n = (bsms_attribute_index * 2);
559 assert(n >= 0 && n+2 <= operands->length(), "oob");
560 // The first 32-bit index points to the beginning of the second part
561 // of the operands array. Make sure this index is in the first part.
562 DEBUG_ONLY(int second_part = build_int_from_shorts(operands->at(0),
563 operands->at(1)));
564 assert(second_part == 0 || n+2 <= second_part, "oob (2)");
565 int offset = build_int_from_shorts(operands->at(n+0),
566 operands->at(n+1));
567 // The offset itself must point into the second part of the array.
568 assert(offset == 0 || offset >= second_part && offset <= operands->length(), "oob (3)");
569 return offset;
570 }
571 static void operand_offset_at_put(Array<u2>* operands, int bsms_attribute_index, int offset) {
572 int n = bsms_attribute_index * 2;
573 assert(n >= 0 && n+2 <= operands->length(), "oob");
574 operands->at_put(n+0, extract_low_short_from_int(offset));
575 operands->at_put(n+1, extract_high_short_from_int(offset));
576 }
577 static int operand_array_length(Array<u2>* operands) {
578 if (operands == NULL || operands->length() == 0) return 0;
579 int second_part = operand_offset_at(operands, 0);
580 return (second_part / 2);
581 }
582
583 #ifdef ASSERT
584 // operand tuples fit together exactly, end to end
585 static int operand_limit_at(Array<u2>* operands, int bsms_attribute_index) {
586 int nextidx = bsms_attribute_index + 1;
587 if (nextidx == operand_array_length(operands))
588 return operands->length();
589 else
590 return operand_offset_at(operands, nextidx);
591 }
592 int bootstrap_operand_limit(int which) {
593 int bsms_attribute_index = bootstrap_methods_attribute_index(which);
594 return operand_limit_at(operands(), bsms_attribute_index);
595 }
596 #endif //ASSERT
597
598 // Layout of InvokeDynamic and Dynamic bootstrap method specifier
599 // data in second part of operands array. This encodes one record in
600 // the BootstrapMethods attribute. The whole specifier also includes
601 // the name and type information from the main constant pool entry.
602 enum {
603 _indy_bsm_offset = 0, // CONSTANT_MethodHandle bsm
604 _indy_argc_offset = 1, // u2 argc
605 _indy_argv_offset = 2 // u2 argv[argc]
606 };
607
608 // These functions are used in RedefineClasses for CP merge
609
610 int operand_offset_at(int bsms_attribute_index) {
611 assert(0 <= bsms_attribute_index &&
612 bsms_attribute_index < operand_array_length(operands()),
613 "Corrupted CP operands");
614 return operand_offset_at(operands(), bsms_attribute_index);
615 }
616 int operand_bootstrap_method_ref_index_at(int bsms_attribute_index) {
617 int offset = operand_offset_at(bsms_attribute_index);
618 return operands()->at(offset + _indy_bsm_offset);
619 }
620 int operand_argument_count_at(int bsms_attribute_index) {
621 int offset = operand_offset_at(bsms_attribute_index);
622 int argc = operands()->at(offset + _indy_argc_offset);
623 return argc;
624 }
625 int operand_argument_index_at(int bsms_attribute_index, int j) {
626 int offset = operand_offset_at(bsms_attribute_index);
627 return operands()->at(offset + _indy_argv_offset + j);
628 }
629 int operand_next_offset_at(int bsms_attribute_index) {
630 int offset = operand_offset_at(bsms_attribute_index) + _indy_argv_offset
631 + operand_argument_count_at(bsms_attribute_index);
632 return offset;
633 }
634 // Compare a bootstrap specifier data in the operands arrays
635 bool compare_operand_to(int bsms_attribute_index1, const constantPoolHandle& cp2,
636 int bsms_attribute_index2, TRAPS);
637 // Find a bootstrap specifier data in the operands array
638 int find_matching_operand(int bsms_attribute_index, const constantPoolHandle& search_cp,
639 int operands_cur_len, TRAPS);
640 // Resize the operands array with delta_len and delta_size
641 void resize_operands(int delta_len, int delta_size, TRAPS);
642 // Extend the operands array with the length and size of the ext_cp operands
643 void extend_operands(const constantPoolHandle& ext_cp, TRAPS);
644 // Shrink the operands array to a smaller array with new_len length
645 void shrink_operands(int new_len, TRAPS);
646
647 int bootstrap_method_ref_index_at(int which) {
648 assert(tag_at(which).has_bootstrap(), "Corrupted constant pool");
649 int op_base = bootstrap_operand_base(which);
650 return operands()->at(op_base + _indy_bsm_offset);
651 }
652 int bootstrap_argument_count_at(int which) {
653 assert(tag_at(which).has_bootstrap(), "Corrupted constant pool");
654 int op_base = bootstrap_operand_base(which);
655 int argc = operands()->at(op_base + _indy_argc_offset);
656 DEBUG_ONLY(int end_offset = op_base + _indy_argv_offset + argc;
657 int next_offset = bootstrap_operand_limit(which));
658 assert(end_offset == next_offset, "matched ending");
659 return argc;
660 }
661 int bootstrap_argument_index_at(int which, int j) {
662 int op_base = bootstrap_operand_base(which);
663 DEBUG_ONLY(int argc = operands()->at(op_base + _indy_argc_offset));
664 assert((uint)j < (uint)argc, "oob");
665 return operands()->at(op_base + _indy_argv_offset + j);
666 }
667
668 // The following methods (name/signature/klass_ref_at, klass_ref_at_noresolve,
669 // name_and_type_ref_index_at) all expect to be passed indices obtained
670 // directly from the bytecode.
671 // If the indices are meant to refer to fields or methods, they are
672 // actually rewritten constant pool cache indices.
673 // The routine remap_instruction_operand_from_cache manages the adjustment
674 // of these values back to constant pool indices.
675
676 // There are also "uncached" versions which do not adjust the operand index; see below.
677
678 // FIXME: Consider renaming these with a prefix "cached_" to make the distinction clear.
679 // In a few cases (the verifier) there are uses before a cpcache has been built,
680 // which are handled by a dynamic check in remap_instruction_operand_from_cache.
681 // FIXME: Remove the dynamic check, and adjust all callers to specify the correct mode.
682
729 oop resolve_constant_at(int index, TRAPS) {
730 constantPoolHandle h_this(THREAD, this);
731 return resolve_constant_at_impl(h_this, index, _no_index_sentinel, NULL, THREAD);
732 }
733
734 oop resolve_cached_constant_at(int cache_index, TRAPS) {
735 constantPoolHandle h_this(THREAD, this);
736 return resolve_constant_at_impl(h_this, _no_index_sentinel, cache_index, NULL, THREAD);
737 }
738
739 oop resolve_possibly_cached_constant_at(int pool_index, TRAPS) {
740 constantPoolHandle h_this(THREAD, this);
741 return resolve_constant_at_impl(h_this, pool_index, _possible_index_sentinel, NULL, THREAD);
742 }
743
744 oop find_cached_constant_at(int pool_index, bool& found_it, TRAPS) {
745 constantPoolHandle h_this(THREAD, this);
746 return resolve_constant_at_impl(h_this, pool_index, _possible_index_sentinel, &found_it, THREAD);
747 }
748
749 enum BootstrapArgumentReferenceMode { R_IFPRESENT = 0, R_FORCE = 1, R_SYMREF = 2 };
750
751 void copy_bootstrap_arguments_at(int index,
752 int start_arg, int end_arg,
753 arrayHandle buf, int pos,
754 BootstrapArgumentReferenceMode resolving,
755 Handle if_not_available, Handle if_null_constant,
756 bool skip_non_null, // don't overwrite previous stuff in buf
757 bool skip_recursion, // don't resolve CONSTANT_Dynamic args
758 TRAPS) {
759 constantPoolHandle h_this(THREAD, this);
760 copy_bootstrap_arguments_at_impl(h_this, index, start_arg, end_arg,
761 buf, pos,
762 resolving, if_not_available, if_null_constant,
763 skip_non_null, skip_recursion, THREAD);
764 }
765
766 // Klass name matches name at offset
767 bool klass_name_at_matches(const InstanceKlass* k, int which);
768
769 // Sizing
770 int length() const { return _length; }
771 void set_length(int length) { _length = length; }
772
773 // Tells whether index is within bounds.
774 bool is_within_bounds(int index) const {
775 return 0 <= index && index < length();
776 }
777
778 // Sizing (in words)
779 static int header_size() {
780 return align_up((int)sizeof(ConstantPool), wordSize) / wordSize;
781 }
782 static int size(int length) { return align_metadata_size(header_size() + length); }
783 int size() const { return size(length()); }
784 #if INCLUDE_SERVICES
785 void collect_statistics(KlassSizeStats *sz) const;
786 #endif
787
788 // ConstantPools should be stored in the read-only region of CDS archive.
789 static bool is_read_only_by_default() { return true; }
790
791 friend class ClassFileParser;
792 friend class SystemDictionary;
793
794 // Used by CDS. These classes need to access the private ConstantPool() constructor.
795 template <class T> friend class CppVtableTesterA;
796 template <class T> friend class CppVtableTesterB;
797 template <class T> friend class CppVtableCloner;
798
799 // Used by compiler to prevent classloading.
800 static Method* method_at_if_loaded (const constantPoolHandle& this_cp, int which);
801 static bool has_appendix_at_if_loaded (const constantPoolHandle& this_cp, int which);
802 static oop appendix_at_if_loaded (const constantPoolHandle& this_cp, int which);
803 static bool has_local_signature_at_if_loaded (const constantPoolHandle& this_cp, int which);
804 static Klass* klass_at_if_loaded (const constantPoolHandle& this_cp, int which);
805
806 // Routines currently used for annotations (only called by jvm.cpp) but which might be used in the
807 // future by other Java code. These take constant pool indices rather than
808 // constant pool cache indices as do the peer methods above.
809 Symbol* uncached_klass_ref_at_noresolve(int which);
810 Symbol* uncached_name_ref_at(int which) { return impl_name_ref_at(which, true); }
811 Symbol* uncached_signature_ref_at(int which) { return impl_signature_ref_at(which, true); }
812 int uncached_klass_ref_index_at(int which) { return impl_klass_ref_index_at(which, true); }
813 int uncached_name_and_type_ref_index_at(int which) { return impl_name_and_type_ref_index_at(which, true); }
814
815 // Sharing
816 int pre_resolve_shared_klasses(TRAPS);
817
818 // Debugging
819 const char* printable_name_at(int which) PRODUCT_RETURN0;
820
821 #ifdef ASSERT
822 enum { CPCACHE_INDEX_TAG = 0x10000 }; // helps keep CP cache indices distinct from CP indices
823 #else
857 assert(tag_at(which).is_string_index(), "Corrupted constant pool");
858 return *int_at_addr(which);
859 }
860
861 // Performs the LinkResolver checks
862 static void verify_constant_pool_resolve(const constantPoolHandle& this_cp, Klass* klass, TRAPS);
863
864 // Implementation of methods that needs an exposed 'this' pointer, in order to
865 // handle GC while executing the method
866 static Klass* klass_at_impl(const constantPoolHandle& this_cp, int which,
867 bool save_resolution_error, TRAPS);
868 static oop string_at_impl(const constantPoolHandle& this_cp, int which, int obj_index, TRAPS);
869
870 static void trace_class_resolution(const constantPoolHandle& this_cp, Klass* k);
871
872 // Resolve string constants (to prevent allocation during compilation)
873 static void resolve_string_constants_impl(const constantPoolHandle& this_cp, TRAPS);
874
875 static oop resolve_constant_at_impl(const constantPoolHandle& this_cp, int index, int cache_index,
876 bool* status_return, TRAPS);
877 static void copy_bootstrap_arguments_at_impl(const constantPoolHandle& this_cp, int index,
878 int start_arg, int end_arg,
879 arrayHandle buf, int pos,
880 BootstrapArgumentReferenceMode resolving,
881 Handle if_not_available,
882 Handle if_null_constant,
883 bool skip_non_null,
884 bool skip_recursion,
885 TRAPS);
886
887 // Exception handling
888 static Symbol* exception_message(const constantPoolHandle& this_cp, int which, constantTag tag, oop pending_exception);
889 static void save_and_throw_exception(const constantPoolHandle& this_cp, int which, constantTag tag, TRAPS);
890
891 public:
892 // Exception handling
893 static void throw_resolution_error(const constantPoolHandle& this_cp, int which, TRAPS);
894
895 // Merging ConstantPool* support:
896 bool compare_entry_to(int index1, const constantPoolHandle& cp2, int index2, TRAPS);
897 void copy_cp_to(int start_i, int end_i, const constantPoolHandle& to_cp, int to_i, TRAPS) {
898 constantPoolHandle h_this(THREAD, this);
899 copy_cp_to_impl(h_this, start_i, end_i, to_cp, to_i, THREAD);
900 }
901 static void copy_cp_to_impl(const constantPoolHandle& from_cp, int start_i, int end_i, const constantPoolHandle& to_cp, int to_i, TRAPS);
902 static void copy_entry_to(const constantPoolHandle& from_cp, int from_i, const constantPoolHandle& to_cp, int to_i, TRAPS);
903 static void copy_operands(const constantPoolHandle& from_cp, const constantPoolHandle& to_cp, TRAPS);
904 int find_matching_entry(int pattern_i, const constantPoolHandle& search_cp, TRAPS);
905 int version() const { return _saved._version; }
|