001/*
002 * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.compiler.common.util;
024
025import jdk.internal.jvmci.common.*;
026import sun.misc.*;
027
028/**
029 * Provides low-level read access from a byte[] array for signed and unsigned values of size 1, 2,
030 * 4, and 8 bytes.
031 *
032 * The class can either be instantiated for sequential access to the byte[] array; or static methods
033 * can be used to read values without the overhead of creating an instance.
034 *
035 * The flag {@code supportsUnalignedMemoryAccess} must be set according to the capabilities of the
036 * hardware architecture: the value {@code true} allows more efficient memory access on
037 * architectures that support unaligned memory accesses; the value {@code false} is the safe
038 * fallback that works on every hardware.
039 */
040public abstract class UnsafeArrayTypeReader implements TypeReader {
041
042    public static int getS1(byte[] data, long byteIndex) {
043        return UnsafeAccess.unsafe.getByte(data, readOffset(data, byteIndex, Byte.BYTES));
044    }
045
046    public static int getU1(byte[] data, long byteIndex) {
047        return UnsafeAccess.unsafe.getByte(data, readOffset(data, byteIndex, Byte.BYTES)) & 0xFF;
048    }
049
050    public static int getS2(byte[] data, long byteIndex, boolean supportsUnalignedMemoryAccess) {
051        if (supportsUnalignedMemoryAccess) {
052            return UnalignedUnsafeArrayTypeReader.getS2(data, byteIndex);
053        } else {
054            return AlignedUnsafeArrayTypeReader.getS2(data, byteIndex);
055        }
056    }
057
058    public static int getU2(byte[] data, long byteIndex, boolean supportsUnalignedMemoryAccess) {
059        return getS2(data, byteIndex, supportsUnalignedMemoryAccess) & 0xFFFF;
060    }
061
062    public static int getS4(byte[] data, long byteIndex, boolean supportsUnalignedMemoryAccess) {
063        if (supportsUnalignedMemoryAccess) {
064            return UnalignedUnsafeArrayTypeReader.getS4(data, byteIndex);
065        } else {
066            return AlignedUnsafeArrayTypeReader.getS4(data, byteIndex);
067        }
068    }
069
070    public static long getU4(byte[] data, long byteIndex, boolean supportsUnalignedMemoryAccess) {
071        return getS4(data, byteIndex, supportsUnalignedMemoryAccess) & 0xFFFFFFFFL;
072    }
073
074    public static long getS8(byte[] data, long byteIndex, boolean supportsUnalignedMemoryAccess) {
075        if (supportsUnalignedMemoryAccess) {
076            return UnalignedUnsafeArrayTypeReader.getS8(data, byteIndex);
077        } else {
078            return AlignedUnsafeArrayTypeReader.getS8(data, byteIndex);
079        }
080    }
081
082    protected static long readOffset(byte[] data, long byteIndex, int numBytes) {
083        assert byteIndex >= 0;
084        assert numBytes > 0;
085        assert byteIndex + numBytes <= data.length;
086        assert Unsafe.ARRAY_BYTE_INDEX_SCALE == 1;
087
088        return byteIndex + Unsafe.ARRAY_BYTE_BASE_OFFSET;
089    }
090
091    public static UnsafeArrayTypeReader create(byte[] data, long byteIndex, boolean supportsUnalignedMemoryAccess) {
092        if (supportsUnalignedMemoryAccess) {
093            return new UnalignedUnsafeArrayTypeReader(data, byteIndex);
094        } else {
095            return new AlignedUnsafeArrayTypeReader(data, byteIndex);
096        }
097    }
098
099    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
142final class UnalignedUnsafeArrayTypeReader extends UnsafeArrayTypeReader {
143    protected static int getS2(byte[] data, long byteIndex) {
144        return UnsafeAccess.unsafe.getShort(data, readOffset(data, byteIndex, Short.BYTES));
145    }
146
147    protected static int getS4(byte[] data, long byteIndex) {
148        return UnsafeAccess.unsafe.getInt(data, readOffset(data, byteIndex, Integer.BYTES));
149    }
150
151    protected static long getS8(byte[] data, long byteIndex) {
152        return UnsafeAccess.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
181class AlignedUnsafeArrayTypeReader extends UnsafeArrayTypeReader {
182    protected static int getS2(byte[] data, long byteIndex) {
183        long offset = readOffset(data, byteIndex, Short.BYTES);
184        return ((UnsafeAccess.unsafe.getByte(data, offset + 0) & 0xFF) << 0) | //
185                        (UnsafeAccess.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 ((UnsafeAccess.unsafe.getByte(data, offset + 0) & 0xFF) << 0) | //
191                        ((UnsafeAccess.unsafe.getByte(data, offset + 1) & 0xFF) << 8) | //
192                        ((UnsafeAccess.unsafe.getByte(data, offset + 2) & 0xFF) << 16) | //
193                        (UnsafeAccess.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) ((UnsafeAccess.unsafe.getByte(data, offset + 0) & 0xFF)) << 0) | //
199                        ((long) ((UnsafeAccess.unsafe.getByte(data, offset + 1) & 0xFF)) << 8) | //
200                        ((long) ((UnsafeAccess.unsafe.getByte(data, offset + 2) & 0xFF)) << 16) | //
201                        ((long) ((UnsafeAccess.unsafe.getByte(data, offset + 3) & 0xFF)) << 24) | //
202                        ((long) ((UnsafeAccess.unsafe.getByte(data, offset + 4) & 0xFF)) << 32) | //
203                        ((long) ((UnsafeAccess.unsafe.getByte(data, offset + 5) & 0xFF)) << 40) | //
204                        ((long) ((UnsafeAccess.unsafe.getByte(data, offset + 6) & 0xFF)) << 48) | //
205                        ((long) (UnsafeAccess.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}