process //export with initLink

This commit is contained in:
Li Jie
2025-09-10 17:17:21 +08:00
parent a2742a9de4
commit 729e5772a0
2 changed files with 9 additions and 22 deletions

View File

@@ -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
}

View File

@@ -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)
}
} 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