< prev index next >

src/hotspot/share/ci/ciInstanceKlass.cpp

Print this page




  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.inline.hpp"
  30 #include "classfile/systemDictionary.hpp"
  31 #include "memory/allocation.hpp"
  32 #include "memory/allocation.inline.hpp"
  33 #include "memory/resourceArea.hpp"
  34 #include "oops/oop.inline.hpp"
  35 #include "oops/fieldStreams.hpp"
  36 #include "runtime/fieldDescriptor.hpp"
  37 #include "runtime/handles.inline.hpp"
  38 #include "runtime/jniHandles.inline.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   _implementor = NULL; // we will fill these lazily
  69 
  70   // Ensure that the metadata wrapped by the ciMetadata is kept alive by GC.
  71   // This is primarily useful for metadata which is considered as weak roots
  72   // by the GC but need to be strong roots if reachable from a current compilation.
  73   // InstanceKlass are created for both weak and strong metadata.  Ensuring this metadata
  74   // alive covers the cases where there are weak roots without performance cost.
  75   oop holder = ik->holder_phantom();
  76   if (ik->is_anonymous()) {
  77     // Though ciInstanceKlass records class loader oop, it's not enough to keep
  78     // VM anonymous classes alive (loader == NULL). Klass holder should be used instead.
  79     // It is enough to record a ciObject, since cached elements are never removed
  80     // during ciObjectFactory lifetime. ciObjectFactory itself is created for
  81     // every compilation and lives for the whole duration of the compilation.
  82     assert(holder != NULL, "holder of anonymous class is the mirror which is never null");
  83     (void)CURRENT_ENV->get_object(holder);
  84   }
  85 
  86   Thread *thread = Thread::current();
  87   if (ciObjectFactory::is_initialized()) {


 105     if (k != SystemDictionary::Object_klass()) {
 106       super();
 107     }
 108     //compute_nonstatic_fields();  // done outside of constructor
 109   }
 110 
 111   _field_cache = NULL;
 112 }
 113 
 114 // Version for unloaded classes:
 115 ciInstanceKlass::ciInstanceKlass(ciSymbol* name,
 116                                  jobject loader, jobject protection_domain)
 117   : ciKlass(name, T_OBJECT)
 118 {
 119   assert(name->byte_at(0) != '[', "not an instance klass");
 120   _init_state = (InstanceKlass::ClassState)0;
 121   _nonstatic_field_size = -1;
 122   _has_nonstatic_fields = false;
 123   _nonstatic_fields = NULL;
 124   _has_injected_fields = -1;

 125   _is_anonymous = false;
 126   _loader = loader;
 127   _protection_domain = protection_domain;
 128   _is_shared = false;
 129   _super = NULL;
 130   _java_mirror = NULL;
 131   _field_cache = NULL;
 132 }
 133 
 134 
 135 
 136 // ------------------------------------------------------------------
 137 // ciInstanceKlass::compute_shared_is_initialized
 138 void ciInstanceKlass::compute_shared_init_state() {
 139   GUARDED_VM_ENTRY(
 140     InstanceKlass* ik = get_instanceKlass();
 141     _init_state = ik->init_state();
 142   )
 143 }
 144 


 182 // ciInstanceKlass::field_cache
 183 //
 184 // Get the field cache associated with this klass.
 185 ciConstantPoolCache* ciInstanceKlass::field_cache() {
 186   if (is_shared()) {
 187     return NULL;
 188   }
 189   if (_field_cache == NULL) {
 190     assert(!is_java_lang_Object(), "Object has no fields");
 191     Arena* arena = CURRENT_ENV->arena();
 192     _field_cache = new (arena) ciConstantPoolCache(arena, 5);
 193   }
 194   return _field_cache;
 195 }
 196 
 197 // ------------------------------------------------------------------
 198 // ciInstanceKlass::get_canonical_holder
 199 //
 200 ciInstanceKlass* ciInstanceKlass::get_canonical_holder(int offset) {
 201   #ifdef ASSERT
 202   if (!(offset >= 0 && offset < layout_helper())) {
 203     tty->print("*** get_canonical_holder(%d) on ", offset);
 204     this->print();
 205     tty->print_cr(" ***");
 206   };
 207   assert(offset >= 0 && offset < layout_helper(), "offset must be tame");
 208   #endif
 209 
 210   if (offset < instanceOopDesc::base_offset_in_bytes()) {
 211     // All header offsets belong properly to java/lang/Object.
 212     return CURRENT_ENV->Object_klass();
 213   }
 214 
 215   ciInstanceKlass* self = this;
 216   for (;;) {
 217     assert(self->is_loaded(), "must be loaded to have size");
 218     ciInstanceKlass* super = self->super();
 219     if (super == NULL || super->nof_nonstatic_fields() == 0 ||
 220         !super->contains_field_offset(offset)) {
 221       return self;
 222     } else {
 223       self = super;  // return super->get_canonical_holder(offset)
 224     }
 225   }
 226 }
 227 


 537   }
 538   return false;
 539 }
 540 
 541 void ciInstanceKlass::compute_injected_fields() {
 542   assert(is_loaded(), "must be loaded");
 543 
 544   int has_injected_fields = 0;
 545   if (super() != NULL && super()->has_injected_fields()) {
 546     has_injected_fields = 1;
 547   } else {
 548     GUARDED_VM_ENTRY({
 549         has_injected_fields = compute_injected_fields_helper() ? 1 : 0;
 550       });
 551   }
 552   // may be concurrently initialized for shared ciInstanceKlass objects
 553   assert(_has_injected_fields == -1 || _has_injected_fields == has_injected_fields, "broken concurrent initialization");
 554   _has_injected_fields = has_injected_fields;
 555 }
 556 













 557 // ------------------------------------------------------------------
 558 // ciInstanceKlass::find_method
 559 //
 560 // Find a method in this klass.
 561 ciMethod* ciInstanceKlass::find_method(ciSymbol* name, ciSymbol* signature) {
 562   VM_ENTRY_MARK;
 563   InstanceKlass* k = get_instanceKlass();
 564   Symbol* name_sym = name->get_symbol();
 565   Symbol* sig_sym= signature->get_symbol();
 566 
 567   Method* m = k->find_method(name_sym, sig_sym);
 568   if (m == NULL)  return NULL;
 569 
 570   return CURRENT_THREAD_ENV->get_method(m);
 571 }
 572 
 573 // ------------------------------------------------------------------
 574 // ciInstanceKlass::is_leaf_type
 575 bool ciInstanceKlass::is_leaf_type() {
 576   assert(is_loaded(), "must be loaded");


 719     sub = sub->next_sibling();
 720   }
 721 
 722   // Dump out the state of the constant pool tags.  During replay the
 723   // tags will be validated for things which shouldn't change and
 724   // classes will be resolved if the tags indicate that they were
 725   // resolved at compile time.
 726   out->print("ciInstanceKlass %s %d %d %d", ik->name()->as_quoted_ascii(),
 727              is_linked(), is_initialized(), cp->length());
 728   for (int index = 1; index < cp->length(); index++) {
 729     out->print(" %d", cp->tags()->at(index));
 730   }
 731   out->cr();
 732   if (is_initialized()) {
 733     //  Dump out the static final fields in case the compilation relies
 734     //  on their value for correct replay.
 735     StaticFinalFieldPrinter sffp(out, ik->name()->as_quoted_ascii());
 736     ik->do_local_static_fields(&sffp);
 737   }
 738 }


























  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.inline.hpp"
  30 #include "classfile/systemDictionary.hpp"
  31 #include "memory/allocation.hpp"
  32 #include "memory/allocation.inline.hpp"
  33 #include "memory/resourceArea.hpp"
  34 #include "oops/oop.inline.hpp"
  35 #include "oops/fieldStreams.hpp"
  36 #include "runtime/fieldDescriptor.hpp"
  37 #include "runtime/handles.inline.hpp"
  38 #include "runtime/jniHandles.inline.hpp"
  39 #include "utilities/macros.hpp"
  40 #if INCLUDE_SHENANDOAHGC
  41 #include "gc/shenandoah/shenandoahBrooksPointer.hpp"
  42 #endif
  43 
  44 // ciInstanceKlass
  45 //
  46 // This class represents a Klass* in the HotSpot virtual machine
  47 // whose Klass part in an InstanceKlass.
  48 
  49 
  50 // ------------------------------------------------------------------
  51 // ciInstanceKlass::ciInstanceKlass
  52 //
  53 // Loaded instance klass.
  54 ciInstanceKlass::ciInstanceKlass(Klass* k) :
  55   ciKlass(k)
  56 {
  57   assert(get_Klass()->is_instance_klass(), "wrong type");
  58   assert(get_instanceKlass()->is_loaded(), "must be at least loaded");
  59   InstanceKlass* ik = get_instanceKlass();
  60 
  61   AccessFlags access_flags = ik->access_flags();
  62   _flags = ciFlags(access_flags);
  63   _has_finalizer = access_flags.has_finalizer();
  64   _has_subklass = ik->subklass() != NULL;
  65   _init_state = ik->init_state();
  66   _nonstatic_field_size = ik->nonstatic_field_size();
  67   _has_nonstatic_fields = ik->has_nonstatic_fields();
  68   _has_nonstatic_concrete_methods = ik->has_nonstatic_concrete_methods();
  69   _is_anonymous = ik->is_anonymous();
  70   _nonstatic_fields = NULL; // initialized lazily by compute_nonstatic_fields:
  71   _has_injected_fields = -1;
  72   _has_object_fields = -1;
  73   _implementor = NULL; // we will fill these lazily
  74 
  75   // Ensure that the metadata wrapped by the ciMetadata is kept alive by GC.
  76   // This is primarily useful for metadata which is considered as weak roots
  77   // by the GC but need to be strong roots if reachable from a current compilation.
  78   // InstanceKlass are created for both weak and strong metadata.  Ensuring this metadata
  79   // alive covers the cases where there are weak roots without performance cost.
  80   oop holder = ik->holder_phantom();
  81   if (ik->is_anonymous()) {
  82     // Though ciInstanceKlass records class loader oop, it's not enough to keep
  83     // VM anonymous classes alive (loader == NULL). Klass holder should be used instead.
  84     // It is enough to record a ciObject, since cached elements are never removed
  85     // during ciObjectFactory lifetime. ciObjectFactory itself is created for
  86     // every compilation and lives for the whole duration of the compilation.
  87     assert(holder != NULL, "holder of anonymous class is the mirror which is never null");
  88     (void)CURRENT_ENV->get_object(holder);
  89   }
  90 
  91   Thread *thread = Thread::current();
  92   if (ciObjectFactory::is_initialized()) {


 110     if (k != SystemDictionary::Object_klass()) {
 111       super();
 112     }
 113     //compute_nonstatic_fields();  // done outside of constructor
 114   }
 115 
 116   _field_cache = NULL;
 117 }
 118 
 119 // Version for unloaded classes:
 120 ciInstanceKlass::ciInstanceKlass(ciSymbol* name,
 121                                  jobject loader, jobject protection_domain)
 122   : ciKlass(name, T_OBJECT)
 123 {
 124   assert(name->byte_at(0) != '[', "not an instance klass");
 125   _init_state = (InstanceKlass::ClassState)0;
 126   _nonstatic_field_size = -1;
 127   _has_nonstatic_fields = false;
 128   _nonstatic_fields = NULL;
 129   _has_injected_fields = -1;
 130   _has_object_fields = -1;
 131   _is_anonymous = false;
 132   _loader = loader;
 133   _protection_domain = protection_domain;
 134   _is_shared = false;
 135   _super = NULL;
 136   _java_mirror = NULL;
 137   _field_cache = NULL;
 138 }
 139 
 140 
 141 
 142 // ------------------------------------------------------------------
 143 // ciInstanceKlass::compute_shared_is_initialized
 144 void ciInstanceKlass::compute_shared_init_state() {
 145   GUARDED_VM_ENTRY(
 146     InstanceKlass* ik = get_instanceKlass();
 147     _init_state = ik->init_state();
 148   )
 149 }
 150 


 188 // ciInstanceKlass::field_cache
 189 //
 190 // Get the field cache associated with this klass.
 191 ciConstantPoolCache* ciInstanceKlass::field_cache() {
 192   if (is_shared()) {
 193     return NULL;
 194   }
 195   if (_field_cache == NULL) {
 196     assert(!is_java_lang_Object(), "Object has no fields");
 197     Arena* arena = CURRENT_ENV->arena();
 198     _field_cache = new (arena) ciConstantPoolCache(arena, 5);
 199   }
 200   return _field_cache;
 201 }
 202 
 203 // ------------------------------------------------------------------
 204 // ciInstanceKlass::get_canonical_holder
 205 //
 206 ciInstanceKlass* ciInstanceKlass::get_canonical_holder(int offset) {
 207   #ifdef ASSERT
 208   if (!((offset >= 0 && offset < layout_helper()) SHENANDOAHGC_ONLY(|| (UseShenandoahGC && offset == ShenandoahBrooksPointer::byte_offset())))) {
 209     tty->print("*** get_canonical_holder(%d) on ", offset);
 210     this->print();
 211     tty->print_cr(" ***");
 212     fatal("offset must be tame");
 213   }
 214   #endif
 215 
 216   if (offset < instanceOopDesc::base_offset_in_bytes()) {
 217     // All header offsets belong properly to java/lang/Object.
 218     return CURRENT_ENV->Object_klass();
 219   }
 220 
 221   ciInstanceKlass* self = this;
 222   for (;;) {
 223     assert(self->is_loaded(), "must be loaded to have size");
 224     ciInstanceKlass* super = self->super();
 225     if (super == NULL || super->nof_nonstatic_fields() == 0 ||
 226         !super->contains_field_offset(offset)) {
 227       return self;
 228     } else {
 229       self = super;  // return super->get_canonical_holder(offset)
 230     }
 231   }
 232 }
 233 


 543   }
 544   return false;
 545 }
 546 
 547 void ciInstanceKlass::compute_injected_fields() {
 548   assert(is_loaded(), "must be loaded");
 549 
 550   int has_injected_fields = 0;
 551   if (super() != NULL && super()->has_injected_fields()) {
 552     has_injected_fields = 1;
 553   } else {
 554     GUARDED_VM_ENTRY({
 555         has_injected_fields = compute_injected_fields_helper() ? 1 : 0;
 556       });
 557   }
 558   // may be concurrently initialized for shared ciInstanceKlass objects
 559   assert(_has_injected_fields == -1 || _has_injected_fields == has_injected_fields, "broken concurrent initialization");
 560   _has_injected_fields = has_injected_fields;
 561 }
 562 
 563 void ciInstanceKlass::compute_object_fields() {
 564   for (int i = 0; i < nof_nonstatic_fields(); i++) {
 565     ciField* f = nonstatic_field_at(i);
 566     if (f->layout_type() == T_OBJECT) {
 567       assert(_has_object_fields == -1 || _has_object_fields == 1, "broken concurrent initialization");
 568       _has_object_fields = 1;
 569       return;
 570     }
 571   }
 572   assert(_has_object_fields == -1 || _has_object_fields == 0, "broken concurrent initialization");
 573   _has_object_fields = 0;
 574 }
 575 
 576 // ------------------------------------------------------------------
 577 // ciInstanceKlass::find_method
 578 //
 579 // Find a method in this klass.
 580 ciMethod* ciInstanceKlass::find_method(ciSymbol* name, ciSymbol* signature) {
 581   VM_ENTRY_MARK;
 582   InstanceKlass* k = get_instanceKlass();
 583   Symbol* name_sym = name->get_symbol();
 584   Symbol* sig_sym= signature->get_symbol();
 585 
 586   Method* m = k->find_method(name_sym, sig_sym);
 587   if (m == NULL)  return NULL;
 588 
 589   return CURRENT_THREAD_ENV->get_method(m);
 590 }
 591 
 592 // ------------------------------------------------------------------
 593 // ciInstanceKlass::is_leaf_type
 594 bool ciInstanceKlass::is_leaf_type() {
 595   assert(is_loaded(), "must be loaded");


 738     sub = sub->next_sibling();
 739   }
 740 
 741   // Dump out the state of the constant pool tags.  During replay the
 742   // tags will be validated for things which shouldn't change and
 743   // classes will be resolved if the tags indicate that they were
 744   // resolved at compile time.
 745   out->print("ciInstanceKlass %s %d %d %d", ik->name()->as_quoted_ascii(),
 746              is_linked(), is_initialized(), cp->length());
 747   for (int index = 1; index < cp->length(); index++) {
 748     out->print(" %d", cp->tags()->at(index));
 749   }
 750   out->cr();
 751   if (is_initialized()) {
 752     //  Dump out the static final fields in case the compilation relies
 753     //  on their value for correct replay.
 754     StaticFinalFieldPrinter sffp(out, ik->name()->as_quoted_ascii());
 755     ik->do_local_static_fields(&sffp);
 756   }
 757 }
 758 
 759 #ifdef ASSERT
 760 bool ciInstanceKlass::debug_final_field_at(int offset) {
 761   GUARDED_VM_ENTRY(
 762     InstanceKlass* ik = get_instanceKlass();
 763     fieldDescriptor fd;
 764     if (ik->find_field_from_offset(offset, false, &fd)) {
 765       return fd.is_final();
 766     }
 767   );
 768   return false;
 769 }
 770 
 771 bool ciInstanceKlass::debug_stable_field_at(int offset) {
 772   GUARDED_VM_ENTRY(
 773     InstanceKlass* ik = get_instanceKlass();
 774     fieldDescriptor fd;
 775     if (ik->find_field_from_offset(offset, false, &fd)) {
 776       return fd.is_stable();
 777     }
 778   );
 779   return false;
 780 }
 781 #endif
< prev index next >