ssa: add llgo:link support to Builder.abiMthd
This commit is contained in:
@@ -282,3 +282,68 @@ func TestErrVarOf(t *testing.T) {
|
||||
g := &ssa.Global{Pkg: ssaPkg}
|
||||
ctx.varOf(nil, g)
|
||||
}
|
||||
|
||||
func TestContextResolveLinkname(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
context *context
|
||||
input string
|
||||
want string
|
||||
panics bool
|
||||
}{
|
||||
{
|
||||
name: "Normal",
|
||||
context: &context{
|
||||
link: map[string]string{
|
||||
"foo": "C.bar",
|
||||
},
|
||||
},
|
||||
input: "foo",
|
||||
want: "bar",
|
||||
},
|
||||
{
|
||||
name: "MultipleLinks",
|
||||
context: &context{
|
||||
link: map[string]string{
|
||||
"foo1": "C.bar1",
|
||||
"foo2": "C.bar2",
|
||||
},
|
||||
},
|
||||
input: "foo2",
|
||||
want: "bar2",
|
||||
},
|
||||
{
|
||||
name: "NoLink",
|
||||
context: &context{link: map[string]string{}},
|
||||
input: "foo",
|
||||
want: "foo",
|
||||
},
|
||||
{
|
||||
name: "InvalidLink",
|
||||
context: &context{
|
||||
link: map[string]string{
|
||||
"foo": "invalid.bar",
|
||||
},
|
||||
},
|
||||
input: "foo",
|
||||
panics: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.panics {
|
||||
defer func() {
|
||||
if r := recover(); r == nil {
|
||||
t.Error("want panic")
|
||||
}
|
||||
}()
|
||||
}
|
||||
got := tt.context.resolveLinkname(tt.input)
|
||||
if !tt.panics {
|
||||
if got != tt.want {
|
||||
t.Errorf("got %q, want %q", got, tt.want)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/goplus/llgo/cl/blocks"
|
||||
"github.com/goplus/llgo/internal/typepatch"
|
||||
@@ -812,6 +813,7 @@ func NewPackageEx(prog llssa.Program, patches Patches, pkg *ssa.Package, files [
|
||||
ctx.initPyModule()
|
||||
ctx.initFiles(pkgPath, files)
|
||||
ret.SetPatch(ctx.patchType)
|
||||
ret.SetResolveLinkname(ctx.resolveLinkname)
|
||||
|
||||
if hasPatch {
|
||||
skips := ctx.skips
|
||||
@@ -904,4 +906,15 @@ func (p *context) patchType(typ types.Type) types.Type {
|
||||
return typ
|
||||
}
|
||||
|
||||
func (p *context) resolveLinkname(name string) string {
|
||||
if link, ok := p.link[name]; ok {
|
||||
prefix, ltarget, _ := strings.Cut(link, ".")
|
||||
if prefix != "C" {
|
||||
panic("resolveLinkname: invalid link: " + link)
|
||||
}
|
||||
return ltarget
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
@@ -154,6 +154,9 @@ func (b Builder) abiMethodOf(mPkg *types.Package, mName string, mSig *types.Sign
|
||||
|
||||
func (b Builder) abiMthd(mPkg *types.Package, mName string, mSig *types.Signature, name, abiTyp, ifn llvm.Value) (ret Expr, tfn llvm.Value) {
|
||||
fullName := FuncName(mPkg, mName, mSig.Recv())
|
||||
if b.Pkg.fnlink != nil {
|
||||
fullName = b.Pkg.fnlink(fullName)
|
||||
}
|
||||
tfn = b.Pkg.NewFunc(fullName, mSig, InGo).impl // TODO(xsw): use rawType to speed up
|
||||
if ifn.IsNil() {
|
||||
ifn = tfn
|
||||
|
||||
@@ -586,6 +586,7 @@ type aPackage struct {
|
||||
named map[types.Type]Expr
|
||||
afterb unsafe.Pointer
|
||||
patch func(types.Type) types.Type
|
||||
fnlink func(string) string
|
||||
|
||||
iRoutine int
|
||||
}
|
||||
@@ -658,6 +659,11 @@ func (p Package) SetPatch(fn func(types.Type) types.Type) {
|
||||
p.patch = fn
|
||||
}
|
||||
|
||||
// SetResolveLinkname sets a function to resolve linkname.
|
||||
func (p Package) SetResolveLinkname(fn func(string) string) {
|
||||
p.fnlink = fn
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
func (p Package) afterBuilder() Builder {
|
||||
|
||||
Reference in New Issue
Block a user