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