1 /*
   2  * Copyright 1999-2007 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(klass, sig_index, ignore);
  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(klass, holder_index,
 104                                                holder_is_accessible)
 105       ->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() &&
 172       this->is_final() && this->is_static()) {
 173     // This field just may be constant.  The only cases where it will
 174     // not be constant are:
 175     //
 176     // 1. The field holds a non-perm-space oop.  The field is, strictly
 177     //    speaking, constant but we cannot embed non-perm-space oops into
 178     //    generated code.  For the time being we need to consider the
 179     //    field to be not constant.
 180     // 2. The field is a *special* static&final field whose value
 181     //    may change.  The three examples are java.lang.System.in,
 182     //    java.lang.System.out, and java.lang.System.err.
 183 
 184     klassOop k = _holder->get_klassOop();
 185     assert( SystemDictionary::system_klass() != NULL, "Check once per vm");
 186     if( k == SystemDictionary::system_klass() ) {
 187       // Check offsets for case 2: System.in, System.out, or System.err
 188       if( _offset == java_lang_System::in_offset_in_bytes()  ||
 189           _offset == java_lang_System::out_offset_in_bytes() ||
 190           _offset == java_lang_System::err_offset_in_bytes() ) {
 191         _is_constant = false;
 192         return;
 193       }
 194     }
 195 
 196     _is_constant = true;
 197     switch(type()->basic_type()) {
 198     case T_BYTE:
 199       _constant_value = ciConstant(type()->basic_type(), k->byte_field(_offset));
 200       break;
 201     case T_CHAR:
 202       _constant_value = ciConstant(type()->basic_type(), k->char_field(_offset));
 203       break;
 204     case T_SHORT:
 205       _constant_value = ciConstant(type()->basic_type(), k->short_field(_offset));
 206       break;
 207     case T_BOOLEAN:
 208       _constant_value = ciConstant(type()->basic_type(), k->bool_field(_offset));
 209       break;
 210     case T_INT:
 211       _constant_value = ciConstant(type()->basic_type(), k->int_field(_offset));
 212       break;
 213     case T_FLOAT:
 214       _constant_value = ciConstant(k->float_field(_offset));
 215       break;
 216     case T_DOUBLE:
 217       _constant_value = ciConstant(k->double_field(_offset));
 218       break;
 219     case T_LONG:
 220       _constant_value = ciConstant(k->long_field(_offset));
 221       break;
 222     case T_OBJECT:
 223     case T_ARRAY:
 224       {
 225         oop o = k->obj_field(_offset);
 226 
 227         // A field will be "constant" if it is known always to be
 228         // a non-null reference to an instance of a particular class,
 229         // or to a particular array.  This can happen even if the instance
 230         // or array is not perm.  In such a case, an "unloaded" ciArray
 231         // or ciInstance is created.  The compiler may be able to use
 232         // information about the object's class (which is exact) or length.
 233 
 234         if (o == NULL) {
 235           _constant_value = ciConstant(type()->basic_type(), ciNullObject::make());
 236         } else {
 237           _constant_value = ciConstant(type()->basic_type(), CURRENT_ENV->get_object(o));
 238           assert(_constant_value.as_object() == CURRENT_ENV->get_object(o), "check interning");
 239         }
 240       }
 241     }
 242   } else {
 243     _is_constant = false;
 244   }
 245 }
 246 
 247 // ------------------------------------------------------------------
 248 // ciField::compute_type
 249 //
 250 // Lazily compute the type, if it is an instance klass.
 251 ciType* ciField::compute_type() {
 252   GUARDED_VM_ENTRY(return compute_type_impl();)
 253 }
 254 
 255 ciType* ciField::compute_type_impl() {
 256   ciKlass* type = CURRENT_ENV->get_klass_by_name_impl(_holder, _signature, false);
 257   if (!type->is_primitive_type() && is_shared()) {
 258     // We must not cache a pointer to an unshared type, in a shared field.
 259     bool type_is_also_shared = false;
 260     if (type->is_type_array_klass()) {
 261       type_is_also_shared = true;  // int[] etc. are explicitly bootstrapped
 262     } else if (type->is_instance_klass()) {
 263       type_is_also_shared = type->as_instance_klass()->is_shared();
 264     } else {
 265       // Currently there is no 'shared' query for array types.
 266       type_is_also_shared = !ciObjectFactory::is_initialized();
 267     }
 268     if (!type_is_also_shared)
 269       return type;              // Bummer.
 270   }
 271   _type = type;
 272   return type;
 273 }
 274 
 275 
 276 // ------------------------------------------------------------------
 277 // ciField::will_link
 278 //
 279 // Can a specific access to this field be made without causing
 280 // link errors?
 281 bool ciField::will_link(ciInstanceKlass* accessing_klass,
 282                         Bytecodes::Code bc) {
 283   VM_ENTRY_MARK;
 284   if (_offset == -1) {
 285     // at creation we couldn't link to our holder so we need to
 286     // maintain that stance, otherwise there's no safe way to use this
 287     // ciField.
 288     return false;
 289   }
 290 
 291   if (_known_to_link_with == accessing_klass) {
 292     return true;
 293   }
 294 
 295   FieldAccessInfo result;
 296   constantPoolHandle c_pool(THREAD,
 297                          accessing_klass->get_instanceKlass()->constants());
 298   LinkResolver::resolve_field(result, c_pool, _cp_index,
 299                               Bytecodes::java_code(bc),
 300                               true, false, KILL_COMPILE_ON_FATAL_(false));
 301 
 302   // update the hit-cache, unless there is a problem with memory scoping:
 303   if (accessing_klass->is_shared() || !is_shared())
 304     _known_to_link_with = accessing_klass;
 305 
 306   return true;
 307 }
 308 
 309 // ------------------------------------------------------------------
 310 // ciField::print
 311 void ciField::print() {
 312   tty->print("<ciField ");
 313   _holder->print_name();
 314   tty->print(".");
 315   _name->print_symbol();
 316   tty->print(" offset=%d type=", _offset);
 317   if (_type != NULL) _type->print_name();
 318   else               tty->print("(reference)");
 319   tty->print(" is_constant=%s", bool_to_str(_is_constant));
 320   if (_is_constant) {
 321     tty->print(" constant_value=");
 322     _constant_value.print();
 323   }
 324   tty->print(">");
 325 }
 326 
 327 // ------------------------------------------------------------------
 328 // ciField::print_name_on
 329 //
 330 // Print the name of this field
 331 void ciField::print_name_on(outputStream* st) {
 332   name()->print_symbol_on(st);
 333 }