1 /*
  2  * Copyright (c) 2023, 2025, 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 #ifndef SHARE_OOPS_FIELDINFO_INLINE_HPP
 26 #define SHARE_OOPS_FIELDINFO_INLINE_HPP
 27 
 28 #include "oops/fieldInfo.hpp"
 29 
 30 #include "memory/metadataFactory.hpp"
 31 #include "oops/constantPool.hpp"
 32 #include "oops/symbol.hpp"
 33 #include "runtime/atomic.hpp"
 34 #include "utilities/checkedCast.hpp"
 35 
 36 inline Symbol* FieldInfo::name(ConstantPool* cp) const {
 37   int index = _name_index;
 38   if (_field_flags.is_injected()) {
 39     return lookup_symbol(index);
 40   }
 41   return cp->symbol_at(index);
 42 }
 43 
 44 inline Symbol* FieldInfo::signature(ConstantPool* cp) const {
 45   int index = _signature_index;
 46   if (_field_flags.is_injected()) {
 47     return lookup_symbol(index);
 48   }
 49   return cp->symbol_at(index);
 50 }
 51 
 52 inline Symbol* FieldInfo::lookup_symbol(int symbol_index) const {
 53   assert(_field_flags.is_injected(), "only injected fields");
 54   return Symbol::vm_symbol_at(static_cast<vmSymbolID>(symbol_index));
 55 }
 56 
 57 inline int FieldInfoStream::num_injected_java_fields(const Array<u1>* fis) {
 58   FieldInfoReader fir(fis);
 59   int java_fields_count;
 60   int injected_fields_count;
 61   fir.read_field_counts(&java_fields_count, &injected_fields_count);
 62   return injected_fields_count;
 63 }
 64 
 65 inline int FieldInfoStream::num_total_fields(const Array<u1>* fis) {
 66   FieldInfoReader fir(fis);
 67   int java_fields_count;
 68   int injected_fields_count;
 69   fir.read_field_counts(&java_fields_count, &injected_fields_count);
 70   return java_fields_count + injected_fields_count;
 71 }
 72 
 73 inline int FieldInfoStream::num_java_fields(const Array<u1>* fis) {
 74   FieldInfoReader fir(fis);
 75   int java_fields_count;
 76   int injected_fields_count;
 77   fir.read_field_counts(&java_fields_count, &injected_fields_count);
 78   return java_fields_count;
 79 }
 80 
 81 template<typename CON>
 82 inline void Mapper<CON>::map_field_info(const FieldInfo& fi) {
 83   _next_index++;  // pre-increment
 84   _consumer->accept_uint(fi.name_index());
 85   _consumer->accept_uint(fi.signature_index());
 86   _consumer->accept_uint(fi.offset());
 87   _consumer->accept_uint(fi.access_flags().as_field_flags());
 88   _consumer->accept_uint(fi.field_flags().as_uint());
 89   if(fi.field_flags().has_any_optionals()) {
 90     if (fi.field_flags().is_initialized()) {
 91       _consumer->accept_uint(fi.initializer_index());
 92     }
 93     if (fi.field_flags().is_generic()) {
 94       _consumer->accept_uint(fi.generic_signature_index());
 95     }
 96     if (fi.field_flags().is_contended()) {
 97       _consumer->accept_uint(fi.contention_group());
 98     }
 99   } else {
100     assert(fi.initializer_index() == 0, "");
101     assert(fi.generic_signature_index() == 0, "");
102     assert(fi.contention_group() == 0, "");
103   }
104 }
105 
106 
107 inline FieldInfoReader::FieldInfoReader(const Array<u1>* fi)
108   : _r(fi->data(), fi->length()),
109     _next_index(0) { }
110 
111 inline void FieldInfoReader::read_field_counts(int* java_fields, int* injected_fields) {
112   *java_fields = next_uint();
113   *injected_fields = next_uint();
114 }
115 
116 inline void FieldInfoReader::read_name_and_signature(u2* name_index, u2* signature_index) {
117   *name_index = checked_cast<u2>(next_uint());
118   *signature_index = checked_cast<u2>(next_uint());
119 }
120 
121 inline void FieldInfoReader::read_field_info(FieldInfo& fi) {
122   fi._index = _next_index++;
123   read_name_and_signature(&fi._name_index, &fi._signature_index);
124   fi._offset = next_uint();
125   fi._access_flags = AccessFlags(checked_cast<u2>(next_uint()));
126   fi._field_flags = FieldInfo::FieldFlags(next_uint());
127   if (fi._field_flags.is_initialized()) {
128     fi._initializer_index = checked_cast<u2>(next_uint());
129   } else {
130     fi._initializer_index = 0;
131   }
132   if (fi._field_flags.is_generic()) {
133     fi._generic_signature_index = checked_cast<u2>(next_uint());
134   } else {
135     fi._generic_signature_index = 0;
136   }
137   if (fi._field_flags.is_contended()) {
138     fi._contention_group = checked_cast<u2>(next_uint());
139   } else {
140     fi._contention_group = 0;
141   }
142 }
143 
144 inline FieldInfoReader&  FieldInfoReader::skip_field_info() {
145   _next_index++;
146   const int name_sig_af_off = 4;  // four items
147   skip(name_sig_af_off);
148   FieldInfo::FieldFlags ff(next_uint());
149   if (ff.has_any_optionals()) {
150     const int init_gen_cont = (ff.is_initialized() +
151                                 ff.is_generic() +
152                                 ff.is_contended());
153     skip(init_gen_cont);  // up to three items
154   }
155   return *this;
156 }
157 
158 // Skip to the nth field.  If the reader is freshly initialized to
159 // the zero index, this will call skip_field_info() n times.
160 inline FieldInfoReader& FieldInfoReader::skip_to_field_info(int n) {
161   assert(n >= _next_index, "already past that index");
162   const int count = n - _next_index;
163   for (int i = 0; i < count; i++)  skip_field_info();
164   assert(_next_index == n, "");
165   return *this;
166 }
167 
168 // for random access, if you know where to go up front:
169 inline FieldInfoReader& FieldInfoReader::set_position_and_next_index(int position, int next_index) {
170   _r.set_position(position);
171   _next_index = next_index;
172   return *this;
173 }
174 
175 inline void FieldStatus::atomic_set_bits(u1& flags, u1 mask) {
176   Atomic::fetch_then_or(&flags, mask);
177 }
178 
179 inline void FieldStatus::atomic_clear_bits(u1& flags, u1 mask) {
180   Atomic::fetch_then_and(&flags, (u1)(~mask));
181 }
182 
183 inline void FieldStatus::update_flag(FieldStatusBitPosition pos, bool z) {
184   if (z) atomic_set_bits(_flags, flag_mask(pos));
185   else atomic_clear_bits(_flags, flag_mask(pos));
186 }
187 
188 inline void FieldStatus::update_access_watched(bool z) { update_flag(_fs_access_watched, z); }
189 inline void FieldStatus::update_modification_watched(bool z) { update_flag(_fs_modification_watched, z); }
190 inline void FieldStatus::update_initialized_final_update(bool z) { update_flag(_initialized_final_update, z); }
191 
192 #endif // SHARE_OOPS_FIELDINFO_INLINE_HPP