ciField.cpp 15.7 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29 30 31 32 33 34 35
#include "precompiled.hpp"
#include "ci/ciField.hpp"
#include "ci/ciInstanceKlass.hpp"
#include "ci/ciUtilities.hpp"
#include "classfile/systemDictionary.hpp"
#include "gc_interface/collectedHeap.inline.hpp"
#include "interpreter/linkResolver.hpp"
#include "memory/universe.inline.hpp"
#include "oops/oop.inline.hpp"
#include "oops/oop.inline2.hpp"
#include "runtime/fieldDescriptor.hpp"
D
duke 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

// ciField
//
// This class represents the result of a field lookup in the VM.
// The lookup may not succeed, in which case the information in
// the ciField will be incomplete.

// The ciObjectFactory cannot create circular data structures in one query.
// To avoid vicious circularities, we initialize ciField::_type to NULL
// for reference types and derive it lazily from the ciField::_signature.
// Primitive types are eagerly initialized, and basic layout queries
// can succeed without initialization, using only the BasicType of the field.

// Notes on bootstrapping and shared CI objects:  A field is shared if and
// only if it is (a) non-static and (b) declared by a shared instance klass.
// This allows non-static field lists to be cached on shared types.
// Because the _type field is lazily initialized, however, there is a
// special restriction that a shared field cannot cache an unshared type.
// This puts a small performance penalty on shared fields with unshared
// types, such as StackTraceElement[] Throwable.stackTrace.
// (Throwable is shared because ClassCastException is shared, but
// StackTraceElement is not presently shared.)

// It is not a vicious circularity for a ciField to recursively create
// the ciSymbols necessary to represent its name and signature.
// Therefore, these items are created eagerly, and the name and signature
// of a shared field are themselves shared symbols.  This somewhat
// pollutes the set of shared CI objects:  It grows from 50 to 93 items,
// with all of the additional 43 being uninteresting shared ciSymbols.
// This adds at most one step to the binary search, an amount which
// decreases for complex compilation tasks.

// ------------------------------------------------------------------
// ciField::ciField
N
never 已提交
70
ciField::ciField(ciInstanceKlass* klass, int index): _known_to_link_with_put(NULL), _known_to_link_with_get(NULL) {
D
duke 已提交
71 72 73 74 75 76 77 78 79 80
  ASSERT_IN_VM;
  CompilerThread *thread = CompilerThread::current();

  assert(ciObjectFactory::is_initialized(), "not a shared field");

  assert(klass->get_instanceKlass()->is_linked(), "must be linked before using its constan-pool");

  constantPoolHandle cpool(thread, klass->get_instanceKlass()->constants());

  // Get the field's name, signature, and type.
81 82
  Symbol* name  = cpool->name_ref_at(index);
  _name = ciEnv::current(thread)->get_symbol(name);
D
duke 已提交
83 84 85

  int nt_index = cpool->name_and_type_ref_index_at(index);
  int sig_index = cpool->signature_ref_index_at(nt_index);
86 87
  Symbol* signature = cpool->symbol_at(sig_index);
  _signature = ciEnv::current(thread)->get_symbol(signature);
D
duke 已提交
88

89
  BasicType field_type = FieldType::basic_type(signature);
D
duke 已提交
90 91 92 93 94 95 96

  // If the field is a pointer type, get the klass of the
  // field.
  if (field_type == T_OBJECT || field_type == T_ARRAY) {
    bool ignore;
    // This is not really a class reference; the index always refers to the
    // field's type signature, as a symbol.  Linkage checks do not apply.
97
    _type = ciEnv::current(thread)->get_klass_by_index(cpool, sig_index, ignore, klass);
D
duke 已提交
98 99 100 101
  } else {
    _type = ciType::make(field_type);
  }

102
  _name = (ciSymbol*)ciEnv::current(thread)->get_symbol(name);
D
duke 已提交
103 104 105 106 107 108 109 110

  // Get the field's declared holder.
  //
  // Note: we actually create a ciInstanceKlass for this klass,
  // even though we may not need to.
  int holder_index = cpool->klass_ref_index_at(index);
  bool holder_is_accessible;
  ciInstanceKlass* declared_holder =
111 112 113
    ciEnv::current(thread)->get_klass_by_index(cpool, holder_index,
                                               holder_is_accessible,
                                               klass)->as_instance_klass();
D
duke 已提交
114 115 116 117

  // The declared holder of this field may not have been loaded.
  // Bail out with partial field information.
  if (!holder_is_accessible) {
118
    // _type has already been set.
D
duke 已提交
119 120 121 122 123 124 125 126
    // The default values for _flags and _constant_value will suffice.
    // We need values for _holder, _offset,  and _is_constant,
    _holder = declared_holder;
    _offset = -1;
    _is_constant = false;
    return;
  }

127
  InstanceKlass* loaded_decl_holder = declared_holder->get_instanceKlass();
D
duke 已提交
128 129 130

  // Perform the field lookup.
  fieldDescriptor field_desc;
131
  Klass* canonical_holder =
132
    loaded_decl_holder->find_field(name, signature, &field_desc);
D
duke 已提交
133 134 135 136 137 138 139 140
  if (canonical_holder == NULL) {
    // Field lookup failed.  Will be detected by will_link.
    _holder = declared_holder;
    _offset = -1;
    _is_constant = false;
    return;
  }

141 142 143 144 145 146 147 148 149 150 151
  // Access check based on declared_holder. canonical_holder should not be used
  // to check access because it can erroneously succeed. If this check fails,
  // propagate the declared holder to will_link() which in turn will bail out
  // compilation for this field access.
  if (!Reflection::verify_field_access(klass->get_Klass(), declared_holder->get_Klass(), canonical_holder, field_desc.access_flags(), true)) {
    _holder = declared_holder;
    _offset = -1;
    _is_constant = false;
    return;
  }

D
duke 已提交
152 153 154 155
  assert(canonical_holder == field_desc.field_holder(), "just checking");
  initialize_from(&field_desc);
}

