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