hotspot/src/share/vm/ci/ciInstanceKlass.cpp

Print this page
rev 611 : Merge
   1 #ifdef USE_PRAGMA_IDENT_SRC
   2 #pragma ident "@(#)ciInstanceKlass.cpp  1.45 07/09/28 10:23:23 JVM"
   3 #endif
   4 /*
   5  * Copyright 1999-2007 Sun Microsystems, Inc.  All Rights Reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  23  * CA 95054 USA or visit www.sun.com if you need additional information or
  24  * have any questions.
  25  *  
  26  */
  27 
  28 #include "incls/_precompiled.incl"
  29 #include "incls/_ciInstanceKlass.cpp.incl"
  30 
  31 // ciInstanceKlass
  32 //
  33 // This class represents a klassOop in the HotSpot virtual machine
  34 // whose Klass part in an instanceKlass.
  35 
  36 // ------------------------------------------------------------------
  37 // ciInstanceKlass::ciInstanceKlass
  38 //
  39 // Loaded instance klass.
  40 ciInstanceKlass::ciInstanceKlass(KlassHandle h_k) : ciKlass(h_k) {


  41   assert(get_Klass()->oop_is_instance(), "wrong type");
  42   instanceKlass* ik = get_instanceKlass();
  43 
  44   AccessFlags access_flags = ik->access_flags();
  45   _flags = ciFlags(access_flags);
  46   _has_finalizer = access_flags.has_finalizer();
  47   _has_subklass = ik->subklass() != NULL;
  48   _is_initialized = ik->is_initialized();
  49   // Next line must follow and use the result of the previous line:
  50   _is_linked = _is_initialized || ik->is_linked();
  51   _nonstatic_field_size = ik->nonstatic_field_size();

  52   _nonstatic_fields = NULL; // initialized lazily by compute_nonstatic_fields:
  53 
  54   _nof_implementors = ik->nof_implementors();
  55   for (int i = 0; i < implementors_limit; i++) {
  56     _implementors[i] = NULL;  // we will fill these lazily
  57   }
  58 
  59   Thread *thread = Thread::current();
  60   if (ciObjectFactory::is_initialized()) {
  61     _loader = JNIHandles::make_local(thread, ik->class_loader());
  62     _protection_domain = JNIHandles::make_local(thread,
  63                                                 ik->protection_domain());
  64     _is_shared = false;
  65   } else {
  66     Handle h_loader(thread, ik->class_loader());
  67     Handle h_protection_domain(thread, ik->protection_domain());
  68     _loader = JNIHandles::make_global(h_loader);
  69     _protection_domain = JNIHandles::make_global(h_protection_domain);
  70     _is_shared = true;
  71   }


  77   if (is_shared()) {
  78     if (h_k() != SystemDictionary::object_klass()) {
  79       super();
  80     }
  81     java_mirror();
  82     //compute_nonstatic_fields();  // done outside of constructor
  83   }
  84 
  85   _field_cache = NULL;
  86 }
  87 
  88 // Version for unloaded classes:
  89 ciInstanceKlass::ciInstanceKlass(ciSymbol* name,
  90                                  jobject loader, jobject protection_domain)
  91   : ciKlass(name, ciInstanceKlassKlass::make())
  92 {
  93   assert(name->byte_at(0) != '[', "not an instance klass");
  94   _is_initialized = false;
  95   _is_linked = false;
  96   _nonstatic_field_size = -1;

  97   _nonstatic_fields = NULL;
  98   _nof_implementors = -1;
  99   _loader = loader;
 100   _protection_domain = protection_domain;
 101   _is_shared = false;
 102   _super = NULL;
 103   _java_mirror = NULL;
 104   _field_cache = NULL;
 105 }
 106 
 107 
 108 
 109 // ------------------------------------------------------------------
 110 // ciInstanceKlass::compute_shared_is_initialized
 111 bool ciInstanceKlass::compute_shared_is_initialized() {
 112   GUARDED_VM_ENTRY(
 113     instanceKlass* ik = get_instanceKlass();
 114     _is_initialized = ik->is_initialized();
 115     return _is_initialized;
 116   )


 185     assert(!is_java_lang_Object(), "Object has no fields");
 186     Arena* arena = CURRENT_ENV->arena();
 187     _field_cache = new (arena) ciConstantPoolCache(arena, 5);
 188   }
 189   return _field_cache;
 190 }
 191 
 192 // ------------------------------------------------------------------
 193 // ciInstanceKlass::get_canonical_holder
 194 //
 195 ciInstanceKlass* ciInstanceKlass::get_canonical_holder(int offset) {
 196   #ifdef ASSERT
 197   if (!(offset >= 0 && offset < layout_helper())) {
 198     tty->print("*** get_canonical_holder(%d) on ", offset);
 199     this->print();
 200     tty->print_cr(" ***");
 201   };
 202   assert(offset >= 0 && offset < layout_helper(), "offset must be tame");
 203   #endif
 204 
 205   if (offset < (instanceOopDesc::header_size() * wordSize)) {
 206     // All header offsets belong properly to java/lang/Object.
 207     return CURRENT_ENV->Object_klass();
 208   }
 209   
 210   ciInstanceKlass* self = this;
 211   for (;;) {
 212     assert(self->is_loaded(), "must be loaded to have size");
 213     ciInstanceKlass* super = self->super();
 214     if (super == NULL || !super->contains_field_offset(offset)) {

 215       return self;
 216     } else {
 217       self = super;  // return super->get_canonical_holder(offset)
 218     }
 219   }
 220 }
 221 
 222 // ------------------------------------------------------------------
 223 // ciInstanceKlass::is_java_lang_Object
 224 //
 225 // Is this klass java.lang.Object?
 226 bool ciInstanceKlass::is_java_lang_Object() {
 227   return equals(CURRENT_ENV->Object_klass());
 228 }
 229 
 230 // ------------------------------------------------------------------
 231 // ciInstanceKlass::uses_default_loader
 232 bool ciInstanceKlass::uses_default_loader() {
 233   VM_ENTRY_MARK;
 234   return loader() == NULL;


 321       ciField* field = _nonstatic_fields->at(i);
 322       int  field_off = field->offset_in_bytes();
 323       if (field_off == field_offset)
 324         return field;
 325       if (field_off > field_offset)
 326         break;
 327       // could do binary search or check bins, but probably not worth it
 328     }
 329     return NULL;
 330   }
 331   VM_ENTRY_MARK;
 332   instanceKlass* k = get_instanceKlass();
 333   fieldDescriptor fd;
 334   if (!k->find_field_from_offset(field_offset, is_static, &fd)) {
 335     return NULL;
 336   }
 337   ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd);
 338   return field;
 339 }
 340 































 341 static int sort_field_by_offset(ciField** a, ciField** b) {
 342   return (*a)->offset_in_bytes() - (*b)->offset_in_bytes();
 343   // (no worries about 32-bit overflow...)
 344 }
 345 
 346 // ------------------------------------------------------------------
 347 // ciInstanceKlass::compute_nonstatic_fields
 348 int ciInstanceKlass::compute_nonstatic_fields() {
 349   assert(is_loaded(), "must be loaded");
 350 
 351   if (_nonstatic_fields != NULL)
 352     return _nonstatic_fields->length();
 353 
 354   // Size in bytes of my fields, including inherited fields.
 355   // About equal to size_helper() - sizeof(oopDesc).
 356   int fsize = nonstatic_field_size() * wordSize;
 357   if (fsize == 0) {     // easy shortcut
 358     Arena* arena = CURRENT_ENV->arena();
 359     _nonstatic_fields = new (arena) GrowableArray<ciField*>(arena, 0, 0, NULL);
 360     return 0;
 361   }
 362   assert(!is_java_lang_Object(), "bootstrap OK");
 363 



 364   ciInstanceKlass* super = this->super();
 365   int      super_fsize = 0;
 366   int      super_flen  = 0;
 367   GrowableArray<ciField*>* super_fields = NULL;
 368   if (super != NULL) {
 369     super_fsize  = super->nonstatic_field_size() * wordSize;
 370     super_flen   = super->nof_nonstatic_fields();
 371     super_fields = super->_nonstatic_fields;
 372     assert(super_flen == 0 || super_fields != NULL, "first get nof_fields");
 373   }
 374 
 375   // See if I am no larger than my super; if so, I can use his fields.
 376   if (fsize == super_fsize) {
 377     _nonstatic_fields = super_fields;
 378     return super_fields->length();
 379   }

 380 
 381   GrowableArray<ciField*>* fields = NULL;
 382   GUARDED_VM_ENTRY({
 383       fields = compute_nonstatic_fields_impl(super_fields);
 384     });
 385 
 386   if (fields == NULL) {
 387     // This can happen if this class (java.lang.Class) has invisible fields.
 388     _nonstatic_fields = super_fields;
 389     return super_fields->length();
 390   }
 391 
 392   int flen = fields->length();
 393 
 394   // Now sort them by offset, ascending.
 395   // (In principle, they could mix with superclass fields.)
 396   fields->sort(sort_field_by_offset);
 397 #ifdef ASSERT
 398   int last_offset = sizeof(oopDesc);
 399   for (int i = 0; i < fields->length(); i++) {
 400     ciField* field = fields->at(i);
 401     int offset = field->offset_in_bytes();
 402     int size   = (field->_type == NULL) ? oopSize : field->size_in_bytes();
 403     assert(last_offset <= offset, "no field overlap");
 404     if (last_offset > (int)sizeof(oopDesc))
 405       assert((offset - last_offset) < BytesPerLong, "no big holes");
 406     // Note:  Two consecutive T_BYTE fields will be separated by wordSize-1
 407     // padding bytes if one of them is declared by a superclass.
 408     // This is a minor inefficiency classFileParser.cpp.
 409     last_offset = offset + size;
 410   }
 411   assert(last_offset <= (int)sizeof(oopDesc) + fsize, "no overflow");
 412 #endif
 413 
 414   _nonstatic_fields = fields;
 415   return flen;
 416 }
 417 
 418 GrowableArray<ciField*>*
 419 ciInstanceKlass::compute_nonstatic_fields_impl(GrowableArray<ciField*>*
 420                                                super_fields) {
 421   ASSERT_IN_VM;
 422   Arena* arena = CURRENT_ENV->arena();
 423   int flen = 0;
 424   GrowableArray<ciField*>* fields = NULL;
 425   instanceKlass* k = get_instanceKlass();
 426   typeArrayOop fields_array = k->fields();
 427   for (int pass = 0; pass <= 1; pass++) {
 428     for (int i = 0, alen = fields_array->length(); i < alen; i += instanceKlass::next_offset) {
 429       fieldDescriptor fd;
 430       fd.initialize(k->as_klassOop(), i);
 431       if (fd.is_static())  continue;


   1 #ifdef USE_PRAGMA_IDENT_SRC
   2 #pragma ident "@(#)ciInstanceKlass.cpp  1.45 07/09/28 10:23:23 JVM"
   3 #endif
   4 /*
   5  * Copyright 1999-2008 Sun Microsystems, Inc.  All Rights Reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  23  * CA 95054 USA or visit www.sun.com if you need additional information or
  24  * have any questions.
  25  *  
  26  */
  27 
  28 #include "incls/_precompiled.incl"
  29 #include "incls/_ciInstanceKlass.cpp.incl"
  30 
  31 // ciInstanceKlass
  32 //
  33 // This class represents a klassOop in the HotSpot virtual machine
  34 // whose Klass part in an instanceKlass.
  35 
  36 // ------------------------------------------------------------------
  37 // ciInstanceKlass::ciInstanceKlass
  38 //
  39 // Loaded instance klass.
  40 ciInstanceKlass::ciInstanceKlass(KlassHandle h_k) :
  41   ciKlass(h_k), _non_static_fields(NULL)
  42 {
  43   assert(get_Klass()->oop_is_instance(), "wrong type");
  44   instanceKlass* ik = get_instanceKlass();
  45 
  46   AccessFlags access_flags = ik->access_flags();
  47   _flags = ciFlags(access_flags);
  48   _has_finalizer = access_flags.has_finalizer();
  49   _has_subklass = ik->subklass() != NULL;
  50   _is_initialized = ik->is_initialized();
  51   // Next line must follow and use the result of the previous line:
  52   _is_linked = _is_initialized || ik->is_linked();
  53   _nonstatic_field_size = ik->nonstatic_field_size();
  54   _has_nonstatic_fields = ik->has_nonstatic_fields();
  55   _nonstatic_fields = NULL; // initialized lazily by compute_nonstatic_fields:
  56 
  57   _nof_implementors = ik->nof_implementors();
  58   for (int i = 0; i < implementors_limit; i++) {
  59     _implementors[i] = NULL;  // we will fill these lazily
  60   }
  61 
  62   Thread *thread = Thread::current();
  63   if (ciObjectFactory::is_initialized()) {
  64     _loader = JNIHandles::make_local(thread, ik->class_loader());
  65     _protection_domain = JNIHandles::make_local(thread,
  66                                                 ik->protection_domain());
  67     _is_shared = false;
  68   } else {
  69     Handle h_loader(thread, ik->class_loader());
  70     Handle h_protection_domain(thread, ik->protection_domain());
  71     _loader = JNIHandles::make_global(h_loader);
  72     _protection_domain = JNIHandles::make_global(h_protection_domain);
  73     _is_shared = true;
  74   }


  80   if (is_shared()) {
  81     if (h_k() != SystemDictionary::object_klass()) {
  82       super();
  83     }
  84     java_mirror();
  85     //compute_nonstatic_fields();  // done outside of constructor
  86   }
  87 
  88   _field_cache = NULL;
  89 }
  90 
  91 // Version for unloaded classes:
  92 ciInstanceKlass::ciInstanceKlass(ciSymbol* name,
  93                                  jobject loader, jobject protection_domain)
  94   : ciKlass(name, ciInstanceKlassKlass::make())
  95 {
  96   assert(name->byte_at(0) != '[', "not an instance klass");
  97   _is_initialized = false;
  98   _is_linked = false;
  99   _nonstatic_field_size = -1;
 100   _has_nonstatic_fields = false;
 101   _nonstatic_fields = NULL;
 102   _nof_implementors = -1;
 103   _loader = loader;
 104   _protection_domain = protection_domain;
 105   _is_shared = false;
 106   _super = NULL;
 107   _java_mirror = NULL;
 108   _field_cache = NULL;
 109 }
 110 
 111 
 112 
 113 // ------------------------------------------------------------------
 114 // ciInstanceKlass::compute_shared_is_initialized
 115 bool ciInstanceKlass::compute_shared_is_initialized() {
 116   GUARDED_VM_ENTRY(
 117     instanceKlass* ik = get_instanceKlass();
 118     _is_initialized = ik->is_initialized();
 119     return _is_initialized;
 120   )


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


 326       ciField* field = _nonstatic_fields->at(i);
 327       int  field_off = field->offset_in_bytes();
 328       if (field_off == field_offset)
 329         return field;
 330       if (field_off > field_offset)
 331         break;
 332       // could do binary search or check bins, but probably not worth it
 333     }
 334     return NULL;
 335   }
 336   VM_ENTRY_MARK;
 337   instanceKlass* k = get_instanceKlass();
 338   fieldDescriptor fd;
 339   if (!k->find_field_from_offset(field_offset, is_static, &fd)) {
 340     return NULL;
 341   }
 342   ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd);
 343   return field;
 344 }
 345 
 346 // ------------------------------------------------------------------
 347 // ciInstanceKlass::non_static_fields.
 348 
 349 class NonStaticFieldFiller: public FieldClosure {
 350   GrowableArray<ciField*>* _arr;
 351   ciEnv* _curEnv;
 352 public:
 353   NonStaticFieldFiller(ciEnv* curEnv, GrowableArray<ciField*>* arr) :
 354     _curEnv(curEnv), _arr(arr)
 355   {}
 356   void do_field(fieldDescriptor* fd) {
 357     ciField* field = new (_curEnv->arena()) ciField(fd);
 358     _arr->append(field);
 359   }
 360 };
 361 
 362 GrowableArray<ciField*>* ciInstanceKlass::non_static_fields() {
 363   if (_non_static_fields == NULL) {
 364     VM_ENTRY_MARK;
 365     ciEnv* curEnv = ciEnv::current();
 366     instanceKlass* ik = get_instanceKlass();
 367     int max_n_fields = ik->fields()->length()/instanceKlass::next_offset;
 368 
 369     _non_static_fields =
 370       new (curEnv->arena()) GrowableArray<ciField*>(max_n_fields);
 371     NonStaticFieldFiller filler(curEnv, _non_static_fields);
 372     ik->do_nonstatic_fields(&filler);
 373   }
 374   return _non_static_fields;
 375 }
 376 
 377 static int sort_field_by_offset(ciField** a, ciField** b) {
 378   return (*a)->offset_in_bytes() - (*b)->offset_in_bytes();
 379   // (no worries about 32-bit overflow...)
 380 }
 381 
 382 // ------------------------------------------------------------------
 383 // ciInstanceKlass::compute_nonstatic_fields
 384 int ciInstanceKlass::compute_nonstatic_fields() {
 385   assert(is_loaded(), "must be loaded");
 386 
 387   if (_nonstatic_fields != NULL)
 388     return _nonstatic_fields->length();
 389 
 390   if (!has_nonstatic_fields()) {



 391     Arena* arena = CURRENT_ENV->arena();
 392     _nonstatic_fields = new (arena) GrowableArray<ciField*>(arena, 0, 0, NULL);
 393     return 0;
 394   }
 395   assert(!is_java_lang_Object(), "bootstrap OK");
 396 
 397   // Size in bytes of my fields, including inherited fields.
 398   int fsize = nonstatic_field_size() * heapOopSize;
 399 
 400   ciInstanceKlass* super = this->super();


 401   GrowableArray<ciField*>* super_fields = NULL;
 402   if (super != NULL && super->has_nonstatic_fields()) {
 403     int super_fsize  = super->nonstatic_field_size() * heapOopSize;
 404     int super_flen   = super->nof_nonstatic_fields();
 405     super_fields = super->_nonstatic_fields;
 406     assert(super_flen == 0 || super_fields != NULL, "first get nof_fields");


 407     // See if I am no larger than my super; if so, I can use his fields.
 408     if (fsize == super_fsize) {
 409       _nonstatic_fields = super_fields;
 410       return super_fields->length();
 411     }
 412   }
 413 
 414   GrowableArray<ciField*>* fields = NULL;
 415   GUARDED_VM_ENTRY({
 416       fields = compute_nonstatic_fields_impl(super_fields);
 417     });
 418 
 419   if (fields == NULL) {
 420     // This can happen if this class (java.lang.Class) has invisible fields.
 421     _nonstatic_fields = super_fields;
 422     return super_fields->length();
 423   }
 424 
 425   int flen = fields->length();
 426 
 427   // Now sort them by offset, ascending.
 428   // (In principle, they could mix with superclass fields.)
 429   fields->sort(sort_field_by_offset);
 430 #ifdef ASSERT
 431   int last_offset = instanceOopDesc::base_offset_in_bytes();
 432   for (int i = 0; i < fields->length(); i++) {
 433     ciField* field = fields->at(i);
 434     int offset = field->offset_in_bytes();
 435     int size   = (field->_type == NULL) ? heapOopSize : field->size_in_bytes();
 436     assert(last_offset <= offset, "no field overlap");
 437     if (last_offset > (int)sizeof(oopDesc))
 438       assert((offset - last_offset) < BytesPerLong, "no big holes");
 439     // Note:  Two consecutive T_BYTE fields will be separated by wordSize-1
 440     // padding bytes if one of them is declared by a superclass.
 441     // This is a minor inefficiency classFileParser.cpp.
 442     last_offset = offset + size;
 443   }
 444   assert(last_offset <= (int)instanceOopDesc::base_offset_in_bytes() + fsize, "no overflow");
 445 #endif
 446 
 447   _nonstatic_fields = fields;
 448   return flen;
 449 }
 450 
 451 GrowableArray<ciField*>*
 452 ciInstanceKlass::compute_nonstatic_fields_impl(GrowableArray<ciField*>*
 453                                                super_fields) {
 454   ASSERT_IN_VM;
 455   Arena* arena = CURRENT_ENV->arena();
 456   int flen = 0;
 457   GrowableArray<ciField*>* fields = NULL;
 458   instanceKlass* k = get_instanceKlass();
 459   typeArrayOop fields_array = k->fields();
 460   for (int pass = 0; pass <= 1; pass++) {
 461     for (int i = 0, alen = fields_array->length(); i < alen; i += instanceKlass::next_offset) {
 462       fieldDescriptor fd;
 463       fd.initialize(k->as_klassOop(), i);
 464       if (fd.is_static())  continue;