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