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