comparison graal/com.oracle.jvmci.meta/src/com/oracle/jvmci/meta/PrimitiveConstant.java @ 21556:48c1ebd24120

renamed com.oracle.graal.api[meta|code] modules to com.oracle.jvmci.[meta|code] (JBS:GRAAL-53)
author Doug Simon <doug.simon@oracle.com>
date Wed, 27 May 2015 00:36:16 +0200
parents graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/PrimitiveConstant.java@082417ac43e4
children
comparison
equal deleted inserted replaced
21555:d12eaef9af72 21556:48c1ebd24120
1 /*
2 * Copyright (c) 2009, 2015, 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.meta;
24
25 import java.nio.*;
26
27 /**
28 * Represents a primitive constant value, such as an integer or floating point number, within the
29 * compiler and across the compiler/runtime interface.
30 */
31 public class PrimitiveConstant extends AbstractValue implements JavaConstant, SerializableConstant {
32
33 /**
34 * The boxed primitive value as a {@code long}. For {@code float} and {@code double} values,
35 * this value is the result of {@link Float#floatToRawIntBits(float)} and
36 * {@link Double#doubleToRawLongBits(double)} respectively.
37 */
38 private final long primitive;
39
40 protected PrimitiveConstant(Kind kind, long primitive) {
41 super(LIRKind.value(kind));
42 this.primitive = primitive;
43
44 assert kind.isPrimitive() || kind == Kind.Illegal;
45 }
46
47 @Override
48 public boolean isNull() {
49 return false;
50 }
51
52 @Override
53 public boolean isDefaultForKind() {
54 return primitive == 0;
55 }
56
57 @Override
58 public boolean asBoolean() {
59 assert getKind() == Kind.Boolean;
60 return primitive != 0L;
61 }
62
63 @Override
64 public int asInt() {
65 assert getKind().getStackKind() == Kind.Int : getKind().getStackKind();
66 return (int) primitive;
67 }
68
69 @Override
70 public long asLong() {
71 assert getKind().isNumericInteger();
72 return primitive;
73 }
74
75 @Override
76 public float asFloat() {
77 assert getKind() == Kind.Float;
78 return Float.intBitsToFloat((int) primitive);
79 }
80
81 @Override
82 public double asDouble() {
83 assert getKind() == Kind.Double;
84 return Double.longBitsToDouble(primitive);
85 }
86
87 @Override
88 public Object asBoxedPrimitive() {
89 switch (getKind()) {
90 case Byte:
91 return Byte.valueOf((byte) primitive);
92 case Boolean:
93 return Boolean.valueOf(asBoolean());
94 case Short:
95 return Short.valueOf((short) primitive);
96 case Char:
97 return Character.valueOf((char) primitive);
98 case Int:
99 return Integer.valueOf(asInt());
100 case Long:
101 return Long.valueOf(asLong());
102 case Float:
103 return Float.valueOf(asFloat());
104 case Double:
105 return Double.valueOf(asDouble());
106 default:
107 throw new IllegalArgumentException("unexpected kind " + getKind());
108 }
109 }
110
111 @Override
112 public int getSerializedSize() {
113 return getKind().getByteCount();
114 }
115
116 @Override
117 public void serialize(ByteBuffer buffer) {
118 switch (getKind()) {
119 case Byte:
120 case Boolean:
121 buffer.put((byte) primitive);
122 break;
123 case Short:
124 buffer.putShort((short) primitive);
125 break;
126 case Char:
127 buffer.putChar((char) primitive);
128 break;
129 case Int:
130 buffer.putInt(asInt());
131 break;
132 case Long:
133 buffer.putLong(asLong());
134 break;
135 case Float:
136 buffer.putFloat(asFloat());
137 break;
138 case Double:
139 buffer.putDouble(asDouble());
140 break;
141 default:
142 throw new IllegalArgumentException("unexpected kind " + getKind());
143 }
144 }
145
146 @Override
147 public int hashCode() {
148 return (int) (primitive ^ (primitive >>> 32)) * (getKind().ordinal() + 31);
149 }
150
151 @Override
152 public boolean equals(Object o) {
153 return o == this || (o instanceof PrimitiveConstant && super.equals(o) && primitive == ((PrimitiveConstant) o).primitive);
154 }
155
156 @Override
157 public String toString() {
158 if (getKind() == Kind.Illegal) {
159 return "illegal";
160 } else {
161 return getKind().getJavaName() + "[" + asBoxedPrimitive() + "|0x" + Long.toHexString(primitive) + "]";
162 }
163 }
164 }