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   friend class NonStaticFieldFiller;
43 
44 private:
45   ciFlags          _flags;
46   ciInstanceKlass* _holder;
47   ciSymbol*        _name;
48   ciSymbol*        _signature;
49   ciType*          _type;
50   int              _offset;
51   bool             _is_constant;
52   ciInstanceKlass* _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 
62   // shared constructor code
63   void initialize_from(fieldDescriptor* fd);
64 
65 public:
flags()66   ciFlags flags() { return _flags; }
67 
68   // Of which klass is this field a member?
69   //
70   // Usage note: the declared holder of a field is the class
71   // referenced by name in the bytecodes.  The canonical holder
72   // is the most general class which holds the field.  This
73   // method returns the canonical holder.  The declared holder
74   // can be accessed via a method in ciBytecodeStream.
75   //
76   // Ex.
77   //     class A {
78   //       public int f = 7;
79   //     }
80   //     class B extends A {
81   //       public void test() {
82   //         System.out.println(f);
83   //       }
84   //     }
85   //
86   //   A java compiler is permitted to compile the access to
87   //   field f as:
88   //
89   //     getfield B.f
90   //
91   //   In that case the declared holder of f would be B and
92   //   the canonical holder of f would be A.
holder()93   ciInstanceKlass* holder() { return _holder; }
94 
95   // Name of this field?
name()96   ciSymbol* name() { return _name; }
97 
98   // Signature of this field?
signature()99   ciSymbol* signature() { return _signature; }
100 
101   // Of what type is this field?
type()102   ciType* type() { return (_type == NULL) ? compute_type() : _type; }
103 
104   // How is this field actually stored in memory?
layout_type()105   BasicType layout_type() { return type2field[(_type == NULL) ? T_OBJECT : _type->basic_type()]; }
106 
107   // How big is this field in memory?
size_in_bytes()108   int size_in_bytes() { return type2aelembytes(layout_type()); }
109 
110   // What is the offset of this field?
offset()111   int offset() {
112     assert(_offset >= 1, "illegal call to offset()");
113     return _offset;
114   }
115 
116   // Same question, explicit units.  (Fields are aligned to the byte level.)
offset_in_bytes()117   int offset_in_bytes() {
118     return offset();
119   }
120 
121   // Is this field shared?
is_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? See ciField::initialize_from() for details
128   // about how a field is determined to be constant.
is_constant()129   bool is_constant() { return _is_constant; }
130 
131   // Get the constant value of this field.
constant_value()132   ciConstant constant_value() {
133     assert(is_static() && is_constant(), "illegal call to constant_value()");
134     return _constant_value;
135   }
136 
137   // Get the constant value of non-static final field in the given
138   // object.
constant_value_of(ciObject * object)139   ciConstant constant_value_of(ciObject* object) {
140     assert(!is_static() && is_constant(), "only if field is non-static constant");
141     assert(object->is_instance(), "must be instance");
142     return object->as_instance()->field_value(this);
143   }
144 
145   // Check for link time errors.  Accessing a field from a
146   // certain class via a certain bytecode may or may not be legal.
147   // This call checks to see if an exception may be raised by
148   // an access of this field.
149   //
150   // Usage note: if the same field is accessed multiple times
151   // in the same compilation, will_link will need to be checked
152   // at each point of access.
153   bool will_link(ciInstanceKlass* accessing_klass,
154                  Bytecodes::Code bc);
155 
156   // Java access flags
is_public()157   bool is_public      () { return flags().is_public(); }
is_private()158   bool is_private     () { return flags().is_private(); }
is_protected()159   bool is_protected   () { return flags().is_protected(); }
is_static()160   bool is_static      () { return flags().is_static(); }
is_final()161   bool is_final       () { return flags().is_final(); }
is_stable()162   bool is_stable      () { return flags().is_stable(); }
is_volatile()163   bool is_volatile    () { return flags().is_volatile(); }
is_transient()164   bool is_transient   () { return flags().is_transient(); }
165   // The field is modified outside of instance initializer methods
166   // (or class/initializer methods if the field is static).
has_initialized_final_update()167   bool has_initialized_final_update() { return flags().has_initialized_final_update(); }
168 
is_call_site_target()169   bool is_call_site_target() {
170     ciInstanceKlass* callsite_klass = CURRENT_ENV->CallSite_klass();
171     if (callsite_klass == NULL)
172       return false;
173     return (holder()->is_subclass_of(callsite_klass) && (name() == ciSymbol::target_name()));
174   }
175 
176   // Debugging output
177   void print();
178   void print_name_on(outputStream* st);
179 };
180 
181 #endif // SHARE_VM_CI_CIFIELD_HPP
182