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 /** 26 * Provides low-level read access for signed and unsigned values of size 1, 2, 4, and 8 bytes. 27 */ 28 public interface TypeReader { 29 30 /** Returns the next byte index to be read. */ 31 long getByteIndex(); 32 33 /** Sets the next byte index to be read. */ 34 void setByteIndex(long byteIndex); 35 36 /** Reads a signed 1 byte value. */ 37 int getS1(); 38 39 /** Reads an unsigned 1 byte value. */ 40 int getU1(); 41 42 /** Reads a signed 2 byte value. */ 43 int getS2(); 44 45 /** Reads an unsigned 2 byte value. */ 46 int getU2(); 47 48 /** Reads a signed 4 byte value. */ 49 int getS4(); 50 51 /** Reads an unsigned 4 byte value. */ 52 long getU4(); 53 54 /** Reads a signed 4 byte value. */ 55 long getS8(); 56 57 /** 58 * Reads a signed value that has been written using {@link TypeWriter#putSV variable byte size 59 * encoding}. 60 */ 61 default long getSV() { 62 long result = 0; 63 int shift = 0; 64 long b; 65 do { 66 b = getU1(); 67 result |= (b & 0x7f) << shift; 68 shift += 7; 69 } while ((b & 0x80) != 0); 70 71 if ((b & 0x40) != 0 && shift < 64) { 72 result |= -1L << shift; 73 } 74 return result; 75 } 76 77 /** 78 * Reads a signed variable byte size encoded value that is known to fit into the range of int. 79 */ 80 default int getSVInt() { 81 return TypeConversion.asS4(getSV()); 82 } 83 84 /** 85 * Reads an unsigned value that has been written using {@link TypeWriter#putSV variable byte 86 * size encoding}. 87 */ 88 default long getUV() { 89 long result = 0; 90 int shift = 0; 91 long b; 92 do { 93 b = getU1(); 94 result |= (b & 0x7f) << shift; 95 shift += 7; 96 } while ((b & 0x80) != 0); 97 98 return result; 99 } 100 101 /** 102 * Reads an unsigned variable byte size encoded value that is known to fit into the range of 103 * int. 104 */ 105 default int getUVInt() { 106 return TypeConversion.asS4(getUV()); 107 } 108 }