| 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 |
{{ include "examples/add/asm.go" -}} |
| 40 |
``` |
| 41 |
|
| 42 |
`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. |
| 43 |
|
| 44 |
```go |
| 45 |
{{ snippet "examples/add/add_test.go" "go:generate" "" -}} |
| 46 |
``` |
| 47 |
|
| 48 |
After running `go generate` the [`add.s`](examples/add/add.s) file will contain the Go assembly. |
| 49 |
|
| 50 |
```s |
| 51 |
{{ include "examples/add/add.s" -}} |
| 52 |
``` |
| 53 |
|
| 54 |
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. |
| 55 |
|
| 56 |
```go |
| 57 |
{{ include "examples/add/stub.go" -}} |
| 58 |
``` |
| 59 |
|
| 60 |
See the [`examples/add`](examples/add) directory for the complete working example. |
| 61 |
|
| 62 |
## Examples |
| 63 |
|
| 64 |
See [`examples`](examples) for the full suite of examples. |
| 65 |
|
| 66 |
### Slice Sum |
| 67 |
|
| 68 |
Sum a slice of `uint64`s: |
| 69 |
|
| 70 |
```go |
| 71 |
{{ snippet "examples/sum/asm.go" "func main" "^}" -}} |
| 72 |
``` |
| 73 |
|
| 74 |
The result from this code generator is: |
| 75 |
|
| 76 |
```s |
| 77 |
{{ include "examples/sum/sum.s" -}} |
| 78 |
``` |
| 79 |
|
| 80 |
Full example at [`examples/sum`](examples/sum). |
| 81 |
|
| 82 |
### Features |
| 83 |
|
| 84 |
For demonstrations of `avo` features: |
| 85 |
|
| 86 |
* **[args](examples/args):** Loading function arguments. |
| 87 |
* **[returns](examples/returns):** Building return values. |
| 88 |
* **[complex](examples/complex):** Working with `complex{64,128}` types. |
| 89 |
* **[data](examples/data):** Defining `DATA` sections. |
| 90 |
* **[ext](examples/ext):** Interacting with types from external packages. |
| 91 |
* **[pragma](examples/pragma):** Apply compiler directives to generated functions. |
| 92 |
|
| 93 |
### Real Examples |
| 94 |
|
| 95 |
Implementations of full algorithms: |
| 96 |
|
| 97 |
* **[sha1](examples/sha1):** [SHA-1](https://en.wikipedia.org/wiki/SHA-1) cryptographic hash. |
| 98 |
* **[fnv1a](examples/fnv1a):** [FNV-1a](https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1a_hash) hash function. |
| 99 |
* **[dot](examples/dot):** Vector dot product. |
| 100 |
* **[md5x16](examples/md5x16):** AVX-512 accelerated [MD5](https://en.wikipedia.org/wiki/MD5). |
| 101 |
* **[geohash](examples/geohash):** Integer [geohash](https://en.wikipedia.org/wiki/Geohash) encoding. |
| 102 |
* **[stadtx](examples/stadtx):** [`StadtX` hash](https://github.com/demerphq/BeagleHash) port from [dgryski/go-stadtx](https://github.com/dgryski/go-stadtx). |
| 103 |
|
| 104 |
## Adopters |
| 105 |
|
| 106 |
Popular projects[^projects] using `avo`: |
| 107 |
|
| 108 |
[^projects]: Projects drawn from the `avo` third-party test suite. Popularity |
| 109 |
estimated from Github star count collected on {{ .Suite.MetadataLastUpdate.Format "Jan 2, 2006" }}. |
| 110 |
|
| 111 |
{{ range .Suite.Projects.Top 10 -}} |
| 112 |
{{ if lt .Metadata.Stars 100 }}{{break}}{{ end -}} |
| 113 |
{{ avatar .Repository.Owner 24 }} [{{ .Repository.Owner }} / **{{ .Repository.Name }}**]({{ .Repository.URL }}) |
| 114 |
:star: {{ stars .Metadata.Stars }} |
| 115 |
> {{ .Metadata.Description }} |
| 116 |
|
| 117 |
{{ end -}} |
| 118 |
|
| 119 |
See the [full list of projects using `avo`](doc/adopters.md). |
| 120 |
|
| 121 |
## Contributing |
| 122 |
|
| 123 |
Contributions to `avo` are welcome: |
| 124 |
|
| 125 |
* 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). |
| 126 |
* [Submit bug reports](https://github.com/mmcloughlin/avo/issues/new) to the issues page. |
| 127 |
* 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). |
| 128 |
* Join us in the [#assembly](https://gophers.slack.com/archives/C6WDZJ70S) channel of [Gophers Slack](https://invite.slack.golangbridge.org/). |
| 129 |
|
| 130 |
## Credits |
| 131 |
|
| 132 |
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). |
| 133 |
|
| 134 |
## License |
| 135 |
|
| 136 |
`avo` is available under the [BSD 3-Clause License](LICENSE). |