# HG changeset patch # User Doug Simon # Date 1352759832 -3600 # Node ID d09b0fed89db20e7e9eed5029b280379ec50aca0 # Parent e522a00b91aad4a113293c0c23cf5a6a8a5f807d added cache for Unsafe in Kind diff -r e522a00b91aa -r d09b0fed89db graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/Kind.java --- a/graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/Kind.java Mon Nov 12 23:14:12 2012 +0100 +++ b/graal/com.oracle.graal.api.meta/src/com/oracle/graal/api/meta/Kind.java Mon Nov 12 23:37:12 2012 +0100 @@ -97,7 +97,7 @@ /** * Checks whether this type is valid as an {@code int} on the Java operand stack. - * + * * @return {@code true} if this type is represented by an {@code int} on the operand stack */ public boolean isStackInt() { @@ -106,7 +106,7 @@ /** * Checks whether this type is a Java primitive type. - * + * * @return {@code true} if this is {@link #Boolean}, {@link #Byte}, {@link #Char}, {@link #Short}, {@link #Int}, * {@link #Long}, {@link #Float} or {@link #Double}. */ @@ -116,7 +116,7 @@ /** * Returns the kind that represents this kind when on the Java operand stack. - * + * * @return the kind used on the operand stack */ public Kind getStackKind() { @@ -128,7 +128,7 @@ /** * Returns the kind corresponding to the Java type string. - * + * * @param typeString the Java type string * @return the kind */ @@ -143,7 +143,7 @@ /** * Returns the kind from the character describing a primitive or void. - * + * * @param ch the character * @return the kind */ @@ -173,7 +173,7 @@ /** * Returns the Kind representing the given Java class. - * + * * @param klass the class * @return the kind */ @@ -203,7 +203,7 @@ /** * Returns the Java class representing this kind. - * + * * @return the Java class */ public Class< ? > toJavaClass() { @@ -233,7 +233,7 @@ /** * Returns the Java class for instances of boxed values of this kind. - * + * * @return the Java class */ public Class< ? > toBoxedJavaClass() { @@ -263,7 +263,7 @@ /** * Checks whether this value type is void. - * + * * @return {@code true} if this type is void */ public final boolean isVoid() { @@ -272,7 +272,7 @@ /** * Checks whether this value type is long. - * + * * @return {@code true} if this type is long */ public final boolean isLong() { @@ -281,7 +281,7 @@ /** * Checks whether this value type is float. - * + * * @return {@code true} if this type is float */ public final boolean isFloat() { @@ -290,7 +290,7 @@ /** * Checks whether this value type is double. - * + * * @return {@code true} if this type is double */ public final boolean isDouble() { @@ -299,7 +299,7 @@ /** * Checks whether this value type is float or double. - * + * * @return {@code true} if this type is float or double */ public final boolean isFloatOrDouble() { @@ -308,7 +308,7 @@ /** * Checks whether this value type is an object type. - * + * * @return {@code true} if this type is an object */ public final boolean isObject() { @@ -317,7 +317,7 @@ /** * Checks whether this value type is an address type. - * + * * @return {@code true} if this type is an address */ public boolean isJsr() { @@ -341,7 +341,7 @@ /** * Gets a formatted string for a given value of this kind. - * + * * @param value a value of this kind * @return a formatted string for {@code value} based on this kind */ @@ -402,7 +402,7 @@ /** * The offset from the origin of an array to the first element. - * + * * @return the offset in bytes */ public final int getArrayBaseOffset() { @@ -433,7 +433,7 @@ /** * The scale used for the index when accessing elements of an array of this kind. - * + * * @return the scale in order to convert the index into a byte offset */ public final int getArrayIndexScale() { @@ -462,35 +462,44 @@ } } + private static Unsafe unsafeCache; + + private static Unsafe unsafe() { + if (unsafeCache == null) { + unsafeCache = Unsafe.getUnsafe(); + } + return unsafeCache; + } + + /** * Utility function for reading a value of this kind using an object and a displacement. - * + * * @param object the object from which the value is read * @param displacement the displacement within the object in bytes * @return the read value encapsulated in a {@link Constant} object */ public Constant readUnsafeConstant(Object object, long displacement) { assert object != null; - Unsafe u = Unsafe.getUnsafe(); switch (this) { case Boolean: - return Constant.forBoolean(u.getBoolean(object, displacement)); + return Constant.forBoolean(unsafe().getBoolean(object, displacement)); case Byte: - return Constant.forByte(u.getByte(object, displacement)); + return Constant.forByte(unsafe().getByte(object, displacement)); case Char: - return Constant.forChar(u.getChar(object, displacement)); + return Constant.forChar(unsafe().getChar(object, displacement)); case Short: - return Constant.forShort(u.getShort(object, displacement)); + return Constant.forShort(unsafe().getShort(object, displacement)); case Int: - return Constant.forInt(u.getInt(object, displacement)); + return Constant.forInt(unsafe().getInt(object, displacement)); case Long: - return Constant.forLong(u.getLong(object, displacement)); + return Constant.forLong(unsafe().getLong(object, displacement)); case Float: - return Constant.forFloat(u.getFloat(object, displacement)); + return Constant.forFloat(unsafe().getFloat(object, displacement)); case Double: - return Constant.forDouble(u.getDouble(object, displacement)); + return Constant.forDouble(unsafe().getDouble(object, displacement)); case Object: - return Constant.forObject(u.getObject(object, displacement)); + return Constant.forObject(unsafe().getObject(object, displacement)); default: assert false : "unexpected kind: " + this; return null; @@ -499,7 +508,7 @@ /** * The minimum value that can be represented as a value of this kind. - * + * * @return the minimum value */ public long getMinValue() { @@ -524,7 +533,7 @@ /** * The maximum value that can be represented as a value of this kind. - * + * * @return the maximum value */ public long getMaxValue() { @@ -549,7 +558,7 @@ /** * Number of bits that are necessary to represent a value of this kind. - * + * * @return the number of bits */ public int getBitCount() {