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 *
23 */
24
25 #include "ci/ciField.hpp"
26 #include "ci/ciInstance.hpp"
27 #include "ci/ciInstanceKlass.hpp"
28 #include "ci/ciUtilities.inline.hpp"
29 #include "classfile/javaClasses.hpp"
30 #include "classfile/vmClasses.hpp"
31 #include "memory/allocation.hpp"
32 #include "memory/allocation.inline.hpp"
33 #include "memory/resourceArea.hpp"
34 #include "oops/fieldStreams.inline.hpp"
35 #include "oops/instanceKlass.inline.hpp"
36 #include "oops/klass.inline.hpp"
37 #include "oops/oop.inline.hpp"
38 #include "runtime/fieldDescriptor.inline.hpp"
39 #include "runtime/handles.inline.hpp"
40 #include "runtime/jniHandles.inline.hpp"
41
42 // ciInstanceKlass
43 //
44 // This class represents a Klass* in the HotSpot virtual machine
45 // whose Klass part in an InstanceKlass.
46
47
48 // ------------------------------------------------------------------
49 // ciInstanceKlass::ciInstanceKlass
50 //
51 // Loaded instance klass.
52 ciInstanceKlass::ciInstanceKlass(Klass* k) :
53 ciKlass(k)
54 {
55 assert(get_Klass()->is_instance_klass(), "wrong type");
56 assert(get_instanceKlass()->is_loaded(), "must be at least loaded");
57 InstanceKlass* ik = get_instanceKlass();
58
59 AccessFlags access_flags = ik->access_flags();
60 _flags = ciFlags(access_flags);
61 _has_finalizer = ik->has_finalizer();
62 _has_subklass = flags().is_final() ? subklass_false : subklass_unknown;
63 _init_state = ik->init_state();
64 _has_nonstatic_fields = ik->has_nonstatic_fields();
65 _has_nonstatic_concrete_methods = ik->has_nonstatic_concrete_methods();
66 _is_hidden = ik->is_hidden();
67 _is_record = ik->is_record();
68 _trust_final_fields = ik->trust_final_fields();
69 _nonstatic_fields = nullptr; // initialized lazily by compute_nonstatic_fields:
70 _has_injected_fields = -1;
71 _implementor = nullptr; // we will fill these lazily
72 _transitive_interfaces = nullptr;
73
74 // Ensure that the metadata wrapped by the ciMetadata is kept alive by GC.
75 // This is primarily useful for metadata which is considered as weak roots
76 // by the GC but need to be strong roots if reachable from a current compilation.
77 // InstanceKlass are created for both weak and strong metadata. Ensuring this metadata
78 // alive covers the cases where there are weak roots without performance cost.
79 oop holder = ik->klass_holder();
80 if (ik->class_loader_data()->has_class_mirror_holder()) {
81 // Though ciInstanceKlass records class loader oop, it's not enough to keep
82 // non-strong hidden classes alive (loader == nullptr). Klass holder should
83 // be used instead. It is enough to record a ciObject, since cached elements are never removed
84 // during ciObjectFactory lifetime. ciObjectFactory itself is created for
85 // every compilation and lives for the whole duration of the compilation.
86 assert(holder != nullptr, "holder of hidden class is the mirror which is never null");
87 (void)CURRENT_ENV->get_object(holder);
88 }
89
90 JavaThread *thread = JavaThread::current();
91 if (ciObjectFactory::is_initialized()) {
98 }
99
100 _has_trusted_loader = compute_has_trusted_loader();
101
102 // Lazy fields get filled in only upon request.
103 _super = nullptr;
104 _java_mirror = nullptr;
105
106 if (is_shared()) {
107 if (k != vmClasses::Object_klass()) {
108 super();
109 }
110 //compute_nonstatic_fields(); // done outside of constructor
111 }
112
113 _field_cache = nullptr;
114 }
115
116 // Version for unloaded classes:
117 ciInstanceKlass::ciInstanceKlass(ciSymbol* name,
118 jobject loader)
119 : ciKlass(name, T_OBJECT)
120 {
121 assert(name->char_at(0) != JVM_SIGNATURE_ARRAY, "not an instance klass");
122 _init_state = (InstanceKlass::ClassState)0;
123 _has_nonstatic_fields = false;
124 _nonstatic_fields = nullptr;
125 _has_injected_fields = -1;
126 _is_hidden = false;
127 _is_record = false;
128 _loader = loader;
129 _is_shared = false;
130 _super = nullptr;
131 _java_mirror = nullptr;
132 _field_cache = nullptr;
133 _has_trusted_loader = compute_has_trusted_loader();
134 }
135
136
137
138 // ------------------------------------------------------------------
139 InstanceKlass::ClassState ciInstanceKlass::compute_init_state() {
140 if (_is_shared && is_loaded()) {
141 // Return cached init state of shared klass
142 ciEnv* env = CURRENT_ENV;
143 assert(env->task() != nullptr, "only calls from compilation are expected here");
144 return env->get_cached_init_state(ident());
370 if (!is_abstract()) return nullptr; // Only applies to abstract classes.
371 if (!has_subklass()) return nullptr; // Must have at least one subklass.
372 VM_ENTRY_MARK;
373 InstanceKlass* ik = get_instanceKlass();
374 Klass* up = ik->up_cast_abstract();
375 assert(up->is_instance_klass(), "must be InstanceKlass");
376 if (ik == up) {
377 return nullptr;
378 }
379 return CURRENT_THREAD_ENV->get_instance_klass(up);
380 }
381
382 // ------------------------------------------------------------------
383 // ciInstanceKlass::has_finalizable_subclass
384 bool ciInstanceKlass::has_finalizable_subclass() {
385 if (!is_loaded()) return true;
386 VM_ENTRY_MARK;
387 return Dependencies::find_finalizable_subclass(get_instanceKlass()) != nullptr;
388 }
389
390 // ------------------------------------------------------------------
391 // ciInstanceKlass::contains_field_offset
392 bool ciInstanceKlass::contains_field_offset(int offset) {
393 VM_ENTRY_MARK;
394 return get_instanceKlass()->contains_field_offset(offset);
395 }
396
397 ciField* ciInstanceKlass::get_nonstatic_field_by_offset(const int field_offset) {
398 for (int i = 0, len = nof_nonstatic_fields(); i < len; i++) {
399 ciField* field = _nonstatic_fields->at(i);
400 int field_off = field->offset_in_bytes();
401 if (field_off == field_offset)
402 return field;
403 }
404 return nullptr;
405 }
406
407 // ------------------------------------------------------------------
408 // ciInstanceKlass::get_field_by_offset
409 ciField* ciInstanceKlass::get_field_by_offset(int field_offset, bool is_static) {
410 if (!is_static) {
411 return get_nonstatic_field_by_offset(field_offset);
412 }
413 VM_ENTRY_MARK;
414 InstanceKlass* k = get_instanceKlass();
415 fieldDescriptor fd;
416 if (!k->find_field_from_offset(field_offset, is_static, &fd)) {
417 return nullptr;
418 }
419 ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd);
420 return field;
421 }
422
423 // ------------------------------------------------------------------
424 // ciInstanceKlass::get_field_by_name
425 ciField* ciInstanceKlass::get_field_by_name(ciSymbol* name, ciSymbol* signature, bool is_static) {
426 VM_ENTRY_MARK;
427 InstanceKlass* k = get_instanceKlass();
428 fieldDescriptor fd;
429 Klass* def = k->find_field(name->get_symbol(), signature->get_symbol(), is_static, &fd);
430 if (def == nullptr) {
431 return nullptr;
432 }
433 ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd);
434 return field;
435 }
436
437 #ifdef ASSERT
438 static void assert_injected_field(InternalFieldStream& fs) {
439 assert(!fs.done(), "invarinat");
440 fieldDescriptor fd = fs.field_descriptor();
441 assert(fd.is_injected(), "invariant");
442 }
443 #endif
444
445 // ------------------------------------------------------------------
446 // ciInstanceKlass::get_injected_instance_field_by_name
447 //
448 // Implements also compute_injected_fields().
449 //
450 ciField* ciInstanceKlass::get_injected_instance_field_by_name(ciSymbol* name, ciSymbol* signature) {
451 VM_ENTRY_MARK;
452 InstanceKlass* const k = get_instanceKlass();
453 const Symbol* const name_symbol = name->get_symbol();
454 assert(name_symbol != nullptr, "invariant");
455 const Symbol* const sig_sym = signature->get_symbol();
456 assert(sig_sym != nullptr, "invariant");
491 ciField* field = get_nonstatic_field_by_offset(field_offset);
492 return field != nullptr ? field->layout_type() : T_ILLEGAL;
493 }
494
495 // Avoid allocating a new ciField by obtaining the field type directly
496 VM_ENTRY_MARK;
497 InstanceKlass* k = get_instanceKlass();
498 fieldDescriptor fd;
499 if (!k->find_field_from_offset(field_offset, is_static, &fd)) {
500 return T_ILLEGAL;
501 }
502
503 // Reproduce the behavior of ciField::layout_type
504 BasicType field_type = fd.field_type();
505 if (is_reference_type(field_type)) {
506 return T_OBJECT;
507 }
508 return type2field[make(field_type)->basic_type()];
509 }
510
511 // ------------------------------------------------------------------
512 // ciInstanceKlass::compute_nonstatic_fields
513 int ciInstanceKlass::compute_nonstatic_fields() {
514 assert(is_loaded(), "must be loaded");
515
516 if (_nonstatic_fields != nullptr)
517 return _nonstatic_fields->length();
518
519 if (!has_nonstatic_fields()) {
520 Arena* arena = CURRENT_ENV->arena();
521 _nonstatic_fields = new (arena) GrowableArray<ciField*>(arena, 0, 0, nullptr);
522 return 0;
523 }
524 assert(!is_java_lang_Object(), "bootstrap OK");
525
526 ciInstanceKlass* super = this->super();
527 GrowableArray<ciField*>* super_fields = nullptr;
528 if (super != nullptr && super->has_nonstatic_fields()) {
529 int super_flen = super->nof_nonstatic_fields();
530 super_fields = super->_nonstatic_fields;
531 assert(super_flen == 0 || super_fields != nullptr, "first get nof_fields");
532 }
533
534 GrowableArray<ciField*>* fields = nullptr;
535 GUARDED_VM_ENTRY({
536 fields = compute_nonstatic_fields_impl(super_fields);
537 });
538
539 if (fields == nullptr) {
540 // This can happen if this class (java.lang.Class) has invisible fields.
541 if (super_fields != nullptr) {
542 _nonstatic_fields = super_fields;
543 return super_fields->length();
544 } else {
545 return 0;
546 }
547 }
548
549 int flen = fields->length();
550
551 _nonstatic_fields = fields;
552 return flen;
553 }
554
555 GrowableArray<ciField*>*
556 ciInstanceKlass::compute_nonstatic_fields_impl(GrowableArray<ciField*>*
557 super_fields) {
558 ASSERT_IN_VM;
559 Arena* arena = CURRENT_ENV->arena();
560 int flen = 0;
561 GrowableArray<ciField*>* fields = nullptr;
562 InstanceKlass* k = get_instanceKlass();
563 for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
564 if (fs.access_flags().is_static()) continue;
565 flen += 1;
566 }
567
568 // allocate the array:
569 if (flen == 0) {
570 return nullptr; // return nothing if none are locally declared
571 }
572 if (super_fields != nullptr) {
573 flen += super_fields->length();
574 }
575 fields = new (arena) GrowableArray<ciField*>(arena, flen, 0, nullptr);
576 if (super_fields != nullptr) {
577 fields->appendAll(super_fields);
578 }
579
580 for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
581 if (fs.access_flags().is_static()) continue;
582 fieldDescriptor& fd = fs.field_descriptor();
583 ciField* field = new (arena) ciField(&fd);
584 fields->append(field);
585 }
586 assert(fields->length() == flen, "sanity");
587 return fields;
588 }
589
590 bool ciInstanceKlass::compute_injected_fields_helper() {
591 ASSERT_IN_VM;
592 InstanceKlass* k = get_instanceKlass();
593
594 for (InternalFieldStream fs(k); !fs.done(); fs.next()) {
595 if (fs.access_flags().is_static()) continue;
596 return true;
597 }
598 return false;
599 }
600
601 void ciInstanceKlass::compute_injected_fields() {
602 assert(is_loaded(), "must be loaded");
603
604 int has_injected_fields = 0;
605 if (super() != nullptr && super()->has_injected_fields()) {
606 has_injected_fields = 1;
607 } else {
679 } else {
680 // Go into the VM to fetch the implementor.
681 VM_ENTRY_MARK;
682 InstanceKlass* ik = get_instanceKlass();
683 Klass* implk = ik->implementor();
684 if (implk != nullptr) {
685 if (implk == ik) {
686 // More than one implementors. Use 'this' in this case.
687 impl = this;
688 } else {
689 impl = CURRENT_THREAD_ENV->get_instance_klass(implk);
690 }
691 }
692 }
693 // Memoize this result.
694 _implementor = impl;
695 }
696 return impl;
697 }
698
699 // Utility class for printing of the contents of the static fields for
700 // use by compilation replay. It only prints out the information that
701 // could be consumed by the compiler, so for primitive types it prints
702 // out the actual value. For Strings it's the actual string value.
703 // For array types it it's first level array size since that's the
704 // only value which statically unchangeable. For all other reference
705 // types it simply prints out the dynamic type.
706
707 class StaticFinalFieldPrinter : public FieldClosure {
708 outputStream* _out;
709 const char* _holder;
710 public:
711 StaticFinalFieldPrinter(outputStream* out, const char* holder) :
712 _out(out),
713 _holder(holder) {
714 }
715 void do_field(fieldDescriptor* fd) {
716 if (fd->is_final() && !fd->has_initial_value()) {
717 ResourceMark rm;
718 oop mirror = fd->field_holder()->java_mirror();
719 _out->print("staticfield %s %s %s ", _holder, fd->name()->as_quoted_ascii(), fd->signature()->as_quoted_ascii());
720 BasicType field_type = fd->field_type();
721 switch (field_type) {
722 case T_BYTE: _out->print_cr("%d", mirror->byte_field(fd->offset())); break;
723 case T_BOOLEAN: _out->print_cr("%d", mirror->bool_field(fd->offset())); break;
724 case T_SHORT: _out->print_cr("%d", mirror->short_field(fd->offset())); break;
725 case T_CHAR: _out->print_cr("%d", mirror->char_field(fd->offset())); break;
726 case T_INT: _out->print_cr("%d", mirror->int_field(fd->offset())); break;
727 case T_LONG: _out->print_cr(INT64_FORMAT, (int64_t)(mirror->long_field(fd->offset()))); break;
728 case T_FLOAT: {
729 float f = mirror->float_field(fd->offset());
730 _out->print_cr("%d", *(int*)&f);
731 break;
732 }
733 case T_DOUBLE: {
734 double d = mirror->double_field(fd->offset());
735 _out->print_cr(INT64_FORMAT, *(int64_t*)&d);
736 break;
737 }
738 case T_ARRAY: // fall-through
739 case T_OBJECT: {
740 oop value = mirror->obj_field_acquire(fd->offset());
741 if (value == nullptr) {
742 if (field_type == T_ARRAY) {
743 _out->print("%d", -1);
744 }
745 _out->cr();
746 } else if (value->is_instance()) {
747 assert(field_type == T_OBJECT, "");
748 if (value->is_a(vmClasses::String_klass())) {
749 const char* ascii_value = java_lang_String::as_quoted_ascii(value);
750 _out->print_cr("\"%s\"", (ascii_value != nullptr) ? ascii_value : "");
751 } else {
752 const char* klass_name = value->klass()->name()->as_quoted_ascii();
753 _out->print_cr("%s", klass_name);
754 }
755 } else if (value->is_array()) {
756 typeArrayOop ta = (typeArrayOop)value;
757 _out->print("%d", ta->length());
758 if (value->is_objArray()) {
759 objArrayOop oa = (objArrayOop)value;
760 const char* klass_name = value->klass()->name()->as_quoted_ascii();
761 _out->print(" %s", klass_name);
762 }
763 _out->cr();
764 } else {
765 ShouldNotReachHere();
766 }
767 break;
768 }
769 default:
770 ShouldNotReachHere();
771 }
772 }
773 }
774 };
775
776 const char *ciInstanceKlass::replay_name() const {
777 return CURRENT_ENV->replay_name(get_instanceKlass());
778 }
779
780 void ciInstanceKlass::dump_replay_instanceKlass(outputStream* out, InstanceKlass* ik) {
781 if (ik->is_hidden()) {
782 const char *name = CURRENT_ENV->dyno_name(ik);
783 if (name != nullptr) {
784 out->print_cr("instanceKlass %s # %s", name, ik->name()->as_quoted_ascii());
785 } else {
786 out->print_cr("# instanceKlass %s", ik->name()->as_quoted_ascii());
787 }
788 } else {
789 out->print_cr("instanceKlass %s", ik->name()->as_quoted_ascii());
790 }
791 }
792
793 GrowableArray<ciInstanceKlass*>* ciInstanceKlass::transitive_interfaces() const{
794 if (_transitive_interfaces == nullptr) {
|
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 *
23 */
24
25 #include "ci/ciField.hpp"
26 #include "ci/ciInlineKlass.hpp"
27 #include "ci/ciInstance.hpp"
28 #include "ci/ciInstanceKlass.hpp"
29 #include "ci/ciUtilities.inline.hpp"
30 #include "classfile/javaClasses.hpp"
31 #include "classfile/systemDictionary.hpp"
32 #include "classfile/vmClasses.hpp"
33 #include "memory/allocation.hpp"
34 #include "memory/allocation.inline.hpp"
35 #include "memory/resourceArea.hpp"
36 #include "oops/fieldStreams.inline.hpp"
37 #include "oops/instanceKlass.inline.hpp"
38 #include "oops/klass.inline.hpp"
39 #include "oops/oop.inline.hpp"
40 #include "runtime/arguments.hpp"
41 #include "runtime/fieldDescriptor.inline.hpp"
42 #include "runtime/handles.inline.hpp"
43 #include "runtime/jniHandles.inline.hpp"
44
45 // ciInstanceKlass
46 //
47 // This class represents a Klass* in the HotSpot virtual machine
48 // whose Klass part in an InstanceKlass.
49
50
51 // ------------------------------------------------------------------
52 // ciInstanceKlass::ciInstanceKlass
53 //
54 // Loaded instance klass.
55 ciInstanceKlass::ciInstanceKlass(Klass* k) :
56 ciKlass(k)
57 {
58 assert(get_Klass()->is_instance_klass(), "wrong type");
59 assert(get_instanceKlass()->is_loaded(), "must be at least loaded");
60 InstanceKlass* ik = get_instanceKlass();
61
62 AccessFlags access_flags = ik->access_flags();
63 _flags = ciFlags(access_flags);
64 _has_finalizer = ik->has_finalizer();
65 _has_subklass = flags().is_final() ? subklass_false : subklass_unknown;
66 _init_state = ik->init_state();
67 _has_nonstatic_fields = ik->has_nonstatic_fields();
68 _has_nonstatic_concrete_methods = ik->has_nonstatic_concrete_methods();
69 _is_hidden = ik->is_hidden();
70 _is_record = ik->is_record();
71 _declared_nonstatic_fields = nullptr; // initialized lazily by compute_nonstatic_fields
72 _nonstatic_fields = nullptr; // initialized lazily by compute_nonstatic_fields
73 _trust_final_fields = ik->trust_final_fields();
74 _has_injected_fields = -1;
75 _implementor = nullptr; // we will fill these lazily
76 _transitive_interfaces = nullptr;
77
78 // Ensure that the metadata wrapped by the ciMetadata is kept alive by GC.
79 // This is primarily useful for metadata which is considered as weak roots
80 // by the GC but need to be strong roots if reachable from a current compilation.
81 // InstanceKlass are created for both weak and strong metadata. Ensuring this metadata
82 // alive covers the cases where there are weak roots without performance cost.
83 oop holder = ik->klass_holder();
84 if (ik->class_loader_data()->has_class_mirror_holder()) {
85 // Though ciInstanceKlass records class loader oop, it's not enough to keep
86 // non-strong hidden classes alive (loader == nullptr). Klass holder should
87 // be used instead. It is enough to record a ciObject, since cached elements are never removed
88 // during ciObjectFactory lifetime. ciObjectFactory itself is created for
89 // every compilation and lives for the whole duration of the compilation.
90 assert(holder != nullptr, "holder of hidden class is the mirror which is never null");
91 (void)CURRENT_ENV->get_object(holder);
92 }
93
94 JavaThread *thread = JavaThread::current();
95 if (ciObjectFactory::is_initialized()) {
102 }
103
104 _has_trusted_loader = compute_has_trusted_loader();
105
106 // Lazy fields get filled in only upon request.
107 _super = nullptr;
108 _java_mirror = nullptr;
109
110 if (is_shared()) {
111 if (k != vmClasses::Object_klass()) {
112 super();
113 }
114 //compute_nonstatic_fields(); // done outside of constructor
115 }
116
117 _field_cache = nullptr;
118 }
119
120 // Version for unloaded classes:
121 ciInstanceKlass::ciInstanceKlass(ciSymbol* name,
122 jobject loader,
123 BasicType bt)
124 : ciKlass(name, bt)
125 {
126 assert(name->char_at(0) != JVM_SIGNATURE_ARRAY, "not an instance klass");
127 _init_state = (InstanceKlass::ClassState)0;
128 _has_nonstatic_fields = false;
129 _declared_nonstatic_fields = nullptr; // initialized lazily by compute_nonstatic_fields
130 _nonstatic_fields = nullptr; // initialized lazily by compute_nonstatic_fields
131 _has_injected_fields = -1;
132 _is_hidden = false;
133 _is_record = false;
134 _loader = loader;
135 _is_shared = false;
136 _super = nullptr;
137 _java_mirror = nullptr;
138 _field_cache = nullptr;
139 _has_trusted_loader = compute_has_trusted_loader();
140 }
141
142
143
144 // ------------------------------------------------------------------
145 InstanceKlass::ClassState ciInstanceKlass::compute_init_state() {
146 if (_is_shared && is_loaded()) {
147 // Return cached init state of shared klass
148 ciEnv* env = CURRENT_ENV;
149 assert(env->task() != nullptr, "only calls from compilation are expected here");
150 return env->get_cached_init_state(ident());
376 if (!is_abstract()) return nullptr; // Only applies to abstract classes.
377 if (!has_subklass()) return nullptr; // Must have at least one subklass.
378 VM_ENTRY_MARK;
379 InstanceKlass* ik = get_instanceKlass();
380 Klass* up = ik->up_cast_abstract();
381 assert(up->is_instance_klass(), "must be InstanceKlass");
382 if (ik == up) {
383 return nullptr;
384 }
385 return CURRENT_THREAD_ENV->get_instance_klass(up);
386 }
387
388 // ------------------------------------------------------------------
389 // ciInstanceKlass::has_finalizable_subclass
390 bool ciInstanceKlass::has_finalizable_subclass() {
391 if (!is_loaded()) return true;
392 VM_ENTRY_MARK;
393 return Dependencies::find_finalizable_subclass(get_instanceKlass()) != nullptr;
394 }
395
396 bool ciInstanceKlass::contains_field_offset(int offset) const {
397 VM_ENTRY_MARK;
398 return get_instanceKlass()->contains_field_offset(offset);
399 }
400
401 ciField* ciInstanceKlass::get_nonstatic_field_by_offset(const int field_offset) {
402 for (int i = 0, len = nof_nonstatic_fields(); i < len; i++) {
403 ciField* field = nonstatic_field_at(i);
404 int field_off = field->offset_in_bytes();
405 if (field_off == field_offset) {
406 return field;
407 }
408 }
409 return nullptr;
410 }
411
412 // ------------------------------------------------------------------
413 // ciInstanceKlass::get_field_by_offset
414 ciField* ciInstanceKlass::get_field_by_offset(int field_offset, bool is_static) {
415 if (!is_static) {
416 return get_nonstatic_field_by_offset(field_offset);
417 }
418
419 VM_ENTRY_MARK;
420 InstanceKlass* k = get_instanceKlass();
421 fieldDescriptor fd;
422 if (!k->find_field_from_offset(field_offset, is_static, &fd)) {
423 return nullptr;
424 }
425 ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd);
426 return field;
427 }
428
429 ciField* ciInstanceKlass::get_non_flat_field_by_offset(int field_offset) {
430 for (int i = 0, len = nof_declared_nonstatic_fields(); i < len; i++) {
431 ciField* field = declared_nonstatic_field_at(i);
432 int field_off = field->offset_in_bytes();
433 if (field_off == field_offset) {
434 return field;
435 }
436 }
437 return nullptr;
438 }
439
440 int ciInstanceKlass::field_index_by_offset(int offset) {
441 int best_offset = 0;
442 int best_index = -1;
443 // Search the field with the given offset
444 for (int i = 0; i < nof_declared_nonstatic_fields(); ++i) {
445 int field_offset = declared_nonstatic_field_at(i)->offset_in_bytes();
446 if (field_offset == offset) {
447 // Exact match
448 return i;
449 } else if (field_offset < offset && field_offset > best_offset) {
450 // No exact match. Save the index of the field with the closest offset that
451 // is smaller than the given field offset. This index corresponds to the
452 // flat field that holds the field we are looking for.
453 best_offset = field_offset;
454 best_index = i;
455 }
456 }
457 assert(best_index >= 0, "field not found");
458 assert(best_offset == offset || declared_nonstatic_field_at(best_index)->type()->is_inlinetype(), "offset should match for non-inline types");
459 return best_index;
460 }
461
462 // ------------------------------------------------------------------
463 // ciInstanceKlass::get_field_by_name
464 ciField* ciInstanceKlass::get_field_by_name(ciSymbol* name, ciSymbol* signature, bool is_static) {
465 VM_ENTRY_MARK;
466 InstanceKlass* k = get_instanceKlass();
467 fieldDescriptor fd;
468 Klass* def = k->find_field(name->get_symbol(), signature->get_symbol(), is_static, &fd);
469 if (def == nullptr) {
470 return nullptr;
471 }
472 ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd);
473 return field;
474 }
475
476 const GrowableArray<ciField*> empty_field_array(0, MemTag::mtCompiler);
477
478 #ifdef ASSERT
479 static void assert_injected_field(InternalFieldStream& fs) {
480 assert(!fs.done(), "invarinat");
481 fieldDescriptor fd = fs.field_descriptor();
482 assert(fd.is_injected(), "invariant");
483 }
484 #endif
485
486 // ------------------------------------------------------------------
487 // ciInstanceKlass::get_injected_instance_field_by_name
488 //
489 // Implements also compute_injected_fields().
490 //
491 ciField* ciInstanceKlass::get_injected_instance_field_by_name(ciSymbol* name, ciSymbol* signature) {
492 VM_ENTRY_MARK;
493 InstanceKlass* const k = get_instanceKlass();
494 const Symbol* const name_symbol = name->get_symbol();
495 assert(name_symbol != nullptr, "invariant");
496 const Symbol* const sig_sym = signature->get_symbol();
497 assert(sig_sym != nullptr, "invariant");
532 ciField* field = get_nonstatic_field_by_offset(field_offset);
533 return field != nullptr ? field->layout_type() : T_ILLEGAL;
534 }
535
536 // Avoid allocating a new ciField by obtaining the field type directly
537 VM_ENTRY_MARK;
538 InstanceKlass* k = get_instanceKlass();
539 fieldDescriptor fd;
540 if (!k->find_field_from_offset(field_offset, is_static, &fd)) {
541 return T_ILLEGAL;
542 }
543
544 // Reproduce the behavior of ciField::layout_type
545 BasicType field_type = fd.field_type();
546 if (is_reference_type(field_type)) {
547 return T_OBJECT;
548 }
549 return type2field[make(field_type)->basic_type()];
550 }
551
552 void ciInstanceKlass::compute_nonstatic_fields() {
553 assert(is_loaded(), "must be loaded");
554
555 if (_nonstatic_fields != nullptr) {
556 assert(_declared_nonstatic_fields != nullptr, "must be initialized at the same time, class %s", name()->as_utf8());
557 return;
558 }
559
560 if (!has_nonstatic_fields()) {
561 _declared_nonstatic_fields = &empty_field_array;
562 _nonstatic_fields = &empty_field_array;
563 return;
564 }
565 assert(!is_java_lang_Object(), "bootstrap OK");
566
567 ciInstanceKlass* super = this->super();
568 assert(super != nullptr, "must have a super class, current class: %s", name()->as_utf8());
569 super->compute_nonstatic_fields();
570 const GrowableArray<ciField*>* super_declared_fields = super->_declared_nonstatic_fields;
571 const GrowableArray<ciField*>* super_fields = super->_nonstatic_fields;
572 assert(super_declared_fields != nullptr && super_fields != nullptr, "must have been initialized, current class: %s, super class: %s", name()->as_utf8(), super->name()->as_utf8());
573
574 GUARDED_VM_ENTRY({
575 compute_nonstatic_fields_impl(super_declared_fields, super_fields);
576 });
577 }
578
579 void ciInstanceKlass::compute_nonstatic_fields_impl(const GrowableArray<ciField*>* super_declared_fields, const GrowableArray<ciField*>* super_fields) {
580 assert(_declared_nonstatic_fields == nullptr && _nonstatic_fields == nullptr, "initialized already");
581 ASSERT_IN_VM;
582 Arena* arena = CURRENT_ENV->arena();
583
584 InstanceKlass* this_klass = get_instanceKlass();
585 int declared_field_num = 0;
586 int field_num = 0;
587 for (JavaFieldStream fs(this_klass); !fs.done(); fs.next()) {
588 if (fs.access_flags().is_static()) {
589 continue;
590 }
591
592 declared_field_num++;
593
594 fieldDescriptor& fd = fs.field_descriptor();
595 if (fd.is_flat()) {
596 InlineKlass* k = this_klass->get_inline_type_field_klass(fd.index());
597 ciInlineKlass* vk = CURRENT_ENV->get_klass(k)->as_inline_klass();
598 field_num += vk->nof_nonstatic_fields();
599 field_num += fd.has_null_marker() ? 1 : 0;
600 } else {
601 field_num++;
602 }
603 }
604
605 GrowableArray<ciField*>* tmp_declared_fields = nullptr;
606 if (declared_field_num != 0) {
607 tmp_declared_fields = new (arena) GrowableArray<ciField*>(arena, declared_field_num + super_declared_fields->length(), 0, nullptr);
608 tmp_declared_fields->appendAll(super_declared_fields);
609 }
610
611 GrowableArray<ciField*>* tmp_fields = nullptr;
612 if (field_num != 0) {
613 tmp_fields = new (arena) GrowableArray<ciField*>(arena, field_num + super_fields->length(), 0, nullptr);
614 tmp_fields->appendAll(super_fields);
615 }
616
617 // For later assertion
618 declared_field_num += super_declared_fields->length();
619 field_num += super_fields->length();
620
621 for (JavaFieldStream fs(this_klass); !fs.done(); fs.next()) {
622 if (fs.access_flags().is_static()) {
623 continue;
624 }
625
626 fieldDescriptor& fd = fs.field_descriptor();
627 ciField* declared_field = new (arena) ciField(&fd);
628 assert(tmp_declared_fields != nullptr, "should be initialized");
629 tmp_declared_fields->append(declared_field);
630
631 if (fd.is_flat()) {
632 // Flat fields are embedded
633 Klass* k = get_instanceKlass()->get_inline_type_field_klass(fd.index());
634 ciInlineKlass* vk = CURRENT_ENV->get_klass(k)->as_inline_klass();
635 // Iterate over fields of the flat inline type and copy them to 'this'
636 for (int i = 0; i < vk->nof_nonstatic_fields(); ++i) {
637 assert(tmp_fields != nullptr, "should be initialized");
638 tmp_fields->append(new (arena) ciField(declared_field, vk->nonstatic_field_at(i)));
639 }
640 if (fd.has_null_marker()) {
641 assert(tmp_fields != nullptr, "should be initialized");
642 tmp_fields->append(new (arena) ciField(declared_field));
643 }
644 } else {
645 assert(tmp_fields != nullptr, "should be initialized");
646 tmp_fields->append(declared_field);
647 }
648 }
649
650 // Now sort them by offset, ascending. In principle, they could mix with superclass fields.
651 if (tmp_declared_fields != nullptr) {
652 assert(tmp_declared_fields->length() == declared_field_num, "sanity check failed for class: %s, number of declared fields: %d, expected: %d",
653 name()->as_utf8(), tmp_declared_fields->length(), declared_field_num);
654 _declared_nonstatic_fields = tmp_declared_fields;
655 } else {
656 _declared_nonstatic_fields = super_declared_fields;
657 }
658
659 if (tmp_fields != nullptr) {
660 assert(tmp_fields->length() == field_num, "sanity check failed for class: %s, number of fields: %d, expected: %d",
661 name()->as_utf8(), tmp_fields->length(), field_num);
662 _nonstatic_fields = tmp_fields;
663 } else {
664 _nonstatic_fields = super_fields;
665 }
666 }
667
668 bool ciInstanceKlass::compute_injected_fields_helper() {
669 ASSERT_IN_VM;
670 InstanceKlass* k = get_instanceKlass();
671
672 for (InternalFieldStream fs(k); !fs.done(); fs.next()) {
673 if (fs.access_flags().is_static()) continue;
674 return true;
675 }
676 return false;
677 }
678
679 void ciInstanceKlass::compute_injected_fields() {
680 assert(is_loaded(), "must be loaded");
681
682 int has_injected_fields = 0;
683 if (super() != nullptr && super()->has_injected_fields()) {
684 has_injected_fields = 1;
685 } else {
757 } else {
758 // Go into the VM to fetch the implementor.
759 VM_ENTRY_MARK;
760 InstanceKlass* ik = get_instanceKlass();
761 Klass* implk = ik->implementor();
762 if (implk != nullptr) {
763 if (implk == ik) {
764 // More than one implementors. Use 'this' in this case.
765 impl = this;
766 } else {
767 impl = CURRENT_THREAD_ENV->get_instance_klass(implk);
768 }
769 }
770 }
771 // Memoize this result.
772 _implementor = impl;
773 }
774 return impl;
775 }
776
777 bool ciInstanceKlass::can_be_inline_klass(bool is_exact) {
778 if (!Arguments::is_valhalla_enabled()) {
779 return false;
780 }
781 if (!is_loaded() || is_inlinetype()) {
782 // Not loaded or known to be an inline klass
783 return true;
784 }
785 if (!is_exact) {
786 // Not exact, check if this is a valid super for an inline klass
787 GUARDED_VM_ENTRY(
788 return !get_instanceKlass()->access_flags().is_identity_class() || is_java_lang_Object();
789 )
790 }
791 return false;
792 }
793
794 // Utility class for printing of the contents of the static fields for
795 // use by compilation replay. It only prints out the information that
796 // could be consumed by the compiler, so for primitive types it prints
797 // out the actual value. For Strings it's the actual string value.
798 // For array types it it's first level array size since that's the
799 // only value which statically unchangeable. For all other reference
800 // types it simply prints out the dynamic type.
801
802 class StaticFieldPrinter : public FieldClosure {
803 protected:
804 outputStream* _out;
805 public:
806 StaticFieldPrinter(outputStream* out) :
807 _out(out) {
808 }
809 void do_field_helper(fieldDescriptor* fd, oop obj, bool is_flat);
810 };
811
812 class StaticFinalFieldPrinter : public StaticFieldPrinter {
813 const char* _holder;
814 public:
815 StaticFinalFieldPrinter(outputStream* out, const char* holder) :
816 StaticFieldPrinter(out), _holder(holder) {
817 }
818 void do_field(fieldDescriptor* fd) {
819 if (fd->is_final() && !fd->has_initial_value()) {
820 ResourceMark rm;
821 InstanceKlass* holder = fd->field_holder();
822 oop mirror = holder->java_mirror();
823 _out->print("staticfield %s %s ", _holder, fd->name()->as_quoted_ascii());
824 BasicType bt = fd->field_type();
825 if (bt != T_OBJECT && bt != T_ARRAY) {
826 _out->print("%s ", fd->signature()->as_quoted_ascii());
827 }
828 do_field_helper(fd, mirror, false);
829 _out->cr();
830 }
831 }
832 };
833
834 class InlineTypeFieldPrinter : public StaticFieldPrinter {
835 oop _obj;
836 public:
837 InlineTypeFieldPrinter(outputStream* out, oop obj) :
838 StaticFieldPrinter(out), _obj(obj) {
839 }
840 void do_field(fieldDescriptor* fd) {
841 do_field_helper(fd, _obj, true);
842 _out->print(" ");
843 }
844 };
845
846 void StaticFieldPrinter::do_field_helper(fieldDescriptor* fd, oop mirror, bool is_flat) {
847 BasicType field_type = fd->field_type();
848 switch (field_type) {
849 case T_BYTE: _out->print("%d", mirror->byte_field(fd->offset())); break;
850 case T_BOOLEAN: _out->print("%d", mirror->bool_field(fd->offset())); break;
851 case T_SHORT: _out->print("%d", mirror->short_field(fd->offset())); break;
852 case T_CHAR: _out->print("%d", mirror->char_field(fd->offset())); break;
853 case T_INT: _out->print("%d", mirror->int_field(fd->offset())); break;
854 case T_LONG: _out->print(INT64_FORMAT, (int64_t)(mirror->long_field(fd->offset()))); break;
855 case T_FLOAT: {
856 float f = mirror->float_field(fd->offset());
857 _out->print("%d", *(int*)&f);
858 break;
859 }
860 case T_DOUBLE: {
861 double d = mirror->double_field(fd->offset());
862 _out->print(INT64_FORMAT, *(int64_t*)&d);
863 break;
864 }
865 case T_ARRAY: // fall-through
866 case T_OBJECT:
867 if (!fd->is_null_free_inline_type()) {
868 _out->print("%s ", fd->signature()->as_quoted_ascii());
869 oop value = mirror->obj_field_acquire(fd->offset());
870 if (value == nullptr) {
871 if (field_type == T_ARRAY) {
872 _out->print("%d", -1);
873 }
874 _out->cr();
875 } else if (value->is_instance()) {
876 assert(field_type == T_OBJECT, "");
877 if (value->is_a(vmClasses::String_klass())) {
878 const char* ascii_value = java_lang_String::as_quoted_ascii(value);
879 _out->print("\"%s\"", (ascii_value != nullptr) ? ascii_value : "");
880 } else {
881 const char* klass_name = value->klass()->name()->as_quoted_ascii();
882 _out->print("%s", klass_name);
883 }
884 } else if (value->is_array()) {
885 arrayOop a = (arrayOop)value;
886 _out->print("%d", a->length());
887 if (value->is_objArray()) {
888 objArrayOop oa = (objArrayOop)value;
889 const char* klass_name = value->klass()->name()->as_quoted_ascii();
890 _out->print(" %s", klass_name);
891 }
892 } else {
893 ShouldNotReachHere();
894 }
895 break;
896 } else {
897 // handling of null free inline type
898 ResetNoHandleMark rnhm;
899 Thread* THREAD = Thread::current();
900 SignatureStream ss(fd->signature(), false);
901 Symbol* name = ss.as_symbol();
902 assert(!HAS_PENDING_EXCEPTION, "can resolve klass?");
903 InstanceKlass* holder = fd->field_holder();
904 InstanceKlass* k = SystemDictionary::find_instance_klass(THREAD, name,
905 Handle(THREAD, holder->class_loader()));
906 guarantee(k != nullptr && !HAS_PENDING_EXCEPTION, "can resolve klass?");
907 InlineKlass* vk = InlineKlass::cast(k);
908 oop obj;
909 if (is_flat) {
910 int field_offset = fd->offset() - vk->payload_offset();
911 obj = cast_to_oop(cast_from_oop<address>(mirror) + field_offset);
912 } else {
913 obj = mirror->obj_field_acquire(fd->offset());
914 }
915 InlineTypeFieldPrinter print_field(_out, obj);
916 vk->do_nonstatic_fields(&print_field);
917 break;
918 }
919 default:
920 ShouldNotReachHere();
921 }
922 }
923
924 const char *ciInstanceKlass::replay_name() const {
925 return CURRENT_ENV->replay_name(get_instanceKlass());
926 }
927
928 void ciInstanceKlass::dump_replay_instanceKlass(outputStream* out, InstanceKlass* ik) {
929 if (ik->is_hidden()) {
930 const char *name = CURRENT_ENV->dyno_name(ik);
931 if (name != nullptr) {
932 out->print_cr("instanceKlass %s # %s", name, ik->name()->as_quoted_ascii());
933 } else {
934 out->print_cr("# instanceKlass %s", ik->name()->as_quoted_ascii());
935 }
936 } else {
937 out->print_cr("instanceKlass %s", ik->name()->as_quoted_ascii());
938 }
939 }
940
941 GrowableArray<ciInstanceKlass*>* ciInstanceKlass::transitive_interfaces() const{
942 if (_transitive_interfaces == nullptr) {
|