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