< prev index next >

src/hotspot/share/ci/ciInstanceKlass.cpp

Print this page




  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   _nof_declared_nonstatic_fields = -1; // 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   Thread *thread = Thread::current();
  72   if (ciObjectFactory::is_initialized()) {
  73     _loader = JNIHandles::make_local(thread, ik->class_loader());
  74     _protection_domain = JNIHandles::make_local(thread,
  75                                                 ik->protection_domain());
  76     _is_shared = false;
  77   } else {
  78     Handle h_loader(thread, ik->class_loader());
  79     Handle h_protection_domain(thread, ik->protection_domain());
  80     _loader = JNIHandles::make_global(h_loader);
  81     _protection_domain = JNIHandles::make_global(h_protection_domain);
  82     _is_shared = true;
  83   }
  84 
  85   // Lazy fields get filled in only upon request.
  86   _super  = NULL;


  89   if (is_shared()) {
  90     if (k != SystemDictionary::Object_klass()) {
  91       super();
  92     }
  93     //compute_nonstatic_fields();  // done outside of constructor
  94   }
  95 
  96   _field_cache = NULL;
  97 }
  98 
  99 // Version for unloaded classes:
 100 ciInstanceKlass::ciInstanceKlass(ciSymbol* name,
 101                                  jobject loader, jobject protection_domain)
 102   : ciKlass(name, T_OBJECT)
 103 {
 104   assert(name->byte_at(0) != '[', "not an instance klass");
 105   _init_state = (InstanceKlass::ClassState)0;
 106   _nonstatic_field_size = -1;
 107   _has_nonstatic_fields = false;
 108   _nonstatic_fields = NULL;            // initialized lazily by compute_nonstatic_fields
 109   _nof_declared_nonstatic_fields = -1; // initialized lazily by compute_nonstatic_fields
 110   _has_injected_fields = -1;
 111   _vcc_klass = NULL;
 112   _is_anonymous = false;
 113   _loader = loader;
 114   _protection_domain = protection_domain;
 115   _is_shared = false;
 116   _super = NULL;
 117   _java_mirror = NULL;
 118   _field_cache = NULL;
 119 }
 120 
 121 
 122 
 123 // ------------------------------------------------------------------
 124 // ciInstanceKlass::compute_shared_is_initialized
 125 void ciInstanceKlass::compute_shared_init_state() {
 126   GUARDED_VM_ENTRY(
 127     InstanceKlass* ik = get_instanceKlass();
 128     _init_state = ik->init_state();
 129   )


 440   return field;
 441 }
 442 
 443 
 444 static int sort_field_by_offset(ciField** a, ciField** b) {
 445   return (*a)->offset_in_bytes() - (*b)->offset_in_bytes();
 446   // (no worries about 32-bit overflow...)
 447 }
 448 
 449 // ------------------------------------------------------------------
 450 // ciInstanceKlass::compute_nonstatic_fields
 451 int ciInstanceKlass::compute_nonstatic_fields() {
 452   assert(is_loaded(), "must be loaded");
 453 
 454   if (_nonstatic_fields != NULL)
 455     return _nonstatic_fields->length();
 456 
 457   if (!has_nonstatic_fields()) {
 458     Arena* arena = CURRENT_ENV->arena();
 459     _nonstatic_fields = new (arena) GrowableArray<ciField*>(arena, 0, 0, NULL);
 460     _nof_declared_nonstatic_fields = 0;
 461     return 0;
 462   }
 463   assert(!is_java_lang_Object(), "bootstrap OK");
 464 
 465   // Size in bytes of my fields, including inherited fields.
 466   int fsize = nonstatic_field_size() * heapOopSize;
 467 
 468   ciInstanceKlass* super = this->super();
 469   GrowableArray<ciField*>* super_fields = NULL;
 470   if (super != NULL && super->has_nonstatic_fields()) {
 471     int super_fsize  = super->nonstatic_field_size() * heapOopSize;
 472     int super_flen   = super->nof_nonstatic_fields();
 473     super_fields = super->_nonstatic_fields;
 474     assert(super_flen == 0 || super_fields != NULL, "first get nof_fields");
 475     // See if I am no larger than my super; if so, I can use his fields.
 476     if (fsize == super_fsize) {
 477       _nonstatic_fields = super_fields;
 478       _nof_declared_nonstatic_fields = super->nof_declared_nonstatic_fields();
 479       return super_fields->length();
 480     }
 481   }
 482 
 483   GrowableArray<ciField*>* fields = NULL;
 484   GUARDED_VM_ENTRY({
 485       fields = compute_nonstatic_fields_impl(super_fields);
 486     });
 487 
 488   if (fields == NULL) {
 489     // This can happen if this class (java.lang.Class) has invisible fields.
 490     if (super_fields != NULL) {
 491       _nonstatic_fields = super_fields;
 492       _nof_declared_nonstatic_fields = super->nof_declared_nonstatic_fields();
 493       return super_fields->length();
 494     } else {
 495       _nof_declared_nonstatic_fields = 0;
 496       return 0;
 497     }
 498   }
 499 
 500   int flen = fields->length();
 501 
 502   // Now sort them by offset, ascending.
 503   // (In principle, they could mix with superclass fields.)
 504   fields->sort(sort_field_by_offset);
 505   _nonstatic_fields = fields;
 506   return flen;
 507 }
 508 
 509 GrowableArray<ciField*>*
 510 ciInstanceKlass::compute_nonstatic_fields_impl(GrowableArray<ciField*>*
 511                                                super_fields) {
 512   ASSERT_IN_VM;
 513   Arena* arena = CURRENT_ENV->arena();
 514   int flen = 0;
 515   GrowableArray<ciField*>* fields = NULL;
 516   InstanceKlass* k = get_instanceKlass();
 517   for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
 518     if (fs.access_flags().is_static())  continue;
 519     flen += 1;
 520   }
 521 
 522   // allocate the array:
 523   if (flen == 0) {
 524     _nof_declared_nonstatic_fields = flen;
 525     return NULL;  // return nothing if none are locally declared
 526   }
 527   if (super_fields != NULL) {
 528     flen += super_fields->length();
 529   }
 530   _nof_declared_nonstatic_fields = flen;
 531 
 532   fields = new (arena) GrowableArray<ciField*>(arena, flen, 0, NULL);
 533   if (super_fields != NULL) {
 534     fields->appendAll(super_fields);
 535   }
 536 
 537   for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
 538     if (fs.access_flags().is_static())  continue;
 539     fieldDescriptor& fd = fs.field_descriptor();
 540     if (fd.field_type() == T_VALUETYPE) {

 541       // Value type fields are embedded
 542       int field_offset = fd.offset();
 543       // Get ValueKlass and adjust number of fields
 544       ciValueKlass* vk = get_field_type_by_offset(field_offset)->as_value_klass();
 545       flen += vk->flattened_field_count() - 1;
 546       // Iterate over fields of the flattened value type and copy them to 'this'
 547       for (int i = 0; i < vk->nof_nonstatic_fields(); ++i) {
 548         ciField* flattened_field = vk->nonstatic_field_at(i);
 549         // Adjust offset to account for missing oop header
 550         int offset = field_offset + (flattened_field->offset() - vk->first_field_offset());
 551         // A flattened field can be treated as final if the non-flattened
 552         // field is declared final or the holder klass is a value type itself.
 553         bool is_final = fd.is_final() || is_valuetype();
 554         ciField* field = new (arena) ciField(flattened_field, this, offset, is_final);
 555         fields->append(field);
 556       }
 557     } else {
 558       ciField* field = new (arena) ciField(&fd);
 559       fields->append(field);
 560     }
 561   }
 562   assert(fields->length() == flen, "sanity");



 563   return fields;
 564 }
 565 
 566 bool ciInstanceKlass::compute_injected_fields_helper() {
 567   ASSERT_IN_VM;
 568   InstanceKlass* k = get_instanceKlass();
 569 
 570   for (InternalFieldStream fs(k); !fs.done(); fs.next()) {
 571     if (fs.access_flags().is_static())  continue;
 572     return true;
 573   }
 574   return false;
 575 }
 576 
 577 void ciInstanceKlass::compute_injected_fields() {
 578   assert(is_loaded(), "must be loaded");
 579 
 580   int has_injected_fields = 0;
 581   if (super() != NULL && super()->has_injected_fields()) {
 582     has_injected_fields = 1;




  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;


  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   )


 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;


< prev index next >