cl: _testdata/importpkg
This commit is contained in:
37
cl/_testdata/importpkg/out.ll
Normal file
37
cl/_testdata/importpkg/out.ll
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
; ModuleID = 'main'
|
||||||
|
source_filename = "main"
|
||||||
|
|
||||||
|
@"init$guard" = global ptr null
|
||||||
|
@hello = global ptr null
|
||||||
|
|
||||||
|
define void @main.init() {
|
||||||
|
_llgo_0:
|
||||||
|
%0 = load i1, ptr @"init$guard", align 1
|
||||||
|
br i1 %0, label %_llgo_2, label %_llgo_1
|
||||||
|
|
||||||
|
_llgo_1: ; preds = %_llgo_0
|
||||||
|
store i1 true, ptr @"init$guard", align 1
|
||||||
|
call void @"github.com/goplus/llgo/cl/internal/stdio.init"()
|
||||||
|
store i8 72, ptr @hello, align 1
|
||||||
|
store i8 101, ptr getelementptr inbounds (i8, ptr @hello, i64 1), align 1
|
||||||
|
store i8 108, ptr getelementptr inbounds (i8, ptr @hello, i64 2), align 1
|
||||||
|
store i8 108, ptr getelementptr inbounds (i8, ptr @hello, i64 3), align 1
|
||||||
|
store i8 111, ptr getelementptr inbounds (i8, ptr @hello, i64 4), align 1
|
||||||
|
store i8 10, ptr getelementptr inbounds (i8, ptr @hello, i64 5), align 1
|
||||||
|
store i8 0, ptr getelementptr inbounds (i8, ptr @hello, i64 6), align 1
|
||||||
|
br label %_llgo_2
|
||||||
|
|
||||||
|
_llgo_2: ; preds = %_llgo_1, %_llgo_0
|
||||||
|
ret void
|
||||||
|
}
|
||||||
|
|
||||||
|
define void @main() {
|
||||||
|
_llgo_0:
|
||||||
|
call void @main.init()
|
||||||
|
call void (ptr, ...) @printf(ptr @hello)
|
||||||
|
ret void
|
||||||
|
}
|
||||||
|
|
||||||
|
declare void @"github.com/goplus/llgo/cl/internal/stdio.init"()
|
||||||
|
|
||||||
|
declare void @printf(ptr, ...)
|
||||||
@@ -19,11 +19,12 @@ package cl
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/ast"
|
"go/ast"
|
||||||
|
"go/types"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
|
||||||
|
|
||||||
|
"github.com/goplus/gop/token"
|
||||||
llssa "github.com/goplus/llgo/ssa"
|
llssa "github.com/goplus/llgo/ssa"
|
||||||
"golang.org/x/tools/go/ssa"
|
"golang.org/x/tools/go/ssa"
|
||||||
)
|
)
|
||||||
@@ -76,16 +77,10 @@ func funcKind(vfn ssa.Value) int {
|
|||||||
return fnNormal
|
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 {
|
type instrAndValue interface {
|
||||||
ssa.Instruction
|
ssa.Instruction
|
||||||
ssa.Value
|
ssa.Value
|
||||||
@@ -95,56 +90,14 @@ type context struct {
|
|||||||
prog llssa.Program
|
prog llssa.Program
|
||||||
pkg llssa.Package
|
pkg llssa.Package
|
||||||
fn llssa.Function
|
fn llssa.Function
|
||||||
|
fset *token.FileSet
|
||||||
goPkg *ssa.Package
|
goPkg *ssa.Package
|
||||||
link map[string]string
|
link map[string]string // pkgPath.nameInPkg => linkname
|
||||||
|
loaded map[*types.Package]none // loaded packages
|
||||||
bvals map[ssa.Value]llssa.Expr // block values
|
bvals map[ssa.Value]llssa.Expr // block values
|
||||||
inits []func()
|
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
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *context) compileType(pkg llssa.Package, member *ssa.Type) {
|
func (p *context) compileType(pkg llssa.Package, member *ssa.Type) {
|
||||||
panic("todo")
|
panic("todo")
|
||||||
}
|
}
|
||||||
@@ -160,7 +113,7 @@ func (p *context) compileGlobal(pkg llssa.Package, gbl *ssa.Global) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *context) compileFunc(pkg llssa.Package, f *ssa.Function) {
|
func (p *context) compileFunc(pkg llssa.Package, f *ssa.Function) {
|
||||||
name := p.funcName(f)
|
name := p.funcName(f.Pkg.Pkg, f)
|
||||||
if debugInstr {
|
if debugInstr {
|
||||||
log.Println("==> NewFunc", name)
|
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.
|
// 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 {
|
type namedMember struct {
|
||||||
name string
|
name string
|
||||||
val ssa.Member
|
val ssa.Member
|
||||||
}
|
}
|
||||||
|
|
||||||
pkg := in.SSA
|
|
||||||
|
|
||||||
// Sort by position, so that the order of the functions in the IR matches
|
// 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,
|
// the order of functions in the source file. This is useful for testing,
|
||||||
// for example.
|
// for example.
|
||||||
@@ -362,10 +311,12 @@ func NewPackage(prog llssa.Program, in *GoPackage) (ret llssa.Package, err error
|
|||||||
ctx := &context{
|
ctx := &context{
|
||||||
prog: prog,
|
prog: prog,
|
||||||
pkg: ret,
|
pkg: ret,
|
||||||
|
fset: fset,
|
||||||
goPkg: pkg,
|
goPkg: pkg,
|
||||||
link: make(map[string]string),
|
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 {
|
for _, m := range members {
|
||||||
member := m.val
|
member := m.val
|
||||||
switch member := member.(type) {
|
switch member := member.(type) {
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestFromTestdata(t *testing.T) {
|
func TestFromTestdata(t *testing.T) {
|
||||||
testFromDir(t, "", "./_testdata")
|
testFromDir(t, "importpkg", "./_testdata")
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -97,7 +97,7 @@ func testCompileEx(t *testing.T, src any, fname, expected string) {
|
|||||||
}
|
}
|
||||||
foo.WriteTo(os.Stderr)
|
foo.WriteTo(os.Stderr)
|
||||||
prog := llssa.NewProgram(nil)
|
prog := llssa.NewProgram(nil)
|
||||||
ret, err := NewPackage(prog, &GoPackage{SSA: foo, Files: files})
|
ret, err := NewPackage(prog, fset, foo, files)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("cl.NewPackage failed:", err)
|
t.Fatal("cl.NewPackage failed:", err)
|
||||||
}
|
}
|
||||||
|
|||||||
140
cl/import.go
Normal file
140
cl/import.go
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package cl
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"go/ast"
|
||||||
|
"go/token"
|
||||||
|
"go/types"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
llssa "github.com/goplus/llgo/ssa"
|
||||||
|
"golang.org/x/tools/go/ssa"
|
||||||
|
)
|
||||||
|
|
||||||
|
type contentLines = [][]byte
|
||||||
|
type contentMap = map[string]contentLines
|
||||||
|
|
||||||
|
func contentOf(m contentMap, file string) (lines contentLines, err error) {
|
||||||
|
if v, ok := m[file]; ok {
|
||||||
|
return v, nil
|
||||||
|
}
|
||||||
|
b, err := os.ReadFile(file)
|
||||||
|
if err == nil {
|
||||||
|
lines = bytes.Split(b, []byte{'\n'})
|
||||||
|
m[file] = lines
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *context) importPkg(pkg *types.Package) {
|
||||||
|
scope := pkg.Scope()
|
||||||
|
if scope.Lookup("LLGoPackage") == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fset := p.fset
|
||||||
|
names := scope.Names()
|
||||||
|
contents := make(contentMap)
|
||||||
|
pkgPath := pkg.Path()
|
||||||
|
for _, name := range names {
|
||||||
|
if token.IsExported(name) {
|
||||||
|
obj := scope.Lookup(name)
|
||||||
|
if obj, ok := obj.(*types.Func); ok {
|
||||||
|
if pos := obj.Pos(); pos != token.NoPos {
|
||||||
|
f := fset.File(pos)
|
||||||
|
if fp := f.PositionFor(pos, false); fp.Line > 2 {
|
||||||
|
lines, err := contentOf(contents, fp.Filename)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if i := fp.Line - 2; i < len(lines) {
|
||||||
|
line := string(lines[i])
|
||||||
|
p.initLinkname(pkgPath, line)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
if doc := decl.Doc; doc != nil {
|
||||||
|
if n := len(doc.List); n > 0 {
|
||||||
|
line := doc.List[n-1].Text
|
||||||
|
p.initLinkname(pkgPath, line)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *context) initLinkname(pkgPath, line string) {
|
||||||
|
const (
|
||||||
|
linkname = "//go:linkname "
|
||||||
|
)
|
||||||
|
if strings.HasPrefix(line, linkname) {
|
||||||
|
text := strings.TrimSpace(line[len(linkname):])
|
||||||
|
if idx := strings.IndexByte(text, ' '); idx > 0 {
|
||||||
|
name := pkgPath + "." + text[:idx]
|
||||||
|
link := strings.TrimLeft(text[idx+1:], " ")
|
||||||
|
p.link[name] = link
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func fullName(pkg *types.Package, name string) string {
|
||||||
|
return pkg.Path() + "." + name
|
||||||
|
}
|
||||||
|
|
||||||
|
func funcName(pkg *types.Package, fn *ssa.Function) string {
|
||||||
|
ret := fullName(pkg, fn.Name())
|
||||||
|
if ret == "main.main" {
|
||||||
|
ret = "main"
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *context) funcName(pkg *types.Package, fn *ssa.Function) string {
|
||||||
|
name := funcName(pkg, fn)
|
||||||
|
if v, ok := p.link[name]; ok {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *context) funcOf(fn *ssa.Function) llssa.Function {
|
||||||
|
pkgTypes := fn.Pkg.Pkg
|
||||||
|
if _, ok := p.loaded[pkgTypes]; !ok {
|
||||||
|
p.loaded[pkgTypes] = none{}
|
||||||
|
p.importPkg(pkgTypes)
|
||||||
|
}
|
||||||
|
pkg := p.pkg
|
||||||
|
name := p.funcName(pkgTypes, fn)
|
||||||
|
if ret := pkg.FuncOf(name); ret != nil {
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
return pkg.NewFunc(name, fn.Signature)
|
||||||
|
}
|
||||||
@@ -2,5 +2,9 @@ package stdio
|
|||||||
|
|
||||||
import _ "unsafe"
|
import _ "unsafe"
|
||||||
|
|
||||||
|
const (
|
||||||
|
LLGoPackage = true
|
||||||
|
)
|
||||||
|
|
||||||
//go:linkname Printf printf
|
//go:linkname Printf printf
|
||||||
func Printf(format *int8, __llgo_va_list ...any)
|
func Printf(format *int8, __llgo_va_list ...any)
|
||||||
|
|||||||
@@ -18,12 +18,12 @@ package llgen
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/importer"
|
|
||||||
"go/parser"
|
"go/parser"
|
||||||
"go/token"
|
"go/token"
|
||||||
"go/types"
|
"go/types"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/goplus/gogen/packages"
|
||||||
"github.com/goplus/llgo/cl"
|
"github.com/goplus/llgo/cl"
|
||||||
"golang.org/x/tools/go/ssa"
|
"golang.org/x/tools/go/ssa"
|
||||||
"golang.org/x/tools/go/ssa/ssautil"
|
"golang.org/x/tools/go/ssa/ssautil"
|
||||||
@@ -51,14 +51,15 @@ func Gen(inFile string, src any) string {
|
|||||||
files := []*ast.File{f}
|
files := []*ast.File{f}
|
||||||
name := f.Name.Name
|
name := f.Name.Name
|
||||||
pkg := types.NewPackage(name, name)
|
pkg := types.NewPackage(name, name)
|
||||||
|
imp := packages.NewImporter(fset)
|
||||||
ssaPkg, _, err := ssautil.BuildPackage(
|
ssaPkg, _, err := ssautil.BuildPackage(
|
||||||
&types.Config{Importer: importer.Default()}, fset, pkg, files, ssa.SanityCheckFunctions)
|
&types.Config{Importer: imp}, fset, pkg, files, ssa.SanityCheckFunctions)
|
||||||
check(err)
|
check(err)
|
||||||
|
|
||||||
ssaPkg.WriteTo(os.Stderr)
|
ssaPkg.WriteTo(os.Stderr)
|
||||||
|
|
||||||
prog := llssa.NewProgram(nil)
|
prog := llssa.NewProgram(nil)
|
||||||
ret, err := cl.NewPackage(prog, &cl.GoPackage{SSA: ssaPkg, Files: files})
|
ret, err := cl.NewPackage(prog, fset, ssaPkg, files)
|
||||||
check(err)
|
check(err)
|
||||||
|
|
||||||
return ret.String()
|
return ret.String()
|
||||||
|
|||||||
Reference in New Issue
Block a user