Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.max.cri/src/com/oracle/max/cri/ri/RiKind.java @ 5504:452f91ebdb54
Moved RiKind to cri.ri package.
author | Thomas Wuerthinger <thomas.wuerthinger@oracle.com> |
---|---|
date | Thu, 07 Jun 2012 17:09:57 +0200 |
parents | graal/com.oracle.max.cri/src/com/oracle/max/cri/ci/RiKind.java@438ab53efdd0 |
children |
comparison
equal
deleted
inserted
replaced
5503:438ab53efdd0 | 5504:452f91ebdb54 |
---|---|
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.ri; | |
24 | |
25 import static com.oracle.max.cri.ri.RiKind.Flags.*; | |
26 | |
27 import java.lang.reflect.*; | |
28 | |
29 import sun.misc.*; | |
30 | |
31 import com.oracle.max.cri.ci.*; | |
32 | |
33 /** | |
34 * Denotes the basic kinds of types in CRI, including the all the Java primitive types, | |
35 * for example, {@link RiKind#Int} for {@code int} and {@link RiKind#Object} | |
36 * for all object types. | |
37 * A kind has a single character short name, a Java name, and a set of flags | |
38 * further describing its behavior. | |
39 */ | |
40 public enum RiKind { | |
41 Boolean('z', "boolean", FIELD_TYPE | RETURN_TYPE | PRIMITIVE | STACK_INT), | |
42 Byte ('b', "byte", FIELD_TYPE | RETURN_TYPE | PRIMITIVE | STACK_INT), | |
43 Short ('s', "short", FIELD_TYPE | RETURN_TYPE | PRIMITIVE | STACK_INT), | |
44 Char ('c', "char", FIELD_TYPE | RETURN_TYPE | PRIMITIVE | STACK_INT), | |
45 Int ('i', "int", FIELD_TYPE | RETURN_TYPE | PRIMITIVE | STACK_INT), | |
46 Float ('f', "float", FIELD_TYPE | RETURN_TYPE | PRIMITIVE), | |
47 Long ('j', "long", FIELD_TYPE | RETURN_TYPE | PRIMITIVE), | |
48 Double ('d', "double", FIELD_TYPE | RETURN_TYPE | PRIMITIVE), | |
49 Object ('a', "Object", FIELD_TYPE | RETURN_TYPE), | |
50 Void ('v', "void", RETURN_TYPE), | |
51 /** Denote a bytecode address in a {@code JSR} bytecode. */ | |
52 Jsr ('r', "jsr", 0), | |
53 /** The non-type. */ | |
54 Illegal('-', "illegal", 0); | |
55 | |
56 public static final RiKind[] VALUES = values(); | |
57 public static final RiKind[] JAVA_VALUES = new RiKind[] {RiKind.Boolean, RiKind.Byte, RiKind.Short, RiKind.Char, RiKind.Int, RiKind.Float, RiKind.Long, RiKind.Double, RiKind.Object}; | |
58 | |
59 RiKind(char ch, String name, int flags) { | |
60 this.typeChar = ch; | |
61 this.javaName = name; | |
62 this.flags = flags; | |
63 } | |
64 | |
65 static class Flags { | |
66 /** | |
67 * Can be an object field type. | |
68 */ | |
69 public static final int FIELD_TYPE = 0x0001; | |
70 /** | |
71 * Can be result type of a method. | |
72 */ | |
73 public static final int RETURN_TYPE = 0x0002; | |
74 /** | |
75 * Behaves as an integer when on Java evaluation stack. | |
76 */ | |
77 public static final int STACK_INT = 0x0004; | |
78 /** | |
79 * Represents a Java primitive type. | |
80 */ | |
81 public static final int PRIMITIVE = 0x0008; | |
82 } | |
83 | |
84 /** | |
85 * The flags for this kind. | |
86 */ | |
87 private final int flags; | |
88 | |
89 /** | |
90 * The name of the kind as a single character. | |
91 */ | |
92 public final char typeChar; | |
93 | |
94 /** | |
95 * The name of this kind which will also be it Java programming language name if | |
96 * it is {@linkplain #isPrimitive() primitive} or {@code void}. | |
97 */ | |
98 public final String javaName; | |
99 | |
100 /** | |
101 * Checks whether this kind is valid as the type of a field. | |
102 * @return {@code true} if this kind is valid as the type of a Java field | |
103 */ | |
104 public boolean isValidFieldType() { | |
105 return (flags & FIELD_TYPE) != 0; | |
106 } | |
107 | |
108 /** | |
109 * Checks whether this kind is valid as the return type of a method. | |
110 * @return {@code true} if this kind is valid as the return type of a Java method | |
111 */ | |
112 public boolean isValidReturnType() { | |
113 return (flags & RETURN_TYPE) != 0; | |
114 } | |
115 | |
116 /** | |
117 * Checks whether this type is valid as an {@code int} on the Java operand stack. | |
118 * @return {@code true} if this type is represented by an {@code int} on the operand stack | |
119 */ | |
120 public boolean isInt() { | |
121 return (flags & STACK_INT) != 0; | |
122 } | |
123 | |
124 /** | |
125 * Checks whether this type is a Java primitive type. | |
126 * @return {@code true} if this is {@link #Boolean}, {@link #Byte}, {@link #Char}, {@link #Short}, | |
127 * {@link #Int}, {@link #Long}, {@link #Float} or {@link #Double}. | |
128 */ | |
129 public boolean isPrimitive() { | |
130 return (flags & PRIMITIVE) != 0; | |
131 } | |
132 | |
133 /** | |
134 * Gets the kind that represents this kind when on the Java operand stack. | |
135 * @return the kind used on the operand stack | |
136 */ | |
137 public RiKind stackKind() { | |
138 if (isInt()) { | |
139 return Int; | |
140 } | |
141 return this; | |
142 } | |
143 | |
144 public static RiKind fromTypeString(String typeString) { | |
145 assert typeString.length() > 0; | |
146 final char first = typeString.charAt(0); | |
147 if (first == '[' || first == 'L') { | |
148 return RiKind.Object; | |
149 } | |
150 return RiKind.fromPrimitiveOrVoidTypeChar(first); | |
151 } | |
152 | |
153 /** | |
154 * Gets the kind from the character describing a primitive or void. | |
155 * @param ch the character | |
156 * @return the kind | |
157 */ | |
158 public static RiKind fromPrimitiveOrVoidTypeChar(char ch) { | |
159 // Checkstyle: stop | |
160 switch (ch) { | |
161 case 'Z': return Boolean; | |
162 case 'C': return Char; | |
163 case 'F': return Float; | |
164 case 'D': return Double; | |
165 case 'B': return Byte; | |
166 case 'S': return Short; | |
167 case 'I': return Int; | |
168 case 'J': return Long; | |
169 case 'V': return Void; | |
170 } | |
171 // Checkstyle: resume | |
172 throw new IllegalArgumentException("unknown primitive or void type character: " + ch); | |
173 } | |
174 | |
175 public Class< ? > toJavaClass() { | |
176 // Checkstyle: stop | |
177 switch(this) { | |
178 case Void: return java.lang.Void.TYPE; | |
179 case Long: return java.lang.Long.TYPE; | |
180 case Int: return java.lang.Integer.TYPE; | |
181 case Byte: return java.lang.Byte.TYPE; | |
182 case Char: return java.lang.Character.TYPE; | |
183 case Double: return java.lang.Double.TYPE; | |
184 case Float: return java.lang.Float.TYPE; | |
185 case Short: return java.lang.Short.TYPE; | |
186 case Boolean: return java.lang.Boolean.TYPE; | |
187 default: return null; | |
188 } | |
189 // Checkstyle: resume | |
190 } | |
191 | |
192 public Class< ? > toBoxedJavaClass() { | |
193 // Checkstyle: stop | |
194 switch(this) { | |
195 case Void: return null; | |
196 case Long: return java.lang.Long.class; | |
197 case Int: return java.lang.Integer.class; | |
198 case Byte: return java.lang.Byte.class; | |
199 case Char: return java.lang.Character.class; | |
200 case Double: return java.lang.Double.class; | |
201 case Float: return java.lang.Float.class; | |
202 case Short: return java.lang.Short.class; | |
203 case Boolean: return java.lang.Boolean.class; | |
204 default: return null; | |
205 } | |
206 // Checkstyle: resume | |
207 } | |
208 | |
209 /** | |
210 * Checks whether this value type is void. | |
211 * @return {@code true} if this type is void | |
212 */ | |
213 public final boolean isVoid() { | |
214 return this == RiKind.Void; | |
215 } | |
216 | |
217 /** | |
218 * Checks whether this value type is long. | |
219 * @return {@code true} if this type is long | |
220 */ | |
221 public final boolean isLong() { | |
222 return this == RiKind.Long; | |
223 } | |
224 | |
225 /** | |
226 * Checks whether this value type is float. | |
227 * @return {@code true} if this type is float | |
228 */ | |
229 public final boolean isFloat() { | |
230 return this == RiKind.Float; | |
231 } | |
232 | |
233 /** | |
234 * Checks whether this value type is double. | |
235 * @return {@code true} if this type is double | |
236 */ | |
237 public final boolean isDouble() { | |
238 return this == RiKind.Double; | |
239 } | |
240 | |
241 /** | |
242 * Checks whether this value type is float or double. | |
243 * @return {@code true} if this type is float or double | |
244 */ | |
245 public final boolean isFloatOrDouble() { | |
246 return this == RiKind.Double || this == RiKind.Float; | |
247 } | |
248 | |
249 /** | |
250 * Checks whether this value type is an object type. | |
251 * @return {@code true} if this type is an object | |
252 */ | |
253 public final boolean isObject() { | |
254 return this == RiKind.Object; | |
255 } | |
256 | |
257 /** | |
258 * Checks whether this value type is an address type. | |
259 * @return {@code true} if this type is an address | |
260 */ | |
261 public boolean isJsr() { | |
262 return this == RiKind.Jsr; | |
263 } | |
264 | |
265 /** | |
266 * Converts this value type to a string. | |
267 */ | |
268 @Override | |
269 public String toString() { | |
270 return javaName; | |
271 } | |
272 | |
273 /** | |
274 * Marker interface for types that should be {@linkplain RiKind#format(Object) formatted} | |
275 * with their {@link Object#toString()} value. | |
276 */ | |
277 public interface FormatWithToString {} | |
278 | |
279 /** | |
280 * Gets a formatted string for a given value of this kind. | |
281 * | |
282 * @param value a value of this kind | |
283 * @return a formatted string for {@code value} based on this kind | |
284 */ | |
285 public String format(Object value) { | |
286 if (isObject()) { | |
287 if (value == null) { | |
288 return "null"; | |
289 } else { | |
290 if (value instanceof String) { | |
291 String s = (String) value; | |
292 if (s.length() > 50) { | |
293 return "\"" + s.substring(0, 30) + "...\""; | |
294 } else { | |
295 return " \"" + s + '"'; | |
296 } | |
297 } else if (value instanceof RiType) { | |
298 return "class " + CiUtil.toJavaName((RiType) value); | |
299 } else if (value instanceof Enum || value instanceof FormatWithToString) { | |
300 return String.valueOf(value); | |
301 } else if (value instanceof Class< ? >) { | |
302 return ((Class< ? >) value).getName() + ".class"; | |
303 } else if (value.getClass().isArray()) { | |
304 return formatArray(value); | |
305 } else { | |
306 return CiUtil.getSimpleName(value.getClass(), true) + "@" + System.identityHashCode(value); | |
307 } | |
308 } | |
309 } else { | |
310 return value.toString(); | |
311 } | |
312 } | |
313 | |
314 private static final int MAX_FORMAT_ARRAY_LENGTH = Integer.getInteger("maxFormatArrayLength", 5); | |
315 | |
316 private static String formatArray(Object array) { | |
317 Class< ? > componentType = array.getClass().getComponentType(); | |
318 assert componentType != null; | |
319 int arrayLength = Array.getLength(array); | |
320 StringBuilder buf = new StringBuilder(CiUtil.getSimpleName(componentType, true)). | |
321 append('['). | |
322 append(arrayLength). | |
323 append("]{"); | |
324 int length = Math.min(MAX_FORMAT_ARRAY_LENGTH, arrayLength); | |
325 boolean primitive = componentType.isPrimitive(); | |
326 for (int i = 0; i < length; i++) { | |
327 if (primitive) { | |
328 buf.append(Array.get(array, i)); | |
329 } else { | |
330 Object o = ((Object[]) array)[i]; | |
331 buf.append(RiKind.Object.format(o)); | |
332 } | |
333 if (i != length - 1) { | |
334 buf.append(", "); | |
335 } | |
336 } | |
337 if (arrayLength != length) { | |
338 buf.append(", ..."); | |
339 } | |
340 return buf.append('}').toString(); | |
341 } | |
342 | |
343 public final char signatureChar() { | |
344 return Character.toUpperCase(typeChar); | |
345 } | |
346 | |
347 public final int arrayBaseOffset() { | |
348 switch(this) { | |
349 case Boolean: | |
350 return Unsafe.ARRAY_BOOLEAN_BASE_OFFSET; | |
351 case Byte: | |
352 return Unsafe.ARRAY_BYTE_BASE_OFFSET; | |
353 case Char: | |
354 return Unsafe.ARRAY_CHAR_BASE_OFFSET; | |
355 case Short: | |
356 return Unsafe.ARRAY_SHORT_BASE_OFFSET; | |
357 case Int: | |
358 return Unsafe.ARRAY_INT_BASE_OFFSET; | |
359 case Long: | |
360 return Unsafe.ARRAY_LONG_BASE_OFFSET; | |
361 case Float: | |
362 return Unsafe.ARRAY_FLOAT_BASE_OFFSET; | |
363 case Double: | |
364 return Unsafe.ARRAY_DOUBLE_BASE_OFFSET; | |
365 case Object: | |
366 return Unsafe.ARRAY_OBJECT_BASE_OFFSET; | |
367 default: | |
368 assert false : "unexpected kind: " + this; | |
369 return -1; | |
370 } | |
371 } | |
372 | |
373 public final int arrayIndexScale() { | |
374 switch(this) { | |
375 case Boolean: | |
376 return Unsafe.ARRAY_BOOLEAN_INDEX_SCALE; | |
377 case Byte: | |
378 return Unsafe.ARRAY_BYTE_INDEX_SCALE; | |
379 case Char: | |
380 return Unsafe.ARRAY_CHAR_INDEX_SCALE; | |
381 case Short: | |
382 return Unsafe.ARRAY_SHORT_INDEX_SCALE; | |
383 case Int: | |
384 return Unsafe.ARRAY_INT_INDEX_SCALE; | |
385 case Long: | |
386 return Unsafe.ARRAY_LONG_INDEX_SCALE; | |
387 case Float: | |
388 return Unsafe.ARRAY_FLOAT_INDEX_SCALE; | |
389 case Double: | |
390 return Unsafe.ARRAY_DOUBLE_INDEX_SCALE; | |
391 case Object: | |
392 return Unsafe.ARRAY_OBJECT_INDEX_SCALE; | |
393 default: | |
394 assert false : "unexpected kind: " + this; | |
395 return -1; | |
396 } | |
397 } | |
398 | |
399 public RiConstant readUnsafeConstant(Object value, long displacement) { | |
400 assert value != null; | |
401 Unsafe u = Unsafe.getUnsafe(); | |
402 switch(this) { | |
403 case Boolean: | |
404 return RiConstant.forBoolean(u.getBoolean(value, displacement)); | |
405 case Byte: | |
406 return RiConstant.forByte(u.getByte(value, displacement)); | |
407 case Char: | |
408 return RiConstant.forChar(u.getChar(value, displacement)); | |
409 case Short: | |
410 return RiConstant.forShort(u.getShort(value, displacement)); | |
411 case Int: | |
412 return RiConstant.forInt(u.getInt(value, displacement)); | |
413 case Long: | |
414 return RiConstant.forLong(u.getLong(value, displacement)); | |
415 case Float: | |
416 return RiConstant.forFloat(u.getFloat(value, displacement)); | |
417 case Double: | |
418 return RiConstant.forDouble(u.getDouble(value, displacement)); | |
419 case Object: | |
420 return RiConstant.forObject(u.getObject(value, displacement)); | |
421 default: | |
422 assert false : "unexpected kind: " + this; | |
423 return null; | |
424 } | |
425 } | |
426 | |
427 public long minValue() { | |
428 switch (this) { | |
429 case Boolean: | |
430 return 0; | |
431 case Byte: | |
432 return java.lang.Byte.MIN_VALUE; | |
433 case Char: | |
434 return java.lang.Character.MIN_VALUE; | |
435 case Short: | |
436 return java.lang.Short.MIN_VALUE; | |
437 case Jsr: | |
438 case Int: | |
439 return java.lang.Integer.MIN_VALUE; | |
440 case Long: | |
441 return java.lang.Long.MIN_VALUE; | |
442 default: | |
443 throw new IllegalArgumentException("illegal call to minValue on " + this); | |
444 } | |
445 } | |
446 | |
447 public long maxValue() { | |
448 switch (this) { | |
449 case Boolean: | |
450 return 1; | |
451 case Byte: | |
452 return java.lang.Byte.MAX_VALUE; | |
453 case Char: | |
454 return java.lang.Character.MAX_VALUE; | |
455 case Short: | |
456 return java.lang.Short.MAX_VALUE; | |
457 case Jsr: | |
458 case Int: | |
459 return java.lang.Integer.MAX_VALUE; | |
460 case Long: | |
461 return java.lang.Long.MAX_VALUE; | |
462 default: | |
463 throw new IllegalArgumentException("illegal call to maxValue on " + this); | |
464 } | |
465 } | |
466 | |
467 } |