1 /*
  2  * Copyright (c) 1999, 2022, 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 "precompiled.hpp"
 26 #include "ci/ciField.hpp"
 27 #include "ci/ciInlineKlass.hpp"
 28 #include "ci/ciInstance.hpp"
 29 #include "ci/ciInstanceKlass.hpp"
 30 #include "ci/ciUtilities.inline.hpp"
 31 #include "classfile/javaClasses.hpp"
 32 #include "classfile/systemDictionary.hpp"
 33 #include "classfile/vmClasses.hpp"
 34 #include "memory/allocation.hpp"
 35 #include "memory/allocation.inline.hpp"
 36 #include "memory/resourceArea.hpp"
 37 #include "oops/instanceKlass.inline.hpp"
 38 #include "oops/klass.inline.hpp"
 39 #include "oops/oop.inline.hpp"
 40 #include "oops/fieldStreams.inline.hpp"
 41 #include "oops/inlineKlass.inline.hpp"
 42 #include "runtime/fieldDescriptor.inline.hpp"
 43 #include "runtime/handles.inline.hpp"
 44 #include "runtime/jniHandles.inline.hpp"
 45 
 46 // ciInstanceKlass
 47 //
 48 // This class represents a Klass* in the HotSpot virtual machine
 49 // whose Klass part in an InstanceKlass.
 50 
 51 
 52 // ------------------------------------------------------------------
 53 // ciInstanceKlass::ciInstanceKlass
 54 //
 55 // Loaded instance klass.
 56 ciInstanceKlass::ciInstanceKlass(Klass* k) :
 57   ciKlass(k)
 58 {
 59   assert(get_Klass()->is_instance_klass(), "wrong type");
 60   assert(get_instanceKlass()->is_loaded(), "must be at least loaded");
 61   InstanceKlass* ik = get_instanceKlass();
 62 
 63   AccessFlags access_flags = ik->access_flags();
 64   _flags = ciFlags(access_flags);
 65   _has_finalizer = access_flags.has_finalizer();
 66   _has_subklass = flags().is_final() ? subklass_false : subklass_unknown;
 67   _init_state = ik->init_state();
 68   _has_nonstatic_fields = ik->has_nonstatic_fields();
 69   _has_nonstatic_concrete_methods = ik->has_nonstatic_concrete_methods();
 70   _is_hidden = ik->is_hidden();
 71   _is_record = ik->is_record();
 72   _nonstatic_fields = NULL; // initialized lazily by compute_nonstatic_fields:
 73   _has_injected_fields = -1;
 74   _implementor = NULL; // we will fill these lazily
 75   _transitive_interfaces = NULL;
 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 == NULL). 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 != NULL, "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()) {
 95     _loader = JNIHandles::make_local(thread, ik->class_loader());
 96     _protection_domain = JNIHandles::make_local(thread,
 97                                                 ik->protection_domain());
 98     _is_shared = false;
 99   } else {
100     Handle h_loader(thread, ik->class_loader());
101     Handle h_protection_domain(thread, ik->protection_domain());
102     _loader = JNIHandles::make_global(h_loader);
103     _protection_domain = JNIHandles::make_global(h_protection_domain);
104     _is_shared = true;
105   }
106 
107   _has_trusted_loader = compute_has_trusted_loader();
108 
109   // Lazy fields get filled in only upon request.
110   _super  = NULL;
111   _java_mirror = NULL;
112 
113   if (is_shared()) {
114     if (k != vmClasses::Object_klass()) {
115       super();
116     }
117     //compute_nonstatic_fields();  // done outside of constructor
118   }
119 
120   _field_cache = NULL;
121 }
122 
123 // Version for unloaded classes:
124 ciInstanceKlass::ciInstanceKlass(ciSymbol* name,
125                                  jobject loader, jobject protection_domain,
126                                  BasicType bt)
127   : ciKlass(name, bt)
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 = NULL;            // initialized lazily by compute_nonstatic_fields
133   _has_injected_fields = -1;
134   _is_hidden = false;
135   _is_record = false;
136   _loader = loader;
137   _protection_domain = protection_domain;
138   _is_shared = false;
139   _super = NULL;
140   _java_mirror = NULL;
141   _field_cache = NULL;
142   _has_trusted_loader = compute_has_trusted_loader();
143 }
144 
145 
146 
147 // ------------------------------------------------------------------
148 // ciInstanceKlass::compute_shared_is_initialized
149 void ciInstanceKlass::compute_shared_init_state() {
150   GUARDED_VM_ENTRY(
151     InstanceKlass* ik = get_instanceKlass();
152     _init_state = ik->init_state();
153   )
154 }
155 
156 // ------------------------------------------------------------------
157 // ciInstanceKlass::compute_shared_has_subklass
158 bool ciInstanceKlass::compute_shared_has_subklass() {
159   GUARDED_VM_ENTRY(
160     InstanceKlass* ik = get_instanceKlass();
161     _has_subklass = ik->subklass() != NULL ? subklass_true : subklass_false;
162     return _has_subklass == subklass_true;
163   )
164 }
165 
166 // ------------------------------------------------------------------
167 // ciInstanceKlass::loader
168 oop ciInstanceKlass::loader() {
169   ASSERT_IN_VM;
170   return JNIHandles::resolve(_loader);
171 }
172 
173 // ------------------------------------------------------------------
174 // ciInstanceKlass::loader_handle
175 jobject ciInstanceKlass::loader_handle() {
176   return _loader;
177 }
178 
179 // ------------------------------------------------------------------
180 // ciInstanceKlass::protection_domain
181 oop ciInstanceKlass::protection_domain() {
182   ASSERT_IN_VM;
183   return JNIHandles::resolve(_protection_domain);
184 }
185 
186 // ------------------------------------------------------------------
187 // ciInstanceKlass::protection_domain_handle
188 jobject ciInstanceKlass::protection_domain_handle() {
189   return _protection_domain;
190 }
191 
192 // ------------------------------------------------------------------
193 // ciInstanceKlass::field_cache
194 //
195 // Get the field cache associated with this klass.
196 ciConstantPoolCache* ciInstanceKlass::field_cache() {
197   if (is_shared()) {
198     return NULL;
199   }
200   if (_field_cache == NULL) {
201     assert(!is_java_lang_Object(), "Object has no fields");
202     Arena* arena = CURRENT_ENV->arena();
203     _field_cache = new (arena) ciConstantPoolCache(arena, 5);
204   }
205   return _field_cache;
206 }
207 
208 // ------------------------------------------------------------------
209 // ciInstanceKlass::get_canonical_holder
210 //
211 ciInstanceKlass* ciInstanceKlass::get_canonical_holder(int offset) {
212   #ifdef ASSERT
213   if (!(offset >= 0 && offset < layout_helper_size_in_bytes())) {
214     tty->print("*** get_canonical_holder(%d) on ", offset);
215     this->print();
216     tty->print_cr(" ***");
217   };
218   assert(offset >= 0 && offset < layout_helper_size_in_bytes(), "offset must be tame");
219   #endif
220 
221   if (offset < instanceOopDesc::base_offset_in_bytes()) {
222     // All header offsets belong properly to java/lang/Object.
223     return CURRENT_ENV->Object_klass();
224   }
225 
226   ciInstanceKlass* self = this;
227   assert(self->is_loaded(), "must be loaded to access field info");
228   ciField* field = self->get_field_by_offset(offset, false);
229   if (field != NULL) {
230     return field->holder();
231   } else {
232     for (;;) {
233       assert(self->is_loaded(), "must be loaded to have size");
234       ciInstanceKlass* super = self->super();
235       if (super == NULL ||
236           super->nof_nonstatic_fields() == 0 ||
237           super->layout_helper_size_in_bytes() <= offset) {
238         return self;
239       } else {
240         self = super;  // return super->get_canonical_holder(offset)
241       }
242     }
243   }
244 }
245 
246 // ------------------------------------------------------------------
247 // ciInstanceKlass::is_java_lang_Object
248 //
249 // Is this klass java.lang.Object?
250 bool ciInstanceKlass::is_java_lang_Object() const {
251   return equals(CURRENT_ENV->Object_klass());
252 }
253 
254 // ------------------------------------------------------------------
255 // ciInstanceKlass::uses_default_loader
256 bool ciInstanceKlass::uses_default_loader() const {
257   // Note:  We do not need to resolve the handle or enter the VM
258   // in order to test null-ness.
259   return _loader == NULL;
260 }
261 
262 // ------------------------------------------------------------------
263 
264 /**
265  * Return basic type of boxed value for box klass or T_OBJECT if not.
266  */
267 BasicType ciInstanceKlass::box_klass_type() const {
268   if (uses_default_loader() && is_loaded()) {
269     return vmClasses::box_klass_type(get_Klass());
270   } else {
271     return T_OBJECT;
272   }
273 }
274 
275 /**
276  * Is this boxing klass?
277  */
278 bool ciInstanceKlass::is_box_klass() const {
279   return is_java_primitive(box_klass_type());
280 }
281 
282 /**
283  *  Is this boxed value offset?
284  */
285 bool ciInstanceKlass::is_boxed_value_offset(int offset) const {
286   BasicType bt = box_klass_type();
287   return is_java_primitive(bt) &&
288          (offset == java_lang_boxing_object::value_offset(bt));
289 }
290 
291 // ------------------------------------------------------------------
292 // ciInstanceKlass::is_in_package
293 //
294 // Is this klass in the given package?
295 bool ciInstanceKlass::is_in_package(const char* packagename, int len) {
296   // To avoid class loader mischief, this test always rejects application classes.
297   if (!uses_default_loader())
298     return false;
299   GUARDED_VM_ENTRY(
300     return is_in_package_impl(packagename, len);
301   )
302 }
303 
304 bool ciInstanceKlass::is_in_package_impl(const char* packagename, int len) {
305   ASSERT_IN_VM;
306 
307   // If packagename contains trailing '/' exclude it from the
308   // prefix-test since we test for it explicitly.
309   if (packagename[len - 1] == '/')
310     len--;
311 
312   if (!name()->starts_with(packagename, len))
313     return false;
314 
315   // Test if the class name is something like "java/lang".
316   if ((len + 1) > name()->utf8_length())
317     return false;
318 
319   // Test for trailing '/'
320   if (name()->char_at(len) != '/')
321     return false;
322 
323   // Make sure it's not actually in a subpackage:
324   if (name()->index_of_at(len+1, "/", 1) >= 0)
325     return false;
326 
327   return true;
328 }
329 
330 // ------------------------------------------------------------------
331 // ciInstanceKlass::print_impl
332 //
333 // Implementation of the print method.
334 void ciInstanceKlass::print_impl(outputStream* st) {
335   ciKlass::print_impl(st);
336   GUARDED_VM_ENTRY(st->print(" loader=" INTPTR_FORMAT, p2i(loader()));)
337   if (is_loaded()) {
338     st->print(" initialized=%s finalized=%s subklass=%s size=%d flags=",
339               bool_to_str(is_initialized()),
340               bool_to_str(has_finalizer()),
341               bool_to_str(has_subklass()),
342               layout_helper());
343 
344     _flags.print_klass_flags(st);
345 
346     if (_super) {
347       st->print(" super=");
348       _super->print_name_on(st);
349     }
350     if (_java_mirror) {
351       st->print(" mirror=PRESENT");
352     }
353   }
354 }
355 
356 // ------------------------------------------------------------------
357 // ciInstanceKlass::super
358 //
359 // Get the superklass of this klass.
360 ciInstanceKlass* ciInstanceKlass::super() {
361   assert(is_loaded(), "must be loaded");
362   if (_super == NULL && !is_java_lang_Object()) {
363     GUARDED_VM_ENTRY(
364       Klass* super_klass = get_instanceKlass()->super();
365       _super = CURRENT_ENV->get_instance_klass(super_klass);
366     )
367   }
368   return _super;
369 }
370 
371 // ------------------------------------------------------------------
372 // ciInstanceKlass::java_mirror
373 //
374 // Get the instance of java.lang.Class corresponding to this klass.
375 // Cache it on this->_java_mirror.
376 ciInstance* ciInstanceKlass::java_mirror() {
377   if (is_shared()) {
378     return ciKlass::java_mirror();
379   }
380   if (_java_mirror == NULL) {
381     _java_mirror = ciKlass::java_mirror();
382   }
383   return _java_mirror;
384 }
385 
386 // ------------------------------------------------------------------
387 // ciInstanceKlass::unique_concrete_subklass
388 ciInstanceKlass* ciInstanceKlass::unique_concrete_subklass() {
389   if (!is_loaded())     return NULL; // No change if class is not loaded
390   if (!is_abstract())   return NULL; // Only applies to abstract classes.
391   if (!has_subklass())  return NULL; // Must have at least one subklass.
392   VM_ENTRY_MARK;
393   InstanceKlass* ik = get_instanceKlass();
394   Klass* up = ik->up_cast_abstract();
395   assert(up->is_instance_klass(), "must be InstanceKlass");
396   if (ik == up) {
397     return NULL;
398   }
399   return CURRENT_THREAD_ENV->get_instance_klass(up);
400 }
401 
402 // ------------------------------------------------------------------
403 // ciInstanceKlass::has_finalizable_subclass
404 bool ciInstanceKlass::has_finalizable_subclass() {
405   if (!is_loaded())     return true;
406   VM_ENTRY_MARK;
407   return Dependencies::find_finalizable_subclass(get_instanceKlass()) != NULL;
408 }
409 
410 // ------------------------------------------------------------------
411 // ciInstanceKlass::contains_field_offset
412 bool ciInstanceKlass::contains_field_offset(int offset) {
413   VM_ENTRY_MARK;
414   return get_instanceKlass()->contains_field_offset(offset);
415 }
416 
417 // ------------------------------------------------------------------
418 // ciInstanceKlass::get_field_by_offset
419 ciField* ciInstanceKlass::get_field_by_offset(int field_offset, bool is_static) {
420   if (!is_static) {
421     for (int i = 0, len = nof_nonstatic_fields(); i < len; i++) {
422       ciField* field = _nonstatic_fields->at(i);
423       int  field_off = field->offset_in_bytes();
424       if (field_off == field_offset)
425         return field;
426       if (field_off > field_offset)
427         break;
428       // could do binary search or check bins, but probably not worth it
429     }
430     return NULL;
431   }
432   VM_ENTRY_MARK;
433   InstanceKlass* k = get_instanceKlass();
434   fieldDescriptor fd;
435   if (!k->find_field_from_offset(field_offset, is_static, &fd)) {
436     return NULL;
437   }
438   ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd);
439   return field;
440 }
441 
442 ciField* ciInstanceKlass::get_non_flattened_field_by_offset(int field_offset) {
443   if (super() != NULL && super()->has_nonstatic_fields()) {
444     ciField* f = super()->get_non_flattened_field_by_offset(field_offset);
445     if (f != NULL) {
446       return f;
447     }
448   }
449 
450   VM_ENTRY_MARK;
451   InstanceKlass* k = get_instanceKlass();
452   Arena* arena = CURRENT_ENV->arena();
453   for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
454     if (fs.access_flags().is_static())  continue;
455     fieldDescriptor& fd = fs.field_descriptor();
456     if (fd.offset() == field_offset) {
457       ciField* f = new (arena) ciField(&fd);
458       return f;
459     }
460   }
461 
462   return NULL;
463 }
464 
465 // ------------------------------------------------------------------
466 // ciInstanceKlass::get_field_by_name
467 ciField* ciInstanceKlass::get_field_by_name(ciSymbol* name, ciSymbol* signature, bool is_static) {
468   VM_ENTRY_MARK;
469   InstanceKlass* k = get_instanceKlass();
470   fieldDescriptor fd;
471   Klass* def = k->find_field(name->get_symbol(), signature->get_symbol(), is_static, &fd);
472   if (def == NULL) {
473     return NULL;
474   }
475   ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd);
476   return field;
477 }
478 
479 
480 static int sort_field_by_offset(ciField** a, ciField** b) {
481   return (*a)->offset_in_bytes() - (*b)->offset_in_bytes();
482   // (no worries about 32-bit overflow...)
483 }
484 
485 // ------------------------------------------------------------------
486 // ciInstanceKlass::compute_nonstatic_fields
487 int ciInstanceKlass::compute_nonstatic_fields() {
488   assert(is_loaded(), "must be loaded");
489 
490   if (_nonstatic_fields != NULL)
491     return _nonstatic_fields->length();
492 
493   if (!has_nonstatic_fields()) {
494     Arena* arena = CURRENT_ENV->arena();
495     _nonstatic_fields = new (arena) GrowableArray<ciField*>(arena, 0, 0, NULL);
496     return 0;
497   }
498   assert(!is_java_lang_Object(), "bootstrap OK");
499 
500   ciInstanceKlass* super = this->super();
501   GrowableArray<ciField*>* super_fields = NULL;
502   if (super != NULL && super->has_nonstatic_fields()) {
503     int super_flen   = super->nof_nonstatic_fields();
504     super_fields = super->_nonstatic_fields;
505     assert(super_flen == 0 || super_fields != NULL, "first get nof_fields");
506   }
507 
508   GrowableArray<ciField*>* fields = NULL;
509   GUARDED_VM_ENTRY({
510       fields = compute_nonstatic_fields_impl(super_fields);
511     });
512 
513   if (fields == NULL) {
514     // This can happen if this class (java.lang.Class) has invisible fields.
515     if (super_fields != NULL) {
516       _nonstatic_fields = super_fields;
517       return super_fields->length();
518     } else {
519       return 0;
520     }
521   }
522 
523   _nonstatic_fields = fields;
524   return fields->length();
525 }
526 
527 GrowableArray<ciField*>* ciInstanceKlass::compute_nonstatic_fields_impl(GrowableArray<ciField*>* super_fields, bool flatten) {
528   ASSERT_IN_VM;
529   Arena* arena = CURRENT_ENV->arena();
530   int flen = 0;
531   GrowableArray<ciField*>* fields = NULL;
532   InstanceKlass* k = get_instanceKlass();
533   for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
534     if (fs.access_flags().is_static())  continue;
535     flen += 1;
536   }
537 
538   // allocate the array:
539   if (flen == 0) {
540     return NULL;  // return nothing if none are locally declared
541   }
542   if (super_fields != NULL) {
543     flen += super_fields->length();
544   }
545 
546   fields = new (arena) GrowableArray<ciField*>(arena, flen, 0, NULL);
547   if (super_fields != NULL) {
548     fields->appendAll(super_fields);
549   }
550 
551   for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
552     if (fs.access_flags().is_static())  continue;
553     fieldDescriptor& fd = fs.field_descriptor();
554     if (fd.is_inlined() && flatten) {
555       // Inline type fields are embedded
556       int field_offset = fd.offset();
557       // Get InlineKlass and adjust number of fields
558       Klass* k = get_instanceKlass()->get_inline_type_field_klass(fd.index());
559       ciInlineKlass* vk = CURRENT_ENV->get_klass(k)->as_inline_klass();
560       flen += vk->nof_nonstatic_fields() - 1;
561       // Iterate over fields of the flattened inline type and copy them to 'this'
562       for (int i = 0; i < vk->nof_nonstatic_fields(); ++i) {
563         ciField* flattened_field = vk->nonstatic_field_at(i);
564         // Adjust offset to account for missing oop header
565         int offset = field_offset + (flattened_field->offset() - vk->first_field_offset());
566         // A flattened field can be treated as final if the non-flattened
567         // field is declared final or the holder klass is an inline type itself.
568         bool is_final = fd.is_final() || is_inlinetype();
569         ciField* field = new (arena) ciField(flattened_field, this, offset, is_final);
570         fields->append(field);
571       }
572     } else {
573       ciField* field = new (arena) ciField(&fd);
574       fields->append(field);
575     }
576   }
577   assert(fields->length() == flen, "sanity");
578   // Now sort them by offset, ascending.
579   // (In principle, they could mix with superclass fields.)
580   fields->sort(sort_field_by_offset);
581   return fields;
582 }
583 
584 bool ciInstanceKlass::compute_injected_fields_helper() {
585   ASSERT_IN_VM;
586   InstanceKlass* k = get_instanceKlass();
587 
588   for (InternalFieldStream fs(k); !fs.done(); fs.next()) {
589     if (fs.access_flags().is_static())  continue;
590     return true;
591   }
592   return false;
593 }
594 
595 void ciInstanceKlass::compute_injected_fields() {
596   assert(is_loaded(), "must be loaded");
597 
598   int has_injected_fields = 0;
599   if (super() != NULL && super()->has_injected_fields()) {
600     has_injected_fields = 1;
601   } else {
602     GUARDED_VM_ENTRY({
603         has_injected_fields = compute_injected_fields_helper() ? 1 : 0;
604       });
605   }
606   // may be concurrently initialized for shared ciInstanceKlass objects
607   assert(_has_injected_fields == -1 || _has_injected_fields == has_injected_fields, "broken concurrent initialization");
608   _has_injected_fields = has_injected_fields;
609 }
610 
611 bool ciInstanceKlass::has_object_fields() const {
612   GUARDED_VM_ENTRY(
613       return get_instanceKlass()->nonstatic_oop_map_size() > 0;
614     );
615 }
616 
617 bool ciInstanceKlass::compute_has_trusted_loader() {
618   ASSERT_IN_VM;
619   oop loader_oop = loader();
620   if (loader_oop == NULL) {
621     return true; // bootstrap class loader
622   }
623   return java_lang_ClassLoader::is_trusted_loader(loader_oop);
624 }
625 
626 // ------------------------------------------------------------------
627 // ciInstanceKlass::find_method
628 //
629 // Find a method in this klass.
630 ciMethod* ciInstanceKlass::find_method(ciSymbol* name, ciSymbol* signature) {
631   VM_ENTRY_MARK;
632   InstanceKlass* k = get_instanceKlass();
633   Symbol* name_sym = name->get_symbol();
634   Symbol* sig_sym= signature->get_symbol();
635 
636   Method* m = k->find_method(name_sym, sig_sym);
637   if (m == NULL)  return NULL;
638 
639   return CURRENT_THREAD_ENV->get_method(m);
640 }
641 
642 // ------------------------------------------------------------------
643 // ciInstanceKlass::is_leaf_type
644 bool ciInstanceKlass::is_leaf_type() {
645   assert(is_loaded(), "must be loaded");
646   if (is_shared()) {
647     return is_final();  // approximately correct
648   } else {
649     return !has_subklass() && (nof_implementors() == 0);
650   }
651 }
652 
653 // ------------------------------------------------------------------
654 // ciInstanceKlass::implementor
655 //
656 // Report an implementor of this interface.
657 // Note that there are various races here, since my copy
658 // of _nof_implementors might be out of date with respect
659 // to results returned by InstanceKlass::implementor.
660 // This is OK, since any dependencies we decide to assert
661 // will be checked later under the Compile_lock.
662 ciInstanceKlass* ciInstanceKlass::implementor() {
663   ciInstanceKlass* impl = _implementor;
664   if (impl == NULL) {
665     if (is_shared()) {
666       impl = this; // assume a well-known interface never has a unique implementor
667     } else {
668       // Go into the VM to fetch the implementor.
669       VM_ENTRY_MARK;
670       MutexLocker ml(Compile_lock);
671       Klass* k = get_instanceKlass()->implementor();
672       if (k != NULL) {
673         if (k == get_instanceKlass()) {
674           // More than one implementors. Use 'this' in this case.
675           impl = this;
676         } else {
677           impl = CURRENT_THREAD_ENV->get_instance_klass(k);
678         }
679       }
680     }
681     // Memoize this result.
682     _implementor = impl;
683   }
684   return impl;
685 }
686 
687 bool ciInstanceKlass::can_be_inline_klass(bool is_exact) {
688   if (!EnableValhalla) {
689     return false;
690   }
691   if (!is_loaded() || is_inlinetype()) {
692     // Not loaded or known to be an inline klass
693     return true;
694   }
695   if (!is_exact) {
696     // Not exact, check if this is a valid super for an inline klass
697     VM_ENTRY_MARK;
698     return !get_instanceKlass()->carries_identity_modifier();
699   }
700   return false;
701 }
702 
703 // Utility class for printing of the contents of the static fields for
704 // use by compilation replay.  It only prints out the information that
705 // could be consumed by the compiler, so for primitive types it prints
706 // out the actual value.  For Strings it's the actual string value.
707 // For array types it it's first level array size since that's the
708 // only value which statically unchangeable.  For all other reference
709 // types it simply prints out the dynamic type.
710 
711 class StaticFieldPrinter : public FieldClosure {
712 protected:
713   outputStream* _out;
714 public:
715   StaticFieldPrinter(outputStream* out) :
716     _out(out) {
717   }
718   void do_field_helper(fieldDescriptor* fd, oop obj, bool flattened);
719 };
720 
721 class StaticFinalFieldPrinter : public StaticFieldPrinter {
722   const char*   _holder;
723  public:
724   StaticFinalFieldPrinter(outputStream* out, const char* holder) :
725     StaticFieldPrinter(out), _holder(holder) {
726   }
727   void do_field(fieldDescriptor* fd) {
728     if (fd->is_final() && !fd->has_initial_value()) {
729       ResourceMark rm;
730       InstanceKlass* holder = fd->field_holder();
731       oop mirror = holder->java_mirror();
732       _out->print("staticfield %s %s ", _holder, fd->name()->as_quoted_ascii());
733       BasicType bt = fd->field_type();
734       if (bt != T_OBJECT && bt != T_ARRAY) {
735         _out->print("%s ", fd->signature()->as_quoted_ascii());
736       }
737       do_field_helper(fd, mirror, false);
738       _out->cr();
739     }
740   }
741 };
742 
743 class InlineTypeFieldPrinter : public StaticFieldPrinter {
744   oop _obj;
745 public:
746   InlineTypeFieldPrinter(outputStream* out, oop obj) :
747     StaticFieldPrinter(out), _obj(obj) {
748   }
749   void do_field(fieldDescriptor* fd) {
750     do_field_helper(fd, _obj, true);
751     _out->print(" ");
752   }
753 };
754 
755 void StaticFieldPrinter::do_field_helper(fieldDescriptor* fd, oop mirror, bool flattened) {
756   BasicType bt = fd->field_type();
757   switch (bt) {
758     case T_BYTE:    _out->print("%d", mirror->byte_field(fd->offset()));   break;
759     case T_BOOLEAN: _out->print("%d", mirror->bool_field(fd->offset()));   break;
760     case T_SHORT:   _out->print("%d", mirror->short_field(fd->offset()));  break;
761     case T_CHAR:    _out->print("%d", mirror->char_field(fd->offset()));   break;
762     case T_INT:     _out->print("%d", mirror->int_field(fd->offset()));    break;
763     case T_LONG:    _out->print(INT64_FORMAT, (int64_t)(mirror->long_field(fd->offset())));   break;
764     case T_FLOAT: {
765       float f = mirror->float_field(fd->offset());
766       _out->print("%d", *(int*)&f);
767       break;
768     }
769     case T_DOUBLE: {
770       double d = mirror->double_field(fd->offset());
771       _out->print(INT64_FORMAT, *(int64_t*)&d);
772       break;
773     }
774     case T_ARRAY:  // fall-through
775     case T_OBJECT: {
776       _out->print("%s ", fd->signature()->as_quoted_ascii());
777       oop value =  mirror->obj_field_acquire(fd->offset());
778       if (value == NULL) {
779         _out->print_cr("null");
780       } else if (value->is_instance()) {
781         assert(fd->field_type() == T_OBJECT, "");
782         if (value->is_a(vmClasses::String_klass())) {
783           const char* ascii_value = java_lang_String::as_quoted_ascii(value);
784           _out->print("\"%s\"", (ascii_value != NULL) ? ascii_value : "");
785          } else {
786           const char* klass_name  = value->klass()->name()->as_quoted_ascii();
787           _out->print("%s", klass_name);
788         }
789       } else if (value->is_array()) {
790         typeArrayOop ta = (typeArrayOop)value;
791         _out->print("%d", ta->length());
792         if (value->is_objArray() || value->is_flatArray()) {
793           objArrayOop oa = (objArrayOop)value;
794           const char* klass_name  = value->klass()->name()->as_quoted_ascii();
795           _out->print(" %s", klass_name);
796         }
797       } else {
798         ShouldNotReachHere();
799       }
800       break;
801     }
802     case T_PRIMITIVE_OBJECT: {
803       ResetNoHandleMark rnhm;
804       Thread* THREAD = Thread::current();
805       SignatureStream ss(fd->signature(), false);
806       Symbol* name = ss.as_symbol();
807       assert(!HAS_PENDING_EXCEPTION, "can resolve klass?");
808       InstanceKlass* holder = fd->field_holder();
809       InstanceKlass* k = SystemDictionary::find_instance_klass(THREAD, name,
810                                                                Handle(THREAD, holder->class_loader()),
811                                                                Handle(THREAD, holder->protection_domain()));
812       assert(k != NULL && !HAS_PENDING_EXCEPTION, "can resolve klass?");
813       InlineKlass* vk = InlineKlass::cast(k);
814       oop obj;
815       if (flattened) {
816         int field_offset = fd->offset() - vk->first_field_offset();
817         obj = cast_to_oop(cast_from_oop<address>(mirror) + field_offset);
818       } else {
819         obj = mirror->obj_field_acquire(fd->offset());
820       }
821       InlineTypeFieldPrinter print_field(_out, obj);
822       vk->do_nonstatic_fields(&print_field);
823       break;
824     }
825     default:
826       ShouldNotReachHere();
827   }
828 }
829 
830 const char *ciInstanceKlass::replay_name() const {
831   return CURRENT_ENV->replay_name(get_instanceKlass());
832 }
833 
834 void ciInstanceKlass::dump_replay_instanceKlass(outputStream* out, InstanceKlass* ik) {
835   if (ik->is_hidden()) {
836     const char *name = CURRENT_ENV->dyno_name(ik);
837     if (name != NULL) {
838       out->print_cr("instanceKlass %s # %s", name, ik->name()->as_quoted_ascii());
839     } else {
840       out->print_cr("# instanceKlass %s", ik->name()->as_quoted_ascii());
841     }
842   } else {
843     out->print_cr("instanceKlass %s", ik->name()->as_quoted_ascii());
844   }
845 }
846 
847 GrowableArray<ciInstanceKlass*>* ciInstanceKlass::transitive_interfaces() const{
848   if (_transitive_interfaces == NULL) {
849     const_cast<ciInstanceKlass*>(this)->compute_transitive_interfaces();
850   }
851   return _transitive_interfaces;
852 }
853 
854 void ciInstanceKlass::compute_transitive_interfaces() {
855   GUARDED_VM_ENTRY(
856           InstanceKlass* ik = get_instanceKlass();
857           Array<InstanceKlass*>* interfaces = ik->transitive_interfaces();
858           int orig_length = interfaces->length();
859           Arena* arena = CURRENT_ENV->arena();
860           int transitive_interfaces_len = orig_length + (is_interface() ? 1 : 0);
861           GrowableArray<ciInstanceKlass*>* transitive_interfaces = new(arena)GrowableArray<ciInstanceKlass*>(arena, transitive_interfaces_len,
862                                                                                                              0, NULL);
863           for (int i = 0; i < orig_length; i++) {
864             transitive_interfaces->append(CURRENT_ENV->get_instance_klass(interfaces->at(i)));
865           }
866           if (is_interface()) {
867             transitive_interfaces->append(this);
868           }
869           _transitive_interfaces = transitive_interfaces;
870   );
871 }
872 
873 void ciInstanceKlass::dump_replay_data(outputStream* out) {
874   ResourceMark rm;
875 
876   InstanceKlass* ik = get_instanceKlass();
877   ConstantPool*  cp = ik->constants();
878 
879   // Try to record related loaded classes
880   Klass* sub = ik->subklass();
881   while (sub != NULL) {
882     if (sub->is_instance_klass()) {
883       InstanceKlass *isub = InstanceKlass::cast(sub);
884       dump_replay_instanceKlass(out, isub);
885     }
886     sub = sub->next_sibling();
887   }
888 
889   // Dump out the state of the constant pool tags.  During replay the
890   // tags will be validated for things which shouldn't change and
891   // classes will be resolved if the tags indicate that they were
892   // resolved at compile time.
893   const char *name = replay_name();
894   out->print("ciInstanceKlass %s %d %d %d", name,
895              is_linked(), is_initialized(), cp->length());
896   for (int index = 1; index < cp->length(); index++) {
897     out->print(" %d", cp->tags()->at(index));
898   }
899   out->cr();
900   if (is_initialized()) {
901     //  Dump out the static final fields in case the compilation relies
902     //  on their value for correct replay.
903     StaticFinalFieldPrinter sffp(out, name);
904     ik->do_local_static_fields(&sffp);
905   }
906 }
907 
908 #ifdef ASSERT
909 bool ciInstanceKlass::debug_final_field_at(int offset) {
910   GUARDED_VM_ENTRY(
911     InstanceKlass* ik = get_instanceKlass();
912     fieldDescriptor fd;
913     if (ik->find_field_from_offset(offset, false, &fd)) {
914       return fd.is_final();
915     }
916   );
917   return false;
918 }
919 
920 bool ciInstanceKlass::debug_stable_field_at(int offset) {
921   GUARDED_VM_ENTRY(
922     InstanceKlass* ik = get_instanceKlass();
923     fieldDescriptor fd;
924     if (ik->find_field_from_offset(offset, false, &fd)) {
925       return fd.is_stable();
926     }
927   );
928   return false;
929 }
930 #endif