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