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
|
(*========================================================================*)
(* *)
(* This software was developed by the University of Cambridge Computer *)
(* Laboratory as part of the Rigorous Engineering of Mainstream Systems *)
(* (REMS) project, funded by EPSRC grant EP/K008528/1. *)
(* *)
(* 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 AUTHOR 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 AUTHOR 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. *)
(*========================================================================*)
default Order dec
val extern forall Nat 'n. (bit['n],[|'n|]) -> bit['n] effect pure ASR
val extern forall Nat 'n. (bit['n],[|'n|]) -> bit['n] effect pure LSR
val extern forall Nat 'n. (bit['n],[|'n|]) -> bit['n] effect pure ROR
val extern forall Nat 'n. (bit['n],[|'n|]) -> bit['n] effect pure ROL
val cast bool -> bit effect pure cast_bool_bit
val cast bit -> int effect pure cast_bit_int
val extern forall Num 'n. int -> bit['n] effect pure cast_int_vec
val extern forall 'n, 'm, 'o, 'n <= 0, 'm <= 'o. [|'n:'m|] -> [|0:'o|] effect pure negative_to_zero
typedef byte = bit[8]
typedef qword = bit[64]
typedef regn = [|15|]
typedef byte_stream = list<byte>
typedef ostream = option<byte_stream>
(* --------------------------------------------------------------------------
Registers
-------------------------------------------------------------------------- *)
(* Program Counter *)
register qword RIP
(* General purpose registers *)
register qword RAX (* 0 *)
register qword RCX (* 1 *)
register qword RDX (* 2 *)
register qword RBX (* 3 *)
register qword RSP (* 4 *)
register qword RBP (* 5 *)
register qword RSI (* 6 *)
register qword RDI (* 7 *)
register qword R8
register qword R9
register qword R10
register qword R11
register qword R12
register qword R13
register qword R14
register qword R15
let (vector<0,16,inc,(register<qword>)>) REG =
[RAX,RCX,RDX,RBX,RSP,RBP,RSI,RDI,R8,R9,R10,R11,R12,R13,R14,R15]
(* Flags *)
register bit CF
register bit PF
register bit AF
register bit ZF
register bit SF
register bit OF
(* --------------------------------------------------------------------------
Memory
-------------------------------------------------------------------------- *)
val extern forall Nat 'n. (qword, [|'n|]) -> (bit[8 * 'n]) effect { rmem } MEM
val extern forall Nat 'n. (qword, [|'n|], bit[8 * 'n]) -> unit effect { wmem } wMEM
(* --------------------------------------------------------------------------
Helper functions
-------------------------------------------------------------------------- *)
(* Instruction addressing modes *)
typedef size = const union {
bool Sz8;
unit Sz16;
unit Sz32;
unit Sz64;
}
typedef base = const union {
unit NoBase;
unit RipBase;
regn RegBase;
}
typedef scale_index = (bit[2],regn)
typedef rm = const union {
regn Reg;
(option<scale_index>,base,qword) Mem;
}
typedef dest_src = const union {
(rm,qword) Rm_i;
(rm,regn) Rm_r;
(regn,rm) R_rm;
}
typedef imm_rm = const union {
rm Rm;
qword Imm;
}
typedef monop_name = enumerate { Dec; Inc; Not; Neg }
typedef binop_name = enumerate {
Add; Or; Adc; Sbb; And; Sub; Xor; Cmp; Rol; Ror; Rcl; Rcr; Shl; Shr; Test; Sar
}
typedef cond = enumerate {
O; NO; B; NB; E; NE; NA; A; S; NS; P; NP; L; NL; NG; G; ALWAYS
}
(* Effective addresses *)
typedef ea = const union {
(size,qword) Ea_i;
(size,regn) Ea_r;
(size,qword) Ea_m;
}
function qword ea_index ((option<scale_index>) index) =
switch (index) {
case None -> 0x0000000000000000
case (Some(scale, idx)) ->
let x = (qword) (0x0000000000000001 << scale) in
let y = (qword) (REG[idx]) in
let z = (bit[128]) (x * y) in
z[63 .. 0]
}
function qword ea_base ((base) b) =
switch b {
case NoBase -> 0x0000000000000000
case RipBase -> RIP
case (RegBase(b)) -> REG[b]
}
function ea ea_rm ((size) sz, (rm) r) =
switch r {
case (Reg(n)) -> Ea_r (sz, n)
case (Mem(idx, b, d)) -> Ea_m (sz, ea_index(idx) + (qword) (ea_base(b) + d))
}
function ea ea_dest ((size) sz, (dest_src) ds) =
switch ds {
case (Rm_i (v, _)) -> ea_rm (sz, v)
case (Rm_r (v, _)) -> ea_rm (sz, v)
case (R_rm (v, _)) -> Ea_r (sz, v)
}
function ea ea_src ((size) sz, (dest_src) ds) =
switch ds {
case (Rm_i (_, v)) -> Ea_i (sz, v)
case (Rm_r (_, v)) -> Ea_r (sz, v)
case (R_rm (_, v)) -> ea_rm (sz, v)
}
function ea ea_imm_rm ((size) sz, (imm_rm) i_rm) =
switch i_rm {
case (Rm (v)) -> ea_rm (sz, v)
case (Imm (v)) -> Ea_i (sz, v)
}
function qword restrictSize ((size) sz, (qword) imm) =
switch sz {
case (Sz8(_)) -> imm & 0x00000000000000FF
case Sz16 -> imm & 0x000000000000FFFF
case Sz32 -> imm & 0x00000000FFFFFFFF
case Sz64 -> imm
}
function regn sub4 ((regn) r) = negative_to_zero (r - 4)
function qword effect { rreg, rmem } EA ((ea) e) =
switch e {
case (Ea_i(sz,i)) -> restrictSize(sz,i)
case (Ea_r((Sz8(have_rex)),r)) ->
if have_rex | r < 4 (* RSP *) | r > 7 (* RDI *) then
REG[r]
else
LSR (REG[sub4 (r)], 8) & 0x00000000000000FF
case (Ea_r(sz,r)) -> restrictSize(sz, REG[r])
case (Ea_m((Sz8(_)),a)) -> EXTZ (MEM(a, 1))
case (Ea_m(Sz16,a)) -> EXTZ (MEM(a, 2))
case (Ea_m(Sz32,a)) -> EXTZ (MEM(a, 4))
case (Ea_m(Sz64,a)) -> MEM(a, 8)
}
function unit effect { wmem, wreg, escape } wEA ((ea) e, (qword) w) =
switch e {
case (Ea_i(_,_)) -> exit ()
case (Ea_r((Sz8(have_rex)),r)) ->
if have_rex | r < 4 (* RSP *) | r > 7 (* RDI *) then
{
(qword) regr := REG[r];
regr[7 .. 0] := w[7 .. 0];
REG[r] := regr
}
else
{
(qword) regr := REG[sub4(r)];
regr[15 .. 8] := (vector<15,8,dec,bit>) (adjust_dec(w[7 .. 0]));
REG[sub4(r)] := regr
}
case (Ea_r(Sz16,r)) ->
{
(qword) regr := REG[r];
regr[15 .. 8] := w[15 .. 8];
REG[r] := regr
}
case (Ea_r(Sz32,r)) -> REG[r] := (qword) (EXTZ (w[31 .. 0]))
case (Ea_r(Sz64,r)) -> REG[r] := w
case (Ea_m((Sz8(_)),a)) -> wMEM(a, 1, w[7 .. 0])
case (Ea_m(Sz16,a)) -> wMEM(a, 2, w[15 .. 0])
case (Ea_m(Sz32,a)) -> wMEM(a, 4, w[31 .. 0])
case (Ea_m(Sz64,a)) -> wMEM(a, 8, w)
}
function (ea, qword, qword) read_dest_src_ea ((size) sz, (dest_src) ds) =
let e = ea_dest (sz, ds) in
(e, EA(e), EA(ea_src(sz, ds)))
function qword call_dest_from_ea ((ea) e) =
switch e {
case (Ea_i(_, i)) -> RIP + i
case (Ea_r(_, r)) -> REG[r]
case (Ea_m(_, a)) -> MEM(a, 8)
}
function qword get_ea_address ((ea) e) =
switch e {
case (Ea_i(_, i)) -> 0x0000000000000000
case (Ea_r(_, r)) -> 0x0000000000000000
case (Ea_m(_, a)) -> 0x0000000000000000
}
function unit jump_to_ea ((ea) e) = RIP := call_dest_from_ea(e)
(* EFLAG updates *)
function bit byte_parity ((byte) b) =
{
(int) acc := 0;
foreach (i from 0 to 7) acc := acc + (int) (b[i]);
(bit) (acc mod 2 == 0)
}
function [|64|] size_width ((size) sz) =
switch sz {
case (Sz8(_)) -> 8
case Sz16 -> 16
case Sz32 -> 32
case Sz64 -> 64
}
function [|63|] size_width_sub1 ((size) sz) =
switch sz {
case (Sz8(_)) -> 7
case Sz16 -> 15
case Sz32 -> 31
case Sz64 -> 63
}
(* XXXXX
function bit word_size_msb ((size) sz, (qword) w) = w[size_width(sz) - 1]
*)
function bit word_size_msb ((size) sz, (qword) w) = w[size_width_sub1(sz)]
function unit write_PF ((qword) w) = PF := byte_parity (w[7 .. 0])
function unit write_SF ((size) sz, (qword) w) = SF := word_size_msb (sz, w)
function unit write_ZF ((size) sz, (qword) w) =
ZF := (bit)
(switch sz {
case (Sz8(_)) -> w[7 .. 0] == 0x00
case Sz16 -> w[15 .. 0] == 0x0000
case Sz32 -> w[31 .. 0] == 0x00000000
case Sz64 -> w == 0x0000000000000000
})
function unit write_arith_eflags_except_CF_OF ((size) sz, (qword) w) =
{
AF := undefined;
write_PF(w);
write_SF(sz, w);
write_ZF(sz, w);
}
function unit write_arith_eflags ((size) sz, (qword) w, (bit) c, (bit) x) =
{
CF := c;
OF := x;
write_arith_eflags_except_CF_OF (sz, w)
}
function unit write_logical_eflags ((size) sz, (qword) w) =
write_arith_eflags (sz, w, bitzero, bitzero)
function unit erase_eflags () =
{
AF := undefined;
CF := undefined;
OF := undefined;
PF := undefined;
SF := undefined;
ZF := undefined;
}
(* XXXXX *)
function nat power ((nat) x, ([|64|]) y) = undefined
function nat value_width ((size) sz) = power (2, size_width(sz))
function bit word_signed_overflow_add ((size) sz, (qword) a, (qword) b) =
(bit) (word_size_msb (sz, a) == word_size_msb (sz, b) &
word_size_msb (sz, a + b) != word_size_msb (sz, a))
function bit word_signed_overflow_sub ((size) sz, (qword) a, (qword) b) =
(bit) (word_size_msb (sz, a) != word_size_msb (sz, b) &
word_size_msb (sz, a - b) != word_size_msb (sz, a))
function (qword, bit, bit) add_with_carry_out ((size) sz, (qword) a, (qword) b) =
(a + b, (bit) ((int) (value_width (sz)) <= unsigned(a) + unsigned(b)),
word_signed_overflow_add (sz, a, b))
function (qword, bit, bit) sub_with_borrow ((size) sz, (qword) a, (qword) b) =
(a - b, (bit) (a < b), word_signed_overflow_sub (sz, a, b))
function unit write_arith_result ((size) sz, (qword) w, (bit) c, (bit) x, (ea) e) =
{
write_arith_eflags (sz, w, c, x);
wEA (e) := w;
}
function unit write_arith_result_no_CF_OF ((size) sz, (qword) w, (ea) e) =
{
write_arith_eflags_except_CF_OF (sz, w);
wEA (e) := w;
}
function unit write_logical_result ((size) sz, (qword) w, (ea) e) =
{
write_arith_eflags_except_CF_OF (sz, w);
wEA (e) := w;
}
function unit write_result_erase_eflags ((qword) w, (ea) e) =
{
erase_eflags ();
wEA (e) := w;
}
function qword effect { escape } sign_extension ((qword) w, (size) size1, (size) size2) =
{
(qword) x := w;
switch (size1, size2) {
case ((Sz8(_)), Sz16) -> x[15 .. 0] := (bit[16]) (EXTS (w[7 .. 0]))
case ((Sz8(_)), Sz32) -> x[31 .. 0] := (bit[32]) (EXTS (w[7 .. 0]))
case ((Sz8(_)), Sz64) -> x := (qword) (EXTS (w[7 .. 0]))
case (Sz16, Sz32) -> x[31 .. 0] := (bit[32]) (EXTS (w[15 .. 0]))
case (Sz16, Sz64) -> x := (qword) (EXTS (w[15 .. 0]))
case (Sz32, Sz64) -> x := (qword) (EXTS (w[31 .. 0]))
case _ -> undefined
};
x;
}
function [|64|] mask_shift ((size) sz, (qword) w) =
if sz == Sz64 then w[5 .. 0] else w[4 .. 0]
function qword rol ((size) sz, (qword) a, (qword) b) =
switch sz {
case (Sz8(_)) -> EXTZ (ROL (a[7 .. 0], b[2 .. 0]))
case Sz16 -> EXTZ (ROL (a[15 .. 0], b[3 .. 0]))
case Sz32 -> EXTZ (ROL (a[31 .. 0], b[4 .. 0]))
case Sz64 -> ROL (a, b[5 .. 0])
}
function qword ror ((size) sz, (qword) a, (qword) b) =
switch sz {
case (Sz8(_)) -> EXTZ (ROR (a[7 .. 0], b[2 .. 0]))
case Sz16 -> EXTZ (ROR (a[15 .. 0], b[3 .. 0]))
case Sz32 -> EXTZ (ROR (a[31 .. 0], b[4 .. 0]))
case Sz64 -> ROR (a, b[5 .. 0])
}
function qword sar ((size) sz, (qword) a, (qword) b) =
switch sz {
case (Sz8(_)) -> EXTZ (ASR (a[7 .. 0], b[2 .. 0]))
case Sz16 -> EXTZ (ASR (a[15 .. 0], b[3 .. 0]))
case Sz32 -> EXTZ (ASR (a[31 .. 0], b[4 .. 0]))
case Sz64 -> ASR (a, b[5 .. 0])
}
function unit write_binop ((size) sz, (binop_name) bop, (qword) a, (qword) b, (ea) e) =
switch bop {
case Add -> let (w,c,x) = add_with_carry_out (sz, a, b) in
write_arith_result (sz, w, c, x, e)
case Sub -> let (w,c,x) = sub_with_borrow (sz, a, b) in
write_arith_result (sz, w, c, x, e)
case Cmp -> let (w,c,x) = sub_with_borrow (sz, a, b) in
write_arith_eflags (sz, w, c, x)
case Test -> write_logical_eflags (sz, a & b)
case And -> write_logical_result (sz, a & b, e)
case Xor -> write_logical_result (sz, a ^ b, e)
case Or -> write_logical_result (sz, a | b, e)
case Rol -> write_result_erase_eflags (rol (sz, a, b), e)
case Ror -> write_result_erase_eflags (ror (sz, a, b), e)
case Sar -> write_result_erase_eflags (sar (sz, a, b), e)
case Shl -> write_result_erase_eflags (a << mask_shift (sz,b), e)
case Shr -> write_result_erase_eflags (LSR (a, mask_shift (sz,b)), e)
case Adc ->
{
let carry = CF in
let (qword) result = a + (qword) (b + carry) in
{
CF := (bit) ((int) (value_width (sz)) <= unsigned(a) + unsigned(b));
OF := undefined;
write_arith_result_no_CF_OF (sz, result, e);
}
}
case Sbb ->
{
let carry = CF in
let (qword) result = a - (qword) (b + carry) in
{
CF := (bit) (unsigned(a) < unsigned(b) + (int) carry);
OF := undefined;
write_arith_result_no_CF_OF (sz, result, e);
}
}
case _ -> exit ()
}
function unit write_monop ((size) sz, (monop_name) mop, (qword) a, (ea) e) =
switch mop {
case Not -> wEA(e) := ~(a)
case Dec -> write_arith_result_no_CF_OF (sz, a - 1, e)
case Inc -> write_arith_result_no_CF_OF (sz, a + 1, e)
case Neg -> { write_arith_result_no_CF_OF (sz, 0 - a, e);
CF := undefined;
}
}
function bool read_cond ((cond) c) =
switch c {
case A -> ~(CF) & ~(ZF)
case NB -> ~(CF)
case B -> CF
case NA -> CF | (bit) ZF
case E -> ZF
case G -> ~(ZF) & (SF == OF)
case NL -> SF == OF
case L -> SF != OF
case NG -> ZF | SF != OF
case NE -> ~(ZF)
case NO -> ~(OF)
case NP -> ~(PF)
case NS -> ~(SF)
case O -> OF
case P -> PF
case S -> SF
case ALWAYS -> true
}
function qword pop_aux () =
let top = MEM(RSP, 8) in
{
RSP := RSP + 8;
top;
}
function unit push_aux ((qword) w) =
{
RSP := RSP - 8;
wMEM(RSP, 8) := w;
}
function unit pop ((rm) r) = wEA (ea_rm (Sz64,r)) := pop_aux()
function unit pop_rip () = RIP := pop_aux()
function unit push ((imm_rm) i) = push_aux (EA (ea_imm_rm (Sz64, i)))
function unit push_rip () = push_aux (RIP)
function unit drop ((qword) i) = if i[7 ..0] != 0 then () else RSP := RSP + i
(* --------------------------------------------------------------------------
Instructions
-------------------------------------------------------------------------- *)
scattered function unit execute
scattered typedef ast = const union
val ast -> unit effect {escape, rmem, rreg, undef, wmem, wreg} execute
(* ==========================================================================
Binop
========================================================================== *)
union ast member (binop_name,size,dest_src) Binop
function clause execute (Binop (bop,sz,ds)) =
let (e, val_dst, val_src) = read_dest_src_ea (sz, ds) in
write_binop (sz, bop, val_dst, val_src, e)
(* ==========================================================================
CALL
========================================================================== *)
union ast member imm_rm CALL
function clause execute (CALL (i)) =
{
push_rip();
jump_to_ea (ea_imm_rm (Sz64, i))
}
(* ==========================================================================
CLC
========================================================================== *)
union ast member unit CLC
function clause execute CLC = CF := false
(* ==========================================================================
CMC
========================================================================== *)
union ast member unit CMC
function clause execute CMC = CF := ~(CF)
(* ==========================================================================
CMPXCHG
========================================================================== *)
union ast member (size,rm,regn) CMPXCHG
function clause execute (CMPXCHG (sz,r,n)) =
let src = Ea_r(sz, n) in
let acc = Ea_r(sz, 0) in (* RAX *)
let dst = ea_rm(sz, r) in
let val_dst = EA(dst) in
let val_acc = EA(src) in
{
write_binop (sz, Cmp, val_acc, val_dst, src);
if val_acc == val_dst then
wEA(dst) := EA (src)
else
wEA(acc) := val_dst;
}
(* ==========================================================================
DIV
========================================================================== *)
union ast member (size,rm) DIV
function clause execute (DIV (sz,r)) =
let w = (int) (value_width(sz)) in
let eax = Ea_r(sz, 0) in (* RAX *)
let edx = Ea_r(sz, 2) in (* RDX *)
let n = unsigned(EA(edx)) * w + unsigned(EA(eax)) in
let d = unsigned(EA(ea_rm(sz, r))) in
let q = n quot d in
let m = n mod d in
if d == 0 | w < q then exit ()
else
{
wEA(eax) := cast_int_vec(q);
wEA(edx) := cast_int_vec(m);
erase_eflags();
}
(* ==========================================================================
Jcc
========================================================================== *)
union ast member (cond,qword) Jcc
function clause execute (Jcc (c,i)) =
if read_cond (c) then RIP := RIP + i else ()
(* ==========================================================================
JMP
========================================================================== *)
union ast member rm JMP
function clause execute (JMP (r)) = RIP := EA (ea_rm (Sz64, r))
(* ==========================================================================
LEA
========================================================================== *)
union ast member (size,dest_src) LEA
function clause execute (LEA (sz,ds)) =
let src = ea_src (sz, ds) in
let dst = ea_dest (sz, ds) in
wEA(dst) := get_ea_address (src)
(* ==========================================================================
LEAVE
========================================================================== *)
union ast member unit LEAVE
function clause execute LEAVE =
{
RSP := RBP;
pop (Reg (5)); (* RBP *)
}
(* ==========================================================================
LOOP
========================================================================== *)
union ast member (cond,qword) LOOP
function clause execute (LOOP (c,i)) =
{
RCX := RCX - 1;
if RCX != 0 & read_cond (c) then RIP := RIP + i else ();
}
(* ==========================================================================
Monop
========================================================================== *)
union ast member (monop_name,size,rm) Monop
function clause execute (Monop (mop,sz,r)) =
let e = ea_rm (sz, r) in write_monop (sz, mop, EA(e), e)
(* ==========================================================================
MOV
========================================================================== *)
union ast member (cond,size,dest_src) MOV
function clause execute (MOV (c,sz,ds)) =
if read_cond (c) then
let src = ea_src (sz, ds) in
let dst = ea_dest (sz, ds) in
wEA(dst) := EA(src)
else ()
(* ==========================================================================
MOVSX
========================================================================== *)
union ast member (size,dest_src,size) MOVSX
function clause execute (MOVSX (sz1,ds,sz2)) =
let src = ea_src (sz1, ds) in
let dst = ea_dest (sz2, ds) in
wEA(dst) := sign_extension (EA(src), sz1, sz2)
(* ==========================================================================
MOVZX
========================================================================== *)
union ast member (size,dest_src,size) MOVZX
function clause execute (MOVZX (sz1,ds,sz2)) =
let src = ea_src (sz1, ds) in
let dst = ea_dest (sz2, ds) in
wEA(dst) := EA(src)
(* ==========================================================================
MUL
========================================================================== *)
union ast member (size,rm) MUL
function clause execute (MUL (sz,r)) =
let eax = Ea_r (sz, 0) in (* RAX *)
let val_eax = EA(eax) in
let val_src = EA(ea_rm (sz, r)) in
switch sz {
case (Sz8(_)) -> wEA(Ea_r(Sz16,0)) := (val_eax * val_src)[63 .. 0]
case _ ->
let m = val_eax * val_src in
let edx = Ea_r (sz, 2) in (* RDX *)
{
wEA(eax) := m[63 .. 0];
wEA(edx) := (LSR (m, size_width(sz)))[63 .. 0]
}
}
(* ==========================================================================
NOP
========================================================================== *)
union ast member nat NOP
function clause execute (NOP (_)) = ()
(* ==========================================================================
POP
========================================================================== *)
union ast member rm POP
function clause execute (POP (r)) = pop(r)
(* ==========================================================================
PUSH
========================================================================== *)
union ast member imm_rm PUSH
function clause execute (PUSH (i)) = push(i)
(* ==========================================================================
RET
========================================================================== *)
union ast member qword RET
function clause execute (RET (i)) =
{
pop_rip();
drop(i);
}
(* ==========================================================================
SET
========================================================================== *)
union ast member (cond,bool,rm) SET
function clause execute (SET (c,b,r)) =
wEA(ea_rm(Sz8(b),r)) := if read_cond (c) then 1 else 0
(* ==========================================================================
STC
========================================================================== *)
union ast member unit STC
function clause execute STC = CF := true
(* ==========================================================================
XADD
========================================================================== *)
union ast member (size,rm,regn) XADD
function clause execute (XADD (sz,r,n)) =
let src = Ea_r (sz, n) in
let dst = ea_rm (sz, r) in
let val_src = EA(src) in
let val_dst = EA(dst) in
{
wEA(src) := val_dst;
write_binop (sz, Add, val_src, val_dst, dst);
}
(* ==========================================================================
XCHG
========================================================================== *)
union ast member (size,rm,regn) XCHG
function clause execute (XCHG (sz,r,n)) =
let src = Ea_r (sz, n) in
let dst = ea_rm (sz, r) in
let val_src = EA(src) in
let val_dst = EA(dst) in
{
wEA(src) := val_dst;
wEA(dst) := val_src;
}
end ast
end execute
|