summaryrefslogtreecommitdiff
path: root/Userland/DevTools/UserspaceEmulator/SoftCPU.h
blob: 21619c0c0dda8100594498203fcfebc0c6342d33 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
/*
 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
 * All rights reserved.
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER OR CONTRIBUTORS 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.
 */

#pragma once

#include "Region.h"
#include "ValueWithShadow.h"
#include <LibX86/Instruction.h>
#include <LibX86/Interpreter.h>

namespace UserspaceEmulator {

class Emulator;
class Region;

union PartAddressableRegister {
    struct {
        u32 full_u32;
    };
    struct {
        u16 low_u16;
        u16 high_u16;
    };
    struct {
        u8 low_u8;
        u8 high_u8;
        u16 also_high_u16;
    };
};

class SoftCPU final
    : public X86::Interpreter
    , public X86::InstructionStream {
public:
    using ValueWithShadowType8 = ValueWithShadow<u8>;
    using ValueWithShadowType16 = ValueWithShadow<u16>;
    using ValueWithShadowType32 = ValueWithShadow<u32>;
    using ValueWithShadowType64 = ValueWithShadow<u64>;

    explicit SoftCPU(Emulator&);
    void dump() const;

    u32 base_eip() const { return m_base_eip; }
    void save_base_eip() { m_base_eip = m_eip; }

    u32 eip() const { return m_eip; }
    void set_eip(u32 eip)
    {
        m_eip = eip;
    }

    struct Flags {
        enum Flag {
            CF = 0x0001,
            PF = 0x0004,
            AF = 0x0010,
            ZF = 0x0040,
            SF = 0x0080,
            TF = 0x0100,
            IF = 0x0200,
            DF = 0x0400,
            OF = 0x0800,
        };
    };

    void push32(ValueWithShadow<u32>);
    ValueWithShadow<u32> pop32();

    void push16(ValueWithShadow<u16>);
    ValueWithShadow<u16> pop16();

    void push_string(const StringView&);
    void push_buffer(const u8* data, size_t);

    u16 segment(X86::SegmentRegister seg) const { return m_segment[(int)seg]; }
    u16& segment(X86::SegmentRegister seg) { return m_segment[(int)seg]; }

    ValueAndShadowReference<u8> gpr8(X86::RegisterIndex8 reg)
    {
        switch (reg) {
        case X86::RegisterAL:
            return { m_gpr[X86::RegisterEAX].low_u8, m_gpr_shadow[X86::RegisterEAX].low_u8 };
        case X86::RegisterAH:
            return { m_gpr[X86::RegisterEAX].high_u8, m_gpr_shadow[X86::RegisterEAX].high_u8 };
        case X86::RegisterBL:
            return { m_gpr[X86::RegisterEBX].low_u8, m_gpr_shadow[X86::RegisterEBX].low_u8 };
        case X86::RegisterBH:
            return { m_gpr[X86::RegisterEBX].high_u8, m_gpr_shadow[X86::RegisterEBX].high_u8 };
        case X86::RegisterCL:
            return { m_gpr[X86::RegisterECX].low_u8, m_gpr_shadow[X86::RegisterECX].low_u8 };
        case X86::RegisterCH:
            return { m_gpr[X86::RegisterECX].high_u8, m_gpr_shadow[X86::RegisterECX].high_u8 };
        case X86::RegisterDL:
            return { m_gpr[X86::RegisterEDX].low_u8, m_gpr_shadow[X86::RegisterEDX].low_u8 };
        case X86::RegisterDH:
            return { m_gpr[X86::RegisterEDX].high_u8, m_gpr_shadow[X86::RegisterEDX].high_u8 };
        }
        VERIFY_NOT_REACHED();
    }

    ValueWithShadow<u8> const_gpr8(X86::RegisterIndex8 reg) const
    {
        switch (reg) {
        case X86::RegisterAL:
            return { m_gpr[X86::RegisterEAX].low_u8, m_gpr_shadow[X86::RegisterEAX].low_u8 };
        case X86::RegisterAH:
            return { m_gpr[X86::RegisterEAX].high_u8, m_gpr_shadow[X86::RegisterEAX].high_u8 };
        case X86::RegisterBL:
            return { m_gpr[X86::RegisterEBX].low_u8, m_gpr_shadow[X86::RegisterEBX].low_u8 };
        case X86::RegisterBH:
            return { m_gpr[X86::RegisterEBX].high_u8, m_gpr_shadow[X86::RegisterEBX].high_u8 };
        case X86::RegisterCL:
            return { m_gpr[X86::RegisterECX].low_u8, m_gpr_shadow[X86::RegisterECX].low_u8 };
        case X86::RegisterCH:
            return { m_gpr[X86::RegisterECX].high_u8, m_gpr_shadow[X86::RegisterECX].high_u8 };
        case X86::RegisterDL:
            return { m_gpr[X86::RegisterEDX].low_u8, m_gpr_shadow[X86::RegisterEDX].low_u8 };
        case X86::RegisterDH:
            return { m_gpr[X86::RegisterEDX].high_u8, m_gpr_shadow[X86::RegisterEDX].high_u8 };
        }
        VERIFY_NOT_REACHED();
    }

    ValueWithShadow<u16> const_gpr16(X86::RegisterIndex16 reg) const
    {
        return { m_gpr[reg].low_u16, m_gpr_shadow[reg].low_u16 };
    }

    ValueAndShadowReference<u16> gpr16(X86::RegisterIndex16 reg)
    {
        return { m_gpr[reg].low_u16, m_gpr_shadow[reg].low_u16 };
    }

    ValueWithShadow<u32> const_gpr32(X86::RegisterIndex32 reg) const
    {
        return { m_gpr[reg].full_u32, m_gpr_shadow[reg].full_u32 };
    }

    ValueAndShadowReference<u32> gpr32(X86::RegisterIndex32 reg)
    {
        return { m_gpr[reg].full_u32, m_gpr_shadow[reg].full_u32 };
    }

    template<typename T>
    ValueWithShadow<T> const_gpr(unsigned register_index) const
    {
        if constexpr (sizeof(T) == 1)
            return const_gpr8((X86::RegisterIndex8)register_index);
        if constexpr (sizeof(T) == 2)
            return const_gpr16((X86::RegisterIndex16)register_index);
        if constexpr (sizeof(T) == 4)
            return const_gpr32((X86::RegisterIndex32)register_index);
    }

    template<typename T>
    ValueAndShadowReference<T> gpr(unsigned register_index)
    {
        if constexpr (sizeof(T) == 1)
            return gpr8((X86::RegisterIndex8)register_index);
        if constexpr (sizeof(T) == 2)
            return gpr16((X86::RegisterIndex16)register_index);
        if constexpr (sizeof(T) == 4)
            return gpr32((X86::RegisterIndex32)register_index);
    }

    ValueWithShadow<u32> source_index(bool a32) const
    {
        if (a32)
            return esi();
        return { si().value(), (u32)si().shadow() & 0xffff };
    }

    ValueWithShadow<u32> destination_index(bool a32) const
    {
        if (a32)
            return edi();
        return { di().value(), (u32)di().shadow() & 0xffff };
    }

    ValueWithShadow<u32> loop_index(bool a32) const
    {
        if (a32)
            return ecx();
        return { cx().value(), (u32)cx().shadow() & 0xffff };
    }

    bool decrement_loop_index(bool a32)
    {
        if (a32) {
            set_ecx({ ecx().value() - 1, ecx().shadow() });
            return ecx().value() == 0;
        }
        set_cx(ValueWithShadow<u16>(cx().value() - 1, cx().shadow()));
        return cx().value() == 0;
    }

    ALWAYS_INLINE void step_source_index(bool a32, u32 step)
    {
        if (a32) {
            if (df())
                set_esi({ esi().value() - step, esi().shadow() });
            else
                set_esi({ esi().value() + step, esi().shadow() });
        } else {
            if (df())
                set_si(ValueWithShadow<u16>(si().value() - step, si().shadow()));
            else
                set_si(ValueWithShadow<u16>(si().value() + step, si().shadow()));
        }
    }

    ALWAYS_INLINE void step_destination_index(bool a32, u32 step)
    {
        if (a32) {
            if (df())
                set_edi({ edi().value() - step, edi().shadow() });
            else
                set_edi({ edi().value() + step, edi().shadow() });
        } else {
            if (df())
                set_di(ValueWithShadow<u16>(di().value() - step, di().shadow()));
            else
                set_di(ValueWithShadow<u16>(di().value() + step, di().shadow()));
        }
    }

    u32 eflags() const { return m_eflags; }
    void set_eflags(ValueWithShadow<u32> eflags)
    {
        m_eflags = eflags.value();
        m_flags_tainted = eflags.is_uninitialized();
    }

    ValueWithShadow<u32> eax() const { return const_gpr32(X86::RegisterEAX); }
    ValueWithShadow<u32> ebx() const { return const_gpr32(X86::RegisterEBX); }
    ValueWithShadow<u32> ecx() const { return const_gpr32(X86::RegisterECX); }
    ValueWithShadow<u32> edx() const { return const_gpr32(X86::RegisterEDX); }
    ValueWithShadow<u32> esp() const { return const_gpr32(X86::RegisterESP); }
    ValueWithShadow<u32> ebp() const { return const_gpr32(X86::RegisterEBP); }
    ValueWithShadow<u32> esi() const { return const_gpr32(X86::RegisterESI); }
    ValueWithShadow<u32> edi() const { return const_gpr32(X86::RegisterEDI); }

    ValueWithShadow<u16> ax() const { return const_gpr16(X86::RegisterAX); }
    ValueWithShadow<u16> bx() const { return const_gpr16(X86::RegisterBX); }
    ValueWithShadow<u16> cx() const { return const_gpr16(X86::RegisterCX); }
    ValueWithShadow<u16> dx() const { return const_gpr16(X86::RegisterDX); }
    ValueWithShadow<u16> sp() const { return const_gpr16(X86::RegisterSP); }
    ValueWithShadow<u16> bp() const { return const_gpr16(X86::RegisterBP); }
    ValueWithShadow<u16> si() const { return const_gpr16(X86::RegisterSI); }
    ValueWithShadow<u16> di() const { return const_gpr16(X86::RegisterDI); }

    ValueWithShadow<u8> al() const { return const_gpr8(X86::RegisterAL); }
    ValueWithShadow<u8> ah() const { return const_gpr8(X86::RegisterAH); }
    ValueWithShadow<u8> bl() const { return const_gpr8(X86::RegisterBL); }
    ValueWithShadow<u8> bh() const { return const_gpr8(X86::RegisterBH); }
    ValueWithShadow<u8> cl() const { return const_gpr8(X86::RegisterCL); }
    ValueWithShadow<u8> ch() const { return const_gpr8(X86::RegisterCH); }
    ValueWithShadow<u8> dl() const { return const_gpr8(X86::RegisterDL); }
    ValueWithShadow<u8> dh() const { return const_gpr8(X86::RegisterDH); }

    void set_eax(ValueWithShadow<u32> value) { gpr32(X86::RegisterEAX) = value; }
    void set_ebx(ValueWithShadow<u32> value) { gpr32(X86::RegisterEBX) = value; }
    void set_ecx(ValueWithShadow<u32> value) { gpr32(X86::RegisterECX) = value; }
    void set_edx(ValueWithShadow<u32> value) { gpr32(X86::RegisterEDX) = value; }
    void set_esp(ValueWithShadow<u32> value) { gpr32(X86::RegisterESP) = value; }
    void set_ebp(ValueWithShadow<u32> value) { gpr32(X86::RegisterEBP) = value; }
    void set_esi(ValueWithShadow<u32> value) { gpr32(X86::RegisterESI) = value; }
    void set_edi(ValueWithShadow<u32> value) { gpr32(X86::RegisterEDI) = value; }

    void set_ax(ValueWithShadow<u16> value) { gpr16(X86::RegisterAX) = value; }
    void set_bx(ValueWithShadow<u16> value) { gpr16(X86::RegisterBX) = value; }
    void set_cx(ValueWithShadow<u16> value) { gpr16(X86::RegisterCX) = value; }
    void set_dx(ValueWithShadow<u16> value) { gpr16(X86::RegisterDX) = value; }
    void set_sp(ValueWithShadow<u16> value) { gpr16(X86::RegisterSP) = value; }
    void set_bp(ValueWithShadow<u16> value) { gpr16(X86::RegisterBP) = value; }
    void set_si(ValueWithShadow<u16> value) { gpr16(X86::RegisterSI) = value; }
    void set_di(ValueWithShadow<u16> value) { gpr16(X86::RegisterDI) = value; }

    void set_al(ValueWithShadow<u8> value) { gpr8(X86::RegisterAL) = value; }
    void set_ah(ValueWithShadow<u8> value) { gpr8(X86::RegisterAH) = value; }
    void set_bl(ValueWithShadow<u8> value) { gpr8(X86::RegisterBL) = value; }
    void set_bh(ValueWithShadow<u8> value) { gpr8(X86::RegisterBH) = value; }
    void set_cl(ValueWithShadow<u8> value) { gpr8(X86::RegisterCL) = value; }
    void set_ch(ValueWithShadow<u8> value) { gpr8(X86::RegisterCH) = value; }
    void set_dl(ValueWithShadow<u8> value) { gpr8(X86::RegisterDL) = value; }
    void set_dh(ValueWithShadow<u8> value) { gpr8(X86::RegisterDH) = value; }

    bool of() const { return m_eflags & Flags::OF; }
    bool sf() const { return m_eflags & Flags::SF; }
    bool zf() const { return m_eflags & Flags::ZF; }
    bool af() const { return m_eflags & Flags::AF; }
    bool pf() const { return m_eflags & Flags::PF; }
    bool cf() const { return m_eflags & Flags::CF; }
    bool df() const { return m_eflags & Flags::DF; }

    void set_flag(Flags::Flag flag, bool value)
    {
        if (value)
            m_eflags |= flag;
        else
            m_eflags &= ~flag;
    }

    void set_of(bool value) { set_flag(Flags::OF, value); }
    void set_sf(bool value) { set_flag(Flags::SF, value); }
    void set_zf(bool value) { set_flag(Flags::ZF, value); }
    void set_af(bool value) { set_flag(Flags::AF, value); }
    void set_pf(bool value) { set_flag(Flags::PF, value); }
    void set_cf(bool value) { set_flag(Flags::CF, value); }
    void set_df(bool value) { set_flag(Flags::DF, value); }

    void set_flags_with_mask(u32 new_flags, u32 mask)
    {
        m_eflags &= ~mask;
        m_eflags |= new_flags & mask;
    }

    void set_flags_oszapc(u32 new_flags)
    {
        set_flags_with_mask(new_flags, Flags::OF | Flags::SF | Flags::ZF | Flags::AF | Flags::PF | Flags::CF);
    }

    void set_flags_oszap(u32 new_flags)
    {
        set_flags_with_mask(new_flags, Flags::OF | Flags::SF | Flags::ZF | Flags::AF | Flags::PF);
    }

    void set_flags_oszpc(u32 new_flags)
    {
        set_flags_with_mask(new_flags, Flags::OF | Flags::SF | Flags::ZF | Flags::PF | Flags::CF);
    }

    void set_flags_oc(u32 new_flags)
    {
        set_flags_with_mask(new_flags, Flags::OF | Flags::CF);
    }

    u16 cs() const { return m_segment[(int)X86::SegmentRegister::CS]; }
    u16 ds() const { return m_segment[(int)X86::SegmentRegister::DS]; }
    u16 es() const { return m_segment[(int)X86::SegmentRegister::ES]; }
    u16 ss() const { return m_segment[(int)X86::SegmentRegister::SS]; }

    ValueWithShadow<u8> read_memory8(X86::LogicalAddress);
    ValueWithShadow<u16> read_memory16(X86::LogicalAddress);
    ValueWithShadow<u32> read_memory32(X86::LogicalAddress);
    ValueWithShadow<u64> read_memory64(X86::LogicalAddress);

    template<typename T>
    ValueWithShadow<T> read_memory(X86::LogicalAddress address)
    {
        if constexpr (sizeof(T) == 1)
            return read_memory8(address);
        if constexpr (sizeof(T) == 2)
            return read_memory16(address);
        if constexpr (sizeof(T) == 4)
            return read_memory32(address);
    }

    void write_memory8(X86::LogicalAddress, ValueWithShadow<u8>);
    void write_memory16(X86::LogicalAddress, ValueWithShadow<u16>);
    void write_memory32(X86::LogicalAddress, ValueWithShadow<u32>);
    void write_memory64(X86::LogicalAddress, ValueWithShadow<u64>);

    template<typename T>
    void write_memory(X86::LogicalAddress address, ValueWithShadow<T> data)
    {
        if constexpr (sizeof(T) == 1)
            return write_memory8(address, data);
        if constexpr (sizeof(T) == 2)
            return write_memory16(address, data);
        if constexpr (sizeof(T) == 4)
            return write_memory32(address, data);
    }

    bool evaluate_condition(u8 condition) const
    {
        switch (condition) {
        case 0:
            return of(); // O
        case 1:
            return !of(); // NO
        case 2:
            return cf(); // B, C, NAE
        case 3:
            return !cf(); // NB, NC, AE
        case 4:
            return zf(); // E, Z
        case 5:
            return !zf(); // NE, NZ
        case 6:
            return (cf() | zf()); // BE, NA
        case 7:
            return !(cf() | zf()); // NBE, A
        case 8:
            return sf(); // S
        case 9:
            return !sf(); // NS
        case 10:
            return pf(); // P, PE
        case 11:
            return !pf(); // NP, PO
        case 12:
            return sf() ^ of(); // L, NGE
        case 13:
            return !(sf() ^ of()); // NL, GE
        case 14:
            return (sf() ^ of()) | zf(); // LE, NG
        case 15:
            return !((sf() ^ of()) | zf()); // NLE, G
        default:
            VERIFY_NOT_REACHED();
        }
        return 0;
    }

    template<bool check_zf, typename Callback>
    void do_once_or_repeat(const X86::Instruction& insn, Callback);

    template<typename A>
    void taint_flags_from(const A& a)
    {
        m_flags_tainted = a.is_uninitialized();
    }

    template<typename A, typename B>
    void taint_flags_from(const A& a, const B& b)
    {
        m_flags_tainted = a.is_uninitialized() || b.is_uninitialized();
    }

    template<typename A, typename B, typename C>
    void taint_flags_from(const A& a, const B& b, const C& c)
    {
        m_flags_tainted = a.is_uninitialized() || b.is_uninitialized() || c.is_uninitialized();
    }

    void warn_if_flags_tainted(const char* message) const;

    // ^X86::InstructionStream
    virtual bool can_read() override { return false; }
    virtual u8 read8() override;
    virtual u16 read16() override;
    virtual u32 read32() override;
    virtual u64 read64() override;

private:
    // ^X86::Interpreter
    virtual void AAA(const X86::Instruction&) override;
    virtual void AAD(const X86::Instruction&) override;
    virtual void AAM(const X86::Instruction&) override;
    virtual void AAS(const X86::Instruction&) override;
    virtual void ADC_AL_imm8(const X86::Instruction&) override;
    virtual void ADC_AX_imm16(const X86::Instruction&) override;
    virtual void ADC_EAX_imm32(const X86::Instruction&) override;
    virtual void ADC_RM16_imm16(const X86::Instruction&) override;
    virtual void ADC_RM16_imm8(const X86::Instruction&) override;
    virtual void ADC_RM16_reg16(const X86::Instruction&) override;
    virtual void ADC_RM32_imm32(const X86::Instruction&) override;
    virtual void ADC_RM32_imm8(const X86::Instruction&) override;
    virtual void ADC_RM32_reg32(const X86::Instruction&) override;
    virtual void ADC_RM8_imm8(const X86::Instruction&) override;
    virtual void ADC_RM8_reg8(const X86::Instruction&) override;
    virtual void ADC_reg16_RM16(const X86::Instruction&) override;
    virtual void ADC_reg32_RM32(const X86::Instruction&) override;
    virtual void ADC_reg8_RM8(const X86::Instruction&) override;
    virtual void ADD_AL_imm8(const X86::Instruction&) override;
    virtual void ADD_AX_imm16(const X86::Instruction&) override;
    virtual void ADD_EAX_imm32(const X86::Instruction&) override;
    virtual void ADD_RM16_imm16(const X86::Instruction&) override;
    virtual void ADD_RM16_imm8(const X86::Instruction&) override;
    virtual void ADD_RM16_reg16(const X86::Instruction&) override;
    virtual void ADD_RM32_imm32(const X86::Instruction&) override;
    virtual void ADD_RM32_imm8(const X86::Instruction&) override;
    virtual void ADD_RM32_reg32(const X86::Instruction&) override;
    virtual void ADD_RM8_imm8(const X86::Instruction&) override;
    virtual void ADD_RM8_reg8(const X86::Instruction&) override;
    virtual void ADD_reg16_RM16(const X86::Instruction&) override;
    virtual void ADD_reg32_RM32(const X86::Instruction&) override;
    virtual void ADD_reg8_RM8(const X86::Instruction&) override;
    virtual void AND_AL_imm8(const X86::Instruction&) override;
    virtual void AND_AX_imm16(const X86::Instruction&) override;
    virtual void AND_EAX_imm32(const X86::Instruction&) override;
    virtual void AND_RM16_imm16(const X86::Instruction&) override;
    virtual void AND_RM16_imm8(const X86::Instruction&) override;
    virtual void AND_RM16_reg16(const X86::Instruction&) override;
    virtual void AND_RM32_imm32(const X86::Instruction&) override;
    virtual void AND_RM32_imm8(const X86::Instruction&) override;
    virtual void AND_RM32_reg32(const X86::Instruction&) override;
    virtual void AND_RM8_imm8(const X86::Instruction&) override;
    virtual void AND_RM8_reg8(const X86::Instruction&) override;
    virtual void AND_reg16_RM16(const X86::Instruction&) override;
    virtual void AND_reg32_RM32(const X86::Instruction&) override;
    virtual void AND_reg8_RM8(const X86::Instruction&) override;
    virtual void ARPL(const X86::Instruction&) override;
    virtual void BOUND(const X86::Instruction&) override;
    virtual void BSF_reg16_RM16(const X86::Instruction&) override;
    virtual void BSF_reg32_RM32(const X86::Instruction&) override;
    virtual void BSR_reg16_RM16(const X86::Instruction&) override;
    virtual void BSR_reg32_RM32(const X86::Instruction&) override;
    virtual void BSWAP_reg32(const X86::Instruction&) override;
    virtual void BTC_RM16_imm8(const X86::Instruction&) override;
    virtual void BTC_RM16_reg16(const X86::Instruction&) override;
    virtual void BTC_RM32_imm8(const X86::Instruction&) override;
    virtual void BTC_RM32_reg32(const X86::Instruction&) override;
    virtual void BTR_RM16_imm8(const X86::Instruction&) override;
    virtual void BTR_RM16_reg16(const X86::Instruction&) override;
    virtual void BTR_RM32_imm8(const X86::Instruction&) override;
    virtual void BTR_RM32_reg32(const X86::Instruction&) override;
    virtual void BTS_RM16_imm8(const X86::Instruction&) override;
    virtual void BTS_RM16_reg16(const X86::Instruction&) override;
    virtual void BTS_RM32_imm8(const X86::Instruction&) override;
    virtual void BTS_RM32_reg32(const X86::Instruction&) override;
    virtual void BT_RM16_imm8(const X86::Instruction&) override;
    virtual void BT_RM16_reg16(const X86::Instruction&) override;
    virtual void BT_RM32_imm8(const X86::Instruction&) override;
    virtual void BT_RM32_reg32(const X86::Instruction&) override;
    virtual void CALL_FAR_mem16(const X86::Instruction&) override;
    virtual void CALL_FAR_mem32(const X86::Instruction&) override;
    virtual void CALL_RM16(const X86::Instruction&) override;
    virtual void CALL_RM32(const X86::Instruction&) override;
    virtual void CALL_imm16(const X86::Instruction&) override;
    virtual void CALL_imm16_imm16(const X86::Instruction&) override;
    virtual void CALL_imm16_imm32(const X86::Instruction&) override;
    virtual void CALL_imm32(const X86::Instruction&) override;
    virtual void CBW(const X86::Instruction&) override;
    virtual void CDQ(const X86::Instruction&) override;
    virtual void CLC(const X86::Instruction&) override;
    virtual void CLD(const X86::Instruction&) override;
    virtual void CLI(const X86::Instruction&) override;
    virtual void CLTS(const X86::Instruction&) override;
    virtual void CMC(const X86::Instruction&) override;
    virtual void CMOVcc_reg16_RM16(const X86::Instruction&) override;
    virtual void CMOVcc_reg32_RM32(const X86::Instruction&) override;
    virtual void CMPSB(const X86::Instruction&) override;
    virtual void CMPSD(const X86::Instruction&) override;
    virtual void CMPSW(const X86::Instruction&) override;
    virtual void CMPXCHG_RM16_reg16(const X86::Instruction&) override;
    virtual void CMPXCHG_RM32_reg32(const X86::Instruction&) override;
    virtual void CMPXCHG_RM8_reg8(const X86::Instruction&) override;
    virtual void CMP_AL_imm8(const X86::Instruction&) override;
    virtual void CMP_AX_imm16(const X86::Instruction&) override;
    virtual void CMP_EAX_imm32(const X86::Instruction&) override;
    virtual void CMP_RM16_imm16(const X86::Instruction&) override;
    virtual void CMP_RM16_imm8(const X86::Instruction&) override;
    virtual void CMP_RM16_reg16(const X86::Instruction&) override;
    virtual void CMP_RM32_imm32(const X86::Instruction&) override;
    virtual void CMP_RM32_imm8(const X86::Instruction&) override;
    virtual void CMP_RM32_reg32(const X86::Instruction&) override;
    virtual void CMP_RM8_imm8(const X86::Instruction&) override;
    virtual void CMP_RM8_reg8(const X86::Instruction&) override;
    virtual void CMP_reg16_RM16(const X86::Instruction&) override;
    virtual void CMP_reg32_RM32(const X86::Instruction&) override;
    virtual void CMP_reg8_RM8(const X86::Instruction&) override;
    virtual void CPUID(const X86::Instruction&) override;
    virtual void CWD(const X86::Instruction&) override;
    virtual void CWDE(const X86::Instruction&) override;
    virtual void DAA(const X86::Instruction&) override;
    virtual void DAS(const X86::Instruction&) override;
    virtual void DEC_RM16(const X86::Instruction&) override;
    virtual void DEC_RM32(const X86::Instruction&) override;
    virtual void DEC_RM8(const X86::Instruction&) override;
    virtual void DEC_reg16(const X86::Instruction&) override;
    virtual void DEC_reg32(const X86::Instruction&) override;
    virtual void DIV_RM16(const X86::Instruction&) override;
    virtual void DIV_RM32(const X86::Instruction&) override;
    virtual void DIV_RM8(const X86::Instruction&) override;
    virtual void ENTER16(const X86::Instruction&) override;
    virtual void ENTER32(const X86::Instruction&) override;
    virtual void ESCAPE(const X86::Instruction&) override;
    virtual void FADD_RM32(const X86::Instruction&) override;
    virtual void FMUL_RM32(const X86::Instruction&) override;
    virtual void FCOM_RM32(const X86::Instruction&) override;
    virtual void FCOMP_RM32(const X86::Instruction&) override;
    virtual void FSUB_RM32(const X86::Instruction&) override;
    virtual void FSUBR_RM32(const X86::Instruction&) override;
    virtual void FDIV_RM32(const X86::Instruction&) override;
    virtual void FDIVR_RM32(const X86::Instruction&) override;
    virtual void FLD_RM32(const X86::Instruction&) override;
    virtual void FXCH(const X86::Instruction&) override;
    virtual void FST_RM32(const X86::Instruction&) override;
    virtual void FNOP(const X86::Instruction&) override;
    virtual void FSTP_RM32(const X86::Instruction&) override;
    virtual void FLDENV(const X86::Instruction&) override;
    virtual void FCHS(const X86::Instruction&) override;
    virtual void FABS(const X86::Instruction&) override;
    virtual void FTST(const X86::Instruction&) override;
    virtual void FXAM(const X86::Instruction&) override;
    virtual void FLDCW(const X86::Instruction&) override;
    virtual void FLD1(const X86::Instruction&) override;
    virtual void FLDL2T(const X86::Instruction&) override;
    virtual void FLDL2E(const X86::Instruction&) override;
    virtual void FLDPI(const X86::Instruction&) override;
    virtual void FLDLG2(const X86::Instruction&) override;
    virtual void FLDLN2(const X86::Instruction&) override;
    virtual void FLDZ(const X86::Instruction&) override;
    virtual void FNSTENV(const X86::Instruction&) override;
    virtual void F2XM1(const X86::Instruction&) override;
    virtual void FYL2X(const X86::Instruction&) override;
    virtual void FPTAN(const X86::Instruction&) override;
    virtual void FPATAN(const X86::Instruction&) override;
    virtual void FXTRACT(const X86::Instruction&) override;
    virtual void FPREM1(const X86::Instruction&) override;
    virtual void FDECSTP(const X86::Instruction&) override;
    virtual void FINCSTP(const X86::Instruction&) override;
    virtual void FNSTCW(const X86::Instruction&) override;
    virtual void FPREM(const X86::Instruction&) override;
    virtual void FYL2XP1(const X86::Instruction&) override;
    virtual void FSQRT(const X86::Instruction&) override;
    virtual void FSINCOS(const X86::Instruction&) override;
    virtual void FRNDINT(const X86::Instruction&) override;
    virtual void FSCALE(const X86::Instruction&) override;
    virtual void FSIN(const X86::Instruction&) override;
    virtual void FCOS(const X86::Instruction&) override;
    virtual void FIADD_RM32(const X86::Instruction&) override;
    virtual void FCMOVB(const X86::Instruction&) override;
    virtual void FIMUL_RM32(const X86::Instruction&) override;
    virtual void FCMOVE(const X86::Instruction&) override;
    virtual void FICOM_RM32(const X86::Instruction&) override;
    virtual void FCMOVBE(const X86::Instruction&) override;
    virtual void FICOMP_RM32(const X86::Instruction&) override;
    virtual void FCMOVU(const X86::Instruction&) override;
    virtual void FISUB_RM32(const X86::Instruction&) override;
    virtual void FISUBR_RM32(const X86::Instruction&) override;
    virtual void FUCOMPP(const X86::Instruction&) override;
    virtual void FIDIV_RM32(const X86::Instruction&) override;
    virtual void FIDIVR_RM32(const X86::Instruction&) override;
    virtual void FILD_RM32(const X86::Instruction&) override;
    virtual void FCMOVNB(const X86::Instruction&) override;
    virtual void FISTTP_RM32(const X86::Instruction&) override;
    virtual void FCMOVNE(const X86::Instruction&) override;
    virtual void FIST_RM32(const X86::Instruction&) override;
    virtual void FCMOVNBE(const X86::Instruction&) override;
    virtual void FISTP_RM32(const X86::Instruction&) override;
    virtual void FCMOVNU(const X86::Instruction&) override;
    virtual void FNENI(const X86::Instruction&) override;
    virtual void FNDISI(const X86::Instruction&) override;
    virtual void FNCLEX(const X86::Instruction&) override;
    virtual void FNINIT(const X86::Instruction&) override;
    virtual void FNSETPM(const X86::Instruction&) override;
    virtual void FLD_RM80(const X86::Instruction&) override;
    virtual void FUCOMI(const X86::Instruction&) override;
    virtual void FCOMI(const X86::Instruction&) override;
    virtual void FSTP_RM80(const X86::Instruction&) override;
    virtual void FADD_RM64(const X86::Instruction&) override;
    virtual void FMUL_RM64(const X86::Instruction&) override;
    virtual void FCOM_RM64(const X86::Instruction&) override;
    virtual void FCOMP_RM64(const X86::Instruction&) override;
    virtual void FSUB_RM64(const X86::Instruction&) override;
    virtual void FSUBR_RM64(const X86::Instruction&) override;
    virtual void FDIV_RM64(const X86::Instruction&) override;
    virtual void FDIVR_RM64(const X86::Instruction&) override;
    virtual void FLD_RM64(const X86::Instruction&) override;
    virtual void FFREE(const X86::Instruction&) override;
    virtual void FISTTP_RM64(const X86::Instruction&) override;
    virtual void FST_RM64(const X86::Instruction&) override;
    virtual void FSTP_RM64(const X86::Instruction&) override;
    virtual void FRSTOR(const X86::Instruction&) override;
    virtual void FUCOM(const X86::Instruction&) override;
    virtual void FUCOMP(const X86::Instruction&) override;
    virtual void FNSAVE(const X86::Instruction&) override;
    virtual void FNSTSW(const X86::Instruction&) override;
    virtual void FIADD_RM16(const X86::Instruction&) override;
    virtual void FADDP(const X86::Instruction&) override;
    virtual void FIMUL_RM16(const X86::Instruction&) override;
    virtual void FMULP(const X86::Instruction&) override;
    virtual void FICOM_RM16(const X86::Instruction&) override;
    virtual void FICOMP_RM16(const X86::Instruction&) override;
    virtual void FCOMPP(const X86::Instruction&) override;
    virtual void FISUB_RM16(const X86::Instruction&) override;
    virtual void FSUBRP(const X86::Instruction&) override;
    virtual void FISUBR_RM16(const X86::Instruction&) override;
    virtual void FSUBP(const X86::Instruction&) override;
    virtual void FIDIV_RM16(const X86::Instruction&) override;
    virtual void FDIVRP(const X86::Instruction&) override;
    virtual void FIDIVR_RM16(const X86::Instruction&) override;
    virtual void FDIVP(const X86::Instruction&) override;
    virtual void FILD_RM16(const X86::Instruction&) override;
    virtual void FFREEP(const X86::Instruction&) override;
    virtual void FISTTP_RM16(const X86::Instruction&) override;
    virtual void FIST_RM16(const X86::Instruction&) override;
    virtual void FISTP_RM16(const X86::Instruction&) override;
    virtual void FBLD_M80(const X86::Instruction&) override;
    virtual void FNSTSW_AX(const X86::Instruction&) override;
    virtual void FILD_RM64(const X86::Instruction&) override;
    virtual void FUCOMIP(const X86::Instruction&) override;
    virtual void FBSTP_M80(const X86::Instruction&) override;
    virtual void FCOMIP(const X86::Instruction&) override;
    virtual void FISTP_RM64(const X86::Instruction&) override;
    virtual void HLT(const X86::Instruction&) override;
    virtual void IDIV_RM16(const X86::Instruction&) override;
    virtual void IDIV_RM32(const X86::Instruction&) override;
    virtual void IDIV_RM8(const X86::Instruction&) override;
    virtual void IMUL_RM16(const X86::Instruction&) override;
    virtual void IMUL_RM32(const X86::Instruction&) override;
    virtual void IMUL_RM8(const X86::Instruction&) override;
    virtual void IMUL_reg16_RM16(const X86::Instruction&) override;
    virtual void IMUL_reg16_RM16_imm16(const X86::Instruction&) override;
    virtual void IMUL_reg16_RM16_imm8(const X86::Instruction&) override;
    virtual void IMUL_reg32_RM32(const X86::Instruction&) override;
    virtual void IMUL_reg32_RM32_imm32(const X86::Instruction&) override;
    virtual void IMUL_reg32_RM32_imm8(const X86::Instruction&) override;
    virtual void INC_RM16(const X86::Instruction&) override;
    virtual void INC_RM32(const X86::Instruction&) override;
    virtual void INC_RM8(const X86::Instruction&) override;
    virtual void INC_reg16(const X86::Instruction&) override;
    virtual void INC_reg32(const X86::Instruction&) override;
    virtual void INSB(const X86::Instruction&) override;
    virtual void INSD(const X86::Instruction&) override;
    virtual void INSW(const X86::Instruction&) override;
    virtual void INT3(const X86::Instruction&) override;
    virtual void INTO(const X86::Instruction&) override;
    virtual void INT_imm8(const X86::Instruction&) override;
    virtual void INVLPG(const X86::Instruction&) override;
    virtual void IN_AL_DX(const X86::Instruction&) override;
    virtual void IN_AL_imm8(const X86::Instruction&) override;
    virtual void IN_AX_DX(const X86::Instruction&) override;
    virtual void IN_AX_imm8(const X86::Instruction&) override;
    virtual void IN_EAX_DX(const X86::Instruction&) override;
    virtual void IN_EAX_imm8(const X86::Instruction&) override;
    virtual void IRET(const X86::Instruction&) override;
    virtual void JCXZ_imm8(const X86::Instruction&) override;
    virtual void JMP_FAR_mem16(const X86::Instruction&) override;
    virtual void JMP_FAR_mem32(const X86::Instruction&) override;
    virtual void JMP_RM16(const X86::Instruction&) override;
    virtual void JMP_RM32(const X86::Instruction&) override;
    virtual void JMP_imm16(const X86::Instruction&) override;
    virtual void JMP_imm16_imm16(const X86::Instruction&) override;
    virtual void JMP_imm16_imm32(const X86::Instruction&) override;
    virtual void JMP_imm32(const X86::Instruction&) override;
    virtual void JMP_short_imm8(const X86::Instruction&) override;
    virtual void Jcc_NEAR_imm(const X86::Instruction&) override;
    virtual void Jcc_imm8(const X86::Instruction&) override;
    virtual void LAHF(const X86::Instruction&) override;
    virtual void LAR_reg16_RM16(const X86::Instruction&) override;
    virtual void LAR_reg32_RM32(const X86::Instruction&) override;
    virtual void LDS_reg16_mem16(const X86::Instruction&) override;
    virtual void LDS_reg32_mem32(const X86::Instruction&) override;
    virtual void LEAVE16(const X86::Instruction&) override;
    virtual void LEAVE32(const X86::Instruction&) override;
    virtual void LEA_reg16_mem16(const X86::Instruction&) override;
    virtual void LEA_reg32_mem32(const X86::Instruction&) override;
    virtual void LES_reg16_mem16(const X86::Instruction&) override;
    virtual void LES_reg32_mem32(const X86::Instruction&) override;
    virtual void LFS_reg16_mem16(const X86::Instruction&) override;
    virtual void LFS_reg32_mem32(const X86::Instruction&) override;
    virtual void LGDT(const X86::Instruction&) override;
    virtual void LGS_reg16_mem16(const X86::Instruction&) override;
    virtual void LGS_reg32_mem32(const X86::Instruction&) override;
    virtual void LIDT(const X86::Instruction&) override;
    virtual void LLDT_RM16(const X86::Instruction&) override;
    virtual void LMSW_RM16(const X86::Instruction&) override;
    virtual void LODSB(const X86::Instruction&) override;
    virtual void LODSD(const X86::Instruction&) override;
    virtual void LODSW(const X86::Instruction&) override;
    virtual void LOOPNZ_imm8(const X86::Instruction&) override;
    virtual void LOOPZ_imm8(const X86::Instruction&) override;
    virtual void LOOP_imm8(const X86::Instruction&) override;
    virtual void LSL_reg16_RM16(const X86::Instruction&) override;
    virtual void LSL_reg32_RM32(const X86::Instruction&) override;
    virtual void LSS_reg16_mem16(const X86::Instruction&) override;
    virtual void LSS_reg32_mem32(const X86::Instruction&) override;
    virtual void LTR_RM16(const X86::Instruction&) override;
    virtual void MOVSB(const X86::Instruction&) override;
    virtual void MOVSD(const X86::Instruction&) override;
    virtual void MOVSW(const X86::Instruction&) override;
    virtual void MOVSX_reg16_RM8(const X86::Instruction&) override;
    virtual void MOVSX_reg32_RM16(const X86::Instruction&) override;
    virtual void MOVSX_reg32_RM8(const X86::Instruction&) override;
    virtual void MOVZX_reg16_RM8(const X86::Instruction&) override;
    virtual void MOVZX_reg32_RM16(const X86::Instruction&) override;
    virtual void MOVZX_reg32_RM8(const X86::Instruction&) override;
    virtual void MOV_AL_moff8(const X86::Instruction&) override;
    virtual void MOV_AX_moff16(const X86::Instruction&) override;
    virtual void MOV_CR_reg32(const X86::Instruction&) override;
    virtual void MOV_DR_reg32(const X86::Instruction&) override;
    virtual void MOV_EAX_moff32(const X86::Instruction&) override;
    virtual void MOV_RM16_imm16(const X86::Instruction&) override;
    virtual void MOV_RM16_reg16(const X86::Instruction&) override;
    virtual void MOV_RM16_seg(const X86::Instruction&) override;
    virtual void MOV_RM32_imm32(const X86::Instruction&) override;
    virtual void MOV_RM32_reg32(const X86::Instruction&) override;
    virtual void MOV_RM8_imm8(const X86::Instruction&) override;
    virtual void MOV_RM8_reg8(const X86::Instruction&) override;
    virtual void MOV_moff16_AX(const X86::Instruction&) override;
    virtual void MOV_moff32_EAX(const X86::Instruction&) override;
    virtual void MOV_moff8_AL(const X86::Instruction&) override;
    virtual void MOV_reg16_RM16(const X86::Instruction&) override;
    virtual void MOV_reg16_imm16(const X86::Instruction&) override;
    virtual void MOV_reg32_CR(const X86::Instruction&) override;
    virtual void MOV_reg32_DR(const X86::Instruction&) override;
    virtual void MOV_reg32_RM32(const X86::Instruction&) override;
    virtual void MOV_reg32_imm32(const X86::Instruction&) override;
    virtual void MOV_reg8_RM8(const X86::Instruction&) override;
    virtual void MOV_reg8_imm8(const X86::Instruction&) override;
    virtual void MOV_seg_RM16(const X86::Instruction&) override;
    virtual void MOV_seg_RM32(const X86::Instruction&) override;
    virtual void MUL_RM16(const X86::Instruction&) override;
    virtual void MUL_RM32(const X86::Instruction&) override;
    virtual void MUL_RM8(const X86::Instruction&) override;
    virtual void NEG_RM16(const X86::Instruction&) override;
    virtual void NEG_RM32(const X86::Instruction&) override;
    virtual void NEG_RM8(const X86::Instruction&) override;
    virtual void NOP(const X86::Instruction&) override;
    virtual void NOT_RM16(const X86::Instruction&) override;
    virtual void NOT_RM32(const X86::Instruction&) override;
    virtual void NOT_RM8(const X86::Instruction&) override;
    virtual void OR_AL_imm8(const X86::Instruction&) override;
    virtual void OR_AX_imm16(const X86::Instruction&) override;
    virtual void OR_EAX_imm32(const X86::Instruction&) override;
    virtual void OR_RM16_imm16(const X86::Instruction&) override;
    virtual void OR_RM16_imm8(const X86::Instruction&) override;
    virtual void OR_RM16_reg16(const X86::Instruction&) override;
    virtual void OR_RM32_imm32(const X86::Instruction&) override;
    virtual void OR_RM32_imm8(const X86::Instruction&) override;
    virtual void OR_RM32_reg32(const X86::Instruction&) override;
    virtual void OR_RM8_imm8(const X86::Instruction&) override;
    virtual void OR_RM8_reg8(const X86::Instruction&) override;
    virtual void OR_reg16_RM16(const X86::Instruction&) override;
    virtual void OR_reg32_RM32(const X86::Instruction&) override;
    virtual void OR_reg8_RM8(const X86::Instruction&) override;
    virtual void OUTSB(const X86::Instruction&) override;
    virtual void OUTSD(const X86::Instruction&) override;
    virtual void OUTSW(const X86::Instruction&) override;
    virtual void OUT_DX_AL(const X86::Instruction&) override;
    virtual void OUT_DX_AX(const X86::Instruction&) override;
    virtual void OUT_DX_EAX(const X86::Instruction&) override;
    virtual void OUT_imm8_AL(const X86::Instruction&) override;
    virtual void OUT_imm8_AX(const X86::Instruction&) override;
    virtual void OUT_imm8_EAX(const X86::Instruction&) override;
    virtual void PADDB_mm1_mm2m64(const X86::Instruction&) override;
    virtual void PADDW_mm1_mm2m64(const X86::Instruction&) override;
    virtual void PADDD_mm1_mm2m64(const X86::Instruction&) override;
    virtual void POPA(const X86::Instruction&) override;
    virtual void POPAD(const X86::Instruction&) override;
    virtual void POPF(const X86::Instruction&) override;
    virtual void POPFD(const X86::Instruction&) override;
    virtual void POP_DS(const X86::Instruction&) override;
    virtual void POP_ES(const X86::Instruction&) override;
    virtual void POP_FS(const X86::Instruction&) override;
    virtual void POP_GS(const X86::Instruction&) override;
    virtual void POP_RM16(const X86::Instruction&) override;
    virtual void POP_RM32(const X86::Instruction&) override;
    virtual void POP_SS(const X86::Instruction&) override;
    virtual void POP_reg16(const X86::Instruction&) override;
    virtual void POP_reg32(const X86::Instruction&) override;
    virtual void PUSHA(const X86::Instruction&) override;
    virtual void PUSHAD(const X86::Instruction&) override;
    virtual void PUSHF(const X86::Instruction&) override;
    virtual void PUSHFD(const X86::Instruction&) override;
    virtual void PUSH_CS(const X86::Instruction&) override;
    virtual void PUSH_DS(const X86::Instruction&) override;
    virtual void PUSH_ES(const X86::Instruction&) override;
    virtual void PUSH_FS(const X86::Instruction&) override;
    virtual void PUSH_GS(const X86::Instruction&) override;
    virtual void PUSH_RM16(const X86::Instruction&) override;
    virtual void PUSH_RM32(const X86::Instruction&) override;
    virtual void PUSH_SP_8086_80186(const X86::Instruction&) override;
    virtual void PUSH_SS(const X86::Instruction&) override;
    virtual void PUSH_imm16(const X86::Instruction&) override;
    virtual void PUSH_imm32(const X86::Instruction&) override;
    virtual void PUSH_imm8(const X86::Instruction&) override;
    virtual void PUSH_reg16(const X86::Instruction&) override;
    virtual void PUSH_reg32(const X86::Instruction&) override;
    virtual void RCL_RM16_1(const X86::Instruction&) override;
    virtual void RCL_RM16_CL(const X86::Instruction&) override;
    virtual void RCL_RM16_imm8(const X86::Instruction&) override;
    virtual void RCL_RM32_1(const X86::Instruction&) override;
    virtual void RCL_RM32_CL(const X86::Instruction&) override;
    virtual void RCL_RM32_imm8(const X86::Instruction&) override;
    virtual void RCL_RM8_1(const X86::Instruction&) override;
    virtual void RCL_RM8_CL(const X86::Instruction&) override;
    virtual void RCL_RM8_imm8(const X86::Instruction&) override;
    virtual void RCR_RM16_1(const X86::Instruction&) override;
    virtual void RCR_RM16_CL(const X86::Instruction&) override;
    virtual void RCR_RM16_imm8(const X86::Instruction&) override;
    virtual void RCR_RM32_1(const X86::Instruction&) override;
    virtual void RCR_RM32_CL(const X86::Instruction&) override;
    virtual void RCR_RM32_imm8(const X86::Instruction&) override;
    virtual void RCR_RM8_1(const X86::Instruction&) override;
    virtual void RCR_RM8_CL(const X86::Instruction&) override;
    virtual void RCR_RM8_imm8(const X86::Instruction&) override;
    virtual void RDTSC(const X86::Instruction&) override;
    virtual void RET(const X86::Instruction&) override;
    virtual void RETF(const X86::Instruction&) override;
    virtual void RETF_imm16(const X86::Instruction&) override;
    virtual void RET_imm16(const X86::Instruction&) override;
    virtual void ROL_RM16_1(const X86::Instruction&) override;
    virtual void ROL_RM16_CL(const X86::Instruction&) override;
    virtual void ROL_RM16_imm8(const X86::Instruction&) override;
    virtual void ROL_RM32_1(const X86::Instruction&) override;
    virtual void ROL_RM32_CL(const X86::Instruction&) override;
    virtual void ROL_RM32_imm8(const X86::Instruction&) override;
    virtual void ROL_RM8_1(const X86::Instruction&) override;
    virtual void ROL_RM8_CL(const X86::Instruction&) override;
    virtual void ROL_RM8_imm8(const X86::Instruction&) override;
    virtual void ROR_RM16_1(const X86::Instruction&) override;
    virtual void ROR_RM16_CL(const X86::Instruction&) override;
    virtual void ROR_RM16_imm8(const X86::Instruction&) override;
    virtual void ROR_RM32_1(const X86::Instruction&) override;
    virtual void ROR_RM32_CL(const X86::Instruction&) override;
    virtual void ROR_RM32_imm8(const X86::Instruction&) override;
    virtual void ROR_RM8_1(const X86::Instruction&) override;
    virtual void ROR_RM8_CL(const X86::Instruction&) override;
    virtual void ROR_RM8_imm8(const X86::Instruction&) override;
    virtual void SAHF(const X86::Instruction&) override;
    virtual void SALC(const X86::Instruction&) override;
    virtual void SAR_RM16_1(const X86::Instruction&) override;
    virtual void SAR_RM16_CL(const X86::Instruction&) override;
    virtual void SAR_RM16_imm8(const X86::Instruction&) override;
    virtual void SAR_RM32_1(const X86::Instruction&) override;
    virtual void SAR_RM32_CL(const X86::Instruction&) override;
    virtual void SAR_RM32_imm8(const X86::Instruction&) override;
    virtual void SAR_RM8_1(const X86::Instruction&) override;
    virtual void SAR_RM8_CL(const X86::Instruction&) override;
    virtual void SAR_RM8_imm8(const X86::Instruction&) override;
    virtual void SBB_AL_imm8(const X86::Instruction&) override;
    virtual void SBB_AX_imm16(const X86::Instruction&) override;
    virtual void SBB_EAX_imm32(const X86::Instruction&) override;
    virtual void SBB_RM16_imm16(const X86::Instruction&) override;
    virtual void SBB_RM16_imm8(const X86::Instruction&) override;
    virtual void SBB_RM16_reg16(const X86::Instruction&) override;
    virtual void SBB_RM32_imm32(const X86::Instruction&) override;
    virtual void SBB_RM32_imm8(const X86::Instruction&) override;
    virtual void SBB_RM32_reg32(const X86::Instruction&) override;
    virtual void SBB_RM8_imm8(const X86::Instruction&) override;
    virtual void SBB_RM8_reg8(const X86::Instruction&) override;
    virtual void SBB_reg16_RM16(const X86::Instruction&) override;
    virtual void SBB_reg32_RM32(const X86::Instruction&) override;
    virtual void SBB_reg8_RM8(const X86::Instruction&) override;
    virtual void SCASB(const X86::Instruction&) override;
    virtual void SCASD(const X86::Instruction&) override;
    virtual void SCASW(const X86::Instruction&) override;
    virtual void SETcc_RM8(const X86::Instruction&) override;
    virtual void SGDT(const X86::Instruction&) override;
    virtual void SHLD_RM16_reg16_CL(const X86::Instruction&) override;
    virtual void SHLD_RM16_reg16_imm8(const X86::Instruction&) override;
    virtual void SHLD_RM32_reg32_CL(const X86::Instruction&) override;
    virtual void SHLD_RM32_reg32_imm8(const X86::Instruction&) override;
    virtual void SHL_RM16_1(const X86::Instruction&) override;
    virtual void SHL_RM16_CL(const X86::Instruction&) override;
    virtual void SHL_RM16_imm8(const X86::Instruction&) override;
    virtual void SHL_RM32_1(const X86::Instruction&) override;
    virtual void SHL_RM32_CL(const X86::Instruction&) override;
    virtual void SHL_RM32_imm8(const X86::Instruction&) override;
    virtual void SHL_RM8_1(const X86::Instruction&) override;
    virtual void SHL_RM8_CL(const X86::Instruction&) override;
    virtual void SHL_RM8_imm8(const X86::Instruction&) override;
    virtual void SHRD_RM16_reg16_CL(const X86::Instruction&) override;
    virtual void SHRD_RM16_reg16_imm8(const X86::Instruction&) override;
    virtual void SHRD_RM32_reg32_CL(const X86::Instruction&) override;
    virtual void SHRD_RM32_reg32_imm8(const X86::Instruction&) override;
    virtual void SHR_RM16_1(const X86::Instruction&) override;
    virtual void SHR_RM16_CL(const X86::Instruction&) override;
    virtual void SHR_RM16_imm8(const X86::Instruction&) override;
    virtual void SHR_RM32_1(const X86::Instruction&) override;
    virtual void SHR_RM32_CL(const X86::Instruction&) override;
    virtual void SHR_RM32_imm8(const X86::Instruction&) override;
    virtual void SHR_RM8_1(const X86::Instruction&) override;
    virtual void SHR_RM8_CL(const X86::Instruction&) override;
    virtual void SHR_RM8_imm8(const X86::Instruction&) override;
    virtual void SIDT(const X86::Instruction&) override;
    virtual void SLDT_RM16(const X86::Instruction&) override;
    virtual void SMSW_RM16(const X86::Instruction&) override;
    virtual void STC(const X86::Instruction&) override;
    virtual void STD(const X86::Instruction&) override;
    virtual void STI(const X86::Instruction&) override;
    virtual void STOSB(const X86::Instruction&) override;
    virtual void STOSD(const X86::Instruction&) override;
    virtual void STOSW(const X86::Instruction&) override;
    virtual void STR_RM16(const X86::Instruction&) override;
    virtual void SUB_AL_imm8(const X86::Instruction&) override;
    virtual void SUB_AX_imm16(const X86::Instruction&) override;
    virtual void SUB_EAX_imm32(const X86::Instruction&) override;
    virtual void SUB_RM16_imm16(const X86::Instruction&) override;
    virtual void SUB_RM16_imm8(const X86::Instruction&) override;
    virtual void SUB_RM16_reg16(const X86::Instruction&) override;
    virtual void SUB_RM32_imm32(const X86::Instruction&) override;
    virtual void SUB_RM32_imm8(const X86::Instruction&) override;
    virtual void SUB_RM32_reg32(const X86::Instruction&) override;
    virtual void SUB_RM8_imm8(const X86::Instruction&) override;
    virtual void SUB_RM8_reg8(const X86::Instruction&) override;
    virtual void SUB_reg16_RM16(const X86::Instruction&) override;
    virtual void SUB_reg32_RM32(const X86::Instruction&) override;
    virtual void SUB_reg8_RM8(const X86::Instruction&) override;
    virtual void TEST_AL_imm8(const X86::Instruction&) override;
    virtual void TEST_AX_imm16(const X86::Instruction&) override;
    virtual void TEST_EAX_imm32(const X86::Instruction&) override;
    virtual void TEST_RM16_imm16(const X86::Instruction&) override;
    virtual void TEST_RM16_reg16(const X86::Instruction&) override;
    virtual void TEST_RM32_imm32(const X86::Instruction&) override;
    virtual void TEST_RM32_reg32(const X86::Instruction&) override;
    virtual void TEST_RM8_imm8(const X86::Instruction&) override;
    virtual void TEST_RM8_reg8(const X86::Instruction&) override;
    virtual void UD0(const X86::Instruction&) override;
    virtual void UD1(const X86::Instruction&) override;
    virtual void UD2(const X86::Instruction&) override;
    virtual void VERR_RM16(const X86::Instruction&) override;
    virtual void VERW_RM16(const X86::Instruction&) override;
    virtual void WAIT(const X86::Instruction&) override;
    virtual void WBINVD(const X86::Instruction&) override;
    virtual void XADD_RM16_reg16(const X86::Instruction&) override;
    virtual void XADD_RM32_reg32(const X86::Instruction&) override;
    virtual void XADD_RM8_reg8(const X86::Instruction&) override;
    virtual void XCHG_AX_reg16(const X86::Instruction&) override;
    virtual void XCHG_EAX_reg32(const X86::Instruction&) override;
    virtual void XCHG_reg16_RM16(const X86::Instruction&) override;
    virtual void XCHG_reg32_RM32(const X86::Instruction&) override;
    virtual void XCHG_reg8_RM8(const X86::Instruction&) override;
    virtual void XLAT(const X86::Instruction&) override;
    virtual void XOR_AL_imm8(const X86::Instruction&) override;
    virtual void XOR_AX_imm16(const X86::Instruction&) override;
    virtual void XOR_EAX_imm32(const X86::Instruction&) override;
    virtual void XOR_RM16_imm16(const X86::Instruction&) override;
    virtual void XOR_RM16_imm8(const X86::Instruction&) override;
    virtual void XOR_RM16_reg16(const X86::Instruction&) override;
    virtual void XOR_RM32_imm32(const X86::Instruction&) override;
    virtual void XOR_RM32_imm8(const X86::Instruction&) override;
    virtual void XOR_RM32_reg32(const X86::Instruction&) override;
    virtual void XOR_RM8_imm8(const X86::Instruction&) override;
    virtual void XOR_RM8_reg8(const X86::Instruction&) override;
    virtual void XOR_reg16_RM16(const X86::Instruction&) override;
    virtual void XOR_reg32_RM32(const X86::Instruction&) override;
    virtual void XOR_reg8_RM8(const X86::Instruction&) override;
    virtual void MOVQ_mm1_mm2m64(const X86::Instruction&) override;
    virtual void EMMS(const X86::Instruction&) override;
    virtual void MOVQ_mm1_m64_mm2(const X86::Instruction&) override;
    virtual void wrap_0xC0(const X86::Instruction&) override;
    virtual void wrap_0xC1_16(const X86::Instruction&) override;
    virtual void wrap_0xC1_32(const X86::Instruction&) override;
    virtual void wrap_0xD0(const X86::Instruction&) override;
    virtual void wrap_0xD1_16(const X86::Instruction&) override;
    virtual void wrap_0xD1_32(const X86::Instruction&) override;
    virtual void wrap_0xD2(const X86::Instruction&) override;
    virtual void wrap_0xD3_16(const X86::Instruction&) override;
    virtual void wrap_0xD3_32(const X86::Instruction&) override;

    template<bool update_dest, bool is_or, typename Op>
    void generic_AL_imm8(Op, const X86::Instruction&);
    template<bool update_dest, bool is_or, typename Op>
    void generic_AX_imm16(Op, const X86::Instruction&);
    template<bool update_dest, bool is_or, typename Op>
    void generic_EAX_imm32(Op, const X86::Instruction&);
    template<bool update_dest, bool is_or, typename Op>
    void generic_RM16_imm16(Op, const X86::Instruction&);
    template<bool update_dest, bool is_or, typename Op>
    void generic_RM16_imm8(Op, const X86::Instruction&);
    template<bool update_dest, typename Op>
    void generic_RM16_unsigned_imm8(Op, const X86::Instruction&);
    template<bool update_dest, bool is_zero_idiom_if_both_operands_same, typename Op>
    void generic_RM16_reg16(Op, const X86::Instruction&);
    template<bool update_dest, bool is_or, typename Op>
    void generic_RM32_imm32(Op, const X86::Instruction&);
    template<bool update_dest, bool is_or, typename Op>
    void generic_RM32_imm8(Op, const X86::Instruction&);
    template<bool update_dest, typename Op>
    void generic_RM32_unsigned_imm8(Op, const X86::Instruction&);
    template<bool update_dest, bool is_zero_idiom_if_both_operands_same, typename Op>
    void generic_RM32_reg32(Op, const X86::Instruction&);
    template<bool update_dest, bool is_or, typename Op>
    void generic_RM8_imm8(Op, const X86::Instruction&);
    template<bool update_dest, bool is_zero_idiom_if_both_operands_same, typename Op>
    void generic_RM8_reg8(Op, const X86::Instruction&);
    template<bool update_dest, bool is_zero_idiom_if_both_operands_same, typename Op>
    void generic_reg16_RM16(Op, const X86::Instruction&);
    template<bool update_dest, bool is_zero_idiom_if_both_operands_same, typename Op>
    void generic_reg32_RM32(Op, const X86::Instruction&);
    template<bool update_dest, bool is_zero_idiom_if_both_operands_same, typename Op>
    void generic_reg8_RM8(Op, const X86::Instruction&);

    template<typename Op>
    void generic_RM8_1(Op, const X86::Instruction&);
    template<typename Op>
    void generic_RM8_CL(Op, const X86::Instruction&);
    template<typename Op>
    void generic_RM16_1(Op, const X86::Instruction&);
    template<typename Op>
    void generic_RM16_CL(Op, const X86::Instruction&);
    template<typename Op>
    void generic_RM32_1(Op, const X86::Instruction&);
    template<typename Op>
    void generic_RM32_CL(Op, const X86::Instruction&);

    void update_code_cache();

    void did_receive_secret_data();

private:
    Emulator& m_emulator;

    PartAddressableRegister m_gpr[8];
    PartAddressableRegister m_gpr_shadow[8];

    u16 m_segment[8] { 0 };
    u32 m_eflags { 0 };

    bool m_flags_tainted { false };

    u32 m_eip { 0 };
    u32 m_base_eip { 0 };

    long double m_fpu[8];
    // FIXME: Shadow for m_fpu.

    // FIXME: Use bits 11 to 13 in the FPU status word for this.
    int m_fpu_top { -1 };

    void fpu_push(long double n)
    {
        ++m_fpu_top;
        fpu_set(0, n);
    }
    long double fpu_pop()
    {
        auto n = fpu_get(0);
        m_fpu_top--;
        return n;
    }
    long double fpu_get(int i)
    {
        VERIFY(i >= 0 && i <= m_fpu_top);
        return m_fpu[m_fpu_top - i];
    }
    void fpu_set(int i, long double n)
    {
        VERIFY(i >= 0 && i <= m_fpu_top);
        m_fpu[m_fpu_top - i] = n;
    }

    // FIXME: Or just something like m_flags_tainted?
    ValueWithShadow<u16> m_fpu_cw { 0, 0 };

    Region* m_cached_code_region { nullptr };
    u8* m_cached_code_base_ptr { nullptr };

    u32 m_secret_handshake_state { 0 };
    u32 m_secret_data[3];
};

ALWAYS_INLINE u8 SoftCPU::read8()
{
    if (!m_cached_code_region || !m_cached_code_region->contains(m_eip))
        update_code_cache();

    u8 value = m_cached_code_base_ptr[m_eip - m_cached_code_region->base()];
    m_eip += 1;
    return value;
}

ALWAYS_INLINE u16 SoftCPU::read16()
{
    if (!m_cached_code_region || !m_cached_code_region->contains(m_eip))
        update_code_cache();

    u16 value = *reinterpret_cast<const u16*>(&m_cached_code_base_ptr[m_eip - m_cached_code_region->base()]);
    m_eip += 2;
    return value;
}

ALWAYS_INLINE u32 SoftCPU::read32()
{
    if (!m_cached_code_region || !m_cached_code_region->contains(m_eip))
        update_code_cache();

    u32 value = *reinterpret_cast<const u32*>(&m_cached_code_base_ptr[m_eip - m_cached_code_region->base()]);
    m_eip += 4;
    return value;
}

ALWAYS_INLINE u64 SoftCPU::read64()
{
    if (!m_cached_code_region || !m_cached_code_region->contains(m_eip))
        update_code_cache();

    auto value = *reinterpret_cast<const u64*>(&m_cached_code_base_ptr[m_eip - m_cached_code_region->base()]);
    m_eip += 8;
    return value;
}

}