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