1 /* 2 * Copyright (c) 2017, 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_OOPS_VALUEKLASS_HPP 26 #define SHARE_VM_OOPS_VALUEKLASS_HPP 27 28 #include "oops/instanceKlass.hpp" 29 #include "oops/method.hpp" 30 #include "oops/oop.inline.hpp" 31 32 // A ValueKlass is a specialized InstanceKlass for value types. 33 34 35 class ValueKlass: public InstanceKlass { 36 friend class VMStructs; 37 friend class InstanceKlass; 38 39 private: 40 41 // Constructor 42 ValueKlass(const ClassFileParser& parser) 43 : InstanceKlass(parser, InstanceKlass::_misc_kind_value_type, InstanceKlass::ID) { 44 _adr_valueklass_fixed_block = valueklass_static_block(); 45 // Addresses used for value type calling convention 46 *((Array<SigEntry>**)adr_extended_sig()) = NULL; 47 *((Array<VMRegPair>**)adr_return_regs()) = NULL; 48 *((address*)adr_pack_handler()) = NULL; 49 *((address*)adr_unpack_handler()) = NULL; 50 assert(pack_handler() == NULL, "pack handler not null"); 51 *((int*)adr_default_value_offset()) = 0; 52 set_prototype_header(markOopDesc::always_locked_prototype()); 53 } 54 55 ValueKlassFixedBlock* valueklass_static_block() const { 56 address adr_jf = adr_value_fields_klasses(); 57 if (adr_jf != NULL) { 58 return (ValueKlassFixedBlock*)(adr_jf + this->java_fields_count() * sizeof(Klass*)); 59 } 60 61 address adr_fing = adr_fingerprint(); 62 if (adr_fing != NULL) { 63 return (ValueKlassFixedBlock*)(adr_fingerprint() + sizeof(u8)); 64 } 65 66 InstanceKlass** adr_host = adr_unsafe_anonymous_host(); 67 if (adr_host != NULL) { 68 return (ValueKlassFixedBlock*)(adr_host + 1); 69 } 70 71 Klass** adr_impl = adr_implementor(); 72 if (adr_impl != NULL) { 73 return (ValueKlassFixedBlock*)(adr_impl + 1); 74 } 75 76 return (ValueKlassFixedBlock*)end_of_nonstatic_oop_maps(); 77 } 78 79 address adr_extended_sig() const { 80 assert(_adr_valueklass_fixed_block != NULL, "Should have been initialized"); 81 return ((address)_adr_valueklass_fixed_block) + in_bytes(byte_offset_of(ValueKlassFixedBlock, _extended_sig)); 82 } 83 84 address adr_return_regs() const { 85 ValueKlassFixedBlock* vkst = valueklass_static_block(); 86 return ((address)_adr_valueklass_fixed_block) + in_bytes(byte_offset_of(ValueKlassFixedBlock, _return_regs)); 87 } 88 89 // pack and unpack handlers for value types return 90 address adr_pack_handler() const { 91 assert(_adr_valueklass_fixed_block != NULL, "Should have been initialized"); 92 return ((address)_adr_valueklass_fixed_block) + in_bytes(byte_offset_of(ValueKlassFixedBlock, _pack_handler)); 93 } 94 95 address adr_unpack_handler() const { 96 assert(_adr_valueklass_fixed_block != NULL, "Should have been initialized"); 97 return ((address)_adr_valueklass_fixed_block) + in_bytes(byte_offset_of(ValueKlassFixedBlock, _unpack_handler)); 98 } 99 100 address pack_handler() const { 101 return *(address*)adr_pack_handler(); 102 } 103 104 address unpack_handler() const { 105 return *(address*)adr_unpack_handler(); 106 } 107 108 address adr_default_value_offset() const { 109 assert(_adr_valueklass_fixed_block != NULL, "Should have been initialized"); 110 return ((address)_adr_valueklass_fixed_block) + in_bytes(default_value_offset_offset()); 111 } 112 113 int collect_fields(GrowableArray<SigEntry>* sig, int base_off = 0) const; 114 115 void cleanup_blobs(); 116 117 protected: 118 // Returns the array class for the n'th dimension 119 Klass* array_klass_impl(bool or_null, int n, TRAPS); 120 121 // Returns the array class with this class as element type 122 Klass* array_klass_impl(bool or_null, TRAPS); 123 124 public: 125 // Type testing 126 bool is_value_slow() const { return true; } 127 128 // Casting from Klass* 129 static ValueKlass* cast(Klass* k) { 130 assert(k->is_value(), "cast to ValueKlass"); 131 return (ValueKlass*) k; 132 } 133 134 // Use this to return the size of an instance in heap words 135 // Implementation is currently simple because all value types are allocated 136 // in Java heap like Java objects. 137 virtual int size_helper() const { 138 return layout_helper_to_size_helper(layout_helper()); 139 } 140 141 // allocate_instance() allocates a stand alone value in the Java heap 142 instanceOop allocate_instance(TRAPS); 143 144 // minimum number of bytes occupied by nonstatic fields, HeapWord aligned or pow2 145 int raw_value_byte_size() const; 146 147 int first_field_offset() const; 148 149 address data_for_oop(oop o) const { 150 return ((address) (void*) o) + first_field_offset(); 151 } 152 153 oop oop_for_data(address data) const { 154 oop o = (oop) (data - first_field_offset()); 155 assert(oopDesc::is_oop(o, false), "Not an oop"); 156 return o; 157 } 158 159 // Query if h/w provides atomic load/store 160 bool is_atomic(); 161 162 bool flatten_array(); 163 164 bool contains_oops() const { return nonstatic_oop_map_count() > 0; } 165 int nonstatic_oop_count(); 166 167 // Prototype general store methods... 168 169 // copy the fields, with no concern for GC barriers 170 void raw_field_copy(void* src, void* dst, size_t raw_byte_size); 171 172 void value_store(void* src, void* dst, bool dst_is_heap, bool dst_uninitialized) { 173 value_store(src, dst, nonstatic_field_size() << LogBytesPerHeapOop, dst_is_heap, dst_uninitialized); 174 } 175 176 // store the value of this klass contained with src into dst, raw data ptr 177 void value_store(void* src, void* dst, size_t raw_byte_size, bool dst_is_heap, bool dst_uninitialized); 178 179 // GC support... 180 181 void iterate_over_inside_oops(OopClosure* f, oop value); 182 183 // oop iterate raw value type data pointer (where oop_addr may not be an oop, but backing/array-element) 184 template <typename T, class OopClosureType> 185 inline void oop_iterate_specialized(const address oop_addr, OopClosureType* closure); 186 187 template <typename T, class OopClosureType> 188 inline void oop_iterate_specialized_bounded(const address oop_addr, OopClosureType* closure, void* lo, void* hi); 189 190 // calling convention support 191 void initialize_calling_convention(TRAPS); 192 Array<SigEntry>* extended_sig() const { 193 return *((Array<SigEntry>**)adr_extended_sig()); 194 } 195 Array<VMRegPair>* return_regs() const { 196 return *((Array<VMRegPair>**)adr_return_regs()); 197 } 198 bool can_be_returned_as_fields() const; 199 void save_oop_fields(const RegisterMap& map, GrowableArray<Handle>& handles) const; 200 void restore_oop_results(RegisterMap& map, GrowableArray<Handle>& handles) const; 201 oop realloc_result(const RegisterMap& reg_map, const GrowableArray<Handle>& handles, TRAPS); 202 static ValueKlass* returned_value_klass(const RegisterMap& reg_map); 203 204 // pack and unpack handlers. Need to be loadable from generated code 205 // so at a fixed offset from the base of the klass pointer. 206 static ByteSize pack_handler_offset() { 207 return byte_offset_of(ValueKlassFixedBlock, _pack_handler); 208 } 209 210 static ByteSize unpack_handler_offset() { 211 return byte_offset_of(ValueKlassFixedBlock, _unpack_handler); 212 } 213 214 static ByteSize default_value_offset_offset() { 215 return byte_offset_of(ValueKlassFixedBlock, _default_value_offset); 216 } 217 218 void set_default_value_offset(int offset) { 219 *((int*)adr_default_value_offset()) = offset; 220 } 221 222 int default_value_offset() { 223 int offset = *((int*)adr_default_value_offset()); 224 assert(offset != 0, "must not be called if not initialized"); 225 return offset; 226 } 227 228 void set_default_value(oop val) { 229 java_mirror()->obj_field_put(default_value_offset(), val); 230 } 231 232 oop default_value() { 233 oop val = java_mirror()->obj_field_acquire(default_value_offset()); 234 assert(oopDesc::is_oop(val), "Sanity check"); 235 assert(val->is_value(), "Sanity check"); 236 assert(val->klass() == this, "sanity check"); 237 return val; 238 } 239 240 void deallocate_contents(ClassLoaderData* loader_data); 241 static void cleanup(ValueKlass* ik) ; 242 243 // Verification 244 void verify_on(outputStream* st); 245 void oop_verify_on(oop obj, outputStream* st); 246 247 }; 248 249 #endif /* SHARE_VM_OOPS_VALUEKLASS_HPP */