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