1 /*
   2  * Copyright (c) 1999, 2017, 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 
  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 = NULL;
  61   init_flags_from(o);
  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 = NULL;
  75   init_flags_from(h());
  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 != NULL, "must supply klass");
  86   _handle = NULL;
  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 = NULL;
  97   _klass = NULL;
  98 }
  99 
 100 // ------------------------------------------------------------------
 101 // ciObject::klass
 102 //
 103 // Get the ciKlass of this ciObject.
 104 ciKlass* ciObject::klass() {
 105   if (_klass == NULL) {
 106     if (_handle == NULL) {
 107       // When both _klass and _handle are NULL, we are dealing
 108       // with the distinguished instance of ciNullObject.
 109       // No one should ask it for its klass.
 110       assert(is_null_object(), "must be null object");
 111       ShouldNotReachHere();
 112       return NULL;
 113     }
 114 
 115     GUARDED_VM_ENTRY(
 116       oop o = get_oop();
 117       _klass = CURRENT_ENV->get_klass(o->klass());
 118     );
 119   }
 120   return _klass;
 121 }
 122 
 123 // ------------------------------------------------------------------
 124 // ciObject::equals
 125 //
 126 // Are two ciObjects equal?
 127 bool ciObject::equals(ciObject* obj) {
 128   return (this == obj);
 129 }
 130 
 131 // ------------------------------------------------------------------
 132 // ciObject::hash
 133 //
 134 // A hash value for the convenience of compilers.
 135 //
 136 // Implementation note: we use the address of the ciObject as the
 137 // basis for the hash.  Use the _ident field, which is well-behaved.
 138 int ciObject::hash() {
 139   return ident() * 31;
 140 }
 141 
 142 // ------------------------------------------------------------------
 143 // ciObject::constant_encoding
 144 //
 145 // The address which the compiler should embed into the
 146 // generated code to represent this oop.  This address
 147 // is not the true address of the oop -- it will get patched
 148 // during nmethod creation.
 149 //
 150 //
 151 //
 152 // Implementation note: we use the handle as the encoding.  The
 153 // nmethod constructor resolves the handle and patches in the oop.
 154 //
 155 // This method should be changed to return an generified address
 156 // to discourage use of the JNI handle.
 157 jobject ciObject::constant_encoding() {
 158   assert(is_null_object() || handle() != NULL, "cannot embed null pointer");
 159   assert(can_be_constant(), "oop must be NULL or perm");
 160   return handle();
 161 }
 162 
 163 // ------------------------------------------------------------------
 164 // ciObject::can_be_constant
 165 bool ciObject::can_be_constant() {
 166   if (ScavengeRootsInCode >= 1)  return true;  // now everybody can encode as a constant
 167   return handle() == NULL;
 168 }
 169 
 170 // ------------------------------------------------------------------
 171 // ciObject::should_be_constant()
 172 bool ciObject::should_be_constant() {
 173   if (ScavengeRootsInCode >= 2)  return true;  // force everybody to be a constant
 174   if (is_null_object()) return true;
 175 
 176   ciEnv* env = CURRENT_ENV;
 177 
 178     // We want Strings and Classes to be embeddable by default since
 179     // they used to be in the perm world.  Not all Strings used to be
 180     // embeddable but there's no easy way to distinguish the interned
 181     // from the regulars ones so just treat them all that way.
 182     if (klass() == env->String_klass() || klass() == env->Class_klass()) {
 183       return true;
 184     }
 185   if (klass()->is_subclass_of(env->MethodHandle_klass()) ||
 186       klass()->is_subclass_of(env->CallSite_klass())) {
 187     assert(ScavengeRootsInCode >= 1, "must be");
 188     // We want to treat these aggressively.
 189     return true;
 190   }
 191 
 192   return handle() == NULL;
 193 }
 194 
 195 // ------------------------------------------------------------------
 196 // ciObject::should_be_constant()
 197 void ciObject::init_flags_from(oop x) {
 198   int flags = 0;
 199   if (x != NULL) {
 200     assert(GC::gc()->heap()->is_in_reserved(x), "must be");
 201     if (x->is_scavengable())
 202       flags |= SCAVENGABLE_FLAG;
 203   }
 204   _ident |= flags;
 205 }
 206 
 207 // ------------------------------------------------------------------
 208 // ciObject::print
 209 //
 210 // Print debugging output about this ciObject.
 211 //
 212 // Implementation note: dispatch to the virtual print_impl behavior
 213 // for this ciObject.
 214 void ciObject::print(outputStream* st) {
 215   st->print("<%s", type_string());
 216   GUARDED_VM_ENTRY(print_impl(st);)
 217   st->print(" ident=%d %s address=" INTPTR_FORMAT ">", ident(),
 218         is_scavengable() ? "SCAVENGABLE" : "",
 219         p2i((address)this));
 220 }
 221 
 222 // ------------------------------------------------------------------
 223 // ciObject::print_oop
 224 //
 225 // Print debugging output about the oop this ciObject represents.
 226 void ciObject::print_oop(outputStream* st) {
 227   if (is_null_object()) {
 228     st->print_cr("NULL");
 229   } else if (!is_loaded()) {
 230     st->print_cr("UNLOADED");
 231   } else {
 232     GUARDED_VM_ENTRY(get_oop()->print_on(st);)
 233   }
 234 }