1 /*
  2  * Copyright (c) 1997, 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 #ifndef SHARE_VM_OOPS_OOPSHIERARCHY_HPP
 26 #define SHARE_VM_OOPS_OOPSHIERARCHY_HPP
 27 
 28 #include "metaprogramming/integralConstant.hpp"
 29 #include "metaprogramming/primitiveConversions.hpp"
 30 #include "runtime/globals.hpp"
 31 #include "utilities/globalDefinitions.hpp"
 32 #include "utilities/macros.hpp"
 33 
 34 // OBJECT hierarchy
 35 // This hierarchy is a representation hierarchy, i.e. if A is a superclass
 36 // of B, A's representation is a prefix of B's representation.
 37 
 38 // Compressed oop class
 39 class narrowOop {
 40 public:
 41   typedef uint32_t PrimitiveType;
 42 
 43 private:
 44   PrimitiveType _value;
 45 
 46 public:
 47   narrowOop() : _value(0) {}
 48   narrowOop(const narrowOop& o) : _value(o._value) {}
 49   narrowOop(const volatile narrowOop& o) : _value(o._value) {}
 50   narrowOop(const PrimitiveType value) : _value(value) {}
 51 
 52   PrimitiveType value() { return _value; }
 53 
 54   bool operator==(const narrowOop o) const { return _value == o._value; }
 55   bool operator!=(const narrowOop o) const { return _value != o._value; }
 56 
 57   narrowOop& operator=(const narrowOop o) { _value = o._value; return *this; }
 58 
 59   operator PrimitiveType () const { return _value; }
 60 };
 61 
 62 template<>
 63 struct PrimitiveConversions::Translate<narrowOop> : public TrueType {
 64   typedef narrowOop Value;
 65   typedef narrowOop::PrimitiveType Decayed;
 66 
 67   static Decayed decay(Value x) { return x.value(); }
 68   static Value recover(Decayed x) { return narrowOop(x); }
 69 };
 70 
 71 // If compressed klass pointers then use narrowKlass.
 72 typedef juint  narrowKlass;
 73 
 74 typedef void* OopOrNarrowOopStar;
 75 typedef class   markOopDesc*                markOop;
 76 
 77 // Because oop and its subclasses <type>Oop are class types, arbitrary
 78 // conversions are not accepted by the compiler.  Applying a cast to
 79 // an oop will cause the best matched conversion operator to be
 80 // invoked returning the underlying oopDesc* type if appropriate.
 81 // No copy constructors, explicit user conversions or operators of
 82 // numerical type should be defined within the oop class. Most C++
 83 // compilers will issue a compile time error concerning the overloading
 84 // ambiguity between operators of numerical and pointer types. If
 85 // a conversion to or from an oop to a numerical type is needed,
 86 // use the inline template methods, cast_*_oop, defined below.
 87 //
 88 // Converting NULL to oop to Handle implicit is no longer accepted by the
 89 // compiler because there are too many steps in the conversion.  Use Handle()
 90 // instead, which generates less code anyway.
 91 
 92 class Thread;
 93 class PromotedObject;
 94 
 95 
 96 class oop {
 97   oopDesc* _o;
 98 
 99 #ifdef CHECK_UNHANDLED_OOPS
100   void register_oop();
101   void unregister_oop();
102 #endif
103 
104   // friend class markOop;
105 public:
106   oop() : _o(NULL)                      { CHECK_UNHANDLED_OOPS_ONLY(if (CheckUnhandledOops) register_oop();) }
107   oop(const oop& o) : _o(o._o)          { CHECK_UNHANDLED_OOPS_ONLY(if (CheckUnhandledOops) register_oop();) }
108   oop(const volatile oop& o) : _o(o._o) { CHECK_UNHANDLED_OOPS_ONLY(if (CheckUnhandledOops) register_oop();) }
109   oop(const void* p) : _o((oopDesc*)p)  { CHECK_UNHANDLED_OOPS_ONLY(if (CheckUnhandledOops) register_oop();) }
110   ~oop()                                {
111     CHECK_UNHANDLED_OOPS_ONLY(if (CheckUnhandledOops) unregister_oop();)
112   }
113 
114   oopDesc* obj() const                { return _o; }
115 
116   // General access
117   oopDesc*  operator->() const        { return _o; }
118   bool operator==(const oop o) const  { return _o == o._o; }
119   bool operator==(void *p) const      { return _o == p; }
120   bool operator!=(const oop o) const  { return _o != o._o; }
121   bool operator!=(void *p) const      { return _o != p; }
122 
123   // Assignment
124   oop& operator=(const oop& o)                            { _o = o._o; return *this; }
125   volatile oop& operator=(const oop& o) volatile          { _o = o._o; return *this; }
126   volatile oop& operator=(const volatile oop& o) volatile { _o = o._o; return *this; }
127 
128   // Explict user conversions
129   operator void* () const             { return (void *)_o; }
130   operator HeapWord* () const         { return (HeapWord*)_o; }
131   operator oopDesc* () const          { return _o; }
132   operator intptr_t* () const         { return (intptr_t*)_o; }
133   operator PromotedObject* () const   { return (PromotedObject*)_o; }
134   operator markOop () const           { return markOop(_o); }
135   operator address () const           { return (address)_o; }
136 
137   // from javaCalls.cpp
138   operator jobject () const           { return (jobject)_o; }
139 
140   // from parNewGeneration and other things that want to get to the end of
141   // an oop for stuff (like ObjArrayKlass.cpp)
142   operator oop* () const              { return (oop *)_o; }
143 };
144 
145 template<>
146 struct PrimitiveConversions::Translate<oop> : public TrueType {
147   typedef oop Value;
148   typedef oopDesc* Decayed;
149 
150   static Decayed decay(Value x) { return x.obj(); }
151   static Value recover(Decayed x) { return oop(x); }
152 };
153 
154 #define DEF_OOP(type)                                                      \
155    class type##OopDesc;                                                    \
156    class type##Oop : public oop {                                          \
157      public:                                                               \
158        type##Oop() : oop() {}                                              \
159        type##Oop(const oop& o) : oop(o) {}                                 \
160        type##Oop(const volatile oop& o) : oop(o) {}                        \
161        type##Oop(const void* p) : oop(p) {}                                \
162        operator type##OopDesc* () const { return (type##OopDesc*)obj(); }  \
163        type##OopDesc* operator->() const {                                 \
164             return (type##OopDesc*)obj();                                  \
165        }                                                                   \
166        type##Oop& operator=(const type##Oop& o) {                          \
167             oop::operator=(o);                                             \
168             return *this;                                                  \
169        }                                                                   \
170        volatile type##Oop& operator=(const type##Oop& o) volatile {        \
171             (void)const_cast<oop&>(oop::operator=(o));                     \
172             return *this;                                                  \
173        }                                                                   \
174        volatile type##Oop& operator=(const volatile type##Oop& o) volatile {\
175             (void)const_cast<oop&>(oop::operator=(o));                     \
176             return *this;                                                  \
177        }                                                                   \
178    };                                                                      \
179                                                                            \
180    template<>                                                              \
181    struct PrimitiveConversions::Translate<type##Oop> : public TrueType {   \
182      typedef type##Oop Value;                                              \
183      typedef type##OopDesc* Decayed;                                       \
184                                                                            \
185      static Decayed decay(Value x) { return (type##OopDesc*)x.obj(); }     \
186      static Value recover(Decayed x) { return type##Oop(x); }              \
187    };
188 
189 DEF_OOP(instance);
190 DEF_OOP(array);
191 DEF_OOP(objArray);
192 DEF_OOP(typeArray);
193 
194 // It is ambiguous C++ behavior to have the oop structure contain explicit
195 // user defined conversions of both numerical and pointer type.
196 // Define inline methods to provide the numerical conversions.
197 template <class T> inline oop cast_to_oop(T value) {
198   return (oop)((void *)(value));
199 }
200 template <class T> inline T cast_from_oop(oop o) {
201   return (T)((void*)o);
202 }
203 
204 // The metadata hierarchy is separate from the oop hierarchy
205 
206 //      class MetaspaceObj
207 class   ConstMethod;
208 class   ConstantPoolCache;
209 class   MethodData;
210 //      class Metadata
211 class   Method;
212 class   ConstantPool;
213 //      class CHeapObj
214 class   CompiledICHolder;
215 
216 
217 // The klass hierarchy is separate from the oop hierarchy.
218 
219 class Klass;
220 class   InstanceKlass;
221 class     InstanceMirrorKlass;
222 class     InstanceClassLoaderKlass;
223 class     InstanceRefKlass;
224 class   ArrayKlass;
225 class     ObjArrayKlass;
226 class     TypeArrayKlass;
227 
228 #endif // SHARE_VM_OOPS_OOPSHIERARCHY_HPP