Movatterモバイル変換


[0]ホーム

URL:


pkgbits

packagestandard library
go1.25.2Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 7, 2025 License:BSD-3-ClauseImports:13Imported by:0

Details

Repository

cs.opensource.google/go/go

Links

Documentation

Overview

Package pkgbits implements low-level coding abstractions for Unified IR's(UIR) binary export data format.

At a low-level, the exported objects of a package are encoded as a bytearray. This array contains byte representations of primitive, potentiallyvariable-length values, such as integers, booleans, strings, and constants.

Additionally, the array may contain values which denote indices in the bytearray itself. These are termed "relocations" and allow for references.

The details of mapping high-level Go constructs to primitives are left toother packages.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

typeAbsElemIdxadded ingo1.25.0

type AbsElemIdx =uint32

An AbsElemIdx, or absolute element index, is an index into the elementsthat is not relative to some other index.

typeCode

type Code interface {// Marker returns the SyncMarker for the Code's dynamic type.Marker()SyncMarker// Value returns the Code's ordinal value.Value()int}

A Code is an enum value that can be encoded into bitstreams.

Code types are preferable for enum types, because they allowDecoder to detect desyncs.

typeCodeObj

type CodeObjint

A CodeObj distinguishes among go/types.Object encodings.

const (ObjAliasCodeObj =iotaObjConstObjTypeObjFuncObjVarObjStub)

func (CodeObj)Marker

func (cCodeObj) Marker()SyncMarker

func (CodeObj)Value

func (cCodeObj) Value()int

typeCodeType

type CodeTypeint

A CodeType distinguishes among go/types.Type encodings.

const (TypeBasicCodeType =iotaTypeNamedTypePointerTypeSliceTypeArrayTypeChanTypeMapTypeSignatureTypeStructTypeInterfaceTypeUnionTypeTypeParam)

func (CodeType)Marker

func (cCodeType) Marker()SyncMarker

func (CodeType)Value

func (cCodeType) Value()int

typeCodeVal

type CodeValint

A CodeVal distinguishes among go/constant.Value encodings.

const (ValBoolCodeVal =iotaValStringValInt64ValBigIntValBigRatValBigFloat)

func (CodeVal)Marker

func (cCodeVal) Marker()SyncMarker

func (CodeVal)Value

func (cCodeVal) Value()int

typeDecoder

type Decoder struct {Relocs []RefTableEntryDatastrings.ReaderIdxRelElemIdx// contains filtered or unexported fields}

A Decoder provides methods for decoding an individual element'sbitstream data.

func (*Decoder)Bool

func (r *Decoder) Bool()bool

Bool decodes and returns a bool value from the element bitstream.

func (*Decoder)Code

func (r *Decoder) Code(markSyncMarker)int

Code decodes a Code value from the element bitstream and returnsits ordinal value. It's the caller's responsibility to convert theresult to an appropriate Code type.

TODO(mdempsky): Ideally this method would have signature "Code[TCode] T" instead, but we don't allow generic methods and thecompiler can't depend on generics yet anyway.

func (*Decoder)Int

func (r *Decoder) Int()int

Int decodes and returns an int value from the element bitstream.

func (*Decoder)Int64

func (r *Decoder) Int64()int64

Int64 decodes and returns an int64 value from the element bitstream.

func (*Decoder)Len

func (r *Decoder) Len()int

Len decodes and returns a non-negative int value from the element bitstream.

func (*Decoder)Reloc

func (r *Decoder) Reloc(kSectionKind)RelElemIdx

Reloc decodes a relocation of expected section k from the elementbitstream and returns an index to the referenced element.

func (*Decoder)String

func (r *Decoder) String()string

String decodes and returns a string value from the elementbitstream.

func (*Decoder)Strings

func (r *Decoder) Strings() []string

Strings decodes and returns a variable-length slice of strings fromthe element bitstream.

func (*Decoder)Sync

func (r *Decoder) Sync(mWantSyncMarker)

Sync decodes a sync marker from the element bitstream and assertsthat it matches the expected marker.

If EnableSync is false, then Sync is a no-op.

func (*Decoder)Uint

func (r *Decoder) Uint()uint

Uint decodes and returns a uint value from the element bitstream.

func (*Decoder)Uint64

func (r *Decoder) Uint64()uint64

Uint64 decodes and returns a uint64 value from the element bitstream.

func (*Decoder)Value

func (r *Decoder) Value()constant.Value

Value decodes and returns a constant.Value from the elementbitstream.

func (*Decoder)Versionadded ingo1.24.0

func (w *Decoder) Version()Version

Version reports the version of the bitstream.

typeEncoder

type Encoder struct {Relocs   []RefTableEntryRelocMap map[RefTableEntry]uint32Databytes.Buffer// accumulated element bitstream dataIdxRelElemIdx// index within relocation section// contains filtered or unexported fields}

An Encoder provides methods for encoding an individual element'sbitstream data.

func (*Encoder)Bool

func (w *Encoder) Bool(bbool)bool

Bool encodes and writes a bool value into the element bitstream,and then returns the bool value.

For simple, 2-alternative encodings, the idiomatic way to call Boolis something like:

if w.Bool(x != 0) {// alternative #1} else {// alternative #2}

For multi-alternative encodings, use Code instead.

func (*Encoder)Code

func (w *Encoder) Code(cCode)

Code encodes and writes a Code value into the element bitstream.

func (*Encoder)Flush

func (w *Encoder) Flush()RelElemIdx

Flush finalizes the element's bitstream and returns itsRelElemIdx.

func (*Encoder)Int

func (w *Encoder) Int(xint)

Int encodes and writes an int value into the element bitstream.

func (*Encoder)Int64

func (w *Encoder) Int64(xint64)

Int64 encodes and writes an int64 value into the element bitstream.

func (*Encoder)Len

func (w *Encoder) Len(xint)

Len encodes and writes a non-negative int value into the element bitstream.

func (*Encoder)Reloc

func (w *Encoder) Reloc(kSectionKind, idxRelElemIdx)

Reloc encodes and writes a relocation for the given (section,index) pair into the element bitstream.

Note: Only the index is formally written into the elementbitstream, so bitstream decoders must know from context whichsection an encoded relocation refers to.

func (*Encoder)String

func (w *Encoder) String(sstring)

String encodes and writes a string value into the elementbitstream.

Internally, strings are deduplicated by adding them to the stringssection (if not already present), and then writing a relocationinto the element bitstream.

func (*Encoder)StringRefadded ingo1.20

func (w *Encoder) StringRef(idxRelElemIdx)

StringRef writes a reference to the given index, which must be apreviously encoded string value.

func (*Encoder)Strings

func (w *Encoder) Strings(ss []string)

Strings encodes and writes a variable-length slice of strings intothe element bitstream.

func (*Encoder)Sync

func (w *Encoder) Sync(mSyncMarker)

func (*Encoder)Uint

func (w *Encoder) Uint(xuint)

Uint encodes and writes a uint value into the element bitstream.

func (*Encoder)Uint64

func (w *Encoder) Uint64(xuint64)

Uint64 encodes and writes a uint64 value into the element bitstream.

func (*Encoder)Value

func (w *Encoder) Value(valconstant.Value)

Value encodes and writes a constant.Value into the elementbitstream.

func (*Encoder)Versionadded ingo1.24.0

func (w *Encoder) Version()Version

Version reports the version of the bitstream.

typeFieldadded ingo1.24.0

type Fieldint

Field denotes a unit of data in the serialized unified IR bitstream.It is conceptually a like field in a structure.

We only really need Fields when the data may or may not be presentin a stream based on the Version of the bitstream.

Unlike much of pkgbits, Fields are not serialized andcan change values as needed.

const (// Flags in a uint32 in the header of a bitstream// that is used to indicate whether optional features are enabled.FlagsField =iota// Deprecated: HasInit was a bool indicating whether a package// has any init functions.HasInit// Deprecated: DerivedFuncInstance was a bool indicating// whether an object was a function instance.DerivedFuncInstance// ObjAlias has a list of TypeParamNames.AliasTypeParamNames// Deprecated: DerivedInfoNeeded was a bool indicating// whether a type was a derived type.DerivedInfoNeeded)

typeIndex

type Indexint32

An Index represents a bitstream element index *within* (i.e., relative to) aparticular section.

typePkgDecoder

type PkgDecoder struct {// contains filtered or unexported fields}

A PkgDecoder provides methods for decoding a package's Unified IRexport data.

funcNewPkgDecoder

func NewPkgDecoder(pkgPath, inputstring)PkgDecoder

NewPkgDecoder returns a PkgDecoder initialized to read the UnifiedIR export data from input. pkgPath is the package path for thecompilation unit that produced the export data.

func (*PkgDecoder)AbsIdx

func (pr *PkgDecoder) AbsIdx(kSectionKind, idxRelElemIdx)int

AbsIdx returns the absolute index for the given (section, index)pair.

func (*PkgDecoder)DataIdx

func (pr *PkgDecoder) DataIdx(kSectionKind, idxRelElemIdx)string

DataIdx returns the raw element bitstream for the given (section,index) pair.

func (*PkgDecoder)Fingerprint

func (pr *PkgDecoder) Fingerprint() [8]byte

Fingerprint returns the package fingerprint.

func (*PkgDecoder)NewDecoder

func (pr *PkgDecoder) NewDecoder(kSectionKind, idxRelElemIdx, markerSyncMarker)Decoder

NewDecoder returns a Decoder for the given (section, index) pair,and decodes the given SyncMarker from the element bitstream.

func (*PkgDecoder)NewDecoderRaw

func (pr *PkgDecoder) NewDecoderRaw(kSectionKind, idxRelElemIdx)Decoder

NewDecoderRaw returns a Decoder for the given (section, index) pair.

Most callers should use NewDecoder instead.

func (*PkgDecoder)NumElems

func (pr *PkgDecoder) NumElems(kSectionKind)int

NumElems returns the number of elements in section k.

func (*PkgDecoder)PeekObj

func (pr *PkgDecoder) PeekObj(idxRelElemIdx) (string,string,CodeObj)

PeekObj returns the package path, object name, and CodeObj for thespecified object index.

func (*PkgDecoder)PeekPkgPath

func (pr *PkgDecoder) PeekPkgPath(idxRelElemIdx)string

PeekPkgPath returns the package path for the specified packageindex.

func (*PkgDecoder)PkgPath

func (pr *PkgDecoder) PkgPath()string

PkgPath returns the package path for the package

TODO(mdempsky): Remove; unneeded since CL 391014.

func (*PkgDecoder)RetireDecoderadded ingo1.20

func (pr *PkgDecoder) RetireDecoder(d *Decoder)

func (*PkgDecoder)StringIdx

func (pr *PkgDecoder) StringIdx(idxRelElemIdx)string

StringIdx returns the string value for the given string index.

func (*PkgDecoder)SyncMarkersadded ingo1.20

func (pr *PkgDecoder) SyncMarkers()bool

SyncMarkers reports whether pr uses sync markers.

func (*PkgDecoder)TempDecoderadded ingo1.20

func (pr *PkgDecoder) TempDecoder(kSectionKind, idxRelElemIdx, markerSyncMarker)Decoder

TempDecoder returns a Decoder for the given (section, index) pair,and decodes the given SyncMarker from the element bitstream.If possible the Decoder should be RetireDecoder'd when it is no longerneeded, this will avoid heap allocations.

func (*PkgDecoder)TempDecoderRawadded ingo1.20

func (pr *PkgDecoder) TempDecoderRaw(kSectionKind, idxRelElemIdx)Decoder

func (*PkgDecoder)TotalElems

func (pr *PkgDecoder) TotalElems()int

TotalElems returns the total number of elements across all sections.

typePkgEncoder

type PkgEncoder struct {// contains filtered or unexported fields}

A PkgEncoder provides methods for encoding a package's Unified IRexport data.

funcNewPkgEncoder

func NewPkgEncoder(versionVersion, syncFramesint)PkgEncoder

NewPkgEncoder returns an initialized PkgEncoder.

syncFrames is the number of caller frames that should be serializedat Sync points. Serializing additional frames results in largerexport data files, but can help diagnosing desync errors inhigher-level Unified IR reader/writer code. If syncFrames isnegative, then sync markers are omitted entirely.

func (*PkgEncoder)DumpTo

func (pw *PkgEncoder) DumpTo(out0io.Writer) (fingerprint [8]byte)

DumpTo writes the package's encoded data to out0 and returns thepackage fingerprint.

func (*PkgEncoder)NewEncoder

func (pw *PkgEncoder) NewEncoder(kSectionKind, markerSyncMarker) *Encoder

NewEncoder returns an Encoder for a new element within the givensection, and encodes the given SyncMarker as the start of theelement bitstream.

func (*PkgEncoder)NewEncoderRaw

func (pw *PkgEncoder) NewEncoderRaw(kSectionKind) *Encoder

NewEncoderRaw returns an Encoder for a new element within the givensection.

Most callers should use NewEncoder instead.

func (*PkgEncoder)StringIdx

func (pw *PkgEncoder) StringIdx(sstring)RelElemIdx

StringIdx adds a string value to the strings section, if notalready present, and returns its index.

func (*PkgEncoder)SyncMarkersadded ingo1.20

func (pw *PkgEncoder) SyncMarkers()bool

SyncMarkers reports whether pw uses sync markers.

typeRefTableEntryadded ingo1.25.0

type RefTableEntry struct {KindSectionKindIdxRelElemIdx}

A RefTableEntry is an entry in an element's reference table. Allelements are preceded by a reference table which provides locationsfor referenced elements.

typeRelElemIdxadded ingo1.25.0

type RelElemIdx =Index

TODO(markfreeman): Make this its own type.A RelElemIdx, or relative element index, is an index into the elementsrelative to some other index, such as the start of a section.

const (PublicRootIdxRelElemIdx = 0PrivateRootIdxRelElemIdx = 1)

Reserved indices within theSectionMeta section.

typeSectionKindadded ingo1.25.0

type SectionKindint32// TODO(markfreeman): Replace with uint8.

A SectionKind indicates a section, as well as the ordering of sections withinunified export data. Any object given a dedicated section can be referred tovia a section / index pair (and thus dereferenced) in other sections.

const (SectionStringSectionKind =iotaSectionMetaSectionPosBaseSectionPkgSectionNameSectionTypeSectionObjSectionObjExtSectionObjDictSectionBody)

typeSyncMarker

type SyncMarkerint

SyncMarker is an enum type that represents markers that may bewritten to export data to ensure the reader and writer staysynchronized.

const (// Low-level coding markers.SyncEOF SyncMarkerSyncBoolSyncInt64SyncUint64SyncStringSyncValueSyncValSyncRelocsSyncRelocSyncUseReloc// Higher-level object and type markers.SyncPublicSyncPosSyncPosBaseSyncObjectSyncObject1SyncPkgSyncPkgDefSyncMethodSyncTypeSyncTypeIdxSyncTypeParamNamesSyncSignatureSyncParamsSyncParamSyncCodeObjSyncSymSyncLocalIdentSyncSelector// Private markers (only known to cmd/compile).SyncPrivateSyncFuncExtSyncVarExtSyncTypeExtSyncPragmaSyncExprListSyncExprsSyncExprSyncExprTypeSyncAssignSyncOpSyncFuncLitSyncCompLitSyncDeclSyncFuncBodySyncOpenScopeSyncCloseScopeSyncCloseAnotherScopeSyncDeclNamesSyncDeclNameSyncStmtsSyncBlockStmtSyncIfStmtSyncForStmtSyncSwitchStmtSyncRangeStmtSyncCaseClauseSyncCommClauseSyncSelectStmtSyncDeclsSyncLabeledStmtSyncUseObjLocalSyncAddLocalSyncLinknameSyncStmt1SyncStmtsEndSyncLabelSyncOptLabelSyncMultiExprSyncRTypeSyncConvRTTI)

func (SyncMarker)String

func (iSyncMarker) String()string

typeVersionadded ingo1.24.0

type Versionuint32

Version indicates a version of a unified IR bitstream.Each Version indicates the addition, removal, or change ofnew data in the bitstream.

These are serialized to disk and the interpretation remains fixed.

const (// V0: initial prototype.//// All data that is not assigned a Field is in version V0// and has not been deprecated.V0Version =iota// V1: adds the Flags uint32 wordV1// V2: removes unused legacy fields and supports type parameters for aliases.// - remove the legacy "has init" bool from the public root// - remove obj's "derived func instance" bool// - add a TypeParamNames field to ObjAlias// - remove derived info "needed" boolV2)

func (Version)Hasadded ingo1.24.0

func (vVersion) Has(fField)bool

Has reports whether field f is present in a bitstream at version v.

Source Files

View all Source files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp