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 sequential write access for signed and unsigned values of size 1, 2, 4, and 8 27 * bytes. 28 */ 29 public interface TypeWriter { 30 31 /** 32 * Returns the number of bytes that have been written, i.e., the byte index of the next byte to 33 * be written. 34 */ 35 long getBytesWritten(); 36 37 /** Writes a signed 1 byte value. */ 38 void putS1(long value); 39 40 /** Writes an unsigned 1 byte value. */ 41 void putU1(long value); 42 43 /** Writes a signed 2 byte value. */ 44 void putS2(long value); 45 46 /** Writes an unsigned 2 byte value. */ 47 void putU2(long value); 48 49 /** Writes a signed 4 byte value. */ 50 void putS4(long value); 51 52 /** Writes an unsigned 4 byte value. */ 53 void putU4(long value); 54 55 /** Writes a signed 8 byte value. */ 56 void putS8(long value); 57 58 /** 59 * Writes a signed value in a variable byte size encoding. 60 */ 61 default void putSV(long value) { 62 long cur = value; 63 while (true) { 64 if (cur >= -64 && cur < 64) { 65 putU1(cur & 0x7f); 66 return; 67 } 68 putU1(0x80 | (cur & 0x7f)); 69 cur = cur >> 7; 70 } 71 } 72 73 /** 74 * Writes an unsigned value in a variable byte size encoding. 75 */ 76 default void putUV(long value) { 77 long cur = value; 78 while (true) { 79 assert cur >= 0; 80 if (cur < 128) { 81 putU1(cur & 0x7f); 82 return; 83 } 84 putU1(0x80 | (cur & 0x7f)); 85 cur = cur >> 7; 86 } 87 } 88 }