changeset 19307:16a2ea2078bc

Truffle: simplify alignPrimitive
author Andreas Woess <andreas.woess@oracle.com>
date Wed, 11 Feb 2015 14:03:06 +0100
parents db637487949a
children 5b2589732c45
files graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/FrameWithoutBoxing.java graal/com.oracle.truffle.api/src/com/oracle/truffle/api/frame/FrameSlotKind.java
diffstat 2 files changed, 40 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/FrameWithoutBoxing.java	Wed Feb 11 16:28:10 2015 +0100
+++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/FrameWithoutBoxing.java	Wed Feb 11 14:03:06 2015 +0100
@@ -22,13 +22,11 @@
  */
 package com.oracle.graal.truffle;
 
-import java.nio.*;
 import java.lang.reflect.*;
 import java.util.*;
 
 import sun.misc.*;
 
-import com.oracle.graal.api.meta.*;
 import com.oracle.truffle.api.*;
 import com.oracle.truffle.api.frame.*;
 
@@ -107,7 +105,7 @@
     }
 
     private byte getByteUnsafe(FrameSlot slot) {
-        long offset = alignPrimitive(slot, Kind.Byte);
+        long offset = getPrimitiveOffset(slot, FrameSlotKind.Byte);
         boolean condition = this.getTags()[slot.getIndex()] == FrameSlotKind.Byte.ordinal();
         return unsafeGetByte(getPrimitiveLocals(), offset, condition, slot);
     }
@@ -119,7 +117,7 @@
     }
 
     private void setByteUnsafe(FrameSlot slot, byte value) {
-        long offset = alignPrimitive(slot, Kind.Boolean);
+        long offset = getPrimitiveOffset(slot, FrameSlotKind.Boolean);
         unsafePutByte(getPrimitiveLocals(), offset, value, slot);
     }
 
@@ -130,7 +128,7 @@
     }
 
     private boolean getBooleanUnsafe(FrameSlot slot) {
-        long offset = alignPrimitive(slot, Kind.Boolean);
+        long offset = getPrimitiveOffset(slot, FrameSlotKind.Boolean);
         boolean condition = this.getTags()[slot.getIndex()] == FrameSlotKind.Boolean.ordinal();
         return unsafeGetBoolean(getPrimitiveLocals(), offset, condition, slot);
     }
@@ -142,7 +140,7 @@
     }
 
     private void setBooleanUnsafe(FrameSlot slot, boolean value) {
-        long offset = alignPrimitive(slot, Kind.Boolean);
+        long offset = getPrimitiveOffset(slot, FrameSlotKind.Boolean);
         unsafePutBoolean(getPrimitiveLocals(), offset, value, slot);
     }
 
@@ -153,7 +151,7 @@
     }
 
     private float getFloatUnsafe(FrameSlot slot) {
-        long offset = alignPrimitive(slot, Kind.Float);
+        long offset = getPrimitiveOffset(slot, FrameSlotKind.Float);
         boolean condition = this.getTags()[slot.getIndex()] == FrameSlotKind.Float.ordinal();
         return unsafeGetFloat(getPrimitiveLocals(), offset, condition, slot);
     }
@@ -165,7 +163,7 @@
     }
 
     private void setFloatUnsafe(FrameSlot slot, float value) {
-        long offset = alignPrimitive(slot, Kind.Float);
+        long offset = getPrimitiveOffset(slot, FrameSlotKind.Float);
         unsafePutFloat(getPrimitiveLocals(), offset, value, slot);
     }
 
@@ -176,7 +174,7 @@
     }
 
     private long getLongUnsafe(FrameSlot slot) {
-        long offset = alignPrimitive(slot, Kind.Long);
+        long offset = getPrimitiveOffset(slot, FrameSlotKind.Long);
         boolean condition = this.getTags()[slot.getIndex()] == FrameSlotKind.Long.ordinal();
         return unsafeGetLong(getPrimitiveLocals(), offset, condition, slot);
     }
@@ -188,7 +186,7 @@
     }
 
     private void setLongUnsafe(FrameSlot slot, long value) {
-        long offset = alignPrimitive(slot, Kind.Long);
+        long offset = getPrimitiveOffset(slot, FrameSlotKind.Long);
         unsafePutLong(getPrimitiveLocals(), offset, value, slot);
     }
 
@@ -199,7 +197,7 @@
     }
 
     private int getIntUnsafe(FrameSlot slot) {
-        long offset = alignPrimitive(slot, Kind.Int);
+        long offset = getPrimitiveOffset(slot, FrameSlotKind.Int);
         boolean condition = this.getTags()[slot.getIndex()] == FrameSlotKind.Int.ordinal();
         return unsafeGetInt(getPrimitiveLocals(), offset, condition, slot);
     }
