ciObject.cpp revision 1472:c18cbe5936b8
1/*
2 * Copyright (c) 1999, 2007, 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#include "incls/_precompiled.incl"
26#include "incls/_ciObject.cpp.incl"
27
28// ciObject
29//
30// This class represents an oop in the HotSpot virtual machine.
31// Its subclasses are structured in a hierarchy which mirrors
32// an aggregate of the VM's oop and klass hierarchies (see
33// oopHierarchy.hpp).  Each instance of ciObject holds a handle
34// to a corresponding oop on the VM side and provides routines
35// for accessing the information in its oop.  By using the ciObject
36// hierarchy for accessing oops in the VM, the compiler ensures
37// that it is safe with respect to garbage collection; that is,
38// GC and compilation can proceed independently without
39// interference.
40//
41// Within the VM, the oop and klass hierarchies are separate.
42// The compiler interface does not preserve this separation --
43// the distinction between `klassOop' and `Klass' are not
44// reflected in the interface and instead the Klass hierarchy
45// is directly modeled as the subclasses of ciKlass.
46
47// ------------------------------------------------------------------
48// ciObject::ciObject
49ciObject::ciObject(oop o) {
50  ASSERT_IN_VM;
51  if (ciObjectFactory::is_initialized()) {
52    _handle = JNIHandles::make_local(o);
53  } else {
54    _handle = JNIHandles::make_global(o);
55  }
56  _klass = NULL;
57  _ident = 0;
58  init_flags_from(o);
59}
60
61// ------------------------------------------------------------------
62// ciObject::ciObject
63//
64ciObject::ciObject(Handle h) {
65  ASSERT_IN_VM;
66  if (ciObjectFactory::is_initialized()) {
67    _handle = JNIHandles::make_local(h());
68  } else {
69    _handle = JNIHandles::make_global(h);
70  }
71  _klass = NULL;
72  _ident = 0;
73  init_flags_from(h());
74}
75
76// ------------------------------------------------------------------
77// ciObject::ciObject
78//
79// Unloaded klass/method variant.  `klass' is the klass of the unloaded
80// klass/method, if that makes sense.
81ciObject::ciObject(ciKlass* klass) {
82  ASSERT_IN_VM;
83  assert(klass != NULL, "must supply klass");
84  _handle = NULL;
85  _klass = klass;
86  _ident = 0;
87}
88
89// ------------------------------------------------------------------
90// ciObject::ciObject
91//
92// NULL variant.  Used only by ciNullObject.
93ciObject::ciObject() {
94  ASSERT_IN_VM;
95  _handle = NULL;
96  _klass = NULL;
97  _ident = 0;
98}
99
100// ------------------------------------------------------------------
101// ciObject::klass
102//
103// Get the ciKlass of this ciObject.
104ciKlass* ciObject::klass() {
105  if (_klass == NULL) {
106    if (_handle == NULL) {
107      // When both _klass and _handle are NULL, we are dealing
108      // with the distinguished instance of ciNullObject.
109      // No one should ask it for its klass.
110      assert(is_null_object(), "must be null object");
111      ShouldNotReachHere();
112      return NULL;
113    }
114
115    GUARDED_VM_ENTRY(
116      oop o = get_oop();
117      _klass = CURRENT_ENV->get_object(o->klass())->as_klass();
118    );
119  }
120  return _klass;
121}
122
123// ------------------------------------------------------------------
124// ciObject::set_ident
125//
126// Set the unique identity number of a ciObject.
127void ciObject::set_ident(uint id) {
128  assert((_ident >> FLAG_BITS) == 0, "must only initialize once");
129  assert( id < ((uint)1 << (BitsPerInt-FLAG_BITS)), "id too big");
130  _ident = _ident + (id << FLAG_BITS);
131}
132
133// ------------------------------------------------------------------
134// ciObject::ident
135//
136// Report the unique identity number of a ciObject.
137uint ciObject::ident() {
138  uint id = _ident >> FLAG_BITS;
139  assert(id != 0, "must be initialized");
140  return id;
141}
142
143// ------------------------------------------------------------------
144// ciObject::equals
145//
146// Are two ciObjects equal?
147bool ciObject::equals(ciObject* obj) {
148  return (this == obj);
149}
150
151// ------------------------------------------------------------------
152// ciObject::hash
153//
154// A hash value for the convenience of compilers.
155//
156// Implementation note: we use the address of the ciObject as the
157// basis for the hash.  Use the _ident field, which is well-behaved.
158int ciObject::hash() {
159  return ident() * 31;
160}
161
162// ------------------------------------------------------------------
163// ciObject::constant_encoding
164//
165// The address which the compiler should embed into the
166// generated code to represent this oop.  This address
167// is not the true address of the oop -- it will get patched
168// during nmethod creation.
169//
170//
171//
172// Implementation note: we use the handle as the encoding.  The
173// nmethod constructor resolves the handle and patches in the oop.
174//
175// This method should be changed to return an generified address
176// to discourage use of the JNI handle.
177jobject ciObject::constant_encoding() {
178  assert(is_null_object() || handle() != NULL, "cannot embed null pointer");
179  assert(can_be_constant(), "oop must be NULL or perm");
180  return handle();
181}
182
183// ------------------------------------------------------------------
184// ciObject::can_be_constant
185bool ciObject::can_be_constant() {
186  if (ScavengeRootsInCode >= 1)  return true;  // now everybody can encode as a constant
187  return handle() == NULL || !is_scavengable();
188}
189
190// ------------------------------------------------------------------
191// ciObject::should_be_constant()
192bool ciObject::should_be_constant() {
193  if (ScavengeRootsInCode >= 2)  return true;  // force everybody to be a constant
194  return handle() == NULL || !is_scavengable();
195}
196
197
198// ------------------------------------------------------------------
199// ciObject::print
200//
201// Print debugging output about this ciObject.
202//
203// Implementation note: dispatch to the virtual print_impl behavior
204// for this ciObject.
205void ciObject::print(outputStream* st) {
206  st->print("<%s", type_string());
207  GUARDED_VM_ENTRY(print_impl(st);)
208  st->print(" ident=%d %s%s address=0x%x>", ident(),
209        is_perm() ? "PERM" : "",
210        is_scavengable() ? "SCAVENGABLE" : "",
211        (address)this);
212}
213
214// ------------------------------------------------------------------
215// ciObject::print_oop
216//
217// Print debugging output about the oop this ciObject represents.
218void ciObject::print_oop(outputStream* st) {
219  if (is_null_object()) {
220    st->print_cr("NULL");
221  } else if (!is_loaded()) {
222    st->print_cr("UNLOADED");
223  } else {
224    GUARDED_VM_ENTRY(get_oop()->print_on(st);)
225  }
226}
227