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