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 ;;; This prefilter is used solely for its side effects, namely to put
225 ;;; the bits found in the REX prefix into the DSTATE for use by other
226 ;;; prefilters and by printers.
227 (defun prefilter-wrxb (value dstate)
228 (declare (type (unsigned-byte 4) value)
229 (type sb!disassem:disassem-state dstate))
230 (sb!disassem:dstate-put-inst-prop dstate 'rex)
231 (when (plusp (logand value #b1000))
232 (sb!disassem:dstate-put-inst-prop dstate 'rex-w))
233 (when (plusp (logand value #b0100))
234 (sb!disassem:dstate-put-inst-prop dstate 'rex-r))
235 (when (plusp (logand value #b0010))
236 (sb!disassem:dstate-put-inst-prop dstate 'rex-x))
237 (when (plusp (logand value #b0001))
238 (sb!disassem:dstate-put-inst-prop dstate 'rex-b))
241 ;;; This prefilter is used solely for its side effect, namely to put
242 ;;; the property OPERAND-SIZE-8 into the DSTATE if VALUE is 0.
243 (defun prefilter-width (value dstate)
244 (declare (type bit value)
245 (type sb!disassem:disassem-state dstate))
247 (sb!disassem:dstate-put-inst-prop dstate 'operand-size-8))
250 ;;; This prefilter is used solely for its side effect, namely to put
251 ;;; the property OPERAND-SIZE-16 into the DSTATE.
252 (defun prefilter-x66 (value dstate)
253 (declare (type (eql #x66) value)
255 (type sb!disassem:disassem-state dstate))
256 (sb!disassem:dstate-put-inst-prop dstate 'operand-size-16))
258 ;;; A register field that can be extended by REX.R.
259 (defun prefilter-reg-r (value dstate)
260 (declare (type reg value)
261 (type sb!disassem:disassem-state dstate))
262 (if (sb!disassem::dstate-get-inst-prop dstate 'rex-r)
266 ;;; A register field that can be extended by REX.B.
267 (defun prefilter-reg-b (value dstate)
268 (declare (type reg value)
269 (type sb!disassem:disassem-state dstate))
270 (if (sb!disassem::dstate-get-inst-prop dstate 'rex-b)
274 ;;; Returns either an integer, meaning a register, or a list of
275 ;;; (BASE-REG OFFSET INDEX-REG INDEX-SCALE), where any component
276 ;;; may be missing or nil to indicate that it's not used or has the
277 ;;; obvious default value (e.g., 1 for the index-scale). VALUE is a list
278 ;;; of the mod and r/m field of the ModRM byte of the instruction.
279 ;;; Depending on VALUE a SIB byte and/or an offset may be read. The
280 ;;; REX.B bit from DSTATE is used to extend the sole register or the
281 ;;; BASE-REG to a full register, the REX.X bit does the same for the
283 (defun prefilter-reg/mem (value dstate)
284 (declare (type list value)
285 (type sb!disassem:disassem-state dstate))
286 (let ((mod (first value))
287 (r/m (second value)))
288 (declare (type (unsigned-byte 2) mod)
289 (type (unsigned-byte 3) r/m))
290 (let ((full-reg (if (sb!disassem:dstate-get-inst-prop dstate 'rex-b)
293 (declare (type full-reg full-reg))
299 (let ((sib (sb!disassem:read-suffix 8 dstate)))
300 (declare (type (unsigned-byte 8) sib))
301 (let ((base-reg (ldb (byte 3 0) sib))
302 (index-reg (ldb (byte 3 3) sib))
303 (index-scale (ldb (byte 2 6) sib)))
304 (declare (type (unsigned-byte 3) base-reg index-reg)
305 (type (unsigned-byte 2) index-scale))
309 (if (= base-reg #b101)
310 (sb!disassem:read-signed-suffix 32 dstate)
313 (sb!disassem:read-signed-suffix 8 dstate))
315 (sb!disassem:read-signed-suffix 32 dstate)))))
316 (list (unless (and (= mod #b00) (= base-reg #b101))
317 (if (sb!disassem:dstate-get-inst-prop dstate 'rex-b)
321 (unless (= index-reg #b100)
322 (if (sb!disassem:dstate-get-inst-prop dstate 'rex-x)
325 (ash 1 index-scale))))))
326 ((and (= mod #b00) (= r/m #b101))
327 (list 'rip (sb!disassem:read-signed-suffix 32 dstate)))
331 (list full-reg (sb!disassem:read-signed-suffix 8 dstate)))
333 (list full-reg (sb!disassem:read-signed-suffix 32 dstate)))))))
335 (defun read-address (value dstate)
336 (declare (ignore value)) ; always nil anyway
337 (sb!disassem:read-suffix (width-bits (inst-operand-size dstate)) dstate))
339 (defun width-bits (width)
348 ;;;; disassembler argument types
350 ;;; Used to capture the lower four bits of the REX prefix.
351 (sb!disassem:define-arg-type wrxb
352 :prefilter #'prefilter-wrxb)
354 (sb!disassem:define-arg-type width
355 :prefilter #'prefilter-width
356 :printer (lambda (value stream dstate)
357 (declare (ignore value))
358 (princ (schar (symbol-name (inst-operand-size dstate)) 0)
361 ;;; Used to capture the effect of the #x66 operand size override prefix.
362 (sb!disassem:define-arg-type x66
363 :prefilter #'prefilter-x66)
365 (sb!disassem:define-arg-type displacement
367 :use-label #'offset-next
368 :printer (lambda (value stream dstate)
369 (sb!disassem:maybe-note-assembler-routine value nil dstate)
370 (print-label value stream dstate)))
372 (sb!disassem:define-arg-type accum
373 :printer (lambda (value stream dstate)
374 (declare (ignore value)
376 (type sb!disassem:disassem-state dstate))
377 (print-reg 0 stream dstate)))
379 (sb!disassem:define-arg-type reg
380 :prefilter #'prefilter-reg-r
381 :printer #'print-reg)
383 (sb!disassem:define-arg-type reg-b
384 :prefilter #'prefilter-reg-b
385 :printer #'print-reg)
387 (sb!disassem:define-arg-type reg-b-default-qword
388 :prefilter #'prefilter-reg-b
389 :printer #'print-reg-default-qword)
391 (sb!disassem:define-arg-type imm-addr
392 :prefilter #'read-address
393 :printer #'print-label)
395 ;;; Normally, immediate values for an operand size of :qword are of size
396 ;;; :dword and are sign-extended to 64 bits. For an exception, see the
397 ;;; argument type definition following this one.
398 (sb!disassem:define-arg-type signed-imm-data
399 :prefilter (lambda (value dstate)
400 (declare (ignore value)) ; always nil anyway
401 (let ((width (width-bits (inst-operand-size dstate))))
404 (sb!disassem:read-signed-suffix width dstate))))
406 ;;; Used by the variant of the MOV instruction with opcode B8 which can
407 ;;; move immediates of all sizes (i.e. including :qword) into a
409 (sb!disassem:define-arg-type signed-imm-data-upto-qword
410 :prefilter (lambda (value dstate)
411 (declare (ignore value)) ; always nil anyway
412 (sb!disassem:read-signed-suffix
413 (width-bits (inst-operand-size dstate))
416 ;;; Used by those instructions that have a default operand size of
417 ;;; :qword. Nevertheless the immediate is at most of size :dword.
418 ;;; The only instruction of this kind having a variant with an immediate
419 ;;; argument is PUSH.
420 (sb!disassem:define-arg-type signed-imm-data-default-qword
421 :prefilter (lambda (value dstate)
422 (declare (ignore value)) ; always nil anyway
423 (let ((width (width-bits
424 (inst-operand-size-default-qword dstate))))
427 (sb!disassem:read-signed-suffix width dstate))))
429 (sb!disassem:define-arg-type signed-imm-byte
430 :prefilter (lambda (value dstate)
431 (declare (ignore value)) ; always nil anyway
432 (sb!disassem:read-signed-suffix 8 dstate)))
434 (sb!disassem:define-arg-type imm-byte
435 :prefilter (lambda (value dstate)
436 (declare (ignore value)) ; always nil anyway
437 (sb!disassem:read-suffix 8 dstate)))
439 ;;; needed for the ret imm16 instruction
440 (sb!disassem:define-arg-type imm-word-16
441 :prefilter (lambda (value dstate)
442 (declare (ignore value)) ; always nil anyway
443 (sb!disassem:read-suffix 16 dstate)))
445 (sb!disassem:define-arg-type reg/mem
446 :prefilter #'prefilter-reg/mem
447 :printer #'print-reg/mem)
448 (sb!disassem:define-arg-type sized-reg/mem
449 ;; Same as reg/mem, but prints an explicit size indicator for
450 ;; memory references.
451 :prefilter #'prefilter-reg/mem
452 :printer #'print-sized-reg/mem)
454 ;;; Arguments of type reg/mem with a fixed size.
455 (sb!disassem:define-arg-type sized-byte-reg/mem
456 :prefilter #'prefilter-reg/mem
457 :printer #'print-sized-byte-reg/mem)
458 (sb!disassem:define-arg-type sized-word-reg/mem
459 :prefilter #'prefilter-reg/mem
460 :printer #'print-sized-word-reg/mem)
461 (sb!disassem:define-arg-type sized-dword-reg/mem
462 :prefilter #'prefilter-reg/mem
463 :printer #'print-sized-dword-reg/mem)
465 ;;; Same as sized-reg/mem, but with a default operand size of :qword.
466 (sb!disassem:define-arg-type sized-reg/mem-default-qword
467 :prefilter #'prefilter-reg/mem
468 :printer #'print-sized-reg/mem-default-qword)
471 (sb!disassem:define-arg-type xmmreg
472 :prefilter #'prefilter-reg-r
473 :printer #'print-xmmreg)
475 (sb!disassem:define-arg-type xmmreg-b
476 :prefilter #'prefilter-reg-b
477 :printer #'print-xmmreg)
479 (sb!disassem:define-arg-type xmmreg/mem
480 :prefilter #'prefilter-reg/mem
481 :printer #'print-xmmreg/mem)
484 (eval-when (:compile-toplevel :load-toplevel :execute)
485 (defparameter *conditions*
488 (:b . 2) (:nae . 2) (:c . 2)
489 (:nb . 3) (:ae . 3) (:nc . 3)
490 (:eq . 4) (:e . 4) (:z . 4)
497 (:np . 11) (:po . 11)
498 (:l . 12) (:nge . 12)
499 (:nl . 13) (:ge . 13)
500 (:le . 14) (:ng . 14)
501 (:nle . 15) (:g . 15)))
502 (defparameter *condition-name-vec*
503 (let ((vec (make-array 16 :initial-element nil)))
504 (dolist (cond *conditions*)
505 (when (null (aref vec (cdr cond)))
506 (setf (aref vec (cdr cond)) (car cond))))
510 ;;; SSE shuffle patterns. The names end in the number of bits of the
511 ;;; immediate byte that are used to encode the pattern and the radix
512 ;;; in which to print the value.
513 (macrolet ((define-sse-shuffle-arg-type (name format-string)
514 `(sb!disassem:define-arg-type ,name
516 :printer (lambda (value stream dstate)
517 (declare (type (unsigned-byte 8) value)
520 (format stream ,format-string value)))))
521 (define-sse-shuffle-arg-type sse-shuffle-pattern-2-2 "#b~2,'0B")
522 (define-sse-shuffle-arg-type sse-shuffle-pattern-8-4 "#4r~4,4,'0R"))
524 ;;; Set assembler parameters. (In CMU CL, this was done with
525 ;;; a call to a macro DEF-ASSEMBLER-PARAMS.)
526 (eval-when (:compile-toplevel :load-toplevel :execute)
527 (setf sb!assem:*assem-scheduler-p* nil))
529 (sb!disassem:define-arg-type condition-code
530 :printer *condition-name-vec*)
532 (defun conditional-opcode (condition)
533 (cdr (assoc condition *conditions* :test #'eq)))
535 ;;;; disassembler instruction formats
537 (eval-when (:compile-toplevel :execute)
538 (defun swap-if (direction field1 separator field2)
539 `(:if (,direction :constant 0)
540 (,field1 ,separator ,field2)
541 (,field2 ,separator ,field1))))
543 (sb!disassem:define-instruction-format (byte 8 :default-printer '(:name))
544 (op :field (byte 8 0))
549 (sb!disassem:define-instruction-format (two-bytes 16
550 :default-printer '(:name))
551 (op :fields (list (byte 8 0) (byte 8 8))))
553 (sb!disassem:define-instruction-format (three-bytes 24
554 :default-printer '(:name))
555 (op :fields (list (byte 8 0) (byte 8 8) (byte 8 16))))
557 ;;; Prefix instructions
559 (sb!disassem:define-instruction-format (rex 8)
560 (rex :field (byte 4 4) :value #b0100)
561 (wrxb :field (byte 4 0) :type 'wrxb))
563 (sb!disassem:define-instruction-format (x66 8)
564 (x66 :field (byte 8 0) :type 'x66 :value #x66))
566 ;;; A one-byte instruction with a #x66 prefix, used to indicate an
567 ;;; operand size of :word.
568 (sb!disassem:define-instruction-format (x66-byte 16
569 :default-printer '(:name))
570 (x66 :field (byte 8 0) :value #x66)
571 (op :field (byte 8 8)))
573 ;;; A one-byte instruction with a REX prefix, used to indicate an
574 ;;; operand size of :qword. REX.W must be 1, the other three bits are
576 (sb!disassem:define-instruction-format (rex-byte 16
577 :default-printer '(:name))
578 (rex :field (byte 5 3) :value #b01001)
579 (op :field (byte 8 8)))
581 (sb!disassem:define-instruction-format (simple 8)
582 (op :field (byte 7 1))
583 (width :field (byte 1 0) :type 'width)
588 ;;; Same as simple, but with direction bit
589 (sb!disassem:define-instruction-format (simple-dir 8 :include 'simple)
590 (op :field (byte 6 2))
591 (dir :field (byte 1 1)))
593 ;;; Same as simple, but with the immediate value occurring by default,
594 ;;; and with an appropiate printer.
595 (sb!disassem:define-instruction-format (accum-imm 8
597 :default-printer '(:name
598 :tab accum ", " imm))
599 (imm :type 'signed-imm-data))
601 (sb!disassem:define-instruction-format (reg-no-width 8
602 :default-printer '(:name :tab reg))
603 (op :field (byte 5 3))
604 (reg :field (byte 3 0) :type 'reg-b)
609 ;;; Same as reg-no-width, but with a default operand size of :qword.
610 (sb!disassem:define-instruction-format (reg-no-width-default-qword 8
611 :include 'reg-no-width
612 :default-printer '(:name :tab reg))
613 (reg :type 'reg-b-default-qword))
615 ;;; Adds a width field to reg-no-width. Note that we can't use
616 ;;; :INCLUDE 'REG-NO-WIDTH here to save typing because that would put
617 ;;; the WIDTH field last, but the prefilter for WIDTH must run before
618 ;;; the one for IMM to be able to determine the correct size of IMM.
619 (sb!disassem:define-instruction-format (reg 8
620 :default-printer '(:name :tab reg))
621 (op :field (byte 4 4))
622 (width :field (byte 1 3) :type 'width)
623 (reg :field (byte 3 0) :type 'reg-b)
628 (sb!disassem:define-instruction-format (rex-reg 16
629 :default-printer '(:name :tab reg))
630 (rex :field (byte 4 4) :value #b0100)
631 (wrxb :field (byte 4 0) :type 'wrxb)
632 (width :field (byte 1 11) :type 'width)
633 (op :field (byte 4 12))
634 (reg :field (byte 3 8) :type 'reg-b)
639 (sb!disassem:define-instruction-format (two-bytes 16
640 :default-printer '(:name))
641 (op :fields (list (byte 8 0) (byte 8 8))))
643 (sb!disassem:define-instruction-format (reg-reg/mem 16
645 `(:name :tab reg ", " reg/mem))
646 (op :field (byte 7 1))
647 (width :field (byte 1 0) :type 'width)
648 (reg/mem :fields (list (byte 2 14) (byte 3 8))
650 (reg :field (byte 3 11) :type 'reg)
654 ;;; same as reg-reg/mem, but with direction bit
655 (sb!disassem:define-instruction-format (reg-reg/mem-dir 16
656 :include 'reg-reg/mem
660 ,(swap-if 'dir 'reg/mem ", " 'reg)))
661 (op :field (byte 6 2))
662 (dir :field (byte 1 1)))
664 ;;; Same as reg-reg/mem, but uses the reg field as a second op code.
665 (sb!disassem:define-instruction-format (reg/mem 16
666 :default-printer '(:name :tab reg/mem))
667 (op :fields (list (byte 7 1) (byte 3 11)))
668 (width :field (byte 1 0) :type 'width)
669 (reg/mem :fields (list (byte 2 14) (byte 3 8))
670 :type 'sized-reg/mem)
674 ;;; Same as reg/mem, but without a width field and with a default
675 ;;; operand size of :qword.
676 (sb!disassem:define-instruction-format (reg/mem-default-qword 16
677 :default-printer '(:name :tab reg/mem))
678 (op :fields (list (byte 8 0) (byte 3 11)))
679 (reg/mem :fields (list (byte 2 14) (byte 3 8))
680 :type 'sized-reg/mem-default-qword))
682 ;;; Same as reg/mem, but with the immediate value occurring by default,
683 ;;; and with an appropiate printer.
684 (sb!disassem:define-instruction-format (reg/mem-imm 16
687 '(:name :tab reg/mem ", " imm))
688 (reg/mem :type 'sized-reg/mem)
689 (imm :type 'signed-imm-data))
691 ;;; Same as reg/mem, but with using the accumulator in the default printer
692 (sb!disassem:define-instruction-format
694 :include 'reg/mem :default-printer '(:name :tab accum ", " reg/mem))
695 (reg/mem :type 'reg/mem) ; don't need a size
696 (accum :type 'accum))
698 ;;; Same as reg-reg/mem, but with a prefix of #b00001111
699 (sb!disassem:define-instruction-format (ext-reg-reg/mem 24
701 `(:name :tab reg ", " reg/mem))
702 (prefix :field (byte 8 0) :value #b00001111)
703 (op :field (byte 7 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)
711 (sb!disassem:define-instruction-format (ext-reg-reg/mem-no-width 24
713 `(:name :tab reg ", " reg/mem))
714 (prefix :field (byte 8 0) :value #b00001111)
715 (op :field (byte 8 8))
716 (reg/mem :fields (list (byte 2 22) (byte 3 16))
718 (reg :field (byte 3 19) :type 'reg)
722 (sb!disassem:define-instruction-format (ext-reg/mem-no-width 24
724 `(:name :tab reg/mem))
725 (prefix :field (byte 8 0) :value #b00001111)
726 (op :fields (list (byte 8 8) (byte 3 19)))
727 (reg/mem :fields (list (byte 2 22) (byte 3 16))
730 ;;; reg-no-width with #x0f prefix
731 (sb!disassem:define-instruction-format (ext-reg-no-width 16
732 :default-printer '(:name :tab reg))
733 (prefix :field (byte 8 0) :value #b00001111)
734 (op :field (byte 5 11))
735 (reg :field (byte 3 8) :type 'reg-b))
737 ;;; Same as reg/mem, but with a prefix of #b00001111
738 (sb!disassem:define-instruction-format (ext-reg/mem 24
739 :default-printer '(:name :tab reg/mem))
740 (prefix :field (byte 8 0) :value #b00001111)
741 (op :fields (list (byte 7 9) (byte 3 19)))
742 (width :field (byte 1 8) :type 'width)
743 (reg/mem :fields (list (byte 2 22) (byte 3 16))
744 :type 'sized-reg/mem)
748 (sb!disassem:define-instruction-format (ext-reg/mem-imm 24
749 :include 'ext-reg/mem
751 '(:name :tab reg/mem ", " imm))
752 (imm :type 'signed-imm-data))
754 ;;;; XMM instructions
756 ;;; All XMM instructions use an extended opcode (#x0F as the first
757 ;;; opcode byte). Therefore in the following "EXT" in the name of the
758 ;;; instruction formats refers to the formats that have an additional
759 ;;; prefix (#x66, #xF2 or #xF3).
761 ;;; Instructions having an XMM register as the destination operand
762 ;;; and an XMM register or a memory location as the source operand.
763 ;;; The size of the operands is implicitly given by the instruction.
764 (sb!disassem:define-instruction-format (xmm-xmm/mem 24
766 '(:name :tab reg ", " reg/mem))
767 (x0f :field (byte 8 0) :value #x0f)
768 (op :field (byte 8 8))
769 (reg/mem :fields (list (byte 2 22) (byte 3 16))
771 (reg :field (byte 3 19) :type 'xmmreg)
775 (sb!disassem:define-instruction-format (ext-xmm-xmm/mem 32
777 '(:name :tab reg ", " reg/mem))
778 (prefix :field (byte 8 0))
779 (x0f :field (byte 8 8) :value #x0f)
780 (op :field (byte 8 16))
781 (reg/mem :fields (list (byte 2 30) (byte 3 24))
783 (reg :field (byte 3 27) :type 'xmmreg)
786 (sb!disassem:define-instruction-format (ext-rex-xmm-xmm/mem 40
788 '(:name :tab reg ", " reg/mem))
789 (prefix :field (byte 8 0))
790 (rex :field (byte 4 12) :value #b0100)
791 (wrxb :field (byte 4 8) :type 'wrxb)
792 (x0f :field (byte 8 16) :value #x0f)
793 (op :field (byte 8 24))
794 (reg/mem :fields (list (byte 2 38) (byte 3 32))
796 (reg :field (byte 3 35) :type 'xmmreg)
799 (sb!disassem:define-instruction-format (ext-2byte-xmm-xmm/mem 40
801 '(:name :tab reg ", " reg/mem))
802 (prefix :field (byte 8 0))
803 (x0f :field (byte 8 8) :value #x0f)
804 (op1 :field (byte 8 16)) ; #x38 or #x3a
805 (op2 :field (byte 8 24))
806 (reg/mem :fields (list (byte 2 38) (byte 3 32))
808 (reg :field (byte 3 35) :type 'xmmreg))
810 (sb!disassem:define-instruction-format (ext-rex-2byte-xmm-xmm/mem 48
812 '(:name :tab reg ", " reg/mem))
813 (prefix :field (byte 8 0))
814 (rex :field (byte 4 12) :value #b0100)
815 (wrxb :field (byte 4 8) :type 'wrxb)
816 (x0f :field (byte 8 16) :value #x0f)
817 (op1 :field (byte 8 24)) ; #x38 or #x3a
818 (op2 :field (byte 8 32))
819 (reg/mem :fields (list (byte 2 46) (byte 3 40))
821 (reg :field (byte 3 43) :type 'xmmreg))
823 ;;; Same as xmm-xmm/mem etc., but with direction bit.
825 (sb!disassem:define-instruction-format (ext-xmm-xmm/mem-dir 32
826 :include 'ext-xmm-xmm/mem
830 ,(swap-if 'dir 'reg ", " 'reg/mem)))
831 (op :field (byte 7 17))
832 (dir :field (byte 1 16)))
834 (sb!disassem:define-instruction-format (ext-rex-xmm-xmm/mem-dir 40
835 :include 'ext-rex-xmm-xmm/mem
839 ,(swap-if 'dir 'reg ", " 'reg/mem)))
840 (op :field (byte 7 25))
841 (dir :field (byte 1 24)))
843 ;;; Instructions having an XMM register as one operand
844 ;;; and a constant (unsigned) byte as the other.
846 (sb!disassem:define-instruction-format (ext-xmm-imm 32
848 '(:name :tab reg/mem ", " imm))
849 (prefix :field (byte 8 0))
850 (x0f :field (byte 8 8) :value #x0f)
851 (op :field (byte 8 16))
852 (/i :field (byte 3 27))
853 (b11 :field (byte 2 30) :value #b11)
854 (reg/mem :field (byte 3 24)
856 (imm :type 'imm-byte))
858 (sb!disassem:define-instruction-format (ext-rex-xmm-imm 40
860 '(:name :tab reg/mem ", " imm))
861 (prefix :field (byte 8 0))
862 (rex :field (byte 4 12) :value #b0100)
863 (wrxb :field (byte 4 8) :type 'wrxb)
864 (x0f :field (byte 8 16) :value #x0f)
865 (op :field (byte 8 24))
866 (/i :field (byte 3 35))
867 (b11 :field (byte 2 38) :value #b11)
868 (reg/mem :field (byte 3 32)
870 (imm :type 'imm-byte))
872 ;;; Instructions having an XMM register as one operand and a general-
873 ;;; -purpose register or a memory location as the other operand.
875 (sb!disassem:define-instruction-format (xmm-reg/mem 24
877 '(:name :tab reg ", " reg/mem))
878 (x0f :field (byte 8 0) :value #x0f)
879 (op :field (byte 8 8))
880 (reg/mem :fields (list (byte 2 22) (byte 3 16))
881 :type 'sized-reg/mem)
882 (reg :field (byte 3 19) :type 'xmmreg))
884 (sb!disassem:define-instruction-format (ext-xmm-reg/mem 32
886 '(:name :tab reg ", " reg/mem))
887 (prefix :field (byte 8 0))
888 (x0f :field (byte 8 8) :value #x0f)
889 (op :field (byte 8 16))
890 (reg/mem :fields (list (byte 2 30) (byte 3 24))
891 :type 'sized-reg/mem)
892 (reg :field (byte 3 27) :type 'xmmreg))
894 (sb!disassem:define-instruction-format (ext-rex-xmm-reg/mem 40
896 '(:name :tab reg ", " reg/mem))
897 (prefix :field (byte 8 0))
898 (rex :field (byte 4 12) :value #b0100)
899 (wrxb :field (byte 4 8) :type 'wrxb)
900 (x0f :field (byte 8 16) :value #x0f)
901 (op :field (byte 8 24))
902 (reg/mem :fields (list (byte 2 38) (byte 3 32))
903 :type 'sized-reg/mem)
904 (reg :field (byte 3 35) :type 'xmmreg))
906 ;;; Instructions having a general-purpose register as one operand and an
907 ;;; XMM register or a memory location as the other operand.
909 (sb!disassem:define-instruction-format (reg-xmm/mem 24
911 '(:name :tab reg ", " reg/mem))
912 (x0f :field (byte 8 0) :value #x0f)
913 (op :field (byte 8 8))
914 (reg/mem :fields (list (byte 2 22) (byte 3 16))
916 (reg :field (byte 3 19) :type 'reg))
918 (sb!disassem:define-instruction-format (ext-reg-xmm/mem 32
920 '(:name :tab reg ", " reg/mem))
921 (prefix :field (byte 8 0))
922 (x0f :field (byte 8 8) :value #x0f)
923 (op :field (byte 8 16))
924 (reg/mem :fields (list (byte 2 30) (byte 3 24))
926 (reg :field (byte 3 27) :type 'reg))
928 (sb!disassem:define-instruction-format (ext-rex-reg-xmm/mem 40
930 '(:name :tab reg ", " reg/mem))
931 (prefix :field (byte 8 0))
932 (rex :field (byte 4 12) :value #b0100)
933 (wrxb :field (byte 4 8) :type 'wrxb)
934 (x0f :field (byte 8 16) :value #x0f)
935 (op :field (byte 8 24))
936 (reg/mem :fields (list (byte 2 38) (byte 3 32))
938 (reg :field (byte 3 35) :type 'reg))
940 ;; XMM comparison instruction
942 (eval-when (:compile-toplevel :load-toplevel :execute)
943 (defparameter *sse-conditions* #(:eq :lt :le :unord :neq :nlt :nle :ord)))
945 (sb!disassem:define-arg-type sse-condition-code
946 ;; Inherit the prefilter from IMM-BYTE to READ-SUFFIX the byte.
948 :printer *sse-conditions*)
950 (sb!disassem:define-instruction-format (string-op 8
952 :default-printer '(:name width)))
954 (sb!disassem:define-instruction-format (short-cond-jump 16)
955 (op :field (byte 4 4))
956 (cc :field (byte 4 0) :type 'condition-code)
957 (label :field (byte 8 8) :type 'displacement))
959 (sb!disassem:define-instruction-format (short-jump 16
960 :default-printer '(:name :tab label))
961 (const :field (byte 4 4) :value #b1110)
962 (op :field (byte 4 0))
963 (label :field (byte 8 8) :type 'displacement))
965 (sb!disassem:define-instruction-format (near-cond-jump 16)
966 (op :fields (list (byte 8 0) (byte 4 12)) :value '(#b00001111 #b1000))
967 (cc :field (byte 4 8) :type 'condition-code)
968 ;; The disassembler currently doesn't let you have an instruction > 32 bits
969 ;; long, so we fake it by using a prefilter to read the offset.
970 (label :type 'displacement
971 :prefilter (lambda (value dstate)
972 (declare (ignore value)) ; always nil anyway
973 (sb!disassem:read-signed-suffix 32 dstate))))
975 (sb!disassem:define-instruction-format (near-jump 8
976 :default-printer '(:name :tab label))
977 (op :field (byte 8 0))
978 ;; The disassembler currently doesn't let you have an instruction > 32 bits
979 ;; long, so we fake it by using a prefilter to read the address.
980 (label :type 'displacement
981 :prefilter (lambda (value dstate)
982 (declare (ignore value)) ; always nil anyway
983 (sb!disassem:read-signed-suffix 32 dstate))))
986 (sb!disassem:define-instruction-format (cond-set 24
987 :default-printer '('set cc :tab reg/mem))
988 (prefix :field (byte 8 0) :value #b00001111)
989 (op :field (byte 4 12) :value #b1001)
990 (cc :field (byte 4 8) :type 'condition-code)
991 (reg/mem :fields (list (byte 2 22) (byte 3 16))
992 :type 'sized-byte-reg/mem)
993 (reg :field (byte 3 19) :value #b000))
995 (sb!disassem:define-instruction-format (cond-move 24
997 '('cmov cc :tab reg ", " reg/mem))
998 (prefix :field (byte 8 0) :value #b00001111)
999 (op :field (byte 4 12) :value #b0100)
1000 (cc :field (byte 4 8) :type 'condition-code)
1001 (reg/mem :fields (list (byte 2 22) (byte 3 16))
1003 (reg :field (byte 3 19) :type 'reg))
1005 (sb!disassem:define-instruction-format (enter-format 32
1006 :default-printer '(:name
1008 (:unless (:constant 0)
1010 (op :field (byte 8 0))
1011 (disp :field (byte 16 8))
1012 (level :field (byte 8 24)))
1014 ;;; Single byte instruction with an immediate byte argument.
1015 (sb!disassem:define-instruction-format (byte-imm 16
1016 :default-printer '(:name :tab code))
1017 (op :field (byte 8 0))
1018 (code :field (byte 8 8)))
1020 ;;; Two byte instruction with an immediate byte argument.
1022 (sb!disassem:define-instruction-format (word-imm 24
1023 :default-printer '(:name :tab code))
1024 (op :field (byte 16 0))
1025 (code :field (byte 8 16)))
1028 ;;;; primitive emitters
1030 (define-bitfield-emitter emit-word 16
1033 (define-bitfield-emitter emit-dword 32
1036 ;;; Most uses of dwords are as displacements or as immediate values in
1037 ;;; 64-bit operations. In these cases they are sign-extended to 64 bits.
1038 ;;; EMIT-DWORD is unsuitable there because it accepts values of type
1039 ;;; (OR (SIGNED-BYTE 32) (UNSIGNED-BYTE 32)), so we provide a more
1040 ;;; restricted emitter here.
1041 (defun emit-signed-dword (segment value)
1042 (declare (type segment segment)
1043 (type (signed-byte 32) value))
1044 (declare (inline emit-dword))
1045 (emit-dword segment value))
1047 (define-bitfield-emitter emit-qword 64
1050 (define-bitfield-emitter emit-byte-with-reg 8
1051 (byte 5 3) (byte 3 0))
1053 (define-bitfield-emitter emit-mod-reg-r/m-byte 8
1054 (byte 2 6) (byte 3 3) (byte 3 0))
1056 (define-bitfield-emitter emit-sib-byte 8
1057 (byte 2 6) (byte 3 3) (byte 3 0))
1059 (define-bitfield-emitter emit-rex-byte 8
1060 (byte 4 4) (byte 1 3) (byte 1 2) (byte 1 1) (byte 1 0))
1066 (defun emit-absolute-fixup (segment fixup &optional quad-p)
1067 (note-fixup segment (if quad-p :absolute64 :absolute) fixup)
1068 (let ((offset (fixup-offset fixup)))
1069 (if (label-p offset)
1070 (emit-back-patch segment
1072 (lambda (segment posn)
1073 (declare (ignore posn))
1074 (let ((val (- (+ (component-header-length)
1075 (or (label-position offset)
1077 other-pointer-lowtag)))
1079 (emit-qword segment val)
1080 (emit-signed-dword segment val)))))
1082 (emit-qword segment (or offset 0))
1083 (emit-signed-dword segment (or offset 0))))))
1085 (defun emit-relative-fixup (segment fixup)
1086 (note-fixup segment :relative fixup)
1087 (emit-signed-dword segment (or (fixup-offset fixup) 0)))
1090 ;;;; the effective-address (ea) structure
1092 (defun reg-tn-encoding (tn)
1093 (declare (type tn tn))
1094 ;; ea only has space for three bits of register number: regs r8
1095 ;; and up are selected by a REX prefix byte which caller is responsible
1096 ;; for having emitted where necessary already
1097 (ecase (sb-name (sc-sb (tn-sc tn)))
1099 (let ((offset (mod (tn-offset tn) 16)))
1100 (logior (ash (logand offset 1) 2)
1103 (mod (tn-offset tn) 8))))
1105 (defstruct (ea (:constructor make-ea (size &key base index scale disp))
1107 ;; note that we can represent an EA with a QWORD size, but EMIT-EA
1108 ;; can't actually emit it on its own: caller also needs to emit REX
1110 (size nil :type (member :byte :word :dword :qword))
1111 (base nil :type (or tn null))
1112 (index nil :type (or tn null))
1113 (scale 1 :type (member 1 2 4 8))
1114 (disp 0 :type (or (unsigned-byte 32) (signed-byte 32) fixup)))
1115 (def!method print-object ((ea ea) stream)
1116 (cond ((or *print-escape* *print-readably*)
1117 (print-unreadable-object (ea stream :type t)
1119 "~S~@[ base=~S~]~@[ index=~S~]~@[ scale=~S~]~@[ disp=~S~]"
1123 (let ((scale (ea-scale ea)))
1124 (if (= scale 1) nil scale))
1127 (format stream "~A PTR [" (symbol-name (ea-size ea)))
1129 (write-string (sb!c::location-print-name (ea-base ea)) stream)
1131 (write-string "+" stream)))
1133 (write-string (sb!c::location-print-name (ea-index ea)) stream))
1134 (unless (= (ea-scale ea) 1)
1135 (format stream "*~A" (ea-scale ea)))
1136 (typecase (ea-disp ea)
1139 (format stream "~@D" (ea-disp ea)))
1141 (format stream "+~A" (ea-disp ea))))
1142 (write-char #\] stream))))
1144 (defun emit-constant-tn-rip (segment constant-tn reg remaining-bytes)
1145 ;; AMD64 doesn't currently have a code object register to use as a
1146 ;; base register for constant access. Instead we use RIP-relative
1147 ;; addressing. The offset from the SIMPLE-FUN-HEADER to the instruction
1148 ;; is passed to the backpatch callback. In addition we need the offset
1149 ;; from the start of the function header to the slot in the CODE-HEADER
1150 ;; that stores the constant. Since we don't know where the code header
1151 ;; starts, instead count backwards from the function header.
1152 (let* ((2comp (component-info *component-being-compiled*))
1153 (constants (ir2-component-constants 2comp))
1154 (len (length constants))
1155 ;; Both CODE-HEADER and SIMPLE-FUN-HEADER are 16-byte aligned.
1156 ;; If there are an even amount of constants, there will be
1157 ;; an extra qword of padding before the function header, which
1158 ;; needs to be adjusted for. XXX: This will break if new slots
1159 ;; are added to the code header.
1160 (offset (* (- (+ len (if (evenp len)
1163 (tn-offset constant-tn))
1165 ;; RIP-relative addressing
1166 (emit-mod-reg-r/m-byte segment #b00 reg #b101)
1167 (emit-back-patch segment
1169 (lambda (segment posn)
1170 ;; The addressing is relative to end of instruction,
1171 ;; i.e. the end of this dword. Hence the + 4.
1172 (emit-signed-dword segment
1173 (+ 4 remaining-bytes
1174 (- (+ offset posn)))))))
1177 (defun emit-label-rip (segment fixup reg remaining-bytes)
1178 (let ((label (fixup-offset fixup)))
1179 ;; RIP-relative addressing
1180 (emit-mod-reg-r/m-byte segment #b00 reg #b101)
1181 (emit-back-patch segment
1183 (lambda (segment posn)
1184 (emit-signed-dword segment
1185 (- (label-position label)
1186 (+ posn 4 remaining-bytes))))))
1189 (defun emit-ea (segment thing reg &key allow-constants (remaining-bytes 0))
1192 ;; this would be eleganter if we had a function that would create
1194 (ecase (sb-name (sc-sb (tn-sc thing)))
1195 ((registers float-registers)
1196 (emit-mod-reg-r/m-byte segment #b11 reg (reg-tn-encoding thing)))
1198 ;; Convert stack tns into an index off RBP.
1199 (let ((disp (frame-byte-offset (tn-offset thing))))
1200 (cond ((<= -128 disp 127)
1201 (emit-mod-reg-r/m-byte segment #b01 reg #b101)
1202 (emit-byte segment disp))
1204 (emit-mod-reg-r/m-byte segment #b10 reg #b101)
1205 (emit-signed-dword segment disp)))))
1207 (unless allow-constants
1210 "Constant TNs can only be directly used in MOV, PUSH, and CMP."))
1211 (emit-constant-tn-rip segment thing reg remaining-bytes))))
1213 (let* ((base (ea-base thing))
1214 (index (ea-index thing))
1215 (scale (ea-scale thing))
1216 (disp (ea-disp thing))
1217 (mod (cond ((or (null base)
1219 (not (= (reg-tn-encoding base) #b101))))
1221 ((and (fixnump disp) (<= -128 disp 127))
1225 (r/m (cond (index #b100)
1227 (t (reg-tn-encoding base)))))
1228 (when (and (fixup-p disp)
1229 (label-p (fixup-offset disp)))
1232 (return-from emit-ea (emit-ea segment disp reg
1233 :allow-constants allow-constants
1234 :remaining-bytes remaining-bytes)))
1235 (when (and (= mod 0) (= r/m #b101))
1236 ;; this is rip-relative in amd64, so we'll use a sib instead
1237 (setf r/m #b100 scale 1))
1238 (emit-mod-reg-r/m-byte segment mod reg r/m)
1240 (let ((ss (1- (integer-length scale)))
1241 (index (if (null index)
1243 (let ((index (reg-tn-encoding index)))
1245 (error "can't index off of ESP")
1247 (base (if (null base)
1249 (reg-tn-encoding base))))
1250 (emit-sib-byte segment ss index base)))
1252 (emit-byte segment disp))
1253 ((or (= mod #b10) (null base))
1255 (emit-absolute-fixup segment disp)
1256 (emit-signed-dword segment disp))))))
1258 (typecase (fixup-offset thing)
1260 (emit-label-rip segment thing reg remaining-bytes))
1262 (emit-mod-reg-r/m-byte segment #b00 reg #b100)
1263 (emit-sib-byte segment 0 #b100 #b101)
1264 (emit-absolute-fixup segment thing))))))
1266 (defun byte-reg-p (thing)
1268 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)
1269 (member (sc-name (tn-sc thing)) *byte-sc-names*)
1272 (defun byte-ea-p (thing)
1274 (ea (eq (ea-size thing) :byte))
1276 (and (member (sc-name (tn-sc thing)) *byte-sc-names*) t))
1279 (defun word-reg-p (thing)
1281 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)
1282 (member (sc-name (tn-sc thing)) *word-sc-names*)
1285 (defun word-ea-p (thing)
1287 (ea (eq (ea-size thing) :word))
1288 (tn (and (member (sc-name (tn-sc thing)) *word-sc-names*) t))
1291 (defun dword-reg-p (thing)
1293 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)
1294 (member (sc-name (tn-sc thing)) *dword-sc-names*)
1297 (defun dword-ea-p (thing)
1299 (ea (eq (ea-size thing) :dword))
1301 (and (member (sc-name (tn-sc thing)) *dword-sc-names*) t))
1304 (defun qword-reg-p (thing)
1306 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)
1307 (member (sc-name (tn-sc thing)) *qword-sc-names*)
1310 (defun qword-ea-p (thing)
1312 (ea (eq (ea-size thing) :qword))
1314 (and (member (sc-name (tn-sc thing)) *qword-sc-names*) t))
1317 ;;; Return true if THING is a general-purpose register TN.
1318 (defun register-p (thing)
1320 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)))
1322 (defun accumulator-p (thing)
1323 (and (register-p thing)
1324 (= (tn-offset thing) 0)))
1326 ;;; Return true if THING is an XMM register TN.
1327 (defun xmm-register-p (thing)
1329 (eq (sb-name (sc-sb (tn-sc thing))) 'float-registers)))
1334 (def!constant +operand-size-prefix-byte+ #b01100110)
1336 (defun maybe-emit-operand-size-prefix (segment size)
1337 (unless (or (eq size :byte)
1338 (eq size :qword) ; REX prefix handles this
1339 (eq size +default-operand-size+))
1340 (emit-byte segment +operand-size-prefix-byte+)))
1342 ;;; A REX prefix must be emitted if at least one of the following
1343 ;;; conditions is true:
1344 ;; 1. The operand size is :QWORD and the default operand size of the
1345 ;; instruction is not :QWORD.
1346 ;;; 2. The instruction references an extended register.
1347 ;;; 3. The instruction references one of the byte registers SIL, DIL,
1350 ;;; Emit a REX prefix if necessary. OPERAND-SIZE is used to determine
1351 ;;; whether to set REX.W. Callers pass it explicitly as :DO-NOT-SET if
1352 ;;; this should not happen, for example because the instruction's
1353 ;;; default operand size is qword. R, X and B are NIL or TNs specifying
1354 ;;; registers the encodings of which are extended with the REX.R, REX.X
1355 ;;; and REX.B bit, respectively. To determine whether one of the byte
1356 ;;; registers is used that can only be accessed using a REX prefix, we
1357 ;;; need only to test R and B, because X is only used for the index
1358 ;;; register of an effective address and therefore never byte-sized.
1359 ;;; For R we can avoid to calculate the size of the TN because it is
1360 ;;; always OPERAND-SIZE. The size of B must be calculated here because
1361 ;;; B can be address-sized (if it is the base register of an effective
1362 ;;; address), of OPERAND-SIZE (if the instruction operates on two
1363 ;;; registers) or of some different size (in the instructions that
1364 ;;; combine arguments of different sizes: MOVZX, MOVSX, MOVSXD and
1365 ;;; several SSE instructions, e.g. CVTSD2SI). We don't distinguish
1366 ;;; between general-purpose and floating point registers for this cause
1367 ;;; because only general-purpose registers can be byte-sized at all.
1368 (defun maybe-emit-rex-prefix (segment operand-size r x b)
1369 (declare (type (member nil :byte :word :dword :qword :do-not-set)
1371 (type (or null tn) r x b))
1373 (if (and r (> (tn-offset r)
1374 ;; offset of r8 is 16, offset of xmm8 is 8
1375 (if (eq (sb-name (sc-sb (tn-sc r)))
1382 ;; Assuming R is a TN describing a general-purpose
1383 ;; register, return true if it references register
1385 (<= 8 (tn-offset r) 15)))
1386 (let ((rex-w (if (eq operand-size :qword) 1 0))
1390 (when (or (not (zerop (logior rex-w rex-r rex-x rex-b)))
1392 (eq operand-size :byte)
1395 (eq (operand-size b) :byte)
1397 (emit-rex-byte segment #b0100 rex-w rex-r rex-x rex-b)))))
1399 ;;; Emit a REX prefix if necessary. The operand size is determined from
1400 ;;; THING or can be overwritten by OPERAND-SIZE. This and REG are always
1401 ;;; passed to MAYBE-EMIT-REX-PREFIX. Additionally, if THING is an EA we
1402 ;;; pass its index and base registers, if it is a register TN, we pass
1404 ;;; In contrast to EMIT-EA above, neither stack TNs nor fixups need to
1405 ;;; be treated specially here: If THING is a stack TN, neither it nor
1406 ;;; any of its components are passed to MAYBE-EMIT-REX-PREFIX which
1407 ;;; works correctly because stack references always use RBP as the base
1408 ;;; register and never use an index register so no extended registers
1409 ;;; need to be accessed. Fixups are assembled using an addressing mode
1410 ;;; of displacement-only or RIP-plus-displacement (see EMIT-EA), so may
1411 ;;; not reference an extended register. The displacement-only addressing
1412 ;;; mode requires that REX.X is 0, which is ensured here.
1413 (defun maybe-emit-rex-for-ea (segment thing reg &key operand-size)
1414 (declare (type (or ea tn fixup) thing)
1415 (type (or null tn) reg)
1416 (type (member nil :byte :word :dword :qword :do-not-set)
1418 (let ((ea-p (ea-p thing)))
1419 (maybe-emit-rex-prefix segment
1420 (or operand-size (operand-size thing))
1422 (and ea-p (ea-index thing))
1423 (cond (ea-p (ea-base thing))
1425 (member (sb-name (sc-sb (tn-sc thing)))
1426 '(float-registers registers)))
1430 (defun operand-size (thing)
1433 ;; FIXME: might as well be COND instead of having to use #. readmacro
1434 ;; to hack up the code
1435 (case (sc-name (tn-sc thing))
1444 ;; added by jrd: float-registers is a separate size (?)
1445 ;; The only place in the code where we are called with THING
1446 ;; being a float-register is in MAYBE-EMIT-REX-PREFIX when it
1447 ;; checks whether THING is a byte register. Thus our result in
1448 ;; these cases could as well be :dword and :qword. I leave it as
1449 ;; :float and :double which is more likely to trigger an aver
1450 ;; instead of silently doing the wrong thing in case this
1451 ;; situation should change. Lutz Euler, 2005-10-23.
1454 (#.*double-sc-names*
1456 (#.*complex-sc-names*
1459 (error "can't tell the size of ~S ~S" thing (sc-name (tn-sc thing))))))
1463 ;; GNA. Guess who spelt "flavor" correctly first time round?
1464 ;; There's a strong argument in my mind to change all uses of
1465 ;; "flavor" to "kind": and similarly with some misguided uses of
1466 ;; "type" here and there. -- CSR, 2005-01-06.
1467 (case (fixup-flavor thing)
1468 ((:foreign-dataref) :qword)))
1472 (defun matching-operand-size (dst src)
1473 (let ((dst-size (operand-size dst))
1474 (src-size (operand-size src)))
1477 (if (eq dst-size src-size)
1479 (error "size mismatch: ~S is a ~S and ~S is a ~S."
1480 dst dst-size src src-size))
1484 (error "can't tell the size of either ~S or ~S" dst src)))))
1486 ;;; Except in a very few cases (MOV instructions A1, A3 and B8 - BF)
1487 ;;; we expect dword data bytes even when 64 bit work is being done.
1488 ;;; But A1 and A3 are currently unused and B8 - BF use EMIT-QWORD
1489 ;;; directly, so we emit all quad constants as dwords, additionally
1490 ;;; making sure that they survive the sign-extension to 64 bits
1492 (defun emit-sized-immediate (segment size value)
1495 (emit-byte segment value))
1497 (emit-word segment value))
1499 (emit-dword segment value))
1501 (emit-signed-dword segment value))))
1505 (define-instruction rex (segment)
1506 (:printer rex () nil :print-name nil)
1508 (bug "REX prefix used as a standalone instruction")))
1510 (define-instruction x66 (segment)
1511 (:printer x66 () nil :print-name nil)
1513 (bug "#X66 prefix used as a standalone instruction")))
1515 (defun emit-prefix (segment name)
1516 (declare (ignorable segment))
1521 (emit-byte segment #xf0))))
1523 (define-instruction lock (segment)
1524 (:printer byte ((op #b11110000)) nil)
1526 (bug "LOCK prefix used as a standalone instruction")))
1528 (define-instruction rep (segment)
1530 (emit-byte segment #b11110011)))
1532 (define-instruction repe (segment)
1533 (:printer byte ((op #b11110011)) nil)
1535 (emit-byte segment #b11110011)))
1537 (define-instruction repne (segment)
1538 (:printer byte ((op #b11110010)) nil)
1540 (emit-byte segment #b11110010)))
1542 ;;;; general data transfer
1544 ;;; This is the part of the MOV instruction emitter that does moving
1545 ;;; of an immediate value into a qword register. We go to some length
1546 ;;; to achieve the shortest possible encoding.
1547 (defun emit-immediate-move-to-qword-register (segment dst src)
1548 (declare (type integer src))
1549 (cond ((typep src '(unsigned-byte 32))
1550 ;; We use the B8 - BF encoding with an operand size of 32 bits
1551 ;; here and let the implicit zero-extension fill the upper half
1552 ;; of the 64-bit destination register. Instruction size: five
1553 ;; or six bytes. (A REX prefix will be emitted only if the
1554 ;; destination is an extended register.)
1555 (maybe-emit-rex-prefix segment :dword nil nil dst)
1556 (emit-byte-with-reg segment #b10111 (reg-tn-encoding dst))
1557 (emit-dword segment src))
1559 (maybe-emit-rex-prefix segment :qword nil nil dst)
1560 (cond ((typep src '(signed-byte 32))
1561 ;; Use the C7 encoding that takes a 32-bit immediate and
1562 ;; sign-extends it to 64 bits. Instruction size: seven
1564 (emit-byte segment #b11000111)
1565 (emit-mod-reg-r/m-byte segment #b11 #b000
1566 (reg-tn-encoding dst))
1567 (emit-signed-dword segment src))
1568 ((<= (- (expt 2 64) (expt 2 31))
1571 ;; This triggers on positive integers of 64 bits length
1572 ;; with the most significant 33 bits being 1. We use the
1573 ;; same encoding as in the previous clause.
1574 (emit-byte segment #b11000111)
1575 (emit-mod-reg-r/m-byte segment #b11 #b000
1576 (reg-tn-encoding dst))
1577 (emit-signed-dword segment (- src (expt 2 64))))
1579 ;; We need a full 64-bit immediate. Instruction size:
1581 (emit-byte-with-reg segment #b10111 (reg-tn-encoding dst))
1582 (emit-qword segment src))))))
1584 (define-instruction mov (segment dst src)
1585 ;; immediate to register
1586 (:printer reg ((op #b1011) (imm nil :type 'signed-imm-data))
1587 '(:name :tab reg ", " imm))
1588 (:printer rex-reg ((op #b1011) (imm nil :type 'signed-imm-data-upto-qword))
1589 '(:name :tab reg ", " imm))
1590 ;; absolute mem to/from accumulator
1591 (:printer simple-dir ((op #b101000) (imm nil :type 'imm-addr))
1592 `(:name :tab ,(swap-if 'dir 'accum ", " '("[" imm "]"))))
1593 ;; register to/from register/memory
1594 (:printer reg-reg/mem-dir ((op #b100010)))
1595 ;; immediate to register/memory
1596 (:printer reg/mem-imm ((op '(#b1100011 #b000))))
1599 (let ((size (matching-operand-size dst src)))
1600 (maybe-emit-operand-size-prefix segment size)
1601 (cond ((register-p dst)
1602 (cond ((integerp src)
1603 (cond ((eq size :qword)
1604 (emit-immediate-move-to-qword-register segment
1607 (maybe-emit-rex-prefix segment size nil nil dst)
1608 (emit-byte-with-reg segment
1612 (reg-tn-encoding dst))
1613 (emit-sized-immediate segment size src))))
1615 (maybe-emit-rex-for-ea segment src dst)
1620 (emit-ea segment src (reg-tn-encoding dst) :allow-constants t))))
1622 ;; C7 only deals with 32 bit immediates even if the
1623 ;; destination is a 64-bit location. The value is
1624 ;; sign-extended in this case.
1625 (maybe-emit-rex-for-ea segment dst nil)
1626 (emit-byte segment (if (eq size :byte) #b11000110 #b11000111))
1627 (emit-ea segment dst #b000)
1628 (emit-sized-immediate segment size src))
1630 (maybe-emit-rex-for-ea segment dst src)
1631 (emit-byte segment (if (eq size :byte) #b10001000 #b10001001))
1632 (emit-ea segment dst (reg-tn-encoding src)))
1634 ;; Generally we can't MOV a fixupped value into an EA, since
1635 ;; MOV on non-registers can only take a 32-bit immediate arg.
1636 ;; Make an exception for :FOREIGN fixups (pretty much just
1637 ;; the runtime asm, since other foreign calls go through the
1638 ;; the linkage table) and for linkage table references, since
1639 ;; these should always end up in low memory.
1640 (aver (or (eq (fixup-flavor src) :foreign)
1641 (eq (fixup-flavor src) :foreign-dataref)
1642 (eq (ea-size dst) :dword)))
1643 (maybe-emit-rex-for-ea segment dst nil)
1644 (emit-byte segment #b11000111)
1645 (emit-ea segment dst #b000)
1646 (emit-absolute-fixup segment src))
1648 (error "bogus arguments to MOV: ~S ~S" dst src))))))
1650 ;;; Emit a sign-extending (if SIGNED-P is true) or zero-extending move.
1651 ;;; To achieve the shortest possible encoding zero extensions into a
1652 ;;; 64-bit destination are assembled as a straight 32-bit MOV (if the
1653 ;;; source size is 32 bits) or as MOVZX with a 32-bit destination (if
1654 ;;; the source size is 8 or 16 bits). Due to the implicit zero extension
1655 ;;; to 64 bits this has the same effect as a MOVZX with 64-bit
1656 ;;; destination but often needs no REX prefix.
1657 (defun emit-move-with-extension (segment dst src signed-p)
1658 (aver (register-p dst))
1659 (let ((dst-size (operand-size dst))
1660 (src-size (operand-size src))
1661 (opcode (if signed-p #b10111110 #b10110110)))
1662 (macrolet ((emitter (operand-size &rest bytes)
1664 (maybe-emit-rex-for-ea segment src dst
1665 :operand-size ,operand-size)
1666 ,@(mapcar (lambda (byte)
1667 `(emit-byte segment ,byte))
1669 (emit-ea segment src (reg-tn-encoding dst)))))
1672 (aver (eq src-size :byte))
1673 (maybe-emit-operand-size-prefix segment :word)
1674 (emitter :word #b00001111 opcode))
1677 (setf dst-size :dword))
1680 (emitter dst-size #b00001111 opcode))
1682 (emitter dst-size #b00001111 (logior opcode 1)))
1684 (aver (or (not signed-p) (eq dst-size :qword)))
1686 (if signed-p #x63 #x8b))))))))) ; movsxd or straight mov
1688 (define-instruction movsx (segment dst src)
1689 (:printer ext-reg-reg/mem-no-width
1690 ((op #b10111110) (reg/mem nil :type 'sized-byte-reg/mem)))
1691 (:printer ext-reg-reg/mem-no-width
1692 ((op #b10111111) (reg/mem nil :type 'sized-word-reg/mem)))
1693 (:emitter (emit-move-with-extension segment dst src :signed)))
1695 (define-instruction movzx (segment dst src)
1696 (:printer ext-reg-reg/mem-no-width
1697 ((op #b10110110) (reg/mem nil :type 'sized-byte-reg/mem)))
1698 (:printer ext-reg-reg/mem-no-width
1699 ((op #b10110111) (reg/mem nil :type 'sized-word-reg/mem)))
1700 (:emitter (emit-move-with-extension segment dst src nil)))
1702 ;;; The regular use of MOVSXD is with an operand size of :qword. This
1703 ;;; sign-extends the dword source into the qword destination register.
1704 ;;; If the operand size is :dword the instruction zero-extends the dword
1705 ;;; source into the qword destination register, i.e. it does the same as
1706 ;;; a dword MOV into a register.
1707 (define-instruction movsxd (segment dst src)
1708 (:printer reg-reg/mem ((op #b0110001) (width 1)
1709 (reg/mem nil :type 'sized-dword-reg/mem)))
1710 (:emitter (emit-move-with-extension segment dst src :signed)))
1712 ;;; this is not a real amd64 instruction, of course
1713 (define-instruction movzxd (segment dst src)
1714 ; (:printer reg-reg/mem ((op #x63) (reg nil :type 'reg)))
1715 (:emitter (emit-move-with-extension segment dst src nil)))
1717 (define-instruction push (segment src)
1719 (:printer reg-no-width-default-qword ((op #b01010)))
1721 (:printer reg/mem-default-qword ((op '(#b11111111 #b110))))
1723 (:printer byte ((op #b01101010) (imm nil :type 'signed-imm-byte))
1725 (:printer byte ((op #b01101000)
1726 (imm nil :type 'signed-imm-data-default-qword))
1728 ;; ### segment registers?
1731 (cond ((integerp src)
1732 (cond ((<= -128 src 127)
1733 (emit-byte segment #b01101010)
1734 (emit-byte segment src))
1736 ;; A REX-prefix is not needed because the operand size
1737 ;; defaults to 64 bits. The size of the immediate is 32
1738 ;; bits and it is sign-extended.
1739 (emit-byte segment #b01101000)
1740 (emit-signed-dword segment src))))
1742 (let ((size (operand-size src)))
1743 (aver (or (eq size :qword) (eq size :word)))
1744 (maybe-emit-operand-size-prefix segment size)
1745 (maybe-emit-rex-for-ea segment src nil :operand-size :do-not-set)
1746 (cond ((register-p src)
1747 (emit-byte-with-reg segment #b01010 (reg-tn-encoding src)))
1749 (emit-byte segment #b11111111)
1750 (emit-ea segment src #b110 :allow-constants t))))))))
1752 (define-instruction pop (segment dst)
1753 (:printer reg-no-width-default-qword ((op #b01011)))
1754 (:printer reg/mem-default-qword ((op '(#b10001111 #b000))))
1756 (let ((size (operand-size dst)))
1757 (aver (or (eq size :qword) (eq size :word)))
1758 (maybe-emit-operand-size-prefix segment size)
1759 (maybe-emit-rex-for-ea segment dst nil :operand-size :do-not-set)
1760 (cond ((register-p dst)
1761 (emit-byte-with-reg segment #b01011 (reg-tn-encoding dst)))
1763 (emit-byte segment #b10001111)
1764 (emit-ea segment dst #b000))))))
1766 (define-instruction xchg (segment operand1 operand2)
1767 ;; Register with accumulator.
1768 (:printer reg-no-width ((op #b10010)) '(:name :tab accum ", " reg))
1769 ;; Register/Memory with Register.
1770 (:printer reg-reg/mem ((op #b1000011)))
1772 (let ((size (matching-operand-size operand1 operand2)))
1773 (maybe-emit-operand-size-prefix segment size)
1774 (labels ((xchg-acc-with-something (acc something)
1775 (if (and (not (eq size :byte)) (register-p something))
1777 (maybe-emit-rex-for-ea segment acc something)
1778 (emit-byte-with-reg segment
1780 (reg-tn-encoding something)))
1781 (xchg-reg-with-something acc something)))
1782 (xchg-reg-with-something (reg something)
1783 (maybe-emit-rex-for-ea segment something reg)
1784 (emit-byte segment (if (eq size :byte) #b10000110 #b10000111))
1785 (emit-ea segment something (reg-tn-encoding reg))))
1786 (cond ((accumulator-p operand1)
1787 (xchg-acc-with-something operand1 operand2))
1788 ((accumulator-p operand2)
1789 (xchg-acc-with-something operand2 operand1))
1790 ((register-p operand1)
1791 (xchg-reg-with-something operand1 operand2))
1792 ((register-p operand2)
1793 (xchg-reg-with-something operand2 operand1))
1795 (error "bogus args to XCHG: ~S ~S" operand1 operand2)))))))
1797 (define-instruction lea (segment dst src)
1798 (:printer reg-reg/mem ((op #b1000110) (width 1)))
1800 (aver (or (dword-reg-p dst) (qword-reg-p dst)))
1801 (maybe-emit-rex-for-ea segment src dst
1802 :operand-size (if (dword-reg-p dst) :dword :qword))
1803 (emit-byte segment #b10001101)
1804 (emit-ea segment src (reg-tn-encoding dst))))
1806 (define-instruction cmpxchg (segment dst src &optional prefix)
1807 ;; Register/Memory with Register.
1808 (:printer ext-reg-reg/mem ((op #b1011000)) '(:name :tab reg/mem ", " reg))
1810 (aver (register-p src))
1811 (emit-prefix segment prefix)
1812 (let ((size (matching-operand-size src dst)))
1813 (maybe-emit-operand-size-prefix segment size)
1814 (maybe-emit-rex-for-ea segment dst src)
1815 (emit-byte segment #b00001111)
1816 (emit-byte segment (if (eq size :byte) #b10110000 #b10110001))
1817 (emit-ea segment dst (reg-tn-encoding src)))))
1820 ;;;; flag control instructions
1822 ;;; CLC -- Clear Carry Flag.
1823 (define-instruction clc (segment)
1824 (:printer byte ((op #b11111000)))
1826 (emit-byte segment #b11111000)))
1828 ;;; CLD -- Clear Direction Flag.
1829 (define-instruction cld (segment)
1830 (:printer byte ((op #b11111100)))
1832 (emit-byte segment #b11111100)))
1834 ;;; CLI -- Clear Iterrupt Enable Flag.
1835 (define-instruction cli (segment)
1836 (:printer byte ((op #b11111010)))
1838 (emit-byte segment #b11111010)))
1840 ;;; CMC -- Complement Carry Flag.
1841 (define-instruction cmc (segment)
1842 (:printer byte ((op #b11110101)))
1844 (emit-byte segment #b11110101)))
1846 ;;; LAHF -- Load AH into flags.
1847 (define-instruction lahf (segment)
1848 (:printer byte ((op #b10011111)))
1850 (emit-byte segment #b10011111)))
1852 ;;; POPF -- Pop flags.
1853 (define-instruction popf (segment)
1854 (:printer byte ((op #b10011101)))
1856 (emit-byte segment #b10011101)))
1858 ;;; PUSHF -- push flags.
1859 (define-instruction pushf (segment)
1860 (:printer byte ((op #b10011100)))
1862 (emit-byte segment #b10011100)))
1864 ;;; SAHF -- Store AH into flags.
1865 (define-instruction sahf (segment)
1866 (:printer byte ((op #b10011110)))
1868 (emit-byte segment #b10011110)))
1870 ;;; STC -- Set Carry Flag.
1871 (define-instruction stc (segment)
1872 (:printer byte ((op #b11111001)))
1874 (emit-byte segment #b11111001)))
1876 ;;; STD -- Set Direction Flag.
1877 (define-instruction std (segment)
1878 (:printer byte ((op #b11111101)))
1880 (emit-byte segment #b11111101)))
1882 ;;; STI -- Set Interrupt Enable Flag.
1883 (define-instruction sti (segment)
1884 (:printer byte ((op #b11111011)))
1886 (emit-byte segment #b11111011)))
1890 (defun emit-random-arith-inst (name segment dst src opcode
1891 &optional allow-constants)
1892 (let ((size (matching-operand-size dst src)))
1893 (maybe-emit-operand-size-prefix segment size)
1896 (cond ((and (not (eq size :byte)) (<= -128 src 127))
1897 (maybe-emit-rex-for-ea segment dst nil)
1898 (emit-byte segment #b10000011)
1899 (emit-ea segment dst opcode :allow-constants allow-constants)
1900 (emit-byte segment src))
1901 ((accumulator-p dst)
1902 (maybe-emit-rex-for-ea segment dst nil)
1909 (emit-sized-immediate segment size src))
1911 (maybe-emit-rex-for-ea segment dst nil)
1912 (emit-byte segment (if (eq size :byte) #b10000000 #b10000001))
1913 (emit-ea segment dst opcode :allow-constants allow-constants)
1914 (emit-sized-immediate segment size src))))
1916 (maybe-emit-rex-for-ea segment dst src)
1920 (if (eq size :byte) #b00000000 #b00000001)))
1921 (emit-ea segment dst (reg-tn-encoding src) :allow-constants allow-constants))
1923 (maybe-emit-rex-for-ea segment src dst)
1927 (if (eq size :byte) #b00000010 #b00000011)))
1928 (emit-ea segment src (reg-tn-encoding dst) :allow-constants allow-constants))
1930 (error "bogus operands to ~A" name)))))
1932 (eval-when (:compile-toplevel :execute)
1933 (defun arith-inst-printer-list (subop)
1934 `((accum-imm ((op ,(dpb subop (byte 3 2) #b0000010))))
1935 (reg/mem-imm ((op (#b1000000 ,subop))))
1936 ;; The redundant encoding #x82 is invalid in 64-bit mode,
1937 ;; therefore we force WIDTH to 1.
1938 (reg/mem-imm ((op (#b1000001 ,subop)) (width 1)
1939 (imm nil :type signed-imm-byte)))
1940 (reg-reg/mem-dir ((op ,(dpb subop (byte 3 1) #b000000)))))))
1942 (define-instruction add (segment dst src &optional prefix)
1943 (:printer-list (arith-inst-printer-list #b000))
1945 (emit-prefix segment prefix)
1946 (emit-random-arith-inst "ADD" segment dst src #b000)))
1948 (define-instruction adc (segment dst src)
1949 (:printer-list (arith-inst-printer-list #b010))
1950 (:emitter (emit-random-arith-inst "ADC" segment dst src #b010)))
1952 (define-instruction sub (segment dst src)
1953 (:printer-list (arith-inst-printer-list #b101))
1954 (:emitter (emit-random-arith-inst "SUB" segment dst src #b101)))
1956 (define-instruction sbb (segment dst src)
1957 (:printer-list (arith-inst-printer-list #b011))
1958 (:emitter (emit-random-arith-inst "SBB" segment dst src #b011)))
1960 (define-instruction cmp (segment dst src)
1961 (:printer-list (arith-inst-printer-list #b111))
1962 (:emitter (emit-random-arith-inst "CMP" segment dst src #b111 t)))
1964 ;;; The one-byte encodings for INC and DEC are used as REX prefixes
1965 ;;; in 64-bit mode so we always use the two-byte form.
1966 (define-instruction inc (segment dst)
1967 (:printer reg/mem ((op '(#b1111111 #b000))))
1969 (let ((size (operand-size dst)))
1970 (maybe-emit-operand-size-prefix segment size)
1971 (maybe-emit-rex-for-ea segment dst nil)
1972 (emit-byte segment (if (eq size :byte) #b11111110 #b11111111))
1973 (emit-ea segment dst #b000))))
1975 (define-instruction dec (segment dst)
1976 (:printer reg/mem ((op '(#b1111111 #b001))))
1978 (let ((size (operand-size dst)))
1979 (maybe-emit-operand-size-prefix segment size)
1980 (maybe-emit-rex-for-ea segment dst nil)
1981 (emit-byte segment (if (eq size :byte) #b11111110 #b11111111))
1982 (emit-ea segment dst #b001))))
1984 (define-instruction neg (segment dst)
1985 (:printer reg/mem ((op '(#b1111011 #b011))))
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) #b11110110 #b11110111))
1991 (emit-ea segment dst #b011))))
1993 (define-instruction mul (segment dst src)
1994 (:printer accum-reg/mem ((op '(#b1111011 #b100))))
1996 (let ((size (matching-operand-size dst src)))
1997 (aver (accumulator-p dst))
1998 (maybe-emit-operand-size-prefix segment size)
1999 (maybe-emit-rex-for-ea segment src nil)
2000 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2001 (emit-ea segment src #b100))))
2003 (define-instruction imul (segment dst &optional src1 src2)
2004 (:printer accum-reg/mem ((op '(#b1111011 #b101))))
2005 (:printer ext-reg-reg/mem-no-width ((op #b10101111)))
2006 (:printer reg-reg/mem ((op #b0110100) (width 1)
2007 (imm nil :type 'signed-imm-data))
2008 '(:name :tab reg ", " reg/mem ", " imm))
2009 (:printer reg-reg/mem ((op #b0110101) (width 1)
2010 (imm nil :type 'signed-imm-byte))
2011 '(:name :tab reg ", " reg/mem ", " imm))
2013 (flet ((r/m-with-immed-to-reg (reg r/m immed)
2014 (let* ((size (matching-operand-size reg r/m))
2015 (sx (and (not (eq size :byte)) (<= -128 immed 127))))
2016 (maybe-emit-operand-size-prefix segment size)
2017 (maybe-emit-rex-for-ea segment r/m reg)
2018 (emit-byte segment (if sx #b01101011 #b01101001))
2019 (emit-ea segment r/m (reg-tn-encoding reg))
2021 (emit-byte segment immed)
2022 (emit-sized-immediate segment size immed)))))
2024 (r/m-with-immed-to-reg dst src1 src2))
2027 (r/m-with-immed-to-reg dst dst src1)
2028 (let ((size (matching-operand-size dst src1)))
2029 (maybe-emit-operand-size-prefix segment size)
2030 (maybe-emit-rex-for-ea segment src1 dst)
2031 (emit-byte segment #b00001111)
2032 (emit-byte segment #b10101111)
2033 (emit-ea segment src1 (reg-tn-encoding dst)))))
2035 (let ((size (operand-size dst)))
2036 (maybe-emit-operand-size-prefix segment size)
2037 (maybe-emit-rex-for-ea segment dst nil)
2038 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2039 (emit-ea segment dst #b101)))))))
2041 (define-instruction div (segment dst src)
2042 (:printer accum-reg/mem ((op '(#b1111011 #b110))))
2044 (let ((size (matching-operand-size dst src)))
2045 (aver (accumulator-p dst))
2046 (maybe-emit-operand-size-prefix segment size)
2047 (maybe-emit-rex-for-ea segment src nil)
2048 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2049 (emit-ea segment src #b110))))
2051 (define-instruction idiv (segment dst src)
2052 (:printer accum-reg/mem ((op '(#b1111011 #b111))))
2054 (let ((size (matching-operand-size dst src)))
2055 (aver (accumulator-p dst))
2056 (maybe-emit-operand-size-prefix segment size)
2057 (maybe-emit-rex-for-ea segment src nil)
2058 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2059 (emit-ea segment src #b111))))
2061 (define-instruction bswap (segment dst)
2062 (:printer ext-reg-no-width ((op #b11001)))
2064 (let ((size (operand-size dst)))
2065 (maybe-emit-rex-prefix segment size nil nil dst)
2066 (emit-byte segment #x0f)
2067 (emit-byte-with-reg segment #b11001 (reg-tn-encoding dst)))))
2069 ;;; CBW -- Convert Byte to Word. AX <- sign_xtnd(AL)
2070 (define-instruction cbw (segment)
2071 (:printer x66-byte ((op #b10011000)))
2073 (maybe-emit-operand-size-prefix segment :word)
2074 (emit-byte segment #b10011000)))
2076 ;;; CWDE -- Convert Word To Double Word Extended. EAX <- sign_xtnd(AX)
2077 (define-instruction cwde (segment)
2078 (:printer byte ((op #b10011000)))
2080 (maybe-emit-operand-size-prefix segment :dword)
2081 (emit-byte segment #b10011000)))
2083 ;;; CDQE -- Convert Double Word To Quad Word Extended. RAX <- sign_xtnd(EAX)
2084 (define-instruction cdqe (segment)
2085 (:printer rex-byte ((op #b10011000)))
2087 (maybe-emit-rex-prefix segment :qword nil nil nil)
2088 (emit-byte segment #b10011000)))
2090 ;;; CWD -- Convert Word to Double Word. DX:AX <- sign_xtnd(AX)
2091 (define-instruction cwd (segment)
2092 (:printer x66-byte ((op #b10011001)))
2094 (maybe-emit-operand-size-prefix segment :word)
2095 (emit-byte segment #b10011001)))
2097 ;;; CDQ -- Convert Double Word to Quad Word. EDX:EAX <- sign_xtnd(EAX)
2098 (define-instruction cdq (segment)
2099 (:printer byte ((op #b10011001)))
2101 (maybe-emit-operand-size-prefix segment :dword)
2102 (emit-byte segment #b10011001)))
2104 ;;; CQO -- Convert Quad Word to Octaword. RDX:RAX <- sign_xtnd(RAX)
2105 (define-instruction cqo (segment)
2106 (:printer rex-byte ((op #b10011001)))
2108 (maybe-emit-rex-prefix segment :qword nil nil nil)
2109 (emit-byte segment #b10011001)))
2111 (define-instruction xadd (segment dst src &optional prefix)
2112 ;; Register/Memory with Register.
2113 (:printer ext-reg-reg/mem ((op #b1100000)) '(:name :tab reg/mem ", " reg))
2115 (aver (register-p src))
2116 (emit-prefix segment prefix)
2117 (let ((size (matching-operand-size src dst)))
2118 (maybe-emit-operand-size-prefix segment size)
2119 (maybe-emit-rex-for-ea segment dst src)
2120 (emit-byte segment #b00001111)
2121 (emit-byte segment (if (eq size :byte) #b11000000 #b11000001))
2122 (emit-ea segment dst (reg-tn-encoding src)))))
2127 (defun emit-shift-inst (segment dst amount opcode)
2128 (let ((size (operand-size dst)))
2129 (maybe-emit-operand-size-prefix segment size)
2130 (multiple-value-bind (major-opcode immed)
2132 (:cl (values #b11010010 nil))
2133 (1 (values #b11010000 nil))
2134 (t (values #b11000000 t)))
2135 (maybe-emit-rex-for-ea segment dst nil)
2137 (if (eq size :byte) major-opcode (logior major-opcode 1)))
2138 (emit-ea segment dst opcode)
2140 (emit-byte segment amount)))))
2142 (eval-when (:compile-toplevel :execute)
2143 (defun shift-inst-printer-list (subop)
2144 `((reg/mem ((op (#b1101000 ,subop)))
2145 (:name :tab reg/mem ", 1"))
2146 (reg/mem ((op (#b1101001 ,subop)))
2147 (:name :tab reg/mem ", " 'cl))
2148 (reg/mem-imm ((op (#b1100000 ,subop))
2149 (imm nil :type imm-byte))))))
2151 (define-instruction rol (segment dst amount)
2153 (shift-inst-printer-list #b000))
2155 (emit-shift-inst segment dst amount #b000)))
2157 (define-instruction ror (segment dst amount)
2159 (shift-inst-printer-list #b001))
2161 (emit-shift-inst segment dst amount #b001)))
2163 (define-instruction rcl (segment dst amount)
2165 (shift-inst-printer-list #b010))
2167 (emit-shift-inst segment dst amount #b010)))
2169 (define-instruction rcr (segment dst amount)
2171 (shift-inst-printer-list #b011))
2173 (emit-shift-inst segment dst amount #b011)))
2175 (define-instruction shl (segment dst amount)
2177 (shift-inst-printer-list #b100))
2179 (emit-shift-inst segment dst amount #b100)))
2181 (define-instruction shr (segment dst amount)
2183 (shift-inst-printer-list #b101))
2185 (emit-shift-inst segment dst amount #b101)))
2187 (define-instruction sar (segment dst amount)
2189 (shift-inst-printer-list #b111))
2191 (emit-shift-inst segment dst amount #b111)))
2193 (defun emit-double-shift (segment opcode dst src amt)
2194 (let ((size (matching-operand-size dst src)))
2195 (when (eq size :byte)
2196 (error "Double shifts can only be used with words."))
2197 (maybe-emit-operand-size-prefix segment size)
2198 (maybe-emit-rex-for-ea segment dst src)
2199 (emit-byte segment #b00001111)
2200 (emit-byte segment (dpb opcode (byte 1 3)
2201 (if (eq amt :cl) #b10100101 #b10100100)))
2202 (emit-ea segment dst (reg-tn-encoding src))
2203 (unless (eq amt :cl)
2204 (emit-byte segment amt))))
2206 (eval-when (:compile-toplevel :execute)
2207 (defun double-shift-inst-printer-list (op)
2208 `((ext-reg-reg/mem-no-width ((op ,(logior op #b100))
2209 (imm nil :type imm-byte))
2210 (:name :tab reg/mem ", " reg ", " imm))
2211 (ext-reg-reg/mem-no-width ((op ,(logior op #b101)))
2212 (:name :tab reg/mem ", " reg ", " 'cl)))))
2214 (define-instruction shld (segment dst src amt)
2215 (:declare (type (or (member :cl) (mod 64)) amt))
2216 (:printer-list (double-shift-inst-printer-list #b10100000))
2218 (emit-double-shift segment #b0 dst src amt)))
2220 (define-instruction shrd (segment dst src amt)
2221 (:declare (type (or (member :cl) (mod 64)) amt))
2222 (:printer-list (double-shift-inst-printer-list #b10101000))
2224 (emit-double-shift segment #b1 dst src amt)))
2226 (define-instruction and (segment dst src)
2228 (arith-inst-printer-list #b100))
2230 (emit-random-arith-inst "AND" segment dst src #b100)))
2232 (define-instruction test (segment this that)
2233 (:printer accum-imm ((op #b1010100)))
2234 (:printer reg/mem-imm ((op '(#b1111011 #b000))))
2235 (:printer reg-reg/mem ((op #b1000010)))
2237 (let ((size (matching-operand-size this that)))
2238 (maybe-emit-operand-size-prefix segment size)
2239 (flet ((test-immed-and-something (immed something)
2240 (cond ((accumulator-p something)
2241 (maybe-emit-rex-for-ea segment something nil)
2243 (if (eq size :byte) #b10101000 #b10101001))
2244 (emit-sized-immediate segment size immed))
2246 (maybe-emit-rex-for-ea segment something nil)
2248 (if (eq size :byte) #b11110110 #b11110111))
2249 (emit-ea segment something #b000)
2250 (emit-sized-immediate segment size immed))))
2251 (test-reg-and-something (reg something)
2252 (maybe-emit-rex-for-ea segment something reg)
2253 (emit-byte segment (if (eq size :byte) #b10000100 #b10000101))
2254 (emit-ea segment something (reg-tn-encoding reg))))
2255 (cond ((integerp that)
2256 (test-immed-and-something that this))
2258 (test-immed-and-something this that))
2260 (test-reg-and-something this that))
2262 (test-reg-and-something that this))
2264 (error "bogus operands for TEST: ~S and ~S" this that)))))))
2266 ;;; Emit the most compact form of the test immediate instruction,
2267 ;;; using an 8 bit test when the immediate is only 8 bits and the
2268 ;;; value is one of the four low registers (rax, rbx, rcx, rdx) or the
2270 (defun emit-optimized-test-inst (x y)
2273 (let ((offset (tn-offset x)))
2274 (cond ((and (sc-is x any-reg descriptor-reg)
2275 (or (= offset rax-offset) (= offset rbx-offset)
2276 (= offset rcx-offset) (= offset rdx-offset)))
2277 (inst test (reg-in-size x :byte) y))
2278 ((sc-is x control-stack)
2279 (inst test (make-ea :byte :base rbp-tn
2280 :disp (frame-byte-offset offset))
2287 (define-instruction or (segment dst src)
2289 (arith-inst-printer-list #b001))
2291 (emit-random-arith-inst "OR" segment dst src #b001)))
2293 (define-instruction xor (segment dst src)
2295 (arith-inst-printer-list #b110))
2297 (emit-random-arith-inst "XOR" segment dst src #b110)))
2299 (define-instruction not (segment dst)
2300 (:printer reg/mem ((op '(#b1111011 #b010))))
2302 (let ((size (operand-size dst)))
2303 (maybe-emit-operand-size-prefix segment size)
2304 (maybe-emit-rex-for-ea segment dst nil)
2305 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2306 (emit-ea segment dst #b010))))
2308 ;;;; string manipulation
2310 (define-instruction cmps (segment size)
2311 (:printer string-op ((op #b1010011)))
2313 (maybe-emit-operand-size-prefix segment size)
2314 (maybe-emit-rex-prefix segment size nil nil nil)
2315 (emit-byte segment (if (eq size :byte) #b10100110 #b10100111))))
2317 (define-instruction ins (segment acc)
2318 (:printer string-op ((op #b0110110)))
2320 (let ((size (operand-size acc)))
2321 (aver (accumulator-p acc))
2322 (maybe-emit-operand-size-prefix segment size)
2323 (maybe-emit-rex-prefix segment size nil nil nil)
2324 (emit-byte segment (if (eq size :byte) #b01101100 #b01101101)))))
2326 (define-instruction lods (segment acc)
2327 (:printer string-op ((op #b1010110)))
2329 (let ((size (operand-size acc)))
2330 (aver (accumulator-p acc))
2331 (maybe-emit-operand-size-prefix segment size)
2332 (maybe-emit-rex-prefix segment size nil nil nil)
2333 (emit-byte segment (if (eq size :byte) #b10101100 #b10101101)))))
2335 (define-instruction movs (segment size)
2336 (:printer string-op ((op #b1010010)))
2338 (maybe-emit-operand-size-prefix segment size)
2339 (maybe-emit-rex-prefix segment size nil nil nil)
2340 (emit-byte segment (if (eq size :byte) #b10100100 #b10100101))))
2342 (define-instruction outs (segment acc)
2343 (:printer string-op ((op #b0110111)))
2345 (let ((size (operand-size acc)))
2346 (aver (accumulator-p acc))
2347 (maybe-emit-operand-size-prefix segment size)
2348 (maybe-emit-rex-prefix segment size nil nil nil)
2349 (emit-byte segment (if (eq size :byte) #b01101110 #b01101111)))))
2351 (define-instruction scas (segment acc)
2352 (:printer string-op ((op #b1010111)))
2354 (let ((size (operand-size acc)))
2355 (aver (accumulator-p acc))
2356 (maybe-emit-operand-size-prefix segment size)
2357 (maybe-emit-rex-prefix segment size nil nil nil)
2358 (emit-byte segment (if (eq size :byte) #b10101110 #b10101111)))))
2360 (define-instruction stos (segment acc)
2361 (:printer string-op ((op #b1010101)))
2363 (let ((size (operand-size acc)))
2364 (aver (accumulator-p acc))
2365 (maybe-emit-operand-size-prefix segment size)
2366 (maybe-emit-rex-prefix segment size nil nil nil)
2367 (emit-byte segment (if (eq size :byte) #b10101010 #b10101011)))))
2369 (define-instruction xlat (segment)
2370 (:printer byte ((op #b11010111)))
2372 (emit-byte segment #b11010111)))
2375 ;;;; bit manipulation
2377 (define-instruction bsf (segment dst src)
2378 (:printer ext-reg-reg/mem-no-width ((op #b10111100)))
2380 (let ((size (matching-operand-size dst src)))
2381 (when (eq size :byte)
2382 (error "can't scan bytes: ~S" src))
2383 (maybe-emit-operand-size-prefix segment size)
2384 (maybe-emit-rex-for-ea segment src dst)
2385 (emit-byte segment #b00001111)
2386 (emit-byte segment #b10111100)
2387 (emit-ea segment src (reg-tn-encoding dst)))))
2389 (define-instruction bsr (segment dst src)
2390 (:printer ext-reg-reg/mem-no-width ((op #b10111101)))
2392 (let ((size (matching-operand-size dst src)))
2393 (when (eq size :byte)
2394 (error "can't scan bytes: ~S" src))
2395 (maybe-emit-operand-size-prefix segment size)
2396 (maybe-emit-rex-for-ea segment src dst)
2397 (emit-byte segment #b00001111)
2398 (emit-byte segment #b10111101)
2399 (emit-ea segment src (reg-tn-encoding dst)))))
2401 (defun emit-bit-test-and-mumble (segment src index opcode)
2402 (let ((size (operand-size src)))
2403 (when (eq size :byte)
2404 (error "can't scan bytes: ~S" src))
2405 (maybe-emit-operand-size-prefix segment size)
2406 (cond ((integerp index)
2407 (maybe-emit-rex-for-ea segment src nil)
2408 (emit-byte segment #b00001111)
2409 (emit-byte segment #b10111010)
2410 (emit-ea segment src opcode)
2411 (emit-byte segment index))
2413 (maybe-emit-rex-for-ea segment src index)
2414 (emit-byte segment #b00001111)
2415 (emit-byte segment (dpb opcode (byte 3 3) #b10000011))
2416 (emit-ea segment src (reg-tn-encoding index))))))
2418 (eval-when (:compile-toplevel :execute)
2419 (defun bit-test-inst-printer-list (subop)
2420 `((ext-reg/mem-imm ((op (#b1011101 ,subop))
2421 (reg/mem nil :type reg/mem)
2422 (imm nil :type imm-byte)
2424 (ext-reg-reg/mem ((op ,(dpb subop (byte 3 2) #b1000001))
2426 (:name :tab reg/mem ", " reg)))))
2428 (define-instruction bt (segment src index)
2429 (:printer-list (bit-test-inst-printer-list #b100))
2431 (emit-bit-test-and-mumble segment src index #b100)))
2433 (define-instruction btc (segment src index)
2434 (:printer-list (bit-test-inst-printer-list #b111))
2436 (emit-bit-test-and-mumble segment src index #b111)))
2438 (define-instruction btr (segment src index)
2439 (:printer-list (bit-test-inst-printer-list #b110))
2441 (emit-bit-test-and-mumble segment src index #b110)))
2443 (define-instruction bts (segment src index)
2444 (:printer-list (bit-test-inst-printer-list #b101))
2446 (emit-bit-test-and-mumble segment src index #b101)))
2449 ;;;; control transfer
2451 (define-instruction call (segment where)
2452 (:printer near-jump ((op #b11101000)))
2453 (:printer reg/mem-default-qword ((op '(#b11111111 #b010))))
2457 (emit-byte segment #b11101000) ; 32 bit relative
2458 (emit-back-patch segment
2460 (lambda (segment posn)
2461 (emit-signed-dword segment
2462 (- (label-position where)
2465 ;; There is no CALL rel64...
2466 (error "Cannot CALL a fixup: ~S" where))
2468 (maybe-emit-rex-for-ea segment where nil :operand-size :do-not-set)
2469 (emit-byte segment #b11111111)
2470 (emit-ea segment where #b010)))))
2472 (defun emit-byte-displacement-backpatch (segment target)
2473 (emit-back-patch segment
2475 (lambda (segment posn)
2476 (let ((disp (- (label-position target) (1+ posn))))
2477 (aver (<= -128 disp 127))
2478 (emit-byte segment disp)))))
2480 (define-instruction jmp (segment cond &optional where)
2481 ;; conditional jumps
2482 (:printer short-cond-jump ((op #b0111)) '('j cc :tab label))
2483 (:printer near-cond-jump () '('j cc :tab label))
2484 ;; unconditional jumps
2485 (:printer short-jump ((op #b1011)))
2486 (:printer near-jump ((op #b11101001)))
2487 (:printer reg/mem-default-qword ((op '(#b11111111 #b100))))
2492 (lambda (segment posn delta-if-after)
2493 (let ((disp (- (label-position where posn delta-if-after)
2495 (when (<= -128 disp 127)
2497 (dpb (conditional-opcode cond)
2500 (emit-byte-displacement-backpatch segment where)
2502 (lambda (segment posn)
2503 (let ((disp (- (label-position where) (+ posn 6))))
2504 (emit-byte segment #b00001111)
2506 (dpb (conditional-opcode cond)
2509 (emit-signed-dword segment disp)))))
2510 ((label-p (setq where cond))
2513 (lambda (segment posn delta-if-after)
2514 (let ((disp (- (label-position where posn delta-if-after)
2516 (when (<= -128 disp 127)
2517 (emit-byte segment #b11101011)
2518 (emit-byte-displacement-backpatch segment where)
2520 (lambda (segment posn)
2521 (let ((disp (- (label-position where) (+ posn 5))))
2522 (emit-byte segment #b11101001)
2523 (emit-signed-dword segment disp)))))
2525 (emit-byte segment #b11101001)
2526 (emit-relative-fixup segment where))
2528 (unless (or (ea-p where) (tn-p where))
2529 (error "don't know what to do with ~A" where))
2530 ;; near jump defaults to 64 bit
2531 ;; w-bit in rex prefix is unnecessary
2532 (maybe-emit-rex-for-ea segment where nil :operand-size :do-not-set)
2533 (emit-byte segment #b11111111)
2534 (emit-ea segment where #b100)))))
2536 (define-instruction ret (segment &optional stack-delta)
2537 (:printer byte ((op #b11000011)))
2538 (:printer byte ((op #b11000010) (imm nil :type 'imm-word-16))
2541 (cond ((and stack-delta (not (zerop stack-delta)))
2542 (emit-byte segment #b11000010)
2543 (emit-word segment stack-delta))
2545 (emit-byte segment #b11000011)))))
2547 (define-instruction jrcxz (segment target)
2548 (:printer short-jump ((op #b0011)))
2550 (emit-byte segment #b11100011)
2551 (emit-byte-displacement-backpatch segment target)))
2553 (define-instruction loop (segment target)
2554 (:printer short-jump ((op #b0010)))
2556 (emit-byte segment #b11100010) ; pfw this was 11100011, or jecxz!!!!
2557 (emit-byte-displacement-backpatch segment target)))
2559 (define-instruction loopz (segment target)
2560 (:printer short-jump ((op #b0001)))
2562 (emit-byte segment #b11100001)
2563 (emit-byte-displacement-backpatch segment target)))
2565 (define-instruction loopnz (segment target)
2566 (:printer short-jump ((op #b0000)))
2568 (emit-byte segment #b11100000)
2569 (emit-byte-displacement-backpatch segment target)))
2571 ;;;; conditional move
2572 (define-instruction cmov (segment cond dst src)
2573 (:printer cond-move ())
2575 (aver (register-p dst))
2576 (let ((size (matching-operand-size dst src)))
2577 (aver (or (eq size :word) (eq size :dword) (eq size :qword)))
2578 (maybe-emit-operand-size-prefix segment size))
2579 (maybe-emit-rex-for-ea segment src dst)
2580 (emit-byte segment #b00001111)
2581 (emit-byte segment (dpb (conditional-opcode cond) (byte 4 0) #b01000000))
2582 (emit-ea segment src (reg-tn-encoding dst))))
2584 ;;;; conditional byte set
2586 (define-instruction set (segment dst cond)
2587 (:printer cond-set ())
2589 (maybe-emit-rex-for-ea segment dst nil)
2590 (emit-byte segment #b00001111)
2591 (emit-byte segment (dpb (conditional-opcode cond) (byte 4 0) #b10010000))
2592 (emit-ea segment dst #b000)))
2596 (define-instruction enter (segment disp &optional (level 0))
2597 (:declare (type (unsigned-byte 16) disp)
2598 (type (unsigned-byte 8) level))
2599 (:printer enter-format ((op #b11001000)))
2601 (emit-byte segment #b11001000)
2602 (emit-word segment disp)
2603 (emit-byte segment level)))
2605 (define-instruction leave (segment)
2606 (:printer byte ((op #b11001001)))
2608 (emit-byte segment #b11001001)))
2610 ;;;; interrupt instructions
2612 (defun snarf-error-junk (sap offset &optional length-only)
2613 (let* ((length (sb!sys:sap-ref-8 sap offset))
2614 (vector (make-array length :element-type '(unsigned-byte 8))))
2615 (declare (type sb!sys:system-area-pointer sap)
2616 (type (unsigned-byte 8) length)
2617 (type (simple-array (unsigned-byte 8) (*)) vector))
2619 (values 0 (1+ length) nil nil))
2621 (sb!kernel:copy-ub8-from-system-area sap (1+ offset)
2623 (collect ((sc-offsets)
2625 (lengths 1) ; the length byte
2627 (error-number (sb!c:read-var-integer vector index)))
2630 (when (>= index length)
2632 (let ((old-index index))
2633 (sc-offsets (sb!c:read-var-integer vector index))
2634 (lengths (- index old-index))))
2635 (values error-number
2641 (defmacro break-cases (breaknum &body cases)
2642 (let ((bn-temp (gensym)))
2643 (collect ((clauses))
2644 (dolist (case cases)
2645 (clauses `((= ,bn-temp ,(car case)) ,@(cdr case))))
2646 `(let ((,bn-temp ,breaknum))
2647 (cond ,@(clauses))))))
2650 (defun break-control (chunk inst stream dstate)
2651 (declare (ignore inst))
2652 (flet ((nt (x) (if stream (sb!disassem:note x dstate))))
2653 ;; XXX: {BYTE,WORD}-IMM-CODE below is a macro defined by the
2654 ;; DEFINE-INSTRUCTION-FORMAT for {BYTE,WORD}-IMM above. Due to
2655 ;; the spectacular design for DEFINE-INSTRUCTION-FORMAT (involving
2656 ;; a call to EVAL in order to define the macros at compile-time
2657 ;; only) they do not even show up as symbols in the target core.
2658 (case #!-ud2-breakpoints (byte-imm-code chunk dstate)
2659 #!+ud2-breakpoints (word-imm-code chunk dstate)
2662 (sb!disassem:handle-break-args #'snarf-error-junk stream dstate))
2665 (sb!disassem:handle-break-args #'snarf-error-junk stream dstate))
2667 (nt "breakpoint trap"))
2668 (#.pending-interrupt-trap
2669 (nt "pending interrupt trap"))
2672 (#.fun-end-breakpoint-trap
2673 (nt "function end breakpoint trap"))
2674 (#.single-step-around-trap
2675 (nt "single-step trap (around)"))
2676 (#.single-step-before-trap
2677 (nt "single-step trap (before)")))))
2679 (define-instruction break (segment code)
2680 (:declare (type (unsigned-byte 8) code))
2681 #!-ud2-breakpoints (:printer byte-imm ((op #b11001100)) '(:name :tab code)
2682 :control #'break-control)
2683 #!+ud2-breakpoints (:printer word-imm ((op #b0000101100001111)) '(:name :tab code)
2684 :control #'break-control)
2686 #!-ud2-breakpoints (emit-byte segment #b11001100)
2687 ;; On darwin, trap handling via SIGTRAP is unreliable, therefore we
2688 ;; throw a sigill with 0x0b0f instead and check for this in the
2689 ;; SIGILL handler and pass it on to the sigtrap handler if
2691 #!+ud2-breakpoints (emit-word segment #b0000101100001111)
2692 (emit-byte segment code)))
2694 (define-instruction int (segment number)
2695 (:declare (type (unsigned-byte 8) number))
2696 (:printer byte-imm ((op #b11001101)))
2700 (emit-byte segment #b11001100))
2702 (emit-byte segment #b11001101)
2703 (emit-byte segment number)))))
2705 (define-instruction iret (segment)
2706 (:printer byte ((op #b11001111)))
2708 (emit-byte segment #b11001111)))
2710 ;;;; processor control
2712 (define-instruction hlt (segment)
2713 (:printer byte ((op #b11110100)))
2715 (emit-byte segment #b11110100)))
2717 (define-instruction nop (segment)
2718 (:printer byte ((op #b10010000)))
2720 (:printer ext-reg/mem-no-width ((op '(#x1f 0))) '(:name))
2722 (emit-byte segment #b10010000)))
2724 ;;; Emit a sequence of single- or multi-byte NOPs to fill AMOUNT many
2725 ;;; bytes with the smallest possible number of such instructions.
2726 (defun emit-long-nop (segment amount)
2727 (declare (type segment segment)
2728 (type index amount))
2729 ;; Pack all instructions into one byte vector to save space.
2730 (let* ((bytes #.(coerce #(#x90
2734 #x0f #x1f #x44 #x00 #x00
2735 #x66 #x0f #x1f #x44 #x00 #x00
2736 #x0f #x1f #x80 #x00 #x00 #x00 #x00
2737 #x0f #x1f #x84 #x00 #x00 #x00 #x00 #x00
2738 #x66 #x0f #x1f #x84 #x00 #x00 #x00 #x00 #x00)
2739 '(vector (unsigned-byte 8))))
2740 (max-length (isqrt (* 2 (length bytes)))))
2742 (let* ((count (min amount max-length))
2743 (start (ash (* count (1- count)) -1)))
2745 (emit-byte segment (aref bytes (+ start i)))))
2746 (if (> amount max-length)
2747 (decf amount max-length)
2750 (define-instruction wait (segment)
2751 (:printer byte ((op #b10011011)))
2753 (emit-byte segment #b10011011)))
2756 ;;;; miscellaneous hackery
2758 (define-instruction byte (segment byte)
2760 (emit-byte segment byte)))
2762 (define-instruction word (segment word)
2764 (emit-word segment word)))
2766 (define-instruction dword (segment dword)
2768 (emit-dword segment dword)))
2770 (defun emit-header-data (segment type)
2771 (emit-back-patch segment
2773 (lambda (segment posn)
2777 (component-header-length))
2781 (define-instruction simple-fun-header-word (segment)
2783 (emit-header-data segment simple-fun-header-widetag)))
2785 (define-instruction lra-header-word (segment)
2787 (emit-header-data segment return-pc-header-widetag)))
2789 ;;;; Instructions required to do floating point operations using SSE
2791 ;; Return a one- or two-element list of printers for SSE instructions.
2792 ;; The one-element list is used in the cases where the REX prefix is
2793 ;; really a prefix and thus automatically supported, the two-element
2794 ;; list is used when the REX prefix is used in an infix position.
2795 (eval-when (:compile-toplevel :execute)
2796 (defun sse-inst-printer-list (inst-format-stem prefix opcode
2797 &key more-fields printer)
2798 (let ((fields `(,@(when prefix
2799 `((prefix ,prefix)))
2802 (inst-formats (if prefix
2803 (list (symbolicate "EXT-" inst-format-stem)
2804 (symbolicate "EXT-REX-" inst-format-stem))
2805 (list inst-format-stem))))
2806 (mapcar (lambda (inst-format)
2807 `(,inst-format ,fields ,@(when printer
2810 (defun 2byte-sse-inst-printer-list (inst-format-stem prefix op1 op2
2811 &key more-fields printer)
2812 (let ((fields `(,@(when prefix
2813 `((prefix, prefix)))
2817 (inst-formats (if prefix
2818 (list (symbolicate "EXT-" inst-format-stem)
2819 (symbolicate "EXT-REX-" inst-format-stem))
2820 (list inst-format-stem))))
2821 (mapcar (lambda (inst-format)
2822 `(,inst-format ,fields ,@(when printer
2826 (defun emit-sse-inst (segment dst src prefix opcode
2827 &key operand-size (remaining-bytes 0))
2829 (emit-byte segment prefix))
2831 (maybe-emit-rex-for-ea segment src dst :operand-size operand-size)
2832 (maybe-emit-rex-for-ea segment src dst))
2833 (emit-byte segment #x0f)
2834 (emit-byte segment opcode)
2835 (emit-ea segment src (reg-tn-encoding dst) :remaining-bytes remaining-bytes))
2837 ;; 0110 0110:0000 1111:0111 00gg: 11 010 xmmreg:imm8
2839 (defun emit-sse-inst-with-imm (segment dst/src imm
2844 (emit-byte segment prefix))
2845 (maybe-emit-rex-prefix segment operand-size dst/src nil nil)
2846 (emit-byte segment #x0F)
2847 (emit-byte segment opcode)
2848 (emit-byte segment (logior (ash (logior #b11000 /i) 3)
2849 (reg-tn-encoding dst/src)))
2850 (emit-byte segment imm))
2852 (defun emit-sse-inst-2byte (segment dst src prefix op1 op2
2853 &key operand-size (remaining-bytes 0))
2855 (emit-byte segment prefix))
2857 (maybe-emit-rex-for-ea segment src dst :operand-size operand-size)
2858 (maybe-emit-rex-for-ea segment src dst))
2859 (emit-byte segment #x0f)
2860 (emit-byte segment op1)
2861 (emit-byte segment op2)
2862 (emit-ea segment src (reg-tn-encoding dst) :remaining-bytes remaining-bytes))
2865 ((define-imm-sse-instruction (name opcode /i)
2866 `(define-instruction ,name (segment dst/src imm)
2868 ',(sse-inst-printer-list 'xmm-imm #x66 opcode
2869 :more-fields `((/i ,/i))))
2871 (emit-sse-inst-with-imm segment dst/src imm
2873 :operand-size :do-not-set)))))
2874 (define-imm-sse-instruction pslldq #x73 7)
2875 (define-imm-sse-instruction psllw-imm #x71 6)
2876 (define-imm-sse-instruction pslld-imm #x72 6)
2877 (define-imm-sse-instruction psllq-imm #x73 6)
2879 (define-imm-sse-instruction psraw-imm #x71 4)
2880 (define-imm-sse-instruction psrad-imm #x72 4)
2882 (define-imm-sse-instruction psrldq #x73 3)
2883 (define-imm-sse-instruction psrlw-imm #x71 2)
2884 (define-imm-sse-instruction psrld-imm #x72 2)
2885 (define-imm-sse-instruction psrlq-imm #x73 2))
2887 ;;; Emit an SSE instruction that has an XMM register as the destination
2888 ;;; operand and for which the size of the operands is implicitly given
2889 ;;; by the instruction.
2890 (defun emit-regular-sse-inst (segment dst src prefix opcode
2891 &key (remaining-bytes 0))
2892 (aver (xmm-register-p dst))
2893 (emit-sse-inst segment dst src prefix opcode
2894 :operand-size :do-not-set
2895 :remaining-bytes remaining-bytes))
2897 (defun emit-regular-2byte-sse-inst (segment dst src prefix op1 op2
2898 &key (remaining-bytes 0))
2899 (aver (xmm-register-p dst))
2900 (emit-sse-inst-2byte segment dst src prefix op1 op2
2901 :operand-size :do-not-set
2902 :remaining-bytes remaining-bytes))
2904 ;;; Instructions having an XMM register as the destination operand
2905 ;;; and an XMM register or a memory location as the source operand.
2906 ;;; The operand size is implicitly given by the instruction.
2908 (macrolet ((define-regular-sse-inst (name prefix opcode)
2909 `(define-instruction ,name (segment dst src)
2911 ',(sse-inst-printer-list 'xmm-xmm/mem prefix opcode))
2913 (emit-regular-sse-inst segment dst src ,prefix ,opcode)))))
2915 (define-regular-sse-inst andpd #x66 #x54)
2916 (define-regular-sse-inst andps nil #x54)
2917 (define-regular-sse-inst andnpd #x66 #x55)
2918 (define-regular-sse-inst andnps nil #x55)
2919 (define-regular-sse-inst orpd #x66 #x56)
2920 (define-regular-sse-inst orps nil #x56)
2921 (define-regular-sse-inst pand #x66 #xdb)
2922 (define-regular-sse-inst pandn #x66 #xdf)
2923 (define-regular-sse-inst por #x66 #xeb)
2924 (define-regular-sse-inst pxor #x66 #xef)
2925 (define-regular-sse-inst xorpd #x66 #x57)
2926 (define-regular-sse-inst xorps nil #x57)
2928 (define-regular-sse-inst comisd #x66 #x2f)
2929 (define-regular-sse-inst comiss nil #x2f)
2930 (define-regular-sse-inst ucomisd #x66 #x2e)
2931 (define-regular-sse-inst ucomiss nil #x2e)
2932 ;; integer comparison
2933 (define-regular-sse-inst pcmpeqb #x66 #x74)
2934 (define-regular-sse-inst pcmpeqw #x66 #x75)
2935 (define-regular-sse-inst pcmpeqd #x66 #x76)
2936 (define-regular-sse-inst pcmpgtb #x66 #x64)
2937 (define-regular-sse-inst pcmpgtw #x66 #x65)
2938 (define-regular-sse-inst pcmpgtd #x66 #x66)
2940 (define-regular-sse-inst maxpd #x66 #x5f)
2941 (define-regular-sse-inst maxps nil #x5f)
2942 (define-regular-sse-inst maxsd #xf2 #x5f)
2943 (define-regular-sse-inst maxss #xf3 #x5f)
2944 (define-regular-sse-inst minpd #x66 #x5d)
2945 (define-regular-sse-inst minps nil #x5d)
2946 (define-regular-sse-inst minsd #xf2 #x5d)
2947 (define-regular-sse-inst minss #xf3 #x5d)
2949 (define-regular-sse-inst pmaxsw #x66 #xee)
2950 (define-regular-sse-inst pmaxub #x66 #xde)
2951 (define-regular-sse-inst pminsw #x66 #xea)
2952 (define-regular-sse-inst pminub #x66 #xda)
2954 (define-regular-sse-inst addpd #x66 #x58)
2955 (define-regular-sse-inst addps nil #x58)
2956 (define-regular-sse-inst addsd #xf2 #x58)
2957 (define-regular-sse-inst addss #xf3 #x58)
2958 (define-regular-sse-inst divpd #x66 #x5e)
2959 (define-regular-sse-inst divps nil #x5e)
2960 (define-regular-sse-inst divsd #xf2 #x5e)
2961 (define-regular-sse-inst divss #xf3 #x5e)
2962 (define-regular-sse-inst mulpd #x66 #x59)
2963 (define-regular-sse-inst mulps nil #x59)
2964 (define-regular-sse-inst mulsd #xf2 #x59)
2965 (define-regular-sse-inst mulss #xf3 #x59)
2966 (define-regular-sse-inst rcpps nil #x53)
2967 (define-regular-sse-inst rcpss #xf3 #x53)
2968 (define-regular-sse-inst rsqrtps nil #x52)
2969 (define-regular-sse-inst rsqrtss #xf3 #x52)
2970 (define-regular-sse-inst sqrtpd #x66 #x51)
2971 (define-regular-sse-inst sqrtps nil #x51)
2972 (define-regular-sse-inst sqrtsd #xf2 #x51)
2973 (define-regular-sse-inst sqrtss #xf3 #x51)
2974 (define-regular-sse-inst subpd #x66 #x5c)
2975 (define-regular-sse-inst subps nil #x5c)
2976 (define-regular-sse-inst subsd #xf2 #x5c)
2977 (define-regular-sse-inst subss #xf3 #x5c)
2978 (define-regular-sse-inst unpckhpd #x66 #x15)
2979 (define-regular-sse-inst unpckhps nil #x15)
2980 (define-regular-sse-inst unpcklpd #x66 #x14)
2981 (define-regular-sse-inst unpcklps nil #x14)
2982 ;; integer arithmetic
2983 (define-regular-sse-inst paddb #x66 #xfc)
2984 (define-regular-sse-inst paddw #x66 #xfd)
2985 (define-regular-sse-inst paddd #x66 #xfe)
2986 (define-regular-sse-inst paddq #x66 #xd4)
2987 (define-regular-sse-inst paddsb #x66 #xec)
2988 (define-regular-sse-inst paddsw #x66 #xed)
2989 (define-regular-sse-inst paddusb #x66 #xdc)
2990 (define-regular-sse-inst paddusw #x66 #xdd)
2991 (define-regular-sse-inst pavgb #x66 #xe0)
2992 (define-regular-sse-inst pavgw #x66 #xe3)
2993 (define-regular-sse-inst pmaddwd #x66 #xf5)
2994 (define-regular-sse-inst pmulhuw #x66 #xe4)
2995 (define-regular-sse-inst pmulhw #x66 #xe5)
2996 (define-regular-sse-inst pmullw #x66 #xd5)
2997 (define-regular-sse-inst pmuludq #x66 #xf4)
2998 (define-regular-sse-inst psadbw #x66 #xf6)
2999 (define-regular-sse-inst psllw #x66 #xf1)
3000 (define-regular-sse-inst pslld #x66 #xf2)
3001 (define-regular-sse-inst psllq #x66 #xf3)
3002 (define-regular-sse-inst psraw #x66 #xe1)
3003 (define-regular-sse-inst psrad #x66 #xe2)
3004 (define-regular-sse-inst psrlw #x66 #xd1)
3005 (define-regular-sse-inst psrld #x66 #xd2)
3006 (define-regular-sse-inst psrlq #x66 #xd3)
3007 (define-regular-sse-inst psubb #x66 #xf8)
3008 (define-regular-sse-inst psubw #x66 #xf9)
3009 (define-regular-sse-inst psubd #x66 #xfa)
3010 (define-regular-sse-inst psubq #x66 #xfb)
3011 (define-regular-sse-inst psubsb #x66 #xe8)
3012 (define-regular-sse-inst psubsw #x66 #xe9)
3013 (define-regular-sse-inst psubusb #x66 #xd8)
3014 (define-regular-sse-inst psubusw #x66 #xd9)
3016 (define-regular-sse-inst cvtdq2pd #xf3 #xe6)
3017 (define-regular-sse-inst cvtdq2ps nil #x5b)
3018 (define-regular-sse-inst cvtpd2dq #xf2 #xe6)
3019 (define-regular-sse-inst cvtpd2ps #x66 #x5a)
3020 (define-regular-sse-inst cvtps2dq #x66 #x5b)
3021 (define-regular-sse-inst cvtps2pd nil #x5a)
3022 (define-regular-sse-inst cvtsd2ss #xf2 #x5a)
3023 (define-regular-sse-inst cvtss2sd #xf3 #x5a)
3024 (define-regular-sse-inst cvttpd2dq #x66 #xe6)
3025 (define-regular-sse-inst cvttps2dq #xf3 #x5b)
3027 (define-regular-sse-inst packsswb #x66 #x63)
3028 (define-regular-sse-inst packssdw #x66 #x6b)
3029 (define-regular-sse-inst packuswb #x66 #x67)
3030 (define-regular-sse-inst punpckhbw #x66 #x68)
3031 (define-regular-sse-inst punpckhwd #x66 #x69)
3032 (define-regular-sse-inst punpckhdq #x66 #x6a)
3033 (define-regular-sse-inst punpckhqdq #x66 #x6d)
3034 (define-regular-sse-inst punpcklbw #x66 #x60)
3035 (define-regular-sse-inst punpcklwd #x66 #x61)
3036 (define-regular-sse-inst punpckldq #x66 #x62)
3037 (define-regular-sse-inst punpcklqdq #x66 #x6c))
3039 (macrolet ((define-xmm-shuffle-sse-inst (name prefix opcode n-bits radix)
3040 (let ((shuffle-pattern
3041 (intern (format nil "SSE-SHUFFLE-PATTERN-~D-~D"
3043 `(define-instruction ,name (segment dst src pattern)
3045 ',(sse-inst-printer-list
3046 'xmm-xmm/mem prefix opcode
3047 :more-fields `((imm nil :type ,shuffle-pattern))
3048 :printer '(:name :tab reg ", " reg/mem ", " imm)))
3051 (aver (typep pattern '(unsigned-byte ,n-bits)))
3052 (emit-regular-sse-inst segment dst src ,prefix ,opcode
3054 (emit-byte segment pattern))))))
3055 (define-xmm-shuffle-sse-inst pshufd #x66 #x70 8 4)
3056 (define-xmm-shuffle-sse-inst pshufhw #xf3 #x70 8 4)
3057 (define-xmm-shuffle-sse-inst pshuflw #xf2 #x70 8 4)
3058 (define-xmm-shuffle-sse-inst shufpd #x66 #xc6 2 2)
3059 (define-xmm-shuffle-sse-inst shufps nil #xc6 8 4))
3061 ;; MASKMOVDQU (dst is DS:RDI)
3062 (define-instruction maskmovdqu (segment src mask)
3064 (sse-inst-printer-list 'xmm-xmm/mem #x66 #xf7))
3066 (aver (xmm-register-p src))
3067 (aver (xmm-register-p mask))
3068 (emit-regular-sse-inst segment src mask #x66 #xf7)))
3070 (macrolet ((define-comparison-sse-inst (name prefix opcode
3071 name-prefix name-suffix)
3072 `(define-instruction ,name (segment op x y)
3074 ',(sse-inst-printer-list
3075 'xmm-xmm/mem prefix opcode
3076 :more-fields '((imm nil :type sse-condition-code))
3077 :printer `(,name-prefix imm ,name-suffix
3078 :tab reg ", " reg/mem)))
3080 (let ((code (position op *sse-conditions*)))
3082 (emit-regular-sse-inst segment x y ,prefix ,opcode
3084 (emit-byte segment code))))))
3085 (define-comparison-sse-inst cmppd #x66 #xc2 "CMP" "PD")
3086 (define-comparison-sse-inst cmpps nil #xc2 "CMP" "PS")
3087 (define-comparison-sse-inst cmpsd #xf2 #xc2 "CMP" "SD")
3088 (define-comparison-sse-inst cmpss #xf3 #xc2 "CMP" "SS"))
3091 (macrolet ((define-movsd/ss-sse-inst (name prefix)
3092 `(define-instruction ,name (segment dst src)
3094 ',(sse-inst-printer-list 'xmm-xmm/mem-dir
3097 (cond ((xmm-register-p dst)
3098 (emit-sse-inst segment dst src ,prefix #x10
3099 :operand-size :do-not-set))
3101 (aver (xmm-register-p src))
3102 (emit-sse-inst segment src dst ,prefix #x11
3103 :operand-size :do-not-set)))))))
3104 (define-movsd/ss-sse-inst movsd #xf2)
3105 (define-movsd/ss-sse-inst movss #xf3))
3108 (macrolet ((define-mov-sse-inst (name prefix opcode-from opcode-to
3109 &key force-to-mem reg-reg-name)
3112 `(define-instruction ,reg-reg-name (segment dst src)
3114 (aver (xmm-register-p dst))
3115 (aver (xmm-register-p src))
3116 (emit-regular-sse-inst segment dst src
3117 ,prefix ,opcode-from))))
3118 (define-instruction ,name (segment dst src)
3120 '(,@(when opcode-from
3121 (sse-inst-printer-list
3122 'xmm-xmm/mem prefix opcode-from))
3123 ,@(sse-inst-printer-list
3124 'xmm-xmm/mem prefix opcode-to
3125 :printer '(:name :tab reg/mem ", " reg))))
3127 (cond ,@(when opcode-from
3128 `(((xmm-register-p dst)
3130 `(aver (not (or (register-p src)
3131 (xmm-register-p src)))))
3132 (emit-regular-sse-inst
3133 segment dst src ,prefix ,opcode-from))))
3135 (aver (xmm-register-p src))
3137 `(aver (not (or (register-p dst)
3138 (xmm-register-p dst)))))
3139 (emit-regular-sse-inst segment src dst
3140 ,prefix ,opcode-to))))))))
3142 (define-mov-sse-inst movapd #x66 #x28 #x29)
3143 (define-mov-sse-inst movaps nil #x28 #x29)
3144 (define-mov-sse-inst movdqa #x66 #x6f #x7f)
3145 (define-mov-sse-inst movdqu #xf3 #x6f #x7f)
3148 (define-mov-sse-inst movntdq #x66 nil #xe7 :force-to-mem t)
3149 (define-mov-sse-inst movntpd #x66 nil #x2b :force-to-mem t)
3150 (define-mov-sse-inst movntps nil nil #x2b :force-to-mem t)
3152 ;; use movhps for movlhps and movlps for movhlps
3153 (define-mov-sse-inst movhpd #x66 #x16 #x17 :force-to-mem t)
3154 (define-mov-sse-inst movhps nil #x16 #x17 :reg-reg-name movlhps)
3155 (define-mov-sse-inst movlpd #x66 #x12 #x13 :force-to-mem t)
3156 (define-mov-sse-inst movlps nil #x12 #x13 :reg-reg-name movhlps)
3157 (define-mov-sse-inst movupd #x66 #x10 #x11)
3158 (define-mov-sse-inst movups nil #x10 #x11))
3161 (define-instruction movq (segment dst src)
3164 (sse-inst-printer-list 'xmm-xmm/mem #xf3 #x7e)
3165 (sse-inst-printer-list 'xmm-xmm/mem #x66 #xd6
3166 :printer '(:name :tab reg/mem ", " reg))))
3168 (cond ((xmm-register-p dst)
3169 (emit-sse-inst segment dst src #xf3 #x7e
3170 :operand-size :do-not-set))
3172 (aver (xmm-register-p src))
3173 (emit-sse-inst segment src dst #x66 #xd6
3174 :operand-size :do-not-set)))))
3176 ;;; Instructions having an XMM register as the destination operand
3177 ;;; and a general-purpose register or a memory location as the source
3178 ;;; operand. The operand size is calculated from the source operand.
3180 ;;; MOVD - Move a 32- or 64-bit value from a general-purpose register or
3181 ;;; a memory location to the low order 32 or 64 bits of an XMM register
3182 ;;; with zero extension or vice versa.
3183 ;;; We do not support the MMX version of this instruction.
3184 (define-instruction movd (segment dst src)
3187 (sse-inst-printer-list 'xmm-reg/mem #x66 #x6e)
3188 (sse-inst-printer-list 'xmm-reg/mem #x66 #x7e
3189 :printer '(:name :tab reg/mem ", " reg))))
3191 (cond ((xmm-register-p dst)
3192 (emit-sse-inst segment dst src #x66 #x6e))
3194 (aver (xmm-register-p src))
3195 (emit-sse-inst segment src dst #x66 #x7e)))))
3197 (define-instruction pinsrw (segment dst src imm)
3199 (sse-inst-printer-list
3200 'xmm-reg/mem #x66 #xc4
3201 :more-fields '((imm nil :type imm-byte))
3202 :printer '(:name :tab reg ", " reg/mem ", " imm)))
3204 (aver (xmm-register-p dst))
3205 (let ((src-size (operand-size src)))
3206 (aver (or (not (register-p src))
3207 (eq src-size :qword) (eq src-size :dword)))
3208 (emit-sse-inst segment dst src #x66 #xc4
3209 :operand-size (if (register-p src) src-size :do-not-set)
3210 :remaining-bytes 1))
3211 (emit-byte segment imm)))
3213 (define-instruction pextrw (segment dst src imm)
3215 (sse-inst-printer-list
3216 'reg-xmm/mem #x66 #xc5
3217 :more-fields '((imm nil :type imm-byte))
3218 :printer '(:name :tab reg ", " reg/mem ", " imm)))
3220 (aver (xmm-register-p src))
3221 (aver (register-p dst))
3222 (let ((dst-size (operand-size dst)))
3223 (aver (or (eq dst-size :qword) (eq dst-size :dword)))
3224 (emit-sse-inst segment dst src #x66 #xc5
3225 :operand-size dst-size
3226 :remaining-bytes 1))
3227 (emit-byte segment imm)))
3229 (macrolet ((define-integer-source-sse-inst (name prefix opcode &key mem-only)
3230 `(define-instruction ,name (segment dst src)
3232 ',(sse-inst-printer-list 'xmm-reg/mem prefix opcode))
3234 (aver (xmm-register-p dst))
3236 `(aver (not (or (register-p src)
3237 (xmm-register-p src)))))
3238 (let ((src-size (operand-size src)))
3239 (aver (or (eq src-size :qword) (eq src-size :dword))))
3240 (emit-sse-inst segment dst src ,prefix ,opcode)))))
3241 (define-integer-source-sse-inst cvtsi2sd #xf2 #x2a)
3242 (define-integer-source-sse-inst cvtsi2ss #xf3 #x2a)
3243 ;; FIXME: memory operand is always a QWORD
3244 (define-integer-source-sse-inst cvtpi2pd #x66 #x2a :mem-only t)
3245 (define-integer-source-sse-inst cvtpi2ps nil #x2a :mem-only t))
3247 ;;; Instructions having a general-purpose register as the destination
3248 ;;; operand and an XMM register or a memory location as the source
3249 ;;; operand. The operand size is calculated from the destination
3252 (macrolet ((define-gpr-destination-sse-inst (name prefix opcode &key reg-only)
3253 `(define-instruction ,name (segment dst src)
3255 ',(sse-inst-printer-list 'reg-xmm/mem prefix opcode))
3257 (aver (register-p dst))
3259 `(aver (xmm-register-p src)))
3260 (let ((dst-size (operand-size dst)))
3261 (aver (or (eq dst-size :qword) (eq dst-size :dword)))
3262 (emit-sse-inst segment dst src ,prefix ,opcode
3263 :operand-size dst-size))))))
3264 (define-gpr-destination-sse-inst cvtsd2si #xf2 #x2d)
3265 (define-gpr-destination-sse-inst cvtss2si #xf3 #x2d)
3266 (define-gpr-destination-sse-inst cvttsd2si #xf2 #x2c)
3267 (define-gpr-destination-sse-inst cvttss2si #xf3 #x2c)
3268 (define-gpr-destination-sse-inst movmskpd #x66 #x50 :reg-only t)
3269 (define-gpr-destination-sse-inst movmskps nil #x50 :reg-only t)
3270 (define-gpr-destination-sse-inst pmovmskb #x66 #xd7 :reg-only t))
3272 ;;;; We call these "2byte" instructions due to their two opcode bytes.
3273 ;;;; Intel and AMD call them three-byte instructions, as they count the
3274 ;;;; 0x0f byte for determining the number of opcode bytes.
3276 ;;; Instructions that take XMM-XMM/MEM and XMM-XMM/MEM-IMM arguments.
3278 (macrolet ((regular-2byte-sse-inst (name prefix op1 op2)
3279 `(define-instruction ,name (segment dst src)
3281 ',(2byte-sse-inst-printer-list '2byte-xmm-xmm/mem prefix op1 op2))
3283 (emit-regular-2byte-sse-inst segment dst src ,prefix ,op1 ,op2))))
3284 (regular-2byte-sse-inst-imm (name prefix op1 op2)
3285 `(define-instruction ,name (segment dst src imm)
3287 ',(2byte-sse-inst-printer-list '2byte-xmm-xmm/mem prefix op1 op2
3288 :more-fields '((imm nil :type imm-byte))
3289 :printer `(:name :tab reg ", " reg/mem ", " imm)))
3291 (aver (typep imm '(unsigned-byte 8)))
3292 (emit-regular-2byte-sse-inst segment dst src ,prefix ,op1 ,op2
3294 (emit-byte segment imm)))))
3295 (regular-2byte-sse-inst pshufb #x66 #x38 #x00)
3296 (regular-2byte-sse-inst phaddw #x66 #x38 #x01)
3297 (regular-2byte-sse-inst phaddd #x66 #x38 #x02)
3298 (regular-2byte-sse-inst phaddsw #x66 #x38 #x03)
3299 (regular-2byte-sse-inst pmaddubsw #x66 #x38 #x04)
3300 (regular-2byte-sse-inst phsubw #x66 #x38 #x05)
3301 (regular-2byte-sse-inst phsubd #x66 #x38 #x06)
3302 (regular-2byte-sse-inst phsubsw #x66 #x38 #x07)
3303 (regular-2byte-sse-inst psignb #x66 #x38 #x08)
3304 (regular-2byte-sse-inst psignw #x66 #x38 #x09)
3305 (regular-2byte-sse-inst psignd #x66 #x38 #x0a)
3306 (regular-2byte-sse-inst pmulhrsw #x66 #x38 #x0b)
3308 (regular-2byte-sse-inst pblendvb #x66 #x38 #x10)
3309 (regular-2byte-sse-inst blendvps #x66 #x38 #x14)
3310 (regular-2byte-sse-inst blendvpd #x66 #x38 #x15)
3311 (regular-2byte-sse-inst ptest #x66 #x38 #x17)
3312 (regular-2byte-sse-inst pabsb #x66 #x38 #x1c)
3313 (regular-2byte-sse-inst pabsw #x66 #x38 #x1d)
3314 (regular-2byte-sse-inst pabsd #x66 #x38 #x1e)
3316 (regular-2byte-sse-inst pmuldq #x66 #x38 #x28)
3317 (regular-2byte-sse-inst pcmpeqq #x66 #x38 #x29)
3318 (regular-2byte-sse-inst packusdw #x66 #x38 #x2b)
3320 (regular-2byte-sse-inst pcmpgtq #x66 #x38 #x37)
3321 (regular-2byte-sse-inst pminsb #x66 #x38 #x38)
3322 (regular-2byte-sse-inst pminsd #x66 #x38 #x39)
3323 (regular-2byte-sse-inst pminuw #x66 #x38 #x3a)
3324 (regular-2byte-sse-inst pminud #x66 #x38 #x3b)
3325 (regular-2byte-sse-inst pmaxsb #x66 #x38 #x3c)
3326 (regular-2byte-sse-inst pmaxsd #x66 #x38 #x3d)
3327 (regular-2byte-sse-inst pmaxuw #x66 #x38 #x3e)
3328 (regular-2byte-sse-inst pmaxud #x66 #x38 #x3f)
3330 (regular-2byte-sse-inst pmulld #x66 #x38 #x40)
3331 (regular-2byte-sse-inst phminposuw #x66 #x38 #x41)
3333 (regular-2byte-sse-inst aesimc #x66 #x38 #xdb)
3334 (regular-2byte-sse-inst aesenc #x66 #x38 #xdc)
3335 (regular-2byte-sse-inst aesenclast #x66 #x38 #xdd)
3336 (regular-2byte-sse-inst aesdec #x66 #x38 #xde)
3337 (regular-2byte-sse-inst aesdeclast #x66 #x38 #xdf)
3339 (regular-2byte-sse-inst-imm roundps #x66 #x3a #x08)
3340 (regular-2byte-sse-inst-imm roundpd #x66 #x3a #x09)
3341 (regular-2byte-sse-inst-imm roundss #x66 #x3a #x0a)
3342 (regular-2byte-sse-inst-imm roundsd #x66 #x3a #x0b)
3343 (regular-2byte-sse-inst-imm blendps #x66 #x3a #x0c)
3344 (regular-2byte-sse-inst-imm blendpd #x66 #x3a #x0d)
3345 (regular-2byte-sse-inst-imm pblendw #x66 #x3a #x0e)
3346 (regular-2byte-sse-inst-imm palignr #x66 #x3a #x0f)
3348 (regular-2byte-sse-inst-imm mpsadbw #x66 #x3a #x42)
3349 (regular-2byte-sse-inst-imm pclmulqdq #x66 #x3a #x44)
3351 (regular-2byte-sse-inst-imm pcmpestrm #x66 #x3a #x60)
3352 (regular-2byte-sse-inst-imm pcmpestri #x66 #x3a #x61)
3353 (regular-2byte-sse-inst-imm pcmpistrm #x66 #x3a #x62)
3354 (regular-2byte-sse-inst-imm pcmpistri #x66 #x3a #x63)
3356 (regular-2byte-sse-inst-imm aeskeygenassist #x66 #x3a #xdf))
3358 ;;; Other SSE instructions
3360 ;; FIXME: is that right!?
3361 (define-instruction movnti (segment dst src)
3362 (:printer ext-reg-reg/mem-no-width ((op #xc3)))
3364 (aver (not (or (register-p dst)
3365 (xmm-register-p dst))))
3366 (aver (register-p src))
3367 (maybe-emit-rex-for-ea segment src dst)
3368 (emit-byte segment #x0f)
3369 (emit-byte segment #xc3)
3370 (emit-ea segment dst (reg-tn-encoding src))))
3372 (define-instruction prefetch (segment type src)
3373 (:printer ext-reg/mem-no-width ((op '(#x18 0)))
3374 '("PREFETCHNTA" :tab reg/mem))
3375 (:printer ext-reg/mem-no-width ((op '(#x18 1)))
3376 '("PREFETCHT0" :tab reg/mem))
3377 (:printer ext-reg/mem-no-width ((op '(#x18 2)))
3378 '("PREFETCHT1" :tab reg/mem))
3379 (:printer ext-reg/mem-no-width ((op '(#x18 3)))
3380 '("PREFETCHT2" :tab reg/mem))
3382 (aver (not (or (register-p src)
3383 (xmm-register-p src))))
3384 (aver (eq (operand-size src) :byte))
3385 (let ((type (position type #(:nta :t0 :t1 :t2))))
3387 (maybe-emit-rex-for-ea segment src nil)
3388 (emit-byte segment #x0f)
3389 (emit-byte segment #x18)
3390 (emit-ea segment src type))))
3392 (define-instruction clflush (segment src)
3393 (:printer ext-reg/mem-no-width ((op '(#xae 7))))
3395 (aver (not (or (register-p src)
3396 (xmm-register-p src))))
3397 (aver (eq (operand-size src) :byte))
3398 (maybe-emit-rex-for-ea segment src nil)
3399 (emit-byte segment #x0f)
3400 (emit-byte segment #xae)
3401 (emit-ea segment src 7)))
3403 (macrolet ((define-fence-instruction (name last-byte)
3404 `(define-instruction ,name (segment)
3405 (:printer three-bytes ((op '(#x0f #xae ,last-byte))))
3407 (emit-byte segment #x0f)
3408 (emit-byte segment #xae)
3409 (emit-byte segment ,last-byte)))))
3410 (define-fence-instruction lfence #b11101000)
3411 (define-fence-instruction mfence #b11110000)
3412 (define-fence-instruction sfence #b11111000))
3414 (define-instruction pause (segment)
3415 (:printer two-bytes ((op '(#xf3 #x90))))
3417 (emit-byte segment #xf3)
3418 (emit-byte segment #x90)))
3420 (define-instruction ldmxcsr (segment src)
3421 (:printer ext-reg/mem-no-width ((op '(#xae 2))))
3423 (aver (not (or (register-p src)
3424 (xmm-register-p src))))
3425 (aver (eq (operand-size src) :dword))
3426 (maybe-emit-rex-for-ea segment src nil)
3427 (emit-byte segment #x0f)
3428 (emit-byte segment #xae)
3429 (emit-ea segment src 2)))
3431 (define-instruction stmxcsr (segment dst)
3432 (:printer ext-reg/mem-no-width ((op '(#xae 3))))
3434 (aver (not (or (register-p dst)
3435 (xmm-register-p dst))))
3436 (aver (eq (operand-size dst) :dword))
3437 (maybe-emit-rex-for-ea segment dst nil)
3438 (emit-byte segment #x0f)
3439 (emit-byte segment #xae)
3440 (emit-ea segment dst 3)))
3444 (define-instruction cpuid (segment)
3445 (:printer two-bytes ((op '(#b00001111 #b10100010))))
3447 (emit-byte segment #b00001111)
3448 (emit-byte segment #b10100010)))
3450 (define-instruction rdtsc (segment)
3451 (:printer two-bytes ((op '(#b00001111 #b00110001))))
3453 (emit-byte segment #b00001111)
3454 (emit-byte segment #b00110001)))
3456 ;;;; Late VM definitions
3458 (defun canonicalize-inline-constant (constant &aux (alignedp nil))
3459 (let ((first (car constant)))
3460 (when (eql first :aligned)
3463 (setf first (car constant)))
3465 (single-float (setf constant (list :single-float first)))
3466 (double-float (setf constant (list :double-float first)))
3467 ((complex single-float)
3468 (setf constant (list :complex-single-float first)))
3469 ((complex double-float)
3470 (setf constant (list :complex-double-float first)))))
3471 (destructuring-bind (type value) constant
3473 ((:byte :word :dword :qword)
3474 (aver (integerp value))
3477 (aver (base-char-p value))
3478 (cons :byte (char-code value)))
3480 (aver (characterp value))
3481 (cons :dword (char-code value)))
3483 (aver (typep value 'single-float))
3484 (cons (if alignedp :oword :dword)
3485 (ldb (byte 32 0) (single-float-bits value))))
3487 (aver (typep value 'double-float))
3488 (cons (if alignedp :oword :qword)
3489 (ldb (byte 64 0) (logior (ash (double-float-high-bits value) 32)
3490 (double-float-low-bits value)))))
3491 ((:complex-single-float)
3492 (aver (typep value '(complex single-float)))
3493 (cons (if alignedp :oword :qword)
3495 (logior (ash (single-float-bits (imagpart value)) 32)
3497 (single-float-bits (realpart value)))))))
3499 (aver (integerp value))
3500 (cons :oword value))
3501 ((:complex-double-float)
3502 (aver (typep value '(complex double-float)))
3504 (logior (ash (double-float-high-bits (imagpart value)) 96)
3505 (ash (double-float-low-bits (imagpart value)) 64)
3506 (ash (ldb (byte 32 0)
3507 (double-float-high-bits (realpart value)))
3509 (double-float-low-bits (realpart value))))))))
3511 (defun inline-constant-value (constant)
3512 (let ((label (gen-label))
3513 (size (ecase (car constant)
3514 ((:byte :word :dword :qword) (car constant))
3515 ((:oword) :qword))))
3516 (values label (make-ea size
3517 :disp (make-fixup nil :code-object label)))))
3519 (defun emit-constant-segment-header (segment constants optimize)
3520 (declare (ignore constants))
3521 (emit-long-nop segment (if optimize 64 16)))
3523 (defun size-nbyte (size)
3531 (defun sort-inline-constants (constants)
3532 (stable-sort constants #'> :key (lambda (constant)
3533 (size-nbyte (caar constant)))))
3535 (defun emit-inline-constant (constant label)
3536 (let ((size (size-nbyte (car constant))))
3537 (emit-alignment (integer-length (1- size)))
3539 (let ((val (cdr constant)))
3541 do (inst byte (ldb (byte 8 0) val))
3542 (setf val (ash val -8))))))