24
25 // A constantPool is an array containing class constants as described in the
26 // class file.
27 //
28 // Most of the constant pool entries are written during class parsing, which
29 // is safe. For klass and string types, the constant pool entry is
30 // modified when the entry is resolved. If a klass or string constant pool
31 // entry is read without a lock, only the resolved state guarantees that
32 // the entry in the constant pool is a klass or String object and
33 // not a symbolOop.
34
35 class SymbolHashMap;
36
37 class constantPoolOopDesc : public oopDesc {
38 friend class VMStructs;
39 friend class BytecodeInterpreter; // Directly extracts an oop in the pool for fast instanceof/checkcast
40 private:
41 typeArrayOop _tags; // the tag array describing the constant pool's contents
42 constantPoolCacheOop _cache; // the cache holding interpreter runtime information
43 klassOop _pool_holder; // the corresponding class
44 int _length; // number of elements in the array
45 // only set to non-zero if constant pool is merged by RedefineClasses
46 int _orig_length;
47
48 void set_tags(typeArrayOop tags) { oop_store_without_check((oop*)&_tags, tags); }
49 void tag_at_put(int which, jbyte t) { tags()->byte_at_put(which, t); }
50 void release_tag_at_put(int which, jbyte t) { tags()->release_byte_at_put(which, t); }
51
52 private:
53 intptr_t* base() const { return (intptr_t*) (((char*) this) + sizeof(constantPoolOopDesc)); }
54 oop* tags_addr() { return (oop*)&_tags; }
55 oop* cache_addr() { return (oop*)&_cache; }
56
57 oop* obj_at_addr(int which) const {
58 assert(is_within_bounds(which), "index out of bounds");
59 return (oop*) &base()[which];
60 }
61
62 jint* int_at_addr(int which) const {
63 assert(is_within_bounds(which), "index out of bounds");
64 return (jint*) &base()[which];
65 }
66
67 jlong* long_at_addr(int which) const {
68 assert(is_within_bounds(which), "index out of bounds");
69 return (jlong*) &base()[which];
70 }
71
72 jfloat* float_at_addr(int which) const {
73 assert(is_within_bounds(which), "index out of bounds");
74 return (jfloat*) &base()[which];
75 }
76
77 jdouble* double_at_addr(int which) const {
78 assert(is_within_bounds(which), "index out of bounds");
79 return (jdouble*) &base()[which];
80 }
81
82 public:
83 typeArrayOop tags() const { return _tags; }
84
85 // Klass holding pool
86 klassOop pool_holder() const { return _pool_holder; }
87 void set_pool_holder(klassOop k) { oop_store_without_check((oop*)&_pool_holder, (oop) k); }
88 oop* pool_holder_addr() { return (oop*)&_pool_holder; }
89
90 // Interpreter runtime support
91 constantPoolCacheOop cache() const { return _cache; }
92 void set_cache(constantPoolCacheOop cache){ oop_store((oop*)&_cache, cache); }
93
94 // Assembly code support
95 static int tags_offset_in_bytes() { return offset_of(constantPoolOopDesc, _tags); }
96 static int cache_offset_in_bytes() { return offset_of(constantPoolOopDesc, _cache); }
97 static int pool_holder_offset_in_bytes() { return offset_of(constantPoolOopDesc, _pool_holder); }
98
99 // Storing constants
100
101 void klass_at_put(int which, klassOop k) {
102 oop_store_without_check((volatile oop *)obj_at_addr(which), oop(k));
103 // The interpreter assumes when the tag is stored, the klass is resolved
104 // and the klassOop is a klass rather than a symbolOop, so we need
255 assert(tag_at(which).is_float(), "Corrupted constant pool");
256 return *float_at_addr(which);
257 }
258
259 jdouble double_at(int which) {
260 assert(tag_at(which).is_double(), "Corrupted constant pool");
261 u8 tmp = Bytes::get_native_u8((address)&base()[which]);
262 return *((jdouble*)&tmp);
263 }
264
265 symbolOop symbol_at(int which) {
266 assert(tag_at(which).is_utf8(), "Corrupted constant pool");
267 return symbolOop(*obj_at_addr(which));
268 }
269
270 oop string_at(int which, TRAPS) {
271 constantPoolHandle h_this(THREAD, this);
272 return string_at_impl(h_this, which, CHECK_NULL);
273 }
274
275 // only called when we are sure a string entry is already resolved (via an
276 // earlier string_at call.
277 oop resolved_string_at(int which) {
278 assert(tag_at(which).is_string(), "Corrupted constant pool");
279 // Must do an acquire here in case another thread resolved the klass
280 // behind our back, lest we later load stale values thru the oop.
281 return (oop)OrderAccess::load_ptr_acquire(obj_at_addr(which));
282 }
283
284 // This method should only be used with a cpool lock or during parsing or gc
285 symbolOop unresolved_string_at(int which) { // Temporary until actual use
286 symbolOop s = symbolOop((oop)OrderAccess::load_ptr_acquire(obj_at_addr(which)));
287 // check that the string is still unresolved.
288 assert(tag_at(which).is_unresolved_string(), "Corrupted constant pool");
289 return s;
290 }
291
292 // Returns an UTF8 for a CONSTANT_String entry at a given index.
293 // UTF8 char* representation was chosen to avoid conversion of
294 // java_lang_Strings at resolved entries into symbolOops
295 // or vice versa.
296 char* string_at_noresolve(int which);
297
298 jint name_and_type_at(int which) {
299 assert(tag_at(which).is_name_and_type(), "Corrupted constant pool");
300 return *int_at_addr(which);
301 }
302
303 // The following methods (klass_ref_at, klass_ref_at_noresolve, name_ref_at,
304 // signature_ref_at, klass_ref_index_at, name_and_type_ref_index_at,
305 // name_ref_index_at, signature_ref_index_at) all expect constant pool indices
306 // from the bytecodes to be passed in, which are actually potentially byte-swapped
307 // contstant pool cache indices. See field_or_method_at.
308
309 // Lookup for entries consisting of (klass_index, name_and_type index)
310 klassOop klass_ref_at(int which, TRAPS);
311 symbolOop klass_ref_at_noresolve(int which);
312 symbolOop name_ref_at(int which);
313 symbolOop signature_ref_at(int which); // the type descriptor
314
315 int klass_ref_index_at(int which);
|
24
25 // A constantPool is an array containing class constants as described in the
26 // class file.
27 //
28 // Most of the constant pool entries are written during class parsing, which
29 // is safe. For klass and string types, the constant pool entry is
30 // modified when the entry is resolved. If a klass or string constant pool
31 // entry is read without a lock, only the resolved state guarantees that
32 // the entry in the constant pool is a klass or String object and
33 // not a symbolOop.
34
35 class SymbolHashMap;
36
37 class constantPoolOopDesc : public oopDesc {
38 friend class VMStructs;
39 friend class BytecodeInterpreter; // Directly extracts an oop in the pool for fast instanceof/checkcast
40 private:
41 typeArrayOop _tags; // the tag array describing the constant pool's contents
42 constantPoolCacheOop _cache; // the cache holding interpreter runtime information
43 klassOop _pool_holder; // the corresponding class
44 int _flags; // a few header bits to describe contents for GC
45 int _length; // number of elements in the array
46 // only set to non-zero if constant pool is merged by RedefineClasses
47 int _orig_length;
48
49 void set_tags(typeArrayOop tags) { oop_store_without_check((oop*)&_tags, tags); }
50 void tag_at_put(int which, jbyte t) { tags()->byte_at_put(which, t); }
51 void release_tag_at_put(int which, jbyte t) { tags()->release_byte_at_put(which, t); }
52
53 enum FlagBit {
54 FB_has_pseudo_string = 2
55 };
56
57 int flags() const { return _flags; }
58 void set_flags(int f) { _flags = f; }
59 bool flag_at(FlagBit fb) const { return (_flags & (1 << (int)fb)) != 0; }
60 void set_flag_at(FlagBit fb);
61 // no clear_flag_at function; they only increase
62
63 private:
64 intptr_t* base() const { return (intptr_t*) (((char*) this) + sizeof(constantPoolOopDesc)); }
65 oop* tags_addr() { return (oop*)&_tags; }
66 oop* cache_addr() { return (oop*)&_cache; }
67
68 oop* obj_at_addr(int which) const {
69 assert(is_within_bounds(which), "index out of bounds");
70 return (oop*) &base()[which];
71 }
72
73 jint* int_at_addr(int which) const {
74 assert(is_within_bounds(which), "index out of bounds");
75 return (jint*) &base()[which];
76 }
77
78 jlong* long_at_addr(int which) const {
79 assert(is_within_bounds(which), "index out of bounds");
80 return (jlong*) &base()[which];
81 }
82
83 jfloat* float_at_addr(int which) const {
84 assert(is_within_bounds(which), "index out of bounds");
85 return (jfloat*) &base()[which];
86 }
87
88 jdouble* double_at_addr(int which) const {
89 assert(is_within_bounds(which), "index out of bounds");
90 return (jdouble*) &base()[which];
91 }
92
93 public:
94 typeArrayOop tags() const { return _tags; }
95
96 bool has_pseudo_string() const { return flag_at(FB_has_pseudo_string); }
97 void set_pseudo_string() { set_flag_at(FB_has_pseudo_string); }
98
99 // Klass holding pool
100 klassOop pool_holder() const { return _pool_holder; }
101 void set_pool_holder(klassOop k) { oop_store_without_check((oop*)&_pool_holder, (oop) k); }
102 oop* pool_holder_addr() { return (oop*)&_pool_holder; }
103
104 // Interpreter runtime support
105 constantPoolCacheOop cache() const { return _cache; }
106 void set_cache(constantPoolCacheOop cache){ oop_store((oop*)&_cache, cache); }
107
108 // Assembly code support
109 static int tags_offset_in_bytes() { return offset_of(constantPoolOopDesc, _tags); }
110 static int cache_offset_in_bytes() { return offset_of(constantPoolOopDesc, _cache); }
111 static int pool_holder_offset_in_bytes() { return offset_of(constantPoolOopDesc, _pool_holder); }
112
113 // Storing constants
114
115 void klass_at_put(int which, klassOop k) {
116 oop_store_without_check((volatile oop *)obj_at_addr(which), oop(k));
117 // The interpreter assumes when the tag is stored, the klass is resolved
118 // and the klassOop is a klass rather than a symbolOop, so we need
269 assert(tag_at(which).is_float(), "Corrupted constant pool");
270 return *float_at_addr(which);
271 }
272
273 jdouble double_at(int which) {
274 assert(tag_at(which).is_double(), "Corrupted constant pool");
275 u8 tmp = Bytes::get_native_u8((address)&base()[which]);
276 return *((jdouble*)&tmp);
277 }
278
279 symbolOop symbol_at(int which) {
280 assert(tag_at(which).is_utf8(), "Corrupted constant pool");
281 return symbolOop(*obj_at_addr(which));
282 }
283
284 oop string_at(int which, TRAPS) {
285 constantPoolHandle h_this(THREAD, this);
286 return string_at_impl(h_this, which, CHECK_NULL);
287 }
288
289 // A "pseudo-string" is an non-string oop that has found is way into
290 // a String entry.
291 // Under AnonymousClasses this can happen if the user patches a live
292 // object into a CONSTANT_String entry of an anonymous class.
293 // Method oops internally created for method handles may also
294 // use pseudo-strings to link themselves to related metaobjects.
295
296 bool is_pseudo_string_at(int which);
297
298 oop pseudo_string_at(int which) {
299 assert(tag_at(which).is_string(), "Corrupted constant pool");
300 return *obj_at_addr(which);
301 }
302
303 void pseudo_string_at_put(int which, oop x) {
304 assert(AnonymousClasses, "");
305 set_pseudo_string(); // mark header
306 assert(tag_at(which).is_string(), "Corrupted constant pool");
307 string_at_put(which, x); // this works just fine
308 }
309
310 // only called when we are sure a string entry is already resolved (via an
311 // earlier string_at call.
312 oop resolved_string_at(int which) {
313 assert(tag_at(which).is_string(), "Corrupted constant pool");
314 // Must do an acquire here in case another thread resolved the klass
315 // behind our back, lest we later load stale values thru the oop.
316 return (oop)OrderAccess::load_ptr_acquire(obj_at_addr(which));
317 }
318
319 // This method should only be used with a cpool lock or during parsing or gc
320 symbolOop unresolved_string_at(int which) { // Temporary until actual use
321 symbolOop s = symbolOop((oop)OrderAccess::load_ptr_acquire(obj_at_addr(which)));
322 // check that the string is still unresolved.
323 assert(tag_at(which).is_unresolved_string(), "Corrupted constant pool");
324 return s;
325 }
326
327 // Returns an UTF8 for a CONSTANT_String entry at a given index.
328 // UTF8 char* representation was chosen to avoid conversion of
329 // java_lang_Strings at resolved entries into symbolOops
330 // or vice versa.
331 // Caller is responsible for checking for pseudo-strings.
332 char* string_at_noresolve(int which);
333
334 jint name_and_type_at(int which) {
335 assert(tag_at(which).is_name_and_type(), "Corrupted constant pool");
336 return *int_at_addr(which);
337 }
338
339 // The following methods (klass_ref_at, klass_ref_at_noresolve, name_ref_at,
340 // signature_ref_at, klass_ref_index_at, name_and_type_ref_index_at,
341 // name_ref_index_at, signature_ref_index_at) all expect constant pool indices
342 // from the bytecodes to be passed in, which are actually potentially byte-swapped
343 // contstant pool cache indices. See field_or_method_at.
344
345 // Lookup for entries consisting of (klass_index, name_and_type index)
346 klassOop klass_ref_at(int which, TRAPS);
347 symbolOop klass_ref_at_noresolve(int which);
348 symbolOop name_ref_at(int which);
349 symbolOop signature_ref_at(int which); // the type descriptor
350
351 int klass_ref_index_at(int which);
|