Debian Codegraph @

examples/returns/README.md

raw symbols SCIP
1 # returns
2
3 Demonstrates working with function return values.
4
5 ## Multiple Unnamed Return Values
6
7 Use `ReturnIndex` to reference unnamed return values. For example, the following function returns `start, start + size`.
8
9 [embedmd]:# (asm.go go /.*TEXT.*Interval/ /RET.*/)
10 ```go
11 TEXT("Interval", NOSPLIT, "func(start, size uint64) (uint64, uint64)")
12 Doc(
13 "Interval returns the (start, end) of an interval with the given start and size.",
14 "Demonstrates multiple unnamed return values.",
15 )
16 start := Load(Param("start"), GP64())
17 size := Load(Param("size"), GP64())
18 end := size
19 ADDQ(start, end)
20 Store(start, ReturnIndex(0))
21 Store(end, ReturnIndex(1))
22 RET()
23 ```
24
25 ## Multiple Named Return Values
26
27 Named return values are referenced much the same as arguments. For example, the following computes `(x0+x1, x0-x1)`.
28
29 [embedmd]:# (asm.go go /.*TEXT.*Butterfly/ /RET.*/)
30 ```go
31 TEXT("Butterfly", NOSPLIT, "func(x0, x1 float64) (y0, y1 float64)")
32 Doc(
33 "Butterfly performs a 2-dimensional butterfly operation: computes (x0+x1, x0-x1).",
34 "Demonstrates multiple named return values.",
35 )
36 x0 := Load(Param("x0"), XMM())
37 x1 := Load(Param("x1"), XMM())
38 y0, y1 := XMM(), XMM()
39 MOVSD(x0, y0)
40 ADDSD(x1, y0)
41 MOVSD(x0, y1)
42 SUBSD(x1, y1)
43 Store(y0, Return("y0"))
44 Store(y1, Return("y1"))
45 RET()
46 ```
47
48 ## Returning Data Structures
49
50 Again just like function arguments, sub-components of return values can be referenced. This enables you to return data structures from your assembly functions.
51
52 The following code returns an array type.
53
54 [embedmd]:# (asm.go go /.*TEXT.*Septuple/ /RET.*/)
55 ```go
56 TEXT("Septuple", NOSPLIT, "func(byte) [7]byte")
57 Doc(
58 "Septuple returns an array of seven of the given byte.",
59 "Demonstrates returning array values.",
60 )
61 b := Load(ParamIndex(0), GP8())
62 for i := 0; i < 7; i++ {
63 Store(b, ReturnIndex(0).Index(i))
64 }
65 RET()
66 ```
67
68 Or a complex type:
69
70 [embedmd]:# (asm.go go /.*TEXT.*CriticalLine/ /RET.*/)
71 ```go
72 TEXT("CriticalLine", NOSPLIT, "func(t float64) complex128")
73 Doc(
74 "CriticalLine returns the complex value 0.5 + it on Riemann's critical line.",
75 "Demonstrates returning complex values.",
76 )
77 t := Load(Param("t"), XMM())
78 half := XMM()
79 MOVSD(ConstData("half", F64(0.5)), half)
80 Store(half, ReturnIndex(0).Real())
81 Store(t, ReturnIndex(0).Imag())
82 RET()
83 ```
84
85 You can even build a struct:
86
87 [embedmd]:# (asm.go go /.*TEXT.*NewStruct/ /RET.*/)
88 ```go
89 TEXT("NewStruct", NOSPLIT, "func(w uint16, p [2]float64, q uint64) Struct")
90 Doc(
91 "NewStruct initializes a Struct value.",
92 "Demonstrates returning struct values.",
93 )
94 w := Load(Param("w"), GP16())
95 x := Load(Param("p").Index(0), XMM())
96 y := Load(Param("p").Index(1), XMM())
97 q := Load(Param("q"), GP64())
98 Store(w, ReturnIndex(0).Field("Word"))
99 Store(x, ReturnIndex(0).Field("Point").Index(0))
100 Store(y, ReturnIndex(0).Field("Point").Index(1))
101 Store(q, ReturnIndex(0).Field("Quad"))
102 RET()
103 ```