Tue, 23 Nov 2010 13:22:55 -0800
6989984: Use standard include model for Hospot
Summary: Replaced MakeDeps and the includeDB files with more standardized solutions.
Reviewed-by: coleenp, kvn, kamg
1 /*
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
25 #include "precompiled.hpp"
26 #include "classfile/javaClasses.hpp"
27 #include "classfile/symbolTable.hpp"
28 #include "classfile/systemDictionary.hpp"
29 #include "classfile/vmSymbols.hpp"
30 #include "interpreter/linkResolver.hpp"
31 #include "memory/oopFactory.hpp"
32 #include "memory/universe.inline.hpp"
33 #include "oops/constantPoolOop.hpp"
34 #include "oops/instanceKlass.hpp"
35 #include "oops/objArrayKlass.hpp"
36 #include "oops/oop.inline.hpp"
37 #include "runtime/fieldType.hpp"
38 #include "runtime/init.hpp"
39 #include "runtime/signature.hpp"
40 #include "runtime/vframe.hpp"
42 void constantPoolOopDesc::set_flag_at(FlagBit fb) {
43 const int MAX_STATE_CHANGES = 2;
44 for (int i = MAX_STATE_CHANGES + 10; i > 0; i--) {
45 int oflags = _flags;
46 int nflags = oflags | (1 << (int)fb);
47 if (Atomic::cmpxchg(nflags, &_flags, oflags) == oflags)
48 return;
49 }
50 assert(false, "failed to cmpxchg flags");
51 _flags |= (1 << (int)fb); // better than nothing
52 }
54 klassOop constantPoolOopDesc::klass_at_impl(constantPoolHandle this_oop, int which, TRAPS) {
55 // A resolved constantPool entry will contain a klassOop, otherwise a symbolOop.
56 // It is not safe to rely on the tag bit's here, since we don't have a lock, and the entry and
57 // tag is not updated atomicly.
58 oop entry = *(this_oop->obj_at_addr(which));
59 if (entry->is_klass()) {
60 // Already resolved - return entry.
61 return (klassOop)entry;
62 }
64 // Acquire lock on constant oop while doing update. After we get the lock, we check if another object
65 // already has updated the object
66 assert(THREAD->is_Java_thread(), "must be a Java thread");
67 bool do_resolve = false;
68 bool in_error = false;
70 symbolHandle name;
71 Handle loader;
72 { ObjectLocker ol(this_oop, THREAD);
74 if (this_oop->tag_at(which).is_unresolved_klass()) {
75 if (this_oop->tag_at(which).is_unresolved_klass_in_error()) {
76 in_error = true;
77 } else {
78 do_resolve = true;
79 name = symbolHandle(THREAD, this_oop->unresolved_klass_at(which));
80 loader = Handle(THREAD, instanceKlass::cast(this_oop->pool_holder())->class_loader());
81 }
82 }
83 } // unlocking constantPool
86 // The original attempt to resolve this constant pool entry failed so find the
87 // original error and throw it again (JVMS 5.4.3).
88 if (in_error) {
89 symbolOop error = SystemDictionary::find_resolution_error(this_oop, which);
90 guarantee(error != (symbolOop)NULL, "tag mismatch with resolution error table");
91 ResourceMark rm;
92 // exception text will be the class name
93 const char* className = this_oop->unresolved_klass_at(which)->as_C_string();
94 THROW_MSG_0(error, className);
95 }
97 if (do_resolve) {
98 // this_oop must be unlocked during resolve_or_fail
99 oop protection_domain = Klass::cast(this_oop->pool_holder())->protection_domain();
100 Handle h_prot (THREAD, protection_domain);
101 klassOop k_oop = SystemDictionary::resolve_or_fail(name, loader, h_prot, true, THREAD);
102 KlassHandle k;
103 if (!HAS_PENDING_EXCEPTION) {
104 k = KlassHandle(THREAD, k_oop);
105 // Do access check for klasses
106 verify_constant_pool_resolve(this_oop, k, THREAD);
107 }
109 // Failed to resolve class. We must record the errors so that subsequent attempts
110 // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
111 if (HAS_PENDING_EXCEPTION) {
112 ResourceMark rm;
113 symbolHandle error(PENDING_EXCEPTION->klass()->klass_part()->name());
115 bool throw_orig_error = false;
116 {
117 ObjectLocker ol (this_oop, THREAD);
119 // some other thread has beaten us and has resolved the class.
120 if (this_oop->tag_at(which).is_klass()) {
121 CLEAR_PENDING_EXCEPTION;
122 entry = this_oop->resolved_klass_at(which);
123 return (klassOop)entry;
124 }
126 if (!PENDING_EXCEPTION->
127 is_a(SystemDictionary::LinkageError_klass())) {
128 // Just throw the exception and don't prevent these classes from
129 // being loaded due to virtual machine errors like StackOverflow
130 // and OutOfMemoryError, etc, or if the thread was hit by stop()
131 // Needs clarification to section 5.4.3 of the VM spec (see 6308271)
132 }
133 else if (!this_oop->tag_at(which).is_unresolved_klass_in_error()) {
134 SystemDictionary::add_resolution_error(this_oop, which, error);
135 this_oop->tag_at_put(which, JVM_CONSTANT_UnresolvedClassInError);
136 } else {
137 // some other thread has put the class in error state.
138 error = symbolHandle(SystemDictionary::find_resolution_error(this_oop, which));
139 assert(!error.is_null(), "checking");
140 throw_orig_error = true;
141 }
142 } // unlocked
144 if (throw_orig_error) {
145 CLEAR_PENDING_EXCEPTION;
146 ResourceMark rm;
147 const char* className = this_oop->unresolved_klass_at(which)->as_C_string();
148 THROW_MSG_0(error, className);
149 }
151 return 0;
152 }
154 if (TraceClassResolution && !k()->klass_part()->oop_is_array()) {
155 // skip resolving the constant pool so that this code get's
156 // called the next time some bytecodes refer to this class.
157 ResourceMark rm;
158 int line_number = -1;
159 const char * source_file = NULL;
160 if (JavaThread::current()->has_last_Java_frame()) {
161 // try to identify the method which called this function.
162 vframeStream vfst(JavaThread::current());
163 if (!vfst.at_end()) {
164 line_number = vfst.method()->line_number_from_bci(vfst.bci());
165 symbolOop s = instanceKlass::cast(vfst.method()->method_holder())->source_file_name();
166 if (s != NULL) {
167 source_file = s->as_C_string();
168 }
169 }
170 }
171 if (k() != this_oop->pool_holder()) {
172 // only print something if the classes are different
173 if (source_file != NULL) {
174 tty->print("RESOLVE %s %s %s:%d\n",
175 instanceKlass::cast(this_oop->pool_holder())->external_name(),
176 instanceKlass::cast(k())->external_name(), source_file, line_number);
177 } else {
178 tty->print("RESOLVE %s %s\n",
179 instanceKlass::cast(this_oop->pool_holder())->external_name(),
180 instanceKlass::cast(k())->external_name());
181 }
182 }
183 return k();
184 } else {
185 ObjectLocker ol (this_oop, THREAD);
186 // Only updated constant pool - if it is resolved.
187 do_resolve = this_oop->tag_at(which).is_unresolved_klass();
188 if (do_resolve) {
189 this_oop->klass_at_put(which, k());
190 }
191 }
192 }
194 entry = this_oop->resolved_klass_at(which);
195 assert(entry->is_klass(), "must be resolved at this point");
196 return (klassOop)entry;
197 }
200 // Does not update constantPoolOop - to avoid any exception throwing. Used
201 // by compiler and exception handling. Also used to avoid classloads for
202 // instanceof operations. Returns NULL if the class has not been loaded or
203 // if the verification of constant pool failed
204 klassOop constantPoolOopDesc::klass_at_if_loaded(constantPoolHandle this_oop, int which) {
205 oop entry = *this_oop->obj_at_addr(which);
206 if (entry->is_klass()) {
207 return (klassOop)entry;
208 } else {
209 assert(entry->is_symbol(), "must be either symbol or klass");
210 Thread *thread = Thread::current();
211 symbolHandle name (thread, (symbolOop)entry);
212 oop loader = instanceKlass::cast(this_oop->pool_holder())->class_loader();
213 oop protection_domain = Klass::cast(this_oop->pool_holder())->protection_domain();
214 Handle h_prot (thread, protection_domain);
215 Handle h_loader (thread, loader);
216 klassOop k = SystemDictionary::find(name, h_loader, h_prot, thread);
218 if (k != NULL) {
219 // Make sure that resolving is legal
220 EXCEPTION_MARK;
221 KlassHandle klass(THREAD, k);
222 // return NULL if verification fails
223 verify_constant_pool_resolve(this_oop, klass, THREAD);
224 if (HAS_PENDING_EXCEPTION) {
225 CLEAR_PENDING_EXCEPTION;
226 return NULL;
227 }
228 return klass();
229 } else {
230 return k;
231 }
232 }
233 }
236 klassOop constantPoolOopDesc::klass_ref_at_if_loaded(constantPoolHandle this_oop, int which) {
237 return klass_at_if_loaded(this_oop, this_oop->klass_ref_index_at(which));
238 }
241 // This is an interface for the compiler that allows accessing non-resolved entries
242 // in the constant pool - but still performs the validations tests. Must be used
243 // in a pre-parse of the compiler - to determine what it can do and not do.
244 // Note: We cannot update the ConstantPool from the vm_thread.
245 klassOop constantPoolOopDesc::klass_ref_at_if_loaded_check(constantPoolHandle this_oop, int index, TRAPS) {
246 int which = this_oop->klass_ref_index_at(index);
247 oop entry = *this_oop->obj_at_addr(which);
248 if (entry->is_klass()) {
249 return (klassOop)entry;
250 } else {
251 assert(entry->is_symbol(), "must be either symbol or klass");
252 symbolHandle name (THREAD, (symbolOop)entry);
253 oop loader = instanceKlass::cast(this_oop->pool_holder())->class_loader();
254 oop protection_domain = Klass::cast(this_oop->pool_holder())->protection_domain();
255 Handle h_loader(THREAD, loader);
256 Handle h_prot (THREAD, protection_domain);
257 KlassHandle k(THREAD, SystemDictionary::find(name, h_loader, h_prot, THREAD));
259 // Do access check for klasses
260 if( k.not_null() ) verify_constant_pool_resolve(this_oop, k, CHECK_NULL);
261 return k();
262 }
263 }
266 symbolOop constantPoolOopDesc::impl_name_ref_at(int which, bool uncached) {
267 int name_index = name_ref_index_at(impl_name_and_type_ref_index_at(which, uncached));
268 return symbol_at(name_index);
269 }
272 symbolOop constantPoolOopDesc::impl_signature_ref_at(int which, bool uncached) {
273 int signature_index = signature_ref_index_at(impl_name_and_type_ref_index_at(which, uncached));
274 return symbol_at(signature_index);
275 }
278 int constantPoolOopDesc::impl_name_and_type_ref_index_at(int which, bool uncached) {
279 int i = which;
280 if (!uncached && cache() != NULL) {
281 if (constantPoolCacheOopDesc::is_secondary_index(which)) {
282 // Invokedynamic index.
283 int pool_index = cache()->main_entry_at(which)->constant_pool_index();
284 if (!AllowTransitionalJSR292 || tag_at(pool_index).is_invoke_dynamic())
285 pool_index = invoke_dynamic_name_and_type_ref_index_at(pool_index);
286 assert(tag_at(pool_index).is_name_and_type(), "");
287 return pool_index;
288 }
289 // change byte-ordering and go via cache
290 i = remap_instruction_operand_from_cache(which);
291 } else {
292 if (AllowTransitionalJSR292 && tag_at(which).is_name_and_type())
293 // invokedynamic index is a simple name-and-type
294 return which;
295 if (tag_at(which).is_invoke_dynamic()) {
296 int pool_index = invoke_dynamic_name_and_type_ref_index_at(which);
297 assert(tag_at(pool_index).is_name_and_type(), "");
298 return pool_index;
299 }
300 }
301 assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
302 assert(!tag_at(i).is_invoke_dynamic(), "Must be handled above");
303 jint ref_index = *int_at_addr(i);
304 return extract_high_short_from_int(ref_index);
305 }
308 int constantPoolOopDesc::impl_klass_ref_index_at(int which, bool uncached) {
309 guarantee(!constantPoolCacheOopDesc::is_secondary_index(which),
310 "an invokedynamic instruction does not have a klass");
311 int i = which;
312 if (!uncached && cache() != NULL) {
313 // change byte-ordering and go via cache
314 i = remap_instruction_operand_from_cache(which);
315 }
316 assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
317 jint ref_index = *int_at_addr(i);
318 return extract_low_short_from_int(ref_index);
319 }
323 int constantPoolOopDesc::remap_instruction_operand_from_cache(int operand) {
324 int cpc_index = operand;
325 DEBUG_ONLY(cpc_index -= CPCACHE_INDEX_TAG);
326 assert((int)(u2)cpc_index == cpc_index, "clean u2");
327 int member_index = cache()->entry_at(cpc_index)->constant_pool_index();
328 return member_index;
329 }
332 void constantPoolOopDesc::verify_constant_pool_resolve(constantPoolHandle this_oop, KlassHandle k, TRAPS) {
333 if (k->oop_is_instance() || k->oop_is_objArray()) {
334 instanceKlassHandle holder (THREAD, this_oop->pool_holder());
335 klassOop elem_oop = k->oop_is_instance() ? k() : objArrayKlass::cast(k())->bottom_klass();
336 KlassHandle element (THREAD, elem_oop);
338 // The element type could be a typeArray - we only need the access check if it is
339 // an reference to another class
340 if (element->oop_is_instance()) {
341 LinkResolver::check_klass_accessability(holder, element, CHECK);
342 }
343 }
344 }
347 int constantPoolOopDesc::name_ref_index_at(int which_nt) {
348 jint ref_index = name_and_type_at(which_nt);
349 return extract_low_short_from_int(ref_index);
350 }
353 int constantPoolOopDesc::signature_ref_index_at(int which_nt) {
354 jint ref_index = name_and_type_at(which_nt);
355 return extract_high_short_from_int(ref_index);
356 }
359 klassOop constantPoolOopDesc::klass_ref_at(int which, TRAPS) {
360 return klass_at(klass_ref_index_at(which), CHECK_NULL);
361 }
364 symbolOop constantPoolOopDesc::klass_name_at(int which) {
365 assert(tag_at(which).is_unresolved_klass() || tag_at(which).is_klass(),
366 "Corrupted constant pool");
367 // A resolved constantPool entry will contain a klassOop, otherwise a symbolOop.
368 // It is not safe to rely on the tag bit's here, since we don't have a lock, and the entry and
369 // tag is not updated atomicly.
370 oop entry = *(obj_at_addr(which));
371 if (entry->is_klass()) {
372 // Already resolved - return entry's name.
373 return klassOop(entry)->klass_part()->name();
374 } else {
375 assert(entry->is_symbol(), "must be either symbol or klass");
376 return (symbolOop)entry;
377 }
378 }
380 symbolOop constantPoolOopDesc::klass_ref_at_noresolve(int which) {
381 jint ref_index = klass_ref_index_at(which);
382 return klass_at_noresolve(ref_index);
383 }
385 symbolOop constantPoolOopDesc::uncached_klass_ref_at_noresolve(int which) {
386 jint ref_index = uncached_klass_ref_index_at(which);
387 return klass_at_noresolve(ref_index);
388 }
390 char* constantPoolOopDesc::string_at_noresolve(int which) {
391 // Test entry type in case string is resolved while in here.
392 oop entry = *(obj_at_addr(which));
393 if (entry->is_symbol()) {
394 return ((symbolOop)entry)->as_C_string();
395 } else if (java_lang_String::is_instance(entry)) {
396 return java_lang_String::as_utf8_string(entry);
397 } else {
398 return (char*)"<pseudo-string>";
399 }
400 }
403 BasicType constantPoolOopDesc::basic_type_for_signature_at(int which) {
404 return FieldType::basic_type(symbol_at(which));
405 }
408 void constantPoolOopDesc::resolve_string_constants_impl(constantPoolHandle this_oop, TRAPS) {
409 for (int index = 1; index < this_oop->length(); index++) { // Index 0 is unused
410 if (this_oop->tag_at(index).is_unresolved_string()) {
411 this_oop->string_at(index, CHECK);
412 }
413 }
414 }
416 // A resolved constant value in the CP cache is represented as a non-null
417 // value. As a special case, this value can be a 'systemObjArray'
418 // which masks an exception object to throw.
419 // This allows a MethodHandle constant reference to throw a consistent
420 // exception every time, if it fails to resolve.
421 static oop decode_exception_from_f1(oop result_oop, TRAPS) {
422 if (result_oop->klass() != Universe::systemObjArrayKlassObj())
423 return result_oop;
425 // Special cases here: Masked null, saved exception.
426 objArrayOop sys_array = (objArrayOop) result_oop;
427 assert(sys_array->length() == 1, "bad system array");
428 if (sys_array->length() == 1) {
429 THROW_OOP_(sys_array->obj_at(0), NULL);
430 }
431 return NULL;
432 }
434 oop constantPoolOopDesc::resolve_constant_at_impl(constantPoolHandle this_oop, int index, int cache_index, TRAPS) {
435 oop result_oop = NULL;
436 Handle throw_exception;
438 if (cache_index == _possible_index_sentinel) {
439 // It is possible that this constant is one which is cached in the CP cache.
440 // We'll do a linear search. This should be OK because this usage is rare.
441 assert(index > 0, "valid index");
442 constantPoolCacheOop cache = this_oop()->cache();
443 for (int i = 0, len = cache->length(); i < len; i++) {
444 ConstantPoolCacheEntry* cpc_entry = cache->entry_at(i);
445 if (!cpc_entry->is_secondary_entry() && cpc_entry->constant_pool_index() == index) {
446 // Switch the query to use this CPC entry.
447 cache_index = i;
448 index = _no_index_sentinel;
449 break;
450 }
451 }
452 if (cache_index == _possible_index_sentinel)
453 cache_index = _no_index_sentinel; // not found
454 }
455 assert(cache_index == _no_index_sentinel || cache_index >= 0, "");
456 assert(index == _no_index_sentinel || index >= 0, "");
458 if (cache_index >= 0) {
459 assert(index == _no_index_sentinel, "only one kind of index at a time");
460 ConstantPoolCacheEntry* cpc_entry = this_oop->cache()->entry_at(cache_index);
461 result_oop = cpc_entry->f1();
462 if (result_oop != NULL) {
463 return decode_exception_from_f1(result_oop, THREAD);
464 // That was easy...
465 }
466 index = cpc_entry->constant_pool_index();
467 }
469 jvalue prim_value; // temp used only in a few cases below
471 int tag_value = this_oop->tag_at(index).value();
472 switch (tag_value) {
474 case JVM_CONSTANT_UnresolvedClass:
475 case JVM_CONSTANT_UnresolvedClassInError:
476 case JVM_CONSTANT_Class:
477 {
478 klassOop resolved = klass_at_impl(this_oop, index, CHECK_NULL);
479 // ldc wants the java mirror.
480 result_oop = resolved->klass_part()->java_mirror();
481 break;
482 }
484 case JVM_CONSTANT_String:
485 case JVM_CONSTANT_UnresolvedString:
486 if (this_oop->is_pseudo_string_at(index)) {
487 result_oop = this_oop->pseudo_string_at(index);
488 break;
489 }
490 result_oop = string_at_impl(this_oop, index, CHECK_NULL);
491 break;
493 case JVM_CONSTANT_Object:
494 result_oop = this_oop->object_at(index);
495 break;
497 case JVM_CONSTANT_MethodHandle:
498 {
499 int ref_kind = this_oop->method_handle_ref_kind_at(index);
500 int callee_index = this_oop->method_handle_klass_index_at(index);
501 symbolHandle name(THREAD, this_oop->method_handle_name_ref_at(index));
502 symbolHandle signature(THREAD, this_oop->method_handle_signature_ref_at(index));
503 if (PrintMiscellaneous)
504 tty->print_cr("resolve JVM_CONSTANT_MethodHandle:%d [%d/%d/%d] %s.%s",
505 ref_kind, index, this_oop->method_handle_index_at(index),
506 callee_index, name->as_C_string(), signature->as_C_string());
507 KlassHandle callee;
508 { klassOop k = klass_at_impl(this_oop, callee_index, CHECK_NULL);
509 callee = KlassHandle(THREAD, k);
510 }
511 KlassHandle klass(THREAD, this_oop->pool_holder());
512 Handle value = SystemDictionary::link_method_handle_constant(klass, ref_kind,
513 callee, name, signature,
514 THREAD);
515 if (HAS_PENDING_EXCEPTION) {
516 throw_exception = Handle(THREAD, PENDING_EXCEPTION);
517 CLEAR_PENDING_EXCEPTION;
518 break;
519 }
520 result_oop = value();
521 assert(result_oop != NULL, "");
522 break;
523 }
525 case JVM_CONSTANT_MethodType:
526 {
527 symbolHandle signature(THREAD, this_oop->method_type_signature_at(index));
528 if (PrintMiscellaneous)
529 tty->print_cr("resolve JVM_CONSTANT_MethodType [%d/%d] %s",
530 index, this_oop->method_type_index_at(index),
531 signature->as_C_string());
532 KlassHandle klass(THREAD, this_oop->pool_holder());
533 bool ignore_is_on_bcp = false;
534 Handle value = SystemDictionary::find_method_handle_type(signature,
535 klass,
536 false,
537 ignore_is_on_bcp,
538 THREAD);
539 if (HAS_PENDING_EXCEPTION) {
540 throw_exception = Handle(THREAD, PENDING_EXCEPTION);
541 CLEAR_PENDING_EXCEPTION;
542 break;
543 }
544 result_oop = value();
545 assert(result_oop != NULL, "");
546 break;
547 }
549 case JVM_CONSTANT_Integer:
550 prim_value.i = this_oop->int_at(index);
551 result_oop = java_lang_boxing_object::create(T_INT, &prim_value, CHECK_NULL);
552 break;
554 case JVM_CONSTANT_Float:
555 prim_value.f = this_oop->float_at(index);
556 result_oop = java_lang_boxing_object::create(T_FLOAT, &prim_value, CHECK_NULL);
557 break;
559 case JVM_CONSTANT_Long:
560 prim_value.j = this_oop->long_at(index);
561 result_oop = java_lang_boxing_object::create(T_LONG, &prim_value, CHECK_NULL);
562 break;
564 case JVM_CONSTANT_Double:
565 prim_value.d = this_oop->double_at(index);
566 result_oop = java_lang_boxing_object::create(T_DOUBLE, &prim_value, CHECK_NULL);
567 break;
569 default:
570 DEBUG_ONLY( tty->print_cr("*** %p: tag at CP[%d/%d] = %d",
571 this_oop(), index, cache_index, tag_value) );
572 assert(false, "unexpected constant tag");
573 break;
574 }
576 if (cache_index >= 0) {
577 // Cache the oop here also.
578 if (throw_exception.not_null()) {
579 objArrayOop sys_array = oopFactory::new_system_objArray(1, CHECK_NULL);
580 sys_array->obj_at_put(0, throw_exception());
581 result_oop = sys_array;
582 throw_exception = Handle(); // be tidy
583 }
584 Handle result_handle(THREAD, result_oop);
585 result_oop = NULL; // safety
586 ObjectLocker ol(this_oop, THREAD);
587 ConstantPoolCacheEntry* cpc_entry = this_oop->cache()->entry_at(cache_index);
588 result_oop = cpc_entry->f1();
589 // Benign race condition: f1 may already be filled in while we were trying to lock.
590 // The important thing here is that all threads pick up the same result.
591 // It doesn't matter which racing thread wins, as long as only one
592 // result is used by all threads, and all future queries.
593 // That result may be either a resolved constant or a failure exception.
594 if (result_oop == NULL) {
595 result_oop = result_handle();
596 cpc_entry->set_f1(result_oop);
597 }
598 return decode_exception_from_f1(result_oop, THREAD);
599 } else {
600 if (throw_exception.not_null()) {
601 THROW_HANDLE_(throw_exception, NULL);
602 }
603 return result_oop;
604 }
605 }
607 oop constantPoolOopDesc::string_at_impl(constantPoolHandle this_oop, int which, TRAPS) {
608 oop entry = *(this_oop->obj_at_addr(which));
609 if (entry->is_symbol()) {
610 ObjectLocker ol(this_oop, THREAD);
611 if (this_oop->tag_at(which).is_unresolved_string()) {
612 // Intern string
613 symbolOop sym = this_oop->unresolved_string_at(which);
614 entry = StringTable::intern(sym, CHECK_(constantPoolOop(NULL)));
615 this_oop->string_at_put(which, entry);
616 } else {
617 // Another thread beat us and interned string, read string from constant pool
618 entry = this_oop->resolved_string_at(which);
619 }
620 }
621 assert(java_lang_String::is_instance(entry), "must be string");
622 return entry;
623 }
626 bool constantPoolOopDesc::is_pseudo_string_at(int which) {
627 oop entry = *(obj_at_addr(which));
628 if (entry->is_symbol())
629 // Not yet resolved, but it will resolve to a string.
630 return false;
631 else if (java_lang_String::is_instance(entry))
632 return false; // actually, it might be a non-interned or non-perm string
633 else
634 // truly pseudo
635 return true;
636 }
639 bool constantPoolOopDesc::klass_name_at_matches(instanceKlassHandle k,
640 int which) {
641 // Names are interned, so we can compare symbolOops directly
642 symbolOop cp_name = klass_name_at(which);
643 return (cp_name == k->name());
644 }
647 int constantPoolOopDesc::pre_resolve_shared_klasses(TRAPS) {
648 ResourceMark rm;
649 int count = 0;
650 for (int index = 1; index < tags()->length(); index++) { // Index 0 is unused
651 if (tag_at(index).is_unresolved_string()) {
652 // Intern string
653 symbolOop sym = unresolved_string_at(index);
654 oop entry = StringTable::intern(sym, CHECK_(-1));
655 string_at_put(index, entry);
656 }
657 }
658 return count;
659 }
662 // Iterate over symbols which are used as class, field, method names and
663 // signatures (in preparation for writing to the shared archive).
665 void constantPoolOopDesc::shared_symbols_iterate(OopClosure* closure) {
666 for (int index = 1; index < length(); index++) { // Index 0 is unused
667 switch (tag_at(index).value()) {
669 case JVM_CONSTANT_UnresolvedClass:
670 closure->do_oop(obj_at_addr(index));
671 break;
673 case JVM_CONSTANT_NameAndType:
674 {
675 int i = *int_at_addr(index);
676 closure->do_oop(obj_at_addr((unsigned)i >> 16));
677 closure->do_oop(obj_at_addr((unsigned)i & 0xffff));
678 }
679 break;
681 case JVM_CONSTANT_Class:
682 case JVM_CONSTANT_InterfaceMethodref:
683 case JVM_CONSTANT_Fieldref:
684 case JVM_CONSTANT_Methodref:
685 case JVM_CONSTANT_Integer:
686 case JVM_CONSTANT_Float:
687 // Do nothing! Not an oop.
688 // These constant types do not reference symbols at this point.
689 break;
691 case JVM_CONSTANT_String:
692 // Do nothing! Not a symbol.
693 break;
695 case JVM_CONSTANT_UnresolvedString:
696 case JVM_CONSTANT_Utf8:
697 // These constants are symbols, but unless these symbols are
698 // actually to be used for something, we don't want to mark them.
699 break;
701 case JVM_CONSTANT_Long:
702 case JVM_CONSTANT_Double:
703 // Do nothing! Not an oop. (But takes two pool entries.)
704 ++index;
705 break;
707 default:
708 ShouldNotReachHere();
709 break;
710 }
711 }
712 }
715 // Iterate over the [one] tags array (in preparation for writing to the
716 // shared archive).
718 void constantPoolOopDesc::shared_tags_iterate(OopClosure* closure) {
719 closure->do_oop(tags_addr());
720 closure->do_oop(operands_addr());
721 }
724 // Iterate over String objects (in preparation for writing to the shared
725 // archive).
727 void constantPoolOopDesc::shared_strings_iterate(OopClosure* closure) {
728 for (int index = 1; index < length(); index++) { // Index 0 is unused
729 switch (tag_at(index).value()) {
731 case JVM_CONSTANT_UnresolvedClass:
732 case JVM_CONSTANT_NameAndType:
733 // Do nothing! Not a String.
734 break;
736 case JVM_CONSTANT_Class:
737 case JVM_CONSTANT_InterfaceMethodref:
738 case JVM_CONSTANT_Fieldref:
739 case JVM_CONSTANT_Methodref:
740 case JVM_CONSTANT_Integer:
741 case JVM_CONSTANT_Float:
742 // Do nothing! Not an oop.
743 // These constant types do not reference symbols at this point.
744 break;
746 case JVM_CONSTANT_String:
747 closure->do_oop(obj_at_addr(index));
748 break;
750 case JVM_CONSTANT_UnresolvedString:
751 case JVM_CONSTANT_Utf8:
752 // These constants are symbols, but unless these symbols are
753 // actually to be used for something, we don't want to mark them.
754 break;
756 case JVM_CONSTANT_Long:
757 case JVM_CONSTANT_Double:
758 // Do nothing! Not an oop. (But takes two pool entries.)
759 ++index;
760 break;
762 default:
763 ShouldNotReachHere();
764 break;
765 }
766 }
767 }
770 // Compare this constant pool's entry at index1 to the constant pool
771 // cp2's entry at index2.
772 bool constantPoolOopDesc::compare_entry_to(int index1, constantPoolHandle cp2,
773 int index2, TRAPS) {
775 jbyte t1 = tag_at(index1).value();
776 jbyte t2 = cp2->tag_at(index2).value();
779 // JVM_CONSTANT_UnresolvedClassInError is equal to JVM_CONSTANT_UnresolvedClass
780 // when comparing
781 if (t1 == JVM_CONSTANT_UnresolvedClassInError) {
782 t1 = JVM_CONSTANT_UnresolvedClass;
783 }
784 if (t2 == JVM_CONSTANT_UnresolvedClassInError) {
785 t2 = JVM_CONSTANT_UnresolvedClass;
786 }
788 if (t1 != t2) {
789 // Not the same entry type so there is nothing else to check. Note
790 // that this style of checking will consider resolved/unresolved
791 // class pairs and resolved/unresolved string pairs as different.
792 // From the constantPoolOop API point of view, this is correct
793 // behavior. See constantPoolKlass::merge() to see how this plays
794 // out in the context of constantPoolOop merging.
795 return false;
796 }
798 switch (t1) {
799 case JVM_CONSTANT_Class:
800 {
801 klassOop k1 = klass_at(index1, CHECK_false);
802 klassOop k2 = cp2->klass_at(index2, CHECK_false);
803 if (k1 == k2) {
804 return true;
805 }
806 } break;
808 case JVM_CONSTANT_ClassIndex:
809 {
810 int recur1 = klass_index_at(index1);
811 int recur2 = cp2->klass_index_at(index2);
812 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
813 if (match) {
814 return true;
815 }
816 } break;
818 case JVM_CONSTANT_Double:
819 {
820 jdouble d1 = double_at(index1);
821 jdouble d2 = cp2->double_at(index2);
822 if (d1 == d2) {
823 return true;
824 }
825 } break;
827 case JVM_CONSTANT_Fieldref:
828 case JVM_CONSTANT_InterfaceMethodref:
829 case JVM_CONSTANT_Methodref:
830 {
831 int recur1 = uncached_klass_ref_index_at(index1);
832 int recur2 = cp2->uncached_klass_ref_index_at(index2);
833 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
834 if (match) {
835 recur1 = uncached_name_and_type_ref_index_at(index1);
836 recur2 = cp2->uncached_name_and_type_ref_index_at(index2);
837 match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
838 if (match) {
839 return true;
840 }
841 }
842 } break;
844 case JVM_CONSTANT_Float:
845 {
846 jfloat f1 = float_at(index1);
847 jfloat f2 = cp2->float_at(index2);
848 if (f1 == f2) {
849 return true;
850 }
851 } break;
853 case JVM_CONSTANT_Integer:
854 {
855 jint i1 = int_at(index1);
856 jint i2 = cp2->int_at(index2);
857 if (i1 == i2) {
858 return true;
859 }
860 } break;
862 case JVM_CONSTANT_Long:
863 {
864 jlong l1 = long_at(index1);
865 jlong l2 = cp2->long_at(index2);
866 if (l1 == l2) {
867 return true;
868 }
869 } break;
871 case JVM_CONSTANT_NameAndType:
872 {
873 int recur1 = name_ref_index_at(index1);
874 int recur2 = cp2->name_ref_index_at(index2);
875 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
876 if (match) {
877 recur1 = signature_ref_index_at(index1);
878 recur2 = cp2->signature_ref_index_at(index2);
879 match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
880 if (match) {
881 return true;
882 }
883 }
884 } break;
886 case JVM_CONSTANT_String:
887 {
888 oop s1 = string_at(index1, CHECK_false);
889 oop s2 = cp2->string_at(index2, CHECK_false);
890 if (s1 == s2) {
891 return true;
892 }
893 } break;
895 case JVM_CONSTANT_StringIndex:
896 {
897 int recur1 = string_index_at(index1);
898 int recur2 = cp2->string_index_at(index2);
899 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
900 if (match) {
901 return true;
902 }
903 } break;
905 case JVM_CONSTANT_UnresolvedClass:
906 {
907 symbolOop k1 = unresolved_klass_at(index1);
908 symbolOop k2 = cp2->unresolved_klass_at(index2);
909 if (k1 == k2) {
910 return true;
911 }
912 } break;
914 case JVM_CONSTANT_MethodType:
915 {
916 int k1 = method_type_index_at(index1);
917 int k2 = cp2->method_type_index_at(index2);
918 if (k1 == k2) {
919 return true;
920 }
921 } break;
923 case JVM_CONSTANT_MethodHandle:
924 {
925 int k1 = method_handle_ref_kind_at(index1);
926 int k2 = cp2->method_handle_ref_kind_at(index2);
927 if (k1 == k2) {
928 int i1 = method_handle_index_at(index1);
929 int i2 = cp2->method_handle_index_at(index2);
930 if (i1 == i2) {
931 return true;
932 }
933 }
934 } break;
936 case JVM_CONSTANT_InvokeDynamic:
937 {
938 int op_count = multi_operand_count_at(index1);
939 if (op_count == cp2->multi_operand_count_at(index2)) {
940 bool all_equal = true;
941 for (int op_i = 0; op_i < op_count; op_i++) {
942 int k1 = multi_operand_ref_at(index1, op_i);
943 int k2 = cp2->multi_operand_ref_at(index2, op_i);
944 if (k1 != k2) {
945 all_equal = false;
946 break;
947 }
948 }
949 if (all_equal) {
950 return true; // got through loop; all elements equal
951 }
952 }
953 } break;
955 case JVM_CONSTANT_UnresolvedString:
956 {
957 symbolOop s1 = unresolved_string_at(index1);
958 symbolOop s2 = cp2->unresolved_string_at(index2);
959 if (s1 == s2) {
960 return true;
961 }
962 } break;
964 case JVM_CONSTANT_Utf8:
965 {
966 symbolOop s1 = symbol_at(index1);
967 symbolOop s2 = cp2->symbol_at(index2);
968 if (s1 == s2) {
969 return true;
970 }
971 } break;
973 // Invalid is used as the tag for the second constant pool entry
974 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
975 // not be seen by itself.
976 case JVM_CONSTANT_Invalid: // fall through
978 default:
979 ShouldNotReachHere();
980 break;
981 }
983 return false;
984 } // end compare_entry_to()
987 // Grow this->operands() to the indicated length, unless it is already at least that long.
988 void constantPoolOopDesc::multi_operand_buffer_grow(int min_length, TRAPS) {
989 int old_length = multi_operand_buffer_fill_pointer();
990 if (old_length >= min_length) return;
991 int new_length = min_length;
992 assert(new_length > _multi_operand_buffer_fill_pointer_offset, "");
993 typeArrayHandle new_operands = oopFactory::new_permanent_intArray(new_length, CHECK);
994 if (operands() == NULL) {
995 new_operands->int_at_put(_multi_operand_buffer_fill_pointer_offset, old_length);
996 } else {
997 // copy fill pointer and everything else
998 for (int i = 0; i < old_length; i++) {
999 new_operands->int_at_put(i, operands()->int_at(i));
1000 }
1001 }
1002 set_operands(new_operands());
1003 }
1006 // Copy this constant pool's entries at start_i to end_i (inclusive)
1007 // to the constant pool to_cp's entries starting at to_i. A total of
1008 // (end_i - start_i) + 1 entries are copied.
1009 void constantPoolOopDesc::copy_cp_to(int start_i, int end_i,
1010 constantPoolHandle to_cp, int to_i, TRAPS) {
1012 int dest_i = to_i; // leave original alone for debug purposes
1014 if (operands() != NULL) {
1015 // pre-grow the target CP's operand buffer
1016 int nops = this->multi_operand_buffer_fill_pointer();
1017 nops += to_cp->multi_operand_buffer_fill_pointer();
1018 to_cp->multi_operand_buffer_grow(nops, CHECK);
1019 }
1021 for (int src_i = start_i; src_i <= end_i; /* see loop bottom */ ) {
1022 copy_entry_to(src_i, to_cp, dest_i, CHECK);
1024 switch (tag_at(src_i).value()) {
1025 case JVM_CONSTANT_Double:
1026 case JVM_CONSTANT_Long:
1027 // double and long take two constant pool entries
1028 src_i += 2;
1029 dest_i += 2;
1030 break;
1032 default:
1033 // all others take one constant pool entry
1034 src_i++;
1035 dest_i++;
1036 break;
1037 }
1038 }
1039 } // end copy_cp_to()
1042 // Copy this constant pool's entry at from_i to the constant pool
1043 // to_cp's entry at to_i.
1044 void constantPoolOopDesc::copy_entry_to(int from_i, constantPoolHandle to_cp,
1045 int to_i, TRAPS) {
1047 switch (tag_at(from_i).value()) {
1048 case JVM_CONSTANT_Class:
1049 {
1050 klassOop k = klass_at(from_i, CHECK);
1051 to_cp->klass_at_put(to_i, k);
1052 } break;
1054 case JVM_CONSTANT_ClassIndex:
1055 {
1056 jint ki = klass_index_at(from_i);
1057 to_cp->klass_index_at_put(to_i, ki);
1058 } break;
1060 case JVM_CONSTANT_Double:
1061 {
1062 jdouble d = double_at(from_i);
1063 to_cp->double_at_put(to_i, d);
1064 // double takes two constant pool entries so init second entry's tag
1065 to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
1066 } break;
1068 case JVM_CONSTANT_Fieldref:
1069 {
1070 int class_index = uncached_klass_ref_index_at(from_i);
1071 int name_and_type_index = uncached_name_and_type_ref_index_at(from_i);
1072 to_cp->field_at_put(to_i, class_index, name_and_type_index);
1073 } break;
1075 case JVM_CONSTANT_Float:
1076 {
1077 jfloat f = float_at(from_i);
1078 to_cp->float_at_put(to_i, f);
1079 } break;
1081 case JVM_CONSTANT_Integer:
1082 {
1083 jint i = int_at(from_i);
1084 to_cp->int_at_put(to_i, i);
1085 } break;
1087 case JVM_CONSTANT_InterfaceMethodref:
1088 {
1089 int class_index = uncached_klass_ref_index_at(from_i);
1090 int name_and_type_index = uncached_name_and_type_ref_index_at(from_i);
1091 to_cp->interface_method_at_put(to_i, class_index, name_and_type_index);
1092 } break;
1094 case JVM_CONSTANT_Long:
1095 {
1096 jlong l = long_at(from_i);
1097 to_cp->long_at_put(to_i, l);
1098 // long takes two constant pool entries so init second entry's tag
1099 to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
1100 } break;
1102 case JVM_CONSTANT_Methodref:
1103 {
1104 int class_index = uncached_klass_ref_index_at(from_i);
1105 int name_and_type_index = uncached_name_and_type_ref_index_at(from_i);
1106 to_cp->method_at_put(to_i, class_index, name_and_type_index);
1107 } break;
1109 case JVM_CONSTANT_NameAndType:
1110 {
1111 int name_ref_index = name_ref_index_at(from_i);
1112 int signature_ref_index = signature_ref_index_at(from_i);
1113 to_cp->name_and_type_at_put(to_i, name_ref_index, signature_ref_index);
1114 } break;
1116 case JVM_CONSTANT_String:
1117 {
1118 oop s = string_at(from_i, CHECK);
1119 to_cp->string_at_put(to_i, s);
1120 } break;
1122 case JVM_CONSTANT_StringIndex:
1123 {
1124 jint si = string_index_at(from_i);
1125 to_cp->string_index_at_put(to_i, si);
1126 } break;
1128 case JVM_CONSTANT_UnresolvedClass:
1129 {
1130 symbolOop k = unresolved_klass_at(from_i);
1131 to_cp->unresolved_klass_at_put(to_i, k);
1132 } break;
1134 case JVM_CONSTANT_UnresolvedClassInError:
1135 {
1136 symbolOop k = unresolved_klass_at(from_i);
1137 to_cp->unresolved_klass_at_put(to_i, k);
1138 to_cp->tag_at_put(to_i, JVM_CONSTANT_UnresolvedClassInError);
1139 } break;
1142 case JVM_CONSTANT_UnresolvedString:
1143 {
1144 symbolOop s = unresolved_string_at(from_i);
1145 to_cp->unresolved_string_at_put(to_i, s);
1146 } break;
1148 case JVM_CONSTANT_Utf8:
1149 {
1150 symbolOop s = symbol_at(from_i);
1151 to_cp->symbol_at_put(to_i, s);
1152 } break;
1154 case JVM_CONSTANT_MethodType:
1155 {
1156 jint k = method_type_index_at(from_i);
1157 to_cp->method_type_index_at_put(to_i, k);
1158 } break;
1160 case JVM_CONSTANT_MethodHandle:
1161 {
1162 int k1 = method_handle_ref_kind_at(from_i);
1163 int k2 = method_handle_index_at(from_i);
1164 to_cp->method_handle_index_at_put(to_i, k1, k2);
1165 } break;
1167 case JVM_CONSTANT_InvokeDynamic:
1168 {
1169 int op_count = multi_operand_count_at(from_i);
1170 int fillp = to_cp->multi_operand_buffer_fill_pointer();
1171 int to_op_base = fillp - _multi_operand_count_offset; // fillp is count offset; get to base
1172 to_cp->multi_operand_buffer_grow(to_op_base + op_count, CHECK);
1173 to_cp->operands()->int_at_put(fillp++, op_count);
1174 assert(fillp == to_op_base + _multi_operand_base_offset, "just wrote count, will now write args");
1175 for (int op_i = 0; op_i < op_count; op_i++) {
1176 int op = multi_operand_ref_at(from_i, op_i);
1177 to_cp->operands()->int_at_put(fillp++, op);
1178 }
1179 assert(fillp <= to_cp->operands()->length(), "oob");
1180 to_cp->set_multi_operand_buffer_fill_pointer(fillp);
1181 to_cp->invoke_dynamic_at_put(to_i, to_op_base, op_count);
1182 #ifdef ASSERT
1183 int k1 = invoke_dynamic_bootstrap_method_ref_index_at(from_i);
1184 int k2 = invoke_dynamic_name_and_type_ref_index_at(from_i);
1185 int k3 = invoke_dynamic_argument_count_at(from_i);
1186 assert(to_cp->check_invoke_dynamic_at(to_i, k1, k2, k3),
1187 "indy structure is OK");
1188 #endif //ASSERT
1189 } break;
1191 // Invalid is used as the tag for the second constant pool entry
1192 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
1193 // not be seen by itself.
1194 case JVM_CONSTANT_Invalid: // fall through
1196 default:
1197 {
1198 jbyte bad_value = tag_at(from_i).value(); // leave a breadcrumb
1199 ShouldNotReachHere();
1200 } break;
1201 }
1202 } // end copy_entry_to()
1205 // Search constant pool search_cp for an entry that matches this
1206 // constant pool's entry at pattern_i. Returns the index of a
1207 // matching entry or zero (0) if there is no matching entry.
1208 int constantPoolOopDesc::find_matching_entry(int pattern_i,
1209 constantPoolHandle search_cp, TRAPS) {
1211 // index zero (0) is not used
1212 for (int i = 1; i < search_cp->length(); i++) {
1213 bool found = compare_entry_to(pattern_i, search_cp, i, CHECK_0);
1214 if (found) {
1215 return i;
1216 }
1217 }
1219 return 0; // entry not found; return unused index zero (0)
1220 } // end find_matching_entry()
1223 #ifndef PRODUCT
1225 const char* constantPoolOopDesc::printable_name_at(int which) {
1227 constantTag tag = tag_at(which);
1229 if (tag.is_unresolved_string() || tag.is_string()) {
1230 return string_at_noresolve(which);
1231 } else if (tag.is_klass() || tag.is_unresolved_klass()) {
1232 return klass_name_at(which)->as_C_string();
1233 } else if (tag.is_symbol()) {
1234 return symbol_at(which)->as_C_string();
1235 }
1236 return "";
1237 }
1239 #endif // PRODUCT
1242 // JVMTI GetConstantPool support
1244 // For temporary use until code is stable.
1245 #define DBG(code)
1247 static const char* WARN_MSG = "Must not be such entry!";
1249 static void print_cpool_bytes(jint cnt, u1 *bytes) {
1250 jint size = 0;
1251 u2 idx1, idx2;
1253 for (jint idx = 1; idx < cnt; idx++) {
1254 jint ent_size = 0;
1255 u1 tag = *bytes++;
1256 size++; // count tag
1258 printf("const #%03d, tag: %02d ", idx, tag);
1259 switch(tag) {
1260 case JVM_CONSTANT_Invalid: {
1261 printf("Invalid");
1262 break;
1263 }
1264 case JVM_CONSTANT_Unicode: {
1265 printf("Unicode %s", WARN_MSG);
1266 break;
1267 }
1268 case JVM_CONSTANT_Utf8: {
1269 u2 len = Bytes::get_Java_u2(bytes);
1270 char str[128];
1271 if (len > 127) {
1272 len = 127;
1273 }
1274 strncpy(str, (char *) (bytes+2), len);
1275 str[len] = '\0';
1276 printf("Utf8 \"%s\"", str);
1277 ent_size = 2 + len;
1278 break;
1279 }
1280 case JVM_CONSTANT_Integer: {
1281 u4 val = Bytes::get_Java_u4(bytes);
1282 printf("int %d", *(int *) &val);
1283 ent_size = 4;
1284 break;
1285 }
1286 case JVM_CONSTANT_Float: {
1287 u4 val = Bytes::get_Java_u4(bytes);
1288 printf("float %5.3ff", *(float *) &val);
1289 ent_size = 4;
1290 break;
1291 }
1292 case JVM_CONSTANT_Long: {
1293 u8 val = Bytes::get_Java_u8(bytes);
1294 printf("long "INT64_FORMAT, *(jlong *) &val);
1295 ent_size = 8;
1296 idx++; // Long takes two cpool slots
1297 break;
1298 }
1299 case JVM_CONSTANT_Double: {
1300 u8 val = Bytes::get_Java_u8(bytes);
1301 printf("double %5.3fd", *(jdouble *)&val);
1302 ent_size = 8;
1303 idx++; // Double takes two cpool slots
1304 break;
1305 }
1306 case JVM_CONSTANT_Class: {
1307 idx1 = Bytes::get_Java_u2(bytes);
1308 printf("class #%03d", idx1);
1309 ent_size = 2;
1310 break;
1311 }
1312 case JVM_CONSTANT_String: {
1313 idx1 = Bytes::get_Java_u2(bytes);
1314 printf("String #%03d", idx1);
1315 ent_size = 2;
1316 break;
1317 }
1318 case JVM_CONSTANT_Fieldref: {
1319 idx1 = Bytes::get_Java_u2(bytes);
1320 idx2 = Bytes::get_Java_u2(bytes+2);
1321 printf("Field #%03d, #%03d", (int) idx1, (int) idx2);
1322 ent_size = 4;
1323 break;
1324 }
1325 case JVM_CONSTANT_Methodref: {
1326 idx1 = Bytes::get_Java_u2(bytes);
1327 idx2 = Bytes::get_Java_u2(bytes+2);
1328 printf("Method #%03d, #%03d", idx1, idx2);
1329 ent_size = 4;
1330 break;
1331 }
1332 case JVM_CONSTANT_InterfaceMethodref: {
1333 idx1 = Bytes::get_Java_u2(bytes);
1334 idx2 = Bytes::get_Java_u2(bytes+2);
1335 printf("InterfMethod #%03d, #%03d", idx1, idx2);
1336 ent_size = 4;
1337 break;
1338 }
1339 case JVM_CONSTANT_NameAndType: {
1340 idx1 = Bytes::get_Java_u2(bytes);
1341 idx2 = Bytes::get_Java_u2(bytes+2);
1342 printf("NameAndType #%03d, #%03d", idx1, idx2);
1343 ent_size = 4;
1344 break;
1345 }
1346 case JVM_CONSTANT_ClassIndex: {
1347 printf("ClassIndex %s", WARN_MSG);
1348 break;
1349 }
1350 case JVM_CONSTANT_UnresolvedClass: {
1351 printf("UnresolvedClass: %s", WARN_MSG);
1352 break;
1353 }
1354 case JVM_CONSTANT_UnresolvedClassInError: {
1355 printf("UnresolvedClassInErr: %s", WARN_MSG);
1356 break;
1357 }
1358 case JVM_CONSTANT_StringIndex: {
1359 printf("StringIndex: %s", WARN_MSG);
1360 break;
1361 }
1362 case JVM_CONSTANT_UnresolvedString: {
1363 printf("UnresolvedString: %s", WARN_MSG);
1364 break;
1365 }
1366 }
1367 printf(";\n");
1368 bytes += ent_size;
1369 size += ent_size;
1370 }
1371 printf("Cpool size: %d\n", size);
1372 fflush(0);
1373 return;
1374 } /* end print_cpool_bytes */
1377 // Returns size of constant pool entry.
1378 jint constantPoolOopDesc::cpool_entry_size(jint idx) {
1379 switch(tag_at(idx).value()) {
1380 case JVM_CONSTANT_Invalid:
1381 case JVM_CONSTANT_Unicode:
1382 return 1;
1384 case JVM_CONSTANT_Utf8:
1385 return 3 + symbol_at(idx)->utf8_length();
1387 case JVM_CONSTANT_Class:
1388 case JVM_CONSTANT_String:
1389 case JVM_CONSTANT_ClassIndex:
1390 case JVM_CONSTANT_UnresolvedClass:
1391 case JVM_CONSTANT_UnresolvedClassInError:
1392 case JVM_CONSTANT_StringIndex:
1393 case JVM_CONSTANT_UnresolvedString:
1394 case JVM_CONSTANT_MethodType:
1395 return 3;
1397 case JVM_CONSTANT_MethodHandle:
1398 return 4; //tag, ref_kind, ref_index
1400 case JVM_CONSTANT_Integer:
1401 case JVM_CONSTANT_Float:
1402 case JVM_CONSTANT_Fieldref:
1403 case JVM_CONSTANT_Methodref:
1404 case JVM_CONSTANT_InterfaceMethodref:
1405 case JVM_CONSTANT_NameAndType:
1406 return 5;
1408 case JVM_CONSTANT_InvokeDynamic:
1409 // u1 tag, u2 bsm, u2 nt, u2 argc, u2 argv[argc]
1410 return 7 + 2 * invoke_dynamic_argument_count_at(idx);
1412 case JVM_CONSTANT_Long:
1413 case JVM_CONSTANT_Double:
1414 return 9;
1415 }
1416 assert(false, "cpool_entry_size: Invalid constant pool entry tag");
1417 return 1;
1418 } /* end cpool_entry_size */
1421 // SymbolHashMap is used to find a constant pool index from a string.
1422 // This function fills in SymbolHashMaps, one for utf8s and one for
1423 // class names, returns size of the cpool raw bytes.
1424 jint constantPoolOopDesc::hash_entries_to(SymbolHashMap *symmap,
1425 SymbolHashMap *classmap) {
1426 jint size = 0;
1428 for (u2 idx = 1; idx < length(); idx++) {
1429 u2 tag = tag_at(idx).value();
1430 size += cpool_entry_size(idx);
1432 switch(tag) {
1433 case JVM_CONSTANT_Utf8: {
1434 symbolOop sym = symbol_at(idx);
1435 symmap->add_entry(sym, idx);
1436 DBG(printf("adding symbol entry %s = %d\n", sym->as_utf8(), idx));
1437 break;
1438 }
1439 case JVM_CONSTANT_Class:
1440 case JVM_CONSTANT_UnresolvedClass:
1441 case JVM_CONSTANT_UnresolvedClassInError: {
1442 symbolOop sym = klass_name_at(idx);
1443 classmap->add_entry(sym, idx);
1444 DBG(printf("adding class entry %s = %d\n", sym->as_utf8(), idx));
1445 break;
1446 }
1447 case JVM_CONSTANT_Long:
1448 case JVM_CONSTANT_Double: {
1449 idx++; // Both Long and Double take two cpool slots
1450 break;
1451 }
1452 }
1453 }
1454 return size;
1455 } /* end hash_utf8_entries_to */
1458 // Copy cpool bytes.
1459 // Returns:
1460 // 0, in case of OutOfMemoryError
1461 // -1, in case of internal error
1462 // > 0, count of the raw cpool bytes that have been copied
1463 int constantPoolOopDesc::copy_cpool_bytes(int cpool_size,
1464 SymbolHashMap* tbl,
1465 unsigned char *bytes) {
1466 u2 idx1, idx2;
1467 jint size = 0;
1468 jint cnt = length();
1469 unsigned char *start_bytes = bytes;
1471 for (jint idx = 1; idx < cnt; idx++) {
1472 u1 tag = tag_at(idx).value();
1473 jint ent_size = cpool_entry_size(idx);
1475 assert(size + ent_size <= cpool_size, "Size mismatch");
1477 *bytes = tag;
1478 DBG(printf("#%03hd tag=%03hd, ", idx, tag));
1479 switch(tag) {
1480 case JVM_CONSTANT_Invalid: {
1481 DBG(printf("JVM_CONSTANT_Invalid"));
1482 break;
1483 }
1484 case JVM_CONSTANT_Unicode: {
1485 assert(false, "Wrong constant pool tag: JVM_CONSTANT_Unicode");
1486 DBG(printf("JVM_CONSTANT_Unicode"));
1487 break;
1488 }
1489 case JVM_CONSTANT_Utf8: {
1490 symbolOop sym = symbol_at(idx);
1491 char* str = sym->as_utf8();
1492 // Warning! It's crashing on x86 with len = sym->utf8_length()
1493 int len = (int) strlen(str);
1494 Bytes::put_Java_u2((address) (bytes+1), (u2) len);
1495 for (int i = 0; i < len; i++) {
1496 bytes[3+i] = (u1) str[i];
1497 }
1498 DBG(printf("JVM_CONSTANT_Utf8: %s ", str));
1499 break;
1500 }
1501 case JVM_CONSTANT_Integer: {
1502 jint val = int_at(idx);
1503 Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
1504 break;
1505 }
1506 case JVM_CONSTANT_Float: {
1507 jfloat val = float_at(idx);
1508 Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
1509 break;
1510 }
1511 case JVM_CONSTANT_Long: {
1512 jlong val = long_at(idx);
1513 Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
1514 idx++; // Long takes two cpool slots
1515 break;
1516 }
1517 case JVM_CONSTANT_Double: {
1518 jdouble val = double_at(idx);
1519 Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
1520 idx++; // Double takes two cpool slots
1521 break;
1522 }
1523 case JVM_CONSTANT_Class:
1524 case JVM_CONSTANT_UnresolvedClass:
1525 case JVM_CONSTANT_UnresolvedClassInError: {
1526 *bytes = JVM_CONSTANT_Class;
1527 symbolOop sym = klass_name_at(idx);
1528 idx1 = tbl->symbol_to_value(sym);
1529 assert(idx1 != 0, "Have not found a hashtable entry");
1530 Bytes::put_Java_u2((address) (bytes+1), idx1);
1531 DBG(printf("JVM_CONSTANT_Class: idx=#%03hd, %s", idx1, sym->as_utf8()));
1532 break;
1533 }
1534 case JVM_CONSTANT_String: {
1535 unsigned int hash;
1536 char *str = string_at_noresolve(idx);
1537 symbolOop sym = SymbolTable::lookup_only(str, (int) strlen(str), hash);
1538 if (sym == NULL) {
1539 // sym can be NULL if string refers to incorrectly encoded JVM_CONSTANT_Utf8
1540 // this can happen with JVM TI; see CR 6839599 for more details
1541 oop string = *(obj_at_addr(idx));
1542 assert(java_lang_String::is_instance(string),"Not a String");
1543 DBG(printf("Error #%03hd tag=%03hd\n", idx, tag));
1544 idx1 = 0;
1545 for (int j = 0; j < tbl->table_size() && idx1 == 0; j++) {
1546 for (SymbolHashMapEntry* cur = tbl->bucket(j); cur != NULL; cur = cur->next()) {
1547 int length;
1548 sym = cur->symbol();
1549 jchar* chars = sym->as_unicode(length);
1550 if (java_lang_String::equals(string, chars, length)) {
1551 idx1 = cur->value();
1552 DBG(printf("Index found: %d\n",idx1));
1553 break;
1554 }
1555 }
1556 }
1557 } else {
1558 idx1 = tbl->symbol_to_value(sym);
1559 }
1560 assert(idx1 != 0, "Have not found a hashtable entry");
1561 Bytes::put_Java_u2((address) (bytes+1), idx1);
1562 DBG(printf("JVM_CONSTANT_String: idx=#%03hd, %s", idx1, str));
1563 break;
1564 }
1565 case JVM_CONSTANT_UnresolvedString: {
1566 *bytes = JVM_CONSTANT_String;
1567 symbolOop sym = unresolved_string_at(idx);
1568 idx1 = tbl->symbol_to_value(sym);
1569 assert(idx1 != 0, "Have not found a hashtable entry");
1570 Bytes::put_Java_u2((address) (bytes+1), idx1);
1571 DBG(char *str = sym->as_utf8());
1572 DBG(printf("JVM_CONSTANT_UnresolvedString: idx=#%03hd, %s", idx1, str));
1573 break;
1574 }
1575 case JVM_CONSTANT_Fieldref:
1576 case JVM_CONSTANT_Methodref:
1577 case JVM_CONSTANT_InterfaceMethodref: {
1578 idx1 = uncached_klass_ref_index_at(idx);
1579 idx2 = uncached_name_and_type_ref_index_at(idx);
1580 Bytes::put_Java_u2((address) (bytes+1), idx1);
1581 Bytes::put_Java_u2((address) (bytes+3), idx2);
1582 DBG(printf("JVM_CONSTANT_Methodref: %hd %hd", idx1, idx2));
1583 break;
1584 }
1585 case JVM_CONSTANT_NameAndType: {
1586 idx1 = name_ref_index_at(idx);
1587 idx2 = signature_ref_index_at(idx);
1588 Bytes::put_Java_u2((address) (bytes+1), idx1);
1589 Bytes::put_Java_u2((address) (bytes+3), idx2);
1590 DBG(printf("JVM_CONSTANT_NameAndType: %hd %hd", idx1, idx2));
1591 break;
1592 }
1593 case JVM_CONSTANT_ClassIndex: {
1594 *bytes = JVM_CONSTANT_Class;
1595 idx1 = klass_index_at(idx);
1596 Bytes::put_Java_u2((address) (bytes+1), idx1);
1597 DBG(printf("JVM_CONSTANT_ClassIndex: %hd", idx1));
1598 break;
1599 }
1600 case JVM_CONSTANT_StringIndex: {
1601 *bytes = JVM_CONSTANT_String;
1602 idx1 = string_index_at(idx);
1603 Bytes::put_Java_u2((address) (bytes+1), idx1);
1604 DBG(printf("JVM_CONSTANT_StringIndex: %hd", idx1));
1605 break;
1606 }
1607 case JVM_CONSTANT_MethodHandle: {
1608 *bytes = JVM_CONSTANT_MethodHandle;
1609 int kind = method_handle_ref_kind_at(idx);
1610 idx1 = method_handle_index_at(idx);
1611 *(bytes+1) = (unsigned char) kind;
1612 Bytes::put_Java_u2((address) (bytes+2), idx1);
1613 DBG(printf("JVM_CONSTANT_MethodHandle: %d %hd", kind, idx1));
1614 break;
1615 }
1616 case JVM_CONSTANT_MethodType: {
1617 *bytes = JVM_CONSTANT_MethodType;
1618 idx1 = method_type_index_at(idx);
1619 Bytes::put_Java_u2((address) (bytes+1), idx1);
1620 DBG(printf("JVM_CONSTANT_MethodType: %hd", idx1));
1621 break;
1622 }
1623 case JVM_CONSTANT_InvokeDynamic: {
1624 *bytes = JVM_CONSTANT_InvokeDynamic;
1625 idx1 = invoke_dynamic_bootstrap_method_ref_index_at(idx);
1626 idx2 = invoke_dynamic_name_and_type_ref_index_at(idx);
1627 int argc = invoke_dynamic_argument_count_at(idx);
1628 Bytes::put_Java_u2((address) (bytes+1), idx1);
1629 Bytes::put_Java_u2((address) (bytes+3), idx2);
1630 Bytes::put_Java_u2((address) (bytes+5), argc);
1631 for (int arg_i = 0; arg_i < argc; arg_i++) {
1632 int arg = invoke_dynamic_argument_index_at(idx, arg_i);
1633 Bytes::put_Java_u2((address) (bytes+7+2*arg_i), arg);
1634 }
1635 DBG(printf("JVM_CONSTANT_InvokeDynamic: %hd %hd [%d]", idx1, idx2, argc));
1636 break;
1637 }
1638 }
1639 DBG(printf("\n"));
1640 bytes += ent_size;
1641 size += ent_size;
1642 }
1643 assert(size == cpool_size, "Size mismatch");
1645 // Keep temorarily for debugging until it's stable.
1646 DBG(print_cpool_bytes(cnt, start_bytes));
1647 return (int)(bytes - start_bytes);
1648 } /* end copy_cpool_bytes */
1651 void SymbolHashMap::add_entry(symbolOop sym, u2 value) {
1652 char *str = sym->as_utf8();
1653 unsigned int hash = compute_hash(str, sym->utf8_length());
1654 unsigned int index = hash % table_size();
1656 // check if already in map
1657 // we prefer the first entry since it is more likely to be what was used in
1658 // the class file
1659 for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
1660 assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
1661 if (en->hash() == hash && en->symbol() == sym) {
1662 return; // already there
1663 }
1664 }
1666 SymbolHashMapEntry* entry = new SymbolHashMapEntry(hash, sym, value);
1667 entry->set_next(bucket(index));
1668 _buckets[index].set_entry(entry);
1669 assert(entry->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
1670 }
1672 SymbolHashMapEntry* SymbolHashMap::find_entry(symbolOop sym) {
1673 assert(sym != NULL, "SymbolHashMap::find_entry - symbol is NULL");
1674 char *str = sym->as_utf8();
1675 int len = sym->utf8_length();
1676 unsigned int hash = SymbolHashMap::compute_hash(str, len);
1677 unsigned int index = hash % table_size();
1678 for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
1679 assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
1680 if (en->hash() == hash && en->symbol() == sym) {
1681 return en;
1682 }
1683 }
1684 return NULL;
1685 }