Skip to content

Commit

Permalink
cl: add Context; rm cl.syminfo
Browse files Browse the repository at this point in the history
  • Loading branch information
visualfc committed Dec 23, 2024
1 parent 070d64f commit eeb4469
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 162 deletions.
12 changes: 7 additions & 5 deletions cl/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ func TestToBackground(t *testing.T) {
}

func TestCollectSkipNames(t *testing.T) {
ctx := &context{skips: make(map[string]none)}
ctx.collectSkipNames("//llgo:skipall")
ctx.collectSkipNames("//llgo:skip")
ctx.collectSkipNames("//llgo:skip abs")
ctx := &Context{}
ctx.collectSkipNames("pkg", "//llgo:skipall")
ctx.collectSkipNames("pkg", "//llgo:skip")
ctx.collectSkipNames("pkg", "//llgo:skip abs")
}

func TestReplaceGoName(t *testing.T) {
Expand Down Expand Up @@ -236,6 +236,7 @@ func TestIgnoreName(t *testing.T) {

func TestErrImport(t *testing.T) {
var ctx context
ctx.Context = NewContext(nil)
pkg := types.NewPackage("foo", "foo")
ctx.importPkg(pkg, nil)

Expand All @@ -249,6 +250,7 @@ func TestErrImport(t *testing.T) {

func TestErrInitLinkname(t *testing.T) {
var ctx context
ctx.Context = NewContext(nil)
ctx.initLinkname("//llgo:link abc", func(name string) (string, bool, bool) {
return "", false, false
})
Expand Down Expand Up @@ -332,7 +334,7 @@ func TestContextResolveLinkname(t *testing.T) {
}
}()
}
ctx := &context{prog: llssa.NewProgram(nil)}
ctx := &Context{prog: llssa.NewProgram(nil)}
for k, v := range tt.link {
ctx.prog.SetLinkname(k, v)
}
Expand Down
56 changes: 41 additions & 15 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"os"
"sort"
"strings"
"sync"

"github.com/goplus/llgo/cl/blocks"
"github.com/goplus/llgo/internal/typepatch"
Expand Down Expand Up @@ -85,8 +86,31 @@ type pkgInfo struct {

type none = struct{}

type Context struct {
prog llssa.Program
skipLines sync.Map // pkgPath => skip lines
patches Patches
kinds map[string]int
cgoExports map[string]string
}

func NewContext(prog llssa.Program) *Context {
return &Context{
prog: prog,
kinds: map[string]int{
"unsafe": PkgDeclOnly,
"runtime/cgo": PkgDeclOnly,
},
cgoExports: make(map[string]string),
}
}

func (p *Context) SetPatches(patches Patches) {
p.patches = patches
}

type context struct {
prog llssa.Program
*Context
pkg llssa.Package
fn llssa.Function
fset *token.FileSet
Expand All @@ -99,7 +123,6 @@ type context struct {
bvals map[ssa.Value]llssa.Expr // block values
vargs map[*ssa.Alloc][]llssa.Expr // varargs

patches Patches
blkInfos []blocks.Info

inits []func()
Expand All @@ -114,7 +137,6 @@ type context struct {
cgoArgs []llssa.Expr
cgoRet llssa.Expr
cgoSymbols []string
cgoExports map[string]string
}

type pkgState byte
Expand Down Expand Up @@ -982,17 +1004,20 @@ type Patches = map[string]Patch

// NewPackage compiles a Go package to LLVM IR package.
func NewPackage(prog llssa.Program, pkg *ssa.Package, files []*ast.File) (ret llssa.Package, err error) {
ret, _, err = NewPackageEx(prog, nil, pkg, files)
bctx := NewContext(prog)
bctx.ParsePkgSyntax(pkg.Pkg, files, false)
ret, _, err = NewPackageEx(bctx, pkg)
return
}

// NewPackageEx compiles a Go package to LLVM IR package.
func NewPackageEx(prog llssa.Program, patches Patches, pkg *ssa.Package, files []*ast.File) (ret llssa.Package, externs []string, err error) {
func NewPackageEx(bctx *Context, pkg *ssa.Package) (ret llssa.Package, externs []string, err error) {
prog := bctx.prog
pkgProg := pkg.Prog
pkgTypes := pkg.Pkg
oldTypes := pkgTypes
pkgName, pkgPath := pkgTypes.Name(), llssa.PathOf(pkgTypes)
patch, hasPatch := patches[pkgPath]
patch, hasPatch := bctx.patches[pkgPath]
if hasPatch {
pkgTypes = patch.Types
pkg.Pkg = pkgTypes
Expand All @@ -1007,28 +1032,29 @@ func NewPackageEx(prog llssa.Program, patches Patches, pkg *ssa.Package, files [
}

ctx := &context{
prog: prog,
Context: bctx,
pkg: ret,
fset: pkgProg.Fset,
goProg: pkgProg,
goTyps: pkgTypes,
goPkg: pkg,
patches: patches,
skips: make(map[string]none),
vargs: make(map[*ssa.Alloc][]llssa.Expr),
loaded: map[*types.Package]*pkgInfo{
types.Unsafe: {kind: PkgDeclOnly}, // TODO(xsw): PkgNoInit or PkgDeclOnly?
},
cgoExports: make(map[string]string),
cgoSymbols: make([]string, 0, 128),
}
ctx.initPyModule()
ctx.initFiles(pkgPath, files)
ctx.prog.SetPatch(ctx.patchType)
ret.SetPatch(ctx.patchType)
ret.SetResolveLinkname(ctx.resolveLinkname)
ret.SetResolveLinkname(bctx.resolveLinkname)

if hasPatch {
if v, ok := bctx.skipLines.Load(pkgPath); ok {
for _, line := range v.([]string) {
ctx.parseSkip(line)
}
}
skips := ctx.skips
typepatch.Merge(pkgTypes, oldTypes, skips, ctx.skipall)
ctx.skips = nil
Expand Down Expand Up @@ -1131,11 +1157,11 @@ func globalType(gbl *ssa.Global) types.Type {
return t
}

func (p *context) type_(typ types.Type, bg llssa.Background) llssa.Type {
func (p *Context) type_(typ types.Type, bg llssa.Background) llssa.Type {
return p.prog.Type(p.patchType(typ), bg)
}

func (p *context) patchType(typ types.Type) types.Type {
func (p *Context) patchType(typ types.Type) types.Type {
if t, ok := typ.(*types.Named); ok {
o := t.Obj()
if pkg := o.Pkg(); typepatch.IsPatched(pkg) {
Expand All @@ -1154,7 +1180,7 @@ func instantiate(orig types.Type, t *types.Named) (typ types.Type) {
return
}

func (p *context) resolveLinkname(name string) string {
func (p *Context) resolveLinkname(name string) string {
if link, ok := p.prog.Linkname(name); ok {
prefix, ltarget, _ := strings.Cut(link, ".")
if prefix != "C" {
Expand Down
Loading

0 comments on commit eeb4469

Please sign in to comment.