From c62e40f8d29027a074a9e7b602f22a2eb5e85f64 Mon Sep 17 00:00:00 2001 From: Michael McLoughlin Date: Mon, 31 Dec 2018 00:23:15 -0800 Subject: [PATCH] pass: add textflags.h if required Closes #12 --- ast.go | 4 +--- examples/add/add.s | 2 -- examples/args/args.s | 2 -- examples/complex/complex.s | 2 -- examples/dot/dot.s | 2 -- examples/fnv1a/fnv1a.s | 2 -- examples/sha1/sha1.s | 2 -- examples/stadtx/stadtx.s | 2 -- examples/sum/sum.s | 2 -- pass/pass.go | 1 + pass/textflag.go | 41 ++++++++++++++++++++++++++++++++++++++ printer/goasm_test.go | 4 ---- tests/cast/cast.s | 2 -- 13 files changed, 43 insertions(+), 25 deletions(-) create mode 100644 pass/textflag.go diff --git a/ast.go b/ast.go index a332726d..99aeaeea 100644 --- a/ast.go +++ b/ast.go @@ -99,9 +99,7 @@ type File struct { } func NewFile() *File { - return &File{ - Includes: []string{"textflag.h"}, - } + return &File{} } func (f *File) AddSection(s Section) { diff --git a/examples/add/add.s b/examples/add/add.s index aad83ce4..e220a3fe 100644 --- a/examples/add/add.s +++ b/examples/add/add.s @@ -1,7 +1,5 @@ // Code generated by command: go run asm.go -out add.s -stubs stub.go. DO NOT EDIT. -#include "textflag.h" - // func Add(x uint64, y uint64) uint64 TEXT ·Add(SB), $0-24 MOVQ x(FP), AX diff --git a/examples/args/args.s b/examples/args/args.s index 9a881f31..90204f70 100644 --- a/examples/args/args.s +++ b/examples/args/args.s @@ -1,7 +1,5 @@ // Code generated by command: go run asm.go -out args.s -stubs stub.go. DO NOT EDIT. -#include "textflag.h" - // func StringLen(s string) int TEXT ·StringLen(SB), $0-24 MOVQ s_len+8(FP), AX diff --git a/examples/complex/complex.s b/examples/complex/complex.s index aaed05c9..a4462674 100644 --- a/examples/complex/complex.s +++ b/examples/complex/complex.s @@ -1,7 +1,5 @@ // Code generated by command: go run asm.go -out complex.s -stubs stub.go. DO NOT EDIT. -#include "textflag.h" - // func Real(z complex128) float64 TEXT ·Real(SB), $0-24 MOVSD z_real(FP), X0 diff --git a/examples/dot/dot.s b/examples/dot/dot.s index 2ff37c65..6297bf9c 100644 --- a/examples/dot/dot.s +++ b/examples/dot/dot.s @@ -1,7 +1,5 @@ // Code generated by command: go run asm.go -out dot.s -stubs stub.go. DO NOT EDIT. -#include "textflag.h" - // func Dot(x []float32, y []float32) float32 TEXT ·Dot(SB), $0-52 MOVQ x_base(FP), AX diff --git a/examples/fnv1a/fnv1a.s b/examples/fnv1a/fnv1a.s index dacb54d4..ffa1964e 100644 --- a/examples/fnv1a/fnv1a.s +++ b/examples/fnv1a/fnv1a.s @@ -1,7 +1,5 @@ // Code generated by command: go run asm.go -out fnv1a.s -stubs stub.go. DO NOT EDIT. -#include "textflag.h" - // func Hash64(data []byte) uint64 TEXT ·Hash64(SB), $0-32 MOVQ data_base(FP), CX diff --git a/examples/sha1/sha1.s b/examples/sha1/sha1.s index 9f564867..55d1abee 100644 --- a/examples/sha1/sha1.s +++ b/examples/sha1/sha1.s @@ -1,7 +1,5 @@ // Code generated by command: go run asm.go -out sha1.s -stubs stub.go. DO NOT EDIT. -#include "textflag.h" - // func block(h *[5]uint32, m []byte) TEXT ·block(SB), $64-32 MOVQ h(FP), AX diff --git a/examples/stadtx/stadtx.s b/examples/stadtx/stadtx.s index 0d91e3f7..52037062 100644 --- a/examples/stadtx/stadtx.s +++ b/examples/stadtx/stadtx.s @@ -1,7 +1,5 @@ // Code generated by command: go run asm.go -out stadtx.s -stubs stub.go. DO NOT EDIT. -#include "textflag.h" - // func Hash(state *State, key []byte) uint64 TEXT ·Hash(SB), $0-40 MOVQ state(FP), AX diff --git a/examples/sum/sum.s b/examples/sum/sum.s index 0282faf7..9862b141 100644 --- a/examples/sum/sum.s +++ b/examples/sum/sum.s @@ -1,7 +1,5 @@ // Code generated by command: go run asm.go -out sum.s -stubs stub.go. DO NOT EDIT. -#include "textflag.h" - // func Sum(xs []uint64) uint64 TEXT ·Sum(SB), $0-32 MOVQ xs_base(FP), AX diff --git a/pass/pass.go b/pass/pass.go index 530bd1e5..2cfc2da1 100644 --- a/pass/pass.go +++ b/pass/pass.go @@ -14,6 +14,7 @@ var Compile = Concat( FunctionPass(AllocateRegisters), FunctionPass(BindRegisters), FunctionPass(VerifyAllocation), + Func(IncludeTextFlagHeader), ) type Interface interface { diff --git a/pass/textflag.go b/pass/textflag.go new file mode 100644 index 00000000..6d4990c9 --- /dev/null +++ b/pass/textflag.go @@ -0,0 +1,41 @@ +package pass + +import ( + "github.com/mmcloughlin/avo" +) + +// IncludeTextFlagHeader includes textflag.h if necessary. +func IncludeTextFlagHeader(f *avo.File) error { + const textflagheader = "textflag.h" + + // Check if we already have it. + for _, path := range f.Includes { + if path == textflagheader { + return nil + } + } + + // Add it if necessary. + if requirestextflags(f) { + f.Includes = append(f.Includes, textflagheader) + } + + return nil +} + +// requirestextflags returns whether the file uses flags in the textflags.h header. +func requirestextflags(f *avo.File) bool { + for _, s := range f.Sections { + var a avo.Attribute + switch s := s.(type) { + case *avo.Function: + a = s.Attributes + case *avo.Global: + a = s.Attributes + } + if a.ContainsTextFlags() { + return true + } + } + return false +} diff --git a/printer/goasm_test.go b/printer/goasm_test.go index 6dd334a6..e84ea4e0 100644 --- a/printer/goasm_test.go +++ b/printer/goasm_test.go @@ -22,8 +22,6 @@ func TestBasic(t *testing.T) { AssertPrintsLines(t, ctx, printer.NewGoAsm, []string{ "// Code generated by avo. DO NOT EDIT.", "", - "#include \"textflag.h\"", - "", "// func add(x uint64, y uint64) uint64", "TEXT ·add(SB), $0-24", "\tMOVQ\tx(FP), AX", @@ -55,8 +53,6 @@ func TestTextDecl(t *testing.T) { AssertPrintsLines(t, ctx, printer.NewGoAsm, []string{ "// Code generated by avo. DO NOT EDIT.", "", - "#include \"textflag.h\"", - "", "// func noargs()", "TEXT ·noargs(SB), $16", // expect only the frame size "\tRET", diff --git a/tests/cast/cast.s b/tests/cast/cast.s index 57d644d5..7fa96e20 100644 --- a/tests/cast/cast.s +++ b/tests/cast/cast.s @@ -1,7 +1,5 @@ // Code generated by command: go run asm.go -out cast.s -stubs stub.go. DO NOT EDIT. -#include "textflag.h" - // func Split(x uint64) (q uint64, l uint32, w uint16, b uint8) TEXT ·Split(SB), $0-23 MOVQ x(FP), AX