src/share/vm/oops/constantPoolOop.cpp

Fri, 27 Feb 2009 13:27:09 -0800

author
twisti
date
Fri, 27 Feb 2009 13:27:09 -0800
changeset 1040
98cb887364d3
parent 951
fc7ab6287598
child 1014
0fbdb4381b99
permissions
-rw-r--r--

6810672: Comment typos
Summary: I have collected some typos I have found while looking at the code.
Reviewed-by: kvn, never

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

mercurial