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' ; ModuleID = 'math'
source_filename = "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 @"math.init$guard" = global ptr null
@__llgo_py.math = linkonce global ptr null @__llgo_py.math = linkonce global ptr null
@0 = private unnamed_addr constant [5 x i8] c"math\00", align 1 @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++ { for i, n := 0, mthds.Len(); i < n; i++ {
mthd := mthds.At(i) mthd := mthds.At(i)
if ssaMthd := prog.MethodValue(mthd); ssaMthd != nil { 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])) 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) pkgTypes, name, ftype := p.funcName(f, true)
if ftype != goFunc { if ftype != goFunc {
if ftype == pyFunc { if ftype == pyFunc {
// TODO(xsw): pyMod == "" // TODO(xsw): pyMod == ""
fnName := pysymPrefix + p.pyMod + "." + name 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 return nil, nil, ignoredFunc
} }
@@ -292,7 +292,7 @@ func (p *context) funcOf(fn *ssa.Function) (aFn llssa.Function, pyFn llssa.PyFun
pkg := p.pkg pkg := p.pkg
fnName := pysymPrefix + mod + "." + name fnName := pysymPrefix + mod + "." + name
if pyFn = pkg.PyFuncOf(fnName); pyFn == nil { if pyFn = pkg.PyFuncOf(fnName); pyFn == nil {
pyFn = pkg.NewPyFunc(fnName, fn.Signature) pyFn = pkg.NewPyFunc(fnName, fn.Signature, true)
return return
} }
} }
@@ -762,7 +762,7 @@ func (p *context) compileFunction(v *ssa.Function) (goFn llssa.Function, pyFn ll
// v.Pkg == nil: means auto generated function? // v.Pkg == nil: means auto generated function?
if v.Pkg == p.goPkg || v.Pkg == nil { if v.Pkg == p.goPkg || v.Pkg == nil {
// function in this package // function in this package
goFn, pyFn, kind = p.compileFuncDecl(p.pkg, v) goFn, pyFn, kind = p.compileFuncDecl(p.pkg, v, true)
if kind != ignoredFunc { if kind != ignoredFunc {
return 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. // Do not try to build generic (non-instantiated) functions.
continue continue
} }
ctx.compileFuncDecl(ret, member) ctx.compileFuncDecl(ret, member, false)
case *ssa.Type: case *ssa.Type:
ctx.compileType(ret, member) ctx.compileType(ret, member)
case *ssa.Global: case *ssa.Global:

Binary file not shown.

Binary file not shown.

View File

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

View File

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