ast,build: add build constraints to File

Updates #3
This commit is contained in:
Michael McLoughlin
2019-01-02 21:35:17 -08:00
parent d92a147644
commit 6202c2c9aa
5 changed files with 58 additions and 9 deletions

2
ast.go
View File

@@ -3,6 +3,7 @@ package avo
import (
"errors"
"github.com/mmcloughlin/avo/buildtags"
"github.com/mmcloughlin/avo/gotypes"
"github.com/mmcloughlin/avo/operand"
"github.com/mmcloughlin/avo/reg"
@@ -94,6 +95,7 @@ type Section interface {
// File represents an assembly file.
type File struct {
Constraints buildtags.Constraints
Includes []string
Sections []Section
}

View File

@@ -5,6 +5,7 @@ import (
"go/types"
"github.com/mmcloughlin/avo"
"github.com/mmcloughlin/avo/buildtags"
"github.com/mmcloughlin/avo/gotypes"
"github.com/mmcloughlin/avo/operand"
"github.com/mmcloughlin/avo/reg"
@@ -46,6 +47,28 @@ func (c *Context) Package(path string) {
c.pkg = pkg
}
func (c *Context) BuildConstraints(t buildtags.ConstraintsConvertable) {
cs := t.ToConstraints()
if err := cs.Validate(); err != nil {
c.AddError(err)
return
}
c.file.Constraints = cs
}
func (c *Context) BuildConstraint(t buildtags.ConstraintConvertable) {
c.BuildConstraints(append(c.file.Constraints, t.ToConstraint()))
}
func (c *Context) BuildConstraintExpr(expr string) {
constraint, err := buildtags.ParseConstraint(expr)
if err != nil {
c.AddError(err)
return
}
c.BuildConstraint(constraint)
}
func (c *Context) Function(name string) {
c.function = avo.NewFunction(name)
c.file.AddSection(c.function)

View File

@@ -4,6 +4,7 @@ import (
"flag"
"os"
"github.com/mmcloughlin/avo/buildtags"
"github.com/mmcloughlin/avo/gotypes"
"github.com/mmcloughlin/avo/operand"
@@ -52,6 +53,10 @@ func Generate() {
}
}
func BuildConstraints(t buildtags.ConstraintsConvertable) { ctx.BuildConstraints(t) }
func BuildConstraint(t buildtags.ConstraintConvertable) { ctx.BuildConstraint(t) }
func BuildConstraintExpr(expr string) { ctx.BuildConstraintExpr(expr) }
func GP8v() reg.GPVirtual { return ctx.GP8v() }
func GP16v() reg.GPVirtual { return ctx.GP16v() }
func GP32v() reg.GPVirtual { return ctx.GP32v() }

View File

@@ -22,8 +22,7 @@ func NewGoAsm(cfg Config) Printer {
}
func (p *goasm) Print(f *avo.File) ([]byte, error) {
p.header()
p.includes(f.Includes)
p.header(f)
for _, s := range f.Sections {
switch s := s.(type) {
case *avo.Function:
@@ -37,15 +36,21 @@ func (p *goasm) Print(f *avo.File) ([]byte, error) {
return p.Result()
}
func (p *goasm) header() {
func (p *goasm) header(f *avo.File) {
p.Comment(p.cfg.GeneratedWarning())
if len(f.Constraints) > 0 {
p.NL()
p.Printf(f.Constraints.GoString())
}
if len(f.Includes) > 0 {
p.NL()
p.includes(f.Includes)
}
}
func (p *goasm) includes(paths []string) {
if len(paths) == 0 {
return
}
p.NL()
for _, path := range paths {
p.Printf("#include \"%s\"\n", path)
}

View File

@@ -67,3 +67,17 @@ func TestTextDecl(t *testing.T) {
"",
})
}
func TestConstraints(t *testing.T) {
ctx := build.NewContext()
ctx.BuildConstraintExpr("linux,386 darwin,!cgo")
ctx.BuildConstraintExpr("!noasm")
AssertPrintsLines(t, ctx, printer.NewGoAsm, []string{
"// Code generated by avo. DO NOT EDIT.",
"",
"// +build linux,386 darwin,!cgo",
"// +build !noasm",
"",
})
}