| 1 | /* |
| 2 | * Copyright (c) 1999, 2018, 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 = NULL; |
| 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 = NULL; |
| 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 != NULL, "must supply klass" ); |
| 87 | _handle = NULL; |
| 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 = NULL; |
| 98 | _klass = NULL; |
| 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 == NULL) { |
| 115 | if (_handle == NULL) { |
| 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 NULL; |
| 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 | int 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() != NULL, "cannot embed null pointer" ); |
| 168 | return handle(); |
| 169 | } |
| 170 | |
| 171 | // ------------------------------------------------------------------ |
| 172 | // ciObject::should_be_constant() |
| 173 | bool ciObject::should_be_constant() { |
| 174 | if (ScavengeRootsInCode >= 2) return true; // force everybody to be a constant |
| 175 | if (is_null_object()) return true; |
| 176 | |
| 177 | ciEnv* env = CURRENT_ENV; |
| 178 | |
| 179 | // We want Strings and Classes to be embeddable by default since |
| 180 | // they used to be in the perm world. Not all Strings used to be |
| 181 | // embeddable but there's no easy way to distinguish the interned |
| 182 | // from the regulars ones so just treat them all that way. |
| 183 | if (klass() == env->String_klass() || klass() == env->Class_klass()) { |
| 184 | return true; |
| 185 | } |
| 186 | if (klass()->is_subclass_of(env->MethodHandle_klass()) || |
| 187 | klass()->is_subclass_of(env->CallSite_klass())) { |
| 188 | // We want to treat these aggressively. |
| 189 | return true; |
| 190 | } |
| 191 | |
| 192 | return handle() == NULL; |
| 193 | } |
| 194 | |
| 195 | // ------------------------------------------------------------------ |
| 196 | // ciObject::print |
| 197 | // |
| 198 | // Print debugging output about this ciObject. |
| 199 | // |
| 200 | // Implementation note: dispatch to the virtual print_impl behavior |
| 201 | // for this ciObject. |
| 202 | void ciObject::print(outputStream* st) { |
| 203 | st->print("<%s" , type_string()); |
| 204 | GUARDED_VM_ENTRY(print_impl(st);) |
| 205 | st->print(" ident=%d address=" INTPTR_FORMAT ">" , ident(), p2i(this)); |
| 206 | } |
| 207 | |
| 208 | // ------------------------------------------------------------------ |
| 209 | // ciObject::print_oop |
| 210 | // |
| 211 | // Print debugging output about the oop this ciObject represents. |
| 212 | void ciObject::print_oop(outputStream* st) { |
| 213 | if (is_null_object()) { |
| 214 | st->print_cr("NULL" ); |
| 215 | } else if (!is_loaded()) { |
| 216 | st->print_cr("UNLOADED" ); |
| 217 | } else { |
| 218 | GUARDED_VM_ENTRY(get_oop()->print_on(st);) |
| 219 | } |
| 220 | } |
| 221 | |