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