2019-01-03 00:46:06 -08:00
<p align="center">
<img src="logo.svg" width="40%" border="0" alt="avo" />
2019-01-03 18:39:59 -08:00
<br />
<a href="https://app.shippable.com/github/mmcloughlin/avo/dashboard"><img src="https://api.shippable.com/projects/5bf9e8f059e32e0700ec360f/badge?branch=master" alt="Build Status" /></a>
<a href="http://godoc.org/github.com/mmcloughlin/avo"><img src="http://img.shields.io/badge/godoc-reference-5272B4.svg" alt="GoDoc" /></a>
2019-01-03 00:46:06 -08:00
</p>
2018-11-04 09:47:49 -08:00
2019-01-03 22:02:02 -08:00
<p align="center">High-level Golang x86 Assembly Generator</p>
2019-01-03 20:02:32 -08:00
2019-01-03 22:02:02 -08:00
`avo` aims to make high-performance Go assembly easier to write, review and maintain. It's a Go package that presents a familiar assembly-like interface, together with features to simplify development without sacrificing performance:
* `avo` programs _ are _ Go programs: use **control structures ** for assembly generation
* **Register allocation**: write your kernels with **virtual registers ** and `avo` assigns physical registers for you
* Automatic **parameter load/stores ** : ensure memory offsets are always correct even for complex data structures
* Generation of **stub files ** to interface with your Go package
Inspired by the [PeachPy ](https://github.com/Maratyszcza/PeachPy ) and [asmjit ](https://github.com/asmjit/asmjit ) projects.
2019-01-06 15:59:23 -08:00
_Note: APIs subject to change while `avo` is still in an experimental phase. You can use it to build [real things ](examples ) but we suggest you pin a version with your package manager of choice._
2019-01-03 20:02:32 -08:00
## Install
Install `avo` with `go get` :
```
$ go get -u github.com/mmcloughlin/avo
```
## Quick Start
`avo` assembly generators are pure Go programs. Let's get started with a function that adds two `uint64` values.
[embedmd]:# (examples/add/asm.go)
```go
// +build ignore
package main
import (
. "github.com/mmcloughlin/avo/build"
)
func main() {
2019-01-06 20:16:26 -08:00
TEXT("Add", NOSPLIT, "func(x, y uint64) uint64")
2019-01-03 20:02:32 -08:00
Doc("Add adds x and y.")
2019-01-04 18:23:44 -08:00
x := Load(Param("x"), GP64())
y := Load(Param("y"), GP64())
2019-01-03 20:02:32 -08:00
ADDQ(x, y)
Store(y, ReturnIndex(0))
RET()
Generate()
}
```
You can `go run` this code to see the assembly output. To integrate this into the rest of your Go package we recommend a [`go:generate` ](https://blog.golang.org/generate ) line to produce the assembly and the corresponding Go stub file.
[embedmd]:# (examples/add/add_test.go go /.*go:generate.*/)
```go
//go:generate go run asm.go -out add.s -stubs stub.go
```
After running `go generate` the [`add.s` ](examples/add/add.s ) file will contain the Go assembly.
[embedmd]:# (examples/add/add.s)
```s
// Code generated by command: go run asm.go -out add.s -stubs stub.go. DO NOT EDIT.
2019-01-06 20:16:26 -08:00
#include "textflag.h"
2019-01-03 20:02:32 -08:00
// func Add(x uint64, y uint64) uint64
2019-01-06 20:16:26 -08:00
TEXT ·Add(SB), NOSPLIT, $0-24
2019-01-03 20:02:32 -08:00
MOVQ x(FP), AX
MOVQ y+8(FP), CX
ADDQ AX, CX
MOVQ CX, ret+16(FP)
RET
```
The same call will produce the stub file [`stub.go` ](examples/add/stub.go ) which will enable the function to be called from your Go code.
[embedmd]:# (examples/add/stub.go)
```go
// Code generated by command: go run asm.go -out add.s -stubs stub.go. DO NOT EDIT.
package add
// Add adds x and y.
func Add(x uint64, y uint64) uint64
```
See the [`examples/add` ](examples/add ) directory for the complete working example.
2019-01-03 22:20:25 -08:00
## Examples
2019-01-03 22:23:51 -08:00
See [`examples` ](examples ) for the full suite of examples.
2019-01-03 22:20:25 -08:00
### Slice Sum
Sum a slice of `uint64` s:
[embedmd]:# (examples/sum/asm.go /func main/ /^}/)
```go
func main() {
2019-01-06 20:16:26 -08:00
TEXT("Sum", NOSPLIT, "func(xs []uint64) uint64")
2019-01-03 22:20:25 -08:00
Doc("Sum returns the sum of the elements in xs.")
2019-01-04 18:23:44 -08:00
ptr := Load(Param("xs").Base(), GP64())
n := Load(Param("xs").Len(), GP64())
2019-01-06 18:06:29 -08:00
// Initialize sum register to zero.
2019-01-04 18:23:44 -08:00
s := GP64()
2019-01-03 22:20:25 -08:00
XORQ(s, s)
2019-01-06 18:06:29 -08:00
// Loop until zero bytes remain.
2019-01-05 18:18:49 -08:00
Label("loop")
2019-01-05 17:44:10 -08:00
CMPQ(n, Imm(0))
JE(LabelRef("done"))
2019-01-06 18:06:29 -08:00
// Load from pointer and add to running sum.
2019-01-05 17:44:10 -08:00
ADDQ(Mem{Base: ptr}, s)
2019-01-06 18:06:29 -08:00
// Advance pointer, decrement byte count.
2019-01-05 17:44:10 -08:00
ADDQ(Imm(8), ptr)
2019-01-03 22:20:25 -08:00
DECQ(n)
2019-01-05 17:44:10 -08:00
JMP(LabelRef("loop"))
2019-01-06 18:06:29 -08:00
// Store sum to return value.
2019-01-05 18:18:49 -08:00
Label("done")
2019-01-03 22:20:25 -08:00
Store(s, ReturnIndex(0))
RET()
Generate()
}
```
2019-01-06 18:06:29 -08:00
The result from this code generator is:
[embedmd]:# (examples/sum/sum.s)
```s
// Code generated by command: go run asm.go -out sum.s -stubs stub.go. DO NOT EDIT.
2019-01-06 20:16:26 -08:00
#include "textflag.h"
2019-01-06 18:06:29 -08:00
// func Sum(xs []uint64) uint64
2019-01-06 20:16:26 -08:00
TEXT ·Sum(SB), NOSPLIT, $0-32
2019-01-06 18:06:29 -08:00
MOVQ xs_base(FP), AX
MOVQ xs_len+8(FP), CX
XORQ DX, DX
loop:
CMPQ CX, $0x00
JE done
ADDQ (AX), DX
ADDQ $0x08, AX
DECQ CX
JMP loop
done:
MOVQ DX, ret+24(FP)
RET
```
Full example at [`examples/sum` ](examples/sum ).
2019-01-03 22:20:25 -08:00
### Parameter Load/Store
`avo` provides deconstruction of complex data datatypes into components. For example, load the length of a string argument with:
[embedmd]:# (examples/args/asm.go go /.*TEXT.*StringLen/ /Load.*/)
```go
2019-01-06 20:16:26 -08:00
TEXT("StringLen", NOSPLIT, "func(s string) int")
2019-01-04 18:23:44 -08:00
strlen := Load(Param("s").Len(), GP64())
2019-01-03 22:20:25 -08:00
```
Index an array:
[embedmd]:# (examples/args/asm.go go /.*TEXT.*ArrayThree/ /Load.*/)
```go
2019-01-06 20:16:26 -08:00
TEXT("ArrayThree", NOSPLIT, "func(a [7]uint64) uint64")
2019-01-04 18:23:44 -08:00
a3 := Load(Param("a").Index(3), GP64())
2019-01-03 22:20:25 -08:00
```
Access a struct field (provided you have loaded your package with the `Package` function):
[embedmd]:# (examples/args/asm.go go /.*TEXT.*FieldFloat64/ /Load.*/)
```go
2019-01-06 20:16:26 -08:00
TEXT("FieldFloat64", NOSPLIT, "func(s Struct) float64")
2019-01-04 18:23:44 -08:00
f64 := Load(Param("s").Field("Float64"), XMM())
2019-01-03 22:20:25 -08:00
```
Component accesses can be arbitrarily nested:
[embedmd]:# (examples/args/asm.go go /.*TEXT.*FieldArrayTwoBTwo/ /Load.*/)
```go
2019-01-06 20:16:26 -08:00
TEXT("FieldArrayTwoBTwo", NOSPLIT, "func(s Struct) byte")
2019-01-04 18:23:44 -08:00
b2 := Load(Param("s").Field("Array").Index(2).Field("B").Index(2), GP8())
2019-01-03 22:20:25 -08:00
```
2019-01-03 22:23:51 -08:00
Very similar techniques apply to writing return values. See [`examples/args` ](examples/args ) and [`examples/returns` ](examples/returns ) for more.
2019-01-03 22:20:25 -08:00
2019-01-06 17:31:45 -08:00
### SHA-1
[SHA-1 ](https://en.wikipedia.org/wiki/SHA-1 ) is an excellent example of how powerful this kind of technique can be. The following is a (hopefully) clearly structured implementation of SHA-1 in `avo` , which ultimately generates a [1000+ line impenetrable assembly file ](examples/sha1/sha1.s ).
[embedmd]:# (examples/sha1/asm.go /func main/ /^}/)
```go
func main() {
2019-01-06 20:16:26 -08:00
TEXT("block", 0, "func(h *[5]uint32, m []byte)")
2019-01-06 17:31:45 -08:00
Doc("block SHA-1 hashes the 64-byte message m into the running state h.")
h := Mem{Base: Load(Param("h"), GP64())}
m := Mem{Base: Load(Param("m").Base(), GP64())}
// Store message values on the stack.
w := AllocLocal(64)
W := func(r int) Mem { return w.Offset((r % 16) * 4) }
// Load initial hash.
h0, h1, h2, h3, h4 := GP32(), GP32(), GP32(), GP32(), GP32()
MOVL(h.Offset(0), h0)
MOVL(h.Offset(4), h1)
MOVL(h.Offset(8), h2)
MOVL(h.Offset(12), h3)
MOVL(h.Offset(16), h4)
// Initialize registers.
a, b, c, d, e := GP32(), GP32(), GP32(), GP32(), GP32()
MOVL(h0, a)
MOVL(h1, b)
MOVL(h2, c)
MOVL(h3, d)
MOVL(h4, e)
// Generate round updates.
quarter := []struct {
F func(Register, Register, Register) Register
K uint32
}{
{choose, 0x5a827999},
{xor, 0x6ed9eba1},
{majority, 0x8f1bbcdc},
{xor, 0xca62c1d6},
}
for r := 0; r < 80; r++ {
q := quarter[r/20]
// Load message value.
u := GP32()
if r < 16 {
MOVL(m.Offset(4*r), u)
BSWAPL(u)
} else {
MOVL(W(r-3), u)
XORL(W(r-8), u)
XORL(W(r-14), u)
XORL(W(r-16), u)
ROLL(U8(1), u)
}
MOVL(u, W(r))
// Compute the next state register.
t := GP32()
MOVL(a, t)
ROLL(U8(5), t)
ADDL(q.F(b, c, d), t)
ADDL(e, t)
ADDL(U32(q.K), t)
ADDL(u, t)
// Update registers.
ROLL(Imm(30), b)
a, b, c, d, e = t, a, b, c, d
}
// Final add.
ADDL(a, h0)
ADDL(b, h1)
ADDL(c, h2)
ADDL(d, h3)
ADDL(e, h4)
// Store results back.
MOVL(h0, h.Offset(0))
MOVL(h1, h.Offset(4))
MOVL(h2, h.Offset(8))
MOVL(h3, h.Offset(12))
MOVL(h4, h.Offset(16))
RET()
Generate()
}
```
This relies on the bitwise functions that are defined as subroutines. For example here is bitwise `choose` ; the others are similar.
[embedmd]:# (examples/sha1/asm.go /func choose/ /^}/)
```go
func choose(b, c, d Register) Register {
r := GP32()
MOVL(d, r)
XORL(c, r)
ANDL(b, r)
XORL(d, r)
return r
}
```
2019-01-06 17:34:07 -08:00
See the complete code at [`examples/sha1` ](examples/sha1 ).
2019-01-03 22:20:25 -08:00
### Real Examples
2019-01-05 00:36:14 -08:00
* **[fnv1a ](examples/fnv1a ):** [FNV-1a ](https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1a_hash ) hash function.
* **[dot ](examples/dot ):** Vector dot product.
* **[geohash ](examples/geohash ):** Integer [geohash ](https://en.wikipedia.org/wiki/Geohash ) encoding.
* **[stadtx ](examples/stadtx ):** [`StadtX` hash ](https://github.com/demerphq/BeagleHash ) port from [dgryski/go-stadtx ](https://github.com/dgryski/go-stadtx ).
2019-01-03 22:20:25 -08:00
2019-01-03 20:49:03 -08:00
## Contributing
Contributions to `avo` are welcome:
* Feedback from using `avo` in a real project is incredibly valuable.
* [Submit bug reports ](https://github.com/mmcloughlin/avo/issues/new ) to the issues page.
* Pull requests accepted. Take a look at outstanding [issues ](https://github.com/mmcloughlin/avo/issues ) for ideas (especially the ["good first issue" ](https://github.com/mmcloughlin/avo/labels/good%20first%20issue ) label).
2019-01-03 20:02:32 -08:00
## License
`avo` is available under the [BSD 3-Clause License ](LICENSE ).