cl: initLinkname support //llgo:link

This commit is contained in:
xushiwei
2024-05-07 15:35:37 +08:00
parent 1038b06510
commit c93fce87da
5 changed files with 62 additions and 33 deletions

View File

@@ -170,7 +170,7 @@ func (p *context) compileMethods(pkg llssa.Package, typ types.Type) {
for i, n := 0, mthds.Len(); i < n; i++ {
mthd := mthds.At(i)
if ssaMthd := prog.MethodValue(mthd); ssaMthd != nil {
p.compileFuncDecl(pkg, mthd.Obj().Pkg(), ssaMthd)
p.compileFuncDecl(pkg, ssaMthd)
}
}
}
@@ -205,8 +205,8 @@ var (
argvTy = types.NewPointer(types.NewPointer(types.Typ[types.Int8]))
)
func (p *context) compileFuncDecl(pkg llssa.Package, pkgTypes *types.Package, f *ssa.Function) llssa.Function {
name, ftype := p.funcName(pkgTypes, f, true)
func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function) llssa.Function {
pkgTypes, name, ftype := p.funcName(f, true)
if ftype != goFunc {
return nil
}
@@ -271,9 +271,7 @@ func (p *context) compileFuncDecl(pkg llssa.Package, pkgTypes *types.Package, f
// funcOf returns a function by name and set ftype = goFunc, cFunc, etc.
// or returns nil and set ftype = llgoCstr, llgoAlloca, llgoUnreachable, etc.
func (p *context) funcOf(fn *ssa.Function) (ret llssa.Function, ftype int) {
pkgTypes := p.ensureLoaded(fn.Pkg.Pkg)
pkg := p.pkg
name, ftype := p.funcName(pkgTypes, fn, false)
_, name, ftype := p.funcName(fn, false)
if ftype == llgoInstr {
switch name {
case "cstr":
@@ -289,9 +287,12 @@ func (p *context) funcOf(fn *ssa.Function) (ret llssa.Function, ftype int) {
default:
panic("unknown llgo instruction: " + name)
}
} else if ret = pkg.FuncOf(name); ret == nil && len(fn.FreeVars) == 0 {
sig := fn.Signature
ret = pkg.NewFuncEx(name, sig, llssa.Background(ftype), false)
} else {
pkg := p.pkg
if ret = pkg.FuncOf(name); ret == nil && len(fn.FreeVars) == 0 {
sig := fn.Signature
ret = pkg.NewFuncEx(name, sig, llssa.Background(ftype), false)
}
}
return
}
@@ -667,7 +668,7 @@ func (p *context) compileFunction(v *ssa.Function) (llssa.Function, int) {
// v.Pkg == nil: means auto generated function?
if v.Pkg == p.goPkg || v.Pkg == nil {
// function in this package
if fn := p.compileFuncDecl(p.pkg, p.goTyps, v); fn != nil {
if fn := p.compileFuncDecl(p.pkg, v); fn != nil {
return fn, goFunc
}
}
@@ -776,11 +777,12 @@ func NewPackage(prog llssa.Program, pkg *ssa.Package, files []*ast.File) (ret ll
member := m.val
switch member := member.(type) {
case *ssa.Function:
if member.TypeParams() != nil {
if member.TypeParams() != nil || member.TypeArgs() != nil {
// TODO(xsw): don't compile generic functions
// Do not try to build generic (non-instantiated) functions.
continue
}
ctx.compileFuncDecl(ret, member.Pkg.Pkg, member)
ctx.compileFuncDecl(ret, member)
case *ssa.Type:
ctx.compileType(ret, member)
case *ssa.Global: