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 // This is essentially a shortcut for:
438 // get_field_by_offset(field_offset, is_static)->layout_type()
439 // except this does not require allocating memory for a new ciField
440 BasicType ciInstanceKlass::get_field_type_by_offset(const int field_offset, const bool is_static) {
441 if (!is_static) {
442 ciField* field = get_nonstatic_field_by_offset(field_offset);
443 return field != nullptr ? field->layout_type() : T_ILLEGAL;
444 }
445
446 // Avoid allocating a new ciField by obtaining the field type directly
447 VM_ENTRY_MARK;
448 InstanceKlass* k = get_instanceKlass();
449 fieldDescriptor fd;
450 if (!k->find_field_from_offset(field_offset, is_static, &fd)) {
451 return T_ILLEGAL;
452 }
453
454 // Reproduce the behavior of ciField::layout_type
455 BasicType field_type = fd.field_type();
456 if (is_reference_type(field_type)) {
457 return T_OBJECT;
458 }
459 return type2field[make(field_type)->basic_type()];
460 }
461
462 // ------------------------------------------------------------------
463 // ciInstanceKlass::compute_nonstatic_fields
464 int ciInstanceKlass::compute_nonstatic_fields() {
465 assert(is_loaded(), "must be loaded");
466
467 if (_nonstatic_fields != nullptr)
468 return _nonstatic_fields->length();
469
470 if (!has_nonstatic_fields()) {
471 Arena* arena = CURRENT_ENV->arena();
472 _nonstatic_fields = new (arena) GrowableArray<ciField*>(arena, 0, 0, nullptr);
473 return 0;
474 }
475 assert(!is_java_lang_Object(), "bootstrap OK");
476
477 ciInstanceKlass* super = this->super();
478 GrowableArray<ciField*>* super_fields = nullptr;
479 if (super != nullptr && super->has_nonstatic_fields()) {
480 int super_flen = super->nof_nonstatic_fields();
481 super_fields = super->_nonstatic_fields;
482 assert(super_flen == 0 || super_fields != nullptr, "first get nof_fields");
483 }
484
485 GrowableArray<ciField*>* fields = nullptr;
486 GUARDED_VM_ENTRY({
487 fields = compute_nonstatic_fields_impl(super_fields);
488 });
489
490 if (fields == nullptr) {
491 // This can happen if this class (java.lang.Class) has invisible fields.
492 if (super_fields != nullptr) {
493 _nonstatic_fields = super_fields;
494 return super_fields->length();
495 } else {
496 return 0;
497 }
498 }
499
500 int flen = fields->length();
501
502 _nonstatic_fields = fields;
503 return flen;
504 }
505
506 GrowableArray<ciField*>*
507 ciInstanceKlass::compute_nonstatic_fields_impl(GrowableArray<ciField*>*
508 super_fields) {
509 ASSERT_IN_VM;
510 Arena* arena = CURRENT_ENV->arena();
511 int flen = 0;
512 GrowableArray<ciField*>* fields = nullptr;
513 InstanceKlass* k = get_instanceKlass();
514 for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
515 if (fs.access_flags().is_static()) continue;
516 flen += 1;
517 }
518
519 // allocate the array:
520 if (flen == 0) {
521 return nullptr; // return nothing if none are locally declared
522 }
523 if (super_fields != nullptr) {
524 flen += super_fields->length();
525 }
526 fields = new (arena) GrowableArray<ciField*>(arena, flen, 0, nullptr);
527 if (super_fields != nullptr) {
528 fields->appendAll(super_fields);
529 }
530
531 for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
532 if (fs.access_flags().is_static()) continue;
533 fieldDescriptor& fd = fs.field_descriptor();
534 ciField* field = new (arena) ciField(&fd);
535 fields->append(field);
536 }
537 assert(fields->length() == flen, "sanity");
538 return fields;
539 }
540
541 bool ciInstanceKlass::compute_injected_fields_helper() {
542 ASSERT_IN_VM;
543 InstanceKlass* k = get_instanceKlass();
544
545 for (InternalFieldStream fs(k); !fs.done(); fs.next()) {
546 if (fs.access_flags().is_static()) continue;
547 return true;
548 }
549 return false;
550 }
551
552 void ciInstanceKlass::compute_injected_fields() {
553 assert(is_loaded(), "must be loaded");
554
555 int has_injected_fields = 0;
556 if (super() != nullptr && super()->has_injected_fields()) {
557 has_injected_fields = 1;
558 } else {
630 } else {
631 // Go into the VM to fetch the implementor.
632 VM_ENTRY_MARK;
633 InstanceKlass* ik = get_instanceKlass();
634 Klass* implk = ik->implementor();
635 if (implk != nullptr) {
636 if (implk == ik) {
637 // More than one implementors. Use 'this' in this case.
638 impl = this;
639 } else {
640 impl = CURRENT_THREAD_ENV->get_instance_klass(implk);
641 }
642 }
643 }
644 // Memoize this result.
645 _implementor = impl;
646 }
647 return impl;
648 }
649
650 // Utility class for printing of the contents of the static fields for
651 // use by compilation replay. It only prints out the information that
652 // could be consumed by the compiler, so for primitive types it prints
653 // out the actual value. For Strings it's the actual string value.
654 // For array types it it's first level array size since that's the
655 // only value which statically unchangeable. For all other reference
656 // types it simply prints out the dynamic type.
657
658 class StaticFinalFieldPrinter : public FieldClosure {
659 outputStream* _out;
660 const char* _holder;
661 public:
662 StaticFinalFieldPrinter(outputStream* out, const char* holder) :
663 _out(out),
664 _holder(holder) {
665 }
666 void do_field(fieldDescriptor* fd) {
667 if (fd->is_final() && !fd->has_initial_value()) {
668 ResourceMark rm;
669 oop mirror = fd->field_holder()->java_mirror();
670 _out->print("staticfield %s %s %s ", _holder, fd->name()->as_quoted_ascii(), fd->signature()->as_quoted_ascii());
671 BasicType field_type = fd->field_type();
672 switch (field_type) {
673 case T_BYTE: _out->print_cr("%d", mirror->byte_field(fd->offset())); break;
674 case T_BOOLEAN: _out->print_cr("%d", mirror->bool_field(fd->offset())); break;
675 case T_SHORT: _out->print_cr("%d", mirror->short_field(fd->offset())); break;
676 case T_CHAR: _out->print_cr("%d", mirror->char_field(fd->offset())); break;
677 case T_INT: _out->print_cr("%d", mirror->int_field(fd->offset())); break;
678 case T_LONG: _out->print_cr(INT64_FORMAT, (int64_t)(mirror->long_field(fd->offset()))); break;
679 case T_FLOAT: {
680 float f = mirror->float_field(fd->offset());
681 _out->print_cr("%d", *(int*)&f);
682 break;
683 }
684 case T_DOUBLE: {
685 double d = mirror->double_field(fd->offset());
686 _out->print_cr(INT64_FORMAT, *(int64_t*)&d);
687 break;
688 }
689 case T_ARRAY: // fall-through
690 case T_OBJECT: {
691 oop value = mirror->obj_field_acquire(fd->offset());
692 if (value == nullptr) {
693 if (field_type == T_ARRAY) {
694 _out->print("%d", -1);
695 }
696 _out->cr();
697 } else if (value->is_instance()) {
698 assert(field_type == T_OBJECT, "");
699 if (value->is_a(vmClasses::String_klass())) {
700 const char* ascii_value = java_lang_String::as_quoted_ascii(value);
701 _out->print_cr("\"%s\"", (ascii_value != nullptr) ? ascii_value : "");
702 } else {
703 const char* klass_name = value->klass()->name()->as_quoted_ascii();
704 _out->print_cr("%s", klass_name);
705 }
706 } else if (value->is_array()) {
707 typeArrayOop ta = (typeArrayOop)value;
708 _out->print("%d", ta->length());
709 if (value->is_objArray()) {
710 objArrayOop oa = (objArrayOop)value;
711 const char* klass_name = value->klass()->name()->as_quoted_ascii();
712 _out->print(" %s", klass_name);
713 }
714 _out->cr();
715 } else {
716 ShouldNotReachHere();
717 }
718 break;
719 }
720 default:
721 ShouldNotReachHere();
722 }
723 }
724 }
725 };
726
727 const char *ciInstanceKlass::replay_name() const {
728 return CURRENT_ENV->replay_name(get_instanceKlass());
729 }
730
731 void ciInstanceKlass::dump_replay_instanceKlass(outputStream* out, InstanceKlass* ik) {
732 if (ik->is_hidden()) {
733 const char *name = CURRENT_ENV->dyno_name(ik);
734 if (name != nullptr) {
735 out->print_cr("instanceKlass %s # %s", name, ik->name()->as_quoted_ascii());
736 } else {
737 out->print_cr("# instanceKlass %s", ik->name()->as_quoted_ascii());
738 }
739 } else {
740 out->print_cr("instanceKlass %s", ik->name()->as_quoted_ascii());
741 }
742 }
743
744 GrowableArray<ciInstanceKlass*>* ciInstanceKlass::transitive_interfaces() const{
745 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 // This is essentially a shortcut for:
479 // get_field_by_offset(field_offset, is_static)->layout_type()
480 // except this does not require allocating memory for a new ciField
481 BasicType ciInstanceKlass::get_field_type_by_offset(const int field_offset, const bool is_static) {
482 if (!is_static) {
483 ciField* field = get_nonstatic_field_by_offset(field_offset);
484 return field != nullptr ? field->layout_type() : T_ILLEGAL;
485 }
486
487 // Avoid allocating a new ciField by obtaining the field type directly
488 VM_ENTRY_MARK;
489 InstanceKlass* k = get_instanceKlass();
490 fieldDescriptor fd;
491 if (!k->find_field_from_offset(field_offset, is_static, &fd)) {
492 return T_ILLEGAL;
493 }
494
495 // Reproduce the behavior of ciField::layout_type
496 BasicType field_type = fd.field_type();
497 if (is_reference_type(field_type)) {
498 return T_OBJECT;
499 }
500 return type2field[make(field_type)->basic_type()];
501 }
502
503 void ciInstanceKlass::compute_nonstatic_fields() {
504 assert(is_loaded(), "must be loaded");
505
506 if (_nonstatic_fields != nullptr) {
507 assert(_declared_nonstatic_fields != nullptr, "must be initialized at the same time, class %s", name()->as_utf8());
508 return;
509 }
510
511 if (!has_nonstatic_fields()) {
512 _declared_nonstatic_fields = &empty_field_array;
513 _nonstatic_fields = &empty_field_array;
514 return;
515 }
516 assert(!is_java_lang_Object(), "bootstrap OK");
517
518 ciInstanceKlass* super = this->super();
519 assert(super != nullptr, "must have a super class, current class: %s", name()->as_utf8());
520 super->compute_nonstatic_fields();
521 const GrowableArray<ciField*>* super_declared_fields = super->_declared_nonstatic_fields;;
522 const GrowableArray<ciField*>* super_fields = super->_nonstatic_fields;
523 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());
524
525 GUARDED_VM_ENTRY({
526 compute_nonstatic_fields_impl(super_declared_fields, super_fields);
527 });
528 }
529
530 void ciInstanceKlass::compute_nonstatic_fields_impl(const GrowableArray<ciField*>* super_declared_fields, const GrowableArray<ciField*>* super_fields) {
531 assert(_declared_nonstatic_fields == nullptr && _nonstatic_fields == nullptr, "initialized already");
532 ASSERT_IN_VM;
533 Arena* arena = CURRENT_ENV->arena();
534
535 InstanceKlass* this_klass = get_instanceKlass();
536 int declared_field_num = 0;
537 int field_num = 0;
538 for (JavaFieldStream fs(this_klass); !fs.done(); fs.next()) {
539 if (fs.access_flags().is_static()) {
540 continue;
541 }
542
543 declared_field_num++;
544
545 fieldDescriptor& fd = fs.field_descriptor();
546 if (fd.is_flat()) {
547 InlineKlass* k = this_klass->get_inline_type_field_klass(fd.index());
548 ciInlineKlass* vk = CURRENT_ENV->get_klass(k)->as_inline_klass();
549 field_num += vk->nof_nonstatic_fields();
550 field_num += fd.has_null_marker() ? 1 : 0;
551 } else {
552 field_num++;
553 }
554 }
555
556 GrowableArray<ciField*>* tmp_declared_fields = nullptr;
557 if (declared_field_num != 0) {
558 tmp_declared_fields = new (arena) GrowableArray<ciField*>(arena, declared_field_num + super_declared_fields->length(), 0, nullptr);
559 tmp_declared_fields->appendAll(super_declared_fields);
560 }
561
562 GrowableArray<ciField*>* tmp_fields = nullptr;
563 if (field_num != 0) {
564 tmp_fields = new (arena) GrowableArray<ciField*>(arena, field_num + super_fields->length(), 0, nullptr);
565 tmp_fields->appendAll(super_fields);
566 }
567
568 // For later assertion
569 declared_field_num += super_declared_fields->length();
570 field_num += super_fields->length();
571
572 for (JavaFieldStream fs(this_klass); !fs.done(); fs.next()) {
573 if (fs.access_flags().is_static()) {
574 continue;
575 }
576
577 fieldDescriptor& fd = fs.field_descriptor();
578 ciField* declared_field = new (arena) ciField(&fd);
579 assert(tmp_declared_fields != nullptr, "should be initialized");
580 tmp_declared_fields->append(declared_field);
581
582 if (fd.is_flat()) {
583 // Flat fields are embedded
584 Klass* k = get_instanceKlass()->get_inline_type_field_klass(fd.index());
585 ciInlineKlass* vk = CURRENT_ENV->get_klass(k)->as_inline_klass();
586 // Iterate over fields of the flat inline type and copy them to 'this'
587 for (int i = 0; i < vk->nof_nonstatic_fields(); ++i) {
588 assert(tmp_fields != nullptr, "should be initialized");
589 tmp_fields->append(new (arena) ciField(declared_field, vk->nonstatic_field_at(i)));
590 }
591 if (fd.has_null_marker()) {
592 assert(tmp_fields != nullptr, "should be initialized");
593 tmp_fields->append(new (arena) ciField(declared_field));
594 }
595 } else {
596 assert(tmp_fields != nullptr, "should be initialized");
597 tmp_fields->append(declared_field);
598 }
599 }
600
601 // Now sort them by offset, ascending. In principle, they could mix with superclass fields.
602 if (tmp_declared_fields != nullptr) {
603 assert(tmp_declared_fields->length() == declared_field_num, "sanity check failed for class: %s, number of declared fields: %d, expected: %d",
604 name()->as_utf8(), tmp_declared_fields->length(), declared_field_num);
605 _declared_nonstatic_fields = tmp_declared_fields;
606 } else {
607 _declared_nonstatic_fields = super_declared_fields;
608 }
609
610 if (tmp_fields != nullptr) {
611 assert(tmp_fields->length() == field_num, "sanity check failed for class: %s, number of fields: %d, expected: %d",
612 name()->as_utf8(), tmp_fields->length(), field_num);
613 _nonstatic_fields = tmp_fields;
614 } else {
615 _nonstatic_fields = super_fields;
616 }
617 }
618
619 bool ciInstanceKlass::compute_injected_fields_helper() {
620 ASSERT_IN_VM;
621 InstanceKlass* k = get_instanceKlass();
622
623 for (InternalFieldStream fs(k); !fs.done(); fs.next()) {
624 if (fs.access_flags().is_static()) continue;
625 return true;
626 }
627 return false;
628 }
629
630 void ciInstanceKlass::compute_injected_fields() {
631 assert(is_loaded(), "must be loaded");
632
633 int has_injected_fields = 0;
634 if (super() != nullptr && super()->has_injected_fields()) {
635 has_injected_fields = 1;
636 } else {
708 } else {
709 // Go into the VM to fetch the implementor.
710 VM_ENTRY_MARK;
711 InstanceKlass* ik = get_instanceKlass();
712 Klass* implk = ik->implementor();
713 if (implk != nullptr) {
714 if (implk == ik) {
715 // More than one implementors. Use 'this' in this case.
716 impl = this;
717 } else {
718 impl = CURRENT_THREAD_ENV->get_instance_klass(implk);
719 }
720 }
721 }
722 // Memoize this result.
723 _implementor = impl;
724 }
725 return impl;
726 }
727
728 bool ciInstanceKlass::can_be_inline_klass(bool is_exact) {
729 if (!Arguments::is_valhalla_enabled()) {
730 return false;
731 }
732 if (!is_loaded() || is_inlinetype()) {
733 // Not loaded or known to be an inline klass
734 return true;
735 }
736 if (!is_exact) {
737 // Not exact, check if this is a valid super for an inline klass
738 GUARDED_VM_ENTRY(
739 return !get_instanceKlass()->access_flags().is_identity_class() || is_java_lang_Object();
740 )
741 }
742 return false;
743 }
744
745 // Utility class for printing of the contents of the static fields for
746 // use by compilation replay. It only prints out the information that
747 // could be consumed by the compiler, so for primitive types it prints
748 // out the actual value. For Strings it's the actual string value.
749 // For array types it it's first level array size since that's the
750 // only value which statically unchangeable. For all other reference
751 // types it simply prints out the dynamic type.
752
753 class StaticFieldPrinter : public FieldClosure {
754 protected:
755 outputStream* _out;
756 public:
757 StaticFieldPrinter(outputStream* out) :
758 _out(out) {
759 }
760 void do_field_helper(fieldDescriptor* fd, oop obj, bool is_flat);
761 };
762
763 class StaticFinalFieldPrinter : public StaticFieldPrinter {
764 const char* _holder;
765 public:
766 StaticFinalFieldPrinter(outputStream* out, const char* holder) :
767 StaticFieldPrinter(out), _holder(holder) {
768 }
769 void do_field(fieldDescriptor* fd) {
770 if (fd->is_final() && !fd->has_initial_value()) {
771 ResourceMark rm;
772 InstanceKlass* holder = fd->field_holder();
773 oop mirror = holder->java_mirror();
774 _out->print("staticfield %s %s ", _holder, fd->name()->as_quoted_ascii());
775 BasicType bt = fd->field_type();
776 if (bt != T_OBJECT && bt != T_ARRAY) {
777 _out->print("%s ", fd->signature()->as_quoted_ascii());
778 }
779 do_field_helper(fd, mirror, false);
780 _out->cr();
781 }
782 }
783 };
784
785 class InlineTypeFieldPrinter : public StaticFieldPrinter {
786 oop _obj;
787 public:
788 InlineTypeFieldPrinter(outputStream* out, oop obj) :
789 StaticFieldPrinter(out), _obj(obj) {
790 }
791 void do_field(fieldDescriptor* fd) {
792 do_field_helper(fd, _obj, true);
793 _out->print(" ");
794 }
795 };
796
797 void StaticFieldPrinter::do_field_helper(fieldDescriptor* fd, oop mirror, bool is_flat) {
798 BasicType field_type = fd->field_type();
799 switch (field_type) {
800 case T_BYTE: _out->print("%d", mirror->byte_field(fd->offset())); break;
801 case T_BOOLEAN: _out->print("%d", mirror->bool_field(fd->offset())); break;
802 case T_SHORT: _out->print("%d", mirror->short_field(fd->offset())); break;
803 case T_CHAR: _out->print("%d", mirror->char_field(fd->offset())); break;
804 case T_INT: _out->print("%d", mirror->int_field(fd->offset())); break;
805 case T_LONG: _out->print(INT64_FORMAT, (int64_t)(mirror->long_field(fd->offset()))); break;
806 case T_FLOAT: {
807 float f = mirror->float_field(fd->offset());
808 _out->print("%d", *(int*)&f);
809 break;
810 }
811 case T_DOUBLE: {
812 double d = mirror->double_field(fd->offset());
813 _out->print(INT64_FORMAT, *(int64_t*)&d);
814 break;
815 }
816 case T_ARRAY: // fall-through
817 case T_OBJECT:
818 if (!fd->is_null_free_inline_type()) {
819 _out->print("%s ", fd->signature()->as_quoted_ascii());
820 oop value = mirror->obj_field_acquire(fd->offset());
821 if (value == nullptr) {
822 if (field_type == T_ARRAY) {
823 _out->print("%d", -1);
824 }
825 _out->cr();
826 } else if (value->is_instance()) {
827 assert(field_type == T_OBJECT, "");
828 if (value->is_a(vmClasses::String_klass())) {
829 const char* ascii_value = java_lang_String::as_quoted_ascii(value);
830 _out->print("\"%s\"", (ascii_value != nullptr) ? ascii_value : "");
831 } else {
832 const char* klass_name = value->klass()->name()->as_quoted_ascii();
833 _out->print("%s", klass_name);
834 }
835 } else if (value->is_array()) {
836 arrayOop a = (arrayOop)value;
837 _out->print("%d", a->length());
838 if (value->is_objArray()) {
839 objArrayOop oa = (objArrayOop)value;
840 const char* klass_name = value->klass()->name()->as_quoted_ascii();
841 _out->print(" %s", klass_name);
842 }
843 } else {
844 ShouldNotReachHere();
845 }
846 break;
847 } else {
848 // handling of null free inline type
849 ResetNoHandleMark rnhm;
850 Thread* THREAD = Thread::current();
851 SignatureStream ss(fd->signature(), false);
852 Symbol* name = ss.as_symbol();
853 assert(!HAS_PENDING_EXCEPTION, "can resolve klass?");
854 InstanceKlass* holder = fd->field_holder();
855 InstanceKlass* k = SystemDictionary::find_instance_klass(THREAD, name,
856 Handle(THREAD, holder->class_loader()));
857 guarantee(k != nullptr && !HAS_PENDING_EXCEPTION, "can resolve klass?");
858 InlineKlass* vk = InlineKlass::cast(k);
859 oop obj;
860 if (is_flat) {
861 int field_offset = fd->offset() - vk->payload_offset();
862 obj = cast_to_oop(cast_from_oop<address>(mirror) + field_offset);
863 } else {
864 obj = mirror->obj_field_acquire(fd->offset());
865 }
866 InlineTypeFieldPrinter print_field(_out, obj);
867 vk->do_nonstatic_fields(&print_field);
868 break;
869 }
870 default:
871 ShouldNotReachHere();
872 }
873 }
874
875 const char *ciInstanceKlass::replay_name() const {
876 return CURRENT_ENV->replay_name(get_instanceKlass());
877 }
878
879 void ciInstanceKlass::dump_replay_instanceKlass(outputStream* out, InstanceKlass* ik) {
880 if (ik->is_hidden()) {
881 const char *name = CURRENT_ENV->dyno_name(ik);
882 if (name != nullptr) {
883 out->print_cr("instanceKlass %s # %s", name, ik->name()->as_quoted_ascii());
884 } else {
885 out->print_cr("# instanceKlass %s", ik->name()->as_quoted_ascii());
886 }
887 } else {
888 out->print_cr("instanceKlass %s", ik->name()->as_quoted_ascii());
889 }
890 }
891
892 GrowableArray<ciInstanceKlass*>* ciInstanceKlass::transitive_interfaces() const{
893 if (_transitive_interfaces == nullptr) {
|