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