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