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

     1  // Copyright 2021 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 pkgbits
     6  
     7  import (
     8  	"github.com/shogo82148/std/go/constant"
     9  	"github.com/shogo82148/std/strings"
    10  )
    11  
    12  // A PkgDecoder provides methods for decoding a package's Unified IR
    13  // export data.
    14  type PkgDecoder struct {
    15  	// version is the file format version.
    16  	version uint32
    17  
    18  	// sync indicates whether the file uses sync markers.
    19  	sync bool
    20  
    21  	// pkgPath is the package path for the package to be decoded.
    22  	//
    23  	// TODO(mdempsky): Remove; unneeded since CL 391014.
    24  	pkgPath string
    25  
    26  	// elemData is the full data payload of the encoded package.
    27  	// Elements are densely and contiguously packed together.
    28  	//
    29  	// The last 8 bytes of elemData are the package fingerprint.
    30  	elemData string
    31  
    32  	// elemEnds stores the byte-offset end positions of element
    33  	// bitstreams within elemData.
    34  	//
    35  	// For example, element I's bitstream data starts at elemEnds[I-1]
    36  	// (or 0, if I==0) and ends at elemEnds[I].
    37  	//
    38  	// Note: elemEnds is indexed by absolute indices, not
    39  	// section-relative indices.
    40  	elemEnds []uint32
    41  
    42  	// elemEndsEnds stores the index-offset end positions of relocation
    43  	// sections within elemEnds.
    44  	//
    45  	// For example, section K's end positions start at elemEndsEnds[K-1]
    46  	// (or 0, if K==0) and end at elemEndsEnds[K].
    47  	elemEndsEnds [numRelocs]uint32
    48  
    49  	scratchRelocEnt []RelocEnt
    50  }
    51  
    52  // PkgPath returns the package path for the package
    53  //
    54  // TODO(mdempsky): Remove; unneeded since CL 391014.
    55  func (pr *PkgDecoder) PkgPath() string
    56  
    57  // SyncMarkers reports whether pr uses sync markers.
    58  func (pr *PkgDecoder) SyncMarkers() bool
    59  
    60  // NewPkgDecoder returns a PkgDecoder initialized to read the Unified
    61  // IR export data from input. pkgPath is the package path for the
    62  // compilation unit that produced the export data.
    63  //
    64  // TODO(mdempsky): Remove pkgPath parameter; unneeded since CL 391014.
    65  func NewPkgDecoder(pkgPath, input string) PkgDecoder
    66  
    67  // NumElems returns the number of elements in section k.
    68  func (pr *PkgDecoder) NumElems(k RelocKind) int
    69  
    70  // TotalElems returns the total number of elements across all sections.
    71  func (pr *PkgDecoder) TotalElems() int
    72  
    73  // Fingerprint returns the package fingerprint.
    74  func (pr *PkgDecoder) Fingerprint() [8]byte
    75  
    76  // AbsIdx returns the absolute index for the given (section, index)
    77  // pair.
    78  func (pr *PkgDecoder) AbsIdx(k RelocKind, idx Index) int
    79  
    80  // DataIdx returns the raw element bitstream for the given (section,
    81  // index) pair.
    82  func (pr *PkgDecoder) DataIdx(k RelocKind, idx Index) string
    83  
    84  // StringIdx returns the string value for the given string index.
    85  func (pr *PkgDecoder) StringIdx(idx Index) string
    86  
    87  // NewDecoder returns a Decoder for the given (section, index) pair,
    88  // and decodes the given SyncMarker from the element bitstream.
    89  func (pr *PkgDecoder) NewDecoder(k RelocKind, idx Index, marker SyncMarker) Decoder
    90  
    91  // TempDecoder returns a Decoder for the given (section, index) pair,
    92  // and decodes the given SyncMarker from the element bitstream.
    93  // If possible the Decoder should be RetireDecoder'd when it is no longer
    94  // needed, this will avoid heap allocations.
    95  func (pr *PkgDecoder) TempDecoder(k RelocKind, idx Index, marker SyncMarker) Decoder
    96  
    97  func (pr *PkgDecoder) RetireDecoder(d *Decoder)
    98  
    99  // NewDecoderRaw returns a Decoder for the given (section, index) pair.
   100  //
   101  // Most callers should use NewDecoder instead.
   102  func (pr *PkgDecoder) NewDecoderRaw(k RelocKind, idx Index) Decoder
   103  
   104  func (pr *PkgDecoder) TempDecoderRaw(k RelocKind, idx Index) Decoder
   105  
   106  // A Decoder provides methods for decoding an individual element's
   107  // bitstream data.
   108  type Decoder struct {
   109  	common *PkgDecoder
   110  
   111  	Relocs []RelocEnt
   112  	Data   strings.Reader
   113  
   114  	k   RelocKind
   115  	Idx Index
   116  }
   117  
   118  // Sync decodes a sync marker from the element bitstream and asserts
   119  // that it matches the expected marker.
   120  //
   121  // If EnableSync is false, then Sync is a no-op.
   122  func (r *Decoder) Sync(mWant SyncMarker)
   123  
   124  // Bool decodes and returns a bool value from the element bitstream.
   125  func (r *Decoder) Bool() bool
   126  
   127  // Int64 decodes and returns an int64 value from the element bitstream.
   128  func (r *Decoder) Int64() int64
   129  
   130  // Int64 decodes and returns a uint64 value from the element bitstream.
   131  func (r *Decoder) Uint64() uint64
   132  
   133  // Len decodes and returns a non-negative int value from the element bitstream.
   134  func (r *Decoder) Len() int
   135  
   136  // Int decodes and returns an int value from the element bitstream.
   137  func (r *Decoder) Int() int
   138  
   139  // Uint decodes and returns a uint value from the element bitstream.
   140  func (r *Decoder) Uint() uint
   141  
   142  // Code decodes a Code value from the element bitstream and returns
   143  // its ordinal value. It's the caller's responsibility to convert the
   144  // result to an appropriate Code type.
   145  //
   146  // TODO(mdempsky): Ideally this method would have signature "Code[T
   147  // Code] T" instead, but we don't allow generic methods and the
   148  // compiler can't depend on generics yet anyway.
   149  func (r *Decoder) Code(mark SyncMarker) int
   150  
   151  // Reloc decodes a relocation of expected section k from the element
   152  // bitstream and returns an index to the referenced element.
   153  func (r *Decoder) Reloc(k RelocKind) Index
   154  
   155  // String decodes and returns a string value from the element
   156  // bitstream.
   157  func (r *Decoder) String() string
   158  
   159  // Strings decodes and returns a variable-length slice of strings from
   160  // the element bitstream.
   161  func (r *Decoder) Strings() []string
   162  
   163  // Value decodes and returns a constant.Value from the element
   164  // bitstream.
   165  func (r *Decoder) Value() constant.Value
   166  
   167  // PeekPkgPath returns the package path for the specified package
   168  // index.
   169  func (pr *PkgDecoder) PeekPkgPath(idx Index) string
   170  
   171  // PeekObj returns the package path, object name, and CodeObj for the
   172  // specified object index.
   173  func (pr *PkgDecoder) PeekObj(idx Index) (string, string, CodeObj)