1package reg
2
3// Register kinds.
4const (
5	KindPseudo Kind = iota
6	KindGP
7	KindVector
8)
9
10// Declare register families.
11var (
12	Pseudo         = &Family{Kind: KindPseudo}
13	GeneralPurpose = &Family{Kind: KindGP}
14	Vector         = &Family{Kind: KindVector}
15
16	Families = []*Family{
17		Pseudo,
18		GeneralPurpose,
19		Vector,
20	}
21)
22
23var familiesByKind = map[Kind]*Family{}
24
25func init() {
26	for _, f := range Families {
27		familiesByKind[f.Kind] = f
28	}
29}
30
31// FamilyOfKind returns the Family of registers of the given kind, or nil if not found.
32func FamilyOfKind(k Kind) *Family {
33	return familiesByKind[k]
34}
35
36// Pseudo registers.
37var (
38	FramePointer   = Pseudo.define(S0, 0, "FP")
39	ProgramCounter = Pseudo.define(S0, 0, "PC")
40	StaticBase     = Pseudo.define(S0, 0, "SB")
41	StackPointer   = Pseudo.define(S0, 0, "SP")
42)
43
44// GP provides additional methods for general purpose registers.
45type GP interface {
46	As8() Register
47	As8L() Register
48	As8H() Register
49	As16() Register
50	As32() Register
51	As64() Register
52}
53
54// GPPhysical is a general-purpose physical register.
55type GPPhysical interface {
56	Physical
57	GP
58}
59
60type gpp struct {
61	Physical
62}
63
64func newgpp(r Physical) GPPhysical { return gpp{Physical: r} }
65
66func (p gpp) As8() Register  { return newgpp(p.as(S8).(Physical)) }
67func (p gpp) As8L() Register { return newgpp(p.as(S8L).(Physical)) }
68func (p gpp) As8H() Register { return newgpp(p.as(S8H).(Physical)) }
69func (p gpp) As16() Register { return newgpp(p.as(S16).(Physical)) }
70func (p gpp) As32() Register { return newgpp(p.as(S32).(Physical)) }
71func (p gpp) As64() Register { return newgpp(p.as(S64).(Physical)) }
72
73// GPVirtual is a general-purpose virtual register.
74type GPVirtual interface {
75	Virtual
76	GP
77}
78
79type gpv struct {
80	Virtual
81}
82
83func newgpv(v Virtual) GPVirtual { return gpv{Virtual: v} }
84
85func (v gpv) As8() Register  { return newgpv(v.as(S8).(Virtual)) }
86func (v gpv) As8L() Register { return newgpv(v.as(S8L).(Virtual)) }
87func (v gpv) As8H() Register { return newgpv(v.as(S8H).(Virtual)) }
88func (v gpv) As16() Register { return newgpv(v.as(S16).(Virtual)) }
89func (v gpv) As32() Register { return newgpv(v.as(S32).(Virtual)) }
90func (v gpv) As64() Register { return newgpv(v.as(S64).(Virtual)) }
91
92func gp(s Spec, id Index, name string, flags ...Info) GPPhysical {
93	r := newgpp(newregister(GeneralPurpose, s, id, name, flags...))
94	GeneralPurpose.add(r)
95	return r
96}
97
98// General purpose registers.
99var (
100	// Low byte
101	AL = gp(S8L, 0, "AL")
102	CL = gp(S8L, 1, "CL")
103	DL = gp(S8L, 2, "DL")
104	BL = gp(S8L, 3, "BL")
105
106	// High byte
107	AH = gp(S8H, 0, "AH")
108	CH = gp(S8H, 1, "CH")
109	DH = gp(S8H, 2, "DH")
110	BH = gp(S8H, 3, "BH")
111
112	// 8-bit
113	SPB  = gp(S8, 4, "SP", Restricted)
114	BPB  = gp(S8, 5, "BP")
115	SIB  = gp(S8, 6, "SI")
116	DIB  = gp(S8, 7, "DI")
117	R8B  = gp(S8, 8, "R8")
118	R9B  = gp(S8, 9, "R9")
119	R10B = gp(S8, 10, "R10")
120	R11B = gp(S8, 11, "R11")
121	R12B = gp(S8, 12, "R12")
122	R13B = gp(S8, 13, "R13")
123	R14B = gp(S8, 14, "R14")
124	R15B = gp(S8, 15, "R15")
125
126	// 16-bit
127	AX   = gp(S16, 0, "AX")
128	CX   = gp(S16, 1, "CX")
129	DX   = gp(S16, 2, "DX")
130	BX   = gp(S16, 3, "BX")
131	SP   = gp(S16, 4, "SP", Restricted)
132	BP   = gp(S16, 5, "BP")
133	SI   = gp(S16, 6, "SI")
134	DI   = gp(S16, 7, "DI")
135	R8W  = gp(S16, 8, "R8")
136	R9W  = gp(S16, 9, "R9")
137	R10W = gp(S16, 10, "R10")
138	R11W = gp(S16, 11, "R11")
139	R12W = gp(S16, 12, "R12")
140	R13W = gp(S16, 13, "R13")
141	R14W = gp(S16, 14, "R14")
142	R15W = gp(S16, 15, "R15")
143
144	// 32-bit
145	EAX  = gp(S32, 0, "AX")
146	ECX  = gp(S32, 1, "CX")
147	EDX  = gp(S32, 2, "DX")
148	EBX  = gp(S32, 3, "BX")
149	ESP  = gp(S32, 4, "SP", Restricted)
150	EBP  = gp(S32, 5, "BP")
151	ESI  = gp(S32, 6, "SI")
152	EDI  = gp(S32, 7, "DI")
153	R8L  = gp(S32, 8, "R8")
154	R9L  = gp(S32, 9, "R9")
155	R10L = gp(S32, 10, "R10")
156	R11L = gp(S32, 11, "R11")
157	R12L = gp(S32, 12, "R12")
158	R13L = gp(S32, 13, "R13")
159	R14L = gp(S32, 14, "R14")
160	R15L = gp(S32, 15, "R15")
161
162	// 64-bit
163	RAX = gp(S64, 0, "AX")
164	RCX = gp(S64, 1, "CX")
165	RDX = gp(S64, 2, "DX")
166	RBX = gp(S64, 3, "BX")
167	RSP = gp(S64, 4, "SP", Restricted)
168	RBP = gp(S64, 5, "BP")
169	RSI = gp(S64, 6, "SI")
170	RDI = gp(S64, 7, "DI")
171	R8  = gp(S64, 8, "R8")
172	R9  = gp(S64, 9, "R9")
173	R10 = gp(S64, 10, "R10")
174	R11 = gp(S64, 11, "R11")
175	R12 = gp(S64, 12, "R12")
176	R13 = gp(S64, 13, "R13")
177	R14 = gp(S64, 14, "R14")
178	R15 = gp(S64, 15, "R15")
179)
180
181// Vec provides methods for vector registers.
182type Vec interface {
183	AsX() Register
184	AsY() Register
185	AsZ() Register
186}
187
188// VecPhysical is a physical vector register.
189type VecPhysical interface {
190	Physical
191	Vec
192}
193
194type vecp struct {
195	Physical
196	Vec
197}
198
199func newvecp(r Physical) VecPhysical { return vecp{Physical: r} }
200
201func (p vecp) AsX() Register { return newvecp(p.as(S128).(Physical)) }
202func (p vecp) AsY() Register { return newvecp(p.as(S256).(Physical)) }
203func (p vecp) AsZ() Register { return newvecp(p.as(S512).(Physical)) }
204
205// VecVirtual is a virtual vector register.
206type VecVirtual interface {
207	Virtual
208	Vec
209}
210
211type vecv struct {
212	Virtual
213	Vec
214}
215
216func newvecv(v Virtual) VecVirtual { return vecv{Virtual: v} }
217
218func (v vecv) AsX() Register { return newvecv(v.as(S128).(Virtual)) }
219func (v vecv) AsY() Register { return newvecv(v.as(S256).(Virtual)) }
220func (v vecv) AsZ() Register { return newvecv(v.as(S512).(Virtual)) }
221
222func vec(s Spec, id Index, name string, flags ...Info) VecPhysical {
223	r := newvecp(newregister(Vector, s, id, name, flags...))
224	Vector.add(r)
225	return r
226}
227
228// Vector registers.
229var (
230	// 128-bit
231	X0  = vec(S128, 0, "X0")
232	X1  = vec(S128, 1, "X1")
233	X2  = vec(S128, 2, "X2")
234	X3  = vec(S128, 3, "X3")
235	X4  = vec(S128, 4, "X4")
236	X5  = vec(S128, 5, "X5")
237	X6  = vec(S128, 6, "X6")
238	X7  = vec(S128, 7, "X7")
239	X8  = vec(S128, 8, "X8")
240	X9  = vec(S128, 9, "X9")
241	X10 = vec(S128, 10, "X10")
242	X11 = vec(S128, 11, "X11")
243	X12 = vec(S128, 12, "X12")
244	X13 = vec(S128, 13, "X13")
245	X14 = vec(S128, 14, "X14")
246	X15 = vec(S128, 15, "X15")
247	X16 = vec(S128, 16, "X16")
248	X17 = vec(S128, 17, "X17")
249	X18 = vec(S128, 18, "X18")
250	X19 = vec(S128, 19, "X19")
251	X20 = vec(S128, 20, "X20")
252	X21 = vec(S128, 21, "X21")
253	X22 = vec(S128, 22, "X22")
254	X23 = vec(S128, 23, "X23")
255	X24 = vec(S128, 24, "X24")
256	X25 = vec(S128, 25, "X25")
257	X26 = vec(S128, 26, "X26")
258	X27 = vec(S128, 27, "X27")
259	X28 = vec(S128, 28, "X28")
260	X29 = vec(S128, 29, "X29")
261	X30 = vec(S128, 30, "X30")
262	X31 = vec(S128, 31, "X31")
263
264	// 256-bit
265	Y0  = vec(S256, 0, "Y0")
266	Y1  = vec(S256, 1, "Y1")
267	Y2  = vec(S256, 2, "Y2")
268	Y3  = vec(S256, 3, "Y3")
269	Y4  = vec(S256, 4, "Y4")
270	Y5  = vec(S256, 5, "Y5")
271	Y6  = vec(S256, 6, "Y6")
272	Y7  = vec(S256, 7, "Y7")
273	Y8  = vec(S256, 8, "Y8")
274	Y9  = vec(S256, 9, "Y9")
275	Y10 = vec(S256, 10, "Y10")
276	Y11 = vec(S256, 11, "Y11")
277	Y12 = vec(S256, 12, "Y12")
278	Y13 = vec(S256, 13, "Y13")
279	Y14 = vec(S256, 14, "Y14")
280	Y15 = vec(S256, 15, "Y15")
281	Y16 = vec(S256, 16, "Y16")
282	Y17 = vec(S256, 17, "Y17")
283	Y18 = vec(S256, 18, "Y18")
284	Y19 = vec(S256, 19, "Y19")
285	Y20 = vec(S256, 20, "Y20")
286	Y21 = vec(S256, 21, "Y21")
287	Y22 = vec(S256, 22, "Y22")
288	Y23 = vec(S256, 23, "Y23")
289	Y24 = vec(S256, 24, "Y24")
290	Y25 = vec(S256, 25, "Y25")
291	Y26 = vec(S256, 26, "Y26")
292	Y27 = vec(S256, 27, "Y27")
293	Y28 = vec(S256, 28, "Y28")
294	Y29 = vec(S256, 29, "Y29")
295	Y30 = vec(S256, 30, "Y30")
296	Y31 = vec(S256, 31, "Y31")
297
298	// 512-bit
299	Z0  = vec(S512, 0, "Z0")
300	Z1  = vec(S512, 1, "Z1")
301	Z2  = vec(S512, 2, "Z2")
302	Z3  = vec(S512, 3, "Z3")
303	Z4  = vec(S512, 4, "Z4")
304	Z5  = vec(S512, 5, "Z5")
305	Z6  = vec(S512, 6, "Z6")
306	Z7  = vec(S512, 7, "Z7")
307	Z8  = vec(S512, 8, "Z8")
308	Z9  = vec(S512, 9, "Z9")
309	Z10 = vec(S512, 10, "Z10")
310	Z11 = vec(S512, 11, "Z11")
311	Z12 = vec(S512, 12, "Z12")
312	Z13 = vec(S512, 13, "Z13")
313	Z14 = vec(S512, 14, "Z14")
314	Z15 = vec(S512, 15, "Z15")
315	Z16 = vec(S512, 16, "Z16")
316	Z17 = vec(S512, 17, "Z17")
317	Z18 = vec(S512, 18, "Z18")
318	Z19 = vec(S512, 19, "Z19")
319	Z20 = vec(S512, 20, "Z20")
320	Z21 = vec(S512, 21, "Z21")
321	Z22 = vec(S512, 22, "Z22")
322	Z23 = vec(S512, 23, "Z23")
323	Z24 = vec(S512, 24, "Z24")
324	Z25 = vec(S512, 25, "Z25")
325	Z26 = vec(S512, 26, "Z26")
326	Z27 = vec(S512, 27, "Z27")
327	Z28 = vec(S512, 28, "Z28")
328	Z29 = vec(S512, 29, "Z29")
329	Z30 = vec(S512, 30, "Z30")
330	Z31 = vec(S512, 31, "Z31")
331)
332