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   _is_hidden = ik->is_hidden();
  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()->has_class_mirror_holder()) {
  78     // Though ciInstanceKlass records class loader oop, it's not enough to keep
  79     // non-strong hidden classes and VM 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 hidden 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) != JVM_SIGNATURE_ARRAY, "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_hidden = 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   assert(self->is_loaded(), "must be loaded to access field info");
 219   ciField* field = self->get_field_by_offset(offset, false);
 220   if (field != NULL) {
 221     return field->holder();
 222   } else {
 223     for (;;) {
 224       assert(self->is_loaded(), "must be loaded to have size");
 225       ciInstanceKlass* super = self->super();
 226       if (super == NULL || super->nof_nonstatic_fields() == 0) {
 227         return self;
 228       } else {
 229         self = super;  // return super->get_canonical_holder(offset)
 230       }
 231     }
 232   }
 233 }
 234 
 235 // ------------------------------------------------------------------
 236 // ciInstanceKlass::is_java_lang_Object
 237 //
 238 // Is this klass java.lang.Object?
 239 bool ciInstanceKlass::is_java_lang_Object() const {
 240   return equals(CURRENT_ENV->Object_klass());
 241 }
 242 
 243 // ------------------------------------------------------------------
 244 // ciInstanceKlass::uses_default_loader
 245 bool ciInstanceKlass::uses_default_loader() const {
 246   // Note:  We do not need to resolve the handle or enter the VM
 247   // in order to test null-ness.
 248   return _loader == NULL;
 249 }
 250 
 251 // ------------------------------------------------------------------
 252 
 253 /**
 254  * Return basic type of boxed value for box klass or T_OBJECT if not.
 255  */
 256 BasicType ciInstanceKlass::box_klass_type() const {
 257   if (uses_default_loader() && is_loaded()) {
 258     return SystemDictionary::box_klass_type(get_Klass());
 259   } else {
 260     return T_OBJECT;
 261   }
 262 }
 263 
 264 /**
 265  * Is this boxing klass?
 266  */
 267 bool ciInstanceKlass::is_box_klass() const {
 268   return is_java_primitive(box_klass_type());
 269 }
 270 
 271 /**
 272  *  Is this boxed value offset?
 273  */
 274 bool ciInstanceKlass::is_boxed_value_offset(int offset) const {
 275   BasicType bt = box_klass_type();
 276   return is_java_primitive(bt) &&
 277          (offset == java_lang_boxing_object::value_offset_in_bytes(bt));
 278 }
 279 
 280 // ------------------------------------------------------------------
 281 // ciInstanceKlass::is_in_package
 282 //
 283 // Is this klass in the given package?
 284 bool ciInstanceKlass::is_in_package(const char* packagename, int len) {
 285   // To avoid class loader mischief, this test always rejects application classes.
 286   if (!uses_default_loader())
 287     return false;
 288   GUARDED_VM_ENTRY(
 289     return is_in_package_impl(packagename, len);
 290   )
 291 }
 292 
 293 bool ciInstanceKlass::is_in_package_impl(const char* packagename, int len) {
 294   ASSERT_IN_VM;
 295 
 296   // If packagename contains trailing '/' exclude it from the
 297   // prefix-test since we test for it explicitly.
 298   if (packagename[len - 1] == '/')
 299     len--;
 300 
 301   if (!name()->starts_with(packagename, len))
 302     return false;
 303 
 304   // Test if the class name is something like "java/lang".
 305   if ((len + 1) > name()->utf8_length())
 306     return false;
 307 
 308   // Test for trailing '/'
 309   if (name()->char_at(len) != '/')
 310     return false;
 311 
 312   // Make sure it's not actually in a subpackage:
 313   if (name()->index_of_at(len+1, "/", 1) >= 0)
 314     return false;
 315 
 316   return true;
 317 }
 318 
 319 // ------------------------------------------------------------------
 320 // ciInstanceKlass::print_impl
 321 //
 322 // Implementation of the print method.
 323 void ciInstanceKlass::print_impl(outputStream* st) {
 324   ciKlass::print_impl(st);
 325   GUARDED_VM_ENTRY(st->print(" loader=" INTPTR_FORMAT, p2i(loader()));)
 326   if (is_loaded()) {
 327     st->print(" loaded=true initialized=%s finalized=%s subklass=%s size=%d flags=",
 328               bool_to_str(is_initialized()),
 329               bool_to_str(has_finalizer()),
 330               bool_to_str(has_subklass()),
 331               layout_helper());
 332 
 333     _flags.print_klass_flags();
 334 
 335     if (_super) {
 336       st->print(" super=");
 337       _super->print_name();
 338     }
 339     if (_java_mirror) {
 340       st->print(" mirror=PRESENT");
 341     }
 342   } else {
 343     st->print(" loaded=false");
 344   }
 345 }
 346 
 347 // ------------------------------------------------------------------
 348 // ciInstanceKlass::super
 349 //
 350 // Get the superklass of this klass.
 351 ciInstanceKlass* ciInstanceKlass::super() {
 352   assert(is_loaded(), "must be loaded");
 353   if (_super == NULL && !is_java_lang_Object()) {
 354     GUARDED_VM_ENTRY(
 355       Klass* super_klass = get_instanceKlass()->super();
 356       _super = CURRENT_ENV->get_instance_klass(super_klass);
 357     )
 358   }
 359   return _super;
 360 }
 361 
 362 // ------------------------------------------------------------------
 363 // ciInstanceKlass::java_mirror
 364 //
 365 // Get the instance of java.lang.Class corresponding to this klass.
 366 // Cache it on this->_java_mirror.
 367 ciInstance* ciInstanceKlass::java_mirror() {
 368   if (is_shared()) {
 369     return ciKlass::java_mirror();
 370   }
 371   if (_java_mirror == NULL) {
 372     _java_mirror = ciKlass::java_mirror();
 373   }
 374   return _java_mirror;
 375 }
 376 
 377 // ------------------------------------------------------------------
 378 // ciInstanceKlass::unique_concrete_subklass
 379 ciInstanceKlass* ciInstanceKlass::unique_concrete_subklass() {
 380   if (!is_loaded())     return NULL; // No change if class is not loaded
 381   if (!is_abstract())   return NULL; // Only applies to abstract classes.
 382   if (!has_subklass())  return NULL; // Must have at least one subklass.
 383   VM_ENTRY_MARK;
 384   InstanceKlass* ik = get_instanceKlass();
 385   Klass* up = ik->up_cast_abstract();
 386   assert(up->is_instance_klass(), "must be InstanceKlass");
 387   if (ik == up) {
 388     return NULL;
 389   }
 390   return CURRENT_THREAD_ENV->get_instance_klass(up);
 391 }
 392 
 393 // ------------------------------------------------------------------
 394 // ciInstanceKlass::has_finalizable_subclass
 395 bool ciInstanceKlass::has_finalizable_subclass() {
 396   if (!is_loaded())     return true;
 397   VM_ENTRY_MARK;
 398   return Dependencies::find_finalizable_subclass(get_instanceKlass()) != NULL;
 399 }
 400 
 401 // ------------------------------------------------------------------
 402 // ciInstanceKlass::contains_field_offset
 403 bool ciInstanceKlass::contains_field_offset(int offset) {
 404   VM_ENTRY_MARK;
 405   return get_instanceKlass()->contains_field_offset(offset);
 406 }
 407 
 408 // ------------------------------------------------------------------
 409 // ciInstanceKlass::get_field_by_offset
 410 ciField* ciInstanceKlass::get_field_by_offset(int field_offset, bool is_static) {
 411   if (!is_static) {
 412     for (int i = 0, len = nof_nonstatic_fields(); i < len; i++) {
 413       ciField* field = _nonstatic_fields->at(i);
 414       int  field_off = field->offset_in_bytes();
 415       if (field_off == field_offset)
 416         return field;
 417       if (field_off > field_offset)
 418         break;
 419       // could do binary search or check bins, but probably not worth it
 420     }
 421     return NULL;
 422   }
 423   VM_ENTRY_MARK;
 424   InstanceKlass* k = get_instanceKlass();
 425   fieldDescriptor fd;
 426   if (!k->find_field_from_offset(field_offset, is_static, &fd)) {
 427     return NULL;
 428   }
 429   ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd);
 430   return field;
 431 }
 432 
 433 // ------------------------------------------------------------------
 434 // ciInstanceKlass::get_field_by_name
 435 ciField* ciInstanceKlass::get_field_by_name(ciSymbol* name, ciSymbol* signature, bool is_static) {
 436   VM_ENTRY_MARK;
 437   InstanceKlass* k = get_instanceKlass();
 438   fieldDescriptor fd;
 439   Klass* def = k->find_field(name->get_symbol(), signature->get_symbol(), is_static, &fd);
 440   if (def == NULL) {
 441     return NULL;
 442   }
 443   ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd);
 444   return field;
 445 }
 446 
 447 
 448 static int sort_field_by_offset(ciField** a, ciField** b) {
 449   return (*a)->offset_in_bytes() - (*b)->offset_in_bytes();
 450   // (no worries about 32-bit overflow...)
 451 }
 452 
 453 // ------------------------------------------------------------------
 454 // ciInstanceKlass::compute_nonstatic_fields
 455 int ciInstanceKlass::compute_nonstatic_fields() {
 456   assert(is_loaded(), "must be loaded");
 457 
 458   if (_nonstatic_fields != NULL)
 459     return _nonstatic_fields->length();
 460 
 461   if (!has_nonstatic_fields()) {
 462     Arena* arena = CURRENT_ENV->arena();
 463     _nonstatic_fields = new (arena) GrowableArray<ciField*>(arena, 0, 0, NULL);
 464     return 0;
 465   }
 466   assert(!is_java_lang_Object(), "bootstrap OK");
 467 
 468   // Size in bytes of my fields, including inherited fields.
 469   int fsize = nonstatic_field_size() * heapOopSize;
 470 
 471   ciInstanceKlass* super = this->super();
 472   GrowableArray<ciField*>* super_fields = NULL;
 473   if (super != NULL && super->has_nonstatic_fields()) {
 474     int super_flen   = super->nof_nonstatic_fields();
 475     super_fields = super->_nonstatic_fields;
 476     assert(super_flen == 0 || super_fields != NULL, "first get nof_fields");
 477   }
 478 
 479   GrowableArray<ciField*>* fields = NULL;
 480   GUARDED_VM_ENTRY({
 481       fields = compute_nonstatic_fields_impl(super_fields);
 482     });
 483 
 484   if (fields == NULL) {
 485     // This can happen if this class (java.lang.Class) has invisible fields.
 486     if (super_fields != NULL) {
 487       _nonstatic_fields = super_fields;
 488       return super_fields->length();
 489     } else {
 490       return 0;
 491     }
 492   }
 493 
 494   int flen = fields->length();
 495 
 496   // Now sort them by offset, ascending.
 497   // (In principle, they could mix with superclass fields.)
 498   fields->sort(sort_field_by_offset);
 499   _nonstatic_fields = fields;
 500   return flen;
 501 }
 502 
 503 GrowableArray<ciField*>*
 504 ciInstanceKlass::compute_nonstatic_fields_impl(GrowableArray<ciField*>*
 505                                                super_fields) {
 506   ASSERT_IN_VM;
 507   Arena* arena = CURRENT_ENV->arena();
 508   int flen = 0;
 509   GrowableArray<ciField*>* fields = NULL;
 510   InstanceKlass* k = get_instanceKlass();
 511   for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
 512     if (fs.access_flags().is_static())  continue;
 513     flen += 1;
 514   }
 515 
 516   // allocate the array:
 517   if (flen == 0) {
 518     return NULL;  // return nothing if none are locally declared
 519   }
 520   if (super_fields != NULL) {
 521     flen += super_fields->length();
 522   }
 523   fields = new (arena) GrowableArray<ciField*>(arena, flen, 0, NULL);
 524   if (super_fields != NULL) {
 525     fields->appendAll(super_fields);
 526   }
 527 
 528   for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
 529     if (fs.access_flags().is_static())  continue;
 530     fieldDescriptor& fd = fs.field_descriptor();
 531     ciField* field = new (arena) ciField(&fd);
 532     fields->append(field);
 533   }
 534   assert(fields->length() == flen, "sanity");
 535   return fields;
 536 }
 537 
 538 bool ciInstanceKlass::compute_injected_fields_helper() {
 539   ASSERT_IN_VM;
 540   InstanceKlass* k = get_instanceKlass();
 541 
 542   for (InternalFieldStream fs(k); !fs.done(); fs.next()) {
 543     if (fs.access_flags().is_static())  continue;
 544     return true;
 545   }
 546   return false;
 547 }
 548 
 549 void ciInstanceKlass::compute_injected_fields() {
 550   assert(is_loaded(), "must be loaded");
 551 
 552   int has_injected_fields = 0;
 553   if (super() != NULL && super()->has_injected_fields()) {
 554     has_injected_fields = 1;
 555   } else {
 556     GUARDED_VM_ENTRY({
 557         has_injected_fields = compute_injected_fields_helper() ? 1 : 0;
 558       });
 559   }
 560   // may be concurrently initialized for shared ciInstanceKlass objects
 561   assert(_has_injected_fields == -1 || _has_injected_fields == has_injected_fields, "broken concurrent initialization");
 562   _has_injected_fields = has_injected_fields;
 563 }
 564 
 565 bool ciInstanceKlass::has_object_fields() const {
 566   GUARDED_VM_ENTRY(
 567       return get_instanceKlass()->nonstatic_oop_map_size() > 0;
 568     );
 569 }
 570 
 571 // ------------------------------------------------------------------
 572 // ciInstanceKlass::find_method
 573 //
 574 // Find a method in this klass.
 575 ciMethod* ciInstanceKlass::find_method(ciSymbol* name, ciSymbol* signature) {
 576   VM_ENTRY_MARK;
 577   InstanceKlass* k = get_instanceKlass();
 578   Symbol* name_sym = name->get_symbol();
 579   Symbol* sig_sym= signature->get_symbol();
 580 
 581   Method* m = k->find_method(name_sym, sig_sym);
 582   if (m == NULL)  return NULL;
 583 
 584   return CURRENT_THREAD_ENV->get_method(m);
 585 }
 586 
 587 // ------------------------------------------------------------------
 588 // ciInstanceKlass::is_leaf_type
 589 bool ciInstanceKlass::is_leaf_type() {
 590   assert(is_loaded(), "must be loaded");
 591   if (is_shared()) {
 592     return is_final();  // approximately correct
 593   } else {
 594     return !has_subklass() && (nof_implementors() == 0);
 595   }
 596 }
 597 
 598 // ------------------------------------------------------------------
 599 // ciInstanceKlass::implementor
 600 //
 601 // Report an implementor of this interface.
 602 // Note that there are various races here, since my copy
 603 // of _nof_implementors might be out of date with respect
 604 // to results returned by InstanceKlass::implementor.
 605 // This is OK, since any dependencies we decide to assert
 606 // will be checked later under the Compile_lock.
 607 ciInstanceKlass* ciInstanceKlass::implementor() {
 608   ciInstanceKlass* impl = _implementor;
 609   if (impl == NULL) {
 610     // Go into the VM to fetch the implementor.
 611     {
 612       VM_ENTRY_MARK;
 613       MutexLocker ml(Compile_lock);
 614       Klass* k = get_instanceKlass()->implementor();
 615       if (k != NULL) {
 616         if (k == get_instanceKlass()) {
 617           // More than one implementors. Use 'this' in this case.
 618           impl = this;
 619         } else {
 620           impl = CURRENT_THREAD_ENV->get_instance_klass(k);
 621         }
 622       }
 623     }
 624     // Memoize this result.
 625     if (!is_shared()) {
 626       _implementor = impl;
 627     }
 628   }
 629   return impl;
 630 }
 631 
 632 ciInstanceKlass* ciInstanceKlass::unsafe_anonymous_host() {
 633   assert(is_loaded(), "must be loaded");
 634   if (is_unsafe_anonymous()) {
 635     VM_ENTRY_MARK
 636     Klass* unsafe_anonymous_host = get_instanceKlass()->unsafe_anonymous_host();
 637     return CURRENT_ENV->get_instance_klass(unsafe_anonymous_host);
 638   }
 639   return NULL;
 640 }
 641 
 642 // Utility class for printing of the contents of the static fields for
 643 // use by compilation replay.  It only prints out the information that
 644 // could be consumed by the compiler, so for primitive types it prints
 645 // out the actual value.  For Strings it's the actual string value.
 646 // For array types it it's first level array size since that's the
 647 // only value which statically unchangeable.  For all other reference
 648 // types it simply prints out the dynamic type.
 649 
 650 class StaticFinalFieldPrinter : public FieldClosure {
 651   outputStream* _out;
 652   const char*   _holder;
 653  public:
 654   StaticFinalFieldPrinter(outputStream* out, const char* holder) :
 655     _out(out),
 656     _holder(holder) {
 657   }
 658   void do_field(fieldDescriptor* fd) {
 659     if (fd->is_final() && !fd->has_initial_value()) {
 660       ResourceMark rm;
 661       oop mirror = fd->field_holder()->java_mirror();
 662       _out->print("staticfield %s %s %s ", _holder, fd->name()->as_quoted_ascii(), fd->signature()->as_quoted_ascii());
 663       switch (fd->field_type()) {
 664         case T_BYTE:    _out->print_cr("%d", mirror->byte_field(fd->offset()));   break;
 665         case T_BOOLEAN: _out->print_cr("%d", mirror->bool_field(fd->offset()));   break;
 666         case T_SHORT:   _out->print_cr("%d", mirror->short_field(fd->offset()));  break;
 667         case T_CHAR:    _out->print_cr("%d", mirror->char_field(fd->offset()));   break;
 668         case T_INT:     _out->print_cr("%d", mirror->int_field(fd->offset()));    break;
 669         case T_LONG:    _out->print_cr(INT64_FORMAT, (int64_t)(mirror->long_field(fd->offset())));   break;
 670         case T_FLOAT: {
 671           float f = mirror->float_field(fd->offset());
 672           _out->print_cr("%d", *(int*)&f);
 673           break;
 674         }
 675         case T_DOUBLE: {
 676           double d = mirror->double_field(fd->offset());
 677           _out->print_cr(INT64_FORMAT, *(int64_t*)&d);
 678           break;
 679         }
 680         case T_ARRAY:  // fall-through
 681         case T_OBJECT: {
 682           oop value =  mirror->obj_field_acquire(fd->offset());
 683           if (value == NULL) {
 684             _out->print_cr("null");
 685           } else if (value->is_instance()) {
 686             assert(fd->field_type() == T_OBJECT, "");
 687             if (value->is_a(SystemDictionary::String_klass())) {
 688               const char* ascii_value = java_lang_String::as_quoted_ascii(value);
 689               _out->print("\"%s\"", (ascii_value != NULL) ? ascii_value : "");
 690             } else {
 691               const char* klass_name  = value->klass()->name()->as_quoted_ascii();
 692               _out->print_cr("%s", klass_name);
 693             }
 694           } else if (value->is_array()) {
 695             typeArrayOop ta = (typeArrayOop)value;
 696             _out->print("%d", ta->length());
 697             if (value->is_objArray()) {
 698               objArrayOop oa = (objArrayOop)value;
 699               const char* klass_name  = value->klass()->name()->as_quoted_ascii();
 700               _out->print(" %s", klass_name);
 701             }
 702             _out->cr();
 703           } else {
 704             ShouldNotReachHere();
 705           }
 706           break;
 707         }
 708         default:
 709           ShouldNotReachHere();
 710         }
 711     }
 712   }
 713 };
 714 
 715 
 716 void ciInstanceKlass::dump_replay_data(outputStream* out) {
 717   ResourceMark rm;
 718 
 719   InstanceKlass* ik = get_instanceKlass();
 720   ConstantPool*  cp = ik->constants();
 721 
 722   // Try to record related loaded classes
 723   Klass* sub = ik->subklass();
 724   while (sub != NULL) {
 725     if (sub->is_instance_klass()) {
 726       out->print_cr("instanceKlass %s", sub->name()->as_quoted_ascii());
 727     }
 728     sub = sub->next_sibling();
 729   }
 730 
 731   // Dump out the state of the constant pool tags.  During replay the
 732   // tags will be validated for things which shouldn't change and
 733   // classes will be resolved if the tags indicate that they were
 734   // resolved at compile time.
 735   out->print("ciInstanceKlass %s %d %d %d", ik->name()->as_quoted_ascii(),
 736              is_linked(), is_initialized(), cp->length());
 737   for (int index = 1; index < cp->length(); index++) {
 738     out->print(" %d", cp->tags()->at(index));
 739   }
 740   out->cr();
 741   if (is_initialized()) {
 742     //  Dump out the static final fields in case the compilation relies
 743     //  on their value for correct replay.
 744     StaticFinalFieldPrinter sffp(out, ik->name()->as_quoted_ascii());
 745     ik->do_local_static_fields(&sffp);
 746   }
 747 }
 748 
 749 #ifdef ASSERT
 750 bool ciInstanceKlass::debug_final_field_at(int offset) {
 751   GUARDED_VM_ENTRY(
 752     InstanceKlass* ik = get_instanceKlass();
 753     fieldDescriptor fd;
 754     if (ik->find_field_from_offset(offset, false, &fd)) {
 755       return fd.is_final();
 756     }
 757   );
 758   return false;
 759 }
 760 
 761 bool ciInstanceKlass::debug_stable_field_at(int offset) {
 762   GUARDED_VM_ENTRY(
 763     InstanceKlass* ik = get_instanceKlass();
 764     fieldDescriptor fd;
 765     if (ik->find_field_from_offset(offset, false, &fd)) {
 766       return fd.is_stable();
 767     }
 768   );
 769   return false;
 770 }
 771 #endif