Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/oops/constantPool.hpp
32285 views
1
/*
2
* Copyright (c) 1997, 2018, 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
*/
24
25
#ifndef SHARE_VM_OOPS_CONSTANTPOOLOOP_HPP
26
#define SHARE_VM_OOPS_CONSTANTPOOLOOP_HPP
27
28
#include "oops/arrayOop.hpp"
29
#include "oops/cpCache.hpp"
30
#include "oops/objArrayOop.hpp"
31
#include "oops/symbol.hpp"
32
#include "oops/typeArrayOop.hpp"
33
#include "runtime/handles.hpp"
34
#include "utilities/constantTag.hpp"
35
#ifdef TARGET_ARCH_x86
36
# include "bytes_x86.hpp"
37
#endif
38
#ifdef TARGET_ARCH_aarch32
39
# include "bytes_aarch32.hpp"
40
#endif
41
#ifdef TARGET_ARCH_aarch64
42
# include "bytes_aarch64.hpp"
43
#endif
44
#ifdef TARGET_ARCH_sparc
45
# include "bytes_sparc.hpp"
46
#endif
47
#ifdef TARGET_ARCH_zero
48
# include "bytes_zero.hpp"
49
#endif
50
#ifdef TARGET_ARCH_arm
51
# include "bytes_arm.hpp"
52
#endif
53
#ifdef TARGET_ARCH_ppc
54
# include "bytes_ppc.hpp"
55
#endif
56
57
// A constantPool is an array containing class constants as described in the
58
// class file.
59
//
60
// Most of the constant pool entries are written during class parsing, which
61
// is safe. For klass types, the constant pool entry is
62
// modified when the entry is resolved. If a klass constant pool
63
// entry is read without a lock, only the resolved state guarantees that
64
// the entry in the constant pool is a klass object and not a Symbol*.
65
66
class SymbolHashMap;
67
68
class CPSlot VALUE_OBJ_CLASS_SPEC {
69
intptr_t _ptr;
70
public:
71
CPSlot(intptr_t ptr): _ptr(ptr) {}
72
CPSlot(Klass* ptr): _ptr((intptr_t)ptr) {}
73
CPSlot(Symbol* ptr): _ptr((intptr_t)ptr | 1) {}
74
75
intptr_t value() { return _ptr; }
76
bool is_resolved() { return (_ptr & 1) == 0; }
77
bool is_unresolved() { return (_ptr & 1) == 1; }
78
79
Symbol* get_symbol() {
80
assert(is_unresolved(), "bad call");
81
return (Symbol*)(_ptr & ~1);
82
}
83
Klass* get_klass() {
84
assert(is_resolved(), "bad call");
85
return (Klass*)_ptr;
86
}
87
};
88
89
class KlassSizeStats;
90
class ConstantPool : public Metadata {
91
friend class VMStructs;
92
friend class BytecodeInterpreter; // Directly extracts an oop in the pool for fast instanceof/checkcast
93
friend class Universe; // For null constructor
94
private:
95
Array<u1>* _tags; // the tag array describing the constant pool's contents
96
ConstantPoolCache* _cache; // the cache holding interpreter runtime information
97
InstanceKlass* _pool_holder; // the corresponding class
98
Array<u2>* _operands; // for variable-sized (InvokeDynamic) nodes, usually empty
99
100
// Array of resolved objects from the constant pool and map from resolved
101
// object index to original constant pool index
102
jobject _resolved_references;
103
Array<u2>* _reference_map;
104
105
enum {
106
_has_preresolution = 1, // Flags
107
_on_stack = 2
108
};
109
110
int _flags; // old fashioned bit twiddling
111
int _length; // number of elements in the array
112
113
union {
114
// set for CDS to restore resolved references
115
int _resolved_reference_length;
116
// keeps version number for redefined classes (used in backtrace)
117
int _version;
118
} _saved;
119
120
Monitor* _lock;
121
122
void set_tags(Array<u1>* tags) { _tags = tags; }
123
void tag_at_put(int which, jbyte t) { tags()->at_put(which, t); }
124
void release_tag_at_put(int which, jbyte t) { tags()->release_at_put(which, t); }
125
126
void set_operands(Array<u2>* operands) { _operands = operands; }
127
128
int flags() const { return _flags; }
129
void set_flags(int f) { _flags = f; }
130
131
private:
132
intptr_t* base() const { return (intptr_t*) (((char*) this) + sizeof(ConstantPool)); }
133
134
CPSlot slot_at(int which) const {
135
assert(is_within_bounds(which), "index out of bounds");
136
// Uses volatile because the klass slot changes without a lock.
137
volatile intptr_t adr = (intptr_t)OrderAccess::load_ptr_acquire(obj_at_addr_raw(which));
138
assert(adr != 0 || which == 0, "cp entry for klass should not be zero");
139
return CPSlot(adr);
140
}
141
142
void slot_at_put(int which, CPSlot s) const {
143
assert(is_within_bounds(which), "index out of bounds");
144
assert(s.value() != 0, "Caught something");
145
*(intptr_t*)&base()[which] = s.value();
146
}
147
intptr_t* obj_at_addr_raw(int which) const {
148
assert(is_within_bounds(which), "index out of bounds");
149
return (intptr_t*) &base()[which];
150
}
151
152
jint* int_at_addr(int which) const {
153
assert(is_within_bounds(which), "index out of bounds");
154
return (jint*) &base()[which];
155
}
156
157
jlong* long_at_addr(int which) const {
158
assert(is_within_bounds(which), "index out of bounds");
159
return (jlong*) &base()[which];
160
}
161
162
jfloat* float_at_addr(int which) const {
163
assert(is_within_bounds(which), "index out of bounds");
164
return (jfloat*) &base()[which];
165
}
166
167
jdouble* double_at_addr(int which) const {
168
assert(is_within_bounds(which), "index out of bounds");
169
return (jdouble*) &base()[which];
170
}
171
172
ConstantPool(Array<u1>* tags);
173
ConstantPool() { assert(DumpSharedSpaces || UseSharedSpaces, "only for CDS"); }
174
public:
175
static ConstantPool* allocate(ClassLoaderData* loader_data, int length, TRAPS);
176
177
bool is_constantPool() const volatile { return true; }
178
179
Array<u1>* tags() const { return _tags; }
180
Array<u2>* operands() const { return _operands; }
181
182
bool has_preresolution() const { return (_flags & _has_preresolution) != 0; }
183
void set_has_preresolution() { _flags |= _has_preresolution; }
184
185
// Redefine classes support. If a method refering to this constant pool
186
// is on the executing stack, or as a handle in vm code, this constant pool
187
// can't be removed from the set of previous versions saved in the instance
188
// class.
189
bool on_stack() const { return (_flags &_on_stack) != 0; }
190
void set_on_stack(const bool value);
191
192
// Klass holding pool
193
InstanceKlass* pool_holder() const { return _pool_holder; }
194
void set_pool_holder(InstanceKlass* k) { _pool_holder = k; }
195
InstanceKlass** pool_holder_addr() { return &_pool_holder; }
196
197
// Interpreter runtime support
198
ConstantPoolCache* cache() const { return _cache; }
199
void set_cache(ConstantPoolCache* cache){ _cache = cache; }
200
201
// Create object cache in the constant pool
202
void initialize_resolved_references(ClassLoaderData* loader_data,
203
intStack reference_map,
204
int constant_pool_map_length,
205
TRAPS);
206
207
// resolved strings, methodHandles and callsite objects from the constant pool
208
objArrayOop resolved_references() const;
209
objArrayOop resolved_references_or_null() const;
210
// mapping resolved object array indexes to cp indexes and back.
211
int object_to_cp_index(int index) { return _reference_map->at(index); }
212
int cp_to_object_index(int index);
213
214
// Invokedynamic indexes.
215
// They must look completely different from normal indexes.
216
// The main reason is that byte swapping is sometimes done on normal indexes.
217
// Finally, it is helpful for debugging to tell the two apart.
218
static bool is_invokedynamic_index(int i) { return (i < 0); }
219
static int decode_invokedynamic_index(int i) { assert(is_invokedynamic_index(i), ""); return ~i; }
220
static int encode_invokedynamic_index(int i) { assert(!is_invokedynamic_index(i), ""); return ~i; }
221
222
223
// The invokedynamic points at a CP cache entry. This entry points back
224
// at the original CP entry (CONSTANT_InvokeDynamic) and also (via f2) at an entry
225
// in the resolved_references array (which provides the appendix argument).
226
int invokedynamic_cp_cache_index(int index) const {
227
assert (is_invokedynamic_index(index), "should be a invokedynamic index");
228
int cache_index = decode_invokedynamic_index(index);
229
return cache_index;
230
}
231
ConstantPoolCacheEntry* invokedynamic_cp_cache_entry_at(int index) const {
232
// decode index that invokedynamic points to.
233
int cp_cache_index = invokedynamic_cp_cache_index(index);
234
return cache()->entry_at(cp_cache_index);
235
}
236
237
// Assembly code support
238
static int tags_offset_in_bytes() { return offset_of(ConstantPool, _tags); }
239
static int cache_offset_in_bytes() { return offset_of(ConstantPool, _cache); }
240
static int pool_holder_offset_in_bytes() { return offset_of(ConstantPool, _pool_holder); }
241
static int resolved_references_offset_in_bytes() { return offset_of(ConstantPool, _resolved_references); }
242
243
// Storing constants
244
245
void klass_at_put(int which, Klass* k) {
246
assert(k != NULL, "resolved class shouldn't be null");
247
assert(is_within_bounds(which), "index out of bounds");
248
OrderAccess::release_store_ptr((Klass* volatile *)obj_at_addr_raw(which), k);
249
// The interpreter assumes when the tag is stored, the klass is resolved
250
// and the Klass* is a klass rather than a Symbol*, so we need
251
// hardware store ordering here.
252
release_tag_at_put(which, JVM_CONSTANT_Class);
253
}
254
255
// For temporary use while constructing constant pool
256
void klass_index_at_put(int which, int name_index) {
257
tag_at_put(which, JVM_CONSTANT_ClassIndex);
258
*int_at_addr(which) = name_index;
259
}
260
261
// Temporary until actual use
262
void unresolved_klass_at_put(int which, Symbol* s) {
263
release_tag_at_put(which, JVM_CONSTANT_UnresolvedClass);
264
slot_at_put(which, s);
265
}
266
267
void method_handle_index_at_put(int which, int ref_kind, int ref_index) {
268
tag_at_put(which, JVM_CONSTANT_MethodHandle);
269
*int_at_addr(which) = ((jint) ref_index<<16) | ref_kind;
270
}
271
272
void method_type_index_at_put(int which, int ref_index) {
273
tag_at_put(which, JVM_CONSTANT_MethodType);
274
*int_at_addr(which) = ref_index;
275
}
276
277
void invoke_dynamic_at_put(int which, int bootstrap_specifier_index, int name_and_type_index) {
278
tag_at_put(which, JVM_CONSTANT_InvokeDynamic);
279
*int_at_addr(which) = ((jint) name_and_type_index<<16) | bootstrap_specifier_index;
280
}
281
282
void unresolved_string_at_put(int which, Symbol* s) {
283
release_tag_at_put(which, JVM_CONSTANT_String);
284
*symbol_at_addr(which) = s;
285
}
286
287
void int_at_put(int which, jint i) {
288
tag_at_put(which, JVM_CONSTANT_Integer);
289
*int_at_addr(which) = i;
290
}
291
292
void long_at_put(int which, jlong l) {
293
tag_at_put(which, JVM_CONSTANT_Long);
294
// *long_at_addr(which) = l;
295
Bytes::put_native_u8((address)long_at_addr(which), *((u8*) &l));
296
}
297
298
void float_at_put(int which, jfloat f) {
299
tag_at_put(which, JVM_CONSTANT_Float);
300
*float_at_addr(which) = f;
301
}
302
303
void double_at_put(int which, jdouble d) {
304
tag_at_put(which, JVM_CONSTANT_Double);
305
// *double_at_addr(which) = d;
306
// u8 temp = *(u8*) &d;
307
Bytes::put_native_u8((address) double_at_addr(which), *((u8*) &d));
308
}
309
310
Symbol** symbol_at_addr(int which) const {
311
assert(is_within_bounds(which), "index out of bounds");
312
return (Symbol**) &base()[which];
313
}
314
315
void symbol_at_put(int which, Symbol* s) {
316
assert(s->refcount() != 0, "should have nonzero refcount");
317
tag_at_put(which, JVM_CONSTANT_Utf8);
318
*symbol_at_addr(which) = s;
319
}
320
321
void string_at_put(int which, int obj_index, oop str) {
322
resolved_references()->obj_at_put(obj_index, str);
323
}
324
325
// For temporary use while constructing constant pool
326
void string_index_at_put(int which, int string_index) {
327
tag_at_put(which, JVM_CONSTANT_StringIndex);
328
*int_at_addr(which) = string_index;
329
}
330
331
void field_at_put(int which, int class_index, int name_and_type_index) {
332
tag_at_put(which, JVM_CONSTANT_Fieldref);
333
*int_at_addr(which) = ((jint) name_and_type_index<<16) | class_index;
334
}
335
336
void method_at_put(int which, int class_index, int name_and_type_index) {
337
tag_at_put(which, JVM_CONSTANT_Methodref);
338
*int_at_addr(which) = ((jint) name_and_type_index<<16) | class_index;
339
}
340
341
void interface_method_at_put(int which, int class_index, int name_and_type_index) {
342
tag_at_put(which, JVM_CONSTANT_InterfaceMethodref);
343
*int_at_addr(which) = ((jint) name_and_type_index<<16) | class_index; // Not so nice
344
}
345
346
void name_and_type_at_put(int which, int name_index, int signature_index) {
347
tag_at_put(which, JVM_CONSTANT_NameAndType);
348
*int_at_addr(which) = ((jint) signature_index<<16) | name_index; // Not so nice
349
}
350
351
// Tag query
352
353
constantTag tag_at(int which) const { return (constantTag)tags()->at_acquire(which); }
354
355
// Fetching constants
356
357
Klass* klass_at(int which, TRAPS) {
358
constantPoolHandle h_this(THREAD, this);
359
return klass_at_impl(h_this, which, THREAD);
360
}
361
362
Symbol* klass_name_at(int which) const; // Returns the name, w/o resolving.
363
364
Klass* resolved_klass_at(int which) const { // Used by Compiler
365
guarantee(tag_at(which).is_klass(), "Corrupted constant pool");
366
// Must do an acquire here in case another thread resolved the klass
367
// behind our back, lest we later load stale values thru the oop.
368
return CPSlot((Klass*)OrderAccess::load_ptr_acquire(obj_at_addr_raw(which))).get_klass();
369
}
370
371
// This method should only be used with a cpool lock or during parsing or gc
372
Symbol* unresolved_klass_at(int which) { // Temporary until actual use
373
Symbol* s = CPSlot((Symbol*)OrderAccess::load_ptr_acquire(obj_at_addr_raw(which))).get_symbol();
374
// check that the klass is still unresolved.
375
assert(tag_at(which).is_unresolved_klass(), "Corrupted constant pool");
376
return s;
377
}
378
379
// RedefineClasses() API support:
380
Symbol* klass_at_noresolve(int which) { return klass_name_at(which); }
381
382
jint int_at(int which) {
383
assert(tag_at(which).is_int(), "Corrupted constant pool");
384
return *int_at_addr(which);
385
}
386
387
jlong long_at(int which) {
388
assert(tag_at(which).is_long(), "Corrupted constant pool");
389
// return *long_at_addr(which);
390
u8 tmp = Bytes::get_native_u8((address)&base()[which]);
391
return *((jlong*)&tmp);
392
}
393
394
jfloat float_at(int which) {
395
assert(tag_at(which).is_float(), "Corrupted constant pool");
396
return *float_at_addr(which);
397
}
398
399
jdouble double_at(int which) {
400
assert(tag_at(which).is_double(), "Corrupted constant pool");
401
u8 tmp = Bytes::get_native_u8((address)&base()[which]);
402
return *((jdouble*)&tmp);
403
}
404
405
Symbol* symbol_at(int which) {
406
assert(tag_at(which).is_utf8(), "Corrupted constant pool");
407
return *symbol_at_addr(which);
408
}
409
410
oop string_at(int which, int obj_index, TRAPS) {
411
constantPoolHandle h_this(THREAD, this);
412
return string_at_impl(h_this, which, obj_index, THREAD);
413
}
414
oop string_at(int which, TRAPS) {
415
int obj_index = cp_to_object_index(which);
416
return string_at(which, obj_index, THREAD);
417
}
418
419
// Version that can be used before string oop array is created.
420
oop uncached_string_at(int which, TRAPS);
421
422
// A "pseudo-string" is an non-string oop that has found is way into
423
// a String entry.
424
// Under EnableInvokeDynamic this can happen if the user patches a live
425
// object into a CONSTANT_String entry of an anonymous class.
426
// Method oops internally created for method handles may also
427
// use pseudo-strings to link themselves to related metaobjects.
428
429
bool is_pseudo_string_at(int which) {
430
// A pseudo string is a string that doesn't have a symbol in the cpSlot
431
return unresolved_string_at(which) == NULL;
432
}
433
434
oop pseudo_string_at(int which, int obj_index) {
435
assert(tag_at(which).is_string(), "Corrupted constant pool");
436
assert(unresolved_string_at(which) == NULL, "shouldn't have symbol");
437
oop s = resolved_references()->obj_at(obj_index);
438
return s;
439
}
440
441
oop pseudo_string_at(int which) {
442
assert(tag_at(which).is_string(), "Corrupted constant pool");
443
assert(unresolved_string_at(which) == NULL, "shouldn't have symbol");
444
int obj_index = cp_to_object_index(which);
445
oop s = resolved_references()->obj_at(obj_index);
446
return s;
447
}
448
449
void pseudo_string_at_put(int which, int obj_index, oop x) {
450
assert(EnableInvokeDynamic, "");
451
assert(tag_at(which).is_string(), "Corrupted constant pool");
452
unresolved_string_at_put(which, NULL); // indicates patched string
453
string_at_put(which, obj_index, x); // this works just fine
454
}
455
456
// only called when we are sure a string entry is already resolved (via an
457
// earlier string_at call.
458
oop resolved_string_at(int which) {
459
assert(tag_at(which).is_string(), "Corrupted constant pool");
460
// Must do an acquire here in case another thread resolved the klass
461
// behind our back, lest we later load stale values thru the oop.
462
// we might want a volatile_obj_at in ObjArrayKlass.
463
int obj_index = cp_to_object_index(which);
464
return resolved_references()->obj_at(obj_index);
465
}
466
467
Symbol* unresolved_string_at(int which) {
468
assert(tag_at(which).is_string(), "Corrupted constant pool");
469
Symbol* s = *symbol_at_addr(which);
470
return s;
471
}
472
473
// Returns an UTF8 for a CONSTANT_String entry at a given index.
474
// UTF8 char* representation was chosen to avoid conversion of
475
// java_lang_Strings at resolved entries into Symbol*s
476
// or vice versa.
477
// Caller is responsible for checking for pseudo-strings.
478
char* string_at_noresolve(int which);
479
480
jint name_and_type_at(int which) {
481
assert(tag_at(which).is_name_and_type(), "Corrupted constant pool");
482
return *int_at_addr(which);
483
}
484
485
private:
486
int method_handle_ref_kind_at(int which, bool error_ok) {
487
assert(tag_at(which).is_method_handle() ||
488
(error_ok && tag_at(which).is_method_handle_in_error()), "Corrupted constant pool");
489
return extract_low_short_from_int(*int_at_addr(which)); // mask out unwanted ref_index bits
490
}
491
int method_handle_index_at(int which, bool error_ok) {
492
assert(tag_at(which).is_method_handle() ||
493
(error_ok && tag_at(which).is_method_handle_in_error()), "Corrupted constant pool");
494
return extract_high_short_from_int(*int_at_addr(which)); // shift out unwanted ref_kind bits
495
}
496
int method_type_index_at(int which, bool error_ok) {
497
assert(tag_at(which).is_method_type() ||
498
(error_ok && tag_at(which).is_method_type_in_error()), "Corrupted constant pool");
499
return *int_at_addr(which);
500
}
501
public:
502
int method_handle_ref_kind_at(int which) {
503
return method_handle_ref_kind_at(which, false);
504
}
505
int method_handle_ref_kind_at_error_ok(int which) {
506
return method_handle_ref_kind_at(which, true);
507
}
508
int method_handle_index_at(int which) {
509
return method_handle_index_at(which, false);
510
}
511
int method_handle_index_at_error_ok(int which) {
512
return method_handle_index_at(which, true);
513
}
514
int method_type_index_at(int which) {
515
return method_type_index_at(which, false);
516
}
517
int method_type_index_at_error_ok(int which) {
518
return method_type_index_at(which, true);
519
}
520
521
// Derived queries:
522
Symbol* method_handle_name_ref_at(int which) {
523
int member = method_handle_index_at(which);
524
return impl_name_ref_at(member, true);
525
}
526
Symbol* method_handle_signature_ref_at(int which) {
527
int member = method_handle_index_at(which);
528
return impl_signature_ref_at(member, true);
529
}
530
int method_handle_klass_index_at(int which) {
531
int member = method_handle_index_at(which);
532
return impl_klass_ref_index_at(member, true);
533
}
534
Symbol* method_type_signature_at(int which) {
535
int sym = method_type_index_at(which);
536
return symbol_at(sym);
537
}
538
539
int invoke_dynamic_name_and_type_ref_index_at(int which) {
540
assert(tag_at(which).is_invoke_dynamic(), "Corrupted constant pool");
541
return extract_high_short_from_int(*int_at_addr(which));
542
}
543
int invoke_dynamic_bootstrap_specifier_index(int which) {
544
assert(tag_at(which).value() == JVM_CONSTANT_InvokeDynamic, "Corrupted constant pool");
545
return extract_low_short_from_int(*int_at_addr(which));
546
}
547
int invoke_dynamic_operand_base(int which) {
548
int bootstrap_specifier_index = invoke_dynamic_bootstrap_specifier_index(which);
549
return operand_offset_at(operands(), bootstrap_specifier_index);
550
}
551
// The first part of the operands array consists of an index into the second part.
552
// Extract a 32-bit index value from the first part.
553
static int operand_offset_at(Array<u2>* operands, int bootstrap_specifier_index) {
554
int n = (bootstrap_specifier_index * 2);
555
assert(n >= 0 && n+2 <= operands->length(), "oob");
556
// The first 32-bit index points to the beginning of the second part
557
// of the operands array. Make sure this index is in the first part.
558
DEBUG_ONLY(int second_part = build_int_from_shorts(operands->at(0),
559
operands->at(1)));
560
assert(second_part == 0 || n+2 <= second_part, "oob (2)");
561
int offset = build_int_from_shorts(operands->at(n+0),
562
operands->at(n+1));
563
// The offset itself must point into the second part of the array.
564
assert(offset == 0 || offset >= second_part && offset <= operands->length(), "oob (3)");
565
return offset;
566
}
567
static void operand_offset_at_put(Array<u2>* operands, int bootstrap_specifier_index, int offset) {
568
int n = bootstrap_specifier_index * 2;
569
assert(n >= 0 && n+2 <= operands->length(), "oob");
570
operands->at_put(n+0, extract_low_short_from_int(offset));
571
operands->at_put(n+1, extract_high_short_from_int(offset));
572
}
573
static int operand_array_length(Array<u2>* operands) {
574
if (operands == NULL || operands->length() == 0) return 0;
575
int second_part = operand_offset_at(operands, 0);
576
return (second_part / 2);
577
}
578
579
#ifdef ASSERT
580
// operand tuples fit together exactly, end to end
581
static int operand_limit_at(Array<u2>* operands, int bootstrap_specifier_index) {
582
int nextidx = bootstrap_specifier_index + 1;
583
if (nextidx == operand_array_length(operands))
584
return operands->length();
585
else
586
return operand_offset_at(operands, nextidx);
587
}
588
int invoke_dynamic_operand_limit(int which) {
589
int bootstrap_specifier_index = invoke_dynamic_bootstrap_specifier_index(which);
590
return operand_limit_at(operands(), bootstrap_specifier_index);
591
}
592
#endif //ASSERT
593
594
// layout of InvokeDynamic bootstrap method specifier (in second part of operands array):
595
enum {
596
_indy_bsm_offset = 0, // CONSTANT_MethodHandle bsm
597
_indy_argc_offset = 1, // u2 argc
598
_indy_argv_offset = 2 // u2 argv[argc]
599
};
600
601
// These functions are used in RedefineClasses for CP merge
602
603
int operand_offset_at(int bootstrap_specifier_index) {
604
assert(0 <= bootstrap_specifier_index &&
605
bootstrap_specifier_index < operand_array_length(operands()),
606
"Corrupted CP operands");
607
return operand_offset_at(operands(), bootstrap_specifier_index);
608
}
609
int operand_bootstrap_method_ref_index_at(int bootstrap_specifier_index) {
610
int offset = operand_offset_at(bootstrap_specifier_index);
611
return operands()->at(offset + _indy_bsm_offset);
612
}
613
int operand_argument_count_at(int bootstrap_specifier_index) {
614
int offset = operand_offset_at(bootstrap_specifier_index);
615
int argc = operands()->at(offset + _indy_argc_offset);
616
return argc;
617
}
618
int operand_argument_index_at(int bootstrap_specifier_index, int j) {
619
int offset = operand_offset_at(bootstrap_specifier_index);
620
return operands()->at(offset + _indy_argv_offset + j);
621
}
622
int operand_next_offset_at(int bootstrap_specifier_index) {
623
int offset = operand_offset_at(bootstrap_specifier_index) + _indy_argv_offset
624
+ operand_argument_count_at(bootstrap_specifier_index);
625
return offset;
626
}
627
// Compare a bootsrap specifier in the operands arrays
628
bool compare_operand_to(int bootstrap_specifier_index1, constantPoolHandle cp2,
629
int bootstrap_specifier_index2, TRAPS);
630
// Find a bootsrap specifier in the operands array
631
int find_matching_operand(int bootstrap_specifier_index, constantPoolHandle search_cp,
632
int operands_cur_len, TRAPS);
633
// Resize the operands array with delta_len and delta_size
634
void resize_operands(int delta_len, int delta_size, TRAPS);
635
// Extend the operands array with the length and size of the ext_cp operands
636
void extend_operands(constantPoolHandle ext_cp, TRAPS);
637
// Shrink the operands array to a smaller array with new_len length
638
void shrink_operands(int new_len, TRAPS);
639
640
641
int invoke_dynamic_bootstrap_method_ref_index_at(int which) {
642
assert(tag_at(which).is_invoke_dynamic(), "Corrupted constant pool");
643
int op_base = invoke_dynamic_operand_base(which);
644
return operands()->at(op_base + _indy_bsm_offset);
645
}
646
int invoke_dynamic_argument_count_at(int which) {
647
assert(tag_at(which).is_invoke_dynamic(), "Corrupted constant pool");
648
int op_base = invoke_dynamic_operand_base(which);
649
int argc = operands()->at(op_base + _indy_argc_offset);
650
DEBUG_ONLY(int end_offset = op_base + _indy_argv_offset + argc;
651
int next_offset = invoke_dynamic_operand_limit(which));
652
assert(end_offset == next_offset, "matched ending");
653
return argc;
654
}
655
int invoke_dynamic_argument_index_at(int which, int j) {
656
int op_base = invoke_dynamic_operand_base(which);
657
DEBUG_ONLY(int argc = operands()->at(op_base + _indy_argc_offset));
658
assert((uint)j < (uint)argc, "oob");
659
return operands()->at(op_base + _indy_argv_offset + j);
660
}
661
662
// The following methods (name/signature/klass_ref_at, klass_ref_at_noresolve,
663
// name_and_type_ref_index_at) all expect to be passed indices obtained
664
// directly from the bytecode.
665
// If the indices are meant to refer to fields or methods, they are
666
// actually rewritten constant pool cache indices.
667
// The routine remap_instruction_operand_from_cache manages the adjustment
668
// of these values back to constant pool indices.
669
670
// There are also "uncached" versions which do not adjust the operand index; see below.
671
672
// FIXME: Consider renaming these with a prefix "cached_" to make the distinction clear.
673
// In a few cases (the verifier) there are uses before a cpcache has been built,
674
// which are handled by a dynamic check in remap_instruction_operand_from_cache.
675
// FIXME: Remove the dynamic check, and adjust all callers to specify the correct mode.
676
677
// Lookup for entries consisting of (klass_index, name_and_type index)
678
Klass* klass_ref_at(int which, TRAPS);
679
Symbol* klass_ref_at_noresolve(int which);
680
Symbol* name_ref_at(int which) { return impl_name_ref_at(which, false); }
681
Symbol* signature_ref_at(int which) { return impl_signature_ref_at(which, false); }
682
683
int klass_ref_index_at(int which) { return impl_klass_ref_index_at(which, false); }
684
int name_and_type_ref_index_at(int which) { return impl_name_and_type_ref_index_at(which, false); }
685
686
// Lookup for entries consisting of (name_index, signature_index)
687
int name_ref_index_at(int which_nt); // == low-order jshort of name_and_type_at(which_nt)
688
int signature_ref_index_at(int which_nt); // == high-order jshort of name_and_type_at(which_nt)
689
690
BasicType basic_type_for_signature_at(int which);
691
692
// Resolve string constants (to prevent allocation during compilation)
693
void resolve_string_constants(TRAPS) {
694
constantPoolHandle h_this(THREAD, this);
695
resolve_string_constants_impl(h_this, CHECK);
696
}
697
698
// CDS support
699
void remove_unshareable_info();
700
void restore_unshareable_info(TRAPS);
701
bool resolve_class_constants(TRAPS);
702
// The ConstantPool vtable is restored by this call when the ConstantPool is
703
// in the shared archive. See patch_klass_vtables() in metaspaceShared.cpp for
704
// all the gory details. SA, dtrace and pstack helpers distinguish metadata
705
// by their vtable.
706
void restore_vtable() { guarantee(is_constantPool(), "vtable restored by this call"); }
707
708
private:
709
enum { _no_index_sentinel = -1, _possible_index_sentinel = -2 };
710
public:
711
712
// Resolve late bound constants.
713
oop resolve_constant_at(int index, TRAPS) {
714
constantPoolHandle h_this(THREAD, this);
715
return resolve_constant_at_impl(h_this, index, _no_index_sentinel, THREAD);
716
}
717
718
oop resolve_cached_constant_at(int cache_index, TRAPS) {
719
constantPoolHandle h_this(THREAD, this);
720
return resolve_constant_at_impl(h_this, _no_index_sentinel, cache_index, THREAD);
721
}
722
723
oop resolve_possibly_cached_constant_at(int pool_index, TRAPS) {
724
constantPoolHandle h_this(THREAD, this);
725
return resolve_constant_at_impl(h_this, pool_index, _possible_index_sentinel, THREAD);
726
}
727
728
oop resolve_bootstrap_specifier_at(int index, TRAPS) {
729
constantPoolHandle h_this(THREAD, this);
730
return resolve_bootstrap_specifier_at_impl(h_this, index, THREAD);
731
}
732
733
// Klass name matches name at offset
734
bool klass_name_at_matches(instanceKlassHandle k, int which);
735
736
// Sizing
737
int length() const { return _length; }
738
void set_length(int length) { _length = length; }
739
740
// Tells whether index is within bounds.
741
bool is_within_bounds(int index) const {
742
return 0 <= index && index < length();
743
}
744
745
// Sizing (in words)
746
static int header_size() { return sizeof(ConstantPool)/HeapWordSize; }
747
static int size(int length) { return align_object_size(header_size() + length); }
748
int size() const { return size(length()); }
749
#if INCLUDE_SERVICES
750
void collect_statistics(KlassSizeStats *sz) const;
751
#endif
752
753
friend class ClassFileParser;
754
friend class SystemDictionary;
755
756
// Used by compiler to prevent classloading.
757
static Method* method_at_if_loaded (constantPoolHandle this_oop, int which);
758
static bool has_appendix_at_if_loaded (constantPoolHandle this_oop, int which);
759
static oop appendix_at_if_loaded (constantPoolHandle this_oop, int which);
760
static bool has_method_type_at_if_loaded (constantPoolHandle this_oop, int which);
761
static oop method_type_at_if_loaded (constantPoolHandle this_oop, int which);
762
static Klass* klass_at_if_loaded (constantPoolHandle this_oop, int which);
763
static Klass* klass_ref_at_if_loaded (constantPoolHandle this_oop, int which);
764
765
// Routines currently used for annotations (only called by jvm.cpp) but which might be used in the
766
// future by other Java code. These take constant pool indices rather than
767
// constant pool cache indices as do the peer methods above.
768
Symbol* uncached_klass_ref_at_noresolve(int which);
769
Symbol* uncached_name_ref_at(int which) { return impl_name_ref_at(which, true); }
770
Symbol* uncached_signature_ref_at(int which) { return impl_signature_ref_at(which, true); }
771
int uncached_klass_ref_index_at(int which) { return impl_klass_ref_index_at(which, true); }
772
int uncached_name_and_type_ref_index_at(int which) { return impl_name_and_type_ref_index_at(which, true); }
773
774
// Sharing
775
int pre_resolve_shared_klasses(TRAPS);
776
777
// Debugging
778
const char* printable_name_at(int which) PRODUCT_RETURN0;
779
780
#ifdef ASSERT
781
enum { CPCACHE_INDEX_TAG = 0x10000 }; // helps keep CP cache indices distinct from CP indices
782
#else
783
enum { CPCACHE_INDEX_TAG = 0 }; // in product mode, this zero value is a no-op
784
#endif //ASSERT
785
786
static int decode_cpcache_index(int raw_index, bool invokedynamic_ok = false) {
787
if (invokedynamic_ok && is_invokedynamic_index(raw_index))
788
return decode_invokedynamic_index(raw_index);
789
else
790
return raw_index - CPCACHE_INDEX_TAG;
791
}
792
793
private:
794
795
void set_resolved_references(jobject s) { _resolved_references = s; }
796
Array<u2>* reference_map() const { return _reference_map; }
797
void set_reference_map(Array<u2>* o) { _reference_map = o; }
798
799
// patch JSR 292 resolved references after the class is linked.
800
void patch_resolved_references(GrowableArray<Handle>* cp_patches);
801
802
Symbol* impl_name_ref_at(int which, bool uncached);
803
Symbol* impl_signature_ref_at(int which, bool uncached);
804
int impl_klass_ref_index_at(int which, bool uncached);
805
int impl_name_and_type_ref_index_at(int which, bool uncached);
806
807
int remap_instruction_operand_from_cache(int operand); // operand must be biased by CPCACHE_INDEX_TAG
808
809
// Used while constructing constant pool (only by ClassFileParser)
810
jint klass_index_at(int which) {
811
assert(tag_at(which).is_klass_index(), "Corrupted constant pool");
812
return *int_at_addr(which);
813
}
814
815
jint string_index_at(int which) {
816
assert(tag_at(which).is_string_index(), "Corrupted constant pool");
817
return *int_at_addr(which);
818
}
819
820
// Performs the LinkResolver checks
821
static void verify_constant_pool_resolve(constantPoolHandle this_oop, KlassHandle klass, TRAPS);
822
823
// Implementation of methods that needs an exposed 'this' pointer, in order to
824
// handle GC while executing the method
825
static Klass* klass_at_impl(constantPoolHandle this_oop, int which, TRAPS);
826
static oop string_at_impl(constantPoolHandle this_oop, int which, int obj_index, TRAPS);
827
828
// Resolve string constants (to prevent allocation during compilation)
829
static void resolve_string_constants_impl(constantPoolHandle this_oop, TRAPS);
830
831
static oop resolve_constant_at_impl(constantPoolHandle this_oop, int index, int cache_index, TRAPS);
832
static oop resolve_bootstrap_specifier_at_impl(constantPoolHandle this_oop, int index, TRAPS);
833
834
// Exception handling
835
static void throw_resolution_error(constantPoolHandle this_oop, int which, TRAPS);
836
static Symbol* exception_message(constantPoolHandle this_oop, int which, constantTag tag, oop pending_exception);
837
static void save_and_throw_exception(constantPoolHandle this_oop, int which, constantTag tag, TRAPS);
838
839
public:
840
// Merging ConstantPool* support:
841
bool compare_entry_to(int index1, constantPoolHandle cp2, int index2, TRAPS);
842
void copy_cp_to(int start_i, int end_i, constantPoolHandle to_cp, int to_i, TRAPS) {
843
constantPoolHandle h_this(THREAD, this);
844
copy_cp_to_impl(h_this, start_i, end_i, to_cp, to_i, THREAD);
845
}
846
static void copy_cp_to_impl(constantPoolHandle from_cp, int start_i, int end_i, constantPoolHandle to_cp, int to_i, TRAPS);
847
static void copy_entry_to(constantPoolHandle from_cp, int from_i, constantPoolHandle to_cp, int to_i, TRAPS);
848
static void copy_operands(constantPoolHandle from_cp, constantPoolHandle to_cp, TRAPS);
849
int find_matching_entry(int pattern_i, constantPoolHandle search_cp, TRAPS);
850
int version() const { return _saved._version; }
851
void set_version(int version) { _saved._version = version; }
852
void increment_and_save_version(int version) {
853
_saved._version = version >= 0 ? (version + 1) : version; // keep overflow
854
}
855
856
void set_resolved_reference_length(int length) { _saved._resolved_reference_length = length; }
857
int resolved_reference_length() const { return _saved._resolved_reference_length; }
858
void set_lock(Monitor* lock) { _lock = lock; }
859
Monitor* lock() { return _lock; }
860
861
// Decrease ref counts of symbols that are in the constant pool
862
// when the holder class is unloaded
863
void unreference_symbols();
864
865
// Deallocate constant pool for RedefineClasses
866
void deallocate_contents(ClassLoaderData* loader_data);
867
void release_C_heap_structures();
868
869
// JVMTI accesss - GetConstantPool, RetransformClasses, ...
870
friend class JvmtiConstantPoolReconstituter;
871
872
private:
873
jint cpool_entry_size(jint idx);
874
jint hash_entries_to(SymbolHashMap *symmap, SymbolHashMap *classmap);
875
876
// Copy cpool bytes into byte array.
877
// Returns:
878
// int > 0, count of the raw cpool bytes that have been copied
879
// 0, OutOfMemory error
880
// -1, Internal error
881
int copy_cpool_bytes(int cpool_size,
882
SymbolHashMap* tbl,
883
unsigned char *bytes);
884
885
public:
886
// Verify
887
void verify_on(outputStream* st);
888
889
// Printing
890
void print_on(outputStream* st) const;
891
void print_value_on(outputStream* st) const;
892
void print_entry_on(int index, outputStream* st);
893
894
const char* internal_name() const { return "{constant pool}"; }
895
896
#ifndef PRODUCT
897
// Compile the world support
898
static void preload_and_initialize_all_classes(ConstantPool* constant_pool, TRAPS);
899
#endif
900
};
901
902
class SymbolHashMapEntry : public CHeapObj<mtSymbol> {
903
private:
904
unsigned int _hash; // 32-bit hash for item
905
SymbolHashMapEntry* _next; // Next element in the linked list for this bucket
906
Symbol* _symbol; // 1-st part of the mapping: symbol => value
907
u2 _value; // 2-nd part of the mapping: symbol => value
908
909
public:
910
unsigned int hash() const { return _hash; }
911
void set_hash(unsigned int hash) { _hash = hash; }
912
913
SymbolHashMapEntry* next() const { return _next; }
914
void set_next(SymbolHashMapEntry* next) { _next = next; }
915
916
Symbol* symbol() const { return _symbol; }
917
void set_symbol(Symbol* sym) { _symbol = sym; }
918
919
u2 value() const { return _value; }
920
void set_value(u2 value) { _value = value; }
921
922
SymbolHashMapEntry(unsigned int hash, Symbol* symbol, u2 value)
923
: _hash(hash), _symbol(symbol), _value(value), _next(NULL) {}
924
925
}; // End SymbolHashMapEntry class
926
927
928
class SymbolHashMapBucket : public CHeapObj<mtSymbol> {
929
930
private:
931
SymbolHashMapEntry* _entry;
932
933
public:
934
SymbolHashMapEntry* entry() const { return _entry; }
935
void set_entry(SymbolHashMapEntry* entry) { _entry = entry; }
936
void clear() { _entry = NULL; }
937
938
}; // End SymbolHashMapBucket class
939
940
941
class SymbolHashMap: public CHeapObj<mtSymbol> {
942
943
private:
944
// Default number of entries in the table
945
enum SymbolHashMap_Constants {
946
_Def_HashMap_Size = 256
947
};
948
949
int _table_size;
950
SymbolHashMapBucket* _buckets;
951
952
void initialize_table(int table_size) {
953
_table_size = table_size;
954
_buckets = NEW_C_HEAP_ARRAY(SymbolHashMapBucket, table_size, mtSymbol);
955
for (int index = 0; index < table_size; index++) {
956
_buckets[index].clear();
957
}
958
}
959
960
public:
961
962
int table_size() const { return _table_size; }
963
964
SymbolHashMap() { initialize_table(_Def_HashMap_Size); }
965
SymbolHashMap(int table_size) { initialize_table(table_size); }
966
967
// hash P(31) from Kernighan & Ritchie
968
static unsigned int compute_hash(const char* str, int len) {
969
unsigned int hash = 0;
970
while (len-- > 0) {
971
hash = 31*hash + (unsigned) *str;
972
str++;
973
}
974
return hash;
975
}
976
977
SymbolHashMapEntry* bucket(int i) {
978
return _buckets[i].entry();
979
}
980
981
void add_entry(Symbol* sym, u2 value);
982
SymbolHashMapEntry* find_entry(Symbol* sym);
983
984
u2 symbol_to_value(Symbol* sym) {
985
SymbolHashMapEntry *entry = find_entry(sym);
986
return (entry == NULL) ? 0 : entry->value();
987
}
988
989
~SymbolHashMap() {
990
SymbolHashMapEntry* next;
991
for (int i = 0; i < _table_size; i++) {
992
for (SymbolHashMapEntry* cur = bucket(i); cur != NULL; cur = next) {
993
next = cur->next();
994
delete(cur);
995
}
996
}
997
FREE_C_HEAP_ARRAY(SymbolHashMapBucket, _buckets, mtSymbol);
998
}
999
}; // End SymbolHashMap class
1000
1001
#endif // SHARE_VM_OOPS_CONSTANTPOOLOOP_HPP
1002
1003