1 ;;;; that part of the description of the x86-64 instruction set
2 ;;;; which can live on the cross-compilation host
4 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
14 ;;; FIXME: SB!DISASSEM: prefixes are used so widely in this file that
15 ;;; I wonder whether the separation of the disassembler from the
16 ;;; virtual machine is valid or adds value.
18 ;;; Note: In CMU CL, this used to be a call to SET-DISASSEM-PARAMS.
19 (setf sb!disassem:*disassem-inst-alignment-bytes* 1)
21 ;;; This type is used mostly in disassembly and represents legacy
22 ;;; registers only. R8-R15 are handled separately.
23 (deftype reg () '(unsigned-byte 3))
25 ;;; This includes legacy registers and R8-R15.
26 (deftype full-reg () '(unsigned-byte 4))
28 ;;; The XMM registers XMM0 - XMM15.
29 (deftype xmmreg () '(unsigned-byte 4))
31 ;;; Default word size for the chip: if the operand size /= :dword
32 ;;; we need to output #x66 (or REX) prefix
33 (def!constant +default-operand-size+ :dword)
35 ;;; The default address size for the chip. It could be overwritten
36 ;;; to :dword with a #x67 prefix, but this is never needed by SBCL
37 ;;; and thus not supported by this assembler/disassembler.
38 (def!constant +default-address-size+ :qword)
40 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
42 (defun offset-next (value dstate)
43 (declare (type integer value)
44 (type sb!disassem:disassem-state dstate))
45 (+ (sb!disassem:dstate-next-addr dstate) value))
47 (defparameter *byte-reg-names*
48 #(al cl dl bl spl bpl sil dil r8b r9b r10b r11b r12b r13b r14b r15b))
49 (defparameter *high-byte-reg-names*
51 (defparameter *word-reg-names*
52 #(ax cx dx bx sp bp si di r8w r9w r10w r11w r12w r13w r14w r15w))
53 (defparameter *dword-reg-names*
54 #(eax ecx edx ebx esp ebp esi edi r8d r9d r10d r11d r12d r13d r14d r15d))
55 (defparameter *qword-reg-names*
56 #(rax rcx rdx rbx rsp rbp rsi rdi r8 r9 r10 r11 r12 r13 r14 r15))
58 ;;; The printers for registers, memory references and immediates need to
59 ;;; take into account the width bit in the instruction, whether a #x66
60 ;;; or a REX prefix was issued, and the contents of the REX prefix.
61 ;;; This is implemented using prefilters to put flags into the slot
62 ;;; INST-PROPERTIES of the DSTATE. These flags are the following
65 ;;; OPERAND-SIZE-8 The width bit was zero
66 ;;; OPERAND-SIZE-16 The "operand size override" prefix (#x66) was found
67 ;;; REX A REX prefix was found
68 ;;; REX-W A REX prefix with the "operand width" bit set was
70 ;;; REX-R A REX prefix with the "register" bit set was found
71 ;;; REX-X A REX prefix with the "index" bit set was found
72 ;;; REX-B A REX prefix with the "base" bit set was found
74 ;;; Return the operand size depending on the prefixes and width bit as
76 (defun inst-operand-size (dstate)
77 (declare (type sb!disassem:disassem-state dstate))
78 (cond ((sb!disassem:dstate-get-inst-prop dstate 'operand-size-8)
80 ((sb!disassem:dstate-get-inst-prop dstate 'rex-w)
82 ((sb!disassem:dstate-get-inst-prop dstate 'operand-size-16)
85 +default-operand-size+)))
87 ;;; The same as INST-OPERAND-SIZE, but for those instructions (e.g.
88 ;;; PUSH, JMP) that have a default operand size of :qword. It can only
89 ;;; be overwritten to :word.
90 (defun inst-operand-size-default-qword (dstate)
91 (declare (type sb!disassem:disassem-state dstate))
92 (if (sb!disassem:dstate-get-inst-prop dstate 'operand-size-16)
96 ;;; Print to STREAM the name of the general-purpose register encoded by
97 ;;; VALUE and of size WIDTH. For robustness, the high byte registers
98 ;;; (AH, BH, CH, DH) are correctly detected, too, although the compiler
99 ;;; does not use them.
100 (defun print-reg-with-width (value width stream dstate)
101 (declare (type full-reg value)
103 (type sb!disassem:disassem-state dstate))
104 (princ (if (and (eq width :byte)
106 (not (sb!disassem:dstate-get-inst-prop dstate 'rex)))
107 (aref *high-byte-reg-names* (- value 4))
109 (:byte *byte-reg-names*)
110 (:word *word-reg-names*)
111 (:dword *dword-reg-names*)
112 (:qword *qword-reg-names*))
115 ;; XXX plus should do some source-var notes
118 (defun print-reg (value stream dstate)
119 (declare (type full-reg value)
121 (type sb!disassem:disassem-state dstate))
122 (print-reg-with-width value
123 (inst-operand-size dstate)
127 (defun print-reg-default-qword (value stream dstate)
128 (declare (type full-reg value)
130 (type sb!disassem:disassem-state dstate))
131 (print-reg-with-width value
132 (inst-operand-size-default-qword dstate)
136 (defun print-byte-reg (value stream dstate)
137 (declare (type full-reg value)
139 (type sb!disassem:disassem-state dstate))
140 (print-reg-with-width value :byte stream dstate))
142 (defun print-addr-reg (value stream dstate)
143 (declare (type full-reg value)
145 (type sb!disassem:disassem-state dstate))
146 (print-reg-with-width value +default-address-size+ stream dstate))
148 ;;; Print a register or a memory reference of the given WIDTH.
149 ;;; If SIZED-P is true, add an explicit size indicator for memory
151 (defun print-reg/mem-with-width (value width sized-p stream dstate)
152 (declare (type (or list full-reg) value)
153 (type (member :byte :word :dword :qword) width)
154 (type boolean sized-p)
156 (type sb!disassem:disassem-state dstate))
157 (if (typep value 'full-reg)
158 (print-reg-with-width value width stream dstate)
159 (print-mem-access value (and sized-p width) stream dstate)))
161 ;;; Print a register or a memory reference. The width is determined by
162 ;;; calling INST-OPERAND-SIZE.
163 (defun print-reg/mem (value stream dstate)
164 (declare (type (or list full-reg) value)
166 (type sb!disassem:disassem-state dstate))
167 (print-reg/mem-with-width
168 value (inst-operand-size dstate) nil stream dstate))
170 ;; Same as print-reg/mem, but prints an explicit size indicator for
171 ;; memory references.
172 (defun print-sized-reg/mem (value stream dstate)
173 (declare (type (or list full-reg) value)
175 (type sb!disassem:disassem-state dstate))
176 (print-reg/mem-with-width
177 value (inst-operand-size dstate) t stream dstate))
179 ;;; Same as print-sized-reg/mem, but with a default operand size of
181 (defun print-sized-reg/mem-default-qword (value stream dstate)
182 (declare (type (or list full-reg) value)
184 (type sb!disassem:disassem-state dstate))
185 (print-reg/mem-with-width
186 value (inst-operand-size-default-qword dstate) t stream dstate))
188 (defun print-sized-byte-reg/mem (value stream dstate)
189 (declare (type (or list full-reg) value)
191 (type sb!disassem:disassem-state dstate))
192 (print-reg/mem-with-width value :byte t stream dstate))
194 (defun print-sized-word-reg/mem (value stream dstate)
195 (declare (type (or list full-reg) value)
197 (type sb!disassem:disassem-state dstate))
198 (print-reg/mem-with-width value :word t stream dstate))
200 (defun print-sized-dword-reg/mem (value stream dstate)
201 (declare (type (or list full-reg) value)
203 (type sb!disassem:disassem-state dstate))
204 (print-reg/mem-with-width value :dword t stream dstate))
206 (defun print-label (value stream dstate)
207 (declare (ignore dstate))
208 (sb!disassem:princ16 value stream))
210 (defun print-xmmreg (value stream dstate)
211 (declare (type xmmreg value)
214 (format stream "XMM~d" value))
216 (defun print-xmmreg/mem (value stream dstate)
217 (declare (type (or list xmmreg) value)
219 (type sb!disassem:disassem-state dstate))
220 (if (typep value 'xmmreg)
221 (print-xmmreg value stream dstate)
222 (print-mem-access value nil stream dstate)))
224 ;; Same as print-xmmreg/mem, but prints an explicit size indicator for
225 ;; memory references.
226 (defun print-sized-xmmreg/mem (value stream dstate)
227 (declare (type (or list xmmreg) value)
229 (type sb!disassem:disassem-state dstate))
230 (if (typep value 'xmmreg)
231 (print-xmmreg value stream dstate)
232 (print-mem-access value (inst-operand-size dstate) stream dstate)))
234 ;;; This prefilter is used solely for its side effects, namely to put
235 ;;; the bits found in the REX prefix into the DSTATE for use by other
236 ;;; prefilters and by printers.
237 (defun prefilter-wrxb (value dstate)
238 (declare (type (unsigned-byte 4) value)
239 (type sb!disassem:disassem-state dstate))
240 (sb!disassem:dstate-put-inst-prop dstate 'rex)
241 (when (plusp (logand value #b1000))
242 (sb!disassem:dstate-put-inst-prop dstate 'rex-w))
243 (when (plusp (logand value #b0100))
244 (sb!disassem:dstate-put-inst-prop dstate 'rex-r))
245 (when (plusp (logand value #b0010))
246 (sb!disassem:dstate-put-inst-prop dstate 'rex-x))
247 (when (plusp (logand value #b0001))
248 (sb!disassem:dstate-put-inst-prop dstate 'rex-b))
251 ;;; This prefilter is used solely for its side effect, namely to put
252 ;;; the property OPERAND-SIZE-8 into the DSTATE if VALUE is 0.
253 (defun prefilter-width (value dstate)
254 (declare (type bit value)
255 (type sb!disassem:disassem-state dstate))
257 (sb!disassem:dstate-put-inst-prop dstate 'operand-size-8))
260 ;;; This prefilter is used solely for its side effect, namely to put
261 ;;; the property OPERAND-SIZE-16 into the DSTATE.
262 (defun prefilter-x66 (value dstate)
263 (declare (type (eql #x66) value)
265 (type sb!disassem:disassem-state dstate))
266 (sb!disassem:dstate-put-inst-prop dstate 'operand-size-16))
268 ;;; A register field that can be extended by REX.R.
269 (defun prefilter-reg-r (value dstate)
270 (declare (type reg value)
271 (type sb!disassem:disassem-state dstate))
272 (if (sb!disassem::dstate-get-inst-prop dstate 'rex-r)
276 ;;; A register field that can be extended by REX.B.
277 (defun prefilter-reg-b (value dstate)
278 (declare (type reg value)
279 (type sb!disassem:disassem-state dstate))
280 (if (sb!disassem::dstate-get-inst-prop dstate 'rex-b)
284 ;;; Returns either an integer, meaning a register, or a list of
285 ;;; (BASE-REG OFFSET INDEX-REG INDEX-SCALE), where any component
286 ;;; may be missing or nil to indicate that it's not used or has the
287 ;;; obvious default value (e.g., 1 for the index-scale). VALUE is a list
288 ;;; of the mod and r/m field of the ModRM byte of the instruction.
289 ;;; Depending on VALUE a SIB byte and/or an offset may be read. The
290 ;;; REX.B bit from DSTATE is used to extend the sole register or the
291 ;;; BASE-REG to a full register, the REX.X bit does the same for the
293 (defun prefilter-reg/mem (value dstate)
294 (declare (type list value)
295 (type sb!disassem:disassem-state dstate))
296 (let ((mod (first value))
297 (r/m (second value)))
298 (declare (type (unsigned-byte 2) mod)
299 (type (unsigned-byte 3) r/m))
300 (let ((full-reg (if (sb!disassem:dstate-get-inst-prop dstate 'rex-b)
303 (declare (type full-reg full-reg))
309 (let ((sib (sb!disassem:read-suffix 8 dstate)))
310 (declare (type (unsigned-byte 8) sib))
311 (let ((base-reg (ldb (byte 3 0) sib))
312 (index-reg (ldb (byte 3 3) sib))
313 (index-scale (ldb (byte 2 6) sib)))
314 (declare (type (unsigned-byte 3) base-reg index-reg)
315 (type (unsigned-byte 2) index-scale))
319 (if (= base-reg #b101)
320 (sb!disassem:read-signed-suffix 32 dstate)
323 (sb!disassem:read-signed-suffix 8 dstate))
325 (sb!disassem:read-signed-suffix 32 dstate)))))
326 (list (unless (and (= mod #b00) (= base-reg #b101))
327 (if (sb!disassem:dstate-get-inst-prop dstate 'rex-b)
331 (unless (= index-reg #b100)
332 (if (sb!disassem:dstate-get-inst-prop dstate 'rex-x)
335 (ash 1 index-scale))))))
336 ((and (= mod #b00) (= r/m #b101))
337 (list 'rip (sb!disassem:read-signed-suffix 32 dstate)))
341 (list full-reg (sb!disassem:read-signed-suffix 8 dstate)))
343 (list full-reg (sb!disassem:read-signed-suffix 32 dstate)))))))
345 (defun read-address (value dstate)
346 (declare (ignore value)) ; always nil anyway
347 (sb!disassem:read-suffix (width-bits (inst-operand-size dstate)) dstate))
349 (defun width-bits (width)
358 ;;;; disassembler argument types
360 ;;; Used to capture the lower four bits of the REX prefix.
361 (sb!disassem:define-arg-type wrxb
362 :prefilter #'prefilter-wrxb)
364 (sb!disassem:define-arg-type width
365 :prefilter #'prefilter-width
366 :printer (lambda (value stream dstate)
367 (declare (ignore value))
368 (princ (schar (symbol-name (inst-operand-size dstate)) 0)
371 ;;; Used to capture the effect of the #x66 operand size override prefix.
372 (sb!disassem:define-arg-type x66
373 :prefilter #'prefilter-x66)
375 (sb!disassem:define-arg-type displacement
377 :use-label #'offset-next
378 :printer (lambda (value stream dstate)
379 (sb!disassem:maybe-note-assembler-routine value nil dstate)
380 (print-label value stream dstate)))
382 (sb!disassem:define-arg-type accum
383 :printer (lambda (value stream dstate)
384 (declare (ignore value)
386 (type sb!disassem:disassem-state dstate))
387 (print-reg 0 stream dstate)))
389 (sb!disassem:define-arg-type reg
390 :prefilter #'prefilter-reg-r
391 :printer #'print-reg)
393 (sb!disassem:define-arg-type reg-b
394 :prefilter #'prefilter-reg-b
395 :printer #'print-reg)
397 (sb!disassem:define-arg-type reg-b-default-qword
398 :prefilter #'prefilter-reg-b
399 :printer #'print-reg-default-qword)
401 (sb!disassem:define-arg-type imm-addr
402 :prefilter #'read-address
403 :printer #'print-label)
405 ;;; Normally, immediate values for an operand size of :qword are of size
406 ;;; :dword and are sign-extended to 64 bits. For an exception, see the
407 ;;; argument type definition following this one.
408 (sb!disassem:define-arg-type signed-imm-data
409 :prefilter (lambda (value dstate)
410 (declare (ignore value)) ; always nil anyway
411 (let ((width (width-bits (inst-operand-size dstate))))
414 (sb!disassem:read-signed-suffix width dstate))))
416 ;;; Used by the variant of the MOV instruction with opcode B8 which can
417 ;;; move immediates of all sizes (i.e. including :qword) into a
419 (sb!disassem:define-arg-type signed-imm-data-upto-qword
420 :prefilter (lambda (value dstate)
421 (declare (ignore value)) ; always nil anyway
422 (sb!disassem:read-signed-suffix
423 (width-bits (inst-operand-size dstate))
426 ;;; Used by those instructions that have a default operand size of
427 ;;; :qword. Nevertheless the immediate is at most of size :dword.
428 ;;; The only instruction of this kind having a variant with an immediate
429 ;;; argument is PUSH.
430 (sb!disassem:define-arg-type signed-imm-data-default-qword
431 :prefilter (lambda (value dstate)
432 (declare (ignore value)) ; always nil anyway
433 (let ((width (width-bits
434 (inst-operand-size-default-qword dstate))))
437 (sb!disassem:read-signed-suffix width dstate))))
439 (sb!disassem:define-arg-type signed-imm-byte
440 :prefilter (lambda (value dstate)
441 (declare (ignore value)) ; always nil anyway
442 (sb!disassem:read-signed-suffix 8 dstate)))
444 (sb!disassem:define-arg-type imm-byte
445 :prefilter (lambda (value dstate)
446 (declare (ignore value)) ; always nil anyway
447 (sb!disassem:read-suffix 8 dstate)))
449 ;;; needed for the ret imm16 instruction
450 (sb!disassem:define-arg-type imm-word-16
451 :prefilter (lambda (value dstate)
452 (declare (ignore value)) ; always nil anyway
453 (sb!disassem:read-suffix 16 dstate)))
455 (sb!disassem:define-arg-type reg/mem
456 :prefilter #'prefilter-reg/mem
457 :printer #'print-reg/mem)
458 (sb!disassem:define-arg-type sized-reg/mem
459 ;; Same as reg/mem, but prints an explicit size indicator for
460 ;; memory references.
461 :prefilter #'prefilter-reg/mem
462 :printer #'print-sized-reg/mem)
464 ;;; Arguments of type reg/mem with a fixed size.
465 (sb!disassem:define-arg-type sized-byte-reg/mem
466 :prefilter #'prefilter-reg/mem
467 :printer #'print-sized-byte-reg/mem)
468 (sb!disassem:define-arg-type sized-word-reg/mem
469 :prefilter #'prefilter-reg/mem
470 :printer #'print-sized-word-reg/mem)
471 (sb!disassem:define-arg-type sized-dword-reg/mem
472 :prefilter #'prefilter-reg/mem
473 :printer #'print-sized-dword-reg/mem)
475 ;;; Same as sized-reg/mem, but with a default operand size of :qword.
476 (sb!disassem:define-arg-type sized-reg/mem-default-qword
477 :prefilter #'prefilter-reg/mem
478 :printer #'print-sized-reg/mem-default-qword)
481 (sb!disassem:define-arg-type xmmreg
482 :prefilter #'prefilter-reg-r
483 :printer #'print-xmmreg)
485 (sb!disassem:define-arg-type xmmreg-b
486 :prefilter #'prefilter-reg-b
487 :printer #'print-xmmreg)
489 (sb!disassem:define-arg-type xmmreg/mem
490 :prefilter #'prefilter-reg/mem
491 :printer #'print-xmmreg/mem)
493 (sb!disassem:define-arg-type sized-xmmreg/mem
494 :prefilter #'prefilter-reg/mem
495 :printer #'print-sized-xmmreg/mem)
498 (eval-when (:compile-toplevel :load-toplevel :execute)
499 (defparameter *conditions*
502 (:b . 2) (:nae . 2) (:c . 2)
503 (:nb . 3) (:ae . 3) (:nc . 3)
504 (:eq . 4) (:e . 4) (:z . 4)
511 (:np . 11) (:po . 11)
512 (:l . 12) (:nge . 12)
513 (:nl . 13) (:ge . 13)
514 (:le . 14) (:ng . 14)
515 (:nle . 15) (:g . 15)))
516 (defparameter *condition-name-vec*
517 (let ((vec (make-array 16 :initial-element nil)))
518 (dolist (cond *conditions*)
519 (when (null (aref vec (cdr cond)))
520 (setf (aref vec (cdr cond)) (car cond))))
524 ;;; SSE shuffle patterns. The names end in the number of bits of the
525 ;;; immediate byte that are used to encode the pattern and the radix
526 ;;; in which to print the value.
527 (macrolet ((define-sse-shuffle-arg-type (name format-string)
528 `(sb!disassem:define-arg-type ,name
530 :printer (lambda (value stream dstate)
531 (declare (type (unsigned-byte 8) value)
534 (format stream ,format-string value)))))
535 (define-sse-shuffle-arg-type sse-shuffle-pattern-2-2 "#b~2,'0B")
536 (define-sse-shuffle-arg-type sse-shuffle-pattern-8-4 "#4r~4,4,'0R"))
538 ;;; Set assembler parameters. (In CMU CL, this was done with
539 ;;; a call to a macro DEF-ASSEMBLER-PARAMS.)
540 (eval-when (:compile-toplevel :load-toplevel :execute)
541 (setf sb!assem:*assem-scheduler-p* nil))
543 (sb!disassem:define-arg-type condition-code
544 :printer *condition-name-vec*)
546 (defun conditional-opcode (condition)
547 (cdr (assoc condition *conditions* :test #'eq)))
549 ;;;; disassembler instruction formats
551 (eval-when (:compile-toplevel :execute)
552 (defun swap-if (direction field1 separator field2)
553 `(:if (,direction :constant 0)
554 (,field1 ,separator ,field2)
555 (,field2 ,separator ,field1))))
557 (sb!disassem:define-instruction-format (byte 8 :default-printer '(:name))
558 (op :field (byte 8 0))
563 (sb!disassem:define-instruction-format (two-bytes 16
564 :default-printer '(:name))
565 (op :fields (list (byte 8 0) (byte 8 8))))
567 (sb!disassem:define-instruction-format (three-bytes 24
568 :default-printer '(:name))
569 (op :fields (list (byte 8 0) (byte 8 8) (byte 8 16))))
571 ;;; Prefix instructions
573 (sb!disassem:define-instruction-format (rex 8)
574 (rex :field (byte 4 4) :value #b0100)
575 (wrxb :field (byte 4 0) :type 'wrxb))
577 (sb!disassem:define-instruction-format (x66 8)
578 (x66 :field (byte 8 0) :type 'x66 :value #x66))
580 ;;; A one-byte instruction with a #x66 prefix, used to indicate an
581 ;;; operand size of :word.
582 (sb!disassem:define-instruction-format (x66-byte 16
583 :default-printer '(:name))
584 (x66 :field (byte 8 0) :value #x66)
585 (op :field (byte 8 8)))
587 ;;; A one-byte instruction with a REX prefix, used to indicate an
588 ;;; operand size of :qword. REX.W must be 1, the other three bits are
590 (sb!disassem:define-instruction-format (rex-byte 16
591 :default-printer '(:name))
592 (rex :field (byte 5 3) :value #b01001)
593 (op :field (byte 8 8)))
595 (sb!disassem:define-instruction-format (simple 8)
596 (op :field (byte 7 1))
597 (width :field (byte 1 0) :type 'width)
602 ;;; Same as simple, but with direction bit
603 (sb!disassem:define-instruction-format (simple-dir 8 :include 'simple)
604 (op :field (byte 6 2))
605 (dir :field (byte 1 1)))
607 ;;; Same as simple, but with the immediate value occurring by default,
608 ;;; and with an appropiate printer.
609 (sb!disassem:define-instruction-format (accum-imm 8
611 :default-printer '(:name
612 :tab accum ", " imm))
613 (imm :type 'signed-imm-data))
615 (sb!disassem:define-instruction-format (reg-no-width 8
616 :default-printer '(:name :tab reg))
617 (op :field (byte 5 3))
618 (reg :field (byte 3 0) :type 'reg-b)
623 ;;; Same as reg-no-width, but with a default operand size of :qword.
624 (sb!disassem:define-instruction-format (reg-no-width-default-qword 8
625 :include 'reg-no-width
626 :default-printer '(:name :tab reg))
627 (reg :type 'reg-b-default-qword))
629 ;;; Adds a width field to reg-no-width. Note that we can't use
630 ;;; :INCLUDE 'REG-NO-WIDTH here to save typing because that would put
631 ;;; the WIDTH field last, but the prefilter for WIDTH must run before
632 ;;; the one for IMM to be able to determine the correct size of IMM.
633 (sb!disassem:define-instruction-format (reg 8
634 :default-printer '(:name :tab reg))
635 (op :field (byte 4 4))
636 (width :field (byte 1 3) :type 'width)
637 (reg :field (byte 3 0) :type 'reg-b)
642 (sb!disassem:define-instruction-format (rex-reg 16
643 :default-printer '(:name :tab reg))
644 (rex :field (byte 4 4) :value #b0100)
645 (wrxb :field (byte 4 0) :type 'wrxb)
646 (width :field (byte 1 11) :type 'width)
647 (op :field (byte 4 12))
648 (reg :field (byte 3 8) :type 'reg-b)
653 (sb!disassem:define-instruction-format (two-bytes 16
654 :default-printer '(:name))
655 (op :fields (list (byte 8 0) (byte 8 8))))
657 (sb!disassem:define-instruction-format (reg-reg/mem 16
659 `(:name :tab reg ", " reg/mem))
660 (op :field (byte 7 1))
661 (width :field (byte 1 0) :type 'width)
662 (reg/mem :fields (list (byte 2 14) (byte 3 8))
664 (reg :field (byte 3 11) :type 'reg)
668 ;;; same as reg-reg/mem, but with direction bit
669 (sb!disassem:define-instruction-format (reg-reg/mem-dir 16
670 :include 'reg-reg/mem
674 ,(swap-if 'dir 'reg/mem ", " 'reg)))
675 (op :field (byte 6 2))
676 (dir :field (byte 1 1)))
678 ;;; Same as reg-reg/mem, but uses the reg field as a second op code.
679 (sb!disassem:define-instruction-format (reg/mem 16
680 :default-printer '(:name :tab reg/mem))
681 (op :fields (list (byte 7 1) (byte 3 11)))
682 (width :field (byte 1 0) :type 'width)
683 (reg/mem :fields (list (byte 2 14) (byte 3 8))
684 :type 'sized-reg/mem)
688 ;;; Same as reg/mem, but without a width field and with a default
689 ;;; operand size of :qword.
690 (sb!disassem:define-instruction-format (reg/mem-default-qword 16
691 :default-printer '(:name :tab reg/mem))
692 (op :fields (list (byte 8 0) (byte 3 11)))
693 (reg/mem :fields (list (byte 2 14) (byte 3 8))
694 :type 'sized-reg/mem-default-qword))
696 ;;; Same as reg/mem, but with the immediate value occurring by default,
697 ;;; and with an appropiate printer.
698 (sb!disassem:define-instruction-format (reg/mem-imm 16
701 '(:name :tab reg/mem ", " imm))
702 (reg/mem :type 'sized-reg/mem)
703 (imm :type 'signed-imm-data))
705 ;;; Same as reg/mem, but with using the accumulator in the default printer
706 (sb!disassem:define-instruction-format
708 :include 'reg/mem :default-printer '(:name :tab accum ", " reg/mem))
709 (reg/mem :type 'reg/mem) ; don't need a size
710 (accum :type 'accum))
712 ;;; Same as reg-reg/mem, but with a prefix of #b00001111
713 (sb!disassem:define-instruction-format (ext-reg-reg/mem 24
715 `(:name :tab reg ", " reg/mem))
716 (prefix :field (byte 8 0) :value #b00001111)
717 (op :field (byte 7 9))
718 (width :field (byte 1 8) :type 'width)
719 (reg/mem :fields (list (byte 2 22) (byte 3 16))
721 (reg :field (byte 3 19) :type 'reg)
725 (sb!disassem:define-instruction-format (ext-reg-reg/mem-no-width 24
727 `(:name :tab reg ", " reg/mem))
728 (prefix :field (byte 8 0) :value #b00001111)
729 (op :field (byte 8 8))
730 (reg/mem :fields (list (byte 2 22) (byte 3 16))
732 (reg :field (byte 3 19) :type 'reg)
736 (sb!disassem:define-instruction-format (ext-reg/mem-no-width 24
738 `(:name :tab reg/mem))
739 (prefix :field (byte 8 0) :value #b00001111)
740 (op :fields (list (byte 8 8) (byte 3 19)))
741 (reg/mem :fields (list (byte 2 22) (byte 3 16))
744 ;;; reg-no-width with #x0f prefix
745 (sb!disassem:define-instruction-format (ext-reg-no-width 16
746 :default-printer '(:name :tab reg))
747 (prefix :field (byte 8 0) :value #b00001111)
748 (op :field (byte 5 11))
749 (reg :field (byte 3 8) :type 'reg-b))
751 ;;; Same as reg/mem, but with a prefix of #b00001111
752 (sb!disassem:define-instruction-format (ext-reg/mem 24
753 :default-printer '(:name :tab reg/mem))
754 (prefix :field (byte 8 0) :value #b00001111)
755 (op :fields (list (byte 7 9) (byte 3 19)))
756 (width :field (byte 1 8) :type 'width)
757 (reg/mem :fields (list (byte 2 22) (byte 3 16))
758 :type 'sized-reg/mem)
762 (sb!disassem:define-instruction-format (ext-reg/mem-imm 24
763 :include 'ext-reg/mem
765 '(:name :tab reg/mem ", " imm))
766 (imm :type 'signed-imm-data))
768 ;;;; XMM instructions
770 ;;; All XMM instructions use an extended opcode (#x0F as the first
771 ;;; opcode byte). Therefore in the following "EXT" in the name of the
772 ;;; instruction formats refers to the formats that have an additional
773 ;;; prefix (#x66, #xF2 or #xF3).
775 ;;; Instructions having an XMM register as the destination operand
776 ;;; and an XMM register or a memory location as the source operand.
777 ;;; The size of the operands is implicitly given by the instruction.
778 (sb!disassem:define-instruction-format (xmm-xmm/mem 24
780 '(:name :tab reg ", " reg/mem))
781 (x0f :field (byte 8 0) :value #x0f)
782 (op :field (byte 8 8))
783 (reg/mem :fields (list (byte 2 22) (byte 3 16))
785 (reg :field (byte 3 19) :type 'xmmreg)
789 (sb!disassem:define-instruction-format (ext-xmm-xmm/mem 32
791 '(:name :tab reg ", " reg/mem))
792 (prefix :field (byte 8 0))
793 (x0f :field (byte 8 8) :value #x0f)
794 (op :field (byte 8 16))
795 (reg/mem :fields (list (byte 2 30) (byte 3 24))
797 (reg :field (byte 3 27) :type 'xmmreg)
800 (sb!disassem:define-instruction-format (ext-rex-xmm-xmm/mem 40
802 '(:name :tab reg ", " reg/mem))
803 (prefix :field (byte 8 0))
804 (rex :field (byte 4 12) :value #b0100)
805 (wrxb :field (byte 4 8) :type 'wrxb)
806 (x0f :field (byte 8 16) :value #x0f)
807 (op :field (byte 8 24))
808 (reg/mem :fields (list (byte 2 38) (byte 3 32))
810 (reg :field (byte 3 35) :type 'xmmreg)
813 ;;; Same as xmm-xmm/mem etc., but with direction bit.
815 (sb!disassem:define-instruction-format (ext-xmm-xmm/mem-dir 32
816 :include 'ext-xmm-xmm/mem
820 ,(swap-if 'dir 'reg ", " 'reg/mem)))
821 (op :field (byte 7 17))
822 (dir :field (byte 1 16)))
824 (sb!disassem:define-instruction-format (ext-rex-xmm-xmm/mem-dir 40
825 :include 'ext-rex-xmm-xmm/mem
829 ,(swap-if 'dir 'reg ", " 'reg/mem)))
830 (op :field (byte 7 25))
831 (dir :field (byte 1 24)))
833 ;;; Instructions having an XMM register as one operand
834 ;;; and a constant (unsigned) byte as the other.
836 (sb!disassem:define-instruction-format (ext-xmm-imm 32
838 '(:name :tab reg/mem ", " imm))
839 (prefix :field (byte 8 0))
840 (x0f :field (byte 8 8) :value #x0f)
841 (op :field (byte 8 16))
842 (/i :field (byte 3 27))
843 (b11 :field (byte 2 30) :value #b11)
844 (reg/mem :field (byte 3 24)
846 (imm :type 'imm-byte))
848 (sb!disassem:define-instruction-format (ext-rex-xmm-imm 40
850 '(:name :tab reg/mem ", " imm))
851 (prefix :field (byte 8 0))
852 (rex :field (byte 4 12) :value #b0100)
853 (wrxb :field (byte 4 8) :type 'wrxb)
854 (x0f :field (byte 8 16) :value #x0f)
855 (op :field (byte 8 24))
856 (/i :field (byte 3 35))
857 (b11 :field (byte 2 38) :value #b11)
858 (reg/mem :field (byte 3 32)
860 (imm :type 'imm-byte))
862 ;;; Instructions having an XMM register as one operand and a general-
863 ;;; -purpose register or a memory location as the other operand.
865 (sb!disassem:define-instruction-format (xmm-reg/mem 24
867 '(:name :tab reg ", " reg/mem))
868 (x0f :field (byte 8 0) :value #x0f)
869 (op :field (byte 8 8))
870 (reg/mem :fields (list (byte 2 22) (byte 3 16))
871 :type 'sized-reg/mem)
872 (reg :field (byte 3 19) :type 'xmmreg))
874 (sb!disassem:define-instruction-format (ext-xmm-reg/mem 32
876 '(:name :tab reg ", " reg/mem))
877 (prefix :field (byte 8 0))
878 (x0f :field (byte 8 8) :value #x0f)
879 (op :field (byte 8 16))
880 (reg/mem :fields (list (byte 2 30) (byte 3 24))
881 :type 'sized-reg/mem)
882 (reg :field (byte 3 27) :type 'xmmreg))
884 (sb!disassem:define-instruction-format (ext-rex-xmm-reg/mem 40
886 '(:name :tab reg ", " reg/mem))
887 (prefix :field (byte 8 0))
888 (rex :field (byte 4 12) :value #b0100)
889 (wrxb :field (byte 4 8) :type 'wrxb)
890 (x0f :field (byte 8 16) :value #x0f)
891 (op :field (byte 8 24))
892 (reg/mem :fields (list (byte 2 38) (byte 3 32))
893 :type 'sized-reg/mem)
894 (reg :field (byte 3 35) :type 'xmmreg))
896 ;;; Instructions having a general-purpose register as one operand and an
897 ;;; XMM register or a memory location as the other operand.
899 (sb!disassem:define-instruction-format (reg-xmm/mem 24
901 '(:name :tab reg ", " reg/mem))
902 (x0f :field (byte 8 0) :value #x0f)
903 (op :field (byte 8 8))
904 (reg/mem :fields (list (byte 2 22) (byte 3 16))
905 :type 'sized-xmmreg/mem)
906 (reg :field (byte 3 19) :type 'reg))
908 (sb!disassem:define-instruction-format (ext-reg-xmm/mem 32
910 '(:name :tab reg ", " reg/mem))
911 (prefix :field (byte 8 0))
912 (x0f :field (byte 8 8) :value #x0f)
913 (op :field (byte 8 16))
914 (reg/mem :fields (list (byte 2 30) (byte 3 24))
915 :type 'sized-xmmreg/mem)
916 (reg :field (byte 3 27) :type 'reg))
918 (sb!disassem:define-instruction-format (ext-rex-reg-xmm/mem 40
920 '(:name :tab reg ", " reg/mem))
921 (prefix :field (byte 8 0))
922 (rex :field (byte 4 12) :value #b0100)
923 (wrxb :field (byte 4 8) :type 'wrxb)
924 (x0f :field (byte 8 16) :value #x0f)
925 (op :field (byte 8 24))
926 (reg/mem :fields (list (byte 2 38) (byte 3 32))
927 :type 'sized-xmmreg/mem)
928 (reg :field (byte 3 35) :type 'reg))
930 ;; XMM comparison instruction
932 (eval-when (:compile-toplevel :load-toplevel :execute)
933 (defparameter *sse-conditions* #(:eq :lt :le :unord :neq :nlt :nle :ord)))
935 (sb!disassem:define-arg-type sse-condition-code
936 ;; Inherit the prefilter from IMM-BYTE to READ-SUFFIX the byte.
938 :printer *sse-conditions*)
940 (sb!disassem:define-instruction-format (string-op 8
942 :default-printer '(:name width)))
944 (sb!disassem:define-instruction-format (short-cond-jump 16)
945 (op :field (byte 4 4))
946 (cc :field (byte 4 0) :type 'condition-code)
947 (label :field (byte 8 8) :type 'displacement))
949 (sb!disassem:define-instruction-format (short-jump 16
950 :default-printer '(:name :tab label))
951 (const :field (byte 4 4) :value #b1110)
952 (op :field (byte 4 0))
953 (label :field (byte 8 8) :type 'displacement))
955 (sb!disassem:define-instruction-format (near-cond-jump 16)
956 (op :fields (list (byte 8 0) (byte 4 12)) :value '(#b00001111 #b1000))
957 (cc :field (byte 4 8) :type 'condition-code)
958 ;; The disassembler currently doesn't let you have an instruction > 32 bits
959 ;; long, so we fake it by using a prefilter to read the offset.
960 (label :type 'displacement
961 :prefilter (lambda (value dstate)
962 (declare (ignore value)) ; always nil anyway
963 (sb!disassem:read-signed-suffix 32 dstate))))
965 (sb!disassem:define-instruction-format (near-jump 8
966 :default-printer '(:name :tab label))
967 (op :field (byte 8 0))
968 ;; The disassembler currently doesn't let you have an instruction > 32 bits
969 ;; long, so we fake it by using a prefilter to read the address.
970 (label :type 'displacement
971 :prefilter (lambda (value dstate)
972 (declare (ignore value)) ; always nil anyway
973 (sb!disassem:read-signed-suffix 32 dstate))))
976 (sb!disassem:define-instruction-format (cond-set 24
977 :default-printer '('set cc :tab reg/mem))
978 (prefix :field (byte 8 0) :value #b00001111)
979 (op :field (byte 4 12) :value #b1001)
980 (cc :field (byte 4 8) :type 'condition-code)
981 (reg/mem :fields (list (byte 2 22) (byte 3 16))
982 :type 'sized-byte-reg/mem)
983 (reg :field (byte 3 19) :value #b000))
985 (sb!disassem:define-instruction-format (cond-move 24
987 '('cmov cc :tab reg ", " reg/mem))
988 (prefix :field (byte 8 0) :value #b00001111)
989 (op :field (byte 4 12) :value #b0100)
990 (cc :field (byte 4 8) :type 'condition-code)
991 (reg/mem :fields (list (byte 2 22) (byte 3 16))
993 (reg :field (byte 3 19) :type 'reg))
995 (sb!disassem:define-instruction-format (enter-format 32
996 :default-printer '(:name
998 (:unless (:constant 0)
1000 (op :field (byte 8 0))
1001 (disp :field (byte 16 8))
1002 (level :field (byte 8 24)))
1004 ;;; Single byte instruction with an immediate byte argument.
1005 (sb!disassem:define-instruction-format (byte-imm 16
1006 :default-printer '(:name :tab code))
1007 (op :field (byte 8 0))
1008 (code :field (byte 8 8)))
1010 ;;; Two byte instruction with an immediate byte argument.
1012 (sb!disassem:define-instruction-format (word-imm 24
1013 :default-printer '(:name :tab code))
1014 (op :field (byte 16 0))
1015 (code :field (byte 8 16)))
1018 ;;;; primitive emitters
1020 (define-bitfield-emitter emit-word 16
1023 (define-bitfield-emitter emit-dword 32
1026 ;;; Most uses of dwords are as displacements or as immediate values in
1027 ;;; 64-bit operations. In these cases they are sign-extended to 64 bits.
1028 ;;; EMIT-DWORD is unsuitable there because it accepts values of type
1029 ;;; (OR (SIGNED-BYTE 32) (UNSIGNED-BYTE 32)), so we provide a more
1030 ;;; restricted emitter here.
1031 (defun emit-signed-dword (segment value)
1032 (declare (type segment segment)
1033 (type (signed-byte 32) value))
1034 (declare (inline emit-dword))
1035 (emit-dword segment value))
1037 (define-bitfield-emitter emit-qword 64
1040 (define-bitfield-emitter emit-byte-with-reg 8
1041 (byte 5 3) (byte 3 0))
1043 (define-bitfield-emitter emit-mod-reg-r/m-byte 8
1044 (byte 2 6) (byte 3 3) (byte 3 0))
1046 (define-bitfield-emitter emit-sib-byte 8
1047 (byte 2 6) (byte 3 3) (byte 3 0))
1049 (define-bitfield-emitter emit-rex-byte 8
1050 (byte 4 4) (byte 1 3) (byte 1 2) (byte 1 1) (byte 1 0))
1056 (defun emit-absolute-fixup (segment fixup &optional quad-p)
1057 (note-fixup segment (if quad-p :absolute64 :absolute) fixup)
1058 (let ((offset (fixup-offset fixup)))
1059 (if (label-p offset)
1060 (emit-back-patch segment
1062 (lambda (segment posn)
1063 (declare (ignore posn))
1064 (let ((val (- (+ (component-header-length)
1065 (or (label-position offset)
1067 other-pointer-lowtag)))
1069 (emit-qword segment val)
1070 (emit-signed-dword segment val)))))
1072 (emit-qword segment (or offset 0))
1073 (emit-signed-dword segment (or offset 0))))))
1075 (defun emit-relative-fixup (segment fixup)
1076 (note-fixup segment :relative fixup)
1077 (emit-signed-dword segment (or (fixup-offset fixup) 0)))
1080 ;;;; the effective-address (ea) structure
1082 (defun reg-tn-encoding (tn)
1083 (declare (type tn tn))
1084 ;; ea only has space for three bits of register number: regs r8
1085 ;; and up are selected by a REX prefix byte which caller is responsible
1086 ;; for having emitted where necessary already
1087 (ecase (sb-name (sc-sb (tn-sc tn)))
1089 (let ((offset (mod (tn-offset tn) 16)))
1090 (logior (ash (logand offset 1) 2)
1093 (mod (tn-offset tn) 8))))
1095 (defstruct (ea (:constructor make-ea (size &key base index scale disp))
1097 ;; note that we can represent an EA with a QWORD size, but EMIT-EA
1098 ;; can't actually emit it on its own: caller also needs to emit REX
1100 (size nil :type (member :byte :word :dword :qword))
1101 (base nil :type (or tn null))
1102 (index nil :type (or tn null))
1103 (scale 1 :type (member 1 2 4 8))
1104 (disp 0 :type (or (unsigned-byte 32) (signed-byte 32) fixup)))
1105 (def!method print-object ((ea ea) stream)
1106 (cond ((or *print-escape* *print-readably*)
1107 (print-unreadable-object (ea stream :type t)
1109 "~S~@[ base=~S~]~@[ index=~S~]~@[ scale=~S~]~@[ disp=~S~]"
1113 (let ((scale (ea-scale ea)))
1114 (if (= scale 1) nil scale))
1117 (format stream "~A PTR [" (symbol-name (ea-size ea)))
1119 (write-string (sb!c::location-print-name (ea-base ea)) stream)
1121 (write-string "+" stream)))
1123 (write-string (sb!c::location-print-name (ea-index ea)) stream))
1124 (unless (= (ea-scale ea) 1)
1125 (format stream "*~A" (ea-scale ea)))
1126 (typecase (ea-disp ea)
1129 (format stream "~@D" (ea-disp ea)))
1131 (format stream "+~A" (ea-disp ea))))
1132 (write-char #\] stream))))
1134 (defun emit-constant-tn-rip (segment constant-tn reg remaining-bytes)
1135 ;; AMD64 doesn't currently have a code object register to use as a
1136 ;; base register for constant access. Instead we use RIP-relative
1137 ;; addressing. The offset from the SIMPLE-FUN-HEADER to the instruction
1138 ;; is passed to the backpatch callback. In addition we need the offset
1139 ;; from the start of the function header to the slot in the CODE-HEADER
1140 ;; that stores the constant. Since we don't know where the code header
1141 ;; starts, instead count backwards from the function header.
1142 (let* ((2comp (component-info *component-being-compiled*))
1143 (constants (ir2-component-constants 2comp))
1144 (len (length constants))
1145 ;; Both CODE-HEADER and SIMPLE-FUN-HEADER are 16-byte aligned.
1146 ;; If there are an even amount of constants, there will be
1147 ;; an extra qword of padding before the function header, which
1148 ;; needs to be adjusted for. XXX: This will break if new slots
1149 ;; are added to the code header.
1150 (offset (* (- (+ len (if (evenp len)
1153 (tn-offset constant-tn))
1155 ;; RIP-relative addressing
1156 (emit-mod-reg-r/m-byte segment #b00 reg #b101)
1157 (emit-back-patch segment
1159 (lambda (segment posn)
1160 ;; The addressing is relative to end of instruction,
1161 ;; i.e. the end of this dword. Hence the + 4.
1162 (emit-signed-dword segment
1163 (+ 4 remaining-bytes
1164 (- (+ offset posn)))))))
1167 (defun emit-label-rip (segment fixup reg remaining-bytes)
1168 (let ((label (fixup-offset fixup)))
1169 ;; RIP-relative addressing
1170 (emit-mod-reg-r/m-byte segment #b00 reg #b101)
1171 (emit-back-patch segment
1173 (lambda (segment posn)
1174 (emit-signed-dword segment
1175 (- (label-position label)
1176 (+ posn 4 remaining-bytes))))))
1179 (defun emit-ea (segment thing reg &key allow-constants (remaining-bytes 0))
1182 ;; this would be eleganter if we had a function that would create
1184 (ecase (sb-name (sc-sb (tn-sc thing)))
1185 ((registers float-registers)
1186 (emit-mod-reg-r/m-byte segment #b11 reg (reg-tn-encoding thing)))
1188 ;; Convert stack tns into an index off RBP.
1189 (let ((disp (frame-byte-offset (tn-offset thing))))
1190 (cond ((<= -128 disp 127)
1191 (emit-mod-reg-r/m-byte segment #b01 reg #b101)
1192 (emit-byte segment disp))
1194 (emit-mod-reg-r/m-byte segment #b10 reg #b101)
1195 (emit-signed-dword segment disp)))))
1197 (unless allow-constants
1200 "Constant TNs can only be directly used in MOV, PUSH, and CMP."))
1201 (emit-constant-tn-rip segment thing reg remaining-bytes))))
1203 (let* ((base (ea-base thing))
1204 (index (ea-index thing))
1205 (scale (ea-scale thing))
1206 (disp (ea-disp thing))
1207 (mod (cond ((or (null base)
1209 (not (= (reg-tn-encoding base) #b101))))
1211 ((and (fixnump disp) (<= -128 disp 127))
1215 (r/m (cond (index #b100)
1217 (t (reg-tn-encoding base)))))
1218 (when (and (fixup-p disp)
1219 (label-p (fixup-offset disp)))
1222 (return-from emit-ea (emit-ea segment disp reg
1223 :allow-constants allow-constants
1224 :remaining-bytes remaining-bytes)))
1225 (when (and (= mod 0) (= r/m #b101))
1226 ;; this is rip-relative in amd64, so we'll use a sib instead
1227 (setf r/m #b100 scale 1))
1228 (emit-mod-reg-r/m-byte segment mod reg r/m)
1230 (let ((ss (1- (integer-length scale)))
1231 (index (if (null index)
1233 (let ((index (reg-tn-encoding index)))
1235 (error "can't index off of ESP")
1237 (base (if (null base)
1239 (reg-tn-encoding base))))
1240 (emit-sib-byte segment ss index base)))
1242 (emit-byte segment disp))
1243 ((or (= mod #b10) (null base))
1245 (emit-absolute-fixup segment disp)
1246 (emit-signed-dword segment disp))))))
1248 (typecase (fixup-offset thing)
1250 (emit-label-rip segment thing reg remaining-bytes))
1252 (emit-mod-reg-r/m-byte segment #b00 reg #b100)
1253 (emit-sib-byte segment 0 #b100 #b101)
1254 (emit-absolute-fixup segment thing))))))
1256 (defun byte-reg-p (thing)
1258 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)
1259 (member (sc-name (tn-sc thing)) *byte-sc-names*)
1262 (defun byte-ea-p (thing)
1264 (ea (eq (ea-size thing) :byte))
1266 (and (member (sc-name (tn-sc thing)) *byte-sc-names*) t))
1269 (defun word-reg-p (thing)
1271 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)
1272 (member (sc-name (tn-sc thing)) *word-sc-names*)
1275 (defun word-ea-p (thing)
1277 (ea (eq (ea-size thing) :word))
1278 (tn (and (member (sc-name (tn-sc thing)) *word-sc-names*) t))
1281 (defun dword-reg-p (thing)
1283 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)
1284 (member (sc-name (tn-sc thing)) *dword-sc-names*)
1287 (defun dword-ea-p (thing)
1289 (ea (eq (ea-size thing) :dword))
1291 (and (member (sc-name (tn-sc thing)) *dword-sc-names*) t))
1294 (defun qword-reg-p (thing)
1296 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)
1297 (member (sc-name (tn-sc thing)) *qword-sc-names*)
1300 (defun qword-ea-p (thing)
1302 (ea (eq (ea-size thing) :qword))
1304 (and (member (sc-name (tn-sc thing)) *qword-sc-names*) t))
1307 ;;; Return true if THING is a general-purpose register TN.
1308 (defun register-p (thing)
1310 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)))
1312 (defun accumulator-p (thing)
1313 (and (register-p thing)
1314 (= (tn-offset thing) 0)))
1316 ;;; Return true if THING is an XMM register TN.
1317 (defun xmm-register-p (thing)
1319 (eq (sb-name (sc-sb (tn-sc thing))) 'float-registers)))
1324 (def!constant +operand-size-prefix-byte+ #b01100110)
1326 (defun maybe-emit-operand-size-prefix (segment size)
1327 (unless (or (eq size :byte)
1328 (eq size :qword) ; REX prefix handles this
1329 (eq size +default-operand-size+))
1330 (emit-byte segment +operand-size-prefix-byte+)))
1332 ;;; A REX prefix must be emitted if at least one of the following
1333 ;;; conditions is true:
1334 ;; 1. The operand size is :QWORD and the default operand size of the
1335 ;; instruction is not :QWORD.
1336 ;;; 2. The instruction references an extended register.
1337 ;;; 3. The instruction references one of the byte registers SIL, DIL,
1340 ;;; Emit a REX prefix if necessary. OPERAND-SIZE is used to determine
1341 ;;; whether to set REX.W. Callers pass it explicitly as :DO-NOT-SET if
1342 ;;; this should not happen, for example because the instruction's
1343 ;;; default operand size is qword. R, X and B are NIL or TNs specifying
1344 ;;; registers the encodings of which are extended with the REX.R, REX.X
1345 ;;; and REX.B bit, respectively. To determine whether one of the byte
1346 ;;; registers is used that can only be accessed using a REX prefix, we
1347 ;;; need only to test R and B, because X is only used for the index
1348 ;;; register of an effective address and therefore never byte-sized.
1349 ;;; For R we can avoid to calculate the size of the TN because it is
1350 ;;; always OPERAND-SIZE. The size of B must be calculated here because
1351 ;;; B can be address-sized (if it is the base register of an effective
1352 ;;; address), of OPERAND-SIZE (if the instruction operates on two
1353 ;;; registers) or of some different size (in the instructions that
1354 ;;; combine arguments of different sizes: MOVZX, MOVSX, MOVSXD and
1355 ;;; several SSE instructions, e.g. CVTSD2SI). We don't distinguish
1356 ;;; between general-purpose and floating point registers for this cause
1357 ;;; because only general-purpose registers can be byte-sized at all.
1358 (defun maybe-emit-rex-prefix (segment operand-size r x b)
1359 (declare (type (member nil :byte :word :dword :qword :do-not-set)
1361 (type (or null tn) r x b))
1363 (if (and r (> (tn-offset r)
1364 ;; offset of r8 is 16, offset of xmm8 is 8
1365 (if (eq (sb-name (sc-sb (tn-sc r)))
1372 ;; Assuming R is a TN describing a general-purpose
1373 ;; register, return true if it references register
1375 (<= 8 (tn-offset r) 15)))
1376 (let ((rex-w (if (eq operand-size :qword) 1 0))
1380 (when (or (not (zerop (logior rex-w rex-r rex-x rex-b)))
1382 (eq operand-size :byte)
1385 (eq (operand-size b) :byte)
1387 (emit-rex-byte segment #b0100 rex-w rex-r rex-x rex-b)))))
1389 ;;; Emit a REX prefix if necessary. The operand size is determined from
1390 ;;; THING or can be overwritten by OPERAND-SIZE. This and REG are always
1391 ;;; passed to MAYBE-EMIT-REX-PREFIX. Additionally, if THING is an EA we
1392 ;;; pass its index and base registers, if it is a register TN, we pass
1394 ;;; In contrast to EMIT-EA above, neither stack TNs nor fixups need to
1395 ;;; be treated specially here: If THING is a stack TN, neither it nor
1396 ;;; any of its components are passed to MAYBE-EMIT-REX-PREFIX which
1397 ;;; works correctly because stack references always use RBP as the base
1398 ;;; register and never use an index register so no extended registers
1399 ;;; need to be accessed. Fixups are assembled using an addressing mode
1400 ;;; of displacement-only or RIP-plus-displacement (see EMIT-EA), so may
1401 ;;; not reference an extended register. The displacement-only addressing
1402 ;;; mode requires that REX.X is 0, which is ensured here.
1403 (defun maybe-emit-rex-for-ea (segment thing reg &key operand-size)
1404 (declare (type (or ea tn fixup) thing)
1405 (type (or null tn) reg)
1406 (type (member nil :byte :word :dword :qword :do-not-set)
1408 (let ((ea-p (ea-p thing)))
1409 (maybe-emit-rex-prefix segment
1410 (or operand-size (operand-size thing))
1412 (and ea-p (ea-index thing))
1413 (cond (ea-p (ea-base thing))
1415 (member (sb-name (sc-sb (tn-sc thing)))
1416 '(float-registers registers)))
1420 (defun operand-size (thing)
1423 ;; FIXME: might as well be COND instead of having to use #. readmacro
1424 ;; to hack up the code
1425 (case (sc-name (tn-sc thing))
1434 ;; added by jrd: float-registers is a separate size (?)
1435 ;; The only place in the code where we are called with THING
1436 ;; being a float-register is in MAYBE-EMIT-REX-PREFIX when it
1437 ;; checks whether THING is a byte register. Thus our result in
1438 ;; these cases could as well be :dword and :qword. I leave it as
1439 ;; :float and :double which is more likely to trigger an aver
1440 ;; instead of silently doing the wrong thing in case this
1441 ;; situation should change. Lutz Euler, 2005-10-23.
1444 (#.*double-sc-names*
1446 (#.*complex-sc-names*
1449 (error "can't tell the size of ~S ~S" thing (sc-name (tn-sc thing))))))
1453 ;; GNA. Guess who spelt "flavor" correctly first time round?
1454 ;; There's a strong argument in my mind to change all uses of
1455 ;; "flavor" to "kind": and similarly with some misguided uses of
1456 ;; "type" here and there. -- CSR, 2005-01-06.
1457 (case (fixup-flavor thing)
1458 ((:foreign-dataref) :qword)))
1462 (defun matching-operand-size (dst src)
1463 (let ((dst-size (operand-size dst))
1464 (src-size (operand-size src)))
1467 (if (eq dst-size src-size)
1469 (error "size mismatch: ~S is a ~S and ~S is a ~S."
1470 dst dst-size src src-size))
1474 (error "can't tell the size of either ~S or ~S" dst src)))))
1476 ;;; Except in a very few cases (MOV instructions A1, A3 and B8 - BF)
1477 ;;; we expect dword data bytes even when 64 bit work is being done.
1478 ;;; But A1 and A3 are currently unused and B8 - BF use EMIT-QWORD
1479 ;;; directly, so we emit all quad constants as dwords, additionally
1480 ;;; making sure that they survive the sign-extension to 64 bits
1482 (defun emit-sized-immediate (segment size value)
1485 (emit-byte segment value))
1487 (emit-word segment value))
1489 (emit-dword segment value))
1491 (emit-signed-dword segment value))))
1495 (define-instruction rex (segment)
1496 (:printer rex () nil :print-name nil)
1498 (bug "REX prefix used as a standalone instruction")))
1500 (define-instruction x66 (segment)
1501 (:printer x66 () nil :print-name nil)
1503 (bug "#X66 prefix used as a standalone instruction")))
1505 (defun emit-prefix (segment name)
1506 (declare (ignorable segment))
1511 (emit-byte segment #xf0))))
1513 (define-instruction lock (segment)
1514 (:printer byte ((op #b11110000)) nil)
1516 (bug "LOCK prefix used as a standalone instruction")))
1518 (define-instruction rep (segment)
1520 (emit-byte segment #b11110011)))
1522 (define-instruction repe (segment)
1523 (:printer byte ((op #b11110011)) nil)
1525 (emit-byte segment #b11110011)))
1527 (define-instruction repne (segment)
1528 (:printer byte ((op #b11110010)) nil)
1530 (emit-byte segment #b11110010)))
1532 ;;;; general data transfer
1534 ;;; This is the part of the MOV instruction emitter that does moving
1535 ;;; of an immediate value into a qword register. We go to some length
1536 ;;; to achieve the shortest possible encoding.
1537 (defun emit-immediate-move-to-qword-register (segment dst src)
1538 (declare (type integer src))
1539 (cond ((typep src '(unsigned-byte 32))
1540 ;; We use the B8 - BF encoding with an operand size of 32 bits
1541 ;; here and let the implicit zero-extension fill the upper half
1542 ;; of the 64-bit destination register. Instruction size: five
1543 ;; or six bytes. (A REX prefix will be emitted only if the
1544 ;; destination is an extended register.)
1545 (maybe-emit-rex-prefix segment :dword nil nil dst)
1546 (emit-byte-with-reg segment #b10111 (reg-tn-encoding dst))
1547 (emit-dword segment src))
1549 (maybe-emit-rex-prefix segment :qword nil nil dst)
1550 (cond ((typep src '(signed-byte 32))
1551 ;; Use the C7 encoding that takes a 32-bit immediate and
1552 ;; sign-extends it to 64 bits. Instruction size: seven
1554 (emit-byte segment #b11000111)
1555 (emit-mod-reg-r/m-byte segment #b11 #b000
1556 (reg-tn-encoding dst))
1557 (emit-signed-dword segment src))
1558 ((<= (- (expt 2 64) (expt 2 31))
1561 ;; This triggers on positive integers of 64 bits length
1562 ;; with the most significant 33 bits being 1. We use the
1563 ;; same encoding as in the previous clause.
1564 (emit-byte segment #b11000111)
1565 (emit-mod-reg-r/m-byte segment #b11 #b000
1566 (reg-tn-encoding dst))
1567 (emit-signed-dword segment (- src (expt 2 64))))
1569 ;; We need a full 64-bit immediate. Instruction size:
1571 (emit-byte-with-reg segment #b10111 (reg-tn-encoding dst))
1572 (emit-qword segment src))))))
1574 (define-instruction mov (segment dst src)
1575 ;; immediate to register
1576 (:printer reg ((op #b1011) (imm nil :type 'signed-imm-data))
1577 '(:name :tab reg ", " imm))
1578 (:printer rex-reg ((op #b1011) (imm nil :type 'signed-imm-data-upto-qword))
1579 '(:name :tab reg ", " imm))
1580 ;; absolute mem to/from accumulator
1581 (:printer simple-dir ((op #b101000) (imm nil :type 'imm-addr))
1582 `(:name :tab ,(swap-if 'dir 'accum ", " '("[" imm "]"))))
1583 ;; register to/from register/memory
1584 (:printer reg-reg/mem-dir ((op #b100010)))
1585 ;; immediate to register/memory
1586 (:printer reg/mem-imm ((op '(#b1100011 #b000))))
1589 (let ((size (matching-operand-size dst src)))
1590 (maybe-emit-operand-size-prefix segment size)
1591 (cond ((register-p dst)
1592 (cond ((integerp src)
1593 (cond ((eq size :qword)
1594 (emit-immediate-move-to-qword-register segment
1597 (maybe-emit-rex-prefix segment size nil nil dst)
1598 (emit-byte-with-reg segment
1602 (reg-tn-encoding dst))
1603 (emit-sized-immediate segment size src))))
1605 (maybe-emit-rex-for-ea segment src dst)
1610 (emit-ea segment src (reg-tn-encoding dst) :allow-constants t))))
1612 ;; C7 only deals with 32 bit immediates even if the
1613 ;; destination is a 64-bit location. The value is
1614 ;; sign-extended in this case.
1615 (maybe-emit-rex-for-ea segment dst nil)
1616 (emit-byte segment (if (eq size :byte) #b11000110 #b11000111))
1617 (emit-ea segment dst #b000)
1618 (emit-sized-immediate segment size src))
1620 (maybe-emit-rex-for-ea segment dst src)
1621 (emit-byte segment (if (eq size :byte) #b10001000 #b10001001))
1622 (emit-ea segment dst (reg-tn-encoding src)))
1624 ;; Generally we can't MOV a fixupped value into an EA, since
1625 ;; MOV on non-registers can only take a 32-bit immediate arg.
1626 ;; Make an exception for :FOREIGN fixups (pretty much just
1627 ;; the runtime asm, since other foreign calls go through the
1628 ;; the linkage table) and for linkage table references, since
1629 ;; these should always end up in low memory.
1630 (aver (or (eq (fixup-flavor src) :foreign)
1631 (eq (fixup-flavor src) :foreign-dataref)
1632 (eq (ea-size dst) :dword)))
1633 (maybe-emit-rex-for-ea segment dst nil)
1634 (emit-byte segment #b11000111)
1635 (emit-ea segment dst #b000)
1636 (emit-absolute-fixup segment src))
1638 (error "bogus arguments to MOV: ~S ~S" dst src))))))
1640 (defun emit-move-with-extension (segment dst src signed-p)
1641 (aver (register-p dst))
1642 (let ((dst-size (operand-size dst))
1643 (src-size (operand-size src))
1644 (opcode (if signed-p #b10111110 #b10110110)))
1647 (aver (eq src-size :byte))
1648 (maybe-emit-operand-size-prefix segment :word)
1649 ;; REX prefix is needed if SRC is SIL, DIL, SPL or BPL.
1650 (maybe-emit-rex-for-ea segment src dst :operand-size :word)
1651 (emit-byte segment #b00001111)
1652 (emit-byte segment opcode)
1653 (emit-ea segment src (reg-tn-encoding dst)))
1657 (maybe-emit-rex-for-ea segment src dst :operand-size dst-size)
1658 (emit-byte segment #b00001111)
1659 (emit-byte segment opcode)
1660 (emit-ea segment src (reg-tn-encoding dst)))
1662 (maybe-emit-rex-for-ea segment src dst :operand-size dst-size)
1663 (emit-byte segment #b00001111)
1664 (emit-byte segment (logior opcode 1))
1665 (emit-ea segment src (reg-tn-encoding dst)))
1667 (aver (eq dst-size :qword))
1668 ;; dst is in reg, src is in modrm
1669 (let ((ea-p (ea-p src)))
1670 (maybe-emit-rex-prefix segment (if signed-p :qword :dword) dst
1671 (and ea-p (ea-index src))
1672 (cond (ea-p (ea-base src))
1675 (emit-byte segment (if signed-p #x63 #x8b)) ;movsxd or straight mov
1676 ;;(emit-byte segment opcode)
1677 (emit-ea segment src (reg-tn-encoding dst)))))))))
1679 (define-instruction movsx (segment dst src)
1680 (:printer ext-reg-reg/mem-no-width
1681 ((op #b10111110) (reg/mem nil :type 'sized-byte-reg/mem)))
1682 (:printer ext-reg-reg/mem-no-width
1683 ((op #b10111111) (reg/mem nil :type 'sized-word-reg/mem)))
1684 (:emitter (emit-move-with-extension segment dst src :signed)))
1686 (define-instruction movzx (segment dst src)
1687 (:printer ext-reg-reg/mem-no-width
1688 ((op #b10110110) (reg/mem nil :type 'sized-byte-reg/mem)))
1689 (:printer ext-reg-reg/mem-no-width
1690 ((op #b10110111) (reg/mem nil :type 'sized-word-reg/mem)))
1691 (:emitter (emit-move-with-extension segment dst src nil)))
1693 ;;; The regular use of MOVSXD is with an operand size of :qword. This
1694 ;;; sign-extends the dword source into the qword destination register.
1695 ;;; If the operand size is :dword the instruction zero-extends the dword
1696 ;;; source into the qword destination register, i.e. it does the same as
1697 ;;; a dword MOV into a register.
1698 (define-instruction movsxd (segment dst src)
1699 (:printer reg-reg/mem ((op #b0110001) (width 1)
1700 (reg/mem nil :type 'sized-dword-reg/mem)))
1701 (:emitter (emit-move-with-extension segment dst src :signed)))
1703 ;;; this is not a real amd64 instruction, of course
1704 (define-instruction movzxd (segment dst src)
1705 ; (:printer reg-reg/mem ((op #x63) (reg nil :type 'reg)))
1706 (:emitter (emit-move-with-extension segment dst src nil)))
1708 (define-instruction push (segment src)
1710 (:printer reg-no-width-default-qword ((op #b01010)))
1712 (:printer reg/mem-default-qword ((op '(#b11111111 #b110))))
1714 (:printer byte ((op #b01101010) (imm nil :type 'signed-imm-byte))
1716 (:printer byte ((op #b01101000)
1717 (imm nil :type 'signed-imm-data-default-qword))
1719 ;; ### segment registers?
1722 (cond ((integerp src)
1723 (cond ((<= -128 src 127)
1724 (emit-byte segment #b01101010)
1725 (emit-byte segment src))
1727 ;; A REX-prefix is not needed because the operand size
1728 ;; defaults to 64 bits. The size of the immediate is 32
1729 ;; bits and it is sign-extended.
1730 (emit-byte segment #b01101000)
1731 (emit-signed-dword segment src))))
1733 (let ((size (operand-size src)))
1734 (aver (or (eq size :qword) (eq size :word)))
1735 (maybe-emit-operand-size-prefix segment size)
1736 (maybe-emit-rex-for-ea segment src nil :operand-size :do-not-set)
1737 (cond ((register-p src)
1738 (emit-byte-with-reg segment #b01010 (reg-tn-encoding src)))
1740 (emit-byte segment #b11111111)
1741 (emit-ea segment src #b110 :allow-constants t))))))))
1743 (define-instruction pop (segment dst)
1744 (:printer reg-no-width-default-qword ((op #b01011)))
1745 (:printer reg/mem-default-qword ((op '(#b10001111 #b000))))
1747 (let ((size (operand-size dst)))
1748 (aver (or (eq size :qword) (eq size :word)))
1749 (maybe-emit-operand-size-prefix segment size)
1750 (maybe-emit-rex-for-ea segment dst nil :operand-size :do-not-set)
1751 (cond ((register-p dst)
1752 (emit-byte-with-reg segment #b01011 (reg-tn-encoding dst)))
1754 (emit-byte segment #b10001111)
1755 (emit-ea segment dst #b000))))))
1757 (define-instruction xchg (segment operand1 operand2)
1758 ;; Register with accumulator.
1759 (:printer reg-no-width ((op #b10010)) '(:name :tab accum ", " reg))
1760 ;; Register/Memory with Register.
1761 (:printer reg-reg/mem ((op #b1000011)))
1763 (let ((size (matching-operand-size operand1 operand2)))
1764 (maybe-emit-operand-size-prefix segment size)
1765 (labels ((xchg-acc-with-something (acc something)
1766 (if (and (not (eq size :byte)) (register-p something))
1768 (maybe-emit-rex-for-ea segment acc something)
1769 (emit-byte-with-reg segment
1771 (reg-tn-encoding something)))
1772 (xchg-reg-with-something acc something)))
1773 (xchg-reg-with-something (reg something)
1774 (maybe-emit-rex-for-ea segment something reg)
1775 (emit-byte segment (if (eq size :byte) #b10000110 #b10000111))
1776 (emit-ea segment something (reg-tn-encoding reg))))
1777 (cond ((accumulator-p operand1)
1778 (xchg-acc-with-something operand1 operand2))
1779 ((accumulator-p operand2)
1780 (xchg-acc-with-something operand2 operand1))
1781 ((register-p operand1)
1782 (xchg-reg-with-something operand1 operand2))
1783 ((register-p operand2)
1784 (xchg-reg-with-something operand2 operand1))
1786 (error "bogus args to XCHG: ~S ~S" operand1 operand2)))))))
1788 (define-instruction lea (segment dst src)
1789 (:printer reg-reg/mem ((op #b1000110) (width 1)))
1791 (aver (or (dword-reg-p dst) (qword-reg-p dst)))
1792 (maybe-emit-rex-for-ea segment src dst
1793 :operand-size :qword)
1794 (emit-byte segment #b10001101)
1795 (emit-ea segment src (reg-tn-encoding dst))))
1797 (define-instruction cmpxchg (segment dst src &optional prefix)
1798 ;; Register/Memory with Register.
1799 (:printer ext-reg-reg/mem ((op #b1011000)) '(:name :tab reg/mem ", " reg))
1801 (aver (register-p src))
1802 (emit-prefix segment prefix)
1803 (let ((size (matching-operand-size src dst)))
1804 (maybe-emit-operand-size-prefix segment size)
1805 (maybe-emit-rex-for-ea segment dst src)
1806 (emit-byte segment #b00001111)
1807 (emit-byte segment (if (eq size :byte) #b10110000 #b10110001))
1808 (emit-ea segment dst (reg-tn-encoding src)))))
1811 ;;;; flag control instructions
1813 ;;; CLC -- Clear Carry Flag.
1814 (define-instruction clc (segment)
1815 (:printer byte ((op #b11111000)))
1817 (emit-byte segment #b11111000)))
1819 ;;; CLD -- Clear Direction Flag.
1820 (define-instruction cld (segment)
1821 (:printer byte ((op #b11111100)))
1823 (emit-byte segment #b11111100)))
1825 ;;; CLI -- Clear Iterrupt Enable Flag.
1826 (define-instruction cli (segment)
1827 (:printer byte ((op #b11111010)))
1829 (emit-byte segment #b11111010)))
1831 ;;; CMC -- Complement Carry Flag.
1832 (define-instruction cmc (segment)
1833 (:printer byte ((op #b11110101)))
1835 (emit-byte segment #b11110101)))
1837 ;;; LAHF -- Load AH into flags.
1838 (define-instruction lahf (segment)
1839 (:printer byte ((op #b10011111)))
1841 (emit-byte segment #b10011111)))
1843 ;;; POPF -- Pop flags.
1844 (define-instruction popf (segment)
1845 (:printer byte ((op #b10011101)))
1847 (emit-byte segment #b10011101)))
1849 ;;; PUSHF -- push flags.
1850 (define-instruction pushf (segment)
1851 (:printer byte ((op #b10011100)))
1853 (emit-byte segment #b10011100)))
1855 ;;; SAHF -- Store AH into flags.
1856 (define-instruction sahf (segment)
1857 (:printer byte ((op #b10011110)))
1859 (emit-byte segment #b10011110)))
1861 ;;; STC -- Set Carry Flag.
1862 (define-instruction stc (segment)
1863 (:printer byte ((op #b11111001)))
1865 (emit-byte segment #b11111001)))
1867 ;;; STD -- Set Direction Flag.
1868 (define-instruction std (segment)
1869 (:printer byte ((op #b11111101)))
1871 (emit-byte segment #b11111101)))
1873 ;;; STI -- Set Interrupt Enable Flag.
1874 (define-instruction sti (segment)
1875 (:printer byte ((op #b11111011)))
1877 (emit-byte segment #b11111011)))
1881 (defun emit-random-arith-inst (name segment dst src opcode
1882 &optional allow-constants)
1883 (let ((size (matching-operand-size dst src)))
1884 (maybe-emit-operand-size-prefix segment size)
1887 (cond ((and (not (eq size :byte)) (<= -128 src 127))
1888 (maybe-emit-rex-for-ea segment dst nil)
1889 (emit-byte segment #b10000011)
1890 (emit-ea segment dst opcode :allow-constants allow-constants)
1891 (emit-byte segment src))
1892 ((accumulator-p dst)
1893 (maybe-emit-rex-for-ea segment dst nil)
1900 (emit-sized-immediate segment size src))
1902 (maybe-emit-rex-for-ea segment dst nil)
1903 (emit-byte segment (if (eq size :byte) #b10000000 #b10000001))
1904 (emit-ea segment dst opcode :allow-constants allow-constants)
1905 (emit-sized-immediate segment size src))))
1907 (maybe-emit-rex-for-ea segment dst src)
1911 (if (eq size :byte) #b00000000 #b00000001)))
1912 (emit-ea segment dst (reg-tn-encoding src) :allow-constants allow-constants))
1914 (maybe-emit-rex-for-ea segment src dst)
1918 (if (eq size :byte) #b00000010 #b00000011)))
1919 (emit-ea segment src (reg-tn-encoding dst) :allow-constants allow-constants))
1921 (error "bogus operands to ~A" name)))))
1923 (eval-when (:compile-toplevel :execute)
1924 (defun arith-inst-printer-list (subop)
1925 `((accum-imm ((op ,(dpb subop (byte 3 2) #b0000010))))
1926 (reg/mem-imm ((op (#b1000000 ,subop))))
1927 ;; The redundant encoding #x82 is invalid in 64-bit mode,
1928 ;; therefore we force WIDTH to 1.
1929 (reg/mem-imm ((op (#b1000001 ,subop)) (width 1)
1930 (imm nil :type signed-imm-byte)))
1931 (reg-reg/mem-dir ((op ,(dpb subop (byte 3 1) #b000000)))))))
1933 (define-instruction add (segment dst src &optional prefix)
1934 (:printer-list (arith-inst-printer-list #b000))
1936 (emit-prefix segment prefix)
1937 (emit-random-arith-inst "ADD" segment dst src #b000)))
1939 (define-instruction adc (segment dst src)
1940 (:printer-list (arith-inst-printer-list #b010))
1941 (:emitter (emit-random-arith-inst "ADC" segment dst src #b010)))
1943 (define-instruction sub (segment dst src)
1944 (:printer-list (arith-inst-printer-list #b101))
1945 (:emitter (emit-random-arith-inst "SUB" segment dst src #b101)))
1947 (define-instruction sbb (segment dst src)
1948 (:printer-list (arith-inst-printer-list #b011))
1949 (:emitter (emit-random-arith-inst "SBB" segment dst src #b011)))
1951 (define-instruction cmp (segment dst src)
1952 (:printer-list (arith-inst-printer-list #b111))
1953 (:emitter (emit-random-arith-inst "CMP" segment dst src #b111 t)))
1955 ;;; The one-byte encodings for INC and DEC are used as REX prefixes
1956 ;;; in 64-bit mode so we always use the two-byte form.
1957 (define-instruction inc (segment dst)
1958 (:printer reg/mem ((op '(#b1111111 #b000))))
1960 (let ((size (operand-size dst)))
1961 (maybe-emit-operand-size-prefix segment size)
1962 (maybe-emit-rex-for-ea segment dst nil)
1963 (emit-byte segment (if (eq size :byte) #b11111110 #b11111111))
1964 (emit-ea segment dst #b000))))
1966 (define-instruction dec (segment dst)
1967 (:printer reg/mem ((op '(#b1111111 #b001))))
1969 (let ((size (operand-size dst)))
1970 (maybe-emit-operand-size-prefix segment size)
1971 (maybe-emit-rex-for-ea segment dst nil)
1972 (emit-byte segment (if (eq size :byte) #b11111110 #b11111111))
1973 (emit-ea segment dst #b001))))
1975 (define-instruction neg (segment dst)
1976 (:printer reg/mem ((op '(#b1111011 #b011))))
1978 (let ((size (operand-size dst)))
1979 (maybe-emit-operand-size-prefix segment size)
1980 (maybe-emit-rex-for-ea segment dst nil)
1981 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
1982 (emit-ea segment dst #b011))))
1984 (define-instruction mul (segment dst src)
1985 (:printer accum-reg/mem ((op '(#b1111011 #b100))))
1987 (let ((size (matching-operand-size dst src)))
1988 (aver (accumulator-p dst))
1989 (maybe-emit-operand-size-prefix segment size)
1990 (maybe-emit-rex-for-ea segment src nil)
1991 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
1992 (emit-ea segment src #b100))))
1994 (define-instruction imul (segment dst &optional src1 src2)
1995 (:printer accum-reg/mem ((op '(#b1111011 #b101))))
1996 (:printer ext-reg-reg/mem-no-width ((op #b10101111)))
1997 (:printer reg-reg/mem ((op #b0110100) (width 1)
1998 (imm nil :type 'signed-imm-data))
1999 '(:name :tab reg ", " reg/mem ", " imm))
2000 (:printer reg-reg/mem ((op #b0110101) (width 1)
2001 (imm nil :type 'signed-imm-byte))
2002 '(:name :tab reg ", " reg/mem ", " imm))
2004 (flet ((r/m-with-immed-to-reg (reg r/m immed)
2005 (let* ((size (matching-operand-size reg r/m))
2006 (sx (and (not (eq size :byte)) (<= -128 immed 127))))
2007 (maybe-emit-operand-size-prefix segment size)
2008 (maybe-emit-rex-for-ea segment r/m reg)
2009 (emit-byte segment (if sx #b01101011 #b01101001))
2010 (emit-ea segment r/m (reg-tn-encoding reg))
2012 (emit-byte segment immed)
2013 (emit-sized-immediate segment size immed)))))
2015 (r/m-with-immed-to-reg dst src1 src2))
2018 (r/m-with-immed-to-reg dst dst src1)
2019 (let ((size (matching-operand-size dst src1)))
2020 (maybe-emit-operand-size-prefix segment size)
2021 (maybe-emit-rex-for-ea segment src1 dst)
2022 (emit-byte segment #b00001111)
2023 (emit-byte segment #b10101111)
2024 (emit-ea segment src1 (reg-tn-encoding dst)))))
2026 (let ((size (operand-size dst)))
2027 (maybe-emit-operand-size-prefix segment size)
2028 (maybe-emit-rex-for-ea segment dst nil)
2029 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2030 (emit-ea segment dst #b101)))))))
2032 (define-instruction div (segment dst src)
2033 (:printer accum-reg/mem ((op '(#b1111011 #b110))))
2035 (let ((size (matching-operand-size dst src)))
2036 (aver (accumulator-p dst))
2037 (maybe-emit-operand-size-prefix segment size)
2038 (maybe-emit-rex-for-ea segment src nil)
2039 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2040 (emit-ea segment src #b110))))
2042 (define-instruction idiv (segment dst src)
2043 (:printer accum-reg/mem ((op '(#b1111011 #b111))))
2045 (let ((size (matching-operand-size dst src)))
2046 (aver (accumulator-p dst))
2047 (maybe-emit-operand-size-prefix segment size)
2048 (maybe-emit-rex-for-ea segment src nil)
2049 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2050 (emit-ea segment src #b111))))
2052 (define-instruction bswap (segment dst)
2053 (:printer ext-reg-no-width ((op #b11001)))
2055 (let ((size (operand-size dst)))
2056 (maybe-emit-rex-prefix segment size nil nil dst)
2057 (emit-byte segment #x0f)
2058 (emit-byte-with-reg segment #b11001 (reg-tn-encoding dst)))))
2060 ;;; CBW -- Convert Byte to Word. AX <- sign_xtnd(AL)
2061 (define-instruction cbw (segment)
2062 (:printer x66-byte ((op #b10011000)))
2064 (maybe-emit-operand-size-prefix segment :word)
2065 (emit-byte segment #b10011000)))
2067 ;;; CWDE -- Convert Word To Double Word Extended. EAX <- sign_xtnd(AX)
2068 (define-instruction cwde (segment)
2069 (:printer byte ((op #b10011000)))
2071 (maybe-emit-operand-size-prefix segment :dword)
2072 (emit-byte segment #b10011000)))
2074 ;;; CDQE -- Convert Double Word To Quad Word Extended. RAX <- sign_xtnd(EAX)
2075 (define-instruction cdqe (segment)
2076 (:printer rex-byte ((op #b10011000)))
2078 (maybe-emit-rex-prefix segment :qword nil nil nil)
2079 (emit-byte segment #b10011000)))
2081 ;;; CWD -- Convert Word to Double Word. DX:AX <- sign_xtnd(AX)
2082 (define-instruction cwd (segment)
2083 (:printer x66-byte ((op #b10011001)))
2085 (maybe-emit-operand-size-prefix segment :word)
2086 (emit-byte segment #b10011001)))
2088 ;;; CDQ -- Convert Double Word to Quad Word. EDX:EAX <- sign_xtnd(EAX)
2089 (define-instruction cdq (segment)
2090 (:printer byte ((op #b10011001)))
2092 (maybe-emit-operand-size-prefix segment :dword)
2093 (emit-byte segment #b10011001)))
2095 ;;; CQO -- Convert Quad Word to Octaword. RDX:RAX <- sign_xtnd(RAX)
2096 (define-instruction cqo (segment)
2097 (:printer rex-byte ((op #b10011001)))
2099 (maybe-emit-rex-prefix segment :qword nil nil nil)
2100 (emit-byte segment #b10011001)))
2102 (define-instruction xadd (segment dst src &optional prefix)
2103 ;; Register/Memory with Register.
2104 (:printer ext-reg-reg/mem ((op #b1100000)) '(:name :tab reg/mem ", " reg))
2106 (aver (register-p src))
2107 (emit-prefix segment prefix)
2108 (let ((size (matching-operand-size src dst)))
2109 (maybe-emit-operand-size-prefix segment size)
2110 (maybe-emit-rex-for-ea segment dst src)
2111 (emit-byte segment #b00001111)
2112 (emit-byte segment (if (eq size :byte) #b11000000 #b11000001))
2113 (emit-ea segment dst (reg-tn-encoding src)))))
2118 (defun emit-shift-inst (segment dst amount opcode)
2119 (let ((size (operand-size dst)))
2120 (maybe-emit-operand-size-prefix segment size)
2121 (multiple-value-bind (major-opcode immed)
2123 (:cl (values #b11010010 nil))
2124 (1 (values #b11010000 nil))
2125 (t (values #b11000000 t)))
2126 (maybe-emit-rex-for-ea segment dst nil)
2128 (if (eq size :byte) major-opcode (logior major-opcode 1)))
2129 (emit-ea segment dst opcode)
2131 (emit-byte segment amount)))))
2133 (eval-when (:compile-toplevel :execute)
2134 (defun shift-inst-printer-list (subop)
2135 `((reg/mem ((op (#b1101000 ,subop)))
2136 (:name :tab reg/mem ", 1"))
2137 (reg/mem ((op (#b1101001 ,subop)))
2138 (:name :tab reg/mem ", " 'cl))
2139 (reg/mem-imm ((op (#b1100000 ,subop))
2140 (imm nil :type imm-byte))))))
2142 (define-instruction rol (segment dst amount)
2144 (shift-inst-printer-list #b000))
2146 (emit-shift-inst segment dst amount #b000)))
2148 (define-instruction ror (segment dst amount)
2150 (shift-inst-printer-list #b001))
2152 (emit-shift-inst segment dst amount #b001)))
2154 (define-instruction rcl (segment dst amount)
2156 (shift-inst-printer-list #b010))
2158 (emit-shift-inst segment dst amount #b010)))
2160 (define-instruction rcr (segment dst amount)
2162 (shift-inst-printer-list #b011))
2164 (emit-shift-inst segment dst amount #b011)))
2166 (define-instruction shl (segment dst amount)
2168 (shift-inst-printer-list #b100))
2170 (emit-shift-inst segment dst amount #b100)))
2172 (define-instruction shr (segment dst amount)
2174 (shift-inst-printer-list #b101))
2176 (emit-shift-inst segment dst amount #b101)))
2178 (define-instruction sar (segment dst amount)
2180 (shift-inst-printer-list #b111))
2182 (emit-shift-inst segment dst amount #b111)))
2184 (defun emit-double-shift (segment opcode dst src amt)
2185 (let ((size (matching-operand-size dst src)))
2186 (when (eq size :byte)
2187 (error "Double shifts can only be used with words."))
2188 (maybe-emit-operand-size-prefix segment size)
2189 (maybe-emit-rex-for-ea segment dst src)
2190 (emit-byte segment #b00001111)
2191 (emit-byte segment (dpb opcode (byte 1 3)
2192 (if (eq amt :cl) #b10100101 #b10100100)))
2193 (emit-ea segment dst (reg-tn-encoding src))
2194 (unless (eq amt :cl)
2195 (emit-byte segment amt))))
2197 (eval-when (:compile-toplevel :execute)
2198 (defun double-shift-inst-printer-list (op)
2199 `((ext-reg-reg/mem-no-width ((op ,(logior op #b100))
2200 (imm nil :type imm-byte)))
2201 (ext-reg-reg/mem-no-width ((op ,(logior op #b101)))
2202 (:name :tab reg/mem ", " reg ", " 'cl)))))
2204 (define-instruction shld (segment dst src amt)
2205 (:declare (type (or (member :cl) (mod 64)) amt))
2206 (:printer-list (double-shift-inst-printer-list #b10100000))
2208 (emit-double-shift segment #b0 dst src amt)))
2210 (define-instruction shrd (segment dst src amt)
2211 (:declare (type (or (member :cl) (mod 64)) amt))
2212 (:printer-list (double-shift-inst-printer-list #b10101000))
2214 (emit-double-shift segment #b1 dst src amt)))
2216 (define-instruction and (segment dst src)
2218 (arith-inst-printer-list #b100))
2220 (emit-random-arith-inst "AND" segment dst src #b100)))
2222 (define-instruction test (segment this that)
2223 (:printer accum-imm ((op #b1010100)))
2224 (:printer reg/mem-imm ((op '(#b1111011 #b000))))
2225 (:printer reg-reg/mem ((op #b1000010)))
2227 (let ((size (matching-operand-size this that)))
2228 (maybe-emit-operand-size-prefix segment size)
2229 (flet ((test-immed-and-something (immed something)
2230 (cond ((accumulator-p something)
2231 (maybe-emit-rex-for-ea segment something nil)
2233 (if (eq size :byte) #b10101000 #b10101001))
2234 (emit-sized-immediate segment size immed))
2236 (maybe-emit-rex-for-ea segment something nil)
2238 (if (eq size :byte) #b11110110 #b11110111))
2239 (emit-ea segment something #b000)
2240 (emit-sized-immediate segment size immed))))
2241 (test-reg-and-something (reg something)
2242 (maybe-emit-rex-for-ea segment something reg)
2243 (emit-byte segment (if (eq size :byte) #b10000100 #b10000101))
2244 (emit-ea segment something (reg-tn-encoding reg))))
2245 (cond ((integerp that)
2246 (test-immed-and-something that this))
2248 (test-immed-and-something this that))
2250 (test-reg-and-something this that))
2252 (test-reg-and-something that this))
2254 (error "bogus operands for TEST: ~S and ~S" this that)))))))
2256 (define-instruction or (segment dst src)
2258 (arith-inst-printer-list #b001))
2260 (emit-random-arith-inst "OR" segment dst src #b001)))
2262 (define-instruction xor (segment dst src)
2264 (arith-inst-printer-list #b110))
2266 (emit-random-arith-inst "XOR" segment dst src #b110)))
2268 (define-instruction not (segment dst)
2269 (:printer reg/mem ((op '(#b1111011 #b010))))
2271 (let ((size (operand-size dst)))
2272 (maybe-emit-operand-size-prefix segment size)
2273 (maybe-emit-rex-for-ea segment dst nil)
2274 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2275 (emit-ea segment dst #b010))))
2277 ;;;; string manipulation
2279 (define-instruction cmps (segment size)
2280 (:printer string-op ((op #b1010011)))
2282 (maybe-emit-operand-size-prefix segment size)
2283 (maybe-emit-rex-prefix segment size nil nil nil)
2284 (emit-byte segment (if (eq size :byte) #b10100110 #b10100111))))
2286 (define-instruction ins (segment acc)
2287 (:printer string-op ((op #b0110110)))
2289 (let ((size (operand-size acc)))
2290 (aver (accumulator-p acc))
2291 (maybe-emit-operand-size-prefix segment size)
2292 (maybe-emit-rex-prefix segment size nil nil nil)
2293 (emit-byte segment (if (eq size :byte) #b01101100 #b01101101)))))
2295 (define-instruction lods (segment acc)
2296 (:printer string-op ((op #b1010110)))
2298 (let ((size (operand-size acc)))
2299 (aver (accumulator-p acc))
2300 (maybe-emit-operand-size-prefix segment size)
2301 (maybe-emit-rex-prefix segment size nil nil nil)
2302 (emit-byte segment (if (eq size :byte) #b10101100 #b10101101)))))
2304 (define-instruction movs (segment size)
2305 (:printer string-op ((op #b1010010)))
2307 (maybe-emit-operand-size-prefix segment size)
2308 (maybe-emit-rex-prefix segment size nil nil nil)
2309 (emit-byte segment (if (eq size :byte) #b10100100 #b10100101))))
2311 (define-instruction outs (segment acc)
2312 (:printer string-op ((op #b0110111)))
2314 (let ((size (operand-size acc)))
2315 (aver (accumulator-p acc))
2316 (maybe-emit-operand-size-prefix segment size)
2317 (maybe-emit-rex-prefix segment size nil nil nil)
2318 (emit-byte segment (if (eq size :byte) #b01101110 #b01101111)))))
2320 (define-instruction scas (segment acc)
2321 (:printer string-op ((op #b1010111)))
2323 (let ((size (operand-size acc)))
2324 (aver (accumulator-p acc))
2325 (maybe-emit-operand-size-prefix segment size)
2326 (maybe-emit-rex-prefix segment size nil nil nil)
2327 (emit-byte segment (if (eq size :byte) #b10101110 #b10101111)))))
2329 (define-instruction stos (segment acc)
2330 (:printer string-op ((op #b1010101)))
2332 (let ((size (operand-size acc)))
2333 (aver (accumulator-p acc))
2334 (maybe-emit-operand-size-prefix segment size)
2335 (maybe-emit-rex-prefix segment size nil nil nil)
2336 (emit-byte segment (if (eq size :byte) #b10101010 #b10101011)))))
2338 (define-instruction xlat (segment)
2339 (:printer byte ((op #b11010111)))
2341 (emit-byte segment #b11010111)))
2344 ;;;; bit manipulation
2346 (define-instruction bsf (segment dst src)
2347 (:printer ext-reg-reg/mem-no-width ((op #b10111100)))
2349 (let ((size (matching-operand-size dst src)))
2350 (when (eq size :byte)
2351 (error "can't scan bytes: ~S" src))
2352 (maybe-emit-operand-size-prefix segment size)
2353 (maybe-emit-rex-for-ea segment src dst)
2354 (emit-byte segment #b00001111)
2355 (emit-byte segment #b10111100)
2356 (emit-ea segment src (reg-tn-encoding dst)))))
2358 (define-instruction bsr (segment dst src)
2359 (:printer ext-reg-reg/mem-no-width ((op #b10111101)))
2361 (let ((size (matching-operand-size dst src)))
2362 (when (eq size :byte)
2363 (error "can't scan bytes: ~S" src))
2364 (maybe-emit-operand-size-prefix segment size)
2365 (maybe-emit-rex-for-ea segment src dst)
2366 (emit-byte segment #b00001111)
2367 (emit-byte segment #b10111101)
2368 (emit-ea segment src (reg-tn-encoding dst)))))
2370 (defun emit-bit-test-and-mumble (segment src index opcode)
2371 (let ((size (operand-size src)))
2372 (when (eq size :byte)
2373 (error "can't scan bytes: ~S" src))
2374 (maybe-emit-operand-size-prefix segment size)
2375 (cond ((integerp index)
2376 (maybe-emit-rex-for-ea segment src nil)
2377 (emit-byte segment #b00001111)
2378 (emit-byte segment #b10111010)
2379 (emit-ea segment src opcode)
2380 (emit-byte segment index))
2382 (maybe-emit-rex-for-ea segment src index)
2383 (emit-byte segment #b00001111)
2384 (emit-byte segment (dpb opcode (byte 3 3) #b10000011))
2385 (emit-ea segment src (reg-tn-encoding index))))))
2387 (eval-when (:compile-toplevel :execute)
2388 (defun bit-test-inst-printer-list (subop)
2389 `((ext-reg/mem-imm ((op (#b1011101 ,subop))
2390 (reg/mem nil :type reg/mem)
2391 (imm nil :type imm-byte)
2393 (ext-reg-reg/mem ((op ,(dpb subop (byte 3 2) #b1000001))
2395 (:name :tab reg/mem ", " reg)))))
2397 (define-instruction bt (segment src index)
2398 (:printer-list (bit-test-inst-printer-list #b100))
2400 (emit-bit-test-and-mumble segment src index #b100)))
2402 (define-instruction btc (segment src index)
2403 (:printer-list (bit-test-inst-printer-list #b111))
2405 (emit-bit-test-and-mumble segment src index #b111)))
2407 (define-instruction btr (segment src index)
2408 (:printer-list (bit-test-inst-printer-list #b110))
2410 (emit-bit-test-and-mumble segment src index #b110)))
2412 (define-instruction bts (segment src index)
2413 (:printer-list (bit-test-inst-printer-list #b101))
2415 (emit-bit-test-and-mumble segment src index #b101)))
2418 ;;;; control transfer
2420 (define-instruction call (segment where)
2421 (:printer near-jump ((op #b11101000)))
2422 (:printer reg/mem-default-qword ((op '(#b11111111 #b010))))
2426 (emit-byte segment #b11101000) ; 32 bit relative
2427 (emit-back-patch segment
2429 (lambda (segment posn)
2430 (emit-signed-dword segment
2431 (- (label-position where)
2434 ;; There is no CALL rel64...
2435 (error "Cannot CALL a fixup: ~S" where))
2437 (maybe-emit-rex-for-ea segment where nil :operand-size :do-not-set)
2438 (emit-byte segment #b11111111)
2439 (emit-ea segment where #b010)))))
2441 (defun emit-byte-displacement-backpatch (segment target)
2442 (emit-back-patch segment
2444 (lambda (segment posn)
2445 (let ((disp (- (label-position target) (1+ posn))))
2446 (aver (<= -128 disp 127))
2447 (emit-byte segment disp)))))
2449 (define-instruction jmp (segment cond &optional where)
2450 ;; conditional jumps
2451 (:printer short-cond-jump ((op #b0111)) '('j cc :tab label))
2452 (:printer near-cond-jump () '('j cc :tab label))
2453 ;; unconditional jumps
2454 (:printer short-jump ((op #b1011)))
2455 (:printer near-jump ((op #b11101001)))
2456 (:printer reg/mem-default-qword ((op '(#b11111111 #b100))))
2461 (lambda (segment posn delta-if-after)
2462 (let ((disp (- (label-position where posn delta-if-after)
2464 (when (<= -128 disp 127)
2466 (dpb (conditional-opcode cond)
2469 (emit-byte-displacement-backpatch segment where)
2471 (lambda (segment posn)
2472 (let ((disp (- (label-position where) (+ posn 6))))
2473 (emit-byte segment #b00001111)
2475 (dpb (conditional-opcode cond)
2478 (emit-signed-dword segment disp)))))
2479 ((label-p (setq where cond))
2482 (lambda (segment posn delta-if-after)
2483 (let ((disp (- (label-position where posn delta-if-after)
2485 (when (<= -128 disp 127)
2486 (emit-byte segment #b11101011)
2487 (emit-byte-displacement-backpatch segment where)
2489 (lambda (segment posn)
2490 (let ((disp (- (label-position where) (+ posn 5))))
2491 (emit-byte segment #b11101001)
2492 (emit-signed-dword segment disp)))))
2494 (emit-byte segment #b11101001)
2495 (emit-relative-fixup segment where))
2497 (unless (or (ea-p where) (tn-p where))
2498 (error "don't know what to do with ~A" where))
2499 ;; near jump defaults to 64 bit
2500 ;; w-bit in rex prefix is unnecessary
2501 (maybe-emit-rex-for-ea segment where nil :operand-size :do-not-set)
2502 (emit-byte segment #b11111111)
2503 (emit-ea segment where #b100)))))
2505 (define-instruction ret (segment &optional stack-delta)
2506 (:printer byte ((op #b11000011)))
2507 (:printer byte ((op #b11000010) (imm nil :type 'imm-word-16))
2510 (cond ((and stack-delta (not (zerop stack-delta)))
2511 (emit-byte segment #b11000010)
2512 (emit-word segment stack-delta))
2514 (emit-byte segment #b11000011)))))
2516 (define-instruction jrcxz (segment target)
2517 (:printer short-jump ((op #b0011)))
2519 (emit-byte segment #b11100011)
2520 (emit-byte-displacement-backpatch segment target)))
2522 (define-instruction loop (segment target)
2523 (:printer short-jump ((op #b0010)))
2525 (emit-byte segment #b11100010) ; pfw this was 11100011, or jecxz!!!!
2526 (emit-byte-displacement-backpatch segment target)))
2528 (define-instruction loopz (segment target)
2529 (:printer short-jump ((op #b0001)))
2531 (emit-byte segment #b11100001)
2532 (emit-byte-displacement-backpatch segment target)))
2534 (define-instruction loopnz (segment target)
2535 (:printer short-jump ((op #b0000)))
2537 (emit-byte segment #b11100000)
2538 (emit-byte-displacement-backpatch segment target)))
2540 ;;;; conditional move
2541 (define-instruction cmov (segment cond dst src)
2542 (:printer cond-move ())
2544 (aver (register-p dst))
2545 (let ((size (matching-operand-size dst src)))
2546 (aver (or (eq size :word) (eq size :dword) (eq size :qword)))
2547 (maybe-emit-operand-size-prefix segment size))
2548 (maybe-emit-rex-for-ea segment src dst)
2549 (emit-byte segment #b00001111)
2550 (emit-byte segment (dpb (conditional-opcode cond) (byte 4 0) #b01000000))
2551 (emit-ea segment src (reg-tn-encoding dst))))
2553 ;;;; conditional byte set
2555 (define-instruction set (segment dst cond)
2556 (:printer cond-set ())
2558 (maybe-emit-rex-for-ea segment dst nil)
2559 (emit-byte segment #b00001111)
2560 (emit-byte segment (dpb (conditional-opcode cond) (byte 4 0) #b10010000))
2561 (emit-ea segment dst #b000)))
2565 (define-instruction enter (segment disp &optional (level 0))
2566 (:declare (type (unsigned-byte 16) disp)
2567 (type (unsigned-byte 8) level))
2568 (:printer enter-format ((op #b11001000)))
2570 (emit-byte segment #b11001000)
2571 (emit-word segment disp)
2572 (emit-byte segment level)))
2574 (define-instruction leave (segment)
2575 (:printer byte ((op #b11001001)))
2577 (emit-byte segment #b11001001)))
2579 ;;;; interrupt instructions
2581 (defun snarf-error-junk (sap offset &optional length-only)
2582 (let* ((length (sb!sys:sap-ref-8 sap offset))
2583 (vector (make-array length :element-type '(unsigned-byte 8))))
2584 (declare (type sb!sys:system-area-pointer sap)
2585 (type (unsigned-byte 8) length)
2586 (type (simple-array (unsigned-byte 8) (*)) vector))
2588 (values 0 (1+ length) nil nil))
2590 (sb!kernel:copy-ub8-from-system-area sap (1+ offset)
2592 (collect ((sc-offsets)
2594 (lengths 1) ; the length byte
2596 (error-number (sb!c:read-var-integer vector index)))
2599 (when (>= index length)
2601 (let ((old-index index))
2602 (sc-offsets (sb!c:read-var-integer vector index))
2603 (lengths (- index old-index))))
2604 (values error-number
2610 (defmacro break-cases (breaknum &body cases)
2611 (let ((bn-temp (gensym)))
2612 (collect ((clauses))
2613 (dolist (case cases)
2614 (clauses `((= ,bn-temp ,(car case)) ,@(cdr case))))
2615 `(let ((,bn-temp ,breaknum))
2616 (cond ,@(clauses))))))
2619 (defun break-control (chunk inst stream dstate)
2620 (declare (ignore inst))
2621 (flet ((nt (x) (if stream (sb!disassem:note x dstate))))
2622 ;; XXX: {BYTE,WORD}-IMM-CODE below is a macro defined by the
2623 ;; DEFINE-INSTRUCTION-FORMAT for {BYTE,WORD}-IMM above. Due to
2624 ;; the spectacular design for DEFINE-INSTRUCTION-FORMAT (involving
2625 ;; a call to EVAL in order to define the macros at compile-time
2626 ;; only) they do not even show up as symbols in the target core.
2627 (case #!-ud2-breakpoints (byte-imm-code chunk dstate)
2628 #!+ud2-breakpoints (word-imm-code chunk dstate)
2631 (sb!disassem:handle-break-args #'snarf-error-junk stream dstate))
2634 (sb!disassem:handle-break-args #'snarf-error-junk stream dstate))
2636 (nt "breakpoint trap"))
2637 (#.pending-interrupt-trap
2638 (nt "pending interrupt trap"))
2641 (#.fun-end-breakpoint-trap
2642 (nt "function end breakpoint trap"))
2643 (#.single-step-around-trap
2644 (nt "single-step trap (around)"))
2645 (#.single-step-before-trap
2646 (nt "single-step trap (before)")))))
2648 (define-instruction break (segment code)
2649 (:declare (type (unsigned-byte 8) code))
2650 #!-ud2-breakpoints (:printer byte-imm ((op #b11001100)) '(:name :tab code)
2651 :control #'break-control)
2652 #!+ud2-breakpoints (:printer word-imm ((op #b0000101100001111)) '(:name :tab code)
2653 :control #'break-control)
2655 #!-ud2-breakpoints (emit-byte segment #b11001100)
2656 ;; On darwin, trap handling via SIGTRAP is unreliable, therefore we
2657 ;; throw a sigill with 0x0b0f instead and check for this in the
2658 ;; SIGILL handler and pass it on to the sigtrap handler if
2660 #!+ud2-breakpoints (emit-word segment #b0000101100001111)
2661 (emit-byte segment code)))
2663 (define-instruction int (segment number)
2664 (:declare (type (unsigned-byte 8) number))
2665 (:printer byte-imm ((op #b11001101)))
2669 (emit-byte segment #b11001100))
2671 (emit-byte segment #b11001101)
2672 (emit-byte segment number)))))
2674 (define-instruction iret (segment)
2675 (:printer byte ((op #b11001111)))
2677 (emit-byte segment #b11001111)))
2679 ;;;; processor control
2681 (define-instruction hlt (segment)
2682 (:printer byte ((op #b11110100)))
2684 (emit-byte segment #b11110100)))
2686 (define-instruction nop (segment)
2687 (:printer byte ((op #b10010000)))
2689 (emit-byte segment #b10010000)))
2691 (define-instruction wait (segment)
2692 (:printer byte ((op #b10011011)))
2694 (emit-byte segment #b10011011)))
2697 ;;;; miscellaneous hackery
2699 (define-instruction byte (segment byte)
2701 (emit-byte segment byte)))
2703 (define-instruction word (segment word)
2705 (emit-word segment word)))
2707 (define-instruction dword (segment dword)
2709 (emit-dword segment dword)))
2711 (defun emit-header-data (segment type)
2712 (emit-back-patch segment
2714 (lambda (segment posn)
2718 (component-header-length))
2722 (define-instruction simple-fun-header-word (segment)
2724 (emit-header-data segment simple-fun-header-widetag)))
2726 (define-instruction lra-header-word (segment)
2728 (emit-header-data segment return-pc-header-widetag)))
2730 ;;;; Instructions required to do floating point operations using SSE
2732 ;; Return a one- or two-element list of printers for SSE instructions.
2733 ;; The one-element list is used in the cases where the REX prefix is
2734 ;; really a prefix and thus automatically supported, the two-element
2735 ;; list is used when the REX prefix is used in an infix position.
2736 (eval-when (:compile-toplevel :execute)
2737 (defun sse-inst-printer-list (inst-format-stem prefix opcode
2738 &key more-fields printer)
2739 (let ((fields `(,@(when prefix
2740 `((prefix ,prefix)))
2743 (inst-formats (if prefix
2744 (list (symbolicate "EXT-" inst-format-stem)
2745 (symbolicate "EXT-REX-" inst-format-stem))
2746 (list inst-format-stem))))
2747 (mapcar (lambda (inst-format)
2748 `(,inst-format ,fields ,@(when printer
2752 (defun emit-sse-inst (segment dst src prefix opcode
2753 &key operand-size (remaining-bytes 0))
2755 (emit-byte segment prefix))
2757 (maybe-emit-rex-for-ea segment src dst :operand-size operand-size)
2758 (maybe-emit-rex-for-ea segment src dst))
2759 (emit-byte segment #x0f)
2760 (emit-byte segment opcode)
2761 (emit-ea segment src (reg-tn-encoding dst) :remaining-bytes remaining-bytes))
2763 ;; 0110 0110:0000 1111:0111 00gg: 11 010 xmmreg:imm8
2765 (defun emit-sse-inst-with-imm (segment dst/src imm
2770 (emit-byte segment prefix))
2771 (maybe-emit-rex-prefix segment operand-size dst/src nil nil)
2772 (emit-byte segment #x0F)
2773 (emit-byte segment opcode)
2774 (emit-byte segment (logior (ash (logior #b11000 /i) 3)
2775 (reg-tn-encoding dst/src)))
2776 (emit-byte segment imm))
2779 ((define-imm-sse-instruction (name opcode /i)
2780 `(define-instruction ,name (segment dst/src imm)
2782 ',(sse-inst-printer-list 'xmm-imm #x66 opcode
2783 :more-fields `((/i ,/i))))
2785 (emit-sse-inst-with-imm segment dst/src imm
2787 :operand-size :do-not-set)))))
2788 (define-imm-sse-instruction pslldq #x73 7)
2789 (define-imm-sse-instruction psllw-imm #x71 6)
2790 (define-imm-sse-instruction pslld-imm #x72 6)
2791 (define-imm-sse-instruction psllq-imm #x73 6)
2793 (define-imm-sse-instruction psraw-imm #x71 4)
2794 (define-imm-sse-instruction psrad-imm #x72 4)
2796 (define-imm-sse-instruction psrldq #x73 3)
2797 (define-imm-sse-instruction psrlw-imm #x71 2)
2798 (define-imm-sse-instruction psrld-imm #x72 2)
2799 (define-imm-sse-instruction psrlq-imm #x73 2))
2801 ;;; Emit an SSE instruction that has an XMM register as the destination
2802 ;;; operand and for which the size of the operands is implicitly given
2803 ;;; by the instruction.
2804 (defun emit-regular-sse-inst (segment dst src prefix opcode
2805 &key (remaining-bytes 0))
2806 (aver (xmm-register-p dst))
2807 (emit-sse-inst segment dst src prefix opcode
2808 :operand-size :do-not-set
2809 :remaining-bytes remaining-bytes))
2811 ;;; Instructions having an XMM register as the destination operand
2812 ;;; and an XMM register or a memory location as the source operand.
2813 ;;; The operand size is implicitly given by the instruction.
2815 (macrolet ((define-regular-sse-inst (name prefix opcode)
2816 `(define-instruction ,name (segment dst src)
2818 ',(sse-inst-printer-list 'xmm-xmm/mem prefix opcode))
2820 (emit-regular-sse-inst segment dst src ,prefix ,opcode)))))
2822 (define-regular-sse-inst andpd #x66 #x54)
2823 (define-regular-sse-inst andps nil #x54)
2824 (define-regular-sse-inst andnpd #x66 #x55)
2825 (define-regular-sse-inst andnps nil #x55)
2826 (define-regular-sse-inst orpd #x66 #x56)
2827 (define-regular-sse-inst orps nil #x56)
2828 (define-regular-sse-inst pand #x66 #xdb)
2829 (define-regular-sse-inst pandn #x66 #xdf)
2830 (define-regular-sse-inst por #x66 #xeb)
2831 (define-regular-sse-inst pxor #x66 #xef)
2832 (define-regular-sse-inst xorpd #x66 #x57)
2833 (define-regular-sse-inst xorps nil #x57)
2835 (define-regular-sse-inst comisd #x66 #x2f)
2836 (define-regular-sse-inst comiss nil #x2f)
2837 (define-regular-sse-inst ucomisd #x66 #x2e)
2838 (define-regular-sse-inst ucomiss nil #x2e)
2839 ;; integer comparison
2840 (define-regular-sse-inst pcmpeqb #x66 #x74)
2841 (define-regular-sse-inst pcmpeqw #x66 #x75)
2842 (define-regular-sse-inst pcmpeqd #x66 #x76)
2843 (define-regular-sse-inst pcmpgtb #x66 #x64)
2844 (define-regular-sse-inst pcmpgtw #x66 #x65)
2845 (define-regular-sse-inst pcmpgtd #x66 #x66)
2847 (define-regular-sse-inst maxpd #x66 #x5f)
2848 (define-regular-sse-inst maxps nil #x5f)
2849 (define-regular-sse-inst maxsd #xf2 #x5f)
2850 (define-regular-sse-inst maxss #xf3 #x5f)
2851 (define-regular-sse-inst minpd #x66 #x5d)
2852 (define-regular-sse-inst minps nil #x5d)
2853 (define-regular-sse-inst minsd #xf2 #x5d)
2854 (define-regular-sse-inst minss #xf3 #x5d)
2856 (define-regular-sse-inst pmaxsw #x66 #xee)
2857 (define-regular-sse-inst pmaxub #x66 #xde)
2858 (define-regular-sse-inst pminsw #x66 #xea)
2859 (define-regular-sse-inst pminub #x66 #xda)
2861 (define-regular-sse-inst addpd #x66 #x58)
2862 (define-regular-sse-inst addps nil #x58)
2863 (define-regular-sse-inst addsd #xf2 #x58)
2864 (define-regular-sse-inst addss #xf3 #x58)
2865 (define-regular-sse-inst divpd #x66 #x5e)
2866 (define-regular-sse-inst divps nil #x5e)
2867 (define-regular-sse-inst divsd #xf2 #x5e)
2868 (define-regular-sse-inst divss #xf3 #x5e)
2869 (define-regular-sse-inst mulpd #x66 #x59)
2870 (define-regular-sse-inst mulps nil #x59)
2871 (define-regular-sse-inst mulsd #xf2 #x59)
2872 (define-regular-sse-inst mulss #xf3 #x59)
2873 (define-regular-sse-inst rcpps nil #x53)
2874 (define-regular-sse-inst rcpss #xf3 #x53)
2875 (define-regular-sse-inst rsqrtps nil #x52)
2876 (define-regular-sse-inst rsqrtss #xf3 #x52)
2877 (define-regular-sse-inst sqrtpd #x66 #x51)
2878 (define-regular-sse-inst sqrtps nil #x51)
2879 (define-regular-sse-inst sqrtsd #xf2 #x51)
2880 (define-regular-sse-inst sqrtss #xf3 #x51)
2881 (define-regular-sse-inst subpd #x66 #x5c)
2882 (define-regular-sse-inst subps nil #x5c)
2883 (define-regular-sse-inst subsd #xf2 #x5c)
2884 (define-regular-sse-inst subss #xf3 #x5c)
2885 (define-regular-sse-inst unpckhpd #x66 #x15)
2886 (define-regular-sse-inst unpckhps nil #x15)
2887 (define-regular-sse-inst unpcklpd #x66 #x14)
2888 (define-regular-sse-inst unpcklps nil #x14)
2889 ;; integer arithmetic
2890 (define-regular-sse-inst paddb #x66 #xfc)
2891 (define-regular-sse-inst paddw #x66 #xfd)
2892 (define-regular-sse-inst paddd #x66 #xfe)
2893 (define-regular-sse-inst paddq #x66 #xd4)
2894 (define-regular-sse-inst paddsb #x66 #xec)
2895 (define-regular-sse-inst paddsw #x66 #xed)
2896 (define-regular-sse-inst paddusb #x66 #xdc)
2897 (define-regular-sse-inst paddusw #x66 #xdd)
2898 (define-regular-sse-inst pavgb #x66 #xe0)
2899 (define-regular-sse-inst pavgw #x66 #xe3)
2900 (define-regular-sse-inst pmaddwd #x66 #xf5)
2901 (define-regular-sse-inst pmulhuw #x66 #xe4)
2902 (define-regular-sse-inst pmulhw #x66 #xe5)
2903 (define-regular-sse-inst pmullw #x66 #xd5)
2904 (define-regular-sse-inst pmuludq #x66 #xf4)
2905 (define-regular-sse-inst psadbw #x66 #xf6)
2906 (define-regular-sse-inst psllw #x66 #xf1)
2907 (define-regular-sse-inst pslld #x66 #xf2)
2908 (define-regular-sse-inst psllq #x66 #xf3)
2909 (define-regular-sse-inst psraw #x66 #xe1)
2910 (define-regular-sse-inst psrad #x66 #xe2)
2911 (define-regular-sse-inst psrlw #x66 #xd1)
2912 (define-regular-sse-inst psrld #x66 #xd2)
2913 (define-regular-sse-inst psrlq #x66 #xd3)
2914 (define-regular-sse-inst psubb #x66 #xf8)
2915 (define-regular-sse-inst psubw #x66 #xf9)
2916 (define-regular-sse-inst psubd #x66 #xfa)
2917 (define-regular-sse-inst psubq #x66 #xfb)
2918 (define-regular-sse-inst psubsb #x66 #xe8)
2919 (define-regular-sse-inst psubsw #x66 #xe9)
2920 (define-regular-sse-inst psubusb #x66 #xd8)
2921 (define-regular-sse-inst psubusw #x66 #xd9)
2923 (define-regular-sse-inst cvtdq2pd #xf3 #xe6)
2924 (define-regular-sse-inst cvtdq2ps nil #x5b)
2925 (define-regular-sse-inst cvtpd2dq #xf2 #xe6)
2926 (define-regular-sse-inst cvtpd2ps #x66 #x5a)
2927 (define-regular-sse-inst cvtps2dq #x66 #x5b)
2928 (define-regular-sse-inst cvtps2pd nil #x5a)
2929 (define-regular-sse-inst cvtsd2ss #xf2 #x5a)
2930 (define-regular-sse-inst cvtss2sd #xf3 #x5a)
2931 (define-regular-sse-inst cvttpd2dq #x66 #xe6)
2932 (define-regular-sse-inst cvttps2dq #xf3 #x5b)
2934 (define-regular-sse-inst packsswb #x66 #x63)
2935 (define-regular-sse-inst packssdw #x66 #x6b)
2936 (define-regular-sse-inst packuswb #x66 #x67)
2937 (define-regular-sse-inst punpckhbw #x66 #x68)
2938 (define-regular-sse-inst punpckhwd #x66 #x69)
2939 (define-regular-sse-inst punpckhdq #x66 #x6a)
2940 (define-regular-sse-inst punpckhqdq #x66 #x6d)
2941 (define-regular-sse-inst punpcklbw #x66 #x60)
2942 (define-regular-sse-inst punpcklwd #x66 #x61)
2943 (define-regular-sse-inst punpckldq #x66 #x62)
2944 (define-regular-sse-inst punpcklqdq #x66 #x6c))
2946 (macrolet ((define-xmm-shuffle-sse-inst (name prefix opcode n-bits radix)
2947 (let ((shuffle-pattern
2948 (intern (format nil "SSE-SHUFFLE-PATTERN-~D-~D"
2950 `(define-instruction ,name (segment dst src pattern)
2952 ',(sse-inst-printer-list
2953 'xmm-xmm/mem prefix opcode
2954 :more-fields `((imm nil :type ,shuffle-pattern))
2955 :printer '(:name :tab reg ", " reg/mem ", " imm)))
2958 (aver (typep pattern '(unsigned-byte ,n-bits)))
2959 (emit-regular-sse-inst segment dst src ,prefix ,opcode
2961 (emit-byte segment pattern))))))
2962 (define-xmm-shuffle-sse-inst pshufd #x66 #x70 8 4)
2963 (define-xmm-shuffle-sse-inst pshufhw #xf3 #x70 8 4)
2964 (define-xmm-shuffle-sse-inst pshuflw #xf2 #x70 8 4)
2965 (define-xmm-shuffle-sse-inst shufpd #x66 #xc6 2 2)
2966 (define-xmm-shuffle-sse-inst shufps nil #xc6 8 4))
2968 ;; MASKMOVDQU (dst is DS:RDI)
2969 (define-instruction maskmovdqu (segment src mask)
2971 (sse-inst-printer-list 'xmm-xmm/mem #x66 #xf7))
2973 (aver (xmm-register-p src))
2974 (aver (xmm-register-p mask))
2975 (emit-regular-sse-inst segment src mask #x66 #xf7)))
2977 (macrolet ((define-comparison-sse-inst (name prefix opcode
2978 name-prefix name-suffix)
2979 `(define-instruction ,name (segment op x y)
2981 ',(sse-inst-printer-list
2982 'xmm-xmm/mem prefix opcode
2983 :more-fields '((imm nil :type sse-condition-code))
2984 :printer `(,name-prefix imm ,name-suffix
2985 :tab reg ", " reg/mem)))
2987 (let ((code (position op *sse-conditions*)))
2989 (emit-regular-sse-inst segment x y ,prefix ,opcode
2991 (emit-byte segment code))))))
2992 (define-comparison-sse-inst cmppd #x66 #xc2 "CMP" "PD")
2993 (define-comparison-sse-inst cmpps nil #xc2 "CMP" "PS")
2994 (define-comparison-sse-inst cmpsd #xf2 #xc2 "CMP" "SD")
2995 (define-comparison-sse-inst cmpss #xf3 #xc2 "CMP" "SS"))
2998 (macrolet ((define-movsd/ss-sse-inst (name prefix)
2999 `(define-instruction ,name (segment dst src)
3001 ',(sse-inst-printer-list 'xmm-xmm/mem-dir
3004 (cond ((xmm-register-p dst)
3005 (emit-sse-inst segment dst src ,prefix #x10
3006 :operand-size :do-not-set))
3008 (aver (xmm-register-p src))
3009 (emit-sse-inst segment src dst ,prefix #x11
3010 :operand-size :do-not-set)))))))
3011 (define-movsd/ss-sse-inst movsd #xf2)
3012 (define-movsd/ss-sse-inst movss #xf3))
3015 (macrolet ((define-mov-sse-inst (name prefix opcode-from opcode-to
3016 &key force-to-mem reg-reg-name)
3019 `(define-instruction ,reg-reg-name (segment dst src)
3021 (aver (xmm-register-p dst))
3022 (aver (xmm-register-p src))
3023 (emit-regular-sse-inst segment dst src
3024 ,prefix ,opcode-from))))
3025 (define-instruction ,name (segment dst src)
3027 '(,@(when opcode-from
3028 (sse-inst-printer-list
3029 'xmm-xmm/mem prefix opcode-from))
3030 ,@(sse-inst-printer-list
3031 'xmm-xmm/mem prefix opcode-to
3032 :printer '(:name :tab reg/mem ", " reg))))
3034 (cond ,@(when opcode-from
3035 `(((xmm-register-p dst)
3037 `(aver (not (or (register-p src)
3038 (xmm-register-p src)))))
3039 (emit-regular-sse-inst
3040 segment dst src ,prefix ,opcode-from))))
3042 (aver (xmm-register-p src))
3044 `(aver (not (or (register-p dst)
3045 (xmm-register-p dst)))))
3046 (emit-regular-sse-inst segment src dst
3047 ,prefix ,opcode-to))))))))
3049 (define-mov-sse-inst movapd #x66 #x28 #x29)
3050 (define-mov-sse-inst movaps nil #x28 #x29)
3051 (define-mov-sse-inst movdqa #x66 #x6f #x7f)
3052 (define-mov-sse-inst movdqu #xf3 #x6f #x7f)
3055 (define-mov-sse-inst movntdq #x66 nil #xe7 :force-to-mem t)
3056 (define-mov-sse-inst movntpd #x66 nil #x2b :force-to-mem t)
3057 (define-mov-sse-inst movntps nil nil #x2b :force-to-mem t)
3059 ;; use movhps for movlhps and movlps for movhlps
3060 (define-mov-sse-inst movhpd #x66 #x16 #x17 :force-to-mem t)
3061 (define-mov-sse-inst movhps nil #x16 #x17 :reg-reg-name movlhps)
3062 (define-mov-sse-inst movlpd #x66 #x12 #x13 :force-to-mem t)
3063 (define-mov-sse-inst movlps nil #x12 #x13 :reg-reg-name movhlps)
3064 (define-mov-sse-inst movupd #x66 #x10 #x11)
3065 (define-mov-sse-inst movups nil #x10 #x11))
3068 (define-instruction movq (segment dst src)
3071 (sse-inst-printer-list 'xmm-xmm/mem #xf3 #x7e)
3072 (sse-inst-printer-list 'xmm-xmm/mem #x66 #xd6
3073 :printer '(:name :tab reg/mem ", " reg))))
3075 (cond ((xmm-register-p dst)
3076 (emit-sse-inst segment dst src #xf3 #x7e
3077 :operand-size :do-not-set))
3079 (aver (xmm-register-p src))
3080 (emit-sse-inst segment src dst #x66 #xd6
3081 :operand-size :do-not-set)))))
3083 ;;; Instructions having an XMM register as the destination operand
3084 ;;; and a general-purpose register or a memory location as the source
3085 ;;; operand. The operand size is calculated from the source operand.
3087 ;;; MOVD - Move a 32- or 64-bit value from a general-purpose register or
3088 ;;; a memory location to the low order 32 or 64 bits of an XMM register
3089 ;;; with zero extension or vice versa.
3090 ;;; We do not support the MMX version of this instruction.
3091 (define-instruction movd (segment dst src)
3094 (sse-inst-printer-list 'xmm-reg/mem #x66 #x6e)
3095 (sse-inst-printer-list 'xmm-reg/mem #x66 #x7e
3096 :printer '(:name :tab reg/mem ", " reg))))
3098 (cond ((xmm-register-p dst)
3099 (emit-sse-inst segment dst src #x66 #x6e))
3101 (aver (xmm-register-p src))
3102 (emit-sse-inst segment src dst #x66 #x7e)))))
3104 (define-instruction pinsrw (segment dst src imm)
3106 (sse-inst-printer-list
3107 'xmm-reg/mem #x66 #xc4
3108 :more-fields '((imm nil :type imm-byte))
3109 :printer '(:name :tab reg ", " reg/mem ", " imm)))
3111 (aver (xmm-register-p dst))
3112 (let ((src-size (operand-size src)))
3113 (aver (or (not (register-p src))
3114 (eq src-size :qword) (eq src-size :dword)))
3115 (emit-sse-inst segment dst src #x66 #xc4
3116 :operand-size (if (register-p src) src-size :do-not-set)
3117 :remaining-bytes 1))
3118 (emit-byte segment imm)))
3120 (define-instruction pextrw (segment dst src imm)
3122 (sse-inst-printer-list
3123 'reg-xmm/mem #x66 #xc5
3124 :more-fields '((imm nil :type imm-byte))
3125 :printer '(:name :tab reg ", " reg/mem ", " imm)))
3127 (aver (xmm-register-p src))
3128 (aver (register-p dst))
3129 (let ((dst-size (operand-size dst)))
3130 (aver (or (eq dst-size :qword) (eq dst-size :dword)))
3131 (emit-sse-inst segment dst src #x66 #xc5
3132 :operand-size dst-size
3133 :remaining-bytes 1))
3134 (emit-byte segment imm)))
3136 (macrolet ((define-integer-source-sse-inst (name prefix opcode &key mem-only)
3137 `(define-instruction ,name (segment dst src)
3139 ',(sse-inst-printer-list 'xmm-reg/mem prefix opcode))
3141 (aver (xmm-register-p dst))
3143 `(aver (not (or (register-p src)
3144 (xmm-register-p src)))))
3145 (let ((src-size (operand-size src)))
3146 (aver (or (eq src-size :qword) (eq src-size :dword))))
3147 (emit-sse-inst segment dst src ,prefix ,opcode)))))
3148 (define-integer-source-sse-inst cvtsi2sd #xf2 #x2a)
3149 (define-integer-source-sse-inst cvtsi2ss #xf3 #x2a)
3150 ;; FIXME: memory operand is always a QWORD
3151 (define-integer-source-sse-inst cvtpi2pd #x66 #x2a :mem-only t)
3152 (define-integer-source-sse-inst cvtpi2ps nil #x2a :mem-only t))
3154 ;;; Instructions having a general-purpose register as the destination
3155 ;;; operand and an XMM register or a memory location as the source
3156 ;;; operand. The operand size is calculated from the destination
3159 (macrolet ((define-gpr-destination-sse-inst (name prefix opcode &key reg-only)
3160 `(define-instruction ,name (segment dst src)
3162 ',(sse-inst-printer-list 'reg-xmm/mem prefix opcode))
3164 (aver (register-p dst))
3166 `(aver (xmm-register-p src)))
3167 (let ((dst-size (operand-size dst)))
3168 (aver (or (eq dst-size :qword) (eq dst-size :dword)))
3169 (emit-sse-inst segment dst src ,prefix ,opcode
3170 :operand-size dst-size))))))
3171 (define-gpr-destination-sse-inst cvtsd2si #xf2 #x2d)
3172 (define-gpr-destination-sse-inst cvtss2si #xf3 #x2d)
3173 (define-gpr-destination-sse-inst cvttsd2si #xf2 #x2c)
3174 (define-gpr-destination-sse-inst cvttss2si #xf3 #x2c)
3175 (define-gpr-destination-sse-inst movmskpd #x66 #x50 :reg-only t)
3176 (define-gpr-destination-sse-inst movmskps nil #x50 :reg-only t)
3177 (define-gpr-destination-sse-inst pmovmskb #x66 #xd7 :reg-only t))
3179 ;;; Other SSE instructions
3181 ;; FIXME: is that right!?
3182 (define-instruction movnti (segment dst src)
3183 (:printer ext-reg-reg/mem-no-width ((op #xc3)))
3185 (aver (not (or (register-p dst)
3186 (xmm-register-p dst))))
3187 (aver (register-p src))
3188 (maybe-emit-rex-for-ea segment src dst)
3189 (emit-byte segment #x0f)
3190 (emit-byte segment #xc3)
3191 (emit-ea segment dst (reg-tn-encoding src))))
3193 (define-instruction prefetch (segment type src)
3194 (:printer ext-reg/mem-no-width ((op '(#x18 0)))
3195 '("PREFETCHNTA" :tab reg/mem))
3196 (:printer ext-reg/mem-no-width ((op '(#x18 1)))
3197 '("PREFETCHT0" :tab reg/mem))
3198 (:printer ext-reg/mem-no-width ((op '(#x18 2)))
3199 '("PREFETCHT1" :tab reg/mem))
3200 (:printer ext-reg/mem-no-width ((op '(#x18 3)))
3201 '("PREFETCHT2" :tab reg/mem))
3203 (aver (not (or (register-p src)
3204 (xmm-register-p src))))
3205 (aver (eq (operand-size src) :byte))
3206 (let ((type (position type #(:nta :t0 :t1 :t2))))
3208 (maybe-emit-rex-for-ea segment src nil)
3209 (emit-byte segment #x0f)
3210 (emit-byte segment #x18)
3211 (emit-ea segment src type))))
3213 (define-instruction clflush (segment src)
3214 (:printer ext-reg/mem-no-width ((op '(#xae 7))))
3216 (aver (not (or (register-p src)
3217 (xmm-register-p src))))
3218 (aver (eq (operand-size src) :byte))
3219 (maybe-emit-rex-for-ea segment src nil)
3220 (emit-byte segment #x0f)
3221 (emit-byte segment #xae)
3222 (emit-ea segment src 7)))
3224 (macrolet ((define-fence-instruction (name last-byte)
3225 `(define-instruction ,name (segment)
3226 (:printer three-bytes ((op '(#x0f #xae ,last-byte))))
3228 (emit-byte segment #x0f)
3229 (emit-byte segment #xae)
3230 (emit-byte segment ,last-byte)))))
3231 (define-fence-instruction lfence #b11101000)
3232 (define-fence-instruction mfence #b11110000)
3233 (define-fence-instruction sfence #b11111000))
3235 (define-instruction pause (segment)
3236 (:printer two-bytes ((op '(#xf3 #x90))))
3238 (emit-byte segment #xf3)
3239 (emit-byte segment #x90)))
3241 (define-instruction ldmxcsr (segment src)
3242 (:printer ext-reg/mem-no-width ((op '(#xae 2))))
3244 (aver (not (or (register-p src)
3245 (xmm-register-p src))))
3246 (aver (eq (operand-size src) :dword))
3247 (maybe-emit-rex-for-ea segment src nil)
3248 (emit-byte segment #x0f)
3249 (emit-byte segment #xae)
3250 (emit-ea segment src 2)))
3252 (define-instruction stmxcsr (segment dst)
3253 (:printer ext-reg/mem-no-width ((op '(#xae 3))))
3255 (aver (not (or (register-p dst)
3256 (xmm-register-p dst))))
3257 (aver (eq (operand-size dst) :dword))
3258 (maybe-emit-rex-for-ea segment dst nil)
3259 (emit-byte segment #x0f)
3260 (emit-byte segment #xae)
3261 (emit-ea segment dst 3)))
3265 (define-instruction cpuid (segment)
3266 (:printer two-bytes ((op '(#b00001111 #b10100010))))
3268 (emit-byte segment #b00001111)
3269 (emit-byte segment #b10100010)))
3271 (define-instruction rdtsc (segment)
3272 (:printer two-bytes ((op '(#b00001111 #b00110001))))
3274 (emit-byte segment #b00001111)
3275 (emit-byte segment #b00110001)))
3277 ;;;; Late VM definitions
3279 (defun canonicalize-inline-constant (constant &aux (alignedp nil))
3280 (let ((first (car constant)))
3281 (when (eql first :aligned)
3284 (setf first (car constant)))
3286 (single-float (setf constant (list :single-float first)))
3287 (double-float (setf constant (list :double-float first)))
3288 ((complex single-float)
3289 (setf constant (list :complex-single-float first)))
3290 ((complex double-float)
3291 (setf constant (list :complex-double-float first)))))
3292 (destructuring-bind (type value) constant
3294 ((:byte :word :dword :qword)
3295 (aver (integerp value))
3298 (aver (base-char-p value))
3299 (cons :byte (char-code value)))
3301 (aver (characterp value))
3302 (cons :dword (char-code value)))
3304 (aver (typep value 'single-float))
3305 (cons (if alignedp :oword :dword)
3306 (ldb (byte 32 0) (single-float-bits value))))
3308 (aver (typep value 'double-float))
3309 (cons (if alignedp :oword :qword)
3310 (ldb (byte 64 0) (logior (ash (double-float-high-bits value) 32)
3311 (double-float-low-bits value)))))
3312 ((:complex-single-float)
3313 (aver (typep value '(complex single-float)))
3314 (cons (if alignedp :oword :qword)
3316 (logior (ash (single-float-bits (imagpart value)) 32)
3318 (single-float-bits (realpart value)))))))
3320 (aver (integerp value))
3321 (cons :oword value))
3322 ((:complex-double-float)
3323 (aver (typep value '(complex double-float)))
3325 (logior (ash (double-float-high-bits (imagpart value)) 96)
3326 (ash (double-float-low-bits (imagpart value)) 64)
3327 (ash (ldb (byte 32 0)
3328 (double-float-high-bits (realpart value)))
3330 (double-float-low-bits (realpart value))))))))
3332 (defun inline-constant-value (constant)
3333 (let ((label (gen-label))
3334 (size (ecase (car constant)
3335 ((:byte :word :dword :qword) (car constant))
3336 ((:oword) :qword))))
3337 (values label (make-ea size
3338 :disp (make-fixup nil :code-object label)))))
3340 (defun emit-constant-segment-header (constants optimize)
3341 (declare (ignore constants))
3342 (loop repeat (if optimize 64 16) do (inst byte #x90)))
3344 (defun size-nbyte (size)
3352 (defun sort-inline-constants (constants)
3353 (stable-sort constants #'> :key (lambda (constant)
3354 (size-nbyte (caar constant)))))
3356 (defun emit-inline-constant (constant label)
3357 (let ((size (size-nbyte (car constant))))
3358 (emit-alignment (integer-length (1- size)))
3360 (let ((val (cdr constant)))
3362 do (inst byte (ldb (byte 8 0) val))
3363 (setf val (ash val -8))))))