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