args

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 7, 2024 License: BSD-3-Clause Imports: 0 Imported by: 0

README

args

Demonstrates how to reference function parameters in avo.

Basics

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.

	TEXT("Second", NOSPLIT, "func(x, y int32) int32")
	y := Load(Param("y"), GP32())
	Store(y, ReturnIndex(0))
	RET()

This avo code will generate the following assembly. Note that parameter references are named to conform to asmdecl rules enforced by go vet.

// func Second(x int32, y int32) int32
TEXT ·Second(SB), NOSPLIT, $0-12
	MOVL y+4(FP), AX
	MOVL AX, ret+8(FP)
	RET

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.

Strings and Slices

Strings and slices actually consist of multiple components under the hood: see reflect.StringHeader and reflect.SliceHeader. The following avo code allows you to load the string length.

	TEXT("StringLen", NOSPLIT, "func(s string) int")
	strlen := Load(Param("s").Len(), GP64())
	Store(strlen, ReturnIndex(0))
	RET()

The same code would work for a slice argument. Likewise Param(...).Base() and Param(...).Cap() will load the base pointer and capacity (slice only).

Array Indexing

Arrays can be indexed with the Index() method. For example, the following returns the third element of the passed array.

	TEXT("ArrayThree", NOSPLIT, "func(a [7]uint64) uint64")
	a3 := Load(Param("a").Index(3), GP64())
	Store(a3, ReturnIndex(0))
	RET()

Struct Fields

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:

	Package("github.com/mmcloughlin/avo/examples/args")

This package contains the struct definition:

type Struct struct {
	Byte       byte
	Int8       int8
	Uint16     uint16
	Int32      int32
	Uint64     uint64
	Float32    float32
	Float64    float64
	String     string
	Slice      []Sub
	Array      [5]Sub
	Complex64  complex64
	Complex128 complex128
}

The following function will return the Float64 field from this struct.

	TEXT("FieldFloat64", NOSPLIT, "func(s Struct) float64")
	f64 := Load(Param("s").Field("Float64"), XMM())
	Store(f64, ReturnIndex(0))
	RET()

Complex Values

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.

	TEXT("FieldComplex64Imag", NOSPLIT, "func(s Struct) float32")
	c64i := Load(Param("s").Field("Complex64").Imag(), XMM())
	Store(c64i, ReturnIndex(0))
	RET()

Nested Data Structures

The above methods may be composed to reference arbitrarily nested data structures. For example, the following returns s.Array[2].B[2].

	TEXT("FieldArrayTwoBTwo", NOSPLIT, "func(s Struct) byte")
	b2 := Load(Param("s").Field("Array").Index(2).Field("B").Index(2), GP8())
	Store(b2, ReturnIndex(0))
	RET()

Documentation

Overview

Package args demonstrates how to load function arguments in avo.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ArrayThree

func ArrayThree(a [7]uint64) uint64

func DereferenceFloat32

func DereferenceFloat32(s *Struct) float32

func FieldArrayOneC

func FieldArrayOneC(s Struct) uint16

func FieldArrayTwoBTwo

func FieldArrayTwoBTwo(s Struct) byte

func FieldByte

func FieldByte(s Struct) byte

func FieldComplex128Real

func FieldComplex128Real(s Struct) float64

func FieldComplex64Imag

func FieldComplex64Imag(s Struct) float32

func FieldFloat32

func FieldFloat32(s Struct) float32

func FieldFloat64

func FieldFloat64(s Struct) float64

func FieldInt32

func FieldInt32(s Struct) int32

func FieldInt8

func FieldInt8(s Struct) int8

func FieldSliceCap

func FieldSliceCap(s Struct) int

func FieldStringLen

func FieldStringLen(s Struct) int

func FieldUint16

func FieldUint16(s Struct) uint16

func FieldUint64

func FieldUint64(s Struct) uint64

func Second

func Second(x int32, y int32) int32

func SliceCap

func SliceCap(s []int) int

func SliceLen

func SliceLen(s []int) int

func StringLen

func StringLen(s string) int

Types

type Struct

type Struct struct {
	Byte       byte
	Int8       int8
	Uint16     uint16
	Int32      int32
	Uint64     uint64
	Float32    float32
	Float64    float64
	String     string
	Slice      []Sub
	Array      [5]Sub
	Complex64  complex64
	Complex128 complex128
}

Struct is a struct containing various datatypes, to help demonstrate struct field access.

type Sub

type Sub struct {
	A uint64
	B [3]byte
	C uint16
}

Sub is a sub-struct of Struct, to demonstrate nested datastructure accesses.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL