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