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