| 1 | # args |
| 2 | |
| 3 | Demonstrates how to reference function parameters in `avo`. |
| 4 | |
| 5 | ## Basics |
| 6 | |
| 7 | Use `Param()` to reference arguments by name. The `Load()` function can be used to load the argument into a register (this will select the correct `MOV` instruction for you). Likewise `Store` and `ReturnIndex` can be used to write the return value. The following function will return its second argument. |
| 8 | |
| 9 | [embedmd]:# (asm.go go /.*TEXT.*Second/ /RET.*/) |
| 10 | ```go |
| 11 | TEXT("Second", NOSPLIT, "func(x, y int32) int32") |
| 12 | y := Load(Param("y"), GP32()) |
| 13 | Store(y, ReturnIndex(0)) |
| 14 | RET() |
| 15 | ``` |
| 16 | |
| 17 | This `avo` code will generate the following assembly. Note that parameter references are named to conform to [`asmdecl`](https://godoc.org/golang.org/x/tools/go/analysis/passes/asmdecl) rules enforced by `go vet`. |
| 18 | |
| 19 | [embedmd]:# (args.s s /.*func Second/ /RET/) |
| 20 | ```s |
| 21 | // func Second(x int32, y int32) int32 |
| 22 | TEXT ·Second(SB), NOSPLIT, $0-12 |
| 23 | MOVL y+4(FP), AX |
| 24 | MOVL AX, ret+8(FP) |
| 25 | RET |
| 26 | ``` |
| 27 | |
| 28 | Primitive types can be loaded as above. Other types consist of sub-components which must be loaded into registers independently; for example strings, slices, arrays, structs and complex values. |
| 29 | |
| 30 | ## Strings and Slices |
| 31 | |
| 32 | Strings and slices actually consist of multiple components under the hood: see [`reflect.StringHeader`](https://golang.org/pkg/reflect/#StringHeader) and [`reflect.SliceHeader`](https://golang.org/pkg/reflect/#SliceHeader). The following `avo` code allows you to load the string length. |
| 33 | |
| 34 | [embedmd]:# (asm.go go /.*TEXT.*StringLen/ /RET.*/) |
| 35 | ```go |
| 36 | TEXT("StringLen", NOSPLIT, "func(s string) int") |
| 37 | strlen := Load(Param("s").Len(), GP64()) |
| 38 | Store(strlen, ReturnIndex(0)) |
| 39 | RET() |
| 40 | ``` |
| 41 | |
| 42 | The same code would work for a slice argument. Likewise `Param(...).Base()` and `Param(...).Cap()` will load the base pointer and capacity (slice only). |
| 43 | |
| 44 | ## Array Indexing |
| 45 | |
| 46 | Arrays can be indexed with the `Index()` method. For example, the following returns the third element of the passed array. |
| 47 | |
| 48 | [embedmd]:# (asm.go go /.*TEXT.*ArrayThree/ /RET.*/) |
| 49 | ```go |
| 50 | TEXT("ArrayThree", NOSPLIT, "func(a [7]uint64) uint64") |
| 51 | a3 := Load(Param("a").Index(3), GP64()) |
| 52 | Store(a3, ReturnIndex(0)) |
| 53 | RET() |
| 54 | ``` |
| 55 | |
| 56 | ## Struct Fields |
| 57 | |
| 58 | Struct fields can be accessed with the `Field()` method. Note that this _requires_ the package to be specified, so that `avo` can parse the type definition. In this example we specify the package with the line: |
| 59 | |
| 60 | [embedmd]:# (asm.go go /.*Package\(.*/) |
| 61 | ```go |
| 62 | Package("github.com/mmcloughlin/avo/examples/args") |
| 63 | ``` |
| 64 | |
| 65 | This package contains the struct definition: |
| 66 | |
| 67 | [embedmd]:# (args.go go /type Struct/ /^}/) |
| 68 | ```go |
| 69 | type Struct struct { |
| 70 | Byte byte |
| 71 | Int8 int8 |
| 72 | Uint16 uint16 |
| 73 | Int32 int32 |
| 74 | Uint64 uint64 |
| 75 | Float32 float32 |
| 76 | Float64 float64 |
| 77 | String string |
| 78 | Slice []Sub |
| 79 | Array [5]Sub |
| 80 | Complex64 complex64 |
| 81 | Complex128 complex128 |
| 82 | } |
| 83 | ``` |
| 84 | |
| 85 | The following function will return the `Float64` field from this struct. |
| 86 | |
| 87 | [embedmd]:# (asm.go go /.*TEXT.*FieldFloat64/ /RET.*/) |
| 88 | ```go |
| 89 | TEXT("FieldFloat64", NOSPLIT, "func(s Struct) float64") |
| 90 | f64 := Load(Param("s").Field("Float64"), XMM()) |
| 91 | Store(f64, ReturnIndex(0)) |
| 92 | RET() |
| 93 | ``` |
| 94 | |
| 95 | ## Complex Values |
| 96 | |
| 97 | Complex types `complex{64,128}` are actually just pairs of `float{32,64}` values. These can be accessed with the `Real()` and `Imag()` methods. For example the following function returns the imaginary part of the `Complex64` struct field. |
| 98 | |
| 99 | [embedmd]:# (asm.go go /.*TEXT.*FieldComplex64Imag/ /RET.*/) |
| 100 | ```go |
| 101 | TEXT("FieldComplex64Imag", NOSPLIT, "func(s Struct) float32") |
| 102 | c64i := Load(Param("s").Field("Complex64").Imag(), XMM()) |
| 103 | Store(c64i, ReturnIndex(0)) |
| 104 | RET() |
| 105 | ``` |
| 106 | |
| 107 | ## Nested Data Structures |
| 108 | |
| 109 | The above methods may be composed to reference arbitrarily nested data structures. For example, the following returns `s.Array[2].B[2]`. |
| 110 | |
| 111 | [embedmd]:# (asm.go go /.*TEXT.*FieldArrayTwoBTwo/ /RET.*/) |
| 112 | ```go |
| 113 | TEXT("FieldArrayTwoBTwo", NOSPLIT, "func(s Struct) byte") |
| 114 | b2 := Load(Param("s").Field("Array").Index(2).Field("B").Index(2), GP8()) |
| 115 | Store(b2, ReturnIndex(0)) |
| 116 | RET() |
| 117 | ``` |