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