aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/noder/decoder.go
blob: 3dc61c6a69208e93dc0e745a5af2e334ff27cc0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// UNREVIEWED

// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package noder

import (
	"encoding/binary"
	"fmt"
	"go/constant"
	"go/token"
	"math/big"
	"os"
	"runtime"
	"strings"

	"cmd/compile/internal/base"
)

type pkgDecoder struct {
	pkgPath string

	elemEndsEnds [numRelocs]uint32
	elemEnds     []uint32
	elemData     string
}

func newPkgDecoder(pkgPath, input string) pkgDecoder {
	pr := pkgDecoder{
		pkgPath: pkgPath,
	}

	// TODO(mdempsky): Implement direct indexing of input string to
	// avoid copying the position information.

	r := strings.NewReader(input)

	assert(binary.Read(r, binary.LittleEndian, pr.elemEndsEnds[:]) == nil)

	pr.elemEnds = make([]uint32, pr.elemEndsEnds[len(pr.elemEndsEnds)-1])
	assert(binary.Read(r, binary.LittleEndian, pr.elemEnds[:]) == nil)

	pos, err := r.Seek(0, os.SEEK_CUR)
	assert(err == nil)

	pr.elemData = input[pos:]
	assert(len(pr.elemData) == int(pr.elemEnds[len(pr.elemEnds)-1]))

	return pr
}

func (pr *pkgDecoder) numElems(k reloc) int {
	count := int(pr.elemEndsEnds[k])
	if k > 0 {
		count -= int(pr.elemEndsEnds[k-1])
	}
	return count
}

func (pr *pkgDecoder) totalElems() int {
	return len(pr.elemEnds)
}

func (pr *pkgDecoder) absIdx(k reloc, idx int) int {
	absIdx := idx
	if k > 0 {
		absIdx += int(pr.elemEndsEnds[k-1])
	}
	if absIdx >= int(pr.elemEndsEnds[k]) {
		base.Fatalf("%v:%v is out of bounds; %v", k, idx, pr.elemEndsEnds)
	}
	return absIdx
}

func (pr *pkgDecoder) dataIdx(k reloc, idx int) string {
	absIdx := pr.absIdx(k, idx)

	var start uint32
	if absIdx > 0 {
		start = pr.elemEnds[absIdx-1]
	}
	end := pr.elemEnds[absIdx]

	return pr.elemData[start:end]
}

func (pr *pkgDecoder) stringIdx(idx int) string {
	return pr.dataIdx(relocString, idx)
}

func (pr *pkgDecoder) newDecoder(k reloc, idx int, marker syncMarker) decoder {
	r := pr.newDecoderRaw(k, idx)
	r.sync(marker)
	return r
}

func (pr *pkgDecoder) newDecoderRaw(k reloc, idx int) decoder {
	r := decoder{
		common: pr,
		k:      k,
		idx:    idx,
	}

	// TODO(mdempsky) r.data.Reset(...) after #44505 is resolved.
	r.data = *strings.NewReader(pr.dataIdx(k, idx))

	r.sync(syncRelocs)
	r.relocs = make([]relocEnt, r.len())
	for i := range r.relocs {
		r.sync(syncReloc)
		r.relocs[i] = relocEnt{reloc(r.len()), r.len()}
	}

	return r
}

type decoder struct {
	common *pkgDecoder

	relocs []relocEnt
	data   strings.Reader

	k   reloc
	idx int
}

func (r *decoder) checkErr(err error) {
	if err != nil {
		base.Fatalf("unexpected error: %v", err)
	}
}

func (r *decoder) rawUvarint() uint64 {
	x, err := binary.ReadUvarint(&r.data)
	r.checkErr(err)
	return x
}

func (r *decoder) rawVarint() int64 {
	ux := r.rawUvarint()

	// Zig-zag decode.
	x := int64(ux >> 1)
	if ux&1 != 0 {
		x = ^x
	}
	return x
}

func (r *decoder) rawReloc(k reloc, idx int) int {
	e := r.relocs[idx]
	assert(e.kind == k)
	return e.idx
}

