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