6
ast.go
6
ast.go
@@ -3,6 +3,7 @@ package avo
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
|
"github.com/mmcloughlin/avo/buildtags"
|
||||||
"github.com/mmcloughlin/avo/gotypes"
|
"github.com/mmcloughlin/avo/gotypes"
|
||||||
"github.com/mmcloughlin/avo/operand"
|
"github.com/mmcloughlin/avo/operand"
|
||||||
"github.com/mmcloughlin/avo/reg"
|
"github.com/mmcloughlin/avo/reg"
|
||||||
@@ -94,8 +95,9 @@ type Section interface {
|
|||||||
|
|
||||||
// File represents an assembly file.
|
// File represents an assembly file.
|
||||||
type File struct {
|
type File struct {
|
||||||
Includes []string
|
Constraints buildtags.Constraints
|
||||||
Sections []Section
|
Includes []string
|
||||||
|
Sections []Section
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFile() *File {
|
func NewFile() *File {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"go/types"
|
"go/types"
|
||||||
|
|
||||||
"github.com/mmcloughlin/avo"
|
"github.com/mmcloughlin/avo"
|
||||||
|
"github.com/mmcloughlin/avo/buildtags"
|
||||||
"github.com/mmcloughlin/avo/gotypes"
|
"github.com/mmcloughlin/avo/gotypes"
|
||||||
"github.com/mmcloughlin/avo/operand"
|
"github.com/mmcloughlin/avo/operand"
|
||||||
"github.com/mmcloughlin/avo/reg"
|
"github.com/mmcloughlin/avo/reg"
|
||||||
@@ -46,6 +47,28 @@ func (c *Context) Package(path string) {
|
|||||||
c.pkg = pkg
|
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) {
|
func (c *Context) Function(name string) {
|
||||||
c.function = avo.NewFunction(name)
|
c.function = avo.NewFunction(name)
|
||||||
c.file.AddSection(c.function)
|
c.file.AddSection(c.function)
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/mmcloughlin/avo/buildtags"
|
||||||
"github.com/mmcloughlin/avo/gotypes"
|
"github.com/mmcloughlin/avo/gotypes"
|
||||||
"github.com/mmcloughlin/avo/operand"
|
"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 GP8v() reg.GPVirtual { return ctx.GP8v() }
|
||||||
func GP16v() reg.GPVirtual { return ctx.GP16v() }
|
func GP16v() reg.GPVirtual { return ctx.GP16v() }
|
||||||
func GP32v() reg.GPVirtual { return ctx.GP32v() }
|
func GP32v() reg.GPVirtual { return ctx.GP32v() }
|
||||||
|
|||||||
@@ -22,8 +22,7 @@ func NewGoAsm(cfg Config) Printer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *goasm) Print(f *avo.File) ([]byte, error) {
|
func (p *goasm) Print(f *avo.File) ([]byte, error) {
|
||||||
p.header()
|
p.header(f)
|
||||||
p.includes(f.Includes)
|
|
||||||
for _, s := range f.Sections {
|
for _, s := range f.Sections {
|
||||||
switch s := s.(type) {
|
switch s := s.(type) {
|
||||||
case *avo.Function:
|
case *avo.Function:
|
||||||
@@ -37,15 +36,21 @@ func (p *goasm) Print(f *avo.File) ([]byte, error) {
|
|||||||
return p.Result()
|
return p.Result()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *goasm) header() {
|
func (p *goasm) header(f *avo.File) {
|
||||||
p.Comment(p.cfg.GeneratedWarning())
|
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) {
|
func (p *goasm) includes(paths []string) {
|
||||||
if len(paths) == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
p.NL()
|
|
||||||
for _, path := range paths {
|
for _, path := range paths {
|
||||||
p.Printf("#include \"%s\"\n", path)
|
p.Printf("#include \"%s\"\n", path)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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",
|
||||||
|
"",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user