ciObject.hpp revision 1879:f95d63e2154a
11541Srgrimes/*
21541Srgrimes * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
31541Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
41541Srgrimes *
51541Srgrimes * This code is free software; you can redistribute it and/or modify it
61541Srgrimes * under the terms of the GNU General Public License version 2 only, as
71541Srgrimes * published by the Free Software Foundation.
81541Srgrimes *
91541Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT
101541Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
111541Srgrimes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
121541Srgrimes * version 2 for more details (a copy is included in the LICENSE file that
131541Srgrimes * accompanied this code).
141541Srgrimes *
151541Srgrimes * You should have received a copy of the GNU General Public License version
161541Srgrimes * 2 along with this work; if not, write to the Free Software Foundation,
171541Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
181541Srgrimes *
191541Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
201541Srgrimes * or visit www.oracle.com if you need additional information or have any
211541Srgrimes * questions.
221541Srgrimes *
231541Srgrimes */
241541Srgrimes
251541Srgrimes#ifndef SHARE_VM_CI_CIOBJECT_HPP
261541Srgrimes#define SHARE_VM_CI_CIOBJECT_HPP
271541Srgrimes
281541Srgrimes#include "ci/ciClassList.hpp"
291541Srgrimes#include "memory/allocation.hpp"
301541Srgrimes#include "runtime/handles.hpp"
311541Srgrimes#include "runtime/jniHandles.hpp"
321541Srgrimes
331541Srgrimes// ciObject
341541Srgrimes//
351541Srgrimes// This class represents an oop in the HotSpot virtual machine.
361541Srgrimes// Its subclasses are structured in a hierarchy which mirrors
371541Srgrimes// an aggregate of the VM's oop and klass hierarchies (see
3844510Swollman// oopHierarchy.hpp).  Each instance of ciObject holds a handle
3950477Speter// to a corresponding oop on the VM side and provides routines
401541Srgrimes// for accessing the information in its oop.  By using the ciObject
411541Srgrimes// hierarchy for accessing oops in the VM, the compiler ensures
421541Srgrimes// that it is safe with respect to garbage collection; that is,
431541Srgrimes// GC and compilation can proceed independently without
4433392Sphk// interference.
451541Srgrimes//
4674914Sjhb// Within the VM, the oop and klass hierarchies are separate.
4768840Sjhb// The compiler interface does not preserve this separation --
48115810Sphk// the distinction between `klassOop' and `Klass' are not
491541Srgrimes// reflected in the interface and instead the Klass hierarchy
50115810Sphk// is directly modeled as the subclasses of ciKlass.
51115810Sphkclass ciObject : public ResourceObj {
52115810Sphk  CI_PACKAGE_ACCESS
53115810Sphk  friend class ciEnv;
54115810Sphk
55115810Sphkprivate:
56115810Sphk  // A JNI handle referring to an oop in the VM.  This
57115810Sphk  // handle may, in a small set of cases, correctly be NULL.
58115810Sphk  jobject  _handle;
5933392Sphk  ciKlass* _klass;
6033392Sphk  uint     _ident;
6133392Sphk
6233392Sphk  enum { FLAG_BITS   = 2 };
6333392Sphk  enum {
6433392Sphk         PERM_FLAG        = 1,
6529680Sgibbs         SCAVENGABLE_FLAG = 2
6629680Sgibbs       };
6729680Sgibbsprotected:
6829680Sgibbs  ciObject();
6933392Sphk  ciObject(oop o);
7068889Sjake  ciObject(Handle h);
712112Swollman  ciObject(ciKlass* klass);
7229680Sgibbs
731541Srgrimes  jobject      handle()  const { return _handle; }
741541Srgrimes  // Get the VM oop that this object holds.
7582127Sdillon  oop get_oop() const {
7682127Sdillon    assert(_handle != NULL, "null oop");
7782127Sdillon    return JNIHandles::resolve_non_null(_handle);
7882127Sdillon  }
7982127Sdillon
8082127Sdillon  void init_flags_from(oop x) {
8182127Sdillon    int flags = 0;
8282127Sdillon    if (x != NULL) {
8382127Sdillon      if (x->is_perm())
8482127Sdillon        flags |= PERM_FLAG;
8582127Sdillon      if (x->is_scavengable())
8682127Sdillon        flags |= SCAVENGABLE_FLAG;
8782127Sdillon    }
8882127Sdillon    _ident |= flags;
8982127Sdillon  }
9082127Sdillon
9182127Sdillon  // Virtual behavior of the print() method.
9282127Sdillon  virtual void print_impl(outputStream* st) {}
9382127Sdillon
9482127Sdillon  virtual const char* type_string() { return "ciObject"; }
9582127Sdillon
9682127Sdillon  void set_ident(uint id);
9782127Sdillonpublic:
9882127Sdillon  // The klass of this ciObject.
9982127Sdillon  ciKlass* klass();
10082127Sdillon
10182127Sdillon  // A number unique to this object.
10282127Sdillon  uint ident();
10382127Sdillon
10482127Sdillon  // Are two ciObjects equal?
10582127Sdillon  bool equals(ciObject* obj);
10682127Sdillon
10782127Sdillon  // A hash value for the convenience of compilers.
10882127Sdillon  int hash();
10982127Sdillon
11082127Sdillon  // Tells if this oop has an encoding as a constant.
11182127Sdillon  // True if is_scavengable is false.
11282127Sdillon  // Also true if ScavengeRootsInCode is non-zero.
11382127Sdillon  // If it does not have an encoding, the compiler is responsible for
11482127Sdillon  // making other arrangements for dealing with the oop.
11582127Sdillon  // See ciEnv::make_array
11682127Sdillon  bool can_be_constant();
11782127Sdillon
11882127Sdillon  // Tells if this oop should be made a constant.
11982127Sdillon  // True if is_scavengable is false or ScavengeRootsInCode > 1.
12093818Sjhb  bool should_be_constant();
12182127Sdillon
12282127Sdillon  // Is this object guaranteed to be in the permanent part of the heap?
12382127Sdillon  // If so, CollectedHeap::can_elide_permanent_oop_store_barriers is relevant.
12429680Sgibbs  // If the answer is false, no guarantees are made.
12529680Sgibbs  bool is_perm() { return (_ident & PERM_FLAG) != 0; }
12629680Sgibbs
12729680Sgibbs  // Might this object possibly move during a scavenge operation?
12829680Sgibbs  // If the answer is true and ScavengeRootsInCode==0, the oop cannot be embedded in code.
12929680Sgibbs  bool is_scavengable() { return (_ident & SCAVENGABLE_FLAG) != 0; }
13029680Sgibbs
13129680Sgibbs  // The address which the compiler should embed into the
13229680Sgibbs  // generated code to represent this oop.  This address
13329680Sgibbs  // is not the true address of the oop -- it will get patched
13432388Sphk  // during nmethod creation.
13529680Sgibbs  //
1361541Srgrimes  // Usage note: no address arithmetic allowed.  Oop must
1371541Srgrimes  // be registered with the oopRecorder.
1381541Srgrimes  jobject constant_encoding();
1391541Srgrimes
14067551Sjhb  // What kind of ciObject is this?
1411541Srgrimes  virtual bool is_null_object() const       { return false; }
142102936Sphk  virtual bool is_call_site() const         { return false; }
143102936Sphk  virtual bool is_cpcache() const           { return false; }
144102936Sphk  virtual bool is_instance()                { return false; }
145102936Sphk  virtual bool is_method()                  { return false; }
146115810Sphk  virtual bool is_method_data()             { return false; }
147115810Sphk  virtual bool is_method_handle() const     { return false; }
148115810Sphk  virtual bool is_array()                   { return false; }
149102936Sphk  virtual bool is_obj_array()               { return false; }
150102936Sphk  virtual bool is_type_array()              { return false; }
151102936Sphk  virtual bool is_symbol()                  { return false; }
152102936Sphk  virtual bool is_type()                    { return false; }
153102936Sphk  virtual bool is_return_address()          { return false; }
1541541Srgrimes  virtual bool is_klass()                   { return false; }
15533392Sphk  virtual bool is_instance_klass()          { return false; }
15633392Sphk  virtual bool is_method_klass()            { return false; }
15733392Sphk  virtual bool is_array_klass()             { return false; }
15829680Sgibbs  virtual bool is_obj_array_klass()         { return false; }
159115810Sphk  virtual bool is_type_array_klass()        { return false; }
160115810Sphk  virtual bool is_symbol_klass()            { return false; }
161115810Sphk  virtual bool is_klass_klass()             { return false; }
16229680Sgibbs  virtual bool is_instance_klass_klass()    { return false; }
16372200Sbmilekic  virtual bool is_array_klass_klass()       { return false; }
16429680Sgibbs  virtual bool is_obj_array_klass_klass()   { return false; }
16529805Sgibbs  virtual bool is_type_array_klass_klass()  { return false; }
16629805Sgibbs
16729805Sgibbs  // Is this a type or value which has no associated class?
16829805Sgibbs  // It is true of primitive types and null objects.
16929805Sgibbs  virtual bool is_classless() const         { return false; }
17029805Sgibbs
17129805Sgibbs  // Is this ciObject a Java Language Object?  That is,
17229805Sgibbs  // is the ciObject an instance or an array
17329680Sgibbs  virtual bool is_java_object()             { return false; }
174115810Sphk
17529805Sgibbs  // Does this ciObject represent a Java Language class?
17629680Sgibbs  // That is, is the ciObject an instanceKlass or arrayKlass?
17729680Sgibbs  virtual bool is_java_klass()              { return false; }
17829680Sgibbs
17929680Sgibbs  // Is this ciObject the ciInstanceKlass representing
18029805Sgibbs  // java.lang.Object()?
18172200Sbmilekic  virtual bool is_java_lang_Object()        { return false; }
18281370Sjhb
18372200Sbmilekic  // Does this ciObject refer to a real oop in the VM?
18429680Sgibbs  //
18529680Sgibbs  // Note: some ciObjects refer to oops which have yet to be
18629680Sgibbs  // created.  We refer to these as "unloaded".  Specifically,
18729680Sgibbs  // there are unloaded ciMethods, ciObjArrayKlasses, and
18829680Sgibbs  // ciInstanceKlasses.  By convention the ciNullObject is
18929680Sgibbs  // considered loaded, and primitive types are considered loaded.
19068889Sjake  bool is_loaded() const {
19129680Sgibbs    return handle() != NULL || is_classless();
19229680Sgibbs  }
19329805Sgibbs
19429680Sgibbs  // Subclass casting with assertions.
19529680Sgibbs  ciNullObject*            as_null_object() {
19668889Sjake    assert(is_null_object(), "bad cast");
19729680Sgibbs    return (ciNullObject*)this;
19844510Swollman  }
19944510Swollman  ciCallSite*              as_call_site() {
20044510Swollman    assert(is_call_site(), "bad cast");
20144510Swollman    return (ciCallSite*) this;
20244510Swollman  }
20344510Swollman  ciCPCache*               as_cpcache() {
20450673Sjlemon    assert(is_cpcache(), "bad cast");
20544510Swollman    return (ciCPCache*) this;
20672200Sbmilekic  }
207115810Sphk  ciInstance*              as_instance() {
20872200Sbmilekic    assert(is_instance(), "bad cast");
209115810Sphk    return (ciInstance*)this;
210115810Sphk  }
211115810Sphk  ciMethod*                as_method() {
212115810Sphk    assert(is_method(), "bad cast");
213102936Sphk    return (ciMethod*)this;
214102936Sphk  }
215102936Sphk  ciMethodData*            as_method_data() {
21629680Sgibbs    assert(is_method_data(), "bad cast");
217102936Sphk    return (ciMethodData*)this;
218102936Sphk  }
219102936Sphk  ciMethodHandle*          as_method_handle() {
220102936Sphk    assert(is_method_handle(), "bad cast");
221110185Sphk    return (ciMethodHandle*) this;
222102936Sphk  }
223102936Sphk  ciArray*                 as_array() {
224110185Sphk    assert(is_array(), "bad cast");
225102936Sphk    return (ciArray*)this;
226102936Sphk  }
227102936Sphk  ciObjArray*              as_obj_array() {
228102936Sphk    assert(is_obj_array(), "bad cast");
22968889Sjake    return (ciObjArray*)this;
23072200Sbmilekic  }
23172200Sbmilekic  ciTypeArray*             as_type_array() {
23229680Sgibbs    assert(is_type_array(), "bad cast");
23329680Sgibbs    return (ciTypeArray*)this;
23429680Sgibbs  }
23529680Sgibbs  ciSymbol*                as_symbol() {
2361541Srgrimes    assert(is_symbol(), "bad cast");
237115810Sphk    return (ciSymbol*)this;
238115810Sphk  }
239115810Sphk  ciType*                  as_type() {
24029680Sgibbs    assert(is_type(), "bad cast");
24172200Sbmilekic    return (ciType*)this;
2421541Srgrimes  }
2431541Srgrimes  ciReturnAddress*         as_return_address() {
2441541Srgrimes    assert(is_return_address(), "bad cast");
2451541Srgrimes    return (ciReturnAddress*)this;
2461541Srgrimes  }
2471541Srgrimes  ciKlass*                 as_klass() {
2481541Srgrimes    assert(is_klass(), "bad cast");
2491541Srgrimes    return (ciKlass*)this;
2501541Srgrimes  }
25129680Sgibbs  ciInstanceKlass*         as_instance_klass() {
25229680Sgibbs    assert(is_instance_klass(), "bad cast");
25329680Sgibbs    return (ciInstanceKlass*)this;
2541541Srgrimes  }
25529680Sgibbs  ciMethodKlass*           as_method_klass() {
25629680Sgibbs    assert(is_method_klass(), "bad cast");
25729680Sgibbs    return (ciMethodKlass*)this;
25829680Sgibbs  }
2591541Srgrimes  ciArrayKlass*            as_array_klass() {
26029680Sgibbs    assert(is_array_klass(), "bad cast");
26129680Sgibbs    return (ciArrayKlass*)this;
26233824Sbde  }
2631541Srgrimes  ciObjArrayKlass*         as_obj_array_klass() {
26469147Sjlemon    assert(is_obj_array_klass(), "bad cast");
2651541Srgrimes    return (ciObjArrayKlass*)this;
26629680Sgibbs  }
26729680Sgibbs  ciTypeArrayKlass*        as_type_array_klass() {
2681541Srgrimes    assert(is_type_array_klass(), "bad cast");
26972200Sbmilekic    return (ciTypeArrayKlass*)this;
2701541Srgrimes  }
2711541Srgrimes  ciSymbolKlass*           as_symbol_klass() {
27229680Sgibbs    assert(is_symbol_klass(), "bad cast");
27329680Sgibbs    return (ciSymbolKlass*)this;
27429680Sgibbs  }
2751541Srgrimes  ciKlassKlass*            as_klass_klass() {
27629680Sgibbs    assert(is_klass_klass(), "bad cast");
27744510Swollman    return (ciKlassKlass*)this;
27844510Swollman  }
2791541Srgrimes  ciInstanceKlassKlass*    as_instance_klass_klass() {
28044510Swollman    assert(is_instance_klass_klass(), "bad cast");
28172200Sbmilekic    return (ciInstanceKlassKlass*)this;
28229680Sgibbs  }
2831541Srgrimes  ciArrayKlassKlass*       as_array_klass_klass() {
2841541Srgrimes    assert(is_array_klass_klass(), "bad cast");
2851541Srgrimes    return (ciArrayKlassKlass*)this;
28629680Sgibbs  }
28733824Sbde  ciObjArrayKlassKlass*    as_obj_array_klass_klass() {
2881541Srgrimes    assert(is_obj_array_klass_klass(), "bad cast");
28929680Sgibbs    return (ciObjArrayKlassKlass*)this;
2901541Srgrimes  }
2911541Srgrimes  ciTypeArrayKlassKlass*   as_type_array_klass_klass() {
29229680Sgibbs    assert(is_type_array_klass_klass(), "bad cast");
29329680Sgibbs    return (ciTypeArrayKlassKlass*)this;
29429680Sgibbs  }
29529680Sgibbs
29629680Sgibbs  // Print debugging output about this ciObject.
29729680Sgibbs  void print(outputStream* st = tty);
29829680Sgibbs
29929680Sgibbs  // Print debugging output about the oop this ciObject represents.
30072200Sbmilekic  void print_oop(outputStream* st = tty);
30144510Swollman};
30244510Swollman
30372200Sbmilekic#endif // SHARE_VM_CI_CIOBJECT_HPP
3041541Srgrimes