src/share/vm/ci/ciInstanceKlass.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6680
78bbf4d43a14
parent 0
f90c822e73f8
child 8856
ac27a9c85bea
permissions
-rw-r--r--

merge

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

mercurial