Skip to content

Commit

Permalink
cl: context
Browse files Browse the repository at this point in the history
  • Loading branch information
visualfc committed Jul 10, 2024
1 parent 222e58e commit 744170c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
28 changes: 21 additions & 7 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,22 @@ type pkgInfo struct {
kind int
}

type Context struct {
pkgs map[string]none // loaded pkgs
link map[string]string // link map
}

func NewContext() *Context {
return &Context{
pkgs: make(map[string]none),
link: make(map[string]string),
}
}

type none = struct{}

type context struct {
*Context
prog llssa.Program
pkg llssa.Package
fn llssa.Function
Expand All @@ -88,7 +101,7 @@ type context struct {
goTyps *types.Package
goPkg *ssa.Package
pyMod string
link map[string]string // pkgPath.nameInPkg => linkname
//link map[string]string // pkgPath.nameInPkg => linkname
skips map[string]none
loaded map[*types.Package]*pkgInfo // loaded packages
bvals map[ssa.Value]llssa.Expr // block values
Expand Down Expand Up @@ -770,12 +783,12 @@ type Patch struct {
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) {
return NewPackageEx(prog, nil, pkg, files)
func NewPackage(c *Context, prog llssa.Program, pkg *ssa.Package, files []*ast.File) (ret llssa.Package, err error) {
return NewPackageEx(c, prog, nil, pkg, files)
}

// 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, err error) {
func NewPackageEx(c *Context, prog llssa.Program, patches Patches, pkg *ssa.Package, files []*ast.File) (ret llssa.Package, err error) {
pkgProg := pkg.Prog
pkgTypes := pkg.Pkg
oldTypes := pkgTypes
Expand All @@ -792,16 +805,17 @@ func NewPackageEx(prog llssa.Program, patches Patches, pkg *ssa.Package, files [
ret = prog.NewPackage(pkgName, pkgPath)

ctx := &context{
Context: c,
prog: prog,
pkg: ret,
fset: pkgProg.Fset,
goProg: pkgProg,
goTyps: pkgTypes,
goPkg: pkg,
patches: patches,
link: make(map[string]string),
skips: make(map[string]none),
vargs: make(map[*ssa.Alloc][]llssa.Expr),
//link: make(map[string]string),
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?
},
Expand Down
20 changes: 16 additions & 4 deletions cl/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func (p *context) importPkg(pkg *types.Package, i *pkgInfo) {
}
start:
i.kind = kind
return
fset := p.fset
names := scope.Names()
syms := newPkgSymInfo()
Expand Down Expand Up @@ -235,7 +236,7 @@ func (p *context) collectSkip(line string, prefix int) {
}
}

func (p *context) initLinknameByDoc(doc *ast.CommentGroup, fullName, inPkgName string, isVar bool) {
func (p *Context) initLinknameByDoc(doc *ast.CommentGroup, fullName, inPkgName string, isVar bool) {
if doc != nil {
if n := len(doc.List); n > 0 {
line := doc.List[n-1].Text
Expand All @@ -246,7 +247,7 @@ func (p *context) initLinknameByDoc(doc *ast.CommentGroup, fullName, inPkgName s
}
}

func (p *context) initLinkname(line string, f func(inPkgName string) (fullName string, isVar, ok bool)) {
func (p *Context) initLinkname(line string, f func(inPkgName string) (fullName string, isVar, ok bool)) {
const (
linkname = "//go:linkname "
llgolink = "//llgo:link "
Expand All @@ -261,7 +262,7 @@ func (p *context) initLinkname(line string, f func(inPkgName string) (fullName s
}
}

func (p *context) initLink(line string, prefix int, f func(inPkgName string) (fullName string, isVar, ok bool)) {
func (p *Context) initLink(line string, prefix int, f func(inPkgName string) (fullName string, isVar, ok bool)) {
text := strings.TrimSpace(line[prefix:])
if idx := strings.IndexByte(text, ' '); idx > 0 {
inPkgName := text[:idx]
Expand Down Expand Up @@ -547,12 +548,23 @@ func (p *context) initPyModule() {
}

// ParsePkgSyntax parses AST of a package to check llgo:type in type declaration.
func ParsePkgSyntax(prog llssa.Program, pkg *types.Package, files []*ast.File) {
func ParsePkgSyntax(ctx *Context, prog llssa.Program, pkg *types.Package, files []*ast.File) {
pkgPath := pkg.Path()
for _, file := range files {
for _, decl := range file.Decls {
switch decl := decl.(type) {
case *ast.FuncDecl:
fullName, inPkgName := astFuncName(pkgPath, decl)
ctx.initLinknameByDoc(decl.Doc, fullName, inPkgName, false)
case *ast.GenDecl:
switch decl.Tok {
case token.VAR:
if len(decl.Specs) == 1 {
if names := decl.Specs[0].(*ast.ValueSpec).Names; len(names) == 1 {
inPkgName := names[0].Name
ctx.initLinknameByDoc(decl.Doc, pkgPath+"."+inPkgName, inPkgName, true)
}
}
case token.TYPE:
handleTypeDecl(prog, pkg, decl)
}
Expand Down
7 changes: 4 additions & 3 deletions internal/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func Do(args []string, conf *Config) {
patches := make(cl.Patches, len(altPkgPaths))
altSSAPkgs(progSSA, patches, altPkgs[1:], verbose)

ctx := &context{progSSA, prog, dedup, patches, make(map[string]none), initial, mode}
ctx := &context{cl.NewContext(), progSSA, prog, dedup, patches, make(map[string]none), initial, mode}
pkgs := buildAllPkgs(ctx, initial, verbose)

var llFiles []string
Expand Down Expand Up @@ -221,6 +221,7 @@ const (
)

type context struct {
ctx *cl.Context
progSSA *ssa.Program
prog llssa.Program
dedup packages.Deduper
Expand Down Expand Up @@ -251,7 +252,7 @@ func buildAllPkgs(ctx *context, initial []*packages.Package, verbose bool) (pkgs
case cl.PkgDeclOnly:
// skip packages that only contain declarations
// and set no export file
cl.ParsePkgSyntax(ctx.prog, pkg.Types, pkg.Syntax)
cl.ParsePkgSyntax(ctx.ctx, ctx.prog, pkg.Types, pkg.Syntax)
pkg.ExportFile = ""
case cl.PkgLinkIR, cl.PkgLinkExtern, cl.PkgPyModule:
if isPkgInLLGo(pkg.PkgPath) {
Expand Down Expand Up @@ -429,7 +430,7 @@ func buildPkg(ctx *context, aPkg *aPackage, verbose bool) {
cl.SetDebug(cl.DbgFlagAll)
}

ret, err := cl.NewPackageEx(ctx.prog, ctx.patches, aPkg.SSA, syntax)
ret, err := cl.NewPackageEx(ctx.ctx, ctx.prog, ctx.patches, aPkg.SSA, syntax)
if showDetail {
llssa.SetDebug(0)
cl.SetDebug(0)
Expand Down

0 comments on commit 744170c

Please sign in to comment.