src/share/vm/ci/ciField.cpp

Wed, 13 Jan 2010 23:05:52 -0800

author
jrose
date
Wed, 13 Jan 2010 23:05:52 -0800
changeset 1608
73b22f919c34
parent 1577
4ce7240d622c
child 1907
c18cbe5936b8
permissions
-rw-r--r--

6912065: final fields in objects need to support inlining optimizations for JSR 292
Reviewed-by: twisti, kvn

duke@435 1 /*
twisti@1573 2 * Copyright 1999-2009 Sun Microsystems, Inc. All Rights Reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 #include "incls/_precompiled.incl"
duke@435 26 #include "incls/_ciField.cpp.incl"
duke@435 27
duke@435 28 // ciField
duke@435 29 //
duke@435 30 // This class represents the result of a field lookup in the VM.
duke@435 31 // The lookup may not succeed, in which case the information in
duke@435 32 // the ciField will be incomplete.
duke@435 33
duke@435 34 // The ciObjectFactory cannot create circular data structures in one query.
duke@435 35 // To avoid vicious circularities, we initialize ciField::_type to NULL
duke@435 36 // for reference types and derive it lazily from the ciField::_signature.
duke@435 37 // Primitive types are eagerly initialized, and basic layout queries
duke@435 38 // can succeed without initialization, using only the BasicType of the field.
duke@435 39
duke@435 40 // Notes on bootstrapping and shared CI objects: A field is shared if and
duke@435 41 // only if it is (a) non-static and (b) declared by a shared instance klass.
duke@435 42 // This allows non-static field lists to be cached on shared types.
duke@435 43 // Because the _type field is lazily initialized, however, there is a
duke@435 44 // special restriction that a shared field cannot cache an unshared type.
duke@435 45 // This puts a small performance penalty on shared fields with unshared
duke@435 46 // types, such as StackTraceElement[] Throwable.stackTrace.
duke@435 47 // (Throwable is shared because ClassCastException is shared, but
duke@435 48 // StackTraceElement is not presently shared.)
duke@435 49
duke@435 50 // It is not a vicious circularity for a ciField to recursively create
duke@435 51 // the ciSymbols necessary to represent its name and signature.
duke@435 52 // Therefore, these items are created eagerly, and the name and signature
duke@435 53 // of a shared field are themselves shared symbols. This somewhat
duke@435 54 // pollutes the set of shared CI objects: It grows from 50 to 93 items,
duke@435 55 // with all of the additional 43 being uninteresting shared ciSymbols.
duke@435 56 // This adds at most one step to the binary search, an amount which
duke@435 57 // decreases for complex compilation tasks.
duke@435 58
duke@435 59 // ------------------------------------------------------------------
duke@435 60 // ciField::ciField
duke@435 61 ciField::ciField(ciInstanceKlass* klass, int index): _known_to_link_with(NULL) {
duke@435 62 ASSERT_IN_VM;
duke@435 63 CompilerThread *thread = CompilerThread::current();
duke@435 64
duke@435 65 assert(ciObjectFactory::is_initialized(), "not a shared field");
duke@435 66
duke@435 67 assert(klass->get_instanceKlass()->is_linked(), "must be linked before using its constan-pool");
duke@435 68
duke@435 69 _cp_index = index;
duke@435 70 constantPoolHandle cpool(thread, klass->get_instanceKlass()->constants());
duke@435 71
duke@435 72 // Get the field's name, signature, and type.
duke@435 73 symbolHandle name (thread, cpool->name_ref_at(index));
duke@435 74 _name = ciEnv::current(thread)->get_object(name())->as_symbol();
duke@435 75
duke@435 76 int nt_index = cpool->name_and_type_ref_index_at(index);
duke@435 77 int sig_index = cpool->signature_ref_index_at(nt_index);
duke@435 78 symbolHandle signature (thread, cpool->symbol_at(sig_index));
duke@435 79 _signature = ciEnv::current(thread)->get_object(signature())->as_symbol();
duke@435 80
duke@435 81 BasicType field_type = FieldType::basic_type(signature());
duke@435 82
duke@435 83 // If the field is a pointer type, get the klass of the
duke@435 84 // field.
duke@435 85 if (field_type == T_OBJECT || field_type == T_ARRAY) {
duke@435 86 bool ignore;
duke@435 87 // This is not really a class reference; the index always refers to the
duke@435 88 // field's type signature, as a symbol. Linkage checks do not apply.
twisti@1573 89 _type = ciEnv::current(thread)->get_klass_by_index(cpool, sig_index, ignore, klass);
duke@435 90 } else {
duke@435 91 _type = ciType::make(field_type);
duke@435 92 }
duke@435 93
duke@435 94 _name = (ciSymbol*)ciEnv::current(thread)->get_object(name());
duke@435 95
duke@435 96 // Get the field's declared holder.
duke@435 97 //
duke@435 98 // Note: we actually create a ciInstanceKlass for this klass,
duke@435 99 // even though we may not need to.
duke@435 100 int holder_index = cpool->klass_ref_index_at(index);
duke@435 101 bool holder_is_accessible;
duke@435 102 ciInstanceKlass* declared_holder =
twisti@1573 103 ciEnv::current(thread)->get_klass_by_index(cpool, holder_index,
twisti@1573 104 holder_is_accessible,
twisti@1573 105 klass)->as_instance_klass();
duke@435 106
duke@435 107 // The declared holder of this field may not have been loaded.
duke@435 108 // Bail out with partial field information.
duke@435 109 if (!holder_is_accessible) {
duke@435 110 // _cp_index and _type have already been set.
duke@435 111 // The default values for _flags and _constant_value will suffice.
duke@435 112 // We need values for _holder, _offset, and _is_constant,
duke@435 113 _holder = declared_holder;
duke@435 114 _offset = -1;
duke@435 115 _is_constant = false;
duke@435 116 return;
duke@435 117 }
duke@435 118
duke@435 119 instanceKlass* loaded_decl_holder = declared_holder->get_instanceKlass();
duke@435 120
duke@435 121 // Perform the field lookup.
duke@435 122 fieldDescriptor field_desc;
duke@435 123 klassOop canonical_holder =
duke@435 124 loaded_decl_holder->find_field(name(), signature(), &field_desc);
duke@435 125 if (canonical_holder == NULL) {
duke@435 126 // Field lookup failed. Will be detected by will_link.
duke@435 127 _holder = declared_holder;
duke@435 128 _offset = -1;
duke@435 129 _is_constant = false;
duke@435 130 return;
duke@435 131 }
duke@435 132
duke@435 133 assert(canonical_holder == field_desc.field_holder(), "just checking");
duke@435 134 initialize_from(&field_desc);
duke@435 135 }
duke@435 136
duke@435 137 ciField::ciField(fieldDescriptor *fd): _known_to_link_with(NULL) {
duke@435 138 ASSERT_IN_VM;
duke@435 139
duke@435 140 _cp_index = -1;
duke@435 141
duke@435 142 // Get the field's name, signature, and type.
duke@435 143 ciEnv* env = CURRENT_ENV;
duke@435 144 _name = env->get_object(fd->name())->as_symbol();
duke@435 145 _signature = env->get_object(fd->signature())->as_symbol();
duke@435 146
duke@435 147 BasicType field_type = fd->field_type();
duke@435 148
duke@435 149 // If the field is a pointer type, get the klass of the
duke@435 150 // field.
duke@435 151 if (field_type == T_OBJECT || field_type == T_ARRAY) {
duke@435 152 _type = NULL; // must call compute_type on first access
duke@435 153 } else {
duke@435 154 _type = ciType::make(field_type);
duke@435 155 }
duke@435 156
duke@435 157 initialize_from(fd);
duke@435 158
duke@435 159 // Either (a) it is marked shared, or else (b) we are done bootstrapping.
duke@435 160 assert(is_shared() || ciObjectFactory::is_initialized(),
duke@435 161 "bootstrap classes must not create & cache unshared fields");
duke@435 162 }
duke@435 163
jrose@1608 164 static bool trust_final_non_static_fields(ciInstanceKlass* holder) {
jrose@1608 165 if (holder == NULL)
jrose@1608 166 return false;
jrose@1608 167 if (holder->name() == ciSymbol::java_lang_System())
jrose@1608 168 // Never trust strangely unstable finals: System.out, etc.
jrose@1608 169 return false;
jrose@1608 170 // Even if general trusting is disabled, trust system-built closures in these packages.
jrose@1608 171 if (holder->is_in_package("java/dyn") || holder->is_in_package("sun/dyn"))
jrose@1608 172 return true;
jrose@1608 173 return TrustFinalNonStaticFields;
jrose@1608 174 }
jrose@1608 175
duke@435 176 void ciField::initialize_from(fieldDescriptor* fd) {
duke@435 177 // Get the flags, offset, and canonical holder of the field.
duke@435 178 _flags = ciFlags(fd->access_flags());
duke@435 179 _offset = fd->offset();
duke@435 180 _holder = CURRENT_ENV->get_object(fd->field_holder())->as_instance_klass();
duke@435 181
duke@435 182 // Check to see if the field is constant.
twisti@1573 183 if (_holder->is_initialized() && this->is_final()) {
twisti@1573 184 if (!this->is_static()) {
twisti@1573 185 // A field can be constant if it's a final static field or if it's
twisti@1573 186 // a final non-static field of a trusted class ({java,sun}.dyn).
jrose@1608 187 if (trust_final_non_static_fields(_holder)) {
twisti@1573 188 _is_constant = true;
twisti@1573 189 return;
twisti@1573 190 }
twisti@1573 191 _is_constant = false;
twisti@1573 192 return;
twisti@1573 193 }
twisti@1573 194
duke@435 195 // This field just may be constant. The only cases where it will
duke@435 196 // not be constant are:
duke@435 197 //
duke@435 198 // 1. The field holds a non-perm-space oop. The field is, strictly
duke@435 199 // speaking, constant but we cannot embed non-perm-space oops into
duke@435 200 // generated code. For the time being we need to consider the
duke@435 201 // field to be not constant.
duke@435 202 // 2. The field is a *special* static&final field whose value
duke@435 203 // may change. The three examples are java.lang.System.in,
duke@435 204 // java.lang.System.out, and java.lang.System.err.
duke@435 205
duke@435 206 klassOop k = _holder->get_klassOop();
never@1577 207 assert( SystemDictionary::System_klass() != NULL, "Check once per vm");
never@1577 208 if( k == SystemDictionary::System_klass() ) {
duke@435 209 // Check offsets for case 2: System.in, System.out, or System.err
duke@435 210 if( _offset == java_lang_System::in_offset_in_bytes() ||
duke@435 211 _offset == java_lang_System::out_offset_in_bytes() ||
duke@435 212 _offset == java_lang_System::err_offset_in_bytes() ) {
duke@435 213 _is_constant = false;
duke@435 214 return;
duke@435 215 }
duke@435 216 }
duke@435 217
duke@435 218 _is_constant = true;
duke@435 219 switch(type()->basic_type()) {
duke@435 220 case T_BYTE:
duke@435 221 _constant_value = ciConstant(type()->basic_type(), k->byte_field(_offset));
duke@435 222 break;
duke@435 223 case T_CHAR:
duke@435 224 _constant_value = ciConstant(type()->basic_type(), k->char_field(_offset));
duke@435 225 break;
duke@435 226 case T_SHORT:
duke@435 227 _constant_value = ciConstant(type()->basic_type(), k->short_field(_offset));
duke@435 228 break;
duke@435 229 case T_BOOLEAN:
duke@435 230 _constant_value = ciConstant(type()->basic_type(), k->bool_field(_offset));
duke@435 231 break;
duke@435 232 case T_INT:
duke@435 233 _constant_value = ciConstant(type()->basic_type(), k->int_field(_offset));
duke@435 234 break;
duke@435 235 case T_FLOAT:
duke@435 236 _constant_value = ciConstant(k->float_field(_offset));
duke@435 237 break;
duke@435 238 case T_DOUBLE:
duke@435 239 _constant_value = ciConstant(k->double_field(_offset));
duke@435 240 break;
duke@435 241 case T_LONG:
duke@435 242 _constant_value = ciConstant(k->long_field(_offset));
duke@435 243 break;
duke@435 244 case T_OBJECT:
duke@435 245 case T_ARRAY:
duke@435 246 {
duke@435 247 oop o = k->obj_field(_offset);
duke@435 248
duke@435 249 // A field will be "constant" if it is known always to be
duke@435 250 // a non-null reference to an instance of a particular class,
duke@435 251 // or to a particular array. This can happen even if the instance
duke@435 252 // or array is not perm. In such a case, an "unloaded" ciArray
duke@435 253 // or ciInstance is created. The compiler may be able to use
duke@435 254 // information about the object's class (which is exact) or length.
duke@435 255
duke@435 256 if (o == NULL) {
duke@435 257 _constant_value = ciConstant(type()->basic_type(), ciNullObject::make());
duke@435 258 } else {
duke@435 259 _constant_value = ciConstant(type()->basic_type(), CURRENT_ENV->get_object(o));
duke@435 260 assert(_constant_value.as_object() == CURRENT_ENV->get_object(o), "check interning");
duke@435 261 }
duke@435 262 }
duke@435 263 }
duke@435 264 } else {
duke@435 265 _is_constant = false;
duke@435 266 }
duke@435 267 }
duke@435 268
duke@435 269 // ------------------------------------------------------------------
duke@435 270 // ciField::compute_type
duke@435 271 //
duke@435 272 // Lazily compute the type, if it is an instance klass.
duke@435 273 ciType* ciField::compute_type() {
duke@435 274 GUARDED_VM_ENTRY(return compute_type_impl();)
duke@435 275 }
duke@435 276
duke@435 277 ciType* ciField::compute_type_impl() {
duke@435 278 ciKlass* type = CURRENT_ENV->get_klass_by_name_impl(_holder, _signature, false);
duke@435 279 if (!type->is_primitive_type() && is_shared()) {
duke@435 280 // We must not cache a pointer to an unshared type, in a shared field.
duke@435 281 bool type_is_also_shared = false;
duke@435 282 if (type->is_type_array_klass()) {
duke@435 283 type_is_also_shared = true; // int[] etc. are explicitly bootstrapped
duke@435 284 } else if (type->is_instance_klass()) {
duke@435 285 type_is_also_shared = type->as_instance_klass()->is_shared();
duke@435 286 } else {
duke@435 287 // Currently there is no 'shared' query for array types.
duke@435 288 type_is_also_shared = !ciObjectFactory::is_initialized();
duke@435 289 }
duke@435 290 if (!type_is_also_shared)
duke@435 291 return type; // Bummer.
duke@435 292 }
duke@435 293 _type = type;
duke@435 294 return type;
duke@435 295 }
duke@435 296
duke@435 297
duke@435 298 // ------------------------------------------------------------------
duke@435 299 // ciField::will_link
duke@435 300 //
duke@435 301 // Can a specific access to this field be made without causing
duke@435 302 // link errors?
duke@435 303 bool ciField::will_link(ciInstanceKlass* accessing_klass,
duke@435 304 Bytecodes::Code bc) {
duke@435 305 VM_ENTRY_MARK;
duke@435 306 if (_offset == -1) {
duke@435 307 // at creation we couldn't link to our holder so we need to
duke@435 308 // maintain that stance, otherwise there's no safe way to use this
duke@435 309 // ciField.
duke@435 310 return false;
duke@435 311 }
duke@435 312
duke@435 313 if (_known_to_link_with == accessing_klass) {
duke@435 314 return true;
duke@435 315 }
duke@435 316
duke@435 317 FieldAccessInfo result;
duke@435 318 constantPoolHandle c_pool(THREAD,
duke@435 319 accessing_klass->get_instanceKlass()->constants());
duke@435 320 LinkResolver::resolve_field(result, c_pool, _cp_index,
duke@435 321 Bytecodes::java_code(bc),
duke@435 322 true, false, KILL_COMPILE_ON_FATAL_(false));
duke@435 323
duke@435 324 // update the hit-cache, unless there is a problem with memory scoping:
duke@435 325 if (accessing_klass->is_shared() || !is_shared())
duke@435 326 _known_to_link_with = accessing_klass;
duke@435 327
duke@435 328 return true;
duke@435 329 }
duke@435 330
duke@435 331 // ------------------------------------------------------------------
duke@435 332 // ciField::print
duke@435 333 void ciField::print() {
duke@435 334 tty->print("<ciField ");
duke@435 335 _holder->print_name();
duke@435 336 tty->print(".");
duke@435 337 _name->print_symbol();
duke@435 338 tty->print(" offset=%d type=", _offset);
duke@435 339 if (_type != NULL) _type->print_name();
duke@435 340 else tty->print("(reference)");
duke@435 341 tty->print(" is_constant=%s", bool_to_str(_is_constant));
duke@435 342 if (_is_constant) {
duke@435 343 tty->print(" constant_value=");
duke@435 344 _constant_value.print();
duke@435 345 }
duke@435 346 tty->print(">");
duke@435 347 }
duke@435 348
duke@435 349 // ------------------------------------------------------------------
duke@435 350 // ciField::print_name_on
duke@435 351 //
duke@435 352 // Print the name of this field
duke@435 353 void ciField::print_name_on(outputStream* st) {
duke@435 354 name()->print_symbol_on(st);
duke@435 355 }

mercurial