172
173 static bool trust_final_non_static_fields(ciInstanceKlass* holder) {
174 if (holder == NULL)
175 return false;
176 if (holder->name() == ciSymbol::java_lang_System())
177 // Never trust strangely unstable finals: System.out, etc.
178 return false;
179 // Even if general trusting is disabled, trust system-built closures in these packages.
180 if (holder->is_in_package("java/lang/invoke") || holder->is_in_package("sun/invoke"))
181 return true;
182 return TrustFinalNonStaticFields;
183 }
184
185 void ciField::initialize_from(fieldDescriptor* fd) {
186 // Get the flags, offset, and canonical holder of the field.
187 _flags = ciFlags(fd->access_flags());
188 _offset = fd->offset();
189 _holder = CURRENT_ENV->get_instance_klass(fd->field_holder());
190
191 // Check to see if the field is constant.
192 if (_holder->is_initialized() && this->is_final()) {
193 if (!this->is_static()) {
194 // A field can be constant if it's a final static field or if
195 // it's a final non-static field of a trusted class (classes in
196 // java.lang.invoke and sun.invoke packages and subpackages).
197 if (trust_final_non_static_fields(_holder)) {
198 _is_constant = true;
199 return;
200 }
201 _is_constant = false;
202 return;
203 }
204
205 // This field just may be constant. The only cases where it will
206 // not be constant are:
207 //
208 // 1. The field holds a non-perm-space oop. The field is, strictly
209 // speaking, constant but we cannot embed non-perm-space oops into
210 // generated code. For the time being we need to consider the
211 // field to be not constant.
212 // 2. The field is a *special* static&final field whose value
213 // may change. The three examples are java.lang.System.in,
214 // java.lang.System.out, and java.lang.System.err.
215
216 KlassHandle k = _holder->get_Klass();
217 assert( SystemDictionary::System_klass() != NULL, "Check once per vm");
218 if( k() == SystemDictionary::System_klass() ) {
219 // Check offsets for case 2: System.in, System.out, or System.err
220 if( _offset == java_lang_System::in_offset_in_bytes() ||
221 _offset == java_lang_System::out_offset_in_bytes() ||
222 _offset == java_lang_System::err_offset_in_bytes() ) {
223 _is_constant = false;
224 return;
225 }
226 }
227
228 Handle mirror = k->java_mirror();
229
230 _is_constant = true;
231 switch(type()->basic_type()) {
232 case T_BYTE:
233 _constant_value = ciConstant(type()->basic_type(), mirror->byte_field(_offset));
234 break;
235 case T_CHAR:
236 _constant_value = ciConstant(type()->basic_type(), mirror->char_field(_offset));
237 break;
238 case T_SHORT:
239 _constant_value = ciConstant(type()->basic_type(), mirror->short_field(_offset));
240 break;
241 case T_BOOLEAN:
242 _constant_value = ciConstant(type()->basic_type(), mirror->bool_field(_offset));
243 break;
244 case T_INT:
245 _constant_value = ciConstant(type()->basic_type(), mirror->int_field(_offset));
246 break;
247 case T_FLOAT:
248 _constant_value = ciConstant(mirror->float_field(_offset));
249 break;
250 case T_DOUBLE:
256 case T_OBJECT:
257 case T_ARRAY:
258 {
259 oop o = mirror->obj_field(_offset);
260
261 // A field will be "constant" if it is known always to be
262 // a non-null reference to an instance of a particular class,
263 // or to a particular array. This can happen even if the instance
264 // or array is not perm. In such a case, an "unloaded" ciArray
265 // or ciInstance is created. The compiler may be able to use
266 // information about the object's class (which is exact) or length.
267
268 if (o == NULL) {
269 _constant_value = ciConstant(type()->basic_type(), ciNullObject::make());
270 } else {
271 _constant_value = ciConstant(type()->basic_type(), CURRENT_ENV->get_object(o));
272 assert(_constant_value.as_object() == CURRENT_ENV->get_object(o), "check interning");
273 }
274 }
275 }
276 } else {
277 _is_constant = false;
278 }
279 }
280
281 // ------------------------------------------------------------------
282 // ciField::compute_type
283 //
284 // Lazily compute the type, if it is an instance klass.
285 ciType* ciField::compute_type() {
286 GUARDED_VM_ENTRY(return compute_type_impl();)
287 }
288
289 ciType* ciField::compute_type_impl() {
290 ciKlass* type = CURRENT_ENV->get_klass_by_name_impl(_holder, constantPoolHandle(), _signature, false);
291 if (!type->is_primitive_type() && is_shared()) {
292 // We must not cache a pointer to an unshared type, in a shared field.
293 bool type_is_also_shared = false;
294 if (type->is_type_array_klass()) {
295 type_is_also_shared = true; // int[] etc. are explicitly bootstrapped
356 if (is_put) {
357 _known_to_link_with_put = accessing_klass;
358 } else {
359 _known_to_link_with_get = accessing_klass;
360 }
361 }
362
363 return true;
364 }
365
366 // ------------------------------------------------------------------
367 // ciField::print
368 void ciField::print() {
369 tty->print("<ciField name=");
370 _holder->print_name();
371 tty->print(".");
372 _name->print_symbol();
373 tty->print(" signature=");
374 _signature->print_symbol();
375 tty->print(" offset=%d type=", _offset);
376 if (_type != NULL) _type->print_name();
377 else tty->print("(reference)");
378 tty->print(" is_constant=%s", bool_to_str(_is_constant));
379 if (_is_constant && is_static()) {
380 tty->print(" constant_value=");
381 _constant_value.print();
382 }
383 tty->print(">");
384 }
385
386 // ------------------------------------------------------------------
387 // ciField::print_name_on
388 //
389 // Print the name of this field
390 void ciField::print_name_on(outputStream* st) {
391 name()->print_symbol_on(st);
392 }
|
172
173 static bool trust_final_non_static_fields(ciInstanceKlass* holder) {
174 if (holder == NULL)
175 return false;
176 if (holder->name() == ciSymbol::java_lang_System())
177 // Never trust strangely unstable finals: System.out, etc.
178 return false;
179 // Even if general trusting is disabled, trust system-built closures in these packages.
180 if (holder->is_in_package("java/lang/invoke") || holder->is_in_package("sun/invoke"))
181 return true;
182 return TrustFinalNonStaticFields;
183 }
184
185 void ciField::initialize_from(fieldDescriptor* fd) {
186 // Get the flags, offset, and canonical holder of the field.
187 _flags = ciFlags(fd->access_flags());
188 _offset = fd->offset();
189 _holder = CURRENT_ENV->get_instance_klass(fd->field_holder());
190
191 // Check to see if the field is constant.
192 bool is_final = this->is_final();
193 bool is_stable = FoldStableValues && this->is_stable();
194 if (_holder->is_initialized() && (is_final || is_stable)) {
195 if (!this->is_static()) {
196 // A field can be constant if it's a final static field or if
197 // it's a final non-static field of a trusted class (classes in
198 // java.lang.invoke and sun.invoke packages and subpackages).
199 if (is_stable || trust_final_non_static_fields(_holder)) {
200 _is_constant = true;
201 return;
202 }
203 _is_constant = false;
204 return;
205 }
206
207 // This field just may be constant. The only cases where it will
208 // not be constant are:
209 //
210 // 1. The field holds a non-perm-space oop. The field is, strictly
211 // speaking, constant but we cannot embed non-perm-space oops into
212 // generated code. For the time being we need to consider the
213 // field to be not constant.
214 // 2. The field is a *special* static&final field whose value
215 // may change. The three examples are java.lang.System.in,
216 // java.lang.System.out, and java.lang.System.err.
217
218 KlassHandle k = _holder->get_Klass();
219 assert( SystemDictionary::System_klass() != NULL, "Check once per vm");
220 if( k() == SystemDictionary::System_klass() ) {
221 // Check offsets for case 2: System.in, System.out, or System.err
222 if( _offset == java_lang_System::in_offset_in_bytes() ||
223 _offset == java_lang_System::out_offset_in_bytes() ||
224 _offset == java_lang_System::err_offset_in_bytes() ) {
225 _is_constant = false;
226 return;
227 }
228 }
229
230 Handle mirror = k->java_mirror();
231
232 switch(type()->basic_type()) {
233 case T_BYTE:
234 _constant_value = ciConstant(type()->basic_type(), mirror->byte_field(_offset));
235 break;
236 case T_CHAR:
237 _constant_value = ciConstant(type()->basic_type(), mirror->char_field(_offset));
238 break;
239 case T_SHORT:
240 _constant_value = ciConstant(type()->basic_type(), mirror->short_field(_offset));
241 break;
242 case T_BOOLEAN:
243 _constant_value = ciConstant(type()->basic_type(), mirror->bool_field(_offset));
244 break;
245 case T_INT:
246 _constant_value = ciConstant(type()->basic_type(), mirror->int_field(_offset));
247 break;
248 case T_FLOAT:
249 _constant_value = ciConstant(mirror->float_field(_offset));
250 break;
251 case T_DOUBLE:
257 case T_OBJECT:
258 case T_ARRAY:
259 {
260 oop o = mirror->obj_field(_offset);
261
262 // A field will be "constant" if it is known always to be
263 // a non-null reference to an instance of a particular class,
264 // or to a particular array. This can happen even if the instance
265 // or array is not perm. In such a case, an "unloaded" ciArray
266 // or ciInstance is created. The compiler may be able to use
267 // information about the object's class (which is exact) or length.
268
269 if (o == NULL) {
270 _constant_value = ciConstant(type()->basic_type(), ciNullObject::make());
271 } else {
272 _constant_value = ciConstant(type()->basic_type(), CURRENT_ENV->get_object(o));
273 assert(_constant_value.as_object() == CURRENT_ENV->get_object(o), "check interning");
274 }
275 }
276 }
277 if (is_stable && _constant_value.is_null_or_zero()) {
278 // It is not a constant after all; treat it as uninitialized.
279 _is_constant = false;
280 } else {
281 _is_constant = true;
282 }
283 } else {
284 _is_constant = false;
285 }
286 }
287
288 // ------------------------------------------------------------------
289 // ciField::compute_type
290 //
291 // Lazily compute the type, if it is an instance klass.
292 ciType* ciField::compute_type() {
293 GUARDED_VM_ENTRY(return compute_type_impl();)
294 }
295
296 ciType* ciField::compute_type_impl() {
297 ciKlass* type = CURRENT_ENV->get_klass_by_name_impl(_holder, constantPoolHandle(), _signature, false);
298 if (!type->is_primitive_type() && is_shared()) {
299 // We must not cache a pointer to an unshared type, in a shared field.
300 bool type_is_also_shared = false;
301 if (type->is_type_array_klass()) {
302 type_is_also_shared = true; // int[] etc. are explicitly bootstrapped
363 if (is_put) {
364 _known_to_link_with_put = accessing_klass;
365 } else {
366 _known_to_link_with_get = accessing_klass;
367 }
368 }
369
370 return true;
371 }
372
373 // ------------------------------------------------------------------
374 // ciField::print
375 void ciField::print() {
376 tty->print("<ciField name=");
377 _holder->print_name();
378 tty->print(".");
379 _name->print_symbol();
380 tty->print(" signature=");
381 _signature->print_symbol();
382 tty->print(" offset=%d type=", _offset);
383 if (_type != NULL)
384 _type->print_name();
385 else
386 tty->print("(reference)");
387 tty->print(" flags=%04x", flags().as_int());
388 tty->print(" is_constant=%s", bool_to_str(_is_constant));
389 if (_is_constant && is_static()) {
390 tty->print(" constant_value=");
391 _constant_value.print();
392 }
393 tty->print(">");
394 }
395
396 // ------------------------------------------------------------------
397 // ciField::print_name_on
398 //
399 // Print the name of this field
400 void ciField::print_name_on(outputStream* st) {
401 name()->print_symbol_on(st);
402 }
|