codekingpro commited on
Commit
de452ad
·
verified ·
1 Parent(s): 04dc9f2

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. go/src/internal/abi/abi.go +104 -0
  2. go/src/internal/abi/abi_amd64.go +18 -0
  3. go/src/internal/abi/abi_arm64.go +17 -0
  4. go/src/internal/abi/abi_generic.go +38 -0
  5. go/src/internal/abi/abi_loong64.go +17 -0
  6. go/src/internal/abi/abi_ppc64x.go +19 -0
  7. go/src/internal/abi/abi_riscv64.go +17 -0
  8. go/src/internal/abi/abi_s390x.go +19 -0
  9. go/src/internal/abi/abi_test.go +79 -0
  10. go/src/internal/abi/abi_test.s +27 -0
  11. go/src/internal/abi/bounds.go +113 -0
  12. go/src/internal/abi/compiletype.go +28 -0
  13. go/src/internal/abi/escape.go +65 -0
  14. go/src/internal/abi/export_test.go +14 -0
  15. go/src/internal/abi/funcpc.go +31 -0
  16. go/src/internal/abi/funcpc_gccgo.go +21 -0
  17. go/src/internal/abi/iface.go +41 -0
  18. go/src/internal/abi/map.go +64 -0
  19. go/src/internal/abi/rangefuncconsts.go +18 -0
  20. go/src/internal/abi/runtime.go +8 -0
  21. go/src/internal/abi/stack.go +33 -0
  22. go/src/internal/abi/stub.s +7 -0
  23. go/src/internal/abi/switch.go +58 -0
  24. go/src/internal/abi/symtab.go +143 -0
  25. go/src/internal/abi/type.go +777 -0
  26. go/src/internal/asan/asan.go +19 -0
  27. go/src/internal/asan/doc.go +10 -0
  28. go/src/internal/asan/noasan.go +17 -0
  29. go/src/internal/bisect/bisect.go +778 -0
  30. go/src/internal/buildcfg/cfg.go +467 -0
  31. go/src/internal/buildcfg/cfg_test.go +165 -0
  32. go/src/internal/buildcfg/exp.go +203 -0
  33. go/src/internal/buildcfg/zbootstrap.go +22 -0
  34. go/src/internal/bytealg/bytealg.go +123 -0
  35. go/src/internal/bytealg/compare_386.s +144 -0
  36. go/src/internal/bytealg/compare_amd64.s +237 -0
  37. go/src/internal/bytealg/compare_arm.s +86 -0
  38. go/src/internal/bytealg/compare_arm64.s +125 -0
  39. go/src/internal/bytealg/compare_generic.go +76 -0
  40. go/src/internal/bytealg/compare_loong64.s +363 -0
  41. go/src/internal/bytealg/compare_mips64x.s +88 -0
  42. go/src/internal/bytealg/compare_mipsx.s +72 -0
  43. go/src/internal/bytealg/compare_native.go +23 -0
  44. go/src/internal/bytealg/compare_ppc64x.s +342 -0
  45. go/src/internal/bytealg/compare_riscv64.s +259 -0
  46. go/src/internal/bytealg/compare_s390x.s +97 -0
  47. go/src/internal/bytealg/compare_wasm.s +115 -0
  48. go/src/internal/bytealg/count_amd64.s +229 -0
  49. go/src/internal/bytealg/count_arm.s +43 -0
  50. go/src/internal/bytealg/count_arm64.s +94 -0
