From 6202c2c9aad9e49bb343359cabe2ee319fb46b45 Mon Sep 17 00:00:00 2001 From: Michael McLoughlin Date: Wed, 2 Jan 2019 21:35:17 -0800 Subject: [PATCH] ast,build: add build constraints to File Updates #3 --- ast.go | 6 ++++-- build/context.go | 23 +++++++++++++++++++++++ build/global.go | 5 +++++ printer/goasm.go | 19 ++++++++++++------- printer/goasm_test.go | 14 ++++++++++++++ 5 files changed, 58 insertions(+), 9 deletions(-) diff --git a/ast.go b/ast.go index d194c9b..c0ba402 100644 --- a/ast.go +++ b/ast.go @@ -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,8 +95,9 @@ type Section interface { // File represents an assembly file. type File struct { - Includes []string - Sections []Section + Constraints buildtags.Constraints + Includes []string + Sections []Section } func NewFile() *File { diff --git a/build/context.go b/build/context.go index 50aa33a..948e042 100644 --- a/build/context.go +++ b/build/context.go @@ -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) diff --git a/build/global.go b/build/global.go index 01e278f..34ddeb7 100644 --- a/build/global.go +++ b/build/global.go @@ -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() } diff --git a/printer/goasm.go b/printer/goasm.go index 2685062..b517c18 100644 --- a/printer/goasm.go +++ b/printer/goasm.go @@ -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) } diff --git a/printer/goasm_test.go b/printer/goasm_test.go index e84ea4e..893249e 100644 --- a/printer/goasm_test.go +++ b/printer/goasm_test.go @@ -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", + "", + }) +}