1 /*
2 * Copyright (c) 1999, 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_CI_CIINSTANCEKLASS_HPP
26 #define SHARE_CI_CIINSTANCEKLASS_HPP
27
28 #include "ci/ciConstantPoolCache.hpp"
29 #include "ci/ciFlags.hpp"
30 #include "ci/ciKlass.hpp"
31 #include "ci/ciSymbol.hpp"
32 #include "oops/instanceKlass.hpp"
33
34 // ciInstanceKlass
35 //
36 // This class represents a Klass* in the HotSpot virtual machine
37 // whose Klass part is an InstanceKlass. It may or may not
38 // be loaded.
39 class ciInstanceKlass : public ciKlass {
40 CI_PACKAGE_ACCESS
41 friend class ciBytecodeStream;
42 friend class ciEnv;
43 friend class ciExceptionHandler;
44 friend class ciMethod;
45 friend class ciField;
46 friend class ciReplay;
47 friend class CompileTrainingData;
48
49 private:
50 enum SubklassValue { subklass_unknown, subklass_false, subklass_true };
51
52 jobject _loader;
53
54 InstanceKlass::ClassState _init_state; // state of class
55 bool _is_shared;
56 bool _has_finalizer;
57 SubklassValue _has_subklass;
58 bool _has_nonstatic_fields;
59 bool _has_nonstatic_concrete_methods;
60 bool _is_hidden;
61 bool _is_record;
62 bool _has_trusted_loader;
63
64 ciFlags _flags;
65
66 // Lazy fields get filled in only upon request.
67 ciInstanceKlass* _super;
68 ciInstance* _java_mirror;
69
70 ciConstantPoolCache* _field_cache; // cached map index->field
71
72 // Fields declared in the bytecode (without nested fields in flat fields),
73 // ordered in JavaFieldStream order, with superclasses first (i.e. from lang.java.Object
74 // to most derived class).
75 const GrowableArray<ciField*>* _declared_nonstatic_fields;
76
77 // Fields laid out in memory (flat fields are expanded into their components). The ciField object
78 // for each primitive component has the holder being this ciInstanceKlass or one of its
79 // superclasses.
80 // Fields are in the same order as in _declared_nonstatic_fields, but flat fields are replaced by
81 // the list of their own fields, ordered the same way (hierarchy traversed top-down, in
82 // JavaFieldStream order).
83 const GrowableArray<ciField*>* _nonstatic_fields;
84
85 int _has_injected_fields; // any non static injected fields? lazily initialized.
86
87 // The possible values of the _implementor fall into following three cases:
88 // null: no implementor.
89 // A ciInstanceKlass that's not itself: one implementor.
90 // Itself: more than one implementor.
91 ciInstanceKlass* _implementor;
92 GrowableArray<ciInstanceKlass*>* _transitive_interfaces;
93
94 void compute_injected_fields();
95 bool compute_injected_fields_helper();
96 void compute_transitive_interfaces();
97
98 ciField* get_non_static_field_by_offset(int field_offset);
99
100 protected:
101 ciInstanceKlass(Klass* k);
102 ciInstanceKlass(ciSymbol* name, jobject loader, BasicType bt = T_OBJECT); // for unloaded klasses
103
104 InstanceKlass* get_instanceKlass() const {
105 return InstanceKlass::cast(get_Klass());
106 }
107
108 oop loader();
109 jobject loader_handle();
110
111 const char* type_string() { return "ciInstanceKlass"; }
112
113 bool is_in_package_impl(const char* packagename, int len);
114
115 void print_impl(outputStream* st);
116
117 ciConstantPoolCache* field_cache();
118
119 bool is_shared() { return _is_shared; }
120
121 void compute_shared_init_state();
122 bool compute_shared_has_subklass();
123 void compute_nonstatic_fields();
124 void compute_nonstatic_fields_impl(const GrowableArray<ciField*>* super_declared_fields, const GrowableArray<ciField*>* super_fields);
125 bool compute_has_trusted_loader();
126
127 // Update the init_state for shared klasses
128 void update_if_shared(InstanceKlass::ClassState expected) {
129 if (_is_shared && _init_state != expected) {
130 if (is_loaded()) compute_shared_init_state();
131 }
132 }
133
134 public:
135 // Has this klass been initialized?
136 bool is_initialized() {
137 update_if_shared(InstanceKlass::fully_initialized);
138 return _init_state == InstanceKlass::fully_initialized;
139 }
140 bool is_not_initialized() {
141 update_if_shared(InstanceKlass::fully_initialized);
142 return _init_state < InstanceKlass::being_initialized;
143 }
144 // Is this klass being initialized?
145 bool is_being_initialized() {
146 update_if_shared(InstanceKlass::being_initialized);
147 return _init_state == InstanceKlass::being_initialized;
148 }
149 // Has this klass been linked?
150 bool is_linked() {
151 update_if_shared(InstanceKlass::linked);
152 return _init_state >= InstanceKlass::linked;
153 }
154 // Is this klass in error state?
155 bool is_in_error_state() {
156 update_if_shared(InstanceKlass::initialization_error);
157 return _init_state == InstanceKlass::initialization_error;
158 }
159
160 // General klass information.
161 ciFlags flags() {
162 assert(is_loaded(), "must be loaded");
163 return _flags;
164 }
165 bool has_finalizer() {
166 assert(is_loaded(), "must be loaded");
167 return _has_finalizer; }
168 bool has_subklass() {
169 assert(is_loaded(), "must be loaded");
170 // Ignore cached subklass_false case.
171 // It could be invalidated by concurrent class loading and
172 // can result in type paradoxes during compilation when
173 // a subclass is observed, but has_subklass() returns false.
174 if (_has_subklass == subklass_true) {
175 return true;
176 }
177 if (flags().is_final()) {
178 return false;
179 }
180 return compute_shared_has_subklass();
181 }
182
183 jint layout_helper_size_in_bytes() {
184 return Klass::layout_helper_size_in_bytes(layout_helper());
185 }
186 jint size_helper() {
187 return (Klass::layout_helper_size_in_bytes(layout_helper())
188 >> LogHeapWordSize);
189 }
190 jint has_nonstatic_fields() {
191 assert(is_loaded(), "must be loaded");
192 return _has_nonstatic_fields; }
193 ciInstanceKlass* super();
194 jint nof_implementors() {
195 ciInstanceKlass* impl;
196 assert(is_loaded(), "must be loaded");
197 impl = implementor();
198 if (impl == nullptr) {
199 return 0;
200 } else if (impl != this) {
201 return 1;
202 } else {
203 return 2;
204 }
205 }
206 bool has_nonstatic_concrete_methods() {
207 assert(is_loaded(), "must be loaded");
208 return _has_nonstatic_concrete_methods;
209 }
210
211 bool is_hidden() const {
212 return _is_hidden;
213 }
214
215 bool is_record() const {
216 return _is_record;
217 }
218
219 ciInstanceKlass* get_canonical_holder(int offset);
220 ciField* get_field_by_offset(int field_offset, bool is_static);
221 ciField* get_field_by_name(ciSymbol* name, ciSymbol* signature, bool is_static);
222 // Get field descriptor at field_offset ignoring flattening
223 ciField* get_non_flat_field_by_offset(int field_offset);
224 // Get the index of the declared field that contains this offset
225 int field_index_by_offset(int offset);
226
227 // Total number of nonstatic fields (including inherited)
228 int nof_declared_nonstatic_fields() {
229 if (_declared_nonstatic_fields == nullptr) {
230 compute_nonstatic_fields();
231 }
232 return _declared_nonstatic_fields->length();
233 }
234 BasicType get_field_type_by_offset(int field_offset, bool is_static);
235
236 int nof_nonstatic_fields() {
237 if (_nonstatic_fields == nullptr) {
238 compute_nonstatic_fields();
239 }
240 return _nonstatic_fields->length();
241 }
242
243 bool has_injected_fields() {
244 if (_has_injected_fields == -1) {
245 compute_injected_fields();
246 }
247 return _has_injected_fields > 0 ? true : false;
248 }
249
250 bool has_object_fields() const;
251
252 ciField* declared_nonstatic_field_at(int i) {
253 assert(_declared_nonstatic_fields != nullptr, "should be initialized");
254 return _declared_nonstatic_fields->at(i);
255 }
256
257 ciField* nonstatic_field_at(int i) {
258 assert(_nonstatic_fields != nullptr, "");
259 return _nonstatic_fields->at(i);
260 }
261
262 ciInstanceKlass* unique_concrete_subklass();
263 bool has_finalizable_subclass();
264
265 bool has_class_initializer();
266
267 bool contains_field_offset(int offset);
268
269 // Get the instance of java.lang.Class corresponding to
270 // this klass. This instance is used for locking of
271 // synchronized static methods of this klass.
272 ciInstance* java_mirror();
273
274 // Java access flags
275 bool is_public () { return flags().is_public(); }
276 bool is_final () { return flags().is_final(); }
277 bool is_interface () { return flags().is_interface(); }
278 bool is_abstract () { return flags().is_abstract(); }
279 bool is_abstract_value_klass() { return is_abstract() && !flags().is_identity(); }
280
281 ciMethod* find_method(ciSymbol* name, ciSymbol* signature);
282 // Note: To find a method from name and type strings, use ciSymbol::make,
283 // but consider adding to vmSymbols.hpp instead.
284
285 bool is_leaf_type();
286 ciInstanceKlass* implementor();
287
288 ciInstanceKlass* unique_implementor() {
289 assert(is_loaded(), "must be loaded");
290 assert(is_interface(), "must be");
291 ciInstanceKlass* impl = implementor();
292 return (impl != this ? impl : nullptr);
293 }
294
295 virtual bool can_be_inline_klass(bool is_exact = false);
296
297 // Is the defining class loader of this class the default loader?
298 bool uses_default_loader() const;
299
300 bool is_java_lang_Object() const;
301
302 BasicType box_klass_type() const;
303 bool is_box_klass() const;
304 bool is_boxed_value_offset(int offset) const;
305
306 // Is this klass in the given package?
307 bool is_in_package(const char* packagename) {
308 return is_in_package(packagename, (int) strlen(packagename));
309 }
310 bool is_in_package(const char* packagename, int len);
311
312 // What kind of ciObject is this?
313 bool is_instance_klass() const { return true; }
314
315 virtual ciKlass* exact_klass() {
316 if (is_loaded() && is_final() && !is_interface()) {
317 return this;
318 }
319 return nullptr;
320 }
321
322 bool can_be_instantiated() {
323 assert(is_loaded(), "must be loaded");
324 return !is_interface() && !is_abstract();
325 }
326
327 bool has_trusted_loader() const {
328 return _has_trusted_loader;
329 }
330 GrowableArray<ciInstanceKlass*>* transitive_interfaces() const;
331
332 // Replay support
333
334 // Dump the current state of this klass for compilation replay.
335 virtual void dump_replay_data(outputStream* out);
336
337 static void dump_replay_instanceKlass(outputStream* out, InstanceKlass* ik);
338
339
340 // Return stable class name suitable for replay file.
341 const char *replay_name() const;
342
343 #ifdef ASSERT
344 bool debug_final_field_at(int offset);
345 bool debug_stable_field_at(int offset);
346 #endif
347 };
348
349 #endif // SHARE_CI_CIINSTANCEKLASS_HPP