cl: _testdata/importpkg
This commit is contained in:
@@ -19,11 +19,12 @@ package cl
|
||||
import (
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/types"
|
||||
"log"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/goplus/gop/token"
|
||||
llssa "github.com/goplus/llgo/ssa"
|
||||
"golang.org/x/tools/go/ssa"
|
||||
)
|
||||
@@ -76,73 +77,25 @@ func funcKind(vfn ssa.Value) int {
|
||||
return fnNormal
|
||||
}
|
||||
|
||||
func funcName(fn *ssa.Function) string {
|
||||
ret := fn.Pkg.Pkg.Path() + "." + fn.Name()
|
||||
if ret == "main.main" {
|
||||
ret = "main"
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
type none = struct{}
|
||||
|
||||
type instrAndValue interface {
|
||||
ssa.Instruction
|
||||
ssa.Value
|
||||
}
|
||||
|
||||
type context struct {
|
||||
prog llssa.Program
|
||||
pkg llssa.Package
|
||||
fn llssa.Function
|
||||
goPkg *ssa.Package
|
||||
link map[string]string
|
||||
bvals map[ssa.Value]llssa.Expr // block values
|
||||
inits []func()
|
||||
}
|
||||
|
||||
func (p *context) initFiles(pkgPath string, files []*ast.File) {
|
||||
for _, file := range files {
|
||||
for _, decl := range file.Decls {
|
||||
if decl, ok := decl.(*ast.FuncDecl); ok {
|
||||
if decl.Recv == nil && decl.Doc != nil {
|
||||
p.initLinkname(pkgPath, decl.Doc)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *context) initLinkname(pkgPath string, doc *ast.CommentGroup) {
|
||||
const (
|
||||
linkname = "//go:linkname "
|
||||
)
|
||||
for _, c := range doc.List {
|
||||
if strings.HasPrefix(c.Text, linkname) {
|
||||
text := strings.TrimSpace(c.Text[len(linkname):])
|
||||
if idx := strings.IndexByte(text, ' '); idx > 0 {
|
||||
name := pkgPath + "." + text[:idx]
|
||||
p.link[name] = strings.TrimLeft(text[idx+1:], " ")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *context) funcName(fn *ssa.Function) string {
|
||||
name := funcName(fn)
|
||||
if v, ok := p.link[name]; ok {
|
||||
return v
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
func (p *context) funcOf(fn *ssa.Function) llssa.Function {
|
||||
pkg := p.pkg
|
||||
name := p.funcName(fn)
|
||||
if ret := pkg.FuncOf(name); ret != nil {
|
||||
return ret
|
||||
}
|
||||
return pkg.NewFunc(name, fn.Signature) // TODO: support linkname
|
||||
prog llssa.Program
|
||||
pkg llssa.Package
|
||||
fn llssa.Function
|
||||
fset *token.FileSet
|
||||
goPkg *ssa.Package
|
||||
link map[string]string // pkgPath.nameInPkg => linkname
|
||||
loaded map[*types.Package]none // loaded packages
|
||||
bvals map[ssa.Value]llssa.Expr // block values
|
||||
inits []func()
|
||||
}
|
||||
|
||||
func (p *context) compileType(pkg llssa.Package, member *ssa.Type) {
|
||||
@@ -160,7 +113,7 @@ func (p *context) compileGlobal(pkg llssa.Package, gbl *ssa.Global) {
|
||||
}
|
||||
|
||||
func (p *context) compileFunc(pkg llssa.Package, f *ssa.Function) {
|
||||
name := p.funcName(f)
|
||||
name := p.funcName(f.Pkg.Pkg, f)
|
||||
if debugInstr {
|
||||
log.Println("==> NewFunc", name)
|
||||
}
|
||||
@@ -329,20 +282,16 @@ func (p *context) compileValues(b llssa.Builder, vals []ssa.Value, hasVArg int)
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
type GoPackage struct {
|
||||
SSA *ssa.Package
|
||||
Files []*ast.File
|
||||
}
|
||||
|
||||
// NewPackage compiles a Go package to LLVM IR package.
|
||||
func NewPackage(prog llssa.Program, in *GoPackage) (ret llssa.Package, err error) {
|
||||
func NewPackage(
|
||||
prog llssa.Program,
|
||||
fset *token.FileSet, pkg *ssa.Package, files []*ast.File) (ret llssa.Package, err error) {
|
||||
|
||||
type namedMember struct {
|
||||
name string
|
||||
val ssa.Member
|
||||
}
|
||||
|
||||
pkg := in.SSA
|
||||
|
||||
// Sort by position, so that the order of the functions in the IR matches
|
||||
// the order of functions in the source file. This is useful for testing,
|
||||
// for example.
|
||||
@@ -360,12 +309,14 @@ func NewPackage(prog llssa.Program, in *GoPackage) (ret llssa.Package, err error
|
||||
ret = prog.NewPackage(pkgTypes.Name(), pkgTypes.Path())
|
||||
|
||||
ctx := &context{
|
||||
prog: prog,
|
||||
pkg: ret,
|
||||
goPkg: pkg,
|
||||
link: make(map[string]string),
|
||||
prog: prog,
|
||||
pkg: ret,
|
||||
fset: fset,
|
||||
goPkg: pkg,
|
||||
link: make(map[string]string),
|
||||
loaded: make(map[*types.Package]none),
|
||||
}
|
||||
ctx.initFiles(pkgTypes.Path(), in.Files)
|
||||
ctx.initFiles(pkgTypes.Path(), files)
|
||||
for _, m := range members {
|
||||
member := m.val
|
||||
switch member := member.(type) {
|
||||
|
||||
Reference in New Issue
Block a user