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