Skip to content

Commit

Permalink
build: unify Label function signatures
Browse files Browse the repository at this point in the history
The Context.Label method and LABEL global function did not agree. Also
breaks the convention I'd like to set that capitalized functions must
agree with existing Go assembly syntax.

To help avoid a conflict with `avo.Label`, attributes were moved to
their own package.

Fixes mmcloughlin#35
  • Loading branch information
mmcloughlin committed Jan 6, 2019
1 parent 87ffa68 commit 602bb51
Show file tree
Hide file tree
Showing 17 changed files with 71 additions and 68 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@ func main() {
n := Load(Param("xs").Len(), GP64())
s := GP64()
XORQ(s, s)
LABEL("loop")
Label("loop")
CMPQ(n, Imm(0))
JE(LabelRef("done"))
ADDQ(Mem{Base: ptr}, s)
ADDQ(Imm(8), ptr)
DECQ(n)
JMP(LabelRef("loop"))
LABEL("done")
Label("done")
Store(s, ReturnIndex(0))
RET()
Generate()
Expand Down
5 changes: 3 additions & 2 deletions ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package avo
import (
"errors"

"github.com/mmcloughlin/avo/attr"
"github.com/mmcloughlin/avo/buildtags"
"github.com/mmcloughlin/avo/gotypes"
"github.com/mmcloughlin/avo/operand"
Expand Down Expand Up @@ -128,7 +129,7 @@ func (f *File) Functions() []*Function {
// Function represents an assembly function.
type Function struct {
Name string
Attributes Attribute
Attributes attr.Attribute
Doc []string
Signature *gotypes.Signature
LocalSize int
Expand Down Expand Up @@ -236,7 +237,7 @@ func (d Datum) Overlaps(other Datum) bool {
// Global represents a DATA section.
type Global struct {
Symbol operand.Symbol
Attributes Attribute
Attributes attr.Attribute
Data []Datum
Size int
}
Expand Down
3 changes: 2 additions & 1 deletion attr.go → attr/attr.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package avo
// Package attr provides attributes for text and data sections.
package attr

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion attr_test.go → attr/attr_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package avo
package attr

import "testing"

Expand Down
11 changes: 6 additions & 5 deletions build/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"go/types"

"github.com/mmcloughlin/avo"
"github.com/mmcloughlin/avo/attr"
"github.com/mmcloughlin/avo/buildtags"
"github.com/mmcloughlin/avo/gotypes"
"github.com/mmcloughlin/avo/operand"
Expand Down Expand Up @@ -90,7 +91,7 @@ func (c *Context) Doc(lines ...string) {
}

// Attributes sets function attributes for the currently active function.
func (c *Context) Attributes(a avo.Attribute) {
func (c *Context) Attributes(a attr.Attribute) {
c.activefunc().Attributes = a
}

Expand Down Expand Up @@ -124,12 +125,12 @@ func (c *Context) AllocLocal(size int) operand.Mem {

// Instruction adds an instruction to the active function.
func (c *Context) Instruction(i *avo.Instruction) {
c.activefunc().AddNode(i)
c.activefunc().AddInstruction(i)
}

// Label adds a label to the active function.
func (c *Context) Label(l avo.Label) {
c.activefunc().AddLabel(l)
func (c *Context) Label(name string) {
c.activefunc().AddLabel(avo.Label(name))
}

func (c *Context) activefunc() *avo.Function {
Expand All @@ -150,7 +151,7 @@ func (c *Context) StaticGlobal(name string) operand.Mem {
}

// DataAttributes sets the attributes on the current active global data section.
func (c *Context) DataAttributes(a avo.Attribute) {
func (c *Context) DataAttributes(a attr.Attribute) {
c.activeglobal().Attributes = a
}

Expand Down
13 changes: 6 additions & 7 deletions build/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"flag"
"os"

"github.com/mmcloughlin/avo/attr"
"github.com/mmcloughlin/avo/buildtags"
"github.com/mmcloughlin/avo/gotypes"
"github.com/mmcloughlin/avo/operand"

"github.com/mmcloughlin/avo/reg"

"github.com/mmcloughlin/avo"
)

// ctx provides a global build context.
Expand All @@ -22,11 +21,8 @@ func TEXT(name, signature string) {
ctx.SignatureExpr(signature)
}

// LABEL adds a label to the active function.
func LABEL(name string) { ctx.Label(avo.Label(name)) }

// GLOBL declares a new static global data section with the given attributes.
func GLOBL(name string, a avo.Attribute) operand.Mem {
func GLOBL(name string, a attr.Attribute) operand.Mem {
// TODO(mbm): should this be static?
g := ctx.StaticGlobal(name)
ctx.DataAttributes(a)
Expand Down Expand Up @@ -119,11 +115,14 @@ func Store(src reg.Register, dst gotypes.Component) { ctx.Store(src, dst) }
func Doc(lines ...string) { ctx.Doc(lines...) }

// Attributes sets function attributes for the currently active function.
func Attributes(a avo.Attribute) { ctx.Attributes(a) }
func Attributes(a attr.Attribute) { ctx.Attributes(a) }

// AllocLocal allocates size bytes in the stack of the currently active function.
// Returns a reference to the base pointer for the newly allocated region.
func AllocLocal(size int) operand.Mem { return ctx.AllocLocal(size) }

// Label adds a label to the active function.
func Label(name string) { ctx.Label(name) }

// ConstData builds a static data section containing just the given constant.
func ConstData(name string, v operand.Constant) operand.Mem { return ctx.ConstData(name, v) }
4 changes: 2 additions & 2 deletions build/pseudo.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package build

import (
"github.com/mmcloughlin/avo"
"github.com/mmcloughlin/avo/attr"
"github.com/mmcloughlin/avo/operand"
"github.com/mmcloughlin/avo/reg"

Expand Down Expand Up @@ -57,7 +57,7 @@ func (c *Context) Store(src reg.Register, dst gotypes.Component) {
// ConstData builds a static data section containing just the given constant.
func (c *Context) ConstData(name string, v operand.Constant) operand.Mem {
g := c.StaticGlobal(name)
c.DataAttributes(avo.RODATA | avo.NOPTR)
c.DataAttributes(attr.RODATA | attr.NOPTR)
c.AppendDatum(v)
return g
}
2 changes: 1 addition & 1 deletion examples/data/asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package main
import (
"math"

. "github.com/mmcloughlin/avo"
. "github.com/mmcloughlin/avo/attr"
. "github.com/mmcloughlin/avo/build"
. "github.com/mmcloughlin/avo/operand"
)
Expand Down
8 changes: 4 additions & 4 deletions examples/dot/asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func main() {
// Loop over blocks and process them with vector instructions.
blockitems := 8 * unroll
blocksize := 4 * blockitems
LABEL("blockloop")
Label("blockloop")
CMPQ(n, U32(blockitems))
JL(LabelRef("tail"))

Expand All @@ -55,11 +55,11 @@ func main() {
JMP(LabelRef("blockloop"))

// Process any trailing entries.
LABEL("tail")
Label("tail")
tail := XMM()
VXORPS(tail, tail, tail)

LABEL("tailloop")
Label("tailloop")
CMPQ(n, U32(0))
JE(LabelRef("reduce"))

Expand All @@ -73,7 +73,7 @@ func main() {
JMP(LabelRef("tailloop"))

// Reduce the lanes to one.
LABEL("reduce")
Label("reduce")
for i := 1; i < unroll; i++ {
VADDPS(acc[0], acc[i], acc[0])
}
Expand Down
4 changes: 2 additions & 2 deletions examples/fnv1a/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func main() {
p := GP64()
MOVQ(Imm(Prime), p)

LABEL("loop")
Label("loop")
CMPQ(n, Imm(0))
JE(LabelRef("done"))
b := GP64()
Expand All @@ -31,7 +31,7 @@ func main() {
DECQ(n)

JMP(LabelRef("loop"))
LABEL("done")
Label("done")
Store(h, ReturnIndex(0))
RET()
Generate()
Expand Down
4 changes: 2 additions & 2 deletions examples/fnv1a/asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func main() {
p := GP64()
MOVQ(Imm(Prime), p)

LABEL("loop")
Label("loop")
CMPQ(n, Imm(0))
JE(LabelRef("done"))
b := GP64()
Expand All @@ -35,7 +35,7 @@ func main() {
DECQ(n)

JMP(LabelRef("loop"))
LABEL("done")
Label("done")
Store(h, ReturnIndex(0))
RET()
Generate()
Expand Down
Loading

0 comments on commit 602bb51

Please sign in to comment.