comparison jvmci/com.oracle.jvmci.hotspot/src/com/oracle/jvmci/hotspot/HotSpotObjectConstantImpl.java @ 21798:395ac43a8578

moved JVMCI sources from graal/ to jvmci/ directory
author Doug Simon <doug.simon@oracle.com>
date Tue, 09 Jun 2015 00:22:49 +0200
parents graal/com.oracle.jvmci.hotspot/src/com/oracle/jvmci/hotspot/HotSpotObjectConstantImpl.java@48c1ebd24120
children
comparison
equal deleted inserted replaced
21797:42452d2dfbec 21798:395ac43a8578
1 /*
2 * Copyright (c) 2009, 2014, 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 package com.oracle.jvmci.hotspot;
24
25 import static com.oracle.jvmci.hotspot.HotSpotResolvedObjectTypeImpl.*;
26
27 import java.lang.invoke.*;
28
29 import com.oracle.jvmci.meta.*;
30
31 import edu.umd.cs.findbugs.annotations.*;
32
33 /**
34 * Represents a constant non-{@code null} object reference, within the compiler and across the
35 * compiler/runtime interface.
36 */
37 public final class HotSpotObjectConstantImpl extends AbstractValue implements HotSpotObjectConstant, HotSpotProxified {
38
39 public static JavaConstant forObject(Object object) {
40 return forObject(object, false);
41 }
42
43 static JavaConstant forObject(Object object, boolean compressed) {
44 if (object == null) {
45 return compressed ? HotSpotCompressedNullConstant.COMPRESSED_NULL : JavaConstant.NULL_POINTER;
46 } else {
47 return new HotSpotObjectConstantImpl(object, compressed);
48 }
49 }
50
51 static JavaConstant forStableArray(Object object, int stableDimension, boolean isDefaultStable) {
52 if (object == null) {
53 return JavaConstant.NULL_POINTER;
54 } else {
55 assert object.getClass().isArray();
56 return new HotSpotObjectConstantImpl(object, false, stableDimension, isDefaultStable);
57 }
58 }
59
60 public static JavaConstant forBoxedValue(Kind kind, Object value) {
61 if (kind == Kind.Object) {
62 return HotSpotObjectConstantImpl.forObject(value);
63 } else {
64 return JavaConstant.forBoxedPrimitive(value);
65 }
66 }
67
68 static Object asBoxedValue(Constant constant) {
69 if (JavaConstant.isNull(constant)) {
70 return null;
71 } else if (constant instanceof HotSpotObjectConstantImpl) {
72 return ((HotSpotObjectConstantImpl) constant).object;
73 } else {
74 return ((JavaConstant) constant).asBoxedPrimitive();
75 }
76 }
77
78 private final Object object;
79 private final boolean compressed;
80 private final byte stableDimension;
81 private final boolean isDefaultStable;
82
83 private HotSpotObjectConstantImpl(Object object, boolean compressed, int stableDimension, boolean isDefaultStable) {
84 super(LIRKind.reference(compressed ? Kind.Int : Kind.Object));
85 this.object = object;
86 this.compressed = compressed;
87 this.stableDimension = (byte) stableDimension;
88 this.isDefaultStable = isDefaultStable;
89 assert object != null;
90 assert stableDimension == 0 || (object != null && object.getClass().isArray());
91 assert stableDimension >= 0 && stableDimension <= 255;
92 assert !isDefaultStable || stableDimension > 0;
93 }
94
95 private HotSpotObjectConstantImpl(Object object, boolean compressed) {
96 this(object, compressed, 0, false);
97 }
98
99 /**
100 * Package-private accessor for the object represented by this constant.
101 */
102 Object object() {
103 return object;
104 }
105
106 /**
107 * Determines if the object represented by this constant is {@link Object#equals(Object) equal}
108 * to a given object.
109 */
110 public boolean isEqualTo(Object obj) {
111 return object.equals(obj);
112 }
113
114 /**
115 * Gets the class of the object represented by this constant.
116 */
117 public Class<?> getObjectClass() {
118 return object.getClass();
119 }
120
121 public boolean isCompressed() {
122 return compressed;
123 }
124
125 public JavaConstant compress() {
126 assert !compressed;
127 return new HotSpotObjectConstantImpl(object, true, stableDimension, isDefaultStable);
128 }
129
130 public JavaConstant uncompress() {
131 assert compressed;
132 return new HotSpotObjectConstantImpl(object, false, stableDimension, isDefaultStable);
133 }
134
135 public HotSpotResolvedObjectType getType() {
136 return fromObjectClass(object.getClass());
137 }
138
139 public JavaConstant getClassLoader() {
140 if (object instanceof Class) {
141 /*
142 * This is an intrinsic for getClassLoader0, which occurs after any security checks. We
143 * can't call that directly so just call getClassLoader.
144 */
145 return HotSpotObjectConstantImpl.forObject(((Class<?>) object).getClassLoader());
146 }
147 return null;
148 }
149
150 public int getIdentityHashCode() {
151 return System.identityHashCode(object);
152 }
153
154 public JavaConstant getComponentType() {
155 if (object instanceof Class) {
156 return HotSpotObjectConstantImpl.forObject(((Class<?>) object).getComponentType());
157 }
158 return null;
159 }
160
161 public JavaConstant getSuperclass() {
162 if (object instanceof Class) {
163 return HotSpotObjectConstantImpl.forObject(((Class<?>) object).getSuperclass());
164 }
165 return null;
166 }
167
168 public JavaConstant getCallSiteTarget(Assumptions assumptions) {
169 if (object instanceof CallSite) {
170 CallSite callSite = (CallSite) object;
171 MethodHandle target = callSite.getTarget();
172 if (!(callSite instanceof ConstantCallSite)) {
173 if (assumptions == null) {
174 return null;
175 }
176 assumptions.record(new Assumptions.CallSiteTargetValue(callSite, target));
177 }
178 return HotSpotObjectConstantImpl.forObject(target);
179 }
180 return null;
181 }
182
183 @SuppressFBWarnings(value = "ES_COMPARING_STRINGS_WITH_EQ", justification = "reference equality is what we want")
184 public boolean isInternedString() {
185 if (object instanceof String) {
186 String s = (String) object;
187 return s.intern() == s;
188 }
189 return false;
190 }
191
192 public <T> T asObject(Class<T> type) {
193 if (type.isInstance(object)) {
194 return type.cast(object);
195 }
196 return null;
197 }
198
199 public Object asObject(ResolvedJavaType type) {
200 if (type.isInstance(this)) {
201 return object;
202 }
203 return null;
204 }
205
206 @Override
207 public boolean isNull() {
208 return false;
209 }
210
211 @Override
212 public boolean isDefaultForKind() {
213 return false;
214 }
215
216 @Override
217 public Object asBoxedPrimitive() {
218 throw new IllegalArgumentException();
219 }
220
221 @Override
222 public int asInt() {
223 throw new IllegalArgumentException();
224 }
225
226 @Override
227 public boolean asBoolean() {
228 throw new IllegalArgumentException();
229 }
230
231 @Override
232 public long asLong() {
233 throw new IllegalArgumentException();
234 }
235
236 @Override
237 public float asFloat() {
238 throw new IllegalArgumentException();
239 }
240
241 @Override
242 public double asDouble() {
243 throw new IllegalArgumentException();
244 }
245
246 @Override
247 public int hashCode() {
248 return System.identityHashCode(object);
249 }
250
251 @Override
252 public boolean equals(Object o) {
253 if (o == this) {
254 return true;
255 } else if (o instanceof HotSpotObjectConstantImpl) {
256 HotSpotObjectConstantImpl other = (HotSpotObjectConstantImpl) o;
257 return super.equals(o) && object == other.object && compressed == other.compressed && stableDimension == other.stableDimension && isDefaultStable == other.isDefaultStable;
258 }
259 return false;
260 }
261
262 @Override
263 public String toValueString() {
264 if (object instanceof String) {
265 return (String) object;
266 } else {
267 return Kind.Object.format(object);
268 }
269 }
270
271 @Override
272 public String toString() {
273 return (compressed ? "NarrowOop" : getKind().getJavaName()) + "[" + Kind.Object.format(object) + "]";
274 }
275
276 /**
277 * Number of stable dimensions if this constant is a stable array.
278 */
279 public int getStableDimension() {
280 return stableDimension & 0xff;
281 }
282
283 /**
284 * Returns {@code true} if this is a stable array constant and its elements should be considered
285 * as stable regardless of whether they are default values.
286 */
287 public boolean isDefaultStable() {
288 return isDefaultStable;
289 }
290 }