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   void set_obj(const void* p)         {
107     raw_set_obj(p);
108     CHECK_UNHANDLED_OOPS_ONLY(if (CheckUnhandledOops) register_oop();)
109   }
110   void raw_set_obj(const void* p)     { _o = (oopDesc*)p; }
111 
112   oop()                               { set_obj(NULL); }
113   oop(const oop& o)                   { set_obj(o.obj()); }
114   oop(const volatile oop& o)          { set_obj(o.obj()); }
115   oop(const void* p)                  { set_obj(p); }
116   ~oop()                              {
117     CHECK_UNHANDLED_OOPS_ONLY(if (CheckUnhandledOops) unregister_oop();)
118   }
119 
120   oopDesc* obj()  const volatile      { return _o; }
121 
122   // General access
123   oopDesc*  operator->() const        { return obj(); }
124   bool operator==(const oop o) const  { return obj() == o.obj(); }
125   bool operator==(void *p) const      { return obj() == p; }
126   bool operator!=(const volatile oop o) const  { return obj() != o.obj(); }
127   bool operator!=(void *p) const      { return obj() != p; }
128 
129   // Assignment
130   oop& operator=(const oop& o)                            { _o = o.obj(); return *this; }
131   volatile oop& operator=(const oop& o) volatile          { _o = o.obj(); return *this; }
132   volatile oop& operator=(const volatile oop& o) volatile { _o = o.obj(); return *this; }
133 
134   // Explict user conversions
135   operator void* () const             { return (void *)obj(); }
136 #ifndef SOLARIS
137   operator void* () const volatile    { return (void *)obj(); }
138 #endif
139   operator HeapWord* () const         { return (HeapWord*)obj(); }
140   operator oopDesc* () const volatile { return obj(); }
141   operator intptr_t* () const         { return (intptr_t*)obj(); }
142   operator PromotedObject* () const   { return (PromotedObject*)obj(); }
143   operator markOop () const volatile  { return markOop(obj()); }
144   operator address   () const         { return (address)obj(); }
145 
146   // from javaCalls.cpp
147   operator jobject () const           { return (jobject)obj(); }
148 
149   // from parNewGeneration and other things that want to get to the end of
150   // an oop for stuff (like ObjArrayKlass.cpp)
151   operator oop* () const              { return (oop *)obj(); }
152 };
153 
154 template<>
155 struct PrimitiveConversions::Translate<oop> : public TrueType {
156   typedef oop Value;
157   typedef oopDesc* Decayed;
158 
159   static Decayed decay(Value x) { return x.obj(); }
160   static Value recover(Decayed x) { return oop(x); }
161 };
162 
163 #define DEF_OOP(type)                                                      \
164    class type##OopDesc;                                                    \
165    class type##Oop : public oop {                                          \
166      public:                                                               \
167        type##Oop() : oop() {}                                              \
168        type##Oop(const oop& o) : oop(o) {}                                 \
169        type##Oop(const volatile oop& o) : oop(o) {}                        \
170        type##Oop(const void* p) : oop(p) {}                                \
171        operator type##OopDesc* () const { return (type##OopDesc*)obj(); }  \
172        type##OopDesc* operator->() const {                                 \
173             return (type##OopDesc*)obj();                                  \
174        }                                                                   \
175        type##Oop& operator=(const type##Oop& o) {                          \
176             oop::operator=(o);                                             \
177             return *this;                                                  \
178        }                                                                   \
179        volatile type##Oop& operator=(const type##Oop& o) volatile {        \
180             (void)const_cast<oop&>(oop::operator=(o));                     \
181             return *this;                                                  \
182        }                                                                   \
183        volatile type##Oop& operator=(const volatile type##Oop& o) volatile {\
184             (void)const_cast<oop&>(oop::operator=(o));                     \
185             return *this;                                                  \
186        }                                                                   \
187    };                                                                      \
188                                                                            \
189    template<>                                                              \
190    struct PrimitiveConversions::Translate<type##Oop> : public TrueType {   \
191      typedef type##Oop Value;                                              \
192      typedef type##OopDesc* Decayed;                                       \
193                                                                            \
194      static Decayed decay(Value x) { return (type##OopDesc*)x.obj(); }     \
195      static Value recover(Decayed x) { return type##Oop(x); }              \
196    };
197 
198 DEF_OOP(instance);
199 DEF_OOP(array);
200 DEF_OOP(objArray);
201 DEF_OOP(typeArray);
202 
203 // It is ambiguous C++ behavior to have the oop structure contain explicit
204 // user defined conversions of both numerical and pointer type.
205 // Define inline methods to provide the numerical conversions.
206 template <class T> inline oop cast_to_oop(T value) {
207   return (oop)((void *)(value));
208 }
209 template <class T> inline T cast_from_oop(oop o) {
210   return (T)((void*)o);
211 }
212 
213 // The metadata hierarchy is separate from the oop hierarchy
214 
215 //      class MetaspaceObj
216 class   ConstMethod;
217 class   ConstantPoolCache;
218 class   MethodData;
219 //      class Metadata
220 class   Method;
221 class   ConstantPool;
222 //      class CHeapObj
223 class   CompiledICHolder;
224 
225 
226 // The klass hierarchy is separate from the oop hierarchy.
227 
228 class Klass;
229 class   InstanceKlass;
230 class     InstanceMirrorKlass;
231 class     InstanceClassLoaderKlass;
232 class     InstanceRefKlass;
233 class   ArrayKlass;
234 class     ObjArrayKlass;
235 class     TypeArrayKlass;
236 
237 #endif // SHARE_VM_OOPS_OOPSHIERARCHY_HPP