@@ -211,7 +209,7 @@
     }
 
     private void setIntUnsafe(FrameSlot slot, int value) {
-        long offset = alignPrimitive(slot, Kind.Int);
+        long offset = getPrimitiveOffset(slot, FrameSlotKind.Int);
         unsafePutInt(getPrimitiveLocals(), offset, value, slot);
     }
 
@@ -222,7 +220,7 @@
     }
 
     private double getDoubleUnsafe(FrameSlot slot) {
-        long offset = alignPrimitive(slot, Kind.Double);
+        long offset = getPrimitiveOffset(slot, FrameSlotKind.Double);
         boolean condition = this.getTags()[slot.getIndex()] == FrameSlotKind.Double.ordinal();
         return unsafeGetDouble(getPrimitiveLocals(), offset, condition, slot);
     }
@@ -234,7 +232,7 @@
     }
 
     private void setDoubleUnsafe(FrameSlot slot, double value) {
-        long offset = alignPrimitive(slot, Kind.Double);
+        long offset = getPrimitiveOffset(slot, FrameSlotKind.Double);
         unsafePutDouble(getPrimitiveLocals(), offset, value, slot);
     }
 
@@ -245,38 +243,34 @@
 
     private void verifySet(FrameSlot slot, FrameSlotKind accessKind) {
         int slotIndex = slot.getIndex();
-        if (slotIndex >= getTags().length) {
+        byte[] tagsArray = getTags();
+        if (slotIndex >= tagsArray.length) {
             CompilerDirectives.transferToInterpreter();
             if (!resize()) {
                 throw new IllegalArgumentException(String.format("The frame slot '%s' is not known by the frame descriptor.", slot));
             }
         }
-        getTags()[slotIndex] = (byte) accessKind.ordinal();
+        tagsArray[slotIndex] = (byte) accessKind.ordinal();
     }
 
     private void verifyGet(FrameSlot slot, FrameSlotKind accessKind) throws FrameSlotTypeException {
         int slotIndex = slot.getIndex();
-        if (slotIndex >= getTags().length) {
+        byte[] tagsArray = getTags();
+        if (slotIndex >= tagsArray.length) {
             CompilerDirectives.transferToInterpreter();
             if (!resize()) {
                 throw new IllegalArgumentException(String.format("The frame slot '%s' is not known by the frame descriptor.", slot));
             }
         }
-        byte tag = this.getTags()[slotIndex];
+        byte tag = tagsArray[slotIndex];
         if (tag != accessKind.ordinal()) {
             CompilerDirectives.transferToInterpreter();
             throw new FrameSlotTypeException();
         }
     }
 
-    private static long alignPrimitive(FrameSlot slot, Kind forKind) {
-        long offset = Unsafe.ARRAY_LONG_BASE_OFFSET + slot.getIndex() * (long) Unsafe.ARRAY_LONG_INDEX_SCALE;
-        if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
-            // On big endian, we use int in long type, which means, when byteCount() <=4, the value
-            // is right aligned in the 4 byte half, which has the lower address.
-            offset += Kind.Long.getByteCount() - Math.min((long) Unsafe.ARRAY_LONG_INDEX_SCALE, 4 + forKind.getByteCount());
-        }
-        return offset;
+    private static long getPrimitiveOffset(FrameSlot slot, FrameSlotKind forKind) {
+        return Unsafe.ARRAY_LONG_BASE_OFFSET + slot.getIndex() * (long) Unsafe.ARRAY_LONG_INDEX_SCALE + forKind.getByteOffset();
     }
 
     @Override
--- a/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/frame/FrameSlotKind.java	Wed Feb 11 16:28:10 2015 +0100
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/frame/FrameSlotKind.java	Wed Feb 11 14:03:06 2015 +0100
@@ -24,13 +24,25 @@
  */
 package com.oracle.truffle.api.frame;
 
+import java.nio.*;
+
 public enum FrameSlotKind {
-    Object,
-    Illegal,
-    Long,
-    Int,
-    Double,
-    Float,
-    Boolean,
-    Byte;
+    Object(0),
+    Illegal(0),
+    Long(0),
+    Int(0),
+    Double(0),
+    Float(0),
+    Boolean(3),
+    Byte(3);
+
+    private final int byteOffset;
+
+    private FrameSlotKind(int bigEndianByteOffset) {
+        this.byteOffset = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN ? bigEndianByteOffset : 0;
+    }
+
+    public int getByteOffset() {
+        return byteOffset;
+    }
 }