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 #include "precompiled.hpp"
  26 #include "gc/shared/barrierSet.hpp"
  27 #include "gc/shared/collectedHeap.inline.hpp"
  28 #include "gc/shared/gcLocker.inline.hpp"
  29 #include "interpreter/interpreter.hpp"
  30 #include "logging/log.hpp"
  31 #include "memory/metadataFactory.hpp"
  32 #include "oops/access.hpp"
  33 #include "oops/compressedOops.inline.hpp"
  34 #include "oops/fieldStreams.hpp"
  35 #include "oops/instanceKlass.inline.hpp"
  36 #include "oops/method.hpp"
  37 #include "oops/oop.inline.hpp"
  38 #include "oops/objArrayKlass.hpp"
  39 #include "oops/valueKlass.hpp"
  40 #include "oops/valueArrayKlass.hpp"
  41 #include "runtime/fieldDescriptor.inline.hpp"
  42 #include "runtime/handles.inline.hpp"
  43 #include "runtime/safepointVerifiers.hpp"
  44 #include "runtime/sharedRuntime.hpp"
  45 #include "runtime/signature.hpp"
  46 #include "runtime/thread.inline.hpp"
  47 #include "utilities/copy.hpp"
  48 
  49 int ValueKlass::first_field_offset_old() {
  50 #ifdef ASSERT
  51   int first_offset = INT_MAX;
  52   for (AllFieldStream fs(this); !fs.done(); fs.next()) {
  53     if (fs.offset() < first_offset) first_offset= fs.offset();
  54   }
  55 #endif
  56   int base_offset = instanceOopDesc::base_offset_in_bytes();
  57   // The first field of value types is aligned on a long boundary
  58   base_offset = align_up(base_offset, BytesPerLong);
  59   assert(base_offset == first_offset, "inconsistent offsets");
  60   return base_offset;
  61 }
  62 
  63 int ValueKlass::raw_value_byte_size() {
  64   int heapOopAlignedSize = nonstatic_field_size() << LogBytesPerHeapOop;
  65   // If bigger than 64 bits or needs oop alignment, then use jlong aligned
  66   // which for values should be jlong aligned, asserts in raw_field_copy otherwise
  67   if (heapOopAlignedSize >= longSize || contains_oops()) {
  68     return heapOopAlignedSize;
  69   }
  70   // Small primitives...
  71   // If a few small basic type fields, return the actual size, i.e.
  72   // 1 byte = 1
  73   // 2 byte = 2
  74   // 3 byte = 4, because pow2 needed for element stores
  75   int first_offset = first_field_offset();
  76   int last_offset  = 0; // find the last offset, add basic type size
  77   int last_tsz     = 0;
  78   for (AllFieldStream fs(this); !fs.done(); fs.next()) {
  79     if (fs.access_flags().is_static()) {
  80       continue;
  81     } else if (fs.offset() > last_offset) {
  82       BasicType type = char2type(fs.signature()->char_at(0));
  83       if (is_java_primitive(type)) {
  84         last_tsz = type2aelembytes(type);
  85       } else if (type == T_VALUETYPE) {
  86         // Not just primitives. Layout aligns embedded value, so use jlong aligned it is
  87         return heapOopAlignedSize;
  88       } else {
  89         guarantee(0, "Unknown type %d", type);
  90       }
  91       assert(last_tsz != 0, "Invariant");
  92       last_offset = fs.offset();
  93     }
  94   }
  95   // Assumes VT with no fields are meaningless and illegal
  96   last_offset += last_tsz;
  97   assert(last_offset > first_offset && last_tsz, "Invariant");
  98   return 1 << upper_log2(last_offset - first_offset);
  99 }
 100 
 101 instanceOop ValueKlass::allocate_instance(TRAPS) {
 102   int size = size_helper();  // Query before forming handle.
 103 
 104   instanceOop oop = (instanceOop)Universe::heap()->obj_allocate(this, size, CHECK_NULL);
 105   assert(oop->mark().is_always_locked(), "Unlocked value type");
 106   return oop;
 107 }
 108 
 109 bool ValueKlass::is_atomic() {
 110   return (nonstatic_field_size() * heapOopSize) <= longSize;
 111 }
 112 
 113 int ValueKlass::nonstatic_oop_count() {
 114   int oops = 0;
 115   int map_count = nonstatic_oop_map_count();
 116   OopMapBlock* block = start_of_nonstatic_oop_maps();
 117   OopMapBlock* end = block + map_count;
 118   while (block != end) {
 119     oops += block->count();
 120     block++;
 121   }
 122   return oops;
 123 }
 124 
 125 // Arrays of...
 126 
 127 bool ValueKlass::flatten_array() {
 128   if (!ValueArrayFlatten) {
 129     return false;
 130   }
 131 
 132   int elem_bytes = raw_value_byte_size();
 133   // Too big
 134   if ((ValueArrayElemMaxFlatSize >= 0) && (elem_bytes > ValueArrayElemMaxFlatSize)) {
 135     return false;
 136   }
 137   // Too many embedded oops
 138   if ((ValueArrayElemMaxFlatOops >= 0) && (nonstatic_oop_count() > ValueArrayElemMaxFlatOops)) {
 139     return false;
 140   }
 141 
 142   return true;
 143 }
 144 
 145 
 146 Klass* ValueKlass::array_klass_impl(ArrayStorageProperties storage_props, bool or_null, int n, TRAPS) {
 147   if (storage_props.is_null_free()) {
 148     return value_array_klass(storage_props, or_null, n, THREAD);
 149   } else {
 150     return InstanceKlass::array_klass_impl(storage_props, or_null, n, THREAD);
 151   }
 152 }
 153 
 154 Klass* ValueKlass::array_klass_impl(ArrayStorageProperties storage_props, bool or_null, TRAPS) {
 155   return array_klass_impl(storage_props, or_null, 1, THREAD);
 156 }
 157 
 158 Klass* ValueKlass::value_array_klass(ArrayStorageProperties storage_props, bool or_null, int rank, TRAPS) {
 159   Klass* vak = acquire_value_array_klass();
 160   if (vak == NULL) {
 161     if (or_null) return NULL;
 162     ResourceMark rm;
 163     {
 164       // Atomic creation of array_klasses
 165       MutexLocker ma(MultiArray_lock, THREAD);
 166       if (get_value_array_klass() == NULL) {
 167         vak = allocate_value_array_klass(CHECK_NULL);
 168         OrderAccess::release_store((Klass**)adr_value_array_klass(), vak);
 169       }
 170     }
 171   }
 172   if (!vak->is_valueArray_klass()) {
 173     storage_props.clear_flattened();
 174   }
 175   if (or_null) {
 176     return vak->array_klass_or_null(storage_props, rank);
 177   }
 178   return vak->array_klass(storage_props, rank, THREAD);
 179 }
 180 
 181 Klass* ValueKlass::allocate_value_array_klass(TRAPS) {
 182   if (flatten_array() && (is_atomic() || (!ValueArrayAtomicAccess))) {
 183     return ValueArrayKlass::allocate_klass(ArrayStorageProperties::flattened_and_null_free, this, THREAD);
 184   }
 185   return ObjArrayKlass::allocate_objArray_klass(ArrayStorageProperties::null_free, 1, this, THREAD);
 186 }
 187 
 188 void ValueKlass::array_klasses_do(void f(Klass* k)) {
 189   InstanceKlass::array_klasses_do(f);
 190   if (get_value_array_klass() != NULL)
 191     ArrayKlass::cast(get_value_array_klass())->array_klasses_do(f);
 192 }
 193 
 194 void ValueKlass::raw_field_copy(void* src, void* dst, size_t raw_byte_size) {
 195   if (!UseNewLayout) {
 196     /*
 197      * Try not to shear fields even if not an atomic store...
 198      *
 199      * First 3 cases handle value array store, otherwise works on the same basis
 200      * as JVM_Clone, at this size data is aligned. The order of primitive types
 201      * is largest to smallest, and it not possible for fields to stradle long
 202      * copy boundaries.
 203      *
 204      * If MT without exclusive access, possible to observe partial value store,
 205      * but not partial primitive and reference field values
 206      */
 207     switch (raw_byte_size) {
 208     case 1:
 209       *((jbyte*) dst) = *(jbyte*)src;
 210       break;
 211     case 2:
 212       *((jshort*) dst) = *(jshort*)src;
 213       break;
 214     case 4:
 215       *((jint*) dst) = *(jint*) src;
 216       break;
 217     default:
 218       assert(raw_byte_size % sizeof(jlong) == 0, "Unaligned raw_byte_size");
 219       Copy::conjoint_jlongs_atomic((jlong*)src, (jlong*)dst, raw_byte_size >> LogBytesPerLong);
 220     }
 221   } else {
 222     int size = this->get_exact_size_in_bytes();
 223     int length;
 224     switch (this->get_alignment()) {
 225     case BytesPerLong:
 226       length = size >> LogBytesPerLong;
 227       if (length > 0) {
 228         Copy::conjoint_jlongs_atomic((jlong*)src, (jlong*)dst, length);
 229         size -= length << LogBytesPerLong;
 230         src = (jlong*)src + length;
 231         dst = (jlong*)dst + length;
 232       }
 233       // Fallthrough
 234     case BytesPerInt:
 235       length = size >> LogBytesPerInt;
 236       if (length > 0) {
 237         Copy::conjoint_jints_atomic((jint*)src, (jint*)dst, length);
 238         size -= length << LogBytesPerInt;
 239         src = (jint*)src + length;
 240         dst = (jint*)dst + length;
 241       }
 242       // Fallthrough
 243     case BytesPerShort:
 244       length = size >> LogBytesPerShort;
 245       if (length > 0) {
 246         Copy::conjoint_jshorts_atomic((jshort*)src, (jshort*)dst, length);
 247         size -= length << LogBytesPerShort;
 248         src = (jshort*)src + length;
 249         dst = (jshort*)dst +length;
 250       }
 251       // Fallthrough
 252     case 1:
 253       if (size > 0) Copy::conjoint_jbytes_atomic((jbyte*)src, (jbyte*)dst, size);
 254       break;
 255     default:
 256       fatal("Unsupported alignment");
 257     }
 258   }
 259 }
 260 
 261 /*
 262  * Store the value of this klass contained with src into dst.
 263  *
 264  * This operation is appropriate for use from vastore, vaload and putfield (for values)
 265  *
 266  * GC barriers currently can lock with no safepoint check and allocate c-heap,
 267  * so raw point is "safe" for now.
 268  *
 269  * Going forward, look to use machine generated (stub gen or bc) version for most used klass layouts
 270  *
 271  */
 272 void ValueKlass::value_store(void* src, void* dst, size_t raw_byte_size, bool dst_heap, bool dst_uninitialized) {
 273   if (contains_oops()) {
 274     if (dst_heap) {
 275       // src/dst aren't oops, need offset to adjust oop map offset
 276       const address dst_oop_addr = ((address) dst) - first_field_offset();
 277 
 278       ModRefBarrierSet* bs = barrier_set_cast<ModRefBarrierSet>(BarrierSet::barrier_set());
 279 
 280       // Pre-barriers...
 281       OopMapBlock* map = start_of_nonstatic_oop_maps();
 282       OopMapBlock* const end = map + nonstatic_oop_map_count();
 283       while (map != end) {
 284         // Shame we can't just use the existing oop iterator...src/dst aren't oop
 285         address doop_address = dst_oop_addr + map->offset();
 286         // TEMP HACK: barrier code need to migrate to => access API (need own versions of value type ops)
 287         if (UseCompressedOops) {
 288           bs->write_ref_array_pre((narrowOop*) doop_address, map->count(), dst_uninitialized);
 289         } else {
 290           bs->write_ref_array_pre((oop*) doop_address, map->count(), dst_uninitialized);
 291         }
 292         map++;
 293       }
 294 
 295       raw_field_copy(src, dst, raw_byte_size);
 296 
 297       // Post-barriers...
 298       map = start_of_nonstatic_oop_maps();
 299       while (map != end) {
 300         address doop_address = dst_oop_addr + map->offset();
 301         bs->write_ref_array((HeapWord*) doop_address, map->count());
 302         map++;
 303       }
 304     } else { // Buffered value case
 305       raw_field_copy(src, dst, raw_byte_size);
 306     }
 307   } else {   // Primitive-only case...
 308     raw_field_copy(src, dst, raw_byte_size);
 309   }
 310 }
 311 
 312 // Value type arguments are not passed by reference, instead each
 313 // field of the value type is passed as an argument. This helper
 314 // function collects the fields of the value types (including embedded
 315 // value type's fields) in a list. Included with the field's type is
 316 // the offset of each field in the value type: i2c and c2i adapters
 317 // need that to load or store fields. Finally, the list of fields is
 318 // sorted in order of increasing offsets: the adapters and the
 319 // compiled code need to agree upon the order of fields.
 320 //
 321 // The list of basic types that is returned starts with a T_VALUETYPE
 322 // and ends with an extra T_VOID. T_VALUETYPE/T_VOID pairs are used as
 323 // delimiters. Every entry between the two is a field of the value
 324 // type. If there's an embedded value type in the list, it also starts
 325 // with a T_VALUETYPE and ends with a T_VOID. This is so we can
 326 // generate a unique fingerprint for the method's adapters and we can
 327 // generate the list of basic types from the interpreter point of view
 328 // (value types passed as reference: iterate on the list until a
 329 // T_VALUETYPE, drop everything until and including the closing
 330 // T_VOID) or the compiler point of view (each field of the value
 331 // types is an argument: drop all T_VALUETYPE/T_VOID from the list).
 332 int ValueKlass::collect_fields(GrowableArray<SigEntry>* sig, int base_off) {
 333   int count = 0;
 334   SigEntry::add_entry(sig, T_VALUETYPE, base_off);
 335   for (AllFieldStream fs(this); !fs.done(); fs.next()) {
 336     if (fs.access_flags().is_static()) continue;
 337     int offset = base_off + fs.offset() - (base_off > 0 ? first_field_offset() : 0);
 338     if (fs.is_flattened()) {
 339       // Resolve klass of flattened value type field and recursively collect fields
 340       Klass* vk = get_value_field_klass(fs.index());
 341       count += ValueKlass::cast(vk)->collect_fields(sig, offset);
 342     } else {
 343       BasicType bt = FieldType::basic_type(fs.signature());
 344       if (bt == T_VALUETYPE) {
 345         bt = T_OBJECT;
 346       }
 347       SigEntry::add_entry(sig, bt, offset);
 348       count += type2size[bt];
 349     }
 350   }
 351   int offset = base_off + size_helper()*HeapWordSize - (base_off > 0 ? first_field_offset() : 0);
 352   SigEntry::add_entry(sig, T_VOID, offset);
 353   if (base_off == 0) {
 354     sig->sort(SigEntry::compare);
 355   }
 356   assert(sig->at(0)._bt == T_VALUETYPE && sig->at(sig->length()-1)._bt == T_VOID, "broken structure");
 357   return count;
 358 }
 359 
 360 void ValueKlass::initialize_calling_convention(TRAPS) {
 361   // Because the pack and unpack handler addresses need to be loadable from generated code,
 362   // they are stored at a fixed offset in the klass metadata. Since value type klasses do
 363   // not have a vtable, the vtable offset is used to store these addresses.
 364   if (is_scalarizable() && (ValueTypeReturnedAsFields || ValueTypePassFieldsAsArgs)) {
 365     ResourceMark rm;
 366     GrowableArray<SigEntry> sig_vk;
 367     int nb_fields = collect_fields(&sig_vk);
 368     Array<SigEntry>* extended_sig = MetadataFactory::new_array<SigEntry>(class_loader_data(), sig_vk.length(), CHECK);
 369     *((Array<SigEntry>**)adr_extended_sig()) = extended_sig;
 370     for (int i = 0; i < sig_vk.length(); i++) {
 371       extended_sig->at_put(i, sig_vk.at(i));
 372     }
 373 
 374     if (ValueTypeReturnedAsFields) {
 375       nb_fields++;
 376       BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, nb_fields);
 377       sig_bt[0] = T_METADATA;
 378       SigEntry::fill_sig_bt(&sig_vk, sig_bt+1);
 379       VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair, nb_fields);
 380       int total = SharedRuntime::java_return_convention(sig_bt, regs, nb_fields);
 381 
 382       if (total > 0) {
 383         Array<VMRegPair>* return_regs = MetadataFactory::new_array<VMRegPair>(class_loader_data(), nb_fields, CHECK);
 384         *((Array<VMRegPair>**)adr_return_regs()) = return_regs;
 385         for (int i = 0; i < nb_fields; i++) {
 386           return_regs->at_put(i, regs[i]);
 387         }
 388 
 389         BufferedValueTypeBlob* buffered_blob = SharedRuntime::generate_buffered_value_type_adapter(this);
 390         *((address*)adr_pack_handler()) = buffered_blob->pack_fields();
 391         *((address*)adr_unpack_handler()) = buffered_blob->unpack_fields();
 392         assert(CodeCache::find_blob(pack_handler()) == buffered_blob, "lost track of blob");
 393       }
 394     }
 395   }
 396 }
 397 
 398 void ValueKlass::deallocate_contents(ClassLoaderData* loader_data) {
 399   if (extended_sig() != NULL) {
 400     MetadataFactory::free_array<SigEntry>(loader_data, extended_sig());
 401   }
 402   if (return_regs() != NULL) {
 403     MetadataFactory::free_array<VMRegPair>(loader_data, return_regs());
 404   }
 405   cleanup_blobs();
 406   InstanceKlass::deallocate_contents(loader_data);
 407 }
 408 
 409 void ValueKlass::cleanup(ValueKlass* ik) {
 410   ik->cleanup_blobs();
 411 }
 412 
 413 void ValueKlass::cleanup_blobs() {
 414   if (pack_handler() != NULL) {
 415     CodeBlob* buffered_blob = CodeCache::find_blob(pack_handler());
 416     assert(buffered_blob->is_buffered_value_type_blob(), "bad blob type");
 417     BufferBlob::free((BufferBlob*)buffered_blob);
 418     *((address*)adr_pack_handler()) = NULL;
 419     *((address*)adr_unpack_handler()) = NULL;
 420   }
 421 }
 422 
 423 // Can this value type be scalarized?
 424 bool ValueKlass::is_scalarizable() const {
 425   return ScalarizeValueTypes;
 426 }
 427 
 428 // Can this value type be returned as multiple values?
 429 bool ValueKlass::can_be_returned_as_fields() const {
 430   return return_regs() != NULL;
 431 }
 432 
 433 // Create handles for all oop fields returned in registers that are going to be live across a safepoint
 434 void ValueKlass::save_oop_fields(const RegisterMap& reg_map, GrowableArray<Handle>& handles) const {
 435   Thread* thread = Thread::current();
 436   const Array<SigEntry>* sig_vk = extended_sig();
 437   const Array<VMRegPair>* regs = return_regs();
 438   int j = 1;
 439 
 440   for (int i = 0; i < sig_vk->length(); i++) {
 441     BasicType bt = sig_vk->at(i)._bt;
 442     if (bt == T_OBJECT || bt == T_ARRAY) {
 443       VMRegPair pair = regs->at(j);
 444       address loc = reg_map.location(pair.first());
 445       oop v = *(oop*)loc;
 446       assert(v == NULL || oopDesc::is_oop(v), "not an oop?");
 447       assert(Universe::heap()->is_in_or_null(v), "must be heap pointer");
 448       handles.push(Handle(thread, v));
 449     }
 450     if (bt == T_VALUETYPE) {
 451       continue;
 452     }
 453     if (bt == T_VOID &&
 454         sig_vk->at(i-1)._bt != T_LONG &&
 455         sig_vk->at(i-1)._bt != T_DOUBLE) {
 456       continue;
 457     }
 458     j++;
 459   }
 460   assert(j == regs->length(), "missed a field?");
 461 }
 462 
 463 // Update oop fields in registers from handles after a safepoint
 464 void ValueKlass::restore_oop_results(RegisterMap& reg_map, GrowableArray<Handle>& handles) const {
 465   assert(ValueTypeReturnedAsFields, "inconsistent");
 466   const Array<SigEntry>* sig_vk = extended_sig();
 467   const Array<VMRegPair>* regs = return_regs();
 468   assert(regs != NULL, "inconsistent");
 469 
 470   int j = 1;
 471   for (int i = 0, k = 0; i < sig_vk->length(); i++) {
 472     BasicType bt = sig_vk->at(i)._bt;
 473     if (bt == T_OBJECT || bt == T_ARRAY) {
 474       VMRegPair pair = regs->at(j);
 475       address loc = reg_map.location(pair.first());
 476       *(oop*)loc = handles.at(k++)();
 477     }
 478     if (bt == T_VALUETYPE) {
 479       continue;
 480     }
 481     if (bt == T_VOID &&
 482         sig_vk->at(i-1)._bt != T_LONG &&
 483         sig_vk->at(i-1)._bt != T_DOUBLE) {
 484       continue;
 485     }
 486     j++;
 487   }
 488   assert(j == regs->length(), "missed a field?");
 489 }
 490 
 491 // Fields are in registers. Create an instance of the value type and
 492 // initialize it with the values of the fields.
 493 oop ValueKlass::realloc_result(const RegisterMap& reg_map, const GrowableArray<Handle>& handles, TRAPS) {
 494   oop new_vt = allocate_instance(CHECK_NULL);
 495   const Array<SigEntry>* sig_vk = extended_sig();
 496   const Array<VMRegPair>* regs = return_regs();
 497 
 498   int j = 1;
 499   int k = 0;
 500   for (int i = 0; i < sig_vk->length(); i++) {
 501     BasicType bt = sig_vk->at(i)._bt;
 502     if (bt == T_VALUETYPE) {
 503       continue;
 504     }
 505     if (bt == T_VOID) {
 506       if (sig_vk->at(i-1)._bt == T_LONG ||
 507           sig_vk->at(i-1)._bt == T_DOUBLE) {
 508         j++;
 509       }
 510       continue;
 511     }
 512     int off = sig_vk->at(i)._offset;
 513     assert(off > 0, "offset in object should be positive");
 514     VMRegPair pair = regs->at(j);
 515     address loc = reg_map.location(pair.first());
 516     switch(bt) {
 517     case T_BOOLEAN: {
 518       new_vt->bool_field_put(off, *(jboolean*)loc);
 519       break;
 520     }
 521     case T_CHAR: {
 522       new_vt->char_field_put(off, *(jchar*)loc);
 523       break;
 524     }
 525     case T_BYTE: {
 526       new_vt->byte_field_put(off, *(jbyte*)loc);
 527       break;
 528     }
 529     case T_SHORT: {
 530       new_vt->short_field_put(off, *(jshort*)loc);
 531       break;
 532     }
 533     case T_INT: {
 534       new_vt->int_field_put(off, *(jint*)loc);
 535       break;
 536     }
 537     case T_LONG: {
 538 #ifdef _LP64
 539       new_vt->double_field_put(off,  *(jdouble*)loc);
 540 #else
 541       Unimplemented();
 542 #endif
 543       break;
 544     }
 545     case T_OBJECT:
 546     case T_ARRAY: {
 547       Handle handle = handles.at(k++);
 548       new_vt->obj_field_put(off, handle());
 549       break;
 550     }
 551     case T_FLOAT: {
 552       new_vt->float_field_put(off,  *(jfloat*)loc);
 553       break;
 554     }
 555     case T_DOUBLE: {
 556       new_vt->double_field_put(off, *(jdouble*)loc);
 557       break;
 558     }
 559     default:
 560       ShouldNotReachHere();
 561     }
 562     *(intptr_t*)loc = 0xDEAD;
 563     j++;
 564   }
 565   assert(j == regs->length(), "missed a field?");
 566   assert(k == handles.length(), "missed an oop?");
 567   return new_vt;
 568 }
 569 
 570 // Check the return register for a ValueKlass oop
 571 ValueKlass* ValueKlass::returned_value_klass(const RegisterMap& map) {
 572   BasicType bt = T_METADATA;
 573   VMRegPair pair;
 574   int nb = SharedRuntime::java_return_convention(&bt, &pair, 1);
 575   assert(nb == 1, "broken");
 576 
 577   address loc = map.location(pair.first());
 578   intptr_t ptr = *(intptr_t*)loc;
 579   if (is_set_nth_bit(ptr, 0)) {
 580     // Oop is tagged, must be a ValueKlass oop
 581     clear_nth_bit(ptr, 0);
 582     assert(Metaspace::contains((void*)ptr), "should be klass");
 583     ValueKlass* vk = (ValueKlass*)ptr;
 584     assert(vk->can_be_returned_as_fields(), "must be able to return as fields");
 585     return vk;
 586   }
 587 #ifdef ASSERT
 588   // Oop is not tagged, must be a valid oop
 589   if (VerifyOops) {
 590     oopDesc::verify(oop((HeapWord*)ptr));
 591   }
 592 #endif
 593   return NULL;
 594 }
 595 
 596 void ValueKlass::verify_on(outputStream* st) {
 597   InstanceKlass::verify_on(st);
 598   guarantee(prototype_header().is_always_locked(), "Prototype header is not always locked");
 599 }
 600 
 601 void ValueKlass::oop_verify_on(oop obj, outputStream* st) {
 602   InstanceKlass::oop_verify_on(obj, st);
 603   guarantee(obj->mark().is_always_locked(), "Header is not always locked");
 604 }