go/src/internal/abi/abi.go ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2020 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 abi
6
+
7
+ import (
8
+ "internal/goarch"
9
+ "unsafe"
10
+ )
11
+
12
+ // RegArgs is a struct that has space for each argument
13
+ // and return value register on the current architecture.
14
+ //
15
+ // Assembly code knows the layout of the first two fields
16
+ // of RegArgs.
17
+ //
18
+ // RegArgs also contains additional space to hold pointers
19
+ // when it may not be safe to keep them only in the integer
20
+ // register space otherwise.
21
+ type RegArgs struct {
22
+ // Values in these slots should be precisely the bit-by-bit
23
+ // representation of how they would appear in a register.
24
+ //
25
+ // This means that on big endian arches, integer values should
26
+ // be in the top bits of the slot. Floats are usually just
27
+ // directly represented, but some architectures treat narrow
28
+ // width floating point values specially (e.g. they're promoted
29
+ // first, or they need to be NaN-boxed).
30
+ Ints [IntArgRegs]uintptr // untyped integer registers
31
+ Floats [FloatArgRegs]uint64 // untyped float registers
32
+
33
+ // Fields above this point are known to assembly.
34
+
35
+ // Ptrs is a space that duplicates Ints but with pointer type,
36
+ // used to make pointers passed or returned in registers
37
+ // visible to the GC by making the type unsafe.Pointer.
38
+ Ptrs [IntArgRegs]unsafe.Pointer
39
+
40
+ // ReturnIsPtr is a bitmap that indicates which registers
41
+ // contain or will contain pointers on the return path from
42
+ // a reflectcall. The i'th bit indicates whether the i'th
43
+ // register contains or will contain a valid Go pointer.
44
+ ReturnIsPtr IntArgRegBitmap
45
+ }
46
+
47
+ func (r *RegArgs) Dump() {
48
+ print("Ints:")
49
+ for _, x := range r.Ints {
50
+ print(" ", x)
51
+ }
52
+ println()
53
+ print("Floats:")
54
+ for _, x := range r.Floats {
55
+ print(" ", x)
56
+ }
57
+ println()
58
+ print("Ptrs:")
59
+ for _, x := range r.Ptrs {
60
+ print(" ", x)
61
+ }
62
+ println()
63
+ }
64
+
65
+ // IntRegArgAddr returns a pointer inside of r.Ints[reg] that is appropriately
66
+ // offset for an argument of size argSize.
67
+ //
68
+ // argSize must be non-zero, fit in a register, and a power-of-two.
69
+ //
70
+ // This method is a helper for dealing with the endianness of different CPU
71
+ // architectures, since sub-word-sized arguments in big endian architectures
72
+ // need to be "aligned" to the upper edge of the register to be interpreted
73
+ // by the CPU correctly.
74
+ func (r *RegArgs) IntRegArgAddr(reg int, argSize uintptr) unsafe.Pointer {
75
+ if argSize > goarch.PtrSize || argSize == 0 || argSize&(argSize-1) != 0 {
76
+ panic("invalid argSize")
77
+ }
78
+ offset := uintptr(0)
79
+ if goarch.BigEndian {
80
+ offset = goarch.PtrSize - argSize
81
+ }
82
+ return unsafe.Pointer(uintptr(unsafe.Pointer(&r.Ints[reg])) + offset)
83
+ }
84
+
85
+ // IntArgRegBitmap is a bitmap large enough to hold one bit per
86
+ // integer argument/return register.
87
+ type IntArgRegBitmap [(IntArgRegs + 7) / 8]uint8
88
+
89
+ // Set sets the i'th bit of the bitmap to 1.
90
+ func (b *IntArgRegBitmap) Set(i int) {
91
+ b[i/8] |= uint8(1) << (i % 8)
92
+ }
93
+
94
+ // Get returns whether the i'th bit of the bitmap is set.
95
+ //
96
+ // nosplit because it's called in extremely sensitive contexts, like
97
+ // on the reflectcall return path.
98
+ //
99
+ //go:nosplit
100
+ func (b *IntArgRegBitmap) Get(i int) bool {
101
+ // Compute p=&b[i/8], but without a bounds check. We don't have the stack for it.
102
+ p := (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(b)) + uintptr(i/8)))
103
+ return *p&(uint8(1)<<(i%8)) != 0
104
+ }
go/src/internal/abi/abi_amd64.go ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2020 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 abi
6
+
7
+ const (
8
+ // See abi_generic.go.
9
+
10
+ // RAX, RBX, RCX, RDI, RSI, R8, R9, R10, R11.
11
+ IntArgRegs = 9
12
+
13
+ // X0 -> X14.
14
+ FloatArgRegs = 15
15
+
16
+ // We use SSE2 registers which support 64-bit float operations.
17
+ EffectiveFloatRegSize = 8
18
+ )
go/src/internal/abi/abi_arm64.go ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 abi
6
+
7
+ const (
8
+ // See abi_generic.go.
9
+
10
+ // R0 - R15.
11
+ IntArgRegs = 16
12
+
13
+ // F0 - F15.
14
+ FloatArgRegs = 16
15
+
16
+ EffectiveFloatRegSize = 8
17
+ )
go/src/internal/abi/abi_generic.go ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2020 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
+ //go:build !goexperiment.regabiargs && !amd64 && !arm64 && !loong64 && !ppc64 && !ppc64le && !riscv64
6
+
7
+ package abi
8
+
9
+ const (
10
+ // ABI-related constants.
11
+ //
12
+ // In the generic case, these are all zero
13
+ // which lets them gracefully degrade to ABI0.
14
+
15
+ // IntArgRegs is the number of registers dedicated
16
+ // to passing integer argument values. Result registers are identical
17
+ // to argument registers, so this number is used for those too.
18
+ IntArgRegs = 0
19
+
20
+ // FloatArgRegs is the number of registers dedicated
21
+ // to passing floating-point argument values. Result registers are
22
+ // identical to argument registers, so this number is used for
23
+ // those too.
24
+ FloatArgRegs = 0
25
+
26
+ // EffectiveFloatRegSize describes the width of floating point
27
+ // registers on the current platform from the ABI's perspective.
28
+ //
29
+ // Since Go only supports 32-bit and 64-bit floating point primitives,
30
+ // this number should be either 0, 4, or 8. 0 indicates no floating
31
+ // point registers for the ABI or that floating point values will be
32
+ // passed via the softfloat ABI.
33
+ //
34
+ // For platforms that support larger floating point register widths,
35
+ // such as x87's 80-bit "registers" (not that we support x87 currently),
36
+ // use 8.
37
+ EffectiveFloatRegSize = 0
38
+ )
go/src/internal/abi/abi_loong64.go ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2023 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 abi
6
+
7
+ const (
8
+ // See abi_generic.go.
9
+
10
+ // R4 - R19
11
+ IntArgRegs = 16
12
+
13
+ // F0 - F15
14
+ FloatArgRegs = 16
15
+
16
+ EffectiveFloatRegSize = 8
17
+ )
go/src/internal/abi/abi_ppc64x.go ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ //go:build ppc64 || ppc64le
6
+
7
+ package abi
8
+
9
+ const (
10
+ // See abi_generic.go.
11
+
12
+ // R3 - R10, R14 - R17.
13
+ IntArgRegs = 12
14
+
15
+ // F1 - F12.
16
+ FloatArgRegs = 12
17
+
18
+ EffectiveFloatRegSize = 8
19
+ )
go/src/internal/abi/abi_riscv64.go ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 abi
6
+
7
+ const (
8
+ // See abi_generic.go.
9
+
10
+ // X8 - X23
11
+ IntArgRegs = 16
12
+
13
+ // F8 - F23.
14
+ FloatArgRegs = 16
15
+
16
+ EffectiveFloatRegSize = 8
17
+ )
go/src/internal/abi/abi_s390x.go ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2025 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
+ //go:build goexperiment.regabiargs
6
+
7
+ package abi
8
+
9
+ const (
10
+ // See abi_generic.go.
11
+
12
+ // R2 - R9.
13
+ IntArgRegs = 8
14
+
15
+ // F0 - F15
16
+ FloatArgRegs = 16
17
+
18
+ EffectiveFloatRegSize = 8
19
+ )
go/src/internal/abi/abi_test.go ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 abi_test
6
+
7
+ import (
8
+ "internal/abi"
9
+ "internal/testenv"
10
+ "path/filepath"
11
+ "strings"
12
+ "testing"
13
+ )
14
+
15
+ func TestFuncPC(t *testing.T) {
16
+ // Test that FuncPC* can get correct function PC.
17
+ pcFromAsm := abi.FuncPCTestFnAddr
18
+
19
+ // Test FuncPC for locally defined function
20
+ pcFromGo := abi.FuncPCTest()
21
+ if pcFromGo != pcFromAsm {
22
+ t.Errorf("FuncPC returns wrong PC, want %x, got %x", pcFromAsm, pcFromGo)
23
+ }
24
+
25
+ // Test FuncPC for imported function
26
+ pcFromGo = abi.FuncPCABI0(abi.FuncPCTestFn)
27
+ if pcFromGo != pcFromAsm {
28
+ t.Errorf("FuncPC returns wrong PC, want %x, got %x", pcFromAsm, pcFromGo)
29
+ }
30
+ }
31
+
32
+ func TestFuncPCCompileError(t *testing.T) {
33
+ // Test that FuncPC* on a function of a mismatched ABI is rejected.
34
+ testenv.MustHaveGoBuild(t)
35
+
36
+ // We want to test internal package, which we cannot normally import.
37
+ // Run the assembler and compiler manually.
38
+ tmpdir := t.TempDir()
39
+ asmSrc := filepath.Join("testdata", "x.s")
40
+ goSrc := filepath.Join("testdata", "x.go")
41
+ symabi := filepath.Join(tmpdir, "symabi")
42
+ obj := filepath.Join(tmpdir, "x.o")
43
+
44
+ // Write an importcfg file for the dependencies of the package.
45
+ importcfgfile := filepath.Join(tmpdir, "hello.importcfg")
46
+ testenv.WriteImportcfg(t, importcfgfile, nil, "internal/abi")
47
+
48
+ // parse assembly code for symabi.
49
+ cmd := testenv.Command(t, testenv.GoToolPath(t), "tool", "asm", "-p=p", "-gensymabis", "-o", symabi, asmSrc)
50
+ out, err := cmd.CombinedOutput()
51
+ if err != nil {
52
+ t.Fatalf("go tool asm -gensymabis failed: %v\n%s", err, out)
53
+ }
54
+
55
+ // compile go code.
56
+ cmd = testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-importcfg="+importcfgfile, "-p=p", "-symabis", symabi, "-o", obj, goSrc)
57
+ out, err = cmd.CombinedOutput()
58
+ if err == nil {
59
+ t.Fatalf("go tool compile did not fail")
60
+ }
61
+
62
+ // Expect errors in line 17, 18, 20, no errors on other lines.
63
+ want := []string{"x.go:17", "x.go:18", "x.go:20"}
64
+ got := strings.Split(string(out), "\n")
65
+ if got[len(got)-1] == "" {
66
+ got = got[:len(got)-1] // remove last empty line
67
+ }
68
+ for i, s := range got {
69
+ if !strings.Contains(s, want[i]) {
70
+ t.Errorf("did not error on line %s", want[i])
71
+ }
72
+ }
73
+ if len(got) != len(want) {
74
+ t.Errorf("unexpected number of errors, want %d, got %d", len(want), len(got))
75
+ }
76
+ if t.Failed() {
77
+ t.Logf("output:\n%s", string(out))
78
+ }
79
+ }
go/src/internal/abi/abi_test.s ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ #include "textflag.h"
6
+
7
+ #ifdef GOARCH_386
8
+ #define PTRSIZE 4
9
+ #endif
10
+ #ifdef GOARCH_arm
11
+ #define PTRSIZE 4
12
+ #endif
13
+ #ifdef GOARCH_mips
14
+ #define PTRSIZE 4
15
+ #endif
16
+ #ifdef GOARCH_mipsle
17
+ #define PTRSIZE 4
18
+ #endif
19
+ #ifndef PTRSIZE
20
+ #define PTRSIZE 8
21
+ #endif
22
+
23
+ TEXT internal∕abi·FuncPCTestFn(SB),NOSPLIT,$0-0
24
+ RET
25
+
26
+ GLOBL internal∕abi·FuncPCTestFnAddr(SB), NOPTR, $PTRSIZE
27
+ DATA internal∕abi·FuncPCTestFnAddr(SB)/PTRSIZE, $internal∕abi·FuncPCTestFn(SB)
go/src/internal/abi/bounds.go ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2025 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 abi
6
+
7
+ // This type and constants are for encoding different
8
+ // kinds of bounds check failures.
9
+ type BoundsErrorCode uint8
10
+
11
+ const (
12
+ BoundsIndex BoundsErrorCode = iota // s[x], 0 <= x < len(s) failed
13
+ BoundsSliceAlen // s[?:x], 0 <= x <= len(s) failed
14
+ BoundsSliceAcap // s[?:x], 0 <= x <= cap(s) failed
15
+ BoundsSliceB // s[x:y], 0 <= x <= y failed (but boundsSliceA didn't happen)
16
+ BoundsSlice3Alen // s[?:?:x], 0 <= x <= len(s) failed
17
+ BoundsSlice3Acap // s[?:?:x], 0 <= x <= cap(s) failed
18
+ BoundsSlice3B // s[?:x:y], 0 <= x <= y failed (but boundsSlice3A didn't happen)
19
+ BoundsSlice3C // s[x:y:?], 0 <= x <= y failed (but boundsSlice3A/B didn't happen)
20
+ BoundsConvert // (*[x]T)(s), 0 <= x <= len(s) failed
21
+ numBoundsCodes
22
+ )
23
+
24
+ const (
25
+ BoundsMaxReg = 15
26
+ BoundsMaxConst = 31
27
+ )
28
+
29
+ // Here's how we encode PCDATA_PanicBounds entries:
30
+
31
+ // We allow 16 registers (0-15) and 32 constants (0-31).
32
+ // Encode the following constant c:
33
+ // bits use
34
+ // -----------------------------
35
+ // 0 x is in a register
36
+ // 1 y is in a register
37
+ //
38
+ // if x is in a register
39
+ // 2 x is signed
40
+ // [3:6] x's register number
41
+ // else
42
+ // [2:6] x's constant value
43
+ //
44
+ // if y is in a register
45
+ // [7:10] y's register number
46
+ // else
47
+ // [7:11] y's constant value
48
+ //
49
+ // The final integer is c * numBoundsCode + code
50
+
51
+ // TODO: 32-bit
52
+
53
+ // Encode bounds failure information into an integer for PCDATA_PanicBounds.
54
+ // Register numbers must be in 0-15. Constants must be in 0-31.
55
+ func BoundsEncode(code BoundsErrorCode, signed, xIsReg, yIsReg bool, xVal, yVal int) int {
56
+ c := int(0)
57
+ if xIsReg {
58
+ c |= 1 << 0
59
+ if signed {
60
+ c |= 1 << 2
61
+ }
62
+ if xVal < 0 || xVal > BoundsMaxReg {
63
+ panic("bad xReg")
64
+ }
65
+ c |= xVal << 3
66
+ } else {
67
+ if xVal < 0 || xVal > BoundsMaxConst {
68
+ panic("bad xConst")
69
+ }
70
+ c |= xVal << 2
71
+ }
72
+ if yIsReg {
73
+ c |= 1 << 1
74
+ if yVal < 0 || yVal > BoundsMaxReg {
75
+ panic("bad yReg")
76
+ }
77
+ c |= yVal << 7
78
+ } else {
79
+ if yVal < 0 || yVal > BoundsMaxConst {
80
+ panic("bad yConst")
81
+ }
82
+ c |= yVal << 7
83
+ }
84
+ return c*int(numBoundsCodes) + int(code)
85
+ }
86
+ func BoundsDecode(v int) (code BoundsErrorCode, signed, xIsReg, yIsReg bool, xVal, yVal int) {
87
+ code = BoundsErrorCode(v % int(numBoundsCodes))
88
+ c := v / int(numBoundsCodes)
89
+ xIsReg = c&1 != 0
90
+ c >>= 1
91
+ yIsReg = c&1 != 0
92
+ c >>= 1
93
+ if xIsReg {
94
+ signed = c&1 != 0
95
+ c >>= 1
96
+ xVal = c & 0xf
97
+ c >>= 4
98
+ } else {
99
+ xVal = c & 0x1f
100
+ c >>= 5
101
+ }
102
+ if yIsReg {
103
+ yVal = c & 0xf
104
+ c >>= 4
105
+ } else {
106
+ yVal = c & 0x1f
107
+ c >>= 5
108
+ }
109
+ if c != 0 {
110
+ panic("BoundsDecode decoding error")
111
+ }
112
+ return
113
+ }
go/src/internal/abi/compiletype.go ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2023 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 abi
6
+
7
+ // These functions are the build-time version of the Go type data structures.
8
+
9
+ // Their contents must be kept in sync with their definitions.
10
+ // Because the host and target type sizes can differ, the compiler and
11
+ // linker cannot use the host information that they might get from
12
+ // either unsafe.Sizeof and Alignof, nor runtime, reflect, or reflectlite.
13
+
14
+ // CommonSize returns sizeof(Type) for a compilation target with a given ptrSize
15
+ func CommonSize(ptrSize int) int { return 4*ptrSize + 8 + 8 }
16
+
17
+ // StructFieldSize returns sizeof(StructField) for a compilation target with a given ptrSize
18
+ func StructFieldSize(ptrSize int) int { return 3 * ptrSize }
19
+
20
+ // UncommonSize returns sizeof(UncommonType). This currently does not depend on ptrSize.
21
+ // This exported function is in an internal package, so it may change to depend on ptrSize in the future.
22
+ func UncommonSize() uint64 { return 4 + 2 + 2 + 4 + 4 }
23
+
24
+ // TFlagOff returns the offset of Type.TFlag for a compilation target with a given ptrSize
25
+ func TFlagOff(ptrSize int) int { return 2*ptrSize + 4 }
26
+
27
+ // ITabTypeOff returns the offset of ITab.Type for a compilation target with a given ptrSize
28
+ func ITabTypeOff(ptrSize int) int { return ptrSize }
go/src/internal/abi/escape.go ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2024 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 abi
6
+
7
+ import "unsafe"
8
+
9
+ // NoEscape hides the pointer p from escape analysis, preventing it
10
+ // from escaping to the heap. It compiles down to nothing.
11
+ //
12
+ // WARNING: This is very subtle to use correctly. The caller must
13
+ // ensure that it's truly safe for p to not escape to the heap by
14
+ // maintaining runtime pointer invariants (for example, that globals
15
+ // and the heap may not generally point into a stack).
16
+ //
17
+ //go:nosplit
18
+ //go:nocheckptr
19
+ func NoEscape(p unsafe.Pointer) unsafe.Pointer {
20
+ x := uintptr(p)
21
+ return unsafe.Pointer(x ^ 0)
22
+ }
23
+
24
+ var alwaysFalse bool
25
+ var escapeSink any
26
+
27
+ // Escape forces any pointers in x to escape to the heap.
28
+ func Escape[T any](x T) T {
29
+ if alwaysFalse {
30
+ escapeSink = x
31
+ }
32
+ return x
33
+ }
34
+
35
+ // EscapeNonString forces v to be on the heap, if v contains a
36
+ // non-string pointer.
37
+ //
38
+ // This is used in hash/maphash.Comparable. We cannot hash pointers
39
+ // to local variables on stack, as their addresses might change on
40
+ // stack growth. Strings are okay as the hash depends on only the
41
+ // content, not the pointer.
42
+ //
43
+ // This is essentially
44
+ //
45
+ // if hasNonStringPointers(T) { Escape(v) }
46
+ //
47
+ // Implemented as a compiler intrinsic.
48
+ func EscapeNonString[T any](v T) { panic("intrinsic") }
49
+
50
+ // EscapeToResultNonString models a data flow edge from v to the result,
51
+ // if v contains a non-string pointer. If v contains only string pointers,
52
+ // it returns a copy of v, but is not modeled as a data flow edge
53
+ // from the escape analysis's perspective.
54
+ //
55
+ // This is used in unique.clone, to model the data flow edge on the
56
+ // value with strings excluded, because strings are cloned (by
57
+ // content).
58
+ //
59
+ // TODO: probably we should define this as a intrinsic and EscapeNonString
60
+ // could just be "heap = EscapeToResultNonString(v)". This way we can model
61
+ // an edge to the result but not necessarily heap.
62
+ func EscapeToResultNonString[T any](v T) T {
63
+ EscapeNonString(v)
64
+ return *(*T)(NoEscape(unsafe.Pointer(&v)))
65
+ }
go/src/internal/abi/export_test.go ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 abi
6
+
7
+ func FuncPCTestFn()
8
+
9
+ var FuncPCTestFnAddr uintptr // address of FuncPCTestFn, directly retrieved from assembly
10
+
11
+ //go:noinline
12
+ func FuncPCTest() uintptr {
13
+ return FuncPCABI0(FuncPCTestFn)
14
+ }
go/src/internal/abi/funcpc.go ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2023 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
+ //go:build !gccgo
6
+
7
+ package abi
8
+
9
+ // FuncPC* intrinsics.
10
+ //
11
+ // CAREFUL: In programs with plugins, FuncPC* can return different values
12
+ // for the same function (because there are actually multiple copies of
13
+ // the same function in the address space). To be safe, don't use the
14
+ // results of this function in any == expression. It is only safe to
15
+ // use the result as an address at which to start executing code.
16
+
17
+ // FuncPCABI0 returns the entry PC of the function f, which must be a
18
+ // direct reference of a function defined as ABI0. Otherwise it is a
19
+ // compile-time error.
20
+ //
21
+ // Implemented as a compile intrinsic.
22
+ func FuncPCABI0(f any) uintptr
23
+
24
+ // FuncPCABIInternal returns the entry PC of the function f. If f is a
25
+ // direct reference of a function, it must be defined as ABIInternal.
26
+ // Otherwise it is a compile-time error. If f is not a direct reference
27
+ // of a defined function, it assumes that f is a func value. Otherwise
28
+ // the behavior is undefined.
29
+ //
30
+ // Implemented as a compile intrinsic.
31
+ func FuncPCABIInternal(f any) uintptr
go/src/internal/abi/funcpc_gccgo.go ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2023 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
+ // For bootstrapping with gccgo.
6
+
7
+ //go:build gccgo
8
+
9
+ package abi
10
+
11
+ import "unsafe"
12
+
13
+ func FuncPCABI0(f interface{}) uintptr {
14
+ words := (*[2]unsafe.Pointer)(unsafe.Pointer(&f))
15
+ return *(*uintptr)(unsafe.Pointer(words[1]))
16
+ }
17
+
18
+ func FuncPCABIInternal(f interface{}) uintptr {
19
+ words := (*[2]unsafe.Pointer)(unsafe.Pointer(&f))
20
+ return *(*uintptr)(unsafe.Pointer(words[1]))
21
+ }
go/src/internal/abi/iface.go ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2023 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 abi
6
+
7
+ import "unsafe"
8
+
9
+ // The first word of every non-empty interface type contains an *ITab.
10
+ // It records the underlying concrete type (Type), the interface type it
11
+ // is implementing (Inter), and some ancillary information.
12
+ //
13
+ // allocated in non-garbage-collected memory
14
+ type ITab struct {
15
+ Inter *InterfaceType
16
+ Type *Type
17
+ Hash uint32 // copy of Type.Hash. Used for type switches.
18
+ Fun [1]uintptr // variable sized. fun[0]==0 means Type does not implement Inter.
19
+ }
20
+
21
+ // EmptyInterface describes the layout of a "interface{}" or a "any."
22
+ // These are represented differently than non-empty interface, as the first
23
+ // word always points to an abi.Type.
24
+ type EmptyInterface struct {
25
+ Type *Type
26
+ Data unsafe.Pointer
27
+ }
28
+
29
+ // NonEmptyInterface describes the layout of an interface that contains any methods.
30
+ type NonEmptyInterface struct {
31
+ ITab *ITab
32
+ Data unsafe.Pointer
33
+ }
34
+
35
+ // CommonInterface describes the layout of both [EmptyInterface] and [NonEmptyInterface].
36
+ type CommonInterface struct {
37
+ // Either an *ITab or a *Type, unexported to avoid accidental use.
38
+ _ unsafe.Pointer
39
+
40
+ Data unsafe.Pointer
41
+ }
go/src/internal/abi/map.go ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2023 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 abi
6
+
7
+ import (
8
+ "unsafe"
9
+ )
10
+
11
+ // Map constants common to several packages
12
+ // runtime/runtime-gdb.py:MapTypePrinter contains its own copy
13
+ const (
14
+ // Number of bits in the group.slot count.
15
+ MapGroupSlotsBits = 3
16
+
17
+ // Number of slots in a group.
18
+ MapGroupSlots = 1 << MapGroupSlotsBits // 8
19
+
20
+ // Maximum key or elem size to keep inline (instead of mallocing per element).
21
+ // Must fit in a uint8.
22
+ MapMaxKeyBytes = 128
23
+ MapMaxElemBytes = 128
24
+
25
+ ctrlEmpty = 0b10000000
26
+ bitsetLSB = 0x0101010101010101
27
+
28
+ // Value of control word with all empty slots.
29
+ MapCtrlEmpty = bitsetLSB * uint64(ctrlEmpty)
30
+ )
31
+
32
+ type MapType struct {
33
+ Type
34
+ Key *Type
35
+ Elem *Type
36
+ Group *Type // internal type representing a slot group
37
+ // function for hashing keys (ptr to key, seed) -> hash
38
+ Hasher func(unsafe.Pointer, uintptr) uintptr
39
+ GroupSize uintptr // == Group.Size_
40
+ SlotSize uintptr // size of key/elem slot
41
+ ElemOff uintptr // offset of elem in key/elem slot
42
+ Flags uint32
43
+ }
44
+
45
+ // Flag values
46
+ const (
47
+ MapNeedKeyUpdate = 1 << iota
48
+ MapHashMightPanic
49
+ MapIndirectKey
50
+ MapIndirectElem
51
+ )
52
+
53
+ func (mt *MapType) NeedKeyUpdate() bool { // true if we need to update key on an overwrite
54
+ return mt.Flags&MapNeedKeyUpdate != 0
55
+ }
56
+ func (mt *MapType) HashMightPanic() bool { // true if hash function might panic
57
+ return mt.Flags&MapHashMightPanic != 0
58
+ }
59
+ func (mt *MapType) IndirectKey() bool { // store ptr to key instead of key itself
60
+ return mt.Flags&MapIndirectKey != 0
61
+ }
62
+ func (mt *MapType) IndirectElem() bool { // store ptr to elem instead of elem itself
63
+ return mt.Flags&MapIndirectElem != 0
64
+ }
go/src/internal/abi/rangefuncconsts.go ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2024 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 abi
6
+
7
+ type RF_State int
8
+
9
+ // These constants are shared between the compiler, which uses them for state functions
10
+ // and panic indicators, and the runtime, which turns them into more meaningful strings
11
+ // For best code generation, RF_DONE and RF_READY should be 0 and 1.
12
+ const (
13
+ RF_DONE = RF_State(iota) // body of loop has exited in a non-panic way
14
+ RF_READY // body of loop has not exited yet, is not running -- this is not a panic index
15
+ RF_PANIC // body of loop is either currently running, or has panicked
16
+ RF_EXHAUSTED // iterator function return, i.e., sequence is "exhausted"
17
+ RF_MISSING_PANIC = 4 // body of loop panicked but iterator function defer-recovered it away
18
+ )
go/src/internal/abi/runtime.go ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2024 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 abi
6
+
7
+ // ZeroValSize is the size in bytes of runtime.zeroVal.
8
+ const ZeroValSize = 1024
go/src/internal/abi/stack.go ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2023 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 abi
6
+
7
+ const (
8
+ // StackNosplitBase is the base maximum number of bytes that a chain of
9
+ // NOSPLIT functions can use.
10
+ //
11
+ // This value must be multiplied by the stack guard multiplier, so do not
12
+ // use it directly. See runtime/stack.go:stackNosplit and
13
+ // cmd/internal/objabi/stack.go:StackNosplit.
14
+ StackNosplitBase = 800
15
+
16
+ // We have three different sequences for stack bounds checks, depending on
17
+ // whether the stack frame of a function is small, big, or huge.
18
+
19
+ // After a stack split check the SP is allowed to be StackSmall bytes below
20
+ // the stack guard.
21
+ //
22
+ // Functions that need frames <= StackSmall can perform the stack check
23
+ // using a single comparison directly between the stack guard and the SP
24
+ // because we ensure that StackSmall bytes of stack space are available
25
+ // beyond the stack guard.
26
+ StackSmall = 128
27
+
28
+ // Functions that need frames <= StackBig can assume that neither
29
+ // SP-framesize nor stackGuard-StackSmall will underflow, and thus use a
30
+ // more efficient check. In order to ensure this, StackBig must be <= the
31
+ // size of the unmapped space at zero.
32
+ StackBig = 4096
33
+ )
go/src/internal/abi/stub.s ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ // Copyright 2023 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
+ // This file silences errors about body-less functions
6
+ // that are provided by intrinsics in the latest version of the compiler,
7
+ // but may not be known to the bootstrap compiler.
go/src/internal/abi/switch.go ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2023 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 abi
6
+
7
+ import "internal/goarch"
8
+
9
+ type InterfaceSwitch struct {
10
+ Cache *InterfaceSwitchCache
11
+ NCases int
12
+
13
+ // Array of NCases elements.
14
+ // Each case must be a non-empty interface type.
15
+ Cases [1]*InterfaceType
16
+ }
17
+
18
+ type InterfaceSwitchCache struct {
19
+ Mask uintptr // mask for index. Must be a power of 2 minus 1
20
+ Entries [1]InterfaceSwitchCacheEntry // Mask+1 entries total
21
+ }
22
+
23
+ type InterfaceSwitchCacheEntry struct {
24
+ // type of source value (a *Type)
25
+ Typ uintptr
26
+ // case # to dispatch to
27
+ Case int
28
+ // itab to use for resulting case variable (a *runtime.itab)
29
+ Itab uintptr
30
+ }
31
+
32
+ func UseInterfaceSwitchCache(arch goarch.ArchFamilyType) bool {
33
+ // We need an atomic load instruction to make the cache multithreaded-safe.
34
+ // (AtomicLoadPtr needs to be implemented in cmd/compile/internal/ssa/_gen/ARCH.rules.)
35
+ switch arch {
36
+ case goarch.AMD64, goarch.ARM64, goarch.LOONG64, goarch.MIPS, goarch.MIPS64, goarch.PPC64, goarch.RISCV64, goarch.S390X:
37
+ return true
38
+ default:
39
+ return false
40
+ }
41
+ }
42
+
43
+ type TypeAssert struct {
44
+ Cache *TypeAssertCache
45
+ Inter *InterfaceType
46
+ CanFail bool
47
+ }
48
+ type TypeAssertCache struct {
49
+ Mask uintptr
50
+ Entries [1]TypeAssertCacheEntry
51
+ }
52
+ type TypeAssertCacheEntry struct {
53
+ // type of source value (a *runtime._type)
54
+ Typ uintptr
55
+ // itab to use for result (a *runtime.itab)
56
+ // nil if CanFail is set and conversion would fail.
57
+ Itab uintptr
58
+ }
go/src/internal/abi/symtab.go ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2023 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 abi
6
+
7
+ // PCLnTabMagic is the version at the start of the PC/line table.
8
+ // This is the start of the .pclntab section, and is also runtime.pcHeader.
9
+ // The magic numbers are chosen such that reading the value with
10
+ // a different endianness does not result in the same value.
11
+ // That lets us the magic number to determine the endianness.
12
+ type PCLnTabMagic uint32
13
+
14
+ const (
15
+ // Initial PCLnTabMagic value used in Go 1.2 through Go 1.15.
16
+ Go12PCLnTabMagic PCLnTabMagic = 0xfffffffb
17
+ // PCLnTabMagic value used in Go 1.16 through Go 1.17.
18
+ // Several fields added to header (CL 241598).
19
+ Go116PCLnTabMagic PCLnTabMagic = 0xfffffffa
20
+ // PCLnTabMagic value used in Go 1.18 through Go 1.19.
21
+ // Entry PC of func data changed from address to offset (CL 351463).
22
+ Go118PCLnTabMagic PCLnTabMagic = 0xfffffff0
23
+ // PCLnTabMagic value used in Go 1.20 and later.
24
+ // A ":" was added to generated symbol names (#37762).
25
+ Go120PCLnTabMagic PCLnTabMagic = 0xfffffff1
26
+
27
+ // CurrentPCLnTabMagic is the value emitted by the current toolchain.
28
+ // This is written by the linker to the pcHeader and read by the
29
+ // runtime and debug/gosym (and external tools like Delve).
30
+ //
31
+ // Change this value when updating the pclntab version.
32
+ // Changing this exported value is OK because is an
33
+ // internal package.
34
+ CurrentPCLnTabMagic = Go120PCLnTabMagic
35
+ )
36
+
37
+ // A FuncFlag records bits about a function, passed to the runtime.
38
+ type FuncFlag uint8
39
+
40
+ const (
41
+ // FuncFlagTopFrame indicates a function that appears at the top of its stack.
42
+ // The traceback routine stop at such a function and consider that a
43
+ // successful, complete traversal of the stack.
44
+ // Examples of TopFrame functions include goexit, which appears
45
+ // at the top of a user goroutine stack, and mstart, which appears
46
+ // at the top of a system goroutine stack.
47
+ FuncFlagTopFrame FuncFlag = 1 << iota
48
+
49
+ // FuncFlagSPWrite indicates a function that writes an arbitrary value to SP
50
+ // (any write other than adding or subtracting a constant amount).
51
+ // The traceback routines cannot encode such changes into the
52
+ // pcsp tables, so the function traceback cannot safely unwind past
53
+ // SPWrite functions. Stopping at an SPWrite function is considered
54
+ // to be an incomplete unwinding of the stack. In certain contexts
55
+ // (in particular garbage collector stack scans) that is a fatal error.
56
+ FuncFlagSPWrite
57
+
58
+ // FuncFlagAsm indicates that a function was implemented in assembly.
59
+ FuncFlagAsm
60
+ )
61
+
62
+ // A FuncID identifies particular functions that need to be treated
63
+ // specially by the runtime.
64
+ // Note that in some situations involving plugins, there may be multiple
65
+ // copies of a particular special runtime function.
66
+ type FuncID uint8
67
+
68
+ const (
69
+ // If you add a FuncID, you probably also want to add an entry to the map in
70
+ // ../../cmd/internal/objabi/funcid.go
71
+
72
+ FuncIDNormal FuncID = iota // not a special function
73
+ FuncID_abort
74
+ FuncID_asmcgocall
75
+ FuncID_asyncPreempt
76
+ FuncID_cgocallback
77
+ FuncID_corostart
78
+ FuncID_debugCallV2
79
+ FuncID_gcBgMarkWorker
80
+ FuncID_goexit
81
+ FuncID_gogo
82
+ FuncID_gopanic
83
+ FuncID_handleAsyncEvent
84
+ FuncID_mcall
85
+ FuncID_morestack
86
+ FuncID_mstart
87
+ FuncID_panicwrap
88
+ FuncID_rt0_go
89
+ FuncID_runtime_main
90
+ FuncID_runFinalizers
91
+ FuncID_runCleanups
92
+ FuncID_sigpanic
93
+ FuncID_systemstack
94
+ FuncID_systemstack_switch
95
+ FuncIDWrapper // any autogenerated code (hash/eq algorithms, method wrappers, etc.)
96
+ )
97
+
98
+ // ArgsSizeUnknown is set in Func.argsize to mark all functions
99
+ // whose argument size is unknown (C vararg functions, and
100
+ // assembly code without an explicit specification).
101
+ // This value is generated by the compiler, assembler, or linker.
102
+ const ArgsSizeUnknown = -0x80000000
103
+
104
+ // IDs for PCDATA and FUNCDATA tables in Go binaries.
105
+ //
106
+ // These must agree with ../../../runtime/funcdata.h.
107
+ const (
108
+ PCDATA_UnsafePoint = 0
109
+ PCDATA_StackMapIndex = 1
110
+ PCDATA_InlTreeIndex = 2
111
+ PCDATA_ArgLiveIndex = 3
112
+ PCDATA_PanicBounds = 4
113
+
114
+ FUNCDATA_ArgsPointerMaps = 0
115
+ FUNCDATA_LocalsPointerMaps = 1
116
+ FUNCDATA_StackObjects = 2
117
+ FUNCDATA_InlTree = 3
118
+ FUNCDATA_OpenCodedDeferInfo = 4
119
+ FUNCDATA_ArgInfo = 5
120
+ FUNCDATA_ArgLiveInfo = 6
121
+ FUNCDATA_WrapInfo = 7
122
+ )
123
+
124
+ // Special values for the PCDATA_UnsafePoint table.
125
+ const (
126
+ UnsafePointSafe = -1 // Safe for async preemption
127
+ UnsafePointUnsafe = -2 // Unsafe for async preemption
128
+
129
+ // UnsafePointRestart1(2) apply on a sequence of instructions, within
130
+ // which if an async preemption happens, we should back off the PC
131
+ // to the start of the sequence when resuming.
132
+ // We need two so we can distinguish the start/end of the sequence
133
+ // in case that two sequences are next to each other.
134
+ UnsafePointRestart1 = -3
135
+ UnsafePointRestart2 = -4
136
+
137
+ // Like UnsafePointRestart1, but back to function entry if async preempted.
138
+ UnsafePointRestartAtEntry = -5
139
+ )
140
+
141
+ const MINFUNC = 16 // minimum size for a function
142
+
143
+ const FuncTabBucketSize = 256 * MINFUNC // size of bucket in the pc->func lookup table
go/src/internal/abi/type.go ADDED
@@ -0,0 +1,777 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2023 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 abi
6
+
7
+ import (
8
+ "unsafe"
9
+ )
10
+
11
+ // Type is the runtime representation of a Go type.
12
+ //
13
+ // Be careful about accessing this type at build time, as the version
14
+ // of this type in the compiler/linker may not have the same layout
15
+ // as the version in the target binary, due to pointer width
16
+ // differences and any experiments. Use cmd/compile/internal/rttype
17
+ // or the functions in compiletype.go to access this type instead.
18
+ // (TODO: this admonition applies to every type in this package.
19
+ // Put it in some shared location?)
20
+ type Type struct {
21
+ Size_ uintptr
22
+ PtrBytes uintptr // number of (prefix) bytes in the type that can contain pointers
23
+ Hash uint32 // hash of type; avoids computation in hash tables
24
+ TFlag TFlag // extra type information flags
25
+ Align_ uint8 // alignment of variable with this type
26
+ FieldAlign_ uint8 // alignment of struct field with this type
27
+ Kind_ Kind // what kind of type this is (string, int, ...)
28
+ // function for comparing objects of this type
29
+ // (ptr to object A, ptr to object B) -> ==?
30
+ Equal func(unsafe.Pointer, unsafe.Pointer) bool
31
+ // GCData stores the GC type data for the garbage collector.
32
+ // Normally, GCData points to a bitmask that describes the
33
+ // ptr/nonptr fields of the type. The bitmask will have at
34
+ // least PtrBytes/ptrSize bits.
35
+ // If the TFlagGCMaskOnDemand bit is set, GCData is instead a
36
+ // **byte and the pointer to the bitmask is one dereference away.
37
+ // The runtime will build the bitmask if needed.
38
+ // (See runtime/type.go:getGCMask.)
39
+ // Note: multiple types may have the same value of GCData,
40
+ // including when TFlagGCMaskOnDemand is set. The types will, of course,
41
+ // have the same pointer layout (but not necessarily the same size).
42
+ GCData *byte
43
+ Str NameOff // string form
44
+ PtrToThis TypeOff // type for pointer to this type, may be zero
45
+ }
46
+
47
+ // A Kind represents the specific kind of type that a Type represents.
48
+ // The zero Kind is not a valid kind.
49
+ type Kind uint8
50
+
51
+ const (
52
+ Invalid Kind = iota
53
+ Bool
54
+ Int
55
+ Int8
56
+ Int16
57
+ Int32
58
+ Int64
59
+ Uint
60
+ Uint8
61
+ Uint16
62
+ Uint32
63
+ Uint64
64
+ Uintptr
65
+ Float32
66
+ Float64
67
+ Complex64
68
+ Complex128
69
+ Array
70
+ Chan
71
+ Func
72
+ Interface
73
+ Map
74
+ Pointer
75
+ Slice
76
+ String
77
+ Struct
78
+ UnsafePointer
79
+ )
80
+
81
+ // TFlag is used by a Type to signal what extra type information is
82
+ // available in the memory directly following the Type value.
83
+ type TFlag uint8
84
+
85
+ const (
86
+ // TFlagUncommon means that there is a data with a type, UncommonType,
87
+ // just beyond the shared-per-type common data. That is, the data
88
+ // for struct types will store their UncommonType at one offset, the
89
+ // data for interface types will store their UncommonType at a different
90
+ // offset. UncommonType is always accessed via a pointer that is computed
91
+ // using trust-us-we-are-the-implementors pointer arithmetic.
92
+ //
93
+ // For example, if t.Kind() == Struct and t.tflag&TFlagUncommon != 0,
94
+ // then t has UncommonType data and it can be accessed as:
95
+ //
96
+ // type structTypeUncommon struct {
97
+ // structType
98
+ // u UncommonType
99
+ // }
100
+ // u := &(*structTypeUncommon)(unsafe.Pointer(t)).u
101
+ TFlagUncommon TFlag = 1 << 0
102
+
103
+ // TFlagExtraStar means the name in the str field has an
104
+ // extraneous '*' prefix. This is because for most types T in
105
+ // a program, the type *T also exists and reusing the str data
106
+ // saves binary size.
107
+ TFlagExtraStar TFlag = 1 << 1
108
+
109
+ // TFlagNamed means the type has a name.
110
+ TFlagNamed TFlag = 1 << 2
111
+
112
+ // TFlagRegularMemory means that equal and hash functions can treat
113
+ // this type as a single region of t.size bytes.
114
+ TFlagRegularMemory TFlag = 1 << 3
115
+
116
+ // TFlagGCMaskOnDemand means that the GC pointer bitmask will be
117
+ // computed on demand at runtime instead of being precomputed at
118
+ // compile time. If this flag is set, the GCData field effectively
119
+ // has type **byte instead of *byte. The runtime will store a
120
+ // pointer to the GC pointer bitmask in *GCData.
121
+ TFlagGCMaskOnDemand TFlag = 1 << 4
122
+
123
+ // TFlagDirectIface means that a value of this type is stored directly
124
+ // in the data field of an interface, instead of indirectly.
125
+ // This flag is just a cached computation of Size_ == PtrBytes == goarch.PtrSize.
126
+ TFlagDirectIface TFlag = 1 << 5
127
+
128
+ // Leaving this breadcrumb behind for dlv. It should not be used, and no
129
+ // Kind should be big enough to set this bit.
130
+ KindDirectIface Kind = 1 << 5
131
+ )
132
+
133
+ // NameOff is the offset to a name from moduledata.types. See resolveNameOff in runtime.
134
+ type NameOff int32
135
+
136
+ // TypeOff is the offset to a type from moduledata.types. See resolveTypeOff in runtime.
137
+ type TypeOff int32
138
+
139
+ // TextOff is an offset from the top of a text section. See (rtype).textOff in runtime.
140
+ type TextOff int32
141
+
142
+ // String returns the name of k.
143
+ func (k Kind) String() string {
144
+ if int(k) < len(kindNames) {
145
+ return kindNames[k]
146
+ }
147
+ return kindNames[0]
148
+ }
149
+
150
+ var kindNames = []string{
151
+ Invalid: "invalid",
152
+ Bool: "bool",
153
+ Int: "int",
154
+ Int8: "int8",
155
+ Int16: "int16",
156
+ Int32: "int32",
157
+ Int64: "int64",
158
+ Uint: "uint",
159
+ Uint8: "uint8",
160
+ Uint16: "uint16",
161
+ Uint32: "uint32",
162
+ Uint64: "uint64",
163
+ Uintptr: "uintptr",
164
+ Float32: "float32",
165
+ Float64: "float64",
166
+ Complex64: "complex64",
167
+ Complex128: "complex128",
168
+ Array: "array",
169
+ Chan: "chan",
170
+ Func: "func",
171
+ Interface: "interface",
172
+ Map: "map",
173
+ Pointer: "ptr",
174
+ Slice: "slice",
175
+ String: "string",
176
+ Struct: "struct",
177
+ UnsafePointer: "unsafe.Pointer",
178
+ }
179
+
180
+ // TypeOf returns the abi.Type of some value.
181
+ func TypeOf(a any) *Type {
182
+ eface := *(*EmptyInterface)(unsafe.Pointer(&a))
183
+ // Types are either static (for compiler-created types) or
184
+ // heap-allocated but always reachable (for reflection-created
185
+ // types, held in the central map). So there is no need to
186
+ // escape types. noescape here help avoid unnecessary escape
187
+ // of v.
188
+ return (*Type)(NoEscape(unsafe.Pointer(eface.Type)))
189
+ }
190
+
191
+ // TypeFor returns the abi.Type for a type parameter.
192
+ func TypeFor[T any]() *Type {
193
+ return (*PtrType)(unsafe.Pointer(TypeOf((*T)(nil)))).Elem
194
+ }
195
+
196
+ func (t *Type) Kind() Kind { return t.Kind_ }
197
+
198
+ func (t *Type) HasName() bool {
199
+ return t.TFlag&TFlagNamed != 0
200
+ }
201
+
202
+ // Pointers reports whether t contains pointers.
203
+ func (t *Type) Pointers() bool { return t.PtrBytes != 0 }
204
+
205
+ // IsDirectIface reports whether t is stored directly in an interface value.
206
+ func (t *Type) IsDirectIface() bool {
207
+ return t.TFlag&TFlagDirectIface != 0
208
+ }
209
+
210
+ func (t *Type) GcSlice(begin, end uintptr) []byte {
211
+ if t.TFlag&TFlagGCMaskOnDemand != 0 {
212
+ panic("GcSlice can't handle on-demand gcdata types")
213
+ }
214
+ return unsafe.Slice(t.GCData, int(end))[begin:]
215
+ }
216
+
217
+ // Method on non-interface type
218
+ type Method struct {
219
+ Name NameOff // name of method
220
+ Mtyp TypeOff // method type (without receiver)
221
+ Ifn TextOff // fn used in interface call (one-word receiver)
222
+ Tfn TextOff // fn used for normal method call
223
+ }
224
+
225
+ // UncommonType is present only for defined types or types with methods
226
+ // (if T is a defined type, the uncommonTypes for T and *T have methods).
227
+ // Using a pointer to this struct reduces the overall size required
228
+ // to describe a non-defined type with no methods.
229
+ type UncommonType struct {
230
+ PkgPath NameOff // import path; empty for built-in types like int, string
231
+ Mcount uint16 // number of methods
232
+ Xcount uint16 // number of exported methods
233
+ Moff uint32 // offset from this uncommontype to [mcount]Method
234
+ _ uint32 // unused
235
+ }
236
+
237
+ func (t *UncommonType) Methods() []Method {
238
+ if t.Mcount == 0 {
239
+ return nil
240
+ }
241
+ return (*[1 << 16]Method)(addChecked(unsafe.Pointer(t), uintptr(t.Moff), "t.mcount > 0"))[:t.Mcount:t.Mcount]
242
+ }
243
+
244
+ func (t *UncommonType) ExportedMethods() []Method {
245
+ if t.Xcount == 0 {
246
+ return nil
247
+ }
248
+ return (*[1 << 16]Method)(addChecked(unsafe.Pointer(t), uintptr(t.Moff), "t.xcount > 0"))[:t.Xcount:t.Xcount]
249
+ }
250
+
251
+ // addChecked returns p+x.
252
+ //
253
+ // The whySafe string is ignored, so that the function still inlines
254
+ // as efficiently as p+x, but all call sites should use the string to
255
+ // record why the addition is safe, which is to say why the addition
256
+ // does not cause x to advance to the very end of p's allocation
257
+ // and therefore point incorrectly at the next block in memory.
258
+ func addChecked(p unsafe.Pointer, x uintptr, whySafe string) unsafe.Pointer {
259
+ return unsafe.Pointer(uintptr(p) + x)
260
+ }
261
+
262
+ // Imethod represents a method on an interface type
263
+ type Imethod struct {
264
+ Name NameOff // name of method
265
+ Typ TypeOff // .(*FuncType) underneath
266
+ }
267
+
268
+ // ArrayType represents a fixed array type.
269
+ type ArrayType struct {
270
+ Type
271
+ Elem *Type // array element type
272
+ Slice *Type // slice type
273
+ Len uintptr
274
+ }
275
+
276
+ // Len returns the length of t if t is an array type, otherwise 0
277
+ func (t *Type) Len() int {
278
+ if t.Kind() == Array {
279
+ return int((*ArrayType)(unsafe.Pointer(t)).Len)
280
+ }
281
+ return 0
282
+ }
283
+
284
+ func (t *Type) Common() *Type {
285
+ return t
286
+ }
287
+
288
+ type ChanDir int
289
+
290
+ const (
291
+ RecvDir ChanDir = 1 << iota // <-chan
292
+ SendDir // chan<-
293
+ BothDir = RecvDir | SendDir // chan
294
+ InvalidDir ChanDir = 0
295
+ )
296
+
297
+ // ChanType represents a channel type
298
+ type ChanType struct {
299
+ Type
300
+ Elem *Type
301
+ Dir ChanDir
302
+ }
303
+
304
+ type structTypeUncommon struct {
305
+ StructType
306
+ u UncommonType
307
+ }
308
+
309
+ // ChanDir returns the direction of t if t is a channel type, otherwise InvalidDir (0).
310
+ func (t *Type) ChanDir() ChanDir {
311
+ if t.Kind() == Chan {
312
+ ch := (*ChanType)(unsafe.Pointer(t))
313
+ return ch.Dir
314
+ }
315
+ return InvalidDir
316
+ }
317
+
318
+ // Uncommon returns a pointer to T's "uncommon" data if there is any, otherwise nil
319
+ func (t *Type) Uncommon() *UncommonType {
320
+ if t.TFlag&TFlagUncommon == 0 {
321
+ return nil
322
+ }
323
+ switch t.Kind() {
324
+ case Struct:
325
+ return &(*structTypeUncommon)(unsafe.Pointer(t)).u
326
+ case Pointer:
327
+ type u struct {
328
+ PtrType
329
+ u UncommonType
330
+ }
331
+ return &(*u)(unsafe.Pointer(t)).u
332
+ case Func:
333
+ type u struct {
334
+ FuncType
335
+ u UncommonType
336
+ }
337
+ return &(*u)(unsafe.Pointer(t)).u
338
+ case Slice:
339
+ type u struct {
340
+ SliceType
341
+ u UncommonType
342
+ }
343
+ return &(*u)(unsafe.Pointer(t)).u
344
+ case Array:
345
+ type u struct {
346
+ ArrayType
347
+ u UncommonType
348
+ }
349
+ return &(*u)(unsafe.Pointer(t)).u
350
+ case Chan:
351
+ type u struct {
352
+ ChanType
353
+ u UncommonType
354
+ }
355
+ return &(*u)(unsafe.Pointer(t)).u
356
+ case Map:
357
+ type u struct {
358
+ MapType
359
+ u UncommonType
360
+ }
361
+ return &(*u)(unsafe.Pointer(t)).u
362
+ case Interface:
363
+ type u struct {
364
+ InterfaceType
365
+ u UncommonType
366
+ }
367
+ return &(*u)(unsafe.Pointer(t)).u
368
+ default:
369
+ type u struct {
370
+ Type
371
+ u UncommonType
372
+ }
373
+ return &(*u)(unsafe.Pointer(t)).u
374
+ }
375
+ }
376
+
377
+ // Elem returns the element type for t if t is an array, channel, map, pointer, or slice, otherwise nil.
378
+ func (t *Type) Elem() *Type {
379
+ switch t.Kind() {
380
+ case Array:
381
+ tt := (*ArrayType)(unsafe.Pointer(t))
382
+ return tt.Elem
383
+ case Chan:
384
+ tt := (*ChanType)(unsafe.Pointer(t))
385
+ return tt.Elem
386
+ case Map:
387
+ tt := (*MapType)(unsafe.Pointer(t))
388
+ return tt.Elem
389
+ case Pointer:
390
+ tt := (*PtrType)(unsafe.Pointer(t))
391
+ return tt.Elem
392
+ case Slice:
393
+ tt := (*SliceType)(unsafe.Pointer(t))
394
+ return tt.Elem
395
+ }
396
+ return nil
397
+ }
398
+
399
+ // StructType returns t cast to a *StructType, or nil if its tag does not match.
400
+ func (t *Type) StructType() *StructType {
401
+ if t.Kind() != Struct {
402
+ return nil
403
+ }
404
+ return (*StructType)(unsafe.Pointer(t))
405
+ }
406
+
407
+ // MapType returns t cast to a *MapType, or nil if its tag does not match.
408
+ func (t *Type) MapType() *MapType {
409
+ if t.Kind() != Map {
410
+ return nil
411
+ }
412
+ return (*MapType)(unsafe.Pointer(t))
413
+ }
414
+
415
+ // ArrayType returns t cast to a *ArrayType, or nil if its tag does not match.
416
+ func (t *Type) ArrayType() *ArrayType {
417
+ if t.Kind() != Array {
418
+ return nil
419
+ }
420
+ return (*ArrayType)(unsafe.Pointer(t))
421
+ }
422
+
423
+ // FuncType returns t cast to a *FuncType, or nil if its tag does not match.
424
+ func (t *Type) FuncType() *FuncType {
425
+ if t.Kind() != Func {
426
+ return nil
427
+ }
428
+ return (*FuncType)(unsafe.Pointer(t))
429
+ }
430
+
431
+ // InterfaceType returns t cast to a *InterfaceType, or nil if its tag does not match.
432
+ func (t *Type) InterfaceType() *InterfaceType {
433
+ if t.Kind() != Interface {
434
+ return nil
435
+ }
436
+ return (*InterfaceType)(unsafe.Pointer(t))
437
+ }
438
+
439
+ // Size returns the size of data with type t.
440
+ func (t *Type) Size() uintptr { return t.Size_ }
441
+
442
+ // Align returns the alignment of data with type t.
443
+ func (t *Type) Align() int { return int(t.Align_) }
444
+
445
+ func (t *Type) FieldAlign() int { return int(t.FieldAlign_) }
446
+
447
+ type InterfaceType struct {
448
+ Type
449
+ PkgPath Name // import path
450
+ Methods []Imethod // sorted by hash
451
+ }
452
+
453
+ func (t *Type) ExportedMethods() []Method {
454
+ ut := t.Uncommon()
455
+ if ut == nil {
456
+ return nil
457
+ }
458
+ return ut.ExportedMethods()
459
+ }
460
+
461
+ func (t *Type) NumMethod() int {
462
+ if t.Kind() == Interface {
463
+ tt := (*InterfaceType)(unsafe.Pointer(t))
464
+ return tt.NumMethod()
465
+ }
466
+ return len(t.ExportedMethods())
467
+ }
468
+
469
+ // NumMethod returns the number of interface methods in the type's method set.
470
+ func (t *InterfaceType) NumMethod() int { return len(t.Methods) }
471
+
472
+ func (t *Type) Key() *Type {
473
+ if t.Kind() == Map {
474
+ return (*MapType)(unsafe.Pointer(t)).Key
475
+ }
476
+ return nil
477
+ }
478
+
479
+ type SliceType struct {
480
+ Type
481
+ Elem *Type // slice element type
482
+ }
483
+
484
+ // FuncType represents a function type.
485
+ //
486
+ // A *Type for each in and out parameter is stored in an array that
487
+ // directly follows the funcType (and possibly its uncommonType). So
488
+ // a function type with one method, one input, and one output is:
489
+ //
490
+ // struct {
491
+ // funcType
492
+ // uncommonType
493
+ // [2]*rtype // [0] is in, [1] is out
494
+ // }
495
+ type FuncType struct {
496
+ Type
497
+ InCount uint16
498
+ OutCount uint16 // top bit is set if last input parameter is ...
499
+ }
500
+
501
+ func (t *FuncType) In(i int) *Type {
502
+ return t.InSlice()[i]
503
+ }
504
+
505
+ func (t *FuncType) NumIn() int {
506
+ return int(t.InCount)
507
+ }
508
+
509
+ func (t *FuncType) NumOut() int {
510
+ return int(t.OutCount & (1<<15 - 1))
511
+ }
512
+
513
+ func (t *FuncType) Out(i int) *Type {
514
+ return (t.OutSlice()[i])
515
+ }
516
+
517
+ func (t *FuncType) InSlice() []*Type {
518
+ uadd := unsafe.Sizeof(*t)
519
+ if t.TFlag&TFlagUncommon != 0 {
520
+ uadd += unsafe.Sizeof(UncommonType{})
521
+ }
522
+ if t.InCount == 0 {
523
+ return nil
524
+ }
525
+ return (*[1 << 16]*Type)(addChecked(unsafe.Pointer(t), uadd, "t.inCount > 0"))[:t.InCount:t.InCount]
526
+ }
527
+ func (t *FuncType) OutSlice() []*Type {
528
+ outCount := uint16(t.NumOut())
529
+ if outCount == 0 {
530
+ return nil
531
+ }
532
+ uadd := unsafe.Sizeof(*t)
533
+ if t.TFlag&TFlagUncommon != 0 {
534
+ uadd += unsafe.Sizeof(UncommonType{})
535
+ }
536
+ return (*[1 << 17]*Type)(addChecked(unsafe.Pointer(t), uadd, "outCount > 0"))[t.InCount : t.InCount+outCount : t.InCount+outCount]
537
+ }
538
+
539
+ func (t *FuncType) IsVariadic() bool {
540
+ return t.OutCount&(1<<15) != 0
541
+ }
542
+
543
+ type PtrType struct {
544
+ Type
545
+ Elem *Type // pointer element (pointed at) type
546
+ }
547
+
548
+ type StructField struct {
549
+ Name Name // name is always non-empty
550
+ Typ *Type // type of field
551
+ Offset uintptr // byte offset of field
552
+ }
553
+
554
+ func (f *StructField) Embedded() bool {
555
+ return f.Name.IsEmbedded()
556
+ }
557
+
558
+ type StructType struct {
559
+ Type
560
+ PkgPath Name
561
+ Fields []StructField
562
+ }
563
+
564
+ // Name is an encoded type Name with optional extra data.
565
+ //
566
+ // The first byte is a bit field containing:
567
+ //
568
+ // 1<<0 the name is exported
569
+ // 1<<1 tag data follows the name
570
+ // 1<<2 pkgPath nameOff follows the name and tag
571
+ // 1<<3 the name is of an embedded (a.k.a. anonymous) field
572
+ //
573
+ // Following that, there is a varint-encoded length of the name,
574
+ // followed by the name itself.
575
+ //
576
+ // If tag data is present, it also has a varint-encoded length
577
+ // followed by the tag itself.
578
+ //
579
+ // If the import path follows, then 4 bytes at the end of
580
+ // the data form a nameOff. The import path is only set for concrete
581
+ // methods that are defined in a different package than their type.
582
+ //
583
+ // If a name starts with "*", then the exported bit represents
584
+ // whether the pointed to type is exported.
585
+ //
586
+ // Note: this encoding must match here and in:
587
+ // cmd/compile/internal/reflectdata/reflect.go
588
+ // cmd/link/internal/ld/decodesym.go
589
+
590
+ type Name struct {
591
+ Bytes *byte
592
+ }
593
+
594
+ // DataChecked does pointer arithmetic on n's Bytes, and that arithmetic is asserted to
595
+ // be safe for the reason in whySafe (which can appear in a backtrace, etc.)
596
+ func (n Name) DataChecked(off int, whySafe string) *byte {
597
+ return (*byte)(addChecked(unsafe.Pointer(n.Bytes), uintptr(off), whySafe))
598
+ }
599
+
600
+ // Data does pointer arithmetic on n's Bytes, and that arithmetic is asserted to
601
+ // be safe because the runtime made the call (other packages use DataChecked)
602
+ func (n Name) Data(off int) *byte {
603
+ return (*byte)(addChecked(unsafe.Pointer(n.Bytes), uintptr(off), "the runtime doesn't need to give you a reason"))
604
+ }
605
+
606
+ // IsExported returns "is n exported?"
607
+ func (n Name) IsExported() bool {
608
+ return (*n.Bytes)&(1<<0) != 0
609
+ }
610
+
611
+ // HasTag returns true iff there is tag data following this name
612
+ func (n Name) HasTag() bool {
613
+ return (*n.Bytes)&(1<<1) != 0
614
+ }
615
+
616
+ // IsEmbedded returns true iff n is embedded (an anonymous field).
617
+ func (n Name) IsEmbedded() bool {
618
+ return (*n.Bytes)&(1<<3) != 0
619
+ }
620
+
621
+ // ReadVarint parses a varint as encoded by encoding/binary.
622
+ // It returns the number of encoded bytes and the encoded value.
623
+ func (n Name) ReadVarint(off int) (int, int) {
624
+ v := 0
625
+ for i := 0; ; i++ {
626
+ x := *n.DataChecked(off+i, "read varint")
627
+ v += int(x&0x7f) << (7 * i)
628
+ if x&0x80 == 0 {
629
+ return i + 1, v
630
+ }
631
+ }
632
+ }
633
+
634
+ // IsBlank indicates whether n is "_".
635
+ func (n Name) IsBlank() bool {
636
+ if n.Bytes == nil {
637
+ return false
638
+ }
639
+ _, l := n.ReadVarint(1)
640
+ return l == 1 && *n.Data(2) == '_'
641
+ }
642
+
643
+ // writeVarint writes n to buf in varint form. Returns the
644
+ // number of bytes written. n must be nonnegative.
645
+ // Writes at most 10 bytes.
646
+ func writeVarint(buf []byte, n int) int {
647
+ for i := 0; ; i++ {
648
+ b := byte(n & 0x7f)
649
+ n >>= 7
650
+ if n == 0 {
651
+ buf[i] = b
652
+ return i + 1
653
+ }
654
+ buf[i] = b | 0x80
655
+ }
656
+ }
657
+
658
+ // Name returns the name of n, or empty if it does not actually have a name.
659
+ func (n Name) Name() string {
660
+ if n.Bytes == nil {
661
+ return ""
662
+ }
663
+ i, l := n.ReadVarint(1)
664
+ return unsafe.String(n.DataChecked(1+i, "non-empty string"), l)
665
+ }
666
+
667
+ // Tag returns the tag string for n, or empty if there is none.
668
+ func (n Name) Tag() string {
669
+ if !n.HasTag() {
670
+ return ""
671
+ }
672
+ i, l := n.ReadVarint(1)
673
+ i2, l2 := n.ReadVarint(1 + i + l)
674
+ return unsafe.String(n.DataChecked(1+i+l+i2, "non-empty string"), l2)
675
+ }
676
+
677
+ func NewName(n, tag string, exported, embedded bool) Name {
678
+ if len(n) >= 1<<29 {
679
+ panic("abi.NewName: name too long: " + n[:1024] + "...")
680
+ }
681
+ if len(tag) >= 1<<29 {
682
+ panic("abi.NewName: tag too long: " + tag[:1024] + "...")
683
+ }
684
+ var nameLen [10]byte
685
+ var tagLen [10]byte
686
+ nameLenLen := writeVarint(nameLen[:], len(n))
687
+ tagLenLen := writeVarint(tagLen[:], len(tag))
688
+
689
+ var bits byte
690
+ l := 1 + nameLenLen + len(n)
691
+ if exported {
692
+ bits |= 1 << 0
693
+ }
694
+ if len(tag) > 0 {
695
+ l += tagLenLen + len(tag)
696
+ bits |= 1 << 1
697
+ }
698
+ if embedded {
699
+ bits |= 1 << 3
700
+ }
701
+
702
+ b := make([]byte, l)
703
+ b[0] = bits
704
+ copy(b[1:], nameLen[:nameLenLen])
705
+ copy(b[1+nameLenLen:], n)
706
+ if len(tag) > 0 {
707
+ tb := b[1+nameLenLen+len(n):]
708
+ copy(tb, tagLen[:tagLenLen])
709
+ copy(tb[tagLenLen:], tag)
710
+ }
711
+
712
+ return Name{Bytes: &b[0]}
713
+ }
714
+
715
+ const (
716
+ TraceArgsLimit = 10 // print no more than 10 args/components
717
+ TraceArgsMaxDepth = 5 // no more than 5 layers of nesting
718
+
719
+ // maxLen is a (conservative) upper bound of the byte stream length. For
720
+ // each arg/component, it has no more than 2 bytes of data (size, offset),
721
+ // and no more than one {, }, ... at each level (it cannot have both the
722
+ // data and ... unless it is the last one, just be conservative). Plus 1
723
+ // for _endSeq.
724
+ TraceArgsMaxLen = (TraceArgsMaxDepth*3+2)*TraceArgsLimit + 1
725
+ )
726
+
727
+ // Populate the data.
728
+ // The data is a stream of bytes, which contains the offsets and sizes of the
729
+ // non-aggregate arguments or non-aggregate fields/elements of aggregate-typed
730
+ // arguments, along with special "operators". Specifically,
731
+ // - for each non-aggregate arg/field/element, its offset from FP (1 byte) and
732
+ // size (1 byte)
733
+ // - special operators:
734
+ // - 0xff - end of sequence
735
+ // - 0xfe - print { (at the start of an aggregate-typed argument)
736
+ // - 0xfd - print } (at the end of an aggregate-typed argument)
737
+ // - 0xfc - print ... (more args/fields/elements)
738
+ // - 0xfb - print _ (offset too large)
739
+ const (
740
+ TraceArgsEndSeq = 0xff
741
+ TraceArgsStartAgg = 0xfe
742
+ TraceArgsEndAgg = 0xfd
743
+ TraceArgsDotdotdot = 0xfc
744
+ TraceArgsOffsetTooLarge = 0xfb
745
+ TraceArgsSpecial = 0xf0 // above this are operators, below this are ordinary offsets
746
+ )
747
+
748
+ // MaxPtrmaskBytes is the maximum length of a GC ptrmask bitmap,
749
+ // which holds 1-bit entries describing where pointers are in a given type.
750
+ // Above this length, the GC information is recorded as a GC program,
751
+ // which can express repetition compactly. In either form, the
752
+ // information is used by the runtime to initialize the heap bitmap,
753
+ // and for large types (like 128 or more words), they are roughly the
754
+ // same speed. GC programs are never much larger and often more
755
+ // compact. (If large arrays are involved, they can be arbitrarily
756
+ // more compact.)
757
+ //
758
+ // The cutoff must be large enough that any allocation large enough to
759
+ // use a GC program is large enough that it does not share heap bitmap
760
+ // bytes with any other objects, allowing the GC program execution to
761
+ // assume an aligned start and not use atomic operations. In the current
762
+ // runtime, this means all malloc size classes larger than the cutoff must
763
+ // be multiples of four words. On 32-bit systems that's 16 bytes, and
764
+ // all size classes >= 16 bytes are 16-byte aligned, so no real constraint.
765
+ // On 64-bit systems, that's 32 bytes, and 32-byte alignment is guaranteed
766
+ // for size classes >= 256 bytes. On a 64-bit system, 256 bytes allocated
767
+ // is 32 pointers, the bits for which fit in 4 bytes. So MaxPtrmaskBytes
768
+ // must be >= 4.
769
+ //
770
+ // We used to use 16 because the GC programs do have some constant overhead
771
+ // to get started, and processing 128 pointers seems to be enough to
772
+ // amortize that overhead well.
773
+ //
774
+ // To make sure that the runtime's chansend can call typeBitsBulkBarrier,
775
+ // we raised the limit to 2048, so that even 32-bit systems are guaranteed to
776
+ // use bitmaps for objects up to 64 kB in size.
777
+ const MaxPtrmaskBytes = 2048
go/src/internal/asan/asan.go ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2024 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
+ //go:build asan
6
+
7
+ package asan
8
+
9
+ import (
10
+ "unsafe"
11
+ )
12
+
13
+ const Enabled = true
14
+
15
+ //go:linkname Read runtime.asanread
16
+ func Read(addr unsafe.Pointer, len uintptr)
17
+
18
+ //go:linkname Write runtime.asanwrite
19
+ func Write(addr unsafe.Pointer, len uintptr)
go/src/internal/asan/doc.go ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2024 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 asan contains helper functions for manually instrumenting
6
+ // code for the address sanitizer.
7
+ // The runtime package intentionally exports these functions only in the
8
+ // asan build; this package exports them unconditionally but without the
9
+ // "asan" build tag they are no-ops.
10
+ package asan
go/src/internal/asan/noasan.go ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2024 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
+ //go:build !asan
6
+
7
+ package asan
8
+
9
+ import (
10
+ "unsafe"
11
+ )
12
+
13
+ const Enabled = false
14
+
15
+ func Read(addr unsafe.Pointer, len uintptr) {}
16
+
17
+ func Write(addr unsafe.Pointer, len uintptr) {}
go/src/internal/bisect/bisect.go ADDED
@@ -0,0 +1,778 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2023 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 bisect can be used by compilers and other programs
6
+ // to serve as a target for the bisect debugging tool.
7
+ // See [golang.org/x/tools/cmd/bisect] for details about using the tool.
8
+ //
9
+ // To be a bisect target, allowing bisect to help determine which of a set of independent
10
+ // changes provokes a failure, a program needs to:
11
+ //
12
+ // 1. Define a way to accept a change pattern on its command line or in its environment.
13
+ // The most common mechanism is a command-line flag.
14
+ // The pattern can be passed to [New] to create a [Matcher], the compiled form of a pattern.
15
+ //
16
+ // 2. Assign each change a unique ID. One possibility is to use a sequence number,
17
+ // but the most common mechanism is to hash some kind of identifying information
18
+ // like the file and line number where the change might be applied.
19
+ // [Hash] hashes its arguments to compute an ID.
20
+ //
21
+ // 3. Enable each change that the pattern says should be enabled.
22
+ // The [Matcher.ShouldEnable] method answers this question for a given change ID.
23
+ //
24
+ // 4. Print a report identifying each change that the pattern says should be printed.
25
+ // The [Matcher.ShouldPrint] method answers this question for a given change ID.
26
+ // The report consists of one more lines on standard error or standard output
27
+ // that contain a “match marker”. [Marker] returns the match marker for a given ID.
28
+ // When bisect reports a change as causing the failure, it identifies the change
29
+ // by printing the report lines with the match marker removed.
30
+ //
31
+ // # Example Usage
32
+ //
33
+ // A program starts by defining how it receives the pattern. In this example, we will assume a flag.
34
+ // The next step is to compile the pattern:
35
+ //
36
+ // m, err := bisect.New(patternFlag)
37
+ // if err != nil {
38
+ // log.Fatal(err)
39
+ // }
40
+ //
41
+ // Then, each time a potential change is considered, the program computes
42
+ // a change ID by hashing identifying information (source file and line, in this case)
43
+ // and then calls m.ShouldPrint and m.ShouldEnable to decide whether to
44
+ // print and enable the change, respectively. The two can return different values
45
+ // depending on whether bisect is trying to find a minimal set of changes to
46
+ // disable or to enable to provoke the failure.
47
+ //
48
+ // It is usually helpful to write a helper function that accepts the identifying information
49
+ // and then takes care of hashing, printing, and reporting whether the identified change
50
+ // should be enabled. For example, a helper for changes identified by a file and line number
51
+ // would be:
52
+ //
53
+ // func ShouldEnable(file string, line int) {
54
+ // h := bisect.Hash(file, line)
55
+ // if m.ShouldPrint(h) {
56
+ // fmt.Fprintf(os.Stderr, "%v %s:%d\n", bisect.Marker(h), file, line)
57
+ // }
58
+ // return m.ShouldEnable(h)
59
+ // }
60
+ //
61
+ // Finally, note that New returns a nil Matcher when there is no pattern,
62
+ // meaning that the target is not running under bisect at all,
63
+ // so all changes should be enabled and none should be printed.
64
+ // In that common case, the computation of the hash can be avoided entirely
65
+ // by checking for m == nil first:
66
+ //
67
+ // func ShouldEnable(file string, line int) bool {
68
+ // if m == nil {
69
+ // return true
70
+ // }
71
+ // h := bisect.Hash(file, line)
72
+ // if m.ShouldPrint(h) {
73
+ // fmt.Fprintf(os.Stderr, "%v %s:%d\n", bisect.Marker(h), file, line)
74
+ // }
75
+ // return m.ShouldEnable(h)
76
+ // }
77
+ //
78
+ // When the identifying information is expensive to format, this code can call
79
+ // [Matcher.MarkerOnly] to find out whether short report lines containing only the
80
+ // marker are permitted for a given run. (Bisect permits such lines when it is
81
+ // still exploring the space of possible changes and will not be showing the
82
+ // output to the user.) If so, the client can choose to print only the marker:
83
+ //
84
+ // func ShouldEnable(file string, line int) bool {
85
+ // if m == nil {
86
+ // return true
87
+ // }
88
+ // h := bisect.Hash(file, line)
89
+ // if m.ShouldPrint(h) {
90
+ // if m.MarkerOnly() {
91
+ // bisect.PrintMarker(os.Stderr, h)
92
+ // } else {
93
+ // fmt.Fprintf(os.Stderr, "%v %s:%d\n", bisect.Marker(h), file, line)
94
+ // }
95
+ // }
96
+ // return m.ShouldEnable(h)
97
+ // }
98
+ //
99
+ // This specific helper – deciding whether to enable a change identified by
100
+ // file and line number and printing about the change when necessary – is
101
+ // provided by the [Matcher.FileLine] method.
102
+ //
103
+ // Another common usage is deciding whether to make a change in a function
104
+ // based on the caller's stack, to identify the specific calling contexts that the
105
+ // change breaks. The [Matcher.Stack] method takes care of obtaining the stack,
106
+ // printing it when necessary, and reporting whether to enable the change
107
+ // based on that stack.
108
+ //
109
+ // # Pattern Syntax
110
+ //
111
+ // Patterns are generated by the bisect tool and interpreted by [New].
112
+ // Users should not have to understand the patterns except when
113
+ // debugging a target's bisect support or debugging the bisect tool itself.
114
+ //
115
+ // The pattern syntax selecting a change is a sequence of bit strings
116
+ // separated by + and - operators. Each bit string denotes the set of
117
+ // changes with IDs ending in those bits, + is set addition, - is set subtraction,
118
+ // and the expression is evaluated in the usual left-to-right order.
119
+ // The special binary number “y” denotes the set of all changes,
120
+ // standing in for the empty bit string.
121
+ // In the expression, all the + operators must appear before all the - operators.
122
+ // A leading + adds to an empty set. A leading - subtracts from the set of all
123
+ // possible suffixes.
124
+ //
125
+ // For example:
126
+ //
127
+ // - “01+10” and “+01+10” both denote the set of changes
128
+ // with IDs ending with the bits 01 or 10.
129
+ //
130
+ // - “01+10-1001” denotes the set of changes with IDs
131
+ // ending with the bits 01 or 10, but excluding those ending in 1001.
132
+ //
133
+ // - “-01-1000” and “y-01-1000 both denote the set of all changes
134
+ // with IDs not ending in 01 nor 1000.
135
+ //
136
+ // - “0+1-01+001” is not a valid pattern, because all the + operators do not
137
+ // appear before all the - operators.
138
+ //
139
+ // In the syntaxes described so far, the pattern specifies the changes to
140
+ // enable and report. If a pattern is prefixed by a “!”, the meaning
141
+ // changes: the pattern specifies the changes to DISABLE and report. This
142
+ // mode of operation is needed when a program passes with all changes
143
+ // enabled but fails with no changes enabled. In this case, bisect
144
+ // searches for minimal sets of changes to disable.
145
+ // Put another way, the leading “!” inverts the result from [Matcher.ShouldEnable]
146
+ // but does not invert the result from [Matcher.ShouldPrint].
147
+ //
148
+ // As a convenience for manual debugging, “n” is an alias for “!y”,
149
+ // meaning to disable and report all changes.
150
+ //
151
+ // Finally, a leading “v” in the pattern indicates that the reports will be shown
152
+ // to the user of bisect to describe the changes involved in a failure.
153
+ // At the API level, the leading “v” causes [Matcher.Visible] to return true.
154
+ // See the next section for details.
155
+ //
156
+ // # Match Reports
157
+ //
158
+ // The target program must enable only those changed matched
159
+ // by the pattern, and it must print a match report for each such change.
160
+ // A match report consists of one or more lines of text that will be
161
+ // printed by the bisect tool to describe a change implicated in causing
162
+ // a failure. Each line in the report for a given change must contain a
163
+ // match marker with that change ID, as returned by [Marker].
164
+ // The markers are elided when displaying the lines to the user.
165
+ //
166
+ // A match marker has the form “[bisect-match 0x1234]” where
167
+ // 0x1234 is the change ID in hexadecimal.
168
+ // An alternate form is “[bisect-match 010101]”, giving the change ID in binary.
169
+ //
170
+ // When [Matcher.Visible] returns false, the match reports are only
171
+ // being processed by bisect to learn the set of enabled changes,
172
+ // not shown to the user, meaning that each report can be a match
173
+ // marker on a line by itself, eliding the usual textual description.
174
+ // When the textual description is expensive to compute,
175
+ // checking [Matcher.Visible] can help the avoid that expense
176
+ // in most runs.
177
+ package bisect
178
+
179
+ import (
180
+ "runtime"
181
+ "sync"
182
+ "sync/atomic"
183
+ )
184
+
185
+ // New creates and returns a new Matcher implementing the given pattern.
186
+ // The pattern syntax is defined in the package doc comment.
187
+ //
188
+ // In addition to the pattern syntax syntax, New("") returns nil, nil.
189
+ // The nil *Matcher is valid for use: it returns true from ShouldEnable
190
+ // and false from ShouldPrint for all changes. Callers can avoid calling
191
+ // [Hash], [Matcher.ShouldEnable], and [Matcher.ShouldPrint] entirely
192
+ // when they recognize the nil Matcher.
193
+ func New(pattern string) (*Matcher, error) {
194
+ if pattern == "" {
195
+ return nil, nil
196
+ }
197
+
198
+ m := new(Matcher)
199
+
200
+ p := pattern
201
+ // Special case for leading 'q' so that 'qn' quietly disables, e.g. fmahash=qn to disable fma
202
+ // Any instance of 'v' disables 'q'.
203
+ if len(p) > 0 && p[0] == 'q' {
204
+ m.quiet = true
205
+ p = p[1:]
206
+ if p == "" {
207
+ return nil, &parseError{"invalid pattern syntax: " + pattern}
208
+ }
209
+ }
210
+ // Allow multiple v, so that “bisect cmd vPATTERN” can force verbose all the time.
211
+ for len(p) > 0 && p[0] == 'v' {
212
+ m.verbose = true
213
+ m.quiet = false
214
+ p = p[1:]
215
+ if p == "" {
216
+ return nil, &parseError{"invalid pattern syntax: " + pattern}
217
+ }
218
+ }
219
+
220
+ // Allow multiple !, each negating the last, so that “bisect cmd !PATTERN” works
221
+ // even when bisect chooses to add its own !.
222
+ m.enable = true
223
+ for len(p) > 0 && p[0] == '!' {
224
+ m.enable = !m.enable
225
+ p = p[1:]
226
+ if p == "" {
227
+ return nil, &parseError{"invalid pattern syntax: " + pattern}
228
+ }
229
+ }
230
+
231
+ if p == "n" {
232
+ // n is an alias for !y.
233
+ m.enable = !m.enable
234
+ p = "y"
235
+ }
236
+
237
+ // Parse actual pattern syntax.
238
+ result := true
239
+ bits := uint64(0)
240
+ start := 0
241
+ wid := 1 // 1-bit (binary); sometimes 4-bit (hex)
242
+ for i := 0; i <= len(p); i++ {
243
+ // Imagine a trailing - at the end of the pattern to flush final suffix
244
+ c := byte('-')
245
+ if i < len(p) {
246
+ c = p[i]
247
+ }
248
+ if i == start && wid == 1 && c == 'x' { // leading x for hex
249
+ start = i + 1
250
+ wid = 4
251
+ continue
252
+ }
253
+ switch c {
254
+ default:
255
+ return nil, &parseError{"invalid pattern syntax: " + pattern}
256
+ case '2', '3', '4', '5', '6', '7', '8', '9':
257
+ if wid != 4 {
258
+ return nil, &parseError{"invalid pattern syntax: " + pattern}
259
+ }
260
+ fallthrough
261
+ case '0', '1':
262
+ bits <<= wid
263
+ bits |= uint64(c - '0')
264
+ case 'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', 'F':
265
+ if wid != 4 {
266
+ return nil, &parseError{"invalid pattern syntax: " + pattern}
267
+ }
268
+ bits <<= 4
269
+ bits |= uint64(c&^0x20 - 'A' + 10)
270
+ case 'y':
271
+ if i+1 < len(p) && (p[i+1] == '0' || p[i+1] == '1') {
272
+ return nil, &parseError{"invalid pattern syntax: " + pattern}
273
+ }
274
+ bits = 0
275
+ case '+', '-':
276
+ if c == '+' && result == false {
277
+ // Have already seen a -. Should be - from here on.
278
+ return nil, &parseError{"invalid pattern syntax (+ after -): " + pattern}
279
+ }
280
+ if i > 0 {
281
+ n := (i - start) * wid
282
+ if n > 64 {
283
+ return nil, &parseError{"pattern bits too long: " + pattern}
284
+ }
285
+ if n <= 0 {
286
+ return nil, &parseError{"invalid pattern syntax: " + pattern}
287
+ }
288
+ if p[start] == 'y' {
289
+ n = 0
290
+ }
291
+ mask := uint64(1)<<n - 1
292
+ m.list = append(m.list, cond{mask, bits, result})
293
+ } else if c == '-' {
294
+ // leading - subtracts from complete set
295
+ m.list = append(m.list, cond{0, 0, true})
296
+ }
297
+ bits = 0
298
+ result = c == '+'
299
+ start = i + 1
300
+ wid = 1
301
+ }
302
+ }
303
+ return m, nil
304
+ }
305
+
306
+ // A Matcher is the parsed, compiled form of a PATTERN string.
307
+ // The nil *Matcher is valid: it has all changes enabled but none reported.
308
+ type Matcher struct {
309
+ verbose bool // annotate reporting with human-helpful information
310
+ quiet bool // disables all reporting. reset if verbose is true. use case is -d=fmahash=qn
311
+ enable bool // when true, list is for “enable and report” (when false, “disable and report”)
312
+ list []cond // conditions; later ones win over earlier ones
313
+ dedup atomic.Pointer[dedup]
314
+ }
315
+
316
+ // A cond is a single condition in the matcher.
317
+ // Given an input id, if id&mask == bits, return the result.
318
+ type cond struct {
319
+ mask uint64
320
+ bits uint64
321
+ result bool
322
+ }
323
+
324
+ // MarkerOnly reports whether it is okay to print only the marker for
325
+ // a given change, omitting the identifying information.
326
+ // MarkerOnly returns true when bisect is using the printed reports
327
+ // only for an intermediate search step, not for showing to users.
328
+ func (m *Matcher) MarkerOnly() bool {
329
+ return !m.verbose
330
+ }
331
+
332
+ // ShouldEnable reports whether the change with the given id should be enabled.
333
+ func (m *Matcher) ShouldEnable(id uint64) bool {
334
+ if m == nil {
335
+ return true
336
+ }
337
+ return m.matchResult(id) == m.enable
338
+ }
339
+
340
+ // ShouldPrint reports whether to print identifying information about the change with the given id.
341
+ func (m *Matcher) ShouldPrint(id uint64) bool {
342
+ if m == nil || m.quiet {
343
+ return false
344
+ }
345
+ return m.matchResult(id)
346
+ }
347
+
348
+ // matchResult returns the result from the first condition that matches id.
349
+ func (m *Matcher) matchResult(id uint64) bool {
350
+ for i := len(m.list) - 1; i >= 0; i-- {
351
+ c := &m.list[i]
352
+ if id&c.mask == c.bits {
353
+ return c.result
354
+ }
355
+ }
356
+ return false
357
+ }
358
+
359
+ // FileLine reports whether the change identified by file and line should be enabled.
360
+ // If the change should be printed, FileLine prints a one-line report to w.
361
+ func (m *Matcher) FileLine(w Writer, file string, line int) bool {
362
+ if m == nil {
363
+ return true
364
+ }
365
+ return m.fileLine(w, file, line)
366
+ }
367
+
368
+ // fileLine does the real work for FileLine.
369
+ // This lets FileLine's body handle m == nil and potentially be inlined.
370
+ func (m *Matcher) fileLine(w Writer, file string, line int) bool {
371
+ h := Hash(file, line)
372
+ if m.ShouldPrint(h) {
373
+ if m.MarkerOnly() {
374
+ PrintMarker(w, h)
375
+ } else {
376
+ printFileLine(w, h, file, line)
377
+ }
378
+ }
379
+ return m.ShouldEnable(h)
380
+ }
381
+
382
+ // printFileLine prints a non-marker-only report for file:line to w.
383
+ func printFileLine(w Writer, h uint64, file string, line int) error {
384
+ const markerLen = 40 // overestimate
385
+ b := make([]byte, 0, markerLen+len(file)+24)
386
+ b = AppendMarker(b, h)
387
+ b = appendFileLine(b, file, line)
388
+ b = append(b, '\n')
389
+ _, err := w.Write(b)
390
+ return err
391
+ }
392
+
393
+ // appendFileLine appends file:line to dst, returning the extended slice.
394
+ func appendFileLine(dst []byte, file string, line int) []byte {
395
+ dst = append(dst, file...)
396
+ dst = append(dst, ':')
397
+ u := uint(line)
398
+ if line < 0 {
399
+ dst = append(dst, '-')
400
+ u = -u
401
+ }
402
+ var buf [24]byte
403
+ i := len(buf)
404
+ for i == len(buf) || u > 0 {
405
+ i--
406
+ buf[i] = '0' + byte(u%10)
407
+ u /= 10
408
+ }
409
+ dst = append(dst, buf[i:]...)
410
+ return dst
411
+ }
412
+
413
+ // MatchStack assigns the current call stack a change ID.
414
+ // If the stack should be printed, MatchStack prints it.
415
+ // Then MatchStack reports whether a change at the current call stack should be enabled.
416
+ func (m *Matcher) Stack(w Writer) bool {
417
+ if m == nil {
418
+ return true
419
+ }
420
+ return m.stack(w)
421
+ }
422
+
423
+ // stack does the real work for Stack.
424
+ // This lets stack's body handle m == nil and potentially be inlined.
425
+ func (m *Matcher) stack(w Writer) bool {
426
+ const maxStack = 16
427
+ var stk [maxStack]uintptr
428
+ n := runtime.Callers(2, stk[:])
429
+ // caller #2 is not for printing; need it to normalize PCs if ASLR.
430
+ if n <= 1 {
431
+ return false
432
+ }
433
+
434
+ base := stk[0]
435
+ // normalize PCs
436
+ for i := range stk[:n] {
437
+ stk[i] -= base
438
+ }
439
+
440
+ h := Hash(stk[:n])
441
+ if m.ShouldPrint(h) {
442
+ var d *dedup
443
+ for {
444
+ d = m.dedup.Load()
445
+ if d != nil {
446
+ break
447
+ }
448
+ d = new(dedup)
449
+ if m.dedup.CompareAndSwap(nil, d) {
450
+ break
451
+ }
452
+ }
453
+
454
+ if m.MarkerOnly() {
455
+ if !d.seenLossy(h) {
456
+ PrintMarker(w, h)
457
+ }
458
+ } else {
459
+ if !d.seen(h) {
460
+ // Restore PCs in stack for printing
461
+ for i := range stk[:n] {
462
+ stk[i] += base
463
+ }
464
+ printStack(w, h, stk[1:n])
465
+ }
466
+ }
467
+ }
468
+ return m.ShouldEnable(h)
469
+ }
470
+
471
+ // Writer is the same interface as io.Writer.
472
+ // It is duplicated here to avoid importing io.
473
+ type Writer interface {
474
+ Write([]byte) (int, error)
475
+ }
476
+
477
+ // PrintMarker prints to w a one-line report containing only the marker for h.
478
+ // It is appropriate to use when [Matcher.ShouldPrint] and [Matcher.MarkerOnly] both return true.
479
+ func PrintMarker(w Writer, h uint64) error {
480
+ var buf [50]byte
481
+ b := AppendMarker(buf[:0], h)
482
+ b = append(b, '\n')
483
+ _, err := w.Write(b)
484
+ return err
485
+ }
486
+
487
+ // printStack prints to w a multi-line report containing a formatting of the call stack stk,
488
+ // with each line preceded by the marker for h.
489
+ func printStack(w Writer, h uint64, stk []uintptr) error {
490
+ buf := make([]byte, 0, 2048)
491
+
492
+ var prefixBuf [100]byte
493
+ prefix := AppendMarker(prefixBuf[:0], h)
494
+
495
+ frames := runtime.CallersFrames(stk)
496
+ for {
497
+ f, more := frames.Next()
498
+ buf = append(buf, prefix...)
499
+ buf = append(buf, f.Function...)
500
+ buf = append(buf, "()\n"...)
501
+ buf = append(buf, prefix...)
502
+ buf = append(buf, '\t')
503
+ buf = appendFileLine(buf, f.File, f.Line)
504
+ buf = append(buf, '\n')
505
+ if !more {
506
+ break
507
+ }
508
+ }
509
+ buf = append(buf, prefix...)
510
+ buf = append(buf, '\n')
511
+ _, err := w.Write(buf)
512
+ return err
513
+ }
514
+
515
+ // Marker returns the match marker text to use on any line reporting details
516
+ // about a match of the given ID.
517
+ // It always returns the hexadecimal format.
518
+ func Marker(id uint64) string {
519
+ return string(AppendMarker(nil, id))
520
+ }
521
+
522
+ // AppendMarker is like [Marker] but appends the marker to dst.
523
+ func AppendMarker(dst []byte, id uint64) []byte {
524
+ const prefix = "[bisect-match 0x"
525
+ var buf [len(prefix) + 16 + 1]byte
526
+ copy(buf[:], prefix)
527
+ for i := 0; i < 16; i++ {
528
+ buf[len(prefix)+i] = "0123456789abcdef"[id>>60]
529
+ id <<= 4
530
+ }
531
+ buf[len(prefix)+16] = ']'
532
+ return append(dst, buf[:]...)
533
+ }
534
+
535
+ // CutMarker finds the first match marker in line and removes it,
536
+ // returning the shortened line (with the marker removed),
537
+ // the ID from the match marker,
538
+ // and whether a marker was found at all.
539
+ // If there is no marker, CutMarker returns line, 0, false.
540
+ func CutMarker(line string) (short string, id uint64, ok bool) {
541
+ // Find first instance of prefix.
542
+ prefix := "[bisect-match "
543
+ i := 0
544
+ for ; ; i++ {
545
+ if i >= len(line)-len(prefix) {
546
+ return line, 0, false
547
+ }
548
+ if line[i] == '[' && line[i:i+len(prefix)] == prefix {
549
+ break
550
+ }
551
+ }
552
+
553
+ // Scan to ].
554
+ j := i + len(prefix)
555
+ for j < len(line) && line[j] != ']' {
556
+ j++
557
+ }
558
+ if j >= len(line) {
559
+ return line, 0, false
560
+ }
561
+
562
+ // Parse id.
563
+ idstr := line[i+len(prefix) : j]
564
+ if len(idstr) >= 3 && idstr[:2] == "0x" {
565
+ // parse hex
566
+ if len(idstr) > 2+16 { // max 0x + 16 digits
567
+ return line, 0, false
568
+ }
569
+ for i := 2; i < len(idstr); i++ {
570
+ id <<= 4
571
+ switch c := idstr[i]; {
572
+ case '0' <= c && c <= '9':
573
+ id |= uint64(c - '0')
574
+ case 'a' <= c && c <= 'f':
575
+ id |= uint64(c - 'a' + 10)
576
+ case 'A' <= c && c <= 'F':
577
+ id |= uint64(c - 'A' + 10)
578
+ }
579
+ }
580
+ } else {
581
+ if idstr == "" || len(idstr) > 64 { // min 1 digit, max 64 digits
582
+ return line, 0, false
583
+ }
584
+ // parse binary
585
+ for i := 0; i < len(idstr); i++ {
586
+ id <<= 1
587
+ switch c := idstr[i]; c {
588
+ default:
589
+ return line, 0, false
590
+ case '0', '1':
591
+ id |= uint64(c - '0')
592
+ }
593
+ }
594
+ }
595
+
596
+ // Construct shortened line.
597
+ // Remove at most one space from around the marker,
598
+ // so that "foo [marker] bar" shortens to "foo bar".
599
+ j++ // skip ]
600
+ if i > 0 && line[i-1] == ' ' {
601
+ i--
602
+ } else if j < len(line) && line[j] == ' ' {
603
+ j++
604
+ }
605
+ short = line[:i] + line[j:]
606
+ return short, id, true
607
+ }
608
+
609
+ // Hash computes a hash of the data arguments,
610
+ // each of which must be of type string, byte, int, uint, int32, uint32, int64, uint64, uintptr, or a slice of one of those types.
611
+ func Hash(data ...any) uint64 {
612
+ h := offset64
613
+ for _, v := range data {
614
+ switch v := v.(type) {
615
+ default:
616
+ // Note: Not printing the type, because reflect.ValueOf(v)
617
+ // would make the interfaces prepared by the caller escape
618
+ // and therefore allocate. This way, Hash(file, line) runs
619
+ // without any allocation. It should be clear from the
620
+ // source code calling Hash what the bad argument was.
621
+ panic("bisect.Hash: unexpected argument type")
622
+ case string:
623
+ h = fnvString(h, v)
624
+ case byte:
625
+ h = fnv(h, v)
626
+ case int:
627
+ h = fnvUint64(h, uint64(v))
628
+ case uint:
629
+ h = fnvUint64(h, uint64(v))
630
+ case int32:
631
+ h = fnvUint32(h, uint32(v))
632
+ case uint32:
633
+ h = fnvUint32(h, v)
634
+ case int64:
635
+ h = fnvUint64(h, uint64(v))
636
+ case uint64:
637
+ h = fnvUint64(h, v)
638
+ case uintptr:
639
+ h = fnvUint64(h, uint64(v))
640
+ case []string:
641
+ for _, x := range v {
642
+ h = fnvString(h, x)
643
+ }
644
+ case []byte:
645
+ for _, x := range v {
646
+ h = fnv(h, x)
647
+ }
648
+ case []int:
649
+ for _, x := range v {
650
+ h = fnvUint64(h, uint64(x))
651
+ }
652
+ case []uint:
653
+ for _, x := range v {
654
+ h = fnvUint64(h, uint64(x))
655
+ }
656
+ case []int32:
657
+ for _, x := range v {
658
+ h = fnvUint32(h, uint32(x))
659
+ }
660
+ case []uint32:
661
+ for _, x := range v {
662
+ h = fnvUint32(h, x)
663
+ }
664
+ case []int64:
665
+ for _, x := range v {
666
+ h = fnvUint64(h, uint64(x))
667
+ }
668
+ case []uint64:
669
+ for _, x := range v {
670
+ h = fnvUint64(h, x)
671
+ }
672
+ case []uintptr:
673
+ for _, x := range v {
674
+ h = fnvUint64(h, uint64(x))
675
+ }
676
+ }
677
+ }
678
+ return h
679
+ }
680
+
681
+ // Trivial error implementation, here to avoid importing errors.
682
+
683
+ // parseError is a trivial error implementation,
684
+ // defined here to avoid importing errors.
685
+ type parseError struct{ text string }
686
+
687
+ func (e *parseError) Error() string { return e.text }
688
+
689
+ // FNV-1a implementation. See Go's hash/fnv/fnv.go.
690
+ // Copied here for simplicity (can handle integers more directly)
691
+ // and to avoid importing hash/fnv.
692
+
693
+ const (
694
+ offset64 uint64 = 14695981039346656037
695
+ prime64 uint64 = 1099511628211
696
+ )
697
+
698
+ func fnv(h uint64, x byte) uint64 {
699
+ h ^= uint64(x)
700
+ h *= prime64
701
+ return h
702
+ }
703
+
704
+ func fnvString(h uint64, x string) uint64 {
705
+ for i := 0; i < len(x); i++ {
706
+ h ^= uint64(x[i])
707
+ h *= prime64
708
+ }
709
+ return h
710
+ }
711
+
712
+ func fnvUint64(h uint64, x uint64) uint64 {
713
+ for i := 0; i < 8; i++ {
714
+ h ^= x & 0xFF
715
+ x >>= 8
716
+ h *= prime64
717
+ }
718
+ return h
719
+ }
720
+
721
+ func fnvUint32(h uint64, x uint32) uint64 {
722
+ for i := 0; i < 4; i++ {
723
+ h ^= uint64(x & 0xFF)
724
+ x >>= 8
725
+ h *= prime64
726
+ }
727
+ return h
728
+ }
729
+
730
+ // A dedup is a deduplicator for call stacks, so that we only print
731
+ // a report for new call stacks, not for call stacks we've already
732
+ // reported.
733
+ //
734
+ // It has two modes: an approximate but lock-free mode that
735
+ // may still emit some duplicates, and a precise mode that uses
736
+ // a lock and never emits duplicates.
737
+ type dedup struct {
738
+ // 128-entry 4-way, lossy cache for seenLossy
739
+ recent [128][4]uint64
740
+
741
+ // complete history for seen
742
+ mu sync.Mutex
743
+ m map[uint64]bool
744
+ }
745
+
746
+ // seen records that h has now been seen and reports whether it was seen before.
747
+ // When seen returns false, the caller is expected to print a report for h.
748
+ func (d *dedup) seen(h uint64) bool {
749
+ d.mu.Lock()
750
+ if d.m == nil {
751
+ d.m = make(map[uint64]bool)
752
+ }
753
+ seen := d.m[h]
754
+ d.m[h] = true
755
+ d.mu.Unlock()
756
+ return seen
757
+ }
758
+
759
+ // seenLossy is a variant of seen that avoids a lock by using a cache of recently seen hashes.
760
+ // Each cache entry is N-way set-associative: h can appear in any of the slots.
761
+ // If h does not appear in any of them, then it is inserted into a random slot,
762
+ // overwriting whatever was there before.
763
+ func (d *dedup) seenLossy(h uint64) bool {
764
+ cache := &d.recent[uint(h)%uint(len(d.recent))]
765
+ for i := 0; i < len(cache); i++ {
766
+ if atomic.LoadUint64(&cache[i]) == h {
767
+ return true
768
+ }
769
+ }
770
+
771
+ // Compute index in set to evict as hash of current set.
772
+ ch := offset64
773
+ for _, x := range cache {
774
+ ch = fnvUint64(ch, x)
775
+ }
776
+ atomic.StoreUint64(&cache[uint(ch)%uint(len(cache))], h)
777
+ return false
778
+ }
go/src/internal/buildcfg/cfg.go ADDED
@@ -0,0 +1,467 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 buildcfg provides access to the build configuration
6
+ // described by the current environment. It is for use by build tools
7
+ // such as cmd/go or cmd/compile and for setting up go/build's Default context.
8
+ //
9
+ // Note that it does NOT provide access to the build configuration used to
10
+ // build the currently-running binary. For that, use runtime.GOOS etc
11
+ // as well as internal/goexperiment.
12
+ package buildcfg
13
+
14
+ import (
15
+ "fmt"
16
+ "os"
17
+ "path/filepath"
18
+ "strconv"
19
+ "strings"
20
+ )
21
+
22
+ var (
23
+ GOROOT = os.Getenv("GOROOT") // cached for efficiency
24
+ GOARCH = envOr("GOARCH", defaultGOARCH)
25
+ GOOS = envOr("GOOS", defaultGOOS)
26
+ GO386 = envOr("GO386", DefaultGO386)
27
+ GOAMD64 = goamd64()
28
+ GOARM = goarm()
29
+ GOARM64 = goarm64()
30
+ GOMIPS = gomips()
31
+ GOMIPS64 = gomips64()
32
+ GOPPC64 = goppc64()
33
+ GORISCV64 = goriscv64()
34
+ GOWASM = gowasm()
35
+ ToolTags = toolTags()
36
+ GO_LDSO = defaultGO_LDSO
37
+ GOFIPS140 = gofips140()
38
+ Version = version
39
+ )
40
+
41
+ // Error is one of the errors found (if any) in the build configuration.
42
+ var Error error
43
+
44
+ // Check exits the program with a fatal error if Error is non-nil.
45
+ func Check() {
46
+ if Error != nil {
47
+ fmt.Fprintf(os.Stderr, "%s: %v\n", filepath.Base(os.Args[0]), Error)
48
+ os.Exit(2)
49
+ }
50
+ }
51
+
52
+ func envOr(key, value string) string {
53
+ if x := os.Getenv(key); x != "" {
54
+ return x
55
+ }
56
+ return value
57
+ }
58
+
59
+ func goamd64() int {
60
+ switch v := envOr("GOAMD64", DefaultGOAMD64); v {
61
+ case "v1":
62
+ return 1
63
+ case "v2":
64
+ return 2
65
+ case "v3":
66
+ return 3
67
+ case "v4":
68
+ return 4
69
+ }
70
+ Error = fmt.Errorf("invalid GOAMD64: must be v1, v2, v3, v4")
71
+ return int(DefaultGOAMD64[len("v")] - '0')
72
+ }
73
+
74
+ func gofips140() string {
75
+ v := envOr("GOFIPS140", DefaultGOFIPS140)
76
+ switch v {
77
+ case "off", "latest", "inprocess", "certified":
78
+ return v
79
+ }
80
+ if isFIPSVersion(v) {
81
+ return v
82
+ }
83
+ Error = fmt.Errorf("invalid GOFIPS140: must be off, latest, inprocess, certified, or v1.Y.Z")
84
+ return DefaultGOFIPS140
85
+ }
86
+
87
+ // isFIPSVersion reports whether v is a valid FIPS version,
88
+ // of the form v1.Y.Z or v1.Y.Z-hhhhhhhh or v1.Y.Z-rcN.
89
+ func isFIPSVersion(v string) bool {
90
+ v, ok := strings.CutPrefix(v, "v1.")
91
+ if !ok {
92
+ return false
93
+ }
94
+ if v, ok = cutNum(v); !ok {
95
+ return false
96
+ }
97
+ if v, ok = strings.CutPrefix(v, "."); !ok {
98
+ return false
99
+ }
100
+ if v, ok = cutNum(v); !ok {
101
+ return false
102
+ }
103
+ if v == "" {
104
+ return true
105
+ }
106
+ if v, ok = strings.CutPrefix(v, "-rc"); ok {
107
+ v, ok = cutNum(v)
108
+ return ok && v == ""
109
+ }
110
+ if v, ok = strings.CutPrefix(v, "-"); ok {
111
+ return len(v) == 8
112
+ }
113
+ return false
114
+ }
115
+
116
+ // cutNum skips the leading text matching [0-9]+
117
+ // in s, returning the rest and whether such text was found.
118
+ func cutNum(s string) (rest string, ok bool) {
119
+ i := 0
120
+ for i < len(s) && '0' <= s[i] && s[i] <= '9' {
121
+ i++
122
+ }
123
+ return s[i:], i > 0
124
+ }
125
+
126
+ type GoarmFeatures struct {
127
+ Version int
128
+ SoftFloat bool
129
+ }
130
+
131
+ func (g GoarmFeatures) String() string {
132
+ armStr := strconv.Itoa(g.Version)
133
+ if g.SoftFloat {
134
+ armStr += ",softfloat"
135
+ } else {
136
+ armStr += ",hardfloat"
137
+ }
138
+ return armStr
139
+ }
140
+
141
+ func goarm() (g GoarmFeatures) {
142
+ const (
143
+ softFloatOpt = ",softfloat"
144
+ hardFloatOpt = ",hardfloat"
145
+ )
146
+ def := DefaultGOARM
147
+ if GOOS == "android" && GOARCH == "arm" {
148
+ // Android arm devices always support GOARM=7.
149
+ def = "7"
150
+ }
151
+ v := envOr("GOARM", def)
152
+
153
+ floatSpecified := false
154
+ if strings.HasSuffix(v, softFloatOpt) {
155
+ g.SoftFloat = true
156
+ floatSpecified = true
157
+ v = v[:len(v)-len(softFloatOpt)]
158
+ }
159
+ if strings.HasSuffix(v, hardFloatOpt) {
160
+ floatSpecified = true
161
+ v = v[:len(v)-len(hardFloatOpt)]
162
+ }
163
+
164
+ switch v {
165
+ case "5":
166
+ g.Version = 5
167
+ case "6":
168
+ g.Version = 6
169
+ case "7":
170
+ g.Version = 7
171
+ default:
172
+ Error = fmt.Errorf("invalid GOARM: must start with 5, 6, or 7, and may optionally end in either %q or %q", hardFloatOpt, softFloatOpt)
173
+ g.Version = int(def[0] - '0')
174
+ }
175
+
176
+ // 5 defaults to softfloat. 6 and 7 default to hardfloat.
177
+ if !floatSpecified && g.Version == 5 {
178
+ g.SoftFloat = true
179
+ }
180
+ return
181
+ }
182
+
183
+ type Goarm64Features struct {
184
+ Version string
185
+ // Large Systems Extension
186
+ LSE bool
187
+ // ARM v8.0 Cryptographic Extension. It includes the following features:
188
+ // * FEAT_AES, which includes the AESD and AESE instructions.
189
+ // * FEAT_PMULL, which includes the PMULL, PMULL2 instructions.
190
+ // * FEAT_SHA1, which includes the SHA1* instructions.
191
+ // * FEAT_SHA256, which includes the SHA256* instructions.
192
+ Crypto bool
193
+ }
194
+
195
+ func (g Goarm64Features) String() string {
196
+ arm64Str := g.Version
197
+ if g.LSE {
198
+ arm64Str += ",lse"
199
+ }
200
+ if g.Crypto {
201
+ arm64Str += ",crypto"
202
+ }
203
+ return arm64Str
204
+ }
205
+
206
+ func ParseGoarm64(v string) (g Goarm64Features, e error) {
207
+ const (
208
+ lseOpt = ",lse"
209
+ cryptoOpt = ",crypto"
210
+ )
211
+
212
+ g.LSE = false
213
+ g.Crypto = false
214
+ // We allow any combination of suffixes, in any order
215
+ for {
216
+ if strings.HasSuffix(v, lseOpt) {
217
+ g.LSE = true
218
+ v = v[:len(v)-len(lseOpt)]
219
+ continue
220
+ }
221
+
222
+ if strings.HasSuffix(v, cryptoOpt) {
223
+ g.Crypto = true
224
+ v = v[:len(v)-len(cryptoOpt)]
225
+ continue
226
+ }
227
+
228
+ break
229
+ }
230
+
231
+ switch v {
232
+ case "v8.0":
233
+ g.Version = v
234
+ case "v8.1", "v8.2", "v8.3", "v8.4", "v8.5", "v8.6", "v8.7", "v8.8", "v8.9",
235
+ "v9.0", "v9.1", "v9.2", "v9.3", "v9.4", "v9.5":
236
+ g.Version = v
237
+ // LSE extension is mandatory starting from 8.1
238
+ g.LSE = true
239
+ default:
240
+ e = fmt.Errorf("invalid GOARM64: must start with v8.{0-9} or v9.{0-5} and may optionally end in %q and/or %q",
241
+ lseOpt, cryptoOpt)
242
+ g.Version = DefaultGOARM64
243
+ }
244
+
245
+ return
246
+ }
247
+
248
+ func goarm64() (g Goarm64Features) {
249
+ g, Error = ParseGoarm64(envOr("GOARM64", DefaultGOARM64))
250
+ return
251
+ }
252
+
253
+ // Returns true if g supports giving ARM64 ISA
254
+ // Note that this function doesn't accept / test suffixes (like ",lse" or ",crypto")
255
+ func (g Goarm64Features) Supports(s string) bool {
256
+ // We only accept "v{8-9}.{0-9}. Everything else is malformed.
257
+ if len(s) != 4 {
258
+ return false
259
+ }
260
+
261
+ major := s[1]
262
+ minor := s[3]
263
+
264
+ // We only accept "v{8-9}.{0-9}. Everything else is malformed.
265
+ if major < '8' || major > '9' ||
266
+ minor < '0' || minor > '9' ||
267
+ s[0] != 'v' || s[2] != '.' {
268
+ return false
269
+ }
270
+
271
+ g_major := g.Version[1]
272
+ g_minor := g.Version[3]
273
+
274
+ if major == g_major {
275
+ return minor <= g_minor
276
+ } else if g_major == '9' {
277
+ // v9.0 diverged from v8.5. This means we should compare with g_minor increased by five.
278
+ return minor <= g_minor+5
279
+ } else {
280
+ return false
281
+ }
282
+ }
283
+
284
+ func gomips() string {
285
+ switch v := envOr("GOMIPS", DefaultGOMIPS); v {
286
+ case "hardfloat", "softfloat":
287
+ return v
288
+ }
289
+ Error = fmt.Errorf("invalid GOMIPS: must be hardfloat, softfloat")
290
+ return DefaultGOMIPS
291
+ }
292
+
293
+ func gomips64() string {
294
+ switch v := envOr("GOMIPS64", DefaultGOMIPS64); v {
295
+ case "hardfloat", "softfloat":
296
+ return v
297
+ }
298
+ Error = fmt.Errorf("invalid GOMIPS64: must be hardfloat, softfloat")
299
+ return DefaultGOMIPS64
300
+ }
301
+
302
+ func goppc64() int {
303
+ switch v := envOr("GOPPC64", DefaultGOPPC64); v {
304
+ case "power8":
305
+ return 8
306
+ case "power9":
307
+ return 9
308
+ case "power10":
309
+ return 10
310
+ }
311
+ Error = fmt.Errorf("invalid GOPPC64: must be power8, power9, power10")
312
+ return int(DefaultGOPPC64[len("power")] - '0')
313
+ }
314
+
315
+ func goriscv64() int {
316
+ switch v := envOr("GORISCV64", DefaultGORISCV64); v {
317
+ case "rva20u64":
318
+ return 20
319
+ case "rva22u64":
320
+ return 22
321
+ case "rva23u64":
322
+ return 23
323
+ }
324
+ Error = fmt.Errorf("invalid GORISCV64: must be rva20u64, rva22u64, rva23u64")
325
+ v := DefaultGORISCV64[len("rva"):]
326
+ i := strings.IndexFunc(v, func(r rune) bool {
327
+ return r < '0' || r > '9'
328
+ })
329
+ year, _ := strconv.Atoi(v[:i])
330
+ return year
331
+ }
332
+
333
+ type gowasmFeatures struct {
334
+ // Legacy features, now always enabled
335
+ //SatConv bool
336
+ //SignExt bool
337
+ }
338
+
339
+ func (f gowasmFeatures) String() string {
340
+ var flags []string
341
+ return strings.Join(flags, ",")
342
+ }
343
+
344
+ func gowasm() (f gowasmFeatures) {
345
+ for opt := range strings.SplitSeq(envOr("GOWASM", ""), ",") {
346
+ switch opt {
347
+ case "satconv":
348
+ // ignore, always enabled
349
+ case "signext":
350
+ // ignore, always enabled
351
+ case "":
352
+ // ignore
353
+ default:
354
+ Error = fmt.Errorf("invalid GOWASM: no such feature %q", opt)
355
+ }
356
+ }
357
+ return
358
+ }
359
+
360
+ func Getgoextlinkenabled() string {
361
+ return envOr("GO_EXTLINK_ENABLED", defaultGO_EXTLINK_ENABLED)
362
+ }
363
+
364
+ func toolTags() []string {
365
+ tags := experimentTags()
366
+ tags = append(tags, gogoarchTags()...)
367
+ return tags
368
+ }
369
+
370
+ func experimentTags() []string {
371
+ var list []string
372
+ // For each experiment that has been enabled in the toolchain, define a
373
+ // build tag with the same name but prefixed by "goexperiment." which can be
374
+ // used for compiling alternative files for the experiment. This allows
375
+ // changes for the experiment, like extra struct fields in the runtime,
376
+ // without affecting the base non-experiment code at all.
377
+ for _, exp := range Experiment.Enabled() {
378
+ list = append(list, "goexperiment."+exp)
379
+ }
380
+ return list
381
+ }
382
+
383
+ // GOGOARCH returns the name and value of the GO$GOARCH setting.
384
+ // For example, if GOARCH is "amd64" it might return "GOAMD64", "v2".
385
+ func GOGOARCH() (name, value string) {
386
+ switch GOARCH {
387
+ case "386":
388
+ return "GO386", GO386
389
+ case "amd64":
390
+ return "GOAMD64", fmt.Sprintf("v%d", GOAMD64)
391
+ case "arm":
392
+ return "GOARM", GOARM.String()
393
+ case "arm64":
394
+ return "GOARM64", GOARM64.String()
395
+ case "mips", "mipsle":
396
+ return "GOMIPS", GOMIPS
397
+ case "mips64", "mips64le":
398
+ return "GOMIPS64", GOMIPS64
399
+ case "ppc64", "ppc64le":
400
+ return "GOPPC64", fmt.Sprintf("power%d", GOPPC64)
401
+ case "riscv64":
402
+ return "GORISCV64", fmt.Sprintf("rva%du64", GORISCV64)
403
+ case "wasm":
404
+ return "GOWASM", GOWASM.String()
405
+ }
406
+ return "", ""
407
+ }
408
+
409
+ func gogoarchTags() []string {
410
+ switch GOARCH {
411
+ case "386":
412
+ return []string{GOARCH + "." + GO386}
413
+ case "amd64":
414
+ var list []string
415
+ for i := 1; i <= GOAMD64; i++ {
416
+ list = append(list, fmt.Sprintf("%s.v%d", GOARCH, i))
417
+ }
418
+ return list
419
+ case "arm":
420
+ var list []string
421
+ for i := 5; i <= GOARM.Version; i++ {
422
+ list = append(list, fmt.Sprintf("%s.%d", GOARCH, i))
423
+ }
424
+ return list
425
+ case "arm64":
426
+ var list []string
427
+ major := int(GOARM64.Version[1] - '0')
428
+ minor := int(GOARM64.Version[3] - '0')
429
+ for i := 0; i <= minor; i++ {
430
+ list = append(list, fmt.Sprintf("%s.v%d.%d", GOARCH, major, i))
431
+ }
432
+ // ARM64 v9.x also includes support of v8.x+5 (i.e. v9.1 includes v8.(1+5) = v8.6).
433
+ if major == 9 {
434
+ for i := 0; i <= minor+5 && i <= 9; i++ {
435
+ list = append(list, fmt.Sprintf("%s.v%d.%d", GOARCH, 8, i))
436
+ }
437
+ }
438
+ return list
439
+ case "mips", "mipsle":
440
+ return []string{GOARCH + "." + GOMIPS}
441
+ case "mips64", "mips64le":
442
+ return []string{GOARCH + "." + GOMIPS64}
443
+ case "ppc64", "ppc64le":
444
+ var list []string
445
+ for i := 8; i <= GOPPC64; i++ {
446
+ list = append(list, fmt.Sprintf("%s.power%d", GOARCH, i))
447
+ }
448
+ return list
449
+ case "riscv64":
450
+ list := []string{GOARCH + "." + "rva20u64"}
451
+ if GORISCV64 >= 22 {
452
+ list = append(list, GOARCH+"."+"rva22u64")
453
+ }
454
+ if GORISCV64 >= 23 {
455
+ list = append(list, GOARCH+"."+"rva23u64")
456
+ }
457
+ return list
458
+ case "wasm":
459
+ var list []string
460
+ // SatConv is always enabled
461
+ list = append(list, GOARCH+".satconv")
462
+ // SignExt is always enabled
463
+ list = append(list, GOARCH+".signext")
464
+ return list
465
+ }
466
+ return nil
467
+ }
go/src/internal/buildcfg/cfg_test.go ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 buildcfg
6
+
7
+ import (
8
+ "os"
9
+ "testing"
10
+ )
11
+
12
+ func TestConfigFlags(t *testing.T) {
13
+ os.Setenv("GOAMD64", "v1")
14
+ if goamd64() != 1 {
15
+ t.Errorf("Wrong parsing of GOAMD64=v1")
16
+ }
17
+ os.Setenv("GOAMD64", "v4")
18
+ if goamd64() != 4 {
19
+ t.Errorf("Wrong parsing of GOAMD64=v4")
20
+ }
21
+ Error = nil
22
+ os.Setenv("GOAMD64", "1")
23
+ if goamd64(); Error == nil {
24
+ t.Errorf("Wrong parsing of GOAMD64=1")
25
+ }
26
+
27
+ os.Setenv("GORISCV64", "rva20u64")
28
+ if goriscv64() != 20 {
29
+ t.Errorf("Wrong parsing of RISCV64=rva20u64")
30
+ }
31
+ os.Setenv("GORISCV64", "rva22u64")
32
+ if goriscv64() != 22 {
33
+ t.Errorf("Wrong parsing of RISCV64=rva22u64")
34
+ }
35
+ os.Setenv("GORISCV64", "rva23u64")
36
+ if goriscv64() != 23 {
37
+ t.Errorf("Wrong parsing of RISCV64=rva23u64")
38
+ }
39
+ Error = nil
40
+ os.Setenv("GORISCV64", "rva22")
41
+ if _ = goriscv64(); Error == nil {
42
+ t.Errorf("Wrong parsing of RISCV64=rva22")
43
+ }
44
+ Error = nil
45
+ os.Setenv("GOARM64", "v7.0")
46
+ if _ = goarm64(); Error == nil {
47
+ t.Errorf("Wrong parsing of GOARM64=7.0")
48
+ }
49
+ Error = nil
50
+ os.Setenv("GOARM64", "8.0")
51
+ if _ = goarm64(); Error == nil {
52
+ t.Errorf("Wrong parsing of GOARM64=8.0")
53
+ }
54
+ Error = nil
55
+ os.Setenv("GOARM64", "v8.0,lsb")
56
+ if _ = goarm64(); Error == nil {
57
+ t.Errorf("Wrong parsing of GOARM64=v8.0,lsb")
58
+ }
59
+ os.Setenv("GOARM64", "v8.0,lse")
60
+ if goarm64().Version != "v8.0" || goarm64().LSE != true || goarm64().Crypto != false {
61
+ t.Errorf("Wrong parsing of GOARM64=v8.0,lse")
62
+ }
63
+ os.Setenv("GOARM64", "v8.0,crypto")
64
+ if goarm64().Version != "v8.0" || goarm64().LSE != false || goarm64().Crypto != true {
65
+ t.Errorf("Wrong parsing of GOARM64=v8.0,crypto")
66
+ }
67
+ os.Setenv("GOARM64", "v8.0,crypto,lse")
68
+ if goarm64().Version != "v8.0" || goarm64().LSE != true || goarm64().Crypto != true {
69
+ t.Errorf("Wrong parsing of GOARM64=v8.0,crypto,lse")
70
+ }
71
+ os.Setenv("GOARM64", "v8.0,lse,crypto")
72
+ if goarm64().Version != "v8.0" || goarm64().LSE != true || goarm64().Crypto != true {
73
+ t.Errorf("Wrong parsing of GOARM64=v8.0,lse,crypto")
74
+ }
75
+ os.Setenv("GOARM64", "v9.0")
76
+ if goarm64().Version != "v9.0" || goarm64().LSE != true || goarm64().Crypto != false {
77
+ t.Errorf("Wrong parsing of GOARM64=v9.0")
78
+ }
79
+ }
80
+
81
+ func TestGoarm64FeaturesSupports(t *testing.T) {
82
+ g, _ := ParseGoarm64("v9.3")
83
+
84
+ if !g.Supports("v9.3") {
85
+ t.Errorf("Wrong goarm64Features.Supports for v9.3, v9.3")
86
+ }
87
+
88
+ if g.Supports("v9.4") {
89
+ t.Errorf("Wrong goarm64Features.Supports for v9.3, v9.4")
90
+ }
91
+
92
+ if !g.Supports("v8.8") {
93
+ t.Errorf("Wrong goarm64Features.Supports for v9.3, v8.8")
94
+ }
95
+
96
+ if g.Supports("v8.9") {
97
+ t.Errorf("Wrong goarm64Features.Supports for v9.3, v8.9")
98
+ }
99
+
100
+ if g.Supports(",lse") {
101
+ t.Errorf("Wrong goarm64Features.Supports for v9.3, ,lse")
102
+ }
103
+ }
104
+
105
+ func TestGogoarchTags(t *testing.T) {
106
+ old_goarch := GOARCH
107
+ old_goarm64 := GOARM64
108
+
109
+ GOARCH = "arm64"
110
+
111
+ os.Setenv("GOARM64", "v9.5")
112
+ GOARM64 = goarm64()
113
+ tags := gogoarchTags()
114
+ want := []string{"arm64.v9.0", "arm64.v9.1", "arm64.v9.2", "arm64.v9.3", "arm64.v9.4", "arm64.v9.5",
115
+ "arm64.v8.0", "arm64.v8.1", "arm64.v8.2", "arm64.v8.3", "arm64.v8.4", "arm64.v8.5", "arm64.v8.6", "arm64.v8.7", "arm64.v8.8", "arm64.v8.9"}
116
+ if len(tags) != len(want) {
117
+ t.Errorf("Wrong number of tags for GOARM64=v9.5")
118
+ } else {
119
+ for i, v := range tags {
120
+ if v != want[i] {
121
+ t.Error("Wrong tags for GOARM64=v9.5")
122
+ break
123
+ }
124
+ }
125
+ }
126
+
127
+ GOARCH = old_goarch
128
+ GOARM64 = old_goarm64
129
+ }
130
+
131
+ var goodFIPS = []string{
132
+ "v1.0.0",
133
+ "v1.0.1",
134
+ "v1.2.0",
135
+ "v1.2.3",
136
+ }
137
+
138
+ var badFIPS = []string{
139
+ "v1.0.0-fips",
140
+ "v1.0.0+fips",
141
+ "1.0.0",
142
+ "x1.0.0",
143
+ }
144
+
145
+ func TestIsFIPSVersion(t *testing.T) {
146
+ // good
147
+ for _, s := range goodFIPS {
148
+ if !isFIPSVersion(s) {
149
+ t.Errorf("isFIPSVersion(%q) = false, want true", s)
150
+ }
151
+ }
152
+ // truncated
153
+ const v = "v1.2.3"
154
+ for i := 0; i < len(v); i++ {
155
+ if isFIPSVersion(v[:i]) {
156
+ t.Errorf("isFIPSVersion(%q) = true, want false", v[:i])
157
+ }
158
+ }
159
+ // bad
160
+ for _, s := range badFIPS {
161
+ if isFIPSVersion(s) {
162
+ t.Errorf("isFIPSVersion(%q) = true, want false", s)
163
+ }
164
+ }
165
+ }
go/src/internal/buildcfg/exp.go ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 buildcfg
6
+
7
+ import (
8
+ "fmt"
9
+ "reflect"
10
+ "strings"
11
+
12
+ "internal/goexperiment"
13
+ )
14
+
15
+ // ExperimentFlags represents a set of GOEXPERIMENT flags relative to a baseline
16
+ // (platform-default) experiment configuration.
17
+ type ExperimentFlags struct {
18
+ goexperiment.Flags
19
+ baseline goexperiment.Flags
20
+ }
21
+
22
+ // Experiment contains the toolchain experiments enabled for the
23
+ // current build.
24
+ //
25
+ // (This is not necessarily the set of experiments the compiler itself
26
+ // was built with.)
27
+ //
28
+ // Experiment.baseline specifies the experiment flags that are enabled by
29
+ // default in the current toolchain. This is, in effect, the "control"
30
+ // configuration and any variation from this is an experiment.
31
+ var Experiment ExperimentFlags = func() ExperimentFlags {
32
+ flags, err := ParseGOEXPERIMENT(GOOS, GOARCH, envOr("GOEXPERIMENT", defaultGOEXPERIMENT))
33
+ if err != nil {
34
+ Error = err
35
+ return ExperimentFlags{}
36
+ }
37
+ return *flags
38
+ }()
39
+
40
+ // DefaultGOEXPERIMENT is the embedded default GOEXPERIMENT string.
41
+ // It is not guaranteed to be canonical.
42
+ const DefaultGOEXPERIMENT = defaultGOEXPERIMENT
43
+
44
+ // FramePointerEnabled enables the use of platform conventions for
45
+ // saving frame pointers.
46
+ //
47
+ // This used to be an experiment, but now it's always enabled on
48
+ // platforms that support it.
49
+ //
50
+ // Note: must agree with runtime.framepointer_enabled.
51
+ var FramePointerEnabled = GOARCH == "amd64" || GOARCH == "arm64"
52
+
53
+ // ParseGOEXPERIMENT parses a (GOOS, GOARCH, GOEXPERIMENT)
54
+ // configuration tuple and returns the enabled and baseline experiment
55
+ // flag sets.
56
+ //
57
+ // TODO(mdempsky): Move to [internal/goexperiment].
58
+ func ParseGOEXPERIMENT(goos, goarch, goexp string) (*ExperimentFlags, error) {
59
+ // regabiSupported is set to true on platforms where register ABI is
60
+ // supported and enabled by default.
61
+ // regabiAlwaysOn is set to true on platforms where register ABI is
62
+ // always on.
63
+ var regabiSupported, regabiAlwaysOn bool
64
+ switch goarch {
65
+ case "amd64", "arm64", "loong64", "ppc64le", "ppc64", "riscv64":
66
+ regabiAlwaysOn = true
67
+ regabiSupported = true
68
+ case "s390x":
69
+ regabiSupported = true
70
+ }
71
+
72
+ // Older versions (anything before V16) of dsymutil don't handle
73
+ // the .debug_rnglists section in DWARF5. See
74
+ // https://github.com/golang/go/issues/26379#issuecomment-2677068742
75
+ // for more context. This disables all DWARF5 on mac, which is not
76
+ // ideal (would be better to disable just for cases where we know
77
+ // the build will use external linking). In the GOOS=aix case, the
78
+ // XCOFF format (as far as can be determined) doesn't seem to
79
+ // support the necessary section subtypes for DWARF-specific
80
+ // things like .debug_addr (needed for DWARF 5).
81
+ dwarf5Supported := (goos != "darwin" && goos != "ios" && goos != "aix")
82
+
83
+ baseline := goexperiment.Flags{
84
+ RegabiWrappers: regabiSupported,
85
+ RegabiArgs: regabiSupported,
86
+ Dwarf5: dwarf5Supported,
87
+ RandomizedHeapBase64: true,
88
+ GreenTeaGC: true,
89
+ }
90
+ flags := &ExperimentFlags{
91
+ Flags: baseline,
92
+ baseline: baseline,
93
+ }
94
+
95
+ // Pick up any changes to the baseline configuration from the
96
+ // GOEXPERIMENT environment. This can be set at make.bash time
97
+ // and overridden at build time.
98
+ if goexp != "" {
99
+ // Create a map of known experiment names.
100
+ names := make(map[string]func(bool))
101
+ rv := reflect.ValueOf(&flags.Flags).Elem()
102
+ rt := rv.Type()
103
+ for i := 0; i < rt.NumField(); i++ {
104
+ field := rv.Field(i)
105
+ names[strings.ToLower(rt.Field(i).Name)] = field.SetBool
106
+ }
107
+
108
+ // "regabi" is an alias for all working regabi
109
+ // subexperiments, and not an experiment itself. Doing
110
+ // this as an alias make both "regabi" and "noregabi"
111
+ // do the right thing.
112
+ names["regabi"] = func(v bool) {
113
+ flags.RegabiWrappers = v
114
+ flags.RegabiArgs = v
115
+ }
116
+
117
+ // Parse names.
118
+ for f := range strings.SplitSeq(goexp, ",") {
119
+ if f == "" {
120
+ continue
121
+ }
122
+ if f == "none" {
123
+ // GOEXPERIMENT=none disables all experiment flags.
124
+ // This is used by cmd/dist, which doesn't know how
125
+ // to build with any experiment flags.
126
+ flags.Flags = goexperiment.Flags{}
127
+ continue
128
+ }
129
+ val := true
130
+ if strings.HasPrefix(f, "no") {
131
+ f, val = f[2:], false
132
+ }
133
+ set, ok := names[f]
134
+ if !ok {
135
+ return nil, fmt.Errorf("unknown GOEXPERIMENT %s", f)
136
+ }
137
+ set(val)
138
+ }
139
+ }
140
+
141
+ if regabiAlwaysOn {
142
+ flags.RegabiWrappers = true
143
+ flags.RegabiArgs = true
144
+ }
145
+ // regabi is only supported on amd64, arm64, loong64, riscv64, s390x, ppc64 and ppc64le.
146
+ if !regabiSupported {
147
+ flags.RegabiWrappers = false
148
+ flags.RegabiArgs = false
149
+ }
150
+ // Check regabi dependencies.
151
+ if flags.RegabiArgs && !flags.RegabiWrappers {
152
+ return nil, fmt.Errorf("GOEXPERIMENT regabiargs requires regabiwrappers")
153
+ }
154
+ return flags, nil
155
+ }
156
+
157
+ // String returns the canonical GOEXPERIMENT string to enable this experiment
158
+ // configuration. (Experiments in the same state as in the baseline are elided.)
159
+ func (exp *ExperimentFlags) String() string {
160
+ return strings.Join(expList(&exp.Flags, &exp.baseline, false), ",")
161
+ }
162
+
163
+ // expList returns the list of lower-cased experiment names for
164
+ // experiments that differ from base. base may be nil to indicate no
165
+ // experiments. If all is true, then include all experiment flags,
166
+ // regardless of base.
167
+ func expList(exp, base *goexperiment.Flags, all bool) []string {
168
+ var list []string
169
+ rv := reflect.ValueOf(exp).Elem()
170
+ var rBase reflect.Value
171
+ if base != nil {
172
+ rBase = reflect.ValueOf(base).Elem()
173
+ }
174
+ rt := rv.Type()
175
+ for i := 0; i < rt.NumField(); i++ {
176
+ name := strings.ToLower(rt.Field(i).Name)
177
+ val := rv.Field(i).Bool()
178
+ baseVal := false
179
+ if base != nil {
180
+ baseVal = rBase.Field(i).Bool()
181
+ }
182
+ if all || val != baseVal {
183
+ if val {
184
+ list = append(list, name)
185
+ } else {
186
+ list = append(list, "no"+name)
187
+ }
188
+ }
189
+ }
190
+ return list
191
+ }
192
+
193
+ // Enabled returns a list of enabled experiments, as
194
+ // lower-cased experiment names.
195
+ func (exp *ExperimentFlags) Enabled() []string {
196
+ return expList(&exp.Flags, nil, false)
197
+ }
198
+
199
+ // All returns a list of all experiment settings.
200
+ // Disabled experiments appear in the list prefixed by "no".
201
+ func (exp *ExperimentFlags) All() []string {
202
+ return expList(&exp.Flags, nil, true)
203
+ }
go/src/internal/buildcfg/zbootstrap.go ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Code generated by go tool dist; DO NOT EDIT.
2
+
3
+ package buildcfg
4
+
5
+ import "runtime"
6
+
7
+ const DefaultGO386 = `sse2`
8
+ const DefaultGOAMD64 = `v1`
9
+ const DefaultGOARM = `7`
10
+ const DefaultGOARM64 = `v8.0`
11
+ const DefaultGOMIPS = `hardfloat`
12
+ const DefaultGOMIPS64 = `hardfloat`
13
+ const DefaultGOPPC64 = `power8`
14
+ const DefaultGORISCV64 = `rva20u64`
15
+ const defaultGOEXPERIMENT = ``
16
+ const defaultGO_EXTLINK_ENABLED = ``
17
+ const defaultGO_LDSO = ``
18
+ const version = `go1.26.3`
19
+ const defaultGOOS = runtime.GOOS
20
+ const defaultGOARCH = runtime.GOARCH
21
+ const DefaultGOFIPS140 = `off`
22
+ const DefaultCGO_ENABLED = ""
go/src/internal/bytealg/bytealg.go ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 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 bytealg
6
+
7
+ import (
8
+ "internal/cpu"
9
+ "unsafe"
10
+ )
11
+
12
+ // Offsets into internal/cpu records for use in assembly.
13
+ const (
14
+ offsetPPC64HasPOWER9 = unsafe.Offsetof(cpu.PPC64.IsPOWER9)
15
+
16
+ offsetRISCV64HasV = unsafe.Offsetof(cpu.RISCV64.HasV)
17
+
18
+ offsetLOONG64HasLSX = unsafe.Offsetof(cpu.Loong64.HasLSX)
19
+ offsetLOONG64HasLASX = unsafe.Offsetof(cpu.Loong64.HasLASX)
20
+
21
+ offsetS390xHasVX = unsafe.Offsetof(cpu.S390X.HasVX)
22
+
23
+ offsetX86HasSSE42 = unsafe.Offsetof(cpu.X86.HasSSE42)
24
+ offsetX86HasAVX2 = unsafe.Offsetof(cpu.X86.HasAVX2)
25
+ offsetX86HasPOPCNT = unsafe.Offsetof(cpu.X86.HasPOPCNT)
26
+ )
27
+
28
+ // MaxLen is the maximum length of the string to be searched for (argument b) in Index.
29
+ // If MaxLen is not 0, make sure MaxLen >= 4.
30
+ var MaxLen int
31
+
32
+ // PrimeRK is the prime base used in Rabin-Karp algorithm.
33
+ const PrimeRK = 16777619
34
+
35
+ // HashStr returns the hash and the appropriate multiplicative
36
+ // factor for use in Rabin-Karp algorithm.
37
+ func HashStr[T string | []byte](sep T) (uint32, uint32) {
38
+ hash := uint32(0)
39
+ for i := 0; i < len(sep); i++ {
40
+ hash = hash*PrimeRK + uint32(sep[i])
41
+ }
42
+ var pow, sq uint32 = 1, PrimeRK
43
+ for i := len(sep); i > 0; i >>= 1 {
44
+ if i&1 != 0 {
45
+ pow *= sq
46
+ }
47
+ sq *= sq
48
+ }
49
+ return hash, pow
50
+ }
51
+
52
+ // HashStrRev returns the hash of the reverse of sep and the
53
+ // appropriate multiplicative factor for use in Rabin-Karp algorithm.
54
+ func HashStrRev[T string | []byte](sep T) (uint32, uint32) {
55
+ hash := uint32(0)
56
+ for i := len(sep) - 1; i >= 0; i-- {
57
+ hash = hash*PrimeRK + uint32(sep[i])
58
+ }
59
+ var pow, sq uint32 = 1, PrimeRK
60
+ for i := len(sep); i > 0; i >>= 1 {
61
+ if i&1 != 0 {
62
+ pow *= sq
63
+ }
64
+ sq *= sq
65
+ }
66
+ return hash, pow
67
+ }
68
+
69
+ // IndexRabinKarp uses the Rabin-Karp search algorithm to return the index of the
70
+ // first occurrence of sep in s, or -1 if not present.
71
+ func IndexRabinKarp[T string | []byte](s, sep T) int {
72
+ // Rabin-Karp search
73
+ hashss, pow := HashStr(sep)
74
+ n := len(sep)
75
+ var h uint32
76
+ for i := 0; i < n; i++ {
77
+ h = h*PrimeRK + uint32(s[i])
78
+ }
79
+ if h == hashss && string(s[:n]) == string(sep) {
80
+ return 0
81
+ }
82
+ for i := n; i < len(s); {
83
+ h *= PrimeRK
84
+ h += uint32(s[i])
85
+ h -= pow * uint32(s[i-n])
86
+ i++
87
+ if h == hashss && string(s[i-n:i]) == string(sep) {
88
+ return i - n
89
+ }
90
+ }
91
+ return -1
92
+ }
93
+
94
+ // LastIndexRabinKarp uses the Rabin-Karp search algorithm to return the last index of the
95
+ // occurrence of sep in s, or -1 if not present.
96
+ func LastIndexRabinKarp[T string | []byte](s, sep T) int {
97
+ // Rabin-Karp search from the end of the string
98
+ hashss, pow := HashStrRev(sep)
99
+ n := len(sep)
100
+ last := len(s) - n
101
+ var h uint32
102
+ for i := len(s) - 1; i >= last; i-- {
103
+ h = h*PrimeRK + uint32(s[i])
104
+ }
105
+ if h == hashss && string(s[last:]) == string(sep) {
106
+ return last
107
+ }
108
+ for i := last - 1; i >= 0; i-- {
109
+ h *= PrimeRK
110
+ h += uint32(s[i])
111
+ h -= pow * uint32(s[i+n])
112
+ if h == hashss && string(s[i:i+n]) == string(sep) {
113
+ return i
114
+ }
115
+ }
116
+ return -1
117
+ }
118
+
119
+ // MakeNoZero makes a slice of length n and capacity of at least n Bytes
120
+ // without zeroing the bytes (including the bytes between len and cap).
121
+ // It is the caller's responsibility to ensure uninitialized bytes
122
+ // do not leak to the end user.
123
+ func MakeNoZero(n int) []byte
go/src/internal/bytealg/compare_386.s ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 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
+ #include "go_asm.h"
6
+ #include "textflag.h"
7
+
8
+ TEXT ·Compare(SB),NOSPLIT,$0-28
9
+ MOVL a_base+0(FP), SI
10
+ MOVL a_len+4(FP), BX
11
+ MOVL b_base+12(FP), DI
12
+ MOVL b_len+16(FP), DX
13
+ LEAL ret+24(FP), AX
14
+ JMP cmpbody<>(SB)
15
+
16
+ TEXT runtime·cmpstring(SB),NOSPLIT,$0-20
17
+ MOVL a_base+0(FP), SI
18
+ MOVL a_len+4(FP), BX
19
+ MOVL b_base+8(FP), DI
20
+ MOVL b_len+12(FP), DX
21
+ LEAL ret+16(FP), AX
22
+ JMP cmpbody<>(SB)
23
+
24
+ // input:
25
+ // SI = a
26
+ // DI = b
27
+ // BX = alen
28
+ // DX = blen
29
+ // AX = address of return word (set to 1/0/-1)
30
+ TEXT cmpbody<>(SB),NOSPLIT,$0-0
31
+ MOVL DX, BP
32
+ SUBL BX, DX // DX = blen-alen
33
+ JLE 2(PC)
34
+ MOVL BX, BP // BP = min(alen, blen)
35
+ CMPL SI, DI
36
+ JEQ allsame
37
+ CMPL BP, $4
38
+ JB small
39
+ #ifdef GO386_softfloat
40
+ JMP mediumloop
41
+ #endif
42
+ largeloop:
43
+ CMPL BP, $16
44
+ JB mediumloop
45
+ MOVOU (SI), X0
46
+ MOVOU (DI), X1
47
+ PCMPEQB X0, X1
48
+ PMOVMSKB X1, BX
49
+ XORL $0xffff, BX // convert EQ to NE
50
+ JNE diff16 // branch if at least one byte is not equal
51
+ ADDL $16, SI
52
+ ADDL $16, DI
53
+ SUBL $16, BP
54
+ JMP largeloop
55
+
56
+ diff16:
57
+ BSFL BX, BX // index of first byte that differs
58
+ XORL DX, DX
59
+ MOVB (SI)(BX*1), CX
60
+ CMPB CX, (DI)(BX*1)
61
+ SETHI DX
62
+ LEAL -1(DX*2), DX // convert 1/0 to +1/-1
63
+ MOVL DX, (AX)
64
+ RET
65
+
66
+ mediumloop:
67
+ CMPL BP, $4
68
+ JBE _0through4
69
+ MOVL (SI), BX
70
+ MOVL (DI), CX
71
+ CMPL BX, CX
72
+ JNE diff4
73
+ ADDL $4, SI
74
+ ADDL $4, DI
75
+ SUBL $4, BP
76
+ JMP mediumloop
77
+
78
+ _0through4:
79
+ MOVL -4(SI)(BP*1), BX
80
+ MOVL -4(DI)(BP*1), CX
81
+ CMPL BX, CX
82
+ JEQ allsame
83
+
84
+ diff4:
85
+ BSWAPL BX // reverse order of bytes
86
+ BSWAPL CX
87
+ XORL BX, CX // find bit differences
88
+ BSRL CX, CX // index of highest bit difference
89
+ SHRL CX, BX // move a's bit to bottom
90
+ ANDL $1, BX // mask bit
91
+ LEAL -1(BX*2), BX // 1/0 => +1/-1
92
+ MOVL BX, (AX)
93
+ RET
94
+
95
+ // 0-3 bytes in common
96
+ small:
97
+ LEAL (BP*8), CX
98
+ NEGL CX
99
+ JEQ allsame
100
+
101
+ // load si
102
+ CMPB SI, $0xfc
103
+ JA si_high
104
+ MOVL (SI), SI
105
+ JMP si_finish
106
+ si_high:
107
+ MOVL -4(SI)(BP*1), SI
108
+ SHRL CX, SI
109
+ si_finish:
110
+ SHLL CX, SI
111
+
112
+ // same for di
113
+ CMPB DI, $0xfc
114
+ JA di_high
115
+ MOVL (DI), DI
116
+ JMP di_finish
117
+ di_high:
118
+ MOVL -4(DI)(BP*1), DI
119
+ SHRL CX, DI
120
+ di_finish:
121
+ SHLL CX, DI
122
+
123
+ BSWAPL SI // reverse order of bytes
124
+ BSWAPL DI
125
+ XORL SI, DI // find bit differences
126
+ JEQ allsame
127
+ BSRL DI, CX // index of highest bit difference
128
+ SHRL CX, SI // move a's bit to bottom
129
+ ANDL $1, SI // mask bit
130
+ LEAL -1(SI*2), BX // 1/0 => +1/-1
131
+ MOVL BX, (AX)
132
+ RET
133
+
134
+ // all the bytes in common are the same, so we just need
135
+ // to compare the lengths.
136
+ allsame:
137
+ XORL BX, BX
138
+ XORL CX, CX
139
+ TESTL DX, DX
140
+ SETLT BX // 1 if alen > blen
141
+ SETEQ CX // 1 if alen == blen
142
+ LEAL -1(CX)(BX*2), BX // 1,0,-1 result
143
+ MOVL BX, (AX)
144
+ RET
go/src/internal/bytealg/compare_amd64.s ADDED
@@ -0,0 +1,237 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 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
+ #include "go_asm.h"
6
+ #include "asm_amd64.h"
7
+ #include "textflag.h"
8
+
9
+ TEXT ·Compare<ABIInternal>(SB),NOSPLIT,$0-56
10
+ // AX = a_base (want in SI)
11
+ // BX = a_len (want in BX)
12
+ // CX = a_cap (unused)
13
+ // DI = b_base (want in DI)
14
+ // SI = b_len (want in DX)
15
+ // R8 = b_cap (unused)
16
+ MOVQ SI, DX
17
+ MOVQ AX, SI
18
+ JMP cmpbody<>(SB)
19
+
20
+ TEXT runtime·cmpstring<ABIInternal>(SB),NOSPLIT,$0-40
21
+ // AX = a_base (want in SI)
22
+ // BX = a_len (want in BX)
23
+ // CX = b_base (want in DI)
24
+ // DI = b_len (want in DX)
25
+ MOVQ AX, SI
26
+ MOVQ DI, DX
27
+ MOVQ CX, DI
28
+ JMP cmpbody<>(SB)
29
+
30
+ // input:
31
+ // SI = a
32
+ // DI = b
33
+ // BX = alen
34
+ // DX = blen
35
+ // output:
36
+ // AX = output (-1/0/1)
37
+ TEXT cmpbody<>(SB),NOSPLIT,$0-0
38
+ CMPQ SI, DI
39
+ JEQ allsame
40
+ CMPQ BX, DX
41
+ MOVQ DX, R8
42
+ CMOVQLT BX, R8 // R8 = min(alen, blen) = # of bytes to compare
43
+ CMPQ R8, $8
44
+ JB small
45
+
46
+ CMPQ R8, $63
47
+ JBE loop
48
+ #ifndef hasAVX2
49
+ CMPB internal∕cpu·X86+const_offsetX86HasAVX2(SB), $1
50
+ JEQ big_loop_avx2
51
+ JMP big_loop
52
+ #else
53
+ JMP big_loop_avx2
54
+ #endif
55
+ loop:
56
+ CMPQ R8, $16
57
+ JBE _0through16
58
+ MOVOU (SI), X0
59
+ MOVOU (DI), X1
60
+ PCMPEQB X0, X1
61
+ PMOVMSKB X1, AX
62
+ XORQ $0xffff, AX // convert EQ to NE
63
+ JNE diff16 // branch if at least one byte is not equal
64
+ ADDQ $16, SI
65
+ ADDQ $16, DI
66
+ SUBQ $16, R8
67
+ JMP loop
68
+
69
+ diff64:
70
+ ADDQ $48, SI
71
+ ADDQ $48, DI
72
+ JMP diff16
73
+ diff48:
74
+ ADDQ $32, SI
75
+ ADDQ $32, DI
76
+ JMP diff16
77
+ diff32:
78
+ ADDQ $16, SI
79
+ ADDQ $16, DI
80
+ // AX = bit mask of differences
81
+ diff16:
82
+ BSFQ AX, BX // index of first byte that differs
83
+ XORQ AX, AX
84
+ MOVB (SI)(BX*1), CX
85
+ CMPB CX, (DI)(BX*1)
86
+ SETHI AX
87
+ LEAQ -1(AX*2), AX // convert 1/0 to +1/-1
88
+ RET
89
+
90
+ // 0 through 16 bytes left, alen>=8, blen>=8
91
+ _0through16:
92
+ CMPQ R8, $8
93
+ JBE _0through8
94
+ MOVQ (SI), AX
95
+ MOVQ (DI), CX
96
+ CMPQ AX, CX
97
+ JNE diff8
98
+ _0through8:
99
+ MOVQ -8(SI)(R8*1), AX
100
+ MOVQ -8(DI)(R8*1), CX
101
+ CMPQ AX, CX
102
+ JEQ allsame
103
+
104
+ // AX and CX contain parts of a and b that differ.
105
+ diff8:
106
+ BSWAPQ AX // reverse order of bytes
107
+ BSWAPQ CX
108
+ XORQ AX, CX
109
+ BSRQ CX, CX // index of highest bit difference
110
+ SHRQ CX, AX // move a's bit to bottom
111
+ ANDQ $1, AX // mask bit
112
+ LEAQ -1(AX*2), AX // 1/0 => +1/-1
113
+ RET
114
+
115
+ // 0-7 bytes in common
116
+ small:
117
+ LEAQ (R8*8), CX // bytes left -> bits left
118
+ NEGQ CX // - bits lift (== 64 - bits left mod 64)
119
+ JEQ allsame
120
+
121
+ // load bytes of a into high bytes of AX
122
+ CMPB SI, $0xf8
123
+ JA si_high
124
+ MOVQ (SI), SI
125
+ JMP si_finish
126
+ si_high:
127
+ MOVQ -8(SI)(R8*1), SI
128
+ SHRQ CX, SI
129
+ si_finish:
130
+ SHLQ CX, SI
131
+
132
+ // load bytes of b in to high bytes of BX
133
+ CMPB DI, $0xf8
134
+ JA di_high
135
+ MOVQ (DI), DI
136
+ JMP di_finish
137
+ di_high:
138
+ MOVQ -8(DI)(R8*1), DI
139
+ SHRQ CX, DI
140
+ di_finish:
141
+ SHLQ CX, DI
142
+
143
+ BSWAPQ SI // reverse order of bytes
144
+ BSWAPQ DI
145
+ XORQ SI, DI // find bit differences
146
+ JEQ allsame
147
+ BSRQ DI, CX // index of highest bit difference
148
+ SHRQ CX, SI // move a's bit to bottom
149
+ ANDQ $1, SI // mask bit
150
+ LEAQ -1(SI*2), AX // 1/0 => +1/-1
151
+ RET
152
+
153
+ allsame:
154
+ XORQ AX, AX
155
+ XORQ CX, CX
156
+ CMPQ BX, DX
157
+ SETGT AX // 1 if alen > blen
158
+ SETEQ CX // 1 if alen == blen
159
+ LEAQ -1(CX)(AX*2), AX // 1,0,-1 result
160
+ RET
161
+
162
+ // this works for >= 64 bytes of data.
163
+ #ifndef hasAVX2
164
+ big_loop:
165
+ MOVOU (SI), X0
166
+ MOVOU (DI), X1
167
+ PCMPEQB X0, X1
168
+ PMOVMSKB X1, AX
169
+ XORQ $0xffff, AX
170
+ JNE diff16
171
+
172
+ MOVOU 16(SI), X0
173
+ MOVOU 16(DI), X1
174
+ PCMPEQB X0, X1
175
+ PMOVMSKB X1, AX
176
+ XORQ $0xffff, AX
177
+ JNE diff32
178
+
179
+ MOVOU 32(SI), X0
180
+ MOVOU 32(DI), X1
181
+ PCMPEQB X0, X1
182
+ PMOVMSKB X1, AX
183
+ XORQ $0xffff, AX
184
+ JNE diff48
185
+
186
+ MOVOU 48(SI), X0
187
+ MOVOU 48(DI), X1
188
+ PCMPEQB X0, X1
189
+ PMOVMSKB X1, AX
190
+ XORQ $0xffff, AX
191
+ JNE diff64
192
+
193
+ ADDQ $64, SI
194
+ ADDQ $64, DI
195
+ SUBQ $64, R8
196
+ CMPQ R8, $64
197
+ JBE loop
198
+ JMP big_loop
199
+ #endif
200
+
201
+ // Compare 64-bytes per loop iteration.
202
+ // Loop is unrolled and uses AVX2.
203
+ big_loop_avx2:
204
+ VMOVDQU (SI), Y2
205
+ VMOVDQU (DI), Y3
206
+ VMOVDQU 32(SI), Y4
207
+ VMOVDQU 32(DI), Y5
208
+ VPCMPEQB Y2, Y3, Y0
209
+ VPMOVMSKB Y0, AX
210
+ XORL $0xffffffff, AX
211
+ JNE diff32_avx2
212
+ VPCMPEQB Y4, Y5, Y6
213
+ VPMOVMSKB Y6, AX
214
+ XORL $0xffffffff, AX
215
+ JNE diff64_avx2
216
+
217
+ ADDQ $64, SI
218
+ ADDQ $64, DI
219
+ SUBQ $64, R8
220
+ CMPQ R8, $64
221
+ JB big_loop_avx2_exit
222
+ JMP big_loop_avx2
223
+
224
+ // Avoid AVX->SSE transition penalty and search first 32 bytes of 64 byte chunk.
225
+ diff32_avx2:
226
+ VZEROUPPER
227
+ JMP diff16
228
+
229
+ // Same as diff32_avx2, but for last 32 bytes.
230
+ diff64_avx2:
231
+ VZEROUPPER
232
+ JMP diff48
233
+
234
+ // For <64 bytes remainder jump to normal loop.
235
+ big_loop_avx2_exit:
236
+ VZEROUPPER
237
+ JMP loop
go/src/internal/bytealg/compare_arm.s ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 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
+ #include "go_asm.h"
6
+ #include "textflag.h"
7
+
8
+ TEXT ·Compare(SB),NOSPLIT|NOFRAME,$0-28
9
+ MOVW a_base+0(FP), R2
10
+ MOVW a_len+4(FP), R0
11
+ MOVW b_base+12(FP), R3
12
+ MOVW b_len+16(FP), R1
13
+ ADD $28, R13, R7
14
+ B cmpbody<>(SB)
15
+
16
+ TEXT runtime·cmpstring(SB),NOSPLIT|NOFRAME,$0-20
17
+ MOVW a_base+0(FP), R2
18
+ MOVW a_len+4(FP), R0
19
+ MOVW b_base+8(FP), R3
20
+ MOVW b_len+12(FP), R1
21
+ ADD $20, R13, R7
22
+ B cmpbody<>(SB)
23
+
24
+ // On entry:
25
+ // R0 is the length of a
26
+ // R1 is the length of b
27
+ // R2 points to the start of a
28
+ // R3 points to the start of b
29
+ // R7 points to return value (-1/0/1 will be written here)
30
+ //
31
+ // On exit:
32
+ // R4, R5, R6 and R8 are clobbered
33
+ TEXT cmpbody<>(SB),NOSPLIT|NOFRAME,$0-0
34
+ CMP R2, R3
35
+ BEQ samebytes
36
+ CMP R0, R1
37
+ MOVW R0, R6
38
+ MOVW.LT R1, R6 // R6 is min(R0, R1)
39
+
40
+ CMP $0, R6
41
+ BEQ samebytes
42
+ CMP $4, R6
43
+ ADD R2, R6 // R2 is current byte in a, R6 is the end of the range to compare
44
+ BLT byte_loop // length < 4
45
+ AND $3, R2, R8
46
+ CMP $0, R8
47
+ BNE byte_loop // unaligned a, use byte-wise compare (TODO: try to align a)
48
+ aligned_a:
49
+ AND $3, R3, R8
50
+ CMP $0, R8
51
+ BNE byte_loop // unaligned b, use byte-wise compare
52
+ AND $0xfffffffc, R6, R8
53
+ // length >= 4
54
+ chunk4_loop:
55
+ MOVW.P 4(R2), R4
56
+ MOVW.P 4(R3), R5
57
+ CMP R4, R5
58
+ BNE cmp
59
+ CMP R2, R8
60
+ BNE chunk4_loop
61
+ CMP R2, R6
62
+ BEQ samebytes // all compared bytes were the same; compare lengths
63
+ byte_loop:
64
+ MOVBU.P 1(R2), R4
65
+ MOVBU.P 1(R3), R5
66
+ CMP R4, R5
67
+ BNE ret
68
+ CMP R2, R6
69
+ BNE byte_loop
70
+ samebytes:
71
+ CMP R0, R1
72
+ MOVW.LT $1, R0
73
+ MOVW.GT $-1, R0
74
+ MOVW.EQ $0, R0
75
+ MOVW R0, (R7)
76
+ RET
77
+ ret:
78
+ // bytes differed
79
+ MOVW.LT $1, R0
80
+ MOVW.GT $-1, R0
81
+ MOVW R0, (R7)
82
+ RET
83
+ cmp:
84
+ SUB $4, R2, R2
85
+ SUB $4, R3, R3
86
+ B byte_loop
go/src/internal/bytealg/compare_arm64.s ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 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
+ #include "go_asm.h"
6
+ #include "textflag.h"
7
+
8
+ TEXT ·Compare<ABIInternal>(SB),NOSPLIT|NOFRAME,$0-56
9
+ // R0 = a_base (want in R0)
10
+ // R1 = a_len (want in R1)
11
+ // R2 = a_cap (unused)
12
+ // R3 = b_base (want in R2)
13
+ // R4 = b_len (want in R3)
14
+ // R5 = b_cap (unused)
15
+ MOVD R3, R2
16
+ MOVD R4, R3
17
+ B cmpbody<>(SB)
18
+
19
+ TEXT runtime·cmpstring<ABIInternal>(SB),NOSPLIT|NOFRAME,$0-40
20
+ // R0 = a_base
21
+ // R1 = a_len
22
+ // R2 = b_base
23
+ // R3 = b_len
24
+ B cmpbody<>(SB)
25
+
26
+ // On entry:
27
+ // R0 points to the start of a
28
+ // R1 is the length of a
29
+ // R2 points to the start of b
30
+ // R3 is the length of b
31
+ //
32
+ // On exit:
33
+ // R0 is the result
34
+ // R4, R5, R6, R8, R9 and R10 are clobbered
35
+ TEXT cmpbody<>(SB),NOSPLIT|NOFRAME,$0-0
36
+ CMP R0, R2
37
+ BEQ samebytes // same starting pointers; compare lengths
38
+ CMP R1, R3
39
+ CSEL LT, R3, R1, R6 // R6 is min(R1, R3)
40
+
41
+ CBZ R6, samebytes
42
+ BIC $0xf, R6, R10
43
+ CBZ R10, small // length < 16
44
+ ADD R0, R10 // end of chunk16
45
+ // length >= 16
46
+ chunk16_loop:
47
+ LDP.P 16(R0), (R4, R8)
48
+ LDP.P 16(R2), (R5, R9)
49
+ CMP R4, R5
50
+ BNE cmp
51
+ CMP R8, R9
52
+ BNE cmpnext
53
+ CMP R10, R0
54
+ BNE chunk16_loop
55
+ AND $0xf, R6, R6
56
+ CBZ R6, samebytes
57
+ SUBS $8, R6
58
+ BLT tail
59
+ // the length of tail > 8 bytes
60
+ MOVD.P 8(R0), R4
61
+ MOVD.P 8(R2), R5
62
+ CMP R4, R5
63
+ BNE cmp
64
+ SUB $8, R6
65
+ // compare last 8 bytes
66
+ tail:
67
+ MOVD (R0)(R6), R4
68
+ MOVD (R2)(R6), R5
69
+ CMP R4, R5
70
+ BEQ samebytes
71
+ cmp:
72
+ REV R4, R4
73
+ REV R5, R5
74
+ CMP R4, R5
75
+ ret:
76
+ MOVD $1, R0
77
+ CNEG HI, R0, R0
78
+ RET
79
+ small:
80
+ TBZ $3, R6, lt_8
81
+ MOVD (R0), R4
82
+ MOVD (R2), R5
83
+ CMP R4, R5
84
+ BNE cmp
85
+ SUBS $8, R6
86
+ BEQ samebytes
87
+ ADD $8, R0
88
+ ADD $8, R2
89
+ SUB $8, R6
90
+ B tail
91
+ lt_8:
92
+ TBZ $2, R6, lt_4
93
+ MOVWU (R0), R4
94
+ MOVWU (R2), R5
95
+ CMPW R4, R5
96
+ BNE cmp
97
+ SUBS $4, R6
98
+ BEQ samebytes
99
+ ADD $4, R0
100
+ ADD $4, R2
101
+ lt_4:
102
+ TBZ $1, R6, lt_2
103
+ MOVHU (R0), R4
104
+ MOVHU (R2), R5
105
+ CMPW R4, R5
106
+ BNE cmp
107
+ ADD $2, R0
108
+ ADD $2, R2
109
+ lt_2:
110
+ TBZ $0, R6, samebytes
111
+ one:
112
+ MOVBU (R0), R4
113
+ MOVBU (R2), R5
114
+ CMPW R4, R5
115
+ BNE ret
116
+ samebytes:
117
+ CMP R3, R1
118
+ CSET NE, R0
119
+ CNEG LO, R0, R0
120
+ RET
121
+ cmpnext:
122
+ REV R8, R4
123
+ REV R9, R5
124
+ CMP R4, R5
125
+ B ret
go/src/internal/bytealg/compare_generic.go ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 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
+ //go:build !386 && !amd64 && !s390x && !arm && !arm64 && !loong64 && !ppc64 && !ppc64le && !mips && !mipsle && !wasm && !mips64 && !mips64le && !riscv64
6
+
7
+ package bytealg
8
+
9
+ import _ "unsafe" // for go:linkname
10
+
11
+ func Compare(a, b []byte) int {
12
+ l := len(a)
13
+ if len(b) < l {
14
+ l = len(b)
15
+ }
16
+ if l == 0 || &a[0] == &b[0] {
17
+ goto samebytes
18
+ }
19
+ for i := 0; i < l; i++ {
20
+ c1, c2 := a[i], b[i]
21
+ if c1 < c2 {
22
+ return -1
23
+ }
24
+ if c1 > c2 {
25
+ return +1
26
+ }
27
+ }
28
+ samebytes:
29
+ if len(a) < len(b) {
30
+ return -1
31
+ }
32
+ if len(a) > len(b) {
33
+ return +1
34
+ }
35
+ return 0
36
+ }
37
+
38
+ func CompareString(a, b string) int {
39
+ return runtime_cmpstring(a, b)
40
+ }
41
+
42
+ // runtime.cmpstring calls are emitted by the compiler.
43
+ //
44
+ // runtime.cmpstring should be an internal detail,
45
+ // but widely used packages access it using linkname.
46
+ // Notable members of the hall of shame include:
47
+ // - gitee.com/zhaochuninhefei/gmgo
48
+ // - github.com/bytedance/gopkg
49
+ // - github.com/songzhibin97/gkit
50
+ //
51
+ // Do not remove or change the type signature.
52
+ // See go.dev/issue/67401.
53
+ //
54
+ //go:linkname runtime_cmpstring runtime.cmpstring
55
+ func runtime_cmpstring(a, b string) int {
56
+ l := len(a)
57
+ if len(b) < l {
58
+ l = len(b)
59
+ }
60
+ for i := 0; i < l; i++ {
61
+ c1, c2 := a[i], b[i]
62
+ if c1 < c2 {
63
+ return -1
64
+ }
65
+ if c1 > c2 {
66
+ return +1
67
+ }
68
+ }
69
+ if len(a) < len(b) {
70
+ return -1
71
+ }
72
+ if len(a) > len(b) {
73
+ return +1
74
+ }
75
+ return 0
76
+ }
go/src/internal/bytealg/compare_loong64.s ADDED
@@ -0,0 +1,363 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ #include "go_asm.h"
6
+ #include "textflag.h"
7
+
8
+ TEXT ·Compare<ABIInternal>(SB),NOSPLIT,$0-56
9
+ // R4 = a_base
10
+ // R5 = a_len
11
+ // R6 = a_cap (unused)
12
+ // R7 = b_base (want in R6)
13
+ // R8 = b_len (want in R7)
14
+ // R9 = b_cap (unused)
15
+ MOVV R7, R6
16
+ MOVV R8, R7
17
+ JMP cmpbody<>(SB)
18
+
19
+ TEXT runtime·cmpstring<ABIInternal>(SB),NOSPLIT,$0-40
20
+ // R4 = a_base
21
+ // R5 = a_len
22
+ // R6 = b_base
23
+ // R7 = b_len
24
+ JMP cmpbody<>(SB)
25
+
26
+ // input:
27
+ // R4: points to the start of a
28
+ // R5: length of a
29
+ // R6: points to the start of b
30
+ // R7: length of b
31
+ // for regabi the return value (-1/0/1) in R4
32
+ TEXT cmpbody<>(SB),NOSPLIT|NOFRAME,$0
33
+ BEQ R4, R6, cmp_len // same start of a and b, then compare lengths
34
+
35
+ SGTU R5, R7, R9
36
+ BNE R9, b_lt_a
37
+ MOVV R5, R14
38
+ JMP entry
39
+
40
+ b_lt_a:
41
+ MOVV R7, R14
42
+
43
+ entry:
44
+ BEQ R14, cmp_len // minlength is 0
45
+
46
+ MOVV $32, R15
47
+ BGE R14, R15, lasx
48
+ tail:
49
+ MOVV $8, R15
50
+ BLT R14, R15, lt_8
51
+ generic8_loop:
52
+ MOVV (R4), R10
53
+ MOVV (R6), R11
54
+ BEQ R10, R11, generic8_equal
55
+
56
+ cmp8:
57
+ AND $0xff, R10, R16
58
+ AND $0xff, R11, R17
59
+ BNE R16, R17, cmp_byte
60
+
61
+ BSTRPICKV $15, R10, $8, R16
62
+ BSTRPICKV $15, R11, $8, R17
63
+ BNE R16, R17, cmp_byte
64
+
65
+ BSTRPICKV $23, R10, $16, R16
66
+ BSTRPICKV $23, R11, $16, R17
67
+ BNE R16, R17, cmp_byte
68
+
69
+ BSTRPICKV $31, R10, $24, R16
70
+ BSTRPICKV $31, R11, $24, R17
71
+ BNE R16, R17, cmp_byte
72
+
73
+ BSTRPICKV $39, R10, $32, R16
74
+ BSTRPICKV $39, R11, $32, R17
75
+ BNE R16, R17, cmp_byte
76
+
77
+ BSTRPICKV $47, R10, $40, R16
78
+ BSTRPICKV $47, R11, $40, R17
79
+ BNE R16, R17, cmp_byte
80
+
81
+ BSTRPICKV $55, R10, $48, R16
82
+ BSTRPICKV $55, R11, $48, R17
83
+ BNE R16, R17, cmp_byte
84
+
85
+ BSTRPICKV $63, R10, $56, R16
86
+ BSTRPICKV $63, R11, $56, R17
87
+ BNE R16, R17, cmp_byte
88
+
89
+ generic8_equal:
90
+ ADDV $-8, R14
91
+ BEQ R14, cmp_len
92
+ ADDV $8, R4
93
+ ADDV $8, R6
94
+ BGE R14, R15, generic8_loop
95
+
96
+ lt_8:
97
+ MOVV $4, R15
98
+ BLT R14, R15, lt_4
99
+
100
+ MOVWU (R4), R10
101
+ MOVWU (R6), R11
102
+ BEQ R10, R11, lt_8_equal
103
+
104
+ AND $0xff, R10, R16
105
+ AND $0xff, R11, R17
106
+ BNE R16, R17, cmp_byte
107
+
108
+ BSTRPICKV $15, R10, $8, R16
109
+ BSTRPICKV $15, R11, $8, R17
110
+ BNE R16, R17, cmp_byte
111
+
112
+ BSTRPICKV $23, R10, $16, R16
113
+ BSTRPICKV $23, R11, $16, R17
114
+ BNE R16, R17, cmp_byte
115
+
116
+ BSTRPICKV $31, R10, $24, R16
117
+ BSTRPICKV $31, R11, $24, R17
118
+ BNE R16, R17, cmp_byte
119
+
120
+ lt_8_equal:
121
+ ADDV $-4, R14
122
+ BEQ R14, cmp_len
123
+ ADDV $4, R4
124
+ ADDV $4, R6
125
+
126
+ lt_4:
127
+ MOVV $2, R15
128
+ BLT R14, R15, lt_2
129
+
130
+ MOVHU (R4), R10
131
+ MOVHU (R6), R11
132
+ BEQ R10, R11, lt_4_equal
133
+
134
+ AND $0xff, R10, R16
135
+ AND $0xff, R11, R17
136
+ BNE R16, R17, cmp_byte
137
+
138
+ BSTRPICKV $15, R10, $8, R16
139
+ BSTRPICKV $15, R11, $8, R17
140
+ BNE R16, R17, cmp_byte
141
+
142
+ lt_4_equal:
143
+ ADDV $-2, R14
144
+ BEQ R14, cmp_len
145
+ ADDV $2, R4
146
+ ADDV $2, R6
147
+
148
+ lt_2:
149
+ MOVBU (R4), R16
150
+ MOVBU (R6), R17
151
+ BNE R16, R17, cmp_byte
152
+ JMP cmp_len
153
+
154
+ // Compare 1 byte taken from R16/R17 that are known to differ.
155
+ cmp_byte:
156
+ SGTU R16, R17, R4 // R4 = 1 if (R16 > R17)
157
+ BNE R0, R4, ret
158
+ MOVV $-1, R4
159
+ RET
160
+
161
+ cmp_len:
162
+ SGTU R5, R7, R8
163
+ SGTU R7, R5, R9
164
+ SUBV R9, R8, R4
165
+
166
+ ret:
167
+ RET
168
+
169
+ lasx:
170
+ MOVV $64, R20
171
+ MOVBU internal∕cpu·Loong64+const_offsetLOONG64HasLASX(SB), R9
172
+ BEQ R9, lsx
173
+
174
+ MOVV $128, R15
175
+ BLT R14, R15, lasx32_loop
176
+ lasx128_loop:
177
+ XVMOVQ (R4), X0
178
+ XVMOVQ (R6), X1
179
+ XVSEQB X0, X1, X0
180
+ XVSETANYEQB X0, FCC0
181
+ BFPT lasx_found_0
182
+
183
+ XVMOVQ 32(R4), X0
184
+ XVMOVQ 32(R6), X1
185
+ XVSEQB X0, X1, X0
186
+ XVSETANYEQB X0, FCC0
187
+ BFPT lasx_found_32
188
+
189
+ XVMOVQ 64(R4), X0
190
+ XVMOVQ 64(R6), X1
191
+ XVSEQB X0, X1, X0
192
+ XVSETANYEQB X0, FCC0
193
+ BFPT lasx_found_64
194
+
195
+ XVMOVQ 96(R4), X0
196
+ XVMOVQ 96(R6), X1
197
+ XVSEQB X0, X1, X0
198
+ XVSETANYEQB X0, FCC0
199
+ BFPT lasx_found_96
200
+
201
+ ADDV $-128, R14
202
+ BEQ R14, cmp_len
203
+ ADDV $128, R4
204
+ ADDV $128, R6
205
+ BGE R14, R15, lasx128_loop
206
+
207
+ MOVV $32, R15
208
+ BLT R14, R15, tail
209
+ lasx32_loop:
210
+ XVMOVQ (R4), X0
211
+ XVMOVQ (R6), X1
212
+ XVSEQB X0, X1, X0
213
+ XVSETANYEQB X0, FCC0
214
+ BFPT lasx_found_0
215
+
216
+ ADDV $-32, R14
217
+ BEQ R14, cmp_len
218
+ ADDV $32, R4
219
+ ADDV $32, R6
220
+ BGE R14, R15, lasx32_loop
221
+ JMP tail
222
+
223
+ lasx_found_0:
224
+ MOVV R0, R11
225
+ JMP lasx_find_byte
226
+
227
+ lasx_found_32:
228
+ MOVV $32, R11
229
+ JMP lasx_find_byte
230
+
231
+ lasx_found_64:
232
+ MOVV $64, R11
233
+ JMP lasx_find_byte
234
+
235
+ lasx_found_96:
236
+ MOVV $96, R11
237
+
238
+ lasx_find_byte:
239
+ XVMOVQ X0.V[0], R10
240
+ CTOV R10, R10
241
+ BNE R10, R20, find_byte
242
+ ADDV $8, R11
243
+
244
+ XVMOVQ X0.V[1], R10
245
+ CTOV R10, R10
246
+ BNE R10, R20, find_byte
247
+ ADDV $8, R11
248
+
249
+ XVMOVQ X0.V[2], R10
250
+ CTOV R10, R10
251
+ BNE R10, R20, find_byte
252
+ ADDV $8, R11
253
+
254
+ XVMOVQ X0.V[3], R10
255
+ CTOV R10, R10
256
+ JMP find_byte
257
+
258
+ lsx:
259
+ MOVBU internal∕cpu·Loong64+const_offsetLOONG64HasLSX(SB), R9
260
+ BEQ R9, generic32_loop
261
+
262
+ MOVV $64, R15
263
+ BLT R14, R15, lsx16_loop
264
+ lsx64_loop:
265
+ VMOVQ (R4), V0
266
+ VMOVQ (R6), V1
267
+ VSEQB V0, V1, V0
268
+ VSETANYEQB V0, FCC0
269
+ BFPT lsx_found_0
270
+
271
+ VMOVQ 16(R4), V0
272
+ VMOVQ 16(R6), V1
273
+ VSEQB V0, V1, V0
274
+ VSETANYEQB V0, FCC0
275
+ BFPT lsx_found_16
276
+
277
+ VMOVQ 32(R4), V0
278
+ VMOVQ 32(R6), V1
279
+ VSEQB V0, V1, V0
280
+ VSETANYEQB V0, FCC0
281
+ BFPT lsx_found_32
282
+
283
+ VMOVQ 48(R4), V0
284
+ VMOVQ 48(R6), V1
285
+ VSEQB V0, V1, V0
286
+ VSETANYEQB V0, FCC0
287
+ BFPT lsx_found_48
288
+
289
+ ADDV $-64, R14
290
+ BEQ R14, cmp_len
291
+ ADDV $64, R4
292
+ ADDV $64, R6
293
+ BGE R14, R15, lsx64_loop
294
+
295
+ MOVV $16, R15
296
+ BLT R14, R15, tail
297
+ lsx16_loop:
298
+ VMOVQ (R4), V0
299
+ VMOVQ (R6), V1
300
+ VSEQB V0, V1, V0
301
+ VSETANYEQB V0, FCC0
302
+ BFPT lsx_found_0
303
+
304
+ ADDV $-16, R14
305
+ BEQ R14, cmp_len
306
+ ADDV $16, R4
307
+ ADDV $16, R6
308
+ BGE R14, R15, lsx16_loop
309
+ JMP tail
310
+
311
+ lsx_found_0:
312
+ MOVV R0, R11
313
+ JMP lsx_find_byte
314
+
315
+ lsx_found_16:
316
+ MOVV $16, R11
317
+ JMP lsx_find_byte
318
+
319
+ lsx_found_32:
320
+ MOVV $32, R11
321
+ JMP lsx_find_byte
322
+
323
+ lsx_found_48:
324
+ MOVV $48, R11
325
+
326
+ lsx_find_byte:
327
+ VMOVQ V0.V[0], R10
328
+ CTOV R10, R10
329
+ BNE R10, R20, find_byte
330
+ ADDV $8, R11
331
+
332
+ VMOVQ V0.V[1], R10
333
+ CTOV R10, R10
334
+
335
+ find_byte:
336
+ SRLV $3, R10
337
+ ADDV R10, R11
338
+ ADDV R11, R4
339
+ ADDV R11, R6
340
+ MOVB (R4), R16
341
+ MOVB (R6), R17
342
+ JMP cmp_byte
343
+
344
+ generic32_loop:
345
+ MOVV (R4), R10
346
+ MOVV (R6), R11
347
+ BNE R10, R11, cmp8
348
+ MOVV 8(R4), R10
349
+ MOVV 8(R6), R11
350
+ BNE R10, R11, cmp8
351
+ MOVV 16(R4), R10
352
+ MOVV 16(R6), R11
353
+ BNE R10, R11, cmp8
354
+ MOVV 24(R4), R10
355
+ MOVV 24(R6), R11
356
+ BNE R10, R11, cmp8
357
+ ADDV $-32, R14
358
+ BEQ R14, cmp_len
359
+ ADDV $32, R4
360
+ ADDV $32, R6
361
+ MOVV $32, R15
362
+ BGE R14, R15, generic32_loop
363
+ JMP tail
go/src/internal/bytealg/compare_mips64x.s ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2019 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
+ //go:build mips64 || mips64le
6
+
7
+ #include "go_asm.h"
8
+ #include "textflag.h"
9
+
10
+ TEXT ·Compare(SB),NOSPLIT,$0-56
11
+ MOVV a_base+0(FP), R3
12
+ MOVV b_base+24(FP), R4
13
+ MOVV a_len+8(FP), R1
14
+ MOVV b_len+32(FP), R2
15
+ MOVV $ret+48(FP), R9
16
+ JMP cmpbody<>(SB)
17
+
18
+ TEXT runtime·cmpstring(SB),NOSPLIT,$0-40
19
+ MOVV a_base+0(FP), R3
20
+ MOVV b_base+16(FP), R4
21
+ MOVV a_len+8(FP), R1
22
+ MOVV b_len+24(FP), R2
23
+ MOVV $ret+32(FP), R9
24
+ JMP cmpbody<>(SB)
25
+
26
+ // On entry:
27
+ // R1 length of a
28
+ // R2 length of b
29
+ // R3 points to the start of a
30
+ // R4 points to the start of b
31
+ // R9 points to the return value (-1/0/1)
32
+ TEXT cmpbody<>(SB),NOSPLIT|NOFRAME,$0
33
+ BEQ R3, R4, samebytes // same start of a and b
34
+
35
+ SGTU R1, R2, R7
36
+ BNE R0, R7, r2_lt_r1
37
+ MOVV R1, R10
38
+ JMP entry
39
+ r2_lt_r1:
40
+ MOVV R2, R10 // R10 is min(R1, R2)
41
+ entry:
42
+ ADDV R3, R10, R8 // R3 start of a, R8 end of a
43
+ BEQ R3, R8, samebytes // length is 0
44
+
45
+ SRLV $4, R10 // R10 is number of chunks
46
+ BEQ R0, R10, byte_loop
47
+
48
+ // make sure both a and b are aligned.
49
+ OR R3, R4, R11
50
+ AND $7, R11
51
+ BNE R0, R11, byte_loop
52
+
53
+ chunk16_loop:
54
+ BEQ R0, R10, byte_loop
55
+ MOVV (R3), R6
56
+ MOVV (R4), R7
57
+ BNE R6, R7, byte_loop
58
+ MOVV 8(R3), R13
59
+ MOVV 8(R4), R14
60
+ ADDV $16, R3
61
+ ADDV $16, R4
62
+ SUBVU $1, R10
63
+ BEQ R13, R14, chunk16_loop
64
+ SUBV $8, R3
65
+ SUBV $8, R4
66
+
67
+ byte_loop:
68
+ BEQ R3, R8, samebytes
69
+ MOVBU (R3), R6
70
+ ADDVU $1, R3
71
+ MOVBU (R4), R7
72
+ ADDVU $1, R4
73
+ BEQ R6, R7, byte_loop
74
+
75
+ byte_cmp:
76
+ SGTU R6, R7, R8 // R8 = 1 if (R6 > R7)
77
+ BNE R0, R8, ret
78
+ MOVV $-1, R8
79
+ JMP ret
80
+
81
+ samebytes:
82
+ SGTU R1, R2, R6
83
+ SGTU R2, R1, R7
84
+ SUBV R7, R6, R8
85
+
86
+ ret:
87
+ MOVV R8, (R9)
88
+ RET
go/src/internal/bytealg/compare_mipsx.s ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 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
+ //go:build mips || mipsle
6
+
7
+ #include "go_asm.h"
8
+ #include "textflag.h"
9
+
10
+ TEXT ·Compare(SB),NOSPLIT,$0-28
11
+ MOVW a_base+0(FP), R3
12
+ MOVW b_base+12(FP), R4
13
+ MOVW a_len+4(FP), R1
14
+ MOVW b_len+16(FP), R2
15
+ BEQ R3, R4, samebytes
16
+ SGTU R1, R2, R7
17
+ MOVW R1, R8
18
+ CMOVN R7, R2, R8 // R8 is min(R1, R2)
19
+
20
+ ADDU R3, R8 // R3 is current byte in a, R8 is last byte in a to compare
21
+ loop:
22
+ BEQ R3, R8, samebytes
23
+
24
+ MOVBU (R3), R6
25
+ ADDU $1, R3
26
+ MOVBU (R4), R7
27
+ ADDU $1, R4
28
+ BEQ R6, R7 , loop
29
+
30
+ SGTU R6, R7, R8
31
+ MOVW $-1, R6
32
+ CMOVZ R8, R6, R8
33
+ JMP cmp_ret
34
+ samebytes:
35
+ SGTU R1, R2, R6
36
+ SGTU R2, R1, R7
37
+ SUBU R7, R6, R8
38
+ cmp_ret:
39
+ MOVW R8, ret+24(FP)
40
+ RET
41
+
42
+ TEXT runtime·cmpstring(SB),NOSPLIT,$0-20
43
+ MOVW a_base+0(FP), R3
44
+ MOVW a_len+4(FP), R1
45
+ MOVW b_base+8(FP), R4
46
+ MOVW b_len+12(FP), R2
47
+ BEQ R3, R4, samebytes
48
+ SGTU R1, R2, R7
49
+ MOVW R1, R8
50
+ CMOVN R7, R2, R8 // R8 is min(R1, R2)
51
+
52
+ ADDU R3, R8 // R3 is current byte in a, R8 is last byte in a to compare
53
+ loop:
54
+ BEQ R3, R8, samebytes // all compared bytes were the same; compare lengths
55
+
56
+ MOVBU (R3), R6
57
+ ADDU $1, R3
58
+ MOVBU (R4), R7
59
+ ADDU $1, R4
60
+ BEQ R6, R7 , loop
61
+ // bytes differed
62
+ SGTU R6, R7, R8
63
+ MOVW $-1, R6
64
+ CMOVZ R8, R6, R8
65
+ JMP cmp_ret
66
+ samebytes:
67
+ SGTU R1, R2, R6
68
+ SGTU R2, R1, R7
69
+ SUBU R7, R6, R8
70
+ cmp_ret:
71
+ MOVW R8, ret+16(FP)
72
+ RET
go/src/internal/bytealg/compare_native.go ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 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
+ //go:build 386 || amd64 || s390x || arm || arm64 || loong64 || ppc64 || ppc64le || mips || mipsle || wasm || mips64 || mips64le || riscv64
6
+
7
+ package bytealg
8
+
9
+ import _ "unsafe" // For go:linkname
10
+
11
+ //go:noescape
12
+ func Compare(a, b []byte) int
13
+
14
+ func CompareString(a, b string) int {
15
+ return abigen_runtime_cmpstring(a, b)
16
+ }
17
+
18
+ // The declaration below generates ABI wrappers for functions
19
+ // implemented in assembly in this package but declared in another
20
+ // package.
21
+
22
+ //go:linkname abigen_runtime_cmpstring runtime.cmpstring
23
+ func abigen_runtime_cmpstring(a, b string) int
go/src/internal/bytealg/compare_ppc64x.s ADDED
@@ -0,0 +1,342 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 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
+ //go:build ppc64 || ppc64le
6
+
7
+ #include "go_asm.h"
8
+ #include "textflag.h"
9
+
10
+ // Helper names for x-form loads in BE ordering.
11
+ #ifdef GOARCH_ppc64le
12
+ #define _LDBEX MOVDBR
13
+ #define _LWBEX MOVWBR
14
+ #define _LHBEX MOVHBR
15
+ #else
16
+ #define _LDBEX MOVD
17
+ #define _LWBEX MOVW
18
+ #define _LHBEX MOVH
19
+ #endif
20
+
21
+ #ifdef GOPPC64_power9
22
+ #define SETB_CR0(rout) SETB CR0, rout
23
+ #define SETB_CR1(rout) SETB CR1, rout
24
+ #define SETB_INIT()
25
+ #define SETB_CR0_NE(rout) SETB_CR0(rout)
26
+ #else
27
+ // A helper macro to emulate SETB on P8. This assumes
28
+ // -1 is in R20, and 1 is in R21. crxlt and crxeq must
29
+ // also be the same CR field.
30
+ #define _SETB(crxlt, crxeq, rout) \
31
+ ISEL crxeq,R0,R21,rout \
32
+ ISEL crxlt,R20,rout,rout
33
+
34
+ // A special case when it is know the comparison
35
+ // will always be not equal. The result must be -1 or 1.
36
+ #define SETB_CR0_NE(rout) \
37
+ ISEL CR0LT,R20,R21,rout
38
+
39
+ #define SETB_CR0(rout) _SETB(CR0LT, CR0EQ, rout)
40
+ #define SETB_CR1(rout) _SETB(CR1LT, CR1EQ, rout)
41
+ #define SETB_INIT() \
42
+ MOVD $-1,R20 \
43
+ MOVD $1,R21
44
+ #endif
45
+
46
+ TEXT ·Compare<ABIInternal>(SB),NOSPLIT|NOFRAME,$0-56
47
+ // incoming:
48
+ // R3 a addr
49
+ // R4 a len
50
+ // R6 b addr
51
+ // R7 b len
52
+ //
53
+ // on entry to cmpbody:
54
+ // R3 return value if len(a) == len(b)
55
+ // R5 a addr
56
+ // R6 b addr
57
+ // R9 min(len(a),len(b))
58
+ SETB_INIT()
59
+ MOVD R3,R5
60
+ CMP R4,R7,CR0
61
+ CMP R3,R6,CR7
62
+ ISEL CR0LT,R4,R7,R9
63
+ SETB_CR0(R3)
64
+ BEQ CR7,LR
65
+ BR cmpbody<>(SB)
66
+
67
+ TEXT runtime·cmpstring<ABIInternal>(SB),NOSPLIT|NOFRAME,$0-40
68
+ // incoming:
69
+ // R3 a addr -> R5
70
+ // R4 a len -> R3
71
+ // R5 b addr -> R6
72
+ // R6 b len -> R4
73
+ //
74
+ // on entry to cmpbody:
75
+ // R3 compare value if compared length is same.
76
+ // R5 a addr
77
+ // R6 b addr
78
+ // R9 min(len(a),len(b))
79
+ SETB_INIT()
80
+ CMP R4,R6,CR0
81
+ CMP R3,R5,CR7
82
+ ISEL CR0LT,R4,R6,R9
83
+ MOVD R5,R6
84
+ MOVD R3,R5
85
+ SETB_CR0(R3)
86
+ BEQ CR7,LR
87
+ BR cmpbody<>(SB)
88
+
89
+ #ifdef GOARCH_ppc64le
90
+ DATA byteswap<>+0(SB)/8, $0x0706050403020100
91
+ DATA byteswap<>+8(SB)/8, $0x0f0e0d0c0b0a0908
92
+ GLOBL byteswap<>+0(SB), RODATA, $16
93
+ #define SWAP V21
94
+ #endif
95
+
96
+ TEXT cmpbody<>(SB),NOSPLIT|NOFRAME,$0-0
97
+ start:
98
+ CMP R9,$16,CR0
99
+ CMP R9,$32,CR1
100
+ CMP R9,$64,CR2
101
+ MOVD $16,R10
102
+ BLT cmp8
103
+ BLT CR1,cmp16
104
+ BLT CR2,cmp32
105
+
106
+ cmp64: // >= 64B
107
+ DCBT (R5) // optimize for size>=64
108
+ DCBT (R6) // cache hint
109
+
110
+ SRD $6,R9,R14 // There is at least one iteration.
111
+ MOVD R14,CTR
112
+ ANDCC $63,R9,R9
113
+ CMP R9,$16,CR1 // Do setup for tail check early on.
114
+ CMP R9,$32,CR2
115
+ CMP R9,$48,CR3
116
+ ADD $-16,R9,R9
117
+
118
+ MOVD $32,R11 // set offsets to load into vector
119
+ MOVD $48,R12 // set offsets to load into vector
120
+
121
+ PCALIGN $16
122
+ cmp64_loop:
123
+ LXVD2X (R5)(R0),V3 // load bytes of A at offset 0 into vector
124
+ LXVD2X (R6)(R0),V4 // load bytes of B at offset 0 into vector
125
+ VCMPEQUDCC V3,V4,V1
126
+ BGE CR6,different // jump out if its different
127
+
128
+ LXVD2X (R5)(R10),V3 // load bytes of A at offset 16 into vector
129
+ LXVD2X (R6)(R10),V4 // load bytes of B at offset 16 into vector
130
+ VCMPEQUDCC V3,V4,V1
131
+ BGE CR6,different
132
+
133
+ LXVD2X (R5)(R11),V3 // load bytes of A at offset 32 into vector
134
+ LXVD2X (R6)(R11),V4 // load bytes of B at offset 32 into vector
135
+ VCMPEQUDCC V3,V4,V1
136
+ BGE CR6,different
137
+
138
+ LXVD2X (R5)(R12),V3 // load bytes of A at offset 64 into vector
139
+ LXVD2X (R6)(R12),V4 // load bytes of B at offset 64 into vector
140
+ VCMPEQUDCC V3,V4,V1
141
+ BGE CR6,different
142
+
143
+ ADD $64,R5,R5 // increment to next 64 bytes of A
144
+ ADD $64,R6,R6 // increment to next 64 bytes of B
145
+ BDNZ cmp64_loop
146
+ BEQ CR0,LR // beqlr
147
+
148
+ // Finish out tail with minimal overlapped checking.
149
+ // Note, 0 tail is handled by beqlr above.
150
+ BLE CR1,cmp64_tail_gt0
151
+ BLE CR2,cmp64_tail_gt16
152
+ BLE CR3,cmp64_tail_gt32
153
+
154
+ cmp64_tail_gt48: // 49 - 63 B
155
+ LXVD2X (R0)(R5),V3
156
+ LXVD2X (R0)(R6),V4
157
+ VCMPEQUDCC V3,V4,V1
158
+ BGE CR6,different
159
+
160
+ LXVD2X (R5)(R10),V3
161
+ LXVD2X (R6)(R10),V4
162
+ VCMPEQUDCC V3,V4,V1
163
+ BGE CR6,different
164
+
165
+ LXVD2X (R5)(R11),V3
166
+ LXVD2X (R6)(R11),V4
167
+ VCMPEQUDCC V3,V4,V1
168
+ BGE CR6,different
169
+
170
+ BR cmp64_tail_gt0
171
+
172
+ PCALIGN $16
173
+ cmp64_tail_gt32: // 33 - 48B
174
+ LXVD2X (R0)(R5),V3
175
+ LXVD2X (R0)(R6),V4
176
+ VCMPEQUDCC V3,V4,V1
177
+ BGE CR6,different
178
+
179
+ LXVD2X (R5)(R10),V3
180
+ LXVD2X (R6)(R10),V4
181
+ VCMPEQUDCC V3,V4,V1
182
+ BGE CR6,different
183
+
184
+ BR cmp64_tail_gt0
185
+
186
+ PCALIGN $16
187
+ cmp64_tail_gt16: // 17 - 32B
188
+ LXVD2X (R0)(R5),V3
189
+ LXVD2X (R0)(R6),V4
190
+ VCMPEQUDCC V3,V4,V1
191
+ BGE CR6,different
192
+
193
+ BR cmp64_tail_gt0
194
+
195
+ PCALIGN $16
196
+ cmp64_tail_gt0: // 1 - 16B
197
+ LXVD2X (R5)(R9),V3
198
+ LXVD2X (R6)(R9),V4
199
+ VCMPEQUDCC V3,V4,V1
200
+ BGE CR6,different
201
+
202
+ RET
203
+
204
+ PCALIGN $16
205
+ cmp32: // 32 - 63B
206
+ ANDCC $31,R9,R9
207
+
208
+ LXVD2X (R0)(R5),V3
209
+ LXVD2X (R0)(R6),V4
210
+ VCMPEQUDCC V3,V4,V1
211
+ BGE CR6,different
212
+
213
+ LXVD2X (R10)(R5),V3
214
+ LXVD2X (R10)(R6),V4
215
+ VCMPEQUDCC V3,V4,V1
216
+ BGE CR6,different
217
+
218
+ BEQ CR0,LR
219
+ ADD R9,R10,R10
220
+
221
+ LXVD2X (R9)(R5),V3
222
+ LXVD2X (R9)(R6),V4
223
+ VCMPEQUDCC V3,V4,V1
224
+ BGE CR6,different
225
+
226
+ LXVD2X (R10)(R5),V3
227
+ LXVD2X (R10)(R6),V4
228
+ VCMPEQUDCC V3,V4,V1
229
+ BGE CR6,different
230
+ RET
231
+
232
+ PCALIGN $16
233
+ cmp16: // 16 - 31B
234
+ ANDCC $15,R9,R9
235
+ LXVD2X (R0)(R5),V3
236
+ LXVD2X (R0)(R6),V4
237
+ VCMPEQUDCC V3,V4,V1
238
+ BGE CR6,different
239
+ BEQ CR0,LR
240
+
241
+ LXVD2X (R9)(R5),V3
242
+ LXVD2X (R9)(R6),V4
243
+ VCMPEQUDCC V3,V4,V1
244
+ BGE CR6,different
245
+ RET
246
+
247
+ PCALIGN $16
248
+ different:
249
+ #ifdef GOARCH_ppc64le
250
+ MOVD $byteswap<>+00(SB),R16
251
+ LXVD2X (R16)(R0),SWAP // Set up swap string
252
+
253
+ VPERM V3,V3,SWAP,V3
254
+ VPERM V4,V4,SWAP,V4
255
+ #endif
256
+
257
+ MFVSRD VS35,R16 // move upper doublewords of A and B into GPR for comparison
258
+ MFVSRD VS36,R10
259
+
260
+ CMPU R16,R10
261
+ BEQ lower
262
+ SETB_CR0_NE(R3)
263
+ RET
264
+
265
+ PCALIGN $16
266
+ lower:
267
+ VSLDOI $8,V3,V3,V3 // move lower doublewords of A and B into GPR for comparison
268
+ MFVSRD VS35,R16
269
+ VSLDOI $8,V4,V4,V4
270
+ MFVSRD VS36,R10
271
+
272
+ CMPU R16,R10
273
+ SETB_CR0_NE(R3)
274
+ RET
275
+
276
+ PCALIGN $16
277
+ cmp8: // 8 - 15B (0 - 15B if GOPPC64_power10)
278
+ #ifdef GOPPC64_power10
279
+ SLD $56,R9,R9
280
+ LXVLL R5,R9,V3 // Load bytes starting from MSB to LSB, unused are zero filled.
281
+ LXVLL R6,R9,V4
282
+ VCMPUQ V3,V4,CR0 // Compare as a 128b integer.
283
+ SETB_CR0(R6)
284
+ ISEL CR0EQ,R3,R6,R3 // If equal, length determines the return value.
285
+ RET
286
+ #else
287
+ CMP R9,$8
288
+ BLT cmp4
289
+ ANDCC $7,R9,R9
290
+ _LDBEX (R0)(R5),R10
291
+ _LDBEX (R0)(R6),R11
292
+ _LDBEX (R9)(R5),R12
293
+ _LDBEX (R9)(R6),R14
294
+ CMPU R10,R11,CR0
295
+ SETB_CR0(R5)
296
+ CMPU R12,R14,CR1
297
+ SETB_CR1(R6)
298
+ CRAND CR0EQ,CR1EQ,CR1EQ // If both equal, length determines return value.
299
+ ISEL CR0EQ,R6,R5,R4
300
+ ISEL CR1EQ,R3,R4,R3
301
+ RET
302
+
303
+ PCALIGN $16
304
+ cmp4: // 4 - 7B
305
+ CMP R9,$4
306
+ BLT cmp2
307
+ ANDCC $3,R9,R9
308
+ _LWBEX (R0)(R5),R10
309
+ _LWBEX (R0)(R6),R11
310
+ _LWBEX (R9)(R5),R12
311
+ _LWBEX (R9)(R6),R14
312
+ RLDIMI $32,R10,$0,R12
313
+ RLDIMI $32,R11,$0,R14
314
+ CMPU R12,R14
315
+ BR cmp0
316
+
317
+ PCALIGN $16
318
+ cmp2: // 2 - 3B
319
+ CMP R9,$2
320
+ BLT cmp1
321
+ ANDCC $1,R9,R9
322
+ _LHBEX (R0)(R5),R10
323
+ _LHBEX (R0)(R6),R11
324
+ _LHBEX (R9)(R5),R12
325
+ _LHBEX (R9)(R6),R14
326
+ RLDIMI $32,R10,$0,R12
327
+ RLDIMI $32,R11,$0,R14
328
+ CMPU R12,R14
329
+ BR cmp0
330
+
331
+ PCALIGN $16
332
+ cmp1:
333
+ CMP R9,$0
334
+ BEQ cmp0
335
+ MOVBZ (R5),R10
336
+ MOVBZ (R6),R11
337
+ CMPU R10,R11
338
+ cmp0:
339
+ SETB_CR0(R6)
340
+ ISEL CR0EQ,R3,R6,R3
341
+ RET
342
+ #endif
go/src/internal/bytealg/compare_riscv64.s ADDED
@@ -0,0 +1,259 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ #include "asm_riscv64.h"
6
+ #include "go_asm.h"
7
+ #include "textflag.h"
8
+
9
+ TEXT ·Compare<ABIInternal>(SB),NOSPLIT|NOFRAME,$0-56
10
+ // X10 = a_base
11
+ // X11 = a_len
12
+ // X12 = a_cap (unused)
13
+ // X13 = b_base (want in X12)
14
+ // X14 = b_len (want in X13)
15
+ // X15 = b_cap (unused)
16
+ MOV X13, X12
17
+ MOV X14, X13
18
+ JMP compare<>(SB)
19
+
20
+ TEXT runtime·cmpstring<ABIInternal>(SB),NOSPLIT|NOFRAME,$0-40
21
+ // X10 = a_base
22
+ // X11 = a_len
23
+ // X12 = b_base
24
+ // X13 = b_len
25
+ JMP compare<>(SB)
26
+
27
+ // On entry:
28
+ // X10 points to start of a
29
+ // X11 length of a
30
+ // X12 points to start of b
31
+ // X13 length of b
32
+ // return value in X10 (-1/0/1)
33
+ TEXT compare<>(SB),NOSPLIT|NOFRAME,$0
34
+ BEQ X10, X12, cmp_len
35
+
36
+ MIN X11, X13, X5
37
+ BEQZ X5, cmp_len
38
+
39
+ MOV $16, X6
40
+ BLT X5, X6, check8_unaligned
41
+
42
+ #ifndef hasV
43
+ MOVB internal∕cpu·RISCV64+const_offsetRISCV64HasV(SB), X6
44
+ BEQZ X6, compare_scalar
45
+ #endif
46
+
47
+ // Use vector if not 8 byte aligned.
48
+ OR X10, X12, X6
49
+ AND $7, X6
50
+ BNEZ X6, vector_loop
51
+
52
+ // Use scalar if 8 byte aligned and <= 128 bytes.
53
+ SUB $128, X5, X6
54
+ BLEZ X6, compare_scalar_aligned
55
+
56
+ PCALIGN $16
57
+ vector_loop:
58
+ VSETVLI X5, E8, M8, TA, MA, X6
59
+ VLE8V (X10), V8
60
+ VLE8V (X12), V16
61
+ VMSNEVV V8, V16, V0
62
+ VFIRSTM V0, X7
63
+ BGEZ X7, vector_not_eq
64
+ ADD X6, X10
65
+ ADD X6, X12
66
+ SUB X6, X5
67
+ BNEZ X5, vector_loop
68
+ JMP cmp_len
69
+
70
+ vector_not_eq:
71
+ // Load first differing bytes in X8/X9.
72
+ ADD X7, X10
73
+ ADD X7, X12
74
+ MOVBU (X10), X8
75
+ MOVBU (X12), X9
76
+ JMP cmp
77
+
78
+ compare_scalar:
79
+ MOV $32, X6
80
+ BLT X5, X6, check8_unaligned
81
+
82
+ // Check alignment - if alignment differs we have to do one byte at a time.
83
+ AND $7, X10, X7
84
+ AND $7, X12, X8
85
+ BNE X7, X8, check8_unaligned
86
+ BEQZ X7, compare32
87
+
88
+ // Check one byte at a time until we reach 8 byte alignment.
89
+ SUB X7, X0, X7
90
+ ADD $8, X7, X7
91
+ SUB X7, X5, X5
92
+ align:
93
+ SUB $1, X7
94
+ MOVBU 0(X10), X8
95
+ MOVBU 0(X12), X9
96
+ BNE X8, X9, cmp
97
+ ADD $1, X10
98
+ ADD $1, X12
99
+ BNEZ X7, align
100
+
101
+ compare_scalar_aligned:
102
+ MOV $32, X6
103
+ BLT X5, X6, check16
104
+ compare32:
105
+ MOV 0(X10), X15
106
+ MOV 0(X12), X16
107
+ MOV 8(X10), X17
108
+ MOV 8(X12), X18
109
+ BNE X15, X16, cmp8a
110
+ BNE X17, X18, cmp8b
111
+ MOV 16(X10), X15
112
+ MOV 16(X12), X16
113
+ MOV 24(X10), X17
114
+ MOV 24(X12), X18
115
+ BNE X15, X16, cmp8a
116
+ BNE X17, X18, cmp8b
117
+ ADD $32, X10
118
+ ADD $32, X12
119
+ SUB $32, X5
120
+ BGE X5, X6, compare32
121
+ BEQZ X5, cmp_len
122
+
123
+ check16:
124
+ MOV $16, X6
125
+ BLT X5, X6, check8_unaligned
126
+ compare16:
127
+ MOV 0(X10), X15
128
+ MOV 0(X12), X16
129
+ MOV 8(X10), X17
130
+ MOV 8(X12), X18
131
+ BNE X15, X16, cmp8a
132
+ BNE X17, X18, cmp8b
133
+ ADD $16, X10
134
+ ADD $16, X12
135
+ SUB $16, X5
136
+ BEQZ X5, cmp_len
137
+
138
+ check8_unaligned:
139
+ MOV $8, X6
140
+ BLT X5, X6, check4_unaligned
141
+ compare8_unaligned:
142
+ MOVBU 0(X10), X8
143
+ MOVBU 1(X10), X15
144
+ MOVBU 2(X10), X17
145
+ MOVBU 3(X10), X19
146
+ MOVBU 4(X10), X21
147
+ MOVBU 5(X10), X23
148
+ MOVBU 6(X10), X25
149
+ MOVBU 7(X10), X29
150
+ MOVBU 0(X12), X9
151
+ MOVBU 1(X12), X16
152
+ MOVBU 2(X12), X18
153
+ MOVBU 3(X12), X20
154
+ MOVBU 4(X12), X22
155
+ MOVBU 5(X12), X24
156
+ MOVBU 6(X12), X28
157
+ MOVBU 7(X12), X30
158
+ BNE X8, X9, cmp1a
159
+ BNE X15, X16, cmp1b
160
+ BNE X17, X18, cmp1c
161
+ BNE X19, X20, cmp1d
162
+ BNE X21, X22, cmp1e
163
+ BNE X23, X24, cmp1f
164
+ BNE X25, X28, cmp1g
165
+ BNE X29, X30, cmp1h
166
+ ADD $8, X10
167
+ ADD $8, X12
168
+ SUB $8, X5
169
+ BGE X5, X6, compare8_unaligned
170
+ BEQZ X5, cmp_len
171
+
172
+ check4_unaligned:
173
+ MOV $4, X6
174
+ BLT X5, X6, compare1
175
+ compare4_unaligned:
176
+ MOVBU 0(X10), X8
177
+ MOVBU 1(X10), X15
178
+ MOVBU 2(X10), X17
179
+ MOVBU 3(X10), X19
180
+ MOVBU 0(X12), X9
181
+ MOVBU 1(X12), X16
182
+ MOVBU 2(X12), X18
183
+ MOVBU 3(X12), X20
184
+ BNE X8, X9, cmp1a
185
+ BNE X15, X16, cmp1b
186
+ BNE X17, X18, cmp1c
187
+ BNE X19, X20, cmp1d
188
+ ADD $4, X10
189
+ ADD $4, X12
190
+ SUB $4, X5
191
+ BGE X5, X6, compare4_unaligned
192
+
193
+ compare1:
194
+ BEQZ X5, cmp_len
195
+ MOVBU 0(X10), X8
196
+ MOVBU 0(X12), X9
197
+ BNE X8, X9, cmp
198
+ ADD $1, X10
199
+ ADD $1, X12
200
+ SUB $1, X5
201
+ JMP compare1
202
+
203
+ // Compare 8 bytes of memory in X15/X16 that are known to differ.
204
+ cmp8a:
205
+ MOV X15, X17
206
+ MOV X16, X18
207
+
208
+ // Compare 8 bytes of memory in X17/X18 that are known to differ.
209
+ cmp8b:
210
+ MOV $0xff, X19
211
+ cmp8_loop:
212
+ AND X17, X19, X8
213
+ AND X18, X19, X9
214
+ BNE X8, X9, cmp
215
+ SLLI $8, X19
216
+ JMP cmp8_loop
217
+
218
+ cmp1a:
219
+ SLTU X9, X8, X5
220
+ SLTU X8, X9, X6
221
+ JMP cmp_ret
222
+ cmp1b:
223
+ SLTU X16, X15, X5
224
+ SLTU X15, X16, X6
225
+ JMP cmp_ret
226
+ cmp1c:
227
+ SLTU X18, X17, X5
228
+ SLTU X17, X18, X6
229
+ JMP cmp_ret
230
+ cmp1d:
231
+ SLTU X20, X19, X5
232
+ SLTU X19, X20, X6
233
+ JMP cmp_ret
234
+ cmp1e:
235
+ SLTU X22, X21, X5
236
+ SLTU X21, X22, X6
237
+ JMP cmp_ret
238
+ cmp1f:
239
+ SLTU X24, X23, X5
240
+ SLTU X23, X24, X6
241
+ JMP cmp_ret
242
+ cmp1g:
243
+ SLTU X28, X25, X5
244
+ SLTU X25, X28, X6
245
+ JMP cmp_ret
246
+ cmp1h:
247
+ SLTU X30, X29, X5
248
+ SLTU X29, X30, X6
249
+ JMP cmp_ret
250
+
251
+ cmp_len:
252
+ MOV X11, X8
253
+ MOV X13, X9
254
+ cmp:
255
+ SLTU X9, X8, X5
256
+ SLTU X8, X9, X6
257
+ cmp_ret:
258
+ SUB X5, X6, X10
259
+ RET
go/src/internal/bytealg/compare_s390x.s ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 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
+ #include "go_asm.h"
6
+ #include "textflag.h"
7
+
8
+ TEXT ·Compare<ABIInternal>(SB),NOSPLIT|NOFRAME,$0-56
9
+ #ifndef GOEXPERIMENT_regabiargs
10
+ MOVD a_base+0(FP), R2
11
+ MOVD a_len+8(FP), R3
12
+ MOVD b_base+24(FP), R4
13
+ MOVD b_len+32(FP), R5
14
+ LA ret+48(FP), R6
15
+ #else
16
+ // R2 = a_base
17
+ // R3 = a_len
18
+ // R4 = a_cap (unused)
19
+ // R5 = b_base (want in R4)
20
+ // R6 = b_len (want in R5)
21
+ // R7 = b_cap (unused)
22
+ MOVD R5, R4
23
+ MOVD R6, R5
24
+ #endif
25
+ BR cmpbody<>(SB)
26
+
27
+ TEXT runtime·cmpstring<ABIInternal>(SB),NOSPLIT|NOFRAME,$0-40
28
+ #ifndef GOEXPERIMENT_regabiargs
29
+ MOVD a_base+0(FP), R2
30
+ MOVD a_len+8(FP), R3
31
+ MOVD b_base+16(FP), R4
32
+ MOVD b_len+24(FP), R5
33
+ LA ret+32(FP), R6
34
+ #endif
35
+ // R2 = a_base
36
+ // R3 = a_len
37
+ // R4 = b_base
38
+ // R5 = b_len
39
+
40
+ BR cmpbody<>(SB)
41
+
42
+ // input:
43
+ // R2 = a
44
+ // R3 = alen
45
+ // R4 = b
46
+ // R5 = blen
47
+ // For regabiargs output value( -1/0/1 ) stored in R2
48
+ // For !regabiargs address of output word( stores -1/0/1 ) stored in R6
49
+ TEXT cmpbody<>(SB),NOSPLIT|NOFRAME,$0-0
50
+ CMPBEQ R2, R4, cmplengths
51
+ MOVD R3, R7
52
+ CMPBLE R3, R5, amin
53
+ MOVD R5, R7
54
+ amin:
55
+ CMPBEQ R7, $0, cmplengths
56
+ CMP R7, $256
57
+ BLE tail
58
+ loop:
59
+ CLC $256, 0(R2), 0(R4)
60
+ BGT gt
61
+ BLT lt
62
+ SUB $256, R7
63
+ MOVD $256(R2), R2
64
+ MOVD $256(R4), R4
65
+ CMP R7, $256
66
+ BGT loop
67
+ tail:
68
+ SUB $1, R7
69
+ EXRL $cmpbodyclc<>(SB), R7
70
+ BGT gt
71
+ BLT lt
72
+ cmplengths:
73
+ CMP R3, R5
74
+ BEQ eq
75
+ BLT lt
76
+ gt:
77
+ MOVD $1, R2
78
+ #ifndef GOEXPERIMENT_regabiargs
79
+ MOVD R2, 0(R6)
80
+ #endif
81
+ RET
82
+ lt:
83
+ MOVD $-1, R2
84
+ #ifndef GOEXPERIMENT_regabiargs
85
+ MOVD R2, 0(R6)
86
+ #endif
87
+ RET
88
+ eq:
89
+ MOVD $0, R2
90
+ #ifndef GOEXPERIMENT_regabiargs
91
+ MOVD R2, 0(R6)
92
+ #endif
93
+ RET
94
+
95
+ TEXT cmpbodyclc<>(SB),NOSPLIT|NOFRAME,$0-0
96
+ CLC $1, 0(R2), 0(R4)
97
+ RET
go/src/internal/bytealg/compare_wasm.s ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 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
+ #include "go_asm.h"
6
+ #include "textflag.h"
7
+
8
+ TEXT ·Compare(SB), NOSPLIT, $0-56
9
+ Get SP
10
+ I64Load a_base+0(FP)
11
+ I64Load a_len+8(FP)
12
+ I64Load b_base+24(FP)
13
+ I64Load b_len+32(FP)
14
+ Call cmpbody<>(SB)
15
+ I64Store ret+48(FP)
16
+ RET
17
+
18
+ TEXT runtime·cmpstring(SB), NOSPLIT, $0-40
19
+ Get SP
20
+ I64Load a_base+0(FP)
21
+ I64Load a_len+8(FP)
22
+ I64Load b_base+16(FP)
23
+ I64Load b_len+24(FP)
24
+ Call cmpbody<>(SB)
25
+ I64Store ret+32(FP)
26
+ RET
27
+
28
+ // params: a, alen, b, blen
29
+ // ret: -1/0/1
30
+ TEXT cmpbody<>(SB), NOSPLIT, $0-0
31
+ // len = min(alen, blen)
32
+ Get R1
33
+ Get R3
34
+ Get R1
35
+ Get R3
36
+ I64LtU
37
+ Select
38
+ Set R4
39
+
40
+ Get R0
41
+ I32WrapI64
42
+ Get R2
43
+ I32WrapI64
44
+ Get R4
45
+ I32WrapI64
46
+ Call memcmp<>(SB)
47
+ I64ExtendI32S
48
+ Tee R5
49
+
50
+ I64Eqz
51
+ If
52
+ // check length
53
+ Get R1
54
+ Get R3
55
+ I64Sub
56
+ Set R5
57
+ End
58
+
59
+ I64Const $0
60
+ I64Const $-1
61
+ I64Const $1
62
+ Get R5
63
+ I64Const $0
64
+ I64LtS
65
+ Select
66
+ Get R5
67
+ I64Eqz
68
+ Select
69
+ Return
70
+
71
+ // compiled with emscripten
72
+ // params: a, b, len
73
+ // ret: <0/0/>0
74
+ TEXT memcmp<>(SB), NOSPLIT, $0-0
75
+ Get R2
76
+ If $1
77
+ Loop
78
+ Get R0
79
+ I32Load8S $0
80
+ Tee R3
81
+ Get R1
82
+ I32Load8S $0
83
+ Tee R4
84
+ I32Eq
85
+ If
86
+ Get R0
87
+ I32Const $1
88
+ I32Add
89
+ Set R0
90
+ Get R1
91
+ I32Const $1
92
+ I32Add
93
+ Set R1
94
+ I32Const $0
95
+ Get R2
96
+ I32Const $-1
97
+ I32Add
98
+ Tee R2
99
+ I32Eqz
100
+ BrIf $3
101
+ Drop
102
+ Br $1
103
+ End
104
+ End
105
+ Get R3
106
+ I32Const $255
107
+ I32And
108
+ Get R4
109
+ I32Const $255
110
+ I32And
111
+ I32Sub
112
+ Else
113
+ I32Const $0
114
+ End
115
+ Return
go/src/internal/bytealg/count_amd64.s ADDED
@@ -0,0 +1,229 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 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
+ #include "go_asm.h"
6
+ #include "asm_amd64.h"
7
+ #include "textflag.h"
8
+
9
+ TEXT ·Count(SB),NOSPLIT,$0-40
10
+ #ifndef hasPOPCNT
11
+ CMPB internal∕cpu·X86+const_offsetX86HasPOPCNT(SB), $1
12
+ JEQ 2(PC)
13
+ JMP ·countGeneric(SB)
14
+ #endif
15
+ MOVQ b_base+0(FP), SI
16
+ MOVQ b_len+8(FP), BX
17
+ MOVB c+24(FP), AL
18
+ LEAQ ret+32(FP), R8
19
+ JMP countbody<>(SB)
20
+
21
+ TEXT ·CountString(SB),NOSPLIT,$0-32
22
+ #ifndef hasPOPCNT
23
+ CMPB internal∕cpu·X86+const_offsetX86HasPOPCNT(SB), $1
24
+ JEQ 2(PC)
25
+ JMP ·countGenericString(SB)
26
+ #endif
27
+ MOVQ s_base+0(FP), SI
28
+ MOVQ s_len+8(FP), BX
29
+ MOVB c+16(FP), AL
30
+ LEAQ ret+24(FP), R8
31
+ JMP countbody<>(SB)
32
+
33
+ // input:
34
+ // SI: data
35
+ // BX: data len
36
+ // AL: byte sought
37
+ // R8: address to put result
38
+ // This function requires the POPCNT instruction.
39
+ TEXT countbody<>(SB),NOSPLIT,$0
40
+ // Shuffle X0 around so that each byte contains
41
+ // the character we're looking for.
42
+ MOVD AX, X0
43
+ PUNPCKLBW X0, X0
44
+ PUNPCKLBW X0, X0
45
+ PSHUFL $0, X0, X0
46
+
47
+ CMPQ BX, $16
48
+ JLT small
49
+
50
+ MOVQ $0, R12 // Accumulator
51
+
52
+ MOVQ SI, DI
53
+
54
+ CMPQ BX, $64
55
+ JAE avx2
56
+ sse:
57
+ LEAQ -16(SI)(BX*1), AX // AX = address of last 16 bytes
58
+ JMP sseloopentry
59
+
60
+ PCALIGN $16
61
+ sseloop:
62
+ // Move the next 16-byte chunk of the data into X1.
63
+ MOVOU (DI), X1
64
+ // Compare bytes in X0 to X1.
65
+ PCMPEQB X0, X1
66
+ // Take the top bit of each byte in X1 and put the result in DX.
67
+ PMOVMSKB X1, DX
68
+ // Count number of matching bytes
69
+ POPCNTL DX, DX
70
+ // Accumulate into R12
71
+ ADDQ DX, R12
72
+ // Advance to next block.
73
+ ADDQ $16, DI
74
+ sseloopentry:
75
+ CMPQ DI, AX
76
+ JBE sseloop
77
+
78
+ // Get the number of bytes to consider in the last 16 bytes
79
+ ANDQ $15, BX
80
+ JZ end
81
+
82
+ // Create mask to ignore overlap between previous 16 byte block
83
+ // and the next.
84
+ MOVQ $16,CX
85
+ SUBQ BX, CX
86
+ MOVQ $0xFFFF, R10
87
+ SARQ CL, R10
88
+ SALQ CL, R10
89
+
90
+ // Process the last 16-byte chunk. This chunk may overlap with the
91
+ // chunks we've already searched so we need to mask part of it.
92
+ MOVOU (AX), X1
93
+ PCMPEQB X0, X1
94
+ PMOVMSKB X1, DX
95
+ // Apply mask
96
+ ANDQ R10, DX
97
+ POPCNTL DX, DX
98
+ ADDQ DX, R12
99
+ end:
100
+ MOVQ R12, (R8)
101
+ RET
102
+
103
+ // handle for lengths < 16
104
+ small:
105
+ TESTQ BX, BX
106
+ JEQ endzero
107
+
108
+ // Check if we'll load across a page boundary.
109
+ LEAQ 16(SI), AX
110
+ TESTW $0xff0, AX
111
+ JEQ endofpage
112
+
113
+ // We must ignore high bytes as they aren't part of our slice.
114
+ // Create mask.
115
+ MOVB BX, CX
116
+ MOVQ $1, R10
117
+ SALQ CL, R10
118
+ SUBQ $1, R10
119
+
120
+ // Load data
121
+ MOVOU (SI), X1
122
+ // Compare target byte with each byte in data.
123
+ PCMPEQB X0, X1
124
+ // Move result bits to integer register.
125
+ PMOVMSKB X1, DX
126
+ // Apply mask
127
+ ANDQ R10, DX
128
+ POPCNTL DX, DX
129
+ // Directly return DX, we don't need to accumulate
130
+ // since we have <16 bytes.
131
+ MOVQ DX, (R8)
132
+ RET
133
+ endzero:
134
+ MOVQ $0, (R8)
135
+ RET
136
+
137
+ endofpage:
138
+ // We must ignore low bytes as they aren't part of our slice.
139
+ MOVQ $16,CX
140
+ SUBQ BX, CX
141
+ MOVQ $0xFFFF, R10
142
+ SARQ CL, R10
143
+ SALQ CL, R10
144
+
145
+ // Load data into the high end of X1.
146
+ MOVOU -16(SI)(BX*1), X1
147
+ // Compare target byte with each byte in data.
148
+ PCMPEQB X0, X1
149
+ // Move result bits to integer register.
150
+ PMOVMSKB X1, DX
151
+ // Apply mask
152
+ ANDQ R10, DX
153
+ // Directly return DX, we don't need to accumulate
154
+ // since we have <16 bytes.
155
+ POPCNTL DX, DX
156
+ MOVQ DX, (R8)
157
+ RET
158
+
159
+ avx2:
160
+ #ifndef hasAVX2
161
+ CMPB internal∕cpu·X86+const_offsetX86HasAVX2(SB), $1
162
+ JNE sse
163
+ #endif
164
+ MOVD AX, X0
165
+ LEAQ -64(SI)(BX*1), R11
166
+ LEAQ (SI)(BX*1), R13
167
+ VPBROADCASTB X0, Y1
168
+ PCALIGN $32
169
+ avx2_loop:
170
+ VMOVDQU (DI), Y2
171
+ VMOVDQU 32(DI), Y4
172
+ VPCMPEQB Y1, Y2, Y3
173
+ VPCMPEQB Y1, Y4, Y5
174
+ VPMOVMSKB Y3, DX
175
+ VPMOVMSKB Y5, CX
176
+ POPCNTL DX, DX
177
+ POPCNTL CX, CX
178
+ ADDQ DX, R12
179
+ ADDQ CX, R12
180
+ ADDQ $64, DI
181
+ CMPQ DI, R11
182
+ JLE avx2_loop
183
+
184
+ // If last block is already processed,
185
+ // skip to the end.
186
+ //
187
+ // This check is NOT an optimization; if the input length is a
188
+ // multiple of 64, we must not go through the last leg of the
189
+ // function because the bit shift count passed to SALQ below would
190
+ // be 64, which is outside of the 0-63 range supported by those
191
+ // instructions.
192
+ //
193
+ // Tests in the bytes and strings packages with input lengths that
194
+ // are multiples of 64 will break if this condition were removed.
195
+ CMPQ DI, R13
196
+ JEQ endavx
197
+
198
+ // Load address of the last 64 bytes.
199
+ // There is an overlap with the previous block.
200
+ MOVQ R11, DI
201
+ VMOVDQU (DI), Y2
202
+ VMOVDQU 32(DI), Y4
203
+ VPCMPEQB Y1, Y2, Y3
204
+ VPCMPEQB Y1, Y4, Y5
205
+ VPMOVMSKB Y3, DX
206
+ VPMOVMSKB Y5, CX
207
+ // Exit AVX mode.
208
+ VZEROUPPER
209
+ SALQ $32, CX
210
+ ORQ CX, DX
211
+
212
+ // Create mask to ignore overlap between previous 64 byte block
213
+ // and the next.
214
+ ANDQ $63, BX
215
+ MOVQ $64, CX
216
+ SUBQ BX, CX
217
+ MOVQ $0xFFFFFFFFFFFFFFFF, R10
218
+ SALQ CL, R10
219
+ // Apply mask
220
+ ANDQ R10, DX
221
+ POPCNTQ DX, DX
222
+ ADDQ DX, R12
223
+ MOVQ R12, (R8)
224
+ RET
225
+ endavx:
226
+ // Exit AVX mode.
227
+ VZEROUPPER
228
+ MOVQ R12, (R8)
229
+ RET
go/src/internal/bytealg/count_arm.s ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2019 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
+ #include "go_asm.h"
6
+ #include "textflag.h"
7
+
8
+ TEXT ·Count(SB),NOSPLIT,$0-20
9
+ MOVW b_base+0(FP), R0
10
+ MOVW b_len+4(FP), R1
11
+ MOVBU c+12(FP), R2
12
+ MOVW $ret+16(FP), R7
13
+ B countbytebody<>(SB)
14
+
15
+ TEXT ·CountString(SB),NOSPLIT,$0-16
16
+ MOVW s_base+0(FP), R0
17
+ MOVW s_len+4(FP), R1
18
+ MOVBU c+8(FP), R2
19
+ MOVW $ret+12(FP), R7
20
+ B countbytebody<>(SB)
21
+
22
+ // Input:
23
+ // R0: data
24
+ // R1: data length
25
+ // R2: byte to find
26
+ // R7: address to put result
27
+ //
28
+ // On exit:
29
+ // R4 and R8 are clobbered
30
+ TEXT countbytebody<>(SB),NOSPLIT,$0
31
+ MOVW $0, R8 // R8 = count of byte to search
32
+ CMP $0, R1
33
+ B.EQ done // short path to handle 0-byte case
34
+ ADD R0, R1 // R1 is the end of the range
35
+ byte_loop:
36
+ MOVBU.P 1(R0), R4
37
+ CMP R4, R2
38
+ ADD.EQ $1, R8
39
+ CMP R0, R1
40
+ B.NE byte_loop
41
+ done:
42
+ MOVW R8, (R7)
43
+ RET
go/src/internal/bytealg/count_arm64.s ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 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
+ #include "go_asm.h"
6
+ #include "textflag.h"
7
+
8
+ // func Count(b []byte, c byte) int
9
+ // input:
10
+ // R0: b ptr
11
+ // R1: b len
12
+ // R2: b cap
13
+ // R3: c byte to search
14
+ // return:
15
+ // R0: result
16
+ TEXT ·Count<ABIInternal>(SB),NOSPLIT,$0-40
17
+ MOVD R3, R2
18
+ B ·CountString<ABIInternal>(SB)
19
+
20
+ // func CountString(s string, c byte) int
21
+ // input:
22
+ // R0: s ptr
23
+ // R1: s len
24
+ // R2: c byte to search (due to ABIInternal upper bits can contain junk)
25
+ // return:
26
+ // R0: result
27
+ TEXT ·CountString<ABIInternal>(SB),NOSPLIT,$0-32
28
+ // R11 = count of byte to search
29
+ MOVD $0, R11
30
+ // short path to handle 0-byte case
31
+ CBZ R1, done
32
+ CMP $0x20, R1
33
+ // jump directly to head if length >= 32
34
+ BHS head
35
+ tail:
36
+ // Work with tail shorter than 32 bytes
37
+ MOVBU.P 1(R0), R5
38
+ SUB $1, R1, R1
39
+ CMP R2.UXTB, R5
40
+ CINC EQ, R11, R11
41
+ CBNZ R1, tail
42
+ done:
43
+ MOVD R11, R0
44
+ RET
45
+ PCALIGN $16
46
+ head:
47
+ ANDS $0x1f, R0, R9
48
+ BEQ chunk
49
+ // Work with not 32-byte aligned head
50
+ BIC $0x1f, R0, R3
51
+ ADD $0x20, R3
52
+ PCALIGN $16
53
+ head_loop:
54
+ MOVBU.P 1(R0), R5
55
+ CMP R2.UXTB, R5
56
+ CINC EQ, R11, R11
57
+ SUB $1, R1, R1
58
+ CMP R0, R3
59
+ BNE head_loop
60
+ chunk:
61
+ BIC $0x1f, R1, R9
62
+ // The first chunk can also be the last
63
+ CBZ R9, tail
64
+ // R3 = end of 32-byte chunks
65
+ ADD R0, R9, R3
66
+ MOVD $1, R5
67
+ VMOV R5, V5.B16
68
+ // R1 = length of tail
69
+ SUB R9, R1, R1
70
+ // Duplicate R2 (byte to search) to 16 1-byte elements of V0
71
+ VMOV R2, V0.B16
72
+ // Clear the low 64-bit element of V7 and V8
73
+ VEOR V7.B8, V7.B8, V7.B8
74
+ VEOR V8.B8, V8.B8, V8.B8
75
+ PCALIGN $16
76
+ // Count the target byte in 32-byte chunk
77
+ chunk_loop:
78
+ VLD1.P (R0), [V1.B16, V2.B16]
79
+ CMP R0, R3
80
+ VCMEQ V0.B16, V1.B16, V3.B16
81
+ VCMEQ V0.B16, V2.B16, V4.B16
82
+ // Clear the higher 7 bits
83
+ VAND V5.B16, V3.B16, V3.B16
84
+ VAND V5.B16, V4.B16, V4.B16
85
+ // Count lanes match the requested byte
86
+ VADDP V4.B16, V3.B16, V6.B16 // 32B->16B
87
+ VUADDLV V6.B16, V7
88
+ // Accumulate the count in low 64-bit element of V8 when inside the loop
89
+ VADD V7, V8
90
+ BNE chunk_loop
91
+ VMOV V8.D[0], R6
92
+ ADD R6, R11, R11
93
+ CBZ R1, done
94
+ B tail