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