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 width sized-p 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 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 (sb!disassem:define-instruction-format (ext-reg/mem-no-width+imm8 24
755 :include 'ext-reg/mem-no-width
757 '(:name :tab reg/mem ", " imm))
758 (imm :type 'imm-byte))
760 ;;;; XMM instructions
762 ;;; All XMM instructions use an extended opcode (#x0F as the first
763 ;;; opcode byte). Therefore in the following "EXT" in the name of the
764 ;;; instruction formats refers to the formats that have an additional
765 ;;; prefix (#x66, #xF2 or #xF3).
767 ;;; Instructions having an XMM register as the destination operand
768 ;;; and an XMM register or a memory location as the source operand.
769 ;;; The size of the operands is implicitly given by the instruction.
770 (sb!disassem:define-instruction-format (xmm-xmm/mem 24
772 '(:name :tab reg ", " reg/mem))
773 (x0f :field (byte 8 0) :value #x0f)
774 (op :field (byte 8 8))
775 (reg/mem :fields (list (byte 2 22) (byte 3 16))
777 (reg :field (byte 3 19) :type 'xmmreg)
781 (sb!disassem:define-instruction-format (ext-xmm-xmm/mem 32
783 '(:name :tab reg ", " reg/mem))
784 (prefix :field (byte 8 0))
785 (x0f :field (byte 8 8) :value #x0f)
786 (op :field (byte 8 16))
787 (reg/mem :fields (list (byte 2 30) (byte 3 24))
789 (reg :field (byte 3 27) :type 'xmmreg)
792 (sb!disassem:define-instruction-format (ext-rex-xmm-xmm/mem 40
794 '(:name :tab reg ", " reg/mem))
795 (prefix :field (byte 8 0))
796 (rex :field (byte 4 12) :value #b0100)
797 (wrxb :field (byte 4 8) :type 'wrxb)
798 (x0f :field (byte 8 16) :value #x0f)
799 (op :field (byte 8 24))
800 (reg/mem :fields (list (byte 2 38) (byte 3 32))
802 (reg :field (byte 3 35) :type 'xmmreg)
805 (sb!disassem:define-instruction-format (ext-2byte-xmm-xmm/mem 40
807 '(:name :tab reg ", " reg/mem))
808 (prefix :field (byte 8 0))
809 (x0f :field (byte 8 8) :value #x0f)
810 (op1 :field (byte 8 16)) ; #x38 or #x3a
811 (op2 :field (byte 8 24))
812 (reg/mem :fields (list (byte 2 38) (byte 3 32))
814 (reg :field (byte 3 35) :type 'xmmreg))
816 (sb!disassem:define-instruction-format (ext-rex-2byte-xmm-xmm/mem 48
818 '(:name :tab reg ", " reg/mem))
819 (prefix :field (byte 8 0))
820 (rex :field (byte 4 12) :value #b0100)
821 (wrxb :field (byte 4 8) :type 'wrxb)
822 (x0f :field (byte 8 16) :value #x0f)
823 (op1 :field (byte 8 24)) ; #x38 or #x3a
824 (op2 :field (byte 8 32))
825 (reg/mem :fields (list (byte 2 46) (byte 3 40))
827 (reg :field (byte 3 43) :type 'xmmreg))
829 ;;; Same as xmm-xmm/mem etc., but with direction bit.
831 (sb!disassem:define-instruction-format (ext-xmm-xmm/mem-dir 32
832 :include 'ext-xmm-xmm/mem
836 ,(swap-if 'dir 'reg ", " 'reg/mem)))
837 (op :field (byte 7 17))
838 (dir :field (byte 1 16)))
840 (sb!disassem:define-instruction-format (ext-rex-xmm-xmm/mem-dir 40
841 :include 'ext-rex-xmm-xmm/mem
845 ,(swap-if 'dir 'reg ", " 'reg/mem)))
846 (op :field (byte 7 25))
847 (dir :field (byte 1 24)))
849 ;;; Instructions having an XMM register as one operand
850 ;;; and a constant (unsigned) byte as the other.
852 (sb!disassem:define-instruction-format (ext-xmm-imm 32
854 '(:name :tab reg/mem ", " imm))
855 (prefix :field (byte 8 0))
856 (x0f :field (byte 8 8) :value #x0f)
857 (op :field (byte 8 16))
858 (/i :field (byte 3 27))
859 (b11 :field (byte 2 30) :value #b11)
860 (reg/mem :field (byte 3 24)
862 (imm :type 'imm-byte))
864 (sb!disassem:define-instruction-format (ext-rex-xmm-imm 40
866 '(:name :tab reg/mem ", " imm))
867 (prefix :field (byte 8 0))
868 (rex :field (byte 4 12) :value #b0100)
869 (wrxb :field (byte 4 8) :type 'wrxb)
870 (x0f :field (byte 8 16) :value #x0f)
871 (op :field (byte 8 24))
872 (/i :field (byte 3 35))
873 (b11 :field (byte 2 38) :value #b11)
874 (reg/mem :field (byte 3 32)
876 (imm :type 'imm-byte))
878 ;;; Instructions having an XMM register as one operand and a general-
879 ;;; -purpose register or a memory location as the other operand.
881 (sb!disassem:define-instruction-format (xmm-reg/mem 24
883 '(:name :tab reg ", " reg/mem))
884 (x0f :field (byte 8 0) :value #x0f)
885 (op :field (byte 8 8))
886 (reg/mem :fields (list (byte 2 22) (byte 3 16))
887 :type 'sized-reg/mem)
888 (reg :field (byte 3 19) :type 'xmmreg)
891 (sb!disassem:define-instruction-format (ext-xmm-reg/mem 32
893 '(:name :tab reg ", " reg/mem))
894 (prefix :field (byte 8 0))
895 (x0f :field (byte 8 8) :value #x0f)
896 (op :field (byte 8 16))
897 (reg/mem :fields (list (byte 2 30) (byte 3 24))
898 :type 'sized-reg/mem)
899 (reg :field (byte 3 27) :type 'xmmreg)
902 (sb!disassem:define-instruction-format (ext-rex-xmm-reg/mem 40
904 '(:name :tab reg ", " reg/mem))
905 (prefix :field (byte 8 0))
906 (rex :field (byte 4 12) :value #b0100)
907 (wrxb :field (byte 4 8) :type 'wrxb)
908 (x0f :field (byte 8 16) :value #x0f)
909 (op :field (byte 8 24))
910 (reg/mem :fields (list (byte 2 38) (byte 3 32))
911 :type 'sized-reg/mem)
912 (reg :field (byte 3 35) :type 'xmmreg)
915 (sb!disassem:define-instruction-format (ext-2byte-xmm-reg/mem 40
917 '(:name :tab reg ", " reg/mem))
918 (prefix :field (byte 8 0))
919 (x0f :field (byte 8 8) :value #x0f)
920 (op1 :field (byte 8 16))
921 (op2 :field (byte 8 24))
922 (reg/mem :fields (list (byte 2 38) (byte 3 32)) :type 'sized-reg/mem)
923 (reg :field (byte 3 35) :type 'xmmreg)
926 ;;; Instructions having a general-purpose register as one operand and an
927 ;;; XMM register or a memory location as the other operand.
929 (sb!disassem:define-instruction-format (reg-xmm/mem 24
931 '(:name :tab reg ", " reg/mem))
932 (x0f :field (byte 8 0) :value #x0f)
933 (op :field (byte 8 8))
934 (reg/mem :fields (list (byte 2 22) (byte 3 16))
936 (reg :field (byte 3 19) :type 'reg))
938 (sb!disassem:define-instruction-format (ext-reg-xmm/mem 32
940 '(:name :tab reg ", " reg/mem))
941 (prefix :field (byte 8 0))
942 (x0f :field (byte 8 8) :value #x0f)
943 (op :field (byte 8 16))
944 (reg/mem :fields (list (byte 2 30) (byte 3 24))
946 (reg :field (byte 3 27) :type 'reg))
948 (sb!disassem:define-instruction-format (ext-rex-reg-xmm/mem 40
950 '(:name :tab reg ", " reg/mem))
951 (prefix :field (byte 8 0))
952 (rex :field (byte 4 12) :value #b0100)
953 (wrxb :field (byte 4 8) :type 'wrxb)
954 (x0f :field (byte 8 16) :value #x0f)
955 (op :field (byte 8 24))
956 (reg/mem :fields (list (byte 2 38) (byte 3 32))
958 (reg :field (byte 3 35) :type 'reg))
960 ;;; Instructions having a general-purpose register or a memory location
961 ;;; as one operand and an a XMM register as the other operand.
963 (sb!disassem:define-instruction-format (ext-reg/mem-xmm 32
965 '(:name :tab reg/mem ", " reg))
966 (prefix :field (byte 8 0))
967 (x0f :field (byte 8 8) :value #x0f)
968 (op :field (byte 8 16))
969 (reg/mem :fields (list (byte 2 30) (byte 3 24))
971 (reg :field (byte 3 27) :type 'xmmreg)
974 (sb!disassem:define-instruction-format (ext-rex-reg/mem-xmm 40
976 '(:name :tab reg/mem ", " reg))
977 (prefix :field (byte 8 0))
978 (rex :field (byte 4 12) :value #b0100)
979 (wrxb :field (byte 4 8) :type 'wrxb)
980 (x0f :field (byte 8 16) :value #x0f)
981 (op :field (byte 8 24))
982 (reg/mem :fields (list (byte 2 38) (byte 3 32))
984 (reg :field (byte 3 35) :type 'xmmreg)
987 (sb!disassem:define-instruction-format (ext-2byte-reg/mem-xmm 40
989 '(:name :tab reg/mem ", " reg))
990 (prefix :field (byte 8 0))
991 (x0f :field (byte 8 8) :value #x0f)
992 (op1 :field (byte 8 16))
993 (op2 :field (byte 8 24))
994 (reg/mem :fields (list (byte 2 38) (byte 3 32)) :type 'reg/mem)
995 (reg :field (byte 3 35) :type 'xmmreg)
998 (sb!disassem:define-instruction-format (ext-rex-2byte-reg/mem-xmm 48
1000 '(:name :tab reg/mem ", " reg))
1001 (prefix :field (byte 8 0))
1002 (rex :field (byte 4 12) :value #b0100)
1003 (wrxb :field (byte 4 8) :type 'wrxb)
1004 (x0f :field (byte 8 16) :value #x0f)
1005 (op1 :field (byte 8 24))
1006 (op2 :field (byte 8 32))
1007 (reg/mem :fields (list (byte 2 46) (byte 3 40)) :type 'reg/mem)
1008 (reg :field (byte 3 43) :type 'xmmreg)
1011 ;;; Instructions having a general-purpose register as one operand and an a
1012 ;;; general-purpose register or a memory location as the other operand,
1013 ;;; and using a prefix byte.
1015 (sb!disassem:define-instruction-format (ext-prefix-reg-reg/mem 32
1017 '(:name :tab reg ", " reg/mem))
1018 (prefix :field (byte 8 0))
1019 (x0f :field (byte 8 8) :value #x0f)
1020 (op :field (byte 8 16))
1021 (reg/mem :fields (list (byte 2 30) (byte 3 24))
1022 :type 'sized-reg/mem)
1023 (reg :field (byte 3 27) :type 'reg))
1025 (sb!disassem:define-instruction-format (ext-rex-prefix-reg-reg/mem 40
1027 '(:name :tab reg ", " reg/mem))
1028 (prefix :field (byte 8 0))
1029 (rex :field (byte 4 12) :value #b0100)
1030 (wrxb :field (byte 4 8) :type 'wrxb)
1031 (x0f :field (byte 8 16) :value #x0f)
1032 (op :field (byte 8 24))
1033 (reg/mem :fields (list (byte 2 38) (byte 3 32))
1034 :type 'sized-reg/mem)
1035 (reg :field (byte 3 35) :type 'reg))
1037 (sb!disassem:define-instruction-format (ext-2byte-prefix-reg-reg/mem 40
1039 '(:name :tab reg ", " reg/mem))
1040 (prefix :field (byte 8 0))
1041 (x0f :field (byte 8 8) :value #x0f)
1042 (op1 :field (byte 8 16)) ; #x38 or #x3a
1043 (op2 :field (byte 8 24))
1044 (reg/mem :fields (list (byte 2 38) (byte 3 32))
1045 :type 'sized-reg/mem)
1046 (reg :field (byte 3 35) :type 'reg))
1048 (sb!disassem:define-instruction-format (ext-rex-2byte-prefix-reg-reg/mem 48
1050 '(:name :tab reg ", " reg/mem))
1051 (prefix :field (byte 8 0))
1052 (rex :field (byte 4 12) :value #b0100)
1053 (wrxb :field (byte 4 8) :type 'wrxb)
1054 (x0f :field (byte 8 16) :value #x0f)
1055 (op1 :field (byte 8 24)) ; #x38 or #x3a
1056 (op2 :field (byte 8 32))
1057 (reg/mem :fields (list (byte 2 46) (byte 3 40))
1058 :type 'sized-reg/mem)
1059 (reg :field (byte 3 43) :type 'reg))
1061 ;; XMM comparison instruction
1063 (eval-when (:compile-toplevel :load-toplevel :execute)
1064 (defparameter *sse-conditions* #(:eq :lt :le :unord :neq :nlt :nle :ord)))
1066 (sb!disassem:define-arg-type sse-condition-code
1067 ;; Inherit the prefilter from IMM-BYTE to READ-SUFFIX the byte.
1069 :printer *sse-conditions*)
1071 (sb!disassem:define-instruction-format (string-op 8
1073 :default-printer '(:name width)))
1075 (sb!disassem:define-instruction-format (short-cond-jump 16)
1076 (op :field (byte 4 4))
1077 (cc :field (byte 4 0) :type 'condition-code)
1078 (label :field (byte 8 8) :type 'displacement))
1080 (sb!disassem:define-instruction-format (short-jump 16
1081 :default-printer '(:name :tab label))
1082 (const :field (byte 4 4) :value #b1110)
1083 (op :field (byte 4 0))
1084 (label :field (byte 8 8) :type 'displacement))
1086 (sb!disassem:define-instruction-format (near-cond-jump 16)
1087 (op :fields (list (byte 8 0) (byte 4 12)) :value '(#b00001111 #b1000))
1088 (cc :field (byte 4 8) :type 'condition-code)
1089 ;; The disassembler currently doesn't let you have an instruction > 32 bits
1090 ;; long, so we fake it by using a prefilter to read the offset.
1091 (label :type 'displacement
1092 :prefilter (lambda (value dstate)
1093 (declare (ignore value)) ; always nil anyway
1094 (sb!disassem:read-signed-suffix 32 dstate))))
1096 (sb!disassem:define-instruction-format (near-jump 8
1097 :default-printer '(:name :tab label))
1098 (op :field (byte 8 0))
1099 ;; The disassembler currently doesn't let you have an instruction > 32 bits
1100 ;; long, so we fake it by using a prefilter to read the address.
1101 (label :type 'displacement
1102 :prefilter (lambda (value dstate)
1103 (declare (ignore value)) ; always nil anyway
1104 (sb!disassem:read-signed-suffix 32 dstate))))
1107 (sb!disassem:define-instruction-format (cond-set 24
1108 :default-printer '('set cc :tab reg/mem))
1109 (prefix :field (byte 8 0) :value #b00001111)
1110 (op :field (byte 4 12) :value #b1001)
1111 (cc :field (byte 4 8) :type 'condition-code)
1112 (reg/mem :fields (list (byte 2 22) (byte 3 16))
1113 :type 'sized-byte-reg/mem)
1114 (reg :field (byte 3 19) :value #b000))
1116 (sb!disassem:define-instruction-format (cond-move 24
1118 '('cmov cc :tab reg ", " reg/mem))
1119 (prefix :field (byte 8 0) :value #b00001111)
1120 (op :field (byte 4 12) :value #b0100)
1121 (cc :field (byte 4 8) :type 'condition-code)
1122 (reg/mem :fields (list (byte 2 22) (byte 3 16))
1124 (reg :field (byte 3 19) :type 'reg))
1126 (sb!disassem:define-instruction-format (enter-format 32
1127 :default-printer '(:name
1129 (:unless (:constant 0)
1131 (op :field (byte 8 0))
1132 (disp :field (byte 16 8))
1133 (level :field (byte 8 24)))
1135 ;;; Single byte instruction with an immediate byte argument.
1136 (sb!disassem:define-instruction-format (byte-imm 16
1137 :default-printer '(:name :tab code))
1138 (op :field (byte 8 0))
1139 (code :field (byte 8 8)))
1141 ;;; Two byte instruction with an immediate byte argument.
1143 (sb!disassem:define-instruction-format (word-imm 24
1144 :default-printer '(:name :tab code))
1145 (op :field (byte 16 0))
1146 (code :field (byte 8 16)))
1148 ;;; F3 escape map - Needs a ton more work.
1150 (sb!disassem:define-instruction-format (F3-escape 24)
1151 (prefix1 :field (byte 8 0) :value #xF3)
1152 (prefix2 :field (byte 8 8) :value #x0F)
1153 (op :field (byte 8 16)))
1155 (sb!disassem:define-instruction-format (rex-F3-escape 32)
1156 ;; F3 is a legacy prefix which was generalized to select an alternate opcode
1157 ;; map. Legacy prefixes are encoded in the instruction before a REX prefix.
1158 (prefix1 :field (byte 8 0) :value #xF3)
1159 (rex :field (byte 4 12) :value 4) ; "prefix2"
1160 (wrxb :field (byte 4 8) :type 'wrxb)
1161 (prefix3 :field (byte 8 16) :value #x0F)
1162 (op :field (byte 8 24)))
1164 (sb!disassem:define-instruction-format (F3-escape-reg-reg/mem 32
1167 '(:name :tab reg "," reg/mem))
1168 (reg/mem :fields (list (byte 2 30) (byte 3 24)) :type 'sized-reg/mem)
1169 (reg :field (byte 3 27) :type 'reg))
1171 (sb!disassem:define-instruction-format (rex-F3-escape-reg-reg/mem 40
1172 :include 'rex-F3-escape
1174 '(:name :tab reg "," reg/mem))
1175 (reg/mem :fields (list (byte 2 38) (byte 3 32)) :type 'sized-reg/mem)
1176 (reg :field (byte 3 35) :type 'reg))
1179 ;;;; primitive emitters
1181 (define-bitfield-emitter emit-word 16
1184 (define-bitfield-emitter emit-dword 32
1187 ;;; Most uses of dwords are as displacements or as immediate values in
1188 ;;; 64-bit operations. In these cases they are sign-extended to 64 bits.
1189 ;;; EMIT-DWORD is unsuitable there because it accepts values of type
1190 ;;; (OR (SIGNED-BYTE 32) (UNSIGNED-BYTE 32)), so we provide a more
1191 ;;; restricted emitter here.
1192 (defun emit-signed-dword (segment value)
1193 (declare (type segment segment)
1194 (type (signed-byte 32) value))
1195 (declare (inline emit-dword))
1196 (emit-dword segment value))
1198 (define-bitfield-emitter emit-qword 64
1201 (define-bitfield-emitter emit-byte-with-reg 8
1202 (byte 5 3) (byte 3 0))
1204 (define-bitfield-emitter emit-mod-reg-r/m-byte 8
1205 (byte 2 6) (byte 3 3) (byte 3 0))
1207 (define-bitfield-emitter emit-sib-byte 8
1208 (byte 2 6) (byte 3 3) (byte 3 0))
1210 (define-bitfield-emitter emit-rex-byte 8
1211 (byte 4 4) (byte 1 3) (byte 1 2) (byte 1 1) (byte 1 0))
1217 (defun emit-absolute-fixup (segment fixup &optional quad-p)
1218 (note-fixup segment (if quad-p :absolute64 :absolute) fixup)
1219 (let ((offset (fixup-offset fixup)))
1220 (if (label-p offset)
1221 (emit-back-patch segment
1223 (lambda (segment posn)
1224 (declare (ignore posn))
1225 (let ((val (- (+ (component-header-length)
1226 (or (label-position offset)
1228 other-pointer-lowtag)))
1230 (emit-qword segment val)
1231 (emit-signed-dword segment val)))))
1233 (emit-qword segment (or offset 0))
1234 (emit-signed-dword segment (or offset 0))))))
1236 (defun emit-relative-fixup (segment fixup)
1237 (note-fixup segment :relative fixup)
1238 (emit-signed-dword segment (or (fixup-offset fixup) 0)))
1241 ;;;; the effective-address (ea) structure
1243 (defun reg-tn-encoding (tn)
1244 (declare (type tn tn))
1245 ;; ea only has space for three bits of register number: regs r8
1246 ;; and up are selected by a REX prefix byte which caller is responsible
1247 ;; for having emitted where necessary already
1248 (ecase (sb-name (sc-sb (tn-sc tn)))
1250 (let ((offset (mod (tn-offset tn) 16)))
1251 (logior (ash (logand offset 1) 2)
1254 (mod (tn-offset tn) 8))))
1256 (defstruct (ea (:constructor make-ea (size &key base index scale disp))
1258 ;; note that we can represent an EA with a QWORD size, but EMIT-EA
1259 ;; can't actually emit it on its own: caller also needs to emit REX
1261 (size nil :type (member :byte :word :dword :qword))
1262 (base nil :type (or tn null))
1263 (index nil :type (or tn null))
1264 (scale 1 :type (member 1 2 4 8))
1265 (disp 0 :type (or (unsigned-byte 32) (signed-byte 32) fixup)))
1266 (def!method print-object ((ea ea) stream)
1267 (cond ((or *print-escape* *print-readably*)
1268 (print-unreadable-object (ea stream :type t)
1270 "~S~@[ base=~S~]~@[ index=~S~]~@[ scale=~S~]~@[ disp=~S~]"
1274 (let ((scale (ea-scale ea)))
1275 (if (= scale 1) nil scale))
1278 (format stream "~A PTR [" (symbol-name (ea-size ea)))
1280 (write-string (sb!c::location-print-name (ea-base ea)) stream)
1282 (write-string "+" stream)))
1284 (write-string (sb!c::location-print-name (ea-index ea)) stream))
1285 (unless (= (ea-scale ea) 1)
1286 (format stream "*~A" (ea-scale ea)))
1287 (typecase (ea-disp ea)
1290 (format stream "~@D" (ea-disp ea)))
1292 (format stream "+~A" (ea-disp ea))))
1293 (write-char #\] stream))))
1295 (defun emit-constant-tn-rip (segment constant-tn reg remaining-bytes)
1296 ;; AMD64 doesn't currently have a code object register to use as a
1297 ;; base register for constant access. Instead we use RIP-relative
1298 ;; addressing. The offset from the SIMPLE-FUN-HEADER to the instruction
1299 ;; is passed to the backpatch callback. In addition we need the offset
1300 ;; from the start of the function header to the slot in the CODE-HEADER
1301 ;; that stores the constant. Since we don't know where the code header
1302 ;; starts, instead count backwards from the function header.
1303 (let* ((2comp (component-info *component-being-compiled*))
1304 (constants (ir2-component-constants 2comp))
1305 (len (length constants))
1306 ;; Both CODE-HEADER and SIMPLE-FUN-HEADER are 16-byte aligned.
1307 ;; If there are an even amount of constants, there will be
1308 ;; an extra qword of padding before the function header, which
1309 ;; needs to be adjusted for. XXX: This will break if new slots
1310 ;; are added to the code header.
1311 (offset (* (- (+ len (if (evenp len)
1314 (tn-offset constant-tn))
1316 ;; RIP-relative addressing
1317 (emit-mod-reg-r/m-byte segment #b00 reg #b101)
1318 (emit-back-patch segment
1320 (lambda (segment posn)
1321 ;; The addressing is relative to end of instruction,
1322 ;; i.e. the end of this dword. Hence the + 4.
1323 (emit-signed-dword segment
1324 (+ 4 remaining-bytes
1325 (- (+ offset posn)))))))
1328 (defun emit-label-rip (segment fixup reg remaining-bytes)
1329 (let ((label (fixup-offset fixup)))
1330 ;; RIP-relative addressing
1331 (emit-mod-reg-r/m-byte segment #b00 reg #b101)
1332 (emit-back-patch segment
1334 (lambda (segment posn)
1335 (emit-signed-dword segment
1336 (- (label-position label)
1337 (+ posn 4 remaining-bytes))))))
1340 (defun emit-ea (segment thing reg &key allow-constants (remaining-bytes 0))
1343 ;; this would be eleganter if we had a function that would create
1345 (ecase (sb-name (sc-sb (tn-sc thing)))
1346 ((registers float-registers)
1347 (emit-mod-reg-r/m-byte segment #b11 reg (reg-tn-encoding thing)))
1349 ;; Convert stack tns into an index off RBP.
1350 (let ((disp (frame-byte-offset (tn-offset thing))))
1351 (cond ((<= -128 disp 127)
1352 (emit-mod-reg-r/m-byte segment #b01 reg #b101)
1353 (emit-byte segment disp))
1355 (emit-mod-reg-r/m-byte segment #b10 reg #b101)
1356 (emit-signed-dword segment disp)))))
1358 (unless allow-constants
1361 "Constant TNs can only be directly used in MOV, PUSH, and CMP."))
1362 (emit-constant-tn-rip segment thing reg remaining-bytes))))
1364 (let* ((base (ea-base thing))
1365 (index (ea-index thing))
1366 (scale (ea-scale thing))
1367 (disp (ea-disp thing))
1368 (mod (cond ((or (null base)
1370 (not (= (reg-tn-encoding base) #b101))))
1372 ((and (fixnump disp) (<= -128 disp 127))
1376 (r/m (cond (index #b100)
1378 (t (reg-tn-encoding base)))))
1379 (when (and (fixup-p disp)
1380 (label-p (fixup-offset disp)))
1383 (return-from emit-ea (emit-ea segment disp reg
1384 :allow-constants allow-constants
1385 :remaining-bytes remaining-bytes)))
1386 (when (and (= mod 0) (= r/m #b101))
1387 ;; this is rip-relative in amd64, so we'll use a sib instead
1388 (setf r/m #b100 scale 1))
1389 (emit-mod-reg-r/m-byte segment mod reg r/m)
1391 (let ((ss (1- (integer-length scale)))
1392 (index (if (null index)
1394 (let ((index (reg-tn-encoding index)))
1396 (error "can't index off of ESP")
1398 (base (if (null base)
1400 (reg-tn-encoding base))))
1401 (emit-sib-byte segment ss index base)))
1403 (emit-byte segment disp))
1404 ((or (= mod #b10) (null base))
1406 (emit-absolute-fixup segment disp)
1407 (emit-signed-dword segment disp))))))
1409 (typecase (fixup-offset thing)
1411 (emit-label-rip segment thing reg remaining-bytes))
1413 (emit-mod-reg-r/m-byte segment #b00 reg #b100)
1414 (emit-sib-byte segment 0 #b100 #b101)
1415 (emit-absolute-fixup segment thing))))))
1417 (defun byte-reg-p (thing)
1419 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)
1420 (member (sc-name (tn-sc thing)) *byte-sc-names*)
1423 (defun byte-ea-p (thing)
1425 (ea (eq (ea-size thing) :byte))
1427 (and (member (sc-name (tn-sc thing)) *byte-sc-names*) t))
1430 (defun word-reg-p (thing)
1432 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)
1433 (member (sc-name (tn-sc thing)) *word-sc-names*)
1436 (defun word-ea-p (thing)
1438 (ea (eq (ea-size thing) :word))
1439 (tn (and (member (sc-name (tn-sc thing)) *word-sc-names*) t))
1442 (defun dword-reg-p (thing)
1444 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)
1445 (member (sc-name (tn-sc thing)) *dword-sc-names*)
1448 (defun dword-ea-p (thing)
1450 (ea (eq (ea-size thing) :dword))
1452 (and (member (sc-name (tn-sc thing)) *dword-sc-names*) t))
1455 (defun qword-reg-p (thing)
1457 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)
1458 (member (sc-name (tn-sc thing)) *qword-sc-names*)
1461 (defun qword-ea-p (thing)
1463 (ea (eq (ea-size thing) :qword))
1465 (and (member (sc-name (tn-sc thing)) *qword-sc-names*) t))
1468 ;;; Return true if THING is a general-purpose register TN.
1469 (defun register-p (thing)
1471 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)))
1473 (defun accumulator-p (thing)
1474 (and (register-p thing)
1475 (= (tn-offset thing) 0)))
1477 ;;; Return true if THING is an XMM register TN.
1478 (defun xmm-register-p (thing)
1480 (eq (sb-name (sc-sb (tn-sc thing))) 'float-registers)))
1485 (def!constant +operand-size-prefix-byte+ #b01100110)
1487 (defun maybe-emit-operand-size-prefix (segment size)
1488 (unless (or (eq size :byte)
1489 (eq size :qword) ; REX prefix handles this
1490 (eq size +default-operand-size+))
1491 (emit-byte segment +operand-size-prefix-byte+)))
1493 ;;; A REX prefix must be emitted if at least one of the following
1494 ;;; conditions is true:
1495 ;; 1. The operand size is :QWORD and the default operand size of the
1496 ;; instruction is not :QWORD.
1497 ;;; 2. The instruction references an extended register.
1498 ;;; 3. The instruction references one of the byte registers SIL, DIL,
1501 ;;; Emit a REX prefix if necessary. OPERAND-SIZE is used to determine
1502 ;;; whether to set REX.W. Callers pass it explicitly as :DO-NOT-SET if
1503 ;;; this should not happen, for example because the instruction's
1504 ;;; default operand size is qword. R, X and B are NIL or TNs specifying
1505 ;;; registers the encodings of which are extended with the REX.R, REX.X
1506 ;;; and REX.B bit, respectively. To determine whether one of the byte
1507 ;;; registers is used that can only be accessed using a REX prefix, we
1508 ;;; need only to test R and B, because X is only used for the index
1509 ;;; register of an effective address and therefore never byte-sized.
1510 ;;; For R we can avoid to calculate the size of the TN because it is
1511 ;;; always OPERAND-SIZE. The size of B must be calculated here because
1512 ;;; B can be address-sized (if it is the base register of an effective
1513 ;;; address), of OPERAND-SIZE (if the instruction operates on two
1514 ;;; registers) or of some different size (in the instructions that
1515 ;;; combine arguments of different sizes: MOVZX, MOVSX, MOVSXD and
1516 ;;; several SSE instructions, e.g. CVTSD2SI). We don't distinguish
1517 ;;; between general-purpose and floating point registers for this cause
1518 ;;; because only general-purpose registers can be byte-sized at all.
1519 (defun maybe-emit-rex-prefix (segment operand-size r x b)
1520 (declare (type (member nil :byte :word :dword :qword :do-not-set)
1522 (type (or null tn) r x b))
1524 (if (and r (> (tn-offset r)
1525 ;; offset of r8 is 16, offset of xmm8 is 8
1526 (if (eq (sb-name (sc-sb (tn-sc r)))
1533 ;; Assuming R is a TN describing a general-purpose
1534 ;; register, return true if it references register
1536 (<= 8 (tn-offset r) 15)))
1537 (let ((rex-w (if (eq operand-size :qword) 1 0))
1541 (when (or (not (zerop (logior rex-w rex-r rex-x rex-b)))
1543 (eq operand-size :byte)
1546 (eq (operand-size b) :byte)
1548 (emit-rex-byte segment #b0100 rex-w rex-r rex-x rex-b)))))
1550 ;;; Emit a REX prefix if necessary. The operand size is determined from
1551 ;;; THING or can be overwritten by OPERAND-SIZE. This and REG are always
1552 ;;; passed to MAYBE-EMIT-REX-PREFIX. Additionally, if THING is an EA we
1553 ;;; pass its index and base registers, if it is a register TN, we pass
1555 ;;; In contrast to EMIT-EA above, neither stack TNs nor fixups need to
1556 ;;; be treated specially here: If THING is a stack TN, neither it nor
1557 ;;; any of its components are passed to MAYBE-EMIT-REX-PREFIX which
1558 ;;; works correctly because stack references always use RBP as the base
1559 ;;; register and never use an index register so no extended registers
1560 ;;; need to be accessed. Fixups are assembled using an addressing mode
1561 ;;; of displacement-only or RIP-plus-displacement (see EMIT-EA), so may
1562 ;;; not reference an extended register. The displacement-only addressing
1563 ;;; mode requires that REX.X is 0, which is ensured here.
1564 (defun maybe-emit-rex-for-ea (segment thing reg &key operand-size)
1565 (declare (type (or ea tn fixup) thing)
1566 (type (or null tn) reg)
1567 (type (member nil :byte :word :dword :qword :do-not-set)
1569 (let ((ea-p (ea-p thing)))
1570 (maybe-emit-rex-prefix segment
1571 (or operand-size (operand-size thing))
1573 (and ea-p (ea-index thing))
1574 (cond (ea-p (ea-base thing))
1576 (member (sb-name (sc-sb (tn-sc thing)))
1577 '(float-registers registers)))
1581 (defun operand-size (thing)
1584 ;; FIXME: might as well be COND instead of having to use #. readmacro
1585 ;; to hack up the code
1586 (case (sc-name (tn-sc thing))
1598 ;; added by jrd: float-registers is a separate size (?)
1599 ;; The only place in the code where we are called with THING
1600 ;; being a float-register is in MAYBE-EMIT-REX-PREFIX when it
1601 ;; checks whether THING is a byte register. Thus our result in
1602 ;; these cases could as well be :dword and :qword. I leave it as
1603 ;; :float and :double which is more likely to trigger an aver
1604 ;; instead of silently doing the wrong thing in case this
1605 ;; situation should change. Lutz Euler, 2005-10-23.
1608 (#.*double-sc-names*
1610 (#.*complex-sc-names*
1613 (error "can't tell the size of ~S ~S" thing (sc-name (tn-sc thing))))))
1617 ;; GNA. Guess who spelt "flavor" correctly first time round?
1618 ;; There's a strong argument in my mind to change all uses of
1619 ;; "flavor" to "kind": and similarly with some misguided uses of
1620 ;; "type" here and there. -- CSR, 2005-01-06.
1621 (case (fixup-flavor thing)
1622 ((:foreign-dataref) :qword)))
1626 (defun matching-operand-size (dst src)
1627 (let ((dst-size (operand-size dst))
1628 (src-size (operand-size src)))
1631 (if (eq dst-size src-size)
1633 (error "size mismatch: ~S is a ~S and ~S is a ~S."
1634 dst dst-size src src-size))
1638 (error "can't tell the size of either ~S or ~S" dst src)))))
1640 ;;; Except in a very few cases (MOV instructions A1, A3 and B8 - BF)
1641 ;;; we expect dword data bytes even when 64 bit work is being done.
1642 ;;; But A1 and A3 are currently unused and B8 - BF use EMIT-QWORD
1643 ;;; directly, so we emit all quad constants as dwords, additionally
1644 ;;; making sure that they survive the sign-extension to 64 bits
1646 (defun emit-sized-immediate (segment size value)
1649 (emit-byte segment value))
1651 (emit-word segment value))
1653 (emit-dword segment value))
1655 (emit-signed-dword segment value))))
1659 (define-instruction rex (segment)
1660 (:printer rex () nil :print-name nil)
1662 (bug "REX prefix used as a standalone instruction")))
1664 (define-instruction x66 (segment)
1665 (:printer x66 () nil :print-name nil)
1667 (bug "#X66 prefix used as a standalone instruction")))
1669 (defun emit-prefix (segment name)
1670 (declare (ignorable segment))
1675 (emit-byte segment #xf0))))
1677 (define-instruction lock (segment)
1678 (:printer byte ((op #b11110000)) nil)
1680 (bug "LOCK prefix used as a standalone instruction")))
1682 (define-instruction rep (segment)
1684 (emit-byte segment #b11110011)))
1686 (define-instruction repe (segment)
1687 (:printer byte ((op #b11110011)) nil)
1689 (emit-byte segment #b11110011)))
1691 (define-instruction repne (segment)
1692 (:printer byte ((op #b11110010)) nil)
1694 (emit-byte segment #b11110010)))
1696 ;;;; general data transfer
1698 ;;; This is the part of the MOV instruction emitter that does moving
1699 ;;; of an immediate value into a qword register. We go to some length
1700 ;;; to achieve the shortest possible encoding.
1701 (defun emit-immediate-move-to-qword-register (segment dst src)
1702 (declare (type integer src))
1703 (cond ((typep src '(unsigned-byte 32))
1704 ;; We use the B8 - BF encoding with an operand size of 32 bits
1705 ;; here and let the implicit zero-extension fill the upper half
1706 ;; of the 64-bit destination register. Instruction size: five
1707 ;; or six bytes. (A REX prefix will be emitted only if the
1708 ;; destination is an extended register.)
1709 (maybe-emit-rex-prefix segment :dword nil nil dst)
1710 (emit-byte-with-reg segment #b10111 (reg-tn-encoding dst))
1711 (emit-dword segment src))
1713 (maybe-emit-rex-prefix segment :qword nil nil dst)
1714 (cond ((typep src '(signed-byte 32))
1715 ;; Use the C7 encoding that takes a 32-bit immediate and
1716 ;; sign-extends it to 64 bits. Instruction size: seven
1718 (emit-byte segment #b11000111)
1719 (emit-mod-reg-r/m-byte segment #b11 #b000
1720 (reg-tn-encoding dst))
1721 (emit-signed-dword segment src))
1722 ((<= (- (expt 2 64) (expt 2 31))
1725 ;; This triggers on positive integers of 64 bits length
1726 ;; with the most significant 33 bits being 1. We use the
1727 ;; same encoding as in the previous clause.
1728 (emit-byte segment #b11000111)
1729 (emit-mod-reg-r/m-byte segment #b11 #b000
1730 (reg-tn-encoding dst))
1731 (emit-signed-dword segment (- src (expt 2 64))))
1733 ;; We need a full 64-bit immediate. Instruction size:
1735 (emit-byte-with-reg segment #b10111 (reg-tn-encoding dst))
1736 (emit-qword segment src))))))
1738 (define-instruction mov (segment dst src)
1739 ;; immediate to register
1740 (:printer reg ((op #b1011) (imm nil :type 'signed-imm-data))
1741 '(:name :tab reg ", " imm))
1742 (:printer rex-reg ((op #b1011) (imm nil :type 'signed-imm-data-upto-qword))
1743 '(:name :tab reg ", " imm))
1744 ;; absolute mem to/from accumulator
1745 (:printer simple-dir ((op #b101000) (imm nil :type 'imm-addr))
1746 `(:name :tab ,(swap-if 'dir 'accum ", " '("[" imm "]"))))
1747 ;; register to/from register/memory
1748 (:printer reg-reg/mem-dir ((op #b100010)))
1749 ;; immediate to register/memory
1750 (:printer reg/mem-imm ((op '(#b1100011 #b000))))
1753 (let ((size (matching-operand-size dst src)))
1754 (maybe-emit-operand-size-prefix segment size)
1755 (cond ((register-p dst)
1756 (cond ((integerp src)
1757 (cond ((eq size :qword)
1758 (emit-immediate-move-to-qword-register segment
1761 (maybe-emit-rex-prefix segment size nil nil dst)
1762 (emit-byte-with-reg segment
1766 (reg-tn-encoding dst))
1767 (emit-sized-immediate segment size src))))
1769 (maybe-emit-rex-for-ea segment src dst)
1774 (emit-ea segment src (reg-tn-encoding dst) :allow-constants t))))
1776 ;; C7 only deals with 32 bit immediates even if the
1777 ;; destination is a 64-bit location. The value is
1778 ;; sign-extended in this case.
1779 (maybe-emit-rex-for-ea segment dst nil)
1780 (emit-byte segment (if (eq size :byte) #b11000110 #b11000111))
1781 (emit-ea segment dst #b000)
1782 (emit-sized-immediate segment size src))
1784 (maybe-emit-rex-for-ea segment dst src)
1785 (emit-byte segment (if (eq size :byte) #b10001000 #b10001001))
1786 (emit-ea segment dst (reg-tn-encoding src)))
1788 ;; Generally we can't MOV a fixupped value into an EA, since
1789 ;; MOV on non-registers can only take a 32-bit immediate arg.
1790 ;; Make an exception for :FOREIGN fixups (pretty much just
1791 ;; the runtime asm, since other foreign calls go through the
1792 ;; the linkage table) and for linkage table references, since
1793 ;; these should always end up in low memory.
1794 (aver (or (eq (fixup-flavor src) :foreign)
1795 (eq (fixup-flavor src) :foreign-dataref)
1796 (eq (ea-size dst) :dword)))
1797 (maybe-emit-rex-for-ea segment dst nil)
1798 (emit-byte segment #b11000111)
1799 (emit-ea segment dst #b000)
1800 (emit-absolute-fixup segment src))
1802 (error "bogus arguments to MOV: ~S ~S" dst src))))))
1804 ;;; Emit a sign-extending (if SIGNED-P is true) or zero-extending move.
1805 ;;; To achieve the shortest possible encoding zero extensions into a
1806 ;;; 64-bit destination are assembled as a straight 32-bit MOV (if the
1807 ;;; source size is 32 bits) or as MOVZX with a 32-bit destination (if
1808 ;;; the source size is 8 or 16 bits). Due to the implicit zero extension
1809 ;;; to 64 bits this has the same effect as a MOVZX with 64-bit
1810 ;;; destination but often needs no REX prefix.
1811 (defun emit-move-with-extension (segment dst src signed-p)
1812 (aver (register-p dst))
1813 (let ((dst-size (operand-size dst))
1814 (src-size (operand-size src))
1815 (opcode (if signed-p #b10111110 #b10110110)))
1816 (macrolet ((emitter (operand-size &rest bytes)
1818 (maybe-emit-rex-for-ea segment src dst
1819 :operand-size ,operand-size)
1820 ,@(mapcar (lambda (byte)
1821 `(emit-byte segment ,byte))
1823 (emit-ea segment src (reg-tn-encoding dst)))))
1826 (aver (eq src-size :byte))
1827 (maybe-emit-operand-size-prefix segment :word)
1828 (emitter :word #b00001111 opcode))
1831 (setf dst-size :dword))
1834 (emitter dst-size #b00001111 opcode))
1836 (emitter dst-size #b00001111 (logior opcode 1)))
1838 (aver (or (not signed-p) (eq dst-size :qword)))
1840 (if signed-p #x63 #x8b))))))))) ; movsxd or straight mov
1842 (define-instruction movsx (segment dst src)
1843 (:printer ext-reg-reg/mem-no-width
1844 ((op #b10111110) (reg/mem nil :type 'sized-byte-reg/mem)))
1845 (:printer ext-reg-reg/mem-no-width
1846 ((op #b10111111) (reg/mem nil :type 'sized-word-reg/mem)))
1847 (:emitter (emit-move-with-extension segment dst src :signed)))
1849 (define-instruction movzx (segment dst src)
1850 (:printer ext-reg-reg/mem-no-width
1851 ((op #b10110110) (reg/mem nil :type 'sized-byte-reg/mem)))
1852 (:printer ext-reg-reg/mem-no-width
1853 ((op #b10110111) (reg/mem nil :type 'sized-word-reg/mem)))
1854 (:emitter (emit-move-with-extension segment dst src nil)))
1856 ;;; The regular use of MOVSXD is with an operand size of :qword. This
1857 ;;; sign-extends the dword source into the qword destination register.
1858 ;;; If the operand size is :dword the instruction zero-extends the dword
1859 ;;; source into the qword destination register, i.e. it does the same as
1860 ;;; a dword MOV into a register.
1861 (define-instruction movsxd (segment dst src)
1862 (:printer reg-reg/mem ((op #b0110001) (width 1)
1863 (reg/mem nil :type 'sized-dword-reg/mem)))
1864 (:emitter (emit-move-with-extension segment dst src :signed)))
1866 ;;; this is not a real amd64 instruction, of course
1867 (define-instruction movzxd (segment dst src)
1868 ; (:printer reg-reg/mem ((op #x63) (reg nil :type 'reg)))
1869 (:emitter (emit-move-with-extension segment dst src nil)))
1871 (define-instruction push (segment src)
1873 (:printer reg-no-width-default-qword ((op #b01010)))
1875 (:printer reg/mem-default-qword ((op '(#b11111111 #b110))))
1877 (:printer byte ((op #b01101010) (imm nil :type 'signed-imm-byte))
1879 (:printer byte ((op #b01101000)
1880 (imm nil :type 'signed-imm-data-default-qword))
1882 ;; ### segment registers?
1885 (cond ((integerp src)
1886 (cond ((<= -128 src 127)
1887 (emit-byte segment #b01101010)
1888 (emit-byte segment src))
1890 ;; A REX-prefix is not needed because the operand size
1891 ;; defaults to 64 bits. The size of the immediate is 32
1892 ;; bits and it is sign-extended.
1893 (emit-byte segment #b01101000)
1894 (emit-signed-dword segment src))))
1896 (let ((size (operand-size src)))
1897 (aver (or (eq size :qword) (eq size :word)))
1898 (maybe-emit-operand-size-prefix segment size)
1899 (maybe-emit-rex-for-ea segment src nil :operand-size :do-not-set)
1900 (cond ((register-p src)
1901 (emit-byte-with-reg segment #b01010 (reg-tn-encoding src)))
1903 (emit-byte segment #b11111111)
1904 (emit-ea segment src #b110 :allow-constants t))))))))
1906 (define-instruction pop (segment dst)
1907 (:printer reg-no-width-default-qword ((op #b01011)))
1908 (:printer reg/mem-default-qword ((op '(#b10001111 #b000))))
1910 (let ((size (operand-size dst)))
1911 (aver (or (eq size :qword) (eq size :word)))
1912 (maybe-emit-operand-size-prefix segment size)
1913 (maybe-emit-rex-for-ea segment dst nil :operand-size :do-not-set)
1914 (cond ((register-p dst)
1915 (emit-byte-with-reg segment #b01011 (reg-tn-encoding dst)))
1917 (emit-byte segment #b10001111)
1918 (emit-ea segment dst #b000))))))
1920 (define-instruction xchg (segment operand1 operand2)
1921 ;; Register with accumulator.
1922 (:printer reg-no-width ((op #b10010)) '(:name :tab accum ", " reg))
1923 ;; Register/Memory with Register.
1924 (:printer reg-reg/mem ((op #b1000011)))
1926 (let ((size (matching-operand-size operand1 operand2)))
1927 (maybe-emit-operand-size-prefix segment size)
1928 (labels ((xchg-acc-with-something (acc something)
1929 (if (and (not (eq size :byte)) (register-p something))
1931 (maybe-emit-rex-for-ea segment acc something)
1932 (emit-byte-with-reg segment
1934 (reg-tn-encoding something)))
1935 (xchg-reg-with-something acc something)))
1936 (xchg-reg-with-something (reg something)
1937 (maybe-emit-rex-for-ea segment something reg)
1938 (emit-byte segment (if (eq size :byte) #b10000110 #b10000111))
1939 (emit-ea segment something (reg-tn-encoding reg))))
1940 (cond ((accumulator-p operand1)
1941 (xchg-acc-with-something operand1 operand2))
1942 ((accumulator-p operand2)
1943 (xchg-acc-with-something operand2 operand1))
1944 ((register-p operand1)
1945 (xchg-reg-with-something operand1 operand2))
1946 ((register-p operand2)
1947 (xchg-reg-with-something operand2 operand1))
1949 (error "bogus args to XCHG: ~S ~S" operand1 operand2)))))))
1951 (define-instruction lea (segment dst src)
1952 (:printer reg-reg/mem ((op #b1000110) (width 1)))
1954 (aver (or (dword-reg-p dst) (qword-reg-p dst)))
1955 (maybe-emit-rex-for-ea segment src dst
1956 :operand-size (if (dword-reg-p dst) :dword :qword))
1957 (emit-byte segment #b10001101)
1958 (emit-ea segment src (reg-tn-encoding dst))))
1960 (define-instruction cmpxchg (segment dst src &optional prefix)
1961 ;; Register/Memory with Register.
1962 (:printer ext-reg-reg/mem ((op #b1011000)) '(:name :tab reg/mem ", " reg))
1964 (aver (register-p src))
1965 (emit-prefix segment prefix)
1966 (let ((size (matching-operand-size src dst)))
1967 (maybe-emit-operand-size-prefix segment size)
1968 (maybe-emit-rex-for-ea segment dst src)
1969 (emit-byte segment #b00001111)
1970 (emit-byte segment (if (eq size :byte) #b10110000 #b10110001))
1971 (emit-ea segment dst (reg-tn-encoding src)))))
1974 ;;;; flag control instructions
1976 ;;; CLC -- Clear Carry Flag.
1977 (define-instruction clc (segment)
1978 (:printer byte ((op #b11111000)))
1980 (emit-byte segment #b11111000)))
1982 ;;; CLD -- Clear Direction Flag.
1983 (define-instruction cld (segment)
1984 (:printer byte ((op #b11111100)))
1986 (emit-byte segment #b11111100)))
1988 ;;; CLI -- Clear Iterrupt Enable Flag.
1989 (define-instruction cli (segment)
1990 (:printer byte ((op #b11111010)))
1992 (emit-byte segment #b11111010)))
1994 ;;; CMC -- Complement Carry Flag.
1995 (define-instruction cmc (segment)
1996 (:printer byte ((op #b11110101)))
1998 (emit-byte segment #b11110101)))
2000 ;;; LAHF -- Load AH into flags.
2001 (define-instruction lahf (segment)
2002 (:printer byte ((op #b10011111)))
2004 (emit-byte segment #b10011111)))
2006 ;;; POPF -- Pop flags.
2007 (define-instruction popf (segment)
2008 (:printer byte ((op #b10011101)))
2010 (emit-byte segment #b10011101)))
2012 ;;; PUSHF -- push flags.
2013 (define-instruction pushf (segment)
2014 (:printer byte ((op #b10011100)))
2016 (emit-byte segment #b10011100)))
2018 ;;; SAHF -- Store AH into flags.
2019 (define-instruction sahf (segment)
2020 (:printer byte ((op #b10011110)))
2022 (emit-byte segment #b10011110)))
2024 ;;; STC -- Set Carry Flag.
2025 (define-instruction stc (segment)
2026 (:printer byte ((op #b11111001)))
2028 (emit-byte segment #b11111001)))
2030 ;;; STD -- Set Direction Flag.
2031 (define-instruction std (segment)
2032 (:printer byte ((op #b11111101)))
2034 (emit-byte segment #b11111101)))
2036 ;;; STI -- Set Interrupt Enable Flag.
2037 (define-instruction sti (segment)
2038 (:printer byte ((op #b11111011)))
2040 (emit-byte segment #b11111011)))
2044 (defun emit-random-arith-inst (name segment dst src opcode
2045 &optional allow-constants)
2046 (let ((size (matching-operand-size dst src)))
2047 (maybe-emit-operand-size-prefix segment size)
2050 (cond ((and (not (eq size :byte)) (<= -128 src 127))
2051 (maybe-emit-rex-for-ea segment dst nil)
2052 (emit-byte segment #b10000011)
2053 (emit-ea segment dst opcode :allow-constants allow-constants)
2054 (emit-byte segment src))
2055 ((accumulator-p dst)
2056 (maybe-emit-rex-for-ea segment dst nil)
2063 (emit-sized-immediate segment size src))
2065 (maybe-emit-rex-for-ea segment dst nil)
2066 (emit-byte segment (if (eq size :byte) #b10000000 #b10000001))
2067 (emit-ea segment dst opcode :allow-constants allow-constants)
2068 (emit-sized-immediate segment size src))))
2070 (maybe-emit-rex-for-ea segment dst src)
2074 (if (eq size :byte) #b00000000 #b00000001)))
2075 (emit-ea segment dst (reg-tn-encoding src) :allow-constants allow-constants))
2077 (maybe-emit-rex-for-ea segment src dst)
2081 (if (eq size :byte) #b00000010 #b00000011)))
2082 (emit-ea segment src (reg-tn-encoding dst) :allow-constants allow-constants))
2084 (error "bogus operands to ~A" name)))))
2086 (eval-when (:compile-toplevel :execute)
2087 (defun arith-inst-printer-list (subop)
2088 `((accum-imm ((op ,(dpb subop (byte 3 2) #b0000010))))
2089 (reg/mem-imm ((op (#b1000000 ,subop))))
2090 ;; The redundant encoding #x82 is invalid in 64-bit mode,
2091 ;; therefore we force WIDTH to 1.
2092 (reg/mem-imm ((op (#b1000001 ,subop)) (width 1)
2093 (imm nil :type signed-imm-byte)))
2094 (reg-reg/mem-dir ((op ,(dpb subop (byte 3 1) #b000000)))))))
2096 (define-instruction add (segment dst src &optional prefix)
2097 (:printer-list (arith-inst-printer-list #b000))
2099 (emit-prefix segment prefix)
2100 (emit-random-arith-inst "ADD" segment dst src #b000)))
2102 (define-instruction adc (segment dst src)
2103 (:printer-list (arith-inst-printer-list #b010))
2104 (:emitter (emit-random-arith-inst "ADC" segment dst src #b010)))
2106 (define-instruction sub (segment dst src)
2107 (:printer-list (arith-inst-printer-list #b101))
2108 (:emitter (emit-random-arith-inst "SUB" segment dst src #b101)))
2110 (define-instruction sbb (segment dst src)
2111 (:printer-list (arith-inst-printer-list #b011))
2112 (:emitter (emit-random-arith-inst "SBB" segment dst src #b011)))
2114 (define-instruction cmp (segment dst src)
2115 (:printer-list (arith-inst-printer-list #b111))
2116 (:emitter (emit-random-arith-inst "CMP" segment dst src #b111 t)))
2118 ;;; The one-byte encodings for INC and DEC are used as REX prefixes
2119 ;;; in 64-bit mode so we always use the two-byte form.
2120 (define-instruction inc (segment dst)
2121 (:printer reg/mem ((op '(#b1111111 #b000))))
2123 (let ((size (operand-size dst)))
2124 (maybe-emit-operand-size-prefix segment size)
2125 (maybe-emit-rex-for-ea segment dst nil)
2126 (emit-byte segment (if (eq size :byte) #b11111110 #b11111111))
2127 (emit-ea segment dst #b000))))
2129 (define-instruction dec (segment dst)
2130 (:printer reg/mem ((op '(#b1111111 #b001))))
2132 (let ((size (operand-size dst)))
2133 (maybe-emit-operand-size-prefix segment size)
2134 (maybe-emit-rex-for-ea segment dst nil)
2135 (emit-byte segment (if (eq size :byte) #b11111110 #b11111111))
2136 (emit-ea segment dst #b001))))
2138 (define-instruction neg (segment dst)
2139 (:printer reg/mem ((op '(#b1111011 #b011))))
2141 (let ((size (operand-size dst)))
2142 (maybe-emit-operand-size-prefix segment size)
2143 (maybe-emit-rex-for-ea segment dst nil)
2144 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2145 (emit-ea segment dst #b011))))
2147 (define-instruction mul (segment dst src)
2148 (:printer accum-reg/mem ((op '(#b1111011 #b100))))
2150 (let ((size (matching-operand-size dst src)))
2151 (aver (accumulator-p dst))
2152 (maybe-emit-operand-size-prefix segment size)
2153 (maybe-emit-rex-for-ea segment src nil)
2154 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2155 (emit-ea segment src #b100))))
2157 (define-instruction imul (segment dst &optional src1 src2)
2158 (:printer accum-reg/mem ((op '(#b1111011 #b101))))
2159 (:printer ext-reg-reg/mem-no-width ((op #b10101111)))
2160 (:printer reg-reg/mem ((op #b0110100) (width 1)
2161 (imm nil :type 'signed-imm-data))
2162 '(:name :tab reg ", " reg/mem ", " imm))
2163 (:printer reg-reg/mem ((op #b0110101) (width 1)
2164 (imm nil :type 'signed-imm-byte))
2165 '(:name :tab reg ", " reg/mem ", " imm))
2167 (flet ((r/m-with-immed-to-reg (reg r/m immed)
2168 (let* ((size (matching-operand-size reg r/m))
2169 (sx (and (not (eq size :byte)) (<= -128 immed 127))))
2170 (maybe-emit-operand-size-prefix segment size)
2171 (maybe-emit-rex-for-ea segment r/m reg)
2172 (emit-byte segment (if sx #b01101011 #b01101001))
2173 (emit-ea segment r/m (reg-tn-encoding reg))
2175 (emit-byte segment immed)
2176 (emit-sized-immediate segment size immed)))))
2178 (r/m-with-immed-to-reg dst src1 src2))
2181 (r/m-with-immed-to-reg dst dst src1)
2182 (let ((size (matching-operand-size dst src1)))
2183 (maybe-emit-operand-size-prefix segment size)
2184 (maybe-emit-rex-for-ea segment src1 dst)
2185 (emit-byte segment #b00001111)
2186 (emit-byte segment #b10101111)
2187 (emit-ea segment src1 (reg-tn-encoding dst)))))
2189 (let ((size (operand-size dst)))
2190 (maybe-emit-operand-size-prefix segment size)
2191 (maybe-emit-rex-for-ea segment dst nil)
2192 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2193 (emit-ea segment dst #b101)))))))
2195 (define-instruction div (segment dst src)
2196 (:printer accum-reg/mem ((op '(#b1111011 #b110))))
2198 (let ((size (matching-operand-size dst src)))
2199 (aver (accumulator-p dst))
2200 (maybe-emit-operand-size-prefix segment size)
2201 (maybe-emit-rex-for-ea segment src nil)
2202 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2203 (emit-ea segment src #b110))))
2205 (define-instruction idiv (segment dst src)
2206 (:printer accum-reg/mem ((op '(#b1111011 #b111))))
2208 (let ((size (matching-operand-size dst src)))
2209 (aver (accumulator-p dst))
2210 (maybe-emit-operand-size-prefix segment size)
2211 (maybe-emit-rex-for-ea segment src nil)
2212 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2213 (emit-ea segment src #b111))))
2215 (define-instruction bswap (segment dst)
2216 (:printer ext-reg-no-width ((op #b11001)))
2218 (let ((size (operand-size dst)))
2219 (maybe-emit-rex-prefix segment size nil nil dst)
2220 (emit-byte segment #x0f)
2221 (emit-byte-with-reg segment #b11001 (reg-tn-encoding dst)))))
2223 ;;; CBW -- Convert Byte to Word. AX <- sign_xtnd(AL)
2224 (define-instruction cbw (segment)
2225 (:printer x66-byte ((op #b10011000)))
2227 (maybe-emit-operand-size-prefix segment :word)
2228 (emit-byte segment #b10011000)))
2230 ;;; CWDE -- Convert Word To Double Word Extended. EAX <- sign_xtnd(AX)
2231 (define-instruction cwde (segment)
2232 (:printer byte ((op #b10011000)))
2234 (maybe-emit-operand-size-prefix segment :dword)
2235 (emit-byte segment #b10011000)))
2237 ;;; CDQE -- Convert Double Word To Quad Word Extended. RAX <- sign_xtnd(EAX)
2238 (define-instruction cdqe (segment)
2239 (:printer rex-byte ((op #b10011000)))
2241 (maybe-emit-rex-prefix segment :qword nil nil nil)
2242 (emit-byte segment #b10011000)))
2244 ;;; CWD -- Convert Word to Double Word. DX:AX <- sign_xtnd(AX)
2245 (define-instruction cwd (segment)
2246 (:printer x66-byte ((op #b10011001)))
2248 (maybe-emit-operand-size-prefix segment :word)
2249 (emit-byte segment #b10011001)))
2251 ;;; CDQ -- Convert Double Word to Quad Word. EDX:EAX <- sign_xtnd(EAX)
2252 (define-instruction cdq (segment)
2253 (:printer byte ((op #b10011001)))
2255 (maybe-emit-operand-size-prefix segment :dword)
2256 (emit-byte segment #b10011001)))
2258 ;;; CQO -- Convert Quad Word to Octaword. RDX:RAX <- sign_xtnd(RAX)
2259 (define-instruction cqo (segment)
2260 (:printer rex-byte ((op #b10011001)))
2262 (maybe-emit-rex-prefix segment :qword nil nil nil)
2263 (emit-byte segment #b10011001)))
2265 (define-instruction xadd (segment dst src &optional prefix)
2266 ;; Register/Memory with Register.
2267 (:printer ext-reg-reg/mem ((op #b1100000)) '(:name :tab reg/mem ", " reg))
2269 (aver (register-p src))
2270 (emit-prefix segment prefix)
2271 (let ((size (matching-operand-size src dst)))
2272 (maybe-emit-operand-size-prefix segment size)
2273 (maybe-emit-rex-for-ea segment dst src)
2274 (emit-byte segment #b00001111)
2275 (emit-byte segment (if (eq size :byte) #b11000000 #b11000001))
2276 (emit-ea segment dst (reg-tn-encoding src)))))
2281 (defun emit-shift-inst (segment dst amount opcode)
2282 (let ((size (operand-size dst)))
2283 (maybe-emit-operand-size-prefix segment size)
2284 (multiple-value-bind (major-opcode immed)
2286 (:cl (values #b11010010 nil))
2287 (1 (values #b11010000 nil))
2288 (t (values #b11000000 t)))
2289 (maybe-emit-rex-for-ea segment dst nil)
2291 (if (eq size :byte) major-opcode (logior major-opcode 1)))
2292 (emit-ea segment dst opcode)
2294 (emit-byte segment amount)))))
2296 (eval-when (:compile-toplevel :execute)
2297 (defun shift-inst-printer-list (subop)
2298 `((reg/mem ((op (#b1101000 ,subop)))
2299 (:name :tab reg/mem ", 1"))
2300 (reg/mem ((op (#b1101001 ,subop)))
2301 (:name :tab reg/mem ", " 'cl))
2302 (reg/mem-imm ((op (#b1100000 ,subop))
2303 (imm nil :type imm-byte))))))
2305 (define-instruction rol (segment dst amount)
2307 (shift-inst-printer-list #b000))
2309 (emit-shift-inst segment dst amount #b000)))
2311 (define-instruction ror (segment dst amount)
2313 (shift-inst-printer-list #b001))
2315 (emit-shift-inst segment dst amount #b001)))
2317 (define-instruction rcl (segment dst amount)
2319 (shift-inst-printer-list #b010))
2321 (emit-shift-inst segment dst amount #b010)))
2323 (define-instruction rcr (segment dst amount)
2325 (shift-inst-printer-list #b011))
2327 (emit-shift-inst segment dst amount #b011)))
2329 (define-instruction shl (segment dst amount)
2331 (shift-inst-printer-list #b100))
2333 (emit-shift-inst segment dst amount #b100)))
2335 (define-instruction shr (segment dst amount)
2337 (shift-inst-printer-list #b101))
2339 (emit-shift-inst segment dst amount #b101)))
2341 (define-instruction sar (segment dst amount)
2343 (shift-inst-printer-list #b111))
2345 (emit-shift-inst segment dst amount #b111)))
2347 (defun emit-double-shift (segment opcode dst src amt)
2348 (let ((size (matching-operand-size dst src)))
2349 (when (eq size :byte)
2350 (error "Double shifts can only be used with words."))
2351 (maybe-emit-operand-size-prefix segment size)
2352 (maybe-emit-rex-for-ea segment dst src)
2353 (emit-byte segment #b00001111)
2354 (emit-byte segment (dpb opcode (byte 1 3)
2355 (if (eq amt :cl) #b10100101 #b10100100)))
2356 (emit-ea segment dst (reg-tn-encoding src))
2357 (unless (eq amt :cl)
2358 (emit-byte segment amt))))
2360 (eval-when (:compile-toplevel :execute)
2361 (defun double-shift-inst-printer-list (op)
2362 `((ext-reg-reg/mem-no-width ((op ,(logior op #b100))
2363 (imm nil :type imm-byte))
2364 (:name :tab reg/mem ", " reg ", " imm))
2365 (ext-reg-reg/mem-no-width ((op ,(logior op #b101)))
2366 (:name :tab reg/mem ", " reg ", " 'cl)))))
2368 (define-instruction shld (segment dst src amt)
2369 (:declare (type (or (member :cl) (mod 64)) amt))
2370 (:printer-list (double-shift-inst-printer-list #b10100000))
2372 (emit-double-shift segment #b0 dst src amt)))
2374 (define-instruction shrd (segment dst src amt)
2375 (:declare (type (or (member :cl) (mod 64)) amt))
2376 (:printer-list (double-shift-inst-printer-list #b10101000))
2378 (emit-double-shift segment #b1 dst src amt)))
2380 (define-instruction and (segment dst src)
2382 (arith-inst-printer-list #b100))
2384 (emit-random-arith-inst "AND" segment dst src #b100)))
2386 (define-instruction test (segment this that)
2387 (:printer accum-imm ((op #b1010100)))
2388 (:printer reg/mem-imm ((op '(#b1111011 #b000))))
2389 (:printer reg-reg/mem ((op #b1000010)))
2391 (let ((size (matching-operand-size this that)))
2392 (maybe-emit-operand-size-prefix segment size)
2393 (flet ((test-immed-and-something (immed something)
2394 (cond ((accumulator-p something)
2395 (maybe-emit-rex-for-ea segment something nil)
2397 (if (eq size :byte) #b10101000 #b10101001))
2398 (emit-sized-immediate segment size immed))
2400 (maybe-emit-rex-for-ea segment something nil)
2402 (if (eq size :byte) #b11110110 #b11110111))
2403 (emit-ea segment something #b000)
2404 (emit-sized-immediate segment size immed))))
2405 (test-reg-and-something (reg something)
2406 (maybe-emit-rex-for-ea segment something reg)
2407 (emit-byte segment (if (eq size :byte) #b10000100 #b10000101))
2408 (emit-ea segment something (reg-tn-encoding reg))))
2409 (cond ((integerp that)
2410 (test-immed-and-something that this))
2412 (test-immed-and-something this that))
2414 (test-reg-and-something this that))
2416 (test-reg-and-something that this))
2418 (error "bogus operands for TEST: ~S and ~S" this that)))))))
2420 ;;; Emit the most compact form of the test immediate instruction,
2421 ;;; using an 8 bit test when the immediate is only 8 bits and the
2422 ;;; value is one of the four low registers (rax, rbx, rcx, rdx) or the
2424 (defun emit-optimized-test-inst (x y)
2427 (let ((offset (tn-offset x)))
2428 (cond ((and (sc-is x any-reg descriptor-reg)
2429 (or (= offset rax-offset) (= offset rbx-offset)
2430 (= offset rcx-offset) (= offset rdx-offset)))
2431 (inst test (reg-in-size x :byte) y))
2432 ((sc-is x control-stack)
2433 (inst test (make-ea :byte :base rbp-tn
2434 :disp (frame-byte-offset offset))
2441 (define-instruction or (segment dst src)
2443 (arith-inst-printer-list #b001))
2445 (emit-random-arith-inst "OR" segment dst src #b001)))
2447 (define-instruction xor (segment dst src)
2449 (arith-inst-printer-list #b110))
2451 (emit-random-arith-inst "XOR" segment dst src #b110)))
2453 (define-instruction not (segment dst)
2454 (:printer reg/mem ((op '(#b1111011 #b010))))
2456 (let ((size (operand-size dst)))
2457 (maybe-emit-operand-size-prefix segment size)
2458 (maybe-emit-rex-for-ea segment dst nil)
2459 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2460 (emit-ea segment dst #b010))))
2462 ;;;; string manipulation
2464 (define-instruction cmps (segment size)
2465 (:printer string-op ((op #b1010011)))
2467 (maybe-emit-operand-size-prefix segment size)
2468 (maybe-emit-rex-prefix segment size nil nil nil)
2469 (emit-byte segment (if (eq size :byte) #b10100110 #b10100111))))
2471 (define-instruction ins (segment acc)
2472 (:printer string-op ((op #b0110110)))
2474 (let ((size (operand-size acc)))
2475 (aver (accumulator-p acc))
2476 (maybe-emit-operand-size-prefix segment size)
2477 (maybe-emit-rex-prefix segment size nil nil nil)
2478 (emit-byte segment (if (eq size :byte) #b01101100 #b01101101)))))
2480 (define-instruction lods (segment acc)
2481 (:printer string-op ((op #b1010110)))
2483 (let ((size (operand-size acc)))
2484 (aver (accumulator-p acc))
2485 (maybe-emit-operand-size-prefix segment size)
2486 (maybe-emit-rex-prefix segment size nil nil nil)
2487 (emit-byte segment (if (eq size :byte) #b10101100 #b10101101)))))
2489 (define-instruction movs (segment size)
2490 (:printer string-op ((op #b1010010)))
2492 (maybe-emit-operand-size-prefix segment size)
2493 (maybe-emit-rex-prefix segment size nil nil nil)
2494 (emit-byte segment (if (eq size :byte) #b10100100 #b10100101))))
2496 (define-instruction outs (segment acc)
2497 (:printer string-op ((op #b0110111)))
2499 (let ((size (operand-size acc)))
2500 (aver (accumulator-p acc))
2501 (maybe-emit-operand-size-prefix segment size)
2502 (maybe-emit-rex-prefix segment size nil nil nil)
2503 (emit-byte segment (if (eq size :byte) #b01101110 #b01101111)))))
2505 (define-instruction scas (segment acc)
2506 (:printer string-op ((op #b1010111)))
2508 (let ((size (operand-size acc)))
2509 (aver (accumulator-p acc))
2510 (maybe-emit-operand-size-prefix segment size)
2511 (maybe-emit-rex-prefix segment size nil nil nil)
2512 (emit-byte segment (if (eq size :byte) #b10101110 #b10101111)))))
2514 (define-instruction stos (segment acc)
2515 (:printer string-op ((op #b1010101)))
2517 (let ((size (operand-size acc)))
2518 (aver (accumulator-p acc))
2519 (maybe-emit-operand-size-prefix segment size)
2520 (maybe-emit-rex-prefix segment size nil nil nil)
2521 (emit-byte segment (if (eq size :byte) #b10101010 #b10101011)))))
2523 (define-instruction xlat (segment)
2524 (:printer byte ((op #b11010111)))
2526 (emit-byte segment #b11010111)))
2529 ;;;; bit manipulation
2531 (define-instruction bsf (segment dst src)
2532 (:printer ext-reg-reg/mem-no-width ((op #b10111100)))
2534 (let ((size (matching-operand-size dst src)))
2535 (when (eq size :byte)
2536 (error "can't scan bytes: ~S" src))
2537 (maybe-emit-operand-size-prefix segment size)
2538 (maybe-emit-rex-for-ea segment src dst)
2539 (emit-byte segment #b00001111)
2540 (emit-byte segment #b10111100)
2541 (emit-ea segment src (reg-tn-encoding dst)))))
2543 (define-instruction bsr (segment dst src)
2544 (:printer ext-reg-reg/mem-no-width ((op #b10111101)))
2546 (let ((size (matching-operand-size dst src)))
2547 (when (eq size :byte)
2548 (error "can't scan bytes: ~S" src))
2549 (maybe-emit-operand-size-prefix segment size)
2550 (maybe-emit-rex-for-ea segment src dst)
2551 (emit-byte segment #b00001111)
2552 (emit-byte segment #b10111101)
2553 (emit-ea segment src (reg-tn-encoding dst)))))
2555 (defun emit-bit-test-and-mumble (segment src index opcode)
2556 (let ((size (operand-size src)))
2557 (when (eq size :byte)
2558 (error "can't scan bytes: ~S" src))
2559 (maybe-emit-operand-size-prefix segment size)
2560 (cond ((integerp index)
2561 (maybe-emit-rex-for-ea segment src nil)
2562 (emit-byte segment #b00001111)
2563 (emit-byte segment #b10111010)
2564 (emit-ea segment src opcode)
2565 (emit-byte segment index))
2567 (maybe-emit-rex-for-ea segment src index)
2568 (emit-byte segment #b00001111)
2569 (emit-byte segment (dpb opcode (byte 3 3) #b10000011))
2570 (emit-ea segment src (reg-tn-encoding index))))))
2572 (eval-when (:compile-toplevel :execute)
2573 (defun bit-test-inst-printer-list (subop)
2574 `((ext-reg/mem-no-width+imm8 ((op (#xBA ,subop))))
2575 (ext-reg-reg/mem-no-width ((op ,(dpb subop (byte 3 3) #b10000011))
2576 (reg/mem nil :type sized-reg/mem))
2577 (:name :tab reg/mem ", " reg)))))
2579 (macrolet ((define (inst opcode-extension)
2580 `(define-instruction ,inst (segment src index)
2581 (:printer-list (bit-test-inst-printer-list ,opcode-extension))
2582 (:emitter (emit-bit-test-and-mumble segment src index
2583 ,opcode-extension)))))
2590 ;;;; control transfer
2592 (define-instruction call (segment where)
2593 (:printer near-jump ((op #b11101000)))
2594 (:printer reg/mem-default-qword ((op '(#b11111111 #b010))))
2598 (emit-byte segment #b11101000) ; 32 bit relative
2599 (emit-back-patch segment
2601 (lambda (segment posn)
2602 (emit-signed-dword segment
2603 (- (label-position where)
2606 ;; There is no CALL rel64...
2607 (error "Cannot CALL a fixup: ~S" where))
2609 (maybe-emit-rex-for-ea segment where nil :operand-size :do-not-set)
2610 (emit-byte segment #b11111111)
2611 (emit-ea segment where #b010)))))
2613 (defun emit-byte-displacement-backpatch (segment target)
2614 (emit-back-patch segment
2616 (lambda (segment posn)
2617 (let ((disp (- (label-position target) (1+ posn))))
2618 (aver (<= -128 disp 127))
2619 (emit-byte segment disp)))))
2621 (define-instruction jmp (segment cond &optional where)
2622 ;; conditional jumps
2623 (:printer short-cond-jump ((op #b0111)) '('j cc :tab label))
2624 (:printer near-cond-jump () '('j cc :tab label))
2625 ;; unconditional jumps
2626 (:printer short-jump ((op #b1011)))
2627 (:printer near-jump ((op #b11101001)))
2628 (:printer reg/mem-default-qword ((op '(#b11111111 #b100))))
2633 (lambda (segment posn delta-if-after)
2634 (let ((disp (- (label-position where posn delta-if-after)
2636 (when (<= -128 disp 127)
2638 (dpb (conditional-opcode cond)
2641 (emit-byte-displacement-backpatch segment where)
2643 (lambda (segment posn)
2644 (let ((disp (- (label-position where) (+ posn 6))))
2645 (emit-byte segment #b00001111)
2647 (dpb (conditional-opcode cond)
2650 (emit-signed-dword segment disp)))))
2651 ((label-p (setq where cond))
2654 (lambda (segment posn delta-if-after)
2655 (let ((disp (- (label-position where posn delta-if-after)
2657 (when (<= -128 disp 127)
2658 (emit-byte segment #b11101011)
2659 (emit-byte-displacement-backpatch segment where)
2661 (lambda (segment posn)
2662 (let ((disp (- (label-position where) (+ posn 5))))
2663 (emit-byte segment #b11101001)
2664 (emit-signed-dword segment disp)))))
2666 (emit-byte segment #b11101001)
2667 (emit-relative-fixup segment where))
2669 (unless (or (ea-p where) (tn-p where))
2670 (error "don't know what to do with ~A" where))
2671 ;; near jump defaults to 64 bit
2672 ;; w-bit in rex prefix is unnecessary
2673 (maybe-emit-rex-for-ea segment where nil :operand-size :do-not-set)
2674 (emit-byte segment #b11111111)
2675 (emit-ea segment where #b100)))))
2677 (define-instruction ret (segment &optional stack-delta)
2678 (:printer byte ((op #b11000011)))
2679 (:printer byte ((op #b11000010) (imm nil :type 'imm-word-16))
2682 (cond ((and stack-delta (not (zerop stack-delta)))
2683 (emit-byte segment #b11000010)
2684 (emit-word segment stack-delta))
2686 (emit-byte segment #b11000011)))))
2688 (define-instruction jrcxz (segment target)
2689 (:printer short-jump ((op #b0011)))
2691 (emit-byte segment #b11100011)
2692 (emit-byte-displacement-backpatch segment target)))
2694 (define-instruction loop (segment target)
2695 (:printer short-jump ((op #b0010)))
2697 (emit-byte segment #b11100010) ; pfw this was 11100011, or jecxz!!!!
2698 (emit-byte-displacement-backpatch segment target)))
2700 (define-instruction loopz (segment target)
2701 (:printer short-jump ((op #b0001)))
2703 (emit-byte segment #b11100001)
2704 (emit-byte-displacement-backpatch segment target)))
2706 (define-instruction loopnz (segment target)
2707 (:printer short-jump ((op #b0000)))
2709 (emit-byte segment #b11100000)
2710 (emit-byte-displacement-backpatch segment target)))
2712 ;;;; conditional move
2713 (define-instruction cmov (segment cond dst src)
2714 (:printer cond-move ())
2716 (aver (register-p dst))
2717 (let ((size (matching-operand-size dst src)))
2718 (aver (or (eq size :word) (eq size :dword) (eq size :qword)))
2719 (maybe-emit-operand-size-prefix segment size))
2720 (maybe-emit-rex-for-ea segment src dst)
2721 (emit-byte segment #b00001111)
2722 (emit-byte segment (dpb (conditional-opcode cond) (byte 4 0) #b01000000))
2723 (emit-ea segment src (reg-tn-encoding dst))))
2725 ;;;; conditional byte set
2727 (define-instruction set (segment dst cond)
2728 (:printer cond-set ())
2730 (maybe-emit-rex-for-ea segment dst nil)
2731 (emit-byte segment #b00001111)
2732 (emit-byte segment (dpb (conditional-opcode cond) (byte 4 0) #b10010000))
2733 (emit-ea segment dst #b000)))
2737 (define-instruction enter (segment disp &optional (level 0))
2738 (:declare (type (unsigned-byte 16) disp)
2739 (type (unsigned-byte 8) level))
2740 (:printer enter-format ((op #b11001000)))
2742 (emit-byte segment #b11001000)
2743 (emit-word segment disp)
2744 (emit-byte segment level)))
2746 (define-instruction leave (segment)
2747 (:printer byte ((op #b11001001)))
2749 (emit-byte segment #b11001001)))
2751 ;;;; interrupt instructions
2753 (defun snarf-error-junk (sap offset &optional length-only)
2754 (let* ((length (sb!sys:sap-ref-8 sap offset))
2755 (vector (make-array length :element-type '(unsigned-byte 8))))
2756 (declare (type sb!sys:system-area-pointer sap)
2757 (type (unsigned-byte 8) length)
2758 (type (simple-array (unsigned-byte 8) (*)) vector))
2760 (values 0 (1+ length) nil nil))
2762 (sb!kernel:copy-ub8-from-system-area sap (1+ offset)
2764 (collect ((sc-offsets)
2766 (lengths 1) ; the length byte
2768 (error-number (sb!c:read-var-integer vector index)))
2771 (when (>= index length)
2773 (let ((old-index index))
2774 (sc-offsets (sb!c:read-var-integer vector index))
2775 (lengths (- index old-index))))
2776 (values error-number
2782 (defmacro break-cases (breaknum &body cases)
2783 (let ((bn-temp (gensym)))
2784 (collect ((clauses))
2785 (dolist (case cases)
2786 (clauses `((= ,bn-temp ,(car case)) ,@(cdr case))))
2787 `(let ((,bn-temp ,breaknum))
2788 (cond ,@(clauses))))))
2791 (defun break-control (chunk inst stream dstate)
2792 (declare (ignore inst))
2793 (flet ((nt (x) (if stream (sb!disassem:note x dstate))))
2794 ;; XXX: {BYTE,WORD}-IMM-CODE below is a macro defined by the
2795 ;; DEFINE-INSTRUCTION-FORMAT for {BYTE,WORD}-IMM above. Due to
2796 ;; the spectacular design for DEFINE-INSTRUCTION-FORMAT (involving
2797 ;; a call to EVAL in order to define the macros at compile-time
2798 ;; only) they do not even show up as symbols in the target core.
2799 (case #!-ud2-breakpoints (byte-imm-code chunk dstate)
2800 #!+ud2-breakpoints (word-imm-code chunk dstate)
2803 (sb!disassem:handle-break-args #'snarf-error-junk stream dstate))
2806 (sb!disassem:handle-break-args #'snarf-error-junk stream dstate))
2808 (nt "breakpoint trap"))
2809 (#.pending-interrupt-trap
2810 (nt "pending interrupt trap"))
2813 (#.fun-end-breakpoint-trap
2814 (nt "function end breakpoint trap"))
2815 (#.single-step-around-trap
2816 (nt "single-step trap (around)"))
2817 (#.single-step-before-trap
2818 (nt "single-step trap (before)")))))
2820 (define-instruction break (segment code)
2821 (:declare (type (unsigned-byte 8) code))
2822 #!-ud2-breakpoints (:printer byte-imm ((op #b11001100)) '(:name :tab code)
2823 :control #'break-control)
2824 #!+ud2-breakpoints (:printer word-imm ((op #b0000101100001111)) '(:name :tab code)
2825 :control #'break-control)
2827 #!-ud2-breakpoints (emit-byte segment #b11001100)
2828 ;; On darwin, trap handling via SIGTRAP is unreliable, therefore we
2829 ;; throw a sigill with 0x0b0f instead and check for this in the
2830 ;; SIGILL handler and pass it on to the sigtrap handler if
2832 #!+ud2-breakpoints (emit-word segment #b0000101100001111)
2833 (emit-byte segment code)))
2835 (define-instruction int (segment number)
2836 (:declare (type (unsigned-byte 8) number))
2837 (:printer byte-imm ((op #b11001101)))
2841 (emit-byte segment #b11001100))
2843 (emit-byte segment #b11001101)
2844 (emit-byte segment number)))))
2846 (define-instruction iret (segment)
2847 (:printer byte ((op #b11001111)))
2849 (emit-byte segment #b11001111)))
2851 ;;;; processor control
2853 (define-instruction hlt (segment)
2854 (:printer byte ((op #b11110100)))
2856 (emit-byte segment #b11110100)))
2858 (define-instruction nop (segment)
2859 (:printer byte ((op #b10010000)))
2861 (:printer ext-reg/mem-no-width ((op '(#x1f 0))) '(:name))
2863 (emit-byte segment #b10010000)))
2865 ;;; Emit a sequence of single- or multi-byte NOPs to fill AMOUNT many
2866 ;;; bytes with the smallest possible number of such instructions.
2867 (defun emit-long-nop (segment amount)
2868 (declare (type segment segment)
2869 (type index amount))
2870 ;; Pack all instructions into one byte vector to save space.
2871 (let* ((bytes #.(coerce #(#x90
2875 #x0f #x1f #x44 #x00 #x00
2876 #x66 #x0f #x1f #x44 #x00 #x00
2877 #x0f #x1f #x80 #x00 #x00 #x00 #x00
2878 #x0f #x1f #x84 #x00 #x00 #x00 #x00 #x00
2879 #x66 #x0f #x1f #x84 #x00 #x00 #x00 #x00 #x00)
2880 '(vector (unsigned-byte 8))))
2881 (max-length (isqrt (* 2 (length bytes)))))
2883 (let* ((count (min amount max-length))
2884 (start (ash (* count (1- count)) -1)))
2886 (emit-byte segment (aref bytes (+ start i)))))
2887 (if (> amount max-length)
2888 (decf amount max-length)
2891 (define-instruction wait (segment)
2892 (:printer byte ((op #b10011011)))
2894 (emit-byte segment #b10011011)))
2897 ;;;; miscellaneous hackery
2899 (define-instruction byte (segment byte)
2901 (emit-byte segment byte)))
2903 (define-instruction word (segment word)
2905 (emit-word segment word)))
2907 (define-instruction dword (segment dword)
2909 (emit-dword segment dword)))
2911 (defun emit-header-data (segment type)
2912 (emit-back-patch segment
2914 (lambda (segment posn)
2918 (component-header-length))
2922 (define-instruction simple-fun-header-word (segment)
2924 (emit-header-data segment simple-fun-header-widetag)))
2926 (define-instruction lra-header-word (segment)
2928 (emit-header-data segment return-pc-header-widetag)))
2930 ;;;; Instructions required to do floating point operations using SSE
2932 ;; Return a one- or two-element list of printers for SSE instructions.
2933 ;; The one-element list is used in the cases where the REX prefix is
2934 ;; really a prefix and thus automatically supported, the two-element
2935 ;; list is used when the REX prefix is used in an infix position.
2936 (eval-when (:compile-toplevel :execute)
2937 (defun sse-inst-printer-list (inst-format-stem prefix opcode
2938 &key more-fields printer)
2939 (let ((fields `(,@(when prefix
2940 `((prefix ,prefix)))
2943 (inst-formats (if prefix
2944 (list (symbolicate "EXT-" inst-format-stem)
2945 (symbolicate "EXT-REX-" inst-format-stem))
2946 (list inst-format-stem))))
2947 (mapcar (lambda (inst-format)
2948 `(,inst-format ,fields ,@(when printer
2951 (defun 2byte-sse-inst-printer-list (inst-format-stem prefix op1 op2
2952 &key more-fields printer)
2953 (let ((fields `(,@(when prefix
2954 `((prefix, prefix)))
2958 (inst-formats (if prefix
2959 (list (symbolicate "EXT-" inst-format-stem)
2960 (symbolicate "EXT-REX-" inst-format-stem))
2961 (list inst-format-stem))))
2962 (mapcar (lambda (inst-format)
2963 `(,inst-format ,fields ,@(when printer
2967 (defun emit-sse-inst (segment dst src prefix opcode
2968 &key operand-size (remaining-bytes 0))
2970 (emit-byte segment prefix))
2972 (maybe-emit-rex-for-ea segment src dst :operand-size operand-size)
2973 (maybe-emit-rex-for-ea segment src dst))
2974 (emit-byte segment #x0f)
2975 (emit-byte segment opcode)
2976 (emit-ea segment src (reg-tn-encoding dst) :remaining-bytes remaining-bytes))
2978 ;; 0110 0110:0000 1111:0111 00gg: 11 010 xmmreg:imm8
2980 (defun emit-sse-inst-with-imm (segment dst/src imm
2985 (emit-byte segment prefix))
2986 (maybe-emit-rex-prefix segment operand-size dst/src nil nil)
2987 (emit-byte segment #x0F)
2988 (emit-byte segment opcode)
2989 (emit-byte segment (logior (ash (logior #b11000 /i) 3)
2990 (reg-tn-encoding dst/src)))
2991 (emit-byte segment imm))
2993 (defun emit-sse-inst-2byte (segment dst src prefix op1 op2
2994 &key operand-size (remaining-bytes 0))
2996 (emit-byte segment prefix))
2998 (maybe-emit-rex-for-ea segment src dst :operand-size operand-size)
2999 (maybe-emit-rex-for-ea segment src dst))
3000 (emit-byte segment #x0f)
3001 (emit-byte segment op1)
3002 (emit-byte segment op2)
3003 (emit-ea segment src (reg-tn-encoding dst) :remaining-bytes remaining-bytes))
3006 ((define-imm-sse-instruction (name opcode /i)
3007 `(define-instruction ,name (segment dst/src imm)
3009 ',(sse-inst-printer-list 'xmm-imm #x66 opcode
3010 :more-fields `((/i ,/i))))
3012 (emit-sse-inst-with-imm segment dst/src imm
3014 :operand-size :do-not-set)))))
3015 (define-imm-sse-instruction pslldq #x73 7)
3016 (define-imm-sse-instruction psllw-imm #x71 6)
3017 (define-imm-sse-instruction pslld-imm #x72 6)
3018 (define-imm-sse-instruction psllq-imm #x73 6)
3020 (define-imm-sse-instruction psraw-imm #x71 4)
3021 (define-imm-sse-instruction psrad-imm #x72 4)
3023 (define-imm-sse-instruction psrldq #x73 3)
3024 (define-imm-sse-instruction psrlw-imm #x71 2)
3025 (define-imm-sse-instruction psrld-imm #x72 2)
3026 (define-imm-sse-instruction psrlq-imm #x73 2))
3028 ;;; Emit an SSE instruction that has an XMM register as the destination
3029 ;;; operand and for which the size of the operands is implicitly given
3030 ;;; by the instruction.
3031 (defun emit-regular-sse-inst (segment dst src prefix opcode
3032 &key (remaining-bytes 0))
3033 (aver (xmm-register-p dst))
3034 (emit-sse-inst segment dst src prefix opcode
3035 :operand-size :do-not-set
3036 :remaining-bytes remaining-bytes))
3038 (defun emit-regular-2byte-sse-inst (segment dst src prefix op1 op2
3039 &key (remaining-bytes 0))
3040 (aver (xmm-register-p dst))
3041 (emit-sse-inst-2byte segment dst src prefix op1 op2
3042 :operand-size :do-not-set
3043 :remaining-bytes remaining-bytes))
3045 ;;; Instructions having an XMM register as the destination operand
3046 ;;; and an XMM register or a memory location as the source operand.
3047 ;;; The operand size is implicitly given by the instruction.
3049 (macrolet ((define-regular-sse-inst (name prefix opcode)
3050 `(define-instruction ,name (segment dst src)
3052 ',(sse-inst-printer-list 'xmm-xmm/mem prefix opcode))
3054 (emit-regular-sse-inst segment dst src ,prefix ,opcode)))))
3056 (define-regular-sse-inst movshdup #xf3 #x16)
3057 (define-regular-sse-inst movsldup #xf3 #x12)
3058 (define-regular-sse-inst movddup #xf2 #x12)
3060 (define-regular-sse-inst andpd #x66 #x54)
3061 (define-regular-sse-inst andps nil #x54)
3062 (define-regular-sse-inst andnpd #x66 #x55)
3063 (define-regular-sse-inst andnps nil #x55)
3064 (define-regular-sse-inst orpd #x66 #x56)
3065 (define-regular-sse-inst orps nil #x56)
3066 (define-regular-sse-inst pand #x66 #xdb)
3067 (define-regular-sse-inst pandn #x66 #xdf)
3068 (define-regular-sse-inst por #x66 #xeb)
3069 (define-regular-sse-inst pxor #x66 #xef)
3070 (define-regular-sse-inst xorpd #x66 #x57)
3071 (define-regular-sse-inst xorps nil #x57)
3073 (define-regular-sse-inst comisd #x66 #x2f)
3074 (define-regular-sse-inst comiss nil #x2f)
3075 (define-regular-sse-inst ucomisd #x66 #x2e)
3076 (define-regular-sse-inst ucomiss nil #x2e)
3077 ;; integer comparison
3078 (define-regular-sse-inst pcmpeqb #x66 #x74)
3079 (define-regular-sse-inst pcmpeqw #x66 #x75)
3080 (define-regular-sse-inst pcmpeqd #x66 #x76)
3081 (define-regular-sse-inst pcmpgtb #x66 #x64)
3082 (define-regular-sse-inst pcmpgtw #x66 #x65)
3083 (define-regular-sse-inst pcmpgtd #x66 #x66)
3085 (define-regular-sse-inst maxpd #x66 #x5f)
3086 (define-regular-sse-inst maxps nil #x5f)
3087 (define-regular-sse-inst maxsd #xf2 #x5f)
3088 (define-regular-sse-inst maxss #xf3 #x5f)
3089 (define-regular-sse-inst minpd #x66 #x5d)
3090 (define-regular-sse-inst minps nil #x5d)
3091 (define-regular-sse-inst minsd #xf2 #x5d)
3092 (define-regular-sse-inst minss #xf3 #x5d)
3094 (define-regular-sse-inst pmaxsw #x66 #xee)
3095 (define-regular-sse-inst pmaxub #x66 #xde)
3096 (define-regular-sse-inst pminsw #x66 #xea)
3097 (define-regular-sse-inst pminub #x66 #xda)
3099 (define-regular-sse-inst addpd #x66 #x58)
3100 (define-regular-sse-inst addps nil #x58)
3101 (define-regular-sse-inst addsd #xf2 #x58)
3102 (define-regular-sse-inst addss #xf3 #x58)
3103 (define-regular-sse-inst addsubpd #x66 #xd0)
3104 (define-regular-sse-inst addsubps #xf2 #xd0)
3105 (define-regular-sse-inst divpd #x66 #x5e)
3106 (define-regular-sse-inst divps nil #x5e)
3107 (define-regular-sse-inst divsd #xf2 #x5e)
3108 (define-regular-sse-inst divss #xf3 #x5e)
3109 (define-regular-sse-inst haddpd #x66 #x7c)
3110 (define-regular-sse-inst haddps #xf2 #x7c)
3111 (define-regular-sse-inst hsubpd #x66 #x7d)
3112 (define-regular-sse-inst hsubps #xf2 #x7d)
3113 (define-regular-sse-inst mulpd #x66 #x59)
3114 (define-regular-sse-inst mulps nil #x59)
3115 (define-regular-sse-inst mulsd #xf2 #x59)
3116 (define-regular-sse-inst mulss #xf3 #x59)
3117 (define-regular-sse-inst rcpps nil #x53)
3118 (define-regular-sse-inst rcpss #xf3 #x53)
3119 (define-regular-sse-inst rsqrtps nil #x52)
3120 (define-regular-sse-inst rsqrtss #xf3 #x52)
3121 (define-regular-sse-inst sqrtpd #x66 #x51)
3122 (define-regular-sse-inst sqrtps nil #x51)
3123 (define-regular-sse-inst sqrtsd #xf2 #x51)
3124 (define-regular-sse-inst sqrtss #xf3 #x51)
3125 (define-regular-sse-inst subpd #x66 #x5c)
3126 (define-regular-sse-inst subps nil #x5c)
3127 (define-regular-sse-inst subsd #xf2 #x5c)
3128 (define-regular-sse-inst subss #xf3 #x5c)
3129 (define-regular-sse-inst unpckhpd #x66 #x15)
3130 (define-regular-sse-inst unpckhps nil #x15)
3131 (define-regular-sse-inst unpcklpd #x66 #x14)
3132 (define-regular-sse-inst unpcklps nil #x14)
3133 ;; integer arithmetic
3134 (define-regular-sse-inst paddb #x66 #xfc)
3135 (define-regular-sse-inst paddw #x66 #xfd)
3136 (define-regular-sse-inst paddd #x66 #xfe)
3137 (define-regular-sse-inst paddq #x66 #xd4)
3138 (define-regular-sse-inst paddsb #x66 #xec)
3139 (define-regular-sse-inst paddsw #x66 #xed)
3140 (define-regular-sse-inst paddusb #x66 #xdc)
3141 (define-regular-sse-inst paddusw #x66 #xdd)
3142 (define-regular-sse-inst pavgb #x66 #xe0)
3143 (define-regular-sse-inst pavgw #x66 #xe3)
3144 (define-regular-sse-inst pmaddwd #x66 #xf5)
3145 (define-regular-sse-inst pmulhuw #x66 #xe4)
3146 (define-regular-sse-inst pmulhw #x66 #xe5)
3147 (define-regular-sse-inst pmullw #x66 #xd5)
3148 (define-regular-sse-inst pmuludq #x66 #xf4)
3149 (define-regular-sse-inst psadbw #x66 #xf6)
3150 (define-regular-sse-inst psllw #x66 #xf1)
3151 (define-regular-sse-inst pslld #x66 #xf2)
3152 (define-regular-sse-inst psllq #x66 #xf3)
3153 (define-regular-sse-inst psraw #x66 #xe1)
3154 (define-regular-sse-inst psrad #x66 #xe2)
3155 (define-regular-sse-inst psrlw #x66 #xd1)
3156 (define-regular-sse-inst psrld #x66 #xd2)
3157 (define-regular-sse-inst psrlq #x66 #xd3)
3158 (define-regular-sse-inst psubb #x66 #xf8)
3159 (define-regular-sse-inst psubw #x66 #xf9)
3160 (define-regular-sse-inst psubd #x66 #xfa)
3161 (define-regular-sse-inst psubq #x66 #xfb)
3162 (define-regular-sse-inst psubsb #x66 #xe8)
3163 (define-regular-sse-inst psubsw #x66 #xe9)
3164 (define-regular-sse-inst psubusb #x66 #xd8)
3165 (define-regular-sse-inst psubusw #x66 #xd9)
3167 (define-regular-sse-inst cvtdq2pd #xf3 #xe6)
3168 (define-regular-sse-inst cvtdq2ps nil #x5b)
3169 (define-regular-sse-inst cvtpd2dq #xf2 #xe6)
3170 (define-regular-sse-inst cvtpd2ps #x66 #x5a)
3171 (define-regular-sse-inst cvtps2dq #x66 #x5b)
3172 (define-regular-sse-inst cvtps2pd nil #x5a)
3173 (define-regular-sse-inst cvtsd2ss #xf2 #x5a)
3174 (define-regular-sse-inst cvtss2sd #xf3 #x5a)
3175 (define-regular-sse-inst cvttpd2dq #x66 #xe6)
3176 (define-regular-sse-inst cvttps2dq #xf3 #x5b)
3178 (define-regular-sse-inst packsswb #x66 #x63)
3179 (define-regular-sse-inst packssdw #x66 #x6b)
3180 (define-regular-sse-inst packuswb #x66 #x67)
3181 (define-regular-sse-inst punpckhbw #x66 #x68)
3182 (define-regular-sse-inst punpckhwd #x66 #x69)
3183 (define-regular-sse-inst punpckhdq #x66 #x6a)
3184 (define-regular-sse-inst punpckhqdq #x66 #x6d)
3185 (define-regular-sse-inst punpcklbw #x66 #x60)
3186 (define-regular-sse-inst punpcklwd #x66 #x61)
3187 (define-regular-sse-inst punpckldq #x66 #x62)
3188 (define-regular-sse-inst punpcklqdq #x66 #x6c))
3190 (macrolet ((define-xmm-shuffle-sse-inst (name prefix opcode n-bits radix)
3191 (let ((shuffle-pattern
3192 (intern (format nil "SSE-SHUFFLE-PATTERN-~D-~D"
3194 `(define-instruction ,name (segment dst src pattern)
3196 ',(sse-inst-printer-list
3197 'xmm-xmm/mem prefix opcode
3198 :more-fields `((imm nil :type ,shuffle-pattern))
3199 :printer '(:name :tab reg ", " reg/mem ", " imm)))
3202 (aver (typep pattern '(unsigned-byte ,n-bits)))
3203 (emit-regular-sse-inst segment dst src ,prefix ,opcode
3205 (emit-byte segment pattern))))))
3206 (define-xmm-shuffle-sse-inst pshufd #x66 #x70 8 4)
3207 (define-xmm-shuffle-sse-inst pshufhw #xf3 #x70 8 4)
3208 (define-xmm-shuffle-sse-inst pshuflw #xf2 #x70 8 4)
3209 (define-xmm-shuffle-sse-inst shufpd #x66 #xc6 2 2)
3210 (define-xmm-shuffle-sse-inst shufps nil #xc6 8 4))
3212 ;; MASKMOVDQU (dst is DS:RDI)
3213 (define-instruction maskmovdqu (segment src mask)
3215 (sse-inst-printer-list 'xmm-xmm/mem #x66 #xf7))
3217 (aver (xmm-register-p src))
3218 (aver (xmm-register-p mask))
3219 (emit-regular-sse-inst segment src mask #x66 #xf7)))
3221 (macrolet ((define-comparison-sse-inst (name prefix opcode
3222 name-prefix name-suffix)
3223 `(define-instruction ,name (segment op x y)
3225 ',(sse-inst-printer-list
3226 'xmm-xmm/mem prefix opcode
3227 :more-fields '((imm nil :type sse-condition-code))
3228 :printer `(,name-prefix imm ,name-suffix
3229 :tab reg ", " reg/mem)))
3231 (let ((code (position op *sse-conditions*)))
3233 (emit-regular-sse-inst segment x y ,prefix ,opcode
3235 (emit-byte segment code))))))
3236 (define-comparison-sse-inst cmppd #x66 #xc2 "CMP" "PD")
3237 (define-comparison-sse-inst cmpps nil #xc2 "CMP" "PS")
3238 (define-comparison-sse-inst cmpsd #xf2 #xc2 "CMP" "SD")
3239 (define-comparison-sse-inst cmpss #xf3 #xc2 "CMP" "SS"))
3242 (macrolet ((define-movsd/ss-sse-inst (name prefix)
3243 `(define-instruction ,name (segment dst src)
3245 ',(sse-inst-printer-list 'xmm-xmm/mem-dir
3248 (cond ((xmm-register-p dst)
3249 (emit-sse-inst segment dst src ,prefix #x10
3250 :operand-size :do-not-set))
3252 (aver (xmm-register-p src))
3253 (emit-sse-inst segment src dst ,prefix #x11
3254 :operand-size :do-not-set)))))))
3255 (define-movsd/ss-sse-inst movsd #xf2)
3256 (define-movsd/ss-sse-inst movss #xf3))
3259 (macrolet ((define-mov-sse-inst (name prefix opcode-from opcode-to
3260 &key force-to-mem reg-reg-name)
3263 `(define-instruction ,reg-reg-name (segment dst src)
3265 (aver (xmm-register-p dst))
3266 (aver (xmm-register-p src))
3267 (emit-regular-sse-inst segment dst src
3268 ,prefix ,opcode-from))))
3269 (define-instruction ,name (segment dst src)
3271 '(,@(when opcode-from
3272 (sse-inst-printer-list
3273 'xmm-xmm/mem prefix opcode-from))
3274 ,@(sse-inst-printer-list
3275 'xmm-xmm/mem prefix opcode-to
3276 :printer '(:name :tab reg/mem ", " reg))))
3278 (cond ,@(when opcode-from
3279 `(((xmm-register-p dst)
3281 `(aver (not (or (register-p src)
3282 (xmm-register-p src)))))
3283 (emit-regular-sse-inst
3284 segment dst src ,prefix ,opcode-from))))
3286 (aver (xmm-register-p src))
3288 `(aver (not (or (register-p dst)
3289 (xmm-register-p dst)))))
3290 (emit-regular-sse-inst segment src dst
3291 ,prefix ,opcode-to))))))))
3293 (define-mov-sse-inst movapd #x66 #x28 #x29)
3294 (define-mov-sse-inst movaps nil #x28 #x29)
3295 (define-mov-sse-inst movdqa #x66 #x6f #x7f)
3296 (define-mov-sse-inst movdqu #xf3 #x6f #x7f)
3299 (define-mov-sse-inst movntdq #x66 nil #xe7 :force-to-mem t)
3300 (define-mov-sse-inst movntpd #x66 nil #x2b :force-to-mem t)
3301 (define-mov-sse-inst movntps nil nil #x2b :force-to-mem t)
3303 ;; use movhps for movlhps and movlps for movhlps
3304 (define-mov-sse-inst movhpd #x66 #x16 #x17 :force-to-mem t)
3305 (define-mov-sse-inst movhps nil #x16 #x17 :reg-reg-name movlhps)
3306 (define-mov-sse-inst movlpd #x66 #x12 #x13 :force-to-mem t)
3307 (define-mov-sse-inst movlps nil #x12 #x13 :reg-reg-name movhlps)
3308 (define-mov-sse-inst movupd #x66 #x10 #x11)
3309 (define-mov-sse-inst movups nil #x10 #x11))
3312 (define-instruction movntdqa (segment dst src)
3314 (2byte-sse-inst-printer-list '2byte-xmm-xmm/mem #x66 #x38 #x2a))
3316 (aver (and (xmm-register-p dst)
3317 (not (xmm-register-p src))))
3318 (emit-regular-2byte-sse-inst segment dst src #x66 #x38 #x2a)))
3321 (define-instruction movq (segment dst src)
3324 (sse-inst-printer-list 'xmm-xmm/mem #xf3 #x7e)
3325 (sse-inst-printer-list 'xmm-xmm/mem #x66 #xd6
3326 :printer '(:name :tab reg/mem ", " reg))))
3328 (cond ((xmm-register-p dst)
3329 (emit-sse-inst segment dst src #xf3 #x7e
3330 :operand-size :do-not-set))
3332 (aver (xmm-register-p src))
3333 (emit-sse-inst segment src dst #x66 #xd6
3334 :operand-size :do-not-set)))))
3336 ;;; Instructions having an XMM register as the destination operand
3337 ;;; and a general-purpose register or a memory location as the source
3338 ;;; operand. The operand size is calculated from the source operand.
3340 ;;; MOVD - Move a 32- or 64-bit value from a general-purpose register or
3341 ;;; a memory location to the low order 32 or 64 bits of an XMM register
3342 ;;; with zero extension or vice versa.
3343 ;;; We do not support the MMX version of this instruction.
3344 (define-instruction movd (segment dst src)
3347 (sse-inst-printer-list 'xmm-reg/mem #x66 #x6e)
3348 (sse-inst-printer-list 'xmm-reg/mem #x66 #x7e
3349 :printer '(:name :tab reg/mem ", " reg))))
3351 (cond ((xmm-register-p dst)
3352 (emit-sse-inst segment dst src #x66 #x6e))
3354 (aver (xmm-register-p src))
3355 (emit-sse-inst segment src dst #x66 #x7e)))))
3357 (macrolet ((define-extract-sse-instruction (name prefix op1 op2 &key explicit-qword)
3358 `(define-instruction ,name (segment dst src imm)
3360 ,(if op2 (if explicit-qword
3361 'ext-rex-2byte-reg/mem-xmm
3362 'ext-2byte-reg/mem-xmm)
3364 ((prefix '(,prefix))
3366 `((op1 '(,op1)) (op2 '(,op2)))
3368 (imm nil :type 'imm-byte))
3369 '(:name :tab reg/mem ", " reg ", " imm))
3371 (aver (and (xmm-register-p src) (not (xmm-register-p dst))))
3373 `(emit-sse-inst-2byte segment dst src ,prefix ,op1 ,op2
3374 :operand-size ,(if explicit-qword
3378 `(emit-sse-inst segment dst src ,prefix ,op1
3379 :operand-size ,(if explicit-qword
3382 :remaining-bytes 1))
3383 (emit-byte segment imm))))
3385 (define-insert-sse-instruction (name prefix op1 op2)
3386 `(define-instruction ,name (segment dst src imm)
3388 ,(if op2 'ext-2byte-xmm-reg/mem 'ext-xmm-reg/mem)
3389 ((prefix '(,prefix))
3391 `((op1 '(,op1)) (op2 '(,op2)))
3393 (imm nil :type 'imm-byte))
3394 '(:name :tab reg ", " reg/mem ", " imm))
3396 (aver (and (xmm-register-p dst) (not (xmm-register-p src))))
3398 `(emit-sse-inst-2byte segment dst src ,prefix ,op1 ,op2
3399 :operand-size :do-not-set
3401 `(emit-sse-inst segment dst src ,prefix ,op1
3402 :operand-size :do-not-set
3403 :remaining-bytes 1))
3404 (emit-byte segment imm)))))
3407 ;; pinsrq not encodable in 64-bit mode
3408 (define-insert-sse-instruction pinsrb #x66 #x3a #x20)
3409 (define-insert-sse-instruction pinsrw #x66 #xc4 nil)
3410 (define-insert-sse-instruction pinsrd #x66 #x3a #x22)
3411 (define-insert-sse-instruction insertps #x66 #x3a #x21)
3413 (define-extract-sse-instruction pextrb #x66 #x3a #x14)
3414 (define-extract-sse-instruction pextrd #x66 #x3a #x16)
3415 (define-extract-sse-instruction pextrq #x66 #x3a #x16 :explicit-qword t)
3416 (define-extract-sse-instruction extractps #x66 #x3a #x17))
3418 ;; PEXTRW has a new 2-byte encoding in SSE4.1 to allow dst to be
3419 ;; a memory address.
3420 (define-instruction pextrw (segment dst src imm)
3423 (2byte-sse-inst-printer-list '2byte-reg/mem-xmm #x66 #x3a #x15
3424 :more-fields '((imm nil :type imm-byte))
3426 '(:name :tab reg/mem ", " reg ", " imm))
3427 (sse-inst-printer-list 'reg/mem-xmm #x66 #xc5
3428 :more-fields '((imm nil :type imm-byte))
3430 '(:name :tab reg/mem ", " reg ", " imm))))
3432 (aver (xmm-register-p src))
3433 (if (not (register-p dst))
3434 (emit-sse-inst-2byte segment dst src #x66 #x3a #x15
3435 :operand-size :do-not-set :remaining-bytes 1)
3436 (emit-sse-inst segment dst src #x66 #xc5
3437 :operand-size :do-not-set :remaining-bytes 1))
3438 (emit-byte segment imm)))
3440 (macrolet ((define-integer-source-sse-inst (name prefix opcode &key mem-only)
3441 `(define-instruction ,name (segment dst src)
3443 ',(sse-inst-printer-list 'xmm-reg/mem prefix opcode))
3445 (aver (xmm-register-p dst))
3447 `(aver (not (or (register-p src)
3448 (xmm-register-p src)))))
3449 (let ((src-size (operand-size src)))
3450 (aver (or (eq src-size :qword) (eq src-size :dword))))
3451 (emit-sse-inst segment dst src ,prefix ,opcode)))))
3452 (define-integer-source-sse-inst cvtsi2sd #xf2 #x2a)
3453 (define-integer-source-sse-inst cvtsi2ss #xf3 #x2a)
3454 ;; FIXME: memory operand is always a QWORD
3455 (define-integer-source-sse-inst cvtpi2pd #x66 #x2a :mem-only t)
3456 (define-integer-source-sse-inst cvtpi2ps nil #x2a :mem-only t))
3458 ;;; Instructions having a general-purpose register as the destination
3459 ;;; operand and an XMM register or a memory location as the source
3460 ;;; operand. The operand size is calculated from the destination
3463 (macrolet ((define-gpr-destination-sse-inst (name prefix opcode &key reg-only)
3464 `(define-instruction ,name (segment dst src)
3466 ',(sse-inst-printer-list 'reg-xmm/mem prefix opcode))
3468 (aver (register-p dst))
3470 `(aver (xmm-register-p src)))
3471 (let ((dst-size (operand-size dst)))
3472 (aver (or (eq dst-size :qword) (eq dst-size :dword)))
3473 (emit-sse-inst segment dst src ,prefix ,opcode
3474 :operand-size dst-size))))))
3475 (define-gpr-destination-sse-inst cvtsd2si #xf2 #x2d)
3476 (define-gpr-destination-sse-inst cvtss2si #xf3 #x2d)
3477 (define-gpr-destination-sse-inst cvttsd2si #xf2 #x2c)
3478 (define-gpr-destination-sse-inst cvttss2si #xf3 #x2c)
3479 (define-gpr-destination-sse-inst movmskpd #x66 #x50 :reg-only t)
3480 (define-gpr-destination-sse-inst movmskps nil #x50 :reg-only t)
3481 (define-gpr-destination-sse-inst pmovmskb #x66 #xd7 :reg-only t))
3483 ;;;; We call these "2byte" instructions due to their two opcode bytes.
3484 ;;;; Intel and AMD call them three-byte instructions, as they count the
3485 ;;;; 0x0f byte for determining the number of opcode bytes.
3487 ;;; Instructions that take XMM-XMM/MEM and XMM-XMM/MEM-IMM arguments.
3489 (macrolet ((regular-2byte-sse-inst (name prefix op1 op2)
3490 `(define-instruction ,name (segment dst src)
3492 ',(2byte-sse-inst-printer-list '2byte-xmm-xmm/mem prefix op1 op2))
3494 (emit-regular-2byte-sse-inst segment dst src ,prefix ,op1 ,op2))))
3495 (regular-2byte-sse-inst-imm (name prefix op1 op2)
3496 `(define-instruction ,name (segment dst src imm)
3498 ',(2byte-sse-inst-printer-list '2byte-xmm-xmm/mem prefix op1 op2
3499 :more-fields '((imm nil :type imm-byte))
3500 :printer `(:name :tab reg ", " reg/mem ", " imm)))
3502 (aver (typep imm '(unsigned-byte 8)))
3503 (emit-regular-2byte-sse-inst segment dst src ,prefix ,op1 ,op2
3505 (emit-byte segment imm)))))
3506 (regular-2byte-sse-inst pshufb #x66 #x38 #x00)
3507 (regular-2byte-sse-inst phaddw #x66 #x38 #x01)
3508 (regular-2byte-sse-inst phaddd #x66 #x38 #x02)
3509 (regular-2byte-sse-inst phaddsw #x66 #x38 #x03)
3510 (regular-2byte-sse-inst pmaddubsw #x66 #x38 #x04)
3511 (regular-2byte-sse-inst phsubw #x66 #x38 #x05)
3512 (regular-2byte-sse-inst phsubd #x66 #x38 #x06)
3513 (regular-2byte-sse-inst phsubsw #x66 #x38 #x07)
3514 (regular-2byte-sse-inst psignb #x66 #x38 #x08)
3515 (regular-2byte-sse-inst psignw #x66 #x38 #x09)
3516 (regular-2byte-sse-inst psignd #x66 #x38 #x0a)
3517 (regular-2byte-sse-inst pmulhrsw #x66 #x38 #x0b)
3519 (regular-2byte-sse-inst ptest #x66 #x38 #x17)
3520 (regular-2byte-sse-inst pabsb #x66 #x38 #x1c)
3521 (regular-2byte-sse-inst pabsw #x66 #x38 #x1d)
3522 (regular-2byte-sse-inst pabsd #x66 #x38 #x1e)
3524 (regular-2byte-sse-inst pmuldq #x66 #x38 #x28)
3525 (regular-2byte-sse-inst pcmpeqq #x66 #x38 #x29)
3526 (regular-2byte-sse-inst packusdw #x66 #x38 #x2b)
3528 (regular-2byte-sse-inst pcmpgtq #x66 #x38 #x37)
3529 (regular-2byte-sse-inst pminsb #x66 #x38 #x38)
3530 (regular-2byte-sse-inst pminsd #x66 #x38 #x39)
3531 (regular-2byte-sse-inst pminuw #x66 #x38 #x3a)
3532 (regular-2byte-sse-inst pminud #x66 #x38 #x3b)
3533 (regular-2byte-sse-inst pmaxsb #x66 #x38 #x3c)
3534 (regular-2byte-sse-inst pmaxsd #x66 #x38 #x3d)
3535 (regular-2byte-sse-inst pmaxuw #x66 #x38 #x3e)
3536 (regular-2byte-sse-inst pmaxud #x66 #x38 #x3f)
3538 (regular-2byte-sse-inst pmulld #x66 #x38 #x40)
3539 (regular-2byte-sse-inst phminposuw #x66 #x38 #x41)
3541 (regular-2byte-sse-inst aesimc #x66 #x38 #xdb)
3542 (regular-2byte-sse-inst aesenc #x66 #x38 #xdc)
3543 (regular-2byte-sse-inst aesenclast #x66 #x38 #xdd)
3544 (regular-2byte-sse-inst aesdec #x66 #x38 #xde)
3545 (regular-2byte-sse-inst aesdeclast #x66 #x38 #xdf)
3547 (regular-2byte-sse-inst pmovsxbw #x66 #x38 #x20)
3548 (regular-2byte-sse-inst pmovsxbd #x66 #x38 #x21)
3549 (regular-2byte-sse-inst pmovsxbq #x66 #x38 #x22)
3550 (regular-2byte-sse-inst pmovsxwd #x66 #x38 #x23)
3551 (regular-2byte-sse-inst pmovsxwq #x66 #x38 #x24)
3552 (regular-2byte-sse-inst pmovsxdq #x66 #x38 #x25)
3554 (regular-2byte-sse-inst pmovzxbw #x66 #x38 #x30)
3555 (regular-2byte-sse-inst pmovzxbd #x66 #x38 #x31)
3556 (regular-2byte-sse-inst pmovzxbq #x66 #x38 #x32)
3557 (regular-2byte-sse-inst pmovzxwd #x66 #x38 #x33)
3558 (regular-2byte-sse-inst pmovzxwq #x66 #x38 #x34)
3559 (regular-2byte-sse-inst pmovzxdq #x66 #x38 #x35)
3561 (regular-2byte-sse-inst-imm roundps #x66 #x3a #x08)
3562 (regular-2byte-sse-inst-imm roundpd #x66 #x3a #x09)
3563 (regular-2byte-sse-inst-imm roundss #x66 #x3a #x0a)
3564 (regular-2byte-sse-inst-imm roundsd #x66 #x3a #x0b)
3565 (regular-2byte-sse-inst-imm blendps #x66 #x3a #x0c)
3566 (regular-2byte-sse-inst-imm blendpd #x66 #x3a #x0d)
3567 (regular-2byte-sse-inst-imm pblendw #x66 #x3a #x0e)
3568 (regular-2byte-sse-inst-imm palignr #x66 #x3a #x0f)
3569 (regular-2byte-sse-inst-imm dpps #x66 #x3a #x40)
3570 (regular-2byte-sse-inst-imm dppd #x66 #x3a #x41)
3572 (regular-2byte-sse-inst-imm mpsadbw #x66 #x3a #x42)
3573 (regular-2byte-sse-inst-imm pclmulqdq #x66 #x3a #x44)
3575 (regular-2byte-sse-inst-imm pcmpestrm #x66 #x3a #x60)
3576 (regular-2byte-sse-inst-imm pcmpestri #x66 #x3a #x61)
3577 (regular-2byte-sse-inst-imm pcmpistrm #x66 #x3a #x62)
3578 (regular-2byte-sse-inst-imm pcmpistri #x66 #x3a #x63)
3580 (regular-2byte-sse-inst-imm aeskeygenassist #x66 #x3a #xdf))
3582 ;;; Other SSE instructions
3584 ;; Instructions implicitly using XMM0 as a mask
3585 (macrolet ((define-sse-inst-implicit-mask (name prefix op1 op2)
3586 `(define-instruction ,name (segment dst src mask)
3588 ',(2byte-sse-inst-printer-list
3589 '2byte-xmm-xmm/mem prefix op1 op2
3590 :printer '(:name :tab reg ", " reg/mem ", XMM0")))
3592 (aver (xmm-register-p dst))
3593 (aver (and (xmm-register-p mask) (= (tn-offset mask) 0)))
3594 (emit-regular-2byte-sse-inst segment dst src ,prefix ,op1 ,op2)))))
3596 (define-sse-inst-implicit-mask pblendvb #x66 #x38 #x10)
3597 (define-sse-inst-implicit-mask blendvps #x66 #x38 #x14)
3598 (define-sse-inst-implicit-mask blendvpd #x66 #x38 #x15))
3600 ;; FIXME: is that right!?
3601 (define-instruction movnti (segment dst src)
3602 (:printer ext-reg-reg/mem-no-width ((op #xc3)))
3604 (aver (not (or (register-p dst)
3605 (xmm-register-p dst))))
3606 (aver (register-p src))
3607 (maybe-emit-rex-for-ea segment src dst)
3608 (emit-byte segment #x0f)
3609 (emit-byte segment #xc3)
3610 (emit-ea segment dst (reg-tn-encoding src))))
3612 (define-instruction prefetch (segment type src)
3613 (:printer ext-reg/mem-no-width ((op '(#x18 0)))
3614 '("PREFETCHNTA" :tab reg/mem))
3615 (:printer ext-reg/mem-no-width ((op '(#x18 1)))
3616 '("PREFETCHT0" :tab reg/mem))
3617 (:printer ext-reg/mem-no-width ((op '(#x18 2)))
3618 '("PREFETCHT1" :tab reg/mem))
3619 (:printer ext-reg/mem-no-width ((op '(#x18 3)))
3620 '("PREFETCHT2" :tab reg/mem))
3622 (aver (not (or (register-p src)
3623 (xmm-register-p src))))
3624 (aver (eq (operand-size src) :byte))
3625 (let ((type (position type #(:nta :t0 :t1 :t2))))
3627 (maybe-emit-rex-for-ea segment src nil)
3628 (emit-byte segment #x0f)
3629 (emit-byte segment #x18)
3630 (emit-ea segment src type))))
3632 (define-instruction clflush (segment src)
3633 (:printer ext-reg/mem-no-width ((op '(#xae 7))))
3635 (aver (not (or (register-p src)
3636 (xmm-register-p src))))
3637 (aver (eq (operand-size src) :byte))
3638 (maybe-emit-rex-for-ea segment src nil)
3639 (emit-byte segment #x0f)
3640 (emit-byte segment #xae)
3641 (emit-ea segment src 7)))
3643 (macrolet ((define-fence-instruction (name last-byte)
3644 `(define-instruction ,name (segment)
3645 (:printer three-bytes ((op '(#x0f #xae ,last-byte))))
3647 (emit-byte segment #x0f)
3648 (emit-byte segment #xae)
3649 (emit-byte segment ,last-byte)))))
3650 (define-fence-instruction lfence #b11101000)
3651 (define-fence-instruction mfence #b11110000)
3652 (define-fence-instruction sfence #b11111000))
3654 (define-instruction pause (segment)
3655 (:printer two-bytes ((op '(#xf3 #x90))))
3657 (emit-byte segment #xf3)
3658 (emit-byte segment #x90)))
3660 (define-instruction ldmxcsr (segment src)
3661 (:printer ext-reg/mem-no-width ((op '(#xae 2))))
3663 (aver (not (or (register-p src)
3664 (xmm-register-p src))))
3665 (aver (eq (operand-size src) :dword))
3666 (maybe-emit-rex-for-ea segment src nil)
3667 (emit-byte segment #x0f)
3668 (emit-byte segment #xae)
3669 (emit-ea segment src 2)))
3671 (define-instruction stmxcsr (segment dst)
3672 (:printer ext-reg/mem-no-width ((op '(#xae 3))))
3674 (aver (not (or (register-p dst)
3675 (xmm-register-p dst))))
3676 (aver (eq (operand-size dst) :dword))
3677 (maybe-emit-rex-for-ea segment dst nil)
3678 (emit-byte segment #x0f)
3679 (emit-byte segment #xae)
3680 (emit-ea segment dst 3)))
3682 (define-instruction popcnt (segment dst src)
3683 (:printer-list `((f3-escape-reg-reg/mem ((op #xB8)))
3684 (rex-f3-escape-reg-reg/mem ((op #xB8)))))
3686 (aver (register-p dst))
3687 (aver (and (register-p dst) (not (eq (operand-size dst) :byte))))
3688 (aver (not (eq (operand-size src) :byte)))
3689 (emit-sse-inst segment dst src #xf3 #xb8)))
3691 (define-instruction crc32 (segment dst src)
3693 `(,@(mapcan (lambda (op2)
3694 (mapcar (lambda (instfmt)
3695 `(,instfmt ((prefix (#xf2)) (op1 (#x38))
3697 '(ext-rex-2byte-prefix-reg-reg/mem
3698 ext-2byte-prefix-reg-reg/mem)))
3701 (let ((dst-size (operand-size dst)))
3702 (aver (and (register-p dst) (not (or (eq dst-size :word)
3703 (eq dst-size :byte)))))
3704 (if (eq (operand-size src) :byte)
3705 (emit-sse-inst-2byte segment dst src #xf2 #x38 #xf0)
3706 (emit-sse-inst-2byte segment dst src #xf2 #x38 #xf1)))))
3710 (define-instruction cpuid (segment)
3711 (:printer two-bytes ((op '(#b00001111 #b10100010))))
3713 (emit-byte segment #b00001111)
3714 (emit-byte segment #b10100010)))
3716 (define-instruction rdtsc (segment)
3717 (:printer two-bytes ((op '(#b00001111 #b00110001))))
3719 (emit-byte segment #b00001111)
3720 (emit-byte segment #b00110001)))
3722 ;;;; Late VM definitions
3724 (defun canonicalize-inline-constant (constant &aux (alignedp nil))
3725 (let ((first (car constant)))
3726 (when (eql first :aligned)
3729 (setf first (car constant)))
3731 (single-float (setf constant (list :single-float first)))
3732 (double-float (setf constant (list :double-float first)))
3733 ((complex single-float)
3734 (setf constant (list :complex-single-float first)))
3735 ((complex double-float)
3736 (setf constant (list :complex-double-float first)))
3739 #-sb-xc-host simd-pack
3740 (setf constant (list :sse (logior (%simd-pack-low first)
3741 (ash (%simd-pack-high first)
3743 (destructuring-bind (type value) constant
3745 ((:byte :word :dword :qword)
3746 (aver (integerp value))
3749 (aver (base-char-p value))
3750 (cons :byte (char-code value)))
3752 (aver (characterp value))
3753 (cons :dword (char-code value)))
3755 (aver (typep value 'single-float))
3756 (cons (if alignedp :oword :dword)
3757 (ldb (byte 32 0) (single-float-bits value))))
3759 (aver (typep value 'double-float))
3760 (cons (if alignedp :oword :qword)
3761 (ldb (byte 64 0) (logior (ash (double-float-high-bits value) 32)
3762 (double-float-low-bits value)))))
3763 ((:complex-single-float)
3764 (aver (typep value '(complex single-float)))
3765 (cons (if alignedp :oword :qword)
3767 (logior (ash (single-float-bits (imagpart value)) 32)
3769 (single-float-bits (realpart value)))))))
3771 (aver (integerp value))
3772 (cons :oword value))
3773 ((:complex-double-float)
3774 (aver (typep value '(complex double-float)))
3776 (logior (ash (double-float-high-bits (imagpart value)) 96)
3777 (ash (double-float-low-bits (imagpart value)) 64)
3778 (ash (ldb (byte 32 0)
3779 (double-float-high-bits (realpart value)))
3781 (double-float-low-bits (realpart value))))))))
3783 (defun inline-constant-value (constant)
3784 (let ((label (gen-label))
3785 (size (ecase (car constant)
3786 ((:byte :word :dword :qword) (car constant))
3787 ((:oword) :qword))))
3788 (values label (make-ea size
3789 :disp (make-fixup nil :code-object label)))))
3791 (defun emit-constant-segment-header (segment constants optimize)
3792 (declare (ignore constants))
3793 (emit-long-nop segment (if optimize 64 16)))
3795 (defun size-nbyte (size)
3803 (defun sort-inline-constants (constants)
3804 (stable-sort constants #'> :key (lambda (constant)
3805 (size-nbyte (caar constant)))))
3807 (defun emit-inline-constant (constant label)
3808 (let ((size (size-nbyte (car constant))))
3809 (emit-alignment (integer-length (1- size)))
3811 (let ((val (cdr constant)))
3813 do (inst byte (ldb (byte 8 0) val))
3814 (setf val (ash val -8))))))