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 ciInstanceKlass* _known_to_link_with_put; 52 ciInstanceKlass* _known_to_link_with_get; 53 ciConstant _constant_value; 54 55 ciType* compute_type(); 56 ciType* compute_type_impl(); 57 58 ciField(ciInstanceKlass* klass, int index); 59 ciField(fieldDescriptor* fd); 60 61 // shared constructor code 62 void initialize_from(fieldDescriptor* fd); 63 64 public: 65 ciFlags flags() { return _flags; } 66 67 // Of which klass is this field a member? 68 // 69 // Usage note: the declared holder of a field is the class 70 // referenced by name in the bytecodes. The canonical holder 71 // is the most general class which holds the field. This 72 // method returns the canonical holder. The declared holder 73 // can be accessed via a method in ciBytecodeStream. 74 // 75 // Ex. 76 // class A { 77 // public int f = 7; 78 // } 79 // class B extends A { 80 // public void test() { 81 // System.out.println(f); 82 // } 83 // } 84 // 85 // A java compiler is permitted to compile the access to 86 // field f as: 87 // 88 // getfield B.f 89 // 90 // In that case the declared holder of f would be B and 91 // the canonical holder of f would be A. 92 ciInstanceKlass* holder() { return _holder; } 93 94 // Name of this field? 95 ciSymbol* name() { return _name; } 96 97 // Signature of this field? 98 ciSymbol* signature() { return _signature; } 99 100 // Of what type is this field? 101 ciType* type() { return (_type == NULL) ? compute_type() : _type; } 102 103 // How is this field actually stored in memory? 104 BasicType layout_type() { return type2field[(_type == NULL) ? T_OBJECT : _type->basic_type()]; } 105 106 // How big is this field in memory? 107 int size_in_bytes() { return type2aelembytes(layout_type()); } 108 109 // What is the offset of this field? 110 int offset() { 111 assert(_offset >= 1, "illegal call to offset()"); 112 return _offset; 113 } 114 115 // Same question, explicit units. (Fields are aligned to the byte level.) 116 int offset_in_bytes() { 117 return offset(); 118 } 119 120 // Is this field shared? 121 bool is_shared() { 122 // non-static fields of shared holders are cached 123 return _holder->is_shared() && !is_static(); 124 } 125 126 // Is this field a constant? 127 // 128 // Clarification: A field is considered constant if: 129 // 1. The field is both static and final 130 // 2. The canonical holder of the field has undergone 131 // static initialization. 132 // 3. 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 it is marked @Stable 137 // and is non-null (or non-zero, if a primitive). 138 // For non-static fields, the null/zero check must be 139 // arranged by the user, as constant_value().is_null_or_zero(). 140 bool is_constant() { return _is_constant; } 141 142 // Get the constant value of this field. 143 ciConstant constant_value() { 144 assert(is_static() && is_constant(), "illegal call to constant_value()"); 145 return _constant_value; 146 } 147 148 // Get the constant value of non-static final field in the given 149 // object. 150 ciConstant constant_value_of(ciObject* object) { 151 assert(!is_static() && is_constant(), "only if field is non-static constant"); 152 assert(object->is_instance(), "must be instance"); 153 return object->as_instance()->field_value(this); 154 } 155 156 // Check for link time errors. Accessing a field from a 157 // certain class via a certain bytecode may or may not be legal. 158 // This call checks to see if an exception may be raised by 159 // an access of this field. 160 // 161 // Usage note: if the same field is accessed multiple times 162 // in the same compilation, will_link will need to be checked 163 // at each point of access. 164 bool will_link(ciInstanceKlass* accessing_klass, 165 Bytecodes::Code bc); 166 167 // Java access flags 168 bool is_public () { return flags().is_public(); } 169 bool is_private () { return flags().is_private(); } 170 bool is_protected () { return flags().is_protected(); } 171 bool is_static () { return flags().is_static(); } 172 bool is_final () { return flags().is_final(); } 173 bool is_stable () { return flags().is_stable(); } 174 bool is_volatile () { return flags().is_volatile(); } 175 bool is_transient () { return flags().is_transient(); } 176 177 bool is_call_site_target() { 178 ciInstanceKlass* callsite_klass = CURRENT_ENV->CallSite_klass(); 179 if (callsite_klass == NULL) 180 return false; 181 return (holder()->is_subclass_of(callsite_klass) && (name() == ciSymbol::target_name())); 182 } 183 184 bool is_autobox_cache() { 185 ciSymbol* klass_name = holder()->name(); 186 return (name() == ciSymbol::cache_field_name() && 187 holder()->uses_default_loader() && 188 (klass_name == ciSymbol::java_lang_Character_CharacterCache() || 189 klass_name == ciSymbol::java_lang_Byte_ByteCache() || 190 klass_name == ciSymbol::java_lang_Short_ShortCache() || 191 klass_name == ciSymbol::java_lang_Integer_IntegerCache() || 192 klass_name == ciSymbol::java_lang_Long_LongCache())); 193 } 194 195 // Debugging output 196 void print(); 197 void print_name_on(outputStream* st); 198 }; 199 200 #endif // SHARE_VM_CI_CIFIELD_HPP