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 #include "ci/ciKlass.hpp"
26 #include "ci/ciSymbol.hpp"
27 #include "ci/ciUtilities.inline.hpp"
28 #include "oops/klass.inline.hpp"
29 #include "oops/oop.inline.hpp"
30
31 // ciKlass
32 //
33 // This class represents a Klass* in the HotSpot virtual
34 // machine.
35
36 // ------------------------------------------------------------------
37 // ciKlass::ciKlass
38 ciKlass::ciKlass(Klass* k) : ciType(k) {
39 assert(get_Klass()->is_klass(), "wrong type");
40 Klass* klass = get_Klass();
41 _layout_helper = klass->layout_helper();
42 Symbol* klass_name = klass->name();
43 assert(klass_name != nullptr, "wrong ciKlass constructor");
44 _name = CURRENT_ENV->get_symbol(klass_name);
45 }
46
47 // ------------------------------------------------------------------
48 // ciKlass::ciKlass
49 //
50 // Nameless klass variant.
51 ciKlass::ciKlass(Klass* k, ciSymbol* name) : ciType(k) {
52 assert(get_Klass()->is_klass(), "wrong type");
53 _name = name;
54 _layout_helper = Klass::_lh_neutral_value;
55 }
56
57 // ------------------------------------------------------------------
58 // ciKlass::ciKlass
59 //
60 // Unloaded klass variant.
61 ciKlass::ciKlass(ciSymbol* name, BasicType bt) : ciType(bt) {
62 _name = name;
63 _layout_helper = Klass::_lh_neutral_value;
64 }
65
66 // ------------------------------------------------------------------
67 // ciKlass::is_subtype_of
68 bool ciKlass::is_subtype_of(ciKlass* that) {
69 assert(this->is_loaded(), "must be loaded: %s", this->name()->as_quoted_ascii());
70 assert(that->is_loaded(), "must be loaded: %s", that->name()->as_quoted_ascii());
71
72 // Check to see if the klasses are identical.
73 if (this == that) {
74 return true;
75 }
76
77 bool is_subtype;
78 GUARDED_VM_ENTRY(is_subtype = get_Klass()->is_subtype_of(that->get_Klass());)
79
80 // Ensure consistency with ciInstanceKlass::has_subklass().
81 assert(!that->is_instance_klass() || // array klasses are irrelevant
82 that->is_interface() || // has_subklass is always false for interfaces
83 !is_subtype || that->as_instance_klass()->has_subklass(), "inconsistent");
84
85 return is_subtype;
86 }
87
88 // ------------------------------------------------------------------
89 // ciKlass::is_subclass_of
90 bool ciKlass::is_subclass_of(ciKlass* that) {
91 assert(this->is_loaded(), "must be loaded: %s", this->name()->as_quoted_ascii());
92 assert(that->is_loaded(), "must be loaded: %s", that->name()->as_quoted_ascii());
93
94 // Check to see if the klasses are identical.
95 if (this == that) {
96 return true;
97 }
98
99 bool is_subclass;
100 GUARDED_VM_ENTRY(is_subclass = get_Klass()->is_subclass_of(that->get_Klass());)
101
102 // Ensure consistency with ciInstanceKlass::has_subklass().
103 assert(!that->is_instance_klass() || // array klasses are irrelevant
104 that->is_interface() || // has_subklass is always false for interfaces
105 !is_subclass || that->as_instance_klass()->has_subklass(), "inconsistent");
106
107 return is_subclass;
108 }
109
110 // ------------------------------------------------------------------
111 // ciKlass::super_depth
112 juint ciKlass::super_depth() {
113 assert(is_loaded(), "must be loaded");
114
115 VM_ENTRY_MARK;
116 Klass* this_klass = get_Klass();
117 return this_klass->super_depth();
118 }
119
120 // ------------------------------------------------------------------
121 // ciKlass::super_check_offset
122 juint ciKlass::super_check_offset() {
123 assert(is_loaded(), "must be loaded");
124
125 VM_ENTRY_MARK;
126 Klass* this_klass = get_Klass();
127 return this_klass->super_check_offset();
128 }
129
130 // ------------------------------------------------------------------
131 // ciKlass::super_of_depth
132 ciKlass* ciKlass::super_of_depth(juint i) {
133 assert(is_loaded(), "must be loaded");
134
135 VM_ENTRY_MARK;
136 Klass* this_klass = get_Klass();
137 Klass* super = this_klass->primary_super_of_depth(i);
138 return (super != nullptr) ? CURRENT_THREAD_ENV->get_klass(super) : nullptr;
139 }
140
141 // ------------------------------------------------------------------
142 // ciKlass::least_common_ancestor
143 //
144 // Get the shared parent of two klasses.
145 //
146 // Implementation note: this method currently goes "over the wall"
147 // and does all of the work on the VM side. It could be rewritten
148 // to use the super() method and do all of the work (aside from the
149 // lazy computation of super()) in native mode. This may be
150 // worthwhile if the compiler is repeatedly requesting the same lca
151 // computation or possibly if most of the superklasses have already
152 // been created as ciObjects anyway. Something to think about...
153 ciKlass*
154 ciKlass::least_common_ancestor(ciKlass* that) {
155 assert(is_loaded() && that->is_loaded(), "must be loaded");
156 // Check to see if the klasses are identical.
157 if (this == that) {
158 return this;
159 }
160
161 VM_ENTRY_MARK;
162 Klass* this_klass = get_Klass();
163 Klass* that_klass = that->get_Klass();
164 Klass* lca = this_klass->LCA(that_klass);
165
166 // Many times the LCA will be either this_klass or that_klass.
167 // Treat these as special cases.
168 if (lca == that_klass) {
169 assert(this->is_subtype_of(that), "sanity");
170 return that;
171 }
172 if (this_klass == lca) {
173 assert(that->is_subtype_of(this), "sanity");
174 return this;
175 }
176
177 // Create the ciInstanceKlass for the lca.
178 ciKlass* result =
179 CURRENT_THREAD_ENV->get_klass(lca);
180
181 assert(this->is_subtype_of(result) && that->is_subtype_of(result), "sanity");
182 return result;
183 }
184
185 // ------------------------------------------------------------------
186 // ciKlass::find_klass
187 //
188 // Find a klass using this klass's class loader.
189 ciKlass* ciKlass::find_klass(ciSymbol* klass_name) {
190 assert(is_loaded(), "cannot find_klass through an unloaded klass");
191 return CURRENT_ENV->get_klass_by_name(this,
192 klass_name, false);
193 }
194
195 // ------------------------------------------------------------------
196 // ciKlass::java_mirror
197 //
198 // Get the instance of java.lang.Class corresponding to this klass.
199 // If it is an unloaded instance or array klass, return an unloaded
200 // mirror object of type Class.
201 ciInstance* ciKlass::java_mirror() {
202 GUARDED_VM_ENTRY(
203 if (!is_loaded())
204 return ciEnv::current()->get_unloaded_klass_mirror(this);
205 oop java_mirror = get_Klass()->java_mirror();
206 return CURRENT_ENV->get_instance(java_mirror);
207 )
208 }
209
210 // ------------------------------------------------------------------
211 // ciKlass::modifier_flags
212 jint ciKlass::modifier_flags() {
213 assert(is_loaded(), "not loaded");
214 GUARDED_VM_ENTRY(
215 return get_Klass()->modifier_flags();
216 )
217 }
218
219 // ------------------------------------------------------------------
220 // ciKlass::access_flags
221 jint ciKlass::access_flags() {
222 assert(is_loaded(), "not loaded");
223 GUARDED_VM_ENTRY(
224 return get_Klass()->access_flags().as_unsigned_short();
225 )
226 }
227
228 // ------------------------------------------------------------------
229 // ciKlass::misc_flags
230 klass_flags_t ciKlass::misc_flags() {
231 assert(is_loaded(), "not loaded");
232 GUARDED_VM_ENTRY(
233 return get_Klass()->misc_flags();
234 )
235 }
236
237 // ------------------------------------------------------------------
238 // ciKlass::print_impl
239 //
240 // Implementation of the print method
241 void ciKlass::print_impl(outputStream* st) {
242 st->print(" name=");
243 print_name_on(st);
244 st->print(" loaded=%s", (is_loaded() ? "true" : "false"));
245 }
246
247 // ------------------------------------------------------------------
248 // ciKlass::print_name
249 //
250 // Print the name of this klass
251 void ciKlass::print_name_on(outputStream* st) {
252 name()->print_symbol_on(st);
253 }
254
255 const char* ciKlass::external_name() const {
256 GUARDED_VM_ENTRY(
257 return get_Klass()->external_name();
258 )
259 }
260
261 // ------------------------------------------------------------------
262 // ciKlass::prototype_header_offset
263 juint ciKlass::prototype_header_offset() {
264 assert(is_loaded(), "must be loaded");
265
266 VM_ENTRY_MARK;
267 Klass* this_klass = get_Klass();
268 return in_bytes(this_klass->prototype_header_offset());
269 }
270
271 // ------------------------------------------------------------------
272 // ciKlass::prototype_header
273 uintptr_t ciKlass::prototype_header() {
274 assert(is_loaded(), "must be loaded");
275
276 VM_ENTRY_MARK;
277 Klass* this_klass = get_Klass();
278 return (uintptr_t)this_klass->prototype_header().to_pointer();
279 }
--- EOF ---