ciObject.hpp revision 0:a61af66fc99e
1/*
2 * Copyright 1999-2006 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25// ciObject
26//
27// This class represents an oop in the HotSpot virtual machine.
28// Its subclasses are structured in a hierarchy which mirrors
29// an aggregate of the VM's oop and klass hierarchies (see
30// oopHierarchy.hpp).  Each instance of ciObject holds a handle
31// to a corresponding oop on the VM side and provides routines
32// for accessing the information in its oop.  By using the ciObject
33// hierarchy for accessing oops in the VM, the compiler ensures
34// that it is safe with respect to garbage collection; that is,
35// GC and compilation can proceed independently without
36// interference.
37//
38// Within the VM, the oop and klass hierarchies are separate.
39// The compiler interface does not preserve this separation --
40// the distinction between `klassOop' and `Klass' are not
41// reflected in the interface and instead the Klass hierarchy
42// is directly modeled as the subclasses of ciKlass.
43class ciObject : public ResourceObj {
44  CI_PACKAGE_ACCESS
45  friend class ciEnv;
46
47private:
48  // A JNI handle referring to an oop in the VM.  This
49  // handle may, in a small set of cases, correctly be NULL.
50  jobject  _handle;
51  ciKlass* _klass;
52  uint     _ident;
53
54  enum { FLAG_BITS   = 1};
55  enum {
56         PERM_FLAG    = 1
57       };
58protected:
59  ciObject();
60  ciObject(oop o);
61  ciObject(Handle h);
62  ciObject(ciKlass* klass);
63
64  jobject      handle()  const { return _handle; }
65  // Get the VM oop that this object holds.
66  oop get_oop() const {
67    assert(_handle != NULL, "null oop");
68    return JNIHandles::resolve_non_null(_handle);
69  }
70
71  void set_perm() {
72    _ident |=  PERM_FLAG;
73  }
74
75  // Virtual behavior of the print() method.
76  virtual void print_impl(outputStream* st) {}
77
78  virtual const char* type_string() { return "ciObject"; }
79
80  void set_ident(uint id);
81public:
82  // The klass of this ciObject.
83  ciKlass* klass();
84
85  // A number unique to this object.
86  uint ident();
87
88  // Are two ciObjects equal?
89  bool equals(ciObject* obj);
90
91  // A hash value for the convenience of compilers.
92  int hash();
93
94  // Tells if this oop has an encoding.  (I.e., is it null or perm?)
95  // If it does not have an encoding, the compiler is responsible for
96  // making other arrangements for dealing with the oop.
97  // See ciEnv::make_perm_array
98  bool has_encoding();
99
100  // Is this object guaranteed to be in the permanent part of the heap?
101  // If so, CollectedHeap::can_elide_permanent_oop_store_barriers is relevant.
102  // If the answer is false, no guarantees are made.
103  bool is_perm() { return (_ident & PERM_FLAG) != 0; }
104
105  // The address which the compiler should embed into the
106  // generated code to represent this oop.  This address
107  // is not the true address of the oop -- it will get patched
108  // during nmethod creation.
109  //
110  // Usage note: no address arithmetic allowed.  Oop must
111  // be registered with the oopRecorder.
112  jobject encoding();
113
114  // What kind of ciObject is this?
115  virtual bool is_null_object() const       { return false; }
116  virtual bool is_instance()                { return false; }
117  virtual bool is_method()                  { return false; }
118  virtual bool is_method_data()             { return false; }
119  virtual bool is_array()                   { return false; }
120  virtual bool is_obj_array()               { return false; }
121  virtual bool is_type_array()              { return false; }
122  virtual bool is_symbol()                  { return false; }
123  virtual bool is_type()                    { return false; }
124  virtual bool is_return_address()          { return false; }
125  virtual bool is_klass()                   { return false; }
126  virtual bool is_instance_klass()          { return false; }
127  virtual bool is_method_klass()            { return false; }
128  virtual bool is_array_klass()             { return false; }
129  virtual bool is_obj_array_klass()         { return false; }
130  virtual bool is_type_array_klass()        { return false; }
131  virtual bool is_symbol_klass()            { return false; }
132  virtual bool is_klass_klass()             { return false; }
133  virtual bool is_instance_klass_klass()    { return false; }
134  virtual bool is_array_klass_klass()       { return false; }
135  virtual bool is_obj_array_klass_klass()   { return false; }
136  virtual bool is_type_array_klass_klass()  { return false; }
137
138  // Is this a type or value which has no associated class?
139  // It is true of primitive types and null objects.
140  virtual bool is_classless() const         { return false; }
141
142  // Is this ciObject a Java Language Object?  That is,
143  // is the ciObject an instance or an array
144  virtual bool is_java_object()             { return false; }
145
146  // Does this ciObject represent a Java Language class?
147  // That is, is the ciObject an instanceKlass or arrayKlass?
148  virtual bool is_java_klass()              { return false; }
149
150  // Is this ciObject the ciInstanceKlass representing
151  // java.lang.Object()?
152  virtual bool is_java_lang_Object()        { return false; }
153
154  // Does this ciObject refer to a real oop in the VM?
155  //
156  // Note: some ciObjects refer to oops which have yet to be
157  // created.  We refer to these as "unloaded".  Specifically,
158  // there are unloaded ciMethods, ciObjArrayKlasses, and
159  // ciInstanceKlasses.  By convention the ciNullObject is
160  // considered loaded, and primitive types are considered loaded.
161  bool is_loaded() const {
162    return handle() != NULL || is_classless();
163  }
164
165  // Subclass casting with assertions.
166  ciNullObject*            as_null_object() {
167    assert(is_null_object(), "bad cast");
168    return (ciNullObject*)this;
169  }
170  ciInstance*              as_instance() {
171    assert(is_instance(), "bad cast");
172    return (ciInstance*)this;
173  }
174  ciMethod*                as_method() {
175    assert(is_method(), "bad cast");
176    return (ciMethod*)this;
177  }
178  ciMethodData*            as_method_data() {
179    assert(is_method_data(), "bad cast");
180    return (ciMethodData*)this;
181  }
182  ciArray*                 as_array() {
183    assert(is_array(), "bad cast");
184    return (ciArray*)this;
185  }
186  ciObjArray*              as_obj_array() {
187    assert(is_obj_array(), "bad cast");
188    return (ciObjArray*)this;
189  }
190  ciTypeArray*             as_type_array() {
191    assert(is_type_array(), "bad cast");
192    return (ciTypeArray*)this;
193  }
194  ciSymbol*                as_symbol() {
195    assert(is_symbol(), "bad cast");
196    return (ciSymbol*)this;
197  }
198  ciType*                  as_type() {
199    assert(is_type(), "bad cast");
200    return (ciType*)this;
201  }
202  ciReturnAddress*         as_return_address() {
203    assert(is_return_address(), "bad cast");
204    return (ciReturnAddress*)this;
205  }
206  ciKlass*                 as_klass() {
207    assert(is_klass(), "bad cast");
208    return (ciKlass*)this;
209  }
210  ciInstanceKlass*         as_instance_klass() {
211    assert(is_instance_klass(), "bad cast");
212    return (ciInstanceKlass*)this;
213  }
214  ciMethodKlass*           as_method_klass() {
215    assert(is_method_klass(), "bad cast");
216    return (ciMethodKlass*)this;
217  }
218  ciArrayKlass*            as_array_klass() {
219    assert(is_array_klass(), "bad cast");
220    return (ciArrayKlass*)this;
221  }
222  ciObjArrayKlass*         as_obj_array_klass() {
223    assert(is_obj_array_klass(), "bad cast");
224    return (ciObjArrayKlass*)this;
225  }
226  ciTypeArrayKlass*        as_type_array_klass() {
227    assert(is_type_array_klass(), "bad cast");
228    return (ciTypeArrayKlass*)this;
229  }
230  ciSymbolKlass*           as_symbol_klass() {
231    assert(is_symbol_klass(), "bad cast");
232    return (ciSymbolKlass*)this;
233  }
234  ciKlassKlass*            as_klass_klass() {
235    assert(is_klass_klass(), "bad cast");
236    return (ciKlassKlass*)this;
237  }
238  ciInstanceKlassKlass*    as_instance_klass_klass() {
239    assert(is_instance_klass_klass(), "bad cast");
240    return (ciInstanceKlassKlass*)this;
241  }
242  ciArrayKlassKlass*       as_array_klass_klass() {
243    assert(is_array_klass_klass(), "bad cast");
244    return (ciArrayKlassKlass*)this;
245  }
246  ciObjArrayKlassKlass*    as_obj_array_klass_klass() {
247    assert(is_obj_array_klass_klass(), "bad cast");
248    return (ciObjArrayKlassKlass*)this;
249  }
250  ciTypeArrayKlassKlass*   as_type_array_klass_klass() {
251    assert(is_type_array_klass_klass(), "bad cast");
252    return (ciTypeArrayKlassKlass*)this;
253  }
254
255  // Print debugging output about this ciObject.
256  void print(outputStream* st = tty);
257
258  // Print debugging output about the oop this ciObject represents.
259  void print_oop(outputStream* st = tty);
260};
261