-
Notifications
You must be signed in to change notification settings - Fork 62
/
x86.d
2010 lines (1723 loc) · 47.4 KB
/
x86.d
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
*
* Higgs JavaScript Virtual Machine
*
* This file is part of the Higgs project. The project is distributed at:
* https://github.com/maximecb/Higgs
*
* Copyright (c) 2012-2015, Maxime Chevalier-Boisvert. All rights reserved.
*
* This software is licensed under the following license (Modified BSD
* License):
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
* NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*****************************************************************************/
module jit.x86;
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.stdint;
import std.bitmanip;
import std.algorithm;
import jit.codeblock;
// Number of x86 registers
const NUM_REGS = 16;
/**
Representation of an x86 register
*/
struct X86Reg
{
alias Type = uint8_t;
enum : Type
{
GP,
FP,
XMM,
IP
}
/// Register type
uint8_t type;
/// Register index number
uint8_t regNo;
/// Size in bits
uint16_t size;
this(Type type, size_t regNo, size_t size)
{
assert (
regNo < NUM_REGS,
"invalid register number"
);
assert (
size <= 256,
"invalid register size"
);
this.type = type;
this.regNo = cast(uint8_t)regNo;
this.size = cast(uint16_t)size;
}
/**
Get a register with the same type and register number
but a potentially different size
*/
X86Reg reg(size_t numBits = 0) const
{
if (numBits is 0 || numBits is this.size)
return this;
return X86Reg(this.type, this.regNo, numBits);
}
/**
Get an operand object for a register of the requested size
*/
X86Opnd opnd(size_t numBits = 0) const
{
return X86Opnd(reg(numBits));
}
/**
Produce a string representation of the register
*/
string toString() const
{
switch (type)
{
case GP:
if (regNo < 8)
{
auto rs = "";
final switch (regNo)
{
case 0: rs = "a"; break;
case 1: rs = "c"; break;
case 2: rs = "d"; break;
case 3: rs = "b"; break;
case 4: rs = "sp"; break;
case 5: rs = "bp"; break;
case 6: rs = "si"; break;
case 7: rs = "di"; break;
}
final switch (size)
{
case 8 : return rs ~ "l";
case 16: return (rs.length == 1)? (rs ~ "x"):rs;
case 32: return (rs.length == 1)? ("e" ~ rs ~ "x"):("e" ~ rs);
case 64: return (rs.length == 1)? ("r" ~ rs ~ "x"):("r" ~ rs);
}
}
else
{
final switch (size)
{
case 8 : return "r" ~ to!string(regNo) ~ "l";
case 16: return "r" ~ to!string(regNo) ~ "w";
case 32: return "r" ~ to!string(regNo) ~ "d";
case 64: return "r" ~ to!string(regNo);
}
}
assert (false);
case XMM:
return "xmm" ~ to!string(regNo);
case FP:
return "st" ~ to!string(regNo);
case IP:
return "rip";
default:
assert (false);
}
}
/**
Comparison operator
*/
bool opEquals(immutable X86Reg that) const
{
return (
this.type == that.type &&
this.regNo == that.regNo &&
this.size == that.size
);
}
/**
Test if the REX prefix is needed to encode this operand
*/
bool rexNeeded() const
{
return (
regNo > 7 ||
(size == 8 && regNo >= 4 && regNo <= 7)
);
}
}
// Auto-generate named register constants
string genRegCsts()
{
auto decls = appender!string();
void genCst(ubyte type, string typeStr, ubyte regNo, ubyte numBits)
{
auto regName = (new X86Reg(type, regNo, numBits)).toString();
auto upName = regName.toUpper();
decls.put(
"immutable X86Reg " ~ upName ~ " = X86Reg(" ~ typeStr ~ ", " ~
to!string(regNo) ~ ", " ~ to!string(numBits) ~ ");\n"
);
}
for (ubyte regNo = 0; regNo < NUM_REGS; ++regNo)
{
genCst(X86Reg.GP, "X86Reg.GP", regNo, 8);
genCst(X86Reg.GP, "X86Reg.GP", regNo, 16);
genCst(X86Reg.GP, "X86Reg.GP", regNo, 32);
genCst(X86Reg.GP, "X86Reg.GP", regNo, 64);
genCst(X86Reg.XMM, "X86Reg.XMM", regNo, 128);
}
// Instruction pointer (RIP)
genCst(X86Reg.IP, "X86Reg.IP", 5, 64);
// Floating-point registers (x87)
genCst(X86Reg.FP, "X86Reg.FP", 0, 80);
return decls.data;
}
mixin(genRegCsts());
unittest
{
X86Reg r = EAX;
assert (r == EAX && EAX == EAX && EAX != EBX);
}
/**
Immediate operand value (constant)
*/
struct X86Imm
{
union
{
// Signed immediate value
int64_t imm;
// Unsigned immediate value
uint64_t unsgImm;
}
/**
Create an immediate operand
*/
this(int64_t imm)
{
this.imm = imm;
}
/**
Create a pointer constant
*/
this(void* ptr)
{
this.unsgImm = cast(uint64_t)ptr;
}
string toString() const
{
return to!string(this.imm);
}
/**
Compute the immediate value size
*/
size_t immSize() const
{
// Compute the smallest size this immediate fits in
if (imm >= int8_t.min && imm <= int8_t.max)
return 8;
if (imm >= int16_t.min && imm <= int16_t.max)
return 16;
if (imm >= int32_t.min && imm <= int32_t.max)
return 32;
return 64;
}
/**
Immediate value size if treated as unsigned
*/
size_t unsgSize() const
{
if (unsgImm <= uint8_t.max)
return 8;
else if (unsgImm <= uint16_t.max)
return 16;
else if (unsgImm <= uint32_t.max)
return 32;
return 64;
}
}
/**
Memory location operand
Note: we assume that the base and index are both 64-bit registers
and that the memory operand always has a base register.
*/
struct X86Mem
{
// Bit field for compact encoding
mixin(bitfields!(
/// Memory location size
uint, "size" , 10,
/// Base register number
uint, "baseRegNo", 4,
/// Index register number
uint, "idxRegNo" , 4,
/// SIB scale exponent value (power of two)
uint, "scaleExp" , 2,
/// Has index register flag
bool, "hasIdx" , 1,
/// IP-relative addressing flag
bool, "isIPRel" , 1,
/// Padding bits
uint, "", 10
));
/// Constant displacement from the base, not scaled
int32_t disp;
this(
size_t size,
X86Reg base,
int64_t disp = 0,
size_t scale = 0,
X86Reg index = RAX,
)
{
assert (
base.size is 64 &&
index.size is 64,
"base and index must be 64-bit registers"
);
assert (
disp >= int32_t.min && disp <= int32_t.max,
"disp value out of bounds"
);
this.size = cast(uint8_t)size;
this.baseRegNo = base.regNo;
this.disp = cast(int32_t)disp;
this.idxRegNo = index.regNo;
this.hasIdx = scale !is 0;
this.isIPRel = base.type is X86Reg.IP;
switch (scale)
{
case 0: break;
case 1: this.scaleExp = 0; break;
case 2: this.scaleExp = 1; break;
case 4: this.scaleExp = 2; break;
case 8: this.scaleExp = 3; break;
default: assert (false);
}
}
string toString() const
{
return toString(this.disp/*, null*/);
}
string toString(int32_t disp/*, const Label label*/) const
{
auto str = "";
/*
assert (
!(label && this.base != RIP),
"label provided when base is not RIP"
);
*/
switch (this.size)
{
case 8: str ~= "byte"; break;
case 16: str ~= "word"; break;
case 32: str ~= "dword"; break;
case 64: str ~= "qword"; break;
case 128: str ~= "oword"; break;
default:
assert (false, "unknown operand size");
}
if (str != "")
str ~= " ";
str ~= X86Reg(isIPRel? X86Reg.IP:X86Reg.GP, this.baseRegNo, 64).toString();
/*
if (label)
{
if (str != "")
str ~= " ";
str ~= label.name;
}
*/
if (disp)
{
if (str != "")
{
if (disp < 0)
str ~= " - " ~ to!string(-disp);
else
str ~= " + " ~ to!string(disp);
}
else
{
str ~= disp;
}
}
if (this.hasIdx)
{
if (str != "")
str ~= " + ";
if (this.scaleExp !is 0)
str ~= to!string(1 << this.scaleExp) ~ " * ";
str ~= X86Reg(X86Reg.GP, this.idxRegNo, 64).toString();
}
return '[' ~ str ~ ']';
}
/**
Test if the REX prefix is needed to encode this operand
*/
bool rexNeeded() const
{
return (baseRegNo > 7) || (hasIdx && idxRegNo > 7);
}
/**
Test if an SIB byte is needed to encode this operand
*/
bool sibNeeded() const
{
return (
this.hasIdx ||
this.baseRegNo == ESP.regNo ||
this.baseRegNo == RSP.regNo ||
this.baseRegNo == R12.regNo
);
}
/**
Compute the size of the displacement field needed
*/
size_t dispSize() const
{
// If using RIP as the base, use disp32
if (isIPRel)
return 32;
// Compute the required displacement size
if (disp != 0)
{
if (disp >= int8_t.min && disp <= int8_t.max)
return 8;
if (disp >= int32_t.min && disp <= int32_t.max)
return 32;
assert (false, "displacement does not fit in 32 bits: " ~ to!string(disp));
}
// If EBP or RBP or R13 is used as the base, displacement must be encoded
if (baseRegNo == EBP.regNo || baseRegNo == RBP.regNo || baseRegNo == R13.regNo)
return 8;
return 0;
}
}
/**
IP-relative memory location
*/
// TODO: reimplement without inheritance, when needed
// just store an X86Mem inside
// or modify X86Mem to support IP-relative mode
/*
class X86IPRel : X86Mem
{
/// Label to use as a reference
Label label;
/// Additional displacement relative to the label
int32_t labelDisp;
this(
size_t size,
Label label,
int32_t disp = 0,
X86Reg index = null,
size_t scale = 1
)
{
super(size, RIP, disp, index, scale);
this.label = label;
this.labelDisp = disp;
}
override string toString() const
{
return super.toString(this.labelDisp, this.label);
}
}
*/
/**
Polymorphic X86 operand wrapper
*/
struct X86Opnd
{
union
{
X86Reg reg;
X86Imm imm;
X86Mem mem;
}
enum Kind : uint8_t
{
NONE,
REG,
IMM,
MEM,
IPREL
}
Kind kind;
static immutable X86Opnd NONE = X86Opnd(Kind.NONE);
this(Kind k) { assert (k is Kind.NONE); kind = k; }
this(X86Reg r) { reg = r; kind = Kind.REG; }
this(X86Imm i) { imm = i; kind = Kind.IMM; }
this(X86Mem m) { mem = m; kind = Kind.MEM; }
/// Memory operand constructor
this(
size_t size,
X86Reg base,
int64_t disp = 0,
size_t scale = 0,
X86Reg index = RAX,
)
{
this(X86Mem(size, base, disp, scale, index));
}
/// Immediate constructor
this(int64_t i) { imm = X86Imm(i); kind = Kind.IMM; }
string toString() const
{
with (Kind) final switch (kind)
{
case REG: return reg.toString();
case IMM: return imm.toString();
case MEM: return mem.toString();
case NONE: return "NONE";
case IPREL: assert(false);
}
}
/// Comparison operator
bool opEquals(immutable X86Opnd that) const
{
if (kind != that.kind)
return false;
with (Kind) final switch (kind)
{
case NONE: return true;
case REG: return reg == that.reg;
case IMM: return imm == that.imm;
case MEM: return mem == that.mem;
case IPREL: assert(false);
}
}
bool isNone() const { return kind is Kind.NONE; }
bool isReg() const { return kind is Kind.REG; }
bool isImm() const { return kind is Kind.IMM; }
bool isMem() const { return kind is Kind.MEM; }
bool isXMM() const { return kind is Kind.REG && reg.type is X86Reg.XMM; }
bool isGPR() const { return kind is Kind.REG && reg.type is X86Reg.GP; }
bool isGPR32() const { return isGPR && reg.size is 32; }
bool isGPR64() const { return isGPR && reg.size is 64; }
bool isMem32() const { return isMem && mem.size is 32; }
bool isMem64() const { return isMem && mem.size is 64; }
bool rexNeeded()
{
return (kind is Kind.REG && reg.rexNeeded) || (kind is Kind.MEM && mem.rexNeeded);
}
bool sibNeeded()
{
return (kind is Kind.MEM && mem.sibNeeded);
}
size_t size()
{
with (Kind) switch (kind)
{
case MEM: return this.mem.size;
case REG: return this.reg.size;
default: assert (false);
}
}
}
/**
Write the REX byte
*/
void writeREX(
CodeBlock cb,
bool wFlag,
uint8_t regNo,
uint8_t idxRegNo = 0,
uint8_t rmRegNo = 0
)
{
// 0 1 0 0 w r x b
// w - 64-bit operand size flag
// r - MODRM.reg extension
// x - SIB.index extension
// b - MODRM.rm or SIB.base extension
auto w = wFlag? 1:0;
auto r = (regNo & 8)? 1:0;
auto x = (idxRegNo & 8)? 1:0;
auto b = (rmRegNo & 8)? 1:0;
// Encode and write the REX byte
auto rexByte = 0x40 + (w << 3) + (r << 2) + (x << 1) + (b);
cb.writeByte(cast(byte)rexByte);
}
/**
Write an opcode byte with an embedded register operand
*/
@nogc void writeOpcode(CodeBlock cb, ubyte opcode, X86Reg rOpnd)
{
// Write the reg field into the opcode byte
uint8_t opByte = opcode | (rOpnd.regNo & 7);
cb.writeByte(opByte);
}
/**
Encode a single operand RM instruction
*/
void writeRMInstr(
char rmOpnd,
ubyte opExt,
opcode...)
(CodeBlock cb, bool szPref, bool rexW, X86Opnd opnd0, X86Opnd opnd1)
{
static assert (opcode.length > 0 && opcode.length <= 3);
// Flag to indicate the REX prefix is needed
bool rexNeeded = rexW || opnd0.rexNeeded || opnd1.rexNeeded;
// Flag to indicate SIB byte is needed
bool sibNeeded = opnd0.sibNeeded || opnd1.sibNeeded;
// r and r/m operands
X86Reg* rOpnd = null;
X86Mem* rmOpndM = null;
X86Reg* rmOpndR = null;
switch (opnd0.kind)
{
case X86Opnd.Kind.REG:
if (rmOpnd is 'l')
rmOpndR = &opnd0.reg;
else
rOpnd = &opnd0.reg;
break;
case X86Opnd.Kind.MEM:
if (rmOpnd is 'l')
rmOpndM = &opnd0.mem;
else
assert (false);
break;
default:
assert (false);
}
switch (opnd1.kind)
{
case X86Opnd.Kind.REG:
if (rmOpnd is 'r')
rmOpndR = &opnd1.reg;
else
rOpnd = &opnd1.reg;
break;
case X86Opnd.Kind.MEM:
if (rmOpnd is 'r')
rmOpndM = &opnd1.mem;
else
assert (false, "mem opnd but right-opnd is not r/m");
break;
case X86Opnd.Kind.NONE:
break;
default:
assert (false, "invalid second operand: " ~ opnd1.toString());
}
// Add the operand-size prefix, if needed
if (szPref == true)
cb.writeByte(0x66);
/*
// Write the prefix bytes to the code block
codeBlock.writeBytes(enc.prefix);
*/
// Add the REX prefix, if needed
if (rexNeeded)
{
// 0 1 0 0 w r x b
// w - 64-bit operand size flag
// r - MODRM.reg extension
// x - SIB.index extension
// b - MODRM.rm or SIB.base extension
uint w = rexW? 1:0;
uint r;
if (rOpnd)
r = (rOpnd.regNo & 8)? 1:0;
else
r = 0;
uint x;
if (sibNeeded && rmOpndM.hasIdx)
x = (rmOpndM.idxRegNo & 8)? 1:0;
else
x = 0;
uint b;
if (rmOpndR)
b = (rmOpndR.regNo & 8)? 1:0;
else if (rmOpndM)
b = (rmOpndM.baseRegNo & 8)? 1:0;
else
b = 0;
// Encode and write the REX byte
auto rexByte = 0x40 + (w << 3) + (r << 2) + (x << 1) + (b);
cb.writeByte(cast(byte)rexByte);
}
// Write the opcode bytes to the code block
cb.writeBytes(opcode);
// MODRM.mod (2 bits)
// MODRM.reg (3 bits)
// MODRM.rm (3 bits)
assert (
!(opExt != 0xFF && rOpnd),
"opcode extension and register operand present"
);
// Encode the mod field
int mod;
if (rmOpndR)
{
mod = 3;
}
else
{
if (rmOpndM.dispSize == 0 || rmOpndM.isIPRel)
mod = 0;
else if (rmOpndM.dispSize == 8)
mod = 1;
else if (rmOpndM.dispSize == 32)
mod = 2;
}
// Encode the reg field
int reg;
if (opExt != 0xFF)
reg = opExt;
else if (rOpnd)
reg = rOpnd.regNo & 7;
else
reg = 0;
// Encode the rm field
int rm;
if (rmOpndR)
{
rm = rmOpndR.regNo & 7;
}
else
{
if (sibNeeded)
rm = 4;
else
rm = rmOpndM.baseRegNo & 7;
}
// Encode and write the ModR/M byte
auto rmByte = (mod << 6) + (reg << 3) + (rm);
cb.writeByte(cast(ubyte)rmByte);
//writefln("rmByte: %s", rmByte);
// Add the SIB byte, if needed
if (sibNeeded)
{
// SIB.scale (2 bits)
// SIB.index (3 bits)
// SIB.base (3 bits)
assert (
rmOpndM !is null,
"expected r/m opnd to be mem loc"
);
// Encode the scale value
int scale = rmOpndM.scaleExp;
// Encode the index value
int index;
if (rmOpndM.hasIdx is false)
index = 4;
else
index = rmOpndM.idxRegNo & 7;
// Encode the base register
auto base = rmOpndM.baseRegNo & 7;
// Encode and write the SIB byte
auto sibByte = (scale << 6) + (index << 3) + (base);
cb.writeByte(cast(uint8_t)sibByte);
}
// Add the displacement size
if (rmOpndM && rmOpndM.dispSize != 0)
cb.writeInt(rmOpndM.disp, rmOpndM.dispSize);
}
/**
Encode an add-like RM instruction with multiple possible encodings
*/
void writeRMMulti(
string mnem,
ubyte opMemReg8,
ubyte opMemRegPref,
ubyte opRegMem8,
ubyte opRegMemPref,
ubyte opMemImm8,
ubyte opMemImmSml,
ubyte opMemImmLrg,
ubyte opExtImm
)
(CodeBlock cb, X86Opnd opnd0, X86Opnd opnd1)
{
// Write a disassembly string
if (!opnd1.isNone)
cb.writeASM(mnem, opnd0, opnd1);
else
cb.writeASM(mnem, opnd0);
// Check the size of opnd0
size_t opndSize;
if (opnd0.isReg)
opndSize = opnd0.reg.size;
else if (opnd0.isMem)
opndSize = opnd0.mem.size;
else
assert (false, "invalid first operand for " ~ mnem ~ ": " ~ opnd0.toString());
// Check the size of opnd1
if (opnd1.isReg)
{
assert (opnd1.reg.size is opndSize, "operand size mismatch");
}
else if (opnd1.isMem)
{
assert (
opnd1.mem.size is opndSize,
format(
"operand size mismatch for %s (%s <= %s)",
mnem,
opndSize,
opnd1.mem.size
)
);
}
else if (opnd1.isImm)
{
assert (
opnd1.imm.immSize <= opndSize,
format(
"immediate too large for dst %s (%s <= %s)",
mnem,
opnd0,
opnd1
)
);
}
assert (opndSize is 8 || opndSize is 16 || opndSize is 32 || opndSize is 64);
auto szPref = opndSize is 16;
auto rexW = opndSize is 64;
// R/M + Reg
if ((opnd0.isMem && opnd1.isReg) ||
(opnd0.isReg && opnd1.isReg))
{
if (opndSize is 8)
cb.writeRMInstr!('l', 0xFF, opMemReg8)(false, false, opnd0, opnd1);
else
cb.writeRMInstr!('l', 0xFF, opMemRegPref)(szPref, rexW, opnd0, opnd1);
}
// Reg + R/M
else if (opnd0.isReg && opnd1.isMem)
{
if (opndSize is 8)
cb.writeRMInstr!('r', 0xFF, opRegMem8)(false, false, opnd0, opnd1);
else
cb.writeRMInstr!('r', 0xFF, opRegMemPref)(szPref, rexW, opnd0, opnd1);
}
// R/M + Imm
else if (opnd1.isImm)
{
auto imm = opnd1.imm;
// 8-bit immediate
if (imm.immSize <= 8)
{
if (opndSize is 8)
cb.writeRMInstr!('l', opExtImm, opMemImm8)(false, false, opnd0, X86Opnd.NONE);
else
cb.writeRMInstr!('l', opExtImm, opMemImmSml)(szPref, rexW, opnd0, X86Opnd.NONE);
cb.writeInt(imm.imm, 8);
}
// 32-bit immediate
else if (imm.immSize <= 32)
{
assert (imm.immSize <= opndSize, "immediate too large for dst");
cb.writeRMInstr!('l', opExtImm, opMemImmLrg)(szPref, rexW, opnd0, X86Opnd.NONE);
cb.writeInt(imm.imm, min(opndSize, 32));
}
// Immediate too large
else
{
assert (false, "immediate value too large");
}
}
// Invalid operands
else
{
assert (
false,
"invalid operand combination for " ~ mnem ~ ":\n" ~
opnd0.toString() ~ "\n" ~
opnd1.toString()
);
}
}
/**
Encode an XMM instruction on 64-bit XMM/M operands
*/
void writeXMM64(
wstring mnem,