Mercurial > hg > graal-compiler
comparison graal/com.oracle.max.cri/src/com/oracle/max/cri/ci/CiKind.java @ 4199:aaac4894175c
Renamed cri packages from sun to oracle.
author | Thomas Wuerthinger <thomas.wuerthinger@oracle.com> |
---|---|
date | Tue, 03 Jan 2012 16:29:28 +0100 |
parents | graal/com.oracle.max.cri/src/com/sun/cri/ci/CiKind.java@e233f5660da4 |
children | 87a12a816e99 |
comparison
equal
deleted
inserted
replaced
4198:8c9c0e1eaab1 | 4199:aaac4894175c |
---|---|
1 /* | |
2 * Copyright (c) 2009, 2011, 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 import static com.oracle.max.cri.ci.CiKind.Flags.*; | |
26 import sun.misc.*; | |
27 | |
28 import com.oracle.max.cri.ri.*; | |
29 | |
30 /** | |
31 * Denotes the basic kinds of types in CRI, including the all the Java primitive types, | |
32 * for example, {@link CiKind#Int} for {@code int} and {@link CiKind#Object} | |
33 * for all object types. | |
34 * A kind has a single character short name, a Java name, and a set of flags | |
35 * further describing its behavior. | |
36 */ | |
37 public enum CiKind { | |
38 Boolean('z', "boolean", FIELD_TYPE | RETURN_TYPE | PRIMITIVE | STACK_INT), | |
39 Byte ('b', "byte", FIELD_TYPE | RETURN_TYPE | PRIMITIVE | STACK_INT), | |
40 Short ('s', "short", FIELD_TYPE | RETURN_TYPE | PRIMITIVE | STACK_INT), | |
41 Char ('c', "char", FIELD_TYPE | RETURN_TYPE | PRIMITIVE | STACK_INT), | |
42 Int ('i', "int", FIELD_TYPE | RETURN_TYPE | PRIMITIVE | STACK_INT), | |
43 Float ('f', "float", FIELD_TYPE | RETURN_TYPE | PRIMITIVE), | |
44 Long ('j', "long", FIELD_TYPE | RETURN_TYPE | PRIMITIVE), | |
45 Double ('d', "double", FIELD_TYPE | RETURN_TYPE | PRIMITIVE), | |
46 Object ('a', "Object", FIELD_TYPE | RETURN_TYPE), | |
47 Void ('v', "void", RETURN_TYPE), | |
48 /** Denote a bytecode address in a {@code JSR} bytecode. */ | |
49 Jsr ('r', "jsr", 0), | |
50 /** The non-type. */ | |
51 Illegal('-', "illegal", 0); | |
52 | |
53 public static final CiKind[] VALUES = values(); | |
54 public static final CiKind[] JAVA_VALUES = new CiKind[] {CiKind.Boolean, CiKind.Byte, CiKind.Short, CiKind.Char, CiKind.Int, CiKind.Float, CiKind.Long, CiKind.Double, CiKind.Object}; | |
55 | |
56 CiKind(char ch, String name, int flags) { | |
57 this.typeChar = ch; | |
58 this.javaName = name; | |
59 this.flags = flags; | |
60 } | |
61 | |
62 static class Flags { | |
63 /** | |
64 * Can be an object field type. | |
65 */ | |
66 public static final int FIELD_TYPE = 0x0001; | |
67 /** | |
68 * Can be result type of a method. | |
69 */ | |
70 public static final int RETURN_TYPE = 0x0002; | |
71 /** | |
72 * Behaves as an integer when on Java evaluation stack. | |
73 */ | |
74 public static final int STACK_INT = 0x0004; | |
75 /** | |
76 * Represents a Java primitive type. | |
77 */ | |
78 public static final int PRIMITIVE = 0x0008; | |
79 } | |
80 | |
81 /** | |
82 * The flags for this kind. | |
83 */ | |
84 private final int flags; | |
85 | |
86 /** | |
87 * The name of the kind as a single character. | |
88 */ | |
89 public final char typeChar; | |
90 | |
91 /** | |
92 * The name of this kind which will also be it Java programming language name if | |
93 * it is {@linkplain #isPrimitive() primitive} or {@code void}. | |
94 */ | |
95 public final String javaName; | |
96 | |
97 /** | |
98 * Checks whether this kind is valid as the type of a field. | |
99 * @return {@code true} if this kind is valid as the type of a Java field | |
100 */ | |
101 public boolean isValidFieldType() { | |
102 return (flags & FIELD_TYPE) != 0; | |
103 } | |
104 | |
105 /** | |
106 * Checks whether this kind is valid as the return type of a method. | |
107 * @return {@code true} if this kind is valid as the return type of a Java method | |
108 */ | |
109 public boolean isValidReturnType() { | |
110 return (flags & RETURN_TYPE) != 0; | |
111 } | |
112 | |
113 /** | |
114 * Checks whether this type is valid as an {@code int} on the Java operand stack. | |
115 * @return {@code true} if this type is represented by an {@code int} on the operand stack | |
116 */ | |
117 public boolean isInt() { | |
118 return (flags & STACK_INT) != 0; | |
119 } | |
120 | |
121 /** | |
122 * Checks whether this type is a Java primitive type. | |
123 * @return {@code true} if this is {@link #Boolean}, {@link #Byte}, {@link #Char}, {@link #Short}, | |
124 * {@link #Int}, {@link #Long}, {@link #Float} or {@link #Double}. | |
125 */ | |
126 public boolean isPrimitive() { | |
127 return (flags & PRIMITIVE) != 0; | |
128 } | |
129 | |
130 /** | |
131 * Gets the kind that represents this kind when on the Java operand stack. | |
132 * @return the kind used on the operand stack | |
133 */ | |
134 public CiKind stackKind() { | |
135 if (isInt()) { | |
136 return Int; | |
137 } | |
138 return this; | |
139 } | |
140 | |
141 public static CiKind fromTypeString(String typeString) { | |
142 assert typeString.length() > 0; | |
143 final char first = typeString.charAt(0); | |
144 if (first == '[' || first == 'L') { | |
145 return CiKind.Object; | |
146 } | |
147 return CiKind.fromPrimitiveOrVoidTypeChar(first); | |
148 } | |
149 | |
150 /** | |
151 * Gets the kind from the character describing a primitive or void. | |
152 * @param ch the character | |
153 * @return the kind | |
154 */ | |
155 public static CiKind fromPrimitiveOrVoidTypeChar(char ch) { | |
156 // Checkstyle: stop | |
157 switch (ch) { | |
158 case 'Z': return Boolean; | |
159 case 'C': return Char; | |
160 case 'F': return Float; | |
161 case 'D': return Double; | |
162 case 'B': return Byte; | |
163 case 'S': return Short; | |
164 case 'I': return Int; | |
165 case 'J': return Long; | |
166 case 'V': return Void; | |
167 } | |
168 // Checkstyle: resume | |
169 throw new IllegalArgumentException("unknown primitive or void type character: " + ch); | |
170 } | |
171 | |
172 public Class< ? > toJavaClass() { | |
173 // Checkstyle: stop | |
174 switch(this) { | |
175 case Void: return java.lang.Void.TYPE; | |
176 case Long: return java.lang.Long.TYPE; | |
177 case Int: return java.lang.Integer.TYPE; | |
178 case Byte: return java.lang.Byte.TYPE; | |
179 case Char: return java.lang.Character.TYPE; | |
180 case Double: return java.lang.Double.TYPE; | |
181 case Float: return java.lang.Float.TYPE; | |
182 case Short: return java.lang.Short.TYPE; | |
183 case Boolean: return java.lang.Boolean.TYPE; | |
184 default: return null; | |
185 } | |
186 // Checkstyle: resume | |
187 } | |
188 | |
189 public Class< ? > toUnboxedJavaClass() { | |
190 // Checkstyle: stop | |
191 switch(this) { | |
192 case Void: return null; | |
193 case Long: return java.lang.Long.class; | |
194 case Int: return java.lang.Integer.class; | |
195 case Byte: return java.lang.Byte.class; | |
196 case Char: return java.lang.Character.class; | |
197 case Double: return java.lang.Double.class; | |
198 case Float: return java.lang.Float.class; | |
199 case Short: return java.lang.Short.class; | |
200 case Boolean: return java.lang.Boolean.class; | |
201 default: return null; | |
202 } | |
203 // Checkstyle: resume | |
204 } | |
205 | |
206 /** | |
207 * Checks whether this value type is void. | |
208 * @return {@code true} if this type is void | |
209 */ | |
210 public final boolean isVoid() { | |
211 return this == CiKind.Void; | |
212 } | |
213 | |
214 /** | |
215 * Checks whether this value type is long. | |
216 * @return {@code true} if this type is long | |
217 */ | |
218 public final boolean isLong() { | |
219 return this == CiKind.Long; | |
220 } | |
221 | |
222 /** | |
223 * Checks whether this value type is float. | |
224 * @return {@code true} if this type is float | |
225 */ | |
226 public final boolean isFloat() { | |
227 return this == CiKind.Float; | |
228 } | |
229 | |
230 /** | |
231 * Checks whether this value type is double. | |
232 * @return {@code true} if this type is double | |
233 */ | |
234 public final boolean isDouble() { | |
235 return this == CiKind.Double; | |
236 } | |
237 | |
238 /** | |
239 * Checks whether this value type is float or double. | |
240 * @return {@code true} if this type is float or double | |
241 */ | |
242 public final boolean isFloatOrDouble() { | |
243 return this == CiKind.Double || this == CiKind.Float; | |
244 } | |
245 | |
246 /** | |
247 * Checks whether this value type is an object type. | |
248 * @return {@code true} if this type is an object | |
249 */ | |
250 public final boolean isObject() { | |
251 return this == CiKind.Object; | |
252 } | |
253 | |
254 /** | |
255 * Checks whether this value type is an address type. | |
256 * @return {@code true} if this type is an address | |
257 */ | |
258 public boolean isJsr() { | |
259 return this == CiKind.Jsr; | |
260 } | |
261 | |
262 /** | |
263 * Converts this value type to a string. | |
264 */ | |
265 @Override | |
266 public String toString() { | |
267 return javaName; | |
268 } | |
269 | |
270 /** | |
271 * Marker interface for types that should be {@linkplain CiKind#format(Object) formatted} | |
272 * with their {@link Object#toString()} value. | |
273 */ | |
274 public interface FormatWithToString {} | |
275 | |
276 /** | |
277 * Gets a formatted string for a given value of this kind. | |
278 * | |
279 * @param value a value of this kind | |
280 * @return a formatted string for {@code value} based on this kind | |
281 */ | |
282 public String format(Object value) { | |
283 if (isObject()) { | |
284 if (value == null) { | |
285 return "null"; | |
286 } else { | |
287 if (value instanceof String) { | |
288 String s = (String) value; | |
289 if (s.length() > 50) { | |
290 return "\"" + s.substring(0, 30) + "...\""; | |
291 } else { | |
292 return " \"" + s + '"'; | |
293 } | |
294 } else if (value instanceof RiType) { | |
295 return "class " + CiUtil.toJavaName((RiType) value); | |
296 } else if (value instanceof Enum || value instanceof FormatWithToString) { | |
297 return String.valueOf(value); | |
298 } else if (value instanceof Class< ? >) { | |
299 return ((Class< ? >) value).getName() + ".class"; | |
300 } else { | |
301 return CiUtil.getSimpleName(value.getClass(), true) + "@" + System.identityHashCode(value); | |
302 } | |
303 } | |
304 } else { | |
305 return value.toString(); | |
306 } | |
307 } | |
308 | |
309 public final char signatureChar() { | |
310 return Character.toUpperCase(typeChar); | |
311 } | |
312 | |
313 public CiConstant readUnsafeConstant(Object value, long displacement) { | |
314 Unsafe u = Unsafe.getUnsafe(); | |
315 switch(this) { | |
316 case Boolean: | |
317 return CiConstant.forBoolean(u.getBoolean(value, displacement)); | |
318 case Byte: | |
319 return CiConstant.forByte(u.getByte(value, displacement)); | |
320 case Char: | |
321 return CiConstant.forChar(u.getChar(value, displacement)); | |
322 case Short: | |
323 return CiConstant.forShort(u.getShort(value, displacement)); | |
324 case Int: | |
325 return CiConstant.forInt(u.getInt(value, displacement)); | |
326 case Long: | |
327 return CiConstant.forLong(u.getLong(value, displacement)); | |
328 case Float: | |
329 return CiConstant.forFloat(u.getFloat(value, displacement)); | |
330 case Double: | |
331 return CiConstant.forDouble(u.getDouble(value, displacement)); | |
332 case Object: | |
333 return CiConstant.forObject(u.getObject(value, displacement)); | |
334 default: | |
335 assert false : "unexpected kind: " + this; | |
336 return null; | |
337 } | |
338 } | |
339 | |
340 } |