1 /*
   2  * Copyright (c) 2015, 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 org.graalvm.compiler.core.common.util;
  24 
  25 import static org.graalvm.compiler.core.common.util.UnsafeAccess.UNSAFE;
  26 import sun.misc.Unsafe;
  27 
  28 /**
  29  * Provides low-level read access from a byte[] array for signed and unsigned values of size 1, 2,
  30  * 4, and 8 bytes.
  31  *
  32  * The class can either be instantiated for sequential access to the byte[] array; or static methods
  33  * can be used to read values without the overhead of creating an instance.
  34  *
  35  * The flag {@code supportsUnalignedMemoryAccess} must be set according to the capabilities of the
  36  * hardware architecture: the value {@code true} allows more efficient memory access on
  37  * architectures that support unaligned memory accesses; the value {@code false} is the safe
  38  * fallback that works on every hardware.
  39  */
  40 public abstract class UnsafeArrayTypeReader implements TypeReader {
  41 
  42     public static int getS1(byte[] data, long byteIndex) {
  43         return UNSAFE.getByte(data, readOffset(data, byteIndex, Byte.BYTES));
  44     }
  45 
  46     public static int getU1(byte[] data, long byteIndex) {
  47         return UNSAFE.getByte(data, readOffset(data, byteIndex, Byte.BYTES)) & 0xFF;
  48     }
  49 
  50     public static int getS2(byte[] data, long byteIndex, boolean supportsUnalignedMemoryAccess) {
  51         if (supportsUnalignedMemoryAccess) {
  52             return UnalignedUnsafeArrayTypeReader.getS2(data, byteIndex);
  53         } else {
  54             return AlignedUnsafeArrayTypeReader.getS2(data, byteIndex);
  55         }
  56     }
  57 
  58     public static int getU2(byte[] data, long byteIndex, boolean supportsUnalignedMemoryAccess) {
  59         return getS2(data, byteIndex, supportsUnalignedMemoryAccess) & 0xFFFF;
  60     }
  61 
  62     public static int getS4(byte[] data, long byteIndex, boolean supportsUnalignedMemoryAccess) {
  63         if (supportsUnalignedMemoryAccess) {
  64             return UnalignedUnsafeArrayTypeReader.getS4(data, byteIndex);
  65         } else {
  66             return AlignedUnsafeArrayTypeReader.getS4(data, byteIndex);
  67         }
  68     }
  69 
  70     public static long getU4(byte[] data, long byteIndex, boolean supportsUnalignedMemoryAccess) {
  71         return getS4(data, byteIndex, supportsUnalignedMemoryAccess) & 0xFFFFFFFFL;
  72     }
  73 
  74     public static long getS8(byte[] data, long byteIndex, boolean supportsUnalignedMemoryAccess) {
  75         if (supportsUnalignedMemoryAccess) {
  76             return UnalignedUnsafeArrayTypeReader.getS8(data, byteIndex);
  77         } else {
  78             return AlignedUnsafeArrayTypeReader.getS8(data, byteIndex);
  79         }
  80     }
  81 
  82     protected static long readOffset(byte[] data, long byteIndex, int numBytes) {
  83         assert byteIndex >= 0;
  84         assert numBytes > 0;
  85         assert byteIndex + numBytes <= data.length;
  86         assert Unsafe.ARRAY_BYTE_INDEX_SCALE == 1;
  87 
  88         return byteIndex + Unsafe.ARRAY_BYTE_BASE_OFFSET;
  89     }
  90 
  91     public static UnsafeArrayTypeReader create(byte[] data, long byteIndex, boolean supportsUnalignedMemoryAccess) {
  92         if (supportsUnalignedMemoryAccess) {
  93             return new UnalignedUnsafeArrayTypeReader(data, byteIndex);
  94         } else {
  95             return new AlignedUnsafeArrayTypeReader(data, byteIndex);
  96         }
  97     }
  98 
  99     protected final byte[] data;
 100     protected long byteIndex;
 101 
 102     protected UnsafeArrayTypeReader(byte[] data, long byteIndex) {
 103         this.data = data;
 104         this.byteIndex = byteIndex;
 105     }
 106 
 107     @Override
 108     public long getByteIndex() {
 109         return byteIndex;
 110     }
 111 
 112     @Override
 113     public void setByteIndex(long byteIndex) {
 114         this.byteIndex = byteIndex;
 115     }
 116 
 117     @Override
 118     public final int getS1() {
 119         int result = getS1(data, byteIndex);
 120         byteIndex += Byte.BYTES;
 121         return result;
 122     }
 123 
 124     @Override
 125     public final int getU1() {
 126         int result = getU1(data, byteIndex);
 127         byteIndex += Byte.BYTES;
 128         return result;
 129     }
 130 
 131     @Override
 132     public final int getU2() {
 133         return getS2() & 0xFFFF;
 134     }
 135 
 136     @Override
 137     public final long getU4() {
 138         return getS4() & 0xFFFFFFFFL;
 139     }
 140 }
 141 
 142 final class UnalignedUnsafeArrayTypeReader extends UnsafeArrayTypeReader {
 143     protected static int getS2(byte[] data, long byteIndex) {
 144         return UNSAFE.getShort(data, readOffset(data, byteIndex, Short.BYTES));
 145     }
 146 
 147     protected static int getS4(byte[] data, long byteIndex) {
 148         return UNSAFE.getInt(data, readOffset(data, byteIndex, Integer.BYTES));
 149     }
 150 
 151     protected static long getS8(byte[] data, long byteIndex) {
 152         return UNSAFE.getLong(data, readOffset(data, byteIndex, Long.BYTES));
 153     }
 154 
 155     protected UnalignedUnsafeArrayTypeReader(byte[] data, long byteIndex) {
 156         super(data, byteIndex);
 157     }
 158 
 159     @Override
 160     public int getS2() {
 161         int result = getS2(data, byteIndex);
 162         byteIndex += Short.BYTES;
 163         return result;
 164     }
 165 
 166     @Override
 167     public int getS4() {
 168         int result = getS4(data, byteIndex);
 169         byteIndex += Integer.BYTES;
 170         return result;
 171     }
 172 
 173     @Override
 174     public long getS8() {
 175         long result = getS8(data, byteIndex);
 176         byteIndex += Long.BYTES;
 177         return result;
 178     }
 179 }
 180 
 181 class AlignedUnsafeArrayTypeReader extends UnsafeArrayTypeReader {
 182     protected static int getS2(byte[] data, long byteIndex) {
 183         long offset = readOffset(data, byteIndex, Short.BYTES);
 184         return ((UNSAFE.getByte(data, offset + 0) & 0xFF) << 0) | //
 185                         (UNSAFE.getByte(data, offset + 1) << 8);
 186     }
 187 
 188     protected static int getS4(byte[] data, long byteIndex) {
 189         long offset = readOffset(data, byteIndex, Integer.BYTES);
 190         return ((UNSAFE.getByte(data, offset + 0) & 0xFF) << 0) | //
 191                         ((UNSAFE.getByte(data, offset + 1) & 0xFF) << 8) | //
 192                         ((UNSAFE.getByte(data, offset + 2) & 0xFF) << 16) | //
 193                         (UNSAFE.getByte(data, offset + 3) << 24);
 194     }
 195 
 196     protected static long getS8(byte[] data, long byteIndex) {
 197         long offset = readOffset(data, byteIndex, Long.BYTES);
 198         return ((long) ((UNSAFE.getByte(data, offset + 0) & 0xFF)) << 0) | //
 199                         ((long) ((UNSAFE.getByte(data, offset + 1) & 0xFF)) << 8) | //
 200                         ((long) ((UNSAFE.getByte(data, offset + 2) & 0xFF)) << 16) | //
 201                         ((long) ((UNSAFE.getByte(data, offset + 3) & 0xFF)) << 24) | //
 202                         ((long) ((UNSAFE.getByte(data, offset + 4) & 0xFF)) << 32) | //
 203                         ((long) ((UNSAFE.getByte(data, offset + 5) & 0xFF)) << 40) | //
 204                         ((long) ((UNSAFE.getByte(data, offset + 6) & 0xFF)) << 48) | //
 205                         ((long) (UNSAFE.getByte(data, offset + 7)) << 56);
 206     }
 207 
 208     protected AlignedUnsafeArrayTypeReader(byte[] data, long byteIndex) {
 209         super(data, byteIndex);
 210     }
 211 
 212     @Override
 213     public int getS2() {
 214         int result = getS2(data, byteIndex);
 215         byteIndex += Short.BYTES;
 216         return result;
 217     }
 218 
 219     @Override
 220     public int getS4() {
 221         int result = getS4(data, byteIndex);
 222         byteIndex += Integer.BYTES;
 223         return result;
 224     }
 225 
 226     @Override
 227     public long getS8() {
 228         long result = getS8(data, byteIndex);
 229         byteIndex += Long.BYTES;
 230         return result;
 231     }
 232 }