From 729e5772a035d7876033cbe980085c249b901e67 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Wed, 10 Sep 2025 17:17:21 +0800 Subject: [PATCH] process //export with initLink --- cl/compile.go | 8 -------- cl/import.go | 23 +++++++++-------------- 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/cl/compile.go b/cl/compile.go index fc24b9c2..9521ff14 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -126,7 +126,6 @@ type context struct { cgoArgs []llssa.Expr cgoRet llssa.Expr cgoSymbols []string - cgoExports map[string]string } type pkgState byte @@ -1008,7 +1007,6 @@ func NewPackageEx(prog llssa.Program, patches Patches, pkg *ssa.Package, files [ loaded: map[*types.Package]*pkgInfo{ types.Unsafe: {kind: PkgDeclOnly}, // TODO(xsw): PkgNoInit or PkgDeclOnly? }, - cgoExports: make(map[string]string), cgoSymbols: make([]string, 0, 128), } ctx.initPyModule() @@ -1044,12 +1042,6 @@ func NewPackageEx(prog llssa.Program, patches Patches, pkg *ssa.Package, files [ fn() } externs = ctx.cgoSymbols - for fnName, exportName := range ctx.cgoExports { - fn := ret.FuncOf(fnName) - if fn != nil { - fn.SetName(exportName) - } - } return } diff --git a/cl/import.go b/cl/import.go index 62318981..16aee17a 100644 --- a/cl/import.go +++ b/cl/import.go @@ -310,7 +310,10 @@ func (p *context) initLinkname(line string, f func(inPkgName string) (fullName s p.initLink(line, len(llgolink), f) return hasLinkname } else if strings.HasPrefix(line, export) { - p.initCgoExport(line, len(export), f) + // rewrite //export FuncName to //export FuncName FuncName + funcName := strings.TrimSpace(line[len(export):]) + line = line + " " + funcName + p.initLink(line, len(export), f) return hasLinkname } else if strings.HasPrefix(line, directive) { // skip unknown annotation but continue to parse the next annotation @@ -319,24 +322,13 @@ func (p *context) initLinkname(line string, f func(inPkgName string) (fullName s return noDirective } -func (p *context) initCgoExport(line string, prefix int, f func(inPkgName string) (fullName string, isVar, ok bool)) { - name := strings.TrimSpace(line[prefix:]) - if fullName, _, ok := f(name); ok { - p.cgoExports[fullName] = name // TODO(xsw): why not use prog.SetLinkname? - } -} - func (p *context) initLink(line string, prefix int, f func(inPkgName string) (fullName string, isVar, ok bool)) { text := strings.TrimSpace(line[prefix:]) if idx := strings.IndexByte(text, ' '); idx > 0 { inPkgName := text[:idx] - if fullName, isVar, ok := f(inPkgName); ok { + if fullName, _, ok := f(inPkgName); ok { link := strings.TrimLeft(text[idx+1:], " ") - if isVar || strings.Contains(link, ".") { // eg. C.printf, C.strlen, llgo.cstr - p.prog.SetLinkname(fullName, link) - } else { - p.prog.SetLinkname(fullName, "C."+link) - } + p.prog.SetLinkname(fullName, link) } else { fmt.Fprintln(os.Stderr, "==>", line) fmt.Fprintf(os.Stderr, "llgo: linkname %s not found and ignored\n", inPkgName) @@ -523,6 +515,9 @@ func (p *context) funcName(fn *ssa.Function) (*types.Package, string, int) { if checkCgo(fname) && !cgoIgnored(fname) { return nil, fname, llgoInstr } + if strings.HasPrefix(fname, "_cgoexp_") { + return nil, fname, ignoredFunc + } if isCgoExternSymbol(fn) { if _, ok := llgoInstrs[fname]; ok { return nil, fname, llgoInstr