diff --git a/cl/_testpy/math/out.ll b/cl/_testpy/math/out.ll index 4fd8d274..f5feead3 100644 --- a/cl/_testpy/math/out.ll +++ b/cl/_testpy/math/out.ll @@ -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 diff --git a/cl/compile.go b/cl/compile.go index 1b1a97bd..109126f4 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -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: diff --git a/py/math/llgo_autogen.lla b/py/math/llgo_autogen.lla index 56d53f34..7c9261cd 100644 Binary files a/py/math/llgo_autogen.lla and b/py/math/llgo_autogen.lla differ diff --git a/py/os/llgo_autogen.lla b/py/os/llgo_autogen.lla index e37b4105..a8378f7b 100644 Binary files a/py/os/llgo_autogen.lla and b/py/os/llgo_autogen.lla differ diff --git a/ssa/decl.go b/ssa/decl.go index f23d23a4..bc6205ec 100644 --- a/ssa/decl.go +++ b/ssa/decl.go @@ -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} diff --git a/ssa/ssa_test.go b/ssa/ssa_test.go index ef60f7af..2e05b4b5 100644 --- a/ssa/ssa_test.go +++ b/ssa/ssa_test.go @@ -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")