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