ciField.cpp revision 3602:da91efe96a93
1227569Sphilip/*
2301388Sarybchik * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
3284555Sarybchik * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4227569Sphilip *
5227569Sphilip * This code is free software; you can redistribute it and/or modify it
6284555Sarybchik * under the terms of the GNU General Public License version 2 only, as
7227569Sphilip * published by the Free Software Foundation.
8284555Sarybchik *
9284555Sarybchik * This code is distributed in the hope that it will be useful, but WITHOUT
10284555Sarybchik * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11284555Sarybchik * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12284555Sarybchik * version 2 for more details (a copy is included in the LICENSE file that
13284555Sarybchik * accompanied this code).
14284555Sarybchik *
15284555Sarybchik * You should have received a copy of the GNU General Public License version
16284555Sarybchik * 2 along with this work; if not, write to the Free Software Foundation,
17284555Sarybchik * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18284555Sarybchik *
19284555Sarybchik * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20284555Sarybchik * or visit www.oracle.com if you need additional information or have any
21284555Sarybchik * questions.
22284555Sarybchik *
23284555Sarybchik */
24284555Sarybchik
25284555Sarybchik#include "precompiled.hpp"
26284555Sarybchik#include "ci/ciField.hpp"
27284555Sarybchik#include "ci/ciInstanceKlass.hpp"
28284555Sarybchik#include "ci/ciUtilities.hpp"
29227569Sphilip#include "classfile/systemDictionary.hpp"
30228078Sphilip#include "gc_interface/collectedHeap.inline.hpp"
31228078Sphilip#include "interpreter/linkResolver.hpp"
32228078Sphilip#include "memory/universe.inline.hpp"
33228078Sphilip#include "oops/oop.inline.hpp"
34227569Sphilip#include "oops/oop.inline2.hpp"
35227569Sphilip#include "runtime/fieldDescriptor.hpp"
36284555Sarybchik
37227569Sphilip// ciField
38227569Sphilip//
39227569Sphilip// This class represents the result of a field lookup in the VM.
40311064Sarybchik// The lookup may not succeed, in which case the information in
41311064Sarybchik// the ciField will be incomplete.
42293927Sarybchik
43227569Sphilip// The ciObjectFactory cannot create circular data structures in one query.
44227569Sphilip// To avoid vicious circularities, we initialize ciField::_type to NULL
45227569Sphilip// for reference types and derive it lazily from the ciField::_signature.
46227569Sphilip// Primitive types are eagerly initialized, and basic layout queries
47227569Sphilip// can succeed without initialization, using only the BasicType of the field.
48342516Sarybchik
49342516Sarybchik// Notes on bootstrapping and shared CI objects:  A field is shared if and
50293927Sarybchik// only if it is (a) non-static and (b) declared by a shared instance klass.
51227569Sphilip// This allows non-static field lists to be cached on shared types.
52227569Sphilip// Because the _type field is lazily initialized, however, there is a
53284555Sarybchik// special restriction that a shared field cannot cache an unshared type.
54284555Sarybchik// This puts a small performance penalty on shared fields with unshared
55284555Sarybchik// types, such as StackTraceElement[] Throwable.stackTrace.
56284555Sarybchik// (Throwable is shared because ClassCastException is shared, but
57227569Sphilip// StackTraceElement is not presently shared.)
58227569Sphilip
59227569Sphilip// It is not a vicious circularity for a ciField to recursively create
60227569Sphilip// the ciSymbols necessary to represent its name and signature.
61227569Sphilip// Therefore, these items are created eagerly, and the name and signature
62227569Sphilip// of a shared field are themselves shared symbols.  This somewhat
63227569Sphilip// pollutes the set of shared CI objects:  It grows from 50 to 93 items,
64227569Sphilip// with all of the additional 43 being uninteresting shared ciSymbols.
65227569Sphilip// This adds at most one step to the binary search, an amount which
66227569Sphilip// decreases for complex compilation tasks.
67227569Sphilip
68227569Sphilip// ------------------------------------------------------------------
69227569Sphilip// ciField::ciField
70227569SphilipciField::ciField(ciInstanceKlass* klass, int index): _known_to_link_with_put(NULL), _known_to_link_with_get(NULL) {
71227569Sphilip  ASSERT_IN_VM;
72227569Sphilip  CompilerThread *thread = CompilerThread::current();
73227569Sphilip
74227569Sphilip  assert(ciObjectFactory::is_initialized(), "not a shared field");
75227569Sphilip
76227569Sphilip  assert(klass->get_instanceKlass()->is_linked(), "must be linked before using its constan-pool");
77293927Sarybchik
78227569Sphilip  _cp_index = index;
79227569Sphilip  constantPoolHandle cpool(thread, klass->get_instanceKlass()->constants());
80227569Sphilip
81227569Sphilip  // Get the field's name, signature, and type.
82311064Sarybchik  Symbol* name  = cpool->name_ref_at(index);
83311064Sarybchik  _name = ciEnv::current(thread)->get_symbol(name);
84293927Sarybchik
85227569Sphilip  int nt_index = cpool->name_and_type_ref_index_at(index);
86227569Sphilip  int sig_index = cpool->signature_ref_index_at(nt_index);
87227569Sphilip  Symbol* signature = cpool->symbol_at(sig_index);
88227569Sphilip  _signature = ciEnv::current(thread)->get_symbol(signature);
89284555Sarybchik
90284555Sarybchik  BasicType field_type = FieldType::basic_type(signature);
91284555Sarybchik
92284555Sarybchik  // If the field is a pointer type, get the klass of the
93293927Sarybchik  // field.
94227569Sphilip  if (field_type == T_OBJECT || field_type == T_ARRAY) {
95284555Sarybchik    bool ignore;
96284555Sarybchik    // This is not really a class reference; the index always refers to the
97284555Sarybchik    // field's type signature, as a symbol.  Linkage checks do not apply.
98227569Sphilip    _type = ciEnv::current(thread)->get_klass_by_index(cpool, sig_index, ignore, klass);
99284555Sarybchik  } else {
100284555Sarybchik    _type = ciType::make(field_type);
101227569Sphilip  }
102227569Sphilip
103280550Sarybchik  _name = (ciSymbol*)ciEnv::current(thread)->get_symbol(name);
104227569Sphilip
105284555Sarybchik  // Get the field's declared holder.
106227569Sphilip  //
107311079Sarybchik  // Note: we actually create a ciInstanceKlass for this klass,
108311079Sarybchik  // even though we may not need to.
109311079Sarybchik  int holder_index = cpool->klass_ref_index_at(index);
110311079Sarybchik  bool holder_is_accessible;
111311079Sarybchik  ciInstanceKlass* declared_holder =
112311079Sarybchik    ciEnv::current(thread)->get_klass_by_index(cpool, holder_index,
113311079Sarybchik                                               holder_is_accessible,
114280550Sarybchik                                               klass)->as_instance_klass();
115280550Sarybchik
116284555Sarybchik  // The declared holder of this field may not have been loaded.
117280550Sarybchik  // Bail out with partial field information.
118280550Sarybchik  if (!holder_is_accessible) {
119284555Sarybchik    // _cp_index and _type have already been set.
120284555Sarybchik    // The default values for _flags and _constant_value will suffice.
121280550Sarybchik    // We need values for _holder, _offset,  and _is_constant,
122284555Sarybchik    _holder = declared_holder;
123280550Sarybchik    _offset = -1;
124280550Sarybchik    _is_constant = false;
125280588Sarybchik    return;
126280588Sarybchik  }
127280588Sarybchik
128280588Sarybchik  InstanceKlass* loaded_decl_holder = declared_holder->get_instanceKlass();
129280550Sarybchik
130284555Sarybchik  // Perform the field lookup.
131284555Sarybchik  fieldDescriptor field_desc;
132227569Sphilip  Klass* canonical_holder =
133284555Sarybchik    loaded_decl_holder->find_field(name, signature, &field_desc);
134284555Sarybchik  if (canonical_holder == NULL) {
135284555Sarybchik    // Field lookup failed.  Will be detected by will_link.
136227569Sphilip    _holder = declared_holder;
137284555Sarybchik    _offset = -1;
138284555Sarybchik    _is_constant = false;
139227569Sphilip    return;
140311768Sarybchik  }
141311768Sarybchik
142311768Sarybchik  assert(canonical_holder == field_desc.field_holder(), "just checking");
143311768Sarybchik  initialize_from(&field_desc);
144284555Sarybchik}
145284555Sarybchik
146284555SarybchikciField::ciField(fieldDescriptor *fd): _known_to_link_with_put(NULL), _known_to_link_with_get(NULL) {
147284555Sarybchik  ASSERT_IN_VM;
148284555Sarybchik
149284555Sarybchik  _cp_index = -1;
150284555Sarybchik
151284555Sarybchik  // Get the field's name, signature, and type.
152284555Sarybchik  ciEnv* env = CURRENT_ENV;
153227569Sphilip  _name = env->get_symbol(fd->name());
154284555Sarybchik  _signature = env->get_symbol(fd->signature());
155284555Sarybchik
156284555Sarybchik  BasicType field_type = fd->field_type();
157227569Sphilip
158342480Sarybchik  // If the field is a pointer type, get the klass of the
159342480Sarybchik  // field.
160227569Sphilip  if (field_type == T_OBJECT || field_type == T_ARRAY) {
161280562Sarybchik    _type = NULL;  // must call compute_type on first access
162280562Sarybchik  } else {
163227569Sphilip    _type = ciType::make(field_type);
164284555Sarybchik  }
165284555Sarybchik
166294381Sarybchik  initialize_from(fd);
167311078Sarybchik
168293953Sarybchik  // Either (a) it is marked shared, or else (b) we are done bootstrapping.
169284555Sarybchik  assert(is_shared() || ciObjectFactory::is_initialized(),
170301365Sarybchik         "bootstrap classes must not create & cache unshared fields");
171301365Sarybchik}
172301365Sarybchik
173301365Sarybchikstatic bool trust_final_non_static_fields(ciInstanceKlass* holder) {
174311495Sarybchik  if (holder == NULL)
175311495Sarybchik    return false;
176227569Sphilip  if (holder->name() == ciSymbol::java_lang_System())
177227569Sphilip    // Never trust strangely unstable finals:  System.out, etc.
178227569Sphilip    return false;
179227569Sphilip  // Even if general trusting is disabled, trust system-built closures in these packages.
180227569Sphilip  if (holder->is_in_package("java/lang/invoke") || holder->is_in_package("sun/invoke"))
181293927Sarybchik    return true;
182227569Sphilip  return TrustFinalNonStaticFields;
183227569Sphilip}
184227569Sphilip
185227569Sphilipvoid ciField::initialize_from(fieldDescriptor* fd) {
186293927Sarybchik  // Get the flags, offset, and canonical holder of the field.
187227569Sphilip  _flags = ciFlags(fd->access_flags());
188227569Sphilip  _offset = fd->offset();
189227569Sphilip  _holder = CURRENT_ENV->get_instance_klass(fd->field_holder());
190342495Sarybchik
191227569Sphilip  // Check to see if the field is constant.
192342495Sarybchik  if (_holder->is_initialized() && this->is_final()) {
193293927Sarybchik    if (!this->is_static()) {
194227569Sphilip      // A field can be constant if it's a final static field or if
195284555Sarybchik      // it's a final non-static field of a trusted class (classes in
196284555Sarybchik      // java.lang.invoke and sun.invoke packages and subpackages).
197227569Sphilip      if (trust_final_non_static_fields(_holder)) {
198227569Sphilip        _is_constant = true;
199227569Sphilip        return;
200227569Sphilip      }
201284555Sarybchik      _is_constant = false;
202227569Sphilip      return;
203227569Sphilip    }
204227569Sphilip
205227569Sphilip    // This field just may be constant.  The only cases where it will
206227569Sphilip    // not be constant are:
207227569Sphilip    //
208293927Sarybchik    // 1. The field holds a non-perm-space oop.  The field is, strictly
209227569Sphilip    //    speaking, constant but we cannot embed non-perm-space oops into
210227569Sphilip    //    generated code.  For the time being we need to consider the
211227569Sphilip    //    field to be not constant.
212227569Sphilip    // 2. The field is a *special* static&final field whose value
213342507Sarybchik    //    may change.  The three examples are java.lang.System.in,
214342507Sarybchik    //    java.lang.System.out, and java.lang.System.err.
215342507Sarybchik
216342507Sarybchik    KlassHandle k = _holder->get_Klass();
217342507Sarybchik    assert( SystemDictionary::System_klass() != NULL, "Check once per vm");
218342507Sarybchik    if( k() == SystemDictionary::System_klass() ) {
219342507Sarybchik      // Check offsets for case 2: System.in, System.out, or System.err
220342507Sarybchik      if( _offset == java_lang_System::in_offset_in_bytes()  ||
221342507Sarybchik          _offset == java_lang_System::out_offset_in_bytes() ||
222342507Sarybchik          _offset == java_lang_System::err_offset_in_bytes() ) {
223342507Sarybchik        _is_constant = false;
224342507Sarybchik        return;
225342507Sarybchik      }
226342507Sarybchik    }
227342507Sarybchik
228342507Sarybchik    Handle mirror = k->java_mirror();
229342507Sarybchik
230342507Sarybchik    _is_constant = true;
231342507Sarybchik    switch(type()->basic_type()) {
232342507Sarybchik    case T_BYTE:
233342507Sarybchik      _constant_value = ciConstant(type()->basic_type(), mirror->byte_field(_offset));
234342507Sarybchik      break;
235342507Sarybchik    case T_CHAR:
236342507Sarybchik      _constant_value = ciConstant(type()->basic_type(), mirror->char_field(_offset));
237342507Sarybchik      break;
238342507Sarybchik    case T_SHORT:
239342507Sarybchik      _constant_value = ciConstant(type()->basic_type(), mirror->short_field(_offset));
240342507Sarybchik      break;
241342507Sarybchik    case T_BOOLEAN:
242342507Sarybchik      _constant_value = ciConstant(type()->basic_type(), mirror->bool_field(_offset));
243342507Sarybchik      break;
244342507Sarybchik    case T_INT:
245342507Sarybchik      _constant_value = ciConstant(type()->basic_type(), mirror->int_field(_offset));
246342507Sarybchik      break;
247342507Sarybchik    case T_FLOAT:
248342507Sarybchik      _constant_value = ciConstant(mirror->float_field(_offset));
249342507Sarybchik      break;
250342507Sarybchik    case T_DOUBLE:
251342507Sarybchik      _constant_value = ciConstant(mirror->double_field(_offset));
252342507Sarybchik      break;
253342507Sarybchik    case T_LONG:
254342507Sarybchik      _constant_value = ciConstant(mirror->long_field(_offset));
255342507Sarybchik      break;
256342507Sarybchik    case T_OBJECT:
257342507Sarybchik    case T_ARRAY:
258342507Sarybchik      {
259342507Sarybchik        oop o = mirror->obj_field(_offset);
260342507Sarybchik
261342507Sarybchik        // A field will be "constant" if it is known always to be
262342507Sarybchik        // a non-null reference to an instance of a particular class,
263342507Sarybchik        // or to a particular array.  This can happen even if the instance
264342507Sarybchik        // or array is not perm.  In such a case, an "unloaded" ciArray
265342507Sarybchik        // or ciInstance is created.  The compiler may be able to use
266342507Sarybchik        // information about the object's class (which is exact) or length.
267342507Sarybchik
268342507Sarybchik        if (o == NULL) {
269342507Sarybchik          _constant_value = ciConstant(type()->basic_type(), ciNullObject::make());
270342507Sarybchik        } else {
271342507Sarybchik          _constant_value = ciConstant(type()->basic_type(), CURRENT_ENV->get_object(o));
272342507Sarybchik          assert(_constant_value.as_object() == CURRENT_ENV->get_object(o), "check interning");
273342507Sarybchik        }
274342507Sarybchik      }
275342507Sarybchik    }
276342507Sarybchik  } else {
277342507Sarybchik    _is_constant = false;
278342507Sarybchik  }
279342507Sarybchik}
280342507Sarybchik
281342507Sarybchik// ------------------------------------------------------------------
282342507Sarybchik// ciField::compute_type
283342507Sarybchik//
284293927Sarybchik// Lazily compute the type, if it is an instance klass.
285227569SphilipciType* ciField::compute_type() {
286227569Sphilip  GUARDED_VM_ENTRY(return compute_type_impl();)
287227569Sphilip}
288227569Sphilip
289227569SphilipciType* ciField::compute_type_impl() {
290227569Sphilip  ciKlass* type = CURRENT_ENV->get_klass_by_name_impl(_holder, constantPoolHandle(), _signature, false);
291227569Sphilip  if (!type->is_primitive_type() && is_shared()) {
292284555Sarybchik    // We must not cache a pointer to an unshared type, in a shared field.
293293927Sarybchik    bool type_is_also_shared = false;
294227569Sphilip    if (type->is_type_array_klass()) {
295227569Sphilip      type_is_also_shared = true;  // int[] etc. are explicitly bootstrapped
296227569Sphilip    } else if (type->is_instance_klass()) {
297284555Sarybchik      type_is_also_shared = type->as_instance_klass()->is_shared();
298342507Sarybchik    } else {
299227569Sphilip      // Currently there is no 'shared' query for array types.
300227569Sphilip      type_is_also_shared = !ciObjectFactory::is_initialized();
301284555Sarybchik    }
302284555Sarybchik    if (!type_is_also_shared)
303284555Sarybchik      return type;              // Bummer.
304284555Sarybchik  }
305284555Sarybchik  _type = type;
306284555Sarybchik  return type;
307284555Sarybchik}
308284555Sarybchik
309284555Sarybchik
310284555Sarybchik// ------------------------------------------------------------------
311227569Sphilip// ciField::will_link
312227569Sphilip//
313284555Sarybchik// Can a specific access to this field be made without causing
314284555Sarybchik// link errors?
315227569Sphilipbool ciField::will_link(ciInstanceKlass* accessing_klass,
316227569Sphilip                        Bytecodes::Code bc) {
317284555Sarybchik  VM_ENTRY_MARK;
318284555Sarybchik  assert(bc == Bytecodes::_getstatic || bc == Bytecodes::_putstatic ||
319227569Sphilip         bc == Bytecodes::_getfield  || bc == Bytecodes::_putfield,
320227569Sphilip         "unexpected bytecode");
321284555Sarybchik
322227569Sphilip  if (_offset == -1) {
323227569Sphilip    // at creation we couldn't link to our holder so we need to
324284555Sarybchik    // maintain that stance, otherwise there's no safe way to use this
325284555Sarybchik    // ciField.
326284555Sarybchik    return false;
327227569Sphilip  }
328227569Sphilip
329284555Sarybchik  // Check for static/nonstatic mismatch
330227569Sphilip  bool is_static = (bc == Bytecodes::_getstatic || bc == Bytecodes::_putstatic);
331284555Sarybchik  if (is_static != this->is_static()) {
332227569Sphilip    return false;
333227569Sphilip  }
334227569Sphilip
335227569Sphilip  // Get and put can have different accessibility rules
336227569Sphilip  bool is_put    = (bc == Bytecodes::_putfield  || bc == Bytecodes::_putstatic);
337284555Sarybchik  if (is_put) {
338227569Sphilip    if (_known_to_link_with_put == accessing_klass) {
339227569Sphilip      return true;
340227569Sphilip    }
341227569Sphilip  } else {
342227569Sphilip    if (_known_to_link_with_get == accessing_klass) {
343284555Sarybchik      return true;
344284555Sarybchik    }
345227569Sphilip  }
346227569Sphilip
347227569Sphilip  FieldAccessInfo result;
348284555Sarybchik  constantPoolHandle c_pool(THREAD,
349284555Sarybchik                         accessing_klass->get_instanceKlass()->constants());
350227569Sphilip  LinkResolver::resolve_field(result, c_pool, _cp_index,
351227569Sphilip                              Bytecodes::java_code(bc),
352227569Sphilip                              true, false, KILL_COMPILE_ON_FATAL_(false));
353284555Sarybchik
354284555Sarybchik  // update the hit-cache, unless there is a problem with memory scoping:
355227569Sphilip  if (accessing_klass->is_shared() || !is_shared()) {
356227569Sphilip    if (is_put) {
357227569Sphilip      _known_to_link_with_put = accessing_klass;
358227569Sphilip    } else {
359227569Sphilip      _known_to_link_with_get = accessing_klass;
360227569Sphilip    }
361227569Sphilip  }
362284555Sarybchik
363284555Sarybchik  return true;
364284555Sarybchik}
365284555Sarybchik
366227569Sphilip// ------------------------------------------------------------------
367227569Sphilip// ciField::print
368227569Sphilipvoid ciField::print() {
369284555Sarybchik  tty->print("<ciField ");
370227569Sphilip  _holder->print_name();
371227569Sphilip  tty->print(".");
372227569Sphilip  _name->print_symbol();
373284555Sarybchik  tty->print(" offset=%d type=", _offset);
374227569Sphilip  if (_type != NULL) _type->print_name();
375227569Sphilip  else               tty->print("(reference)");
376227569Sphilip  tty->print(" is_constant=%s", bool_to_str(_is_constant));
377227569Sphilip  if (_is_constant && is_static()) {
378227569Sphilip    tty->print(" constant_value=");
379227569Sphilip    _constant_value.print();
380227569Sphilip  }
381227569Sphilip  tty->print(">");
382227569Sphilip}
383227569Sphilip
384227569Sphilip// ------------------------------------------------------------------
385227569Sphilip// ciField::print_name_on
386227569Sphilip//
387227569Sphilip// Print the name of this field
388227569Sphilipvoid ciField::print_name_on(outputStream* st) {
389227569Sphilip  name()->print_symbol_on(st);
390227569Sphilip}
391227569Sphilip