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/ciObject.hpp"
26 #include "ci/ciUtilities.inline.hpp"
27 #include "gc/shared/collectedHeap.inline.hpp"
28 #include "oops/oop.inline.hpp"
29 #include "runtime/jniHandles.inline.hpp"
30
31 // ciObject
32 //
33 // This class represents an oop in the HotSpot virtual machine.
34 // Its subclasses are structured in a hierarchy which mirrors
35 // an aggregate of the VM's oop and klass hierarchies (see
36 // oopHierarchy.hpp). Each instance of ciObject holds a handle
37 // to a corresponding oop on the VM side and provides routines
38 // for accessing the information in its oop. By using the ciObject
39 // hierarchy for accessing oops in the VM, the compiler ensures
40 // that it is safe with respect to garbage collection; that is,
41 // GC and compilation can proceed independently without
42 // interference.
43 //
44 // Within the VM, the oop and klass hierarchies are separate.
45 // The compiler interface does not preserve this separation --
46 // the distinction between `Klass*' and `Klass' are not
47 // reflected in the interface and instead the Klass hierarchy
48 // is directly modeled as the subclasses of ciKlass.
49
50 // ------------------------------------------------------------------
51 // ciObject::ciObject
52 ciObject::ciObject(oop o) {
53 ASSERT_IN_VM;
54 if (ciObjectFactory::is_initialized()) {
55 _handle = JNIHandles::make_local(o);
56 } else {
57 Handle obj(Thread::current(), o);
58 _handle = JNIHandles::make_global(obj);
59 }
60 _klass = nullptr;
61 assert(oopDesc::is_oop_or_null(o), "Checking");
62 }
63
64 // ------------------------------------------------------------------
65 // ciObject::ciObject
66 //
67 ciObject::ciObject(Handle h) {
68 ASSERT_IN_VM;
69 if (ciObjectFactory::is_initialized()) {
70 _handle = JNIHandles::make_local(h());
71 } else {
72 _handle = JNIHandles::make_global(h);
73 }
74 _klass = nullptr;
75 assert(oopDesc::is_oop_or_null(h()), "Checking");
76 }
77
78 // ------------------------------------------------------------------
79 // ciObject::ciObject
80 //
81 // Unloaded klass/method variant. `klass' is the klass of the unloaded
82 // klass/method, if that makes sense.
83 ciObject::ciObject(ciKlass* klass) {
84 ASSERT_IN_VM;
85 assert(klass != nullptr, "must supply klass");
86 _handle = nullptr;
87 _klass = klass;
88 }
89
90 // ------------------------------------------------------------------
91 // ciObject::ciObject
92 //
93 // null variant. Used only by ciNullObject.
94 ciObject::ciObject() {
95 ASSERT_IN_VM;
96 _handle = nullptr;
97 _klass = nullptr;
98 }
99
100 // ------------------------------------------------------------------
101 // ciObject::get_oop
102 //
103 // Get the oop of this ciObject.
104 oop ciObject::get_oop() const {
105 return JNIHandles::resolve_non_null(_handle);
106 }
107
108 // ------------------------------------------------------------------
109 // ciObject::klass
110 //
111 // Get the ciKlass of this ciObject.
112 ciKlass* ciObject::klass() {
113 if (_klass == nullptr) {
114 if (_handle == nullptr) {
115 // When both _klass and _handle are null, we are dealing
116 // with the distinguished instance of ciNullObject.
117 // No one should ask it for its klass.
118 assert(is_null_object(), "must be null object");
119 ShouldNotReachHere();
120 return nullptr;
121 }
122
123 GUARDED_VM_ENTRY(
124 oop o = get_oop();
125 _klass = CURRENT_ENV->get_klass(o->klass());
126 );
127 }
128 return _klass;
129 }
130
131 // ------------------------------------------------------------------
132 // ciObject::equals
133 //
134 // Are two ciObjects equal?
135 bool ciObject::equals(ciObject* obj) {
136 return (this == obj);
137 }
138
139 // ------------------------------------------------------------------
140 // ciObject::hash
141 //
142 // A hash value for the convenience of compilers.
143 //
144 // Implementation note: we use the address of the ciObject as the
145 // basis for the hash. Use the _ident field, which is well-behaved.
146 uint ciObject::hash() {
147 return ident() * 31;
148 }
149
150 // ------------------------------------------------------------------
151 // ciObject::constant_encoding
152 //
153 // The address which the compiler should embed into the
154 // generated code to represent this oop. This address
155 // is not the true address of the oop -- it will get patched
156 // during nmethod creation.
157 //
158 //
159 //
160 // Implementation note: we use the handle as the encoding. The
161 // nmethod constructor resolves the handle and patches in the oop.
162 //
163 // This method should be changed to return an generified address
164 // to discourage use of the JNI handle.
165 jobject ciObject::constant_encoding() {
166 assert(is_null_object() || handle() != nullptr, "cannot embed null pointer");
167 return handle();
168 }
169
170 // ------------------------------------------------------------------
171 // ciObject::check_constant_value_cache()
172 //
173 // Cache constant value lookups to ensure that consistent values are observed
174 // during compilation because fields may be (re-)initialized concurrently.
175 ciConstant ciObject::check_constant_value_cache(int off, BasicType bt) {
176 if (_constant_values != nullptr) {
177 for (int i = 0; i < _constant_values->length(); ++i) {
178 ConstantValue cached_val = _constant_values->at(i);
179 if (cached_val.off() == off) {
180 assert(cached_val.value().basic_type() == bt, "unexpected type");
181 return cached_val.value();
182 }
183 }
184 }
185 return ciConstant();
186 }
187
188 // ------------------------------------------------------------------
189 // ciObject::add_to_constant_value_cache()
190 //
191 // Add a constant value to the cache.
192 void ciObject::add_to_constant_value_cache(int off, ciConstant val) {
193 assert(val.is_valid(), "value must be valid");
194 assert(!check_constant_value_cache(off, val.basic_type()).is_valid(), "duplicate");
195 if (_constant_values == nullptr) {
196 Arena* arena = CURRENT_ENV->arena();
197 _constant_values = new (arena) GrowableArray<ConstantValue>(arena, 1, 0, ConstantValue());
198 }
199 _constant_values->append(ConstantValue(off, val));
200 }
201
202 // ------------------------------------------------------------------
203 // ciObject::should_be_constant()
204 bool ciObject::should_be_constant() {
205 if (ScavengeRootsInCode >= 2) return true; // force everybody to be a constant
206 if (is_null_object()) return true;
207
208 ciEnv* env = CURRENT_ENV;
209
210 // We want Strings and Classes to be embeddable by default since
211 // they used to be in the perm world. Not all Strings used to be
212 // embeddable but there's no easy way to distinguish the interned
213 // from the regulars ones so just treat them all that way.
214 if (klass() == env->String_klass() || klass() == env->Class_klass()) {
215 return true;
216 }
217 if (klass()->is_subclass_of(env->MethodHandle_klass()) ||
218 klass()->is_subclass_of(env->CallSite_klass())) {
219 // We want to treat these aggressively.
220 return true;
221 }
222
223 return handle() == nullptr;
224 }
225
226 // ------------------------------------------------------------------
227 // ciObject::print
228 //
229 // Print debugging output about this ciObject.
230 //
231 // Implementation note: dispatch to the virtual print_impl behavior
232 // for this ciObject.
233 void ciObject::print(outputStream* st) {
234 st->print("<%s", type_string());
235 GUARDED_VM_ENTRY(print_impl(st);)
236 st->print(" ident=%d address=" INTPTR_FORMAT ">", ident(), p2i(this));
237 }
238
239 // ------------------------------------------------------------------
240 // ciObject::print_oop
241 //
242 // Print debugging output about the oop this ciObject represents.
243 void ciObject::print_oop(outputStream* st) {
244 if (is_null_object()) {
245 st->print_cr("nullptr");
246 } else if (!is_loaded()) {
247 st->print_cr("UNLOADED");
248 } else {
249 GUARDED_VM_ENTRY(get_oop()->print_on(st);)
250 }
251 }