1 /*
   2  * Copyright (c) 1999, 2013, 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 #ifndef SHARE_VM_CI_CIFIELD_HPP
  26 #define SHARE_VM_CI_CIFIELD_HPP
  27 
  28 #include "ci/ciClassList.hpp"
  29 #include "ci/ciConstant.hpp"
  30 #include "ci/ciFlags.hpp"
  31 #include "ci/ciInstance.hpp"
  32 
  33 // ciField
  34 //
  35 // This class represents the result of a field lookup in the VM.
  36 // The lookup may not succeed, in which case the information in
  37 // the ciField will be incomplete.
  38 class ciField : public ResourceObj {
  39   CI_PACKAGE_ACCESS
  40   friend class ciEnv;
  41   friend class ciInstanceKlass;
  42 
  43 private:
  44   ciFlags          _flags;
  45   ciInstanceKlass* _holder;
  46   ciSymbol*        _name;
  47   ciSymbol*        _signature;
  48   ciType*          _type;
  49   int              _offset;
  50   bool             _is_constant;
  51   bool             _is_flattened;
  52   ciMethod*        _known_to_link_with_put;
  53   ciInstanceKlass* _known_to_link_with_get;
  54   ciConstant       _constant_value;
  55 
  56   ciType* compute_type();
  57   ciType* compute_type_impl();
  58 
  59   ciField(ciInstanceKlass* klass, int index);
  60   ciField(fieldDescriptor* fd);
  61   ciField(ciField* field, ciInstanceKlass* holder, int offset, bool is_final);
  62 
  63   // shared constructor code
  64   void initialize_from(fieldDescriptor* fd);
  65 
  66 public:
  67   ciFlags flags() const { return _flags; }
  68 
  69   // Of which klass is this field a member?
  70   //
  71   // Usage note: the declared holder of a field is the class
  72   // referenced by name in the bytecodes.  The canonical holder
  73   // is the most general class which holds the field.  This
  74   // method returns the canonical holder.  The declared holder
  75   // can be accessed via a method in ciBytecodeStream.
  76   //
  77   // Ex.
  78   //     class A {
  79   //       public int f = 7;
  80   //     }
  81   //     class B extends A {
  82   //       public void test() {
  83   //         System.out.println(f);
  84   //       }
  85   //     }
  86   //
  87   //   A java compiler is permitted to compile the access to
  88   //   field f as:
  89   //
  90   //     getfield B.f
  91   //
  92   //   In that case the declared holder of f would be B and
  93   //   the canonical holder of f would be A.
  94   ciInstanceKlass* holder() const { return _holder; }
  95 
  96   // Name of this field?
  97   ciSymbol* name() const { return _name; }
  98 
  99   // Signature of this field?
 100   ciSymbol* signature() const { return _signature; }
 101 
 102   // Of what type is this field?
 103   ciType* type() { return (_type == NULL) ? compute_type() : _type; }
 104 
 105   // How is this field actually stored in memory?
 106   BasicType layout_type() { return type2field[(_type == NULL) ? T_OBJECT : _type->basic_type()]; }
 107 
 108   // How big is this field in memory?
 109   int size_in_bytes() { return type2aelembytes(layout_type()); }
 110 
 111   // What is the offset of this field?
 112   int offset() const {
 113     assert(_offset >= 1, "illegal call to offset()");
 114     return _offset;
 115   }
 116 
 117   // Same question, explicit units.  (Fields are aligned to the byte level.)
 118   int offset_in_bytes() const {
 119     return offset();
 120   }
 121 
 122   // Is this field shared?
 123   bool is_shared() {
 124     // non-static fields of shared holders are cached
 125     return _holder->is_shared() && !is_static();
 126   }
 127 
 128   // Is this field a constant?
 129   //
 130   // Clarification: A field is considered constant if:
 131   //   1. The field is both static and final
 132   //   2. The field is not one of the special static/final
 133   //      non-constant fields.  These are java.lang.System.in
 134   //      and java.lang.System.out.  Abomination.
 135   //
 136   // A field is also considered constant if
 137   // - it is marked @Stable and is non-null (or non-zero, if a primitive) or
 138   // - it is trusted or
 139   // - it is the target field of a CallSite object.
 140   //
 141   // See ciField::initialize_from() for more details.
 142   //
 143   // A user should also check the field value (constant_value().is_valid()), since
 144   // constant fields of non-initialized classes don't have values yet.
 145   bool is_constant() const { return _is_constant; }
 146 
 147   // Get the constant value of the static field.
 148   ciConstant constant_value();
 149 
 150   bool is_static_constant() {
 151     return is_static() && is_constant() && constant_value().is_valid();
 152   }
 153 
 154   // Get the constant value of non-static final field in the given
 155   // object.
 156   ciConstant constant_value_of(ciObject* object);
 157 
 158   // Check for link time errors.  Accessing a field from a
 159   // certain method via a certain bytecode may or may not be legal.
 160   // This call checks to see if an exception may be raised by
 161   // an access of this field.
 162   //
 163   // Usage note: if the same field is accessed multiple times
 164   // in the same compilation, will_link will need to be checked
 165   // at each point of access.
 166   bool will_link(ciMethod* accessing_method,
 167                  Bytecodes::Code bc);
 168 
 169   // Java access flags
 170   bool is_public               () const { return flags().is_public(); }
 171   bool is_private              () const { return flags().is_private(); }
 172   bool is_protected            () const { return flags().is_protected(); }
 173   bool is_static               () const { return flags().is_static(); }
 174   bool is_final                () const { return flags().is_final(); }
 175   bool is_stable               () const { return flags().is_stable(); }
 176   bool is_volatile             () const { return flags().is_volatile(); }
 177   bool is_transient            () const { return flags().is_transient(); }
 178   bool is_flattened            () const { return _is_flattened; }
 179   // The field is modified outside of instance initializer methods
 180   // (or class/initializer methods if the field is static).
 181   bool has_initialized_final_update() const { return flags().has_initialized_final_update(); }
 182 
 183   bool is_call_site_target() {
 184     ciInstanceKlass* callsite_klass = CURRENT_ENV->CallSite_klass();
 185     if (callsite_klass == NULL)
 186       return false;
 187     return (holder()->is_subclass_of(callsite_klass) && (name() == ciSymbol::target_name()));
 188   }
 189 
 190   bool is_autobox_cache() {
 191     ciSymbol* klass_name = holder()->name();
 192     return (name() == ciSymbol::cache_field_name() &&
 193             holder()->uses_default_loader() &&
 194             (klass_name == ciSymbol::java_lang_Character_CharacterCache() ||
 195              klass_name == ciSymbol::java_lang_Byte_ByteCache() ||
 196              klass_name == ciSymbol::java_lang_Short_ShortCache() ||
 197              klass_name == ciSymbol::java_lang_Integer_IntegerCache() ||
 198              klass_name == ciSymbol::java_lang_Long_LongCache()));
 199   }
 200 
 201   // Debugging output
 202   void print();
 203   void print_name_on(outputStream* st);
 204 };
 205 
 206 #endif // SHARE_VM_CI_CIFIELD_HPP