Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/ci/ciField.cpp
32285 views
/*1* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "ci/ciField.hpp"26#include "ci/ciInstanceKlass.hpp"27#include "ci/ciUtilities.hpp"28#include "classfile/systemDictionary.hpp"29#include "gc_interface/collectedHeap.inline.hpp"30#include "interpreter/linkResolver.hpp"31#include "memory/universe.inline.hpp"32#include "oops/oop.inline.hpp"33#include "oops/oop.inline2.hpp"34#include "runtime/fieldDescriptor.hpp"3536// ciField37//38// This class represents the result of a field lookup in the VM.39// The lookup may not succeed, in which case the information in40// the ciField will be incomplete.4142// The ciObjectFactory cannot create circular data structures in one query.43// To avoid vicious circularities, we initialize ciField::_type to NULL44// for reference types and derive it lazily from the ciField::_signature.45// Primitive types are eagerly initialized, and basic layout queries46// can succeed without initialization, using only the BasicType of the field.4748// Notes on bootstrapping and shared CI objects: A field is shared if and49// only if it is (a) non-static and (b) declared by a shared instance klass.50// This allows non-static field lists to be cached on shared types.51// Because the _type field is lazily initialized, however, there is a52// special restriction that a shared field cannot cache an unshared type.53// This puts a small performance penalty on shared fields with unshared54// types, such as StackTraceElement[] Throwable.stackTrace.55// (Throwable is shared because ClassCastException is shared, but56// StackTraceElement is not presently shared.)5758// It is not a vicious circularity for a ciField to recursively create59// the ciSymbols necessary to represent its name and signature.60// Therefore, these items are created eagerly, and the name and signature61// of a shared field are themselves shared symbols. This somewhat62// pollutes the set of shared CI objects: It grows from 50 to 93 items,63// with all of the additional 43 being uninteresting shared ciSymbols.64// This adds at most one step to the binary search, an amount which65// decreases for complex compilation tasks.6667// ------------------------------------------------------------------68// ciField::ciField69ciField::ciField(ciInstanceKlass* klass, int index): _known_to_link_with_put(NULL), _known_to_link_with_get(NULL) {70ASSERT_IN_VM;71CompilerThread *thread = CompilerThread::current();7273assert(ciObjectFactory::is_initialized(), "not a shared field");7475assert(klass->get_instanceKlass()->is_linked(), "must be linked before using its constan-pool");7677constantPoolHandle cpool(thread, klass->get_instanceKlass()->constants());7879// Get the field's name, signature, and type.80Symbol* name = cpool->name_ref_at(index);81_name = ciEnv::current(thread)->get_symbol(name);8283int nt_index = cpool->name_and_type_ref_index_at(index);84int sig_index = cpool->signature_ref_index_at(nt_index);85Symbol* signature = cpool->symbol_at(sig_index);86_signature = ciEnv::current(thread)->get_symbol(signature);8788BasicType field_type = FieldType::basic_type(signature);8990// If the field is a pointer type, get the klass of the91// field.92if (field_type == T_OBJECT || field_type == T_ARRAY) {93bool ignore;94// This is not really a class reference; the index always refers to the95// field's type signature, as a symbol. Linkage checks do not apply.96_type = ciEnv::current(thread)->get_klass_by_index(cpool, sig_index, ignore, klass);97} else {98_type = ciType::make(field_type);99}100101_name = (ciSymbol*)ciEnv::current(thread)->get_symbol(name);102103// Get the field's declared holder.104//105// Note: we actually create a ciInstanceKlass for this klass,106// even though we may not need to.107int holder_index = cpool->klass_ref_index_at(index);108bool holder_is_accessible;109ciInstanceKlass* declared_holder =110ciEnv::current(thread)->get_klass_by_index(cpool, holder_index,111holder_is_accessible,112klass)->as_instance_klass();113114// The declared holder of this field may not have been loaded.115// Bail out with partial field information.116if (!holder_is_accessible) {117// _type has already been set.118// The default values for _flags and _constant_value will suffice.119// We need values for _holder, _offset, and _is_constant,120_holder = declared_holder;121_offset = -1;122_is_constant = false;123return;124}125126InstanceKlass* loaded_decl_holder = declared_holder->get_instanceKlass();127128// Perform the field lookup.129fieldDescriptor field_desc;130Klass* canonical_holder =131loaded_decl_holder->find_field(name, signature, &field_desc);132if (canonical_holder == NULL) {133// Field lookup failed. Will be detected by will_link.134_holder = declared_holder;135_offset = -1;136_is_constant = false;137return;138}139140// Access check based on declared_holder. canonical_holder should not be used141// to check access because it can erroneously succeed. If this check fails,142// propagate the declared holder to will_link() which in turn will bail out143// compilation for this field access.144if (!Reflection::verify_field_access(klass->get_Klass(), declared_holder->get_Klass(), canonical_holder, field_desc.access_flags(), true)) {145_holder = declared_holder;146_offset = -1;147_is_constant = false;148return;149}150151assert(canonical_holder == field_desc.field_holder(), "just checking");152initialize_from(&field_desc);153}154155ciField::ciField(fieldDescriptor *fd): _known_to_link_with_put(NULL), _known_to_link_with_get(NULL) {156ASSERT_IN_VM;157158// Get the field's name, signature, and type.159ciEnv* env = CURRENT_ENV;160_name = env->get_symbol(fd->name());161_signature = env->get_symbol(fd->signature());162163BasicType field_type = fd->field_type();164165// If the field is a pointer type, get the klass of the166// field.167if (field_type == T_OBJECT || field_type == T_ARRAY) {168_type = NULL; // must call compute_type on first access169} else {170_type = ciType::make(field_type);171}172173initialize_from(fd);174175// Either (a) it is marked shared, or else (b) we are done bootstrapping.176assert(is_shared() || ciObjectFactory::is_initialized(),177"bootstrap classes must not create & cache unshared fields");178}179180static bool trust_final_non_static_fields(ciInstanceKlass* holder) {181if (holder == NULL)182return false;183if (holder->name() == ciSymbol::java_lang_System())184// Never trust strangely unstable finals: System.out, etc.185return false;186// Even if general trusting is disabled, trust system-built closures in these packages.187if (holder->is_in_package("java/lang/invoke") || holder->is_in_package("sun/invoke"))188return true;189// Trust Atomic*FieldUpdaters: they are very important for performance, and make up one190// more reason not to use Unsafe, if their final fields are trusted. See more in JDK-8140483.191if (holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicIntegerFieldUpdater_Impl() ||192holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicLongFieldUpdater_CASUpdater() ||193holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicLongFieldUpdater_LockedUpdater() ||194holder->name() == ciSymbol::java_util_concurrent_atomic_AtomicReferenceFieldUpdater_Impl()) {195return true;196}197return TrustFinalNonStaticFields;198}199200void ciField::initialize_from(fieldDescriptor* fd) {201// Get the flags, offset, and canonical holder of the field.202_flags = ciFlags(fd->access_flags());203_offset = fd->offset();204_holder = CURRENT_ENV->get_instance_klass(fd->field_holder());205206// Check to see if the field is constant.207bool is_final = this->is_final();208bool is_stable = FoldStableValues && this->is_stable();209if (_holder->is_initialized() && ((is_final && !has_initialized_final_update()) || is_stable)) {210if (!this->is_static()) {211// A field can be constant if it's a final static field or if212// it's a final non-static field of a trusted class (classes in213// java.lang.invoke and sun.invoke packages and subpackages).214if (is_stable || trust_final_non_static_fields(_holder)) {215_is_constant = true;216return;217}218_is_constant = false;219return;220}221222// This field just may be constant. The only cases where it will223// not be constant are:224//225// 1. The field holds a non-perm-space oop. The field is, strictly226// speaking, constant but we cannot embed non-perm-space oops into227// generated code. For the time being we need to consider the228// field to be not constant.229// 2. The field is a *special* static&final field whose value230// may change. The three examples are java.lang.System.in,231// java.lang.System.out, and java.lang.System.err.232233KlassHandle k = _holder->get_Klass();234assert( SystemDictionary::System_klass() != NULL, "Check once per vm");235if( k() == SystemDictionary::System_klass() ) {236// Check offsets for case 2: System.in, System.out, or System.err237if( _offset == java_lang_System::in_offset_in_bytes() ||238_offset == java_lang_System::out_offset_in_bytes() ||239_offset == java_lang_System::err_offset_in_bytes() ) {240_is_constant = false;241return;242}243}244245Handle mirror = k->java_mirror();246247switch(type()->basic_type()) {248case T_BYTE:249_constant_value = ciConstant(type()->basic_type(), mirror->byte_field(_offset));250break;251case T_CHAR:252_constant_value = ciConstant(type()->basic_type(), mirror->char_field(_offset));253break;254case T_SHORT:255_constant_value = ciConstant(type()->basic_type(), mirror->short_field(_offset));256break;257case T_BOOLEAN:258_constant_value = ciConstant(type()->basic_type(), mirror->bool_field(_offset));259break;260case T_INT:261_constant_value = ciConstant(type()->basic_type(), mirror->int_field(_offset));262break;263case T_FLOAT:264_constant_value = ciConstant(mirror->float_field(_offset));265break;266case T_DOUBLE:267_constant_value = ciConstant(mirror->double_field(_offset));268break;269case T_LONG:270_constant_value = ciConstant(mirror->long_field(_offset));271break;272case T_OBJECT:273case T_ARRAY:274{275oop o = mirror->obj_field(_offset);276277// A field will be "constant" if it is known always to be278// a non-null reference to an instance of a particular class,279// or to a particular array. This can happen even if the instance280// or array is not perm. In such a case, an "unloaded" ciArray281// or ciInstance is created. The compiler may be able to use282// information about the object's class (which is exact) or length.283284if (o == NULL) {285_constant_value = ciConstant(type()->basic_type(), ciNullObject::make());286} else {287_constant_value = ciConstant(type()->basic_type(), CURRENT_ENV->get_object(o));288assert(_constant_value.as_object() == CURRENT_ENV->get_object(o), "check interning");289}290}291}292if (is_stable && _constant_value.is_null_or_zero()) {293// It is not a constant after all; treat it as uninitialized.294_is_constant = false;295} else {296_is_constant = true;297}298} else {299_is_constant = false;300}301}302303// ------------------------------------------------------------------304// ciField::compute_type305//306// Lazily compute the type, if it is an instance klass.307ciType* ciField::compute_type() {308GUARDED_VM_ENTRY(return compute_type_impl();)309}310311ciType* ciField::compute_type_impl() {312ciKlass* type = CURRENT_ENV->get_klass_by_name_impl(_holder, constantPoolHandle(), _signature, false);313if (!type->is_primitive_type() && is_shared()) {314// We must not cache a pointer to an unshared type, in a shared field.315bool type_is_also_shared = false;316if (type->is_type_array_klass()) {317type_is_also_shared = true; // int[] etc. are explicitly bootstrapped318} else if (type->is_instance_klass()) {319type_is_also_shared = type->as_instance_klass()->is_shared();320} else {321// Currently there is no 'shared' query for array types.322type_is_also_shared = !ciObjectFactory::is_initialized();323}324if (!type_is_also_shared)325return type; // Bummer.326}327_type = type;328return type;329}330331332// ------------------------------------------------------------------333// ciField::will_link334//335// Can a specific access to this field be made without causing336// link errors?337bool ciField::will_link(ciInstanceKlass* accessing_klass,338Bytecodes::Code bc) {339VM_ENTRY_MARK;340assert(bc == Bytecodes::_getstatic || bc == Bytecodes::_putstatic ||341bc == Bytecodes::_getfield || bc == Bytecodes::_putfield,342"unexpected bytecode");343344if (_offset == -1) {345// at creation we couldn't link to our holder so we need to346// maintain that stance, otherwise there's no safe way to use this347// ciField.348return false;349}350351// Check for static/nonstatic mismatch352bool is_static = (bc == Bytecodes::_getstatic || bc == Bytecodes::_putstatic);353if (is_static != this->is_static()) {354return false;355}356357// Get and put can have different accessibility rules358bool is_put = (bc == Bytecodes::_putfield || bc == Bytecodes::_putstatic);359if (is_put) {360if (_known_to_link_with_put == accessing_klass) {361return true;362}363} else {364if (_known_to_link_with_get == accessing_klass) {365return true;366}367}368369fieldDescriptor result;370LinkResolver::resolve_field(result, _holder->get_instanceKlass(),371_name->get_symbol(), _signature->get_symbol(),372accessing_klass->get_Klass(), bc, true, false,373KILL_COMPILE_ON_FATAL_(false));374375// update the hit-cache, unless there is a problem with memory scoping:376if (accessing_klass->is_shared() || !is_shared()) {377if (is_put) {378_known_to_link_with_put = accessing_klass;379} else {380_known_to_link_with_get = accessing_klass;381}382}383384return true;385}386387// ------------------------------------------------------------------388// ciField::print389void ciField::print() {390tty->print("<ciField name=");391_holder->print_name();392tty->print(".");393_name->print_symbol();394tty->print(" signature=");395_signature->print_symbol();396tty->print(" offset=%d type=", _offset);397if (_type != NULL)398_type->print_name();399else400tty->print("(reference)");401tty->print(" flags=%04x", flags().as_int());402tty->print(" is_constant=%s", bool_to_str(_is_constant));403if (_is_constant && is_static()) {404tty->print(" constant_value=");405_constant_value.print();406}407tty->print(">");408}409410// ------------------------------------------------------------------411// ciField::print_name_on412//413// Print the name of this field414void ciField::print_name_on(outputStream* st) {415name()->print_symbol_on(st);416}417418419