Eliminate COLD-FSET. It's just fop-funcall of %DEFUN
[sbcl.git] / src / compiler / x86-64 / insts.lisp
blob4895b0d94fdaf46b840cb8c0578739fce0e41204
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
5 ;;;; more information.
6 ;;;;
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.
13 (in-package "SB!VM")
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*
50 #(ah ch dh bh))
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
63 ;;; symbols:
64 ;;;
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
69 ;;; found
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
75 ;;; stored in DSTATE.
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)
79 :byte)
80 ((sb!disassem:dstate-get-inst-prop dstate 'rex-w)
81 :qword)
82 ((sb!disassem:dstate-get-inst-prop dstate 'operand-size-16)
83 :word)
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)
93 :word
94 :qword))
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)
102 (type stream stream)
103 (type sb!disassem:disassem-state dstate))
104 (princ (if (and (eq width :byte)
105 (<= 4 value 7)
106 (not (sb!disassem:dstate-get-inst-prop dstate 'rex)))
107 (aref *high-byte-reg-names* (- value 4))
108 (aref (ecase width
109 (:byte *byte-reg-names*)
110 (:word *word-reg-names*)
111 (:dword *dword-reg-names*)
112 (:qword *qword-reg-names*))
113 value))
114 stream)
115 ;; XXX plus should do some source-var notes
118 (defun print-reg (value stream dstate)
119 (declare (type full-reg value)
120 (type stream stream)
121 (type sb!disassem:disassem-state dstate))
122 (print-reg-with-width value
123 (inst-operand-size dstate)
124 stream
125 dstate))
127 (defun print-reg-default-qword (value stream dstate)
128 (declare (type full-reg value)
129 (type stream stream)
130 (type sb!disassem:disassem-state dstate))
131 (print-reg-with-width value
132 (inst-operand-size-default-qword dstate)
133 stream
134 dstate))
136 (defun print-byte-reg (value stream dstate)
137 (declare (type full-reg value)
138 (type stream stream)
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)
144 (type stream stream)
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
150 ;;; references.
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)
155 (type stream stream)
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-ref (if sized-p :sized-ref :ref) value 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)
165 (type stream stream)
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)
174 (type stream stream)
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
180 ;;; :qword.
181 (defun print-sized-reg/mem-default-qword (value stream dstate)
182 (declare (type (or list full-reg) value)
183 (type stream stream)
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)
190 (type stream stream)
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)
196 (type stream stream)
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)
202 (type stream stream)
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)
212 (type stream stream)
213 (ignore dstate))
214 (format stream "XMM~d" value))
216 (defun print-xmmreg/mem (value stream dstate)
217 (declare (type (or list xmmreg) value)
218 (type stream stream)
219 (type sb!disassem:disassem-state dstate))
220 (if (typep value 'xmmreg)
221 (print-xmmreg value stream dstate)
222 (print-mem-ref :ref value nil stream dstate)))
224 ;;; This prefilter is used solely for its side effects, namely to put
225 ;;; the bits found in the REX prefix into the DSTATE for use by other
226 ;;; prefilters and by printers.
227 (defun prefilter-wrxb (value dstate)
228 (declare (type (unsigned-byte 4) value)
229 (type sb!disassem:disassem-state dstate))
230 (sb!disassem:dstate-put-inst-prop dstate 'rex)
231 (when (plusp (logand value #b1000))
232 (sb!disassem:dstate-put-inst-prop dstate 'rex-w))
233 (when (plusp (logand value #b0100))
234 (sb!disassem:dstate-put-inst-prop dstate 'rex-r))
235 (when (plusp (logand value #b0010))
236 (sb!disassem:dstate-put-inst-prop dstate 'rex-x))
237 (when (plusp (logand value #b0001))
238 (sb!disassem:dstate-put-inst-prop dstate 'rex-b))
239 value)
241 ;;; The two following prefilters are used instead of prefilter-wrxb when
242 ;;; the bits of the REX prefix need to be treated individually. They are
243 ;;; always used together, so only the first one sets the REX property.
244 (defun prefilter-rex-w (value dstate)
245 (declare (type bit value)
246 (type sb!disassem:disassem-state dstate))
247 (sb!disassem:dstate-put-inst-prop dstate 'rex)
248 (when (plusp value)
249 (sb!disassem:dstate-put-inst-prop dstate 'rex-w)))
250 (defun prefilter-rex-b (value dstate)
251 (declare (type bit value)
252 (type sb!disassem:disassem-state dstate))
253 (when (plusp value)
254 (sb!disassem:dstate-put-inst-prop dstate 'rex-b)))
256 ;;; This prefilter is used solely for its side effect, namely to put
257 ;;; the property OPERAND-SIZE-8 into the DSTATE if VALUE is 0.
258 (defun prefilter-width (value dstate)
259 (declare (type bit value)
260 (type sb!disassem:disassem-state dstate))
261 (when (zerop value)
262 (sb!disassem:dstate-put-inst-prop dstate 'operand-size-8))
263 value)
265 ;;; This prefilter is used solely for its side effect, namely to put
266 ;;; the property OPERAND-SIZE-16 into the DSTATE.
267 (defun prefilter-x66 (value dstate)
268 (declare (type (eql #x66) value)
269 (ignore value)
270 (type sb!disassem:disassem-state dstate))
271 (sb!disassem:dstate-put-inst-prop dstate 'operand-size-16))
273 ;;; A register field that can be extended by REX.R.
274 (defun prefilter-reg-r (value dstate)
275 (declare (type reg value)
276 (type sb!disassem:disassem-state dstate))
277 (if (sb!disassem::dstate-get-inst-prop dstate 'rex-r)
278 (+ value 8)
279 value))
281 ;;; A register field that can be extended by REX.B.
282 (defun prefilter-reg-b (value dstate)
283 (declare (type reg value)
284 (type sb!disassem:disassem-state dstate))
285 (if (sb!disassem::dstate-get-inst-prop dstate 'rex-b)
286 (+ value 8)
287 value))
289 ;;; Returns either an integer, meaning a register, or a list of
290 ;;; (BASE-REG OFFSET INDEX-REG INDEX-SCALE), where any component
291 ;;; may be missing or nil to indicate that it's not used or has the
292 ;;; obvious default value (e.g., 1 for the index-scale). VALUE is a list
293 ;;; of the mod and r/m field of the ModRM byte of the instruction.
294 ;;; Depending on VALUE a SIB byte and/or an offset may be read. The
295 ;;; REX.B bit from DSTATE is used to extend the sole register or the
296 ;;; BASE-REG to a full register, the REX.X bit does the same for the
297 ;;; INDEX-REG.
298 (defun prefilter-reg/mem (value dstate)
299 (declare (type list value)
300 (type sb!disassem:disassem-state dstate))
301 (flet ((extend (bit-name reg)
302 (logior (if (sb!disassem:dstate-get-inst-prop dstate bit-name) 8 0)
303 reg)))
304 (declare (inline extend))
305 (let* ((mod (the (unsigned-byte 2) (first value)))
306 (r/m (the (unsigned-byte 3) (second value)))
307 (full-reg (extend 'rex-b r/m)))
308 (cond ((= mod #b11)
309 ;; registers
310 full-reg)
311 ((= r/m #b100) ; SIB byte - rex.b is "don't care"
312 (let* ((sib (the (unsigned-byte 8)
313 (sb!disassem:read-suffix 8 dstate)))
314 (base-reg (ldb (byte 3 0) sib))
315 (index-reg (extend 'rex-x (ldb (byte 3 3) sib)))
316 (offset
317 (case mod
318 (#b00
319 (if (= base-reg #b101)
320 (sb!disassem:read-signed-suffix 32 dstate)
321 nil))
322 (#b01
323 (sb!disassem:read-signed-suffix 8 dstate))
324 (#b10
325 (sb!disassem:read-signed-suffix 32 dstate)))))
326 (list (unless (and (= mod #b00) (= base-reg #b101))
327 (extend 'rex-b base-reg))
328 offset
329 (unless (= index-reg #b100) index-reg) ; index can't be RSP
330 (ash 1 (ldb (byte 2 6) sib)))))
331 ;; rex.b is not decoded in determining RIP-relative mode
332 ((and (= mod #b00) (= r/m #b101))
333 (list 'rip (sb!disassem:read-signed-suffix 32 dstate)))
334 ((= mod #b00)
335 (list full-reg))
336 ((= mod #b01)
337 (list full-reg (sb!disassem:read-signed-suffix 8 dstate)))
338 (t ; (= mod #b10)
339 (list full-reg (sb!disassem:read-signed-suffix 32 dstate)))))))
341 (defun read-address (value dstate)
342 (declare (ignore value)) ; always nil anyway
343 (sb!disassem:read-suffix (width-bits (inst-operand-size dstate)) dstate))
345 (defun width-bits (width)
346 (ecase width
347 (:byte 8)
348 (:word 16)
349 (:dword 32)
350 (:qword 64)))
352 (defun print-imm/asm-routine (value stream dstate)
353 (sb!disassem:maybe-note-assembler-routine value nil dstate)
354 (sb!disassem:maybe-note-static-symbol value dstate)
355 (princ value stream))
356 ) ; EVAL-WHEN
358 ;;;; disassembler argument types
360 ;;; Used to capture the lower four bits of the REX prefix all at once ...
361 (sb!disassem:define-arg-type wrxb
362 :prefilter #'prefilter-wrxb)
363 ;;; ... or individually (not needed for REX.R and REX.X).
364 (sb!disassem:define-arg-type rex-w
365 :prefilter #'prefilter-rex-w)
366 (sb!disassem:define-arg-type rex-b
367 :prefilter #'prefilter-rex-b)
369 (sb!disassem:define-arg-type width
370 :prefilter #'prefilter-width
371 :printer (lambda (value stream dstate)
372 (declare (ignore value))
373 (princ (schar (symbol-name (inst-operand-size dstate)) 0)
374 stream)))
376 ;;; Used to capture the effect of the #x66 operand size override prefix.
377 (sb!disassem:define-arg-type x66
378 :prefilter #'prefilter-x66)
380 (sb!disassem:define-arg-type displacement
381 :sign-extend t
382 :use-label #'offset-next
383 :printer (lambda (value stream dstate)
384 (sb!disassem:maybe-note-assembler-routine value nil dstate)
385 (print-label value stream dstate)))
387 (sb!disassem:define-arg-type accum
388 :printer (lambda (value stream dstate)
389 (declare (ignore value)
390 (type stream stream)
391 (type sb!disassem:disassem-state dstate))
392 (print-reg 0 stream dstate)))
394 (sb!disassem:define-arg-type reg
395 :prefilter #'prefilter-reg-r
396 :printer #'print-reg)
398 (sb!disassem:define-arg-type reg-b
399 :prefilter #'prefilter-reg-b
400 :printer #'print-reg)
402 (sb!disassem:define-arg-type reg-b-default-qword
403 :prefilter #'prefilter-reg-b
404 :printer #'print-reg-default-qword)
406 (sb!disassem:define-arg-type imm-addr
407 :prefilter #'read-address
408 :printer #'print-label)
410 ;;; Normally, immediate values for an operand size of :qword are of size
411 ;;; :dword and are sign-extended to 64 bits. For an exception, see the
412 ;;; argument type definition of SIGNED-IMM-DATA-UPTO-QWORD below.
413 (sb!disassem:define-arg-type signed-imm-data
414 :prefilter (lambda (value dstate)
415 (declare (ignore value)) ; always nil anyway
416 (let ((width (width-bits (inst-operand-size dstate))))
417 (when (= width 64)
418 (setf width 32))
419 (sb!disassem:read-signed-suffix width dstate))))
421 (sb!disassem:define-arg-type signed-imm-data/asm-routine
422 :type 'signed-imm-data
423 :printer #'print-imm/asm-routine)
425 ;;; Used by the variant of the MOV instruction with opcode B8 which can
426 ;;; move immediates of all sizes (i.e. including :qword) into a
427 ;;; register.
428 (sb!disassem:define-arg-type signed-imm-data-upto-qword
429 :prefilter (lambda (value dstate)
430 (declare (ignore value)) ; always nil anyway
431 (sb!disassem:read-signed-suffix
432 (width-bits (inst-operand-size dstate))
433 dstate)))
435 (sb!disassem:define-arg-type signed-imm-data-upto-qword/asm-routine
436 :type 'signed-imm-data-upto-qword
437 :printer #'print-imm/asm-routine)
440 ;;; Used by those instructions that have a default operand size of
441 ;;; :qword. Nevertheless the immediate is at most of size :dword.
442 ;;; The only instruction of this kind having a variant with an immediate
443 ;;; argument is PUSH.
444 (sb!disassem:define-arg-type signed-imm-data-default-qword
445 :prefilter (lambda (value dstate)
446 (declare (ignore value)) ; always nil anyway
447 (let ((width (width-bits
448 (inst-operand-size-default-qword dstate))))
449 (when (= width 64)
450 (setf width 32))
451 (sb!disassem:read-signed-suffix width dstate))))
453 (sb!disassem:define-arg-type signed-imm-byte
454 :prefilter (lambda (value dstate)
455 (declare (ignore value)) ; always nil anyway
456 (sb!disassem:read-signed-suffix 8 dstate)))
458 (sb!disassem:define-arg-type imm-byte
459 :prefilter (lambda (value dstate)
460 (declare (ignore value)) ; always nil anyway
461 (sb!disassem:read-suffix 8 dstate)))
463 ;;; needed for the ret imm16 instruction
464 (sb!disassem:define-arg-type imm-word-16
465 :prefilter (lambda (value dstate)
466 (declare (ignore value)) ; always nil anyway
467 (sb!disassem:read-suffix 16 dstate)))
469 (sb!disassem:define-arg-type reg/mem
470 :prefilter #'prefilter-reg/mem
471 :printer #'print-reg/mem)
472 (sb!disassem:define-arg-type sized-reg/mem
473 ;; Same as reg/mem, but prints an explicit size indicator for
474 ;; memory references.
475 :prefilter #'prefilter-reg/mem
476 :printer #'print-sized-reg/mem)
478 ;;; Arguments of type reg/mem with a fixed size.
479 (sb!disassem:define-arg-type sized-byte-reg/mem
480 :prefilter #'prefilter-reg/mem
481 :printer #'print-sized-byte-reg/mem)
482 (sb!disassem:define-arg-type sized-word-reg/mem
483 :prefilter #'prefilter-reg/mem
484 :printer #'print-sized-word-reg/mem)
485 (sb!disassem:define-arg-type sized-dword-reg/mem
486 :prefilter #'prefilter-reg/mem
487 :printer #'print-sized-dword-reg/mem)
489 ;;; Same as sized-reg/mem, but with a default operand size of :qword.
490 (sb!disassem:define-arg-type sized-reg/mem-default-qword
491 :prefilter #'prefilter-reg/mem
492 :printer #'print-sized-reg/mem-default-qword)
494 ;;; XMM registers
495 (sb!disassem:define-arg-type xmmreg
496 :prefilter #'prefilter-reg-r
497 :printer #'print-xmmreg)
499 (sb!disassem:define-arg-type xmmreg-b
500 :prefilter #'prefilter-reg-b
501 :printer #'print-xmmreg)
503 (sb!disassem:define-arg-type xmmreg/mem
504 :prefilter #'prefilter-reg/mem
505 :printer #'print-xmmreg/mem)
508 (eval-when (:compile-toplevel :load-toplevel :execute)
509 (defparameter *conditions*
510 '((:o . 0)
511 (:no . 1)
512 (:b . 2) (:nae . 2) (:c . 2)
513 (:nb . 3) (:ae . 3) (:nc . 3)
514 (:eq . 4) (:e . 4) (:z . 4)
515 (:ne . 5) (:nz . 5)
516 (:be . 6) (:na . 6)
517 (:nbe . 7) (:a . 7)
518 (:s . 8)
519 (:ns . 9)
520 (:p . 10) (:pe . 10)
521 (:np . 11) (:po . 11)
522 (:l . 12) (:nge . 12)
523 (:nl . 13) (:ge . 13)
524 (:le . 14) (:ng . 14)
525 (:nle . 15) (:g . 15)))
526 (defparameter *condition-name-vec*
527 (let ((vec (make-array 16 :initial-element nil)))
528 (dolist (cond *conditions*)
529 (when (null (aref vec (cdr cond)))
530 (setf (aref vec (cdr cond)) (car cond))))
531 vec))
532 ) ; EVAL-WHEN
534 ;;; SSE shuffle patterns. The names end in the number of bits of the
535 ;;; immediate byte that are used to encode the pattern and the radix
536 ;;; in which to print the value.
537 (macrolet ((define-sse-shuffle-arg-type (name format-string)
538 `(sb!disassem:define-arg-type ,name
539 :type 'imm-byte
540 :printer (lambda (value stream dstate)
541 (declare (type (unsigned-byte 8) value)
542 (type stream stream)
543 (ignore dstate))
544 (format stream ,format-string value)))))
545 (define-sse-shuffle-arg-type sse-shuffle-pattern-2-2 "#b~2,'0B")
546 (define-sse-shuffle-arg-type sse-shuffle-pattern-8-4 "#4r~4,4,'0R"))
548 ;;; Set assembler parameters. (In CMU CL, this was done with
549 ;;; a call to a macro DEF-ASSEMBLER-PARAMS.)
550 (eval-when (:compile-toplevel :load-toplevel :execute)
551 (setf sb!assem:*assem-scheduler-p* nil))
553 (sb!disassem:define-arg-type condition-code
554 :printer *condition-name-vec*)
556 (defun conditional-opcode (condition)
557 (cdr (assoc condition *conditions* :test #'eq)))
559 ;;;; disassembler instruction formats
561 (eval-when (:compile-toplevel :execute)
562 (defun swap-if (direction field1 separator field2)
563 `(:if (,direction :constant 0)
564 (,field1 ,separator ,field2)
565 (,field2 ,separator ,field1))))
567 (sb!disassem:define-instruction-format (byte 8 :default-printer '(:name))
568 (op :field (byte 8 0))
569 ;; optional fields
570 (accum :type 'accum)
571 (imm))
573 (sb!disassem:define-instruction-format (two-bytes 16
574 :default-printer '(:name))
575 (op :fields (list (byte 8 0) (byte 8 8))))
577 (sb!disassem:define-instruction-format (three-bytes 24
578 :default-printer '(:name))
579 (op :fields (list (byte 8 0) (byte 8 8) (byte 8 16))))
581 ;;; Prefix instructions
583 (sb!disassem:define-instruction-format (rex 8)
584 (rex :field (byte 4 4) :value #b0100)
585 (wrxb :field (byte 4 0) :type 'wrxb))
587 (sb!disassem:define-instruction-format (x66 8)
588 (x66 :field (byte 8 0) :type 'x66 :value #x66))
590 ;;; A one-byte instruction with a #x66 prefix, used to indicate an
591 ;;; operand size of :word.
592 (sb!disassem:define-instruction-format (x66-byte 16
593 :default-printer '(:name))
594 (x66 :field (byte 8 0) :value #x66)
595 (op :field (byte 8 8)))
597 ;;; A one-byte instruction with a REX prefix, used to indicate an
598 ;;; operand size of :qword. REX.W must be 1, the other three bits are
599 ;;; ignored.
600 (sb!disassem:define-instruction-format (rex-byte 16
601 :default-printer '(:name))
602 (rex :field (byte 5 3) :value #b01001)
603 (op :field (byte 8 8)))
605 (sb!disassem:define-instruction-format (simple 8)
606 (op :field (byte 7 1))
607 (width :field (byte 1 0) :type 'width)
608 ;; optional fields
609 (accum :type 'accum)
610 (imm))
612 ;;; Same as simple, but with direction bit
613 (sb!disassem:define-instruction-format (simple-dir 8 :include simple)
614 (op :field (byte 6 2))
615 (dir :field (byte 1 1)))
617 ;;; Same as simple, but with the immediate value occurring by default,
618 ;;; and with an appropiate printer.
619 (sb!disassem:define-instruction-format (accum-imm 8
620 :include simple
621 :default-printer '(:name
622 :tab accum ", " imm))
623 (imm :type 'signed-imm-data))
625 (sb!disassem:define-instruction-format (reg-no-width 8
626 :default-printer '(:name :tab reg))
627 (op :field (byte 5 3))
628 (reg :field (byte 3 0) :type 'reg-b)
629 ;; optional fields
630 (accum :type 'accum)
631 (imm))
633 ;;; This is reg-no-width with a mandatory REX prefix and accum field,
634 ;;; with the ability to match against REX.W and REX.B individually.
635 ;;; REX.R and REX.X are ignored.
636 (sb!disassem:define-instruction-format (rex-accum-reg 16
637 :default-printer
638 '(:name :tab accum ", " reg))
639 (rex :field (byte 4 4) :value #b0100)
640 (rex-w :field (byte 1 3) :type 'rex-w)
641 (rex-b :field (byte 1 0) :type 'rex-b)
642 (op :field (byte 5 11))
643 (reg :field (byte 3 8) :type 'reg-b)
644 (accum :type 'accum))
646 ;;; Same as reg-no-width, but with a default operand size of :qword.
647 (sb!disassem:define-instruction-format (reg-no-width-default-qword 8
648 :include reg-no-width
649 :default-printer '(:name :tab reg))
650 (reg :type 'reg-b-default-qword))
652 ;;; Adds a width field to reg-no-width. Note that we can't use
653 ;;; :INCLUDE REG-NO-WIDTH here to save typing because that would put
654 ;;; the WIDTH field last, but the prefilter for WIDTH must run before
655 ;;; the one for IMM to be able to determine the correct size of IMM.
656 (sb!disassem:define-instruction-format (reg 8
657 :default-printer '(:name :tab reg))
658 (op :field (byte 4 4))
659 (width :field (byte 1 3) :type 'width)
660 (reg :field (byte 3 0) :type 'reg-b)
661 ;; optional fields
662 (accum :type 'accum)
663 (imm))
665 (sb!disassem:define-instruction-format (rex-reg 16
666 :default-printer '(:name :tab reg))
667 (rex :field (byte 4 4) :value #b0100)
668 (wrxb :field (byte 4 0) :type 'wrxb)
669 (width :field (byte 1 11) :type 'width)
670 (op :field (byte 4 12))
671 (reg :field (byte 3 8) :type 'reg-b)
672 ;; optional fields
673 (accum :type 'accum)
674 (imm))
676 (sb!disassem:define-instruction-format (reg-reg/mem 16
677 :default-printer
678 `(:name :tab reg ", " reg/mem))
679 (op :field (byte 7 1))
680 (width :field (byte 1 0) :type 'width)
681 (reg/mem :fields (list (byte 2 14) (byte 3 8))
682 :type 'reg/mem :reader reg-r/m-inst-r/m-arg)
683 (reg :field (byte 3 11) :type 'reg)
684 ;; optional fields
685 (imm))
687 ;;; same as reg-reg/mem, but with direction bit
688 (sb!disassem:define-instruction-format (reg-reg/mem-dir 16
689 :include reg-reg/mem
690 :default-printer
691 `(:name
692 :tab
693 ,(swap-if 'dir 'reg/mem ", " 'reg)))
694 (op :field (byte 6 2))
695 (dir :field (byte 1 1)))
697 ;;; Same as reg-reg/mem, but uses the reg field as a second op code.
698 (sb!disassem:define-instruction-format (reg/mem 16
699 :default-printer '(:name :tab reg/mem))
700 (op :fields (list (byte 7 1) (byte 3 11)))
701 (width :field (byte 1 0) :type 'width)
702 (reg/mem :fields (list (byte 2 14) (byte 3 8))
703 :type 'sized-reg/mem)
704 ;; optional fields
705 (imm))
707 ;;; Same as reg/mem, but without a width field and with a default
708 ;;; operand size of :qword.
709 (sb!disassem:define-instruction-format (reg/mem-default-qword 16
710 :default-printer '(:name :tab reg/mem))
711 (op :fields (list (byte 8 0) (byte 3 11)))
712 (reg/mem :fields (list (byte 2 14) (byte 3 8))
713 :type 'sized-reg/mem-default-qword))
715 ;;; Same as reg/mem, but with the immediate value occurring by default,
716 ;;; and with an appropiate printer.
717 (sb!disassem:define-instruction-format (reg/mem-imm 16
718 :include reg/mem
719 :default-printer
720 '(:name :tab reg/mem ", " imm))
721 (reg/mem :type 'sized-reg/mem)
722 (imm :type 'signed-imm-data))
724 (sb!disassem:define-instruction-format (reg/mem-imm/asm-routine 16
725 :include reg/mem-imm
726 :default-printer
727 '(:name :tab reg/mem ", " imm))
728 (reg/mem :type 'sized-reg/mem)
729 (imm :type 'signed-imm-data/asm-routine))
731 ;;; Same as reg/mem, but with using the accumulator in the default printer
732 (sb!disassem:define-instruction-format
733 (accum-reg/mem 16
734 :include reg/mem :default-printer '(:name :tab accum ", " reg/mem))
735 (reg/mem :type 'reg/mem) ; don't need a size
736 (accum :type 'accum))
738 ;;; Same as reg-reg/mem, but with a prefix of #b00001111
739 (sb!disassem:define-instruction-format (ext-reg-reg/mem 24
740 :default-printer
741 `(:name :tab reg ", " reg/mem))
742 (prefix :field (byte 8 0) :value #b00001111)
743 (op :field (byte 7 9))
744 (width :field (byte 1 8) :type 'width)
745 (reg/mem :fields (list (byte 2 22) (byte 3 16))
746 :type 'reg/mem)
747 (reg :field (byte 3 19) :type 'reg)
748 ;; optional fields
749 (imm))
751 (sb!disassem:define-instruction-format (ext-reg-reg/mem-no-width 24
752 :default-printer
753 `(:name :tab reg ", " reg/mem))
754 (prefix :field (byte 8 0) :value #b00001111)
755 (op :field (byte 8 8))
756 (reg/mem :fields (list (byte 2 22) (byte 3 16))
757 :type 'reg/mem)
758 (reg :field (byte 3 19) :type 'reg)
759 ;; optional fields
760 (imm))
762 (sb!disassem:define-instruction-format (ext-reg/mem-no-width 24
763 :default-printer
764 `(:name :tab reg/mem))
765 (prefix :field (byte 8 0) :value #b00001111)
766 (op :fields (list (byte 8 8) (byte 3 19)))
767 (reg/mem :fields (list (byte 2 22) (byte 3 16))
768 :type 'reg/mem))
770 ;;; reg-no-width with #x0f prefix
771 (sb!disassem:define-instruction-format (ext-reg-no-width 16
772 :default-printer '(:name :tab reg))
773 (prefix :field (byte 8 0) :value #b00001111)
774 (op :field (byte 5 11))
775 (reg :field (byte 3 8) :type 'reg-b))
777 ;;; Same as reg/mem, but with a prefix of #b00001111
778 (sb!disassem:define-instruction-format (ext-reg/mem 24
779 :default-printer '(:name :tab reg/mem))
780 (prefix :field (byte 8 0) :value #b00001111)
781 (op :fields (list (byte 7 9) (byte 3 19)))
782 (width :field (byte 1 8) :type 'width)
783 (reg/mem :fields (list (byte 2 22) (byte 3 16))
784 :type 'sized-reg/mem)
785 ;; optional fields
786 (imm))
788 (sb!disassem:define-instruction-format (ext-reg/mem-imm 24
789 :include ext-reg/mem
790 :default-printer
791 '(:name :tab reg/mem ", " imm))
792 (imm :type 'signed-imm-data))
794 (sb!disassem:define-instruction-format (ext-reg/mem-no-width+imm8 24
795 :include ext-reg/mem-no-width
796 :default-printer
797 '(:name :tab reg/mem ", " imm))
798 (imm :type 'imm-byte))
800 ;;;; XMM instructions
802 ;;; All XMM instructions use an extended opcode (#x0F as the first
803 ;;; opcode byte). Therefore in the following "EXT" in the name of the
804 ;;; instruction formats refers to the formats that have an additional
805 ;;; prefix (#x66, #xF2 or #xF3).
807 ;;; Instructions having an XMM register as the destination operand
808 ;;; and an XMM register or a memory location as the source operand.
809 ;;; The size of the operands is implicitly given by the instruction.
810 (sb!disassem:define-instruction-format (xmm-xmm/mem 24
811 :default-printer
812 '(:name :tab reg ", " reg/mem))
813 (x0f :field (byte 8 0) :value #x0f)
814 (op :field (byte 8 8))
815 (reg/mem :fields (list (byte 2 22) (byte 3 16))
816 :type 'xmmreg/mem)
817 (reg :field (byte 3 19) :type 'xmmreg)
818 ;; optional fields
819 (imm))
821 (sb!disassem:define-instruction-format (ext-xmm-xmm/mem 32
822 :default-printer
823 '(:name :tab reg ", " reg/mem))
824 (prefix :field (byte 8 0))
825 (x0f :field (byte 8 8) :value #x0f)
826 (op :field (byte 8 16))
827 (reg/mem :fields (list (byte 2 30) (byte 3 24))
828 :type 'xmmreg/mem)
829 (reg :field (byte 3 27) :type 'xmmreg)
830 (imm))
832 (sb!disassem:define-instruction-format (ext-rex-xmm-xmm/mem 40
833 :default-printer
834 '(:name :tab reg ", " reg/mem))
835 (prefix :field (byte 8 0))
836 (rex :field (byte 4 12) :value #b0100)
837 (wrxb :field (byte 4 8) :type 'wrxb)
838 (x0f :field (byte 8 16) :value #x0f)
839 (op :field (byte 8 24))
840 (reg/mem :fields (list (byte 2 38) (byte 3 32))
841 :type 'xmmreg/mem)
842 (reg :field (byte 3 35) :type 'xmmreg)
843 (imm))
845 (sb!disassem:define-instruction-format (ext-2byte-xmm-xmm/mem 40
846 :default-printer
847 '(:name :tab reg ", " reg/mem))
848 (prefix :field (byte 8 0))
849 (x0f :field (byte 8 8) :value #x0f)
850 (op1 :field (byte 8 16)) ; #x38 or #x3a
851 (op2 :field (byte 8 24))
852 (reg/mem :fields (list (byte 2 38) (byte 3 32))
853 :type 'xmmreg/mem)
854 (reg :field (byte 3 35) :type 'xmmreg))
856 (sb!disassem:define-instruction-format (ext-rex-2byte-xmm-xmm/mem 48
857 :default-printer
858 '(:name :tab reg ", " reg/mem))
859 (prefix :field (byte 8 0))
860 (rex :field (byte 4 12) :value #b0100)
861 (wrxb :field (byte 4 8) :type 'wrxb)
862 (x0f :field (byte 8 16) :value #x0f)
863 (op1 :field (byte 8 24)) ; #x38 or #x3a
864 (op2 :field (byte 8 32))
865 (reg/mem :fields (list (byte 2 46) (byte 3 40))
866 :type 'xmmreg/mem)
867 (reg :field (byte 3 43) :type 'xmmreg))
869 ;;; Same as xmm-xmm/mem etc., but with direction bit.
871 (sb!disassem:define-instruction-format (ext-xmm-xmm/mem-dir 32
872 :include ext-xmm-xmm/mem
873 :default-printer
874 `(:name
875 :tab
876 ,(swap-if 'dir 'reg ", " 'reg/mem)))
877 (op :field (byte 7 17))
878 (dir :field (byte 1 16)))
880 (sb!disassem:define-instruction-format (ext-rex-xmm-xmm/mem-dir 40
881 :include ext-rex-xmm-xmm/mem
882 :default-printer
883 `(:name
884 :tab
885 ,(swap-if 'dir 'reg ", " 'reg/mem)))
886 (op :field (byte 7 25))
887 (dir :field (byte 1 24)))
889 ;;; Instructions having an XMM register as one operand
890 ;;; and a constant (unsigned) byte as the other.
892 (sb!disassem:define-instruction-format (ext-xmm-imm 32
893 :default-printer
894 '(:name :tab reg/mem ", " imm))
895 (prefix :field (byte 8 0))
896 (x0f :field (byte 8 8) :value #x0f)
897 (op :field (byte 8 16))
898 (/i :field (byte 3 27))
899 (b11 :field (byte 2 30) :value #b11)
900 (reg/mem :field (byte 3 24)
901 :type 'xmmreg-b)
902 (imm :type 'imm-byte))
904 (sb!disassem:define-instruction-format (ext-rex-xmm-imm 40
905 :default-printer
906 '(:name :tab reg/mem ", " imm))
907 (prefix :field (byte 8 0))
908 (rex :field (byte 4 12) :value #b0100)
909 (wrxb :field (byte 4 8) :type 'wrxb)
910 (x0f :field (byte 8 16) :value #x0f)
911 (op :field (byte 8 24))
912 (/i :field (byte 3 35))
913 (b11 :field (byte 2 38) :value #b11)
914 (reg/mem :field (byte 3 32)
915 :type 'xmmreg-b)
916 (imm :type 'imm-byte))
918 ;;; Instructions having an XMM register as one operand and a general-
919 ;;; -purpose register or a memory location as the other operand.
921 (sb!disassem:define-instruction-format (xmm-reg/mem 24
922 :default-printer
923 '(:name :tab reg ", " reg/mem))
924 (x0f :field (byte 8 0) :value #x0f)
925 (op :field (byte 8 8))
926 (reg/mem :fields (list (byte 2 22) (byte 3 16))
927 :type 'sized-reg/mem)
928 (reg :field (byte 3 19) :type 'xmmreg)
929 (imm))
931 (sb!disassem:define-instruction-format (ext-xmm-reg/mem 32
932 :default-printer
933 '(:name :tab reg ", " reg/mem))
934 (prefix :field (byte 8 0))
935 (x0f :field (byte 8 8) :value #x0f)
936 (op :field (byte 8 16))
937 (reg/mem :fields (list (byte 2 30) (byte 3 24))
938 :type 'sized-reg/mem)
939 (reg :field (byte 3 27) :type 'xmmreg)
940 (imm))
942 (sb!disassem:define-instruction-format (ext-rex-xmm-reg/mem 40
943 :default-printer
944 '(:name :tab reg ", " reg/mem))
945 (prefix :field (byte 8 0))
946 (rex :field (byte 4 12) :value #b0100)
947 (wrxb :field (byte 4 8) :type 'wrxb)
948 (x0f :field (byte 8 16) :value #x0f)
949 (op :field (byte 8 24))
950 (reg/mem :fields (list (byte 2 38) (byte 3 32))
951 :type 'sized-reg/mem)
952 (reg :field (byte 3 35) :type 'xmmreg)
953 (imm))
955 (sb!disassem:define-instruction-format (ext-2byte-xmm-reg/mem 40
956 :default-printer
957 '(:name :tab reg ", " reg/mem))
958 (prefix :field (byte 8 0))
959 (x0f :field (byte 8 8) :value #x0f)
960 (op1 :field (byte 8 16))
961 (op2 :field (byte 8 24))
962 (reg/mem :fields (list (byte 2 38) (byte 3 32)) :type 'sized-reg/mem)
963 (reg :field (byte 3 35) :type 'xmmreg)
964 (imm))
966 ;;; Instructions having a general-purpose register as one operand and an
967 ;;; XMM register or a memory location as the other operand.
969 (sb!disassem:define-instruction-format (reg-xmm/mem 24
970 :default-printer
971 '(:name :tab reg ", " reg/mem))
972 (x0f :field (byte 8 0) :value #x0f)
973 (op :field (byte 8 8))
974 (reg/mem :fields (list (byte 2 22) (byte 3 16))
975 :type 'xmmreg/mem)
976 (reg :field (byte 3 19) :type 'reg))
978 (sb!disassem:define-instruction-format (ext-reg-xmm/mem 32
979 :default-printer
980 '(:name :tab reg ", " reg/mem))
981 (prefix :field (byte 8 0))
982 (x0f :field (byte 8 8) :value #x0f)
983 (op :field (byte 8 16))
984 (reg/mem :fields (list (byte 2 30) (byte 3 24))
985 :type 'xmmreg/mem)
986 (reg :field (byte 3 27) :type 'reg))
988 (sb!disassem:define-instruction-format (ext-rex-reg-xmm/mem 40
989 :default-printer
990 '(:name :tab reg ", " reg/mem))
991 (prefix :field (byte 8 0))
992 (rex :field (byte 4 12) :value #b0100)
993 (wrxb :field (byte 4 8) :type 'wrxb)
994 (x0f :field (byte 8 16) :value #x0f)
995 (op :field (byte 8 24))
996 (reg/mem :fields (list (byte 2 38) (byte 3 32))
997 :type 'xmmreg/mem)
998 (reg :field (byte 3 35) :type 'reg))
1000 ;;; Instructions having a general-purpose register or a memory location
1001 ;;; as one operand and an a XMM register as the other operand.
1003 (sb!disassem:define-instruction-format (ext-reg/mem-xmm 32
1004 :default-printer
1005 '(:name :tab reg/mem ", " reg))
1006 (prefix :field (byte 8 0))
1007 (x0f :field (byte 8 8) :value #x0f)
1008 (op :field (byte 8 16))
1009 (reg/mem :fields (list (byte 2 30) (byte 3 24))
1010 :type 'reg/mem)
1011 (reg :field (byte 3 27) :type 'xmmreg)
1012 (imm))
1014 (sb!disassem:define-instruction-format (ext-rex-reg/mem-xmm 40
1015 :default-printer
1016 '(:name :tab reg/mem ", " reg))
1017 (prefix :field (byte 8 0))
1018 (rex :field (byte 4 12) :value #b0100)
1019 (wrxb :field (byte 4 8) :type 'wrxb)
1020 (x0f :field (byte 8 16) :value #x0f)
1021 (op :field (byte 8 24))
1022 (reg/mem :fields (list (byte 2 38) (byte 3 32))
1023 :type 'reg/mem)
1024 (reg :field (byte 3 35) :type 'xmmreg)
1025 (imm))
1027 (sb!disassem:define-instruction-format (ext-2byte-reg/mem-xmm 40
1028 :default-printer
1029 '(:name :tab reg/mem ", " reg))
1030 (prefix :field (byte 8 0))
1031 (x0f :field (byte 8 8) :value #x0f)
1032 (op1 :field (byte 8 16))
1033 (op2 :field (byte 8 24))
1034 (reg/mem :fields (list (byte 2 38) (byte 3 32)) :type 'reg/mem)
1035 (reg :field (byte 3 35) :type 'xmmreg)
1036 (imm))
1038 (sb!disassem:define-instruction-format (ext-rex-2byte-reg/mem-xmm 48
1039 :default-printer
1040 '(:name :tab reg/mem ", " reg))
1041 (prefix :field (byte 8 0))
1042 (rex :field (byte 4 12) :value #b0100)
1043 (wrxb :field (byte 4 8) :type 'wrxb)
1044 (x0f :field (byte 8 16) :value #x0f)
1045 (op1 :field (byte 8 24))
1046 (op2 :field (byte 8 32))
1047 (reg/mem :fields (list (byte 2 46) (byte 3 40)) :type 'reg/mem)
1048 (reg :field (byte 3 43) :type 'xmmreg)
1049 (imm))
1051 ;;; Instructions having a general-purpose register as one operand and an a
1052 ;;; general-purpose register or a memory location as the other operand,
1053 ;;; and using a prefix byte.
1055 (sb!disassem:define-instruction-format (ext-prefix-reg-reg/mem 32
1056 :default-printer
1057 '(:name :tab reg ", " reg/mem))
1058 (prefix :field (byte 8 0))
1059 (x0f :field (byte 8 8) :value #x0f)
1060 (op :field (byte 8 16))
1061 (reg/mem :fields (list (byte 2 30) (byte 3 24))
1062 :type 'sized-reg/mem)
1063 (reg :field (byte 3 27) :type 'reg))
1065 (sb!disassem:define-instruction-format (ext-rex-prefix-reg-reg/mem 40
1066 :default-printer
1067 '(:name :tab reg ", " reg/mem))
1068 (prefix :field (byte 8 0))
1069 (rex :field (byte 4 12) :value #b0100)
1070 (wrxb :field (byte 4 8) :type 'wrxb)
1071 (x0f :field (byte 8 16) :value #x0f)
1072 (op :field (byte 8 24))
1073 (reg/mem :fields (list (byte 2 38) (byte 3 32))
1074 :type 'sized-reg/mem)
1075 (reg :field (byte 3 35) :type 'reg))
1077 (sb!disassem:define-instruction-format (ext-2byte-prefix-reg-reg/mem 40
1078 :default-printer
1079 '(:name :tab reg ", " reg/mem))
1080 (prefix :field (byte 8 0))
1081 (x0f :field (byte 8 8) :value #x0f)
1082 (op1 :field (byte 8 16)) ; #x38 or #x3a
1083 (op2 :field (byte 8 24))
1084 (reg/mem :fields (list (byte 2 38) (byte 3 32))
1085 :type 'sized-reg/mem)
1086 (reg :field (byte 3 35) :type 'reg))
1088 (sb!disassem:define-instruction-format (ext-rex-2byte-prefix-reg-reg/mem 48
1089 :default-printer
1090 '(:name :tab reg ", " reg/mem))
1091 (prefix :field (byte 8 0))
1092 (rex :field (byte 4 12) :value #b0100)
1093 (wrxb :field (byte 4 8) :type 'wrxb)
1094 (x0f :field (byte 8 16) :value #x0f)
1095 (op1 :field (byte 8 24)) ; #x38 or #x3a
1096 (op2 :field (byte 8 32))
1097 (reg/mem :fields (list (byte 2 46) (byte 3 40))
1098 :type 'sized-reg/mem)
1099 (reg :field (byte 3 43) :type 'reg))
1101 ;; XMM comparison instruction
1103 (eval-when (:compile-toplevel :load-toplevel :execute)
1104 (defparameter *sse-conditions* #(:eq :lt :le :unord :neq :nlt :nle :ord)))
1106 (sb!disassem:define-arg-type sse-condition-code
1107 ;; Inherit the prefilter from IMM-BYTE to READ-SUFFIX the byte.
1108 :type 'imm-byte
1109 :printer *sse-conditions*)
1111 (sb!disassem:define-instruction-format (string-op 8
1112 :include simple
1113 :default-printer '(:name width)))
1115 (sb!disassem:define-instruction-format (short-cond-jump 16)
1116 (op :field (byte 4 4))
1117 (cc :field (byte 4 0) :type 'condition-code)
1118 (label :field (byte 8 8) :type 'displacement))
1120 (sb!disassem:define-instruction-format (short-jump 16
1121 :default-printer '(:name :tab label))
1122 (const :field (byte 4 4) :value #b1110)
1123 (op :field (byte 4 0))
1124 (label :field (byte 8 8) :type 'displacement))
1126 (sb!disassem:define-instruction-format (near-cond-jump 16)
1127 (op :fields (list (byte 8 0) (byte 4 12)) :value '(#b00001111 #b1000))
1128 (cc :field (byte 4 8) :type 'condition-code)
1129 ;; The disassembler currently doesn't let you have an instruction > 32 bits
1130 ;; long, so we fake it by using a prefilter to read the offset.
1131 (label :type 'displacement
1132 :prefilter (lambda (value dstate)
1133 (declare (ignore value)) ; always nil anyway
1134 (sb!disassem:read-signed-suffix 32 dstate))))
1136 (sb!disassem:define-instruction-format (near-jump 8
1137 :default-printer '(:name :tab label))
1138 (op :field (byte 8 0))
1139 ;; The disassembler currently doesn't let you have an instruction > 32 bits
1140 ;; long, so we fake it by using a prefilter to read the address.
1141 (label :type 'displacement
1142 :prefilter (lambda (value dstate)
1143 (declare (ignore value)) ; always nil anyway
1144 (sb!disassem:read-signed-suffix 32 dstate))))
1147 (sb!disassem:define-instruction-format (cond-set 24
1148 :default-printer '('set cc :tab reg/mem))
1149 (prefix :field (byte 8 0) :value #b00001111)
1150 (op :field (byte 4 12) :value #b1001)
1151 (cc :field (byte 4 8) :type 'condition-code)
1152 (reg/mem :fields (list (byte 2 22) (byte 3 16))
1153 :type 'sized-byte-reg/mem)
1154 (reg :field (byte 3 19) :value #b000))
1156 (sb!disassem:define-instruction-format (cond-move 24
1157 :default-printer
1158 '('cmov cc :tab reg ", " reg/mem))
1159 (prefix :field (byte 8 0) :value #b00001111)
1160 (op :field (byte 4 12) :value #b0100)
1161 (cc :field (byte 4 8) :type 'condition-code)
1162 (reg/mem :fields (list (byte 2 22) (byte 3 16))
1163 :type 'reg/mem)
1164 (reg :field (byte 3 19) :type 'reg))
1166 (sb!disassem:define-instruction-format (enter-format 32
1167 :default-printer '(:name
1168 :tab disp
1169 (:unless (:constant 0)
1170 ", " level)))
1171 (op :field (byte 8 0))
1172 (disp :field (byte 16 8))
1173 (level :field (byte 8 24)))
1175 ;;; Single byte instruction with an immediate byte argument.
1176 (sb!disassem:define-instruction-format (byte-imm 16
1177 :default-printer '(:name :tab code))
1178 (op :field (byte 8 0))
1179 (code :field (byte 8 8) :reader byte-imm-code))
1181 ;;; Two byte instruction with an immediate byte argument.
1183 (sb!disassem:define-instruction-format (word-imm 24
1184 :default-printer '(:name :tab code))
1185 (op :field (byte 16 0))
1186 (code :field (byte 8 16) :reader word-imm-code))
1188 ;;; F3 escape map - Needs a ton more work.
1190 (sb!disassem:define-instruction-format (F3-escape 24)
1191 (prefix1 :field (byte 8 0) :value #xF3)
1192 (prefix2 :field (byte 8 8) :value #x0F)
1193 (op :field (byte 8 16)))
1195 (sb!disassem:define-instruction-format (rex-F3-escape 32)
1196 ;; F3 is a legacy prefix which was generalized to select an alternate opcode
1197 ;; map. Legacy prefixes are encoded in the instruction before a REX prefix.
1198 (prefix1 :field (byte 8 0) :value #xF3)
1199 (rex :field (byte 4 12) :value 4) ; "prefix2"
1200 (wrxb :field (byte 4 8) :type 'wrxb)
1201 (prefix3 :field (byte 8 16) :value #x0F)
1202 (op :field (byte 8 24)))
1204 (sb!disassem:define-instruction-format (F3-escape-reg-reg/mem 32
1205 :include F3-escape
1206 :default-printer
1207 '(:name :tab reg ", " reg/mem))
1208 (reg/mem :fields (list (byte 2 30) (byte 3 24)) :type 'sized-reg/mem)
1209 (reg :field (byte 3 27) :type 'reg))
1211 (sb!disassem:define-instruction-format (rex-F3-escape-reg-reg/mem 40
1212 :include rex-F3-escape
1213 :default-printer
1214 '(:name :tab reg ", " reg/mem))
1215 (reg/mem :fields (list (byte 2 38) (byte 3 32)) :type 'sized-reg/mem)
1216 (reg :field (byte 3 35) :type 'reg))
1219 ;;;; primitive emitters
1221 (define-bitfield-emitter emit-word 16
1222 (byte 16 0))
1224 (define-bitfield-emitter emit-dword 32
1225 (byte 32 0))
1227 ;;; Most uses of dwords are as displacements or as immediate values in
1228 ;;; 64-bit operations. In these cases they are sign-extended to 64 bits.
1229 ;;; EMIT-DWORD is unsuitable there because it accepts values of type
1230 ;;; (OR (SIGNED-BYTE 32) (UNSIGNED-BYTE 32)), so we provide a more
1231 ;;; restricted emitter here.
1232 (defun emit-signed-dword (segment value)
1233 (declare (type segment segment)
1234 (type (signed-byte 32) value))
1235 (declare (inline emit-dword))
1236 (emit-dword segment value))
1238 (define-bitfield-emitter emit-qword 64
1239 (byte 64 0))
1241 (define-bitfield-emitter emit-byte-with-reg 8
1242 (byte 5 3) (byte 3 0))
1244 (define-bitfield-emitter emit-mod-reg-r/m-byte 8
1245 (byte 2 6) (byte 3 3) (byte 3 0))
1247 (define-bitfield-emitter emit-sib-byte 8
1248 (byte 2 6) (byte 3 3) (byte 3 0))
1250 (define-bitfield-emitter emit-rex-byte 8
1251 (byte 4 4) (byte 1 3) (byte 1 2) (byte 1 1) (byte 1 0))
1255 ;;;; fixup emitters
1257 (defun emit-absolute-fixup (segment fixup &optional quad-p)
1258 (note-fixup segment (if quad-p :absolute64 :absolute) fixup)
1259 (let ((offset (fixup-offset fixup)))
1260 (if (label-p offset)
1261 (emit-back-patch segment
1262 (if quad-p 8 4)
1263 (lambda (segment posn)
1264 (declare (ignore posn))
1265 (let ((val (- (+ (component-header-length)
1266 (or (label-position offset)
1268 other-pointer-lowtag)))
1269 (if quad-p
1270 (emit-qword segment val)
1271 (emit-signed-dword segment val)))))
1272 (if quad-p
1273 (emit-qword segment (or offset 0))
1274 (emit-signed-dword segment (or offset 0))))))
1276 (defun emit-relative-fixup (segment fixup)
1277 (note-fixup segment :relative fixup)
1278 (emit-signed-dword segment (or (fixup-offset fixup) 0)))
1281 ;;;; the effective-address (ea) structure
1283 (defun reg-tn-encoding (tn)
1284 (declare (type tn tn))
1285 ;; ea only has space for three bits of register number: regs r8
1286 ;; and up are selected by a REX prefix byte which caller is responsible
1287 ;; for having emitted where necessary already
1288 (ecase (sb-name (sc-sb (tn-sc tn)))
1289 (registers
1290 (let ((offset (mod (tn-offset tn) 16)))
1291 (logior (ash (logand offset 1) 2)
1292 (ash offset -1))))
1293 (float-registers
1294 (mod (tn-offset tn) 8))))
1296 (defstruct (ea (:constructor make-ea (size &key base index scale disp))
1297 (:copier nil))
1298 ;; note that we can represent an EA with a QWORD size, but EMIT-EA
1299 ;; can't actually emit it on its own: caller also needs to emit REX
1300 ;; prefix
1301 (size nil :type (member :byte :word :dword :qword))
1302 (base nil :type (or tn null))
1303 (index nil :type (or tn null))
1304 (scale 1 :type (member 1 2 4 8))
1305 (disp 0 :type (or (unsigned-byte 32) (signed-byte 32) fixup)))
1306 (def!method print-object ((ea ea) stream)
1307 (cond ((or *print-escape* *print-readably*)
1308 (print-unreadable-object (ea stream :type t)
1309 (format stream
1310 "~S~@[ base=~S~]~@[ index=~S~]~@[ scale=~S~]~@[ disp=~S~]"
1311 (ea-size ea)
1312 (ea-base ea)
1313 (ea-index ea)
1314 (let ((scale (ea-scale ea)))
1315 (if (= scale 1) nil scale))
1316 (ea-disp ea))))
1318 (format stream "~A PTR [" (symbol-name (ea-size ea)))
1319 (when (ea-base ea)
1320 (write-string (sb!c::location-print-name (ea-base ea)) stream)
1321 (when (ea-index ea)
1322 (write-string "+" stream)))
1323 (when (ea-index ea)
1324 (write-string (sb!c::location-print-name (ea-index ea)) stream))
1325 (unless (= (ea-scale ea) 1)
1326 (format stream "*~A" (ea-scale ea)))
1327 (typecase (ea-disp ea)
1328 (null)
1329 (integer
1330 (format stream "~@D" (ea-disp ea)))
1332 (format stream "+~A" (ea-disp ea))))
1333 (write-char #\] stream))))
1335 (defun emit-constant-tn-rip (segment constant-tn reg remaining-bytes)
1336 ;; AMD64 doesn't currently have a code object register to use as a
1337 ;; base register for constant access. Instead we use RIP-relative
1338 ;; addressing. The offset from the SIMPLE-FUN-HEADER to the instruction
1339 ;; is passed to the backpatch callback. In addition we need the offset
1340 ;; from the start of the function header to the slot in the CODE-HEADER
1341 ;; that stores the constant. Since we don't know where the code header
1342 ;; starts, instead count backwards from the function header.
1343 (let* ((2comp (component-info *component-being-compiled*))
1344 (constants (ir2-component-constants 2comp))
1345 (len (length constants))
1346 ;; Both CODE-HEADER and SIMPLE-FUN-HEADER are 16-byte aligned.
1347 ;; If there are an even amount of constants, there will be
1348 ;; an extra qword of padding before the function header, which
1349 ;; needs to be adjusted for. XXX: This will break if new slots
1350 ;; are added to the code header.
1351 (offset (* (- (+ len (if (evenp len)
1354 (tn-offset constant-tn))
1355 n-word-bytes)))
1356 ;; RIP-relative addressing
1357 (emit-mod-reg-r/m-byte segment #b00 reg #b101)
1358 (emit-back-patch segment
1360 (lambda (segment posn)
1361 ;; The addressing is relative to end of instruction,
1362 ;; i.e. the end of this dword. Hence the + 4.
1363 (emit-signed-dword segment
1364 (+ 4 remaining-bytes
1365 (- (+ offset posn)))))))
1366 (values))
1368 (defun emit-label-rip (segment fixup reg remaining-bytes)
1369 (let ((label (fixup-offset fixup)))
1370 ;; RIP-relative addressing
1371 (emit-mod-reg-r/m-byte segment #b00 reg #b101)
1372 (emit-back-patch segment
1374 (lambda (segment posn)
1375 (emit-signed-dword segment
1376 (- (label-position label)
1377 (+ posn 4 remaining-bytes))))))
1378 (values))
1380 (defun emit-ea (segment thing reg &key allow-constants (remaining-bytes 0))
1381 (etypecase thing
1383 ;; this would be eleganter if we had a function that would create
1384 ;; an ea given a tn
1385 (ecase (sb-name (sc-sb (tn-sc thing)))
1386 ((registers float-registers)
1387 (emit-mod-reg-r/m-byte segment #b11 reg (reg-tn-encoding thing)))
1388 (stack
1389 ;; Convert stack tns into an index off RBP.
1390 (let ((disp (frame-byte-offset (tn-offset thing))))
1391 (cond ((<= -128 disp 127)
1392 (emit-mod-reg-r/m-byte segment #b01 reg #b101)
1393 (emit-byte segment disp))
1395 (emit-mod-reg-r/m-byte segment #b10 reg #b101)
1396 (emit-signed-dword segment disp)))))
1397 (constant
1398 (unless allow-constants
1399 ;; Why?
1400 (error
1401 "Constant TNs can only be directly used in MOV, PUSH, and CMP."))
1402 (emit-constant-tn-rip segment thing reg remaining-bytes))))
1404 (let* ((base (ea-base thing))
1405 (index (ea-index thing))
1406 (scale (ea-scale thing))
1407 (disp (ea-disp thing))
1408 (mod (cond ((or (null base)
1409 (and (eql disp 0)
1410 (not (= (reg-tn-encoding base) #b101))))
1411 #b00)
1412 ((and (fixnump disp) (<= -128 disp 127))
1413 #b01)
1415 #b10)))
1416 (r/m (cond (index #b100)
1417 ((null base) #b101)
1418 (t (reg-tn-encoding base)))))
1419 (when (and (fixup-p disp)
1420 (label-p (fixup-offset disp)))
1421 (aver (null base))
1422 (aver (null index))
1423 (return-from emit-ea (emit-ea segment disp reg
1424 :allow-constants allow-constants
1425 :remaining-bytes remaining-bytes)))
1426 (when (and (= mod 0) (= r/m #b101))
1427 ;; this is rip-relative in amd64, so we'll use a sib instead
1428 (setf r/m #b100 scale 1))
1429 (emit-mod-reg-r/m-byte segment mod reg r/m)
1430 (when (= r/m #b100)
1431 (let ((ss (1- (integer-length scale)))
1432 (index (if (null index)
1433 #b100
1434 (if (location= index rsp-tn)
1435 (error "can't index off of RSP")
1436 (reg-tn-encoding index))))
1437 (base (if (null base)
1438 #b101
1439 (reg-tn-encoding base))))
1440 (emit-sib-byte segment ss index base)))
1441 (cond ((= mod #b01)
1442 (emit-byte segment disp))
1443 ((or (= mod #b10) (null base))
1444 (if (fixup-p disp)
1445 (emit-absolute-fixup segment disp)
1446 (emit-signed-dword segment disp))))))
1447 (fixup
1448 (typecase (fixup-offset thing)
1449 (label
1450 (emit-label-rip segment thing reg remaining-bytes))
1452 (emit-mod-reg-r/m-byte segment #b00 reg #b100)
1453 (emit-sib-byte segment 0 #b100 #b101)
1454 (emit-absolute-fixup segment thing))))))
1456 (defun byte-reg-p (thing)
1457 (and (tn-p thing)
1458 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)
1459 (member (sc-name (tn-sc thing)) *byte-sc-names*)
1462 (defun byte-ea-p (thing)
1463 (typecase thing
1464 (ea (eq (ea-size thing) :byte))
1466 (and (member (sc-name (tn-sc thing)) *byte-sc-names*) t))
1467 (t nil)))
1469 (defun word-reg-p (thing)
1470 (and (tn-p thing)
1471 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)
1472 (member (sc-name (tn-sc thing)) *word-sc-names*)
1475 (defun word-ea-p (thing)
1476 (typecase thing
1477 (ea (eq (ea-size thing) :word))
1478 (tn (and (member (sc-name (tn-sc thing)) *word-sc-names*) t))
1479 (t nil)))
1481 (defun dword-reg-p (thing)
1482 (and (tn-p thing)
1483 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)
1484 (member (sc-name (tn-sc thing)) *dword-sc-names*)
1487 (defun dword-ea-p (thing)
1488 (typecase thing
1489 (ea (eq (ea-size thing) :dword))
1491 (and (member (sc-name (tn-sc thing)) *dword-sc-names*) t))
1492 (t nil)))
1494 (defun qword-reg-p (thing)
1495 (and (tn-p thing)
1496 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)
1497 (member (sc-name (tn-sc thing)) *qword-sc-names*)
1500 (defun qword-ea-p (thing)
1501 (typecase thing
1502 (ea (eq (ea-size thing) :qword))
1504 (and (member (sc-name (tn-sc thing)) *qword-sc-names*) t))
1505 (t nil)))
1507 ;;; Return true if THING is a general-purpose register TN.
1508 (defun register-p (thing)
1509 (and (tn-p thing)
1510 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)))
1512 (defun accumulator-p (thing)
1513 (and (register-p thing)
1514 (= (tn-offset thing) 0)))
1516 ;;; Return true if THING is an XMM register TN.
1517 (defun xmm-register-p (thing)
1518 (and (tn-p thing)
1519 (eq (sb-name (sc-sb (tn-sc thing))) 'float-registers)))
1522 ;;;; utilities
1524 (def!constant +operand-size-prefix-byte+ #b01100110)
1526 (defun maybe-emit-operand-size-prefix (segment size)
1527 (unless (or (eq size :byte)
1528 (eq size :qword) ; REX prefix handles this
1529 (eq size +default-operand-size+))
1530 (emit-byte segment +operand-size-prefix-byte+)))
1532 ;;; A REX prefix must be emitted if at least one of the following
1533 ;;; conditions is true:
1534 ;; 1. The operand size is :QWORD and the default operand size of the
1535 ;; instruction is not :QWORD.
1536 ;;; 2. The instruction references an extended register.
1537 ;;; 3. The instruction references one of the byte registers SIL, DIL,
1538 ;;; SPL or BPL.
1540 ;;; Emit a REX prefix if necessary. OPERAND-SIZE is used to determine
1541 ;;; whether to set REX.W. Callers pass it explicitly as :DO-NOT-SET if
1542 ;;; this should not happen, for example because the instruction's
1543 ;;; default operand size is qword. R, X and B are NIL or TNs specifying
1544 ;;; registers the encodings of which are extended with the REX.R, REX.X
1545 ;;; and REX.B bit, respectively. To determine whether one of the byte
1546 ;;; registers is used that can only be accessed using a REX prefix, we
1547 ;;; need only to test R and B, because X is only used for the index
1548 ;;; register of an effective address and therefore never byte-sized.
1549 ;;; For R we can avoid to calculate the size of the TN because it is
1550 ;;; always OPERAND-SIZE. The size of B must be calculated here because
1551 ;;; B can be address-sized (if it is the base register of an effective
1552 ;;; address), of OPERAND-SIZE (if the instruction operates on two
1553 ;;; registers) or of some different size (in the instructions that
1554 ;;; combine arguments of different sizes: MOVZX, MOVSX, MOVSXD and
1555 ;;; several SSE instructions, e.g. CVTSD2SI). We don't distinguish
1556 ;;; between general-purpose and floating point registers for this cause
1557 ;;; because only general-purpose registers can be byte-sized at all.
1558 (defun maybe-emit-rex-prefix (segment operand-size r x b)
1559 (declare (type (member nil :byte :word :dword :qword :do-not-set)
1560 operand-size)
1561 (type (or null tn) r x b))
1562 (labels ((if-hi (r)
1563 (if (and r (> (tn-offset r)
1564 ;; offset of r8 is 16, offset of xmm8 is 8
1565 (if (eq (sb-name (sc-sb (tn-sc r)))
1566 'float-registers)
1568 15)))
1571 (reg-4-7-p (r)
1572 ;; Assuming R is a TN describing a general-purpose
1573 ;; register, return true if it references register
1574 ;; 4 upto 7.
1575 (<= 8 (tn-offset r) 15)))
1576 (let ((rex-w (if (eq operand-size :qword) 1 0))
1577 (rex-r (if-hi r))
1578 (rex-x (if-hi x))
1579 (rex-b (if-hi b)))
1580 (when (or (not (zerop (logior rex-w rex-r rex-x rex-b)))
1581 (and r
1582 (eq operand-size :byte)
1583 (reg-4-7-p r))
1584 (and b
1585 (eq (operand-size b) :byte)
1586 (reg-4-7-p b)))
1587 (emit-rex-byte segment #b0100 rex-w rex-r rex-x rex-b)))))
1589 ;;; Emit a REX prefix if necessary. The operand size is determined from
1590 ;;; THING or can be overwritten by OPERAND-SIZE. This and REG are always
1591 ;;; passed to MAYBE-EMIT-REX-PREFIX. Additionally, if THING is an EA we
1592 ;;; pass its index and base registers, if it is a register TN, we pass
1593 ;;; only itself.
1594 ;;; In contrast to EMIT-EA above, neither stack TNs nor fixups need to
1595 ;;; be treated specially here: If THING is a stack TN, neither it nor
1596 ;;; any of its components are passed to MAYBE-EMIT-REX-PREFIX which
1597 ;;; works correctly because stack references always use RBP as the base
1598 ;;; register and never use an index register so no extended registers
1599 ;;; need to be accessed. Fixups are assembled using an addressing mode
1600 ;;; of displacement-only or RIP-plus-displacement (see EMIT-EA), so may
1601 ;;; not reference an extended register. The displacement-only addressing
1602 ;;; mode requires that REX.X is 0, which is ensured here.
1603 (defun maybe-emit-rex-for-ea (segment thing reg &key operand-size)
1604 (declare (type (or ea tn fixup) thing)
1605 (type (or null tn) reg)
1606 (type (member nil :byte :word :dword :qword :do-not-set)
1607 operand-size))
1608 (let ((ea-p (ea-p thing)))
1609 (maybe-emit-rex-prefix segment
1610 (or operand-size (operand-size thing))
1612 (and ea-p (ea-index thing))
1613 (cond (ea-p (ea-base thing))
1614 ((and (tn-p thing)
1615 (member (sb-name (sc-sb (tn-sc thing)))
1616 '(float-registers registers)))
1617 thing)
1618 (t nil)))))
1620 (defun operand-size (thing)
1621 (typecase thing
1623 ;; FIXME: might as well be COND instead of having to use #. readmacro
1624 ;; to hack up the code
1625 (case (sc-name (tn-sc thing))
1626 #!+sb-simd-pack
1627 (#.*oword-sc-names*
1628 :oword)
1629 (#.*qword-sc-names*
1630 :qword)
1631 (#.*dword-sc-names*
1632 :dword)
1633 (#.*word-sc-names*
1634 :word)
1635 (#.*byte-sc-names*
1636 :byte)
1637 ;; added by jrd: float-registers is a separate size (?)
1638 ;; The only place in the code where we are called with THING
1639 ;; being a float-register is in MAYBE-EMIT-REX-PREFIX when it
1640 ;; checks whether THING is a byte register. Thus our result in
1641 ;; these cases could as well be :dword and :qword. I leave it as
1642 ;; :float and :double which is more likely to trigger an aver
1643 ;; instead of silently doing the wrong thing in case this
1644 ;; situation should change. Lutz Euler, 2005-10-23.
1645 (#.*float-sc-names*
1646 :float)
1647 (#.*double-sc-names*
1648 :double)
1649 (#.*complex-sc-names*
1650 :complex)
1652 (error "can't tell the size of ~S ~S" thing (sc-name (tn-sc thing))))))
1654 (ea-size thing))
1655 (fixup
1656 ;; GNA. Guess who spelt "flavor" correctly first time round?
1657 ;; There's a strong argument in my mind to change all uses of
1658 ;; "flavor" to "kind": and similarly with some misguided uses of
1659 ;; "type" here and there. -- CSR, 2005-01-06.
1660 (case (fixup-flavor thing)
1661 ((:foreign-dataref) :qword)))
1663 nil)))
1665 (defun matching-operand-size (dst src)
1666 (let ((dst-size (operand-size dst))
1667 (src-size (operand-size src)))
1668 (if dst-size
1669 (if src-size
1670 (if (eq dst-size src-size)
1671 dst-size
1672 (error "size mismatch: ~S is a ~S and ~S is a ~S."
1673 dst dst-size src src-size))
1674 dst-size)
1675 (if src-size
1676 src-size
1677 (error "can't tell the size of either ~S or ~S" dst src)))))
1679 ;;; Except in a very few cases (MOV instructions A1, A3 and B8 - BF)
1680 ;;; we expect dword data bytes even when 64 bit work is being done.
1681 ;;; But A1 and A3 are currently unused and B8 - BF use EMIT-QWORD
1682 ;;; directly, so we emit all quad constants as dwords, additionally
1683 ;;; making sure that they survive the sign-extension to 64 bits
1684 ;;; unchanged.
1685 (defun emit-sized-immediate (segment size value)
1686 (ecase size
1687 (:byte
1688 (emit-byte segment value))
1689 (:word
1690 (emit-word segment value))
1691 (:dword
1692 (emit-dword segment value))
1693 (:qword
1694 (emit-signed-dword segment value))))
1696 ;;;; prefixes
1698 (define-instruction rex (segment)
1699 (:printer rex () nil :print-name nil)
1700 (:emitter
1701 (bug "REX prefix used as a standalone instruction")))
1703 (define-instruction x66 (segment)
1704 (:printer x66 () nil :print-name nil)
1705 (:emitter
1706 (bug "#X66 prefix used as a standalone instruction")))
1708 (defun emit-prefix (segment name)
1709 (declare (ignorable segment))
1710 (ecase name
1711 ((nil))
1712 (:lock
1713 #!+sb-thread
1714 (emit-byte segment #xf0))))
1716 (define-instruction lock (segment)
1717 (:printer byte ((op #b11110000)) nil)
1718 (:emitter
1719 (bug "LOCK prefix used as a standalone instruction")))
1721 (define-instruction rep (segment)
1722 (:emitter
1723 (emit-byte segment #b11110011)))
1725 (define-instruction repe (segment)
1726 (:printer byte ((op #b11110011)) nil)
1727 (:emitter
1728 (emit-byte segment #b11110011)))
1730 (define-instruction repne (segment)
1731 (:printer byte ((op #b11110010)) nil)
1732 (:emitter
1733 (emit-byte segment #b11110010)))
1735 ;;;; general data transfer
1737 ;;; This is the part of the MOV instruction emitter that does moving
1738 ;;; of an immediate value into a qword register. We go to some length
1739 ;;; to achieve the shortest possible encoding.
1740 (defun emit-immediate-move-to-qword-register (segment dst src)
1741 (declare (type integer src))
1742 (cond ((typep src '(unsigned-byte 32))
1743 ;; We use the B8 - BF encoding with an operand size of 32 bits
1744 ;; here and let the implicit zero-extension fill the upper half
1745 ;; of the 64-bit destination register. Instruction size: five
1746 ;; or six bytes. (A REX prefix will be emitted only if the
1747 ;; destination is an extended register.)
1748 (maybe-emit-rex-prefix segment :dword nil nil dst)
1749 (emit-byte-with-reg segment #b10111 (reg-tn-encoding dst))
1750 (emit-dword segment src))
1752 (maybe-emit-rex-prefix segment :qword nil nil dst)
1753 (cond ((typep src '(signed-byte 32))
1754 ;; Use the C7 encoding that takes a 32-bit immediate and
1755 ;; sign-extends it to 64 bits. Instruction size: seven
1756 ;; bytes.
1757 (emit-byte segment #b11000111)
1758 (emit-mod-reg-r/m-byte segment #b11 #b000
1759 (reg-tn-encoding dst))
1760 (emit-signed-dword segment src))
1761 ((<= (- (expt 2 64) (expt 2 31))
1763 (1- (expt 2 64)))
1764 ;; This triggers on positive integers of 64 bits length
1765 ;; with the most significant 33 bits being 1. We use the
1766 ;; same encoding as in the previous clause.
1767 (emit-byte segment #b11000111)
1768 (emit-mod-reg-r/m-byte segment #b11 #b000
1769 (reg-tn-encoding dst))
1770 (emit-signed-dword segment (- src (expt 2 64))))
1772 ;; We need a full 64-bit immediate. Instruction size:
1773 ;; ten bytes.
1774 (emit-byte-with-reg segment #b10111 (reg-tn-encoding dst))
1775 (emit-qword segment src))))))
1777 (define-instruction mov (segment dst src)
1778 ;; immediate to register
1779 (:printer reg ((op #b1011) (imm nil :type 'signed-imm-data/asm-routine))
1780 '(:name :tab reg ", " imm))
1781 (:printer rex-reg ((op #b1011)
1782 (imm nil :type 'signed-imm-data-upto-qword/asm-routine))
1783 '(:name :tab reg ", " imm))
1784 ;; absolute mem to/from accumulator
1785 (:printer simple-dir ((op #b101000) (imm nil :type 'imm-addr))
1786 `(:name :tab ,(swap-if 'dir 'accum ", " '("[" imm "]"))))
1787 ;; register to/from register/memory
1788 (:printer reg-reg/mem-dir ((op #b100010)))
1789 ;; immediate to register/memory
1790 (:printer reg/mem-imm/asm-routine ((op '(#b1100011 #b000))))
1792 (:emitter
1793 (let ((size (matching-operand-size dst src)))
1794 (maybe-emit-operand-size-prefix segment size)
1795 (cond ((register-p dst)
1796 (cond ((integerp src)
1797 (cond ((eq size :qword)
1798 (emit-immediate-move-to-qword-register segment
1799 dst src))
1801 (maybe-emit-rex-prefix segment size nil nil dst)
1802 (emit-byte-with-reg segment
1803 (if (eq size :byte)
1804 #b10110
1805 #b10111)
1806 (reg-tn-encoding dst))
1807 (emit-sized-immediate segment size src))))
1808 ((and (fixup-p src)
1809 (or (eq (fixup-flavor src) :foreign)
1810 (eq (fixup-flavor src) :assembly-routine)))
1811 (maybe-emit-rex-prefix segment :dword nil nil dst)
1812 (emit-byte-with-reg segment #b10111 (reg-tn-encoding dst))
1813 (emit-absolute-fixup segment src))
1815 (maybe-emit-rex-for-ea segment src dst)
1816 (emit-byte segment
1817 (if (eq size :byte)
1818 #b10001010
1819 #b10001011))
1820 (emit-ea segment src (reg-tn-encoding dst)
1821 :allow-constants t))))
1822 ((integerp src)
1823 ;; C7 only deals with 32 bit immediates even if the
1824 ;; destination is a 64-bit location. The value is
1825 ;; sign-extended in this case.
1826 (maybe-emit-rex-for-ea segment dst nil)
1827 (emit-byte segment (if (eq size :byte) #b11000110 #b11000111))
1828 (emit-ea segment dst #b000)
1829 (emit-sized-immediate segment size src))
1830 ((register-p src)
1831 (maybe-emit-rex-for-ea segment dst src)
1832 (emit-byte segment (if (eq size :byte) #b10001000 #b10001001))
1833 (emit-ea segment dst (reg-tn-encoding src)))
1834 ((fixup-p src)
1835 ;; Generally we can't MOV a fixupped value into an EA, since
1836 ;; MOV on non-registers can only take a 32-bit immediate arg.
1837 ;; Make an exception for :FOREIGN fixups (pretty much just
1838 ;; the runtime asm, since other foreign calls go through the
1839 ;; the linkage table) and for linkage table references, since
1840 ;; these should always end up in low memory.
1841 (aver (or (member (fixup-flavor src)
1842 '(:foreign :foreign-dataref :symbol-tls-index))
1843 (eq (ea-size dst) :dword)))
1844 (maybe-emit-rex-for-ea segment dst nil)
1845 (emit-byte segment #b11000111)
1846 (emit-ea segment dst #b000)
1847 (emit-absolute-fixup segment src))
1849 (error "bogus arguments to MOV: ~S ~S" dst src))))))
1851 ;;; Emit a sign-extending (if SIGNED-P is true) or zero-extending move.
1852 ;;; To achieve the shortest possible encoding zero extensions into a
1853 ;;; 64-bit destination are assembled as a straight 32-bit MOV (if the
1854 ;;; source size is 32 bits) or as MOVZX with a 32-bit destination (if
1855 ;;; the source size is 8 or 16 bits). Due to the implicit zero extension
1856 ;;; to 64 bits this has the same effect as a MOVZX with 64-bit
1857 ;;; destination but often needs no REX prefix.
1858 (defun emit-move-with-extension (segment dst src signed-p)
1859 (aver (register-p dst))
1860 (let ((dst-size (operand-size dst))
1861 (src-size (operand-size src))
1862 (opcode (if signed-p #b10111110 #b10110110)))
1863 (macrolet ((emitter (operand-size &rest bytes)
1864 `(progn
1865 (maybe-emit-rex-for-ea segment src dst
1866 :operand-size ,operand-size)
1867 ,@(mapcar (lambda (byte)
1868 `(emit-byte segment ,byte))
1869 bytes)
1870 (emit-ea segment src (reg-tn-encoding dst)))))
1871 (ecase dst-size
1872 (:word
1873 (aver (eq src-size :byte))
1874 (maybe-emit-operand-size-prefix segment :word)
1875 (emitter :word #b00001111 opcode))
1876 ((:dword :qword)
1877 (unless signed-p
1878 (setf dst-size :dword))
1879 (ecase src-size
1880 (:byte
1881 (emitter dst-size #b00001111 opcode))
1882 (:word
1883 (emitter dst-size #b00001111 (logior opcode 1)))
1884 (:dword
1885 (aver (or (not signed-p) (eq dst-size :qword)))
1886 (emitter dst-size
1887 (if signed-p #x63 #x8b))))))))) ; movsxd or straight mov
1889 ;; MOV[SZ]X - #x66 or REX selects the destination REG size, wherein :byte isn't
1890 ;; a possibility. The 'width' bit selects a source r/m size of :byte or :word.
1891 (sb!disassem:define-instruction-format
1892 (move-with-extension 24 :include ext-reg-reg/mem
1893 :default-printer
1894 '(:name :tab reg ", "
1895 (:cond ((width :constant 0) (:using #'print-sized-byte-reg/mem reg/mem))
1896 (t (:using #'print-sized-word-reg/mem reg/mem)))))
1897 (width :prefilter nil)) ; doesn't affect DSTATE
1899 (define-instruction movsx (segment dst src)
1900 (:printer move-with-extension ((op #b1011111)))
1901 (:emitter (emit-move-with-extension segment dst src :signed)))
1903 (define-instruction movzx (segment dst src)
1904 (:printer move-with-extension ((op #b1011011)))
1905 (:emitter (emit-move-with-extension segment dst src nil)))
1907 ;;; The regular use of MOVSXD is with an operand size of :qword. This
1908 ;;; sign-extends the dword source into the qword destination register.
1909 ;;; If the operand size is :dword the instruction zero-extends the dword
1910 ;;; source into the qword destination register, i.e. it does the same as
1911 ;;; a dword MOV into a register.
1912 (define-instruction movsxd (segment dst src)
1913 (:printer reg-reg/mem ((op #b0110001) (width 1)
1914 (reg/mem nil :type 'sized-dword-reg/mem)))
1915 (:emitter (emit-move-with-extension segment dst src :signed)))
1917 ;;; this is not a real amd64 instruction, of course
1918 (define-instruction movzxd (segment dst src)
1919 ; (:printer reg-reg/mem ((op #x63) (reg nil :type 'reg)))
1920 (:emitter (emit-move-with-extension segment dst src nil)))
1922 (define-instruction push (segment src)
1923 ;; register
1924 (:printer reg-no-width-default-qword ((op #b01010)))
1925 ;; register/memory
1926 (:printer reg/mem-default-qword ((op '(#b11111111 #b110))))
1927 ;; immediate
1928 (:printer byte ((op #b01101010) (imm nil :type 'signed-imm-byte))
1929 '(:name :tab imm))
1930 (:printer byte ((op #b01101000)
1931 (imm nil :type 'signed-imm-data-default-qword))
1932 '(:name :tab imm))
1933 ;; ### segment registers?
1935 (:emitter
1936 (cond ((integerp src)
1937 (cond ((<= -128 src 127)
1938 (emit-byte segment #b01101010)
1939 (emit-byte segment src))
1941 ;; A REX-prefix is not needed because the operand size
1942 ;; defaults to 64 bits. The size of the immediate is 32
1943 ;; bits and it is sign-extended.
1944 (emit-byte segment #b01101000)
1945 (emit-signed-dword segment src))))
1947 (let ((size (operand-size src)))
1948 (aver (or (eq size :qword) (eq size :word)))
1949 (maybe-emit-operand-size-prefix segment size)
1950 (maybe-emit-rex-for-ea segment src nil :operand-size :do-not-set)
1951 (cond ((register-p src)
1952 (emit-byte-with-reg segment #b01010 (reg-tn-encoding src)))
1954 (emit-byte segment #b11111111)
1955 (emit-ea segment src #b110 :allow-constants t))))))))
1957 (define-instruction pop (segment dst)
1958 (:printer reg-no-width-default-qword ((op #b01011)))
1959 (:printer reg/mem-default-qword ((op '(#b10001111 #b000))))
1960 (:emitter
1961 (let ((size (operand-size dst)))
1962 (aver (or (eq size :qword) (eq size :word)))
1963 (maybe-emit-operand-size-prefix segment size)
1964 (maybe-emit-rex-for-ea segment dst nil :operand-size :do-not-set)
1965 (cond ((register-p dst)
1966 (emit-byte-with-reg segment #b01011 (reg-tn-encoding dst)))
1968 (emit-byte segment #b10001111)
1969 (emit-ea segment dst #b000))))))
1971 ;;; Compared to x86 we need to take two particularities into account
1972 ;;; here:
1973 ;;; * XCHG EAX, EAX can't be encoded as #x90 as the processor interprets
1974 ;;; that opcode as NOP while XCHG EAX, EAX is specified to clear the
1975 ;;; upper half of RAX. We need to use the long form #x87 #xC0 instead.
1976 ;;; * The opcode #x90 is not only used for NOP and XCHG RAX, RAX and
1977 ;;; XCHG AX, AX, but also for XCHG RAX, R8 (and the corresponding 32-
1978 ;;; and 16-bit versions). The printer for the NOP instruction (further
1979 ;;; below) matches all these encodings so needs to be overridden here
1980 ;;; for the cases that need to print as XCHG.
1981 ;;; Assembler and disassembler chained then map these special cases as
1982 ;;; follows:
1983 ;;; (INST NOP) -> 90 -> NOP
1984 ;;; (INST XCHG RAX-TN RAX-TN) -> 4890 -> NOP
1985 ;;; (INST XCHG EAX-TN EAX-TN) -> 87C0 -> XCHG EAX, EAX
1986 ;;; (INST XCHG AX-TN AX-TN) -> 6690 -> NOP
1987 ;;; (INST XCHG RAX-TN R8-TN) -> 4990 -> XCHG RAX, R8
1988 ;;; (INST XCHG EAX-TN R8D-TN) -> 4190 -> XCHG EAX, R8D
1989 ;;; (INST XCHG AX-TN R8W-TN) -> 664190 -> XCHG AX, R8W
1990 ;;; The disassembler additionally correctly matches encoding variants
1991 ;;; that the assembler doesn't generate, for example 4E90 prints as NOP
1992 ;;; and 4F90 as XCHG RAX, R8 (both because REX.R and REX.X are ignored).
1993 (define-instruction xchg (segment operand1 operand2)
1994 ;; This printer matches all patterns that encode exchanging RAX with
1995 ;; R8, EAX with R8D, or AX with R8W. These consist of the opcode #x90
1996 ;; with a REX prefix with REX.B = 1, and possibly the #x66 prefix.
1997 ;; We rely on the prefix automatism for the #x66 prefix, but
1998 ;; explicitly match the REX prefix as we need to provide a value for
1999 ;; REX.B, and to override the NOP printer by virtue of a longer match.
2000 (:printer rex-accum-reg ((rex-b 1) (op #b10010) (reg #b000)))
2001 ;; Register with accumulator.
2002 (:printer reg-no-width ((op #b10010)) '(:name :tab accum ", " reg))
2003 ;; Register/Memory with Register.
2004 (:printer reg-reg/mem ((op #b1000011)))
2005 (:emitter
2006 (let ((size (matching-operand-size operand1 operand2)))
2007 (maybe-emit-operand-size-prefix segment size)
2008 (labels ((xchg-acc-with-something (acc something)
2009 (if (and (not (eq size :byte))
2010 (register-p something)
2011 ;; Don't use the short encoding for XCHG EAX, EAX:
2012 (not (and (= (tn-offset something) eax-offset)
2013 (eq size :dword))))
2014 (progn
2015 (maybe-emit-rex-for-ea segment something acc)
2016 (emit-byte-with-reg segment
2017 #b10010
2018 (reg-tn-encoding something)))
2019 (xchg-reg-with-something acc something)))
2020 (xchg-reg-with-something (reg something)
2021 (maybe-emit-rex-for-ea segment something reg)
2022 (emit-byte segment (if (eq size :byte) #b10000110 #b10000111))
2023 (emit-ea segment something (reg-tn-encoding reg))))
2024 (cond ((accumulator-p operand1)
2025 (xchg-acc-with-something operand1 operand2))
2026 ((accumulator-p operand2)
2027 (xchg-acc-with-something operand2 operand1))
2028 ((register-p operand1)
2029 (xchg-reg-with-something operand1 operand2))
2030 ((register-p operand2)
2031 (xchg-reg-with-something operand2 operand1))
2033 (error "bogus args to XCHG: ~S ~S" operand1 operand2)))))))
2035 ;; It's an error to compile instructions without their labeler and printer defined
2036 ;; in the compiler, even though they aren't called.
2037 ;; This stems from compile-time use of (MAKE-VALSRC #'f '#'f)
2038 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
2040 ;; If the filtered VALUE (R/M field of LEA) should be treated as a label,
2041 ;; return the virtual address, otherwise the value unchanged.
2042 (defun lea-compute-label (value dstate)
2043 (if (and (listp value) (eq (first value) 'rip))
2044 (+ (sb!disassem:dstate-next-addr dstate) (second value))
2045 value))
2047 ;; Figure out whether LEA should print its EA with just the stuff in brackets,
2048 ;; or additionally show the EA as either a label or a hex literal.
2049 (defun lea-print-ea (value stream dstate)
2050 (let ((width (inst-operand-size dstate)))
2051 (etypecase value
2052 (list
2053 ;; Indicate to PRINT-MEM-REF that this is not a memory access.
2054 (print-mem-ref :compute value width stream dstate)
2055 (when (eq (first value) 'rip)
2056 (let ((addr (+ (sb!disassem:dstate-next-addr dstate) (second value))))
2057 (sb!disassem:note (lambda (s) (format s "= #x~x" addr))
2058 dstate))))
2060 ;; We're robust in allowing VALUE to be an integer (a register),
2061 ;; though LEA Rx,Ry is an illegal instruction.
2062 ;; A label should never have memory address of 0 to 15 so this case is
2063 ;; unambiguous for the most part, except maybe in a compiler trace file
2064 ;; which starts disassembling as if the origin were zero.
2065 (full-reg (print-reg-with-width value width stream dstate))
2067 ;; Unfortunately the "label" case sees either an integer or string,
2068 ;; because MAP-SEGMENT-INSTRUCTIONS happens twice (really thrice).
2069 ;; - DETERMINE-OPCODE-BOUNDS in target-insts. Label = integer from prefilter.
2070 ;; - ADD-SEGMENT-LABELS. Never calls instruction printers.
2071 ;; - DISASSEMBLE-SEGMENT. Label = string
2072 ;; and we need a different 'arg-form-kind' than the one in VALUE,
2073 ;; because :USE-LABEL forces the printing pass to see only a label string.
2074 ;; Unlike for JMP and CALL, this isn't reasonable, as no one instruction
2075 ;; corresponds to, say, "LEA RAX,L1". We want [RIP+disp] or [mem_absolute]
2076 ;; so extract the filtered not-labelized value for PRINT-MEM-REF.
2077 ((or string integer)
2078 (print-mem-ref :compute
2079 (reg-r/m-inst-r/m-arg sb!disassem::dchunk-zero dstate)
2080 width stream dstate)
2081 (when (stringp value) ; Don't note anything during -OPCODE-BOUNDS pass
2082 (sb!disassem:note (lambda (s) (format s "= ~A" value)) dstate))))))
2084 ) ; EVAL-WHEN
2086 (define-instruction lea (segment dst src)
2087 (:printer
2088 reg-reg/mem
2089 ((op #b1000110) (width 1)
2090 (reg/mem nil :use-label #'lea-compute-label :printer #'lea-print-ea)))
2091 (:emitter
2092 (aver (or (dword-reg-p dst) (qword-reg-p dst)))
2093 (maybe-emit-rex-for-ea segment src dst
2094 :operand-size (if (dword-reg-p dst) :dword :qword))
2095 (emit-byte segment #b10001101)
2096 (emit-ea segment src (reg-tn-encoding dst))))
2098 (define-instruction cmpxchg (segment dst src &optional prefix)
2099 ;; Register/Memory with Register.
2100 (:printer ext-reg-reg/mem ((op #b1011000)) '(:name :tab reg/mem ", " reg))
2101 (:emitter
2102 (aver (register-p src))
2103 (emit-prefix segment prefix)
2104 (let ((size (matching-operand-size src dst)))
2105 (maybe-emit-operand-size-prefix segment size)
2106 (maybe-emit-rex-for-ea segment dst src)
2107 (emit-byte segment #b00001111)
2108 (emit-byte segment (if (eq size :byte) #b10110000 #b10110001))
2109 (emit-ea segment dst (reg-tn-encoding src)))))
2111 (define-instruction cmpxchg16b (segment mem &optional prefix)
2112 (:printer ext-reg-reg/mem-no-width ((op #xC7)) '(:name :tab reg/mem))
2113 (:emitter
2114 (aver (not (register-p mem)))
2115 (emit-prefix segment prefix)
2116 (maybe-emit-rex-for-ea segment mem nil :operand-size :qword)
2117 (emit-byte segment #x0F)
2118 (emit-byte segment #xC7)
2119 (emit-ea segment mem 1))) ; operand extension
2122 ;;;; flag control instructions
2124 ;;; CLC -- Clear Carry Flag.
2125 (define-instruction clc (segment)
2126 (:printer byte ((op #b11111000)))
2127 (:emitter
2128 (emit-byte segment #b11111000)))
2130 ;;; CLD -- Clear Direction Flag.
2131 (define-instruction cld (segment)
2132 (:printer byte ((op #b11111100)))
2133 (:emitter
2134 (emit-byte segment #b11111100)))
2136 ;;; CLI -- Clear Iterrupt Enable Flag.
2137 (define-instruction cli (segment)
2138 (:printer byte ((op #b11111010)))
2139 (:emitter
2140 (emit-byte segment #b11111010)))
2142 ;;; CMC -- Complement Carry Flag.
2143 (define-instruction cmc (segment)
2144 (:printer byte ((op #b11110101)))
2145 (:emitter
2146 (emit-byte segment #b11110101)))
2148 ;;; LAHF -- Load AH into flags.
2149 (define-instruction lahf (segment)
2150 (:printer byte ((op #b10011111)))
2151 (:emitter
2152 (emit-byte segment #b10011111)))
2154 ;;; POPF -- Pop flags.
2155 (define-instruction popf (segment)
2156 (:printer byte ((op #b10011101)))
2157 (:emitter
2158 (emit-byte segment #b10011101)))
2160 ;;; PUSHF -- push flags.
2161 (define-instruction pushf (segment)
2162 (:printer byte ((op #b10011100)))
2163 (:emitter
2164 (emit-byte segment #b10011100)))
2166 ;;; SAHF -- Store AH into flags.
2167 (define-instruction sahf (segment)
2168 (:printer byte ((op #b10011110)))
2169 (:emitter
2170 (emit-byte segment #b10011110)))
2172 ;;; STC -- Set Carry Flag.
2173 (define-instruction stc (segment)
2174 (:printer byte ((op #b11111001)))
2175 (:emitter
2176 (emit-byte segment #b11111001)))
2178 ;;; STD -- Set Direction Flag.
2179 (define-instruction std (segment)
2180 (:printer byte ((op #b11111101)))
2181 (:emitter
2182 (emit-byte segment #b11111101)))
2184 ;;; STI -- Set Interrupt Enable Flag.
2185 (define-instruction sti (segment)
2186 (:printer byte ((op #b11111011)))
2187 (:emitter
2188 (emit-byte segment #b11111011)))
2190 ;;;; arithmetic
2192 (defun emit-random-arith-inst (name segment dst src opcode
2193 &optional allow-constants)
2194 (let ((size (matching-operand-size dst src)))
2195 (maybe-emit-operand-size-prefix segment size)
2196 (cond
2197 ((integerp src)
2198 (cond ((and (not (eq size :byte)) (<= -128 src 127))
2199 (maybe-emit-rex-for-ea segment dst nil)
2200 (emit-byte segment #b10000011)
2201 (emit-ea segment dst opcode :allow-constants allow-constants)
2202 (emit-byte segment src))
2203 ((accumulator-p dst)
2204 (maybe-emit-rex-for-ea segment dst nil)
2205 (emit-byte segment
2206 (dpb opcode
2207 (byte 3 3)
2208 (if (eq size :byte)
2209 #b00000100
2210 #b00000101)))
2211 (emit-sized-immediate segment size src))
2213 (maybe-emit-rex-for-ea segment dst nil)
2214 (emit-byte segment (if (eq size :byte) #b10000000 #b10000001))
2215 (emit-ea segment dst opcode :allow-constants allow-constants)
2216 (emit-sized-immediate segment size src))))
2217 ((register-p src)
2218 (maybe-emit-rex-for-ea segment dst src)
2219 (emit-byte segment
2220 (dpb opcode
2221 (byte 3 3)
2222 (if (eq size :byte) #b00000000 #b00000001)))
2223 (emit-ea segment dst (reg-tn-encoding src)
2224 :allow-constants allow-constants))
2225 ((register-p dst)
2226 (maybe-emit-rex-for-ea segment src dst)
2227 (emit-byte segment
2228 (dpb opcode
2229 (byte 3 3)
2230 (if (eq size :byte) #b00000010 #b00000011)))
2231 (emit-ea segment src (reg-tn-encoding dst)
2232 :allow-constants allow-constants))
2234 (error "bogus operands to ~A" name)))))
2236 (eval-when (:compile-toplevel :execute)
2237 (defun arith-inst-printer-list (subop)
2238 `((accum-imm ((op ,(dpb subop (byte 3 2) #b0000010))))
2239 (reg/mem-imm ((op (#b1000000 ,subop))))
2240 ;; The redundant encoding #x82 is invalid in 64-bit mode,
2241 ;; therefore we force WIDTH to 1.
2242 (reg/mem-imm ((op (#b1000001 ,subop)) (width 1)
2243 (imm nil :type signed-imm-byte)))
2244 (reg-reg/mem-dir ((op ,(dpb subop (byte 3 1) #b000000)))))))
2246 (define-instruction add (segment dst src &optional prefix)
2247 (:printer-list (arith-inst-printer-list #b000))
2248 (:emitter
2249 (emit-prefix segment prefix)
2250 (emit-random-arith-inst "ADD" segment dst src #b000)))
2252 (define-instruction adc (segment dst src)
2253 (:printer-list (arith-inst-printer-list #b010))
2254 (:emitter (emit-random-arith-inst "ADC" segment dst src #b010)))
2256 (define-instruction sub (segment dst src)
2257 (:printer-list (arith-inst-printer-list #b101))
2258 (:emitter (emit-random-arith-inst "SUB" segment dst src #b101)))
2260 (define-instruction sbb (segment dst src)
2261 (:printer-list (arith-inst-printer-list #b011))
2262 (:emitter (emit-random-arith-inst "SBB" segment dst src #b011)))
2264 (define-instruction cmp (segment dst src)
2265 (:printer-list (arith-inst-printer-list #b111))
2266 (:emitter (emit-random-arith-inst "CMP" segment dst src #b111 t)))
2268 ;;; The one-byte encodings for INC and DEC are used as REX prefixes
2269 ;;; in 64-bit mode so we always use the two-byte form.
2270 (define-instruction inc (segment dst)
2271 (:printer reg/mem ((op '(#b1111111 #b000))))
2272 (:emitter
2273 (let ((size (operand-size dst)))
2274 (maybe-emit-operand-size-prefix segment size)
2275 (maybe-emit-rex-for-ea segment dst nil)
2276 (emit-byte segment (if (eq size :byte) #b11111110 #b11111111))
2277 (emit-ea segment dst #b000))))
2279 (define-instruction dec (segment dst)
2280 (:printer reg/mem ((op '(#b1111111 #b001))))
2281 (:emitter
2282 (let ((size (operand-size dst)))
2283 (maybe-emit-operand-size-prefix segment size)
2284 (maybe-emit-rex-for-ea segment dst nil)
2285 (emit-byte segment (if (eq size :byte) #b11111110 #b11111111))
2286 (emit-ea segment dst #b001))))
2288 (define-instruction neg (segment dst)
2289 (:printer reg/mem ((op '(#b1111011 #b011))))
2290 (:emitter
2291 (let ((size (operand-size dst)))
2292 (maybe-emit-operand-size-prefix segment size)
2293 (maybe-emit-rex-for-ea segment dst nil)
2294 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2295 (emit-ea segment dst #b011))))
2297 (define-instruction mul (segment dst src)
2298 (:printer accum-reg/mem ((op '(#b1111011 #b100))))
2299 (:emitter
2300 (let ((size (matching-operand-size dst src)))
2301 (aver (accumulator-p dst))
2302 (maybe-emit-operand-size-prefix segment size)
2303 (maybe-emit-rex-for-ea segment src nil)
2304 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2305 (emit-ea segment src #b100))))
2307 (define-instruction imul (segment dst &optional src1 src2)
2308 (:printer accum-reg/mem ((op '(#b1111011 #b101))))
2309 (:printer ext-reg-reg/mem-no-width ((op #b10101111)))
2310 (:printer reg-reg/mem ((op #b0110100) (width 1)
2311 (imm nil :type 'signed-imm-data))
2312 '(:name :tab reg ", " reg/mem ", " imm))
2313 (:printer reg-reg/mem ((op #b0110101) (width 1)
2314 (imm nil :type 'signed-imm-byte))
2315 '(:name :tab reg ", " reg/mem ", " imm))
2316 (:emitter
2317 (flet ((r/m-with-immed-to-reg (reg r/m immed)
2318 (let* ((size (matching-operand-size reg r/m))
2319 (sx (and (not (eq size :byte)) (<= -128 immed 127))))
2320 (maybe-emit-operand-size-prefix segment size)
2321 (maybe-emit-rex-for-ea segment r/m reg)
2322 (emit-byte segment (if sx #b01101011 #b01101001))
2323 (emit-ea segment r/m (reg-tn-encoding reg))
2324 (if sx
2325 (emit-byte segment immed)
2326 (emit-sized-immediate segment size immed)))))
2327 (cond (src2
2328 (r/m-with-immed-to-reg dst src1 src2))
2329 (src1
2330 (if (integerp src1)
2331 (r/m-with-immed-to-reg dst dst src1)
2332 (let ((size (matching-operand-size dst src1)))
2333 (maybe-emit-operand-size-prefix segment size)
2334 (maybe-emit-rex-for-ea segment src1 dst)
2335 (emit-byte segment #b00001111)
2336 (emit-byte segment #b10101111)
2337 (emit-ea segment src1 (reg-tn-encoding dst)))))
2339 (let ((size (operand-size dst)))
2340 (maybe-emit-operand-size-prefix segment size)
2341 (maybe-emit-rex-for-ea segment dst nil)
2342 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2343 (emit-ea segment dst #b101)))))))
2345 (define-instruction div (segment dst src)
2346 (:printer accum-reg/mem ((op '(#b1111011 #b110))))
2347 (:emitter
2348 (let ((size (matching-operand-size dst src)))
2349 (aver (accumulator-p dst))
2350 (maybe-emit-operand-size-prefix segment size)
2351 (maybe-emit-rex-for-ea segment src nil)
2352 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2353 (emit-ea segment src #b110))))
2355 (define-instruction idiv (segment dst src)
2356 (:printer accum-reg/mem ((op '(#b1111011 #b111))))
2357 (:emitter
2358 (let ((size (matching-operand-size dst src)))
2359 (aver (accumulator-p dst))
2360 (maybe-emit-operand-size-prefix segment size)
2361 (maybe-emit-rex-for-ea segment src nil)
2362 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2363 (emit-ea segment src #b111))))
2365 (define-instruction bswap (segment dst)
2366 (:printer ext-reg-no-width ((op #b11001)))
2367 (:emitter
2368 (let ((size (operand-size dst)))
2369 (maybe-emit-rex-prefix segment size nil nil dst)
2370 (emit-byte segment #x0f)
2371 (emit-byte-with-reg segment #b11001 (reg-tn-encoding dst)))))
2373 ;;; CBW -- Convert Byte to Word. AX <- sign_xtnd(AL)
2374 (define-instruction cbw (segment)
2375 (:printer x66-byte ((op #b10011000)))
2376 (:emitter
2377 (maybe-emit-operand-size-prefix segment :word)
2378 (emit-byte segment #b10011000)))
2380 ;;; CWDE -- Convert Word To Double Word Extended. EAX <- sign_xtnd(AX)
2381 (define-instruction cwde (segment)
2382 (:printer byte ((op #b10011000)))
2383 (:emitter
2384 (maybe-emit-operand-size-prefix segment :dword)
2385 (emit-byte segment #b10011000)))
2387 ;;; CDQE -- Convert Double Word To Quad Word Extended. RAX <- sign_xtnd(EAX)
2388 (define-instruction cdqe (segment)
2389 (:printer rex-byte ((op #b10011000)))
2390 (:emitter
2391 (maybe-emit-rex-prefix segment :qword nil nil nil)
2392 (emit-byte segment #b10011000)))
2394 ;;; CWD -- Convert Word to Double Word. DX:AX <- sign_xtnd(AX)
2395 (define-instruction cwd (segment)
2396 (:printer x66-byte ((op #b10011001)))
2397 (:emitter
2398 (maybe-emit-operand-size-prefix segment :word)
2399 (emit-byte segment #b10011001)))
2401 ;;; CDQ -- Convert Double Word to Quad Word. EDX:EAX <- sign_xtnd(EAX)
2402 (define-instruction cdq (segment)
2403 (:printer byte ((op #b10011001)))
2404 (:emitter
2405 (maybe-emit-operand-size-prefix segment :dword)
2406 (emit-byte segment #b10011001)))
2408 ;;; CQO -- Convert Quad Word to Octaword. RDX:RAX <- sign_xtnd(RAX)
2409 (define-instruction cqo (segment)
2410 (:printer rex-byte ((op #b10011001)))
2411 (:emitter
2412 (maybe-emit-rex-prefix segment :qword nil nil nil)
2413 (emit-byte segment #b10011001)))
2415 (define-instruction xadd (segment dst src &optional prefix)
2416 ;; Register/Memory with Register.
2417 (:printer ext-reg-reg/mem ((op #b1100000)) '(:name :tab reg/mem ", " reg))
2418 (:emitter
2419 (aver (register-p src))
2420 (emit-prefix segment prefix)
2421 (let ((size (matching-operand-size src dst)))
2422 (maybe-emit-operand-size-prefix segment size)
2423 (maybe-emit-rex-for-ea segment dst src)
2424 (emit-byte segment #b00001111)
2425 (emit-byte segment (if (eq size :byte) #b11000000 #b11000001))
2426 (emit-ea segment dst (reg-tn-encoding src)))))
2429 ;;;; logic
2431 (defun emit-shift-inst (segment dst amount opcode)
2432 (let ((size (operand-size dst)))
2433 (maybe-emit-operand-size-prefix segment size)
2434 (multiple-value-bind (major-opcode immed)
2435 (case amount
2436 (:cl (values #b11010010 nil))
2437 (1 (values #b11010000 nil))
2438 (t (values #b11000000 t)))
2439 (maybe-emit-rex-for-ea segment dst nil)
2440 (emit-byte segment
2441 (if (eq size :byte) major-opcode (logior major-opcode 1)))
2442 (emit-ea segment dst opcode)
2443 (when immed
2444 (emit-byte segment amount)))))
2446 (sb!disassem:define-instruction-format
2447 (shift-inst 16 :include reg/mem
2448 :default-printer '(:name :tab reg/mem ", " (:if (varying :positive) 'cl 1)))
2449 (op :fields (list (byte 6 2) (byte 3 11)))
2450 (varying :field (byte 1 1)))
2452 (macrolet ((define (name subop)
2453 `(define-instruction ,name (segment dst amount)
2454 (:printer shift-inst ((op '(#b110100 ,subop)))) ; shift by CL or 1
2455 (:printer reg/mem-imm ((op '(#b1100000 ,subop))
2456 (imm nil :type 'imm-byte)))
2457 (:emitter (emit-shift-inst segment dst amount ,subop)))))
2458 (define rol #b000)
2459 (define ror #b001)
2460 (define rcl #b010)
2461 (define rcr #b011)
2462 (define shl #b100)
2463 (define shr #b101)
2464 (define sar #b111))
2466 (defun emit-double-shift (segment opcode dst src amt)
2467 (let ((size (matching-operand-size dst src)))
2468 (when (eq size :byte)
2469 (error "Double shifts can only be used with words."))
2470 (maybe-emit-operand-size-prefix segment size)
2471 (maybe-emit-rex-for-ea segment dst src)
2472 (emit-byte segment #b00001111)
2473 (emit-byte segment (dpb opcode (byte 1 3)
2474 (if (eq amt :cl) #b10100101 #b10100100)))
2475 (emit-ea segment dst (reg-tn-encoding src))
2476 (unless (eq amt :cl)
2477 (emit-byte segment amt))))
2479 (eval-when (:compile-toplevel :execute)
2480 (defun double-shift-inst-printer-list (op)
2481 `((ext-reg-reg/mem-no-width ((op ,(logior op #b100))
2482 (imm nil :type imm-byte))
2483 (:name :tab reg/mem ", " reg ", " imm))
2484 (ext-reg-reg/mem-no-width ((op ,(logior op #b101)))
2485 (:name :tab reg/mem ", " reg ", " 'cl)))))
2487 (define-instruction shld (segment dst src amt)
2488 (:declare (type (or (member :cl) (mod 64)) amt))
2489 (:printer-list (double-shift-inst-printer-list #b10100000))
2490 (:emitter
2491 (emit-double-shift segment #b0 dst src amt)))
2493 (define-instruction shrd (segment dst src amt)
2494 (:declare (type (or (member :cl) (mod 64)) amt))
2495 (:printer-list (double-shift-inst-printer-list #b10101000))
2496 (:emitter
2497 (emit-double-shift segment #b1 dst src amt)))
2499 (define-instruction and (segment dst src)
2500 (:printer-list
2501 (arith-inst-printer-list #b100))
2502 (:emitter
2503 (emit-random-arith-inst "AND" segment dst src #b100)))
2505 (define-instruction test (segment this that)
2506 (:printer accum-imm ((op #b1010100)))
2507 (:printer reg/mem-imm ((op '(#b1111011 #b000))))
2508 (:printer reg-reg/mem ((op #b1000010)))
2509 (:emitter
2510 (let ((size (matching-operand-size this that)))
2511 (maybe-emit-operand-size-prefix segment size)
2512 (flet ((test-immed-and-something (immed something)
2513 (cond ((accumulator-p something)
2514 (maybe-emit-rex-for-ea segment something nil)
2515 (emit-byte segment
2516 (if (eq size :byte) #b10101000 #b10101001))
2517 (emit-sized-immediate segment size immed))
2519 (maybe-emit-rex-for-ea segment something nil)
2520 (emit-byte segment
2521 (if (eq size :byte) #b11110110 #b11110111))
2522 (emit-ea segment something #b000)
2523 (emit-sized-immediate segment size immed))))
2524 (test-reg-and-something (reg something)
2525 (maybe-emit-rex-for-ea segment something reg)
2526 (emit-byte segment (if (eq size :byte) #b10000100 #b10000101))
2527 (emit-ea segment something (reg-tn-encoding reg))))
2528 (cond ((integerp that)
2529 (test-immed-and-something that this))
2530 ((integerp this)
2531 (test-immed-and-something this that))
2532 ((register-p this)
2533 (test-reg-and-something this that))
2534 ((register-p that)
2535 (test-reg-and-something that this))
2537 (error "bogus operands for TEST: ~S and ~S" this that)))))))
2539 ;;; Emit the most compact form of the test immediate instruction,
2540 ;;; using an 8 bit test when the immediate is only 8 bits and the
2541 ;;; value is one of the four low registers (rax, rbx, rcx, rdx) or the
2542 ;;; control stack.
2543 (defun emit-optimized-test-inst (x y)
2544 (typecase y
2545 ((unsigned-byte 7)
2546 (let ((offset (tn-offset x)))
2547 (cond ((and (sc-is x any-reg descriptor-reg)
2548 (or (= offset rax-offset) (= offset rbx-offset)
2549 (= offset rcx-offset) (= offset rdx-offset)))
2550 (inst test (reg-in-size x :byte) y))
2551 ((sc-is x control-stack)
2552 (inst test (make-ea :byte :base rbp-tn
2553 :disp (frame-byte-offset offset))
2556 (inst test x y)))))
2558 (inst test x y))))
2560 (define-instruction or (segment dst src)
2561 (:printer-list
2562 (arith-inst-printer-list #b001))
2563 (:emitter
2564 (emit-random-arith-inst "OR" segment dst src #b001)))
2566 (define-instruction xor (segment dst src)
2567 (:printer-list
2568 (arith-inst-printer-list #b110))
2569 (:emitter
2570 (emit-random-arith-inst "XOR" segment dst src #b110)))
2572 (define-instruction not (segment dst)
2573 (:printer reg/mem ((op '(#b1111011 #b010))))
2574 (:emitter
2575 (let ((size (operand-size dst)))
2576 (maybe-emit-operand-size-prefix segment size)
2577 (maybe-emit-rex-for-ea segment dst nil)
2578 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2579 (emit-ea segment dst #b010))))
2581 ;;;; string manipulation
2583 (define-instruction cmps (segment size)
2584 (:printer string-op ((op #b1010011)))
2585 (:emitter
2586 (maybe-emit-operand-size-prefix segment size)
2587 (maybe-emit-rex-prefix segment size nil nil nil)
2588 (emit-byte segment (if (eq size :byte) #b10100110 #b10100111))))
2590 (define-instruction ins (segment acc)
2591 (:printer string-op ((op #b0110110)))
2592 (:emitter
2593 (let ((size (operand-size acc)))
2594 (aver (accumulator-p acc))
2595 (maybe-emit-operand-size-prefix segment size)
2596 (maybe-emit-rex-prefix segment size nil nil nil)
2597 (emit-byte segment (if (eq size :byte) #b01101100 #b01101101)))))
2599 (define-instruction lods (segment acc)
2600 (:printer string-op ((op #b1010110)))
2601 (:emitter
2602 (let ((size (operand-size acc)))
2603 (aver (accumulator-p acc))
2604 (maybe-emit-operand-size-prefix segment size)
2605 (maybe-emit-rex-prefix segment size nil nil nil)
2606 (emit-byte segment (if (eq size :byte) #b10101100 #b10101101)))))
2608 (define-instruction movs (segment size)
2609 (:printer string-op ((op #b1010010)))
2610 (:emitter
2611 (maybe-emit-operand-size-prefix segment size)
2612 (maybe-emit-rex-prefix segment size nil nil nil)
2613 (emit-byte segment (if (eq size :byte) #b10100100 #b10100101))))
2615 (define-instruction outs (segment acc)
2616 (:printer string-op ((op #b0110111)))
2617 (:emitter
2618 (let ((size (operand-size acc)))
2619 (aver (accumulator-p acc))
2620 (maybe-emit-operand-size-prefix segment size)
2621 (maybe-emit-rex-prefix segment size nil nil nil)
2622 (emit-byte segment (if (eq size :byte) #b01101110 #b01101111)))))
2624 (define-instruction scas (segment acc)
2625 (:printer string-op ((op #b1010111)))
2626 (:emitter
2627 (let ((size (operand-size acc)))
2628 (aver (accumulator-p acc))
2629 (maybe-emit-operand-size-prefix segment size)
2630 (maybe-emit-rex-prefix segment size nil nil nil)
2631 (emit-byte segment (if (eq size :byte) #b10101110 #b10101111)))))
2633 (define-instruction stos (segment acc)
2634 (:printer string-op ((op #b1010101)))
2635 (:emitter
2636 (let ((size (operand-size acc)))
2637 (aver (accumulator-p acc))
2638 (maybe-emit-operand-size-prefix segment size)
2639 (maybe-emit-rex-prefix segment size nil nil nil)
2640 (emit-byte segment (if (eq size :byte) #b10101010 #b10101011)))))
2642 (define-instruction xlat (segment)
2643 (:printer byte ((op #b11010111)))
2644 (:emitter
2645 (emit-byte segment #b11010111)))
2648 ;;;; bit manipulation
2650 (define-instruction bsf (segment dst src)
2651 (:printer ext-reg-reg/mem-no-width ((op #b10111100)))
2652 (:emitter
2653 (let ((size (matching-operand-size dst src)))
2654 (when (eq size :byte)
2655 (error "can't scan bytes: ~S" src))
2656 (maybe-emit-operand-size-prefix segment size)
2657 (maybe-emit-rex-for-ea segment src dst)
2658 (emit-byte segment #b00001111)
2659 (emit-byte segment #b10111100)
2660 (emit-ea segment src (reg-tn-encoding dst)))))
2662 (define-instruction bsr (segment dst src)
2663 (:printer ext-reg-reg/mem-no-width ((op #b10111101)))
2664 (:emitter
2665 (let ((size (matching-operand-size dst src)))
2666 (when (eq size :byte)
2667 (error "can't scan bytes: ~S" src))
2668 (maybe-emit-operand-size-prefix segment size)
2669 (maybe-emit-rex-for-ea segment src dst)
2670 (emit-byte segment #b00001111)
2671 (emit-byte segment #b10111101)
2672 (emit-ea segment src (reg-tn-encoding dst)))))
2674 (defun emit-bit-test-and-mumble (segment src index opcode)
2675 (let ((size (operand-size src)))
2676 (when (eq size :byte)
2677 (error "can't scan bytes: ~S" src))
2678 (maybe-emit-operand-size-prefix segment size)
2679 (cond ((integerp index)
2680 (maybe-emit-rex-for-ea segment src nil)
2681 (emit-byte segment #b00001111)
2682 (emit-byte segment #b10111010)
2683 (emit-ea segment src opcode)
2684 (emit-byte segment index))
2686 (maybe-emit-rex-for-ea segment src index)
2687 (emit-byte segment #b00001111)
2688 (emit-byte segment (dpb opcode (byte 3 3) #b10000011))
2689 (emit-ea segment src (reg-tn-encoding index))))))
2691 (eval-when (:compile-toplevel :execute)
2692 (defun bit-test-inst-printer-list (subop)
2693 `((ext-reg/mem-no-width+imm8 ((op (#xBA ,subop))))
2694 (ext-reg-reg/mem-no-width ((op ,(dpb subop (byte 3 3) #b10000011))
2695 (reg/mem nil :type sized-reg/mem))
2696 (:name :tab reg/mem ", " reg)))))
2698 (macrolet ((define (inst opcode-extension)
2699 `(define-instruction ,inst (segment src index)
2700 (:printer-list (bit-test-inst-printer-list ,opcode-extension))
2701 (:emitter (emit-bit-test-and-mumble segment src index
2702 ,opcode-extension)))))
2703 (define bt 4)
2704 (define bts 5)
2705 (define btr 6)
2706 (define btc 7))
2709 ;;;; control transfer
2711 (define-instruction call (segment where)
2712 (:printer near-jump ((op #b11101000)))
2713 (:printer reg/mem-default-qword ((op '(#b11111111 #b010))))
2714 (:emitter
2715 (typecase where
2716 (label
2717 (emit-byte segment #b11101000) ; 32 bit relative
2718 (emit-back-patch segment
2720 (lambda (segment posn)
2721 (emit-signed-dword segment
2722 (- (label-position where)
2723 (+ posn 4))))))
2724 (fixup
2725 ;; There is no CALL rel64...
2726 (error "Cannot CALL a fixup: ~S" where))
2728 (maybe-emit-rex-for-ea segment where nil :operand-size :do-not-set)
2729 (emit-byte segment #b11111111)
2730 (emit-ea segment where #b010)))))
2732 (defun emit-byte-displacement-backpatch (segment target)
2733 (emit-back-patch segment
2735 (lambda (segment posn)
2736 (let ((disp (- (label-position target) (1+ posn))))
2737 (aver (<= -128 disp 127))
2738 (emit-byte segment disp)))))
2740 (define-instruction jmp (segment cond &optional where)
2741 ;; conditional jumps
2742 (:printer short-cond-jump ((op #b0111)) '('j cc :tab label))
2743 (:printer near-cond-jump () '('j cc :tab label))
2744 ;; unconditional jumps
2745 (:printer short-jump ((op #b1011)))
2746 (:printer near-jump ((op #b11101001)))
2747 (:printer reg/mem-default-qword ((op '(#b11111111 #b100))))
2748 (:emitter
2749 (cond (where
2750 (emit-chooser
2751 segment 6 2
2752 (lambda (segment posn delta-if-after)
2753 (let ((disp (- (label-position where posn delta-if-after)
2754 (+ posn 2))))
2755 (when (<= -128 disp 127)
2756 (emit-byte segment
2757 (dpb (conditional-opcode cond)
2758 (byte 4 0)
2759 #b01110000))
2760 (emit-byte-displacement-backpatch segment where)
2761 t)))
2762 (lambda (segment posn)
2763 (let ((disp (- (label-position where) (+ posn 6))))
2764 (emit-byte segment #b00001111)
2765 (emit-byte segment
2766 (dpb (conditional-opcode cond)
2767 (byte 4 0)
2768 #b10000000))
2769 (emit-signed-dword segment disp)))))
2770 ((label-p (setq where cond))
2771 (emit-chooser
2772 segment 5 0
2773 (lambda (segment posn delta-if-after)
2774 (let ((disp (- (label-position where posn delta-if-after)
2775 (+ posn 2))))
2776 (when (<= -128 disp 127)
2777 (emit-byte segment #b11101011)
2778 (emit-byte-displacement-backpatch segment where)
2779 t)))
2780 (lambda (segment posn)
2781 (let ((disp (- (label-position where) (+ posn 5))))
2782 (emit-byte segment #b11101001)
2783 (emit-signed-dword segment disp)))))
2784 ((fixup-p where)
2785 (emit-byte segment #b11101001)
2786 (emit-relative-fixup segment where))
2788 (unless (or (ea-p where) (tn-p where))
2789 (error "don't know what to do with ~A" where))
2790 ;; near jump defaults to 64 bit
2791 ;; w-bit in rex prefix is unnecessary
2792 (maybe-emit-rex-for-ea segment where nil :operand-size :do-not-set)
2793 (emit-byte segment #b11111111)
2794 (emit-ea segment where #b100)))))
2796 (define-instruction ret (segment &optional stack-delta)
2797 (:printer byte ((op #b11000011)))
2798 (:printer byte ((op #b11000010) (imm nil :type 'imm-word-16))
2799 '(:name :tab imm))
2800 (:emitter
2801 (cond ((and stack-delta (not (zerop stack-delta)))
2802 (emit-byte segment #b11000010)
2803 (emit-word segment stack-delta))
2805 (emit-byte segment #b11000011)))))
2807 (define-instruction jrcxz (segment target)
2808 (:printer short-jump ((op #b0011)))
2809 (:emitter
2810 (emit-byte segment #b11100011)
2811 (emit-byte-displacement-backpatch segment target)))
2813 (define-instruction loop (segment target)
2814 (:printer short-jump ((op #b0010)))
2815 (:emitter
2816 (emit-byte segment #b11100010) ; pfw this was 11100011, or jecxz!!!!
2817 (emit-byte-displacement-backpatch segment target)))
2819 (define-instruction loopz (segment target)
2820 (:printer short-jump ((op #b0001)))
2821 (:emitter
2822 (emit-byte segment #b11100001)
2823 (emit-byte-displacement-backpatch segment target)))
2825 (define-instruction loopnz (segment target)
2826 (:printer short-jump ((op #b0000)))
2827 (:emitter
2828 (emit-byte segment #b11100000)
2829 (emit-byte-displacement-backpatch segment target)))
2831 ;;;; conditional move
2832 (define-instruction cmov (segment cond dst src)
2833 (:printer cond-move ())
2834 (:emitter
2835 (aver (register-p dst))
2836 (let ((size (matching-operand-size dst src)))
2837 (aver (or (eq size :word) (eq size :dword) (eq size :qword)))
2838 (maybe-emit-operand-size-prefix segment size))
2839 (maybe-emit-rex-for-ea segment src dst)
2840 (emit-byte segment #b00001111)
2841 (emit-byte segment (dpb (conditional-opcode cond) (byte 4 0) #b01000000))
2842 (emit-ea segment src (reg-tn-encoding dst) :allow-constants t)))
2844 ;;;; conditional byte set
2846 (define-instruction set (segment dst cond)
2847 (:printer cond-set ())
2848 (:emitter
2849 (maybe-emit-rex-for-ea segment dst nil :operand-size :byte)
2850 (emit-byte segment #b00001111)
2851 (emit-byte segment (dpb (conditional-opcode cond) (byte 4 0) #b10010000))
2852 (emit-ea segment dst #b000)))
2854 ;;;; enter/leave
2856 (define-instruction enter (segment disp &optional (level 0))
2857 (:declare (type (unsigned-byte 16) disp)
2858 (type (unsigned-byte 8) level))
2859 (:printer enter-format ((op #b11001000)))
2860 (:emitter
2861 (emit-byte segment #b11001000)
2862 (emit-word segment disp)
2863 (emit-byte segment level)))
2865 (define-instruction leave (segment)
2866 (:printer byte ((op #b11001001)))
2867 (:emitter
2868 (emit-byte segment #b11001001)))
2870 ;;;; interrupt instructions
2872 (defun snarf-error-junk (sap offset &optional length-only)
2873 (let* ((length (sap-ref-8 sap offset))
2874 (vector (make-array length :element-type '(unsigned-byte 8))))
2875 (declare (type system-area-pointer sap)
2876 (type (unsigned-byte 8) length)
2877 (type (simple-array (unsigned-byte 8) (*)) vector))
2878 (cond (length-only
2879 (values 0 (1+ length) nil nil))
2881 (copy-ub8-from-system-area sap (1+ offset) vector 0 length)
2882 (collect ((sc-offsets)
2883 (lengths))
2884 (lengths 1) ; the length byte
2885 (let* ((index 0)
2886 (error-number (read-var-integer vector index)))
2887 (lengths index)
2888 (loop
2889 (when (>= index length)
2890 (return))
2891 (let ((old-index index))
2892 (sc-offsets (read-var-integer vector index))
2893 (lengths (- index old-index))))
2894 (values error-number
2895 (1+ length)
2896 (sc-offsets)
2897 (lengths))))))))
2900 (defmacro break-cases (breaknum &body cases)
2901 (let ((bn-temp (gensym)))
2902 (collect ((clauses))
2903 (dolist (case cases)
2904 (clauses `((= ,bn-temp ,(car case)) ,@(cdr case))))
2905 `(let ((,bn-temp ,breaknum))
2906 (cond ,@(clauses))))))
2909 (defun break-control (chunk inst stream dstate)
2910 (declare (ignore inst))
2911 (flet ((nt (x) (if stream (sb!disassem:note x dstate))))
2912 (case #!-ud2-breakpoints (byte-imm-code chunk dstate)
2913 #!+ud2-breakpoints (word-imm-code chunk dstate)
2914 (#.error-trap
2915 (nt "error trap")
2916 (sb!disassem:handle-break-args #'snarf-error-junk stream dstate))
2917 (#.cerror-trap
2918 (nt "cerror trap")
2919 (sb!disassem:handle-break-args #'snarf-error-junk stream dstate))
2920 (#.breakpoint-trap
2921 (nt "breakpoint trap"))
2922 (#.pending-interrupt-trap
2923 (nt "pending interrupt trap"))
2924 (#.halt-trap
2925 (nt "halt trap"))
2926 (#.fun-end-breakpoint-trap
2927 (nt "function end breakpoint trap"))
2928 (#.single-step-around-trap
2929 (nt "single-step trap (around)"))
2930 (#.single-step-before-trap
2931 (nt "single-step trap (before)"))
2932 (#.invalid-arg-count-trap
2933 (nt "Invalid argument count trap")))))
2935 (define-instruction break (segment code)
2936 (:declare (type (unsigned-byte 8) code))
2937 #!-ud2-breakpoints (:printer byte-imm ((op #b11001100))
2938 '(:name :tab code) :control #'break-control)
2939 #!+ud2-breakpoints (:printer word-imm ((op #b0000101100001111))
2940 '(:name :tab code) :control #'break-control)
2941 (:emitter
2942 #!-ud2-breakpoints (emit-byte segment #b11001100)
2943 ;; On darwin, trap handling via SIGTRAP is unreliable, therefore we
2944 ;; throw a sigill with 0x0b0f instead and check for this in the
2945 ;; SIGILL handler and pass it on to the sigtrap handler if
2946 ;; appropriate
2947 #!+ud2-breakpoints (emit-word segment #b0000101100001111)
2948 (emit-byte segment code)))
2950 (define-instruction int (segment number)
2951 (:declare (type (unsigned-byte 8) number))
2952 (:printer byte-imm ((op #b11001101)))
2953 (:emitter
2954 (etypecase number
2955 ((member 3)
2956 (emit-byte segment #b11001100))
2957 ((unsigned-byte 8)
2958 (emit-byte segment #b11001101)
2959 (emit-byte segment number)))))
2961 (define-instruction iret (segment)
2962 (:printer byte ((op #b11001111)))
2963 (:emitter
2964 (emit-byte segment #b11001111)))
2966 ;;;; processor control
2968 (define-instruction hlt (segment)
2969 (:printer byte ((op #b11110100)))
2970 (:emitter
2971 (emit-byte segment #b11110100)))
2973 (define-instruction nop (segment)
2974 (:printer byte ((op #b10010000)))
2975 ;; multi-byte NOP
2976 (:printer ext-reg/mem-no-width ((op '(#x1f 0))) '(:name))
2977 (:emitter
2978 (emit-byte segment #b10010000)))
2980 ;;; Emit a sequence of single- or multi-byte NOPs to fill AMOUNT many
2981 ;;; bytes with the smallest possible number of such instructions.
2982 (defun emit-long-nop (segment amount)
2983 (declare (type segment segment)
2984 (type index amount))
2985 ;; Pack all instructions into one byte vector to save space.
2986 (let* ((bytes #.(!coerce-to-specialized
2987 #(#x90
2988 #x66 #x90
2989 #x0f #x1f #x00
2990 #x0f #x1f #x40 #x00
2991 #x0f #x1f #x44 #x00 #x00
2992 #x66 #x0f #x1f #x44 #x00 #x00
2993 #x0f #x1f #x80 #x00 #x00 #x00 #x00
2994 #x0f #x1f #x84 #x00 #x00 #x00 #x00 #x00
2995 #x66 #x0f #x1f #x84 #x00 #x00 #x00 #x00 #x00)
2996 '(unsigned-byte 8)))
2997 (max-length (isqrt (* 2 (length bytes)))))
2998 (loop
2999 (let* ((count (min amount max-length))
3000 (start (ash (* count (1- count)) -1)))
3001 (dotimes (i count)
3002 (emit-byte segment (aref bytes (+ start i)))))
3003 (if (> amount max-length)
3004 (decf amount max-length)
3005 (return)))))
3007 (define-instruction wait (segment)
3008 (:printer byte ((op #b10011011)))
3009 (:emitter
3010 (emit-byte segment #b10011011)))
3013 ;;;; miscellaneous hackery
3015 (define-instruction byte (segment byte)
3016 (:emitter
3017 (emit-byte segment byte)))
3019 (define-instruction word (segment word)
3020 (:emitter
3021 (emit-word segment word)))
3023 (define-instruction dword (segment dword)
3024 (:emitter
3025 (emit-dword segment dword)))
3027 (defun emit-header-data (segment type)
3028 (emit-back-patch segment
3029 n-word-bytes
3030 (lambda (segment posn)
3031 (emit-qword segment
3032 (logior type
3033 (ash (+ posn
3034 (component-header-length))
3035 (- n-widetag-bits
3036 word-shift)))))))
3038 (define-instruction simple-fun-header-word (segment)
3039 (:emitter
3040 (emit-header-data segment simple-fun-header-widetag)))
3042 (define-instruction lra-header-word (segment)
3043 (:emitter
3044 (emit-header-data segment return-pc-header-widetag)))
3046 ;;;; Instructions required to do floating point operations using SSE
3048 ;; Return a one- or two-element list of printers for SSE instructions.
3049 ;; The one-element list is used in the cases where the REX prefix is
3050 ;; really a prefix and thus automatically supported, the two-element
3051 ;; list is used when the REX prefix is used in an infix position.
3052 (eval-when (:compile-toplevel :execute)
3053 (defun sse-inst-printer-list (inst-format-stem prefix opcode
3054 &key more-fields printer)
3055 (let ((fields `(,@(when prefix
3056 `((prefix ,prefix)))
3057 (op ,opcode)
3058 ,@more-fields))
3059 (inst-formats (if prefix
3060 (list (symbolicate "EXT-" inst-format-stem)
3061 (symbolicate "EXT-REX-" inst-format-stem))
3062 (list inst-format-stem))))
3063 (mapcar (lambda (inst-format)
3064 `(,inst-format ,fields ,@(when printer
3065 (list printer))))
3066 inst-formats)))
3067 (defun 2byte-sse-inst-printer-list (inst-format-stem prefix op1 op2
3068 &key more-fields printer)
3069 (let ((fields `(,@(when prefix
3070 `((prefix, prefix)))
3071 (op1 ,op1)
3072 (op2 ,op2)
3073 ,@more-fields))
3074 (inst-formats (if prefix
3075 (list (symbolicate "EXT-" inst-format-stem)
3076 (symbolicate "EXT-REX-" inst-format-stem))
3077 (list inst-format-stem))))
3078 (mapcar (lambda (inst-format)
3079 `(,inst-format ,fields ,@(when printer
3080 (list printer))))
3081 inst-formats))))
3083 (defun emit-sse-inst (segment dst src prefix opcode
3084 &key operand-size (remaining-bytes 0))
3085 (when prefix
3086 (emit-byte segment prefix))
3087 (if operand-size
3088 (maybe-emit-rex-for-ea segment src dst :operand-size operand-size)
3089 (maybe-emit-rex-for-ea segment src dst))
3090 (emit-byte segment #x0f)
3091 (emit-byte segment opcode)
3092 (emit-ea segment src (reg-tn-encoding dst) :remaining-bytes remaining-bytes))
3094 ;; 0110 0110:0000 1111:0111 00gg: 11 010 xmmreg:imm8
3096 (defun emit-sse-inst-with-imm (segment dst/src imm
3097 prefix opcode /i
3098 &key operand-size)
3099 (aver (<= 0 /i 7))
3100 (when prefix
3101 (emit-byte segment prefix))
3102 ;; dst/src is encoded in the r/m field, not r; REX.B must be
3103 ;; set to use extended XMM registers
3104 (maybe-emit-rex-prefix segment operand-size nil nil dst/src)
3105 (emit-byte segment #x0F)
3106 (emit-byte segment opcode)
3107 (emit-byte segment (logior (ash (logior #b11000 /i) 3)
3108 (reg-tn-encoding dst/src)))
3109 (emit-byte segment imm))
3111 (defun emit-sse-inst-2byte (segment dst src prefix op1 op2
3112 &key operand-size (remaining-bytes 0))
3113 (when prefix
3114 (emit-byte segment prefix))
3115 (if operand-size
3116 (maybe-emit-rex-for-ea segment src dst :operand-size operand-size)
3117 (maybe-emit-rex-for-ea segment src dst))
3118 (emit-byte segment #x0f)
3119 (emit-byte segment op1)
3120 (emit-byte segment op2)
3121 (emit-ea segment src (reg-tn-encoding dst) :remaining-bytes remaining-bytes))
3123 (macrolet
3124 ((define-imm-sse-instruction (name opcode /i)
3125 `(define-instruction ,name (segment dst/src imm)
3126 (:printer-list
3127 ',(sse-inst-printer-list 'xmm-imm #x66 opcode
3128 :more-fields `((/i ,/i))))
3129 (:emitter
3130 (emit-sse-inst-with-imm segment dst/src imm
3131 #x66 ,opcode ,/i
3132 :operand-size :do-not-set)))))
3133 (define-imm-sse-instruction pslldq #x73 7)
3134 (define-imm-sse-instruction psllw-imm #x71 6)
3135 (define-imm-sse-instruction pslld-imm #x72 6)
3136 (define-imm-sse-instruction psllq-imm #x73 6)
3138 (define-imm-sse-instruction psraw-imm #x71 4)
3139 (define-imm-sse-instruction psrad-imm #x72 4)
3141 (define-imm-sse-instruction psrldq #x73 3)
3142 (define-imm-sse-instruction psrlw-imm #x71 2)
3143 (define-imm-sse-instruction psrld-imm #x72 2)
3144 (define-imm-sse-instruction psrlq-imm #x73 2))
3146 ;;; Emit an SSE instruction that has an XMM register as the destination
3147 ;;; operand and for which the size of the operands is implicitly given
3148 ;;; by the instruction.
3149 (defun emit-regular-sse-inst (segment dst src prefix opcode
3150 &key (remaining-bytes 0))
3151 (aver (xmm-register-p dst))
3152 (emit-sse-inst segment dst src prefix opcode
3153 :operand-size :do-not-set
3154 :remaining-bytes remaining-bytes))
3156 (defun emit-regular-2byte-sse-inst (segment dst src prefix op1 op2
3157 &key (remaining-bytes 0))
3158 (aver (xmm-register-p dst))
3159 (emit-sse-inst-2byte segment dst src prefix op1 op2
3160 :operand-size :do-not-set
3161 :remaining-bytes remaining-bytes))
3163 ;;; Instructions having an XMM register as the destination operand
3164 ;;; and an XMM register or a memory location as the source operand.
3165 ;;; The operand size is implicitly given by the instruction.
3167 (macrolet ((define-regular-sse-inst (name prefix opcode)
3168 `(define-instruction ,name (segment dst src)
3169 (:printer-list
3170 ',(sse-inst-printer-list 'xmm-xmm/mem prefix opcode))
3171 (:emitter
3172 (emit-regular-sse-inst segment dst src ,prefix ,opcode)))))
3173 ;; moves
3174 (define-regular-sse-inst movshdup #xf3 #x16)
3175 (define-regular-sse-inst movsldup #xf3 #x12)
3176 (define-regular-sse-inst movddup #xf2 #x12)
3177 ;; logical
3178 (define-regular-sse-inst andpd #x66 #x54)
3179 (define-regular-sse-inst andps nil #x54)
3180 (define-regular-sse-inst andnpd #x66 #x55)
3181 (define-regular-sse-inst andnps nil #x55)
3182 (define-regular-sse-inst orpd #x66 #x56)
3183 (define-regular-sse-inst orps nil #x56)
3184 (define-regular-sse-inst pand #x66 #xdb)
3185 (define-regular-sse-inst pandn #x66 #xdf)
3186 (define-regular-sse-inst por #x66 #xeb)
3187 (define-regular-sse-inst pxor #x66 #xef)
3188 (define-regular-sse-inst xorpd #x66 #x57)
3189 (define-regular-sse-inst xorps nil #x57)
3190 ;; comparison
3191 (define-regular-sse-inst comisd #x66 #x2f)
3192 (define-regular-sse-inst comiss nil #x2f)
3193 (define-regular-sse-inst ucomisd #x66 #x2e)
3194 (define-regular-sse-inst ucomiss nil #x2e)
3195 ;; integer comparison
3196 (define-regular-sse-inst pcmpeqb #x66 #x74)
3197 (define-regular-sse-inst pcmpeqw #x66 #x75)
3198 (define-regular-sse-inst pcmpeqd #x66 #x76)
3199 (define-regular-sse-inst pcmpgtb #x66 #x64)
3200 (define-regular-sse-inst pcmpgtw #x66 #x65)
3201 (define-regular-sse-inst pcmpgtd #x66 #x66)
3202 ;; max/min
3203 (define-regular-sse-inst maxpd #x66 #x5f)
3204 (define-regular-sse-inst maxps nil #x5f)
3205 (define-regular-sse-inst maxsd #xf2 #x5f)
3206 (define-regular-sse-inst maxss #xf3 #x5f)
3207 (define-regular-sse-inst minpd #x66 #x5d)
3208 (define-regular-sse-inst minps nil #x5d)
3209 (define-regular-sse-inst minsd #xf2 #x5d)
3210 (define-regular-sse-inst minss #xf3 #x5d)
3211 ;; integer max/min
3212 (define-regular-sse-inst pmaxsw #x66 #xee)
3213 (define-regular-sse-inst pmaxub #x66 #xde)
3214 (define-regular-sse-inst pminsw #x66 #xea)
3215 (define-regular-sse-inst pminub #x66 #xda)
3216 ;; arithmetic
3217 (define-regular-sse-inst addpd #x66 #x58)
3218 (define-regular-sse-inst addps nil #x58)
3219 (define-regular-sse-inst addsd #xf2 #x58)
3220 (define-regular-sse-inst addss #xf3 #x58)
3221 (define-regular-sse-inst addsubpd #x66 #xd0)
3222 (define-regular-sse-inst addsubps #xf2 #xd0)
3223 (define-regular-sse-inst divpd #x66 #x5e)
3224 (define-regular-sse-inst divps nil #x5e)
3225 (define-regular-sse-inst divsd #xf2 #x5e)
3226 (define-regular-sse-inst divss #xf3 #x5e)
3227 (define-regular-sse-inst haddpd #x66 #x7c)
3228 (define-regular-sse-inst haddps #xf2 #x7c)
3229 (define-regular-sse-inst hsubpd #x66 #x7d)
3230 (define-regular-sse-inst hsubps #xf2 #x7d)
3231 (define-regular-sse-inst mulpd #x66 #x59)
3232 (define-regular-sse-inst mulps nil #x59)
3233 (define-regular-sse-inst mulsd #xf2 #x59)
3234 (define-regular-sse-inst mulss #xf3 #x59)
3235 (define-regular-sse-inst rcpps nil #x53)
3236 (define-regular-sse-inst rcpss #xf3 #x53)
3237 (define-regular-sse-inst rsqrtps nil #x52)
3238 (define-regular-sse-inst rsqrtss #xf3 #x52)
3239 (define-regular-sse-inst sqrtpd #x66 #x51)
3240 (define-regular-sse-inst sqrtps nil #x51)
3241 (define-regular-sse-inst sqrtsd #xf2 #x51)
3242 (define-regular-sse-inst sqrtss #xf3 #x51)
3243 (define-regular-sse-inst subpd #x66 #x5c)
3244 (define-regular-sse-inst subps nil #x5c)
3245 (define-regular-sse-inst subsd #xf2 #x5c)
3246 (define-regular-sse-inst subss #xf3 #x5c)
3247 (define-regular-sse-inst unpckhpd #x66 #x15)
3248 (define-regular-sse-inst unpckhps nil #x15)
3249 (define-regular-sse-inst unpcklpd #x66 #x14)
3250 (define-regular-sse-inst unpcklps nil #x14)
3251 ;; integer arithmetic
3252 (define-regular-sse-inst paddb #x66 #xfc)
3253 (define-regular-sse-inst paddw #x66 #xfd)
3254 (define-regular-sse-inst paddd #x66 #xfe)
3255 (define-regular-sse-inst paddq #x66 #xd4)
3256 (define-regular-sse-inst paddsb #x66 #xec)
3257 (define-regular-sse-inst paddsw #x66 #xed)
3258 (define-regular-sse-inst paddusb #x66 #xdc)
3259 (define-regular-sse-inst paddusw #x66 #xdd)
3260 (define-regular-sse-inst pavgb #x66 #xe0)
3261 (define-regular-sse-inst pavgw #x66 #xe3)
3262 (define-regular-sse-inst pmaddwd #x66 #xf5)
3263 (define-regular-sse-inst pmulhuw #x66 #xe4)
3264 (define-regular-sse-inst pmulhw #x66 #xe5)
3265 (define-regular-sse-inst pmullw #x66 #xd5)
3266 (define-regular-sse-inst pmuludq #x66 #xf4)
3267 (define-regular-sse-inst psadbw #x66 #xf6)
3268 (define-regular-sse-inst psllw #x66 #xf1)
3269 (define-regular-sse-inst pslld #x66 #xf2)
3270 (define-regular-sse-inst psllq #x66 #xf3)
3271 (define-regular-sse-inst psraw #x66 #xe1)
3272 (define-regular-sse-inst psrad #x66 #xe2)
3273 (define-regular-sse-inst psrlw #x66 #xd1)
3274 (define-regular-sse-inst psrld #x66 #xd2)
3275 (define-regular-sse-inst psrlq #x66 #xd3)
3276 (define-regular-sse-inst psubb #x66 #xf8)
3277 (define-regular-sse-inst psubw #x66 #xf9)
3278 (define-regular-sse-inst psubd #x66 #xfa)
3279 (define-regular-sse-inst psubq #x66 #xfb)
3280 (define-regular-sse-inst psubsb #x66 #xe8)
3281 (define-regular-sse-inst psubsw #x66 #xe9)
3282 (define-regular-sse-inst psubusb #x66 #xd8)
3283 (define-regular-sse-inst psubusw #x66 #xd9)
3284 ;; conversion
3285 (define-regular-sse-inst cvtdq2pd #xf3 #xe6)
3286 (define-regular-sse-inst cvtdq2ps nil #x5b)
3287 (define-regular-sse-inst cvtpd2dq #xf2 #xe6)
3288 (define-regular-sse-inst cvtpd2ps #x66 #x5a)
3289 (define-regular-sse-inst cvtps2dq #x66 #x5b)
3290 (define-regular-sse-inst cvtps2pd nil #x5a)
3291 (define-regular-sse-inst cvtsd2ss #xf2 #x5a)
3292 (define-regular-sse-inst cvtss2sd #xf3 #x5a)
3293 (define-regular-sse-inst cvttpd2dq #x66 #xe6)
3294 (define-regular-sse-inst cvttps2dq #xf3 #x5b)
3295 ;; integer
3296 (define-regular-sse-inst packsswb #x66 #x63)
3297 (define-regular-sse-inst packssdw #x66 #x6b)
3298 (define-regular-sse-inst packuswb #x66 #x67)
3299 (define-regular-sse-inst punpckhbw #x66 #x68)
3300 (define-regular-sse-inst punpckhwd #x66 #x69)
3301 (define-regular-sse-inst punpckhdq #x66 #x6a)
3302 (define-regular-sse-inst punpckhqdq #x66 #x6d)
3303 (define-regular-sse-inst punpcklbw #x66 #x60)
3304 (define-regular-sse-inst punpcklwd #x66 #x61)
3305 (define-regular-sse-inst punpckldq #x66 #x62)
3306 (define-regular-sse-inst punpcklqdq #x66 #x6c))
3308 (macrolet ((define-xmm-shuffle-sse-inst (name prefix opcode n-bits radix)
3309 (let ((shuffle-pattern
3310 (intern (format nil "SSE-SHUFFLE-PATTERN-~D-~D"
3311 n-bits radix))))
3312 `(define-instruction ,name (segment dst src pattern)
3313 (:printer-list
3314 ',(sse-inst-printer-list
3315 'xmm-xmm/mem prefix opcode
3316 :more-fields `((imm nil :type ,shuffle-pattern))
3317 :printer '(:name :tab reg ", " reg/mem ", " imm)))
3319 (:emitter
3320 (aver (typep pattern '(unsigned-byte ,n-bits)))
3321 (emit-regular-sse-inst segment dst src ,prefix ,opcode
3322 :remaining-bytes 1)
3323 (emit-byte segment pattern))))))
3324 (define-xmm-shuffle-sse-inst pshufd #x66 #x70 8 4)
3325 (define-xmm-shuffle-sse-inst pshufhw #xf3 #x70 8 4)
3326 (define-xmm-shuffle-sse-inst pshuflw #xf2 #x70 8 4)
3327 (define-xmm-shuffle-sse-inst shufpd #x66 #xc6 2 2)
3328 (define-xmm-shuffle-sse-inst shufps nil #xc6 8 4))
3330 ;; MASKMOVDQU (dst is DS:RDI)
3331 (define-instruction maskmovdqu (segment src mask)
3332 (:printer-list
3333 (sse-inst-printer-list 'xmm-xmm/mem #x66 #xf7))
3334 (:emitter
3335 (aver (xmm-register-p src))
3336 (aver (xmm-register-p mask))
3337 (emit-regular-sse-inst segment src mask #x66 #xf7)))
3339 (macrolet ((define-comparison-sse-inst (name prefix opcode
3340 name-prefix name-suffix)
3341 `(define-instruction ,name (segment op x y)
3342 (:printer-list
3343 ',(sse-inst-printer-list
3344 'xmm-xmm/mem prefix opcode
3345 :more-fields '((imm nil :type sse-condition-code))
3346 :printer `(,name-prefix imm ,name-suffix
3347 :tab reg ", " reg/mem)))
3348 (:emitter
3349 (let ((code (position op *sse-conditions*)))
3350 (aver code)
3351 (emit-regular-sse-inst segment x y ,prefix ,opcode
3352 :remaining-bytes 1)
3353 (emit-byte segment code))))))
3354 (define-comparison-sse-inst cmppd #x66 #xc2 "CMP" "PD")
3355 (define-comparison-sse-inst cmpps nil #xc2 "CMP" "PS")
3356 (define-comparison-sse-inst cmpsd #xf2 #xc2 "CMP" "SD")
3357 (define-comparison-sse-inst cmpss #xf3 #xc2 "CMP" "SS"))
3359 ;;; MOVSD, MOVSS
3360 (macrolet ((define-movsd/ss-sse-inst (name prefix)
3361 `(define-instruction ,name (segment dst src)
3362 (:printer-list
3363 ',(sse-inst-printer-list 'xmm-xmm/mem-dir
3364 prefix #b0001000))
3365 (:emitter
3366 (cond ((xmm-register-p dst)
3367 (emit-sse-inst segment dst src ,prefix #x10
3368 :operand-size :do-not-set))
3370 (aver (xmm-register-p src))
3371 (emit-sse-inst segment src dst ,prefix #x11
3372 :operand-size :do-not-set)))))))
3373 (define-movsd/ss-sse-inst movsd #xf2)
3374 (define-movsd/ss-sse-inst movss #xf3))
3376 ;;; Packed MOVs
3377 (macrolet ((define-mov-sse-inst (name prefix opcode-from opcode-to
3378 &key force-to-mem reg-reg-name)
3379 `(progn
3380 ,(when reg-reg-name
3381 `(define-instruction ,reg-reg-name (segment dst src)
3382 (:emitter
3383 (aver (xmm-register-p dst))
3384 (aver (xmm-register-p src))
3385 (emit-regular-sse-inst segment dst src
3386 ,prefix ,opcode-from))))
3387 (define-instruction ,name (segment dst src)
3388 (:printer-list
3389 '(,@(when opcode-from
3390 (sse-inst-printer-list
3391 'xmm-xmm/mem prefix opcode-from))
3392 ,@(sse-inst-printer-list
3393 'xmm-xmm/mem prefix opcode-to
3394 :printer '(:name :tab reg/mem ", " reg))))
3395 (:emitter
3396 (cond ,@(when opcode-from
3397 `(((xmm-register-p dst)
3398 ,(when force-to-mem
3399 `(aver (not (or (register-p src)
3400 (xmm-register-p src)))))
3401 (emit-regular-sse-inst
3402 segment dst src ,prefix ,opcode-from))))
3404 (aver (xmm-register-p src))
3405 ,(when force-to-mem
3406 `(aver (not (or (register-p dst)
3407 (xmm-register-p dst)))))
3408 (emit-regular-sse-inst segment src dst
3409 ,prefix ,opcode-to))))))))
3410 ;; direction bit?
3411 (define-mov-sse-inst movapd #x66 #x28 #x29)
3412 (define-mov-sse-inst movaps nil #x28 #x29)
3413 (define-mov-sse-inst movdqa #x66 #x6f #x7f)
3414 (define-mov-sse-inst movdqu #xf3 #x6f #x7f)
3416 ;; streaming
3417 (define-mov-sse-inst movntdq #x66 nil #xe7 :force-to-mem t)
3418 (define-mov-sse-inst movntpd #x66 nil #x2b :force-to-mem t)
3419 (define-mov-sse-inst movntps nil nil #x2b :force-to-mem t)
3421 ;; use movhps for movlhps and movlps for movhlps
3422 (define-mov-sse-inst movhpd #x66 #x16 #x17 :force-to-mem t)
3423 (define-mov-sse-inst movhps nil #x16 #x17 :reg-reg-name movlhps)
3424 (define-mov-sse-inst movlpd #x66 #x12 #x13 :force-to-mem t)
3425 (define-mov-sse-inst movlps nil #x12 #x13 :reg-reg-name movhlps)
3426 (define-mov-sse-inst movupd #x66 #x10 #x11)
3427 (define-mov-sse-inst movups nil #x10 #x11))
3429 ;;; MOVNTDQA
3430 (define-instruction movntdqa (segment dst src)
3431 (:printer-list
3432 (2byte-sse-inst-printer-list '2byte-xmm-xmm/mem #x66 #x38 #x2a))
3433 (:emitter
3434 (aver (and (xmm-register-p dst)
3435 (not (xmm-register-p src))))
3436 (emit-regular-2byte-sse-inst segment dst src #x66 #x38 #x2a)))
3438 ;;; MOVQ
3439 (define-instruction movq (segment dst src)
3440 (:printer-list
3441 (append
3442 (sse-inst-printer-list 'xmm-xmm/mem #xf3 #x7e)
3443 (sse-inst-printer-list 'xmm-xmm/mem #x66 #xd6
3444 :printer '(:name :tab reg/mem ", " reg))))
3445 (:emitter
3446 (cond ((xmm-register-p dst)
3447 (emit-sse-inst segment dst src #xf3 #x7e
3448 :operand-size :do-not-set))
3450 (aver (xmm-register-p src))
3451 (emit-sse-inst segment src dst #x66 #xd6
3452 :operand-size :do-not-set)))))
3454 ;;; Instructions having an XMM register as the destination operand
3455 ;;; and a general-purpose register or a memory location as the source
3456 ;;; operand. The operand size is calculated from the source operand.
3458 ;;; MOVD - Move a 32- or 64-bit value from a general-purpose register or
3459 ;;; a memory location to the low order 32 or 64 bits of an XMM register
3460 ;;; with zero extension or vice versa.
3461 ;;; We do not support the MMX version of this instruction.
3462 (define-instruction movd (segment dst src)
3463 (:printer-list
3464 (append
3465 (sse-inst-printer-list 'xmm-reg/mem #x66 #x6e)
3466 (sse-inst-printer-list 'xmm-reg/mem #x66 #x7e
3467 :printer '(:name :tab reg/mem ", " reg))))
3468 (:emitter
3469 (cond ((xmm-register-p dst)
3470 (emit-sse-inst segment dst src #x66 #x6e))
3472 (aver (xmm-register-p src))
3473 (emit-sse-inst segment src dst #x66 #x7e)))))
3475 (macrolet ((define-extract-sse-instruction (name prefix op1 op2
3476 &key explicit-qword)
3477 `(define-instruction ,name (segment dst src imm)
3478 (:printer
3479 ,(if op2 (if explicit-qword
3480 'ext-rex-2byte-reg/mem-xmm
3481 'ext-2byte-reg/mem-xmm)
3482 'ext-reg/mem-xmm)
3483 ((prefix '(,prefix))
3484 ,@(if op2
3485 `((op1 '(,op1)) (op2 '(,op2)))
3486 `((op '(,op1))))
3487 (imm nil :type 'imm-byte))
3488 '(:name :tab reg/mem ", " reg ", " imm))
3489 (:emitter
3490 (aver (and (xmm-register-p src) (not (xmm-register-p dst))))
3491 ,(if op2
3492 `(emit-sse-inst-2byte segment dst src ,prefix ,op1 ,op2
3493 :operand-size ,(if explicit-qword
3494 :qword
3495 :do-not-set)
3496 :remaining-bytes 1)
3497 `(emit-sse-inst segment dst src ,prefix ,op1
3498 :operand-size ,(if explicit-qword
3499 :qword
3500 :do-not-set)
3501 :remaining-bytes 1))
3502 (emit-byte segment imm))))
3504 (define-insert-sse-instruction (name prefix op1 op2)
3505 `(define-instruction ,name (segment dst src imm)
3506 (:printer
3507 ,(if op2 'ext-2byte-xmm-reg/mem 'ext-xmm-reg/mem)
3508 ((prefix '(,prefix))
3509 ,@(if op2
3510 `((op1 '(,op1)) (op2 '(,op2)))
3511 `((op '(,op1))))
3512 (imm nil :type 'imm-byte))
3513 '(:name :tab reg ", " reg/mem ", " imm))
3514 (:emitter
3515 (aver (and (xmm-register-p dst) (not (xmm-register-p src))))
3516 ,(if op2
3517 `(emit-sse-inst-2byte segment dst src ,prefix ,op1 ,op2
3518 :operand-size :do-not-set
3519 :remaining-bytes 1)
3520 `(emit-sse-inst segment dst src ,prefix ,op1
3521 :operand-size :do-not-set
3522 :remaining-bytes 1))
3523 (emit-byte segment imm)))))
3526 ;; pinsrq not encodable in 64-bit mode
3527 (define-insert-sse-instruction pinsrb #x66 #x3a #x20)
3528 (define-insert-sse-instruction pinsrw #x66 #xc4 nil)
3529 (define-insert-sse-instruction pinsrd #x66 #x3a #x22)
3530 (define-insert-sse-instruction insertps #x66 #x3a #x21)
3532 (define-extract-sse-instruction pextrb #x66 #x3a #x14)
3533 (define-extract-sse-instruction pextrd #x66 #x3a #x16)
3534 (define-extract-sse-instruction pextrq #x66 #x3a #x16 :explicit-qword t)
3535 (define-extract-sse-instruction extractps #x66 #x3a #x17))
3537 ;; PEXTRW has a new 2-byte encoding in SSE4.1 to allow dst to be
3538 ;; a memory address.
3539 (define-instruction pextrw (segment dst src imm)
3540 (:printer-list
3541 (append
3542 (2byte-sse-inst-printer-list '2byte-reg/mem-xmm #x66 #x3a #x15
3543 :more-fields '((imm nil :type imm-byte))
3544 :printer
3545 '(:name :tab reg/mem ", " reg ", " imm))
3546 (sse-inst-printer-list 'reg/mem-xmm #x66 #xc5
3547 :more-fields '((imm nil :type imm-byte))
3548 :printer
3549 '(:name :tab reg/mem ", " reg ", " imm))))
3550 (:emitter
3551 (aver (xmm-register-p src))
3552 (if (not (register-p dst))
3553 (emit-sse-inst-2byte segment dst src #x66 #x3a #x15
3554 :operand-size :do-not-set :remaining-bytes 1)
3555 (emit-sse-inst segment dst src #x66 #xc5
3556 :operand-size :do-not-set :remaining-bytes 1))
3557 (emit-byte segment imm)))
3559 (macrolet ((define-integer-source-sse-inst (name prefix opcode &key mem-only)
3560 `(define-instruction ,name (segment dst src)
3561 (:printer-list
3562 ',(sse-inst-printer-list 'xmm-reg/mem prefix opcode))
3563 (:emitter
3564 (aver (xmm-register-p dst))
3565 ,(when mem-only
3566 `(aver (not (or (register-p src)
3567 (xmm-register-p src)))))
3568 (let ((src-size (operand-size src)))
3569 (aver (or (eq src-size :qword) (eq src-size :dword))))
3570 (emit-sse-inst segment dst src ,prefix ,opcode)))))
3571 (define-integer-source-sse-inst cvtsi2sd #xf2 #x2a)
3572 (define-integer-source-sse-inst cvtsi2ss #xf3 #x2a)
3573 ;; FIXME: memory operand is always a QWORD
3574 (define-integer-source-sse-inst cvtpi2pd #x66 #x2a :mem-only t)
3575 (define-integer-source-sse-inst cvtpi2ps nil #x2a :mem-only t))
3577 ;;; Instructions having a general-purpose register as the destination
3578 ;;; operand and an XMM register or a memory location as the source
3579 ;;; operand. The operand size is calculated from the destination
3580 ;;; operand.
3582 (macrolet ((define-gpr-destination-sse-inst (name prefix opcode &key reg-only)
3583 `(define-instruction ,name (segment dst src)
3584 (:printer-list
3585 ',(sse-inst-printer-list 'reg-xmm/mem prefix opcode))
3586 (:emitter
3587 (aver (register-p dst))
3588 ,(when reg-only
3589 `(aver (xmm-register-p src)))
3590 (let ((dst-size (operand-size dst)))
3591 (aver (or (eq dst-size :qword) (eq dst-size :dword)))
3592 (emit-sse-inst segment dst src ,prefix ,opcode
3593 :operand-size dst-size))))))
3594 (define-gpr-destination-sse-inst cvtsd2si #xf2 #x2d)
3595 (define-gpr-destination-sse-inst cvtss2si #xf3 #x2d)
3596 (define-gpr-destination-sse-inst cvttsd2si #xf2 #x2c)
3597 (define-gpr-destination-sse-inst cvttss2si #xf3 #x2c)
3598 (define-gpr-destination-sse-inst movmskpd #x66 #x50 :reg-only t)
3599 (define-gpr-destination-sse-inst movmskps nil #x50 :reg-only t)
3600 (define-gpr-destination-sse-inst pmovmskb #x66 #xd7 :reg-only t))
3602 ;;;; We call these "2byte" instructions due to their two opcode bytes.
3603 ;;;; Intel and AMD call them three-byte instructions, as they count the
3604 ;;;; 0x0f byte for determining the number of opcode bytes.
3606 ;;; Instructions that take XMM-XMM/MEM and XMM-XMM/MEM-IMM arguments.
3608 (macrolet ((regular-2byte-sse-inst (name prefix op1 op2)
3609 `(define-instruction ,name (segment dst src)
3610 (:printer-list
3611 ',(2byte-sse-inst-printer-list '2byte-xmm-xmm/mem prefix
3612 op1 op2))
3613 (:emitter
3614 (emit-regular-2byte-sse-inst segment dst src ,prefix
3615 ,op1 ,op2))))
3616 (regular-2byte-sse-inst-imm (name prefix op1 op2)
3617 `(define-instruction ,name (segment dst src imm)
3618 (:printer-list
3619 ',(2byte-sse-inst-printer-list
3620 '2byte-xmm-xmm/mem prefix op1 op2
3621 :more-fields '((imm nil :type imm-byte))
3622 :printer `(:name :tab reg ", " reg/mem ", " imm)))
3623 (:emitter
3624 (aver (typep imm '(unsigned-byte 8)))
3625 (emit-regular-2byte-sse-inst segment dst src ,prefix ,op1 ,op2
3626 :remaining-bytes 1)
3627 (emit-byte segment imm)))))
3628 (regular-2byte-sse-inst pshufb #x66 #x38 #x00)
3629 (regular-2byte-sse-inst phaddw #x66 #x38 #x01)
3630 (regular-2byte-sse-inst phaddd #x66 #x38 #x02)
3631 (regular-2byte-sse-inst phaddsw #x66 #x38 #x03)
3632 (regular-2byte-sse-inst pmaddubsw #x66 #x38 #x04)
3633 (regular-2byte-sse-inst phsubw #x66 #x38 #x05)
3634 (regular-2byte-sse-inst phsubd #x66 #x38 #x06)
3635 (regular-2byte-sse-inst phsubsw #x66 #x38 #x07)
3636 (regular-2byte-sse-inst psignb #x66 #x38 #x08)
3637 (regular-2byte-sse-inst psignw #x66 #x38 #x09)
3638 (regular-2byte-sse-inst psignd #x66 #x38 #x0a)
3639 (regular-2byte-sse-inst pmulhrsw #x66 #x38 #x0b)
3641 (regular-2byte-sse-inst ptest #x66 #x38 #x17)
3642 (regular-2byte-sse-inst pabsb #x66 #x38 #x1c)
3643 (regular-2byte-sse-inst pabsw #x66 #x38 #x1d)
3644 (regular-2byte-sse-inst pabsd #x66 #x38 #x1e)
3646 (regular-2byte-sse-inst pmuldq #x66 #x38 #x28)
3647 (regular-2byte-sse-inst pcmpeqq #x66 #x38 #x29)
3648 (regular-2byte-sse-inst packusdw #x66 #x38 #x2b)
3650 (regular-2byte-sse-inst pcmpgtq #x66 #x38 #x37)
3651 (regular-2byte-sse-inst pminsb #x66 #x38 #x38)
3652 (regular-2byte-sse-inst pminsd #x66 #x38 #x39)
3653 (regular-2byte-sse-inst pminuw #x66 #x38 #x3a)
3654 (regular-2byte-sse-inst pminud #x66 #x38 #x3b)
3655 (regular-2byte-sse-inst pmaxsb #x66 #x38 #x3c)
3656 (regular-2byte-sse-inst pmaxsd #x66 #x38 #x3d)
3657 (regular-2byte-sse-inst pmaxuw #x66 #x38 #x3e)
3658 (regular-2byte-sse-inst pmaxud #x66 #x38 #x3f)
3660 (regular-2byte-sse-inst pmulld #x66 #x38 #x40)
3661 (regular-2byte-sse-inst phminposuw #x66 #x38 #x41)
3663 (regular-2byte-sse-inst aesimc #x66 #x38 #xdb)
3664 (regular-2byte-sse-inst aesenc #x66 #x38 #xdc)
3665 (regular-2byte-sse-inst aesenclast #x66 #x38 #xdd)
3666 (regular-2byte-sse-inst aesdec #x66 #x38 #xde)
3667 (regular-2byte-sse-inst aesdeclast #x66 #x38 #xdf)
3669 (regular-2byte-sse-inst pmovsxbw #x66 #x38 #x20)
3670 (regular-2byte-sse-inst pmovsxbd #x66 #x38 #x21)
3671 (regular-2byte-sse-inst pmovsxbq #x66 #x38 #x22)
3672 (regular-2byte-sse-inst pmovsxwd #x66 #x38 #x23)
3673 (regular-2byte-sse-inst pmovsxwq #x66 #x38 #x24)
3674 (regular-2byte-sse-inst pmovsxdq #x66 #x38 #x25)
3676 (regular-2byte-sse-inst pmovzxbw #x66 #x38 #x30)
3677 (regular-2byte-sse-inst pmovzxbd #x66 #x38 #x31)
3678 (regular-2byte-sse-inst pmovzxbq #x66 #x38 #x32)
3679 (regular-2byte-sse-inst pmovzxwd #x66 #x38 #x33)
3680 (regular-2byte-sse-inst pmovzxwq #x66 #x38 #x34)
3681 (regular-2byte-sse-inst pmovzxdq #x66 #x38 #x35)
3683 (regular-2byte-sse-inst-imm roundps #x66 #x3a #x08)
3684 (regular-2byte-sse-inst-imm roundpd #x66 #x3a #x09)
3685 (regular-2byte-sse-inst-imm roundss #x66 #x3a #x0a)
3686 (regular-2byte-sse-inst-imm roundsd #x66 #x3a #x0b)
3687 (regular-2byte-sse-inst-imm blendps #x66 #x3a #x0c)
3688 (regular-2byte-sse-inst-imm blendpd #x66 #x3a #x0d)
3689 (regular-2byte-sse-inst-imm pblendw #x66 #x3a #x0e)
3690 (regular-2byte-sse-inst-imm palignr #x66 #x3a #x0f)
3691 (regular-2byte-sse-inst-imm dpps #x66 #x3a #x40)
3692 (regular-2byte-sse-inst-imm dppd #x66 #x3a #x41)
3694 (regular-2byte-sse-inst-imm mpsadbw #x66 #x3a #x42)
3695 (regular-2byte-sse-inst-imm pclmulqdq #x66 #x3a #x44)
3697 (regular-2byte-sse-inst-imm pcmpestrm #x66 #x3a #x60)
3698 (regular-2byte-sse-inst-imm pcmpestri #x66 #x3a #x61)
3699 (regular-2byte-sse-inst-imm pcmpistrm #x66 #x3a #x62)
3700 (regular-2byte-sse-inst-imm pcmpistri #x66 #x3a #x63)
3702 (regular-2byte-sse-inst-imm aeskeygenassist #x66 #x3a #xdf))
3704 ;;; Other SSE instructions
3706 ;; Instructions implicitly using XMM0 as a mask
3707 (macrolet ((define-sse-inst-implicit-mask (name prefix op1 op2)
3708 `(define-instruction ,name (segment dst src mask)
3709 (:printer-list
3710 ',(2byte-sse-inst-printer-list
3711 '2byte-xmm-xmm/mem prefix op1 op2
3712 :printer '(:name :tab reg ", " reg/mem ", XMM0")))
3713 (:emitter
3714 (aver (xmm-register-p dst))
3715 (aver (and (xmm-register-p mask) (= (tn-offset mask) 0)))
3716 (emit-regular-2byte-sse-inst segment dst src ,prefix
3717 ,op1 ,op2)))))
3719 (define-sse-inst-implicit-mask pblendvb #x66 #x38 #x10)
3720 (define-sse-inst-implicit-mask blendvps #x66 #x38 #x14)
3721 (define-sse-inst-implicit-mask blendvpd #x66 #x38 #x15))
3723 ;; FIXME: is that right!?
3724 (define-instruction movnti (segment dst src)
3725 (:printer ext-reg-reg/mem-no-width ((op #xc3)))
3726 (:emitter
3727 (aver (not (or (register-p dst)
3728 (xmm-register-p dst))))
3729 (aver (register-p src))
3730 (maybe-emit-rex-for-ea segment src dst)
3731 (emit-byte segment #x0f)
3732 (emit-byte segment #xc3)
3733 (emit-ea segment dst (reg-tn-encoding src))))
3735 (define-instruction prefetch (segment type src)
3736 (:printer ext-reg/mem-no-width ((op '(#x18 0)))
3737 '("PREFETCHNTA" :tab reg/mem))
3738 (:printer ext-reg/mem-no-width ((op '(#x18 1)))
3739 '("PREFETCHT0" :tab reg/mem))
3740 (:printer ext-reg/mem-no-width ((op '(#x18 2)))
3741 '("PREFETCHT1" :tab reg/mem))
3742 (:printer ext-reg/mem-no-width ((op '(#x18 3)))
3743 '("PREFETCHT2" :tab reg/mem))
3744 (:emitter
3745 (aver (not (or (register-p src)
3746 (xmm-register-p src))))
3747 (aver (eq (operand-size src) :byte))
3748 (let ((type (position type #(:nta :t0 :t1 :t2))))
3749 (aver type)
3750 (maybe-emit-rex-for-ea segment src nil)
3751 (emit-byte segment #x0f)
3752 (emit-byte segment #x18)
3753 (emit-ea segment src type))))
3755 (define-instruction clflush (segment src)
3756 (:printer ext-reg/mem-no-width ((op '(#xae 7))))
3757 (:emitter
3758 (aver (not (or (register-p src)
3759 (xmm-register-p src))))
3760 (aver (eq (operand-size src) :byte))
3761 (maybe-emit-rex-for-ea segment src nil)
3762 (emit-byte segment #x0f)
3763 (emit-byte segment #xae)
3764 (emit-ea segment src 7)))
3766 (macrolet ((define-fence-instruction (name last-byte)
3767 `(define-instruction ,name (segment)
3768 (:printer three-bytes ((op '(#x0f #xae ,last-byte))))
3769 (:emitter
3770 (emit-byte segment #x0f)
3771 (emit-byte segment #xae)
3772 (emit-byte segment ,last-byte)))))
3773 (define-fence-instruction lfence #b11101000)
3774 (define-fence-instruction mfence #b11110000)
3775 (define-fence-instruction sfence #b11111000))
3777 (define-instruction pause (segment)
3778 (:printer two-bytes ((op '(#xf3 #x90))))
3779 (:emitter
3780 (emit-byte segment #xf3)
3781 (emit-byte segment #x90)))
3783 (define-instruction ldmxcsr (segment src)
3784 (:printer ext-reg/mem-no-width ((op '(#xae 2))))
3785 (:emitter
3786 (aver (not (or (register-p src)
3787 (xmm-register-p src))))
3788 (aver (eq (operand-size src) :dword))
3789 (maybe-emit-rex-for-ea segment src nil)
3790 (emit-byte segment #x0f)
3791 (emit-byte segment #xae)
3792 (emit-ea segment src 2)))
3794 (define-instruction stmxcsr (segment dst)
3795 (:printer ext-reg/mem-no-width ((op '(#xae 3))))
3796 (:emitter
3797 (aver (not (or (register-p dst)
3798 (xmm-register-p dst))))
3799 (aver (eq (operand-size dst) :dword))
3800 (maybe-emit-rex-for-ea segment dst nil)
3801 (emit-byte segment #x0f)
3802 (emit-byte segment #xae)
3803 (emit-ea segment dst 3)))
3805 (define-instruction popcnt (segment dst src)
3806 (:printer-list `((f3-escape-reg-reg/mem ((op #xB8)))
3807 (rex-f3-escape-reg-reg/mem ((op #xB8)))))
3808 (:emitter
3809 (aver (register-p dst))
3810 (aver (and (register-p dst) (not (eq (operand-size dst) :byte))))
3811 (aver (not (eq (operand-size src) :byte)))
3812 (emit-sse-inst segment dst src #xf3 #xb8)))
3814 (define-instruction crc32 (segment dst src)
3815 (:printer-list
3816 `(,@(mapcan (lambda (op2)
3817 (mapcar (lambda (instfmt)
3818 `(,instfmt ((prefix (#xf2)) (op1 (#x38))
3819 (op2 (,op2)))))
3820 '(ext-rex-2byte-prefix-reg-reg/mem
3821 ext-2byte-prefix-reg-reg/mem)))
3822 '(#xf0 #xf1))))
3823 (:emitter
3824 (let ((dst-size (operand-size dst)))
3825 (aver (and (register-p dst) (not (or (eq dst-size :word)
3826 (eq dst-size :byte)))))
3827 (if (eq (operand-size src) :byte)
3828 (emit-sse-inst-2byte segment dst src #xf2 #x38 #xf0)
3829 (emit-sse-inst-2byte segment dst src #xf2 #x38 #xf1)))))
3831 ;;;; Miscellany
3833 (define-instruction cpuid (segment)
3834 (:printer two-bytes ((op '(#b00001111 #b10100010))))
3835 (:emitter
3836 (emit-byte segment #b00001111)
3837 (emit-byte segment #b10100010)))
3839 (define-instruction rdtsc (segment)
3840 (:printer two-bytes ((op '(#b00001111 #b00110001))))
3841 (:emitter
3842 (emit-byte segment #b00001111)
3843 (emit-byte segment #b00110001)))
3845 ;;;; Late VM definitions
3847 (defun canonicalize-inline-constant (constant &aux (alignedp nil))
3848 (let ((first (car constant)))
3849 (when (eql first :aligned)
3850 (setf alignedp t)
3851 (pop constant)
3852 (setf first (car constant)))
3853 (typecase first
3854 (single-float (setf constant (list :single-float first)))
3855 (double-float (setf constant (list :double-float first)))
3857 #+sb-xc-host
3858 ((complex
3859 ;; It's an error (perhaps) on the host to use simd-pack type.
3860 ;; [and btw it's disconcerting that this isn't an ETYPECASE.]
3861 (error "xc-host can't reference complex float")))
3862 #-sb-xc-host
3863 (((complex single-float)
3864 (setf constant (list :complex-single-float first)))
3865 ((complex double-float)
3866 (setf constant (list :complex-double-float first)))
3867 #!+sb-simd-pack
3868 (simd-pack
3869 (setq constant
3870 (list :sse (logior (%simd-pack-low first)
3871 (ash (%simd-pack-high first) 64))))))))
3872 (destructuring-bind (type value) constant
3873 (ecase type
3874 ((:byte :word :dword :qword)
3875 (aver (integerp value))
3876 (cons type value))
3877 ((:base-char)
3878 #!+sb-unicode (aver (base-char-p value))
3879 (cons :byte (char-code value)))
3880 ((:character)
3881 (aver (characterp value))
3882 (cons :dword (char-code value)))
3883 ((:single-float)
3884 (aver (typep value 'single-float))
3885 (cons (if alignedp :oword :dword)
3886 (ldb (byte 32 0) (single-float-bits value))))
3887 ((:double-float)
3888 (aver (typep value 'double-float))
3889 (cons (if alignedp :oword :qword)
3890 (ldb (byte 64 0) (logior (ash (double-float-high-bits value) 32)
3891 (double-float-low-bits value)))))
3892 ((:complex-single-float)
3893 (aver (typep value '(complex single-float)))
3894 (cons (if alignedp :oword :qword)
3895 (ldb (byte 64 0)
3896 (logior (ash (single-float-bits (imagpart value)) 32)
3897 (ldb (byte 32 0)
3898 (single-float-bits (realpart value)))))))
3899 ((:oword :sse)
3900 (aver (integerp value))
3901 (cons :oword value))
3902 ((:complex-double-float)
3903 (aver (typep value '(complex double-float)))
3904 (cons :oword
3905 (logior (ash (double-float-high-bits (imagpart value)) 96)
3906 (ash (double-float-low-bits (imagpart value)) 64)
3907 (ash (ldb (byte 32 0)
3908 (double-float-high-bits (realpart value)))
3910 (double-float-low-bits (realpart value))))))))
3912 (defun inline-constant-value (constant)
3913 (let ((label (gen-label))
3914 (size (ecase (car constant)
3915 ((:byte :word :dword :qword) (car constant))
3916 ((:oword) :qword))))
3917 (values label (make-ea size
3918 :disp (make-fixup nil :code-object label)))))
3920 (defun emit-constant-segment-header (segment constants optimize)
3921 (declare (ignore constants))
3922 (emit-long-nop segment (if optimize 64 16)))
3924 (defun size-nbyte (size)
3925 (ecase size
3926 (:byte 1)
3927 (:word 2)
3928 (:dword 4)
3929 (:qword 8)
3930 (:oword 16)))
3932 (defun sort-inline-constants (constants)
3933 (stable-sort constants #'> :key (lambda (constant)
3934 (size-nbyte (caar constant)))))
3936 (defun emit-inline-constant (constant label)
3937 (let ((size (size-nbyte (car constant))))
3938 (emit-alignment (integer-length (1- size)))
3939 (emit-label label)
3940 (let ((val (cdr constant)))
3941 (loop repeat size
3942 do (inst byte (ldb (byte 8 0) val))
3943 (setf val (ash val -8))))))