llgo/ssa: PyFunction; NewPyFunc

This commit is contained in:
xushiwei
2024-05-11 21:55:50 +08:00
parent 15499ddc14
commit a2d7a8c978
8 changed files with 129 additions and 68 deletions

View File

@@ -19,7 +19,6 @@ package ssa
import (
"go/token"
"go/types"
"log"
"github.com/goplus/llvm"
"golang.org/x/tools/go/types/typeutil"
@@ -260,8 +259,9 @@ func (p Program) NewPackage(name, pkgPath string) Package {
gbls := make(map[string]Global)
fns := make(map[string]Function)
stubs := make(map[string]Function)
pyfns := make(map[string]PyFunction)
p.needRuntime = false
return &aPackage{mod, gbls, fns, stubs, p}
return &aPackage{mod, gbls, fns, stubs, pyfns, p}
}
// PyObjectPtrPtr returns the **py.Object type.
@@ -365,6 +365,7 @@ type aPackage struct {
vars map[string]Global
fns map[string]Function
stubs map[string]Function
pyfns map[string]PyFunction
Prog Program
}
@@ -377,40 +378,6 @@ func (p Package) NewConst(name string, val constant.Value) NamedConst {
}
*/
// NewVar creates a new global variable.
func (p Package) NewVar(name string, typ types.Type, bg Background) Global {
t := p.Prog.Type(typ, bg)
gbl := llvm.AddGlobal(p.mod, t.ll, name)
ret := &aGlobal{Expr{gbl, t}}
p.vars[name] = ret
return ret
}
// VarOf returns a global variable by name.
func (p Package) VarOf(name string) Global {
return p.vars[name]
}
// NewFunc creates a new function.
func (p Package) NewFunc(name string, sig *types.Signature, bg Background) Function {
return p.NewFuncEx(name, sig, bg, false)
}
// NewFuncEx creates a new function.
func (p Package) NewFuncEx(name string, sig *types.Signature, bg Background, hasFreeVars bool) Function {
if v, ok := p.fns[name]; ok {
return v
}
t := p.Prog.FuncDecl(sig, bg)
if debugInstr {
log.Println("NewFunc", name, t.raw.Type, "hasFreeVars:", hasFreeVars)
}
fn := llvm.AddFunction(p.mod, name, t.ll)
ret := newFunction(fn, t, p, p.Prog, hasFreeVars)
p.fns[name] = ret
return ret
}
func (p Package) rtFunc(fnName string) Expr {
fn := p.Prog.runtime().Scope().Lookup(fnName).(*types.Func)
name := FullName(fn.Pkg(), fnName)
@@ -456,11 +423,6 @@ func (p Package) closureStub(b Builder, t *types.Struct, v Expr) Expr {
return b.aggregateValue(prog.rawType(t), v.impl, nilVal)
}
// FuncOf returns a function by name.
func (p Package) FuncOf(name string) Function {
return p.fns[name]
}
// -----------------------------------------------------------------------------
// String returns a string representation of the package.
@@ -521,6 +483,13 @@ func (p Program) tyImportPyModule() *types.Signature {
return p.pyImpTy
}
// ImportPyMod imports a Python module.
func (b Builder) ImportPyMod(path string) Expr {
pkg := b.Func.Pkg
fnImp := pkg.cpyFunc("PyImport_ImportModule", b.Prog.tyImportPyModule())
return b.Call(fnImp, b.CStr(path))
}
// NewPyModVar creates a new global variable for a Python module.
func (p Package) NewPyModVar(name string) Global {
prog := p.Prog
@@ -531,11 +500,4 @@ func (p Package) NewPyModVar(name string) Global {
return g
}
// ImportPyMod imports a Python module.
func (b Builder) ImportPyMod(path string) Expr {
pkg := b.Func.Pkg
fnImp := pkg.cpyFunc("PyImport_ImportModule", b.Prog.tyImportPyModule())
return b.Call(fnImp, b.CStr(path))
}
// -----------------------------------------------------------------------------