2019-01-03 00:46:06 -08:00
<p align="center">
2026-03-06 20:46:23 +00:00
<img src ="logo.svg" width="40%" border="0" alt="avo"/>
2026-03-06 20:14:02 +00:00
</p>
2019-01-03 22:02:02 -08:00
2026-03-06 20:14:02 +00:00
<p align="center"><b>Generate x86 Assembly with Go</b></p>
2021-04-21 22:03:43 -07:00
2026-03-06 20:14:02 +00:00
<code>Avo</code> makes high-performance Go assembly easier to write, review and maintain. The <code>avo</code> package presents a fimiliar assembly-like interface that simplifies development without sacrificing performance:
2019-05-14 21:00:33 -07:00
2026-03-06 20:14:02 +00:00
* Use Go control structures for assembly generation; <code>avo</code> programs are Go progams
* Register allocation: write functions with virtual registers and <code>avo</code> assigns physical registers for you
* Automatically load arguments and store return values: ensures memory offsets are correct for complex structures
* Generation of stub files to interface with your go packages
2019-01-03 20:02:32 -08:00
2026-03-06 20:14:02 +00:00
# Quick Start
2019-01-03 20:02:32 -08:00
2026-03-06 20:14:02 +00:00
install `avo` with `go get` :
2019-01-03 20:02:32 -08:00
```
2026-03-06 20:14:02 +00:00
$ go get -u sources.truenas.cloud/code/avo
2019-01-03 20:02:32 -08:00
```
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
2026-03-06 20:14:02 +00:00
import . "sources.truenas.cloud/code/avo/build"
2019-01-03 20:02:32 -08:00
func main() {
2026-03-06 20:14:02 +00:00
TEXT("Add", NOSPLIT, "func(x, y uint64) uint64")
Doc("Add adds x and y.")
x := Load(Param("x"), GP64())
y := Load(Param("y"), GP64())
ADDQ(x, y)
Store(y, ReturnIndex(0))
RET()
Generate()
2019-01-03 20:02:32 -08:00
}
2026-03-06 20:14:02 +00:00
2019-01-03 20:02:32 -08:00
```
2026-03-06 20:14:02 +00: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
2026-03-06 20:14:02 +00:00
// code generated by command: go run asm.go -out add.s -stubs stubs.go. DO NOT EDIT
2019-01-03 20:02:32 -08:00
2026-03-06 20:14:02 +00:00
#include "text.flag.h"
2019-01-06 20:16:26 -08:00
2019-01-03 20:02:32 -08:00
// func Add(x uint64, y uint64) uint64
2026-03-06 20:14:02 +00:00
TEXT .Add(SB), NOSPLIT, $0-24
MOVQ x+0(FP), AX
MOVQ y+8(FP), CX
ADDQ AX, CX
MOVQ CX, ret+16(FP)
RET
2019-01-03 20:02:32 -08:00
```
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
2026-03-06 20:14:02 +00:00
// Code generated by command: go run asm.go -out add.s -stubs stub.go. DO NOT EDIT
2019-01-03 20:02:32 -08:00
package add
// Add adds x and y.
2026-03-06 20:14:02 +00:00
2019-01-03 20:02:32 -08:00
func Add(x uint64, y uint64) uint64
2026-03-06 20:14:02 +00:00
2019-01-03 20:02:32 -08:00
```
2026-03-06 20:14:02 +00:00
See the [`examples` ](examples/add ) directory for the complete working example.
2019-01-03 20:02:32 -08:00
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() {
2026-03-06 20:14:02 +00:00
TEXT("Sum", NOSPLIT, "func(xs []uint64) uint64")
Doc("Sum returns the sum of the elements in xs.")
ptr := Load(Param("xs").Base(), GP64())
n := Load(Param("xs").Len(), GP64())
Comment("Initialize sum register to zero.")
s := GP64()
XORQ(s, s)
Label("loop")
Comment("Loop until zero bytes remain.")
CMPQ(n, Imm(0))
JE(LabelRef("done"))
Comment("Load from pointer and add to running sum.")
ADDQ(Mem{Base: ptr}, s)
Comment("Advance pointer, decrement byte count.")
ADDQ(Imm(8), ptr)
DECQ(n)
JMP(LabelRef("loop"))
Label("done")
Comment("Store sum to return value.")
Store(s, ReturnIndex(0))
RET()
Generate()
2019-01-03 22:20:25 -08:00
```
2026-03-06 20:14:02 +00:00
The result for this code generator is:
2019-01-06 18:06:29 -08:00
```s
// Code generated by command: go run asm.go -out sum.s -stubs stub.go. DO NOT EDIT.
2026-03-06 20:14:02 +00:00
#include "text.flag.h"
2019-01-06 20:16:26 -08:00
2019-01-06 18:06:29 -08:00
// func Sum(xs []uint64) uint64
2026-03-06 20:14:02 +00:00
2019-01-06 20:16:26 -08:00
TEXT ·Sum(SB), NOSPLIT, $0-32
2026-03-06 20:14:02 +00:00
MOVQ xs_base+0(FP), AX
MOVQ xs_len+8(FP), CX
2019-01-11 10:57:38 -08:00
2026-03-06 20:14:02 +00:00
// Intitialize sum register to zero
2019-01-10 21:21:41 -08:00
2026-03-06 20:14:02 +00:00
XORQ DX, DX
2019-01-11 10:57:38 -08:00
2026-03-06 20:14:02 +00:00
loop:
// Loop until zero bytes remain.
CMPQ CX, $0x00
JE done
2019-01-11 10:57:38 -08:00
2026-03-06 20:14:02 +00:00
// Load from pointer and add to running sum.
ADDQ (AX), DX
2019-01-10 21:21:41 -08:00
2026-03-06 20:14:02 +00:00
// Advance pointer, decrement byte count.
ADDQ $0x08, AX
DECQ CX
JMP loop
2019-01-06 18:06:29 -08:00
done:
2026-03-06 20:14:02 +00:00
// Store sum to return value.
MOVQ DX, ret+24(FP)
RET
2019-01-06 18:06:29 -08:00
```
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
2019-01-03 20:49:03 -08:00
## Contributing
Contributions to `avo` are welcome:
2026-03-06 20:14:02 +00:00
* Feedback from using `avo` in a real project is incredibly valuable. Consider [porting an existing project to `avo` ](https://sources.truenas.cloud/code/avo/issues/40 ).
* [Submit bug reports ](https://sources.truenas.cloud/code/avo/issues/new ) to the issues page.
* Pull requests accepted. Take a look at outstanding [issues ](https://sources.truenas.cloud/code/avo/issues ) for ideas (especially the ["good first issue" ](https://sources.truenas.cloud/code/avo/labels/good%20first%20issue ) label).
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 ).
2026-03-06 20:14:02 +00:00