N
never 已提交
156
ciField::ciField(fieldDescriptor *fd): _known_to_link_with_put(NULL), _known_to_link_with_get(NULL) {
D
duke 已提交
157 158 159 160
  ASSERT_IN_VM;

  // Get the field's name, signature, and type.
  ciEnv* env = CURRENT_ENV;
161 162
  _name = env->get_symbol(fd->name());
  _signature = env->get_symbol(fd->signature());
D
duke 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

  BasicType field_type = fd->field_type();

  // If the field is a pointer type, get the klass of the
  // field.
  if (field_type == T_OBJECT || field_type == T_ARRAY) {
    _type = NULL;  // must call compute_type on first access
  } else {
    _type = ciType::make(field_type);
  }

  initialize_from(fd);

  // Either (a) it is marked shared, or else (b) we are done bootstrapping.
  assert(is_shared() || ciObjectFactory::is_initialized(),
         "bootstrap classes must not create & cache unshared fields");
}

181 182 183 184 185 186 187
static bool trust_final_non_static_fields(ciInstanceKlass* holder) {
  if (holder == NULL)
    return false;
  if (holder->name() == ciSymbol::java_lang_System())
    // Never trust strangely unstable finals:  System.out, etc.
    return false;
  // Even if general trusting is disabled, trust system-built closures in these packages.
188
  if (holder->is_in_package("java/lang/invoke") || holder->is_in_package("sun/invoke"))
189
    return true;
190 191 192 193 194 195 196 197
  // Trust Atomic*FieldUpdaters: they are very important for performance, and make up one
  // more reason not to use Unsafe, if their final fields are trusted. See more in JDK-8140483.
  if (holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicIntegerFieldUpdater_Impl() ||
      holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicLongFieldUpdater_CASUpdater() ||
      holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicLongFieldUpdater_LockedUpdater() ||
      holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicReferenceFieldUpdater_Impl()) {
    return true;
  }
198 199 200
  return TrustFinalNonStaticFields;
}

