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) {
  44     set_has_vcc_klass();
  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   }
  53 
  54   address adr_extended_sig() const {
  55     address adr_vcc = adr_vcc_klass();
  56     if (adr_vcc == NULL) {
  57       address adr_jf = adr_value_fields_klasses();
  58       if (adr_jf != NULL) {
  59         return adr_jf + this->java_fields_count() * sizeof(Klass*);
  60       }
  61 
  62       address adr_fing = adr_fingerprint();
  63       if (adr_fing != NULL) {
  64         return adr_fingerprint() + sizeof(u8);
  65       }
  66 
  67       InstanceKlass** adr_host = adr_host_klass();
  68       if (adr_host != NULL) {
  69         return (address)(adr_host + 1);
  70       }
  71 
  72       Klass** adr_impl = adr_implementor();
  73       if (adr_impl != NULL) {
  74         return (address)(adr_impl + 1);
  75       }
  76 
  77       return (address)end_of_nonstatic_oop_maps();
  78     } else {
  79       return adr_vcc + sizeof(Klass*);
  80     }
  81   }
  82 
  83   address adr_return_regs() const {
  84     return adr_extended_sig() + sizeof(intptr_t);
  85   }
  86 
  87   // pack and unpack handlers for value types return
  88   address adr_pack_handler() const {
  89     return (address)this + in_bytes(pack_handler_offset());
  90   }
  91 
  92   address adr_unpack_handler() const {
  93     return (address)this + in_bytes(unpack_handler_offset());
  94   }
  95 
  96   address pack_handler() const {
  97     return *(address*)adr_pack_handler();
  98   }
  99 
 100   address unpack_handler() const {
 101     return *(address*)adr_unpack_handler();
 102   }
 103 
 104   address adr_default_value_offset() const {
 105     return (address)this + in_bytes(default_value_offset_offset());
 106   }
 107 
 108   // static Klass* array_klass_impl(InstanceKlass* this_k, bool or_null, int n, TRAPS);
 109 
 110   GrowableArray<SigEntry> collect_fields(int base_off = 0) const;
 111 
 112   void cleanup_blobs();
 113 
 114  protected:
 115   // Returns the array class for the n'th dimension
 116   Klass* array_klass_impl(bool or_null, int n, TRAPS);
 117 
 118   // Returns the array class with this class as element type
 119   Klass* array_klass_impl(bool or_null, TRAPS);
 120 
 121  public:
 122   // Type testing
 123   bool is_value_slow() const        { return true; }
 124   bool is__Value() const { return (this == SystemDictionary::___Value_klass()); }
 125 
 126   // Casting from Klass*
 127   static ValueKlass* cast(Klass* k) {
 128     assert(k->is_value(), "cast to ValueKlass");
 129     return (ValueKlass*) k;
 130   }
 131 
 132   // Use this to return the size of an instance in heap words
 133   // Implementation is currently simple because all value types are allocated
 134   // in Java heap like Java objects.
 135   virtual int size_helper() const {
 136     return layout_helper_to_size_helper(layout_helper());
 137   }
 138 
 139   // allocate_instance() allocates a stand alone value in the Java heap
 140   instanceOop allocate_instance(TRAPS);
 141   // allocate_buffered_or_heap_instance() tries to allocate a value in the
 142   // thread local value buffer, if allocation fails, it allocates it in the
 143   // Java heap
 144   instanceOop allocate_buffered_or_heap_instance(bool* in_heap, TRAPS);
 145 
 146   // minimum number of bytes occupied by nonstatic fields, HeapWord aligned or pow2
 147   int raw_value_byte_size() const;
 148 
 149   int first_field_offset() const;
 150 
 151   address data_for_oop(oop o) const {
 152     return ((address) (void*) o) + first_field_offset();
 153   }
 154 
 155   oop oop_for_data(address data) const {
 156     oop o = (oop) (data - first_field_offset());
 157     assert(oopDesc::is_oop(o, false), "Not an oop");
 158     return o;
 159   }
 160 
 161   void set_if_bufferable() {
 162     bool bufferable;
 163 
 164     int size_in_heap_words = size_helper();
 165     int base_offset = instanceOopDesc::base_offset_in_bytes();
 166     size_t size_in_bytes = size_in_heap_words * HeapWordSize - base_offset;
 167     bufferable = size_in_bytes <= BigValueTypeThreshold;
 168     if (size_in_bytes > VTBufferChunk::max_alloc_size()) bufferable = false;
 169     if (ValueTypesBufferMaxMemory == 0) bufferable = false;
 170     if (bufferable) {
 171       _extra_flags |= _extra_is_bufferable;
 172     } else {
 173       _extra_flags &= ~_extra_is_bufferable;
 174     }
 175   }
 176 
 177   bool is_bufferable() const          {
 178     return (_extra_flags & _extra_is_bufferable) != 0;
 179   }
 180 
 181   // Query if h/w provides atomic load/store
 182   bool is_atomic();
 183 
 184   bool flatten_array();
 185 
 186   bool contains_oops() const { return nonstatic_oop_map_count() > 0; }
 187   int nonstatic_oop_count();
 188 
 189   // Prototype general store methods...
 190 
 191   // copy the fields, with no concern for GC barriers
 192   void raw_field_copy(void* src, void* dst, size_t raw_byte_size);
 193 
 194   void value_store(void* src, void* dst, bool dst_is_heap, bool dst_uninitialized) {
 195     value_store(src, dst, nonstatic_field_size() << LogBytesPerHeapOop, dst_is_heap, dst_uninitialized);
 196   }
 197 
 198   // store the value of this klass contained with src into dst, raw data ptr
 199   void value_store(void* src, void* dst, size_t raw_byte_size, bool dst_is_heap, bool dst_uninitialized);
 200 
 201   oop unbox(Handle src, InstanceKlass* target_klass, TRAPS);
 202   oop box(Handle src, InstanceKlass* target_klass, TRAPS);
 203 
 204   // GC support...
 205 
 206   void iterate_over_inside_oops(OopClosure* f, oop value);
 207 
 208   // oop iterate raw value type data pointer (where oop_addr may not be an oop, but backing/array-element)
 209   template <bool nv, typename T, class OopClosureType>
 210   inline void oop_iterate_specialized(const address oop_addr, OopClosureType* closure);
 211 
 212   template <bool nv, typename T, class OopClosureType>
 213   inline void oop_iterate_specialized_bounded(const address oop_addr, OopClosureType* closure, void* lo, void* hi);
 214 
 215   // calling convention support
 216   void initialize_calling_convention();
 217   Array<SigEntry>* extended_sig() const {
 218     assert(!is__Value(), "make no sense for __Value");
 219     return *((Array<SigEntry>**)adr_extended_sig());
 220   }
 221   Array<VMRegPair>* return_regs() const {
 222     assert(!is__Value(), "make no sense for __Value");
 223     return *((Array<VMRegPair>**)adr_return_regs());
 224   }
 225   bool can_be_returned_as_fields() const;
 226   void save_oop_fields(const RegisterMap& map, GrowableArray<Handle>& handles) const;
 227   void restore_oop_results(RegisterMap& map, GrowableArray<Handle>& handles) const;
 228   oop realloc_result(const RegisterMap& reg_map, const GrowableArray<Handle>& handles, bool buffered, TRAPS);
 229   static ValueKlass* returned_value_klass(const RegisterMap& reg_map);
 230 
 231   // pack and unpack handlers. Need to be loadable from generated code
 232   // so at a fixed offset from the base of the klass pointer.
 233   static ByteSize pack_handler_offset() {
 234     return in_ByteSize(InstanceKlass::header_size() * wordSize);
 235   }
 236 
 237   static ByteSize unpack_handler_offset() {
 238     return in_ByteSize((InstanceKlass::header_size()+1) * wordSize);
 239   }
 240 
 241   static ByteSize default_value_offset_offset() {
 242     return in_ByteSize((InstanceKlass::header_size()+2) * wordSize);
 243   }
 244 
 245   void set_default_value_offset(int offset) {
 246     *((int*)adr_default_value_offset()) = offset;
 247   }
 248 
 249   int default_value_offset() {
 250     int offset = *((int*)adr_default_value_offset());
 251     assert(offset != 0, "must not be called if not initialized");
 252     return offset;
 253   }
 254 
 255   void set_default_value(oop val) {
 256     java_mirror()->obj_field_put(default_value_offset(), val);
 257   }
 258 
 259   oop default_value() {
 260     oop val = java_mirror()->obj_field_acquire(default_value_offset());
 261     assert(oopDesc::is_oop(val), "Sanity check");
 262     assert(val->is_value(), "Sanity check");
 263     assert(val->klass() == this, "sanity check");
 264     return val;
 265   }
 266 
 267   void deallocate_contents(ClassLoaderData* loader_data);
 268   static void cleanup(ValueKlass* ik) ;
 269 };
 270 
 271 #endif /* SHARE_VM_OOPS_VALUEKLASS_HPP */