code generate a single compiler intrinsics table

This simplifies the code generator and global variables a bit.
pull/871/head
Daniel Martí 8 months ago
parent 2259abb89f
commit b49a13c556

@ -1,6 +1,6 @@
// Code generated by scripts/gen_go_std_tables.go; DO NOT EDIT. // Code generated by scripts/gen_go_std_tables.go; DO NOT EDIT.
// Generated from Go version go1.22.0. // Generated from Go version go1.22.6.
package main package main
@ -53,148 +53,152 @@ var runtimeLinknamed = []string{
"net", "net",
} }
var compilerIntrinsicsPkgs = map[string]bool{ var compilerIntrinsics = map[string]map[string]bool{
"math": true, "math": {
"math/big": true, "Abs": true,
"math/bits": true, "Ceil": true,
"runtime": true, "Copysign": true,
"runtime/internal/atomic": true, "FMA": true,
"runtime/internal/math": true, "Floor": true,
"runtime/internal/sys": true, "Round": true,
"sync": true, "RoundToEven": true,
"sync/atomic": true, "Trunc": true,
} "sqrt": true,
},
var compilerIntrinsicsFuncs = map[string]bool{ "math/big": {
"math.Abs": true, "mulWW": true,
"math.Ceil": true, },
"math.Copysign": true, "math/bits": {
"math.FMA": true, "Add": true,
"math.Floor": true, "Add64": true,
"math.Round": true, "Div": true,
"math.RoundToEven": true, "Div64": true,
"math.Trunc": true, "Len": true,
"math.sqrt": true, "Len16": true,
"math/big.mulWW": true, "Len32": true,
"math/bits.Add": true, "Len64": true,
"math/bits.Add64": true, "Len8": true,
"math/bits.Div": true, "Mul": true,
"math/bits.Div64": true, "Mul64": true,
"math/bits.Len": true, "OnesCount": true,
"math/bits.Len16": true, "OnesCount16": true,
"math/bits.Len32": true, "OnesCount32": true,
"math/bits.Len64": true, "OnesCount64": true,
"math/bits.Len8": true, "OnesCount8": true,
"math/bits.Mul": true, "Reverse": true,
"math/bits.Mul64": true, "Reverse16": true,
"math/bits.OnesCount": true, "Reverse32": true,
"math/bits.OnesCount16": true, "Reverse64": true,
"math/bits.OnesCount32": true, "Reverse8": true,
"math/bits.OnesCount64": true, "ReverseBytes16": true,
"math/bits.OnesCount8": true, "ReverseBytes32": true,
"math/bits.Reverse": true, "ReverseBytes64": true,
"math/bits.Reverse16": true, "RotateLeft": true,
"math/bits.Reverse32": true, "RotateLeft16": true,
"math/bits.Reverse64": true, "RotateLeft32": true,
"math/bits.Reverse8": true, "RotateLeft64": true,
"math/bits.ReverseBytes16": true, "RotateLeft8": true,
"math/bits.ReverseBytes32": true, "Sub": true,
"math/bits.ReverseBytes64": true, "Sub64": true,
"math/bits.RotateLeft": true, "TrailingZeros16": true,
"math/bits.RotateLeft16": true, "TrailingZeros32": true,
"math/bits.RotateLeft32": true, "TrailingZeros64": true,
"math/bits.RotateLeft64": true, "TrailingZeros8": true,
"math/bits.RotateLeft8": true, },
"math/bits.Sub": true, "runtime": {
"math/bits.Sub64": true, "publicationBarrier": true,
"math/bits.TrailingZeros16": true, },
"math/bits.TrailingZeros32": true, "runtime/internal/atomic": {
"math/bits.TrailingZeros64": true, "And": true,
"math/bits.TrailingZeros8": true, "And8": true,
"runtime.publicationBarrier": true, "Cas": true,
"runtime/internal/atomic.And": true, "Cas64": true,
"runtime/internal/atomic.And8": true, "CasRel": true,
"runtime/internal/atomic.Cas": true, "Casint32": true,
"runtime/internal/atomic.Cas64": true, "Casint64": true,
"runtime/internal/atomic.CasRel": true, "Casp1": true,
"runtime/internal/atomic.Casint32": true, "Casuintptr": true,
"runtime/internal/atomic.Casint64": true, "Load": true,
"runtime/internal/atomic.Casp1": true, "Load64": true,
"runtime/internal/atomic.Casuintptr": true, "Load8": true,
"runtime/internal/atomic.Load": true, "LoadAcq": true,
"runtime/internal/atomic.Load64": true, "LoadAcq64": true,
"runtime/internal/atomic.Load8": true, "LoadAcquintptr": true,
"runtime/internal/atomic.LoadAcq": true, "Loadint32": true,
"runtime/internal/atomic.LoadAcq64": true, "Loadint64": true,
"runtime/internal/atomic.LoadAcquintptr": true, "Loadp": true,
"runtime/internal/atomic.Loadint32": true, "Loaduint": true,
"runtime/internal/atomic.Loadint64": true, "Loaduintptr": true,
"runtime/internal/atomic.Loadp": true, "Or": true,
"runtime/internal/atomic.Loaduint": true, "Or8": true,
"runtime/internal/atomic.Loaduintptr": true, "Store": true,
"runtime/internal/atomic.Or": true, "Store64": true,
"runtime/internal/atomic.Or8": true, "Store8": true,
"runtime/internal/atomic.Store": true, "StoreRel": true,
"runtime/internal/atomic.Store64": true, "StoreRel64": true,
"runtime/internal/atomic.Store8": true, "StoreReluintptr": true,
"runtime/internal/atomic.StoreRel": true, "Storeint32": true,
"runtime/internal/atomic.StoreRel64": true, "Storeint64": true,
"runtime/internal/atomic.StoreReluintptr": true, "StorepNoWB": true,
"runtime/internal/atomic.Storeint32": true, "Storeuintptr": true,
"runtime/internal/atomic.Storeint64": true, "Xadd": true,
"runtime/internal/atomic.StorepNoWB": true, "Xadd64": true,
"runtime/internal/atomic.Storeuintptr": true, "Xaddint32": true,
"runtime/internal/atomic.Xadd": true, "Xaddint64": true,
"runtime/internal/atomic.Xadd64": true, "Xadduintptr": true,
"runtime/internal/atomic.Xaddint32": true, "Xchg": true,
"runtime/internal/atomic.Xaddint64": true, "Xchg64": true,
"runtime/internal/atomic.Xadduintptr": true, "Xchgint32": true,
"runtime/internal/atomic.Xchg": true, "Xchgint64": true,
"runtime/internal/atomic.Xchg64": true, "Xchguintptr": true,
"runtime/internal/atomic.Xchgint32": true, },
"runtime/internal/atomic.Xchgint64": true, "runtime/internal/math": {
"runtime/internal/atomic.Xchguintptr": true, "MulUintptr": true,
"runtime/internal/math.Add64": true, },
"runtime/internal/math.Mul64": true, "runtime/internal/sys": {
"runtime/internal/math.MulUintptr": true, "Bswap32": true,
"runtime/internal/sys.Bswap32": true, "Bswap64": true,
"runtime/internal/sys.Bswap64": true, "Len64": true,
"runtime/internal/sys.Len64": true, "Len8": true,
"runtime/internal/sys.Len8": true, "OnesCount64": true,
"runtime/internal/sys.OnesCount64": true, "Prefetch": true,
"runtime/internal/sys.Prefetch": true, "PrefetchStreamed": true,
"runtime/internal/sys.PrefetchStreamed": true, "TrailingZeros32": true,
"runtime/internal/sys.TrailingZeros32": true, "TrailingZeros64": true,
"runtime/internal/sys.TrailingZeros64": true, "TrailingZeros8": true,
"runtime/internal/sys.TrailingZeros8": true, },
"sync.runtime_LoadAcquintptr": true, "sync": {
"sync.runtime_StoreReluintptr": true, "runtime_LoadAcquintptr": true,
"sync/atomic.AddInt32": true, "runtime_StoreReluintptr": true,
"sync/atomic.AddInt64": true, },
"sync/atomic.AddUint32": true, "sync/atomic": {
"sync/atomic.AddUint64": true, "AddInt32": true,
"sync/atomic.AddUintptr": true, "AddInt64": true,
"sync/atomic.CompareAndSwapInt32": true, "AddUint32": true,
"sync/atomic.CompareAndSwapInt64": true, "AddUint64": true,
"sync/atomic.CompareAndSwapUint32": true, "AddUintptr": true,
"sync/atomic.CompareAndSwapUint64": true, "CompareAndSwapInt32": true,
"sync/atomic.CompareAndSwapUintptr": true, "CompareAndSwapInt64": true,
"sync/atomic.LoadInt32": true, "CompareAndSwapUint32": true,
"sync/atomic.LoadInt64": true, "CompareAndSwapUint64": true,
"sync/atomic.LoadPointer": true, "CompareAndSwapUintptr": true,
"sync/atomic.LoadUint32": true, "LoadInt32": true,
"sync/atomic.LoadUint64": true, "LoadInt64": true,
"sync/atomic.LoadUintptr": true, "LoadPointer": true,
"sync/atomic.StoreInt32": true, "LoadUint32": true,
"sync/atomic.StoreInt64": true, "LoadUint64": true,
"sync/atomic.StoreUint32": true, "LoadUintptr": true,
"sync/atomic.StoreUint64": true, "StoreInt32": true,
"sync/atomic.StoreUintptr": true, "StoreInt64": true,
"sync/atomic.SwapInt32": true, "StoreUint32": true,
"sync/atomic.SwapInt64": true, "StoreUint64": true,
"sync/atomic.SwapUint32": true, "StoreUintptr": true,
"sync/atomic.SwapUint64": true, "SwapInt32": true,
"sync/atomic.SwapUintptr": true, "SwapInt64": true,
"SwapUint32": true,
"SwapUint64": true,
"SwapUintptr": true,
},
} }
var reflectSkipPkg = map[string]bool{ var reflectSkipPkg = map[string]bool{

@ -869,7 +869,7 @@ func (tf *transformer) replaceAsmNames(buf *bytes.Buffer, remaining []byte) {
name := string(remaining[:nameEnd]) name := string(remaining[:nameEnd])
remaining = remaining[nameEnd:] remaining = remaining[nameEnd:]
if lpkg.ToObfuscate && !compilerIntrinsicsFuncs[lpkg.ImportPath+"."+name] { if lpkg.ToObfuscate && !compilerIntrinsics[lpkg.ImportPath][name] {
newName := hashWithPackage(lpkg, name) newName := hashWithPackage(lpkg, name)
if flagDebug { // TODO(mvdan): remove once https://go.dev/issue/53465 if fixed if flagDebug { // TODO(mvdan): remove once https://go.dev/issue/53465 if fixed
log.Printf("asm name %q hashed with %x to %q", name, tf.curPkg.GarbleActionID, newName) log.Printf("asm name %q hashed with %x to %q", name, tf.curPkg.GarbleActionID, newName)
@ -1107,7 +1107,7 @@ func (tf *transformer) transformDirectives(comments []*ast.CommentGroup) {
func (tf *transformer) transformLinkname(localName, newName string) (string, string) { func (tf *transformer) transformLinkname(localName, newName string) (string, string) {
// obfuscate the local name, if the current package is obfuscated // obfuscate the local name, if the current package is obfuscated
if tf.curPkg.ToObfuscate && !compilerIntrinsicsFuncs[tf.curPkg.ImportPath+"."+localName] { if tf.curPkg.ToObfuscate && !compilerIntrinsics[tf.curPkg.ImportPath][localName] {
localName = hashWithPackage(tf.curPkg, localName) localName = hashWithPackage(tf.curPkg, localName)
} }
if newName == "" { if newName == "" {
@ -1169,7 +1169,7 @@ func (tf *transformer) transformLinkname(localName, newName string) (string, str
panic(err) // shouldn't happen panic(err) // shouldn't happen
} }
if !lpkg.ToObfuscate || compilerIntrinsicsFuncs[lpkg.ImportPath+"."+foreignName] { if !lpkg.ToObfuscate || compilerIntrinsics[lpkg.ImportPath][foreignName] {
// We're not obfuscating that package or name. // We're not obfuscating that package or name.
return localName, newName return localName, newName
} }
@ -2010,7 +2010,7 @@ func (tf *transformer) transformGoFile(file *ast.File) *ast.File {
case *types.TypeName: case *types.TypeName:
debugName = "type" debugName = "type"
case *types.Func: case *types.Func:
if compilerIntrinsicsFuncs[path+"."+name] { if compilerIntrinsics[path][name] {
return true return true
} }

@ -1,6 +1,8 @@
// Copyright (c) 2024, The Garble Authors. // Copyright (c) 2024, The Garble Authors.
// See LICENSE for licensing information. // See LICENSE for licensing information.
//go:build ignore
// This is a program used with `go generate`, so it handles errors via panic. // This is a program used with `go generate`, so it handles errors via panic.
package main package main
@ -40,40 +42,35 @@ var runtimeLinknamed = []string{
"net", "net",
} }
var compilerIntrinsicsPkgs = map[string]bool{ var compilerIntrinsics = map[string]map[string]bool{
{{- range $path := .CompilerIntrinsicsPaths }}
"{{ $path }}": true,
{{- end }}
}
var compilerIntrinsicsFuncs = map[string]bool{
{{- range $intr := .CompilerIntrinsics }} {{- range $intr := .CompilerIntrinsics }}
"{{ $intr.Path }}.{{ $intr.Name }}": true, "{{ $intr.Path }}": {
{{- range $name := $intr.Names }}
"{{ $name }}": true,
{{- end }}
},
{{- end }} {{- end }}
} }
var reflectSkipPkg = map[string]bool{ var reflectSkipPkg = map[string]bool{
"fmt": true, "fmt": true,
} }
`)) `[1:]))
type tmplData struct { type tmplData struct {
GoVersion string GoVersion string
RuntimeAndDeps []string RuntimeAndDeps []string
RuntimeLinknamed []string RuntimeLinknamed []string
CompilerIntrinsics []tmplIntrinsic CompilerIntrinsics []tmplIntrinsic
CompilerIntrinsicsPaths []string
} }
type tmplIntrinsic struct { type tmplIntrinsic struct {
Path, Name string Path string
Names []string
} }
func (t tmplIntrinsic) Compare(t2 tmplIntrinsic) int { func (t tmplIntrinsic) Compare(t2 tmplIntrinsic) int {
if c := cmp.Compare(t.Path, t2.Path); c != 0 { return cmp.Compare(t.Path, t2.Path)
return c
}
return cmp.Compare(t.Name, t2.Name)
} }
func (t tmplIntrinsic) Equal(t2 tmplIntrinsic) bool { func (t tmplIntrinsic) Equal(t2 tmplIntrinsic) bool {
@ -141,8 +138,8 @@ func main() {
return slices.Contains(runtimeAndDeps, path) return slices.Contains(runtimeAndDeps, path)
}) })
compilerIntrinsicsIndexByPath := make(map[string]int)
var compilerIntrinsics []tmplIntrinsic var compilerIntrinsics []tmplIntrinsic
var compilerIntrinsicsPaths []string
for _, line := range strings.Split(readFile(filepath.Join( for _, line := range strings.Split(readFile(filepath.Join(
goroot, "src", "cmd", "compile", "internal", "ssagen", "ssa.go", goroot, "src", "cmd", "compile", "internal", "ssagen", "ssa.go",
)), "\n") { )), "\n") {
@ -150,25 +147,34 @@ func main() {
if m == nil { if m == nil {
continue continue
} }
compilerIntrinsics = append(compilerIntrinsics, tmplIntrinsic{ path, name := m[2], m[3]
Path: m[2], if i := compilerIntrinsicsIndexByPath[path]; i == 0 {
Name: m[3], compilerIntrinsicsIndexByPath[path] = len(compilerIntrinsics)
}) compilerIntrinsics = append(compilerIntrinsics, tmplIntrinsic{
compilerIntrinsicsPaths = append(compilerIntrinsicsPaths, m[2]) Path: path,
Names: []string{name},
})
} else {
compilerIntrinsics[i].Names = append(compilerIntrinsics[i].Names, name)
}
} }
slices.SortFunc(compilerIntrinsics, tmplIntrinsic.Compare) slices.SortFunc(compilerIntrinsics, tmplIntrinsic.Compare)
compilerIntrinsics = slices.CompactFunc(compilerIntrinsics, tmplIntrinsic.Equal) compilerIntrinsics = slices.CompactFunc(compilerIntrinsics, tmplIntrinsic.Equal)
slices.Sort(compilerIntrinsicsPaths) for path := range compilerIntrinsics {
compilerIntrinsicsPaths = slices.Compact(compilerIntrinsicsPaths) intr := &compilerIntrinsics[path]
slices.Sort(intr.Names)
intr.Names = slices.Compact(intr.Names)
}
var buf bytes.Buffer var buf bytes.Buffer
tmplTables.Execute(&buf, tmplData{ if err := tmplTables.Execute(&buf, tmplData{
GoVersion: goversion, GoVersion: goversion,
RuntimeAndDeps: runtimeAndDeps, RuntimeAndDeps: runtimeAndDeps,
RuntimeLinknamed: runtimeLinknamed, RuntimeLinknamed: runtimeLinknamed,
CompilerIntrinsics: compilerIntrinsics, CompilerIntrinsics: compilerIntrinsics,
CompilerIntrinsicsPaths: compilerIntrinsicsPaths, }); err != nil {
}) panic(err)
}
out := buf.Bytes() out := buf.Bytes()
formatted, err := format.Source(out) formatted, err := format.Source(out)
if err != nil { if err != nil {

@ -191,7 +191,7 @@ func (p *listedPackage) obfuscatedImportPath() string {
return p.ImportPath return p.ImportPath
} }
// Intrinsics are matched by package import path as well. // Intrinsics are matched by package import path as well.
if compilerIntrinsicsPkgs[p.ImportPath] { if _, ok := compilerIntrinsics[p.ImportPath]; ok {
return p.ImportPath return p.ImportPath
} }
if !p.ToObfuscate { if !p.ToObfuscate {

Loading…
Cancel
Save