diff --git a/cl/_testpy/math/in.go b/cl/_testpy/math/in.go new file mode 100644 index 00000000..5d6936f9 --- /dev/null +++ b/cl/_testpy/math/in.go @@ -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 diff --git a/cl/_testpy/math/out.ll b/cl/_testpy/math/out.ll new file mode 100644 index 00000000..e69de29b diff --git a/cl/compile.go b/cl/compile.go index d98ac674..9460df95 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -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 diff --git a/cl/compile_test.go b/cl/compile_test.go index 76be7e7a..1af95938 100644 --- a/cl/compile_test.go +++ b/cl/compile_test.go @@ -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) } diff --git a/cl/import.go b/cl/import.go index 81e628bb..952f92e5 100644 --- a/cl/import.go +++ b/cl/import.go @@ -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 + } +} + +// ----------------------------------------------------------------------------- diff --git a/internal/pyimport/llgo_autogen.lla b/internal/pyimport/llgo_autogen.lla deleted file mode 100644 index d839012f..00000000 Binary files a/internal/pyimport/llgo_autogen.lla and /dev/null differ diff --git a/internal/pyimport/pyimport.go b/internal/pyimport/pyimport.go index 69f96fad..72c23de7 100644 --- a/internal/pyimport/pyimport.go +++ b/internal/pyimport/pyimport.go @@ -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 diff --git a/ssa/type_cvt.go b/ssa/type_cvt.go index d6691f1e..2f447cc7 100644 --- a/ssa/type_cvt.go +++ b/ssa/type_cvt.go @@ -40,6 +40,7 @@ const ( inUnknown Background = iota InGo InC + InPython ) // Type convert a Go/C type into raw type.