-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathemit_inst.go
392 lines (360 loc) · 9.62 KB
/
emit_inst.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package x64
import (
"fmt"
"math/bits"
. "github.com/wdamron/x64/internal/flags"
)
func (a *Assembler) emitInst() error {
buf := &a.b
match := &a.match
addrSize, opSize := match.addrSize, match.opSize
inst := match.inst
enc := match.enc
flags := enc.flags
op := enc.op[:enc.oplen()]
switch a.instPrefix {
case lockPrefix:
if flags&LOCK == 0 {
return fmt.Errorf("LOCK prefix unsupported for %s", inst.Name())
}
buf.Byte(lockPrefix)
case repPrefix:
if flags&(REP|REPE) == 0 {
return fmt.Errorf("REP/REPE/REPZ prefix unsupported for %s", inst.Name())
}
buf.Byte(repPrefix)
case repnePrefix:
if flags&REPE == 0 {
return fmt.Errorf("REPNE/REPNZ prefix unsupported for %s", inst.Name())
}
buf.Byte(repnePrefix)
default:
}
// determine if we need an address size override prefix
prefAddr := addrSize == 4
var prefMod byte
var hasPrefMod bool
var prefSeg byte
var hasPrefSeg bool
var prefSize bool
var rexW bool
var vexL bool
// determine if size prefixes are necessary
if hasFlag(flags, AUTO_SIZE) || hasFlag(flags, AUTO_NO32) || hasFlag(flags, AUTO_REXW) || hasFlag(flags, AUTO_VEXL) {
if opSize < 0 {
return fmt.Errorf("Bad formatting data for %s (op size = %v); no wildcard sizes", inst.Name(), opSize)
}
if hasFlag(flags, AUTO_NO32) {
switch opSize {
case 2:
prefSize = true
case 8:
// ok
default:
return fmt.Errorf("Unsupported operation size for 64-bit mode instruction %s: %v", inst.Name(), opSize)
}
} else if hasFlag(flags, AUTO_REXW) {
switch {
case opSize == 8:
rexW = true
case opSize != 4:
return fmt.Errorf("16-bit arguments are not supported for %s", inst.Name())
}
} else if hasFlag(flags, AUTO_VEXL) {
switch {
case opSize == 32:
vexL = true
case opSize != 16:
return fmt.Errorf("Bad operation size for AUTO_VEXL instruction %s: %v", inst.Name(), opSize)
}
} else if opSize == 2 {
prefSize = true
} else if opSize == 8 {
rexW = true
} else if opSize != 4 {
return fmt.Errorf("Bad operation size for instruction %s: %v", inst.Name(), opSize)
}
}
prefSize = prefSize || hasFlag(flags, WORD_SIZE) || hasFlag(flags, PREF_66)
rexW = rexW || hasFlag(flags, WITH_REXW)
vexL = vexL || hasFlag(flags, WITH_VEXL)
prefAddr = prefAddr || hasFlag(flags, PREF_67)
switch {
case hasFlag(flags, PREF_F0):
prefMod = 0xf0
hasPrefMod = true
case hasFlag(flags, PREF_F2):
prefMod = 0xf2
hasPrefMod = true
case hasFlag(flags, PREF_F3):
prefMod = 0xf3
hasPrefMod = true
}
needRex, err := a.checkRex(rexW)
if err != nil {
return err
}
var immOp byte
var hasImmOp bool
if hasFlag(flags, IMM_OP) {
immOp = op[len(op)-1]
op = op[:len(op)-1]
hasImmOp = true
}
if hasPrefSeg {
buf.Byte(prefSeg)
}
if prefAddr {
buf.Byte(0x67)
}
if hasFlag(flags, VEX_OP) || hasFlag(flags, XOP_OP) {
var pref uint8
switch {
case prefSize:
pref = 1
case prefMod == 0xf3:
pref = 2
case prefMod == 0xf2:
pref = 3
}
// map_sel is stored in the first byte of the opcode
mapSel := uint8(op[0])
op = op[1:]
a.emitVexXop(buf, enc, mapSel, pref, rexW, vexL)
} else {
if hasPrefMod {
buf.Byte(prefMod)
}
if prefSize {
buf.Byte(0x66)
}
if needRex {
a.emitRex(buf, match.r, match.m, rexW)
}
}
// if rm is embedded in the last opcode byte, push it here
if hasFlag(flags, SHORT_ARG) {
last := op[len(op)-1]
op = op[:len(op)-1]
buf.Bytes(op)
rm := match.m
match.m = nil
if rm == nil {
return fmt.Errorf("Bad formatting data for %s", inst.Name())
}
reg, ok := rm.(Reg)
if !ok {
return fmt.Errorf("Bad formatting data for %s", inst.Name())
}
buf.Byte(last + byte(reg.Num())&7)
} else {
buf.Bytes(op)
}
if match.m != nil {
// Direct ModRM addressing
if r2, ok := match.m.(Reg); ok {
r1, ok := match.r.(Reg)
if !ok {
r1 = Reg(Reg(addrSize)<<16 | REG_LEGACY<<8 | Reg(enc.reg()))
}
emitMSIB(buf, modDirect, r1, r2)
// Indirect ModRM (+SIB) addressing
} else if match.memOffset >= 0 {
m := match.mem
r, ok := match.r.(Reg)
if !ok {
r = Reg(Reg(addrSize)<<16 | REG_LEGACY<<8 | Reg(enc.reg()))
}
// check addressing mode special cases
modeVsib := m.Index != 0 && (m.Index.Family() == REG_XMM || m.Index.Family() == REG_YMM)
mode16 := addrSize == 2
modeRipRel := m.Base != 0 && m.Base.Family() == REG_RIP
modeRbpBase := m.Base != 0 && m.Base.Family() == REG_LEGACY && (m.Base.Num() == RBP.Num() || m.Base.Num() == R13.Num())
if modeVsib {
base := m.Base
mode := modDisp8
if base != 0 {
if m.Disp != nil && m.Disp.width() != 1 {
mode = modDisp32
}
} else {
base, mode = RBP, modNoBase
}
// always need a SIB byte for VSIB addressing
emitMSIB(buf, mode, r, Reg(4))
emitMSIB(buf, uint8(bits.TrailingZeros8(m.Scale)), m.Index, base)
if m.Disp != nil {
if mode == modDisp8 {
buf.Int8(int8(m.Disp.Int32()))
} else {
buf.Int32(m.Disp.Int32())
}
} else if mode == modDisp8 {
// no displacement was asked for, but we have to encode one as there's a base
buf.Int8(0)
} else {
// modNoBase requires a dword displacement, and if we got here no displacement was asked for.
buf.Int32(0)
}
} else if mode16 {
// 16-bit mode: the index/base combination has been encoded in the base register.
// this register is guaranteed to be present.
mode := modNoDisp
switch {
case m.Disp != nil && m.Disp.width() == 1:
mode = modDisp8
case m.Disp != nil:
mode = modDisp32
case modeRbpBase:
mode = modDisp8
}
// only need a mod.r/m byte for 16-bit addressing
emitMSIB(buf, mode, r, m.Base)
if m.Disp != nil {
if mode == modDisp8 {
buf.Int8(int8(m.Disp.Int32()))
} else {
buf.Int16(int16(m.Disp.Int32()))
}
} else if mode == modDisp8 {
buf.Int8(0)
}
} else if modeRipRel {
emitMSIB(buf, modNoDisp, r, Reg(5))
if m.Disp != nil {
buf.Int32(int32(m.Disp.Int32()))
if ld, ok := m.Disp.(LabelDisp); ok {
// the displacement will be patched with the relative label-offset + displacement during Finalize
a.relocDisp(ld)
} else if label, ok := m.Disp.(LabelArg); ok {
// the displacement will be patched with the relative label-offset during Finalize
a.reloc(label.label(), 4)
}
} else {
buf.Int32(0)
}
} else {
// normal addressing
base := m.Base
mode := modDisp32
switch {
case modeRbpBase && m.Disp == nil:
// RBP can only be encoded as base if a displacement is present.
mode = modDisp8
case m.Disp == nil || base == 0:
// mode_nodisp if no base is to be encoded. note that in these scenarions a 32-bit disp has to be emitted
mode = modNoDisp
case m.Disp != nil && m.Disp.width() == 1:
mode = modDisp8
}
// if there's an index we need to escape into the SIB byte
if m.Index != 0 {
if base == 0 {
base = RBP & (Reg(addrSize) << 16)
}
emitMSIB(buf, mode, r, RSP)
emitMSIB(buf, uint8(bits.TrailingZeros8(m.Scale)), m.Index, base)
} else if base != 0 {
emitMSIB(buf, mode, r, base)
} else {
emitMSIB(buf, mode, r, RSP)
emitMSIB(buf, 0, RSP, RBP)
}
// displacement
if m.Disp != nil {
width := uint8(1)
if mode == modDisp8 {
buf.Int8(int8(m.Disp.Int32()))
} else {
buf.Int32(m.Disp.Int32())
width = 4
}
if ld, ok := m.Disp.(LabelDisp); ok {
// the displacement will be patched with the relative label-offset + displacement during Finalize
a.relocDisp(ld)
} else if label, ok := m.Disp.(LabelArg); ok {
// the displacement will be patched with the relative label-offset during Finalize
a.reloc(label.label(), width)
}
} else if base == 0 {
buf.Int32(0)
} else if mode == modDisp8 {
buf.Int8(0)
}
}
}
}
// opcode encoded after the displacement
if hasImmOp {
buf.Byte(immOp)
}
// register in immediate argument
if match.i != nil {
ireg := match.i.(Reg)
b := ireg.Num() << 4
if len(match.imms) > 0 {
// if immediates are present, the register argument will be merged into the
// first immediate byte.
imm, ok := match.imms[0].(Imm8)
if !ok {
return fmt.Errorf("Bad formatting data for %s", inst.Name())
}
match.imms = match.imms[1:]
b = b | (uint8(imm) & 0xf)
}
buf.Byte(byte(b))
}
// immediates
for _, arg := range match.imms {
if imm, ok := arg.(ImmArg); ok {
switch imm.width() {
case 1:
buf.Int8(int8(imm.Int64()))
case 2:
buf.Int16(int16(imm.Int64()))
case 4:
buf.Int32(int32(imm.Int64()))
case 8:
buf.Int64(imm.Int64())
}
} else if rel, ok := arg.(RelArg); ok {
switch rel.width() {
case 1:
buf.Int8(int8(rel.Int32()))
case 2:
buf.Int16(int16(rel.Int32()))
case 4:
buf.Int32(int32(rel.Int32()))
}
} else if ld, ok := arg.(LabelDisp); ok {
width := ld.width()
switch width {
case 1:
buf.Int8(0)
case 2:
buf.Int16(0)
case 4:
buf.Int32(0)
default:
return fmt.Errorf("Invalid label displacement (up to 32-bit displacements are supported): %v", width)
}
// the displacement will be patched with the relative label-offset + displacement during Finalize
a.relocDisp(ld)
} else if label, ok := arg.(LabelArg); ok {
width := label.width()
switch width {
case 1:
buf.Int8(0)
case 2:
buf.Int16(0)
case 4:
buf.Int32(0)
default:
return fmt.Errorf("Invalid label displacement (up to 32-bit displacements are supported): %v", width)
}
// the displacement will be patched with the relative label-offset during Finalize
a.reloc(label.label(), width)
}
}
return nil
}