1 ;;;; that part of the description of the x86-64 instruction set
2 ;;;; which can live on the cross-compilation host
4 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
14 ;;; FIXME: SB!DISASSEM: prefixes are used so widely in this file that
15 ;;; I wonder whether the separation of the disassembler from the
16 ;;; virtual machine is valid or adds value.
18 ;;; Note: In CMU CL, this used to be a call to SET-DISASSEM-PARAMS.
19 (setf sb!disassem:*disassem-inst-alignment-bytes* 1)
21 ;;; This type is used mostly in disassembly and represents legacy
22 ;;; registers only. R8-R15 are handled separately.
23 (deftype reg () '(unsigned-byte 3))
25 ;;; This includes legacy registers and R8-R15.
26 (deftype full-reg () '(unsigned-byte 4))
28 ;;; The XMM registers XMM0 - XMM15.
29 (deftype xmmreg () '(unsigned-byte 4))
31 ;;; Default word size for the chip: if the operand size /= :dword
32 ;;; we need to output #x66 (or REX) prefix
33 (def!constant +default-operand-size+ :dword)
35 ;;; The default address size for the chip. It could be overwritten
36 ;;; to :dword with a #x67 prefix, but this is never needed by SBCL
37 ;;; and thus not supported by this assembler/disassembler.
38 (def!constant +default-address-size+ :qword)
40 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
42 (defun offset-next (value dstate)
43 (declare (type integer value)
44 (type sb!disassem:disassem-state dstate))
45 (+ (sb!disassem:dstate-next-addr dstate) value))
47 (defparameter *byte-reg-names*
48 #(al cl dl bl spl bpl sil dil r8b r9b r10b r11b r12b r13b r14b r15b))
49 (defparameter *high-byte-reg-names*
51 (defparameter *word-reg-names*
52 #(ax cx dx bx sp bp si di r8w r9w r10w r11w r12w r13w r14w r15w))
53 (defparameter *dword-reg-names*
54 #(eax ecx edx ebx esp ebp esi edi r8d r9d r10d r11d r12d r13d r14d r15d))
55 (defparameter *qword-reg-names*
56 #(rax rcx rdx rbx rsp rbp rsi rdi r8 r9 r10 r11 r12 r13 r14 r15))
58 ;;; The printers for registers, memory references and immediates need to
59 ;;; take into account the width bit in the instruction, whether a #x66
60 ;;; or a REX prefix was issued, and the contents of the REX prefix.
61 ;;; This is implemented using prefilters to put flags into the slot
62 ;;; INST-PROPERTIES of the DSTATE. These flags are the following
65 ;;; OPERAND-SIZE-8 The width bit was zero
66 ;;; OPERAND-SIZE-16 The "operand size override" prefix (#x66) was found
67 ;;; REX A REX prefix was found
68 ;;; REX-W A REX prefix with the "operand width" bit set was
70 ;;; REX-R A REX prefix with the "register" bit set was found
71 ;;; REX-X A REX prefix with the "index" bit set was found
72 ;;; REX-B A REX prefix with the "base" bit set was found
74 ;;; Return the operand size depending on the prefixes and width bit as
76 (defun inst-operand-size (dstate)
77 (declare (type sb!disassem:disassem-state dstate))
78 (cond ((sb!disassem:dstate-get-inst-prop dstate 'operand-size-8)
80 ((sb!disassem:dstate-get-inst-prop dstate 'rex-w)
82 ((sb!disassem:dstate-get-inst-prop dstate 'operand-size-16)
85 +default-operand-size+)))
87 ;;; The same as INST-OPERAND-SIZE, but for those instructions (e.g.
88 ;;; PUSH, JMP) that have a default operand size of :qword. It can only
89 ;;; be overwritten to :word.
90 (defun inst-operand-size-default-qword (dstate)
91 (declare (type sb!disassem:disassem-state dstate))
92 (if (sb!disassem:dstate-get-inst-prop dstate 'operand-size-16)
96 ;;; Print to STREAM the name of the general-purpose register encoded by
97 ;;; VALUE and of size WIDTH. For robustness, the high byte registers
98 ;;; (AH, BH, CH, DH) are correctly detected, too, although the compiler
99 ;;; does not use them.
100 (defun print-reg-with-width (value width stream dstate)
101 (declare (type full-reg value)
103 (type sb!disassem:disassem-state dstate))
104 (princ (if (and (eq width :byte)
106 (not (sb!disassem:dstate-get-inst-prop dstate 'rex)))
107 (aref *high-byte-reg-names* (- value 4))
109 (:byte *byte-reg-names*)
110 (:word *word-reg-names*)
111 (:dword *dword-reg-names*)
112 (:qword *qword-reg-names*))
115 ;; XXX plus should do some source-var notes
118 (defun print-reg (value stream dstate)
119 (declare (type full-reg value)
121 (type sb!disassem:disassem-state dstate))
122 (print-reg-with-width value
123 (inst-operand-size dstate)
127 (defun print-reg-default-qword (value stream dstate)
128 (declare (type full-reg value)
130 (type sb!disassem:disassem-state dstate))
131 (print-reg-with-width value
132 (inst-operand-size-default-qword dstate)
136 (defun print-byte-reg (value stream dstate)
137 (declare (type full-reg value)
139 (type sb!disassem:disassem-state dstate))
140 (print-reg-with-width value :byte stream dstate))
142 (defun print-addr-reg (value stream dstate)
143 (declare (type full-reg value)
145 (type sb!disassem:disassem-state dstate))
146 (print-reg-with-width value +default-address-size+ stream dstate))
148 ;;; Print a register or a memory reference of the given WIDTH.
149 ;;; If SIZED-P is true, add an explicit size indicator for memory
151 (defun print-reg/mem-with-width (value width sized-p stream dstate)
152 (declare (type (or list full-reg) value)
153 (type (member :byte :word :dword :qword) width)
154 (type boolean sized-p)
156 (type sb!disassem:disassem-state dstate))
157 (if (typep value 'full-reg)
158 (print-reg-with-width value width stream dstate)
159 (print-mem-access value (and sized-p width) stream dstate)))
161 ;;; Print a register or a memory reference. The width is determined by
162 ;;; calling INST-OPERAND-SIZE.
163 (defun print-reg/mem (value stream dstate)
164 (declare (type (or list full-reg) value)
166 (type sb!disassem:disassem-state dstate))
167 (print-reg/mem-with-width
168 value (inst-operand-size dstate) nil stream dstate))
170 ;; Same as print-reg/mem, but prints an explicit size indicator for
171 ;; memory references.
172 (defun print-sized-reg/mem (value stream dstate)
173 (declare (type (or list full-reg) value)
175 (type sb!disassem:disassem-state dstate))
176 (print-reg/mem-with-width
177 value (inst-operand-size dstate) t stream dstate))
179 ;;; Same as print-sized-reg/mem, but with a default operand size of
181 (defun print-sized-reg/mem-default-qword (value stream dstate)
182 (declare (type (or list full-reg) value)
184 (type sb!disassem:disassem-state dstate))
185 (print-reg/mem-with-width
186 value (inst-operand-size-default-qword dstate) t stream dstate))
188 (defun print-sized-byte-reg/mem (value stream dstate)
189 (declare (type (or list full-reg) value)
191 (type sb!disassem:disassem-state dstate))
192 (print-reg/mem-with-width value :byte t stream dstate))
194 (defun print-sized-word-reg/mem (value stream dstate)
195 (declare (type (or list full-reg) value)
197 (type sb!disassem:disassem-state dstate))
198 (print-reg/mem-with-width value :word t stream dstate))
200 (defun print-sized-dword-reg/mem (value stream dstate)
201 (declare (type (or list full-reg) value)
203 (type sb!disassem:disassem-state dstate))
204 (print-reg/mem-with-width value :dword t stream dstate))
206 (defun print-label (value stream dstate)
207 (declare (ignore dstate))
208 (sb!disassem:princ16 value stream))
210 (defun print-xmmreg (value stream dstate)
211 (declare (type xmmreg value)
214 (format stream "XMM~d" value))
216 (defun print-xmmreg/mem (value stream dstate)
217 (declare (type (or list xmmreg) value)
219 (type sb!disassem:disassem-state dstate))
220 (if (typep value 'xmmreg)
221 (print-xmmreg value stream dstate)
222 (print-mem-access value nil stream dstate)))
224 ;; Same as print-xmmreg/mem, but prints an explicit size indicator for
225 ;; memory references.
226 (defun print-sized-xmmreg/mem (value stream dstate)
227 (declare (type (or list xmmreg) value)
229 (type sb!disassem:disassem-state dstate))
230 (if (typep value 'xmmreg)
231 (print-xmmreg value stream dstate)
232 (print-mem-access value (inst-operand-size dstate) stream dstate)))
234 ;;; This prefilter is used solely for its side effects, namely to put
235 ;;; the bits found in the REX prefix into the DSTATE for use by other
236 ;;; prefilters and by printers.
237 (defun prefilter-wrxb (value dstate)
238 (declare (type (unsigned-byte 4) value)
239 (type sb!disassem:disassem-state dstate))
240 (sb!disassem:dstate-put-inst-prop dstate 'rex)
241 (when (plusp (logand value #b1000))
242 (sb!disassem:dstate-put-inst-prop dstate 'rex-w))
243 (when (plusp (logand value #b0100))
244 (sb!disassem:dstate-put-inst-prop dstate 'rex-r))
245 (when (plusp (logand value #b0010))
246 (sb!disassem:dstate-put-inst-prop dstate 'rex-x))
247 (when (plusp (logand value #b0001))
248 (sb!disassem:dstate-put-inst-prop dstate 'rex-b))
251 ;;; This prefilter is used solely for its side effect, namely to put
252 ;;; the property OPERAND-SIZE-8 into the DSTATE if VALUE is 0.
253 (defun prefilter-width (value dstate)
254 (declare (type bit value)
255 (type sb!disassem:disassem-state dstate))
257 (sb!disassem:dstate-put-inst-prop dstate 'operand-size-8))
260 ;;; This prefilter is used solely for its side effect, namely to put
261 ;;; the property OPERAND-SIZE-16 into the DSTATE.
262 (defun prefilter-x66 (value dstate)
263 (declare (type (eql #x66) value)
265 (type sb!disassem:disassem-state dstate))
266 (sb!disassem:dstate-put-inst-prop dstate 'operand-size-16))
268 ;;; A register field that can be extended by REX.R.
269 (defun prefilter-reg-r (value dstate)
270 (declare (type reg value)
271 (type sb!disassem:disassem-state dstate))
272 (if (sb!disassem::dstate-get-inst-prop dstate 'rex-r)
276 ;;; A register field that can be extended by REX.B.
277 (defun prefilter-reg-b (value dstate)
278 (declare (type reg value)
279 (type sb!disassem:disassem-state dstate))
280 (if (sb!disassem::dstate-get-inst-prop dstate 'rex-b)
284 ;;; Returns either an integer, meaning a register, or a list of
285 ;;; (BASE-REG OFFSET INDEX-REG INDEX-SCALE), where any component
286 ;;; may be missing or nil to indicate that it's not used or has the
287 ;;; obvious default value (e.g., 1 for the index-scale). VALUE is a list
288 ;;; of the mod and r/m field of the ModRM byte of the instruction.
289 ;;; Depending on VALUE a SIB byte and/or an offset may be read. The
290 ;;; REX.B bit from DSTATE is used to extend the sole register or the
291 ;;; BASE-REG to a full register, the REX.X bit does the same for the
293 (defun prefilter-reg/mem (value dstate)
294 (declare (type list value)
295 (type sb!disassem:disassem-state dstate))
296 (let ((mod (first value))
297 (r/m (second value)))
298 (declare (type (unsigned-byte 2) mod)
299 (type (unsigned-byte 3) r/m))
300 (let ((full-reg (if (sb!disassem:dstate-get-inst-prop dstate 'rex-b)
303 (declare (type full-reg full-reg))
309 (let ((sib (sb!disassem:read-suffix 8 dstate)))
310 (declare (type (unsigned-byte 8) sib))
311 (let ((base-reg (ldb (byte 3 0) sib))
312 (index-reg (ldb (byte 3 3) sib))
313 (index-scale (ldb (byte 2 6) sib)))
314 (declare (type (unsigned-byte 3) base-reg index-reg)
315 (type (unsigned-byte 2) index-scale))
319 (if (= base-reg #b101)
320 (sb!disassem:read-signed-suffix 32 dstate)
323 (sb!disassem:read-signed-suffix 8 dstate))
325 (sb!disassem:read-signed-suffix 32 dstate)))))
326 (list (unless (and (= mod #b00) (= base-reg #b101))
327 (if (sb!disassem:dstate-get-inst-prop dstate 'rex-b)
331 (unless (= index-reg #b100)
332 (if (sb!disassem:dstate-get-inst-prop dstate 'rex-x)
335 (ash 1 index-scale))))))
336 ((and (= mod #b00) (= r/m #b101))
337 (list 'rip (sb!disassem:read-signed-suffix 32 dstate)))
341 (list full-reg (sb!disassem:read-signed-suffix 8 dstate)))
343 (list full-reg (sb!disassem:read-signed-suffix 32 dstate)))))))
345 (defun read-address (value dstate)
346 (declare (ignore value)) ; always nil anyway
347 (sb!disassem:read-suffix (width-bits (inst-operand-size dstate)) dstate))
349 (defun width-bits (width)
358 ;;;; disassembler argument types
360 ;;; Used to capture the lower four bits of the REX prefix.
361 (sb!disassem:define-arg-type wrxb
362 :prefilter #'prefilter-wrxb)
364 (sb!disassem:define-arg-type width
365 :prefilter #'prefilter-width
366 :printer (lambda (value stream dstate)
367 (declare (ignore value))
368 (princ (schar (symbol-name (inst-operand-size dstate)) 0)
371 ;;; Used to capture the effect of the #x66 operand size override prefix.
372 (sb!disassem:define-arg-type x66
373 :prefilter #'prefilter-x66)
375 (sb!disassem:define-arg-type displacement
377 :use-label #'offset-next
378 :printer (lambda (value stream dstate)
379 (sb!disassem:maybe-note-assembler-routine value nil dstate)
380 (print-label value stream dstate)))
382 (sb!disassem:define-arg-type accum
383 :printer (lambda (value stream dstate)
384 (declare (ignore value)
386 (type sb!disassem:disassem-state dstate))
387 (print-reg 0 stream dstate)))
389 (sb!disassem:define-arg-type reg
390 :prefilter #'prefilter-reg-r
391 :printer #'print-reg)
393 (sb!disassem:define-arg-type reg-b
394 :prefilter #'prefilter-reg-b
395 :printer #'print-reg)
397 (sb!disassem:define-arg-type reg-b-default-qword
398 :prefilter #'prefilter-reg-b
399 :printer #'print-reg-default-qword)
401 (sb!disassem:define-arg-type imm-addr
402 :prefilter #'read-address
403 :printer #'print-label)
405 ;;; Normally, immediate values for an operand size of :qword are of size
406 ;;; :dword and are sign-extended to 64 bits. For an exception, see the
407 ;;; argument type definition following this one.
408 (sb!disassem:define-arg-type signed-imm-data
409 :prefilter (lambda (value dstate)
410 (declare (ignore value)) ; always nil anyway
411 (let ((width (width-bits (inst-operand-size dstate))))
414 (sb!disassem:read-signed-suffix width dstate))))
416 ;;; Used by the variant of the MOV instruction with opcode B8 which can
417 ;;; move immediates of all sizes (i.e. including :qword) into a
419 (sb!disassem:define-arg-type signed-imm-data-upto-qword
420 :prefilter (lambda (value dstate)
421 (declare (ignore value)) ; always nil anyway
422 (sb!disassem:read-signed-suffix
423 (width-bits (inst-operand-size dstate))
426 ;;; Used by those instructions that have a default operand size of
427 ;;; :qword. Nevertheless the immediate is at most of size :dword.
428 ;;; The only instruction of this kind having a variant with an immediate
429 ;;; argument is PUSH.
430 (sb!disassem:define-arg-type signed-imm-data-default-qword
431 :prefilter (lambda (value dstate)
432 (declare (ignore value)) ; always nil anyway
433 (let ((width (width-bits
434 (inst-operand-size-default-qword dstate))))
437 (sb!disassem:read-signed-suffix width dstate))))
439 (sb!disassem:define-arg-type signed-imm-byte
440 :prefilter (lambda (value dstate)
441 (declare (ignore value)) ; always nil anyway
442 (sb!disassem:read-signed-suffix 8 dstate)))
444 (sb!disassem:define-arg-type imm-byte
445 :prefilter (lambda (value dstate)
446 (declare (ignore value)) ; always nil anyway
447 (sb!disassem:read-suffix 8 dstate)))
449 ;;; needed for the ret imm16 instruction
450 (sb!disassem:define-arg-type imm-word-16
451 :prefilter (lambda (value dstate)
452 (declare (ignore value)) ; always nil anyway
453 (sb!disassem:read-suffix 16 dstate)))
455 (sb!disassem:define-arg-type reg/mem
456 :prefilter #'prefilter-reg/mem
457 :printer #'print-reg/mem)
458 (sb!disassem:define-arg-type sized-reg/mem
459 ;; Same as reg/mem, but prints an explicit size indicator for
460 ;; memory references.
461 :prefilter #'prefilter-reg/mem
462 :printer #'print-sized-reg/mem)
464 ;;; Arguments of type reg/mem with a fixed size.
465 (sb!disassem:define-arg-type sized-byte-reg/mem
466 :prefilter #'prefilter-reg/mem
467 :printer #'print-sized-byte-reg/mem)
468 (sb!disassem:define-arg-type sized-word-reg/mem
469 :prefilter #'prefilter-reg/mem
470 :printer #'print-sized-word-reg/mem)
471 (sb!disassem:define-arg-type sized-dword-reg/mem
472 :prefilter #'prefilter-reg/mem
473 :printer #'print-sized-dword-reg/mem)
475 ;;; Same as sized-reg/mem, but with a default operand size of :qword.
476 (sb!disassem:define-arg-type sized-reg/mem-default-qword
477 :prefilter #'prefilter-reg/mem
478 :printer #'print-sized-reg/mem-default-qword)
481 (sb!disassem:define-arg-type xmmreg
482 :prefilter #'prefilter-reg-r
483 :printer #'print-xmmreg)
485 (sb!disassem:define-arg-type xmmreg/mem
486 :prefilter #'prefilter-reg/mem
487 :printer #'print-xmmreg/mem)
489 (sb!disassem:define-arg-type sized-xmmreg/mem
490 :prefilter #'prefilter-reg/mem
491 :printer #'print-sized-xmmreg/mem)
494 (eval-when (:compile-toplevel :load-toplevel :execute)
495 (defparameter *conditions*
498 (:b . 2) (:nae . 2) (:c . 2)
499 (:nb . 3) (:ae . 3) (:nc . 3)
500 (:eq . 4) (:e . 4) (:z . 4)
507 (:np . 11) (:po . 11)
508 (:l . 12) (:nge . 12)
509 (:nl . 13) (:ge . 13)
510 (:le . 14) (:ng . 14)
511 (:nle . 15) (:g . 15)))
512 (defparameter *condition-name-vec*
513 (let ((vec (make-array 16 :initial-element nil)))
514 (dolist (cond *conditions*)
515 (when (null (aref vec (cdr cond)))
516 (setf (aref vec (cdr cond)) (car cond))))
520 ;;; Set assembler parameters. (In CMU CL, this was done with
521 ;;; a call to a macro DEF-ASSEMBLER-PARAMS.)
522 (eval-when (:compile-toplevel :load-toplevel :execute)
523 (setf sb!assem:*assem-scheduler-p* nil))
525 (sb!disassem:define-arg-type condition-code
526 :printer *condition-name-vec*)
528 (defun conditional-opcode (condition)
529 (cdr (assoc condition *conditions* :test #'eq)))
531 ;;;; disassembler instruction formats
533 (eval-when (:compile-toplevel :execute)
534 (defun swap-if (direction field1 separator field2)
535 `(:if (,direction :constant 0)
536 (,field1 ,separator ,field2)
537 (,field2 ,separator ,field1))))
539 (sb!disassem:define-instruction-format (byte 8 :default-printer '(:name))
540 (op :field (byte 8 0))
545 ;;; A one-byte instruction with a #x66 prefix, used to indicate an
546 ;;; operand size of :word.
547 (sb!disassem:define-instruction-format (x66-byte 16
548 :default-printer '(:name))
549 (x66 :field (byte 8 0) :value #x66)
550 (op :field (byte 8 8)))
552 ;;; A one-byte instruction with a REX prefix, used to indicate an
553 ;;; operand size of :qword. REX.W must be 1, the other three bits are
555 (sb!disassem:define-instruction-format (rex-byte 16
556 :default-printer '(:name))
557 (rex :field (byte 5 3) :value #b01001)
558 (op :field (byte 8 8)))
560 (sb!disassem:define-instruction-format (simple 8)
561 (op :field (byte 7 1))
562 (width :field (byte 1 0) :type 'width)
567 (sb!disassem:define-instruction-format (rex-simple 16)
568 (rex :field (byte 4 4) :value #b0100)
569 (wrxb :field (byte 4 0) :type 'wrxb)
570 (op :field (byte 7 9))
571 (width :field (byte 1 8) :type 'width)
576 ;;; Same as simple, but with direction bit
577 (sb!disassem:define-instruction-format (simple-dir 8 :include 'simple)
578 (op :field (byte 6 2))
579 (dir :field (byte 1 1)))
581 ;;; Same as simple, but with the immediate value occurring by default,
582 ;;; and with an appropiate printer.
583 (sb!disassem:define-instruction-format (accum-imm 8
585 :default-printer '(:name
586 :tab accum ", " imm))
587 (imm :type 'signed-imm-data))
589 (sb!disassem:define-instruction-format (rex-accum-imm 16
591 :default-printer '(:name
592 :tab accum ", " imm))
593 (imm :type 'signed-imm-data))
595 (sb!disassem:define-instruction-format (reg-no-width 8
596 :default-printer '(:name :tab reg))
597 (op :field (byte 5 3))
598 (reg :field (byte 3 0) :type 'reg-b)
603 (sb!disassem:define-instruction-format (rex-reg-no-width 16
604 :default-printer '(:name :tab reg))
605 (rex :field (byte 4 4) :value #b0100)
606 (wrxb :field (byte 4 0) :type 'wrxb)
607 (op :field (byte 5 11))
608 (reg :field (byte 3 8) :type 'reg-b)
613 ;;; Same as reg-no-width, but with a default operand size of :qword.
614 (sb!disassem:define-instruction-format (reg-no-width-default-qword 8
615 :include 'reg-no-width
616 :default-printer '(:name :tab reg))
617 (reg :type 'reg-b-default-qword))
619 ;;; Same as rex-reg-no-width, but with a default operand size of :qword.
620 (sb!disassem:define-instruction-format (rex-reg-no-width-default-qword 16
621 :include 'rex-reg-no-width
622 :default-printer '(:name :tab reg))
623 (reg :type 'reg-b-default-qword))
625 ;;; Adds a width field to reg-no-width. Note that we can't use
626 ;;; :INCLUDE 'REG-NO-WIDTH here to save typing because that would put
627 ;;; the WIDTH field last, but the prefilter for WIDTH must run before
628 ;;; the one for IMM to be able to determine the correct size of IMM.
629 (sb!disassem:define-instruction-format (reg 8
630 :default-printer '(:name :tab reg))
631 (op :field (byte 4 4))
632 (width :field (byte 1 3) :type 'width)
633 (reg :field (byte 3 0) :type 'reg-b)
638 (sb!disassem:define-instruction-format (rex-reg 16
639 :default-printer '(:name :tab reg))
640 (rex :field (byte 4 4) :value #b0100)
641 (wrxb :field (byte 4 0) :type 'wrxb)
642 (width :field (byte 1 11) :type 'width)
643 (op :field (byte 4 12))
644 (reg :field (byte 3 8) :type 'reg-b)
649 (sb!disassem:define-instruction-format (two-bytes 16
650 :default-printer '(:name))
651 (op :fields (list (byte 8 0) (byte 8 8))))
653 (sb!disassem:define-instruction-format (reg-reg/mem 16
655 `(:name :tab reg ", " reg/mem))
656 (op :field (byte 7 1))
657 (width :field (byte 1 0) :type 'width)
658 (reg/mem :fields (list (byte 2 14) (byte 3 8))
660 (reg :field (byte 3 11) :type 'reg)
664 (sb!disassem:define-instruction-format (rex-reg-reg/mem 24
666 `(:name :tab reg ", " reg/mem))
667 (rex :field (byte 4 4) :value #b0100)
668 (wrxb :field (byte 4 0) :type 'wrxb)
669 (width :field (byte 1 8) :type 'width)
670 (op :field (byte 7 9))
671 (reg/mem :fields (list (byte 2 22) (byte 3 16))
673 (reg :field (byte 3 19) :type 'reg)
677 ;;; same as reg-reg/mem, but with direction bit
678 (sb!disassem:define-instruction-format (reg-reg/mem-dir 16
679 :include 'reg-reg/mem
683 ,(swap-if 'dir 'reg/mem ", " 'reg)))
684 (op :field (byte 6 2))
685 (dir :field (byte 1 1)))
687 (sb!disassem:define-instruction-format (rex-reg-reg/mem-dir 24
688 :include 'rex-reg-reg/mem
692 ,(swap-if 'dir 'reg/mem ", " 'reg)))
693 (op :field (byte 6 10))
694 (dir :field (byte 1 9)))
696 (sb!disassem:define-instruction-format (x66-reg-reg/mem-dir 24
700 ,(swap-if 'dir 'reg/mem ", " 'reg)))
701 (x66 :field (byte 8 0) :type 'x66 :value #x66)
702 (op :field (byte 6 10))
703 (dir :field (byte 1 9))
704 (width :field (byte 1 8) :type 'width)
705 (reg/mem :fields (list (byte 2 22) (byte 3 16))
707 (reg :field (byte 3 19) :type 'reg))
709 (sb!disassem:define-instruction-format (x66-rex-reg-reg/mem-dir 32
713 ,(swap-if 'dir 'reg/mem ", " 'reg)))
714 (x66 :field (byte 8 0) :type 'x66 :value #x66)
715 (rex :field (byte 4 12) :value #b0100)
716 (wrxb :field (byte 4 8) :type 'wrxb)
717 (op :field (byte 6 18))
718 (dir :field (byte 1 17))
719 (width :field (byte 1 16) :type 'width)
720 (reg/mem :fields (list (byte 2 30) (byte 3 24))
722 (reg :field (byte 3 27) :type 'reg))
724 ;;; Same as reg-reg/mem, but uses the reg field as a second op code.
725 (sb!disassem:define-instruction-format (reg/mem 16
726 :default-printer '(:name :tab reg/mem))
727 (op :fields (list (byte 7 1) (byte 3 11)))
728 (width :field (byte 1 0) :type 'width)
729 (reg/mem :fields (list (byte 2 14) (byte 3 8))
730 :type 'sized-reg/mem)
734 (sb!disassem:define-instruction-format (rex-reg/mem 24
735 :default-printer '(:name :tab reg/mem))
736 (rex :field (byte 4 4) :value #b0100)
737 (wrxb :field (byte 4 0) :type 'wrxb)
738 (op :fields (list (byte 7 9) (byte 3 19)))
739 (width :field (byte 1 8) :type 'width)
740 (reg/mem :fields (list (byte 2 22) (byte 3 16))
741 :type 'sized-reg/mem)
745 ;;; Same as reg/mem, but without a width field and with a default
746 ;;; operand size of :qword.
747 (sb!disassem:define-instruction-format (reg/mem-default-qword 16
748 :default-printer '(:name :tab reg/mem))
749 (op :fields (list (byte 8 0) (byte 3 11)))
750 (reg/mem :fields (list (byte 2 14) (byte 3 8))
751 :type 'sized-reg/mem-default-qword))
753 (sb!disassem:define-instruction-format (rex-reg/mem-default-qword 24
754 :default-printer '(:name :tab reg/mem))
755 (rex :field (byte 4 4) :value #b0100)
756 (wrxb :field (byte 4 0) :type 'wrxb)
757 (op :fields (list (byte 8 8) (byte 3 19)))
758 (reg/mem :fields (list (byte 2 22) (byte 3 16))
759 :type 'sized-reg/mem-default-qword))
761 ;;; Same as reg/mem, but with the immediate value occurring by default,
762 ;;; and with an appropiate printer.
763 (sb!disassem:define-instruction-format (reg/mem-imm 16
766 '(:name :tab reg/mem ", " imm))
767 (reg/mem :type 'sized-reg/mem)
768 (imm :type 'signed-imm-data))
770 (sb!disassem:define-instruction-format (rex-reg/mem-imm 24
771 :include 'rex-reg/mem
773 '(:name :tab reg/mem ", " imm))
774 (reg/mem :type 'sized-reg/mem)
775 (imm :type 'signed-imm-data))
777 ;;; Same as reg/mem, but with using the accumulator in the default printer
778 (sb!disassem:define-instruction-format
780 :include 'reg/mem :default-printer '(:name :tab accum ", " reg/mem))
781 (reg/mem :type 'reg/mem) ; don't need a size
782 (accum :type 'accum))
784 (sb!disassem:define-instruction-format (rex-accum-reg/mem 24
785 :include 'rex-reg/mem
787 '(:name :tab accum ", " reg/mem))
788 (reg/mem :type 'reg/mem) ; don't need a size
789 (accum :type 'accum))
791 ;;; Same as reg-reg/mem, but with a prefix of #b00001111
792 (sb!disassem:define-instruction-format (ext-reg-reg/mem 24
794 `(:name :tab reg ", " reg/mem))
795 (prefix :field (byte 8 0) :value #b00001111)
796 (op :field (byte 7 9))
797 (width :field (byte 1 8) :type 'width)
798 (reg/mem :fields (list (byte 2 22) (byte 3 16))
800 (reg :field (byte 3 19) :type 'reg)
804 (sb!disassem:define-instruction-format (ext-reg-reg/mem-no-width 24
806 `(:name :tab reg ", " reg/mem))
807 (prefix :field (byte 8 0) :value #b00001111)
808 (op :field (byte 8 8))
809 (reg/mem :fields (list (byte 2 22) (byte 3 16))
811 (reg :field (byte 3 19) :type 'reg))
813 (sb!disassem:define-instruction-format (rex-ext-reg-reg/mem-no-width 32
815 `(:name :tab reg ", " reg/mem))
816 (rex :field (byte 4 4) :value #b0100)
817 (wrxb :field (byte 4 0) :type 'wrxb)
818 (prefix :field (byte 8 8) :value #b00001111)
819 (op :field (byte 8 16))
820 (reg/mem :fields (list (byte 2 30) (byte 3 24))
822 (reg :field (byte 3 27) :type 'reg))
824 ;;; reg-no-width with #x0f prefix
825 (sb!disassem:define-instruction-format (ext-reg-no-width 16
826 :default-printer '(:name :tab reg))
827 (prefix :field (byte 8 0) :value #b00001111)
828 (op :field (byte 5 11))
829 (reg :field (byte 3 8) :type 'reg-b))
831 ;;; Same as reg/mem, but with a prefix of #b00001111
832 (sb!disassem:define-instruction-format (ext-reg/mem 24
833 :default-printer '(:name :tab reg/mem))
834 (prefix :field (byte 8 0) :value #b00001111)
835 (op :fields (list (byte 7 9) (byte 3 19)))
836 (width :field (byte 1 8) :type 'width)
837 (reg/mem :fields (list (byte 2 22) (byte 3 16))
838 :type 'sized-reg/mem)
842 (sb!disassem:define-instruction-format (ext-reg/mem-imm 24
843 :include 'ext-reg/mem
845 '(:name :tab reg/mem ", " imm))
846 (imm :type 'signed-imm-data))
848 ;;;; XMM instructions
850 ;;; All XMM instructions use an extended opcode (#x0F as the first
851 ;;; opcode byte). Therefore in the following "EXT" in the name of the
852 ;;; instruction formats refers to the formats that have an additional
853 ;;; prefix (#x66, #xF2 or #xF3).
855 ;;; Instructions having an XMM register as the destination operand
856 ;;; and an XMM register or a memory location as the source operand.
857 ;;; The size of the operands is implicitly given by the instruction.
858 (sb!disassem:define-instruction-format (xmm-xmm/mem 24
860 '(:name :tab reg ", " reg/mem))
861 (x0f :field (byte 8 0) :value #x0f)
862 (op :field (byte 8 8))
863 (reg/mem :fields (list (byte 2 22) (byte 3 16))
865 (reg :field (byte 3 19) :type 'xmmreg))
867 (sb!disassem:define-instruction-format (rex-xmm-xmm/mem 32
869 '(:name :tab reg ", " reg/mem))
870 (x0f :field (byte 8 0) :value #x0f)
871 (rex :field (byte 4 12) :value #b0100)
872 (wrxb :field (byte 4 8) :type 'wrxb)
873 (op :field (byte 8 16))
874 (reg/mem :fields (list (byte 2 30) (byte 3 24))
876 (reg :field (byte 3 27) :type 'xmmreg))
878 (sb!disassem:define-instruction-format (ext-xmm-xmm/mem 32
880 '(:name :tab reg ", " reg/mem))
881 (prefix :field (byte 8 0))
882 (x0f :field (byte 8 8) :value #x0f)
883 (op :field (byte 8 16))
884 (reg/mem :fields (list (byte 2 30) (byte 3 24))
886 (reg :field (byte 3 27) :type 'xmmreg))
888 (sb!disassem:define-instruction-format (ext-rex-xmm-xmm/mem 40
890 '(:name :tab reg ", " reg/mem))
891 (prefix :field (byte 8 0))
892 (rex :field (byte 4 12) :value #b0100)
893 (wrxb :field (byte 4 8) :type 'wrxb)
894 (x0f :field (byte 8 16) :value #x0f)
895 (op :field (byte 8 24))
896 (reg/mem :fields (list (byte 2 38) (byte 3 32))
898 (reg :field (byte 3 35) :type 'xmmreg))
900 ;;; Same as xmm-xmm/mem etc., but with direction bit.
902 (sb!disassem:define-instruction-format (ext-xmm-xmm/mem-dir 32
903 :include 'ext-xmm-xmm/mem
907 ,(swap-if 'dir 'reg ", " 'reg/mem)))
908 (op :field (byte 7 17))
909 (dir :field (byte 1 16)))
911 (sb!disassem:define-instruction-format (ext-rex-xmm-xmm/mem-dir 40
912 :include 'ext-rex-xmm-xmm/mem
916 ,(swap-if 'dir 'reg ", " 'reg/mem)))
917 (op :field (byte 7 25))
918 (dir :field (byte 1 24)))
920 ;;; Instructions having an XMM register as one operand and a general-
921 ;;; -purpose register or a memory location as the other operand.
923 (sb!disassem:define-instruction-format (ext-xmm-reg/mem 32
925 '(:name :tab reg ", " reg/mem))
926 (prefix :field (byte 8 0))
927 (x0f :field (byte 8 8) :value #x0f)
928 (op :field (byte 8 16))
929 (reg/mem :fields (list (byte 2 30) (byte 3 24))
930 :type 'sized-reg/mem)
931 (reg :field (byte 3 27) :type 'xmmreg))
933 (sb!disassem:define-instruction-format (ext-rex-xmm-reg/mem 40
935 '(:name :tab reg ", " reg/mem))
936 (prefix :field (byte 8 0))
937 (rex :field (byte 4 12) :value #b0100)
938 (wrxb :field (byte 4 8) :type 'wrxb)
939 (x0f :field (byte 8 16) :value #x0f)
940 (op :field (byte 8 24))
941 (reg/mem :fields (list (byte 2 38) (byte 3 32))
942 :type 'sized-reg/mem)
943 (reg :field (byte 3 35) :type 'xmmreg))
945 ;;; Instructions having a general-purpose register as one operand and an
946 ;;; XMM register or a memory location as the other operand.
948 (sb!disassem:define-instruction-format (ext-reg-xmm/mem 32
950 '(:name :tab reg ", " reg/mem))
951 (prefix :field (byte 8 0))
952 (x0f :field (byte 8 8) :value #x0f)
953 (op :field (byte 8 16))
954 (reg/mem :fields (list (byte 2 30) (byte 3 24))
955 :type 'sized-xmmreg/mem)
956 (reg :field (byte 3 27) :type 'reg))
958 (sb!disassem:define-instruction-format (ext-rex-reg-xmm/mem 40
960 '(:name :tab reg ", " reg/mem))
961 (prefix :field (byte 8 0))
962 (rex :field (byte 4 12) :value #b0100)
963 (wrxb :field (byte 4 8) :type 'wrxb)
964 (x0f :field (byte 8 16) :value #x0f)
965 (op :field (byte 8 24))
966 (reg/mem :fields (list (byte 2 38) (byte 3 32))
967 :type 'sized-xmmreg/mem)
968 (reg :field (byte 3 35) :type 'reg))
970 (sb!disassem:define-instruction-format (string-op 8
972 :default-printer '(:name width)))
974 (sb!disassem:define-instruction-format (rex-string-op 16
976 :default-printer '(:name width)))
978 (sb!disassem:define-instruction-format (short-cond-jump 16)
979 (op :field (byte 4 4))
980 (cc :field (byte 4 0) :type 'condition-code)
981 (label :field (byte 8 8) :type 'displacement))
983 (sb!disassem:define-instruction-format (short-jump 16
984 :default-printer '(:name :tab label))
985 (const :field (byte 4 4) :value #b1110)
986 (op :field (byte 4 0))
987 (label :field (byte 8 8) :type 'displacement))
989 (sb!disassem:define-instruction-format (near-cond-jump 16)
990 (op :fields (list (byte 8 0) (byte 4 12)) :value '(#b00001111 #b1000))
991 (cc :field (byte 4 8) :type 'condition-code)
992 ;; The disassembler currently doesn't let you have an instruction > 32 bits
993 ;; long, so we fake it by using a prefilter to read the offset.
994 (label :type 'displacement
995 :prefilter (lambda (value dstate)
996 (declare (ignore value)) ; always nil anyway
997 (sb!disassem:read-signed-suffix 32 dstate))))
999 (sb!disassem:define-instruction-format (near-jump 8
1000 :default-printer '(:name :tab label))
1001 (op :field (byte 8 0))
1002 ;; The disassembler currently doesn't let you have an instruction > 32 bits
1003 ;; long, so we fake it by using a prefilter to read the address.
1004 (label :type 'displacement
1005 :prefilter (lambda (value dstate)
1006 (declare (ignore value)) ; always nil anyway
1007 (sb!disassem:read-signed-suffix 32 dstate))))
1010 (sb!disassem:define-instruction-format (cond-set 24
1011 :default-printer '('set cc :tab reg/mem))
1012 (prefix :field (byte 8 0) :value #b00001111)
1013 (op :field (byte 4 12) :value #b1001)
1014 (cc :field (byte 4 8) :type 'condition-code)
1015 (reg/mem :fields (list (byte 2 22) (byte 3 16))
1016 :type 'sized-byte-reg/mem)
1017 (reg :field (byte 3 19) :value #b000))
1019 (sb!disassem:define-instruction-format (cond-move 24
1021 '('cmov cc :tab reg ", " reg/mem))
1022 (prefix :field (byte 8 0) :value #b00001111)
1023 (op :field (byte 4 12) :value #b0100)
1024 (cc :field (byte 4 8) :type 'condition-code)
1025 (reg/mem :fields (list (byte 2 22) (byte 3 16))
1027 (reg :field (byte 3 19) :type 'reg))
1029 (sb!disassem:define-instruction-format (rex-cond-move 32
1031 '('cmov cc :tab reg ", " reg/mem))
1032 (rex :field (byte 4 4) :value #b0100)
1033 (wrxb :field (byte 4 0) :type 'wrxb)
1034 (prefix :field (byte 8 8) :value #b00001111)
1035 (op :field (byte 4 20) :value #b0100)
1036 (cc :field (byte 4 16) :type 'condition-code)
1037 (reg/mem :fields (list (byte 2 30) (byte 3 24))
1039 (reg :field (byte 3 27) :type 'reg))
1041 (sb!disassem:define-instruction-format (enter-format 32
1042 :default-printer '(:name
1044 (:unless (:constant 0)
1046 (op :field (byte 8 0))
1047 (disp :field (byte 16 8))
1048 (level :field (byte 8 24)))
1050 ;;; Single byte instruction with an immediate byte argument.
1051 (sb!disassem:define-instruction-format (byte-imm 16
1052 :default-printer '(:name :tab code))
1053 (op :field (byte 8 0))
1054 (code :field (byte 8 8)))
1056 ;;;; primitive emitters
1058 (define-bitfield-emitter emit-word 16
1061 (define-bitfield-emitter emit-dword 32
1064 ;;; Most uses of dwords are as displacements or as immediate values in
1065 ;;; 64-bit operations. In these cases they are sign-extended to 64 bits.
1066 ;;; EMIT-DWORD is unsuitable there because it accepts values of type
1067 ;;; (OR (SIGNED-BYTE 32) (UNSIGNED-BYTE 32)), so we provide a more
1068 ;;; restricted emitter here.
1069 (defun emit-signed-dword (segment value)
1070 (declare (type segment segment)
1071 (type (signed-byte 32) value))
1072 (declare (inline emit-dword))
1073 (emit-dword segment value))
1075 (define-bitfield-emitter emit-qword 64
1078 (define-bitfield-emitter emit-byte-with-reg 8
1079 (byte 5 3) (byte 3 0))
1081 (define-bitfield-emitter emit-mod-reg-r/m-byte 8
1082 (byte 2 6) (byte 3 3) (byte 3 0))
1084 (define-bitfield-emitter emit-sib-byte 8
1085 (byte 2 6) (byte 3 3) (byte 3 0))
1087 (define-bitfield-emitter emit-rex-byte 8
1088 (byte 4 4) (byte 1 3) (byte 1 2) (byte 1 1) (byte 1 0))
1094 (defun emit-absolute-fixup (segment fixup &optional quad-p)
1095 (note-fixup segment (if quad-p :absolute64 :absolute) fixup)
1096 (let ((offset (fixup-offset fixup)))
1097 (if (label-p offset)
1098 (emit-back-patch segment
1100 (lambda (segment posn)
1101 (declare (ignore posn))
1102 (let ((val (- (+ (component-header-length)
1103 (or (label-position offset)
1105 other-pointer-lowtag)))
1107 (emit-qword segment val)
1108 (emit-signed-dword segment val)))))
1110 (emit-qword segment (or offset 0))
1111 (emit-signed-dword segment (or offset 0))))))
1113 (defun emit-relative-fixup (segment fixup)
1114 (note-fixup segment :relative fixup)
1115 (emit-signed-dword segment (or (fixup-offset fixup) 0)))
1118 ;;;; the effective-address (ea) structure
1120 (defun reg-tn-encoding (tn)
1121 (declare (type tn tn))
1122 ;; ea only has space for three bits of register number: regs r8
1123 ;; and up are selected by a REX prefix byte which caller is responsible
1124 ;; for having emitted where necessary already
1125 (ecase (sb-name (sc-sb (tn-sc tn)))
1127 (let ((offset (mod (tn-offset tn) 16)))
1128 (logior (ash (logand offset 1) 2)
1131 (mod (tn-offset tn) 8))))
1133 (defstruct (ea (:constructor make-ea (size &key base index scale disp))
1135 ;; note that we can represent an EA with a QWORD size, but EMIT-EA
1136 ;; can't actually emit it on its own: caller also needs to emit REX
1138 (size nil :type (member :byte :word :dword :qword))
1139 (base nil :type (or tn null))
1140 (index nil :type (or tn null))
1141 (scale 1 :type (member 1 2 4 8))
1142 (disp 0 :type (or (unsigned-byte 32) (signed-byte 32) fixup)))
1143 (def!method print-object ((ea ea) stream)
1144 (cond ((or *print-escape* *print-readably*)
1145 (print-unreadable-object (ea stream :type t)
1147 "~S~@[ base=~S~]~@[ index=~S~]~@[ scale=~S~]~@[ disp=~S~]"
1151 (let ((scale (ea-scale ea)))
1152 (if (= scale 1) nil scale))
1155 (format stream "~A PTR [" (symbol-name (ea-size ea)))
1157 (write-string (sb!c::location-print-name (ea-base ea)) stream)
1159 (write-string "+" stream)))
1161 (write-string (sb!c::location-print-name (ea-index ea)) stream))
1162 (unless (= (ea-scale ea) 1)
1163 (format stream "*~A" (ea-scale ea)))
1164 (typecase (ea-disp ea)
1167 (format stream "~@D" (ea-disp ea)))
1169 (format stream "+~A" (ea-disp ea))))
1170 (write-char #\] stream))))
1172 (defun emit-constant-tn-rip (segment constant-tn reg)
1173 ;; AMD64 doesn't currently have a code object register to use as a
1174 ;; base register for constant access. Instead we use RIP-relative
1175 ;; addressing. The offset from the SIMPLE-FUN-HEADER to the instruction
1176 ;; is passed to the backpatch callback. In addition we need the offset
1177 ;; from the start of the function header to the slot in the CODE-HEADER
1178 ;; that stores the constant. Since we don't know where the code header
1179 ;; starts, instead count backwards from the function header.
1180 (let* ((2comp (component-info *component-being-compiled*))
1181 (constants (ir2-component-constants 2comp))
1182 (len (length constants))
1183 ;; Both CODE-HEADER and SIMPLE-FUN-HEADER are 16-byte aligned.
1184 ;; If there are an even amount of constants, there will be
1185 ;; an extra qword of padding before the function header, which
1186 ;; needs to be adjusted for. XXX: This will break if new slots
1187 ;; are added to the code header.
1188 (offset (* (- (+ len (if (evenp len)
1191 (tn-offset constant-tn))
1193 ;; RIP-relative addressing
1194 (emit-mod-reg-r/m-byte segment #b00 reg #b101)
1195 (emit-back-patch segment
1197 (lambda (segment posn)
1198 ;; The addressing is relative to end of instruction,
1199 ;; i.e. the end of this dword. Hence the + 4.
1200 (emit-signed-dword segment
1201 (+ 4 (- (+ offset posn)))))))
1204 (defun emit-label-rip (segment fixup reg)
1205 (let ((label (fixup-offset fixup)))
1206 ;; RIP-relative addressing
1207 (emit-mod-reg-r/m-byte segment #b00 reg #b101)
1208 (emit-back-patch segment
1210 (lambda (segment posn)
1211 (emit-signed-dword segment (- (label-position label)
1215 (defun emit-ea (segment thing reg &optional allow-constants)
1218 ;; this would be eleganter if we had a function that would create
1220 (ecase (sb-name (sc-sb (tn-sc thing)))
1221 ((registers float-registers)
1222 (emit-mod-reg-r/m-byte segment #b11 reg (reg-tn-encoding thing)))
1224 ;; Convert stack tns into an index off RBP.
1225 (let ((disp (- (* (1+ (tn-offset thing)) n-word-bytes))))
1226 (cond ((<= -128 disp 127)
1227 (emit-mod-reg-r/m-byte segment #b01 reg #b101)
1228 (emit-byte segment disp))
1230 (emit-mod-reg-r/m-byte segment #b10 reg #b101)
1231 (emit-signed-dword segment disp)))))
1233 (unless allow-constants
1236 "Constant TNs can only be directly used in MOV, PUSH, and CMP."))
1237 (emit-constant-tn-rip segment thing reg))))
1239 (let* ((base (ea-base thing))
1240 (index (ea-index thing))
1241 (scale (ea-scale thing))
1242 (disp (ea-disp thing))
1243 (mod (cond ((or (null base)
1245 (not (= (reg-tn-encoding base) #b101))))
1247 ((and (fixnump disp) (<= -128 disp 127))
1251 (r/m (cond (index #b100)
1253 (t (reg-tn-encoding base)))))
1254 (when (and (= mod 0) (= r/m #b101))
1255 ;; this is rip-relative in amd64, so we'll use a sib instead
1256 (setf r/m #b100 scale 1))
1257 (emit-mod-reg-r/m-byte segment mod reg r/m)
1259 (let ((ss (1- (integer-length scale)))
1260 (index (if (null index)
1262 (let ((index (reg-tn-encoding index)))
1264 (error "can't index off of ESP")
1266 (base (if (null base)
1268 (reg-tn-encoding base))))
1269 (emit-sib-byte segment ss index base)))
1271 (emit-byte segment disp))
1272 ((or (= mod #b10) (null base))
1274 (emit-absolute-fixup segment disp)
1275 (emit-signed-dword segment disp))))))
1277 (typecase (fixup-offset thing)
1279 (emit-label-rip segment thing reg))
1281 (emit-mod-reg-r/m-byte segment #b00 reg #b100)
1282 (emit-sib-byte segment 0 #b100 #b101)
1283 (emit-absolute-fixup segment thing))))))
1285 (defun byte-reg-p (thing)
1287 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)
1288 (member (sc-name (tn-sc thing)) *byte-sc-names*)
1291 (defun byte-ea-p (thing)
1293 (ea (eq (ea-size thing) :byte))
1295 (and (member (sc-name (tn-sc thing)) *byte-sc-names*) t))
1298 (defun word-reg-p (thing)
1300 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)
1301 (member (sc-name (tn-sc thing)) *word-sc-names*)
1304 (defun word-ea-p (thing)
1306 (ea (eq (ea-size thing) :word))
1307 (tn (and (member (sc-name (tn-sc thing)) *word-sc-names*) t))
1310 (defun dword-reg-p (thing)
1312 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)
1313 (member (sc-name (tn-sc thing)) *dword-sc-names*)
1316 (defun dword-ea-p (thing)
1318 (ea (eq (ea-size thing) :dword))
1320 (and (member (sc-name (tn-sc thing)) *dword-sc-names*) t))
1323 (defun qword-reg-p (thing)
1325 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)
1326 (member (sc-name (tn-sc thing)) *qword-sc-names*)
1329 (defun qword-ea-p (thing)
1331 (ea (eq (ea-size thing) :qword))
1333 (and (member (sc-name (tn-sc thing)) *qword-sc-names*) t))
1336 ;;; Return true if THING is a general-purpose register TN.
1337 (defun register-p (thing)
1339 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)))
1341 (defun accumulator-p (thing)
1342 (and (register-p thing)
1343 (= (tn-offset thing) 0)))
1345 ;;; Return true if THING is an XMM register TN.
1346 (defun xmm-register-p (thing)
1348 (eq (sb-name (sc-sb (tn-sc thing))) 'float-registers)))
1353 (def!constant +operand-size-prefix-byte+ #b01100110)
1355 (defun maybe-emit-operand-size-prefix (segment size)
1356 (unless (or (eq size :byte)
1357 (eq size :qword) ; REX prefix handles this
1358 (eq size +default-operand-size+))
1359 (emit-byte segment +operand-size-prefix-byte+)))
1361 ;;; A REX prefix must be emitted if at least one of the following
1362 ;;; conditions is true:
1363 ;; 1. The operand size is :QWORD and the default operand size of the
1364 ;; instruction is not :QWORD.
1365 ;;; 2. The instruction references an extended register.
1366 ;;; 3. The instruction references one of the byte registers SIL, DIL,
1369 ;;; Emit a REX prefix if necessary. OPERAND-SIZE is used to determine
1370 ;;; whether to set REX.W. Callers pass it explicitly as :DO-NOT-SET if
1371 ;;; this should not happen, for example because the instruction's
1372 ;;; default operand size is qword. R, X and B are NIL or TNs specifying
1373 ;;; registers the encodings of which are extended with the REX.R, REX.X
1374 ;;; and REX.B bit, respectively. To determine whether one of the byte
1375 ;;; registers is used that can only be accessed using a REX prefix, we
1376 ;;; need only to test R and B, because X is only used for the index
1377 ;;; register of an effective address and therefore never byte-sized.
1378 ;;; For R we can avoid to calculate the size of the TN because it is
1379 ;;; always OPERAND-SIZE. The size of B must be calculated here because
1380 ;;; B can be address-sized (if it is the base register of an effective
1381 ;;; address), of OPERAND-SIZE (if the instruction operates on two
1382 ;;; registers) or of some different size (in the instructions that
1383 ;;; combine arguments of different sizes: MOVZX, MOVSX, MOVSXD and
1384 ;;; several SSE instructions, e.g. CVTSD2SI). We don't distinguish
1385 ;;; between general-purpose and floating point registers for this cause
1386 ;;; because only general-purpose registers can be byte-sized at all.
1387 (defun maybe-emit-rex-prefix (segment operand-size r x b)
1388 (declare (type (member nil :byte :word :dword :qword :do-not-set)
1390 (type (or null tn) r x b))
1392 (if (and r (> (tn-offset r)
1393 ;; offset of r8 is 16, offset of xmm8 is 8
1394 (if (eq (sb-name (sc-sb (tn-sc r)))
1401 ;; Assuming R is a TN describing a general-purpose
1402 ;; register, return true if it references register
1404 (<= 8 (tn-offset r) 15)))
1405 (let ((rex-w (if (eq operand-size :qword) 1 0))
1409 (when (or (not (zerop (logior rex-w rex-r rex-x rex-b)))
1411 (eq operand-size :byte)
1414 (eq (operand-size b) :byte)
1416 (emit-rex-byte segment #b0100 rex-w rex-r rex-x rex-b)))))
1418 ;;; Emit a REX prefix if necessary. The operand size is determined from
1419 ;;; THING or can be overwritten by OPERAND-SIZE. This and REG are always
1420 ;;; passed to MAYBE-EMIT-REX-PREFIX. Additionally, if THING is an EA we
1421 ;;; pass its index and base registers, if it is a register TN, we pass
1423 ;;; In contrast to EMIT-EA above, neither stack TNs nor fixups need to
1424 ;;; be treated specially here: If THING is a stack TN, neither it nor
1425 ;;; any of its components are passed to MAYBE-EMIT-REX-PREFIX which
1426 ;;; works correctly because stack references always use RBP as the base
1427 ;;; register and never use an index register so no extended registers
1428 ;;; need to be accessed. Fixups are assembled using an addressing mode
1429 ;;; of displacement-only or RIP-plus-displacement (see EMIT-EA), so may
1430 ;;; not reference an extended register. The displacement-only addressing
1431 ;;; mode requires that REX.X is 0, which is ensured here.
1432 (defun maybe-emit-rex-for-ea (segment thing reg &key operand-size)
1433 (declare (type (or ea tn fixup) thing)
1434 (type (or null tn) reg)
1435 (type (member nil :byte :word :dword :qword :do-not-set)
1437 (let ((ea-p (ea-p thing)))
1438 (maybe-emit-rex-prefix segment
1439 (or operand-size (operand-size thing))
1441 (and ea-p (ea-index thing))
1442 (cond (ea-p (ea-base thing))
1444 (member (sb-name (sc-sb (tn-sc thing)))
1445 '(float-registers registers)))
1449 (defun operand-size (thing)
1452 ;; FIXME: might as well be COND instead of having to use #. readmacro
1453 ;; to hack up the code
1454 (case (sc-name (tn-sc thing))
1463 ;; added by jrd: float-registers is a separate size (?)
1464 ;; The only place in the code where we are called with THING
1465 ;; being a float-register is in MAYBE-EMIT-REX-PREFIX when it
1466 ;; checks whether THING is a byte register. Thus our result in
1467 ;; these cases could as well be :dword and :qword. I leave it as
1468 ;; :float and :double which is more likely to trigger an aver
1469 ;; instead of silently doing the wrong thing in case this
1470 ;; situation should change. Lutz Euler, 2005-10-23.
1473 (#.*double-sc-names*
1476 (error "can't tell the size of ~S ~S" thing (sc-name (tn-sc thing))))))
1480 ;; GNA. Guess who spelt "flavor" correctly first time round?
1481 ;; There's a strong argument in my mind to change all uses of
1482 ;; "flavor" to "kind": and similarly with some misguided uses of
1483 ;; "type" here and there. -- CSR, 2005-01-06.
1484 (case (fixup-flavor thing)
1485 ((:foreign-dataref) :qword)))
1489 (defun matching-operand-size (dst src)
1490 (let ((dst-size (operand-size dst))
1491 (src-size (operand-size src)))
1494 (if (eq dst-size src-size)
1496 (error "size mismatch: ~S is a ~S and ~S is a ~S."
1497 dst dst-size src src-size))
1501 (error "can't tell the size of either ~S or ~S" dst src)))))
1503 ;;; Except in a very few cases (MOV instructions A1, A3 and B8 - BF)
1504 ;;; we expect dword data bytes even when 64 bit work is being done.
1505 ;;; But A1 and A3 are currently unused and B8 - BF use EMIT-QWORD
1506 ;;; directly, so we emit all quad constants as dwords, additionally
1507 ;;; making sure that they survive the sign-extension to 64 bits
1509 (defun emit-sized-immediate (segment size value)
1512 (emit-byte segment value))
1514 (emit-word segment value))
1516 (emit-dword segment value))
1518 (emit-signed-dword segment value))))
1520 ;;;; general data transfer
1522 ;;; This is the part of the MOV instruction emitter that does moving
1523 ;;; of an immediate value into a qword register. We go to some length
1524 ;;; to achieve the shortest possible encoding.
1525 (defun emit-immediate-move-to-qword-register (segment dst src)
1526 (declare (type integer src))
1527 (cond ((typep src '(unsigned-byte 32))
1528 ;; We use the B8 - BF encoding with an operand size of 32 bits
1529 ;; here and let the implicit zero-extension fill the upper half
1530 ;; of the 64-bit destination register. Instruction size: five
1531 ;; or six bytes. (A REX prefix will be emitted only if the
1532 ;; destination is an extended register.)
1533 (maybe-emit-rex-prefix segment :dword nil nil dst)
1534 (emit-byte-with-reg segment #b10111 (reg-tn-encoding dst))
1535 (emit-dword segment src))
1537 (maybe-emit-rex-prefix segment :qword nil nil dst)
1538 (cond ((typep src '(signed-byte 32))
1539 ;; Use the C7 encoding that takes a 32-bit immediate and
1540 ;; sign-extends it to 64 bits. Instruction size: seven
1542 (emit-byte segment #b11000111)
1543 (emit-mod-reg-r/m-byte segment #b11 #b000
1544 (reg-tn-encoding dst))
1545 (emit-signed-dword segment src))
1546 ((<= (- (expt 2 64) (expt 2 31))
1549 ;; This triggers on positive integers of 64 bits length
1550 ;; with the most significant 33 bits being 1. We use the
1551 ;; same encoding as in the previous clause.
1552 (emit-byte segment #b11000111)
1553 (emit-mod-reg-r/m-byte segment #b11 #b000
1554 (reg-tn-encoding dst))
1555 (emit-signed-dword segment (- src (expt 2 64))))
1557 ;; We need a full 64-bit immediate. Instruction size:
1559 (emit-byte-with-reg segment #b10111 (reg-tn-encoding dst))
1560 (emit-qword segment src))))))
1562 (define-instruction mov (segment dst src)
1563 ;; immediate to register
1564 (:printer reg ((op #b1011) (imm nil :type 'signed-imm-data))
1565 '(:name :tab reg ", " imm))
1566 (:printer rex-reg ((op #b1011) (imm nil :type 'signed-imm-data-upto-qword))
1567 '(:name :tab reg ", " imm))
1568 ;; absolute mem to/from accumulator
1569 (:printer simple-dir ((op #b101000) (imm nil :type 'imm-addr))
1570 `(:name :tab ,(swap-if 'dir 'accum ", " '("[" imm "]"))))
1571 ;; register to/from register/memory
1572 (:printer reg-reg/mem-dir ((op #b100010)))
1573 (:printer rex-reg-reg/mem-dir ((op #b100010)))
1574 (:printer x66-reg-reg/mem-dir ((op #b100010)))
1575 (:printer x66-rex-reg-reg/mem-dir ((op #b100010)))
1576 ;; immediate to register/memory
1577 (:printer reg/mem-imm ((op '(#b1100011 #b000))))
1578 (:printer rex-reg/mem-imm ((op '(#b1100011 #b000))))
1581 (let ((size (matching-operand-size dst src)))
1582 (maybe-emit-operand-size-prefix segment size)
1583 (cond ((register-p dst)
1584 (cond ((integerp src)
1585 (cond ((eq size :qword)
1586 (emit-immediate-move-to-qword-register segment
1589 (maybe-emit-rex-prefix segment size nil nil dst)
1590 (emit-byte-with-reg segment
1594 (reg-tn-encoding dst))
1595 (emit-sized-immediate segment size src))))
1597 (maybe-emit-rex-for-ea segment src dst)
1602 (emit-ea segment src (reg-tn-encoding dst) t))))
1604 ;; C7 only deals with 32 bit immediates even if the
1605 ;; destination is a 64-bit location. The value is
1606 ;; sign-extended in this case.
1607 (maybe-emit-rex-for-ea segment dst nil)
1608 (emit-byte segment (if (eq size :byte) #b11000110 #b11000111))
1609 (emit-ea segment dst #b000)
1610 (emit-sized-immediate segment size src))
1612 (maybe-emit-rex-for-ea segment dst src)
1613 (emit-byte segment (if (eq size :byte) #b10001000 #b10001001))
1614 (emit-ea segment dst (reg-tn-encoding src)))
1616 ;; Generally we can't MOV a fixupped value into an EA, since
1617 ;; MOV on non-registers can only take a 32-bit immediate arg.
1618 ;; Make an exception for :FOREIGN fixups (pretty much just
1619 ;; the runtime asm, since other foreign calls go through the
1620 ;; the linkage table) and for linkage table references, since
1621 ;; these should always end up in low memory.
1622 (aver (or (eq (fixup-flavor src) :foreign)
1623 (eq (fixup-flavor src) :foreign-dataref)
1624 (eq (ea-size dst) :dword)))
1625 (maybe-emit-rex-for-ea segment dst nil)
1626 (emit-byte segment #b11000111)
1627 (emit-ea segment dst #b000)
1628 (emit-absolute-fixup segment src))
1630 (error "bogus arguments to MOV: ~S ~S" dst src))))))
1632 (defun emit-move-with-extension (segment dst src signed-p)
1633 (aver (register-p dst))
1634 (let ((dst-size (operand-size dst))
1635 (src-size (operand-size src))
1636 (opcode (if signed-p #b10111110 #b10110110)))
1639 (aver (eq src-size :byte))
1640 (maybe-emit-operand-size-prefix segment :word)
1641 ;; REX prefix is needed if SRC is SIL, DIL, SPL or BPL.
1642 (maybe-emit-rex-for-ea segment src dst :operand-size :word)
1643 (emit-byte segment #b00001111)
1644 (emit-byte segment opcode)
1645 (emit-ea segment src (reg-tn-encoding dst)))
1649 (maybe-emit-rex-for-ea segment src dst :operand-size dst-size)
1650 (emit-byte segment #b00001111)
1651 (emit-byte segment opcode)
1652 (emit-ea segment src (reg-tn-encoding dst)))
1654 (maybe-emit-rex-for-ea segment src dst :operand-size dst-size)
1655 (emit-byte segment #b00001111)
1656 (emit-byte segment (logior opcode 1))
1657 (emit-ea segment src (reg-tn-encoding dst)))
1659 (aver (eq dst-size :qword))
1660 ;; dst is in reg, src is in modrm
1661 (let ((ea-p (ea-p src)))
1662 (maybe-emit-rex-prefix segment (if signed-p :qword :dword) dst
1663 (and ea-p (ea-index src))
1664 (cond (ea-p (ea-base src))
1667 (emit-byte segment #x63) ;movsxd
1668 ;;(emit-byte segment opcode)
1669 (emit-ea segment src (reg-tn-encoding dst)))))))))
1671 (define-instruction movsx (segment dst src)
1672 (:printer ext-reg-reg/mem-no-width
1673 ((op #b10111110) (reg/mem nil :type 'sized-byte-reg/mem)))
1674 (:printer rex-ext-reg-reg/mem-no-width
1675 ((op #b10111110) (reg/mem nil :type 'sized-byte-reg/mem)))
1676 (:printer ext-reg-reg/mem-no-width
1677 ((op #b10111111) (reg/mem nil :type 'sized-word-reg/mem)))
1678 (:printer rex-ext-reg-reg/mem-no-width
1679 ((op #b10111111) (reg/mem nil :type 'sized-word-reg/mem)))
1680 (:emitter (emit-move-with-extension segment dst src :signed)))
1682 (define-instruction movzx (segment dst src)
1683 (:printer ext-reg-reg/mem-no-width
1684 ((op #b10110110) (reg/mem nil :type 'sized-byte-reg/mem)))
1685 (:printer rex-ext-reg-reg/mem-no-width
1686 ((op #b10110110) (reg/mem nil :type 'sized-byte-reg/mem)))
1687 (:printer ext-reg-reg/mem-no-width
1688 ((op #b10110111) (reg/mem nil :type 'sized-word-reg/mem)))
1689 (:printer rex-ext-reg-reg/mem-no-width
1690 ((op #b10110111) (reg/mem nil :type 'sized-word-reg/mem)))
1691 (:emitter (emit-move-with-extension segment dst src nil)))
1693 ;;; The regular use of MOVSXD is with an operand size of :qword. This
1694 ;;; sign-extends the dword source into the qword destination register.
1695 ;;; If the operand size is :dword the instruction zero-extends the dword
1696 ;;; source into the qword destination register, i.e. it does the same as
1697 ;;; a dword MOV into a register.
1698 (define-instruction movsxd (segment dst src)
1699 (:printer reg-reg/mem ((op #b0110001) (width 1)
1700 (reg/mem nil :type 'sized-dword-reg/mem)))
1701 (:printer rex-reg-reg/mem ((op #b0110001) (width 1)
1702 (reg/mem nil :type 'sized-dword-reg/mem)))
1703 (:emitter (emit-move-with-extension segment dst src :signed)))
1705 ;;; this is not a real amd64 instruction, of course
1706 (define-instruction movzxd (segment dst src)
1707 ; (:printer reg-reg/mem ((op #x63) (reg nil :type 'reg)))
1708 (:emitter (emit-move-with-extension segment dst src nil)))
1710 (define-instruction push (segment src)
1712 (:printer reg-no-width-default-qword ((op #b01010)))
1713 (:printer rex-reg-no-width-default-qword ((op #b01010)))
1715 (:printer reg/mem-default-qword ((op '(#b11111111 #b110))))
1716 (:printer rex-reg/mem-default-qword ((op '(#b11111111 #b110))))
1718 (:printer byte ((op #b01101010) (imm nil :type 'signed-imm-byte))
1720 (:printer byte ((op #b01101000)
1721 (imm nil :type 'signed-imm-data-default-qword))
1723 ;; ### segment registers?
1726 (cond ((integerp src)
1727 (cond ((<= -128 src 127)
1728 (emit-byte segment #b01101010)
1729 (emit-byte segment src))
1731 ;; A REX-prefix is not needed because the operand size
1732 ;; defaults to 64 bits. The size of the immediate is 32
1733 ;; bits and it is sign-extended.
1734 (emit-byte segment #b01101000)
1735 (emit-signed-dword segment src))))
1737 (let ((size (operand-size src)))
1738 (aver (or (eq size :qword) (eq size :word)))
1739 (maybe-emit-operand-size-prefix segment size)
1740 (maybe-emit-rex-for-ea segment src nil :operand-size :do-not-set)
1741 (cond ((register-p src)
1742 (emit-byte-with-reg segment #b01010 (reg-tn-encoding src)))
1744 (emit-byte segment #b11111111)
1745 (emit-ea segment src #b110 t))))))))
1747 (define-instruction pop (segment dst)
1748 (:printer reg-no-width-default-qword ((op #b01011)))
1749 (:printer rex-reg-no-width-default-qword ((op #b01011)))
1750 (:printer reg/mem-default-qword ((op '(#b10001111 #b000))))
1751 (:printer rex-reg/mem-default-qword ((op '(#b10001111 #b000))))
1753 (let ((size (operand-size dst)))
1754 (aver (or (eq size :qword) (eq size :word)))
1755 (maybe-emit-operand-size-prefix segment size)
1756 (maybe-emit-rex-for-ea segment dst nil :operand-size :do-not-set)
1757 (cond ((register-p dst)
1758 (emit-byte-with-reg segment #b01011 (reg-tn-encoding dst)))
1760 (emit-byte segment #b10001111)
1761 (emit-ea segment dst #b000))))))
1763 (define-instruction xchg (segment operand1 operand2)
1764 ;; Register with accumulator.
1765 (:printer reg-no-width ((op #b10010)) '(:name :tab accum ", " reg))
1766 ;; Register/Memory with Register.
1767 (:printer reg-reg/mem ((op #b1000011)))
1768 (:printer rex-reg-reg/mem ((op #b1000011)))
1770 (let ((size (matching-operand-size operand1 operand2)))
1771 (maybe-emit-operand-size-prefix segment size)
1772 (labels ((xchg-acc-with-something (acc something)
1773 (if (and (not (eq size :byte)) (register-p something))
1775 (maybe-emit-rex-for-ea segment acc something)
1776 (emit-byte-with-reg segment
1778 (reg-tn-encoding something)))
1779 (xchg-reg-with-something acc something)))
1780 (xchg-reg-with-something (reg something)
1781 (maybe-emit-rex-for-ea segment something reg)
1782 (emit-byte segment (if (eq size :byte) #b10000110 #b10000111))
1783 (emit-ea segment something (reg-tn-encoding reg))))
1784 (cond ((accumulator-p operand1)
1785 (xchg-acc-with-something operand1 operand2))
1786 ((accumulator-p operand2)
1787 (xchg-acc-with-something operand2 operand1))
1788 ((register-p operand1)
1789 (xchg-reg-with-something operand1 operand2))
1790 ((register-p operand2)
1791 (xchg-reg-with-something operand2 operand1))
1793 (error "bogus args to XCHG: ~S ~S" operand1 operand2)))))))
1795 (define-instruction lea (segment dst src)
1796 (:printer rex-reg-reg/mem ((op #b1000110)))
1797 (:printer reg-reg/mem ((op #b1000110) (width 1)))
1799 (aver (or (dword-reg-p dst) (qword-reg-p dst)))
1800 (maybe-emit-rex-for-ea segment src dst
1801 :operand-size :qword)
1802 (emit-byte segment #b10001101)
1803 (emit-ea segment src (reg-tn-encoding dst))))
1805 (define-instruction cmpxchg (segment dst src)
1806 ;; Register/Memory with Register.
1807 (:printer ext-reg-reg/mem ((op #b1011000)) '(:name :tab reg/mem ", " reg))
1809 (aver (register-p src))
1810 (let ((size (matching-operand-size src dst)))
1811 (maybe-emit-operand-size-prefix segment size)
1812 (maybe-emit-rex-for-ea segment dst src)
1813 (emit-byte segment #b00001111)
1814 (emit-byte segment (if (eq size :byte) #b10110000 #b10110001))
1815 (emit-ea segment dst (reg-tn-encoding src)))))
1819 (define-instruction fs-segment-prefix (segment)
1821 (emit-byte segment #x64)))
1823 ;;;; flag control instructions
1825 ;;; CLC -- Clear Carry Flag.
1826 (define-instruction clc (segment)
1827 (:printer byte ((op #b11111000)))
1829 (emit-byte segment #b11111000)))
1831 ;;; CLD -- Clear Direction Flag.
1832 (define-instruction cld (segment)
1833 (:printer byte ((op #b11111100)))
1835 (emit-byte segment #b11111100)))
1837 ;;; CLI -- Clear Iterrupt Enable Flag.
1838 (define-instruction cli (segment)
1839 (:printer byte ((op #b11111010)))
1841 (emit-byte segment #b11111010)))
1843 ;;; CMC -- Complement Carry Flag.
1844 (define-instruction cmc (segment)
1845 (:printer byte ((op #b11110101)))
1847 (emit-byte segment #b11110101)))
1849 ;;; LAHF -- Load AH into flags.
1850 (define-instruction lahf (segment)
1851 (:printer byte ((op #b10011111)))
1853 (emit-byte segment #b10011111)))
1855 ;;; POPF -- Pop flags.
1856 (define-instruction popf (segment)
1857 (:printer byte ((op #b10011101)))
1859 (emit-byte segment #b10011101)))
1861 ;;; PUSHF -- push flags.
1862 (define-instruction pushf (segment)
1863 (:printer byte ((op #b10011100)))
1865 (emit-byte segment #b10011100)))
1867 ;;; SAHF -- Store AH into flags.
1868 (define-instruction sahf (segment)
1869 (:printer byte ((op #b10011110)))
1871 (emit-byte segment #b10011110)))
1873 ;;; STC -- Set Carry Flag.
1874 (define-instruction stc (segment)
1875 (:printer byte ((op #b11111001)))
1877 (emit-byte segment #b11111001)))
1879 ;;; STD -- Set Direction Flag.
1880 (define-instruction std (segment)
1881 (:printer byte ((op #b11111101)))
1883 (emit-byte segment #b11111101)))
1885 ;;; STI -- Set Interrupt Enable Flag.
1886 (define-instruction sti (segment)
1887 (:printer byte ((op #b11111011)))
1889 (emit-byte segment #b11111011)))
1893 (defun emit-random-arith-inst (name segment dst src opcode
1894 &optional allow-constants)
1895 (let ((size (matching-operand-size dst src)))
1896 (maybe-emit-operand-size-prefix segment size)
1899 (cond ((and (not (eq size :byte)) (<= -128 src 127))
1900 (maybe-emit-rex-for-ea segment dst nil)
1901 (emit-byte segment #b10000011)
1902 (emit-ea segment dst opcode allow-constants)
1903 (emit-byte segment src))
1904 ((accumulator-p dst)
1905 (maybe-emit-rex-for-ea segment dst nil)
1912 (emit-sized-immediate segment size src))
1914 (maybe-emit-rex-for-ea segment dst nil)
1915 (emit-byte segment (if (eq size :byte) #b10000000 #b10000001))
1916 (emit-ea segment dst opcode allow-constants)
1917 (emit-sized-immediate segment size src))))
1919 (maybe-emit-rex-for-ea segment dst src)
1923 (if (eq size :byte) #b00000000 #b00000001)))
1924 (emit-ea segment dst (reg-tn-encoding src) allow-constants))
1926 (maybe-emit-rex-for-ea segment src dst)
1930 (if (eq size :byte) #b00000010 #b00000011)))
1931 (emit-ea segment src (reg-tn-encoding dst) allow-constants))
1933 (error "bogus operands to ~A" name)))))
1935 (eval-when (:compile-toplevel :execute)
1936 (defun arith-inst-printer-list (subop)
1937 `((accum-imm ((op ,(dpb subop (byte 3 2) #b0000010))))
1938 (rex-accum-imm ((op ,(dpb subop (byte 3 2) #b0000010))))
1939 (reg/mem-imm ((op (#b1000000 ,subop))))
1940 (rex-reg/mem-imm ((op (#b1000000 ,subop))))
1941 ;; The redundant encoding #x82 is invalid in 64-bit mode,
1942 ;; therefore we force WIDTH to 1.
1943 (reg/mem-imm ((op (#b1000001 ,subop)) (width 1)
1944 (imm nil :type signed-imm-byte)))
1945 (rex-reg/mem-imm ((op (#b1000001 ,subop)) (width 1)
1946 (imm nil :type signed-imm-byte)))
1947 (reg-reg/mem-dir ((op ,(dpb subop (byte 3 1) #b000000))))
1948 (rex-reg-reg/mem-dir ((op ,(dpb subop (byte 3 1) #b000000))))))
1951 (define-instruction add (segment dst src)
1952 (:printer-list (arith-inst-printer-list #b000))
1953 (:emitter (emit-random-arith-inst "ADD" segment dst src #b000)))
1955 (define-instruction adc (segment dst src)
1956 (:printer-list (arith-inst-printer-list #b010))
1957 (:emitter (emit-random-arith-inst "ADC" segment dst src #b010)))
1959 (define-instruction sub (segment dst src)
1960 (:printer-list (arith-inst-printer-list #b101))
1961 (:emitter (emit-random-arith-inst "SUB" segment dst src #b101)))
1963 (define-instruction sbb (segment dst src)
1964 (:printer-list (arith-inst-printer-list #b011))
1965 (:emitter (emit-random-arith-inst "SBB" segment dst src #b011)))
1967 (define-instruction cmp (segment dst src)
1968 (:printer-list (arith-inst-printer-list #b111))
1969 (:emitter (emit-random-arith-inst "CMP" segment dst src #b111 t)))
1971 ;;; The one-byte encodings for INC and DEC are used as REX prefixes
1972 ;;; in 64-bit mode so we always use the two-byte form.
1973 (define-instruction inc (segment dst)
1974 (:printer reg/mem ((op '(#b1111111 #b000))))
1975 (:printer rex-reg/mem ((op '(#b1111111 #b000))))
1977 (let ((size (operand-size dst)))
1978 (maybe-emit-operand-size-prefix segment size)
1979 (maybe-emit-rex-for-ea segment dst nil)
1980 (emit-byte segment (if (eq size :byte) #b11111110 #b11111111))
1981 (emit-ea segment dst #b000))))
1983 (define-instruction dec (segment dst)
1984 (:printer reg/mem ((op '(#b1111111 #b001))))
1985 (:printer rex-reg/mem ((op '(#b1111111 #b001))))
1987 (let ((size (operand-size dst)))
1988 (maybe-emit-operand-size-prefix segment size)
1989 (maybe-emit-rex-for-ea segment dst nil)
1990 (emit-byte segment (if (eq size :byte) #b11111110 #b11111111))
1991 (emit-ea segment dst #b001))))
1993 (define-instruction neg (segment dst)
1994 (:printer reg/mem ((op '(#b1111011 #b011))))
1995 (:printer rex-reg/mem ((op '(#b1111011 #b011))))
1997 (let ((size (operand-size dst)))
1998 (maybe-emit-operand-size-prefix segment size)
1999 (maybe-emit-rex-for-ea segment dst nil)
2000 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2001 (emit-ea segment dst #b011))))
2003 (define-instruction mul (segment dst src)
2004 (:printer accum-reg/mem ((op '(#b1111011 #b100))))
2005 (:printer rex-accum-reg/mem ((op '(#b1111011 #b100))))
2007 (let ((size (matching-operand-size dst src)))
2008 (aver (accumulator-p dst))
2009 (maybe-emit-operand-size-prefix segment size)
2010 (maybe-emit-rex-for-ea segment src nil)
2011 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2012 (emit-ea segment src #b100))))
2014 (define-instruction imul (segment dst &optional src1 src2)
2015 (:printer accum-reg/mem ((op '(#b1111011 #b101))))
2016 (:printer rex-accum-reg/mem ((op '(#b1111011 #b101))))
2017 (:printer ext-reg-reg/mem-no-width ((op #b10101111)))
2018 (:printer rex-ext-reg-reg/mem-no-width ((op #b10101111)))
2019 (:printer reg-reg/mem ((op #b0110100) (width 1)
2020 (imm nil :type 'signed-imm-data))
2021 '(:name :tab reg ", " reg/mem ", " imm))
2022 (:printer rex-reg-reg/mem ((op #b0110100) (width 1)
2023 (imm nil :type 'signed-imm-data))
2024 '(:name :tab reg ", " reg/mem ", " imm))
2025 (:printer reg-reg/mem ((op #b0110101) (width 1)
2026 (imm nil :type 'signed-imm-byte))
2027 '(:name :tab reg ", " reg/mem ", " imm))
2028 (:printer rex-reg-reg/mem ((op #b0110101) (width 1)
2029 (imm nil :type 'signed-imm-byte))
2030 '(:name :tab reg ", " reg/mem ", " imm))
2032 (flet ((r/m-with-immed-to-reg (reg r/m immed)
2033 (let* ((size (matching-operand-size reg r/m))
2034 (sx (and (not (eq size :byte)) (<= -128 immed 127))))
2035 (maybe-emit-operand-size-prefix segment size)
2036 (maybe-emit-rex-for-ea segment r/m reg)
2037 (emit-byte segment (if sx #b01101011 #b01101001))
2038 (emit-ea segment r/m (reg-tn-encoding reg))
2040 (emit-byte segment immed)
2041 (emit-sized-immediate segment size immed)))))
2043 (r/m-with-immed-to-reg dst src1 src2))
2046 (r/m-with-immed-to-reg dst dst src1)
2047 (let ((size (matching-operand-size dst src1)))
2048 (maybe-emit-operand-size-prefix segment size)
2049 (maybe-emit-rex-for-ea segment src1 dst)
2050 (emit-byte segment #b00001111)
2051 (emit-byte segment #b10101111)
2052 (emit-ea segment src1 (reg-tn-encoding dst)))))
2054 (let ((size (operand-size dst)))
2055 (maybe-emit-operand-size-prefix segment size)
2056 (maybe-emit-rex-for-ea segment dst nil)
2057 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2058 (emit-ea segment dst #b101)))))))
2060 (define-instruction div (segment dst src)
2061 (:printer accum-reg/mem ((op '(#b1111011 #b110))))
2062 (:printer rex-accum-reg/mem ((op '(#b1111011 #b110))))
2064 (let ((size (matching-operand-size dst src)))
2065 (aver (accumulator-p dst))
2066 (maybe-emit-operand-size-prefix segment size)
2067 (maybe-emit-rex-for-ea segment src nil)
2068 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2069 (emit-ea segment src #b110))))
2071 (define-instruction idiv (segment dst src)
2072 (:printer accum-reg/mem ((op '(#b1111011 #b111))))
2073 (:printer rex-accum-reg/mem ((op '(#b1111011 #b111))))
2075 (let ((size (matching-operand-size dst src)))
2076 (aver (accumulator-p dst))
2077 (maybe-emit-operand-size-prefix segment size)
2078 (maybe-emit-rex-for-ea segment src nil)
2079 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2080 (emit-ea segment src #b111))))
2082 (define-instruction bswap (segment dst)
2083 (:printer ext-reg-no-width ((op #b11001)))
2085 (let ((size (operand-size dst)))
2086 (maybe-emit-rex-prefix segment size nil nil dst)
2087 (emit-byte segment #x0f)
2088 (emit-byte-with-reg segment #b11001 (reg-tn-encoding dst)))))
2090 ;;; CBW -- Convert Byte to Word. AX <- sign_xtnd(AL)
2091 (define-instruction cbw (segment)
2092 (:printer x66-byte ((op #b10011000)))
2094 (maybe-emit-operand-size-prefix segment :word)
2095 (emit-byte segment #b10011000)))
2097 ;;; CWDE -- Convert Word To Double Word Extended. EAX <- sign_xtnd(AX)
2098 (define-instruction cwde (segment)
2099 (:printer byte ((op #b10011000)))
2101 (maybe-emit-operand-size-prefix segment :dword)
2102 (emit-byte segment #b10011000)))
2104 ;;; CDQE -- Convert Double Word To Quad Word Extended. RAX <- sign_xtnd(EAX)
2105 (define-instruction cdqe (segment)
2106 (:printer rex-byte ((op #b10011000)))
2108 (maybe-emit-rex-prefix segment :qword nil nil nil)
2109 (emit-byte segment #b10011000)))
2111 ;;; CWD -- Convert Word to Double Word. DX:AX <- sign_xtnd(AX)
2112 (define-instruction cwd (segment)
2113 (:printer x66-byte ((op #b10011001)))
2115 (maybe-emit-operand-size-prefix segment :word)
2116 (emit-byte segment #b10011001)))
2118 ;;; CDQ -- Convert Double Word to Quad Word. EDX:EAX <- sign_xtnd(EAX)
2119 (define-instruction cdq (segment)
2120 (:printer byte ((op #b10011001)))
2122 (maybe-emit-operand-size-prefix segment :dword)
2123 (emit-byte segment #b10011001)))
2125 ;;; CQO -- Convert Quad Word to Octaword. RDX:RAX <- sign_xtnd(RAX)
2126 (define-instruction cqo (segment)
2127 (:printer rex-byte ((op #b10011001)))
2129 (maybe-emit-rex-prefix segment :qword nil nil nil)
2130 (emit-byte segment #b10011001)))
2132 (define-instruction xadd (segment dst src)
2133 ;; Register/Memory with Register.
2134 (:printer ext-reg-reg/mem ((op #b1100000)) '(:name :tab reg/mem ", " reg))
2136 (aver (register-p src))
2137 (let ((size (matching-operand-size src dst)))
2138 (maybe-emit-operand-size-prefix segment size)
2139 (maybe-emit-rex-for-ea segment dst src)
2140 (emit-byte segment #b00001111)
2141 (emit-byte segment (if (eq size :byte) #b11000000 #b11000001))
2142 (emit-ea segment dst (reg-tn-encoding src)))))
2147 (defun emit-shift-inst (segment dst amount opcode)
2148 (let ((size (operand-size dst)))
2149 (maybe-emit-operand-size-prefix segment size)
2150 (multiple-value-bind (major-opcode immed)
2152 (:cl (values #b11010010 nil))
2153 (1 (values #b11010000 nil))
2154 (t (values #b11000000 t)))
2155 (maybe-emit-rex-for-ea segment dst nil)
2157 (if (eq size :byte) major-opcode (logior major-opcode 1)))
2158 (emit-ea segment dst opcode)
2160 (emit-byte segment amount)))))
2162 (eval-when (:compile-toplevel :execute)
2163 (defun shift-inst-printer-list (subop)
2164 `((reg/mem ((op (#b1101000 ,subop)))
2165 (:name :tab reg/mem ", 1"))
2166 (rex-reg/mem ((op (#b1101000 ,subop)))
2167 (:name :tab reg/mem ", 1"))
2168 (reg/mem ((op (#b1101001 ,subop)))
2169 (:name :tab reg/mem ", " 'cl))
2170 (rex-reg/mem ((op (#b1101001 ,subop)))
2171 (:name :tab reg/mem ", " 'cl))
2172 (reg/mem-imm ((op (#b1100000 ,subop))
2173 (imm nil :type imm-byte)))
2174 (rex-reg/mem-imm ((op (#b1100000 ,subop))
2175 (imm nil :type imm-byte))))))
2177 (define-instruction rol (segment dst amount)
2179 (shift-inst-printer-list #b000))
2181 (emit-shift-inst segment dst amount #b000)))
2183 (define-instruction ror (segment dst amount)
2185 (shift-inst-printer-list #b001))
2187 (emit-shift-inst segment dst amount #b001)))
2189 (define-instruction rcl (segment dst amount)
2191 (shift-inst-printer-list #b010))
2193 (emit-shift-inst segment dst amount #b010)))
2195 (define-instruction rcr (segment dst amount)
2197 (shift-inst-printer-list #b011))
2199 (emit-shift-inst segment dst amount #b011)))
2201 (define-instruction shl (segment dst amount)
2203 (shift-inst-printer-list #b100))
2205 (emit-shift-inst segment dst amount #b100)))
2207 (define-instruction shr (segment dst amount)
2209 (shift-inst-printer-list #b101))
2211 (emit-shift-inst segment dst amount #b101)))
2213 (define-instruction sar (segment dst amount)
2215 (shift-inst-printer-list #b111))
2217 (emit-shift-inst segment dst amount #b111)))
2219 (defun emit-double-shift (segment opcode dst src amt)
2220 (let ((size (matching-operand-size dst src)))
2221 (when (eq size :byte)
2222 (error "Double shifts can only be used with words."))
2223 (maybe-emit-operand-size-prefix segment size)
2224 (maybe-emit-rex-for-ea segment dst src)
2225 (emit-byte segment #b00001111)
2226 (emit-byte segment (dpb opcode (byte 1 3)
2227 (if (eq amt :cl) #b10100101 #b10100100)))
2228 (emit-ea segment dst (reg-tn-encoding src))
2229 (unless (eq amt :cl)
2230 (emit-byte segment amt))))
2232 (eval-when (:compile-toplevel :execute)
2233 (defun double-shift-inst-printer-list (op)
2235 (ext-reg-reg/mem-imm ((op ,(logior op #b100))
2236 (imm nil :type signed-imm-byte)))
2237 (ext-reg-reg/mem ((op ,(logior op #b101)))
2238 (:name :tab reg/mem ", " 'cl)))))
2240 (define-instruction shld (segment dst src amt)
2241 (:declare (type (or (member :cl) (mod 32)) amt))
2242 (:printer-list (double-shift-inst-printer-list #b10100000))
2244 (emit-double-shift segment #b0 dst src amt)))
2246 (define-instruction shrd (segment dst src amt)
2247 (:declare (type (or (member :cl) (mod 32)) amt))
2248 (:printer-list (double-shift-inst-printer-list #b10101000))
2250 (emit-double-shift segment #b1 dst src amt)))
2252 (define-instruction and (segment dst src)
2254 (arith-inst-printer-list #b100))
2256 (emit-random-arith-inst "AND" segment dst src #b100)))
2258 (define-instruction test (segment this that)
2259 (:printer accum-imm ((op #b1010100)))
2260 (:printer rex-accum-imm ((op #b1010100)))
2261 (:printer reg/mem-imm ((op '(#b1111011 #b000))))
2262 (:printer rex-reg/mem-imm ((op '(#b1111011 #b000))))
2263 (:printer reg-reg/mem ((op #b1000010)))
2264 (:printer rex-reg-reg/mem ((op #b1000010)))
2266 (let ((size (matching-operand-size this that)))
2267 (maybe-emit-operand-size-prefix segment size)
2268 (flet ((test-immed-and-something (immed something)
2269 (cond ((accumulator-p something)
2270 (maybe-emit-rex-for-ea segment something nil)
2272 (if (eq size :byte) #b10101000 #b10101001))
2273 (emit-sized-immediate segment size immed))
2275 (maybe-emit-rex-for-ea segment something nil)
2277 (if (eq size :byte) #b11110110 #b11110111))
2278 (emit-ea segment something #b000)
2279 (emit-sized-immediate segment size immed))))
2280 (test-reg-and-something (reg something)
2281 (maybe-emit-rex-for-ea segment something reg)
2282 (emit-byte segment (if (eq size :byte) #b10000100 #b10000101))
2283 (emit-ea segment something (reg-tn-encoding reg))))
2284 (cond ((integerp that)
2285 (test-immed-and-something that this))
2287 (test-immed-and-something this that))
2289 (test-reg-and-something this that))
2291 (test-reg-and-something that this))
2293 (error "bogus operands for TEST: ~S and ~S" this that)))))))
2295 (define-instruction or (segment dst src)
2297 (arith-inst-printer-list #b001))
2299 (emit-random-arith-inst "OR" segment dst src #b001)))
2301 (define-instruction xor (segment dst src)
2303 (arith-inst-printer-list #b110))
2305 (emit-random-arith-inst "XOR" segment dst src #b110)))
2307 (define-instruction not (segment dst)
2308 (:printer reg/mem ((op '(#b1111011 #b010))))
2309 (:printer rex-reg/mem ((op '(#b1111011 #b010))))
2311 (let ((size (operand-size dst)))
2312 (maybe-emit-operand-size-prefix segment size)
2313 (maybe-emit-rex-for-ea segment dst nil)
2314 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2315 (emit-ea segment dst #b010))))
2317 ;;;; string manipulation
2319 (define-instruction cmps (segment size)
2320 (:printer string-op ((op #b1010011)))
2321 (:printer rex-string-op ((op #b1010011)))
2323 (maybe-emit-operand-size-prefix segment size)
2324 (maybe-emit-rex-prefix segment size nil nil nil)
2325 (emit-byte segment (if (eq size :byte) #b10100110 #b10100111))))
2327 (define-instruction ins (segment acc)
2328 (:printer string-op ((op #b0110110)))
2329 (:printer rex-string-op ((op #b0110110)))
2331 (let ((size (operand-size acc)))
2332 (aver (accumulator-p acc))
2333 (maybe-emit-operand-size-prefix segment size)
2334 (maybe-emit-rex-prefix segment size nil nil nil)
2335 (emit-byte segment (if (eq size :byte) #b01101100 #b01101101)))))
2337 (define-instruction lods (segment acc)
2338 (:printer string-op ((op #b1010110)))
2339 (:printer rex-string-op ((op #b1010110)))
2341 (let ((size (operand-size acc)))
2342 (aver (accumulator-p acc))
2343 (maybe-emit-operand-size-prefix segment size)
2344 (maybe-emit-rex-prefix segment size nil nil nil)
2345 (emit-byte segment (if (eq size :byte) #b10101100 #b10101101)))))
2347 (define-instruction movs (segment size)
2348 (:printer string-op ((op #b1010010)))
2349 (:printer rex-string-op ((op #b1010010)))
2351 (maybe-emit-operand-size-prefix segment size)
2352 (maybe-emit-rex-prefix segment size nil nil nil)
2353 (emit-byte segment (if (eq size :byte) #b10100100 #b10100101))))
2355 (define-instruction outs (segment acc)
2356 (:printer string-op ((op #b0110111)))
2357 (:printer rex-string-op ((op #b0110111)))
2359 (let ((size (operand-size acc)))
2360 (aver (accumulator-p acc))
2361 (maybe-emit-operand-size-prefix segment size)
2362 (maybe-emit-rex-prefix segment size nil nil nil)
2363 (emit-byte segment (if (eq size :byte) #b01101110 #b01101111)))))
2365 (define-instruction scas (segment acc)
2366 (:printer string-op ((op #b1010111)))
2367 (:printer rex-string-op ((op #b1010111)))
2369 (let ((size (operand-size acc)))
2370 (aver (accumulator-p acc))
2371 (maybe-emit-operand-size-prefix segment size)
2372 (maybe-emit-rex-prefix segment size nil nil nil)
2373 (emit-byte segment (if (eq size :byte) #b10101110 #b10101111)))))
2375 (define-instruction stos (segment acc)
2376 (:printer string-op ((op #b1010101)))
2377 (:printer rex-string-op ((op #b1010101)))
2379 (let ((size (operand-size acc)))
2380 (aver (accumulator-p acc))
2381 (maybe-emit-operand-size-prefix segment size)
2382 (maybe-emit-rex-prefix segment size nil nil nil)
2383 (emit-byte segment (if (eq size :byte) #b10101010 #b10101011)))))
2385 (define-instruction xlat (segment)
2386 (:printer byte ((op #b11010111)))
2388 (emit-byte segment #b11010111)))
2390 (define-instruction rep (segment)
2392 (emit-byte segment #b11110010)))
2394 (define-instruction repe (segment)
2395 (:printer byte ((op #b11110011)))
2397 (emit-byte segment #b11110011)))
2399 (define-instruction repne (segment)
2400 (:printer byte ((op #b11110010)))
2402 (emit-byte segment #b11110010)))
2405 ;;;; bit manipulation
2407 (define-instruction bsf (segment dst src)
2408 (:printer ext-reg-reg/mem-no-width ((op #b10111100)))
2409 (:printer rex-ext-reg-reg/mem-no-width ((op #b10111100)))
2411 (let ((size (matching-operand-size dst src)))
2412 (when (eq size :byte)
2413 (error "can't scan bytes: ~S" src))
2414 (maybe-emit-operand-size-prefix segment size)
2415 (maybe-emit-rex-for-ea segment src dst)
2416 (emit-byte segment #b00001111)
2417 (emit-byte segment #b10111100)
2418 (emit-ea segment src (reg-tn-encoding dst)))))
2420 (define-instruction bsr (segment dst src)
2421 (:printer ext-reg-reg/mem-no-width ((op #b10111101)))
2422 (:printer rex-ext-reg-reg/mem-no-width ((op #b10111101)))
2424 (let ((size (matching-operand-size dst src)))
2425 (when (eq size :byte)
2426 (error "can't scan bytes: ~S" src))
2427 (maybe-emit-operand-size-prefix segment size)
2428 (maybe-emit-rex-for-ea segment src dst)
2429 (emit-byte segment #b00001111)
2430 (emit-byte segment #b10111101)
2431 (emit-ea segment src (reg-tn-encoding dst)))))
2433 (defun emit-bit-test-and-mumble (segment src index opcode)
2434 (let ((size (operand-size src)))
2435 (when (eq size :byte)
2436 (error "can't scan bytes: ~S" src))
2437 (maybe-emit-operand-size-prefix segment size)
2438 (cond ((integerp index)
2439 (maybe-emit-rex-for-ea segment src nil)
2440 (emit-byte segment #b00001111)
2441 (emit-byte segment #b10111010)
2442 (emit-ea segment src opcode)
2443 (emit-byte segment index))
2445 (maybe-emit-rex-for-ea segment src index)
2446 (emit-byte segment #b00001111)
2447 (emit-byte segment (dpb opcode (byte 3 3) #b10000011))
2448 (emit-ea segment src (reg-tn-encoding index))))))
2450 (eval-when (:compile-toplevel :execute)
2451 (defun bit-test-inst-printer-list (subop)
2452 `((ext-reg/mem-imm ((op (#b1011101 ,subop))
2453 (reg/mem nil :type reg/mem)
2454 (imm nil :type imm-byte)
2456 (ext-reg-reg/mem ((op ,(dpb subop (byte 3 2) #b1000001))
2458 (:name :tab reg/mem ", " reg)))))
2460 (define-instruction bt (segment src index)
2461 (:printer-list (bit-test-inst-printer-list #b100))
2463 (emit-bit-test-and-mumble segment src index #b100)))
2465 (define-instruction btc (segment src index)
2466 (:printer-list (bit-test-inst-printer-list #b111))
2468 (emit-bit-test-and-mumble segment src index #b111)))
2470 (define-instruction btr (segment src index)
2471 (:printer-list (bit-test-inst-printer-list #b110))
2473 (emit-bit-test-and-mumble segment src index #b110)))
2475 (define-instruction bts (segment src index)
2476 (:printer-list (bit-test-inst-printer-list #b101))
2478 (emit-bit-test-and-mumble segment src index #b101)))
2481 ;;;; control transfer
2483 (define-instruction call (segment where)
2484 (:printer near-jump ((op #b11101000)))
2485 (:printer reg/mem-default-qword ((op '(#b11111111 #b010))))
2486 (:printer rex-reg/mem-default-qword ((op '(#b11111111 #b010))))
2490 (emit-byte segment #b11101000) ; 32 bit relative
2491 (emit-back-patch segment
2493 (lambda (segment posn)
2494 (emit-signed-dword segment
2495 (- (label-position where)
2498 (emit-byte segment #b11101000)
2499 (emit-relative-fixup segment where))
2501 (maybe-emit-rex-for-ea segment where nil :operand-size :do-not-set)
2502 (emit-byte segment #b11111111)
2503 (emit-ea segment where #b010)))))
2505 (defun emit-byte-displacement-backpatch (segment target)
2506 (emit-back-patch segment
2508 (lambda (segment posn)
2509 (let ((disp (- (label-position target) (1+ posn))))
2510 (aver (<= -128 disp 127))
2511 (emit-byte segment disp)))))
2513 (define-instruction jmp (segment cond &optional where)
2514 ;; conditional jumps
2515 (:printer short-cond-jump ((op #b0111)) '('j cc :tab label))
2516 (:printer near-cond-jump () '('j cc :tab label))
2517 ;; unconditional jumps
2518 (:printer short-jump ((op #b1011)))
2519 (:printer near-jump ((op #b11101001)))
2520 (:printer reg/mem-default-qword ((op '(#b11111111 #b100))))
2521 (:printer rex-reg/mem-default-qword ((op '(#b11111111 #b100))))
2526 (lambda (segment posn delta-if-after)
2527 (let ((disp (- (label-position where posn delta-if-after)
2529 (when (<= -128 disp 127)
2531 (dpb (conditional-opcode cond)
2534 (emit-byte-displacement-backpatch segment where)
2536 (lambda (segment posn)
2537 (let ((disp (- (label-position where) (+ posn 6))))
2538 (emit-byte segment #b00001111)
2540 (dpb (conditional-opcode cond)
2543 (emit-signed-dword segment disp)))))
2544 ((label-p (setq where cond))
2547 (lambda (segment posn delta-if-after)
2548 (let ((disp (- (label-position where posn delta-if-after)
2550 (when (<= -128 disp 127)
2551 (emit-byte segment #b11101011)
2552 (emit-byte-displacement-backpatch segment where)
2554 (lambda (segment posn)
2555 (let ((disp (- (label-position where) (+ posn 5))))
2556 (emit-byte segment #b11101001)
2557 (emit-signed-dword segment disp)))))
2559 (emit-byte segment #b11101001)
2560 (emit-relative-fixup segment where))
2562 (unless (or (ea-p where) (tn-p where))
2563 (error "don't know what to do with ~A" where))
2564 ;; near jump defaults to 64 bit
2565 ;; w-bit in rex prefix is unnecessary
2566 (maybe-emit-rex-for-ea segment where nil :operand-size :do-not-set)
2567 (emit-byte segment #b11111111)
2568 (emit-ea segment where #b100)))))
2570 (define-instruction jmp-short (segment label)
2572 (emit-byte segment #b11101011)
2573 (emit-byte-displacement-backpatch segment label)))
2575 (define-instruction ret (segment &optional stack-delta)
2576 (:printer byte ((op #b11000011)))
2577 (:printer byte ((op #b11000010) (imm nil :type 'imm-word-16))
2581 (emit-byte segment #b11000010)
2582 (emit-word segment stack-delta))
2584 (emit-byte segment #b11000011)))))
2586 (define-instruction jecxz (segment target)
2587 (:printer short-jump ((op #b0011)))
2589 (emit-byte segment #b11100011)
2590 (emit-byte-displacement-backpatch segment target)))
2592 (define-instruction loop (segment target)
2593 (:printer short-jump ((op #b0010)))
2595 (emit-byte segment #b11100010) ; pfw this was 11100011, or jecxz!!!!
2596 (emit-byte-displacement-backpatch segment target)))
2598 (define-instruction loopz (segment target)
2599 (:printer short-jump ((op #b0001)))
2601 (emit-byte segment #b11100001)
2602 (emit-byte-displacement-backpatch segment target)))
2604 (define-instruction loopnz (segment target)
2605 (:printer short-jump ((op #b0000)))
2607 (emit-byte segment #b11100000)
2608 (emit-byte-displacement-backpatch segment target)))
2610 ;;;; conditional move
2611 (define-instruction cmov (segment cond dst src)
2612 (:printer cond-move ())
2613 (:printer rex-cond-move ())
2615 (aver (register-p dst))
2616 (let ((size (matching-operand-size dst src)))
2617 (aver (or (eq size :word) (eq size :dword) (eq size :qword)))
2618 (maybe-emit-operand-size-prefix segment size))
2619 (maybe-emit-rex-for-ea segment src dst)
2620 (emit-byte segment #b00001111)
2621 (emit-byte segment (dpb (conditional-opcode cond) (byte 4 0) #b01000000))
2622 (emit-ea segment src (reg-tn-encoding dst))))
2624 ;;;; conditional byte set
2626 (define-instruction set (segment dst cond)
2627 (:printer cond-set ())
2629 (maybe-emit-rex-for-ea segment dst nil)
2630 (emit-byte segment #b00001111)
2631 (emit-byte segment (dpb (conditional-opcode cond) (byte 4 0) #b10010000))
2632 (emit-ea segment dst #b000)))
2636 (define-instruction enter (segment disp &optional (level 0))
2637 (:declare (type (unsigned-byte 16) disp)
2638 (type (unsigned-byte 8) level))
2639 (:printer enter-format ((op #b11001000)))
2641 (emit-byte segment #b11001000)
2642 (emit-word segment disp)
2643 (emit-byte segment level)))
2645 (define-instruction leave (segment)
2646 (:printer byte ((op #b11001001)))
2648 (emit-byte segment #b11001001)))
2650 ;;;; interrupt instructions
2652 (defun snarf-error-junk (sap offset &optional length-only)
2653 (let* ((length (sb!sys:sap-ref-8 sap offset))
2654 (vector (make-array length :element-type '(unsigned-byte 8))))
2655 (declare (type sb!sys:system-area-pointer sap)
2656 (type (unsigned-byte 8) length)
2657 (type (simple-array (unsigned-byte 8) (*)) vector))
2659 (values 0 (1+ length) nil nil))
2661 (sb!kernel:copy-ub8-from-system-area sap (1+ offset)
2663 (collect ((sc-offsets)
2665 (lengths 1) ; the length byte
2667 (error-number (sb!c:read-var-integer vector index)))
2670 (when (>= index length)
2672 (let ((old-index index))
2673 (sc-offsets (sb!c:read-var-integer vector index))
2674 (lengths (- index old-index))))
2675 (values error-number
2681 (defmacro break-cases (breaknum &body cases)
2682 (let ((bn-temp (gensym)))
2683 (collect ((clauses))
2684 (dolist (case cases)
2685 (clauses `((= ,bn-temp ,(car case)) ,@(cdr case))))
2686 `(let ((,bn-temp ,breaknum))
2687 (cond ,@(clauses))))))
2690 (defun break-control (chunk inst stream dstate)
2691 (declare (ignore inst))
2692 (flet ((nt (x) (if stream (sb!disassem:note x dstate))))
2693 ;; FIXME: Make sure that BYTE-IMM-CODE is defined. The genesis
2694 ;; map has it undefined; and it should be easier to look in the target
2695 ;; Lisp (with (DESCRIBE 'BYTE-IMM-CODE)) than to definitively deduce
2696 ;; from first principles whether it's defined in some way that genesis
2698 (case (byte-imm-code chunk dstate)
2701 (sb!disassem:handle-break-args #'snarf-error-junk stream dstate))
2704 (sb!disassem:handle-break-args #'snarf-error-junk stream dstate))
2706 (nt "breakpoint trap"))
2707 (#.pending-interrupt-trap
2708 (nt "pending interrupt trap"))
2711 (#.fun-end-breakpoint-trap
2712 (nt "function end breakpoint trap"))
2713 (#.single-step-around-trap
2714 (nt "single-step trap (around)"))
2715 (#.single-step-before-trap
2716 (nt "single-step trap (before)")))))
2718 (define-instruction break (segment code)
2719 (:declare (type (unsigned-byte 8) code))
2720 (:printer byte-imm ((op #b11001100)) '(:name :tab code)
2721 :control #'break-control)
2723 (emit-byte segment #b11001100)
2724 (emit-byte segment code)))
2726 (define-instruction int (segment number)
2727 (:declare (type (unsigned-byte 8) number))
2728 (:printer byte-imm ((op #b11001101)))
2732 (emit-byte segment #b11001100))
2734 (emit-byte segment #b11001101)
2735 (emit-byte segment number)))))
2737 (define-instruction iret (segment)
2738 (:printer byte ((op #b11001111)))
2740 (emit-byte segment #b11001111)))
2742 ;;;; processor control
2744 (define-instruction hlt (segment)
2745 (:printer byte ((op #b11110100)))
2747 (emit-byte segment #b11110100)))
2749 (define-instruction nop (segment)
2750 (:printer byte ((op #b10010000)))
2752 (emit-byte segment #b10010000)))
2754 (define-instruction wait (segment)
2755 (:printer byte ((op #b10011011)))
2757 (emit-byte segment #b10011011)))
2759 (define-instruction lock (segment)
2760 (:printer byte ((op #b11110000)))
2762 (emit-byte segment #b11110000)))
2764 ;;;; miscellaneous hackery
2766 (define-instruction byte (segment byte)
2768 (emit-byte segment byte)))
2770 (define-instruction word (segment word)
2772 (emit-word segment word)))
2774 (define-instruction dword (segment dword)
2776 (emit-dword segment dword)))
2778 (defun emit-header-data (segment type)
2779 (emit-back-patch segment
2781 (lambda (segment posn)
2785 (component-header-length))
2789 (define-instruction simple-fun-header-word (segment)
2791 (emit-header-data segment simple-fun-header-widetag)))
2793 (define-instruction lra-header-word (segment)
2795 (emit-header-data segment return-pc-header-widetag)))
2797 ;;;; Instructions required to do floating point operations using SSE
2799 (defun emit-sse-inst (segment dst src prefix opcode &key operand-size)
2801 (emit-byte segment prefix))
2803 (maybe-emit-rex-for-ea segment src dst :operand-size operand-size)
2804 (maybe-emit-rex-for-ea segment src dst))
2805 (emit-byte segment #x0f)
2806 (emit-byte segment opcode)
2807 (emit-ea segment src (reg-tn-encoding dst)))
2809 ;;; Emit an SSE instruction that has an XMM register as the destination
2810 ;;; operand and for which the size of the operands is implicitly given
2811 ;;; by the instruction.
2812 (defun emit-regular-sse-inst (segment dst src prefix opcode)
2813 (aver (xmm-register-p dst))
2814 (emit-sse-inst segment dst src prefix opcode
2815 :operand-size :do-not-set))
2817 ;;; Instructions having an XMM register as the destination operand
2818 ;;; and an XMM register or a memory location as the source operand.
2819 ;;; The operand size is implicitly given by the instruction.
2821 (macrolet ((define-regular-sse-inst (name prefix opcode)
2822 `(define-instruction ,name (segment dst src)
2824 `((:printer ext-xmm-xmm/mem
2825 ((prefix ,prefix) (op ,opcode)))
2826 (:printer ext-rex-xmm-xmm/mem
2827 ((prefix ,prefix) (op ,opcode))))
2828 `((:printer xmm-xmm/mem ((op ,opcode)))
2829 (:printer rex-xmm-xmm/mem ((op ,opcode)))))
2831 (emit-regular-sse-inst segment dst src ,prefix ,opcode)))))
2833 (define-regular-sse-inst andpd #x66 #x54)
2834 (define-regular-sse-inst andps nil #x54)
2835 (define-regular-sse-inst xorpd #x66 #x57)
2836 (define-regular-sse-inst xorps nil #x57)
2838 (define-regular-sse-inst comisd #x66 #x2f)
2839 (define-regular-sse-inst comiss nil #x2f)
2841 (define-regular-sse-inst addsd #xf2 #x58)
2842 (define-regular-sse-inst addss #xf3 #x58)
2843 (define-regular-sse-inst divsd #xf2 #x5e)
2844 (define-regular-sse-inst divss #xf3 #x5e)
2845 (define-regular-sse-inst mulsd #xf2 #x59)
2846 (define-regular-sse-inst mulss #xf3 #x59)
2847 (define-regular-sse-inst subsd #xf2 #x5c)
2848 (define-regular-sse-inst subss #xf3 #x5c)
2849 (define-regular-sse-inst sqrtsd #xf2 #x51)
2850 (define-regular-sse-inst sqrtss #xf3 #x51)
2852 (define-regular-sse-inst cvtsd2ss #xf2 #x5a)
2853 (define-regular-sse-inst cvtss2sd #xf3 #x5a)
2854 (define-regular-sse-inst cvtdq2pd #xf3 #xe6)
2855 (define-regular-sse-inst cvtdq2ps nil #x5b))
2858 (macrolet ((define-movsd/ss-sse-inst (name prefix)
2859 `(define-instruction ,name (segment dst src)
2860 (:printer ext-xmm-xmm/mem-dir ((prefix ,prefix)
2862 (:printer ext-rex-xmm-xmm/mem-dir ((prefix ,prefix)
2865 (cond ((xmm-register-p dst)
2866 (emit-sse-inst segment dst src ,prefix #x10
2867 :operand-size :do-not-set))
2869 (aver (xmm-register-p src))
2870 (emit-sse-inst segment src dst ,prefix #x11
2871 :operand-size :do-not-set)))))))
2872 (define-movsd/ss-sse-inst movsd #xf2)
2873 (define-movsd/ss-sse-inst movss #xf3))
2876 (define-instruction movq (segment dst src)
2877 (:printer ext-xmm-xmm/mem ((prefix #xf3) (op #x7e)))
2878 (:printer ext-rex-xmm-xmm/mem ((prefix #xf3) (op #x7e)))
2879 (:printer ext-xmm-xmm/mem ((prefix #x66) (op #xd6))
2880 '(:name :tab reg/mem ", " reg))
2881 (:printer ext-rex-xmm-xmm/mem ((prefix #x66) (op #xd6))
2882 '(:name :tab reg/mem ", " reg))
2884 (cond ((xmm-register-p dst)
2885 (emit-sse-inst segment dst src #xf3 #x7e
2886 :operand-size :do-not-set))
2888 (aver (xmm-register-p src))
2889 (emit-sse-inst segment src dst #x66 #xd6
2890 :operand-size :do-not-set)))))
2892 ;;; Instructions having an XMM register as the destination operand
2893 ;;; and a general-purpose register or a memory location as the source
2894 ;;; operand. The operand size is calculated from the source operand.
2896 ;;; MOVD - Move a 32- or 64-bit value from a general-purpose register or
2897 ;;; a memory location to the low order 32 or 64 bits of an XMM register
2898 ;;; with zero extension or vice versa.
2899 ;;; We do not support the MMX version of this instruction.
2900 (define-instruction movd (segment dst src)
2901 (:printer ext-xmm-reg/mem ((prefix #x66) (op #x6e)))
2902 (:printer ext-rex-xmm-reg/mem ((prefix #x66) (op #x6e)))
2903 (:printer ext-xmm-reg/mem ((prefix #x66) (op #x7e))
2904 '(:name :tab reg/mem ", " reg))
2905 (:printer ext-rex-xmm-reg/mem ((prefix #x66) (op #x7e))
2906 '(:name :tab reg/mem ", " reg))
2908 (cond ((xmm-register-p dst)
2909 (emit-sse-inst segment dst src #x66 #x6e))
2911 (aver (xmm-register-p src))
2912 (emit-sse-inst segment src dst #x66 #x7e)))))
2914 (macrolet ((define-integer-source-sse-inst (name prefix opcode)
2915 `(define-instruction ,name (segment dst src)
2916 (:printer ext-xmm-reg/mem ((prefix ,prefix) (op ,opcode)))
2917 (:printer ext-rex-xmm-reg/mem ((prefix ,prefix) (op ,opcode)))
2919 (aver (xmm-register-p dst))
2920 (let ((src-size (operand-size src)))
2921 (aver (or (eq src-size :qword) (eq src-size :dword))))
2922 (emit-sse-inst segment dst src ,prefix ,opcode)))))
2923 (define-integer-source-sse-inst cvtsi2sd #xf2 #x2a)
2924 (define-integer-source-sse-inst cvtsi2ss #xf3 #x2a))
2926 ;;; Instructions having a general-purpose register as the destination
2927 ;;; operand and an XMM register or a memory location as the source
2928 ;;; operand. The operand size is calculated from the destination
2931 (macrolet ((define-gpr-destination-sse-inst (name prefix opcode)
2932 `(define-instruction ,name (segment dst src)
2933 (:printer ext-reg-xmm/mem ((prefix ,prefix) (op ,opcode)))
2934 (:printer ext-rex-reg-xmm/mem ((prefix ,prefix) (op ,opcode)))
2936 (aver (register-p dst))
2937 (let ((dst-size (operand-size dst)))
2938 (aver (or (eq dst-size :qword) (eq dst-size :dword)))
2939 (emit-sse-inst segment dst src ,prefix ,opcode
2940 :operand-size dst-size))))))
2941 (define-gpr-destination-sse-inst cvtsd2si #xf2 #x2d)
2942 (define-gpr-destination-sse-inst cvtss2si #xf3 #x2d)
2943 (define-gpr-destination-sse-inst cvttsd2si #xf2 #x2c)
2944 (define-gpr-destination-sse-inst cvttss2si #xf3 #x2c))
2946 ;;; Other SSE instructions
2948 (define-instruction ldmxcsr (segment src)
2950 (emit-byte segment #x0f)
2951 (emit-byte segment #xae)
2952 (emit-ea segment src 2)))
2954 (define-instruction stmxcsr (segment dst)
2956 (emit-byte segment #x0f)
2957 (emit-byte segment #xae)
2958 (emit-ea segment dst 3)))