-
Notifications
You must be signed in to change notification settings - Fork 18
/
asm-x86.lisp
2211 lines (1893 loc) · 68 KB
/
asm-x86.lisp
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
;;;;------------------------------------------------------------------
;;;;
;;;; Copyright (C) 2007 Frode V. Fjeld
;;;;
;;;; Description: x86 assembler for 16, 32, and 64-bit modes.
;;;; Author: Frode Vatvedt Fjeld <frodef@acm.org>
;;;; Distribution: See the accompanying file COPYING.
;;;;
;;;; $Id: asm-x86.lisp,v 1.39 2008-08-06 09:43:36 ffjeld Exp $
;;;;
;;;;------------------------------------------------------------------
(defpackage asm-x86
(:use :common-lisp :asm)
(:export #:assemble-instruction
#:disassemble-instruction
#:*cpu-mode*
#:*position-independent-p*))
(in-package asm-x86)
(defvar *cpu-mode* :32-bit)
(defvar *instruction-encoders*
(make-hash-table :test 'eq))
(defvar *use-jcc-16-bit-p* nil
"Whether to use 16-bit JCC instructions in 32-bit mode.")
(defun prefix-lookup (prefix-name)
(cdr (or (assoc prefix-name
'((:operand-size-override . #x66)
(:address-size-override . #x67)
(:16-bit-operand . #x66)
(:16-bit-address . #x67)
(:lock . #xf0)
(:repne . #xf2)
(:repz . #xf3)
(:cs-override . #x2e)
(:ss-override . #x36)
(:ds-override . #x3e)
(:es-override . #x26)
(:fs-override . #x64)
(:gs-override . #x65)))
(error "There is no instruction prefix named ~S." prefix-name))))
(defun rex-encode (rexes &key rm)
(let ((rex (logior (if (null rexes)
0
(+ #x40 (loop for rex in rexes
sum (ecase rex
(:rex.w #b1000)
(:rex.r #b0100)
(:rex.x #b0010)
(:rex.b #b0001)))))
(if (not rm)
0
(ldb (byte 1 3) rm)))))
(unless (zerop rex)
(list rex))))
(deftype octet ()
'(unsigned-byte 8))
(deftype xint (size)
`(or (unsigned-byte ,size)
(signed-byte ,size)))
(deftype uint (size)
`(unsigned-byte ,size))
(deftype sint (size)
`(signed-byte ,size))
(defun integer-to-octets (i n)
"Return list of n octets, encoding i in signed little endian."
(loop for b from 0 below (* 8 n) by 8
collect (ldb (byte 8 b) i)))
(defun encode-values-fun (operator legacy-prefixes prefixes rexes opcode mod reg rm scale index base displacement immediate operand-size address-size)
(assert opcode)
(when (or (and (eq address-size :32-bit)
(eq *cpu-mode* :64-bit))
(and (eq address-size :16-bit)
(eq *cpu-mode* :32-bit))
(and (eq address-size :64-bit)
(eq *cpu-mode* :32-bit))
(and (eq address-size :32-bit)
(eq *cpu-mode* :16-bit)))
(pushnew :address-size-override
prefixes))
(when (or (and (eq operand-size :16-bit)
(eq *cpu-mode* :64-bit))
(and (eq operand-size :16-bit)
(eq *cpu-mode* :32-bit))
(and (eq operand-size :32-bit)
(eq *cpu-mode* :16-bit)))
(pushnew :operand-size-override
prefixes))
(let ((code (append legacy-prefixes
(mapcar #'prefix-lookup (reverse prefixes))
(rex-encode rexes :rm rm)
(when (< 16 (integer-length opcode))
(list (ldb (byte 8 16) opcode)))
(when (< 8(integer-length opcode))
(list (ldb (byte 8 8) opcode)))
(list (ldb (byte 8 0) opcode))
(when (or mod reg rm)
(assert (and mod reg rm) (mod reg rm)
"Either all or none of mod, reg, and rm must be defined. mod=~S, reg=~S, rm=~S." mod reg rm)
(check-type mod (unsigned-byte 2))
(list (logior (ash (ldb (byte 2 0) mod)
6)
(ash (ldb (byte 3 0) reg)
3)
(ash (ldb (byte 3 0) rm)
0))))
(when (or scale index base)
(assert (and scale index base) (scale index base)
"Either all or none of scale, index, and base must be defined. scale=~S, index=~S, base=~S." scale index base)
(check-type scale (unsigned-byte 2))
(check-type index (unsigned-byte 4))
(check-type base (unsigned-byte 4))
(list (logior (ash (ldb (byte 2 0) scale)
6)
(ash (ldb (byte 3 0) index)
3)
(ash (ldb (byte 3 0) base)
0))))
displacement
immediate)))
(append (compute-extra-prefixes operator *pc* (length code))
code)))
(defmacro encode (values-form)
`(multiple-value-call #'encode-values-fun operator legacy-prefixes ,values-form))
(defmacro merge-encodings (form1 form2)
`(multiple-value-bind (prefixes1 rexes1 opcode1 mod1 reg1 rm1 scale1 index1 base1 displacement1 immediate1 operand-size1 address-size1)
,form1
(multiple-value-bind (prefixes2 rexes2 opcode2 mod2 reg2 rm2 scale2 index2 base2 displacement2 immediate2 operand-size2 address-size2)
,form2
(macrolet ((getone (a b name)
`(cond
((and ,a ,b)
(error "~A doubly defined: ~S and ~S." ',name ,a ,b))
(,a)
(,b))))
(encoded-values :prefixes (append prefixes1 prefixes2)
:rex (append (if (listp rexes1)
rexes1
(list rexes1))
(if (listp rexes2)
rexes2
(list rexes2)))
:opcode (getone opcode1 opcode2 opcode)
:mod (getone mod1 mod2 mod)
:reg (getone reg1 reg2 reg)
:rm (getone rm1 rm2 rm)
:scale (getone scale1 scale2 scale)
:index (getone index1 index2 index)
:base (getone base1 base2 base)
:displacement (getone displacement1 displacement2 displacement)
:immediate (getone immediate1 immediate2 immediate)
:operand-size (getone operand-size1 operand-size2 operand-size)
:address-size (getone address-size1 address-size2 address-size))))))
(defun encoded-values (&key prefixes prefix rex opcode mod reg rm scale index base displacement immediate operand-size address-size)
(values (append (when prefix
(list prefix))
prefixes)
(if (keywordp rex)
(list rex)
rex)
opcode
mod reg rm
scale index base
displacement
immediate
operand-size
address-size))
(defun assemble-instruction (instruction)
"Assemble a single instruction to a list of octets of x86 machine code, according to *cpu-mode* etc."
(multiple-value-bind (instruction legacy-prefixes options)
(if (listp (car instruction))
(values (cdr instruction)
(remove-if #'listp (car instruction))
(remove-if #'keywordp (car instruction)))
(values instruction
nil
nil))
(destructuring-bind (operator &rest operands)
instruction
(multiple-value-bind (code failp)
(apply (or (gethash operator *instruction-encoders*)
(error "Unknown instruction operator ~S in ~S." operator instruction))
operator
(mapcar #'prefix-lookup legacy-prefixes)
operands)
(cond
(failp
(error "Unable to encode ~S." instruction))
((null options)
code)
((assoc :size options)
(assert (= (second (assoc :size options))
(length code)))
code))))))
(defmacro define-operator (operator operator-mode lambda-list &body body)
(check-type operator keyword)
(labels ((find-forms (body)
(cond
((atom body)
nil)
((member (car body) '(reg-modrm modrm opcode imm-modrm imm opcode-reg
opcode-reg-imm pc-rel moffset sreg-modrm reg-cr
far-pointer))
(list body))
(t (mapcan #'find-forms body)))))
(let ((defun-name (intern (format nil "~A-~A" 'instruction-encoder operator))))
`(progn
(defun ,defun-name (operator legacy-prefixes ,@lambda-list)
(declare (ignorable operator legacy-prefixes))
(let ((operator-mode ',operator-mode)
(default-rex nil))
(declare (ignorable operator-mode default-rex))
(macrolet ((disassembler (&body body)
(declare (ignore body)))
(assembler (&body body)
`(progn ,@body)))
(block operator
,@body
(values nil 'fail)))))
(setf (gethash ',operator *instruction-encoders*)
',defun-name)
(macrolet ((disassembler (&body body)
`(progn ,@body))
(assembler (&body body)
(declare (ignore body))))
(let ((operator ',operator)
(operator-mode ',operator-mode)
(operand-formals ',lambda-list))
(declare (ignorable operator operand-formals operator-mode))
,@(find-forms body)))
',operator))))
(defmacro define-operator/none (name lambda-list &body body)
`(define-operator ,name nil ,lambda-list ,@body))
(deftype list-of (&rest elements)
"A list with elements of specified type(s)."
(labels ((make-list-of (elements)
(if (null elements)
'null
`(cons ,(car elements)
,(make-list-of (cdr elements))))))
(make-list-of elements)))
(deftype list-of* (&rest elements)
"A list starting with elements of specified type(s)."
(labels ((make-list-of (elements)
(if (null elements)
'list
`(cons ,(car elements)
,(make-list-of (cdr elements))))))
(make-list-of elements)))
(defparameter *opcode-disassemblers-16*
(make-array 256 :initial-element nil))
(defparameter *opcode-disassemblers-32*
(make-array 256 :initial-element nil))
(defparameter *opcode-disassemblers-64*
(make-array 256 :initial-element nil))
(deftype disassembly-decoder ()
'(list-of* boolean keyword (or keyword null) symbol))
(defun (setf opcode-disassembler) (decoder opcode operator-mode)
(check-type decoder disassembly-decoder)
(labels ((set-it (table pos)
(check-type pos (integer 0 *))
(check-type table (simple-vector 256))
(let ((bit-pos (* 8 (1- (ceiling (integer-length pos) 8)))))
(if (not (plusp bit-pos))
(progn
#+(or) (unless (or (eq nil decoder)
(eq nil (svref table pos))
(equal decoder (svref table pos)))
(warn "Redefining disassembler for ~@[~(~A~) ~]opcode #x~X from ~{~S ~}to ~{~S~^ ~}."
operator-mode opcode (svref table pos) decoder))
(setf (svref table pos) decoder))
(set-it (or (svref table (ldb (byte 8 bit-pos) pos))
(setf (svref table (ldb (byte 8 bit-pos) pos))
(make-array 256 :initial-element nil)))
(ldb (byte bit-pos 0) pos))))))
(ecase operator-mode
(:16-bit
(set-it *opcode-disassemblers-16* opcode))
(:32-bit
(set-it *opcode-disassemblers-32* opcode))
(:64-bit
(set-it *opcode-disassemblers-64* opcode))
((:8-bit nil)
(set-it *opcode-disassemblers-16* opcode)
(set-it *opcode-disassemblers-32* opcode)
(set-it *opcode-disassemblers-64* opcode)))))
(defmacro pop-code (code-place &optional context)
`(progn
(unless ,code-place
(error "End of byte-stream in the middle of an instruction."))
(let ((x (pop ,code-place)))
(check-type x (unsigned-byte 8) ,(format nil "an octet (context: ~A)" context))
x)))
(defmacro code-call (form &optional (code-place (case (car form) ((funcall apply) (third form)) (t (second form)))))
"Execute form, then 'magically' update the code binding with the secondary return value from form."
`(let (tmp)
(declare (ignorable tmp))
(setf (values tmp ,code-place) ,form)))
(defmacro define-disassembler ((operator opcode &optional cpu-mode digit backup-p operand-size) lambda-list &body body)
(cond
(digit
`(loop for mod from #b00 to #b11
do (loop for r/m from #b000 to #b111
as ext-opcode = (logior (ash ,opcode 8)
(ash ,digit 3)
(ash mod 6)
r/m)
do (define-disassembler (,operator ext-opcode ,cpu-mode nil t ,operand-size) ,lambda-list ,@body))))
((symbolp lambda-list)
`(setf (opcode-disassembler ,opcode ,cpu-mode) (list ,backup-p ,operator ,(or operand-size cpu-mode) ',lambda-list ,@body)))
(t (let ((defun-name (intern (format nil "~A-~A-~X~@[-~A~]" 'disassembler operator opcode cpu-mode))))
`(progn
(defun ,defun-name ,lambda-list ,@body)
(setf (opcode-disassembler ,opcode ',cpu-mode) (list ,backup-p ,operator ',(or operand-size cpu-mode) ',defun-name))
',defun-name)))))
(defun disassemble-simple-prefix (code operator opcode operand-size address-size rex)
(declare (ignore opcode rex))
(let ((instruction (code-call (disassemble-instruction code operand-size address-size nil))))
(values (if (consp (car instruction))
(list* (list* operator (car instruction))
(cdr instruction))
(list* (list operator)
instruction))
code)))
(define-disassembler (:lock #xf0) disassemble-simple-prefix)
(define-disassembler (:repne #xf2) disassemble-simple-prefix)
(define-disassembler (:repz #xf3) disassemble-simple-prefix)
(define-disassembler (:cs-override #x2e) disassemble-simple-prefix)
(define-disassembler (:ss-override #x36) disassemble-simple-prefix)
(define-disassembler (:ds-override #x3e) disassemble-simple-prefix)
(define-disassembler (:es-override #x26) disassemble-simple-prefix)
(define-disassembler (:fs-override #x64) disassemble-simple-prefix)
(define-disassembler (:gs-override #x65) disassemble-simple-prefix)
(define-disassembler (:operand-size-override #x66 :32-bit) (code operator opcode operand-size address-size rex)
(declare (ignore operator opcode operand-size rex))
(disassemble-instruction code :16-bit address-size nil))
(define-disassembler (:address-size-override #x67 :32-bit) (code operator opcode operand-size address-size rex)
(declare (ignore operator opcode address-size rex))
(disassemble-instruction code operand-size :16-bit nil))
(define-disassembler (:operand-size-override #x66 :16-bit) (code operator opcode operand-size address-size rex)
(declare (ignore operator opcode operand-size rex))
(disassemble-instruction code :32-bit address-size nil))
(define-disassembler (:address-size-override #x67 :16-bit) (code operator opcode operand-size address-size rex)
(declare (ignore operator opcode address-size rex))
(disassemble-instruction code operand-size :32-bit nil))
(defmacro define-operator/8 (operator lambda-list &body body)
`(define-operator ,operator :8-bit ,lambda-list
(let ((default-rex nil))
(declare (ignorable default-rex))
,@body)))
(defmacro define-operator/16 (operator lambda-list &body body)
`(define-operator ,operator :16-bit ,lambda-list
(let ((default-rex nil))
(declare (ignorable default-rex))
,@body)))
(defmacro define-operator/32 (operator lambda-list &body body)
`(define-operator ,operator :32-bit ,lambda-list
(let ((default-rex nil))
(declare (ignorable default-rex))
,@body)))
(defmacro define-operator/64 (operator lambda-list &body body)
`(define-operator ,operator :64-bit ,lambda-list
(let ((default-rex '(:rex.w)))
(declare (ignorable default-rex))
,@body)))
(defmacro define-operator/64* (operator lambda-list &body body)
`(define-operator ,operator :64-bit ,lambda-list
(let ((default-rex (case *cpu-mode*
(:64-bit nil)
(t '(:rex.w)))))
(declare (ignorable default-rex))
,@body)))
(defmacro define-operator* ((&key |16| |32| |64| dispatch) args &body body)
(let ((body16 (subst '(xint 16) :int-16-32-64
(subst :dx :dx-edx-rdx
(subst :ax :ax-eax-rax body))))
(body32 (subst '(xint 32) :int-16-32-64
(subst :edx :dx-edx-rdx
(subst :eax :ax-eax-rax body))))
(body64 (subst '(sint 32) :int-16-32-64
(subst :rdx :dx-edx-rdx
(subst :rax :ax-eax-rax body)))))
`(progn
,(when |16|
`(define-operator/16 ,|16| ,args ,@body16))
,(when |32|
`(define-operator/32 ,|32| ,args ,@body32))
,(when |64|
`(define-operator/64 ,|64| ,args ,@body64))
,(when dispatch
(let ((dispatch-name (intern (format nil "~A-~A" 'instruction-dispatcher dispatch))))
`(progn
(defun ,dispatch-name (&rest args)
(declare (dynamic-extent args))
(loop for encoder in (ecase *cpu-mode*
(:32-bit ',(remove nil (list |32| |16| |64|)))
(:64-bit ',(remove nil (list |64| |32| |16|)))
(:16-bit ',(remove nil (list |16| |32| |64|))))
thereis (apply (gethash encoder *instruction-encoders*) args)
finally (return (values nil 'fail))))
(setf (gethash ',dispatch *instruction-encoders*)
',dispatch-name))))
nil)))
(defun resolve-and-encode (x type &key size)
(encode-integer (cond
((typep x type)
x)
((integerp x)
(error "Immediate value #x~X out of range for ~S." x type))
((assoc x *symtab*)
(let ((value (cdr (assoc x *symtab*))))
(assert (typep value type))
value))
(t (error "Unresolved symbol ~S (size ~S)." x size)))
type))
(defun resolve-pc-relative (operand)
(etypecase operand
(pc-relative-operand
(reduce #'+ (cdr operand)
:key #'resolve-operand))
(symbol-reference
(assert *pc* (*pc*) "Cannot encode a pc-relative operand without a value for ~S." '*pc*)
(- (resolve-operand operand)
*pc*))))
(defun encode-integer (i type)
(assert (typep i type))
(let ((bit-size (cadr type)))
(loop for b upfrom 0 below bit-size by 8
collect (ldb (byte 8 b) i))))
(defun type-octet-size (type)
(assert (member (car type)
'(sint uint xint))
(type))
(values (ceiling (cadr type) 8)))
(defun opcode-octet-size (opcode)
(loop do (setf opcode (ash opcode -8))
count t
while (plusp opcode)))
(defun parse-indirect-operand (operand)
(assert (indirect-operand-p operand))
(let (reg offsets reg2 reg-scale)
(dolist (expr operand)
(etypecase expr
(register-operand
(if reg
(setf reg2 expr)
(setf reg expr)))
((cons register-operand
(cons (member 1 2 4 8) null))
(when reg
(assert (not reg-scale))
(setf reg2 reg))
(setf reg (first expr)
reg-scale (second expr)))
(immediate-operand
(push expr offsets))
((cons (eql :+))
(dolist (term (cdr expr))
(push term offsets)))))
(when (and (eq reg2 :esp)
(or (not reg-scale)
(eql 1 reg-scale)))
(psetf reg reg2
reg2 reg))
(values reg offsets reg2 (if (not reg)
nil
(or reg-scale 1)))))
(defun register-set-by-mode (mode)
(ecase mode
(:8-bit '(:al :cl :dl :bl :ah :ch :dh :bh))
(:16-bit '(:ax :cx :dx :bx :sp :bp :si :di))
(:32-bit '(:eax :ecx :edx :ebx :esp :ebp :esi :edi))
(:64-bit '(:rax :rcx :rdx :rbx :rsp :rbp :rsi :rdi :r8 :r9 :r10 :r11 :r12 :13 :r14 :r15))
(:mm '(:mm0 :mm1 :mm2 :mm3 :mm4 :mm5 :mm6 :mm7))
(:xmm '(:xmm0 :xmm1 :xmm2 :xmm3 :xmm4 :xmm5 :xmm6 :xmm7))
(:segment '(:es :cs :ss :ds :fs :gs))))
(defun encode-reg/mem (operand mode)
(check-type mode (member nil :8-bit :16-bit :32-bit :64-bit :mm :xmm))
(if (and mode (keywordp operand))
(encoded-values :mod #b11
:rm (or (position operand (register-set-by-mode mode))
(error "Unknown ~(~D~) register ~S." mode operand)))
(multiple-value-bind (reg offsets reg2 reg-scale)
(parse-indirect-operand operand)
(check-type reg-scale (member nil 1 2 4 8))
(assert (or (not reg2)
(and reg reg2)))
(assert (or (not reg-scale)
(and reg reg-scale)))
(let ((offset (reduce #'+ offsets
:key #'resolve-operand)))
(cond
((and (not reg)
(eq mode :16-bit)
(typep offset '(xint 16)))
(encoded-values :mod #b00
:rm #b110
:address-size :16-bit
:displacement (encode-integer offset '(xint 16))))
((and (not reg)
(typep offset '(xint 32)))
(encoded-values :mod #b00
:rm #b101
:address-size :32-bit
:displacement (encode-integer offset '(xint 32))))
((and (eq reg :sp)
(not reg2)
(= 1 reg-scale))
(etypecase offset
((eql 0)
(encoded-values :mod #b00
:rm #b100
:scale 0
:index #b100
:base #b100
:address-size :16-bit))
((sint 8)
(encoded-values :mod #b01
:rm #b100
:displacement (encode-integer offset '(sint 8))
:scale 0
:index #b100
:base #b100
:address-size :16-bit))
((xint 32)
(encoded-values :mod #b10
:rm #b100
:displacement (encode-integer offset '(xint 32))
:scale 0
:index #b100
:base #b100
:address-size :16-bit))))
((and (eq reg :esp)
(= 1 reg-scale))
(let ((reg2-index (or (position reg2 '(:eax :ecx :edx :ebx nil :ebp :esi :edi))
(error "Unknown reg2 [F] ~S." reg2))))
(etypecase offset
((eql 0)
(encoded-values :mod #b00
:rm #b100
:scale 0
:index reg2-index
:base #b100
:address-size :32-bit))
((sint 8)
(encoded-values :mod #b01
:rm #b100
:displacement (encode-integer offset '(sint 8))
:scale 0
:index reg2-index
:base #b100
:address-size :32-bit))
((xint 32)
(encoded-values :mod #b10
:rm #b100
:displacement (encode-integer offset '(xint 32))
:scale 0
:index reg2-index
:base #b100
:address-size :32-bit)))))
((and (eq reg :rsp)
(not reg2)
(= 1 reg-scale))
(etypecase offset
((eql 0)
(encoded-values :mod #b00
:rm #b100
:scale 0
:index #b100
:base #b100
:address-size :64-bit))
((sint 8)
(encoded-values :mod #b01
:rm #b100
:displacement (encode-integer offset '(sint 8))
:scale 0
:index #b100
:base #b100
:address-size :64-bit))
((sint 32)
(encoded-values :mod #b10
:rm #b100
:displacement (encode-integer offset '(sint 32))
:scale 0
:index #b100
:base #b100
:address-size :64-bit))))
(t (multiple-value-bind (register-index map address-size)
(let* ((map32 '(:eax :ecx :edx :ebx :esp :ebp :esi :edi))
(index32 (position reg map32))
(map64 '(:rax :rcx :rdx :rbx :rsp :rbp :rsi :rdi :r8 :r9 :r10 :r11 :r12 :r13 :r14 :r15))
(index64 (unless index32
(position reg map64))))
(if index32
(values index32 map32 :32-bit)
(values index64 map64 :64-bit)))
(cond
((and (not reg2)
register-index
(= 1 reg-scale)
(and (zerop offset)
(not (= register-index #b101))))
(encoded-values :mod #b00
:rm register-index
:address-size address-size))
((and (not reg2)
register-index
(= 1 reg-scale)
(typep offset '(sint 8)))
(encoded-values :mod #b01
:rm register-index
:displacement (encode-integer offset '(sint 8))
:address-size address-size))
((and (not reg2)
register-index
(= 1 reg-scale)
(or (typep offset '(sint 32))
(and (eq :32-bit address-size)
(typep offset '(xint 32)))))
(encoded-values :mod #b10
:rm register-index
:displacement (encode-integer offset '(sint 32))
:address-size address-size))
((and (not reg2)
register-index
(if (eq :64-bit *cpu-mode*)
(typep offset '(sint 32))
(typep offset '(xint 32)))
(not (= #b100 register-index)))
(encoded-values :rm #b100
:mod #b00
:index register-index
:base #b101
:scale (or (position reg-scale '(1 2 4 8))
(error "Unknown register scale ~S." reg-scale))
:displacement (encode-integer offset '(xint 32))))
((and reg2
register-index
(zerop offset)
(not (= register-index #b100)))
(encoded-values :mod #b00
:rm #b100
:scale (or (position reg-scale '(1 2 4 8))
(error "Unknown register scale ~S." reg-scale))
:index register-index
:base (or (position reg2 map)
(error "unknown reg2 [A] ~S" reg2))
:address-size address-size))
((and reg2
register-index
(typep offset '(sint 8))
(not (= register-index #b100)))
(encoded-values :mod #b01
:rm #b100
:scale (position reg-scale '(1 2 4 8))
:index register-index
:base (or (position reg2 map)
(error "unknown reg2 [B] ~S" reg2))
:address-size address-size
:displacement (encode-integer offset '(sint 8))))
((and reg2
register-index
(eq :32-bit address-size)
(typep offset '(sint 8))
(not (= register-index #b100)))
(encoded-values :mod #b01
:rm #b100
:scale (position reg-scale '(1 2 4 8))
:index register-index
:base (or (position reg2 map)
(error "unknown reg2 [C] ~S." reg2))
:address-size address-size
:displacement (encode-integer offset '(sint 8))))
((and reg2
register-index
(eq :32-bit address-size)
(typep offset '(xint 32))
(not (= register-index #b100)))
(encoded-values :mod #b10
:rm #b100
:scale (position reg-scale '(1 2 4 8))
:index register-index
:base (or (position reg2 map)
(error "unknown reg2 [D] ~S." reg2))
:address-size address-size
:displacement (encode-integer offset '(xint 32))))
((and reg2
register-index
(eq :64-bit address-size)
(typep offset '(sint 32))
(not (= register-index #b100)))
(encoded-values :mod #b01
:rm #b100
:scale (position reg-scale '(1 2 4 8))
:index register-index
:base (or (position reg2 map)
(error "unknown reg2 [E] ~S" reg2))
:address-size address-size
:displacement (encode-integer offset '(sint 32))))
(t (let ((rm16 (position-if (lambda (x)
(or (and (eq (car x) reg)
(eq (cdr x) reg2))
(and (eq (car x) reg2)
(eq (cdr x) reg))))
'((:bx . :si) (:bx . :di) (:bp . :si) (:bp . :di)
(:si) (:di) (:bp) (:bx)))))
(cond
((and rm16
(zerop offset)
(not (= #b110 rm16)))
(encoded-values :mod #b00
:rm rm16
:address-size :16-bit))
((and rm16
(typep offset '(sint 8)))
(encoded-values :mod #b01
:rm rm16
:address-size :16-bit
:displacement (encode-integer offset '(sint 8))))
((and rm16
(typep offset '(xint 16)))
(encoded-values :mod #b10
:rm rm16
:address-size :16-bit
:displacement (encode-integer offset '(xint 16))))
(t (error "Huh? reg: ~S, reg2: ~S, scale: ~S, offset: ~S" reg reg2 reg-scale offset)))))))))))))
(defun operand-ordering (formals &rest arrangement)
(loop with rarrangement = (reverse arrangement)
for formal in formals
when (getf rarrangement formal)
collect it))
(defun order-operands (ordering &rest operands)
(loop for key in ordering
collect (or (getf operands key)
(error "No operand ~S in ~S." key operands))))
(defun decode-integer (code type)
"Decode an integer of specified type."
(let* ((bit-size (cadr type))
(unsigned-integer (loop for b from 0 below bit-size by 8
sum (ash (pop-code code integer) b))))
(values (if (or (not (member (car type) '(sint signed-byte)))
(not (logbitp (1- bit-size) unsigned-integer)))
unsigned-integer
(- (ldb (byte bit-size 0)
(1+ (lognot unsigned-integer)))))
code)))
(defun disassemble-instruction (code &optional override-operand-size override-address-size rex)
(labels ((lookup-decoder (table opcode)
(let* ((backup-code code)
(datum (pop-code code))
(opcode (logior (ash opcode 8)
datum))
(decoder (svref table datum)))
(typecase decoder
(vector
(lookup-decoder decoder opcode))
(disassembly-decoder
(when (car decoder)
(setf code backup-code))
(values (cdr decoder)
opcode))
(t (error "No disassembler registered for opcode #x~X." opcode))))))
(multiple-value-bind (decoder opcode)
(lookup-decoder (ecase (or override-operand-size *cpu-mode*)
(:16-bit *opcode-disassemblers-16*)
(:32-bit *opcode-disassemblers-32*)
(:64-bit *opcode-disassemblers-64*))
0)
(destructuring-bind (operator operand-size decoder-function &rest extra-args)
decoder
(values (code-call (apply decoder-function
code
operator
opcode
(or operand-size override-operand-size)
(or override-address-size *cpu-mode*)
rex
extra-args))
code)))))
(defun decode-no-operands (code operator opcode operand-size address-size rex &rest fixed-operands)
(declare (ignore opcode operand-size address-size rex))
(values (list* operator
(remove nil fixed-operands))
code))
(defun decode-reg-cr (code operator opcode operand-size address-size rex operand-ordering)
(declare (ignore opcode operand-size address-size))
(let ((modrm (pop-code code)))
(values (list* operator
(order-operands operand-ordering
:reg (nth (ldb (byte 3 0) modrm)
(register-set-by-mode (if rex :64-bit :32-bit)))
:cr (nth (ldb (byte 3 3) modrm)
'(:cr0 :cr1 :cr2 :cr3 :cr4 :cr5 :cr6 :cr7))))
code)))
(defun decode-reg-modrm (code operator opcode operand-size address-size rex operand-ordering &optional (reg-mode operand-size))
(declare (ignore opcode rex))
(values (list* operator
(order-operands operand-ordering
:reg (nth (ldb (byte 3 3) (car code))
(register-set-by-mode reg-mode))
:modrm (ecase address-size
(:32-bit
(code-call (decode-reg-modrm-32 code operand-size)))
(:16-bit
(code-call (decode-reg-modrm-16 code operand-size))))))
code))
(defun decode-modrm (code operator opcode operand-size address-size rex)
(declare (ignore opcode rex))
(values (list operator
(ecase address-size
(:32-bit
(code-call (decode-reg-modrm-32 code operand-size)))
(:16-bit
(code-call (decode-reg-modrm-16 code operand-size)))))
code))
(defun decode-imm-modrm (code operator opcode operand-size address-size rex imm-type operand-ordering &key fixed-modrm)
(declare (ignore opcode rex))
(values (list* operator
(order-operands operand-ordering
:modrm (or fixed-modrm
(when (member :modrm operand-ordering)
(ecase address-size
(:32-bit
(code-call (decode-reg-modrm-32 code operand-size)))
(:16-bit
(code-call (decode-reg-modrm-16 code operand-size))))))
:imm (code-call (decode-integer code imm-type))))
code))
(defun decode-far-pointer (code operator opcode operand-size address-size rex type)
(declare (ignore opcode operand-size address-size rex))
(let ((offset (code-call (decode-integer code type)))
(segment (code-call (decode-integer code '(uint 16)))))
(values (list operator
segment
(list offset))
code)))
(defun decode-pc-rel (code operator opcode operand-size address-size rex type)
(declare (ignore opcode operand-size address-size rex))
(values (list operator
`(:pc+ ,(code-call (decode-integer code type))))
code))
(defun decode-moffset (code operator opcode operand-size address-size rex type operand-ordering fixed-operand)
(declare (ignore opcode operand-size address-size rex))
(values (list* operator
(order-operands operand-ordering
:moffset (list (code-call (decode-integer code type)))
:fixed fixed-operand))
code))
(defun decode-opcode-reg (code operator opcode operand-size address-size rex operand-ordering extra-operand)
(declare (ignore address-size rex))
(values (list* operator
(order-operands operand-ordering
:reg (nth (ldb (byte 3 0) opcode)
(register-set-by-mode operand-size))
:extra extra-operand))
code))
(defun decode-opcode-reg-imm (code operator opcode operand-size address-size rex operand-ordering imm-type)
(declare (ignore address-size rex))
(values (list* operator
(order-operands operand-ordering
:reg (nth (ldb (byte 3 0) opcode)
(register-set-by-mode operand-size))
:imm (code-call (decode-integer code imm-type))))
code))
(defun decode-reg-modrm-16 (code operand-size)
(let* ((modrm (pop-code code mod/rm))
(mod (ldb (byte 2 6) modrm))
(reg (ldb (byte 3 3) modrm))
(r/m (ldb (byte 3 0) modrm)))
(values (if (= mod #b11)
(nth reg (register-set-by-mode operand-size))
(flet ((operands (i)
(nth i '((:bx :si) (:bx :di) (:bp :si) (:bp :di) (:si) (:di) (:bp) (:bx)))))
(ecase mod
(#b00
(case r/m
(#b110 (list (code-call (decode-integer code '(uint 16)))))
(t (operands r/m))))
(#b01
(append (operands r/m)
(list (code-call (decode-integer code '(sint 8))))))
(#b10
(append (operands r/m)
(list (code-call (decode-integer code '(uint 16)))))))))
code)))
(defun decode-reg-modrm-32 (code operand-size)
"Return a list of the REG, and the MOD/RM operands."
(let* ((modrm (pop-code code mod/rm))
(mod (ldb (byte 2 6) modrm))
(r/m (ldb (byte 3 0) modrm)))
(values (if (= mod #b11)
(nth r/m (register-set-by-mode operand-size))
(flet ((decode-sib ()
(let* ((sib (pop-code code sib))
(ss (ldb (byte 2 6) sib))
(index (ldb (byte 3 3) sib))
(base (ldb (byte 3 0) sib)))
(nconc (unless (= index #b100)
(let ((index-reg (nth index (register-set-by-mode :32-bit))))
(if (= ss #b00)
(list index-reg)
(list (list index-reg (ash 2 ss))))))
(if (/= base #b101)
(list (nth base (register-set-by-mode :32-bit)))
(ecase mod
(#b00 nil)
((#b01 #b10) (list :ebp))))))))
(ecase mod
(#b00 (case r/m
(#b100 (decode-sib))
(#b101 (code-call (decode-integer code '(uint 32))))
(t (list (nth r/m (register-set-by-mode :32-bit))))))
(#b01 (case r/m
(#b100 (nconc(decode-sib)
(list (code-call (decode-integer code '(sint 8))))))
(t (list (nth r/m (register-set-by-mode :32-bit))
(code-call (decode-integer code '(sint 8)))))))
(#b10 (case r/m
(#b100 (nconc (decode-sib)
(list (code-call (decode-integer code '(uint 32))))))
(t (list (nth r/m (register-set-by-mode :32-bit))
(code-call (decode-integer code '(uint 32))))))))))
code)))
(defmacro return-when (form)
`(let ((x ,form))
(when x (return-from operator x))))
(defmacro return-values-when (form)
`(let ((x (encode ,form)))
(when x (return-from operator x))))