From 994502077af10b07ff440c17ba0a7273cf85741b Mon Sep 17 00:00:00 2001 From: xushiwei Date: Sat, 15 Jun 2024 14:56:03 +0800 Subject: [PATCH] cl: collectSkipNames; processPkg bugfix --- c/math/math.go | 3 --- cl/compile.go | 25 ++++++++++++++++--------- cl/import.go | 39 +++++++++++++++++++++++++++++++++++---- internal/lib/math/math.go | 8 +------- 4 files changed, 52 insertions(+), 23 deletions(-) diff --git a/c/math/math.go b/c/math/math.go index ee6c4932..063acc00 100644 --- a/c/math/math.go +++ b/c/math/math.go @@ -149,9 +149,6 @@ func Nextafter(x, y float64) float64 //go:linkname Pow C.pow func Pow(x, y float64) float64 -//go:linkname Pow10 C.pow10 -func Pow10(x c.Int) float64 - //go:linkname Remainder C.remainder func Remainder(x, y float64) float64 diff --git a/cl/compile.go b/cl/compile.go index 61b97d49..c84e43d3 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -136,6 +136,8 @@ type pkgInfo struct { kind int } +type none struct{} + type context struct { prog llssa.Program pkg llssa.Package @@ -145,7 +147,8 @@ type context struct { goTyps *types.Package goPkg *ssa.Package 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 bvals map[ssa.Value]llssa.Expr // block values 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, goPkg: pkg, link: make(map[string]string), + skips: make(map[string]none), vargs: make(map[*ssa.Alloc][]llssa.Expr), loaded: map[*types.Package]*pkgInfo{ 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, pkg) + for len(ctx.inits) > 0 { + inits := ctx.inits + ctx.inits = nil + for _, ini := range inits { + ini() + } + } return } @@ -999,8 +1010,11 @@ func processPkg(ctx *context, ret llssa.Package, pkg *ssa.Package) { } members := make([]*namedMember, 0, len(pkg.Members)) + skips := ctx.skips for name, v := range pkg.Members { - members = append(members, &namedMember{name, v}) + if _, ok := skips[name]; !ok { + members = append(members, &namedMember{name, v}) + } } sort.Slice(members, func(i, j int) bool { 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) } } - for len(ctx.inits) > 0 { - inits := ctx.inits - ctx.inits = nil - for _, ini := range inits { - ini() - } - } } // ----------------------------------------------------------------------------- diff --git a/cl/import.go b/cl/import.go index a79144d6..970758a2 100644 --- a/cl/import.go +++ b/cl/import.go @@ -168,10 +168,20 @@ func (p *context) initFiles(pkgPath string, files []*ast.File) { fullName, inPkgName := astFuncName(pkgPath, decl) p.initLinknameByDoc(decl.Doc, fullName, inPkgName, false) case *ast.GenDecl: - if decl.Tok == token.VAR && len(decl.Specs) == 1 { - if names := decl.Specs[0].(*ast.ValueSpec).Names; len(names) == 1 { - inPkgName := names[0].Name - p.initLinknameByDoc(decl.Doc, pkgPath+"."+inPkgName, inPkgName, true) + switch decl.Tok { + case token.VAR: + if len(decl.Specs) == 1 { + if names := decl.Specs[0].(*ast.ValueSpec).Names; len(names) == 1 { + inPkgName := names[0].Name + 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) + } } } } @@ -179,6 +189,27 @@ func (p *context) initFiles(pkgPath string, files []*ast.File) { } } +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) { if doc != nil { if n := len(doc.List); n > 0 { diff --git a/internal/lib/math/math.go b/internal/lib/math/math.go index be6f1c8d..09ccdaac 100644 --- a/internal/lib/math/math.go +++ b/internal/lib/math/math.go @@ -16,6 +16,7 @@ package math +// llgo:skip sin cos import ( _ "unsafe" @@ -173,13 +174,6 @@ func Nextafter(x, y float64) float64 //go:linkname Pow C.pow 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 func Remainder(x, y float64) float64