Skip to content

Commit

Permalink
pass: add textflags.h if required
Browse files Browse the repository at this point in the history
  • Loading branch information
mmcloughlin committed Dec 31, 2018
1 parent 4aaf6bc commit c62e40f
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 25 deletions.
4 changes: 1 addition & 3 deletions ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ type File struct {
}

func NewFile() *File {
return &File{
Includes: []string{"textflag.h"},
}
return &File{}
}

func (f *File) AddSection(s Section) {
Expand Down
2 changes: 0 additions & 2 deletions examples/add/add.s
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 0 additions & 2 deletions examples/args/args.s
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 0 additions & 2 deletions examples/complex/complex.s
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 0 additions & 2 deletions examples/dot/dot.s
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 0 additions & 2 deletions examples/fnv1a/fnv1a.s
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 0 additions & 2 deletions examples/sha1/sha1.s
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 0 additions & 2 deletions examples/stadtx/stadtx.s
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 0 additions & 2 deletions examples/sum/sum.s
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions pass/pass.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var Compile = Concat(
FunctionPass(AllocateRegisters),
FunctionPass(BindRegisters),
FunctionPass(VerifyAllocation),
Func(IncludeTextFlagHeader),
)

type Interface interface {
Expand Down
41 changes: 41 additions & 0 deletions pass/textflag.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 0 additions & 4 deletions printer/goasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 0 additions & 2 deletions tests/cast/cast.s
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit c62e40f

Please sign in to comment.