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