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