cl: initPyModule

This commit is contained in:
xushiwei
2024-05-11 10:07:46 +08:00
parent 2560a333b6
commit 427d87be68
8 changed files with 48 additions and 0 deletions

14
cl/_testpy/math/in.go Normal file
View File

@@ -0,0 +1,14 @@
package math
import (
_ "unsafe"
"github.com/goplus/llgo/py"
)
const (
LLGoPackage = "py.math"
)
//go:linkname Sqrt py.sqrt
func Sqrt(x *py.Object) *py.Object

0
cl/_testpy/math/out.ll Normal file
View File

View File

@@ -143,6 +143,7 @@ type context struct {
goProg *ssa.Program goProg *ssa.Program
goTyps *types.Package goTyps *types.Package
goPkg *ssa.Package goPkg *ssa.Package
pyMod string
link map[string]string // pkgPath.nameInPkg => linkname link map[string]string // pkgPath.nameInPkg => linkname
loaded map[*types.Package]*pkgInfo // loaded packages loaded map[*types.Package]*pkgInfo // loaded packages
bvals map[ssa.Value]llssa.Expr // block values bvals map[ssa.Value]llssa.Expr // block values
@@ -213,6 +214,11 @@ var (
func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function) llssa.Function { func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function) llssa.Function {
pkgTypes, name, ftype := p.funcName(f, true) pkgTypes, name, ftype := p.funcName(f, true)
if ftype != goFunc { if ftype != goFunc {
if ftype == pyFunc {
// TODO(xsw): pyMod == ""
fn := "__llgo_py." + p.pyMod + "." + name
pkg.NewVar(fn, types.Typ[types.Int], llssa.InC)
}
return nil return nil
} }
fn := pkg.FuncOf(name) fn := pkg.FuncOf(name)
@@ -815,6 +821,7 @@ func NewPackage(prog llssa.Program, pkg *ssa.Package, files []*ast.File) (ret ll
types.Unsafe: {kind: PkgDeclOnly}, // TODO(xsw): PkgNoInit or PkgDeclOnly? types.Unsafe: {kind: PkgDeclOnly}, // TODO(xsw): PkgNoInit or PkgDeclOnly?
}, },
} }
ctx.initPyModule()
ctx.initFiles(pkgPath, files) ctx.initFiles(pkgPath, files)
for _, m := range members { for _, m := range members {
member := m.val member := m.val

View File

@@ -28,6 +28,10 @@ func testCompile(t *testing.T, src, expected string) {
cltest.TestCompileEx(t, src, "foo.go", expected) cltest.TestCompileEx(t, src, "foo.go", expected)
} }
func TestFromTestpy(t *testing.T) {
cltest.FromDir(t, "", "./_testpy", false)
}
func TestFromTestlibc(t *testing.T) { func TestFromTestlibc(t *testing.T) {
cltest.FromDir(t, "", "./_testlibc", false) cltest.FromDir(t, "", "./_testlibc", false)
} }

View File

@@ -30,6 +30,8 @@ import (
"golang.org/x/tools/go/ssa" "golang.org/x/tools/go/ssa"
) )
// -----------------------------------------------------------------------------
type symInfo struct { type symInfo struct {
file string file string
fullName string fullName string
@@ -305,6 +307,7 @@ const (
ignoredFunc = iota ignoredFunc = iota
goFunc = int(llssa.InGo) goFunc = int(llssa.InGo)
cFunc = int(llssa.InC) cFunc = int(llssa.InC)
pyFunc = int(llssa.InPython)
llgoInstr = -1 llgoInstr = -1
llgoInstrBase = 0x80 llgoInstrBase = 0x80
@@ -341,6 +344,9 @@ func (p *context) funcName(fn *ssa.Function, ignore bool) (*types.Package, strin
if strings.HasPrefix(v, "C.") { if strings.HasPrefix(v, "C.") {
return nil, v[2:], cFunc return nil, v[2:], cFunc
} }
if strings.HasPrefix(v, "py.") {
return nil, v[3:], pyFunc
}
if strings.HasPrefix(v, "llgo.") { if strings.HasPrefix(v, "llgo.") {
return nil, v[5:], llgoInstr return nil, v[5:], llgoInstr
} }
@@ -393,3 +399,13 @@ func pkgKindByPath(pkgPath string) int {
} }
return PkgNormal return PkgNormal
} }
// -----------------------------------------------------------------------------
func (p *context) initPyModule() {
if kind, mod := pkgKindByScope(p.goTyps.Scope()); kind == PkgPyModule {
p.pyMod = mod
}
}
// -----------------------------------------------------------------------------

Binary file not shown.

View File

@@ -23,10 +23,16 @@ import (
"github.com/goplus/llgo/py" "github.com/goplus/llgo/py"
) )
const (
LLGoPackage = "decl"
)
/*
func init() { func init() {
py.Initialize() py.Initialize()
py.SetProgramName(*c.Argv) py.SetProgramName(*c.Argv)
} }
*/
//go:linkname Module C.PyImport_ImportModule //go:linkname Module C.PyImport_ImportModule
func Module(name *c.Char) *py.Module func Module(name *c.Char) *py.Module

View File

@@ -40,6 +40,7 @@ const (
inUnknown Background = iota inUnknown Background = iota
InGo InGo
InC InC
InPython
) )
// Type convert a Go/C type into raw type. // Type convert a Go/C type into raw type.