1 /*
   2  * Copyright (c) 1999, 2012, 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 #ifndef SHARE_VM_CI_CIOBJECT_HPP
  26 #define SHARE_VM_CI_CIOBJECT_HPP
  27 
  28 #include "ci/ciBaseObject.hpp"
  29 #include "ci/ciClassList.hpp"
  30 #include "gc/shared/barrierSet.hpp"
  31 #include "memory/allocation.hpp"
  32 #include "runtime/handles.hpp"
  33 #include "runtime/jniHandles.hpp"
  34 
  35 // ciObject
  36 //
  37 // This class represents an oop in the HotSpot virtual machine.
  38 // Its subclasses are structured in a hierarchy which mirrors
  39 // an aggregate of the VM's oop and klass hierarchies (see
  40 // oopHierarchy.hpp).  Each instance of ciObject holds a handle
  41 // to a corresponding oop on the VM side and provides routines
  42 // for accessing the information in its oop.  By using the ciObject
  43 // hierarchy for accessing oops in the VM, the compiler ensures
  44 // that it is safe with respect to garbage collection; that is,
  45 // GC and compilation can proceed independently without
  46 // interference.
  47 //
  48 // Within the VM, the oop and klass hierarchies are separate.
  49 // The compiler interface does not preserve this separation --
  50 // the distinction between `Klass*' and `Klass' are not
  51 // reflected in the interface and instead the Klass hierarchy
  52 // is directly modeled as the subclasses of ciKlass.
  53 class ciObject : public ciBaseObject {
  54   CI_PACKAGE_ACCESS
  55   friend class ciEnv;
  56 
  57 private:
  58   // A JNI handle referring to an oop in the VM.  This
  59   // handle may, in a small set of cases, correctly be NULL.
  60   jobject  _handle;
  61   ciKlass* _klass;
  62 
  63 protected:
  64   ciObject();
  65   ciObject(oop o);
  66   ciObject(Handle h);
  67   ciObject(ciKlass* klass);
  68 
  69   jobject      handle()  const { return _handle; }
  70   // Get the VM oop that this object holds.
  71   oop get_oop() const {
  72     assert(_handle != NULL, "null oop");
  73     oop obj = JNIHandles::resolve_non_null(_handle);
  74     assert(obj == oopDesc::bs()->resolve_and_maybe_copy_oop(obj), "expect to-space copy");
  75     return obj;
  76   }
  77 
  78   void init_flags_from(oop x);
  79 
  80   // Virtual behavior of the print() method.
  81   virtual void print_impl(outputStream* st) {}
  82 
  83   virtual const char* type_string() { return "ciObject"; }
  84 
  85 public:
  86   // The klass of this ciObject.
  87   ciKlass* klass();
  88 
  89   // Are two ciObjects equal?
  90   bool equals(ciObject* obj);
  91 
  92   // A hash value for the convenience of compilers.
  93   int hash();
  94 
  95   // Tells if this oop has an encoding as a constant.
  96   // True if is_perm is true.
  97   // Also true if ScavengeRootsInCode is non-zero.
  98   // If it does not have an encoding, the compiler is responsible for
  99   // making other arrangements for dealing with the oop.
 100   // See ciEnv::make_array
 101   bool can_be_constant();
 102 
 103   // Tells if this oop should be made a constant.
 104   // True if is_perm is true or ScavengeRootsInCode > 1.
 105   bool should_be_constant();
 106 
 107   // Might this object possibly move during a scavenge operation?
 108   // If the answer is true and ScavengeRootsInCode==0, the oop cannot be embedded in code.
 109   bool is_scavengable() { return (_ident & SCAVENGABLE_FLAG) != 0; }
 110 
 111   // The address which the compiler should embed into the
 112   // generated code to represent this oop.  This address
 113   // is not the true address of the oop -- it will get patched
 114   // during nmethod creation.
 115   //
 116   // Usage note: no address arithmetic allowed.  Oop must
 117   // be registered with the oopRecorder.
 118   jobject constant_encoding();
 119 
 120   virtual bool is_object() const            { return true; }
 121 
 122   // What kind of ciObject is this?
 123   virtual bool is_null_object()       const { return false; }
 124   virtual bool is_call_site()         const { return false; }
 125   virtual bool is_cpcache()           const { return false; }
 126   virtual bool is_instance()                { return false; }
 127   virtual bool is_member_name()       const { return false; }
 128   virtual bool is_method_handle()     const { return false; }
 129   virtual bool is_method_type()       const { return false; }
 130   virtual bool is_array()                   { return false; }
 131   virtual bool is_obj_array()               { return false; }
 132   virtual bool is_type_array()              { return false; }
 133 
 134   // Is this a type or value which has no associated class?
 135   // It is true of primitive types and null objects.
 136   virtual bool is_classless() const         { return false; }
 137   virtual void dump_replay_data(outputStream* st) { /* do nothing */ }
 138 
 139   // Note: some ciObjects refer to oops which have yet to be created.
 140   // We refer to these as "unloaded".  Specifically, there are
 141   // unloaded instances of java.lang.Class,
 142   // java.lang.invoke.MethodHandle, and java.lang.invoke.MethodType.
 143   // By convention the ciNullObject is considered loaded, and
 144   // primitive types are considered loaded.
 145   bool is_loaded() const {
 146     return handle() != NULL || is_classless();
 147   }
 148 
 149   // Subclass casting with assertions.
 150   ciNullObject* as_null_object() {
 151     assert(is_null_object(), "bad cast");
 152     return (ciNullObject*)this;
 153   }
 154   ciCallSite* as_call_site() {
 155     assert(is_call_site(), "bad cast");
 156     return (ciCallSite*)this;
 157   }
 158   ciInstance* as_instance() {
 159     assert(is_instance(), "bad cast");
 160     return (ciInstance*)this;
 161   }
 162   ciMemberName* as_member_name() {
 163     assert(is_member_name(), "bad cast");
 164     return (ciMemberName*)this;
 165   }
 166   ciMethodHandle* as_method_handle() {
 167     assert(is_method_handle(), "bad cast");
 168     return (ciMethodHandle*)this;
 169   }
 170   ciMethodType* as_method_type() {
 171     assert(is_method_type(), "bad cast");
 172     return (ciMethodType*)this;
 173   }
 174   ciArray* as_array() {
 175     assert(is_array(), "bad cast");
 176     return (ciArray*)this;
 177   }
 178   ciObjArray* as_obj_array() {
 179     assert(is_obj_array(), "bad cast");
 180     return (ciObjArray*)this;
 181   }
 182   ciTypeArray* as_type_array() {
 183     assert(is_type_array(), "bad cast");
 184     return (ciTypeArray*)this;
 185   }
 186 
 187   // Print debugging output about this ciObject.
 188   void print(outputStream* st);
 189   void print() { print(tty); }  // GDB cannot handle default arguments
 190 
 191   // Print debugging output about the oop this ciObject represents.
 192   void print_oop(outputStream* st = tty);
 193 };
 194 
 195 #endif // SHARE_VM_CI_CIOBJECT_HPP