49 // only if it is (a) non-static and (b) declared by a shared instance klass.
50 // This allows non-static field lists to be cached on shared types.
51 // Because the _type field is lazily initialized, however, there is a
52 // special restriction that a shared field cannot cache an unshared type.
53 // This puts a small performance penalty on shared fields with unshared
54 // types, such as StackTraceElement[] Throwable.stackTrace.
55 // (Throwable is shared because ClassCastException is shared, but
56 // StackTraceElement is not presently shared.)
57
58 // It is not a vicious circularity for a ciField to recursively create
59 // the ciSymbols necessary to represent its name and signature.
60 // Therefore, these items are created eagerly, and the name and signature
61 // of a shared field are themselves shared symbols. This somewhat
62 // pollutes the set of shared CI objects: It grows from 50 to 93 items,
63 // with all of the additional 43 being uninteresting shared ciSymbols.
64 // This adds at most one step to the binary search, an amount which
65 // decreases for complex compilation tasks.
66
67 // ------------------------------------------------------------------
68 // ciField::ciField
69 ciField::ciField(ciInstanceKlass* klass, int index): _known_to_link_with_put(NULL), _known_to_link_with_get(NULL) {
70 ASSERT_IN_VM;
71 CompilerThread *thread = CompilerThread::current();
72
73 assert(ciObjectFactory::is_initialized(), "not a shared field");
74
75 assert(klass->get_instanceKlass()->is_linked(), "must be linked before using its constant-pool");
76
77 constantPoolHandle cpool(thread, klass->get_instanceKlass()->constants());
78
79 // Get the field's name, signature, and type.
80 Symbol* name = cpool->name_ref_at(index);
81 _name = ciEnv::current(thread)->get_symbol(name);
82
83 int nt_index = cpool->name_and_type_ref_index_at(index);
84 int sig_index = cpool->signature_ref_index_at(nt_index);
85 Symbol* signature = cpool->symbol_at(sig_index);
86 _signature = ciEnv::current(thread)->get_symbol(signature);
87
88 BasicType field_type = FieldType::basic_type(signature);
89
156 _offset = -1;
157 _is_constant = false;
158 return;
159 }
160
161 // Access check based on declared_holder. canonical_holder should not be used
162 // to check access because it can erroneously succeed. If this check fails,
163 // propagate the declared holder to will_link() which in turn will bail out
164 // compilation for this field access.
165 if (!Reflection::verify_field_access(klass->get_Klass(), declared_holder->get_Klass(), canonical_holder, field_desc.access_flags(), true)) {
166 _holder = declared_holder;
167 _offset = -1;
168 _is_constant = false;
169 return;
170 }
171
172 assert(canonical_holder == field_desc.field_holder(), "just checking");
173 initialize_from(&field_desc);
174 }
175
176 ciField::ciField(fieldDescriptor *fd): _known_to_link_with_put(NULL), _known_to_link_with_get(NULL) {
177 ASSERT_IN_VM;
178
179 // Get the field's name, signature, and type.
180 ciEnv* env = CURRENT_ENV;
181 _name = env->get_symbol(fd->name());
182 _signature = env->get_symbol(fd->signature());
183
184 BasicType field_type = fd->field_type();
185
186 // If the field is a pointer type, get the klass of the
187 // field.
188 if (field_type == T_OBJECT || field_type == T_ARRAY) {
189 _type = NULL; // must call compute_type on first access
190 } else {
191 _type = ciType::make(field_type);
192 }
193
194 initialize_from(fd);
195
196 // Either (a) it is marked shared, or else (b) we are done bootstrapping.
220 // Trust Atomic*FieldUpdaters: they are very important for performance, and make up one
221 // more reason not to use Unsafe, if their final fields are trusted. See more in JDK-8140483.
222 if (holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicIntegerFieldUpdater_Impl() ||
223 holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicLongFieldUpdater_CASUpdater() ||
224 holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicLongFieldUpdater_LockedUpdater() ||
225 holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicReferenceFieldUpdater_Impl()) {
226 return true;
227 }
228 return TrustFinalNonStaticFields;
229 }
230
231 void ciField::initialize_from(fieldDescriptor* fd) {
232 // Get the flags, offset, and canonical holder of the field.
233 _flags = ciFlags(fd->access_flags());
234 _offset = fd->offset();
235 _holder = CURRENT_ENV->get_instance_klass(fd->field_holder());
236
237 // Check to see if the field is constant.
238 Klass* k = _holder->get_Klass();
239 bool is_stable_field = FoldStableValues && is_stable();
240 if (is_final() || is_stable_field) {
241 if (is_static()) {
242 // This field just may be constant. The only case where it will
243 // not be constant is when the field is a *special* static & final field
244 // whose value may change. The three examples are java.lang.System.in,
245 // java.lang.System.out, and java.lang.System.err.
246 assert(SystemDictionary::System_klass() != NULL, "Check once per vm");
247 if (k == SystemDictionary::System_klass()) {
248 // Check offsets for case 2: System.in, System.out, or System.err
249 if( _offset == java_lang_System::in_offset_in_bytes() ||
250 _offset == java_lang_System::out_offset_in_bytes() ||
251 _offset == java_lang_System::err_offset_in_bytes() ) {
252 _is_constant = false;
253 return;
254 }
255 }
256 _is_constant = true;
257 } else {
258 // An instance field can be constant if it's a final static field or if
259 // it's a final non-static field of a trusted class (classes in
260 // java.lang.invoke and sun.invoke packages and subpackages).
261 _is_constant = is_stable_field || trust_final_non_static_fields(_holder);
262 }
263 } else {
264 // For CallSite objects treat the target field as a compile time constant.
265 assert(SystemDictionary::CallSite_klass() != NULL, "should be already initialized");
266 if (k == SystemDictionary::CallSite_klass() &&
267 _offset == java_lang_invoke_CallSite::target_offset_in_bytes()) {
268 _is_constant = true;
269 } else {
270 // Non-final & non-stable fields are not constants.
271 _is_constant = false;
272 }
273 }
274 }
275
276 // ------------------------------------------------------------------
277 // ciField::constant_value
278 // Get the constant value of a this static field.
279 ciConstant ciField::constant_value() {
280 assert(is_static() && is_constant(), "illegal call to constant_value()");
281 if (!_holder->is_initialized()) {
282 return ciConstant(); // Not initialized yet
283 }
284 if (_constant_value.basic_type() == T_ILLEGAL) {
285 // Static fields are placed in mirror objects.
286 VM_ENTRY_MARK;
287 ciInstance* mirror = CURRENT_ENV->get_instance(_holder->get_Klass()->java_mirror());
323 type_is_also_shared = true; // int[] etc. are explicitly bootstrapped
324 } else if (type->is_instance_klass()) {
325 type_is_also_shared = type->as_instance_klass()->is_shared();
326 } else {
327 // Currently there is no 'shared' query for array types.
328 type_is_also_shared = !ciObjectFactory::is_initialized();
329 }
330 if (!type_is_also_shared)
331 return type; // Bummer.
332 }
333 _type = type;
334 return type;
335 }
336
337
338 // ------------------------------------------------------------------
339 // ciField::will_link
340 //
341 // Can a specific access to this field be made without causing
342 // link errors?
343 bool ciField::will_link(ciInstanceKlass* accessing_klass,
344 Bytecodes::Code bc) {
345 VM_ENTRY_MARK;
346 assert(bc == Bytecodes::_getstatic || bc == Bytecodes::_putstatic ||
347 bc == Bytecodes::_getfield || bc == Bytecodes::_putfield,
348 "unexpected bytecode");
349
350 if (_offset == -1) {
351 // at creation we couldn't link to our holder so we need to
352 // maintain that stance, otherwise there's no safe way to use this
353 // ciField.
354 return false;
355 }
356
357 // Check for static/nonstatic mismatch
358 bool is_static = (bc == Bytecodes::_getstatic || bc == Bytecodes::_putstatic);
359 if (is_static != this->is_static()) {
360 return false;
361 }
362
363 // Get and put can have different accessibility rules
364 bool is_put = (bc == Bytecodes::_putfield || bc == Bytecodes::_putstatic);
365 if (is_put) {
366 if (_known_to_link_with_put == accessing_klass) {
367 return true;
368 }
369 } else {
370 if (_known_to_link_with_get == accessing_klass) {
371 return true;
372 }
373 }
374
375 LinkInfo link_info(_holder->get_instanceKlass(),
376 _name->get_symbol(), _signature->get_symbol(),
377 accessing_klass->get_Klass());
378 fieldDescriptor result;
379 LinkResolver::resolve_field(result, link_info, bc, false, KILL_COMPILE_ON_FATAL_(false));
380
381 // update the hit-cache, unless there is a problem with memory scoping:
382 if (accessing_klass->is_shared() || !is_shared()) {
383 if (is_put) {
384 _known_to_link_with_put = accessing_klass;
385 } else {
386 _known_to_link_with_get = accessing_klass;
387 }
388 }
389
390 return true;
391 }
392
393 // ------------------------------------------------------------------
394 // ciField::print
395 void ciField::print() {
396 tty->print("<ciField name=");
397 _holder->print_name();
398 tty->print(".");
399 _name->print_symbol();
400 tty->print(" signature=");
401 _signature->print_symbol();
402 tty->print(" offset=%d type=", _offset);
403 if (_type != NULL)
404 _type->print_name();
405 else
406 tty->print("(reference)");
|
49 // only if it is (a) non-static and (b) declared by a shared instance klass.
50 // This allows non-static field lists to be cached on shared types.
51 // Because the _type field is lazily initialized, however, there is a
52 // special restriction that a shared field cannot cache an unshared type.
53 // This puts a small performance penalty on shared fields with unshared
54 // types, such as StackTraceElement[] Throwable.stackTrace.
55 // (Throwable is shared because ClassCastException is shared, but
56 // StackTraceElement is not presently shared.)
57
58 // It is not a vicious circularity for a ciField to recursively create
59 // the ciSymbols necessary to represent its name and signature.
60 // Therefore, these items are created eagerly, and the name and signature
61 // of a shared field are themselves shared symbols. This somewhat
62 // pollutes the set of shared CI objects: It grows from 50 to 93 items,
63 // with all of the additional 43 being uninteresting shared ciSymbols.
64 // This adds at most one step to the binary search, an amount which
65 // decreases for complex compilation tasks.
66
67 // ------------------------------------------------------------------
68 // ciField::ciField
69 ciField::ciField(ciInstanceKlass* klass, int index) :
70 _known_to_link_with_put(NULL), _known_to_link_with_get(NULL) {
71 ASSERT_IN_VM;
72 CompilerThread *thread = CompilerThread::current();
73
74 assert(ciObjectFactory::is_initialized(), "not a shared field");
75
76 assert(klass->get_instanceKlass()->is_linked(), "must be linked before using its constant-pool");
77
78 constantPoolHandle cpool(thread, klass->get_instanceKlass()->constants());
79
80 // Get the field's name, signature, and type.
81 Symbol* name = cpool->name_ref_at(index);
82 _name = ciEnv::current(thread)->get_symbol(name);
83
84 int nt_index = cpool->name_and_type_ref_index_at(index);
85 int sig_index = cpool->signature_ref_index_at(nt_index);
86 Symbol* signature = cpool->symbol_at(sig_index);
87 _signature = ciEnv::current(thread)->get_symbol(signature);
88
89 BasicType field_type = FieldType::basic_type(signature);
90
157 _offset = -1;
158 _is_constant = false;
159 return;
160 }
161
162 // Access check based on declared_holder. canonical_holder should not be used
163 // to check access because it can erroneously succeed. If this check fails,
164 // propagate the declared holder to will_link() which in turn will bail out
165 // compilation for this field access.
166 if (!Reflection::verify_field_access(klass->get_Klass(), declared_holder->get_Klass(), canonical_holder, field_desc.access_flags(), true)) {
167 _holder = declared_holder;
168 _offset = -1;
169 _is_constant = false;
170 return;
171 }
172
173 assert(canonical_holder == field_desc.field_holder(), "just checking");
174 initialize_from(&field_desc);
175 }
176
177 ciField::ciField(fieldDescriptor *fd) :
178 _known_to_link_with_put(NULL), _known_to_link_with_get(NULL) {
179 ASSERT_IN_VM;
180
181 // Get the field's name, signature, and type.
182 ciEnv* env = CURRENT_ENV;
183 _name = env->get_symbol(fd->name());
184 _signature = env->get_symbol(fd->signature());
185
186 BasicType field_type = fd->field_type();
187
188 // If the field is a pointer type, get the klass of the
189 // field.
190 if (field_type == T_OBJECT || field_type == T_ARRAY) {
191 _type = NULL; // must call compute_type on first access
192 } else {
193 _type = ciType::make(field_type);
194 }
195
196 initialize_from(fd);
197
198 // Either (a) it is marked shared, or else (b) we are done bootstrapping.
222 // Trust Atomic*FieldUpdaters: they are very important for performance, and make up one
223 // more reason not to use Unsafe, if their final fields are trusted. See more in JDK-8140483.
224 if (holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicIntegerFieldUpdater_Impl() ||
225 holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicLongFieldUpdater_CASUpdater() ||
226 holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicLongFieldUpdater_LockedUpdater() ||
227 holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicReferenceFieldUpdater_Impl()) {
228 return true;
229 }
230 return TrustFinalNonStaticFields;
231 }
232
233 void ciField::initialize_from(fieldDescriptor* fd) {
234 // Get the flags, offset, and canonical holder of the field.
235 _flags = ciFlags(fd->access_flags());
236 _offset = fd->offset();
237 _holder = CURRENT_ENV->get_instance_klass(fd->field_holder());
238
239 // Check to see if the field is constant.
240 Klass* k = _holder->get_Klass();
241 bool is_stable_field = FoldStableValues && is_stable();
242 if ((is_final() && !has_initialized_final_update()) || is_stable_field) {
243 if (is_static()) {
244 // This field just may be constant. The only case where it will
245 // not be constant is when the field is a *special* static & final field
246 // whose value may change. The three examples are java.lang.System.in,
247 // java.lang.System.out, and java.lang.System.err.
248 assert(SystemDictionary::System_klass() != NULL, "Check once per vm");
249 if (k == SystemDictionary::System_klass()) {
250 // Check offsets for case 2: System.in, System.out, or System.err
251 if( _offset == java_lang_System::in_offset_in_bytes() ||
252 _offset == java_lang_System::out_offset_in_bytes() ||
253 _offset == java_lang_System::err_offset_in_bytes() ) {
254 _is_constant = false;
255 return;
256 }
257 }
258 _is_constant = true;
259 } else {
260 // An instance field can be constant if it's a final static field or if
261 // it's a final non-static field of a trusted class (classes in
262 // java.lang.invoke and sun.invoke packages and subpackages).
263 _is_constant = is_stable_field || trust_final_non_static_fields(_holder);
264 }
265 } else {
266 // For CallSite objects treat the target field as a compile time constant.
267 assert(SystemDictionary::CallSite_klass() != NULL, "should be already initialized");
268 if (k == SystemDictionary::CallSite_klass() &&
269 _offset == java_lang_invoke_CallSite::target_offset_in_bytes()) {
270 assert(!has_initialized_final_update(), "CallSite is not supposed to have writes to final fields outside initializers");
271 _is_constant = true;
272 } else {
273 // Non-final & non-stable fields are not constants.
274 _is_constant = false;
275 }
276 }
277 }
278
279 // ------------------------------------------------------------------
280 // ciField::constant_value
281 // Get the constant value of a this static field.
282 ciConstant ciField::constant_value() {
283 assert(is_static() && is_constant(), "illegal call to constant_value()");
284 if (!_holder->is_initialized()) {
285 return ciConstant(); // Not initialized yet
286 }
287 if (_constant_value.basic_type() == T_ILLEGAL) {
288 // Static fields are placed in mirror objects.
289 VM_ENTRY_MARK;
290 ciInstance* mirror = CURRENT_ENV->get_instance(_holder->get_Klass()->java_mirror());
326 type_is_also_shared = true; // int[] etc. are explicitly bootstrapped
327 } else if (type->is_instance_klass()) {
328 type_is_also_shared = type->as_instance_klass()->is_shared();
329 } else {
330 // Currently there is no 'shared' query for array types.
331 type_is_also_shared = !ciObjectFactory::is_initialized();
332 }
333 if (!type_is_also_shared)
334 return type; // Bummer.
335 }
336 _type = type;
337 return type;
338 }
339
340
341 // ------------------------------------------------------------------
342 // ciField::will_link
343 //
344 // Can a specific access to this field be made without causing
345 // link errors?
346 bool ciField::will_link(ciMethod* accessing_method,
347 Bytecodes::Code bc) {
348 VM_ENTRY_MARK;
349 assert(bc == Bytecodes::_getstatic || bc == Bytecodes::_putstatic ||
350 bc == Bytecodes::_getfield || bc == Bytecodes::_putfield,
351 "unexpected bytecode");
352
353 if (_offset == -1) {
354 // at creation we couldn't link to our holder so we need to
355 // maintain that stance, otherwise there's no safe way to use this
356 // ciField.
357 return false;
358 }
359
360 // Check for static/nonstatic mismatch
361 bool is_static = (bc == Bytecodes::_getstatic || bc == Bytecodes::_putstatic);
362 if (is_static != this->is_static()) {
363 return false;
364 }
365
366 // Get and put can have different accessibility rules
367 bool is_put = (bc == Bytecodes::_putfield || bc == Bytecodes::_putstatic);
368 if (is_put) {
369 if (_known_to_link_with_put == accessing_method) {
370 return true;
371 }
372 } else {
373 if (_known_to_link_with_get == accessing_method->holder()) {
374 return true;
375 }
376 }
377
378 LinkInfo link_info(_holder->get_instanceKlass(),
379 _name->get_symbol(), _signature->get_symbol(),
380 accessing_method->get_Method());
381 fieldDescriptor result;
382 LinkResolver::resolve_field(result, link_info, bc, false, KILL_COMPILE_ON_FATAL_(false));
383
384 // update the hit-cache, unless there is a problem with memory scoping:
385 if (accessing_method->holder()->is_shared() || !is_shared()) {
386 if (is_put) {
387 _known_to_link_with_put = accessing_method;
388 } else {
389 _known_to_link_with_get = accessing_method->holder();
390 }
391 }
392
393 return true;
394 }
395
396 // ------------------------------------------------------------------
397 // ciField::print
398 void ciField::print() {
399 tty->print("<ciField name=");
400 _holder->print_name();
401 tty->print(".");
402 _name->print_symbol();
403 tty->print(" signature=");
404 _signature->print_symbol();
405 tty->print(" offset=%d type=", _offset);
406 if (_type != NULL)
407 _type->print_name();
408 else
409 tty->print("(reference)");
|