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.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   init_flags_from(o);
  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   init_flags_from(h());
  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   assert(can_be_constant(), "oop must be NULL or perm");
 169   return handle();
 170 }
 171 
 172 // ------------------------------------------------------------------
 173 // ciObject::can_be_constant
 174 bool ciObject::can_be_constant() {
 175   if (ScavengeRootsInCode >= 1)  return true;  // now everybody can encode as a constant
 176   return handle() == NULL;
 177 }
 178 
 179 // ------------------------------------------------------------------
 180 // ciObject::should_be_constant()
 181 bool ciObject::should_be_constant() {
 182   if (ScavengeRootsInCode >= 2)  return true;  // force everybody to be a constant
 183   if (is_null_object()) return true;
 184 
 185   ciEnv* env = CURRENT_ENV;
 186 
 187     // We want Strings and Classes to be embeddable by default since
 188     // they used to be in the perm world.  Not all Strings used to be
 189     // embeddable but there's no easy way to distinguish the interned
 190     // from the regulars ones so just treat them all that way.
 191     if (klass() == env->String_klass() || klass() == env->Class_klass()) {
 192       return true;
 193     }
 194   if (klass()->is_subclass_of(env->MethodHandle_klass()) ||
 195       klass()->is_subclass_of(env->CallSite_klass())) {
 196     assert(ScavengeRootsInCode >= 1, "must be");
 197     // We want to treat these aggressively.
 198     return true;
 199   }
 200 
 201   return handle() == NULL;
 202 }
 203 
 204 // ------------------------------------------------------------------
 205 // ciObject::should_be_constant()
 206 void ciObject::init_flags_from(oop x) {
 207   int flags = 0;
 208   if (x != NULL) {
 209     assert(Universe::heap()->is_in_reserved(x), "must be");
 210     if (x->is_scavengable())
 211       flags |= SCAVENGABLE_FLAG;
 212   }
 213   _ident |= flags;
 214 }
 215 
 216 // ------------------------------------------------------------------
 217 // ciObject::print
 218 //
 219 // Print debugging output about this ciObject.
 220 //
 221 // Implementation note: dispatch to the virtual print_impl behavior
 222 // for this ciObject.
 223 void ciObject::print(outputStream* st) {
 224   st->print("<%s", type_string());
 225   GUARDED_VM_ENTRY(print_impl(st);)
 226   st->print(" ident=%d %s address=" INTPTR_FORMAT ">", ident(),
 227         is_scavengable() ? "SCAVENGABLE" : "",
 228         p2i((address)this));
 229 }
 230 
 231 // ------------------------------------------------------------------
 232 // ciObject::print_oop
 233 //
 234 // Print debugging output about the oop this ciObject represents.
 235 void ciObject::print_oop(outputStream* st) {
 236   if (is_null_object()) {
 237     st->print_cr("NULL");
 238   } else if (!is_loaded()) {
 239     st->print_cr("UNLOADED");
 240   } else {
 241     GUARDED_VM_ENTRY(get_oop()->print_on(st);)
 242   }
 243 }