llgo/ssa: NewPyFunc fix

This commit is contained in:
xushiwei
2024-05-12 00:24:56 +08:00
parent 6c32fe87e6
commit 64c13fa9ae
6 changed files with 24 additions and 16 deletions

View File

@@ -4,8 +4,8 @@ source_filename = "main"
@"main.init$guard" = global ptr null @"main.init$guard" = global ptr null
@__llgo_argc = global ptr null @__llgo_argc = global ptr null
@__llgo_argv = global ptr null @__llgo_argv = global ptr null
@sqrt = external global ptr @__llgo_py.math.sqrt = linkonce global ptr null
@getcwd = external global ptr @__llgo_py.os.getcwd = linkonce global ptr null
@0 = private unnamed_addr constant [14 x i8] c"sqrt(2) = %f\0A\00", align 1 @0 = private unnamed_addr constant [14 x i8] c"sqrt(2) = %f\0A\00", align 1
@1 = private unnamed_addr constant [10 x i8] c"cwd = %s\0A\00", align 1 @1 = private unnamed_addr constant [10 x i8] c"cwd = %s\0A\00", align 1
@@ -31,8 +31,8 @@ _llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.init"() call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init() call void @main.init()
%2 = call ptr @PyFloat_FromDouble(double 2.000000e+00) %2 = call ptr @PyFloat_FromDouble(double 2.000000e+00)
%3 = call ptr @PyObject_CallOneArg(ptr @sqrt, ptr %2) %3 = call ptr @PyObject_CallOneArg(ptr @__llgo_py.math.sqrt, ptr %2)
%4 = call ptr @PyObject_CallNoArg(ptr @getcwd) %4 = call ptr @PyObject_CallNoArgs(ptr @__llgo_py.os.getcwd)
%5 = call double @PyFloat_AsDouble() %5 = call double @PyFloat_AsDouble()
%6 = call i32 (ptr, ...) @printf(ptr @0, double %5) %6 = call i32 (ptr, ...) @printf(ptr @0, double %5)
%7 = call ptr @PyBytes_AsString() %7 = call ptr @PyBytes_AsString()
@@ -50,7 +50,7 @@ declare ptr @PyFloat_FromDouble(double)
declare ptr @PyObject_CallOneArg(ptr, ptr) declare ptr @PyObject_CallOneArg(ptr, ptr)
declare ptr @PyObject_CallNoArg(ptr) declare ptr @PyObject_CallNoArgs(ptr)
declare double @PyFloat_AsDouble() declare double @PyFloat_AsDouble()

View File

@@ -285,13 +285,18 @@ func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function) (llssa.Fun
// funcOf returns a function by name and set ftype = goFunc, cFunc, etc. // funcOf returns a function by name and set ftype = goFunc, cFunc, etc.
// or returns nil and set ftype = llgoCstr, llgoAlloca, llgoUnreachable, etc. // or returns nil and set ftype = llgoCstr, llgoAlloca, llgoUnreachable, etc.
func (p *context) funcOf(fn *ssa.Function) (aFn llssa.Function, pyFn llssa.PyFunction, ftype int) { func (p *context) funcOf(fn *ssa.Function) (aFn llssa.Function, pyFn llssa.PyFunction, ftype int) {
_, name, ftype := p.funcName(fn, false) pkgTypes, name, ftype := p.funcName(fn, false)
switch ftype { switch ftype {
case pyFunc: case pyFunc:
pkg := p.pkg if kind, mod := pkgKindByScope(pkgTypes.Scope()); kind == PkgPyModule {
if pyFn = pkg.PyFuncOf(name); pyFn == nil { pkg := p.pkg
pyFn = pkg.NewPyFunc(name, fn.Signature) fnName := pysymPrefix + mod + "." + name
if pyFn = pkg.PyFuncOf(fnName); pyFn == nil {
pyFn = pkg.NewPyFunc(fnName, fn.Signature)
return
}
} }
ftype = ignoredFunc
case llgoInstr: case llgoInstr:
switch name { switch name {
case "cstr": case "cstr":

BIN
py/math/llgo_autogen.lla Normal file

Binary file not shown.

BIN
py/os/llgo_autogen.lla Normal file

Binary file not shown.

View File

@@ -298,7 +298,10 @@ func (p Package) NewPyFunc(name string, sig *types.Signature) PyFunction {
if v, ok := p.pyfns[name]; ok { if v, ok := p.pyfns[name]; ok {
return v return v
} }
obj := p.NewVar(name, p.Prog.PyObjectPtrPtr().RawType(), InC) prog := p.Prog
obj := p.NewVar(name, prog.PyObjectPtrPtr().RawType(), InC)
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

@@ -137,7 +137,7 @@ type aProgram struct {
pyObjPPtr Type pyObjPPtr Type
pyImpTy *types.Signature pyImpTy *types.Signature
callNoArg *types.Signature callNoArgs *types.Signature
callOneArg *types.Signature callOneArg *types.Signature
needRuntime bool needRuntime bool
@@ -485,14 +485,14 @@ func (p Program) tyImportPyModule() *types.Signature {
return p.pyImpTy return p.pyImpTy
} }
func (p Program) tyCallNoArg() *types.Signature { func (p Program) tyCallNoArgs() *types.Signature {
if p.callNoArg == nil { if p.callNoArgs == nil {
objPtr := p.PyObjectPtr().raw.Type objPtr := p.PyObjectPtr().raw.Type
paramObjPtr := types.NewParam(token.NoPos, nil, "", objPtr) paramObjPtr := types.NewParam(token.NoPos, nil, "", objPtr)
params := types.NewTuple(paramObjPtr) params := types.NewTuple(paramObjPtr)
p.callNoArg = types.NewSignatureType(nil, nil, nil, params, params, false) p.callNoArgs = types.NewSignatureType(nil, nil, nil, params, params, false)
} }
return p.callNoArg return p.callNoArgs
} }
func (p Program) tyCallOneArg() *types.Signature { func (p Program) tyCallOneArg() *types.Signature {
@@ -531,7 +531,7 @@ func (b Builder) pyCall(fn Expr, args []Expr) (ret Expr) {
n := params.Len() n := params.Len()
switch n { switch n {
case 0: case 0:
call := pkg.pyFunc("PyObject_CallNoArg", prog.tyCallNoArg()) call := pkg.pyFunc("PyObject_CallNoArgs", prog.tyCallNoArgs())
ret = b.Call(call, fn) ret = b.Call(call, fn)
case 1: case 1:
call := pkg.pyFunc("PyObject_CallOneArg", prog.tyCallOneArg()) call := pkg.pyFunc("PyObject_CallOneArg", prog.tyCallOneArg())