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 _nonstatic_fields = nullptr; // initialized lazily by compute_nonstatic_fields:
69 _has_injected_fields = -1;
70 _implementor = nullptr; // we will fill these lazily
71 _transitive_interfaces = nullptr;
72
73 // Ensure that the metadata wrapped by the ciMetadata is kept alive by GC.
74 // This is primarily useful for metadata which is considered as weak roots
75 // by the GC but need to be strong roots if reachable from a current compilation.
76 // InstanceKlass are created for both weak and strong metadata. Ensuring this metadata
77 // alive covers the cases where there are weak roots without performance cost.
78 oop holder = ik->klass_holder();
79 if (ik->class_loader_data()->has_class_mirror_holder()) {
80 // Though ciInstanceKlass records class loader oop, it's not enough to keep
81 // non-strong hidden classes alive (loader == nullptr). Klass holder should
82 // be used instead. It is enough to record a ciObject, since cached elements are never removed
83 // during ciObjectFactory lifetime. ciObjectFactory itself is created for
84 // every compilation and lives for the whole duration of the compilation.
85 assert(holder != nullptr, "holder of hidden class is the mirror which is never null");
86 (void)CURRENT_ENV->get_object(holder);
87 }
88
89 JavaThread *thread = JavaThread::current();
90 if (ciObjectFactory::is_initialized()) {
97 }
98
99 _has_trusted_loader = compute_has_trusted_loader();
100
101 // Lazy fields get filled in only upon request.
102 _super = nullptr;
103 _java_mirror = nullptr;
104
105 if (is_shared()) {
106 if (k != vmClasses::Object_klass()) {
107 super();
108 }
109 //compute_nonstatic_fields(); // done outside of constructor
110 }
111
112 _field_cache = nullptr;
113 }
114
115 // Version for unloaded classes:
116 ciInstanceKlass::ciInstanceKlass(ciSymbol* name,
117 jobject loader)
118 : ciKlass(name, T_OBJECT)
119 {
120 assert(name->char_at(0) != JVM_SIGNATURE_ARRAY, "not an instance klass");
121 _init_state = (InstanceKlass::ClassState)0;
122 _has_nonstatic_fields = false;
123 _nonstatic_fields = nullptr;
124 _has_injected_fields = -1;
125 _is_hidden = false;
126 _is_record = false;
127 _loader = loader;
128 _is_shared = false;
129 _super = nullptr;
130 _java_mirror = nullptr;
131 _field_cache = nullptr;
132 _has_trusted_loader = compute_has_trusted_loader();
133 }
134
135
136
137 // ------------------------------------------------------------------
138 // ciInstanceKlass::compute_shared_is_initialized
139 void ciInstanceKlass::compute_shared_init_state() {
140 GUARDED_VM_ENTRY(
141 InstanceKlass* ik = get_instanceKlass();
142 _init_state = ik->init_state();
143 )
301 if (name()->index_of_at(len+1, "/", 1) >= 0)
302 return false;
303
304 return true;
305 }
306
307 // ------------------------------------------------------------------
308 // ciInstanceKlass::print_impl
309 //
310 // Implementation of the print method.
311 void ciInstanceKlass::print_impl(outputStream* st) {
312 ciKlass::print_impl(st);
313 GUARDED_VM_ENTRY(st->print(" loader=" INTPTR_FORMAT, p2i(loader()));)
314 if (is_loaded()) {
315 st->print(" initialized=%s finalized=%s subklass=%s size=%d flags=",
316 bool_to_str(is_initialized()),
317 bool_to_str(has_finalizer()),
318 bool_to_str(has_subklass()),
319 layout_helper());
320
321 _flags.print_klass_flags();
322
323 if (_super) {
324 st->print(" super=");
325 _super->print_name();
326 }
327 if (_java_mirror) {
328 st->print(" mirror=PRESENT");
329 }
330 }
331 }
332
333 // ------------------------------------------------------------------
334 // ciInstanceKlass::super
335 //
336 // Get the superklass of this klass.
337 ciInstanceKlass* ciInstanceKlass::super() {
338 assert(is_loaded(), "must be loaded");
339 if (_super == nullptr && !is_java_lang_Object()) {
340 GUARDED_VM_ENTRY(
341 Klass* super_klass = get_instanceKlass()->super();
342 _super = CURRENT_ENV->get_instance_klass(super_klass);
343 )
344 }
345 return _super;
379 // ------------------------------------------------------------------
380 // ciInstanceKlass::has_finalizable_subclass
381 bool ciInstanceKlass::has_finalizable_subclass() {
382 if (!is_loaded()) return true;
383 VM_ENTRY_MARK;
384 return Dependencies::find_finalizable_subclass(get_instanceKlass()) != nullptr;
385 }
386
387 // ------------------------------------------------------------------
388 // ciInstanceKlass::contains_field_offset
389 bool ciInstanceKlass::contains_field_offset(int offset) {
390 VM_ENTRY_MARK;
391 return get_instanceKlass()->contains_field_offset(offset);
392 }
393
394 // ------------------------------------------------------------------
395 // ciInstanceKlass::get_field_by_offset
396 ciField* ciInstanceKlass::get_field_by_offset(int field_offset, bool is_static) {
397 if (!is_static) {
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 VM_ENTRY_MARK;
407 InstanceKlass* k = get_instanceKlass();
408 fieldDescriptor fd;
409 if (!k->find_field_from_offset(field_offset, is_static, &fd)) {
410 return nullptr;
411 }
412 ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd);
413 return field;
414 }
415
416 // ------------------------------------------------------------------
417 // ciInstanceKlass::get_field_by_name
418 ciField* ciInstanceKlass::get_field_by_name(ciSymbol* name, ciSymbol* signature, bool is_static) {
419 VM_ENTRY_MARK;
420 InstanceKlass* k = get_instanceKlass();
421 fieldDescriptor fd;
422 Klass* def = k->find_field(name->get_symbol(), signature->get_symbol(), is_static, &fd);
423 if (def == nullptr) {
424 return nullptr;
425 }
426 ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd);
427 return field;
428 }
429
430
431 // ------------------------------------------------------------------
432 // ciInstanceKlass::compute_nonstatic_fields
433 int ciInstanceKlass::compute_nonstatic_fields() {
434 assert(is_loaded(), "must be loaded");
435
436 if (_nonstatic_fields != nullptr)
437 return _nonstatic_fields->length();
438
439 if (!has_nonstatic_fields()) {
440 Arena* arena = CURRENT_ENV->arena();
441 _nonstatic_fields = new (arena) GrowableArray<ciField*>(arena, 0, 0, nullptr);
442 return 0;
443 }
444 assert(!is_java_lang_Object(), "bootstrap OK");
445
446 ciInstanceKlass* super = this->super();
447 GrowableArray<ciField*>* super_fields = nullptr;
448 if (super != nullptr && super->has_nonstatic_fields()) {
449 int super_flen = super->nof_nonstatic_fields();
450 super_fields = super->_nonstatic_fields;
451 assert(super_flen == 0 || super_fields != nullptr, "first get nof_fields");
452 }
453
454 GrowableArray<ciField*>* fields = nullptr;
455 GUARDED_VM_ENTRY({
456 fields = compute_nonstatic_fields_impl(super_fields);
457 });
458
459 if (fields == nullptr) {
460 // This can happen if this class (java.lang.Class) has invisible fields.
461 if (super_fields != nullptr) {
462 _nonstatic_fields = super_fields;
463 return super_fields->length();
464 } else {
465 return 0;
466 }
467 }
468
469 int flen = fields->length();
470
471 _nonstatic_fields = fields;
472 return flen;
473 }
474
475 GrowableArray<ciField*>*
476 ciInstanceKlass::compute_nonstatic_fields_impl(GrowableArray<ciField*>*
477 super_fields) {
478 ASSERT_IN_VM;
479 Arena* arena = CURRENT_ENV->arena();
480 int flen = 0;
481 GrowableArray<ciField*>* fields = nullptr;
482 InstanceKlass* k = get_instanceKlass();
483 for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
484 if (fs.access_flags().is_static()) continue;
485 flen += 1;
486 }
487
488 // allocate the array:
489 if (flen == 0) {
490 return nullptr; // return nothing if none are locally declared
491 }
492 if (super_fields != nullptr) {
493 flen += super_fields->length();
494 }
495 fields = new (arena) GrowableArray<ciField*>(arena, flen, 0, nullptr);
496 if (super_fields != nullptr) {
497 fields->appendAll(super_fields);
498 }
499
500 for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
501 if (fs.access_flags().is_static()) continue;
502 fieldDescriptor& fd = fs.field_descriptor();
503 ciField* field = new (arena) ciField(&fd);
504 fields->append(field);
505 }
506 assert(fields->length() == flen, "sanity");
507 return fields;
508 }
509
510 bool ciInstanceKlass::compute_injected_fields_helper() {
511 ASSERT_IN_VM;
512 InstanceKlass* k = get_instanceKlass();
513
514 for (InternalFieldStream fs(k); !fs.done(); fs.next()) {
515 if (fs.access_flags().is_static()) continue;
516 return true;
517 }
518 return false;
519 }
520
521 void ciInstanceKlass::compute_injected_fields() {
522 assert(is_loaded(), "must be loaded");
523
524 int has_injected_fields = 0;
525 if (super() != nullptr && super()->has_injected_fields()) {
526 has_injected_fields = 1;
527 } else {
593 } else {
594 // Go into the VM to fetch the implementor.
595 VM_ENTRY_MARK;
596 InstanceKlass* ik = get_instanceKlass();
597 Klass* implk = ik->implementor();
598 if (implk != nullptr) {
599 if (implk == ik) {
600 // More than one implementors. Use 'this' in this case.
601 impl = this;
602 } else {
603 impl = CURRENT_THREAD_ENV->get_instance_klass(implk);
604 }
605 }
606 }
607 // Memoize this result.
608 _implementor = impl;
609 }
610 return impl;
611 }
612
613 // Utility class for printing of the contents of the static fields for
614 // use by compilation replay. It only prints out the information that
615 // could be consumed by the compiler, so for primitive types it prints
616 // out the actual value. For Strings it's the actual string value.
617 // For array types it it's first level array size since that's the
618 // only value which statically unchangeable. For all other reference
619 // types it simply prints out the dynamic type.
620
621 class StaticFinalFieldPrinter : public FieldClosure {
622 outputStream* _out;
623 const char* _holder;
624 public:
625 StaticFinalFieldPrinter(outputStream* out, const char* holder) :
626 _out(out),
627 _holder(holder) {
628 }
629 void do_field(fieldDescriptor* fd) {
630 if (fd->is_final() && !fd->has_initial_value()) {
631 ResourceMark rm;
632 oop mirror = fd->field_holder()->java_mirror();
633 _out->print("staticfield %s %s %s ", _holder, fd->name()->as_quoted_ascii(), fd->signature()->as_quoted_ascii());
634 BasicType field_type = fd->field_type();
635 switch (field_type) {
636 case T_BYTE: _out->print_cr("%d", mirror->byte_field(fd->offset())); break;
637 case T_BOOLEAN: _out->print_cr("%d", mirror->bool_field(fd->offset())); break;
638 case T_SHORT: _out->print_cr("%d", mirror->short_field(fd->offset())); break;
639 case T_CHAR: _out->print_cr("%d", mirror->char_field(fd->offset())); break;
640 case T_INT: _out->print_cr("%d", mirror->int_field(fd->offset())); break;
641 case T_LONG: _out->print_cr(INT64_FORMAT, (int64_t)(mirror->long_field(fd->offset()))); break;
642 case T_FLOAT: {
643 float f = mirror->float_field(fd->offset());
644 _out->print_cr("%d", *(int*)&f);
645 break;
646 }
647 case T_DOUBLE: {
648 double d = mirror->double_field(fd->offset());
649 _out->print_cr(INT64_FORMAT, *(int64_t*)&d);
650 break;
651 }
652 case T_ARRAY: // fall-through
653 case T_OBJECT: {
654 oop value = mirror->obj_field_acquire(fd->offset());
655 if (value == nullptr) {
656 if (field_type == T_ARRAY) {
657 _out->print("%d", -1);
658 }
659 _out->cr();
660 } else if (value->is_instance()) {
661 assert(field_type == T_OBJECT, "");
662 if (value->is_a(vmClasses::String_klass())) {
663 const char* ascii_value = java_lang_String::as_quoted_ascii(value);
664 _out->print_cr("\"%s\"", (ascii_value != nullptr) ? ascii_value : "");
665 } else {
666 const char* klass_name = value->klass()->name()->as_quoted_ascii();
667 _out->print_cr("%s", klass_name);
668 }
669 } else if (value->is_array()) {
670 typeArrayOop ta = (typeArrayOop)value;
671 _out->print("%d", ta->length());
672 if (value->is_objArray()) {
673 objArrayOop oa = (objArrayOop)value;
674 const char* klass_name = value->klass()->name()->as_quoted_ascii();
675 _out->print(" %s", klass_name);
676 }
677 _out->cr();
678 } else {
679 ShouldNotReachHere();
680 }
681 break;
682 }
683 default:
684 ShouldNotReachHere();
685 }
686 }
687 }
688 };
689
690 const char *ciInstanceKlass::replay_name() const {
691 return CURRENT_ENV->replay_name(get_instanceKlass());
692 }
693
694 void ciInstanceKlass::dump_replay_instanceKlass(outputStream* out, InstanceKlass* ik) {
695 if (ik->is_hidden()) {
696 const char *name = CURRENT_ENV->dyno_name(ik);
697 if (name != nullptr) {
698 out->print_cr("instanceKlass %s # %s", name, ik->name()->as_quoted_ascii());
699 } else {
700 out->print_cr("# instanceKlass %s", ik->name()->as_quoted_ascii());
701 }
702 } else {
703 out->print_cr("instanceKlass %s", ik->name()->as_quoted_ascii());
704 }
705 }
706
707 GrowableArray<ciInstanceKlass*>* ciInstanceKlass::transitive_interfaces() const{
708 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/inlineKlass.inline.hpp"
38 #include "oops/instanceKlass.inline.hpp"
39 #include "oops/klass.inline.hpp"
40 #include "oops/oop.inline.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 _has_injected_fields = -1;
74 _implementor = nullptr; // we will fill these lazily
75 _transitive_interfaces = nullptr;
76
77 // Ensure that the metadata wrapped by the ciMetadata is kept alive by GC.
78 // This is primarily useful for metadata which is considered as weak roots
79 // by the GC but need to be strong roots if reachable from a current compilation.
80 // InstanceKlass are created for both weak and strong metadata. Ensuring this metadata
81 // alive covers the cases where there are weak roots without performance cost.
82 oop holder = ik->klass_holder();
83 if (ik->class_loader_data()->has_class_mirror_holder()) {
84 // Though ciInstanceKlass records class loader oop, it's not enough to keep
85 // non-strong hidden classes alive (loader == nullptr). Klass holder should
86 // be used instead. It is enough to record a ciObject, since cached elements are never removed
87 // during ciObjectFactory lifetime. ciObjectFactory itself is created for
88 // every compilation and lives for the whole duration of the compilation.
89 assert(holder != nullptr, "holder of hidden class is the mirror which is never null");
90 (void)CURRENT_ENV->get_object(holder);
91 }
92
93 JavaThread *thread = JavaThread::current();
94 if (ciObjectFactory::is_initialized()) {
101 }
102
103 _has_trusted_loader = compute_has_trusted_loader();
104
105 // Lazy fields get filled in only upon request.
106 _super = nullptr;
107 _java_mirror = nullptr;
108
109 if (is_shared()) {
110 if (k != vmClasses::Object_klass()) {
111 super();
112 }
113 //compute_nonstatic_fields(); // done outside of constructor
114 }
115
116 _field_cache = nullptr;
117 }
118
119 // Version for unloaded classes:
120 ciInstanceKlass::ciInstanceKlass(ciSymbol* name,
121 jobject loader,
122 BasicType bt)
123 : ciKlass(name, bt)
124 {
125 assert(name->char_at(0) != JVM_SIGNATURE_ARRAY, "not an instance klass");
126 _init_state = (InstanceKlass::ClassState)0;
127 _has_nonstatic_fields = false;
128 _declared_nonstatic_fields = nullptr; // initialized lazily by compute_nonstatic_fields
129 _nonstatic_fields = nullptr; // initialized lazily by compute_nonstatic_fields
130 _has_injected_fields = -1;
131 _is_hidden = false;
132 _is_record = false;
133 _loader = loader;
134 _is_shared = false;
135 _super = nullptr;
136 _java_mirror = nullptr;
137 _field_cache = nullptr;
138 _has_trusted_loader = compute_has_trusted_loader();
139 }
140
141
142
143 // ------------------------------------------------------------------
144 // ciInstanceKlass::compute_shared_is_initialized
145 void ciInstanceKlass::compute_shared_init_state() {
146 GUARDED_VM_ENTRY(
147 InstanceKlass* ik = get_instanceKlass();
148 _init_state = ik->init_state();
149 )
307 if (name()->index_of_at(len+1, "/", 1) >= 0)
308 return false;
309
310 return true;
311 }
312
313 // ------------------------------------------------------------------
314 // ciInstanceKlass::print_impl
315 //
316 // Implementation of the print method.
317 void ciInstanceKlass::print_impl(outputStream* st) {
318 ciKlass::print_impl(st);
319 GUARDED_VM_ENTRY(st->print(" loader=" INTPTR_FORMAT, p2i(loader()));)
320 if (is_loaded()) {
321 st->print(" initialized=%s finalized=%s subklass=%s size=%d flags=",
322 bool_to_str(is_initialized()),
323 bool_to_str(has_finalizer()),
324 bool_to_str(has_subklass()),
325 layout_helper());
326
327 _flags.print_klass_flags(st);
328
329 if (_super) {
330 st->print(" super=");
331 _super->print_name_on(st);
332 }
333 if (_java_mirror) {
334 st->print(" mirror=PRESENT");
335 }
336 }
337 }
338
339 // ------------------------------------------------------------------
340 // ciInstanceKlass::super
341 //
342 // Get the superklass of this klass.
343 ciInstanceKlass* ciInstanceKlass::super() {
344 assert(is_loaded(), "must be loaded");
345 if (_super == nullptr && !is_java_lang_Object()) {
346 GUARDED_VM_ENTRY(
347 Klass* super_klass = get_instanceKlass()->super();
348 _super = CURRENT_ENV->get_instance_klass(super_klass);
349 )
350 }
351 return _super;
385 // ------------------------------------------------------------------
386 // ciInstanceKlass::has_finalizable_subclass
387 bool ciInstanceKlass::has_finalizable_subclass() {
388 if (!is_loaded()) return true;
389 VM_ENTRY_MARK;
390 return Dependencies::find_finalizable_subclass(get_instanceKlass()) != nullptr;
391 }
392
393 // ------------------------------------------------------------------
394 // ciInstanceKlass::contains_field_offset
395 bool ciInstanceKlass::contains_field_offset(int offset) {
396 VM_ENTRY_MARK;
397 return get_instanceKlass()->contains_field_offset(offset);
398 }
399
400 // ------------------------------------------------------------------
401 // ciInstanceKlass::get_field_by_offset
402 ciField* ciInstanceKlass::get_field_by_offset(int field_offset, bool is_static) {
403 if (!is_static) {
404 for (int i = 0, len = nof_nonstatic_fields(); i < len; i++) {
405 ciField* field = nonstatic_field_at(i);
406 int field_off = field->offset_in_bytes();
407 if (field_off == field_offset) {
408 return field;
409 }
410 }
411 return nullptr;
412 }
413
414 VM_ENTRY_MARK;
415 InstanceKlass* k = get_instanceKlass();
416 fieldDescriptor fd;
417 if (!k->find_field_from_offset(field_offset, is_static, &fd)) {
418 return nullptr;
419 }
420 ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd);
421 return field;
422 }
423
424 ciField* ciInstanceKlass::get_non_flat_field_by_offset(int field_offset) {
425 for (int i = 0, len = nof_declared_nonstatic_fields(); i < len; i++) {
426 ciField* field = declared_nonstatic_field_at(i);
427 int field_off = field->offset_in_bytes();
428 if (field_off == field_offset) {
429 return field;
430 } else if (field_off > field_offset) {
431 break;
432 }
433 }
434 return nullptr;
435 }
436
437 int ciInstanceKlass::field_index_by_offset(int offset) {
438 assert(contains_field_offset(offset), "invalid field offset");
439 int best_offset = 0;
440 int best_index = -1;
441 // Search the field with the given offset
442 for (int i = 0; i < nof_declared_nonstatic_fields(); ++i) {
443 int field_offset = declared_nonstatic_field_at(i)->offset_in_bytes();
444 if (field_offset == offset) {
445 // Exact match
446 return i;
447 } else if (field_offset < offset && field_offset > best_offset) {
448 // No exact match. Save the index of the field with the closest offset that
449 // is smaller than the given field offset. This index corresponds to the
450 // flat field that holds the field we are looking for.
451 best_offset = field_offset;
452 best_index = i;
453 }
454 }
455 assert(best_index >= 0, "field not found");
456 assert(best_offset == offset || declared_nonstatic_field_at(best_index)->type()->is_inlinetype(), "offset should match for non-inline types");
457 return best_index;
458 }
459
460 // ------------------------------------------------------------------
461 // ciInstanceKlass::get_field_by_name
462 ciField* ciInstanceKlass::get_field_by_name(ciSymbol* name, ciSymbol* signature, bool is_static) {
463 VM_ENTRY_MARK;
464 InstanceKlass* k = get_instanceKlass();
465 fieldDescriptor fd;
466 Klass* def = k->find_field(name->get_symbol(), signature->get_symbol(), is_static, &fd);
467 if (def == nullptr) {
468 return nullptr;
469 }
470 ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd);
471 return field;
472 }
473
474 static int sort_field_by_offset(ciField** a, ciField** b) {
475 return (*a)->offset_in_bytes() - (*b)->offset_in_bytes();
476 // (no worries about 32-bit overflow...)
477 }
478
479 const GrowableArray<ciField*> empty_field_array(0, MemTag::mtCompiler);
480
481 void ciInstanceKlass::compute_nonstatic_fields() {
482 assert(is_loaded(), "must be loaded");
483
484 if (_nonstatic_fields != nullptr) {
485 assert(_declared_nonstatic_fields != nullptr, "must be initialized at the same time, class %s", name()->as_utf8());
486 return;
487 }
488
489 if (!has_nonstatic_fields()) {
490 _declared_nonstatic_fields = &empty_field_array;
491 _nonstatic_fields = &empty_field_array;
492 return;
493 }
494 assert(!is_java_lang_Object(), "bootstrap OK");
495
496 ciInstanceKlass* super = this->super();
497 assert(super != nullptr, "must have a super class, current class: %s", name()->as_utf8());
498 super->compute_nonstatic_fields();
499 const GrowableArray<ciField*>* super_declared_fields = super->_declared_nonstatic_fields;;
500 const GrowableArray<ciField*>* super_fields = super->_nonstatic_fields;
501 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());
502
503 GUARDED_VM_ENTRY({
504 compute_nonstatic_fields_impl(super_declared_fields, super_fields);
505 });
506 }
507
508 void ciInstanceKlass::compute_nonstatic_fields_impl(const GrowableArray<ciField*>* super_declared_fields, const GrowableArray<ciField*>* super_fields) {
509 assert(_declared_nonstatic_fields == nullptr && _nonstatic_fields == nullptr, "initialized already");
510 ASSERT_IN_VM;
511 Arena* arena = CURRENT_ENV->arena();
512
513 InstanceKlass* this_klass = get_instanceKlass();
514 int declared_field_num = 0;
515 int field_num = 0;
516 for (JavaFieldStream fs(this_klass); !fs.done(); fs.next()) {
517 if (fs.access_flags().is_static()) {
518 continue;
519 }
520
521 declared_field_num++;
522
523 fieldDescriptor& fd = fs.field_descriptor();
524 if (fd.is_flat()) {
525 InlineKlass* k = this_klass->get_inline_type_field_klass(fd.index());
526 ciInlineKlass* vk = CURRENT_ENV->get_klass(k)->as_inline_klass();
527 field_num += vk->nof_nonstatic_fields();
528 field_num += fd.has_null_marker() ? 1 : 0;
529 } else {
530 field_num++;
531 }
532 }
533
534 GrowableArray<ciField*>* tmp_declared_fields = nullptr;
535 if (declared_field_num != 0) {
536 tmp_declared_fields = new (arena) GrowableArray<ciField*>(arena, declared_field_num + super_declared_fields->length(), 0, nullptr);
537 tmp_declared_fields->appendAll(super_declared_fields);
538 }
539
540 GrowableArray<ciField*>* tmp_fields = nullptr;
541 if (field_num != 0) {
542 tmp_fields = new (arena) GrowableArray<ciField*>(arena, field_num + super_fields->length(), 0, nullptr);
543 tmp_fields->appendAll(super_fields);
544 }
545
546 // For later assertion
547 declared_field_num += super_declared_fields->length();
548 field_num += super_fields->length();
549
550 for (JavaFieldStream fs(this_klass); !fs.done(); fs.next()) {
551 if (fs.access_flags().is_static()) {
552 continue;
553 }
554
555 fieldDescriptor& fd = fs.field_descriptor();
556 ciField* declared_field = new (arena) ciField(&fd);
557 assert(tmp_declared_fields != nullptr, "should be initialized");
558 tmp_declared_fields->append(declared_field);
559
560 if (fd.is_flat()) {
561 // Flat fields are embedded
562 int field_offset = fd.offset();
563 Klass* k = get_instanceKlass()->get_inline_type_field_klass(fd.index());
564 ciInlineKlass* vk = CURRENT_ENV->get_klass(k)->as_inline_klass();
565 // Iterate over fields of the flat inline type and copy them to 'this'
566 for (int i = 0; i < vk->nof_nonstatic_fields(); ++i) {
567 assert(tmp_fields != nullptr, "should be initialized");
568 tmp_fields->append(new (arena) ciField(declared_field, vk->nonstatic_field_at(i)));
569 }
570 if (fd.has_null_marker()) {
571 assert(tmp_fields != nullptr, "should be initialized");
572 tmp_fields->append(new (arena) ciField(declared_field));
573 }
574 } else {
575 assert(tmp_fields != nullptr, "should be initialized");
576 tmp_fields->append(declared_field);
577 }
578 }
579
580 // Now sort them by offset, ascending. In principle, they could mix with superclass fields.
581 if (tmp_declared_fields != nullptr) {
582 assert(tmp_declared_fields->length() == declared_field_num, "sanity check failed for class: %s, number of declared fields: %d, expected: %d",
583 name()->as_utf8(), tmp_declared_fields->length(), declared_field_num);
584 tmp_declared_fields->sort(sort_field_by_offset);
585 _declared_nonstatic_fields = tmp_declared_fields;
586 } else {
587 _declared_nonstatic_fields = super_declared_fields;
588 }
589
590 if (tmp_fields != nullptr) {
591 assert(tmp_fields->length() == field_num, "sanity check failed for class: %s, number of fields: %d, expected: %d",
592 name()->as_utf8(), tmp_fields->length(), field_num);
593 tmp_fields->sort(sort_field_by_offset);
594 _nonstatic_fields = tmp_fields;
595 } else {
596 _nonstatic_fields = super_fields;
597 }
598 }
599
600 bool ciInstanceKlass::compute_injected_fields_helper() {
601 ASSERT_IN_VM;
602 InstanceKlass* k = get_instanceKlass();
603
604 for (InternalFieldStream fs(k); !fs.done(); fs.next()) {
605 if (fs.access_flags().is_static()) continue;
606 return true;
607 }
608 return false;
609 }
610
611 void ciInstanceKlass::compute_injected_fields() {
612 assert(is_loaded(), "must be loaded");
613
614 int has_injected_fields = 0;
615 if (super() != nullptr && super()->has_injected_fields()) {
616 has_injected_fields = 1;
617 } else {
683 } else {
684 // Go into the VM to fetch the implementor.
685 VM_ENTRY_MARK;
686 InstanceKlass* ik = get_instanceKlass();
687 Klass* implk = ik->implementor();
688 if (implk != nullptr) {
689 if (implk == ik) {
690 // More than one implementors. Use 'this' in this case.
691 impl = this;
692 } else {
693 impl = CURRENT_THREAD_ENV->get_instance_klass(implk);
694 }
695 }
696 }
697 // Memoize this result.
698 _implementor = impl;
699 }
700 return impl;
701 }
702
703 bool ciInstanceKlass::can_be_inline_klass(bool is_exact) {
704 if (!EnableValhalla) {
705 return false;
706 }
707 if (!is_loaded() || is_inlinetype()) {
708 // Not loaded or known to be an inline klass
709 return true;
710 }
711 if (!is_exact) {
712 // Not exact, check if this is a valid super for an inline klass
713 VM_ENTRY_MARK;
714 return !get_instanceKlass()->access_flags().is_identity_class() || is_java_lang_Object() ;
715 }
716 return false;
717 }
718
719 // Utility class for printing of the contents of the static fields for
720 // use by compilation replay. It only prints out the information that
721 // could be consumed by the compiler, so for primitive types it prints
722 // out the actual value. For Strings it's the actual string value.
723 // For array types it it's first level array size since that's the
724 // only value which statically unchangeable. For all other reference
725 // types it simply prints out the dynamic type.
726
727 class StaticFieldPrinter : public FieldClosure {
728 protected:
729 outputStream* _out;
730 public:
731 StaticFieldPrinter(outputStream* out) :
732 _out(out) {
733 }
734 void do_field_helper(fieldDescriptor* fd, oop obj, bool is_flat);
735 };
736
737 class StaticFinalFieldPrinter : public StaticFieldPrinter {
738 const char* _holder;
739 public:
740 StaticFinalFieldPrinter(outputStream* out, const char* holder) :
741 StaticFieldPrinter(out), _holder(holder) {
742 }
743 void do_field(fieldDescriptor* fd) {
744 if (fd->is_final() && !fd->has_initial_value()) {
745 ResourceMark rm;
746 InstanceKlass* holder = fd->field_holder();
747 oop mirror = holder->java_mirror();
748 _out->print("staticfield %s %s ", _holder, fd->name()->as_quoted_ascii());
749 BasicType bt = fd->field_type();
750 if (bt != T_OBJECT && bt != T_ARRAY) {
751 _out->print("%s ", fd->signature()->as_quoted_ascii());
752 }
753 do_field_helper(fd, mirror, false);
754 _out->cr();
755 }
756 }
757 };
758
759 class InlineTypeFieldPrinter : public StaticFieldPrinter {
760 oop _obj;
761 public:
762 InlineTypeFieldPrinter(outputStream* out, oop obj) :
763 StaticFieldPrinter(out), _obj(obj) {
764 }
765 void do_field(fieldDescriptor* fd) {
766 do_field_helper(fd, _obj, true);
767 _out->print(" ");
768 }
769 };
770
771 void StaticFieldPrinter::do_field_helper(fieldDescriptor* fd, oop mirror, bool is_flat) {
772 BasicType field_type = fd->field_type();
773 switch (field_type) {
774 case T_BYTE: _out->print("%d", mirror->byte_field(fd->offset())); break;
775 case T_BOOLEAN: _out->print("%d", mirror->bool_field(fd->offset())); break;
776 case T_SHORT: _out->print("%d", mirror->short_field(fd->offset())); break;
777 case T_CHAR: _out->print("%d", mirror->char_field(fd->offset())); break;
778 case T_INT: _out->print("%d", mirror->int_field(fd->offset())); break;
779 case T_LONG: _out->print(INT64_FORMAT, (int64_t)(mirror->long_field(fd->offset()))); break;
780 case T_FLOAT: {
781 float f = mirror->float_field(fd->offset());
782 _out->print("%d", *(int*)&f);
783 break;
784 }
785 case T_DOUBLE: {
786 double d = mirror->double_field(fd->offset());
787 _out->print(INT64_FORMAT, *(int64_t*)&d);
788 break;
789 }
790 case T_ARRAY: // fall-through
791 case T_OBJECT:
792 if (!fd->is_null_free_inline_type()) {
793 _out->print("%s ", fd->signature()->as_quoted_ascii());
794 oop value = mirror->obj_field_acquire(fd->offset());
795 if (value == nullptr) {
796 if (field_type == T_ARRAY) {
797 _out->print("%d", -1);
798 }
799 _out->cr();
800 } else if (value->is_instance()) {
801 assert(field_type == T_OBJECT, "");
802 if (value->is_a(vmClasses::String_klass())) {
803 const char* ascii_value = java_lang_String::as_quoted_ascii(value);
804 _out->print("\"%s\"", (ascii_value != nullptr) ? ascii_value : "");
805 } else {
806 const char* klass_name = value->klass()->name()->as_quoted_ascii();
807 _out->print("%s", klass_name);
808 }
809 } else if (value->is_array()) {
810 typeArrayOop ta = (typeArrayOop)value;
811 _out->print("%d", ta->length());
812 if (value->is_objArray() || value->is_flatArray()) {
813 objArrayOop oa = (objArrayOop)value;
814 const char* klass_name = value->klass()->name()->as_quoted_ascii();
815 _out->print(" %s", klass_name);
816 }
817 } else {
818 ShouldNotReachHere();
819 }
820 break;
821 } else {
822 // handling of null free inline type
823 ResetNoHandleMark rnhm;
824 Thread* THREAD = Thread::current();
825 SignatureStream ss(fd->signature(), false);
826 Symbol* name = ss.as_symbol();
827 assert(!HAS_PENDING_EXCEPTION, "can resolve klass?");
828 InstanceKlass* holder = fd->field_holder();
829 InstanceKlass* k = SystemDictionary::find_instance_klass(THREAD, name,
830 Handle(THREAD, holder->class_loader()));
831 assert(k != nullptr && !HAS_PENDING_EXCEPTION, "can resolve klass?");
832 InlineKlass* vk = InlineKlass::cast(k);
833 oop obj;
834 if (is_flat) {
835 int field_offset = fd->offset() - vk->payload_offset();
836 obj = cast_to_oop(cast_from_oop<address>(mirror) + field_offset);
837 } else {
838 obj = mirror->obj_field_acquire(fd->offset());
839 }
840 InlineTypeFieldPrinter print_field(_out, obj);
841 vk->do_nonstatic_fields(&print_field);
842 break;
843 }
844 default:
845 ShouldNotReachHere();
846 }
847 }
848
849 const char *ciInstanceKlass::replay_name() const {
850 return CURRENT_ENV->replay_name(get_instanceKlass());
851 }
852
853 void ciInstanceKlass::dump_replay_instanceKlass(outputStream* out, InstanceKlass* ik) {
854 if (ik->is_hidden()) {
855 const char *name = CURRENT_ENV->dyno_name(ik);
856 if (name != nullptr) {
857 out->print_cr("instanceKlass %s # %s", name, ik->name()->as_quoted_ascii());
858 } else {
859 out->print_cr("# instanceKlass %s", ik->name()->as_quoted_ascii());
860 }
861 } else {
862 out->print_cr("instanceKlass %s", ik->name()->as_quoted_ascii());
863 }
864 }
865
866 GrowableArray<ciInstanceKlass*>* ciInstanceKlass::transitive_interfaces() const{
867 if (_transitive_interfaces == nullptr) {
|