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