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