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 />
2023-01-07 10:46:51 -08:00
<img src="https://img.shields.io/github/actions/workflow/status/mmcloughlin/avo/ci.yml?style=flat-square" alt="Build Status" />
2020-02-01 15:50:37 -05:00
<a href="https://pkg.go.dev/github.com/mmcloughlin/avo"><img src="https://img.shields.io/badge/doc-reference-007d9b?logo=go&style=flat-square" alt="go.dev" /></a>
2019-01-13 12:06:47 -08:00
<a href="https://goreportcard.com/report/github.com/mmcloughlin/avo"><img src="https://goreportcard.com/badge/github.com/mmcloughlin/avo?style=flat-square" alt="Go Report Card" /></a>
2019-01-03 00:46:06 -08:00
</p>
2018-11-04 09:47:49 -08:00
2019-01-06 22:37:27 -08:00
<p align="center">Generate x86 Assembly with Go</p>
2019-01-03 20:02:32 -08:00
2019-01-06 22:37:27 -08:00
`avo` makes high-performance Go assembly easier to write, review and maintain. The `avo` package presents a familiar assembly-like interface that simplifies development without sacrificing performance:
2019-01-03 22:02:02 -08:00
2019-01-06 22:37:27 -08:00
* **Use Go control structures** for assembly generation; `avo` programs _ are _ Go programs
* **Register allocation**: write functions with virtual registers and `avo` assigns physical registers for you
2019-01-06 22:39:07 -08:00
* **Automatically load arguments and store return values**: ensure memory offsets are correct for complex structures
2019-01-06 22:37:27 -08:00
* **Generation of stub files** to interface with your Go package
2019-01-03 22:02:02 -08:00
2021-04-21 22:03:43 -07:00
For more about `avo` :
* Introductory talk ["Better `x86` Assembly Generation with Go" ](https://www.youtube.com/watch?v=6Y5CZ7_tyA4 ) at [dotGo 2019 ](https://2019.dotgo.eu/ ) ([slides ](https://speakerdeck.com/mmcloughlin/better-x86-assembly-generation-with-go ))
* [Longer tutorial at Gophercon 2019 ](https://www.youtube.com/watch?v=WaD8sNqroAw ) showing a highly-optimized dot product ([slides ](https://speakerdeck.com/mmcloughlin/better-x86-assembly-generation-with-go-gophercon-2019 ))
2022-02-21 11:21:01 -08:00
* Watch [Filippo Valsorda ](https://filippo.io/ ) live code the [rewrite of `filippo.io/edwards25519` assembly with `avo` ](https://vimeo.com/679848853 )
2022-04-17 23:57:05 -07:00
* Explore [projects using `avo` ](doc/adopters.md )
2021-04-21 22:03:43 -07:00
* Discuss `avo` and general Go assembly topics in the [#assembly ](https://gophers.slack.com/archives/C6WDZJ70S ) channel of [Gophers Slack ](https://invite.slack.golangbridge.org/ )
2019-05-14 21:00:33 -07:00
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
2019-01-06 22:37:27 -08:00
## Quick Start
2019-01-03 20:02:32 -08:00
Install `avo` with `go get` :
```
$ go get -u github.com/mmcloughlin/avo
```
2019-01-06 22:37:27 -08:00
`avo` assembly generators are pure Go programs. Here's a function that adds two `uint64` values:
2019-01-03 20:02:32 -08:00
```go
2021-10-29 01:18:34 -07:00
//go:build ignore
2019-01-03 20:02:32 -08:00
package main
2019-04-13 22:53:13 -05:00
import . "github.com/mmcloughlin/avo/build"
2019-01-03 20:02:32 -08:00
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()
}
```
2019-01-06 22:37:27 -08:00
`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.
2019-01-03 20:02:32 -08:00
```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.
```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-13 11:59:14 -08:00
MOVQ x+0(FP), AX
2019-01-10 21:21:41 -08:00
MOVQ y+8(FP), CX
ADDQ AX, CX
MOVQ CX, ret+16(FP)
2019-01-03 20:02:32 -08:00
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.
```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:
```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
2019-01-11 10:57:38 -08:00
Comment("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
2019-01-05 18:18:49 -08:00
Label("loop")
2019-01-11 11:06:10 -08:00
Comment("Loop until zero bytes remain.")
2019-01-05 17:44:10 -08:00
CMPQ(n, Imm(0))
JE(LabelRef("done"))
2019-01-06 18:06:29 -08:00
2019-01-11 10:57:38 -08:00
Comment("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
2019-01-11 10:57:38 -08:00
Comment("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
2019-01-05 18:18:49 -08:00
Label("done")
2019-01-11 11:06:10 -08:00
Comment("Store sum to return value.")
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:
```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-13 11:59:14 -08:00
MOVQ xs_base+0(FP), AX
2019-01-10 21:21:41 -08:00
MOVQ xs_len+8(FP), CX
2019-01-11 10:57:38 -08:00
// Initialize sum register to zero.
2019-01-10 21:21:41 -08:00
XORQ DX, DX
2019-01-06 18:06:29 -08:00
loop:
2019-01-11 11:06:10 -08:00
// Loop until zero bytes remain.
2019-01-10 21:21:41 -08:00
CMPQ CX, $0x00
JE done
2019-01-11 10:57:38 -08:00
// Load from pointer and add to running sum.
2019-01-10 21:21:41 -08:00
ADDQ (AX), DX
2019-01-11 10:57:38 -08:00
// Advance pointer, decrement byte count.
2019-01-10 21:21:41 -08:00
ADDQ $0x08, AX
DECQ CX
JMP loop
2019-01-06 18:06:29 -08:00
done:
2019-01-11 11:06:10 -08:00
// Store sum to return value.
2019-01-10 21:21:41 -08:00
MOVQ DX, ret+24(FP)
2019-01-06 18:06:29 -08:00
RET
```
Full example at [`examples/sum` ](examples/sum ).
2019-01-07 22:21:16 -08:00
### Features
2019-01-03 22:20:25 -08:00
2019-01-07 22:21:16 -08:00
For demonstrations of `avo` features:
2019-01-03 22:20:25 -08:00
2019-01-07 22:21:16 -08:00
* **[args ](examples/args ):** Loading function arguments.
* **[returns ](examples/returns ):** Building return values.
* **[complex ](examples/complex ):** Working with `complex{64,128}` types.
2019-01-22 22:35:01 -08:00
* **[data ](examples/data ):** Defining `DATA` sections.
* **[ext ](examples/ext ):** Interacting with types from external packages.
2019-09-16 11:01:48 -07:00
* **[pragma ](examples/pragma ):** Apply compiler directives to generated functions.
2019-01-06 17:34:07 -08:00
2019-01-03 22:20:25 -08:00
### Real Examples
2019-01-07 22:21:16 -08:00
Implementations of full algorithms:
* **[sha1 ](examples/sha1 ):** [SHA-1 ](https://en.wikipedia.org/wiki/SHA-1 ) cryptographic hash.
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.
2021-11-12 18:35:36 -08:00
* **[md5x16 ](examples/md5x16 ):** AVX-512 accelerated [MD5 ](https://en.wikipedia.org/wiki/MD5 ).
2019-01-05 00:36:14 -08:00
* **[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
2022-04-17 22:38:54 -07:00
## Adopters
2022-05-08 17:34:16 -07:00
Popular projects[^projects] using `avo` :
[^projects]: Projects drawn from the `avo` third-party test suite. Popularity
2024-03-01 05:30:37 +00:00
estimated from Github star count collected on Mar 1, 2024.
2022-04-17 22:38:54 -07:00
2022-04-23 21:58:06 -07:00
<img src="https://images.weserv.nl?fit=cover&h=24&mask=circle&maxage=7d&url=https%3A%2F%2Fgithub.com%2Fgolang.png&w=24" width="24" height="24" hspace="4" valign="middle" /> [golang / **go** ](https://github.com/golang/go )
2024-03-01 05:30:37 +00:00
:star: 118.2k
2022-04-23 21:58:06 -07:00
> The Go programming language
2022-04-24 20:20:11 -07:00
<img src="https://images.weserv.nl?fit=cover&h=24&mask=circle&maxage=7d&url=https%3A%2F%2Fgithub.com%2Fklauspost.png&w=24" width="24" height="24" hspace="4" valign="middle" /> [klauspost / **compress** ](https://github.com/klauspost/compress )
2024-02-01 05:31:15 +00:00
:star: 4.4k
2022-04-17 22:38:54 -07:00
> Optimized Go Compression Packages
2022-04-24 20:20:11 -07:00
<img src="https://images.weserv.nl?fit=cover&h=24&mask=circle&maxage=7d&url=https%3A%2F%2Fgithub.com%2Fgolang.png&w=24" width="24" height="24" hspace="4" valign="middle" /> [golang / **crypto** ](https://github.com/golang/crypto )
2024-01-01 05:31:31 +00:00
:star: 2.9k
2022-04-17 22:38:54 -07:00
> [mirror] Go supplementary cryptography libraries
2022-04-18 00:49:52 -07:00
<img src="https://images.weserv.nl?fit=cover&h=24&mask=circle&maxage=7d&url=https%3A%2F%2Fgithub.com%2Fklauspost.png&w=24" width="24" height="24" hspace="4" valign="middle" /> [klauspost / **reedsolomon** ](https://github.com/klauspost/reedsolomon )
2024-01-01 05:31:31 +00:00
:star: 1.8k
2022-04-17 22:38:54 -07:00
> Reed-Solomon Erasure Coding in Go
2024-01-08 21:11:12 -05:00
<img src="https://images.weserv.nl?fit=cover&h=24&mask=circle&maxage=7d&url=https%3A%2F%2Fgithub.com%2Fbytedance.png&w=24" width="24" height="24" hspace="4" valign="middle" /> [bytedance / **gopkg** ](https://github.com/bytedance/gopkg )
2024-03-01 05:30:37 +00:00
:star: 1.5k
2024-01-08 21:11:12 -05:00
> Universal Utilities for Go
2022-04-24 22:18:26 -07:00
<img src="https://images.weserv.nl?fit=cover&h=24&mask=circle&maxage=7d&url=https%3A%2F%2Fgithub.com%2Fcloudflare.png&w=24" width="24" height="24" hspace="4" valign="middle" /> [cloudflare / **circl** ](https://github.com/cloudflare/circl )
2023-11-01 05:33:21 +00:00
:star: 1.1k
2022-04-24 22:18:26 -07:00
> CIRCL: Cloudflare Interoperable Reusable Cryptographic Library
2023-01-07 18:54:05 +00:00
<img src="https://images.weserv.nl?fit=cover&h=24&mask=circle&maxage=7d&url=https%3A%2F%2Fgithub.com%2Fsegmentio.png&w=24" width="24" height="24" hspace="4" valign="middle" /> [segmentio / **asm** ](https://github.com/segmentio/asm )
2024-03-01 05:30:37 +00:00
:star: 837
2023-01-07 18:54:05 +00:00
> Go library providing algorithms optimized to leverage the characteristics of modern CPUs
2022-04-18 00:49:52 -07:00
<img src="https://images.weserv.nl?fit=cover&h=24&mask=circle&maxage=7d&url=https%3A%2F%2Fgithub.com%2Fzeebo.png&w=24" width="24" height="24" hspace="4" valign="middle" /> [zeebo / **xxh3** ](https://github.com/zeebo/xxh3 )
2024-03-01 05:30:37 +00:00
:star: 368
2022-04-17 22:38:54 -07:00
> XXH3 algorithm in Go
2024-01-01 05:31:31 +00:00
<img src="https://images.weserv.nl?fit=cover&h=24&mask=circle&maxage=7d&url=https%3A%2F%2Fgithub.com%2Fzeebo.png&w=24" width="24" height="24" hspace="4" valign="middle" /> [zeebo / **blake3** ](https://github.com/zeebo/blake3 )
2024-03-01 05:30:37 +00:00
:star: 359
2024-01-01 05:31:31 +00:00
> Pure Go implementation of BLAKE3 with AVX2 and SSE4.1 acceleration
2022-11-26 22:18:44 +00:00
<img src="https://images.weserv.nl?fit=cover&h=24&mask=circle&maxage=7d&url=https%3A%2F%2Fgithub.com%2Flukechampine.png&w=24" width="24" height="24" hspace="4" valign="middle" /> [lukechampine / **blake3** ](https://github.com/lukechampine/blake3 )
2024-03-01 05:30:37 +00:00
:star: 324
2022-11-26 22:18:44 +00:00
> A pure-Go implementation of the BLAKE3 cryptographic hash function
2022-04-17 23:57:05 -07:00
See the [full list of projects using `avo` ](doc/adopters.md ).
2019-01-03 20:49:03 -08:00
## Contributing
Contributions to `avo` are welcome:
2019-01-08 00:19:46 -08:00
* Feedback from using `avo` in a real project is incredibly valuable. Consider [porting an existing project to `avo` ](https://github.com/mmcloughlin/avo/issues/40 ).
2019-01-03 20:49:03 -08:00
* [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).
2020-12-16 15:13:06 -08:00
* Join us in the [#assembly ](https://gophers.slack.com/archives/C6WDZJ70S ) channel of [Gophers Slack ](https://invite.slack.golangbridge.org/ ).
2019-01-03 20:49:03 -08:00
2019-01-06 22:37:27 -08:00
## Credits
2019-01-08 08:54:56 -08:00
Inspired by the [PeachPy ](https://github.com/Maratyszcza/PeachPy ) and [asmjit ](https://github.com/asmjit/asmjit ) projects. Thanks to [Damian Gryski ](https://github.com/dgryski ) for advice, and his [extensive library of PeachPy Go projects ](https://github.com/mmcloughlin/avo/issues/40 ).
2019-01-06 22:37:27 -08:00
2019-01-03 20:02:32 -08:00
## License
`avo` is available under the [BSD 3-Clause License ](LICENSE ).