llgo/ssa: NewPyFunc add param doInit

This commit is contained in:
xushiwei
2024-05-12 12:14:26 +08:00
parent 8a0189b079
commit 2e3cc49782
6 changed files with 14 additions and 12 deletions

View File

@@ -1,7 +1,7 @@
; ModuleID = 'math'
source_filename = "math"
@__llgo_py.math.sqrt = linkonce global ptr null
@__llgo_py.math.sqrt = external global ptr
@"math.init$guard" = global ptr null
@__llgo_py.math = linkonce global ptr null
@0 = private unnamed_addr constant [5 x i8] c"math\00", align 1

View File

@@ -176,7 +176,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, ssaMthd)
p.compileFuncDecl(pkg, ssaMthd, false)
}
}
}
@@ -211,13 +211,13 @@ var (
argvTy = types.NewPointer(types.NewPointer(types.Typ[types.Int8]))
)
func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function) (llssa.Function, llssa.PyFunction, int) {
func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function, call bool) (llssa.Function, llssa.PyFunction, int) {
pkgTypes, name, ftype := p.funcName(f, true)
if ftype != goFunc {
if ftype == pyFunc {
// TODO(xsw): pyMod == ""
fnName := pysymPrefix + p.pyMod + "." + name
return nil, pkg.NewPyFunc(fnName, f.Signature), pyFunc
return nil, pkg.NewPyFunc(fnName, f.Signature, call), pyFunc
}
return nil, nil, ignoredFunc
}
@@ -292,7 +292,7 @@ func (p *context) funcOf(fn *ssa.Function) (aFn llssa.Function, pyFn llssa.PyFun
pkg := p.pkg
fnName := pysymPrefix + mod + "." + name
if pyFn = pkg.PyFuncOf(fnName); pyFn == nil {
pyFn = pkg.NewPyFunc(fnName, fn.Signature)
pyFn = pkg.NewPyFunc(fnName, fn.Signature, true)
return
}
}
@@ -762,7 +762,7 @@ func (p *context) compileFunction(v *ssa.Function) (goFn llssa.Function, pyFn ll
// v.Pkg == nil: means auto generated function?
if v.Pkg == p.goPkg || v.Pkg == nil {
// function in this package
goFn, pyFn, kind = p.compileFuncDecl(p.pkg, v)
goFn, pyFn, kind = p.compileFuncDecl(p.pkg, v, true)
if kind != ignoredFunc {
return
}
@@ -881,7 +881,7 @@ func NewPackage(prog llssa.Program, pkg *ssa.Package, files []*ast.File) (ret ll
// Do not try to build generic (non-instantiated) functions.
continue
}
ctx.compileFuncDecl(ret, member)
ctx.compileFuncDecl(ret, member, false)
case *ssa.Type:
ctx.compileType(ret, member)
case *ssa.Global:

Binary file not shown.

Binary file not shown.

View File

@@ -294,15 +294,17 @@ type aPyFunction struct {
type PyFunction = *aPyFunction
// NewPyFunc creates a new python function.
func (p Package) NewPyFunc(name string, sig *types.Signature) PyFunction {
func (p Package) NewPyFunc(name string, sig *types.Signature, doInit bool) PyFunction {
if v, ok := p.pyfns[name]; ok {
return v
}
prog := p.Prog
prog.needPyInit = true
obj := p.NewVar(name, prog.PyObjectPtrPtr().RawType(), InC)
obj.Init(prog.Null(obj.Type))
obj.impl.SetLinkage(llvm.LinkOnceAnyLinkage)
if doInit {
obj.Init(prog.Null(obj.Type))
obj.impl.SetLinkage(llvm.LinkOnceAnyLinkage)
}
ty := &aType{obj.ll, rawType{sig}, vkPyFunc}
expr := Expr{obj.impl, ty}
ret := &aPyFunction{expr, obj}

View File

@@ -143,8 +143,8 @@ func TestPyFunc(t *testing.T) {
prog.SetPython(py)
pkg := prog.NewPackage("bar", "foo/bar")
sig := types.NewSignatureType(nil, nil, nil, nil, nil, false)
a := pkg.NewPyFunc("a", sig)
if pkg.NewPyFunc("a", sig) != a {
a := pkg.NewPyFunc("a", sig, false)
if pkg.NewPyFunc("a", sig, false) != a {
t.Fatal("NewPyFunc(a) failed")
}
foo := pkg.NewPyModVar("foo")