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  public:
  41   ValueKlass() { assert(DumpSharedSpaces || UseSharedSpaces, "only for CDS"); }
  42 
  43  private:
  44 
  45   // Constructor
  46   ValueKlass(const ClassFileParser& parser);
  47 
  48   ValueKlassFixedBlock* valueklass_static_block() const {
  49     address adr_jf = adr_value_fields_klasses();
  50     if (adr_jf != NULL) {
  51       return (ValueKlassFixedBlock*)(adr_jf + this->java_fields_count() * sizeof(Klass*));
  52     }
  53 
  54     address adr_fing = adr_fingerprint();
  55     if (adr_fing != NULL) {
  56       return (ValueKlassFixedBlock*)(adr_fingerprint() + sizeof(u8));
  57     }
  58 
  59     InstanceKlass** adr_host = adr_unsafe_anonymous_host();
  60     if (adr_host != NULL) {
  61       return (ValueKlassFixedBlock*)(adr_host + 1);
  62     }
  63 
  64     Klass* volatile* adr_impl = adr_implementor();
  65     if (adr_impl != NULL) {
  66       return (ValueKlassFixedBlock*)(adr_impl + 1);
  67     }
  68 
  69     return (ValueKlassFixedBlock*)end_of_nonstatic_oop_maps();
  70   }
  71 
  72   address adr_extended_sig() const {
  73     assert(_adr_valueklass_fixed_block != NULL, "Should have been initialized");
  74     return ((address)_adr_valueklass_fixed_block) + in_bytes(byte_offset_of(ValueKlassFixedBlock, _extended_sig));
  75   }
  76 
  77   address adr_return_regs() const {
  78     ValueKlassFixedBlock* vkst = valueklass_static_block();
  79     return ((address)_adr_valueklass_fixed_block) + in_bytes(byte_offset_of(ValueKlassFixedBlock, _return_regs));
  80   }
  81 
  82   // pack and unpack handlers for value types return
  83   address adr_pack_handler() const {
  84     assert(_adr_valueklass_fixed_block != NULL, "Should have been initialized");
  85     return ((address)_adr_valueklass_fixed_block) + in_bytes(byte_offset_of(ValueKlassFixedBlock, _pack_handler));
  86   }
  87 
  88   address adr_pack_handler_jobject() const {
  89     assert(_adr_valueklass_fixed_block != NULL, "Should have been initialized");
  90     return ((address)_adr_valueklass_fixed_block) + in_bytes(byte_offset_of(ValueKlassFixedBlock, _pack_handler_jobject));
  91   }
  92 
  93   address adr_unpack_handler() const {
  94     assert(_adr_valueklass_fixed_block != NULL, "Should have been initialized");
  95     return ((address)_adr_valueklass_fixed_block) + in_bytes(byte_offset_of(ValueKlassFixedBlock, _unpack_handler));
  96   }
  97 
  98   address adr_default_value_offset() const {
  99     assert(_adr_valueklass_fixed_block != NULL, "Should have been initialized");
 100     return ((address)_adr_valueklass_fixed_block) + in_bytes(default_value_offset_offset());
 101   }
 102 
 103   address adr_value_array_klass() const {
 104     assert(_adr_valueklass_fixed_block != NULL, "Should have been initialized");
 105     return ((address)_adr_valueklass_fixed_block) + in_bytes(byte_offset_of(ValueKlassFixedBlock, _value_array_klass));
 106   }
 107 
 108   Klass* get_value_array_klass() const {
 109     return *(Klass**)adr_value_array_klass();
 110   }
 111 
 112   Klass* acquire_value_array_klass() const {
 113     return Atomic::load_acquire((Klass**)adr_value_array_klass());
 114   }
 115 
 116   Klass* allocate_value_array_klass(TRAPS);
 117 
 118   address adr_alignment() const {
 119     assert(_adr_valueklass_fixed_block != NULL, "Should have been initialized");
 120     return ((address)_adr_valueklass_fixed_block) + in_bytes(byte_offset_of(ValueKlassFixedBlock, _alignment));
 121   }
 122 
 123   address adr_first_field_offset() const {
 124     assert(_adr_valueklass_fixed_block != NULL, "Should have been initialized");
 125     return ((address)_adr_valueklass_fixed_block) + in_bytes(byte_offset_of(ValueKlassFixedBlock, _first_field_offset));
 126   }
 127 
 128   address adr_exact_size_in_bytes() const {
 129     assert(_adr_valueklass_fixed_block != NULL, "Should have been initialized");
 130     return ((address)_adr_valueklass_fixed_block) + in_bytes(byte_offset_of(ValueKlassFixedBlock, _exact_size_in_bytes));
 131   }
 132 
 133  public:
 134   int get_alignment() const {
 135     return *(int*)adr_alignment();
 136   }
 137 
 138   void set_alignment(int alignment) {
 139     *(int*)adr_alignment() = alignment;
 140   }
 141 
 142   int first_field_offset() const {
 143     int offset = *(int*)adr_first_field_offset();
 144     assert(offset != 0, "Must be initialized before use");
 145     return *(int*)adr_first_field_offset();
 146   }
 147 
 148   void set_first_field_offset(int offset) {
 149     *(int*)adr_first_field_offset() = offset;
 150   }
 151 
 152   int get_exact_size_in_bytes() const {
 153     return *(int*)adr_exact_size_in_bytes();
 154   }
 155 
 156   void set_exact_size_in_bytes(int exact_size) {
 157     *(int*)adr_exact_size_in_bytes() = exact_size;
 158   }
 159 
 160   int first_field_offset_old();
 161 
 162   virtual void remove_unshareable_info();
 163   virtual void restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS);
 164   virtual void metaspace_pointers_do(MetaspaceClosure* it);
 165 
 166  private:
 167   int collect_fields(GrowableArray<SigEntry>* sig, int base_off = 0);
 168 
 169   void cleanup_blobs();
 170 
 171 
 172  protected:
 173   // Returns the array class for the n'th dimension
 174   Klass* array_klass_impl(ArrayStorageProperties storage_props, bool or_null, int n, TRAPS);
 175 
 176   // Returns the array class with this class as element type
 177   Klass* array_klass_impl(ArrayStorageProperties storage_props, bool or_null, TRAPS);
 178 
 179   // Specifically flat array klass
 180   Klass* value_array_klass(ArrayStorageProperties storage_props, bool or_null, int rank, TRAPS);
 181 
 182  public:
 183   // Type testing
 184   bool is_value_slow() const        { return true; }
 185 
 186   // value_mirror is the primary mirror
 187   oop value_mirror() const    { return java_lang_Class::inline_type_mirror(java_mirror()); }
 188   oop indirect_mirror() const { return java_lang_Class::indirect_type_mirror(java_mirror()); }
 189 
 190   // Casting from Klass*
 191   static ValueKlass* cast(Klass* k);
 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   // initialized to default value (cleared memory)
 205   instanceOop allocate_instance(TRAPS);
 206   // allocates a stand alone value buffer in the Java heap
 207   // DOES NOT have memory cleared, user MUST initialize payload before
 208   // returning to Java (i.e.: value_copy)
 209   instanceOop allocate_instance_buffer(TRAPS);
 210 
 211   // minimum number of bytes occupied by nonstatic fields, HeapWord aligned or pow2
 212   int raw_value_byte_size();
 213 
 214   address data_for_oop(oop o) const;
 215   oop oop_for_data(address data) const;
 216 
 217   // Query if this class promises atomicity one way or another
 218   bool is_atomic() { return is_naturally_atomic() || is_declared_atomic(); }
 219 
 220   bool flatten_array();
 221 
 222   bool contains_oops() const { return nonstatic_oop_map_count() > 0; }
 223   int nonstatic_oop_count();
 224 
 225   // General store methods
 226   //
 227   // Normally loads and store methods would be found in *Oops classes, but since values can be
 228   // "in-lined" (flattened) into containing oops, these methods reside here in ValueKlass.
 229   //
 230   // "value_copy_*_to_new_*" assume new memory (i.e. IS_DEST_UNINITIALIZED for write barriers)
 231 
 232   void value_copy_payload_to_new_oop(void* src, oop dst);
 233   void value_copy_oop_to_new_oop(oop src, oop dst);
 234   void value_copy_oop_to_new_payload(oop src, void* dst);
 235   void value_copy_oop_to_payload(oop src, void* dst);
 236 
 237   oop read_flattened_field(oop obj, int offset, TRAPS);
 238   void write_flattened_field(oop obj, int offset, oop value, TRAPS);
 239 
 240   // oop iterate raw value type data pointer (where oop_addr may not be an oop, but backing/array-element)
 241   template <typename T, class OopClosureType>
 242   inline void oop_iterate_specialized(const address oop_addr, OopClosureType* closure);
 243 
 244   template <typename T, class OopClosureType>
 245   inline void oop_iterate_specialized_bounded(const address oop_addr, OopClosureType* closure, void* lo, void* hi);
 246 
 247   // calling convention support
 248   void initialize_calling_convention(TRAPS);
 249   Array<SigEntry>* extended_sig() const {
 250     return *((Array<SigEntry>**)adr_extended_sig());
 251   }
 252   Array<VMRegPair>* return_regs() const {
 253     return *((Array<VMRegPair>**)adr_return_regs());
 254   }
 255   bool is_scalarizable() const;
 256   bool can_be_returned_as_fields() const;
 257   void save_oop_fields(const RegisterMap& map, GrowableArray<Handle>& handles) const;
 258   void restore_oop_results(RegisterMap& map, GrowableArray<Handle>& handles) const;
 259   oop realloc_result(const RegisterMap& reg_map, const GrowableArray<Handle>& handles, TRAPS);
 260   static ValueKlass* returned_value_klass(const RegisterMap& reg_map);
 261 
 262   address pack_handler() const {
 263     return *(address*)adr_pack_handler();
 264   }
 265 
 266   address unpack_handler() const {
 267     return *(address*)adr_unpack_handler();
 268   }
 269 
 270   // pack and unpack handlers. Need to be loadable from generated code
 271   // so at a fixed offset from the base of the klass pointer.
 272   static ByteSize pack_handler_offset() {
 273     return byte_offset_of(ValueKlassFixedBlock, _pack_handler);
 274   }
 275 
 276   static ByteSize pack_handler_jobject_offset() {
 277     return byte_offset_of(ValueKlassFixedBlock, _pack_handler_jobject);
 278   }
 279 
 280   static ByteSize unpack_handler_offset() {
 281     return byte_offset_of(ValueKlassFixedBlock, _unpack_handler);
 282   }
 283 
 284   static ByteSize default_value_offset_offset() {
 285     return byte_offset_of(ValueKlassFixedBlock, _default_value_offset);
 286   }
 287 
 288   static ByteSize first_field_offset_offset() {
 289     return byte_offset_of(ValueKlassFixedBlock, _first_field_offset);
 290   }
 291 
 292   void set_default_value_offset(int offset) {
 293     *((int*)adr_default_value_offset()) = offset;
 294   }
 295 
 296   int default_value_offset() {
 297     int offset = *((int*)adr_default_value_offset());
 298     assert(offset != 0, "must not be called if not initialized");
 299     return offset;
 300   }
 301 
 302   void set_default_value(oop val) {
 303     java_mirror()->obj_field_put(default_value_offset(), val);
 304     indirect_mirror()->obj_field_put(default_value_offset(), val);
 305   }
 306 
 307   oop default_value();
 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 */