Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.max.cri/src/com/oracle/max/cri/ri/RiConstant.java @ 5502:13aee5aba8cc
Moved RiConstant to cri.ri package.
author | Thomas Wuerthinger <thomas.wuerthinger@oracle.com> |
---|---|
date | Thu, 07 Jun 2012 17:07:42 +0200 |
parents | graal/com.oracle.max.cri/src/com/oracle/max/cri/ci/RiConstant.java@d89b20486d87 |
children | 438ab53efdd0 |
comparison
equal
deleted
inserted
replaced
5501:d89b20486d87 | 5502:13aee5aba8cc |
---|---|
1 /* | |
2 * Copyright (c) 2009, 2012, 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.max.cri.ri; | |
24 | |
25 import com.oracle.max.cri.ci.*; | |
26 | |
27 /** | |
28 * Represents a constant (boxed) value, such as an integer, floating point number, or object reference, | |
29 * within the compiler and across the compiler/runtime interface. Exports a set of {@code CiConstant} | |
30 * instances that represent frequently used constant values, such as {@link #ZERO}. | |
31 */ | |
32 public final class RiConstant extends CiValue { | |
33 private static final long serialVersionUID = -6355452536852663986L; | |
34 | |
35 private static final RiConstant[] INT_CONSTANT_CACHE = new RiConstant[100]; | |
36 static { | |
37 for (int i = 0; i < INT_CONSTANT_CACHE.length; ++i) { | |
38 INT_CONSTANT_CACHE[i] = new RiConstant(CiKind.Int, i); | |
39 } | |
40 } | |
41 | |
42 public static final RiConstant NULL_OBJECT = new RiConstant(CiKind.Object, null); | |
43 public static final RiConstant INT_MINUS_1 = new RiConstant(CiKind.Int, -1); | |
44 public static final RiConstant INT_0 = forInt(0); | |
45 public static final RiConstant INT_1 = forInt(1); | |
46 public static final RiConstant INT_2 = forInt(2); | |
47 public static final RiConstant INT_3 = forInt(3); | |
48 public static final RiConstant INT_4 = forInt(4); | |
49 public static final RiConstant INT_5 = forInt(5); | |
50 public static final RiConstant LONG_0 = new RiConstant(CiKind.Long, 0L); | |
51 public static final RiConstant LONG_1 = new RiConstant(CiKind.Long, 1L); | |
52 public static final RiConstant FLOAT_0 = new RiConstant(CiKind.Float, Float.floatToRawIntBits(0.0F)); | |
53 public static final RiConstant FLOAT_1 = new RiConstant(CiKind.Float, Float.floatToRawIntBits(1.0F)); | |
54 public static final RiConstant FLOAT_2 = new RiConstant(CiKind.Float, Float.floatToRawIntBits(2.0F)); | |
55 public static final RiConstant DOUBLE_0 = new RiConstant(CiKind.Double, Double.doubleToRawLongBits(0.0D)); | |
56 public static final RiConstant DOUBLE_1 = new RiConstant(CiKind.Double, Double.doubleToRawLongBits(1.0D)); | |
57 public static final RiConstant TRUE = new RiConstant(CiKind.Boolean, 1L); | |
58 public static final RiConstant FALSE = new RiConstant(CiKind.Boolean, 0L); | |
59 | |
60 static { | |
61 assert NULL_OBJECT.isDefaultValue(); | |
62 assert INT_0.isDefaultValue(); | |
63 assert FLOAT_0.isDefaultValue(); | |
64 assert DOUBLE_0.isDefaultValue(); | |
65 assert FALSE.isDefaultValue(); | |
66 | |
67 // Ensure difference between 0.0f and -0.0f is preserved | |
68 assert FLOAT_0 != forFloat(-0.0F); | |
69 assert !forFloat(-0.0F).isDefaultValue(); | |
70 | |
71 // Ensure difference between 0.0d and -0.0d is preserved | |
72 assert DOUBLE_0 != forDouble(-0.0d); | |
73 assert !forDouble(-0.0D).isDefaultValue(); | |
74 | |
75 assert NULL_OBJECT.isNull(); | |
76 } | |
77 | |
78 /** | |
79 * The boxed object value. This is ignored iff {@code !kind.isObject()}. | |
80 */ | |
81 private final Object object; | |
82 | |
83 /** | |
84 * The boxed primitive value as a {@code long}. This is ignored iff {@code kind.isObject()}. | |
85 * For {@code float} and {@code double} values, this value is the result of | |
86 * {@link Float#floatToRawIntBits(float)} and {@link Double#doubleToRawLongBits(double)} respectively. | |
87 */ | |
88 private final long primitive; | |
89 | |
90 /** | |
91 * Create a new constant represented by the specified object reference. | |
92 * | |
93 * @param kind the type of this constant | |
94 * @param object the value of this constant | |
95 */ | |
96 private RiConstant(CiKind kind, Object object) { | |
97 super(kind); | |
98 this.object = object; | |
99 this.primitive = 0L; | |
100 } | |
101 | |
102 /** | |
103 * Create a new constant represented by the specified primitive. | |
104 * | |
105 * @param kind the type of this constant | |
106 * @param primitive the value of this constant | |
107 */ | |
108 public RiConstant(CiKind kind, long primitive) { | |
109 super(kind); | |
110 this.object = null; | |
111 this.primitive = primitive; | |
112 } | |
113 | |
114 /** | |
115 * Checks whether this constant is non-null. | |
116 * @return {@code true} if this constant is a primitive, or an object constant that is not null | |
117 */ | |
118 public boolean isNonNull() { | |
119 return !kind.isObject() || object != null; | |
120 } | |
121 | |
122 /** | |
123 * Checks whether this constant is null. | |
124 * @return {@code true} if this constant is the null constant | |
125 */ | |
126 public boolean isNull() { | |
127 return kind.isObject() && object == null; | |
128 } | |
129 | |
130 @Override | |
131 public String toString() { | |
132 return kind.javaName + "[" + kind.format(boxedValue()) + (kind != CiKind.Object ? "|0x" + Long.toHexString(primitive) : "") + "]"; | |
133 } | |
134 | |
135 /** | |
136 * Gets this constant's value as a string. | |
137 * | |
138 * @return this constant's value as a string | |
139 */ | |
140 public String valueString() { | |
141 if (kind.isPrimitive()) { | |
142 return boxedValue().toString(); | |
143 } else if (kind.isObject()) { | |
144 if (object == null) { | |
145 return "null"; | |
146 } else if (object instanceof String) { | |
147 return "\"" + object + "\""; | |
148 } else { | |
149 return "<object: " + kind.format(object) + ">"; | |
150 } | |
151 } else if (kind.isJsr()) { | |
152 return "bci:" + boxedValue().toString(); | |
153 } else { | |
154 return "???"; | |
155 } | |
156 } | |
157 | |
158 /** | |
159 * Returns the value of this constant as a boxed Java value. | |
160 * @return the value of this constant | |
161 */ | |
162 public Object boxedValue() { | |
163 // Checkstyle: stop | |
164 switch (kind) { | |
165 case Byte: return (byte) asInt(); | |
166 case Boolean: return asInt() == 0 ? Boolean.FALSE : Boolean.TRUE; | |
167 case Short: return (short) asInt(); | |
168 case Char: return (char) asInt(); | |
169 case Jsr: return (int) primitive; | |
170 case Int: return asInt(); | |
171 case Long: return asLong(); | |
172 case Float: return asFloat(); | |
173 case Double: return asDouble(); | |
174 case Object: return object; | |
175 } | |
176 // Checkstyle: resume | |
177 throw new IllegalArgumentException(); | |
178 } | |
179 | |
180 private boolean valueEqual(RiConstant other, boolean ignoreKind) { | |
181 // must have equivalent kinds to be equal | |
182 if (!ignoreKind && kind != other.kind) { | |
183 return false; | |
184 } | |
185 if (kind.isObject()) { | |
186 return object == other.object; | |
187 } | |
188 return primitive == other.primitive; | |
189 } | |
190 | |
191 /** | |
192 * Converts this constant to a primitive int. | |
193 * @return the int value of this constant | |
194 */ | |
195 public int asInt() { | |
196 if (kind.stackKind().isInt() || kind.isJsr()) { | |
197 return (int) primitive; | |
198 } | |
199 throw new Error("Constant is not int: " + this); | |
200 } | |
201 | |
202 /** | |
203 * Converts this constant to a primitive boolean. | |
204 * @return the boolean value of this constant | |
205 */ | |
206 public boolean asBoolean() { | |
207 if (kind == CiKind.Boolean) { | |
208 return primitive != 0L; | |
209 } | |
210 throw new Error("Constant is not boolean: " + this); | |
211 } | |
212 | |
213 /** | |
214 * Converts this constant to a primitive long. | |
215 * @return the long value of this constant | |
216 */ | |
217 public long asLong() { | |
218 // Checkstyle: stop | |
219 switch (kind.stackKind()) { | |
220 case Jsr: | |
221 case Int: | |
222 case Long: return primitive; | |
223 case Float: return (long) asFloat(); | |
224 case Double: return (long) asDouble(); | |
225 default: throw new Error("Constant is not long: " + this); | |
226 } | |
227 // Checkstyle: resume | |
228 } | |
229 | |
230 /** | |
231 * Converts this constant to a primitive float. | |
232 * @return the float value of this constant | |
233 */ | |
234 public float asFloat() { | |
235 if (kind.isFloat()) { | |
236 return Float.intBitsToFloat((int) primitive); | |
237 } | |
238 throw new Error("Constant is not float: " + this); | |
239 } | |
240 | |
241 /** | |
242 * Converts this constant to a primitive double. | |
243 * @return the double value of this constant | |
244 */ | |
245 public double asDouble() { | |
246 if (kind.isFloat()) { | |
247 return Float.intBitsToFloat((int) primitive); | |
248 } | |
249 if (kind.isDouble()) { | |
250 return Double.longBitsToDouble(primitive); | |
251 } | |
252 throw new Error("Constant is not double: " + this); | |
253 } | |
254 | |
255 /** | |
256 * Converts this constant to the object reference it represents. | |
257 * @return the object which this constant represents | |
258 */ | |
259 public Object asObject() { | |
260 if (kind.isObject()) { | |
261 return object; | |
262 } | |
263 throw new Error("Constant is not object: " + this); | |
264 } | |
265 | |
266 /** | |
267 * Converts this constant to the jsr reference it represents. | |
268 * @return the object which this constant represents | |
269 */ | |
270 public int asJsr() { | |
271 if (kind.isJsr()) { | |
272 return (int) primitive; | |
273 } | |
274 throw new Error("Constant is not jsr: " + this); | |
275 } | |
276 | |
277 /** | |
278 * Unchecked access to a primitive value. | |
279 * @return | |
280 */ | |
281 public long asPrimitive() { | |
282 if (kind.isObject()) { | |
283 throw new Error("Constant is not primitive: " + this); | |
284 } | |
285 return primitive; | |
286 } | |
287 | |
288 /** | |
289 * Computes the hashcode of this constant. | |
290 * @return a suitable hashcode for this constant | |
291 */ | |
292 @Override | |
293 public int hashCode() { | |
294 if (kind.isObject()) { | |
295 return System.identityHashCode(object); | |
296 } | |
297 return (int) primitive; | |
298 } | |
299 | |
300 /** | |
301 * Checks whether this constant equals another object. This is only | |
302 * true if the other object is a constant and has the same value. | |
303 * @param o the object to compare equality | |
304 * @return {@code true} if this constant is equivalent to the specified object | |
305 */ | |
306 @Override | |
307 public boolean equals(Object o) { | |
308 return o == this || o instanceof RiConstant && valueEqual((RiConstant) o, false); | |
309 } | |
310 | |
311 /** | |
312 * Checks whether this constant is identical to another constant or has the same value as it. | |
313 * @param other the constant to compare for equality against this constant | |
314 * @return {@code true} if this constant is equivalent to {@code other} | |
315 */ | |
316 public boolean equivalent(RiConstant other) { | |
317 return other == this || valueEqual(other, false); | |
318 } | |
319 | |
320 /** | |
321 * Checks whether this constant is the default value for its type. | |
322 * @return {@code true} if the value is the default value for its type; {@code false} otherwise | |
323 */ | |
324 public boolean isDefaultValue() { | |
325 // Checkstyle: stop | |
326 switch (kind.stackKind()) { | |
327 case Int: return asInt() == 0; | |
328 case Long: return asLong() == 0; | |
329 case Float: return this == FLOAT_0; | |
330 case Double: return this == DOUBLE_0; | |
331 case Object: return object == null; | |
332 } | |
333 // Checkstyle: resume | |
334 throw new IllegalArgumentException("Cannot det default CiConstant for kind " + kind); | |
335 } | |
336 | |
337 /** | |
338 * Gets the default value for a given kind. | |
339 * | |
340 * @return the default value for {@code kind}'s {@linkplain CiKind#stackKind() stack kind} | |
341 */ | |
342 public static RiConstant defaultValue(CiKind kind) { | |
343 // Checkstyle: stop | |
344 switch (kind.stackKind()) { | |
345 case Int: return INT_0; | |
346 case Long: return LONG_0; | |
347 case Float: return FLOAT_0; | |
348 case Double: return DOUBLE_0; | |
349 case Object: return NULL_OBJECT; | |
350 } | |
351 // Checkstyle: resume | |
352 throw new IllegalArgumentException("Cannot get default CiConstant for kind " + kind); | |
353 } | |
354 | |
355 /** | |
356 * Creates a boxed double constant. | |
357 * @param d the double value to box | |
358 * @return a boxed copy of {@code value} | |
359 */ | |
360 public static RiConstant forDouble(double d) { | |
361 if (Double.compare(0.0D, d) == 0) { | |
362 return DOUBLE_0; | |
363 } | |
364 if (Double.compare(d, 1.0D) == 0) { | |
365 return DOUBLE_1; | |
366 } | |
367 return new RiConstant(CiKind.Double, Double.doubleToRawLongBits(d)); | |
368 } | |
369 | |
370 /** | |
371 * Creates a boxed float constant. | |
372 * @param f the float value to box | |
373 * @return a boxed copy of {@code value} | |
374 */ | |
375 public static RiConstant forFloat(float f) { | |
376 if (Float.compare(f, 0.0F) == 0) { | |
377 return FLOAT_0; | |
378 } | |
379 if (Float.compare(f, 1.0F) == 0) { | |
380 return FLOAT_1; | |
381 } | |
382 if (Float.compare(f, 2.0F) == 0) { | |
383 return FLOAT_2; | |
384 } | |
385 return new RiConstant(CiKind.Float, Float.floatToRawIntBits(f)); | |
386 } | |
387 | |
388 /** | |
389 * Creates a boxed long constant. | |
390 * @param i the long value to box | |
391 * @return a boxed copy of {@code value} | |
392 */ | |
393 public static RiConstant forLong(long i) { | |
394 return i == 0 ? LONG_0 : i == 1 ? LONG_1 : new RiConstant(CiKind.Long, i); | |
395 } | |
396 | |
397 /** | |
398 * Creates a boxed integer constant. | |
399 * @param i the integer value to box | |
400 * @return a boxed copy of {@code value} | |
401 */ | |
402 public static RiConstant forInt(int i) { | |
403 if (i == -1) { | |
404 return INT_MINUS_1; | |
405 } | |
406 if (i >= 0 && i < INT_CONSTANT_CACHE.length) { | |
407 return INT_CONSTANT_CACHE[i]; | |
408 } | |
409 return new RiConstant(CiKind.Int, i); | |
410 } | |
411 | |
412 /** | |
413 * Creates a boxed byte constant. | |
414 * @param i the byte value to box | |
415 * @return a boxed copy of {@code value} | |
416 */ | |
417 public static RiConstant forByte(byte i) { | |
418 return new RiConstant(CiKind.Byte, i); | |
419 } | |
420 | |
421 /** | |
422 * Creates a boxed boolean constant. | |
423 * @param i the boolean value to box | |
424 * @return a boxed copy of {@code value} | |
425 */ | |
426 public static RiConstant forBoolean(boolean i) { | |
427 return i ? TRUE : FALSE; | |
428 } | |
429 | |
430 /** | |
431 * Creates a boxed char constant. | |
432 * @param i the char value to box | |
433 * @return a boxed copy of {@code value} | |
434 */ | |
435 public static RiConstant forChar(char i) { | |
436 return new RiConstant(CiKind.Char, i); | |
437 } | |
438 | |
439 /** | |
440 * Creates a boxed short constant. | |
441 * @param i the short value to box | |
442 * @return a boxed copy of {@code value} | |
443 */ | |
444 public static RiConstant forShort(short i) { | |
445 return new RiConstant(CiKind.Short, i); | |
446 } | |
447 | |
448 /** | |
449 * Creates a boxed address (jsr/ret address) constant. | |
450 * @param i the address value to box | |
451 * @return a boxed copy of {@code value} | |
452 */ | |
453 public static RiConstant forJsr(int i) { | |
454 return new RiConstant(CiKind.Jsr, i); | |
455 } | |
456 | |
457 /** | |
458 * Creates a boxed object constant. | |
459 * @param o the object value to box | |
460 * @return a boxed copy of {@code value} | |
461 */ | |
462 public static RiConstant forObject(Object o) { | |
463 if (o == null) { | |
464 return NULL_OBJECT; | |
465 } | |
466 return new RiConstant(CiKind.Object, o); | |
467 } | |
468 | |
469 /** | |
470 * Creates a boxed constant for the given kind from an Object. | |
471 * The object needs to be of the Java boxed type corresponding to the kind. | |
472 * @param kind the kind of the constant to create | |
473 * @param value the Java boxed value: a Byte instance for CiKind Byte, etc. | |
474 * @return the boxed copy of {@code value} | |
475 */ | |
476 public static RiConstant forBoxed(CiKind kind, Object value) { | |
477 switch (kind) { | |
478 case Boolean: | |
479 return forBoolean((Boolean) value); | |
480 case Byte: | |
481 return forByte((Byte) value); | |
482 case Char: | |
483 return forChar((Character) value); | |
484 case Short: | |
485 return forShort((Short) value); | |
486 case Int: | |
487 return forInt((Integer) value); | |
488 case Long: | |
489 return forLong((Long) value); | |
490 case Float: | |
491 return forFloat((Float) value); | |
492 case Double: | |
493 return forDouble((Double) value); | |
494 case Object: | |
495 return forObject(value); | |
496 default: | |
497 throw new RuntimeException("cannot create CiConstant for boxed " + kind + " value"); | |
498 } | |
499 } | |
500 } |