| 1 | # add |
| 2 | |
| 3 | Add two numbers with `avo`. |
| 4 | |
| 5 | The [code generator](asm.go) is as follows: |
| 6 | |
| 7 | [embedmd]:# (asm.go) |
| 8 | ```go |
| 9 | //go:build ignore |
| 10 | |
| 11 | package main |
| 12 | |
| 13 | import . "github.com/mmcloughlin/avo/build" |
| 14 | |
| 15 | func main() { |
| 16 | TEXT("Add", NOSPLIT, "func(x, y uint64) uint64") |
| 17 | Doc("Add adds x and y.") |
| 18 | x := Load(Param("x"), GP64()) |
| 19 | y := Load(Param("y"), GP64()) |
| 20 | ADDQ(x, y) |
| 21 | Store(y, ReturnIndex(0)) |
| 22 | RET() |
| 23 | Generate() |
| 24 | } |
| 25 | ``` |
| 26 | |
| 27 | We use a [`go:generate`](https://blog.golang.org/generate) line to produce the assembly and Go stub files together. |
| 28 | |
| 29 | [embedmd]:# (add_test.go go /.*go:generate.*/) |
| 30 | ```go |
| 31 | //go:generate go run asm.go -out add.s -stubs stub.go |
| 32 | ``` |
| 33 | |
| 34 | This produces [`add.s`](add.s) as follows: |
| 35 | |
| 36 | [embedmd]:# (add.s) |
| 37 | ```s |
| 38 | // Code generated by command: go run asm.go -out add.s -stubs stub.go. DO NOT EDIT. |
| 39 | |
| 40 | #include "textflag.h" |
| 41 | |
| 42 | // func Add(x uint64, y uint64) uint64 |
| 43 | TEXT ·Add(SB), NOSPLIT, $0-24 |
| 44 | MOVQ x+0(FP), AX |
| 45 | MOVQ y+8(FP), CX |
| 46 | ADDQ AX, CX |
| 47 | MOVQ CX, ret+16(FP) |
| 48 | RET |
| 49 | ``` |