diff options
| author | Matthew Dempsky <mdempsky@google.com> | 2022-05-18 13:26:38 -0700 |
|---|---|---|
| committer | Matthew Dempsky <mdempsky@google.com> | 2022-05-25 16:16:01 +0000 |
| commit | 4e4db1e2572190bf172bcb8532047bc18571366f (patch) | |
| tree | 458f0c750f7be7c348b60d8991b7a35883980d8e /src/internal/pkgbits | |
| parent | 8841699160946263859ea492779bea4aa909f1de (diff) | |
| download | go-4e4db1e2572190bf172bcb8532047bc18571366f.tar.xz | |
internal/pkgbits: add Index type
Element indices are very common in the pkgbits API, so introduce a new
defined type to help make that clearer.
Change-Id: I8f9493e2335601c740eb403d1fdcd11183122907
Reviewed-on: https://go-review.googlesource.com/c/go/+/407435
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/internal/pkgbits')
| -rw-r--r-- | src/internal/pkgbits/decoder.go | 24 | ||||
| -rw-r--r-- | src/internal/pkgbits/encoder.go | 20 | ||||
| -rw-r--r-- | src/internal/pkgbits/reloc.go | 10 |
3 files changed, 29 insertions, 25 deletions
diff --git a/src/internal/pkgbits/decoder.go b/src/internal/pkgbits/decoder.go index a2367b7e99..0b5fd9705c 100644 --- a/src/internal/pkgbits/decoder.go +++ b/src/internal/pkgbits/decoder.go @@ -108,8 +108,8 @@ func (pr *PkgDecoder) Fingerprint() [8]byte { // AbsIdx returns the absolute index for the given (section, index) // pair. -func (pr *PkgDecoder) AbsIdx(k RelocKind, idx int) int { - absIdx := idx +func (pr *PkgDecoder) AbsIdx(k RelocKind, idx Index) int { + absIdx := int(idx) if k > 0 { absIdx += int(pr.elemEndsEnds[k-1]) } @@ -121,7 +121,7 @@ func (pr *PkgDecoder) AbsIdx(k RelocKind, idx int) int { // DataIdx returns the raw element bitstream for the given (section, // index) pair. -func (pr *PkgDecoder) DataIdx(k RelocKind, idx int) string { +func (pr *PkgDecoder) DataIdx(k RelocKind, idx Index) string { absIdx := pr.AbsIdx(k, idx) var start uint32 @@ -134,13 +134,13 @@ func (pr *PkgDecoder) DataIdx(k RelocKind, idx int) string { } // StringIdx returns the string value for the given string index. -func (pr *PkgDecoder) StringIdx(idx int) string { +func (pr *PkgDecoder) StringIdx(idx Index) string { return pr.DataIdx(RelocString, idx) } // NewDecoder returns a Decoder for the given (section, index) pair, // and decodes the given SyncMarker from the element bitstream. -func (pr *PkgDecoder) NewDecoder(k RelocKind, idx int, marker SyncMarker) Decoder { +func (pr *PkgDecoder) NewDecoder(k RelocKind, idx Index, marker SyncMarker) Decoder { r := pr.NewDecoderRaw(k, idx) r.Sync(marker) return r @@ -149,7 +149,7 @@ func (pr *PkgDecoder) NewDecoder(k RelocKind, idx int, marker SyncMarker) Decode // NewDecoderRaw returns a Decoder for the given (section, index) pair. // // Most callers should use NewDecoder instead. -func (pr *PkgDecoder) NewDecoderRaw(k RelocKind, idx int) Decoder { +func (pr *PkgDecoder) NewDecoderRaw(k RelocKind, idx Index) Decoder { r := Decoder{ common: pr, k: k, @@ -163,7 +163,7 @@ func (pr *PkgDecoder) NewDecoderRaw(k RelocKind, idx int) Decoder { r.Relocs = make([]RelocEnt, r.Len()) for i := range r.Relocs { r.Sync(SyncReloc) - r.Relocs[i] = RelocEnt{RelocKind(r.Len()), r.Len()} + r.Relocs[i] = RelocEnt{RelocKind(r.Len()), Index(r.Len())} } return r @@ -178,7 +178,7 @@ type Decoder struct { Data strings.Reader k RelocKind - Idx int + Idx Index } func (r *Decoder) checkErr(err error) { @@ -204,7 +204,7 @@ func (r *Decoder) rawVarint() int64 { return x } -func (r *Decoder) rawReloc(k RelocKind, idx int) int { +func (r *Decoder) rawReloc(k RelocKind, idx int) Index { e := r.Relocs[idx] assert(e.Kind == k) return e.Idx @@ -313,7 +313,7 @@ func (r *Decoder) Code(mark SyncMarker) int { // Reloc decodes a relocation of expected section k from the element // bitstream and returns an index to the referenced element. -func (r *Decoder) Reloc(k RelocKind) int { +func (r *Decoder) Reloc(k RelocKind) Index { r.Sync(SyncUseReloc) return r.rawReloc(k, r.Len()) } @@ -390,7 +390,7 @@ func (r *Decoder) bigFloat() *big.Float { // PeekPkgPath returns the package path for the specified package // index. -func (pr *PkgDecoder) PeekPkgPath(idx int) string { +func (pr *PkgDecoder) PeekPkgPath(idx Index) string { r := pr.NewDecoder(RelocPkg, idx, SyncPkgDef) path := r.String() if path == "" { @@ -401,7 +401,7 @@ func (pr *PkgDecoder) PeekPkgPath(idx int) string { // PeekObj returns the package path, object name, and CodeObj for the // specified object index. -func (pr *PkgDecoder) PeekObj(idx int) (string, string, CodeObj) { +func (pr *PkgDecoder) PeekObj(idx Index) (string, string, CodeObj) { r := pr.NewDecoder(RelocName, idx, SyncObject1) r.Sync(SyncSym) r.Sync(SyncPkg) diff --git a/src/internal/pkgbits/encoder.go b/src/internal/pkgbits/encoder.go index 9fddb58237..1326a135cf 100644 --- a/src/internal/pkgbits/encoder.go +++ b/src/internal/pkgbits/encoder.go @@ -23,7 +23,7 @@ type PkgEncoder struct { // stringsIdx maps previously encoded strings to their index within // the RelocString section, to allow deduplication. That is, // elems[RelocString][stringsIdx[s]] == s (if present). - stringsIdx map[string]int + stringsIdx map[string]Index syncFrames int } @@ -36,7 +36,7 @@ type PkgEncoder struct { // higher-level Unified IR reader/writer code. func NewPkgEncoder(syncFrames int) PkgEncoder { return PkgEncoder{ - stringsIdx: make(map[string]int), + stringsIdx: make(map[string]Index), syncFrames: syncFrames, } } @@ -87,13 +87,13 @@ func (pw *PkgEncoder) DumpTo(out0 io.Writer) (fingerprint [8]byte) { // StringIdx adds a string value to the strings section, if not // already present, and returns its index. -func (pw *PkgEncoder) StringIdx(s string) int { +func (pw *PkgEncoder) StringIdx(s string) Index { if idx, ok := pw.stringsIdx[s]; ok { assert(pw.elems[RelocString][idx] == s) return idx } - idx := len(pw.elems[RelocString]) + idx := Index(len(pw.elems[RelocString])) pw.elems[RelocString] = append(pw.elems[RelocString], s) pw.stringsIdx[s] = idx return idx @@ -113,7 +113,7 @@ func (pw *PkgEncoder) NewEncoder(k RelocKind, marker SyncMarker) Encoder { // // Most callers should use NewEncoder instead. func (pw *PkgEncoder) NewEncoderRaw(k RelocKind) Encoder { - idx := len(pw.elems[k]) + idx := Index(len(pw.elems[k])) pw.elems[k] = append(pw.elems[k], "") // placeholder return Encoder{ @@ -134,11 +134,11 @@ type Encoder struct { encodingRelocHeader bool k RelocKind - Idx int // index within relocation section + Idx Index // index within relocation section } // Flush finalizes the element's bitstream and returns its Index. -func (w *Encoder) Flush() int { +func (w *Encoder) Flush() Index { var sb bytes.Buffer // TODO(mdempsky): strings.Builder after #44505 is resolved // Backup the data so we write the relocations at the front. @@ -157,7 +157,7 @@ func (w *Encoder) Flush() int { for _, rEnt := range w.Relocs { w.Sync(SyncReloc) w.Len(int(rEnt.Kind)) - w.Len(rEnt.Idx) + w.Len(int(rEnt.Idx)) } io.Copy(&sb, &w.Data) @@ -190,7 +190,7 @@ func (w *Encoder) rawVarint(x int64) { w.rawUvarint(ux) } -func (w *Encoder) rawReloc(r RelocKind, idx int) int { +func (w *Encoder) rawReloc(r RelocKind, idx Index) int { // TODO(mdempsky): Use map for lookup; this takes quadratic time. for i, rEnt := range w.Relocs { if rEnt.Kind == r && rEnt.Idx == idx { @@ -279,7 +279,7 @@ func (w *Encoder) Uint(x uint) { w.Uint64(uint64(x)) } // Note: Only the index is formally written into the element // bitstream, so bitstream decoders must know from context which // section an encoded relocation refers to. -func (w *Encoder) Reloc(r RelocKind, idx int) { +func (w *Encoder) Reloc(r RelocKind, idx Index) { w.Sync(SyncUseReloc) w.Len(w.rawReloc(r, idx)) } diff --git a/src/internal/pkgbits/reloc.go b/src/internal/pkgbits/reloc.go index 84cf03ef98..7a8f04ab3f 100644 --- a/src/internal/pkgbits/reloc.go +++ b/src/internal/pkgbits/reloc.go @@ -7,19 +7,23 @@ package pkgbits // A RelocKind indicates a particular section within a unified IR export. type RelocKind int +// An Index represents a bitstream element index within a particular +// section. +type Index int + // A relocEnt (relocation entry) is an entry in an element's local // reference table. // // TODO(mdempsky): Rename this too. type RelocEnt struct { Kind RelocKind - Idx int + Idx Index } // Reserved indices within the meta relocation section. const ( - PublicRootIdx = 0 - PrivateRootIdx = 1 + PublicRootIdx Index = 0 + PrivateRootIdx Index = 1 ) const ( |
