1 /*
  2  * Copyright (c) 2017, 2026, 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 "cds/archiveUtils.hpp"
 26 #include "cds/cdsConfig.hpp"
 27 #include "classfile/vmSymbols.hpp"
 28 #include "code/codeCache.hpp"
 29 #include "gc/shared/barrierSet.hpp"
 30 #include "gc/shared/collectedHeap.inline.hpp"
 31 #include "gc/shared/gcLocker.inline.hpp"
 32 #include "interpreter/interpreter.hpp"
 33 #include "logging/log.hpp"
 34 #include "memory/metadataFactory.hpp"
 35 #include "memory/metaspaceClosure.hpp"
 36 #include "oops/access.hpp"
 37 #include "oops/arrayKlass.hpp"
 38 #include "oops/compressedOops.inline.hpp"
 39 #include "oops/fieldStreams.inline.hpp"
 40 #include "oops/flatArrayKlass.hpp"
 41 #include "oops/inlineKlass.inline.hpp"
 42 #include "oops/instanceKlass.inline.hpp"
 43 #include "oops/method.hpp"
 44 #include "oops/objArrayKlass.hpp"
 45 #include "oops/oop.inline.hpp"
 46 #include "oops/refArrayKlass.hpp"
 47 #include "runtime/fieldDescriptor.inline.hpp"
 48 #include "runtime/handles.inline.hpp"
 49 #include "runtime/registerMap.hpp"
 50 #include "runtime/safepointVerifiers.hpp"
 51 #include "runtime/sharedRuntime.hpp"
 52 #include "runtime/signature.hpp"
 53 #include "runtime/thread.inline.hpp"
 54 #include "utilities/copy.hpp"
 55 #include "utilities/stringUtils.hpp"
 56 
 57 InlineKlass::Members::Members()
 58   : _extended_sig(nullptr),
 59     _return_regs(nullptr),
 60     _pack_handler(nullptr),
 61     _pack_handler_jobject(nullptr),
 62     _unpack_handler(nullptr),
 63     _null_reset_value_offset(0),
 64     _payload_offset(-1),
 65     _payload_size_in_bytes(-1),
 66     _payload_alignment(-1),
 67     _non_atomic_size_in_bytes(-1),
 68     _non_atomic_alignment(-1),
 69     _atomic_size_in_bytes(-1),
 70     _nullable_size_in_bytes(-1),
 71     _null_marker_offset(-1) {
 72 }
 73 
 74 InlineKlass::InlineKlass() {
 75   assert(CDSConfig::is_dumping_archive() || UseSharedSpaces, "only for CDS");
 76 }
 77 
 78 // Constructor
 79 InlineKlass::InlineKlass(const ClassFileParser& parser)
 80     : InstanceKlass(parser, InlineKlass::Kind, markWord::inline_type_prototype()) {
 81   assert(is_inline_klass(), "sanity");
 82   assert(prototype_header().is_inline_type(), "sanity");
 83 
 84   // Set up the offset to the members of this klass
 85   _adr_inline_klass_members = calculate_members_address();
 86 
 87   // Placement install the members
 88   new (_adr_inline_klass_members) Members();
 89 
 90   // Sanity check construction of the members
 91   assert(pack_handler() == nullptr, "pack handler not null");
 92 }
 93 
 94 address InlineKlass::calculate_members_address() const {
 95   // The members are placed after all other contents inherited from the InstanceKlass
 96   return end_of_instance_klass();
 97 }
 98 
 99 oop InlineKlass::null_reset_value() {
100   assert(is_initialized() || is_being_initialized() || is_in_error_state(), "null reset value is set at the beginning of initialization");
101   oop val = java_mirror()->obj_field_acquire(null_reset_value_offset());
102   assert(val != nullptr, "Sanity check");
103   return val;
104 }
105 
106 void InlineKlass::set_null_reset_value(oop val) {
107   assert(val != nullptr, "Sanity check");
108   assert(oopDesc::is_oop(val), "Sanity check");
109   assert(val->is_inline_type(), "Sanity check");
110   assert(val->klass() == this, "sanity check");
111   java_mirror()->obj_field_put(null_reset_value_offset(), val);
112 }
113 
114 instanceOop InlineKlass::allocate_instance(TRAPS) {
115   instanceOop oop = InstanceKlass::allocate_instance(CHECK_NULL);
116   assert(oop->mark().is_inline_type(), "Expected inline type");
117   return oop;
118 }
119 
120 int InlineKlass::nonstatic_oop_count() {
121   int oops = 0;
122   int map_count = nonstatic_oop_map_count();
123   OopMapBlock* block = start_of_nonstatic_oop_maps();
124   OopMapBlock* end = block + map_count;
125   while (block != end) {
126     oops += block->count();
127     block++;
128   }
129   return oops;
130 }
131 
132 int InlineKlass::layout_size_in_bytes(LayoutKind kind) const {
133   switch(kind) {
134     case LayoutKind::NULL_FREE_NON_ATOMIC_FLAT:
135       assert(has_non_atomic_layout(), "Layout not available");
136       return non_atomic_size_in_bytes();
137       break;
138     case LayoutKind::NULL_FREE_ATOMIC_FLAT:
139       assert(has_atomic_layout(), "Layout not available");
140       return atomic_size_in_bytes();
141       break;
142     case LayoutKind::NULLABLE_ATOMIC_FLAT:
143       assert(has_nullable_atomic_layout(), "Layout not available");
144       return nullable_atomic_size_in_bytes();
145       break;
146     case LayoutKind::BUFFERED:
147       return payload_size_in_bytes();
148       break;
149     default:
150       ShouldNotReachHere();
151   }
152 }
153 
154 int InlineKlass::layout_alignment(LayoutKind kind) const {
155   switch(kind) {
156     case LayoutKind::NULL_FREE_NON_ATOMIC_FLAT:
157       assert(has_non_atomic_layout(), "Layout not available");
158       return non_atomic_alignment();
159       break;
160     case LayoutKind::NULL_FREE_ATOMIC_FLAT:
161       assert(has_atomic_layout(), "Layout not available");
162       return atomic_size_in_bytes();
163       break;
164     case LayoutKind::NULLABLE_ATOMIC_FLAT:
165       assert(has_nullable_atomic_layout(), "Layout not available");
166       return nullable_atomic_size_in_bytes();
167       break;
168     case LayoutKind::BUFFERED:
169       return payload_alignment();
170       break;
171     default:
172       ShouldNotReachHere();
173   }
174 }
175 
176 bool InlineKlass::is_layout_supported(LayoutKind lk) {
177   switch(lk) {
178     case LayoutKind::NULL_FREE_NON_ATOMIC_FLAT:
179       return has_non_atomic_layout();
180       break;
181     case LayoutKind::NULL_FREE_ATOMIC_FLAT:
182       return has_atomic_layout();
183       break;
184     case LayoutKind::NULLABLE_ATOMIC_FLAT:
185       return has_nullable_atomic_layout();
186       break;
187     case LayoutKind::BUFFERED:
188       return true;
189       break;
190     default:
191       ShouldNotReachHere();
192   }
193 }
194 
195 void InlineKlass::copy_payload_to_addr(void* src, void* dst, LayoutKind lk, bool dest_is_initialized) {
196   assert(is_layout_supported(lk), "Unsupported layout");
197   assert(lk != LayoutKind::REFERENCE && lk != LayoutKind::UNKNOWN, "Sanity check");
198   switch(lk) {
199     case LayoutKind::NULLABLE_ATOMIC_FLAT: {
200       if (is_payload_marked_as_null((address)src)) {
201         // copy null_reset value to dest
202         if (dest_is_initialized) {
203           HeapAccess<>::value_copy(payload_addr(null_reset_value()), dst, this, lk);
204         } else {
205           HeapAccess<IS_DEST_UNINITIALIZED>::value_copy(payload_addr(null_reset_value()), dst, this, lk);
206         }
207       } else {
208         // Copy has to be performed, even if this is an empty value, because of the null marker
209         if (dest_is_initialized) {
210           HeapAccess<>::value_copy(src, dst, this, lk);
211         } else {
212           HeapAccess<IS_DEST_UNINITIALIZED>::value_copy(src, dst, this, lk);
213         }
214       }
215     }
216     break;
217     case LayoutKind::BUFFERED:
218     case LayoutKind::NULL_FREE_ATOMIC_FLAT:
219     case LayoutKind::NULL_FREE_NON_ATOMIC_FLAT: {
220       if (is_empty_inline_type()) return; // nothing to do
221       if (dest_is_initialized) {
222         HeapAccess<>::value_copy(src, dst, this, lk);
223       } else {
224         HeapAccess<IS_DEST_UNINITIALIZED>::value_copy(src, dst, this, lk);
225       }
226     }
227     break;
228     default:
229       ShouldNotReachHere();
230   }
231 }
232 
233 oop InlineKlass::read_payload_from_addr(const oop src, size_t offset, LayoutKind lk, TRAPS) {
234   assert(src != nullptr, "Must be");
235   assert(is_layout_supported(lk), "Unsupported layout");
236   switch(lk) {
237     case LayoutKind::NULLABLE_ATOMIC_FLAT: {
238       if (is_payload_marked_as_null((address)((char*)(oopDesc*)src + offset))) {
239         return nullptr;
240       }
241     } // Fallthrough
242     case LayoutKind::BUFFERED:
243     case LayoutKind::NULL_FREE_ATOMIC_FLAT:
244     case LayoutKind::NULL_FREE_NON_ATOMIC_FLAT: {
245       Handle obj_h(THREAD, src);
246       oop res = allocate_instance(CHECK_NULL);
247       copy_payload_to_addr((void*)(cast_from_oop<char*>(obj_h()) + offset), payload_addr(res), lk, false);
248       if (LayoutKindHelper::is_nullable_flat(lk)) {
249         if(is_payload_marked_as_null(payload_addr(res))) {
250           return nullptr;
251         }
252       }
253       return res;
254     }
255     break;
256     default:
257       ShouldNotReachHere();
258   }
259 }
260 
261 void InlineKlass::write_value_to_addr(oop src, void* dst, LayoutKind lk, TRAPS) {
262   void* src_addr = nullptr;
263   if (src == nullptr) {
264     if (!LayoutKindHelper::is_nullable_flat(lk)) {
265       THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Value is null");
266     }
267     // Writing null to a nullable flat field/element is usually done by writing
268     // the whole pre-allocated null_reset_value at the payload address to ensure
269     // that the null marker and all potential oops are reset to "zeros".
270     // However, the null_reset_value is allocated during class initialization.
271     // If the current value of the field is null, it is possible that the class
272     // of the field has not been initialized yet and thus the null_reset_value
273     // might not be available yet.
274     // Writing null over an already null value should not trigger class initialization.
275     // The solution is to detect null being written over null cases and return immediately
276     // (writing null over null is a no-op from a field modification point of view)
277     if (is_payload_marked_as_null((address)dst)) return;
278     src_addr = payload_addr(null_reset_value());
279   } else {
280     src_addr = payload_addr(src);
281     if (LayoutKindHelper::is_nullable_flat(lk)) {
282       mark_payload_as_non_null((address)src_addr);
283     }
284   }
285   copy_payload_to_addr(src_addr, dst, lk, true /* dest_is_initialized */);
286 }
287 
288 // Arrays of...
289 
290 bool InlineKlass::maybe_flat_in_array() {
291   if (!UseArrayFlattening) {
292     return false;
293   }
294   // Too many embedded oops
295   if ((FlatArrayElementMaxOops >= 0) && (nonstatic_oop_count() > FlatArrayElementMaxOops)) {
296     return false;
297   }
298   // No flat layout?
299   if (!has_nullable_atomic_layout() && !has_atomic_layout() && !has_non_atomic_layout()) {
300     return false;
301   }
302   return true;
303 }
304 
305 bool InlineKlass::is_always_flat_in_array() {
306   if (!UseArrayFlattening) {
307     return false;
308   }
309   // Too many embedded oops
310   if ((FlatArrayElementMaxOops >= 0) && (nonstatic_oop_count() > FlatArrayElementMaxOops)) {
311     return false;
312   }
313 
314   // An instance is always flat in an array if we have all layouts. Note that this could change in the future when the
315   // flattening policies are updated or if new APIs are added that allow the creation of reference arrays directly.
316   return has_nullable_atomic_layout() && has_atomic_layout() && has_non_atomic_layout();
317 }
318 
319 // Inline type arguments are not passed by reference, instead each
320 // field of the inline type is passed as an argument. This helper
321 // function collects the flat field (recursively)
322 // in a list. Included with the field's type is
323 // the offset of each field in the inline type: i2c and c2i adapters
324 // need that to load or store fields. Finally, the list of fields is
325 // sorted in order of increasing offsets: the adapters and the
326 // compiled code need to agree upon the order of fields.
327 //
328 // The list of basic types that is returned starts with a T_METADATA
329 // and ends with an extra T_VOID. T_METADATA/T_VOID pairs are used as
330 // delimiters. Every entry between the two is a field of the inline
331 // type. If there's an embedded inline type in the list, it also starts
332 // with a T_METADATA and ends with a T_VOID. This is so we can
333 // generate a unique fingerprint for the method's adapters and we can
334 // generate the list of basic types from the interpreter point of view
335 // (inline types passed as reference: iterate on the list until a
336 // T_METADATA, drop everything until and including the closing
337 // T_VOID) or the compiler point of view (each field of the inline
338 // types is an argument: drop all T_METADATA/T_VOID from the list).
339 //
340 // Value classes could also have fields in abstract super value classes.
341 // Use a HierarchicalFieldStream to get them as well.
342 int InlineKlass::collect_fields(GrowableArray<SigEntry>* sig, int base_off, int null_marker_offset) {
343   int count = 0;
344   SigEntry::add_entry(sig, T_METADATA, name(), base_off);
345   for (TopDownHierarchicalNonStaticFieldStreamBase fs(this); !fs.done(); fs.next()) {
346     assert(!fs.access_flags().is_static(), "TopDownHierarchicalNonStaticFieldStreamBase should not let static fields pass.");
347     int offset = base_off + fs.offset() - (base_off > 0 ? payload_offset() : 0);
348     InstanceKlass* field_holder = fs.field_descriptor().field_holder();
349     // TODO 8284443 Use different heuristic to decide what should be scalarized in the calling convention
350     if (fs.is_flat()) {
351       // Resolve klass of flat field and recursively collect fields
352       int field_null_marker_offset = -1;
353       if (!fs.is_null_free_inline_type()) {
354         field_null_marker_offset = base_off + fs.null_marker_offset() - (base_off > 0 ? payload_offset() : 0);
355       }
356       Klass* vk = field_holder->get_inline_type_field_klass(fs.index());
357       count += InlineKlass::cast(vk)->collect_fields(sig, offset, field_null_marker_offset);
358     } else {
359       BasicType bt = Signature::basic_type(fs.signature());
360       SigEntry::add_entry(sig, bt,  fs.name(), offset);
361       count += type2size[bt];
362     }
363   }
364   int offset = base_off + size_helper()*HeapWordSize - (base_off > 0 ? payload_offset() : 0);
365   // Null markers are no real fields, add them manually at the end (C2 relies on this) of the flat fields
366   if (null_marker_offset != -1) {
367     SigEntry::add_null_marker(sig, name(), null_marker_offset);
368     count++;
369   }
370   SigEntry::add_entry(sig, T_VOID, name(), offset);
371   assert(sig->at(0)._bt == T_METADATA && sig->at(sig->length()-1)._bt == T_VOID, "broken structure");
372   return count;
373 }
374 
375 void InlineKlass::initialize_calling_convention(TRAPS) {
376   // Because the pack and unpack handler addresses need to be loadable from generated code,
377   // they are stored at a fixed offset in the klass metadata. Since inline type klasses do
378   // not have a vtable, the vtable offset is used to store these addresses.
379   if (InlineTypeReturnedAsFields || InlineTypePassFieldsAsArgs) {
380     ResourceMark rm;
381     GrowableArray<SigEntry> sig_vk;
382     int nb_fields = collect_fields(&sig_vk);
383     if (*PrintInlineKlassFields != '\0') {
384       const char* class_name_str = _name->as_C_string();
385       if (StringUtils::class_list_match(PrintInlineKlassFields, class_name_str)) {
386         ttyLocker ttyl;
387         tty->print_cr("Fields of InlineKlass: %s", class_name_str);
388         for (const SigEntry& entry : sig_vk) {
389           tty->print("  %s: %s+%d", entry._name->as_C_string(), type2name(entry._bt), entry._offset);
390           if (entry._null_marker) {
391             tty->print(" (null marker)");
392           }
393           tty->print_cr("");
394         }
395       }
396     }
397     Array<SigEntry>* extended_sig = MetadataFactory::new_array<SigEntry>(class_loader_data(), sig_vk.length(), CHECK);
398     set_extended_sig(extended_sig);
399     for (int i = 0; i < sig_vk.length(); i++) {
400       extended_sig->at_put(i, sig_vk.at(i));
401     }
402     if (can_be_returned_as_fields(/* init= */ true)) {
403       nb_fields++;
404       BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, nb_fields);
405       sig_bt[0] = T_METADATA;
406       SigEntry::fill_sig_bt(&sig_vk, sig_bt+1);
407       VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair, nb_fields);
408       int total = SharedRuntime::java_return_convention(sig_bt, regs, nb_fields);
409 
410       if (total > 0) {
411         Array<VMRegPair>* return_regs = MetadataFactory::new_array<VMRegPair>(class_loader_data(), nb_fields, CHECK);
412         set_return_regs(return_regs);
413         for (int i = 0; i < nb_fields; i++) {
414           return_regs->at_put(i, regs[i]);
415         }
416 
417         BufferedInlineTypeBlob* buffered_blob = SharedRuntime::generate_buffered_inline_type_adapter(this);
418         if (buffered_blob == nullptr) {
419           THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), "Out of space in CodeCache for adapters");
420         }
421         set_pack_handler(buffered_blob->pack_fields());
422         set_pack_handler_jobject(buffered_blob->pack_fields_jobject());
423         set_unpack_handler(buffered_blob->unpack_fields());
424         assert(CodeCache::find_blob(pack_handler()) == buffered_blob, "lost track of blob");
425         assert(can_be_returned_as_fields(), "sanity");
426       }
427     }
428     if (!can_be_returned_as_fields() && !can_be_passed_as_fields()) {
429       MetadataFactory::free_array<SigEntry>(class_loader_data(), extended_sig);
430       assert(return_regs() == nullptr, "sanity");
431     }
432   }
433 }
434 
435 void InlineKlass::deallocate_contents(ClassLoaderData* loader_data) {
436   if (extended_sig() != nullptr) {
437     MetadataFactory::free_array<SigEntry>(loader_data, members()._extended_sig);
438     set_extended_sig(nullptr);
439   }
440   if (return_regs() != nullptr) {
441     MetadataFactory::free_array<VMRegPair>(loader_data, members()._return_regs);
442     set_return_regs(nullptr);
443   }
444   cleanup_blobs();
445   InstanceKlass::deallocate_contents(loader_data);
446 }
447 
448 void InlineKlass::cleanup(InlineKlass* ik) {
449   ik->cleanup_blobs();
450 }
451 
452 void InlineKlass::cleanup_blobs() {
453   if (pack_handler() != nullptr) {
454     CodeBlob* buffered_blob = CodeCache::find_blob(pack_handler());
455     assert(buffered_blob->is_buffered_inline_type_blob(), "bad blob type");
456     BufferBlob::free((BufferBlob*)buffered_blob);
457     set_pack_handler(nullptr);
458     set_pack_handler_jobject(nullptr);
459     set_unpack_handler(nullptr);
460   }
461 }
462 
463 // Can this inline type be passed as multiple values?
464 bool InlineKlass::can_be_passed_as_fields() const {
465   return InlineTypePassFieldsAsArgs;
466 }
467 
468 // Can this inline type be returned as multiple values?
469 bool InlineKlass::can_be_returned_as_fields(bool init) const {
470   return InlineTypeReturnedAsFields && (init || return_regs() != nullptr);
471 }
472 
473 // Create handles for all oop fields returned in registers that are going to be live across a safepoint
474 void InlineKlass::save_oop_fields(const RegisterMap& reg_map, GrowableArray<Handle>& handles) const {
475   Thread* thread = Thread::current();
476   const Array<SigEntry>* sig_vk = extended_sig();
477   const Array<VMRegPair>* regs = return_regs();
478   int j = 1;
479 
480   for (int i = 0; i < sig_vk->length(); i++) {
481     BasicType bt = sig_vk->at(i)._bt;
482     if (bt == T_OBJECT || bt == T_ARRAY) {
483       VMRegPair pair = regs->at(j);
484       address loc = reg_map.location(pair.first(), nullptr);
485       oop o = *(oop*)loc;
486       assert(oopDesc::is_oop_or_null(o), "Bad oop value: " PTR_FORMAT, p2i(o));
487       handles.push(Handle(thread, o));
488     }
489     if (bt == T_METADATA) {
490       continue;
491     }
492     if (bt == T_VOID &&
493         sig_vk->at(i-1)._bt != T_LONG &&
494         sig_vk->at(i-1)._bt != T_DOUBLE) {
495       continue;
496     }
497     j++;
498   }
499   assert(j == regs->length(), "missed a field?");
500 }
501 
502 // Update oop fields in registers from handles after a safepoint
503 void InlineKlass::restore_oop_results(RegisterMap& reg_map, GrowableArray<Handle>& handles) const {
504   assert(InlineTypeReturnedAsFields, "Inline types should never be returned as fields");
505   const Array<SigEntry>* sig_vk = extended_sig();
506   const Array<VMRegPair>* regs = return_regs();
507   assert(regs != nullptr, "inconsistent");
508 
509   int j = 1;
510   int k = 0;
511   for (int i = 0; i < sig_vk->length(); i++) {
512     BasicType bt = sig_vk->at(i)._bt;
513     if (bt == T_OBJECT || bt == T_ARRAY) {
514       VMRegPair pair = regs->at(j);
515       address loc = reg_map.location(pair.first(), nullptr);
516       *(oop*)loc = handles.at(k++)();
517     }
518     if (bt == T_METADATA) {
519       continue;
520     }
521     if (bt == T_VOID &&
522         sig_vk->at(i-1)._bt != T_LONG &&
523         sig_vk->at(i-1)._bt != T_DOUBLE) {
524       continue;
525     }
526     j++;
527   }
528   assert(k == handles.length(), "missed a handle?");
529   assert(j == regs->length(), "missed a field?");
530 }
531 
532 // Fields are in registers. Create an instance of the inline type and
533 // initialize it with the values of the fields.
534 oop InlineKlass::realloc_result(const RegisterMap& reg_map, const GrowableArray<Handle>& handles, TRAPS) {
535   oop new_vt = allocate_instance(CHECK_NULL);
536   const Array<SigEntry>* sig_vk = extended_sig();
537   const Array<VMRegPair>* regs = return_regs();
538 
539   int j = 1;
540   int k = 0;
541   for (int i = 0; i < sig_vk->length(); i++) {
542     BasicType bt = sig_vk->at(i)._bt;
543     if (bt == T_METADATA) {
544       continue;
545     }
546     if (bt == T_VOID) {
547       if (sig_vk->at(i-1)._bt == T_LONG ||
548           sig_vk->at(i-1)._bt == T_DOUBLE) {
549         j++;
550       }
551       continue;
552     }
553     int off = sig_vk->at(i)._offset;
554     assert(off > 0, "offset in object should be positive");
555     VMRegPair pair = regs->at(j);
556     address loc = reg_map.location(pair.first(), nullptr);
557     switch(bt) {
558     case T_BOOLEAN: {
559       new_vt->bool_field_put(off, *(jboolean*)loc);
560       break;
561     }
562     case T_CHAR: {
563       new_vt->char_field_put(off, *(jchar*)loc);
564       break;
565     }
566     case T_BYTE: {
567       new_vt->byte_field_put(off, *(jbyte*)loc);
568       break;
569     }
570     case T_SHORT: {
571       new_vt->short_field_put(off, *(jshort*)loc);
572       break;
573     }
574     case T_INT: {
575       new_vt->int_field_put(off, *(jint*)loc);
576       break;
577     }
578     case T_LONG: {
579 #ifdef _LP64
580       new_vt->double_field_put(off,  *(jdouble*)loc);
581 #else
582       Unimplemented();
583 #endif
584       break;
585     }
586     case T_OBJECT:
587     case T_ARRAY: {
588       Handle handle = handles.at(k++);
589       new_vt->obj_field_put(off, handle());
590       break;
591     }
592     case T_FLOAT: {
593       new_vt->float_field_put(off,  *(jfloat*)loc);
594       break;
595     }
596     case T_DOUBLE: {
597       new_vt->double_field_put(off, *(jdouble*)loc);
598       break;
599     }
600     default:
601       ShouldNotReachHere();
602     }
603     *(intptr_t*)loc = 0xDEAD;
604     j++;
605   }
606   assert(j == regs->length(), "missed a field?");
607   assert(k == handles.length(), "missed an oop?");
608   return new_vt;
609 }
610 
611 // Check if we return an inline type in scalarized form, i.e. check if either
612 // - The return value is a tagged InlineKlass pointer, or
613 // - The return value is an inline type oop that is also returned in scalarized form
614 InlineKlass* InlineKlass::returned_inline_klass(const RegisterMap& map, bool* return_oop, Method* method) {
615   BasicType bt = T_METADATA;
616   VMRegPair pair;
617   int nb = SharedRuntime::java_return_convention(&bt, &pair, 1);
618   assert(nb == 1, "broken");
619 
620   address loc = map.location(pair.first(), nullptr);
621   intptr_t ptr = *(intptr_t*)loc;
622   if (is_set_nth_bit(ptr, 0)) {
623     // Return value is tagged, must be an InlineKlass pointer
624     clear_nth_bit(ptr, 0);
625     assert(Metaspace::contains((void*)ptr), "should be klass");
626     InlineKlass* vk = (InlineKlass*)ptr;
627     assert(vk->can_be_returned_as_fields(), "must be able to return as fields");
628     if (return_oop != nullptr) {
629       // Not returning an oop
630       *return_oop = false;
631     }
632     return vk;
633   }
634   // Return value is not tagged, must be a valid oop
635   oop o = cast_to_oop(ptr);
636   assert(oopDesc::is_oop_or_null(o), "Bad oop return: " PTR_FORMAT, ptr);
637   if (return_oop != nullptr && o != nullptr && o->is_inline_type()) {
638     // Check if inline type is also returned in scalarized form
639     InlineKlass* vk_val = InlineKlass::cast(o->klass());
640     InlineKlass* vk_sig = method->returns_inline_type();
641     if (vk_val->can_be_returned_as_fields() && vk_sig != nullptr) {
642       assert(vk_val == vk_sig, "Unexpected return value");
643       return vk_val;
644     }
645   }
646   return nullptr;
647 }
648 
649 // CDS support
650 #if INCLUDE_CDS
651 
652 void InlineKlass::remove_unshareable_info() {
653   InstanceKlass::remove_unshareable_info();
654 
655   // update it to point to the "buffered" copy of this class.
656   _adr_inline_klass_members = calculate_members_address();
657   ArchivePtrMarker::mark_pointer(&_adr_inline_klass_members);
658 
659   set_extended_sig(nullptr);
660   set_return_regs(nullptr);
661   set_pack_handler(nullptr);
662   set_pack_handler_jobject(nullptr);
663   set_unpack_handler(nullptr);
664 
665   assert(pack_handler() == nullptr, "pack handler not null");
666 }
667 
668 #endif // CDS
669 
670 // Verification
671 
672 void InlineKlass::verify_on(outputStream* st) {
673   InstanceKlass::verify_on(st);
674   guarantee(prototype_header().is_inline_type(), "Prototype header is not inline type");
675 }
676 
677 void InlineKlass::oop_verify_on(oop obj, outputStream* st) {
678   InstanceKlass::oop_verify_on(obj, st);
679   guarantee(obj->mark().is_inline_type(), "Header is not inline type");
680 }