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