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 "ci/ciConstant.hpp"
 26 #include "ci/ciField.hpp"
 27 #include "ci/ciInlineKlass.hpp"
 28 #include "ci/ciUtilities.inline.hpp"
 29 #include "oops/array.hpp"
 30 #include "runtime/signature.hpp"
 31 #include "utilities/globalDefinitions.hpp"
 32 
 33 // Offset of the first field in the inline type
 34 int ciInlineKlass::payload_offset() const {
 35   GUARDED_VM_ENTRY(return to_InlineKlass()->payload_offset();)
 36 }
 37 
 38 // Could any array containing an instance of this value class ever be flat?
 39 bool ciInlineKlass::maybe_flat_in_array() const {
 40   GUARDED_VM_ENTRY(return to_InlineKlass()->maybe_flat_in_array();)
 41 }
 42 
 43 // Are arrays containing an instance of this value class always flat?
 44 bool ciInlineKlass::is_always_flat_in_array() const {
 45   GUARDED_VM_ENTRY(return to_InlineKlass()->is_always_flat_in_array();)
 46 }
 47 
 48 // Can this inline type be passed as multiple values?
 49 bool ciInlineKlass::can_be_passed_as_fields() const {
 50   GUARDED_VM_ENTRY(return to_InlineKlass()->can_be_passed_as_fields();)
 51 }
 52 
 53 // Can this inline type be returned as multiple values?
 54 bool ciInlineKlass::can_be_returned_as_fields() const {
 55   GUARDED_VM_ENTRY(return to_InlineKlass()->can_be_returned_as_fields();)
 56 }
 57 
 58 bool ciInlineKlass::is_empty() {
 59   // Do not use InlineKlass::is_empty_inline_type here because it does
 60   // consider the container empty even if fields of empty inline types
 61   // are not flat
 62   return nof_declared_nonstatic_fields() == 0;
 63 }
 64 
 65 int ciInlineKlass::inline_arg_length() const {
 66   VM_ENTRY_MARK;
 67   return get_InlineKlass()->extended_sig()->length();
 68 }
 69 
 70 // When passing an inline type's fields as arguments, count the number
 71 // of argument slots that are needed
 72 int ciInlineKlass::inline_arg_slots() const {
 73   VM_ENTRY_MARK;
 74   const Array<SigEntry>* sig_vk = get_InlineKlass()->extended_sig();
 75   int slots = 0;
 76   for (int i = 0; i < sig_vk->length(); i++) {
 77     BasicType bt = sig_vk->at(i)._bt;
 78     if (bt == T_METADATA || bt == T_VOID) {
 79       continue;
 80     }
 81     slots += type2size[bt];
 82   }
 83   return slots;
 84 }
 85 
 86 bool ciInlineKlass::contains_oops() const {
 87   GUARDED_VM_ENTRY(return get_InlineKlass()->contains_oops();)
 88 }
 89 
 90 int ciInlineKlass::oop_count() const {
 91   GUARDED_VM_ENTRY(return get_InlineKlass()->nonstatic_oop_count();)
 92 }
 93 
 94 address ciInlineKlass::pack_handler() const {
 95   GUARDED_VM_ENTRY(return get_InlineKlass()->pack_handler();)
 96 }
 97 
 98 address ciInlineKlass::unpack_handler() const {
 99   GUARDED_VM_ENTRY(return get_InlineKlass()->unpack_handler();)
100 }
101 
102 InlineKlass* ciInlineKlass::get_InlineKlass() const {
103   GUARDED_VM_ENTRY(return to_InlineKlass();)
104 }
105 
106 bool ciInlineKlass::has_null_free_non_atomic_layout() const {
107   GUARDED_VM_ENTRY(return get_InlineKlass()->has_null_free_non_atomic_layout();)
108 }
109 
110 bool ciInlineKlass::has_null_free_atomic_layout() const {
111   GUARDED_VM_ENTRY(return get_InlineKlass()->has_null_free_atomic_layout();)
112 }
113 
114 bool ciInlineKlass::has_nullable_atomic_layout() const {
115   GUARDED_VM_ENTRY(return get_InlineKlass()->has_nullable_atomic_layout();)
116 }
117 
118 int ciInlineKlass::null_marker_offset_in_payload() const {
119   GUARDED_VM_ENTRY(return get_InlineKlass()->null_marker_offset_in_payload();)
120 }
121 
122 // Convert size of atomic layout in bytes to corresponding BasicType
123 BasicType ciInlineKlass::atomic_size_to_basic_type(bool null_free) const {
124   VM_ENTRY_MARK
125   InlineKlass* vk = get_InlineKlass();
126   assert(!null_free || vk->has_null_free_atomic_layout(), "No null-free atomic layout available");
127   assert( null_free || vk->has_nullable_atomic_layout(), "No nullable atomic layout available");
128   int size = null_free ? vk->null_free_atomic_size_in_bytes() : vk->nullable_atomic_size_in_bytes();
129   BasicType bt = T_ILLEGAL;
130   if (size == sizeof(jlong)) {
131     bt = T_LONG;
132   } else if (size == sizeof(jint)) {
133     bt = T_INT;
134   } else if (size == sizeof(jshort)) {
135     bt = T_SHORT;
136   } else if (size == sizeof(jbyte)) {
137     bt = T_BYTE;
138   } else {
139     assert(false, "Unsupported size: %d", size);
140   }
141   return bt;
142 }
143 
144 bool ciInlineKlass::is_naturally_atomic(bool null_free) {
145   return null_free ? (nof_nonstatic_fields() <= 1) : (nof_nonstatic_fields() == 0);
146 }
147 
148 int ciInlineKlass::field_map_offset() const {
149   GUARDED_VM_ENTRY(return get_InlineKlass()->acmp_maps_offset();)
150 }
151 
152 ciConstant ciInlineKlass::get_field_map() const {
153   VM_ENTRY_MARK
154   InlineKlass* vk = get_InlineKlass();
155   oop array = vk->java_mirror()->obj_field(vk->acmp_maps_offset());
156   return ciConstant(T_ARRAY, CURRENT_ENV->get_object(array));
157 }
158 
159 // All fields of this object are zero even if they are null-free. As a result, this object should
160 // only be used to reset the payload of fields or array elements and should not be leaked
161 // elsewhere.
162 ciConstant ciInlineKlass::get_null_reset_value() {
163   assert(is_initialized(), "null_reset_value is only allocated during initialization of %s", name()->as_utf8());
164   VM_ENTRY_MARK
165   InlineKlass* vk = get_InlineKlass();
166   oop null_reset_value = vk->null_reset_value();
167   return ciConstant(T_OBJECT, CURRENT_ENV->get_object(null_reset_value));
168 }