annotate src/share/vm/oops/constantPoolOop.cpp @ 0:a61af66fc99e

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children a45484ea312d
rev   line source
duke@0 1 /*
duke@0 2 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
duke@0 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@0 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@0 21 * have any questions.
duke@0 22 *
duke@0 23 */
duke@0 24
duke@0 25 # include "incls/_precompiled.incl"
duke@0 26 # include "incls/_constantPoolOop.cpp.incl"
duke@0 27
duke@0 28 klassOop constantPoolOopDesc::klass_at_impl(constantPoolHandle this_oop, int which, TRAPS) {
duke@0 29 // A resolved constantPool entry will contain a klassOop, otherwise a symbolOop.
duke@0 30 // It is not safe to rely on the tag bit's here, since we don't have a lock, and the entry and
duke@0 31 // tag is not updated atomicly.
duke@0 32 oop entry = *(this_oop->obj_at_addr(which));
duke@0 33 if (entry->is_klass()) {
duke@0 34 // Already resolved - return entry.
duke@0 35 return (klassOop)entry;
duke@0 36 }
duke@0 37
duke@0 38 // Acquire lock on constant oop while doing update. After we get the lock, we check if another object
duke@0 39 // already has updated the object
duke@0 40 assert(THREAD->is_Java_thread(), "must be a Java thread");
duke@0 41 bool do_resolve = false;
duke@0 42 bool in_error = false;
duke@0 43
duke@0 44 symbolHandle name;
duke@0 45 Handle loader;
duke@0 46 { ObjectLocker ol(this_oop, THREAD);
duke@0 47
duke@0 48 if (this_oop->tag_at(which).is_unresolved_klass()) {
duke@0 49 if (this_oop->tag_at(which).is_unresolved_klass_in_error()) {
duke@0 50 in_error = true;
duke@0 51 } else {
duke@0 52 do_resolve = true;
duke@0 53 name = symbolHandle(THREAD, this_oop->unresolved_klass_at(which));
duke@0 54 loader = Handle(THREAD, instanceKlass::cast(this_oop->pool_holder())->class_loader());
duke@0 55 }
duke@0 56 }
duke@0 57 } // unlocking constantPool
duke@0 58
duke@0 59
duke@0 60 // The original attempt to resolve this constant pool entry failed so find the
duke@0 61 // original error and throw it again (JVMS 5.4.3).
duke@0 62 if (in_error) {
duke@0 63 symbolOop error = SystemDictionary::find_resolution_error(this_oop, which);
duke@0 64 guarantee(error != (symbolOop)NULL, "tag mismatch with resolution error table");
duke@0 65 ResourceMark rm;
duke@0 66 // exception text will be the class name
duke@0 67 const char* className = this_oop->unresolved_klass_at(which)->as_C_string();
duke@0 68 THROW_MSG_0(error, className);
duke@0 69 }
duke@0 70
duke@0 71 if (do_resolve) {
duke@0 72 // this_oop must be unlocked during resolve_or_fail
duke@0 73 oop protection_domain = Klass::cast(this_oop->pool_holder())->protection_domain();
duke@0 74 Handle h_prot (THREAD, protection_domain);
duke@0 75 klassOop k_oop = SystemDictionary::resolve_or_fail(name, loader, h_prot, true, THREAD);
duke@0 76 KlassHandle k;
duke@0 77 if (!HAS_PENDING_EXCEPTION) {
duke@0 78 k = KlassHandle(THREAD, k_oop);
duke@0 79 // Do access check for klasses
duke@0 80 verify_constant_pool_resolve(this_oop, k, THREAD);
duke@0 81 }
duke@0 82
duke@0 83 // Failed to resolve class. We must record the errors so that subsequent attempts
duke@0 84 // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
duke@0 85 if (HAS_PENDING_EXCEPTION) {
duke@0 86 ResourceMark rm;
duke@0 87 symbolHandle error(PENDING_EXCEPTION->klass()->klass_part()->name());
duke@0 88
duke@0 89 bool throw_orig_error = false;
duke@0 90 {
duke@0 91 ObjectLocker ol (this_oop, THREAD);
duke@0 92
duke@0 93 // some other thread has beaten us and has resolved the class.
duke@0 94 if (this_oop->tag_at(which).is_klass()) {
duke@0 95 CLEAR_PENDING_EXCEPTION;
duke@0 96 entry = this_oop->resolved_klass_at(which);
duke@0 97 return (klassOop)entry;
duke@0 98 }
duke@0 99
duke@0 100 if (!PENDING_EXCEPTION->
duke@0 101 is_a(SystemDictionary::linkageError_klass())) {
duke@0 102 // Just throw the exception and don't prevent these classes from
duke@0 103 // being loaded due to virtual machine errors like StackOverflow
duke@0 104 // and OutOfMemoryError, etc, or if the thread was hit by stop()
duke@0 105 // Needs clarification to section 5.4.3 of the VM spec (see 6308271)
duke@0 106 }
duke@0 107 else if (!this_oop->tag_at(which).is_unresolved_klass_in_error()) {
duke@0 108 SystemDictionary::add_resolution_error(this_oop, which, error);
duke@0 109 this_oop->tag_at_put(which, JVM_CONSTANT_UnresolvedClassInError);
duke@0 110 } else {
duke@0 111 // some other thread has put the class in error state.
duke@0 112 error = symbolHandle(SystemDictionary::find_resolution_error(this_oop, which));
duke@0 113 assert(!error.is_null(), "checking");
duke@0 114 throw_orig_error = true;
duke@0 115 }
duke@0 116 } // unlocked
duke@0 117
duke@0 118 if (throw_orig_error) {
duke@0 119 CLEAR_PENDING_EXCEPTION;
duke@0 120 ResourceMark rm;
duke@0 121 const char* className = this_oop->unresolved_klass_at(which)->as_C_string();
duke@0 122 THROW_MSG_0(error, className);
duke@0 123 }
duke@0 124
duke@0 125 return 0;
duke@0 126 }
duke@0 127
duke@0 128 if (TraceClassResolution && !k()->klass_part()->oop_is_array()) {
duke@0 129 // skip resolving the constant pool so that this code get's
duke@0 130 // called the next time some bytecodes refer to this class.
duke@0 131 ResourceMark rm;
duke@0 132 int line_number = -1;
duke@0 133 const char * source_file = NULL;
duke@0 134 if (JavaThread::current()->has_last_Java_frame()) {
duke@0 135 // try to identify the method which called this function.
duke@0 136 vframeStream vfst(JavaThread::current());
duke@0 137 if (!vfst.at_end()) {
duke@0 138 line_number = vfst.method()->line_number_from_bci(vfst.bci());
duke@0 139 symbolOop s = instanceKlass::cast(vfst.method()->method_holder())->source_file_name();
duke@0 140 if (s != NULL) {
duke@0 141 source_file = s->as_C_string();
duke@0 142 }
duke@0 143 }
duke@0 144 }
duke@0 145 if (k() != this_oop->pool_holder()) {
duke@0 146 // only print something if the classes are different
duke@0 147 if (source_file != NULL) {
duke@0 148 tty->print("RESOLVE %s %s %s:%d\n",
duke@0 149 instanceKlass::cast(this_oop->pool_holder())->external_name(),
duke@0 150 instanceKlass::cast(k())->external_name(), source_file, line_number);
duke@0 151 } else {
duke@0 152 tty->print("RESOLVE %s %s\n",
duke@0 153 instanceKlass::cast(this_oop->pool_holder())->external_name(),
duke@0 154 instanceKlass::cast(k())->external_name());
duke@0 155 }
duke@0 156 }
duke@0 157 return k();
duke@0 158 } else {
duke@0 159 ObjectLocker ol (this_oop, THREAD);
duke@0 160 // Only updated constant pool - if it is resolved.
duke@0 161 do_resolve = this_oop->tag_at(which).is_unresolved_klass();
duke@0 162 if (do_resolve) {
duke@0 163 this_oop->klass_at_put(which, k());
duke@0 164 }
duke@0 165 }
duke@0 166 }
duke@0 167
duke@0 168 entry = this_oop->resolved_klass_at(which);
duke@0 169 assert(entry->is_klass(), "must be resolved at this point");
duke@0 170 return (klassOop)entry;
duke@0 171 }
duke@0 172
duke@0 173
duke@0 174 // Does not update constantPoolOop - to avoid any exception throwing. Used
duke@0 175 // by compiler and exception handling. Also used to avoid classloads for
duke@0 176 // instanceof operations. Returns NULL if the class has not been loaded or
duke@0 177 // if the verification of constant pool failed
duke@0 178 klassOop constantPoolOopDesc::klass_at_if_loaded(constantPoolHandle this_oop, int which) {
duke@0 179 oop entry = *this_oop->obj_at_addr(which);
duke@0 180 if (entry->is_klass()) {
duke@0 181 return (klassOop)entry;
duke@0 182 } else {
duke@0 183 assert(entry->is_symbol(), "must be either symbol or klass");
duke@0 184 Thread *thread = Thread::current();
duke@0 185 symbolHandle name (thread, (symbolOop)entry);
duke@0 186 oop loader = instanceKlass::cast(this_oop->pool_holder())->class_loader();
duke@0 187 oop protection_domain = Klass::cast(this_oop->pool_holder())->protection_domain();
duke@0 188 Handle h_prot (thread, protection_domain);
duke@0 189 Handle h_loader (thread, loader);
duke@0 190 klassOop k = SystemDictionary::find(name, h_loader, h_prot, thread);
duke@0 191
duke@0 192 if (k != NULL) {
duke@0 193 // Make sure that resolving is legal
duke@0 194 EXCEPTION_MARK;
duke@0 195 KlassHandle klass(THREAD, k);
duke@0 196 // return NULL if verification fails
duke@0 197 verify_constant_pool_resolve(this_oop, klass, THREAD);
duke@0 198 if (HAS_PENDING_EXCEPTION) {
duke@0 199 CLEAR_PENDING_EXCEPTION;
duke@0 200 return NULL;
duke@0 201 }
duke@0 202 return klass();
duke@0 203 } else {
duke@0 204 return k;
duke@0 205 }
duke@0 206 }
duke@0 207 }
duke@0 208
duke@0 209
duke@0 210 klassOop constantPoolOopDesc::klass_ref_at_if_loaded(constantPoolHandle this_oop, int which) {
duke@0 211 return klass_at_if_loaded(this_oop, this_oop->klass_ref_index_at(which));
duke@0 212 }
duke@0 213
duke@0 214
duke@0 215 // This is an interface for the compiler that allows accessing non-resolved entries
duke@0 216 // in the constant pool - but still performs the validations tests. Must be used
duke@0 217 // in a pre-parse of the compiler - to determine what it can do and not do.
duke@0 218 // Note: We cannot update the ConstantPool from the vm_thread.
duke@0 219 klassOop constantPoolOopDesc::klass_ref_at_if_loaded_check(constantPoolHandle this_oop, int index, TRAPS) {
duke@0 220 int which = this_oop->klass_ref_index_at(index);
duke@0 221 oop entry = *this_oop->obj_at_addr(which);
duke@0 222 if (entry->is_klass()) {
duke@0 223 return (klassOop)entry;
duke@0 224 } else {
duke@0 225 assert(entry->is_symbol(), "must be either symbol or klass");
duke@0 226 symbolHandle name (THREAD, (symbolOop)entry);
duke@0 227 oop loader = instanceKlass::cast(this_oop->pool_holder())->class_loader();
duke@0 228 oop protection_domain = Klass::cast(this_oop->pool_holder())->protection_domain();
duke@0 229 Handle h_loader(THREAD, loader);
duke@0 230 Handle h_prot (THREAD, protection_domain);
duke@0 231 KlassHandle k(THREAD, SystemDictionary::find(name, h_loader, h_prot, THREAD));
duke@0 232
duke@0 233 // Do access check for klasses
duke@0 234 if( k.not_null() ) verify_constant_pool_resolve(this_oop, k, CHECK_NULL);
duke@0 235 return k();
duke@0 236 }
duke@0 237 }
duke@0 238
duke@0 239
duke@0 240 symbolOop constantPoolOopDesc::uncached_name_ref_at(int which) {
duke@0 241 jint ref_index = name_and_type_at(uncached_name_and_type_ref_index_at(which));
duke@0 242 int name_index = extract_low_short_from_int(ref_index);
duke@0 243 return symbol_at(name_index);
duke@0 244 }
duke@0 245
duke@0 246
duke@0 247 symbolOop constantPoolOopDesc::uncached_signature_ref_at(int which) {
duke@0 248 jint ref_index = name_and_type_at(uncached_name_and_type_ref_index_at(which));
duke@0 249 int signature_index = extract_high_short_from_int(ref_index);
duke@0 250 return symbol_at(signature_index);
duke@0 251 }
duke@0 252
duke@0 253
duke@0 254 int constantPoolOopDesc::uncached_name_and_type_ref_index_at(int which) {
duke@0 255 jint ref_index = field_or_method_at(which, true);
duke@0 256 return extract_high_short_from_int(ref_index);
duke@0 257 }
duke@0 258
duke@0 259
duke@0 260 int constantPoolOopDesc::uncached_klass_ref_index_at(int which) {
duke@0 261 jint ref_index = field_or_method_at(which, true);
duke@0 262 return extract_low_short_from_int(ref_index);
duke@0 263 }
duke@0 264
duke@0 265
duke@0 266 void constantPoolOopDesc::verify_constant_pool_resolve(constantPoolHandle this_oop, KlassHandle k, TRAPS) {
duke@0 267 if (k->oop_is_instance() || k->oop_is_objArray()) {
duke@0 268 instanceKlassHandle holder (THREAD, this_oop->pool_holder());
duke@0 269 klassOop elem_oop = k->oop_is_instance() ? k() : objArrayKlass::cast(k())->bottom_klass();
duke@0 270 KlassHandle element (THREAD, elem_oop);
duke@0 271
duke@0 272 // The element type could be a typeArray - we only need the access check if it is
duke@0 273 // an reference to another class
duke@0 274 if (element->oop_is_instance()) {
duke@0 275 LinkResolver::check_klass_accessability(holder, element, CHECK);
duke@0 276 }
duke@0 277 }
duke@0 278 }
duke@0 279
duke@0 280
duke@0 281 int constantPoolOopDesc::klass_ref_index_at(int which) {
duke@0 282 jint ref_index = field_or_method_at(which, false);
duke@0 283 return extract_low_short_from_int(ref_index);
duke@0 284 }
duke@0 285
duke@0 286
duke@0 287 int constantPoolOopDesc::name_and_type_ref_index_at(int which) {
duke@0 288 jint ref_index = field_or_method_at(which, false);
duke@0 289 return extract_high_short_from_int(ref_index);
duke@0 290 }
duke@0 291
duke@0 292
duke@0 293 int constantPoolOopDesc::name_ref_index_at(int which) {
duke@0 294 jint ref_index = name_and_type_at(which);
duke@0 295 return extract_low_short_from_int(ref_index);
duke@0 296 }
duke@0 297
duke@0 298
duke@0 299 int constantPoolOopDesc::signature_ref_index_at(int which) {
duke@0 300 jint ref_index = name_and_type_at(which);
duke@0 301 return extract_high_short_from_int(ref_index);
duke@0 302 }
duke@0 303
duke@0 304
duke@0 305 klassOop constantPoolOopDesc::klass_ref_at(int which, TRAPS) {
duke@0 306 return klass_at(klass_ref_index_at(which), CHECK_NULL);
duke@0 307 }
duke@0 308
duke@0 309
duke@0 310 symbolOop constantPoolOopDesc::klass_name_at(int which) {
duke@0 311 assert(tag_at(which).is_unresolved_klass() || tag_at(which).is_klass(),
duke@0 312 "Corrupted constant pool");
duke@0 313 // A resolved constantPool entry will contain a klassOop, otherwise a symbolOop.
duke@0 314 // It is not safe to rely on the tag bit's here, since we don't have a lock, and the entry and
duke@0 315 // tag is not updated atomicly.
duke@0 316 oop entry = *(obj_at_addr(which));
duke@0 317 if (entry->is_klass()) {
duke@0 318 // Already resolved - return entry's name.
duke@0 319 return klassOop(entry)->klass_part()->name();
duke@0 320 } else {
duke@0 321 assert(entry->is_symbol(), "must be either symbol or klass");
duke@0 322 return (symbolOop)entry;
duke@0 323 }
duke@0 324 }
duke@0 325
duke@0 326 symbolOop constantPoolOopDesc::klass_ref_at_noresolve(int which) {
duke@0 327 jint ref_index = klass_ref_index_at(which);
duke@0 328 return klass_at_noresolve(ref_index);
duke@0 329 }
duke@0 330
duke@0 331 char* constantPoolOopDesc::string_at_noresolve(int which) {
duke@0 332 // Test entry type in case string is resolved while in here.
duke@0 333 oop entry = *(obj_at_addr(which));
duke@0 334 if (entry->is_symbol()) {
duke@0 335 return ((symbolOop)entry)->as_C_string();
duke@0 336 } else {
duke@0 337 return java_lang_String::as_utf8_string(entry);
duke@0 338 }
duke@0 339 }
duke@0 340
duke@0 341
duke@0 342 symbolOop constantPoolOopDesc::name_ref_at(int which) {
duke@0 343 jint ref_index = name_and_type_at(name_and_type_ref_index_at(which));
duke@0 344 int name_index = extract_low_short_from_int(ref_index);
duke@0 345 return symbol_at(name_index);
duke@0 346 }
duke@0 347
duke@0 348
duke@0 349 symbolOop constantPoolOopDesc::signature_ref_at(int which) {
duke@0 350 jint ref_index = name_and_type_at(name_and_type_ref_index_at(which));
duke@0 351 int signature_index = extract_high_short_from_int(ref_index);
duke@0 352 return symbol_at(signature_index);
duke@0 353 }
duke@0 354
duke@0 355
duke@0 356 BasicType constantPoolOopDesc::basic_type_for_signature_at(int which) {
duke@0 357 return FieldType::basic_type(symbol_at(which));
duke@0 358 }
duke@0 359
duke@0 360
duke@0 361 void constantPoolOopDesc::resolve_string_constants_impl(constantPoolHandle this_oop, TRAPS) {
duke@0 362 for (int index = 1; index < this_oop->length(); index++) { // Index 0 is unused
duke@0 363 if (this_oop->tag_at(index).is_unresolved_string()) {
duke@0 364 this_oop->string_at(index, CHECK);
duke@0 365 }
duke@0 366 }
duke@0 367 }
duke@0 368
duke@0 369 oop constantPoolOopDesc::string_at_impl(constantPoolHandle this_oop, int which, TRAPS) {
duke@0 370 oop entry = *(this_oop->obj_at_addr(which));
duke@0 371 if (entry->is_symbol()) {
duke@0 372 ObjectLocker ol(this_oop, THREAD);
duke@0 373 if (this_oop->tag_at(which).is_unresolved_string()) {
duke@0 374 // Intern string
duke@0 375 symbolOop sym = this_oop->unresolved_string_at(which);
duke@0 376 entry = StringTable::intern(sym, CHECK_(constantPoolOop(NULL)));
duke@0 377 this_oop->string_at_put(which, entry);
duke@0 378 } else {
duke@0 379 // Another thread beat us and interned string, read string from constant pool
duke@0 380 entry = this_oop->resolved_string_at(which);
duke@0 381 }
duke@0 382 }
duke@0 383 assert(java_lang_String::is_instance(entry), "must be string");
duke@0 384 return entry;
duke@0 385 }
duke@0 386
duke@0 387
duke@0 388 bool constantPoolOopDesc::klass_name_at_matches(instanceKlassHandle k,
duke@0 389 int which) {
duke@0 390 // Names are interned, so we can compare symbolOops directly
duke@0 391 symbolOop cp_name = klass_name_at(which);
duke@0 392 return (cp_name == k->name());
duke@0 393 }
duke@0 394
duke@0 395
duke@0 396 int constantPoolOopDesc::pre_resolve_shared_klasses(TRAPS) {
duke@0 397 ResourceMark rm;
duke@0 398 int count = 0;
duke@0 399 for (int index = 1; index < tags()->length(); index++) { // Index 0 is unused
duke@0 400 if (tag_at(index).is_unresolved_string()) {
duke@0 401 // Intern string
duke@0 402 symbolOop sym = unresolved_string_at(index);
duke@0 403 oop entry = StringTable::intern(sym, CHECK_(-1));
duke@0 404 string_at_put(index, entry);
duke@0 405 }
duke@0 406 }
duke@0 407 return count;
duke@0 408 }
duke@0 409
duke@0 410
duke@0 411 // Iterate over symbols which are used as class, field, method names and
duke@0 412 // signatures (in preparation for writing to the shared archive).
duke@0 413
duke@0 414 void constantPoolOopDesc::shared_symbols_iterate(OopClosure* closure) {
duke@0 415 for (int index = 1; index < length(); index++) { // Index 0 is unused
duke@0 416 switch (tag_at(index).value()) {
duke@0 417
duke@0 418 case JVM_CONSTANT_UnresolvedClass:
duke@0 419 closure->do_oop(obj_at_addr(index));
duke@0 420 break;
duke@0 421
duke@0 422 case JVM_CONSTANT_NameAndType:
duke@0 423 {
duke@0 424 int i = *int_at_addr(index);
duke@0 425 closure->do_oop(obj_at_addr((unsigned)i >> 16));
duke@0 426 closure->do_oop(obj_at_addr((unsigned)i & 0xffff));
duke@0 427 }
duke@0 428 break;
duke@0 429
duke@0 430 case JVM_CONSTANT_Class:
duke@0 431 case JVM_CONSTANT_InterfaceMethodref:
duke@0 432 case JVM_CONSTANT_Fieldref:
duke@0 433 case JVM_CONSTANT_Methodref:
duke@0 434 case JVM_CONSTANT_Integer:
duke@0 435 case JVM_CONSTANT_Float:
duke@0 436 // Do nothing! Not an oop.
duke@0 437 // These constant types do not reference symbols at this point.
duke@0 438 break;
duke@0 439
duke@0 440 case JVM_CONSTANT_String:
duke@0 441 // Do nothing! Not a symbol.
duke@0 442 break;
duke@0 443
duke@0 444 case JVM_CONSTANT_UnresolvedString:
duke@0 445 case JVM_CONSTANT_Utf8:
duke@0 446 // These constants are symbols, but unless these symbols are
duke@0 447 // actually to be used for something, we don't want to mark them.
duke@0 448 break;
duke@0 449
duke@0 450 case JVM_CONSTANT_Long:
duke@0 451 case JVM_CONSTANT_Double:
duke@0 452 // Do nothing! Not an oop. (But takes two pool entries.)
duke@0 453 ++index;
duke@0 454 break;
duke@0 455
duke@0 456 default:
duke@0 457 ShouldNotReachHere();
duke@0 458 break;
duke@0 459 }
duke@0 460 }
duke@0 461 }
duke@0 462
duke@0 463
duke@0 464 // Iterate over the [one] tags array (in preparation for writing to the
duke@0 465 // shared archive).
duke@0 466
duke@0 467 void constantPoolOopDesc::shared_tags_iterate(OopClosure* closure) {
duke@0 468 closure->do_oop(tags_addr());
duke@0 469 }
duke@0 470
duke@0 471
duke@0 472 // Iterate over String objects (in preparation for writing to the shared
duke@0 473 // archive).
duke@0 474
duke@0 475 void constantPoolOopDesc::shared_strings_iterate(OopClosure* closure) {
duke@0 476 for (int index = 1; index < length(); index++) { // Index 0 is unused
duke@0 477 switch (tag_at(index).value()) {
duke@0 478
duke@0 479 case JVM_CONSTANT_UnresolvedClass:
duke@0 480 case JVM_CONSTANT_NameAndType:
duke@0 481 // Do nothing! Not a String.
duke@0 482 break;
duke@0 483
duke@0 484 case JVM_CONSTANT_Class:
duke@0 485 case JVM_CONSTANT_InterfaceMethodref:
duke@0 486 case JVM_CONSTANT_Fieldref:
duke@0 487 case JVM_CONSTANT_Methodref:
duke@0 488 case JVM_CONSTANT_Integer:
duke@0 489 case JVM_CONSTANT_Float:
duke@0 490 // Do nothing! Not an oop.
duke@0 491 // These constant types do not reference symbols at this point.
duke@0 492 break;
duke@0 493
duke@0 494 case JVM_CONSTANT_String:
duke@0 495 closure->do_oop(obj_at_addr(index));
duke@0 496 break;
duke@0 497
duke@0 498 case JVM_CONSTANT_UnresolvedString:
duke@0 499 case JVM_CONSTANT_Utf8:
duke@0 500 // These constants are symbols, but unless these symbols are
duke@0 501 // actually to be used for something, we don't want to mark them.
duke@0 502 break;
duke@0 503
duke@0 504 case JVM_CONSTANT_Long:
duke@0 505 case JVM_CONSTANT_Double:
duke@0 506 // Do nothing! Not an oop. (But takes two pool entries.)
duke@0 507 ++index;
duke@0 508 break;
duke@0 509
duke@0 510 default:
duke@0 511 ShouldNotReachHere();
duke@0 512 break;
duke@0 513 }
duke@0 514 }
duke@0 515 }
duke@0 516
duke@0 517
duke@0 518 // Compare this constant pool's entry at index1 to the constant pool
duke@0 519 // cp2's entry at index2.
duke@0 520 bool constantPoolOopDesc::compare_entry_to(int index1, constantPoolHandle cp2,
duke@0 521 int index2, TRAPS) {
duke@0 522
duke@0 523 jbyte t1 = tag_at(index1).value();
duke@0 524 jbyte t2 = cp2->tag_at(index2).value();
duke@0 525
duke@0 526
duke@0 527 // JVM_CONSTANT_UnresolvedClassInError is equal to JVM_CONSTANT_UnresolvedClass
duke@0 528 // when comparing
duke@0 529 if (t1 == JVM_CONSTANT_UnresolvedClassInError) {
duke@0 530 t1 = JVM_CONSTANT_UnresolvedClass;
duke@0 531 }
duke@0 532 if (t2 == JVM_CONSTANT_UnresolvedClassInError) {
duke@0 533 t2 = JVM_CONSTANT_UnresolvedClass;
duke@0 534 }
duke@0 535
duke@0 536 if (t1 != t2) {
duke@0 537 // Not the same entry type so there is nothing else to check. Note
duke@0 538 // that this style of checking will consider resolved/unresolved
duke@0 539 // class pairs and resolved/unresolved string pairs as different.
duke@0 540 // From the constantPoolOop API point of view, this is correct
duke@0 541 // behavior. See constantPoolKlass::merge() to see how this plays
duke@0 542 // out in the context of constantPoolOop merging.
duke@0 543 return false;
duke@0 544 }
duke@0 545
duke@0 546 switch (t1) {
duke@0 547 case JVM_CONSTANT_Class:
duke@0 548 {
duke@0 549 klassOop k1 = klass_at(index1, CHECK_false);
duke@0 550 klassOop k2 = cp2->klass_at(index2, CHECK_false);
duke@0 551 if (k1 == k2) {
duke@0 552 return true;
duke@0 553 }
duke@0 554 } break;
duke@0 555
duke@0 556 case JVM_CONSTANT_ClassIndex:
duke@0 557 {
duke@0 558 int recur1 = klass_index_at(index1);
duke@0 559 int recur2 = cp2->klass_index_at(index2);
duke@0 560 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
duke@0 561 if (match) {
duke@0 562 return true;
duke@0 563 }
duke@0 564 } break;
duke@0 565
duke@0 566 case JVM_CONSTANT_Double:
duke@0 567 {
duke@0 568 jdouble d1 = double_at(index1);
duke@0 569 jdouble d2 = cp2->double_at(index2);
duke@0 570 if (d1 == d2) {
duke@0 571 return true;
duke@0 572 }
duke@0 573 } break;
duke@0 574
duke@0 575 case JVM_CONSTANT_Fieldref:
duke@0 576 case JVM_CONSTANT_InterfaceMethodref:
duke@0 577 case JVM_CONSTANT_Methodref:
duke@0 578 {
duke@0 579 int recur1 = uncached_klass_ref_index_at(index1);
duke@0 580 int recur2 = cp2->uncached_klass_ref_index_at(index2);
duke@0 581 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
duke@0 582 if (match) {
duke@0 583 recur1 = uncached_name_and_type_ref_index_at(index1);
duke@0 584 recur2 = cp2->uncached_name_and_type_ref_index_at(index2);
duke@0 585 match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
duke@0 586 if (match) {
duke@0 587 return true;
duke@0 588 }
duke@0 589 }
duke@0 590 } break;
duke@0 591
duke@0 592 case JVM_CONSTANT_Float:
duke@0 593 {
duke@0 594 jfloat f1 = float_at(index1);
duke@0 595 jfloat f2 = cp2->float_at(index2);
duke@0 596 if (f1 == f2) {
duke@0 597 return true;
duke@0 598 }
duke@0 599 } break;
duke@0 600
duke@0 601 case JVM_CONSTANT_Integer:
duke@0 602 {
duke@0 603 jint i1 = int_at(index1);
duke@0 604 jint i2 = cp2->int_at(index2);
duke@0 605 if (i1 == i2) {
duke@0 606 return true;
duke@0 607 }
duke@0 608 } break;
duke@0 609
duke@0 610 case JVM_CONSTANT_Long:
duke@0 611 {
duke@0 612 jlong l1 = long_at(index1);
duke@0 613 jlong l2 = cp2->long_at(index2);
duke@0 614 if (l1 == l2) {
duke@0 615 return true;
duke@0 616 }
duke@0 617 } break;
duke@0 618
duke@0 619 case JVM_CONSTANT_NameAndType:
duke@0 620 {
duke@0 621 int recur1 = name_ref_index_at(index1);
duke@0 622 int recur2 = cp2->name_ref_index_at(index2);
duke@0 623 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
duke@0 624 if (match) {
duke@0 625 recur1 = signature_ref_index_at(index1);
duke@0 626 recur2 = cp2->signature_ref_index_at(index2);
duke@0 627 match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
duke@0 628 if (match) {
duke@0 629 return true;
duke@0 630 }
duke@0 631 }
duke@0 632 } break;
duke@0 633
duke@0 634 case JVM_CONSTANT_String:
duke@0 635 {
duke@0 636 oop s1 = string_at(index1, CHECK_false);
duke@0 637 oop s2 = cp2->string_at(index2, CHECK_false);
duke@0 638 if (s1 == s2) {
duke@0 639 return true;
duke@0 640 }
duke@0 641 } break;
duke@0 642
duke@0 643 case JVM_CONSTANT_StringIndex:
duke@0 644 {
duke@0 645 int recur1 = string_index_at(index1);
duke@0 646 int recur2 = cp2->string_index_at(index2);
duke@0 647 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
duke@0 648 if (match) {
duke@0 649 return true;
duke@0 650 }
duke@0 651 } break;
duke@0 652
duke@0 653 case JVM_CONSTANT_UnresolvedClass:
duke@0 654 {
duke@0 655 symbolOop k1 = unresolved_klass_at(index1);
duke@0 656 symbolOop k2 = cp2->unresolved_klass_at(index2);
duke@0 657 if (k1 == k2) {
duke@0 658 return true;
duke@0 659 }
duke@0 660 } break;
duke@0 661
duke@0 662 case JVM_CONSTANT_UnresolvedString:
duke@0 663 {
duke@0 664 symbolOop s1 = unresolved_string_at(index1);
duke@0 665 symbolOop s2 = cp2->unresolved_string_at(index2);
duke@0 666 if (s1 == s2) {
duke@0 667 return true;
duke@0 668 }
duke@0 669 } break;
duke@0 670
duke@0 671 case JVM_CONSTANT_Utf8:
duke@0 672 {
duke@0 673 symbolOop s1 = symbol_at(index1);
duke@0 674 symbolOop s2 = cp2->symbol_at(index2);
duke@0 675 if (s1 == s2) {
duke@0 676 return true;
duke@0 677 }
duke@0 678 } break;
duke@0 679
duke@0 680 // Invalid is used as the tag for the second constant pool entry
duke@0 681 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
duke@0 682 // not be seen by itself.
duke@0 683 case JVM_CONSTANT_Invalid: // fall through
duke@0 684
duke@0 685 default:
duke@0 686 ShouldNotReachHere();
duke@0 687 break;
duke@0 688 }
duke@0 689
duke@0 690 return false;
duke@0 691 } // end compare_entry_to()
duke@0 692
duke@0 693
duke@0 694 // Copy this constant pool's entries at start_i to end_i (inclusive)
duke@0 695 // to the constant pool to_cp's entries starting at to_i. A total of
duke@0 696 // (end_i - start_i) + 1 entries are copied.
duke@0 697 void constantPoolOopDesc::copy_cp_to(int start_i, int end_i,
duke@0 698 constantPoolHandle to_cp, int to_i, TRAPS) {
duke@0 699
duke@0 700 int dest_i = to_i; // leave original alone for debug purposes
duke@0 701
duke@0 702 for (int src_i = start_i; src_i <= end_i; /* see loop bottom */ ) {
duke@0 703 copy_entry_to(src_i, to_cp, dest_i, CHECK);
duke@0 704
duke@0 705 switch (tag_at(src_i).value()) {
duke@0 706 case JVM_CONSTANT_Double:
duke@0 707 case JVM_CONSTANT_Long:
duke@0 708 // double and long take two constant pool entries
duke@0 709 src_i += 2;
duke@0 710 dest_i += 2;
duke@0 711 break;
duke@0 712
duke@0 713 default:
duke@0 714 // all others take one constant pool entry
duke@0 715 src_i++;
duke@0 716 dest_i++;
duke@0 717 break;
duke@0 718 }
duke@0 719 }
duke@0 720 } // end copy_cp_to()
duke@0 721
duke@0 722
duke@0 723 // Copy this constant pool's entry at from_i to the constant pool
duke@0 724 // to_cp's entry at to_i.
duke@0 725 void constantPoolOopDesc::copy_entry_to(int from_i, constantPoolHandle to_cp,
duke@0 726 int to_i, TRAPS) {
duke@0 727
duke@0 728 switch (tag_at(from_i).value()) {
duke@0 729 case JVM_CONSTANT_Class:
duke@0 730 {
duke@0 731 klassOop k = klass_at(from_i, CHECK);
duke@0 732 to_cp->klass_at_put(to_i, k);
duke@0 733 } break;
duke@0 734
duke@0 735 case JVM_CONSTANT_ClassIndex:
duke@0 736 {
duke@0 737 jint ki = klass_index_at(from_i);
duke@0 738 to_cp->klass_index_at_put(to_i, ki);
duke@0 739 } break;
duke@0 740
duke@0 741 case JVM_CONSTANT_Double:
duke@0 742 {
duke@0 743 jdouble d = double_at(from_i);
duke@0 744 to_cp->double_at_put(to_i, d);
duke@0 745 // double takes two constant pool entries so init second entry's tag
duke@0 746 to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
duke@0 747 } break;
duke@0 748
duke@0 749 case JVM_CONSTANT_Fieldref:
duke@0 750 {
duke@0 751 int class_index = uncached_klass_ref_index_at(from_i);
duke@0 752 int name_and_type_index = uncached_name_and_type_ref_index_at(from_i);
duke@0 753 to_cp->field_at_put(to_i, class_index, name_and_type_index);
duke@0 754 } break;
duke@0 755
duke@0 756 case JVM_CONSTANT_Float:
duke@0 757 {
duke@0 758 jfloat f = float_at(from_i);
duke@0 759 to_cp->float_at_put(to_i, f);
duke@0 760 } break;
duke@0 761
duke@0 762 case JVM_CONSTANT_Integer:
duke@0 763 {
duke@0 764 jint i = int_at(from_i);
duke@0 765 to_cp->int_at_put(to_i, i);
duke@0 766 } break;
duke@0 767
duke@0 768 case JVM_CONSTANT_InterfaceMethodref:
duke@0 769 {
duke@0 770 int class_index = uncached_klass_ref_index_at(from_i);
duke@0 771 int name_and_type_index = uncached_name_and_type_ref_index_at(from_i);
duke@0 772 to_cp->interface_method_at_put(to_i, class_index, name_and_type_index);
duke@0 773 } break;
duke@0 774
duke@0 775 case JVM_CONSTANT_Long:
duke@0 776 {
duke@0 777 jlong l = long_at(from_i);
duke@0 778 to_cp->long_at_put(to_i, l);
duke@0 779 // long takes two constant pool entries so init second entry's tag
duke@0 780 to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
duke@0 781 } break;
duke@0 782
duke@0 783 case JVM_CONSTANT_Methodref:
duke@0 784 {
duke@0 785 int class_index = uncached_klass_ref_index_at(from_i);
duke@0 786 int name_and_type_index = uncached_name_and_type_ref_index_at(from_i);
duke@0 787 to_cp->method_at_put(to_i, class_index, name_and_type_index);
duke@0 788 } break;
duke@0 789
duke@0 790 case JVM_CONSTANT_NameAndType:
duke@0 791 {
duke@0 792 int name_ref_index = name_ref_index_at(from_i);
duke@0 793 int signature_ref_index = signature_ref_index_at(from_i);
duke@0 794 to_cp->name_and_type_at_put(to_i, name_ref_index, signature_ref_index);
duke@0 795 } break;
duke@0 796
duke@0 797 case JVM_CONSTANT_String:
duke@0 798 {
duke@0 799 oop s = string_at(from_i, CHECK);
duke@0 800 to_cp->string_at_put(to_i, s);
duke@0 801 } break;
duke@0 802
duke@0 803 case JVM_CONSTANT_StringIndex:
duke@0 804 {
duke@0 805 jint si = string_index_at(from_i);
duke@0 806 to_cp->string_index_at_put(to_i, si);
duke@0 807 } break;
duke@0 808
duke@0 809 case JVM_CONSTANT_UnresolvedClass:
duke@0 810 {
duke@0 811 symbolOop k = unresolved_klass_at(from_i);
duke@0 812 to_cp->unresolved_klass_at_put(to_i, k);
duke@0 813 } break;
duke@0 814
duke@0 815 case JVM_CONSTANT_UnresolvedClassInError:
duke@0 816 {
duke@0 817 symbolOop k = unresolved_klass_at(from_i);
duke@0 818 to_cp->unresolved_klass_at_put(to_i, k);
duke@0 819 to_cp->tag_at_put(to_i, JVM_CONSTANT_UnresolvedClassInError);
duke@0 820 } break;
duke@0 821
duke@0 822
duke@0 823 case JVM_CONSTANT_UnresolvedString:
duke@0 824 {
duke@0 825 symbolOop s = unresolved_string_at(from_i);
duke@0 826 to_cp->unresolved_string_at_put(to_i, s);
duke@0 827 } break;
duke@0 828
duke@0 829 case JVM_CONSTANT_Utf8:
duke@0 830 {
duke@0 831 symbolOop s = symbol_at(from_i);
duke@0 832 to_cp->symbol_at_put(to_i, s);
duke@0 833 } break;
duke@0 834
duke@0 835 // Invalid is used as the tag for the second constant pool entry
duke@0 836 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
duke@0 837 // not be seen by itself.
duke@0 838 case JVM_CONSTANT_Invalid: // fall through
duke@0 839
duke@0 840 default:
duke@0 841 {
duke@0 842 jbyte bad_value = tag_at(from_i).value(); // leave a breadcrumb
duke@0 843 ShouldNotReachHere();
duke@0 844 } break;
duke@0 845 }
duke@0 846 } // end copy_entry_to()
duke@0 847
duke@0 848
duke@0 849 // Search constant pool search_cp for an entry that matches this
duke@0 850 // constant pool's entry at pattern_i. Returns the index of a
duke@0 851 // matching entry or zero (0) if there is no matching entry.
duke@0 852 int constantPoolOopDesc::find_matching_entry(int pattern_i,
duke@0 853 constantPoolHandle search_cp, TRAPS) {
duke@0 854
duke@0 855 // index zero (0) is not used
duke@0 856 for (int i = 1; i < search_cp->length(); i++) {
duke@0 857 bool found = compare_entry_to(pattern_i, search_cp, i, CHECK_0);
duke@0 858 if (found) {
duke@0 859 return i;
duke@0 860 }
duke@0 861 }
duke@0 862
duke@0 863 return 0; // entry not found; return unused index zero (0)
duke@0 864 } // end find_matching_entry()
duke@0 865
duke@0 866
duke@0 867 #ifndef PRODUCT
duke@0 868
duke@0 869 const char* constantPoolOopDesc::printable_name_at(int which) {
duke@0 870
duke@0 871 constantTag tag = tag_at(which);
duke@0 872
duke@0 873 if (tag.is_unresolved_string() || tag.is_string()) {
duke@0 874 return string_at_noresolve(which);
duke@0 875 } else if (tag.is_klass() || tag.is_unresolved_klass()) {
duke@0 876 return klass_name_at(which)->as_C_string();
duke@0 877 } else if (tag.is_symbol()) {
duke@0 878 return symbol_at(which)->as_C_string();
duke@0 879 }
duke@0 880 return "";
duke@0 881 }
duke@0 882
duke@0 883 #endif // PRODUCT
duke@0 884
duke@0 885
duke@0 886 // JVMTI GetConstantPool support
duke@0 887
duke@0 888 // For temporary use until code is stable.
duke@0 889 #define DBG(code)
duke@0 890
duke@0 891 static const char* WARN_MSG = "Must not be such entry!";
duke@0 892
duke@0 893 static void print_cpool_bytes(jint cnt, u1 *bytes) {
duke@0 894 jint size = 0;
duke@0 895 u2 idx1, idx2;
duke@0 896
duke@0 897 for (jint idx = 1; idx < cnt; idx++) {
duke@0 898 jint ent_size = 0;
duke@0 899 u1 tag = *bytes++;
duke@0 900 size++; // count tag
duke@0 901
duke@0 902 printf("const #%03d, tag: %02d ", idx, tag);
duke@0 903 switch(tag) {
duke@0 904 case JVM_CONSTANT_Invalid: {
duke@0 905 printf("Invalid");
duke@0 906 break;
duke@0 907 }
duke@0 908 case JVM_CONSTANT_Unicode: {
duke@0 909 printf("Unicode %s", WARN_MSG);
duke@0 910 break;
duke@0 911 }
duke@0 912 case JVM_CONSTANT_Utf8: {
duke@0 913 u2 len = Bytes::get_Java_u2(bytes);
duke@0 914 char str[128];
duke@0 915 if (len > 127) {
duke@0 916 len = 127;
duke@0 917 }
duke@0 918 strncpy(str, (char *) (bytes+2), len);
duke@0 919 str[len] = '\0';
duke@0 920 printf("Utf8 \"%s\"", str);
duke@0 921 ent_size = 2 + len;
duke@0 922 break;
duke@0 923 }
duke@0 924 case JVM_CONSTANT_Integer: {
duke@0 925 u4 val = Bytes::get_Java_u4(bytes);
duke@0 926 printf("int %d", *(int *) &val);
duke@0 927 ent_size = 4;
duke@0 928 break;
duke@0 929 }
duke@0 930 case JVM_CONSTANT_Float: {
duke@0 931 u4 val = Bytes::get_Java_u4(bytes);
duke@0 932 printf("float %5.3ff", *(float *) &val);
duke@0 933 ent_size = 4;
duke@0 934 break;
duke@0 935 }
duke@0 936 case JVM_CONSTANT_Long: {
duke@0 937 u8 val = Bytes::get_Java_u8(bytes);
duke@0 938 printf("long %lldl", *(jlong *) &val);
duke@0 939 ent_size = 8;
duke@0 940 idx++; // Long takes two cpool slots
duke@0 941 break;
duke@0 942 }
duke@0 943 case JVM_CONSTANT_Double: {
duke@0 944 u8 val = Bytes::get_Java_u8(bytes);
duke@0 945 printf("double %5.3fd", *(jdouble *)&val);
duke@0 946 ent_size = 8;
duke@0 947 idx++; // Double takes two cpool slots
duke@0 948 break;
duke@0 949 }
duke@0 950 case JVM_CONSTANT_Class: {
duke@0 951 idx1 = Bytes::get_Java_u2(bytes);
duke@0 952 printf("class #%03d", idx1);
duke@0 953 ent_size = 2;
duke@0 954 break;
duke@0 955 }
duke@0 956 case JVM_CONSTANT_String: {
duke@0 957 idx1 = Bytes::get_Java_u2(bytes);
duke@0 958 printf("String #%03d", idx1);
duke@0 959 ent_size = 2;
duke@0 960 break;
duke@0 961 }
duke@0 962 case JVM_CONSTANT_Fieldref: {
duke@0 963 idx1 = Bytes::get_Java_u2(bytes);
duke@0 964 idx2 = Bytes::get_Java_u2(bytes+2);
duke@0 965 printf("Field #%03d, #%03d", (int) idx1, (int) idx2);
duke@0 966 ent_size = 4;
duke@0 967 break;
duke@0 968 }
duke@0 969 case JVM_CONSTANT_Methodref: {
duke@0 970 idx1 = Bytes::get_Java_u2(bytes);
duke@0 971 idx2 = Bytes::get_Java_u2(bytes+2);
duke@0 972 printf("Method #%03d, #%03d", idx1, idx2);
duke@0 973 ent_size = 4;
duke@0 974 break;
duke@0 975 }
duke@0 976 case JVM_CONSTANT_InterfaceMethodref: {
duke@0 977 idx1 = Bytes::get_Java_u2(bytes);
duke@0 978 idx2 = Bytes::get_Java_u2(bytes+2);
duke@0 979 printf("InterfMethod #%03d, #%03d", idx1, idx2);
duke@0 980 ent_size = 4;
duke@0 981 break;
duke@0 982 }
duke@0 983 case JVM_CONSTANT_NameAndType: {
duke@0 984 idx1 = Bytes::get_Java_u2(bytes);
duke@0 985 idx2 = Bytes::get_Java_u2(bytes+2);
duke@0 986 printf("NameAndType #%03d, #%03d", idx1, idx2);
duke@0 987 ent_size = 4;
duke@0 988 break;
duke@0 989 }
duke@0 990 case JVM_CONSTANT_ClassIndex: {
duke@0 991 printf("ClassIndex %s", WARN_MSG);
duke@0 992 break;
duke@0 993 }
duke@0 994 case JVM_CONSTANT_UnresolvedClass: {
duke@0 995 printf("UnresolvedClass: %s", WARN_MSG);
duke@0 996 break;
duke@0 997 }
duke@0 998 case JVM_CONSTANT_UnresolvedClassInError: {
duke@0 999 printf("UnresolvedClassInErr: %s", WARN_MSG);
duke@0 1000 break;
duke@0 1001 }
duke@0 1002 case JVM_CONSTANT_StringIndex: {
duke@0 1003 printf("StringIndex: %s", WARN_MSG);
duke@0 1004 break;
duke@0 1005 }
duke@0 1006 case JVM_CONSTANT_UnresolvedString: {
duke@0 1007 printf("UnresolvedString: %s", WARN_MSG);
duke@0 1008 break;
duke@0 1009 }
duke@0 1010 }
duke@0 1011 printf(";\n");
duke@0 1012 bytes += ent_size;
duke@0 1013 size += ent_size;
duke@0 1014 }
duke@0 1015 printf("Cpool size: %d\n", size);
duke@0 1016 fflush(0);
duke@0 1017 return;
duke@0 1018 } /* end print_cpool_bytes */
duke@0 1019
duke@0 1020
duke@0 1021 // Returns size of constant pool entry.
duke@0 1022 jint constantPoolOopDesc::cpool_entry_size(jint idx) {
duke@0 1023 switch(tag_at(idx).value()) {
duke@0 1024 case JVM_CONSTANT_Invalid:
duke@0 1025 case JVM_CONSTANT_Unicode:
duke@0 1026 return 1;
duke@0 1027
duke@0 1028 case JVM_CONSTANT_Utf8:
duke@0 1029 return 3 + symbol_at(idx)->utf8_length();
duke@0 1030
duke@0 1031 case JVM_CONSTANT_Class:
duke@0 1032 case JVM_CONSTANT_String:
duke@0 1033 case JVM_CONSTANT_ClassIndex:
duke@0 1034 case JVM_CONSTANT_UnresolvedClass:
duke@0 1035 case JVM_CONSTANT_UnresolvedClassInError:
duke@0 1036 case JVM_CONSTANT_StringIndex:
duke@0 1037 case JVM_CONSTANT_UnresolvedString:
duke@0 1038 return 3;
duke@0 1039
duke@0 1040 case JVM_CONSTANT_Integer:
duke@0 1041 case JVM_CONSTANT_Float:
duke@0 1042 case JVM_CONSTANT_Fieldref:
duke@0 1043 case JVM_CONSTANT_Methodref:
duke@0 1044 case JVM_CONSTANT_InterfaceMethodref:
duke@0 1045 case JVM_CONSTANT_NameAndType:
duke@0 1046 return 5;
duke@0 1047
duke@0 1048 case JVM_CONSTANT_Long:
duke@0 1049 case JVM_CONSTANT_Double:
duke@0 1050 return 9;
duke@0 1051 }
duke@0 1052 assert(false, "cpool_entry_size: Invalid constant pool entry tag");
duke@0 1053 return 1;
duke@0 1054 } /* end cpool_entry_size */
duke@0 1055
duke@0 1056
duke@0 1057 // SymbolHashMap is used to find a constant pool index from a string.
duke@0 1058 // This function fills in SymbolHashMaps, one for utf8s and one for
duke@0 1059 // class names, returns size of the cpool raw bytes.
duke@0 1060 jint constantPoolOopDesc::hash_entries_to(SymbolHashMap *symmap,
duke@0 1061 SymbolHashMap *classmap) {
duke@0 1062 jint size = 0;
duke@0 1063
duke@0 1064 for (u2 idx = 1; idx < length(); idx++) {
duke@0 1065 u2 tag = tag_at(idx).value();
duke@0 1066 size += cpool_entry_size(idx);
duke@0 1067
duke@0 1068 switch(tag) {
duke@0 1069 case JVM_CONSTANT_Utf8: {
duke@0 1070 symbolOop sym = symbol_at(idx);
duke@0 1071 symmap->add_entry(sym, idx);
duke@0 1072 DBG(printf("adding symbol entry %s = %d\n", sym->as_utf8(), idx));
duke@0 1073 break;
duke@0 1074 }
duke@0 1075 case JVM_CONSTANT_Class:
duke@0 1076 case JVM_CONSTANT_UnresolvedClass:
duke@0 1077 case JVM_CONSTANT_UnresolvedClassInError: {
duke@0 1078 symbolOop sym = klass_name_at(idx);
duke@0 1079 classmap->add_entry(sym, idx);
duke@0 1080 DBG(printf("adding class entry %s = %d\n", sym->as_utf8(), idx));
duke@0 1081 break;
duke@0 1082 }
duke@0 1083 case JVM_CONSTANT_Long:
duke@0 1084 case JVM_CONSTANT_Double: {
duke@0 1085 idx++; // Both Long and Double take two cpool slots
duke@0 1086 break;
duke@0 1087 }
duke@0 1088 }
duke@0 1089 }
duke@0 1090 return size;
duke@0 1091 } /* end hash_utf8_entries_to */
duke@0 1092
duke@0 1093
duke@0 1094 // Copy cpool bytes.
duke@0 1095 // Returns:
duke@0 1096 // 0, in case of OutOfMemoryError
duke@0 1097 // -1, in case of internal error
duke@0 1098 // > 0, count of the raw cpool bytes that have been copied
duke@0 1099 int constantPoolOopDesc::copy_cpool_bytes(int cpool_size,
duke@0 1100 SymbolHashMap* tbl,
duke@0 1101 unsigned char *bytes) {
duke@0 1102 u2 idx1, idx2;
duke@0 1103 jint size = 0;
duke@0 1104 jint cnt = length();
duke@0 1105 unsigned char *start_bytes = bytes;
duke@0 1106
duke@0 1107 for (jint idx = 1; idx < cnt; idx++) {
duke@0 1108 u1 tag = tag_at(idx).value();
duke@0 1109 jint ent_size = cpool_entry_size(idx);
duke@0 1110
duke@0 1111 assert(size + ent_size <= cpool_size, "Size mismatch");
duke@0 1112
duke@0 1113 *bytes = tag;
duke@0 1114 DBG(printf("#%03hd tag=%03hd, ", idx, tag));
duke@0 1115 switch(tag) {
duke@0 1116 case JVM_CONSTANT_Invalid: {
duke@0 1117 DBG(printf("JVM_CONSTANT_Invalid"));
duke@0 1118 break;
duke@0 1119 }
duke@0 1120 case JVM_CONSTANT_Unicode: {
duke@0 1121 assert(false, "Wrong constant pool tag: JVM_CONSTANT_Unicode");
duke@0 1122 DBG(printf("JVM_CONSTANT_Unicode"));
duke@0 1123 break;
duke@0 1124 }
duke@0 1125 case JVM_CONSTANT_Utf8: {
duke@0 1126 symbolOop sym = symbol_at(idx);
duke@0 1127 char* str = sym->as_utf8();
duke@0 1128 // Warning! It's crashing on x86 with len = sym->utf8_length()
duke@0 1129 int len = (int) strlen(str);
duke@0 1130 Bytes::put_Java_u2((address) (bytes+1), (u2) len);
duke@0 1131 for (int i = 0; i < len; i++) {
duke@0 1132 bytes[3+i] = (u1) str[i];
duke@0 1133 }
duke@0 1134 DBG(printf("JVM_CONSTANT_Utf8: %s ", str));
duke@0 1135 break;
duke@0 1136 }
duke@0 1137 case JVM_CONSTANT_Integer: {
duke@0 1138 jint val = int_at(idx);
duke@0 1139 Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
duke@0 1140 break;
duke@0 1141 }
duke@0 1142 case JVM_CONSTANT_Float: {
duke@0 1143 jfloat val = float_at(idx);
duke@0 1144 Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
duke@0 1145 break;
duke@0 1146 }
duke@0 1147 case JVM_CONSTANT_Long: {
duke@0 1148 jlong val = long_at(idx);
duke@0 1149 Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
duke@0 1150 idx++; // Long takes two cpool slots
duke@0 1151 break;
duke@0 1152 }
duke@0 1153 case JVM_CONSTANT_Double: {
duke@0 1154 jdouble val = double_at(idx);
duke@0 1155 Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
duke@0 1156 idx++; // Double takes two cpool slots
duke@0 1157 break;
duke@0 1158 }
duke@0 1159 case JVM_CONSTANT_Class:
duke@0 1160 case JVM_CONSTANT_UnresolvedClass:
duke@0 1161 case JVM_CONSTANT_UnresolvedClassInError: {
duke@0 1162 *bytes = JVM_CONSTANT_Class;
duke@0 1163 symbolOop sym = klass_name_at(idx);
duke@0 1164 idx1 = tbl->symbol_to_value(sym);
duke@0 1165 assert(idx1 != 0, "Have not found a hashtable entry");
duke@0 1166 Bytes::put_Java_u2((address) (bytes+1), idx1);
duke@0 1167 DBG(printf("JVM_CONSTANT_Class: idx=#%03hd, %s", idx1, sym->as_utf8()));
duke@0 1168 break;
duke@0 1169 }
duke@0 1170 case JVM_CONSTANT_String: {
duke@0 1171 unsigned int hash;
duke@0 1172 char *str = string_at_noresolve(idx);
duke@0 1173 symbolOop sym = SymbolTable::lookup_only(str, (int) strlen(str), hash);
duke@0 1174 idx1 = tbl->symbol_to_value(sym);
duke@0 1175 assert(idx1 != 0, "Have not found a hashtable entry");
duke@0 1176 Bytes::put_Java_u2((address) (bytes+1), idx1);
duke@0 1177 DBG(printf("JVM_CONSTANT_String: idx=#%03hd, %s", idx1, str));
duke@0 1178 break;
duke@0 1179 }
duke@0 1180 case JVM_CONSTANT_UnresolvedString: {
duke@0 1181 *bytes = JVM_CONSTANT_String;
duke@0 1182 symbolOop sym = unresolved_string_at(idx);
duke@0 1183 idx1 = tbl->symbol_to_value(sym);
duke@0 1184 assert(idx1 != 0, "Have not found a hashtable entry");
duke@0 1185 Bytes::put_Java_u2((address) (bytes+1), idx1);
duke@0 1186 DBG(char *str = sym->as_utf8());
duke@0 1187 DBG(printf("JVM_CONSTANT_UnresolvedString: idx=#%03hd, %s", idx1, str));
duke@0 1188 break;
duke@0 1189 }
duke@0 1190 case JVM_CONSTANT_Fieldref:
duke@0 1191 case JVM_CONSTANT_Methodref:
duke@0 1192 case JVM_CONSTANT_InterfaceMethodref: {
duke@0 1193 idx1 = uncached_klass_ref_index_at(idx);
duke@0 1194 idx2 = uncached_name_and_type_ref_index_at(idx);
duke@0 1195 Bytes::put_Java_u2((address) (bytes+1), idx1);
duke@0 1196 Bytes::put_Java_u2((address) (bytes+3), idx2);
duke@0 1197 DBG(printf("JVM_CONSTANT_Methodref: %hd %hd", idx1, idx2));
duke@0 1198 break;
duke@0 1199 }
duke@0 1200 case JVM_CONSTANT_NameAndType: {
duke@0 1201 idx1 = name_ref_index_at(idx);
duke@0 1202 idx2 = signature_ref_index_at(idx);
duke@0 1203 Bytes::put_Java_u2((address) (bytes+1), idx1);
duke@0 1204 Bytes::put_Java_u2((address) (bytes+3), idx2);
duke@0 1205 DBG(printf("JVM_CONSTANT_NameAndType: %hd %hd", idx1, idx2));
duke@0 1206 break;
duke@0 1207 }
duke@0 1208 case JVM_CONSTANT_ClassIndex: {
duke@0 1209 *bytes = JVM_CONSTANT_Class;
duke@0 1210 idx1 = klass_index_at(idx);
duke@0 1211 Bytes::put_Java_u2((address) (bytes+1), idx1);
duke@0 1212 DBG(printf("JVM_CONSTANT_ClassIndex: %hd", idx1));
duke@0 1213 break;
duke@0 1214 }
duke@0 1215 case JVM_CONSTANT_StringIndex: {
duke@0 1216 *bytes = JVM_CONSTANT_String;
duke@0 1217 idx1 = string_index_at(idx);
duke@0 1218 Bytes::put_Java_u2((address) (bytes+1), idx1);
duke@0 1219 DBG(printf("JVM_CONSTANT_StringIndex: %hd", idx1));
duke@0 1220 break;
duke@0 1221 }
duke@0 1222 }
duke@0 1223 DBG(printf("\n"));
duke@0 1224 bytes += ent_size;
duke@0 1225 size += ent_size;
duke@0 1226 }
duke@0 1227 assert(size == cpool_size, "Size mismatch");
duke@0 1228
duke@0 1229 // Keep temorarily for debugging until it's stable.
duke@0 1230 DBG(print_cpool_bytes(cnt, start_bytes));
duke@0 1231 return (int)(bytes - start_bytes);
duke@0 1232 } /* end copy_cpool_bytes */
duke@0 1233
duke@0 1234
duke@0 1235 void SymbolHashMap::add_entry(symbolOop sym, u2 value) {
duke@0 1236 char *str = sym->as_utf8();
duke@0 1237 unsigned int hash = compute_hash(str, sym->utf8_length());
duke@0 1238 unsigned int index = hash % table_size();
duke@0 1239
duke@0 1240 // check if already in map
duke@0 1241 // we prefer the first entry since it is more likely to be what was used in
duke@0 1242 // the class file
duke@0 1243 for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
duke@0 1244 assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
duke@0 1245 if (en->hash() == hash && en->symbol() == sym) {
duke@0 1246 return; // already there
duke@0 1247 }
duke@0 1248 }
duke@0 1249
duke@0 1250 SymbolHashMapEntry* entry = new SymbolHashMapEntry(hash, sym, value);
duke@0 1251 entry->set_next(bucket(index));
duke@0 1252 _buckets[index].set_entry(entry);
duke@0 1253 assert(entry->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
duke@0 1254 }
duke@0 1255
duke@0 1256 SymbolHashMapEntry* SymbolHashMap::find_entry(symbolOop sym) {
duke@0 1257 assert(sym != NULL, "SymbolHashMap::find_entry - symbol is NULL");
duke@0 1258 char *str = sym->as_utf8();
duke@0 1259 int len = sym->utf8_length();
duke@0 1260 unsigned int hash = SymbolHashMap::compute_hash(str, len);
duke@0 1261 unsigned int index = hash % table_size();
duke@0 1262 for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
duke@0 1263 assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
duke@0 1264 if (en->hash() == hash && en->symbol() == sym) {
duke@0 1265 return en;
duke@0 1266 }
duke@0 1267 }
duke@0 1268 return NULL;
duke@0 1269 }