D
duke 已提交
201 202 203 204
void ciField::initialize_from(fieldDescriptor* fd) {
  // Get the flags, offset, and canonical holder of the field.
  _flags = ciFlags(fd->access_flags());
  _offset = fd->offset();
205
  _holder = CURRENT_ENV->get_instance_klass(fd->field_holder());
D
duke 已提交
206 207

  // Check to see if the field is constant.
208 209
  bool is_final = this->is_final();
  bool is_stable = FoldStableValues && this->is_stable();
210
  if (_holder->is_initialized() && ((is_final && !has_initialized_final_update()) || is_stable)) {
211
    if (!this->is_static()) {
212 213 214
      // A field can be constant if it's a final static field or if
      // it's a final non-static field of a trusted class (classes in
      // java.lang.invoke and sun.invoke packages and subpackages).
215
      if (is_stable || trust_final_non_static_fields(_holder)) {
216 217 218 219 220 221 222
        _is_constant = true;
        return;
      }
      _is_constant = false;
      return;
    }

D
duke 已提交
223 224 225 226 227 228 229 230 231 232 233
    // This field just may be constant.  The only cases where it will
    // not be constant are:
    //
    // 1. The field holds a non-perm-space oop.  The field is, strictly
    //    speaking, constant but we cannot embed non-perm-space oops into
    //    generated code.  For the time being we need to consider the
    //    field to be not constant.
    // 2. The field is a *special* static&final field whose value
    //    may change.  The three examples are java.lang.System.in,
    //    java.lang.System.out, and java.lang.System.err.

234
    KlassHandle k = _holder->get_Klass();
235
    assert( SystemDictionary::System_klass() != NULL, "Check once per vm");
N
never 已提交
236
    if( k() == SystemDictionary::System_klass() ) {
D
duke 已提交
237 238 239 240 241 242 243 244 245
      // Check offsets for case 2: System.in, System.out, or System.err
      if( _offset == java_lang_System::in_offset_in_bytes()  ||
          _offset == java_lang_System::out_offset_in_bytes() ||
          _offset == java_lang_System::err_offset_in_bytes() ) {
        _is_constant = false;
        return;
      }
    }

246 247
    Handle mirror = k->java_mirror();

D
duke 已提交
248 249
    switch(type()->basic_type()) {
    case T_BYTE:
250
      _constant_value = ciConstant(type()->basic_type(), mirror->byte_field(_offset));
D
duke 已提交
251 252
      break;
    case T_CHAR:
253
      _constant_value = ciConstant(type()->basic_type(), mirror->char_field(_offset));
D
duke 已提交
254 255
      break;
    case T_SHORT:
256
      _constant_value = ciConstant(type()->basic_type(), mirror->short_field(_offset));
D
duke 已提交
257 258
      break;
    case T_BOOLEAN:
259
      _constant_value = ciConstant(type()->basic_type(), mirror->bool_field(_offset));
D
duke 已提交
260 261
      break;
    case T_INT:
262
      _constant_value = ciConstant(type()->basic_type(), mirror->int_field(_offset));
D
duke 已提交
263 264
      break;
    case T_FLOAT:
265
      _constant_value = ciConstant(mirror->float_field(_offset));
D
duke 已提交
266 267
      break;
    case T_DOUBLE:
268
      _constant_value = ciConstant(mirror->double_field(_offset));
D
duke 已提交
269 270
      break;
    case T_LONG:
271
      _constant_value = ciConstant(mirror->long_field(_offset));
D
duke 已提交
272 273 274 275
      break;
    case T_OBJECT:
    case T_ARRAY:
      {
276
        oop o = mirror->obj_field(_offset);
D
duke 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292

        // A field will be "constant" if it is known always to be
        // a non-null reference to an instance of a particular class,
        // or to a particular array.  This can happen even if the instance
        // or array is not perm.  In such a case, an "unloaded" ciArray
        // or ciInstance is created.  The compiler may be able to use
        // information about the object's class (which is exact) or length.

        if (o == NULL) {
          _constant_value = ciConstant(type()->basic_type(), ciNullObject::make());
        } else {
          _constant_value = ciConstant(type()->basic_type(), CURRENT_ENV->get_object(o));
          assert(_constant_value.as_object() == CURRENT_ENV->get_object(o), "check interning");
        }
      }
    }
293 294 295 296 297 298
    if (is_stable && _constant_value.is_null_or_zero()) {
      // It is not a constant after all; treat it as uninitialized.
      _is_constant = false;
    } else {
      _is_constant = true;
    }
D
duke 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312
  } else {
    _is_constant = false;
  }
}

// ------------------------------------------------------------------
// ciField::compute_type
//
// Lazily compute the type, if it is an instance klass.
ciType* ciField::compute_type() {
  GUARDED_VM_ENTRY(return compute_type_impl();)
}

