Permalink
Browse files

build: unify Label function signatures

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 #35
  • Loading branch information...
mmcloughlin committed Jan 6, 2019
1 parent 87ffa68 commit 602bb5197c749ef848e12394ae4e686140064ca4
@@ -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()
5 ast.go
@@ -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"
@@ -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
@@ -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
}
@@ -1,4 +1,5 @@
package avo
// Package attr provides attributes for text and data sections.
package attr

import (
"fmt"
@@ -1,4 +1,4 @@
package avo
package attr

import "testing"

@@ -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"
@@ -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
}

@@ -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 {
@@ -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
}

@@ -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.
@@ -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)
@@ -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) }
@@ -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"

@@ -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
}
@@ -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"
)
@@ -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"))

@@ -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"))

@@ -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])
}
@@ -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()
@@ -31,7 +31,7 @@ func main() {
DECQ(n)
JMP(LabelRef("loop"))
LABEL("done")
Label("done")
Store(h, ReturnIndex(0))
RET()
Generate()
@@ -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()
@@ -35,7 +35,7 @@ func main() {
DECQ(n)

JMP(LabelRef("loop"))
LABEL("done")
Label("done")
Store(h, ReturnIndex(0))
RET()
Generate()
Oops, something went wrong.

0 comments on commit 602bb51

Please sign in to comment.