github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/cmd/internal/gcprog/gcprog.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package gcprog implements an encoder for packed GC pointer bitmaps,
     6  // known as GC programs.
     7  //
     8  // # Program Format
     9  //
    10  // The GC program encodes a sequence of 0 and 1 bits indicating scalar or pointer words in an object.
    11  // The encoding is a simple Lempel-Ziv program, with codes to emit literal bits and to repeat the
    12  // last n bits c times.
    13  //
    14  // The possible codes are:
    15  //
    16  //	00000000: stop
    17  //	0nnnnnnn: emit n bits copied from the next (n+7)/8 bytes, least significant bit first
    18  //	10000000 n c: repeat the previous n bits c times; n, c are varints
    19  //	1nnnnnnn c: repeat the previous n bits c times; c is a varint
    20  //
    21  // The numbers n and c, when they follow a code, are encoded as varints
    22  // using the same encoding as encoding/binary's Uvarint.
    23  package gcprog
    24  
    25  import (
    26  	"github.com/shogo82148/std/io"
    27  )
    28  
    29  // A Writer is an encoder for GC programs.
    30  //
    31  // The typical use of a Writer is to call Init, maybe call Debug,
    32  // make a sequence of Ptr, Advance, Repeat, and Append calls
    33  // to describe the data type, and then finally call End.
    34  type Writer struct {
    35  	writeByte func(byte)
    36  	index     int64
    37  	b         [progMaxLiteral]byte
    38  	nb        int
    39  	debug     io.Writer
    40  	debugBuf  []byte
    41  }
    42  
    43  // Init initializes w to write a new GC program
    44  // by calling writeByte for each byte in the program.
    45  func (w *Writer) Init(writeByte func(byte))
    46  
    47  // Debug causes the writer to print a debugging trace to out
    48  // during future calls to methods like Ptr, Advance, and End.
    49  // It also enables debugging checks during the encoding.
    50  func (w *Writer) Debug(out io.Writer)
    51  
    52  // BitIndex returns the number of bits written to the bit stream so far.
    53  func (w *Writer) BitIndex() int64
    54  
    55  // End marks the end of the program, writing any remaining bytes.
    56  func (w *Writer) End()
    57  
    58  // Ptr emits a 1 into the bit stream at the given bit index.
    59  // that is, it records that the index'th word in the object memory is a pointer.
    60  // Any bits between the current index and the new index
    61  // are set to zero, meaning the corresponding words are scalars.
    62  func (w *Writer) Ptr(index int64)
    63  
    64  // ShouldRepeat reports whether it would be worthwhile to
    65  // use a Repeat to describe c elements of n bits each,
    66  // compared to just emitting c copies of the n-bit description.
    67  func (w *Writer) ShouldRepeat(n, c int64) bool
    68  
    69  // Repeat emits an instruction to repeat the description
    70  // of the last n words c times (including the initial description, c+1 times in total).
    71  func (w *Writer) Repeat(n, c int64)
    72  
    73  // ZeroUntil adds zeros to the bit stream until reaching the given index;
    74  // that is, it records that the words from the most recent pointer until
    75  // the index'th word are scalars.
    76  // ZeroUntil is usually called in preparation for a call to Repeat, Append, or End.
    77  func (w *Writer) ZeroUntil(index int64)
    78  
    79  // Append emits the given GC program into the current output.
    80  // The caller asserts that the program emits n bits (describes n words),
    81  // and Append panics if that is not true.
    82  func (w *Writer) Append(prog []byte, n int64)