ciType* ciField::compute_type_impl() {
313
  ciKlass* type = CURRENT_ENV->get_klass_by_name_impl(_holder, constantPoolHandle(), _signature, false);
D
duke 已提交
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
  if (!type->is_primitive_type() && is_shared()) {
    // We must not cache a pointer to an unshared type, in a shared field.
    bool type_is_also_shared = false;
    if (type->is_type_array_klass()) {
      type_is_also_shared = true;  // int[] etc. are explicitly bootstrapped
    } else if (type->is_instance_klass()) {
      type_is_also_shared = type->as_instance_klass()->is_shared();
    } else {
      // Currently there is no 'shared' query for array types.
      type_is_also_shared = !ciObjectFactory::is_initialized();
    }
    if (!type_is_also_shared)
      return type;              // Bummer.
  }
  _type = type;
  return type;
}


// ------------------------------------------------------------------
// ciField::will_link
//
// Can a specific access to this field be made without causing
// link errors?
bool ciField::will_link(ciInstanceKlass* accessing_klass,
                        Bytecodes::Code bc) {
  VM_ENTRY_MARK;
N
never 已提交
341 342 343 344
  assert(bc == Bytecodes::_getstatic || bc == Bytecodes::_putstatic ||
         bc == Bytecodes::_getfield  || bc == Bytecodes::_putfield,
         "unexpected bytecode");

D
duke 已提交
345 346 347 348 349 350 351
  if (_offset == -1) {
    // at creation we couldn't link to our holder so we need to
    // maintain that stance, otherwise there's no safe way to use this
    // ciField.
    return false;
  }

N
never 已提交
352 353 354 355 356 357 358 359 360 361 362 363
  // Check for static/nonstatic mismatch
  bool is_static = (bc == Bytecodes::_getstatic || bc == Bytecodes::_putstatic);
  if (is_static != this->is_static()) {
    return false;
  }

  // Get and put can have different accessibility rules
  bool is_put    = (bc == Bytecodes::_putfield  || bc == Bytecodes::_putstatic);
  if (is_put) {
    if (_known_to_link_with_put == accessing_klass) {
      return true;
    }
N
never 已提交
364
  } else {
N
never 已提交
365 366 367
    if (_known_to_link_with_get == accessing_klass) {
      return true;
    }
D
duke 已提交
368 369
  }

370 371 372 373 374
  fieldDescriptor result;
  LinkResolver::resolve_field(result, _holder->get_instanceKlass(),
                              _name->get_symbol(), _signature->get_symbol(),
                              accessing_klass->get_Klass(), bc, true, false,
                              KILL_COMPILE_ON_FATAL_(false));
D
duke 已提交
375 376

  // update the hit-cache, unless there is a problem with memory scoping:
N
never 已提交
377 378 379 380 381 382 383
  if (accessing_klass->is_shared() || !is_shared()) {
    if (is_put) {
      _known_to_link_with_put = accessing_klass;
    } else {
      _known_to_link_with_get = accessing_klass;
    }
  }
D
duke 已提交
384 385 386 387 388 389 390

  return true;
}

// ------------------------------------------------------------------
// ciField::print
void ciField::print() {
391
  tty->print("<ciField name=");
D
duke 已提交
392 393 394
  _holder->print_name();
  tty->print(".");
  _name->print_symbol();
395 396
  tty->print(" signature=");
  _signature->print_symbol();
D
duke 已提交
397
  tty->print(" offset=%d type=", _offset);
398 399 400 401 402
  if (_type != NULL)
    _type->print_name();
  else
    tty->print("(reference)");
  tty->print(" flags=%04x", flags().as_int());
D
duke 已提交
403
  tty->print(" is_constant=%s", bool_to_str(_is_constant));
404
  if (_is_constant && is_static()) {
D
duke 已提交
405 406 407 408 409 410 411 412 413 414 415 416 417
    tty->print(" constant_value=");
    _constant_value.print();
  }
  tty->print(">");
}

// ------------------------------------------------------------------
// ciField::print_name_on
//
// Print the name of this field
void ciField::print_name_on(outputStream* st) {
  name()->print_symbol_on(st);
}