Debian Codegraph @

src/runtime/mkduff.go

raw
1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build ignore
6
7 // runtime·duffzero is a Duff's device for zeroing memory.
8 // The compiler jumps to computed addresses within
9 // the routine to zero chunks of memory.
10 // Do not change duffzero without also
11 // changing the uses in cmd/compile/internal/*/*.go.
12
13 // runtime·duffcopy is a Duff's device for copying memory.
14 // The compiler jumps to computed addresses within
15 // the routine to copy chunks of memory.
16 // Source and destination must not overlap.
17 // Do not change duffcopy without also
18 // changing the uses in cmd/compile/internal/*/*.go.
19
20 // See the zero* and copy* generators below
21 // for architecture-specific comments.
22
23 // mkduff generates duff_*.s.
24 package main
25
26 import (
27 "bytes"
28 "fmt"
29 "io"
30 "log"
31 "os"
32 )
33
34 func main() {
35 gen("386", notags, zero386, copy386)
36 gen("arm", notags, zeroARM, copyARM)
37 gen("ppc64x", tagsPPC64x, zeroPPC64x, copyPPC64x)
38 gen("mips64x", tagsMIPS64x, zeroMIPS64x, copyMIPS64x)
39 }
40
41 func gen(arch string, tags, zero, copy func(io.Writer)) {
42 var buf bytes.Buffer
43
44 fmt.Fprintln(&buf, "// Code generated by mkduff.go; DO NOT EDIT.")
45 fmt.Fprintln(&buf, "// Run go generate from src/runtime to update.")
46 fmt.Fprintln(&buf, "// See mkduff.go for comments.")
47 tags(&buf)
48 fmt.Fprintln(&buf, "#include \"textflag.h\"")
49 fmt.Fprintln(&buf)
50 zero(&buf)
51 fmt.Fprintln(&buf)
52 copy(&buf)
53
54 if err := os.WriteFile("duff_"+arch+".s", buf.Bytes(), 0644); err != nil {
55 log.Fatalln(err)
56 }
57 }
58
59 func notags(w io.Writer) { fmt.Fprintln(w) }
60
61 func zeroAMD64(w io.Writer) {
62 // X15: zero
63 // DI: ptr to memory to be zeroed
64 // DI is updated as a side effect.
65 fmt.Fprintln(w, "TEXT runtime·duffzero<ABIInternal>(SB), NOSPLIT|NOFRAME, $0-0")
66 for i := 0; i < 16; i++ {
67 fmt.Fprintln(w, "\tMOVUPS\tX15,(DI)")
68 fmt.Fprintln(w, "\tMOVUPS\tX15,16(DI)")
69 fmt.Fprintln(w, "\tMOVUPS\tX15,32(DI)")
70 fmt.Fprintln(w, "\tMOVUPS\tX15,48(DI)")
71 fmt.Fprintln(w, "\tLEAQ\t64(DI),DI") // We use lea instead of add, to avoid clobbering flags
72 fmt.Fprintln(w)
73 }
74 fmt.Fprintln(w, "\tRET")
75 }
76
77 func copyAMD64(w io.Writer) {
78 // SI: ptr to source memory
79 // DI: ptr to destination memory
80 // SI and DI are updated as a side effect.
81 //
82 // This is equivalent to a sequence of MOVSQ but
83 // for some reason that is 3.5x slower than this code.
84 fmt.Fprintln(w, "TEXT runtime·duffcopy<ABIInternal>(SB), NOSPLIT|NOFRAME, $0-0")
85 for i := 0; i < 64; i++ {
86 fmt.Fprintln(w, "\tMOVUPS\t(SI), X0")
87 fmt.Fprintln(w, "\tADDQ\t$16, SI")
88 fmt.Fprintln(w, "\tMOVUPS\tX0, (DI)")
89 fmt.Fprintln(w, "\tADDQ\t$16, DI")
90 fmt.Fprintln(w)
91 }
92 fmt.Fprintln(w, "\tRET")
93 }
94
95 func zero386(w io.Writer) {
96 // AX: zero
97 // DI: ptr to memory to be zeroed
98 // DI is updated as a side effect.
99 fmt.Fprintln(w, "TEXT runtime·duffzero(SB), NOSPLIT, $0-0")
100 for i := 0; i < 128; i++ {
101 fmt.Fprintln(w, "\tSTOSL")
102 }
103 fmt.Fprintln(w, "\tRET")
104 }
105
106 func copy386(w io.Writer) {
107 // SI: ptr to source memory
108 // DI: ptr to destination memory
109 // SI and DI are updated as a side effect.
110 //
111 // This is equivalent to a sequence of MOVSL but
112 // for some reason MOVSL is really slow.
113 fmt.Fprintln(w, "TEXT runtime·duffcopy(SB), NOSPLIT, $0-0")
114 for i := 0; i < 128; i++ {
115 fmt.Fprintln(w, "\tMOVL\t(SI), CX")
116 fmt.Fprintln(w, "\tADDL\t$4, SI")
117 fmt.Fprintln(w, "\tMOVL\tCX, (DI)")
118 fmt.Fprintln(w, "\tADDL\t$4, DI")
119 fmt.Fprintln(w)
120 }
121 fmt.Fprintln(w, "\tRET")
122 }
123
124 func zeroARM(w io.Writer) {
125 // R0: zero
126 // R1: ptr to memory to be zeroed
127 // R1 is updated as a side effect.
128 fmt.Fprintln(w, "TEXT runtime·duffzero(SB), NOSPLIT, $0-0")
129 for i := 0; i < 128; i++ {
130 fmt.Fprintln(w, "\tMOVW.P\tR0, 4(R1)")
131 }
132 fmt.Fprintln(w, "\tRET")
133 }
134
135 func copyARM(w io.Writer) {
136 // R0: scratch space
137 // R1: ptr to source memory
138 // R2: ptr to destination memory
139 // R1 and R2 are updated as a side effect
140 fmt.Fprintln(w, "TEXT runtime·duffcopy(SB), NOSPLIT, $0-0")
141 for i := 0; i < 128; i++ {
142 fmt.Fprintln(w, "\tMOVW.P\t4(R1), R0")
143 fmt.Fprintln(w, "\tMOVW.P\tR0, 4(R2)")
144 fmt.Fprintln(w)
145 }
146 fmt.Fprintln(w, "\tRET")
147 }
148
149 func zeroARM64(w io.Writer) {
150 // ZR: always zero
151 // R20: ptr to memory to be zeroed
152 // On return, R20 points to the last zeroed dword.
153 fmt.Fprintln(w, "TEXT runtime·duffzero<ABIInternal>(SB), NOSPLIT|NOFRAME, $0-0")
154 for i := 0; i < 63; i++ {
155 fmt.Fprintln(w, "\tSTP.P\t(ZR, ZR), 16(R20)")
156 }
157 fmt.Fprintln(w, "\tSTP\t(ZR, ZR), (R20)")
158 fmt.Fprintln(w, "\tRET")
159 }
160
161 func copyARM64(w io.Writer) {
162 // R20: ptr to source memory
163 // R21: ptr to destination memory
164 // R26, R27 (aka REGTMP): scratch space
165 // R20 and R21 are updated as a side effect
166 fmt.Fprintln(w, "TEXT runtime·duffcopy<ABIInternal>(SB), NOSPLIT|NOFRAME, $0-0")
167
168 for i := 0; i < 64; i++ {
169 fmt.Fprintln(w, "\tLDP.P\t16(R20), (R26, R27)")
170 fmt.Fprintln(w, "\tSTP.P\t(R26, R27), 16(R21)")
171 fmt.Fprintln(w)
172 }
173 fmt.Fprintln(w, "\tRET")
174 }
175
176 func tagsPPC64x(w io.Writer) {
177 fmt.Fprintln(w)
178 fmt.Fprintln(w, "//go:build ppc64 || ppc64le")
179 fmt.Fprintln(w)
180 }
181
182 func zeroPPC64x(w io.Writer) {
183 // R0: always zero
184 // R3 (aka REGRT1): ptr to memory to be zeroed - 8
185 // On return, R3 points to the last zeroed dword.
186 fmt.Fprintln(w, "TEXT runtime·duffzero<ABIInternal>(SB), NOSPLIT|NOFRAME, $0-0")
187 for i := 0; i < 128; i++ {
188 fmt.Fprintln(w, "\tMOVDU\tR0, 8(R20)")
189 }
190 fmt.Fprintln(w, "\tRET")
191 }
192
193 func copyPPC64x(w io.Writer) {
194 // duffcopy is not used on PPC64.
195 fmt.Fprintln(w, "TEXT runtime·duffcopy<ABIInternal>(SB), NOSPLIT|NOFRAME, $0-0")
196 for i := 0; i < 128; i++ {
197 fmt.Fprintln(w, "\tMOVDU\t8(R20), R5")
198 fmt.Fprintln(w, "\tMOVDU\tR5, 8(R21)")
199 }
200 fmt.Fprintln(w, "\tRET")
201 }
202
203 func tagsMIPS64x(w io.Writer) {
204 fmt.Fprintln(w)
205 fmt.Fprintln(w, "//go:build mips64 || mips64le")
206 fmt.Fprintln(w)
207 }
208
209 func zeroMIPS64x(w io.Writer) {
210 // R0: always zero
211 // R1 (aka REGRT1): ptr to memory to be zeroed - 8
212 // On return, R1 points to the last zeroed dword.
213 fmt.Fprintln(w, "TEXT runtime·duffzero(SB), NOSPLIT|NOFRAME, $0-0")
214 for i := 0; i < 128; i++ {
215 fmt.Fprintln(w, "\tMOVV\tR0, 8(R1)")
216 fmt.Fprintln(w, "\tADDV\t$8, R1")
217 }
218 fmt.Fprintln(w, "\tRET")
219 }
220
221 func copyMIPS64x(w io.Writer) {
222 fmt.Fprintln(w, "TEXT runtime·duffcopy(SB), NOSPLIT|NOFRAME, $0-0")
223 for i := 0; i < 128; i++ {
224 fmt.Fprintln(w, "\tMOVV\t(R1), R23")
225 fmt.Fprintln(w, "\tADDV\t$8, R1")
226 fmt.Fprintln(w, "\tMOVV\tR23, (R2)")
227 fmt.Fprintln(w, "\tADDV\t$8, R2")
228 fmt.Fprintln(w)
229 }
230 fmt.Fprintln(w, "\tRET")
231 }