cl: initPyModule
This commit is contained in:
14
cl/_testpy/math/in.go
Normal file
14
cl/_testpy/math/in.go
Normal 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
0
cl/_testpy/math/out.ll
Normal 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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
16
cl/import.go
16
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
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
Binary file not shown.
@@ -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
|
||||
|
||||
@@ -40,6 +40,7 @@ const (
|
||||
inUnknown Background = iota
|
||||
InGo
|
||||
InC
|
||||
InPython
|
||||
)
|
||||
|
||||
// Type convert a Go/C type into raw type.
|
||||
|
||||
Reference in New Issue
Block a user