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