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