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
goTyps *types.Package
goPkg *ssa.Package
pyMod string
link map[string]string // pkgPath.nameInPkg => linkname
loaded map[*types.Package]*pkgInfo // loaded packages
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 {
pkgTypes, name, ftype := p.funcName(f, true)
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
}
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?
},
}
ctx.initPyModule()
ctx.initFiles(pkgPath, files)
for _, m := range members {
member := m.val

View File

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

View File

@@ -30,6 +30,8 @@ import (
"golang.org/x/tools/go/ssa"
)
// -----------------------------------------------------------------------------
type symInfo struct {
file string
fullName string
@@ -305,6 +307,7 @@ const (
ignoredFunc = iota
goFunc = int(llssa.InGo)
cFunc = int(llssa.InC)
pyFunc = int(llssa.InPython)
llgoInstr = -1
llgoInstrBase = 0x80
@@ -341,6 +344,9 @@ func (p *context) funcName(fn *ssa.Function, ignore bool) (*types.Package, strin
if strings.HasPrefix(v, "C.") {
return nil, v[2:], cFunc
}
if strings.HasPrefix(v, "py.") {
return nil, v[3:], pyFunc
}
if strings.HasPrefix(v, "llgo.") {
return nil, v[5:], llgoInstr
}
@@ -393,3 +399,13 @@ func pkgKindByPath(pkgPath string) int {
}
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"
)
const (
LLGoPackage = "decl"
)
/*
func init() {
py.Initialize()
py.SetProgramName(*c.Argv)
}
*/
//go:linkname Module C.PyImport_ImportModule
func Module(name *c.Char) *py.Module

View File

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