src/share/vm/ci/ciInstanceKlass.cpp

Thu, 24 May 2018 18:41:44 +0800

author
aoqi
date
Thu, 24 May 2018 18:41:44 +0800
changeset 8856
ac27a9c85bea
parent 8739
0b85ccd62409
parent 6876
710a3c8b516e
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
coleenp@8739 2 * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "ci/ciField.hpp"
aoqi@0 27 #include "ci/ciInstance.hpp"
aoqi@0 28 #include "ci/ciInstanceKlass.hpp"
aoqi@0 29 #include "ci/ciUtilities.hpp"
aoqi@0 30 #include "classfile/systemDictionary.hpp"
aoqi@0 31 #include "memory/allocation.hpp"
aoqi@0 32 #include "memory/allocation.inline.hpp"
aoqi@0 33 #include "oops/oop.inline.hpp"
aoqi@0 34 #include "oops/fieldStreams.hpp"
aoqi@0 35 #include "runtime/fieldDescriptor.hpp"
aoqi@0 36
aoqi@0 37 // ciInstanceKlass
aoqi@0 38 //
aoqi@0 39 // This class represents a Klass* in the HotSpot virtual machine
aoqi@0 40 // whose Klass part in an InstanceKlass.
aoqi@0 41
aoqi@0 42 // ------------------------------------------------------------------
aoqi@0 43 // ciInstanceKlass::ciInstanceKlass
aoqi@0 44 //
aoqi@0 45 // Loaded instance klass.
aoqi@0 46 ciInstanceKlass::ciInstanceKlass(KlassHandle h_k) :
aoqi@0 47 ciKlass(h_k), _non_static_fields(NULL)
aoqi@0 48 {
aoqi@0 49 assert(get_Klass()->oop_is_instance(), "wrong type");
aoqi@0 50 assert(get_instanceKlass()->is_loaded(), "must be at least loaded");
aoqi@0 51 InstanceKlass* ik = get_instanceKlass();
aoqi@0 52
aoqi@0 53 AccessFlags access_flags = ik->access_flags();
aoqi@0 54 _flags = ciFlags(access_flags);
aoqi@0 55 _has_finalizer = access_flags.has_finalizer();
aoqi@0 56 _has_subklass = ik->subklass() != NULL;
aoqi@0 57 _init_state = ik->init_state();
aoqi@0 58 _nonstatic_field_size = ik->nonstatic_field_size();
aoqi@0 59 _has_nonstatic_fields = ik->has_nonstatic_fields();
aoqi@0 60 _has_default_methods = ik->has_default_methods();
coleenp@8739 61 _is_anonymous = ik->is_anonymous();
aoqi@0 62 _nonstatic_fields = NULL; // initialized lazily by compute_nonstatic_fields:
aoqi@0 63
aoqi@0 64 _implementor = NULL; // we will fill these lazily
aoqi@0 65
aoqi@0 66 Thread *thread = Thread::current();
aoqi@0 67 if (ciObjectFactory::is_initialized()) {
aoqi@0 68 _loader = JNIHandles::make_local(thread, ik->class_loader());
aoqi@0 69 _protection_domain = JNIHandles::make_local(thread,
aoqi@0 70 ik->protection_domain());
aoqi@0 71 _is_shared = false;
aoqi@0 72 } else {
aoqi@0 73 Handle h_loader(thread, ik->class_loader());
aoqi@0 74 Handle h_protection_domain(thread, ik->protection_domain());
aoqi@0 75 _loader = JNIHandles::make_global(h_loader);
aoqi@0 76 _protection_domain = JNIHandles::make_global(h_protection_domain);
aoqi@0 77 _is_shared = true;
aoqi@0 78 }
aoqi@0 79
aoqi@0 80 // Lazy fields get filled in only upon request.
aoqi@0 81 _super = NULL;
aoqi@0 82 _java_mirror = NULL;
aoqi@0 83
aoqi@0 84 if (is_shared()) {
aoqi@0 85 if (h_k() != SystemDictionary::Object_klass()) {
aoqi@0 86 super();
aoqi@0 87 }
aoqi@0 88 //compute_nonstatic_fields(); // done outside of constructor
aoqi@0 89 }
aoqi@0 90
aoqi@0 91 _field_cache = NULL;
aoqi@0 92 }
aoqi@0 93
aoqi@0 94 // Version for unloaded classes:
aoqi@0 95 ciInstanceKlass::ciInstanceKlass(ciSymbol* name,
aoqi@0 96 jobject loader, jobject protection_domain)
aoqi@0 97 : ciKlass(name, T_OBJECT)
aoqi@0 98 {
aoqi@0 99 assert(name->byte_at(0) != '[', "not an instance klass");
aoqi@0 100 _init_state = (InstanceKlass::ClassState)0;
aoqi@0 101 _nonstatic_field_size = -1;
aoqi@0 102 _has_nonstatic_fields = false;
aoqi@0 103 _nonstatic_fields = NULL;
coleenp@8739 104 _is_anonymous = false;
aoqi@0 105 _loader = loader;
aoqi@0 106 _protection_domain = protection_domain;
aoqi@0 107 _is_shared = false;
aoqi@0 108 _super = NULL;
aoqi@0 109 _java_mirror = NULL;
aoqi@0 110 _field_cache = NULL;
aoqi@0 111 }
aoqi@0 112
aoqi@0 113
aoqi@0 114
aoqi@0 115 // ------------------------------------------------------------------
aoqi@0 116 // ciInstanceKlass::compute_shared_is_initialized
aoqi@0 117 void ciInstanceKlass::compute_shared_init_state() {
aoqi@0 118 GUARDED_VM_ENTRY(
aoqi@0 119 InstanceKlass* ik = get_instanceKlass();
aoqi@0 120 _init_state = ik->init_state();
aoqi@0 121 )
aoqi@0 122 }
aoqi@0 123
aoqi@0 124 // ------------------------------------------------------------------
aoqi@0 125 // ciInstanceKlass::compute_shared_has_subklass
aoqi@0 126 bool ciInstanceKlass::compute_shared_has_subklass() {
aoqi@0 127 GUARDED_VM_ENTRY(
aoqi@0 128 InstanceKlass* ik = get_instanceKlass();
aoqi@0 129 _has_subklass = ik->subklass() != NULL;
aoqi@0 130 return _has_subklass;
aoqi@0 131 )
aoqi@0 132 }
aoqi@0 133
aoqi@0 134 // ------------------------------------------------------------------
aoqi@0 135 // ciInstanceKlass::loader
aoqi@0 136 oop ciInstanceKlass::loader() {
aoqi@0 137 ASSERT_IN_VM;
aoqi@0 138 return JNIHandles::resolve(_loader);
aoqi@0 139 }
aoqi@0 140
aoqi@0 141 // ------------------------------------------------------------------
aoqi@0 142 // ciInstanceKlass::loader_handle
aoqi@0 143 jobject ciInstanceKlass::loader_handle() {
aoqi@0 144 return _loader;
aoqi@0 145 }
aoqi@0 146
aoqi@0 147 // ------------------------------------------------------------------
aoqi@0 148 // ciInstanceKlass::protection_domain
aoqi@0 149 oop ciInstanceKlass::protection_domain() {
aoqi@0 150 ASSERT_IN_VM;
aoqi@0 151 return JNIHandles::resolve(_protection_domain);
aoqi@0 152 }
aoqi@0 153
aoqi@0 154 // ------------------------------------------------------------------
aoqi@0 155 // ciInstanceKlass::protection_domain_handle
aoqi@0 156 jobject ciInstanceKlass::protection_domain_handle() {
aoqi@0 157 return _protection_domain;
aoqi@0 158 }
aoqi@0 159
aoqi@0 160 // ------------------------------------------------------------------
aoqi@0 161 // ciInstanceKlass::field_cache
aoqi@0 162 //
aoqi@0 163 // Get the field cache associated with this klass.
aoqi@0 164 ciConstantPoolCache* ciInstanceKlass::field_cache() {
aoqi@0 165 if (is_shared()) {
aoqi@0 166 return NULL;
aoqi@0 167 }
aoqi@0 168 if (_field_cache == NULL) {
aoqi@0 169 assert(!is_java_lang_Object(), "Object has no fields");
aoqi@0 170 Arena* arena = CURRENT_ENV->arena();
aoqi@0 171 _field_cache = new (arena) ciConstantPoolCache(arena, 5);
aoqi@0 172 }
aoqi@0 173 return _field_cache;
aoqi@0 174 }
aoqi@0 175
aoqi@0 176 // ------------------------------------------------------------------
aoqi@0 177 // ciInstanceKlass::get_canonical_holder
aoqi@0 178 //
aoqi@0 179 ciInstanceKlass* ciInstanceKlass::get_canonical_holder(int offset) {
aoqi@0 180 #ifdef ASSERT
aoqi@0 181 if (!(offset >= 0 && offset < layout_helper())) {
aoqi@0 182 tty->print("*** get_canonical_holder(%d) on ", offset);
aoqi@0 183 this->print();
aoqi@0 184 tty->print_cr(" ***");
aoqi@0 185 };
aoqi@0 186 assert(offset >= 0 && offset < layout_helper(), "offset must be tame");
aoqi@0 187 #endif
aoqi@0 188
aoqi@0 189 if (offset < instanceOopDesc::base_offset_in_bytes()) {
aoqi@0 190 // All header offsets belong properly to java/lang/Object.
aoqi@0 191 return CURRENT_ENV->Object_klass();
aoqi@0 192 }
aoqi@0 193
aoqi@0 194 ciInstanceKlass* self = this;
aoqi@0 195 for (;;) {
aoqi@0 196 assert(self->is_loaded(), "must be loaded to have size");
aoqi@0 197 ciInstanceKlass* super = self->super();
aoqi@0 198 if (super == NULL || super->nof_nonstatic_fields() == 0 ||
aoqi@0 199 !super->contains_field_offset(offset)) {
aoqi@0 200 return self;
aoqi@0 201 } else {
aoqi@0 202 self = super; // return super->get_canonical_holder(offset)
aoqi@0 203 }
aoqi@0 204 }
aoqi@0 205 }
aoqi@0 206
aoqi@0 207 // ------------------------------------------------------------------
aoqi@0 208 // ciInstanceKlass::is_java_lang_Object
aoqi@0 209 //
aoqi@0 210 // Is this klass java.lang.Object?
aoqi@0 211 bool ciInstanceKlass::is_java_lang_Object() const {
aoqi@0 212 return equals(CURRENT_ENV->Object_klass());
aoqi@0 213 }
aoqi@0 214
aoqi@0 215 // ------------------------------------------------------------------
aoqi@0 216 // ciInstanceKlass::uses_default_loader
aoqi@0 217 bool ciInstanceKlass::uses_default_loader() const {
aoqi@0 218 // Note: We do not need to resolve the handle or enter the VM
aoqi@0 219 // in order to test null-ness.
aoqi@0 220 return _loader == NULL;
aoqi@0 221 }
aoqi@0 222
aoqi@0 223 // ------------------------------------------------------------------
aoqi@0 224
aoqi@0 225 /**
aoqi@0 226 * Return basic type of boxed value for box klass or T_OBJECT if not.
aoqi@0 227 */
aoqi@0 228 BasicType ciInstanceKlass::box_klass_type() const {
aoqi@0 229 if (uses_default_loader() && is_loaded()) {
aoqi@0 230 return SystemDictionary::box_klass_type(get_Klass());
aoqi@0 231 } else {
aoqi@0 232 return T_OBJECT;
aoqi@0 233 }
aoqi@0 234 }
aoqi@0 235
aoqi@0 236 /**
aoqi@0 237 * Is this boxing klass?
aoqi@0 238 */
aoqi@0 239 bool ciInstanceKlass::is_box_klass() const {
aoqi@0 240 return is_java_primitive(box_klass_type());
aoqi@0 241 }
aoqi@0 242
aoqi@0 243 /**
aoqi@0 244 * Is this boxed value offset?
aoqi@0 245 */
aoqi@0 246 bool ciInstanceKlass::is_boxed_value_offset(int offset) const {
aoqi@0 247 BasicType bt = box_klass_type();
aoqi@0 248 return is_java_primitive(bt) &&
aoqi@0 249 (offset == java_lang_boxing_object::value_offset_in_bytes(bt));
aoqi@0 250 }
aoqi@0 251
aoqi@0 252 // ------------------------------------------------------------------
aoqi@0 253 // ciInstanceKlass::is_in_package
aoqi@0 254 //
aoqi@0 255 // Is this klass in the given package?
aoqi@0 256 bool ciInstanceKlass::is_in_package(const char* packagename, int len) {
aoqi@0 257 // To avoid class loader mischief, this test always rejects application classes.
aoqi@0 258 if (!uses_default_loader())
aoqi@0 259 return false;
aoqi@0 260 GUARDED_VM_ENTRY(
aoqi@0 261 return is_in_package_impl(packagename, len);
aoqi@0 262 )
aoqi@0 263 }
aoqi@0 264
aoqi@0 265 bool ciInstanceKlass::is_in_package_impl(const char* packagename, int len) {
aoqi@0 266 ASSERT_IN_VM;
aoqi@0 267
aoqi@0 268 // If packagename contains trailing '/' exclude it from the
aoqi@0 269 // prefix-test since we test for it explicitly.
aoqi@0 270 if (packagename[len - 1] == '/')
aoqi@0 271 len--;
aoqi@0 272
aoqi@0 273 if (!name()->starts_with(packagename, len))
aoqi@0 274 return false;
aoqi@0 275
aoqi@0 276 // Test if the class name is something like "java/lang".
aoqi@0 277 if ((len + 1) > name()->utf8_length())
aoqi@0 278 return false;
aoqi@0 279
aoqi@0 280 // Test for trailing '/'
aoqi@0 281 if ((char) name()->byte_at(len) != '/')
aoqi@0 282 return false;
aoqi@0 283
aoqi@0 284 // Make sure it's not actually in a subpackage:
aoqi@0 285 if (name()->index_of_at(len+1, "/", 1) >= 0)
aoqi@0 286 return false;
aoqi@0 287
aoqi@0 288 return true;
aoqi@0 289 }
aoqi@0 290
aoqi@0 291 // ------------------------------------------------------------------
aoqi@0 292 // ciInstanceKlass::print_impl
aoqi@0 293 //
aoqi@0 294 // Implementation of the print method.
aoqi@0 295 void ciInstanceKlass::print_impl(outputStream* st) {
aoqi@0 296 ciKlass::print_impl(st);
aoqi@0 297 GUARDED_VM_ENTRY(st->print(" loader=" INTPTR_FORMAT, p2i((address)loader()));)
aoqi@0 298 if (is_loaded()) {
aoqi@0 299 st->print(" loaded=true initialized=%s finalized=%s subklass=%s size=%d flags=",
aoqi@0 300 bool_to_str(is_initialized()),
aoqi@0 301 bool_to_str(has_finalizer()),
aoqi@0 302 bool_to_str(has_subklass()),
aoqi@0 303 layout_helper());
aoqi@0 304
aoqi@0 305 _flags.print_klass_flags();
aoqi@0 306
aoqi@0 307 if (_super) {
aoqi@0 308 st->print(" super=");
aoqi@0 309 _super->print_name();
aoqi@0 310 }
aoqi@0 311 if (_java_mirror) {
aoqi@0 312 st->print(" mirror=PRESENT");
aoqi@0 313 }
aoqi@0 314 } else {
aoqi@0 315 st->print(" loaded=false");
aoqi@0 316 }
aoqi@0 317 }
aoqi@0 318
aoqi@0 319 // ------------------------------------------------------------------
aoqi@0 320 // ciInstanceKlass::super
aoqi@0 321 //
aoqi@0 322 // Get the superklass of this klass.
aoqi@0 323 ciInstanceKlass* ciInstanceKlass::super() {
aoqi@0 324 assert(is_loaded(), "must be loaded");
aoqi@0 325 if (_super == NULL && !is_java_lang_Object()) {
aoqi@0 326 GUARDED_VM_ENTRY(
aoqi@0 327 Klass* super_klass = get_instanceKlass()->super();
aoqi@0 328 _super = CURRENT_ENV->get_instance_klass(super_klass);
aoqi@0 329 )
aoqi@0 330 }
aoqi@0 331 return _super;
aoqi@0 332 }
aoqi@0 333
aoqi@0 334 // ------------------------------------------------------------------
aoqi@0 335 // ciInstanceKlass::java_mirror
aoqi@0 336 //
aoqi@0 337 // Get the instance of java.lang.Class corresponding to this klass.
aoqi@0 338 // Cache it on this->_java_mirror.
aoqi@0 339 ciInstance* ciInstanceKlass::java_mirror() {
aoqi@0 340 if (is_shared()) {
aoqi@0 341 return ciKlass::java_mirror();
aoqi@0 342 }
aoqi@0 343 if (_java_mirror == NULL) {
aoqi@0 344 _java_mirror = ciKlass::java_mirror();
aoqi@0 345 }
aoqi@0 346 return _java_mirror;
aoqi@0 347 }
aoqi@0 348
aoqi@0 349 // ------------------------------------------------------------------
aoqi@0 350 // ciInstanceKlass::unique_concrete_subklass
aoqi@0 351 ciInstanceKlass* ciInstanceKlass::unique_concrete_subklass() {
aoqi@0 352 if (!is_loaded()) return NULL; // No change if class is not loaded
aoqi@0 353 if (!is_abstract()) return NULL; // Only applies to abstract classes.
aoqi@0 354 if (!has_subklass()) return NULL; // Must have at least one subklass.
aoqi@0 355 VM_ENTRY_MARK;
aoqi@0 356 InstanceKlass* ik = get_instanceKlass();
aoqi@0 357 Klass* up = ik->up_cast_abstract();
aoqi@0 358 assert(up->oop_is_instance(), "must be InstanceKlass");
aoqi@0 359 if (ik == up) {
aoqi@0 360 return NULL;
aoqi@0 361 }
aoqi@0 362 return CURRENT_THREAD_ENV->get_instance_klass(up);
aoqi@0 363 }
aoqi@0 364
aoqi@0 365 // ------------------------------------------------------------------
aoqi@0 366 // ciInstanceKlass::has_finalizable_subclass
aoqi@0 367 bool ciInstanceKlass::has_finalizable_subclass() {
aoqi@0 368 if (!is_loaded()) return true;
aoqi@0 369 VM_ENTRY_MARK;
aoqi@0 370 return Dependencies::find_finalizable_subclass(get_instanceKlass()) != NULL;
aoqi@0 371 }
aoqi@0 372
aoqi@0 373 // ------------------------------------------------------------------
aoqi@0 374 // ciInstanceKlass::get_field_by_offset
aoqi@0 375 ciField* ciInstanceKlass::get_field_by_offset(int field_offset, bool is_static) {
aoqi@0 376 if (!is_static) {
aoqi@0 377 for (int i = 0, len = nof_nonstatic_fields(); i < len; i++) {
aoqi@0 378 ciField* field = _nonstatic_fields->at(i);
aoqi@0 379 int field_off = field->offset_in_bytes();
aoqi@0 380 if (field_off == field_offset)
aoqi@0 381 return field;
aoqi@0 382 if (field_off > field_offset)
aoqi@0 383 break;
aoqi@0 384 // could do binary search or check bins, but probably not worth it
aoqi@0 385 }
aoqi@0 386 return NULL;
aoqi@0 387 }
aoqi@0 388 VM_ENTRY_MARK;
aoqi@0 389 InstanceKlass* k = get_instanceKlass();
aoqi@0 390 fieldDescriptor fd;
aoqi@0 391 if (!k->find_field_from_offset(field_offset, is_static, &fd)) {
aoqi@0 392 return NULL;
aoqi@0 393 }
aoqi@0 394 ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd);
aoqi@0 395 return field;
aoqi@0 396 }
aoqi@0 397
aoqi@0 398 // ------------------------------------------------------------------
aoqi@0 399 // ciInstanceKlass::get_field_by_name
aoqi@0 400 ciField* ciInstanceKlass::get_field_by_name(ciSymbol* name, ciSymbol* signature, bool is_static) {
aoqi@0 401 VM_ENTRY_MARK;
aoqi@0 402 InstanceKlass* k = get_instanceKlass();
aoqi@0 403 fieldDescriptor fd;
aoqi@0 404 Klass* def = k->find_field(name->get_symbol(), signature->get_symbol(), is_static, &fd);
aoqi@0 405 if (def == NULL) {
aoqi@0 406 return NULL;
aoqi@0 407 }
aoqi@0 408 ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd);
aoqi@0 409 return field;
aoqi@0 410 }
aoqi@0 411
aoqi@0 412 // ------------------------------------------------------------------
aoqi@0 413 // ciInstanceKlass::non_static_fields.
aoqi@0 414
aoqi@0 415 class NonStaticFieldFiller: public FieldClosure {
aoqi@0 416 GrowableArray<ciField*>* _arr;
aoqi@0 417 ciEnv* _curEnv;
aoqi@0 418 public:
aoqi@0 419 NonStaticFieldFiller(ciEnv* curEnv, GrowableArray<ciField*>* arr) :
aoqi@0 420 _curEnv(curEnv), _arr(arr)
aoqi@0 421 {}
aoqi@0 422 void do_field(fieldDescriptor* fd) {
aoqi@0 423 ciField* field = new (_curEnv->arena()) ciField(fd);
aoqi@0 424 _arr->append(field);
aoqi@0 425 }
aoqi@0 426 };
aoqi@0 427
aoqi@0 428 GrowableArray<ciField*>* ciInstanceKlass::non_static_fields() {
aoqi@0 429 if (_non_static_fields == NULL) {
aoqi@0 430 VM_ENTRY_MARK;
aoqi@0 431 ciEnv* curEnv = ciEnv::current();
aoqi@0 432 InstanceKlass* ik = get_instanceKlass();
aoqi@0 433 int max_n_fields = ik->java_fields_count();
aoqi@0 434
aoqi@0 435 Arena* arena = curEnv->arena();
aoqi@0 436 _non_static_fields =
aoqi@0 437 new (arena) GrowableArray<ciField*>(arena, max_n_fields, 0, NULL);
aoqi@0 438 NonStaticFieldFiller filler(curEnv, _non_static_fields);
aoqi@0 439 ik->do_nonstatic_fields(&filler);
aoqi@0 440 }
aoqi@0 441 return _non_static_fields;
aoqi@0 442 }
aoqi@0 443
aoqi@0 444 static int sort_field_by_offset(ciField** a, ciField** b) {
aoqi@0 445 return (*a)->offset_in_bytes() - (*b)->offset_in_bytes();
aoqi@0 446 // (no worries about 32-bit overflow...)
aoqi@0 447 }
aoqi@0 448
aoqi@0 449 // ------------------------------------------------------------------
aoqi@0 450 // ciInstanceKlass::compute_nonstatic_fields
aoqi@0 451 int ciInstanceKlass::compute_nonstatic_fields() {
aoqi@0 452 assert(is_loaded(), "must be loaded");
aoqi@0 453
aoqi@0 454 if (_nonstatic_fields != NULL)
aoqi@0 455 return _nonstatic_fields->length();
aoqi@0 456
aoqi@0 457 if (!has_nonstatic_fields()) {
aoqi@0 458 Arena* arena = CURRENT_ENV->arena();
aoqi@0 459 _nonstatic_fields = new (arena) GrowableArray<ciField*>(arena, 0, 0, NULL);
aoqi@0 460 return 0;
aoqi@0 461 }
aoqi@0 462 assert(!is_java_lang_Object(), "bootstrap OK");
aoqi@0 463
aoqi@0 464 // Size in bytes of my fields, including inherited fields.
aoqi@0 465 int fsize = nonstatic_field_size() * heapOopSize;
aoqi@0 466
aoqi@0 467 ciInstanceKlass* super = this->super();
aoqi@0 468 GrowableArray<ciField*>* super_fields = NULL;
aoqi@0 469 if (super != NULL && super->has_nonstatic_fields()) {
aoqi@0 470 int super_fsize = super->nonstatic_field_size() * heapOopSize;
aoqi@0 471 int super_flen = super->nof_nonstatic_fields();
aoqi@0 472 super_fields = super->_nonstatic_fields;
aoqi@0 473 assert(super_flen == 0 || super_fields != NULL, "first get nof_fields");
aoqi@0 474 // See if I am no larger than my super; if so, I can use his fields.
aoqi@0 475 if (fsize == super_fsize) {
aoqi@0 476 _nonstatic_fields = super_fields;
aoqi@0 477 return super_fields->length();
aoqi@0 478 }
aoqi@0 479 }
aoqi@0 480
aoqi@0 481 GrowableArray<ciField*>* fields = NULL;
aoqi@0 482 GUARDED_VM_ENTRY({
aoqi@0 483 fields = compute_nonstatic_fields_impl(super_fields);
aoqi@0 484 });
aoqi@0 485
aoqi@0 486 if (fields == NULL) {
aoqi@0 487 // This can happen if this class (java.lang.Class) has invisible fields.
aoqi@0 488 _nonstatic_fields = super_fields;
aoqi@0 489 return super_fields->length();
aoqi@0 490 }
aoqi@0 491
aoqi@0 492 int flen = fields->length();
aoqi@0 493
aoqi@0 494 // Now sort them by offset, ascending.
aoqi@0 495 // (In principle, they could mix with superclass fields.)
aoqi@0 496 fields->sort(sort_field_by_offset);
aoqi@0 497 _nonstatic_fields = fields;
aoqi@0 498 return flen;
aoqi@0 499 }
aoqi@0 500
aoqi@0 501 GrowableArray<ciField*>*
aoqi@0 502 ciInstanceKlass::compute_nonstatic_fields_impl(GrowableArray<ciField*>*
aoqi@0 503 super_fields) {
aoqi@0 504 ASSERT_IN_VM;
aoqi@0 505 Arena* arena = CURRENT_ENV->arena();
aoqi@0 506 int flen = 0;
aoqi@0 507 GrowableArray<ciField*>* fields = NULL;
aoqi@0 508 InstanceKlass* k = get_instanceKlass();
aoqi@0 509 for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
aoqi@0 510 if (fs.access_flags().is_static()) continue;
aoqi@0 511 flen += 1;
aoqi@0 512 }
aoqi@0 513
aoqi@0 514 // allocate the array:
aoqi@0 515 if (flen == 0) {
aoqi@0 516 return NULL; // return nothing if none are locally declared
aoqi@0 517 }
aoqi@0 518 if (super_fields != NULL) {
aoqi@0 519 flen += super_fields->length();
aoqi@0 520 }
aoqi@0 521 fields = new (arena) GrowableArray<ciField*>(arena, flen, 0, NULL);
aoqi@0 522 if (super_fields != NULL) {
aoqi@0 523 fields->appendAll(super_fields);
aoqi@0 524 }
aoqi@0 525
aoqi@0 526 for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
aoqi@0 527 if (fs.access_flags().is_static()) continue;
aoqi@0 528 fieldDescriptor& fd = fs.field_descriptor();
aoqi@0 529 ciField* field = new (arena) ciField(&fd);
aoqi@0 530 fields->append(field);
aoqi@0 531 }
aoqi@0 532 assert(fields->length() == flen, "sanity");
aoqi@0 533 return fields;
aoqi@0 534 }
aoqi@0 535
aoqi@0 536 // ------------------------------------------------------------------
aoqi@0 537 // ciInstanceKlass::find_method
aoqi@0 538 //
aoqi@0 539 // Find a method in this klass.
aoqi@0 540 ciMethod* ciInstanceKlass::find_method(ciSymbol* name, ciSymbol* signature) {
aoqi@0 541 VM_ENTRY_MARK;
aoqi@0 542 InstanceKlass* k = get_instanceKlass();
aoqi@0 543 Symbol* name_sym = name->get_symbol();
aoqi@0 544 Symbol* sig_sym= signature->get_symbol();
aoqi@0 545
aoqi@0 546 Method* m = k->find_method(name_sym, sig_sym);
aoqi@0 547 if (m == NULL) return NULL;
aoqi@0 548
aoqi@0 549 return CURRENT_THREAD_ENV->get_method(m);
aoqi@0 550 }
aoqi@0 551
aoqi@0 552 // ------------------------------------------------------------------
aoqi@0 553 // ciInstanceKlass::is_leaf_type
aoqi@0 554 bool ciInstanceKlass::is_leaf_type() {
aoqi@0 555 assert(is_loaded(), "must be loaded");
aoqi@0 556 if (is_shared()) {
aoqi@0 557 return is_final(); // approximately correct
aoqi@0 558 } else {
aoqi@0 559 return !_has_subklass && (nof_implementors() == 0);
aoqi@0 560 }
aoqi@0 561 }
aoqi@0 562
aoqi@0 563 // ------------------------------------------------------------------
aoqi@0 564 // ciInstanceKlass::implementor
aoqi@0 565 //
aoqi@0 566 // Report an implementor of this interface.
aoqi@0 567 // Note that there are various races here, since my copy
aoqi@0 568 // of _nof_implementors might be out of date with respect
aoqi@0 569 // to results returned by InstanceKlass::implementor.
aoqi@0 570 // This is OK, since any dependencies we decide to assert
aoqi@0 571 // will be checked later under the Compile_lock.
aoqi@0 572 ciInstanceKlass* ciInstanceKlass::implementor() {
aoqi@0 573 ciInstanceKlass* impl = _implementor;
aoqi@0 574 if (impl == NULL) {
aoqi@0 575 // Go into the VM to fetch the implementor.
aoqi@0 576 {
aoqi@0 577 VM_ENTRY_MARK;
aoqi@0 578 Klass* k = get_instanceKlass()->implementor();
aoqi@0 579 if (k != NULL) {
aoqi@0 580 if (k == get_instanceKlass()) {
aoqi@0 581 // More than one implementors. Use 'this' in this case.
aoqi@0 582 impl = this;
aoqi@0 583 } else {
aoqi@0 584 impl = CURRENT_THREAD_ENV->get_instance_klass(k);
aoqi@0 585 }
aoqi@0 586 }
aoqi@0 587 }
aoqi@0 588 // Memoize this result.
aoqi@0 589 if (!is_shared()) {
aoqi@0 590 _implementor = impl;
aoqi@0 591 }
aoqi@0 592 }
aoqi@0 593 return impl;
aoqi@0 594 }
aoqi@0 595
coleenp@8739 596 ciInstanceKlass* ciInstanceKlass::host_klass() {
coleenp@8739 597 assert(is_loaded(), "must be loaded");
coleenp@8739 598 if (is_anonymous()) {
coleenp@8739 599 VM_ENTRY_MARK
coleenp@8739 600 Klass* host_klass = get_instanceKlass()->host_klass();
coleenp@8739 601 return CURRENT_ENV->get_instance_klass(host_klass);
coleenp@8739 602 }
coleenp@8739 603 return NULL;
coleenp@8739 604 }
coleenp@8739 605
aoqi@0 606 // Utility class for printing of the contents of the static fields for
aoqi@0 607 // use by compilation replay. It only prints out the information that
aoqi@0 608 // could be consumed by the compiler, so for primitive types it prints
aoqi@0 609 // out the actual value. For Strings it's the actual string value.
aoqi@0 610 // For array types it it's first level array size since that's the
aoqi@0 611 // only value which statically unchangeable. For all other reference
aoqi@0 612 // types it simply prints out the dynamic type.
aoqi@0 613
aoqi@0 614 class StaticFinalFieldPrinter : public FieldClosure {
aoqi@0 615 outputStream* _out;
aoqi@0 616 const char* _holder;
aoqi@0 617 public:
aoqi@0 618 StaticFinalFieldPrinter(outputStream* out, const char* holder) :
aoqi@0 619 _out(out),
aoqi@0 620 _holder(holder) {
aoqi@0 621 }
aoqi@0 622 void do_field(fieldDescriptor* fd) {
aoqi@0 623 if (fd->is_final() && !fd->has_initial_value()) {
aoqi@0 624 ResourceMark rm;
aoqi@0 625 oop mirror = fd->field_holder()->java_mirror();
aoqi@0 626 _out->print("staticfield %s %s %s ", _holder, fd->name()->as_quoted_ascii(), fd->signature()->as_quoted_ascii());
aoqi@0 627 switch (fd->field_type()) {
aoqi@0 628 case T_BYTE: _out->print_cr("%d", mirror->byte_field(fd->offset())); break;
aoqi@0 629 case T_BOOLEAN: _out->print_cr("%d", mirror->bool_field(fd->offset())); break;
aoqi@0 630 case T_SHORT: _out->print_cr("%d", mirror->short_field(fd->offset())); break;
aoqi@0 631 case T_CHAR: _out->print_cr("%d", mirror->char_field(fd->offset())); break;
aoqi@0 632 case T_INT: _out->print_cr("%d", mirror->int_field(fd->offset())); break;
aoqi@0 633 case T_LONG: _out->print_cr(INT64_FORMAT, (int64_t)(mirror->long_field(fd->offset()))); break;
aoqi@0 634 case T_FLOAT: {
aoqi@0 635 float f = mirror->float_field(fd->offset());
aoqi@0 636 _out->print_cr("%d", *(int*)&f);
aoqi@0 637 break;
aoqi@0 638 }
aoqi@0 639 case T_DOUBLE: {
aoqi@0 640 double d = mirror->double_field(fd->offset());
aoqi@0 641 _out->print_cr(INT64_FORMAT, *(int64_t*)&d);
aoqi@0 642 break;
aoqi@0 643 }
aoqi@0 644 case T_ARRAY: {
aoqi@0 645 oop value = mirror->obj_field_acquire(fd->offset());
aoqi@0 646 if (value == NULL) {
aoqi@0 647 _out->print_cr("null");
aoqi@0 648 } else {
aoqi@0 649 typeArrayOop ta = (typeArrayOop)value;
aoqi@0 650 _out->print("%d", ta->length());
aoqi@0 651 if (value->is_objArray()) {
aoqi@0 652 objArrayOop oa = (objArrayOop)value;
aoqi@0 653 const char* klass_name = value->klass()->name()->as_quoted_ascii();
aoqi@0 654 _out->print(" %s", klass_name);
aoqi@0 655 }
aoqi@0 656 _out->cr();
aoqi@0 657 }
aoqi@0 658 break;
aoqi@0 659 }
aoqi@0 660 case T_OBJECT: {
aoqi@0 661 oop value = mirror->obj_field_acquire(fd->offset());
aoqi@0 662 if (value == NULL) {
aoqi@0 663 _out->print_cr("null");
aoqi@0 664 } else if (value->is_instance()) {
aoqi@0 665 if (value->is_a(SystemDictionary::String_klass())) {
aoqi@0 666 _out->print("\"");
aoqi@0 667 _out->print_raw(java_lang_String::as_quoted_ascii(value));
aoqi@0 668 _out->print_cr("\"");
aoqi@0 669 } else {
aoqi@0 670 const char* klass_name = value->klass()->name()->as_quoted_ascii();
aoqi@0 671 _out->print_cr("%s", klass_name);
aoqi@0 672 }
aoqi@0 673 } else {
aoqi@0 674 ShouldNotReachHere();
aoqi@0 675 }
aoqi@0 676 break;
aoqi@0 677 }
aoqi@0 678 default:
aoqi@0 679 ShouldNotReachHere();
aoqi@0 680 }
aoqi@0 681 }
aoqi@0 682 }
aoqi@0 683 };
aoqi@0 684
aoqi@0 685
aoqi@0 686 void ciInstanceKlass::dump_replay_data(outputStream* out) {
aoqi@0 687 ResourceMark rm;
aoqi@0 688
aoqi@0 689 InstanceKlass* ik = get_instanceKlass();
aoqi@0 690 ConstantPool* cp = ik->constants();
aoqi@0 691
aoqi@0 692 // Try to record related loaded classes
aoqi@0 693 Klass* sub = ik->subklass();
aoqi@0 694 while (sub != NULL) {
aoqi@0 695 if (sub->oop_is_instance()) {
aoqi@0 696 out->print_cr("instanceKlass %s", sub->name()->as_quoted_ascii());
aoqi@0 697 }
aoqi@0 698 sub = sub->next_sibling();
aoqi@0 699 }
aoqi@0 700
aoqi@0 701 // Dump out the state of the constant pool tags. During replay the
aoqi@0 702 // tags will be validated for things which shouldn't change and
aoqi@0 703 // classes will be resolved if the tags indicate that they were
aoqi@0 704 // resolved at compile time.
aoqi@0 705 out->print("ciInstanceKlass %s %d %d %d", ik->name()->as_quoted_ascii(),
aoqi@0 706 is_linked(), is_initialized(), cp->length());
aoqi@0 707 for (int index = 1; index < cp->length(); index++) {
aoqi@0 708 out->print(" %d", cp->tags()->at(index));
aoqi@0 709 }
aoqi@0 710 out->cr();
aoqi@0 711 if (is_initialized()) {
aoqi@0 712 // Dump out the static final fields in case the compilation relies
aoqi@0 713 // on their value for correct replay.
aoqi@0 714 StaticFinalFieldPrinter sffp(out, ik->name()->as_quoted_ascii());
aoqi@0 715 ik->do_local_static_fields(&sffp);
aoqi@0 716 }
aoqi@0 717 }

mercurial