comparison graal/com.oracle.max.cri/src/com/oracle/max/cri/ci/RiConstant.java @ 5501:d89b20486d87

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