/
encode.go
242 lines (195 loc) · 6.87 KB
/
encode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright (c) 2018 Timo Savola. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package in
import (
"github.com/tsavola/wag/internal/gen/reg"
"github.com/tsavola/wag/wa"
)
// cat sets bit 26 based on type category.
func cat(t wa.Type) uint32 {
return uint32(t.Category()) << 26
}
// sf sets bit 31 based on size.
func sf(t wa.Size) uint32 {
bit4 := uint32(t & 8)
return bit4 << 28
}
// sfN sets bits 22 and 31 based on size.
func sfN(t wa.Size) uint32 {
bit4 := uint32(t & 8)
return bit4<<28 | bit4<<19
}
// sizeLSB sets bit 30 based on type size.
func sizeLSB(t wa.Type) uint32 {
bit4 := uint32(t & 8)
return bit4 << 27
}
// scalarType sets bit 22 based on size.
func scalarType(t wa.Size) uint32 {
bit4 := uint32(t & 8)
return bit4 << 19
}
func Int9(i int32) uint32 { return uint32(i) & 0x1ff }
func Uint12(i uint64) uint32 { return uint32(i) & 0xfff }
func Int14(i int32) uint32 { return uint32(i) & 0x3fff }
func Uint16(i uint64) uint32 { return uint32(i) & 0xffff }
func Int19(i int32) uint32 { return uint32(i) & 0x7ffff }
func Int26(i int32) uint32 { return uint32(i) & 0x3ffffff }
type Cond uint32
const (
EQ = Cond(0x0) // equal to
NE = Cond(0x1) // not equal to
CS = Cond(0x2) // carry set
CC = Cond(0x3) // carry clear
MI = Cond(0x4) // minus, negative
PL = Cond(0x5) // positive or zero
VS = Cond(0x6) // signed overflow
VC = Cond(0x7) // no signed overflow
HI = Cond(0x8) // greater than (unsigned)
LS = Cond(0x9) // less than or equal to (unsigned)
GE = Cond(0xa) // greater than or equal to (signed)
LT = Cond(0xb) // less than (signed)
GT = Cond(0xc) // greater than (signed)
LE = Cond(0xd) // less than or equal to (signed)
HS = CS // greater than or equal to (unsigned)
LO = CC // less than (unsigned)
)
type S uint32
const (
Unscaled = S(0 << 12)
Scaled = S(1 << 12)
)
type Shift uint32
const (
LSL = Shift(0 << 22)
LSR = Shift(1 << 22)
ASR = Shift(2 << 22)
)
type Ext uint32
const (
UXTB = Ext(0 << 13)
UXTH = Ext(1 << 13)
UXTW = Ext(2 << 13)
UXTX = Ext(3 << 13)
SXTB = Ext(4 << 13)
SXTH = Ext(5 << 13)
SXTW = Ext(6 << 13)
SXTX = Ext(7 << 13)
)
func SizeZeroExt(t wa.Size) Ext {
bit3 := uint32(t & 8)
return UXTW | Ext(bit3<<10)
}
func SizeSignExt(t wa.Size) Ext {
bit3 := uint32(t & 8)
return SXTW | Ext(bit3<<10)
}
type CondImm19 uint32
type Imm16 uint32
type Imm26 uint32
type Reg uint32
type RegImm14Bit uint32
type RegImm16HwSf uint32
type RegImm19Imm2 uint32
type RegImm19Size uint32
type RegRegCondRegSf uint32
type RegRegImm3ExtRegSf uint32
type RegRegImm6Imm6NSf uint32
type RegRegImm6RegShiftSf uint32
type RegRegImm9 uint32
type RegRegImm9Size uint32
type RegRegImm12ShiftSf uint32
type RegRegImm12Size uint32
type RegRegSf uint32
type RegRegSOptionReg uint32
type RegRegSOptionRegSize uint32
type RegRegCondRegType uint32
type RegRegType uint32
type RegRegTypeSf uint32
type RegRegRegSf uint32
type RegRegRegType uint32
type RegRegRegRegSf uint32
type DiscardRegRegType uint32
type SystemReg uint32
func (op CondImm19) CondI19(cond Cond, imm uint32) uint32 {
return uint32(op) | imm<<5 | uint32(cond)
}
func (op Imm16) I16(imm uint32) uint32 {
return uint32(op) | imm
}
func (op Imm26) I26(imm uint32) uint32 {
return uint32(op) | imm
}
func (op Reg) Rn(rn reg.R) uint32 {
return uint32(op) | uint32(rn)<<5
}
func (op RegImm14Bit) RtI14Bit(rt reg.R, imm, bit uint32) uint32 {
return uint32(op) | (bit&0x20)<<26 | (bit&0x1f)<<19 | imm<<5 | uint32(rt)
}
func (op RegImm16HwSf) RdI16Hw(rd reg.R, imm, hw uint32, t wa.Size) uint32 {
return uint32(op) | sf(t) | hw<<21 | imm<<5 | uint32(rd)
}
func (op RegImm19Imm2) RdI19hiI2lo(r reg.R, hi, lo uint32) uint32 {
return uint32(op) | lo<<29 | hi<<5 | uint32(r)
}
func (op RegImm19Size) RtI19(r reg.R, imm uint32, t wa.Size) uint32 {
return uint32(op) | sf(t) | imm<<5 | uint32(r)
}
func (op RegRegCondRegSf) RdRnCondRm(rd, rn reg.R, cond Cond, rm reg.R, t wa.Size) uint32 {
return uint32(op) | sf(t) | uint32(rm)<<16 | uint32(cond)<<12 | uint32(rn)<<5 | uint32(rd)
}
func (op RegRegImm3ExtRegSf) RdRnI3ExtRm(rd, rn reg.R, imm uint32, option Ext, rm reg.R, t wa.Size) uint32 {
return uint32(op) | sf(t) | uint32(rm)<<16 | uint32(option) | imm<<10 | uint32(rn)<<5 | uint32(rd)
}
func (op RegRegImm6Imm6NSf) RdRnI6sI6r(rd, rn reg.R, imms, immr uint32, t wa.Size) uint32 {
return uint32(op) | sfN(t) | immr<<16 | imms<<10 | uint32(rn)<<5 | uint32(rd)
}
func (op RegRegImm6RegShiftSf) RdRnI6RmS2(rd, rn reg.R, imm uint32, rm reg.R, shift Shift, t wa.Size) uint32 {
return uint32(op) | sf(t) | uint32(shift) | uint32(rm)<<16 | imm<<10 | uint32(rn)<<5 | uint32(rd)
}
func (op RegRegImm9) RtRnI9(rt, rn reg.R, imm uint32) uint32 {
return uint32(op) | imm<<12 | uint32(rn)<<5 | uint32(rt)
}
func (op RegRegImm9Size) RtRnI9(rt, rn reg.R, imm uint32, t wa.Type) uint32 {
return uint32(op) | sizeLSB(t) | cat(t) | imm<<12 | uint32(rn)<<5 | uint32(rt)
}
func (op RegRegImm12ShiftSf) RdRnI12S2(rd, rn reg.R, imm, shift uint32, t wa.Size) uint32 {
return uint32(op) | sf(t) | shift<<22 | imm<<10 | uint32(rn)<<5 | uint32(rd)
}
func (op RegRegImm12Size) RdRnI12(rt, rn reg.R, imm uint32, t wa.Type) uint32 {
return uint32(op) | sizeLSB(t) | cat(t) | imm<<10 | uint32(rn)<<5 | uint32(rt)
}
func (op RegRegSf) RdRn(rd, rn reg.R, t wa.Size) uint32 {
return uint32(op) | sf(t) | uint32(rn)<<5 | uint32(rd)
}
func (op RegRegSOptionReg) RtRnSOptionRm(rt, rn reg.R, s S, option Ext, rm reg.R) uint32 {
return uint32(op) | uint32(rm)<<16 | uint32(option) | uint32(s) | uint32(rn)<<5 | uint32(rt)
}
func (op RegRegSOptionRegSize) RtRnSOptionRm(rt, rn reg.R, s S, option Ext, rm reg.R, t wa.Type) uint32 {
return uint32(op) | sizeLSB(t) | cat(t) | uint32(rm)<<16 | uint32(option) | uint32(s) | uint32(rn)<<5 | uint32(rt)
}
func (op RegRegCondRegType) RdRnCondRm(rd, rn reg.R, cond Cond, rm reg.R, t wa.Size) uint32 {
return uint32(op) | scalarType(t) | uint32(rm)<<16 | uint32(cond)<<12 | uint32(rn)<<5 | uint32(rd)
}
func (op RegRegType) RdRn(rd, rn reg.R, t wa.Size) uint32 {
return uint32(op) | scalarType(t) | uint32(rn)<<5 | uint32(rd)
}
func (op RegRegTypeSf) RdRn(rd, rn reg.R, floatType, intType wa.Size) uint32 {
return uint32(op) | sf(intType) | scalarType(floatType) | uint32(rn)<<5 | uint32(rd)
}
func (op RegRegRegSf) RdRnRm(rd, rn, rm reg.R, t wa.Size) uint32 {
return uint32(op) | sf(t) | uint32(rm)<<16 | uint32(rn)<<5 | uint32(rd)
}
func (op RegRegRegType) RdRnRm(rd, rn, rm reg.R, t wa.Size) uint32 {
return uint32(op) | scalarType(t) | uint32(rm)<<16 | uint32(rn)<<5 | uint32(rd)
}
func (op RegRegRegRegSf) RdRnRaRm(rd, rn, ra, rm reg.R, t wa.Size) uint32 {
return uint32(op) | sf(t) | uint32(rm)<<16 | uint32(ra)<<10 | uint32(rn)<<5 | uint32(rd)
}
func (op DiscardRegRegType) RnRm(rn, rm reg.R, t wa.Size) uint32 {
return uint32(op) | scalarType(t) | uint32(rm)<<16 | uint32(rn)<<5
}
func (op SystemReg) Rt(rt reg.R) uint32 {
return uint32(op) | uint32(rt)
}