1 /*
  2  * Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 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 
 57   InstanceKlass* ik = get_instanceKlass();
 58 
 59 #ifdef ASSERT
 60   if (!ik->is_loaded()) {
 61     ResourceMark rm;
 62     ik->print_on(tty);
 63     assert(false, "must be at least loaded: %s", ik->name()->as_C_string());
 64   }
 65 #endif // ASSERT
 66 
 67   AccessFlags access_flags = ik->access_flags();
 68   _flags = ciFlags(access_flags);
 69   _has_finalizer = ik->has_finalizer();
 70   _has_subklass = flags().is_final() ? subklass_false : subklass_unknown;
 71   _init_state = compute_init_state(ik);
 72   _has_nonstatic_fields = ik->has_nonstatic_fields();
 73   _has_nonstatic_concrete_methods = ik->has_nonstatic_concrete_methods();
 74   _is_hidden = ik->is_hidden();
 75   _is_record = ik->is_record();
 76   _trust_final_fields = ik->trust_final_fields();
 77   _nonstatic_fields = nullptr; // initialized lazily by compute_nonstatic_fields:
 78   _has_injected_fields = -1;
 79   _implementor = nullptr; // we will fill these lazily
 80   _transitive_interfaces = nullptr;
 81 
 82   // Ensure that the metadata wrapped by the ciMetadata is kept alive by GC.
 83   // This is primarily useful for metadata which is considered as weak roots
 84   // by the GC but need to be strong roots if reachable from a current compilation.
 85   // InstanceKlass are created for both weak and strong metadata.  Ensuring this metadata
 86   // alive covers the cases where there are weak roots without performance cost.
 87   oop holder = ik->klass_holder();
 88   if (ik->class_loader_data()->has_class_mirror_holder()) {
 89     // Though ciInstanceKlass records class loader oop, it's not enough to keep
 90     // non-strong hidden classes alive (loader == nullptr). Klass holder should
 91     // be used instead. It is enough to record a ciObject, since cached elements are never removed
 92     // during ciObjectFactory lifetime. ciObjectFactory itself is created for
 93     // every compilation and lives for the whole duration of the compilation.
 94     assert(holder != nullptr, "holder of hidden class is the mirror which is never null");
 95     (void)CURRENT_ENV->get_object(holder);
 96   }
 97 
 98   JavaThread *thread = JavaThread::current();
 99   if (ciObjectFactory::is_initialized()) {
100     _loader = JNIHandles::make_local(thread, ik->class_loader());
101     _is_shared = false;
102   } else {
103     Handle h_loader(thread, ik->class_loader());
104     _loader = JNIHandles::make_global(h_loader);
105     _is_shared = true;
106   }
107 
108   _has_trusted_loader = compute_has_trusted_loader();
109 
110   // Lazy fields get filled in only upon request.
111   _super  = nullptr;
112   _java_mirror = nullptr;
113 
114   if (is_shared()) {
115     if (k != vmClasses::Object_klass()) {
116       super();
117     }
118     //compute_nonstatic_fields();  // done outside of constructor
119   }
120 
121   _field_cache = nullptr;
122 }
123 
124 // Version for unloaded classes:
125 ciInstanceKlass::ciInstanceKlass(ciSymbol* name,
126                                  jobject loader)
127   : ciKlass(name, T_OBJECT)
128 {
129   assert(name->char_at(0) != JVM_SIGNATURE_ARRAY, "not an instance klass");
130   _init_state = (InstanceKlass::ClassState)0;
131   _has_nonstatic_fields = false;
132   _nonstatic_fields = nullptr;
133   _has_injected_fields = -1;
134   _is_hidden = false;
135   _is_record = false;
136   _loader = loader;
137   _is_shared = false;
138   _super = nullptr;
139   _java_mirror = nullptr;
140   _field_cache = nullptr;
141   _has_trusted_loader = compute_has_trusted_loader();
142 }
143 
144 
145 
146 // ------------------------------------------------------------------
147 // ciInstanceKlass::compute_shared_is_initialized
148 void ciInstanceKlass::compute_shared_init_state() {
149   GUARDED_VM_ENTRY(
150     InstanceKlass* ik = get_instanceKlass();
151     _init_state = compute_init_state(ik);
152   )
153 }
154 
155 InstanceKlass::ClassState ciInstanceKlass::compute_init_state(InstanceKlass* ik) {
156   ASSERT_IN_VM;
157   ciEnv* env = CURRENT_ENV;
158   if (env != nullptr && env->is_precompile()) {
159     return env->compute_init_state_for_precompiled(ik);
160   } else {
161     return ik->init_state();
162   }
163 }
164 
165 // ------------------------------------------------------------------
166 // ciInstanceKlass::compute_shared_has_subklass
167 bool ciInstanceKlass::compute_shared_has_subklass() {
168   GUARDED_VM_ENTRY(
169     InstanceKlass* ik = get_instanceKlass();
170     _has_subklass = ik->subklass() != nullptr ? subklass_true : subklass_false;
171     return _has_subklass == subklass_true;
172   )
173 }
174 
175 // ------------------------------------------------------------------
176 // ciInstanceKlass::loader
177 oop ciInstanceKlass::loader() {
178   ASSERT_IN_VM;
179   return JNIHandles::resolve(_loader);
180 }
181 
182 // ------------------------------------------------------------------
183 // ciInstanceKlass::loader_handle
184 jobject ciInstanceKlass::loader_handle() {
185   return _loader;
186 }
187 
188 // ------------------------------------------------------------------
189 // ciInstanceKlass::field_cache
190 //
191 // Get the field cache associated with this klass.
192 ciConstantPoolCache* ciInstanceKlass::field_cache() {
193   if (is_shared()) {
194     return nullptr;
195   }
196   if (_field_cache == nullptr) {
197     assert(!is_java_lang_Object(), "Object has no fields");
198     Arena* arena = CURRENT_ENV->arena();
199     _field_cache = new (arena) ciConstantPoolCache(arena, 5);
200   }
201   return _field_cache;
202 }
203 
204 // ------------------------------------------------------------------
205 // ciInstanceKlass::get_canonical_holder
206 //
207 ciInstanceKlass* ciInstanceKlass::get_canonical_holder(int offset) {
208   #ifdef ASSERT
209   if (!(offset >= 0 && offset < layout_helper_size_in_bytes())) {
210     tty->print("*** get_canonical_holder(%d) on ", offset);
211     this->print();
212     tty->print_cr(" ***");
213   };
214   assert(offset >= 0 && offset < layout_helper_size_in_bytes(), "offset must be tame");
215   #endif
216 
217   if (offset < instanceOopDesc::base_offset_in_bytes()) {
218     // All header offsets belong properly to java/lang/Object.
219     return CURRENT_ENV->Object_klass();
220   }
221 
222   ciInstanceKlass* self = this;
223   assert(self->is_loaded(), "must be loaded to access field info");
224   ciField* field = self->get_field_by_offset(offset, false);
225   if (field != nullptr) {
226     return field->holder();
227   } else {
228     for (;;) {
229       assert(self->is_loaded(), "must be loaded to have size");
230       ciInstanceKlass* super = self->super();
231       if (super == nullptr ||
232           super->nof_nonstatic_fields() == 0 ||
233           super->layout_helper_size_in_bytes() <= offset) {
234         return self;
235       } else {
236         self = super;  // return super->get_canonical_holder(offset)
237       }
238     }
239   }
240 }
241 
242 // ------------------------------------------------------------------
243 // ciInstanceKlass::is_java_lang_Object
244 //
245 // Is this klass java.lang.Object?
246 bool ciInstanceKlass::is_java_lang_Object() const {
247   return equals(CURRENT_ENV->Object_klass());
248 }
249 
250 // ------------------------------------------------------------------
251 // ciInstanceKlass::uses_default_loader
252 bool ciInstanceKlass::uses_default_loader() const {
253   // Note:  We do not need to resolve the handle or enter the VM
254   // in order to test null-ness.
255   return _loader == nullptr;
256 }
257 
258 // ------------------------------------------------------------------
259 
260 /**
261  * Return basic type of boxed value for box klass or T_OBJECT if not.
262  */
263 BasicType ciInstanceKlass::box_klass_type() const {
264   if (uses_default_loader() && is_loaded()) {
265     return vmClasses::box_klass_type(get_Klass());
266   } else {
267     return T_OBJECT;
268   }
269 }
270 
271 /**
272  * Is this boxing klass?
273  */
274 bool ciInstanceKlass::is_box_klass() const {
275   return is_java_primitive(box_klass_type());
276 }
277 
278 /**
279  *  Is this boxed value offset?
280  */
281 bool ciInstanceKlass::is_boxed_value_offset(int offset) const {
282   BasicType bt = box_klass_type();
283   return is_java_primitive(bt) &&
284          (offset == java_lang_boxing_object::value_offset(bt));
285 }
286 
287 // ------------------------------------------------------------------
288 // ciInstanceKlass::is_in_package
289 //
290 // Is this klass in the given package?
291 bool ciInstanceKlass::is_in_package(const char* packagename, int len) {
292   // To avoid class loader mischief, this test always rejects application classes.
293   if (!uses_default_loader())
294     return false;
295   GUARDED_VM_ENTRY(
296     return is_in_package_impl(packagename, len);
297   )
298 }
299 
300 bool ciInstanceKlass::is_in_package_impl(const char* packagename, int len) {
301   ASSERT_IN_VM;
302 
303   // If packagename contains trailing '/' exclude it from the
304   // prefix-test since we test for it explicitly.
305   if (packagename[len - 1] == '/')
306     len--;
307 
308   if (!name()->starts_with(packagename, len))
309     return false;
310 
311   // Test if the class name is something like "java/lang".
312   if ((len + 1) > name()->utf8_length())
313     return false;
314 
315   // Test for trailing '/'
316   if (name()->char_at(len) != '/')
317     return false;
318 
319   // Make sure it's not actually in a subpackage:
320   if (name()->index_of_at(len+1, "/", 1) >= 0)
321     return false;
322 
323   return true;
324 }
325 
326 // ------------------------------------------------------------------
327 // ciInstanceKlass::print_impl
328 //
329 // Implementation of the print method.
330 void ciInstanceKlass::print_impl(outputStream* st) {
331   ciKlass::print_impl(st);
332   GUARDED_VM_ENTRY(st->print(" loader=" INTPTR_FORMAT, p2i(loader()));)
333   if (is_loaded()) {
334     st->print(" initialized=%s finalized=%s subklass=%s size=%d flags=",
335               bool_to_str(is_initialized()),
336               bool_to_str(has_finalizer()),
337               bool_to_str(has_subklass()),
338               layout_helper());
339 
340     _flags.print_klass_flags(st);
341 
342     if (_super) {
343       st->print(" super=");
344       _super->print_name_on(st);
345     }
346     if (_java_mirror) {
347       st->print(" mirror=PRESENT");
348     }
349   }
350 }
351 
352 // ------------------------------------------------------------------
353 // ciInstanceKlass::super
354 //
355 // Get the superklass of this klass.
356 ciInstanceKlass* ciInstanceKlass::super() {
357   assert(is_loaded(), "must be loaded");
358   if (_super == nullptr && !is_java_lang_Object()) {
359     GUARDED_VM_ENTRY(
360       Klass* super_klass = get_instanceKlass()->super();
361       _super = CURRENT_ENV->get_instance_klass(super_klass);
362     )
363   }
364   return _super;
365 }
366 
367 // ------------------------------------------------------------------
368 // ciInstanceKlass::java_mirror
369 //
370 // Get the instance of java.lang.Class corresponding to this klass.
371 // Cache it on this->_java_mirror.
372 ciInstance* ciInstanceKlass::java_mirror() {
373   if (is_shared()) {
374     return ciKlass::java_mirror();
375   }
376   if (_java_mirror == nullptr) {
377     _java_mirror = ciKlass::java_mirror();
378   }
379   return _java_mirror;
380 }
381 
382 // ------------------------------------------------------------------
383 // ciInstanceKlass::unique_concrete_subklass
384 ciInstanceKlass* ciInstanceKlass::unique_concrete_subklass() {
385   if (!is_loaded())     return nullptr; // No change if class is not loaded
386   if (!is_abstract())   return nullptr; // Only applies to abstract classes.
387   if (!has_subklass())  return nullptr; // Must have at least one subklass.
388   VM_ENTRY_MARK;
389   InstanceKlass* ik = get_instanceKlass();
390   Klass* up = ik->up_cast_abstract();
391   assert(up->is_instance_klass(), "must be InstanceKlass");
392   if (ik == up) {
393     return nullptr;
394   }
395   return CURRENT_THREAD_ENV->get_instance_klass(up);
396 }
397 
398 // ------------------------------------------------------------------
399 // ciInstanceKlass::has_finalizable_subclass
400 bool ciInstanceKlass::has_finalizable_subclass() {
401   if (!is_loaded())     return true;
402   VM_ENTRY_MARK;
403   return Dependencies::find_finalizable_subclass(get_instanceKlass()) != nullptr;
404 }
405 
406 // ------------------------------------------------------------------
407 // ciInstanceKlass::contains_field_offset
408 bool ciInstanceKlass::contains_field_offset(int offset) {
409   VM_ENTRY_MARK;
410   return get_instanceKlass()->contains_field_offset(offset);
411 }
412 
413 ciField* ciInstanceKlass::get_non_static_field_by_offset(const int field_offset) {
414   for (int i = 0, len = nof_nonstatic_fields(); i < len; i++) {
415     ciField* field = _nonstatic_fields->at(i);
416     int field_off = field->offset_in_bytes();
417     if (field_off == field_offset)
418       return field;
419   }
420   return nullptr;
421 }
422 
423 // ------------------------------------------------------------------
424 // ciInstanceKlass::get_field_by_offset
425 ciField* ciInstanceKlass::get_field_by_offset(int field_offset, bool is_static) {
426   if (!is_static) {
427     return get_non_static_field_by_offset(field_offset);
428   }
429   VM_ENTRY_MARK;
430   InstanceKlass* k = get_instanceKlass();
431   fieldDescriptor fd;
432   if (!k->find_field_from_offset(field_offset, is_static, &fd)) {
433     return nullptr;
434   }
435   ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd);
436   return field;
437 }
438 
439 // ------------------------------------------------------------------
440 // ciInstanceKlass::get_field_by_name
441 ciField* ciInstanceKlass::get_field_by_name(ciSymbol* name, ciSymbol* signature, bool is_static) {
442   VM_ENTRY_MARK;
443   InstanceKlass* k = get_instanceKlass();
444   fieldDescriptor fd;
445   Klass* def = k->find_field(name->get_symbol(), signature->get_symbol(), is_static, &fd);
446   if (def == nullptr) {
447     return nullptr;
448   }
449   ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd);
450   return field;
451 }
452 
453 // This is essentially a shortcut for:
454 //   get_field_by_offset(field_offset, is_static)->layout_type()
455 // except this does not require allocating memory for a new ciField
456 BasicType ciInstanceKlass::get_field_type_by_offset(const int field_offset, const bool is_static) {
457   if (!is_static) {
458     ciField* field = get_non_static_field_by_offset(field_offset);
459     return field != nullptr ? field->layout_type() : T_ILLEGAL;
460   }
461 
462   // Avoid allocating a new ciField by obtaining the field type directly
463   VM_ENTRY_MARK;
464   InstanceKlass* k = get_instanceKlass();
465   fieldDescriptor fd;
466   if (!k->find_field_from_offset(field_offset, is_static, &fd)) {
467     return T_ILLEGAL;
468   }
469 
470   // Reproduce the behavior of ciField::layout_type
471   BasicType field_type = fd.field_type();
472   if (is_reference_type(field_type)) {
473     return T_OBJECT;
474   }
475   return type2field[make(field_type)->basic_type()];
476 }
477 
478 // ------------------------------------------------------------------
479 // ciInstanceKlass::compute_nonstatic_fields
480 int ciInstanceKlass::compute_nonstatic_fields() {
481   assert(is_loaded(), "must be loaded");
482 
483   if (_nonstatic_fields != nullptr)
484     return _nonstatic_fields->length();
485 
486   if (!has_nonstatic_fields()) {
487     Arena* arena = CURRENT_ENV->arena();
488     _nonstatic_fields = new (arena) GrowableArray<ciField*>(arena, 0, 0, nullptr);
489     return 0;
490   }
491   assert(!is_java_lang_Object(), "bootstrap OK");
492 
493   ciInstanceKlass* super = this->super();
494   GrowableArray<ciField*>* super_fields = nullptr;
495   if (super != nullptr && super->has_nonstatic_fields()) {
496     int super_flen   = super->nof_nonstatic_fields();
497     super_fields = super->_nonstatic_fields;
498     assert(super_flen == 0 || super_fields != nullptr, "first get nof_fields");
499   }
500 
501   GrowableArray<ciField*>* fields = nullptr;
502   GUARDED_VM_ENTRY({
503       fields = compute_nonstatic_fields_impl(super_fields);
504     });
505 
506   if (fields == nullptr) {
507     // This can happen if this class (java.lang.Class) has invisible fields.
508     if (super_fields != nullptr) {
509       _nonstatic_fields = super_fields;
510       return super_fields->length();
511     } else {
512       return 0;
513     }
514   }
515 
516   int flen = fields->length();
517 
518   _nonstatic_fields = fields;
519   return flen;
520 }
521 
522 GrowableArray<ciField*>*
523 ciInstanceKlass::compute_nonstatic_fields_impl(GrowableArray<ciField*>*
524                                                super_fields) {
525   ASSERT_IN_VM;
526   Arena* arena = CURRENT_ENV->arena();
527   int flen = 0;
528   GrowableArray<ciField*>* fields = nullptr;
529   InstanceKlass* k = get_instanceKlass();
530   for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
531     if (fs.access_flags().is_static())  continue;
532     flen += 1;
533   }
534 
535   // allocate the array:
536   if (flen == 0) {
537     return nullptr;  // return nothing if none are locally declared
538   }
539   if (super_fields != nullptr) {
540     flen += super_fields->length();
541   }
542   fields = new (arena) GrowableArray<ciField*>(arena, flen, 0, nullptr);
543   if (super_fields != nullptr) {
544     fields->appendAll(super_fields);
545   }
546 
547   for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
548     if (fs.access_flags().is_static())  continue;
549     fieldDescriptor& fd = fs.field_descriptor();
550     ciField* field = new (arena) ciField(&fd);
551     fields->append(field);
552   }
553   assert(fields->length() == flen, "sanity");
554   return fields;
555 }
556 
557 bool ciInstanceKlass::compute_injected_fields_helper() {
558   ASSERT_IN_VM;
559   InstanceKlass* k = get_instanceKlass();
560 
561   for (InternalFieldStream fs(k); !fs.done(); fs.next()) {
562     if (fs.access_flags().is_static())  continue;
563     return true;
564   }
565   return false;
566 }
567 
568 void ciInstanceKlass::compute_injected_fields() {
569   assert(is_loaded(), "must be loaded");
570 
571   int has_injected_fields = 0;
572   if (super() != nullptr && super()->has_injected_fields()) {
573     has_injected_fields = 1;
574   } else {
575     GUARDED_VM_ENTRY({
576         has_injected_fields = compute_injected_fields_helper() ? 1 : 0;
577       });
578   }
579   // may be concurrently initialized for shared ciInstanceKlass objects
580   assert(_has_injected_fields == -1 || _has_injected_fields == has_injected_fields, "broken concurrent initialization");
581   _has_injected_fields = has_injected_fields;
582 }
583 
584 bool ciInstanceKlass::has_object_fields() const {
585   GUARDED_VM_ENTRY(
586       return get_instanceKlass()->nonstatic_oop_map_size() > 0;
587     );
588 }
589 
590 bool ciInstanceKlass::compute_has_trusted_loader() {
591   ASSERT_IN_VM;
592   oop loader_oop = loader();
593   if (loader_oop == nullptr) {
594     return true; // bootstrap class loader
595   }
596   return java_lang_ClassLoader::is_trusted_loader(loader_oop);
597 }
598 
599 bool ciInstanceKlass::has_class_initializer() {
600   VM_ENTRY_MARK;
601   return get_instanceKlass()->class_initializer() != nullptr;
602 }
603 
604 // ------------------------------------------------------------------
605 // ciInstanceKlass::find_method
606 //
607 // Find a method in this klass.
608 ciMethod* ciInstanceKlass::find_method(ciSymbol* name, ciSymbol* signature) {
609   VM_ENTRY_MARK;
610   InstanceKlass* k = get_instanceKlass();
611   Symbol* name_sym = name->get_symbol();
612   Symbol* sig_sym= signature->get_symbol();
613 
614   Method* m = k->find_method(name_sym, sig_sym);
615   if (m == nullptr)  return nullptr;
616 
617   return CURRENT_THREAD_ENV->get_method(m);
618 }
619 
620 // ------------------------------------------------------------------
621 // ciInstanceKlass::is_leaf_type
622 bool ciInstanceKlass::is_leaf_type() {
623   assert(is_loaded(), "must be loaded");
624   if (is_shared()) {
625     return is_final();  // approximately correct
626   } else {
627     return !has_subklass() && (!is_interface() || nof_implementors() == 0);
628   }
629 }
630 
631 // ------------------------------------------------------------------
632 // ciInstanceKlass::implementor
633 //
634 // Report an implementor of this interface.
635 // Note that there are various races here, since my copy
636 // of _nof_implementors might be out of date with respect
637 // to results returned by InstanceKlass::implementor.
638 // This is OK, since any dependencies we decide to assert
639 // will be checked later under the Compile_lock.
640 ciInstanceKlass* ciInstanceKlass::implementor() {
641   assert(is_interface(), "required");
642   ciInstanceKlass* impl = _implementor;
643   if (impl == nullptr) {
644     if (is_shared()) {
645       impl = this; // assume a well-known interface never has a unique implementor
646     } else {
647       // Go into the VM to fetch the implementor.
648       VM_ENTRY_MARK;
649       InstanceKlass* ik = get_instanceKlass();
650       Klass* implk = ik->implementor();
651       if (implk != nullptr) {
652         if (implk == ik) {
653           // More than one implementors. Use 'this' in this case.
654           impl = this;
655         } else {
656           impl = CURRENT_THREAD_ENV->get_instance_klass(implk);
657         }
658       }
659     }
660     // Memoize this result.
661     _implementor = impl;
662   }
663   return impl;
664 }
665 
666 // Utility class for printing of the contents of the static fields for
667 // use by compilation replay.  It only prints out the information that
668 // could be consumed by the compiler, so for primitive types it prints
669 // out the actual value.  For Strings it's the actual string value.
670 // For array types it it's first level array size since that's the
671 // only value which statically unchangeable.  For all other reference
672 // types it simply prints out the dynamic type.
673 
674 class StaticFinalFieldPrinter : public FieldClosure {
675   outputStream* _out;
676   const char*   _holder;
677  public:
678   StaticFinalFieldPrinter(outputStream* out, const char* holder) :
679     _out(out),
680     _holder(holder) {
681   }
682   void do_field(fieldDescriptor* fd) {
683     if (fd->is_final() && !fd->has_initial_value()) {
684       ResourceMark rm;
685       oop mirror = fd->field_holder()->java_mirror();
686       _out->print("staticfield %s %s %s ", _holder, fd->name()->as_quoted_ascii(), fd->signature()->as_quoted_ascii());
687       BasicType field_type = fd->field_type();
688       switch (field_type) {
689         case T_BYTE:    _out->print_cr("%d", mirror->byte_field(fd->offset()));   break;
690         case T_BOOLEAN: _out->print_cr("%d", mirror->bool_field(fd->offset()));   break;
691         case T_SHORT:   _out->print_cr("%d", mirror->short_field(fd->offset()));  break;
692         case T_CHAR:    _out->print_cr("%d", mirror->char_field(fd->offset()));   break;
693         case T_INT:     _out->print_cr("%d", mirror->int_field(fd->offset()));    break;
694         case T_LONG:    _out->print_cr(INT64_FORMAT, (int64_t)(mirror->long_field(fd->offset())));   break;
695         case T_FLOAT: {
696           float f = mirror->float_field(fd->offset());
697           _out->print_cr("%d", *(int*)&f);
698           break;
699         }
700         case T_DOUBLE: {
701           double d = mirror->double_field(fd->offset());
702           _out->print_cr(INT64_FORMAT, *(int64_t*)&d);
703           break;
704         }
705         case T_ARRAY:  // fall-through
706         case T_OBJECT: {
707           oop value =  mirror->obj_field_acquire(fd->offset());
708           if (value == nullptr) {
709             if (field_type == T_ARRAY) {
710               _out->print("%d", -1);
711             }
712             _out->cr();
713           } else if (value->is_instance()) {
714             assert(field_type == T_OBJECT, "");
715             if (value->is_a(vmClasses::String_klass())) {
716               const char* ascii_value = java_lang_String::as_quoted_ascii(value);
717               _out->print_cr("\"%s\"", (ascii_value != nullptr) ? ascii_value : "");
718             } else {
719               const char* klass_name  = value->klass()->name()->as_quoted_ascii();
720               _out->print_cr("%s", klass_name);
721             }
722           } else if (value->is_array()) {
723             typeArrayOop ta = (typeArrayOop)value;
724             _out->print("%d", ta->length());
725             if (value->is_objArray()) {
726               objArrayOop oa = (objArrayOop)value;
727               const char* klass_name  = value->klass()->name()->as_quoted_ascii();
728               _out->print(" %s", klass_name);
729             }
730             _out->cr();
731           } else {
732             ShouldNotReachHere();
733           }
734           break;
735         }
736         default:
737           ShouldNotReachHere();
738         }
739     }
740   }
741 };
742 
743 const char *ciInstanceKlass::replay_name() const {
744   return CURRENT_ENV->replay_name(get_instanceKlass());
745 }
746 
747 void ciInstanceKlass::dump_replay_instanceKlass(outputStream* out, InstanceKlass* ik) {
748   if (ik->is_hidden()) {
749     const char *name = CURRENT_ENV->dyno_name(ik);
750     if (name != nullptr) {
751       out->print_cr("instanceKlass %s # %s", name, ik->name()->as_quoted_ascii());
752     } else {
753       out->print_cr("# instanceKlass %s", ik->name()->as_quoted_ascii());
754     }
755   } else {
756     out->print_cr("instanceKlass %s", ik->name()->as_quoted_ascii());
757   }
758 }
759 
760 GrowableArray<ciInstanceKlass*>* ciInstanceKlass::transitive_interfaces() const{
761   if (_transitive_interfaces == nullptr) {
762     const_cast<ciInstanceKlass*>(this)->compute_transitive_interfaces();
763   }
764   return _transitive_interfaces;
765 }
766 
767 void ciInstanceKlass::compute_transitive_interfaces() {
768   GUARDED_VM_ENTRY(
769           InstanceKlass* ik = get_instanceKlass();
770           Array<InstanceKlass*>* interfaces = ik->transitive_interfaces();
771           int orig_length = interfaces->length();
772           Arena* arena = CURRENT_ENV->arena();
773           int transitive_interfaces_len = orig_length + (is_interface() ? 1 : 0);
774           GrowableArray<ciInstanceKlass*>* transitive_interfaces = new(arena)GrowableArray<ciInstanceKlass*>(arena, transitive_interfaces_len,
775                                                                                                              0, nullptr);
776           for (int i = 0; i < orig_length; i++) {
777             transitive_interfaces->append(CURRENT_ENV->get_instance_klass(interfaces->at(i)));
778           }
779           if (is_interface()) {
780             transitive_interfaces->append(this);
781           }
782           _transitive_interfaces = transitive_interfaces;
783   );
784 }
785 
786 void ciInstanceKlass::dump_replay_data(outputStream* out) {
787   ResourceMark rm;
788 
789   InstanceKlass* ik = get_instanceKlass();
790   ConstantPool*  cp = ik->constants();
791 
792   // Try to record related loaded classes
793   Klass* sub = ik->subklass();
794   while (sub != nullptr) {
795     if (sub->is_instance_klass()) {
796       InstanceKlass *isub = InstanceKlass::cast(sub);
797       dump_replay_instanceKlass(out, isub);
798     }
799     sub = sub->next_sibling();
800   }
801 
802   // Dump out the state of the constant pool tags.  During replay the
803   // tags will be validated for things which shouldn't change and
804   // classes will be resolved if the tags indicate that they were
805   // resolved at compile time.
806   const char *name = replay_name();
807   out->print("ciInstanceKlass %s %d %d %d", name,
808              is_linked(), is_initialized(), cp->length());
809   for (int index = 1; index < cp->length(); index++) {
810     out->print(" %d", cp->tags()->at(index));
811   }
812   out->cr();
813   if (is_initialized()) {
814     //  Dump out the static final fields in case the compilation relies
815     //  on their value for correct replay.
816     StaticFinalFieldPrinter sffp(out, name);
817     ik->do_local_static_fields(&sffp);
818   }
819 }
820 
821 #ifdef ASSERT
822 bool ciInstanceKlass::debug_final_field_at(int offset) {
823   GUARDED_VM_ENTRY(
824     InstanceKlass* ik = get_instanceKlass();
825     fieldDescriptor fd;
826     if (ik->find_field_from_offset(offset, false, &fd)) {
827       return fd.is_final();
828     }
829   );
830   return false;
831 }
832 
833 bool ciInstanceKlass::debug_stable_field_at(int offset) {
834   GUARDED_VM_ENTRY(
835     InstanceKlass* ik = get_instanceKlass();
836     fieldDescriptor fd;
837     if (ik->find_field_from_offset(offset, false, &fd)) {
838       return fd.is_stable();
839     }
840   );
841   return false;
842 }
843 #endif