func (r *decoder) sync(mWant syncMarker) {
	if !enableSync {
		return
	}

	pos, _ := r.data.Seek(0, os.SEEK_CUR) // TODO(mdempsky): io.SeekCurrent after #44505 is resolved
	mHave := syncMarker(r.rawUvarint())
	writerPCs := make([]int, r.rawUvarint())
	for i := range writerPCs {
		writerPCs[i] = int(r.rawUvarint())
	}

	if mHave == mWant {
		return
	}

	// There's some tension here between printing:
	//
	// (1) full file paths that tools can recognize (e.g., so emacs
	//     hyperlinks the "file:line" text for easy navigation), or
	//
	// (2) short file paths that are easier for humans to read (e.g., by
	//     omitting redundant or irrelevant details, so it's easier to
	//     focus on the useful bits that remain).
	//
	// The current formatting favors the former, as it seems more
	// helpful in practice. But perhaps the formatting could be improved
	// to better address both concerns. For example, use relative file
	// paths if they would be shorter, or rewrite file paths to contain
	// "$GOROOT" (like objabi.AbsFile does) if tools can be taught how
	// to reliably expand that again.

	fmt.Printf("export data desync: package %q, section %v, index %v, offset %v\n", r.common.pkgPath, r.k, r.idx, pos)

	fmt.Printf("\nfound %v, written at:\n", mHave)
	if len(writerPCs) == 0 {
		fmt.Printf("\t[stack trace unavailable; recompile package %q with -d=syncframes]\n", r.common.pkgPath)
	}
	for _, pc := range writerPCs {
		fmt.Printf("\t%s\n", r.common.stringIdx(r.rawReloc(relocString, pc)))
	}

	fmt.Printf("\nexpected %v, reading at:\n", mWant)
	var readerPCs [32]uintptr // TODO(mdempsky): Dynamically size?
	n := runtime.Callers(2, readerPCs[:])
	for _, pc := range fmtFrames(readerPCs[:n]...) {
		fmt.Printf("\t%s\n", pc)
	}

	// We already printed a stack trace for the reader, so now we can
	// simply exit. Printing a second one with panic or base.Fatalf
	// would just be noise.
	os.Exit(1)
}

func (r *decoder) bool() bool {
	r.sync(syncBool)
	x, err := r.data.ReadByte()
	r.checkErr(err)
	assert(x < 2)
	return x != 0
}

func (r *decoder) int64() int64 {
	r.sync(syncInt64)
	return r.rawVarint()
}

func (r *decoder) uint64() uint64 {
	r.sync(syncUint64)
	return r.rawUvarint()
}

func (r *decoder) len() int   { x := r.uint64(); v := int(x); assert(uint64(v) == x); return v }
func (r *decoder) int() int   { x := r.int64(); v := int(x); assert(int64(v) == x); return v }
func (r *decoder) uint() uint { x := r.uint64(); v := uint(x); assert(uint64(v) == x); return v }

func (r *decoder) code(mark syncMarker) int {
	r.sync(mark)
	return r.len()
}

func (r *decoder) reloc(k reloc) int {
	r.sync(syncUseReloc)
	return r.rawReloc(k, r.len())
}

func (r *decoder) string() string {
	r.sync(syncString)
	return r.common.stringIdx(r.reloc(relocString))
}

func (r *decoder) strings() []string {
	res := make([]string, r.len())
	for i := range res {
		res[i] = r.string()
	}
	return res
}

func (r *decoder) rawValue() constant.Value {
	isComplex := r.bool()
	val := r.scalar()
	if isComplex {
		val = constant.BinaryOp(val, token.ADD, constant.MakeImag(r.scalar()))
	}
	return val
}

func (r *decoder) scalar() constant.Value {
	switch tag := codeVal(r.code(syncVal)); tag {
	default:
		panic(fmt.Sprintf("unexpected scalar tag: %v", tag))

	case valBool:
		return constant.MakeBool(r.bool())
	case valString:
		return constant.MakeString(r.string())
	case valInt64:
		return constant.MakeInt64(r.int64())
	case valBigInt:
		return constant.Make(r.bigInt())
	case valBigRat:
		num := r.bigInt()
		denom := r.bigInt()
		return constant.Make(new(big.Rat).SetFrac(num, denom))
	case valBigFloat:
		return constant.Make(r.bigFloat())
	}
}

func (r *decoder) bigInt() *big.Int {
	v := new(big.Int).SetBytes([]byte(r.string()))
	if r.bool() {
		v.Neg(v)
	}
	return v
}

func (r *decoder) bigFloat() *big.Float {
	v := new(big.Float).SetPrec(512)
	assert(v.UnmarshalText([]byte(r.string())) == nil)
	return v
}