cl: collectSkipNames; processPkg bugfix

This commit is contained in:
xushiwei
2024-06-15 14:56:03 +08:00
parent 7d8bed16b0
commit 994502077a
4 changed files with 52 additions and 23 deletions

View File

@@ -149,9 +149,6 @@ func Nextafter(x, y float64) float64
//go:linkname Pow C.pow //go:linkname Pow C.pow
func Pow(x, y float64) float64 func Pow(x, y float64) float64
//go:linkname Pow10 C.pow10
func Pow10(x c.Int) float64
//go:linkname Remainder C.remainder //go:linkname Remainder C.remainder
func Remainder(x, y float64) float64 func Remainder(x, y float64) float64

View File

@@ -136,6 +136,8 @@ type pkgInfo struct {
kind int kind int
} }
type none struct{}
type context struct { type context struct {
prog llssa.Program prog llssa.Program
pkg llssa.Package pkg llssa.Package
@@ -146,6 +148,7 @@ type context struct {
goPkg *ssa.Package goPkg *ssa.Package
pyMod string pyMod string
link map[string]string // pkgPath.nameInPkg => linkname link map[string]string // pkgPath.nameInPkg => linkname
skips map[string]none
loaded map[*types.Package]*pkgInfo // loaded packages loaded map[*types.Package]*pkgInfo // loaded packages
bvals map[ssa.Value]llssa.Expr // block values bvals map[ssa.Value]llssa.Expr // block values
vargs map[*ssa.Alloc][]llssa.Expr // varargs vargs map[*ssa.Alloc][]llssa.Expr // varargs
@@ -977,6 +980,7 @@ func NewPackageEx(prog llssa.Program, pkg, alt *ssa.Package, files []*ast.File)
goTyps: pkgTypes, goTyps: pkgTypes,
goPkg: pkg, goPkg: pkg,
link: make(map[string]string), link: make(map[string]string),
skips: make(map[string]none),
vargs: make(map[*ssa.Alloc][]llssa.Expr), vargs: make(map[*ssa.Alloc][]llssa.Expr),
loaded: map[*types.Package]*pkgInfo{ loaded: map[*types.Package]*pkgInfo{
types.Unsafe: {kind: PkgDeclOnly}, // TODO(xsw): PkgNoInit or PkgDeclOnly? types.Unsafe: {kind: PkgDeclOnly}, // TODO(xsw): PkgNoInit or PkgDeclOnly?
@@ -989,6 +993,13 @@ func NewPackageEx(prog llssa.Program, pkg, alt *ssa.Package, files []*ast.File)
processPkg(ctx, ret, alt) processPkg(ctx, ret, alt)
} }
processPkg(ctx, ret, pkg) processPkg(ctx, ret, pkg)
for len(ctx.inits) > 0 {
inits := ctx.inits
ctx.inits = nil
for _, ini := range inits {
ini()
}
}
return return
} }
@@ -999,9 +1010,12 @@ func processPkg(ctx *context, ret llssa.Package, pkg *ssa.Package) {
} }
members := make([]*namedMember, 0, len(pkg.Members)) members := make([]*namedMember, 0, len(pkg.Members))
skips := ctx.skips
for name, v := range pkg.Members { for name, v := range pkg.Members {
if _, ok := skips[name]; !ok {
members = append(members, &namedMember{name, v}) members = append(members, &namedMember{name, v})
} }
}
sort.Slice(members, func(i, j int) bool { sort.Slice(members, func(i, j int) bool {
return members[i].name < members[j].name return members[i].name < members[j].name
}) })
@@ -1022,13 +1036,6 @@ func processPkg(ctx *context, ret llssa.Package, pkg *ssa.Package) {
ctx.compileGlobal(ret, member) ctx.compileGlobal(ret, member)
} }
} }
for len(ctx.inits) > 0 {
inits := ctx.inits
ctx.inits = nil
for _, ini := range inits {
ini()
}
}
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------

View File

@@ -168,16 +168,47 @@ func (p *context) initFiles(pkgPath string, files []*ast.File) {
fullName, inPkgName := astFuncName(pkgPath, decl) fullName, inPkgName := astFuncName(pkgPath, decl)
p.initLinknameByDoc(decl.Doc, fullName, inPkgName, false) p.initLinknameByDoc(decl.Doc, fullName, inPkgName, false)
case *ast.GenDecl: case *ast.GenDecl:
if decl.Tok == token.VAR && len(decl.Specs) == 1 { switch decl.Tok {
case token.VAR:
if len(decl.Specs) == 1 {
if names := decl.Specs[0].(*ast.ValueSpec).Names; len(names) == 1 { if names := decl.Specs[0].(*ast.ValueSpec).Names; len(names) == 1 {
inPkgName := names[0].Name inPkgName := names[0].Name
p.initLinknameByDoc(decl.Doc, pkgPath+"."+inPkgName, inPkgName, true) p.initLinknameByDoc(decl.Doc, pkgPath+"."+inPkgName, inPkgName, true)
} }
} }
case token.IMPORT:
if doc := decl.Doc; doc != nil {
if n := len(doc.List); n > 0 {
line := doc.List[n-1].Text
p.collectSkipNames(line)
} }
} }
} }
} }
}
}
}
func (p *context) collectSkipNames(line string) {
const (
skip = "//llgo:skip "
skip2 = "// llgo:skip "
)
if strings.HasPrefix(line, skip2) {
p.collectSkip(line, len(skip2))
} else if strings.HasPrefix(line, skip) {
p.collectSkip(line, len(skip))
}
}
func (p *context) collectSkip(line string, prefix int) {
names := strings.Split(line[prefix:], " ")
for _, name := range names {
if name != "" {
p.skips[name] = none{}
}
}
}
func (p *context) initLinknameByDoc(doc *ast.CommentGroup, fullName, inPkgName string, isVar bool) { func (p *context) initLinknameByDoc(doc *ast.CommentGroup, fullName, inPkgName string, isVar bool) {
if doc != nil { if doc != nil {

View File

@@ -16,6 +16,7 @@
package math package math
// llgo:skip sin cos
import ( import (
_ "unsafe" _ "unsafe"
@@ -173,13 +174,6 @@ func Nextafter(x, y float64) float64
//go:linkname Pow C.pow //go:linkname Pow C.pow
func Pow(x, y float64) float64 func Pow(x, y float64) float64
//go:linkname cPow10 C.pow10
func cPow10(x c.Int) float64
func Pow10(x int) float64 {
return cPow10(c.Int(x))
}
//go:linkname Remainder C.remainder //go:linkname Remainder C.remainder
func Remainder(x, y float64) float64 func Remainder(x, y float64) float64