Skip to content

Commit 69b412b

Browse files
committed
internal/pkgbits: better documentation
Change-Id: I3f96a6e8a43faa5c8111b9d979aa37822c1dce06 Reviewed-on: https://go-review.googlesource.com/c/go/+/407434 Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Cuong Manh Le <[email protected]> Run-TryBot: Matthew Dempsky <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: David Chase <[email protected]>
1 parent ec464ed commit 69b412b

File tree

7 files changed

+93
-5
lines changed

7 files changed

+93
-5
lines changed

src/cmd/compile/internal/importer/support.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,13 @@ type anyType struct{}
138138
func (t anyType) Underlying() types2.Type { return t }
139139
func (t anyType) String() string { return "any" }
140140

141+
// See cmd/compile/internal/noder.derivedInfo.
141142
type derivedInfo struct {
142143
idx int
143144
needed bool
144145
}
145146

147+
// See cmd/compile/internal/noder.typeInfo.
146148
type typeInfo struct {
147149
idx int
148150
derived bool

src/cmd/compile/internal/noder/writer.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,20 @@ type writerDict struct {
109109
itabs []itabInfo
110110
}
111111

112+
// A derivedInfo represents a reference to an encoded generic Go type.
112113
type derivedInfo struct {
113114
idx int
114115
needed bool
115116
}
116117

118+
// A typeInfo represents a reference to an encoded Go type.
119+
//
120+
// If derived is true, then the typeInfo represents a generic Go type
121+
// that contains type parameters. In this case, idx is an index into
122+
// the readerDict.derived{,Types} arrays.
123+
//
124+
// Otherwise, the typeInfo represents a non-generic Go type, and idx
125+
// is an index into the reader.typs array instead.
117126
type typeInfo struct {
118127
idx int
119128
derived bool

src/go/internal/gcimporter/support.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,13 @@ type anyType struct{}
155155
func (t anyType) Underlying() types.Type { return t }
156156
func (t anyType) String() string { return "any" }
157157

158+
// See cmd/compile/internal/noder.derivedInfo.
158159
type derivedInfo struct {
159160
idx int
160161
needed bool
161162
}
162163

164+
// See cmd/compile/internal/noder.typeInfo.
163165
type typeInfo struct {
164166
idx int
165167
derived bool

src/internal/pkgbits/decoder.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,36 @@ import (
1717
"strings"
1818
)
1919

20+
// A PkgDecoder provides methods for decoding a package's Unified IR
21+
// export data.
2022
type PkgDecoder struct {
23+
// pkgPath is the package path for the package to be decoded.
24+
//
25+
// TODO(mdempsky): Remove; unneeded since CL 391014.
2126
pkgPath string
2227

28+
// elemData is the full data payload of the encoded package.
29+
// Elements are densely and contiguously packed together.
30+
//
31+
// The last 8 bytes of elemData are the package fingerprint.
32+
elemData string
33+
34+
// elemEnds stores the byte-offset end positions of element
35+
// bitstreams within elemData.
36+
//
37+
// For example, element I's bitstream data starts at elemEnds[I-1]
38+
// (or 0, if I==0) and ends at elemEnds[I].
39+
//
40+
// Note: elemEnds is indexed by absolute indices, not
41+
// section-relative indices.
42+
elemEnds []uint32
43+
44+
// elemEndsEnds stores the index-offset end positions of relocation
45+
// sections within elemEnds.
46+
//
47+
// For example, section K's end positions start at elemEndsEnds[K-1]
48+
// (or 0, if K==0) and end at elemEndsEnds[K].
2349
elemEndsEnds [numRelocs]uint32
24-
elemEnds []uint32
25-
elemData string // last 8 bytes are fingerprint
2650
}
2751

2852
func (pr *PkgDecoder) PkgPath() string { return pr.pkgPath }
@@ -55,6 +79,7 @@ func NewPkgDecoder(pkgPath, input string) PkgDecoder {
5579
return pr
5680
}
5781

82+
// NumElems returns the number of elements in section k.
5883
func (pr *PkgDecoder) NumElems(k RelocKind) int {
5984
count := int(pr.elemEndsEnds[k])
6085
if k > 0 {
@@ -63,16 +88,20 @@ func (pr *PkgDecoder) NumElems(k RelocKind) int {
6388
return count
6489
}
6590

91+
// TotalElems returns the total number of elements across all sections.
6692
func (pr *PkgDecoder) TotalElems() int {
6793
return len(pr.elemEnds)
6894
}
6995

96+
// Fingerprint returns the package fingerprint.
7097
func (pr *PkgDecoder) Fingerprint() [8]byte {
7198
var fp [8]byte
7299
copy(fp[:], pr.elemData[len(pr.elemData)-8:])
73100
return fp
74101
}
75102

103+
// AbsIdx returns the absolute index for the given (section, index)
104+
// pair.
76105
func (pr *PkgDecoder) AbsIdx(k RelocKind, idx int) int {
77106
absIdx := idx
78107
if k > 0 {
@@ -84,6 +113,8 @@ func (pr *PkgDecoder) AbsIdx(k RelocKind, idx int) int {
84113
return absIdx
85114
}
86115

116+
// DataIdx returns the raw element bitstream for the given (section,
117+
// index) pair.
87118
func (pr *PkgDecoder) DataIdx(k RelocKind, idx int) string {
88119
absIdx := pr.AbsIdx(k, idx)
89120

@@ -126,6 +157,8 @@ func (pr *PkgDecoder) NewDecoderRaw(k RelocKind, idx int) Decoder {
126157
return r
127158
}
128159

160+
// A Decoder provides methods for decoding an individual element's
161+
// bitstream data.
129162
type Decoder struct {
130163
common *PkgDecoder
131164

src/internal/pkgbits/doc.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2022 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 implements low-level coding abstractions for
6+
// Unified IR's export data format.
7+
//
8+
// At a low-level, a package is a collection of bitstream elements.
9+
// Each element has a "kind" and a dense, non-negative index.
10+
// Elements can be randomly accessed given their kind and index.
11+
//
12+
// Individual elements are sequences of variable-length values (e.g.,
13+
// integers, booleans, strings, go/constant values, cross-references
14+
// to other elements). Package pkgbits provides APIs for encoding and
15+
// decoding these low-level values, but the details of mapping
16+
// higher-level Go constructs into elements is left to higher-level
17+
// abstractions.
18+
//
19+
// Elements may cross-reference each other with "relocations." For
20+
// example, an element representing a pointer type has a relocation
21+
// referring to the element type.
22+
//
23+
// Go constructs may be composed as a constellation of multiple
24+
// elements. For example, a declared function may have one element to
25+
// describe the object (e.g., its name, type, position), and a
26+
// separate element to describe its function body. This allows readers
27+
// some flexibility in efficiently seeking or re-reading data (e.g.,
28+
// inlining requires re-reading the function body for each inlined
29+
// call, without needing to re-read the object-level details).
30+
package pkgbits

src/internal/pkgbits/encoder.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@ import (
1616
"runtime"
1717
)
1818

19+
// A PkgEncoder provides methods for encoding a package's Unified IR
20+
// export data.
1921
type PkgEncoder struct {
22+
// elems holds the bitstream for previously encoded elements.
2023
elems [numRelocs][]string
2124

25+
// stringsIdx maps previously encoded strings to their index within
26+
// the RelocString section, to allow deduplication.
2227
stringsIdx map[string]int
2328

2429
syncFrames int
@@ -31,6 +36,8 @@ func NewPkgEncoder(syncFrames int) PkgEncoder {
3136
}
3237
}
3338

39+
// DumpTo writes the package's encoded data to out0 and returns the
40+
// package fingerprint.
3441
func (pw *PkgEncoder) DumpTo(out0 io.Writer) (fingerprint [8]byte) {
3542
h := md5.New()
3643
out := io.MultiWriter(out0, h)
@@ -41,12 +48,14 @@ func (pw *PkgEncoder) DumpTo(out0 io.Writer) (fingerprint [8]byte) {
4148

4249
writeUint32(0) // version
4350

51+
// Write elemEndsEnds.
4452
var sum uint32
4553
for _, elems := range &pw.elems {
4654
sum += uint32(len(elems))
4755
writeUint32(sum)
4856
}
4957

58+
// Write elemEnds.
5059
sum = 0
5160
for _, elems := range &pw.elems {
5261
for _, elem := range elems {
@@ -55,13 +64,15 @@ func (pw *PkgEncoder) DumpTo(out0 io.Writer) (fingerprint [8]byte) {
5564
}
5665
}
5766

67+
// Write elemData.
5868
for _, elems := range &pw.elems {
5969
for _, elem := range elems {
6070
_, err := io.WriteString(out, elem)
6171
assert(err == nil)
6272
}
6373
}
6474

75+
// Write fingerprint.
6576
copy(fingerprint[:], h.Sum(nil))
6677
_, err := out0.Write(fingerprint[:])
6778
assert(err == nil)
@@ -98,8 +109,8 @@ func (pw *PkgEncoder) NewEncoderRaw(k RelocKind) Encoder {
98109
}
99110
}
100111

101-
// Encoders
102-
112+
// An Encoder provides methods for encoding an individual element's
113+
// bitstream data.
103114
type Encoder struct {
104115
p *PkgEncoder
105116

@@ -112,6 +123,7 @@ type Encoder struct {
112123
Idx int
113124
}
114125

126+
// Flush finalizes the element's bitstream and returns its Index.
115127
func (w *Encoder) Flush() int {
116128
var sb bytes.Buffer // TODO(mdempsky): strings.Builder after #44505 is resolved
117129

src/internal/pkgbits/reloc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ package pkgbits
99
// A RelocKind indicates a particular section within a unified IR export.
1010
type RelocKind int
1111

12-
// A relocEnt (relocation entry) is an entry in an atom's local
12+
// A relocEnt (relocation entry) is an entry in an element's local
1313
// reference table.
1414
//
1515
// TODO(mdempsky): Rename this too.

0 commit comments

Comments
 (0)