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 = ik->subklass() != NULL;
  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   _has_object_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->holder_phantom();
  77   if (ik->is_unsafe_anonymous()) {
  78     // Though ciInstanceKlass records class loader oop, it's not enough to keep
  79     // 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 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   _has_object_fields = -1;
 127   _is_unsafe_anonymous = 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;
 153     return _has_subklass;
 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 void ciInstanceKlass::compute_object_fields() {
 560   for (int i = 0; i < nof_nonstatic_fields(); i++) {
 561     ciField* f = nonstatic_field_at(i);
 562     if (f->layout_type() == T_OBJECT) {
 563       assert(_has_object_fields == -1 || _has_object_fields == 1, "broken concurrent initialization");
 564       _has_object_fields = 1;
 565       return;
 566     }
 567   }
 568   assert(_has_object_fields == -1 || _has_object_fields == 0, "broken concurrent initialization");
 569   _has_object_fields = 0;
 570 }
 571 
 572 // ------------------------------------------------------------------
 573 // ciInstanceKlass::find_method
 574 //
 575 // Find a method in this klass.
 576 ciMethod* ciInstanceKlass::find_method(ciSymbol* name, ciSymbol* signature) {
 577   VM_ENTRY_MARK;
 578   InstanceKlass* k = get_instanceKlass();
 579   Symbol* name_sym = name->get_symbol();
 580   Symbol* sig_sym= signature->get_symbol();
 581 
 582   Method* m = k->find_method(name_sym, sig_sym);
 583   if (m == NULL)  return NULL;
 584 
 585   return CURRENT_THREAD_ENV->get_method(m);
 586 }
 587 
 588 // ------------------------------------------------------------------
 589 // ciInstanceKlass::is_leaf_type
 590 bool ciInstanceKlass::is_leaf_type() {
 591   assert(is_loaded(), "must be loaded");
 592   if (is_shared()) {
 593     return is_final();  // approximately correct
 594   } else {
 595     return !_has_subklass && (nof_implementors() == 0);
 596   }
 597 }
 598 
 599 // ------------------------------------------------------------------
 600 // ciInstanceKlass::implementor
 601 //
 602 // Report an implementor of this interface.
 603 // Note that there are various races here, since my copy
 604 // of _nof_implementors might be out of date with respect
 605 // to results returned by InstanceKlass::implementor.
 606 // This is OK, since any dependencies we decide to assert
 607 // will be checked later under the Compile_lock.
 608 ciInstanceKlass* ciInstanceKlass::implementor() {
 609   ciInstanceKlass* impl = _implementor;
 610   if (impl == NULL) {
 611     // Go into the VM to fetch the implementor.
 612     {
 613       VM_ENTRY_MARK;
 614       MutexLocker ml(Compile_lock);
 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::unsafe_anonymous_host() {
 634   assert(is_loaded(), "must be loaded");
 635   if (is_unsafe_anonymous()) {
 636     VM_ENTRY_MARK
 637     Klass* unsafe_anonymous_host = get_instanceKlass()->unsafe_anonymous_host();
 638     return CURRENT_ENV->get_instance_klass(unsafe_anonymous_host);
 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 }