| 1 | <p align="center"> |
| 2 | <img src="logo.svg" width="40%" border="0" alt="avo" /> |
| 3 | <br /> |
| 4 | <img src="https://img.shields.io/github/actions/workflow/status/mmcloughlin/avo/ci.yml?style=flat-square" alt="Build Status" /> |
| 5 | <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> |
| 6 | <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> |
| 7 | </p> |
| 8 | |
| 9 | <p align="center">Generate x86 Assembly with Go</p> |
| 10 | |
| 11 | `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: |
| 12 | |
| 13 | * **Use Go control structures** for assembly generation; `avo` programs _are_ Go programs |
| 14 | * **Register allocation**: write functions with virtual registers and `avo` assigns physical registers for you |
| 15 | * **Automatically load arguments and store return values**: ensure memory offsets are correct for complex structures |
| 16 | * **Generation of stub files** to interface with your Go package |
| 17 | |
| 18 | For more about `avo`: |
| 19 | |
| 20 | * 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)) |
| 21 | * [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)) |
| 22 | * Watch [Filippo Valsorda](https://filippo.io/) live code the [rewrite of `filippo.io/edwards25519` assembly with `avo`](https://vimeo.com/679848853) |
| 23 | * Explore [projects using `avo`](doc/adopters.md) |
| 24 | * 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/) |
| 25 | |
| 26 | _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._ |
| 27 | |
| 28 | ## Quick Start |
| 29 | |
| 30 | Install `avo` with `go get`: |
| 31 | |
| 32 | ``` |
| 33 | $ go get -u github.com/mmcloughlin/avo |
| 34 | ``` |
| 35 | |
| 36 | `avo` assembly generators are pure Go programs. Here's a function that adds two `uint64` values: |
| 37 | |
| 38 | ```go |
| 39 | //go:build ignore |
| 40 | |
| 41 | package main |
| 42 | |
| 43 | import . "github.com/mmcloughlin/avo/build" |
| 44 | |
| 45 | func main() { |
| 46 | TEXT("Add", NOSPLIT, "func(x, y uint64) uint64") |
| 47 | Doc("Add adds x and y.") |
| 48 | x := Load(Param("x"), GP64()) |
| 49 | y := Load(Param("y"), GP64()) |
| 50 | ADDQ(x, y) |
| 51 | Store(y, ReturnIndex(0)) |
| 52 | RET() |
| 53 | Generate() |
| 54 | } |
| 55 | ``` |
| 56 | |
| 57 | `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. |
| 58 | |
| 59 | ```go |
| 60 | //go:generate go run asm.go -out add.s -stubs stub.go |
| 61 | ``` |
| 62 | |
| 63 | After running `go generate` the [`add.s`](examples/add/add.s) file will contain the Go assembly. |
| 64 | |
| 65 | ```s |
| 66 | // Code generated by command: go run asm.go -out add.s -stubs stub.go. DO NOT EDIT. |
| 67 | |
| 68 | #include "textflag.h" |
| 69 | |
| 70 | // func Add(x uint64, y uint64) uint64 |
| 71 | TEXT ·Add(SB), NOSPLIT, $0-24 |
| 72 | MOVQ x+0(FP), AX |
| 73 | MOVQ y+8(FP), CX |
| 74 | ADDQ AX, CX |
| 75 | MOVQ CX, ret+16(FP) |
| 76 | RET |
| 77 | ``` |
| 78 | |
| 79 | 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. |
| 80 | |
| 81 | ```go |
| 82 | // Code generated by command: go run asm.go -out add.s -stubs stub.go. DO NOT EDIT. |
| 83 | |
| 84 | package add |
| 85 | |
| 86 | // Add adds x and y. |
| 87 | func Add(x uint64, y uint64) uint64 |
| 88 | ``` |
| 89 | |
| 90 | See the [`examples/add`](examples/add) directory for the complete working example. |
| 91 | |
| 92 | ## Examples |
| 93 | |
| 94 | See [`examples`](examples) for the full suite of examples. |
| 95 | |
| 96 | ### Slice Sum |
| 97 | |
| 98 | Sum a slice of `uint64`s: |
| 99 | |
| 100 | ```go |
| 101 | func main() { |
| 102 | TEXT("Sum", NOSPLIT, "func(xs []uint64) uint64") |
| 103 | Doc("Sum returns the sum of the elements in xs.") |
| 104 | ptr := Load(Param("xs").Base(), GP64()) |
| 105 | n := Load(Param("xs").Len(), GP64()) |
| 106 | |
| 107 | Comment("Initialize sum register to zero.") |
| 108 | s := GP64() |
| 109 | XORQ(s, s) |
| 110 | |
| 111 | Label("loop") |
| 112 | Comment("Loop until zero bytes remain.") |
| 113 | CMPQ(n, Imm(0)) |
| 114 | JE(LabelRef("done")) |
| 115 | |
| 116 | Comment("Load from pointer and add to running sum.") |
| 117 | ADDQ(Mem{Base: ptr}, s) |
| 118 | |
| 119 | Comment("Advance pointer, decrement byte count.") |
| 120 | ADDQ(Imm(8), ptr) |
| 121 | DECQ(n) |
| 122 | JMP(LabelRef("loop")) |
| 123 | |
| 124 | Label("done") |
| 125 | Comment("Store sum to return value.") |
| 126 | Store(s, ReturnIndex(0)) |
| 127 | RET() |
| 128 | Generate() |
| 129 | } |
| 130 | ``` |
| 131 | |
| 132 | The result from this code generator is: |
| 133 | |
| 134 | ```s |
| 135 | // Code generated by command: go run asm.go -out sum.s -stubs stub.go. DO NOT EDIT. |
| 136 | |
| 137 | #include "textflag.h" |
| 138 | |
| 139 | // func Sum(xs []uint64) uint64 |
| 140 | TEXT ·Sum(SB), NOSPLIT, $0-32 |
| 141 | MOVQ xs_base+0(FP), AX |
| 142 | MOVQ xs_len+8(FP), CX |
| 143 | |
| 144 | // Initialize sum register to zero. |
| 145 | XORQ DX, DX |
| 146 | |
| 147 | loop: |
| 148 | // Loop until zero bytes remain. |
| 149 | CMPQ CX, $0x00 |
| 150 | JE done |
| 151 | |
| 152 | // Load from pointer and add to running sum. |
| 153 | ADDQ (AX), DX |
| 154 | |
| 155 | // Advance pointer, decrement byte count. |
| 156 | ADDQ $0x08, AX |
| 157 | DECQ CX |
| 158 | JMP loop |
| 159 | |
| 160 | done: |
| 161 | // Store sum to return value. |
| 162 | MOVQ DX, ret+24(FP) |
| 163 | RET |
| 164 | ``` |
| 165 | |
| 166 | Full example at [`examples/sum`](examples/sum). |
| 167 | |
| 168 | ### Features |
| 169 | |
| 170 | For demonstrations of `avo` features: |
| 171 | |
| 172 | * **[args](examples/args):** Loading function arguments. |
| 173 | * **[returns](examples/returns):** Building return values. |
| 174 | * **[complex](examples/complex):** Working with `complex{64,128}` types. |
| 175 | * **[data](examples/data):** Defining `DATA` sections. |
| 176 | * **[ext](examples/ext):** Interacting with types from external packages. |
| 177 | * **[pragma](examples/pragma):** Apply compiler directives to generated functions. |
| 178 | |
| 179 | ### Real Examples |
| 180 | |
| 181 | Implementations of full algorithms: |
| 182 | |
| 183 | * **[sha1](examples/sha1):** [SHA-1](https://en.wikipedia.org/wiki/SHA-1) cryptographic hash. |
| 184 | * **[fnv1a](examples/fnv1a):** [FNV-1a](https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1a_hash) hash function. |
| 185 | * **[dot](examples/dot):** Vector dot product. |
| 186 | * **[md5x16](examples/md5x16):** AVX-512 accelerated [MD5](https://en.wikipedia.org/wiki/MD5). |
| 187 | * **[geohash](examples/geohash):** Integer [geohash](https://en.wikipedia.org/wiki/Geohash) encoding. |
| 188 | * **[stadtx](examples/stadtx):** [`StadtX` hash](https://github.com/demerphq/BeagleHash) port from [dgryski/go-stadtx](https://github.com/dgryski/go-stadtx). |
| 189 | |
| 190 | ## Adopters |
| 191 | |
| 192 | Popular projects[^projects] using `avo`: |
| 193 | |
| 194 | [^projects]: Projects drawn from the `avo` third-party test suite. Popularity |
| 195 | estimated from Github star count collected on Jan 1, 2024. |
| 196 | |
| 197 | <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) |
| 198 | :star: 116.7k |
| 199 | > The Go programming language |
| 200 | |
| 201 | <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) |
| 202 | :star: 4.3k |
| 203 | > Optimized Go Compression Packages |
| 204 | |
| 205 | <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) |
| 206 | :star: 2.9k |
| 207 | > [mirror] Go supplementary cryptography libraries |
| 208 | |
| 209 | <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) |
| 210 | :star: 1.8k |
| 211 | > Reed-Solomon Erasure Coding in Go |
| 212 | |
| 213 | <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) |
| 214 | :star: 1.1k |
| 215 | > CIRCL: Cloudflare Interoperable Reusable Cryptographic Library |
| 216 | |
| 217 | <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) |
| 218 | :star: 820 |
| 219 | > Go library providing algorithms optimized to leverage the characteristics of modern CPUs |
| 220 | |
| 221 | <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) |
| 222 | :star: 358 |
| 223 | > XXH3 algorithm in Go |
| 224 | |
| 225 | <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) |
| 226 | :star: 353 |
| 227 | > Pure Go implementation of BLAKE3 with AVX2 and SSE4.1 acceleration |
| 228 | |
| 229 | <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) |
| 230 | :star: 316 |
| 231 | > A pure-Go implementation of the BLAKE3 cryptographic hash function |
| 232 | |
| 233 | <img src="https://images.weserv.nl?fit=cover&h=24&mask=circle&maxage=7d&url=https%3A%2F%2Fgithub.com%2Fminio.png&w=24" width="24" height="24" hspace="4" valign="middle" /> [minio / **md5-simd**](https://github.com/minio/md5-simd) |
| 234 | :star: 159 |
| 235 | > Accelerate aggregated MD5 hashing performance up to 8x for AVX512 and 4x for AVX2. Useful for server applications that need to compute many MD5 sums in parallel. |
| 236 | |
| 237 | See the [full list of projects using `avo`](doc/adopters.md). |
| 238 | |
| 239 | ## Contributing |
| 240 | |
| 241 | Contributions to `avo` are welcome: |
| 242 | |
| 243 | * 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). |
| 244 | * [Submit bug reports](https://github.com/mmcloughlin/avo/issues/new) to the issues page. |
| 245 | * 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). |
| 246 | * Join us in the [#assembly](https://gophers.slack.com/archives/C6WDZJ70S) channel of [Gophers Slack](https://invite.slack.golangbridge.org/). |
| 247 | |
| 248 | ## Credits |
| 249 | |
| 250 | 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). |
| 251 | |
| 252 | ## License |
| 253 | |
| 254 | `avo` is available under the [BSD 3-Clause License](LICENSE). |