x86-64: Allow choice of 0xCC or 0xCE as the interrupt instruction
[sbcl.git] / src / compiler / x86-64 / insts.lisp
blob1c44e1704cb23745e22e266fe0f803079ed0b1c0
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!X86-64-ASM")
15 (eval-when (:compile-toplevel :load-toplevel :execute)
16 ;; Imports from this package into SB-VM
17 (import '(conditional-opcode
18 register-p xmm-register-p ; FIXME: rename REGISTER-P to GPR-P
19 ea-p sized-ea
20 make-ea ea-disp) "SB!VM")
21 ;; Imports from SB-VM into this package
22 (import '(sb!vm::*byte-sc-names* sb!vm::*word-sc-names*
23 sb!vm::*dword-sc-names* sb!vm::*qword-sc-names*
24 sb!vm::frame-byte-offset
25 sb!vm::registers sb!vm::float-registers sb!vm::stack))) ; SB names
27 ;;; Note: In CMU CL, this used to be a call to SET-DISASSEM-PARAMS.
28 (setf *disassem-inst-alignment-bytes* 1)
30 ;;; This type is used mostly in disassembly and represents legacy
31 ;;; registers only. R8-R15 are handled separately.
32 (deftype reg () '(unsigned-byte 3))
34 ;;; This includes legacy registers and R8-R15.
35 (deftype full-reg () '(unsigned-byte 4))
37 ;;; The XMM registers XMM0 - XMM15.
38 (deftype xmmreg () '(unsigned-byte 4))
40 ;;; Default word size for the chip: if the operand size /= :dword
41 ;;; we need to output #x66 (or REX) prefix
42 (defconstant +default-operand-size+ :dword)
44 ;;; The default address size for the chip. It could be overwritten
45 ;;; to :dword with a #x67 prefix, but this is never needed by SBCL
46 ;;; and thus not supported by this assembler/disassembler.
47 (defconstant +default-address-size+ :qword)
49 ;;; The printers for registers, memory references and immediates need to
50 ;;; take into account the width bit in the instruction, whether a #x66
51 ;;; or a REX prefix was issued, and the contents of the REX prefix.
52 ;;; This is implemented using prefilters to put flags into the slot
53 ;;; INST-PROPERTIES of the DSTATE. These flags are the following
54 ;;; symbols:
55 ;;;
56 ;;; OPERAND-SIZE-8 The width bit was zero
57 ;;; OPERAND-SIZE-16 The "operand size override" prefix (#x66) was found
58 ;;; REX A REX prefix was found
59 ;;; REX-W A REX prefix with the "operand width" bit set was
60 ;;; found
61 ;;; REX-R A REX prefix with the "register" bit set was found
62 ;;; REX-X A REX prefix with the "index" bit set was found
63 ;;; REX-B A REX prefix with the "base" bit set was found
64 (defconstant +allow-qword-imm+ #b10000000)
65 (defconstant +operand-size-8+ #b01000000)
66 (defconstant +operand-size-16+ #b00100000)
67 (defconstant +rex+ #b00010000)
68 ;;; The next 4 exactly correspond to the bits in the REX prefix itself,
69 ;;; to avoid unpacking and stuffing into inst-properties one at a time.
70 (defconstant +rex-w+ #b1000)
71 (defconstant +rex-r+ #b0100)
72 (defconstant +rex-x+ #b0010)
73 (defconstant +rex-b+ #b0001)
75 ;;; Return the operand size depending on the prefixes and width bit as
76 ;;; stored in DSTATE.
77 (defun inst-operand-size (dstate)
78 (declare (type disassem-state dstate))
79 (cond ((dstate-get-inst-prop dstate +operand-size-8+) :byte)
80 ((dstate-get-inst-prop dstate +rex-w+) :qword)
81 ((dstate-get-inst-prop dstate +operand-size-16+) :word)
82 (t +default-operand-size+)))
84 ;;; The same as INST-OPERAND-SIZE, but for those instructions (e.g.
85 ;;; PUSH, JMP) that have a default operand size of :qword. It can only
86 ;;; be overwritten to :word.
87 (defun inst-operand-size-default-qword (dstate)
88 (declare (type disassem-state dstate))
89 (if (dstate-get-inst-prop dstate +operand-size-16+) :word :qword))
91 ;;; This prefilter is used solely for its side effect, namely to put
92 ;;; the property OPERAND-SIZE-8 into the DSTATE if VALUE is 0.
93 (defun prefilter-width (dstate value)
94 (declare (type bit value) (type disassem-state dstate))
95 (when (zerop value)
96 (dstate-put-inst-prop dstate +operand-size-8+))
97 value)
99 ;;; A register field that can be extended by REX.R.
100 (defun prefilter-reg-r (dstate value)
101 (declare (type reg value) (type disassem-state dstate))
102 (if (dstate-get-inst-prop dstate +rex-r+) (+ value 8) value))
104 ;;; A register field that can be extended by REX.B.
105 (defun prefilter-reg-b (dstate value)
106 (declare (type reg value) (type disassem-state dstate))
107 (if (dstate-get-inst-prop dstate +rex-b+) (+ value 8) value))
109 (defun width-bits (width)
110 (ecase width
111 (:byte 8)
112 (:word 16)
113 (:dword 32)
114 (:qword 64)))
117 ;;;; disassembler argument types
119 ;;; Used to capture the lower four bits of the REX prefix all at once ...
120 (define-arg-type wrxb
121 :prefilter (lambda (dstate value)
122 (dstate-put-inst-prop dstate (logior +rex+ (logand value #b1111)))
123 value))
124 ;;; ... or individually (not needed for REX.R and REX.X).
125 ;;; They are always used together, so only the first one sets the REX property.
126 (define-arg-type rex-w
127 :prefilter (lambda (dstate value)
128 (dstate-put-inst-prop dstate
129 (logior +rex+ (if (plusp value) +rex-w+ 0)))))
130 (define-arg-type rex-b
131 :prefilter (lambda (dstate value)
132 (dstate-put-inst-prop dstate (if (plusp value) +rex-b+ 0))))
134 (define-arg-type width
135 :prefilter #'prefilter-width
136 :printer (lambda (value stream dstate)
137 (declare (ignore value))
138 (princ (schar (symbol-name (inst-operand-size dstate)) 0)
139 stream)))
141 ;;; Used to capture the effect of the #x66 operand size override prefix.
142 (define-arg-type x66
143 :prefilter (lambda (dstate junk)
144 (declare (ignore junk))
145 (dstate-put-inst-prop dstate +operand-size-16+)))
147 ;;; Find the Lisp object, if any, called by a "CALL rel32offs"
148 ;;; instruction format and add it as an end-of-line comment,
149 ;;; but not on the host, since NOTE is in target-disassem.
150 #!+(and immobile-space (not (host-feature sb-xc-host)))
151 (defun maybe-note-lisp-callee (value dstate)
152 (awhen (sb!vm::find-called-object value)
153 (note (lambda (stream) (princ it stream)) dstate)))
155 (define-arg-type displacement
156 :sign-extend t
157 :use-label (lambda (value dstate) (+ (dstate-next-addr dstate) value))
158 :printer (lambda (value stream dstate)
159 (or #!+immobile-space
160 (and (integerp value) (maybe-note-lisp-callee value dstate))
161 (maybe-note-assembler-routine value nil dstate))
162 (print-label value stream dstate)))
164 (define-arg-type accum
165 :printer (lambda (value stream dstate)
166 (declare (ignore value)
167 (type stream stream)
168 (type disassem-state dstate))
169 (print-reg 0 stream dstate)))
171 (define-arg-type reg
172 :prefilter #'prefilter-reg-r
173 :printer #'print-reg)
175 (define-arg-type reg-b
176 :prefilter #'prefilter-reg-b
177 :printer #'print-reg)
179 (define-arg-type reg-b-default-qword
180 :prefilter #'prefilter-reg-b
181 :printer #'print-reg-default-qword)
183 (define-arg-type imm-addr
184 :prefilter (lambda (dstate)
185 (read-suffix (width-bits (inst-operand-size dstate)) dstate))
186 :printer #'print-label)
188 ;;; Normally, immediate values for an operand size of :qword are of size
189 ;;; :dword and are sign-extended to 64 bits.
190 ;;; The exception is that opcode group 0xB8 .. 0xBF allows a :qword immediate.
191 (define-arg-type signed-imm-data
192 :prefilter (lambda (dstate &aux (width (inst-operand-size dstate)))
193 (when (and (not (dstate-get-inst-prop dstate +allow-qword-imm+))
194 (eq width :qword))
195 (setf width :dword))
196 (read-signed-suffix (width-bits width) dstate))
197 :printer (lambda (value stream dstate)
198 (if (maybe-note-static-symbol value dstate)
199 (princ16 value stream)
200 (princ value stream))))
202 (define-arg-type signed-imm-data/asm-routine
203 :type 'signed-imm-data
204 :printer #'print-imm/asm-routine)
206 ;;; Used by those instructions that have a default operand size of
207 ;;; :qword. Nevertheless the immediate is at most of size :dword.
208 ;;; The only instruction of this kind having a variant with an immediate
209 ;;; argument is PUSH.
210 (define-arg-type signed-imm-data-default-qword
211 :prefilter (lambda (dstate)
212 (let ((width (width-bits
213 (inst-operand-size-default-qword dstate))))
214 (when (= width 64)
215 (setf width 32))
216 (read-signed-suffix width dstate))))
218 (define-arg-type signed-imm-byte
219 :prefilter (lambda (dstate)
220 (read-signed-suffix 8 dstate)))
222 (define-arg-type imm-byte
223 :prefilter (lambda (dstate)
224 (read-suffix 8 dstate)))
226 ;;; needed for the ret imm16 instruction
227 (define-arg-type imm-word-16
228 :prefilter (lambda (dstate)
229 (read-suffix 16 dstate)))
231 (define-arg-type reg/mem
232 :prefilter #'prefilter-reg/mem
233 :printer #'print-reg/mem)
234 (define-arg-type sized-reg/mem
235 ;; Same as reg/mem, but prints an explicit size indicator for
236 ;; memory references.
237 :prefilter #'prefilter-reg/mem
238 :printer #'print-sized-reg/mem)
240 ;;; Arguments of type reg/mem with a fixed size.
241 (define-arg-type sized-byte-reg/mem
242 :prefilter #'prefilter-reg/mem
243 :printer #'print-sized-byte-reg/mem)
244 (define-arg-type sized-word-reg/mem
245 :prefilter #'prefilter-reg/mem
246 :printer #'print-sized-word-reg/mem)
247 (define-arg-type sized-dword-reg/mem
248 :prefilter #'prefilter-reg/mem
249 :printer #'print-sized-dword-reg/mem)
251 ;;; Same as sized-reg/mem, but with a default operand size of :qword.
252 (define-arg-type sized-reg/mem-default-qword
253 :prefilter #'prefilter-reg/mem
254 :printer #'print-sized-reg/mem-default-qword)
256 ;;; XMM registers
257 (define-arg-type xmmreg
258 :prefilter #'prefilter-reg-r
259 :printer #'print-xmmreg)
261 (define-arg-type xmmreg-b
262 :prefilter #'prefilter-reg-b
263 :printer #'print-xmmreg)
265 (define-arg-type xmmreg/mem
266 :prefilter #'prefilter-reg/mem
267 :printer #'print-xmmreg/mem)
269 (defconstant-eqx +conditions+
270 '((:o . 0)
271 (:no . 1)
272 (:b . 2) (:nae . 2) (:c . 2)
273 (:nb . 3) (:ae . 3) (:nc . 3)
274 (:eq . 4) (:e . 4) (:z . 4)
275 (:ne . 5) (:nz . 5)
276 (:be . 6) (:na . 6)
277 (:nbe . 7) (:a . 7)
278 (:s . 8)
279 (:ns . 9)
280 (:p . 10) (:pe . 10)
281 (:np . 11) (:po . 11)
282 (:l . 12) (:nge . 12)
283 (:nl . 13) (:ge . 13)
284 (:le . 14) (:ng . 14)
285 (:nle . 15) (:g . 15))
286 #'equal)
287 (defconstant-eqx sb!vm::+condition-name-vec+
288 #.(let ((vec (make-array 16 :initial-element nil)))
289 (dolist (cond +conditions+ vec)
290 (when (null (aref vec (cdr cond)))
291 (setf (aref vec (cdr cond)) (car cond)))))
292 #'equalp)
294 ;;; SSE shuffle patterns. The names end in the number of bits of the
295 ;;; immediate byte that are used to encode the pattern and the radix
296 ;;; in which to print the value.
297 (macrolet ((define-sse-shuffle-arg-type (name format-string)
298 `(define-arg-type ,name
299 :type 'imm-byte
300 :printer (lambda (value stream dstate)
301 (declare (type (unsigned-byte 8) value)
302 (type stream stream)
303 (ignore dstate))
304 (format stream ,format-string value)))))
305 (define-sse-shuffle-arg-type sse-shuffle-pattern-2-2 "#b~2,'0B")
306 (define-sse-shuffle-arg-type sse-shuffle-pattern-8-4 "#4r~4,4,'0R"))
308 ;;; Set assembler parameters. (In CMU CL, this was done with
309 ;;; a call to a macro DEF-ASSEMBLER-PARAMS.)
310 (eval-when (:compile-toplevel :load-toplevel :execute)
311 (setf sb!assem:*assem-scheduler-p* nil))
313 (define-arg-type condition-code :printer sb!vm::+condition-name-vec+)
315 (defun conditional-opcode (condition)
316 (cdr (assoc condition +conditions+ :test #'eq)))
318 ;;;; disassembler instruction formats
320 (defun swap-if (direction field1 separator field2)
321 `(:if (,direction :constant 0)
322 (,field1 ,separator ,field2)
323 (,field2 ,separator ,field1)))
325 (define-instruction-format (byte 8 :default-printer '(:name))
326 (op :field (byte 8 0))
327 ;; optional fields
328 (accum :type 'accum)
329 (imm))
331 (define-instruction-format (two-bytes 16
332 :default-printer '(:name))
333 (op :fields (list (byte 8 0) (byte 8 8))))
335 (define-instruction-format (three-bytes 24
336 :default-printer '(:name))
337 (op :fields (list (byte 8 0) (byte 8 8) (byte 8 16))))
339 ;;; Prefix instructions
341 (define-instruction-format (rex 8)
342 (rex :field (byte 4 4) :value #b0100)
343 (wrxb :field (byte 4 0) :type 'wrxb))
345 (define-instruction-format (x66 8)
346 (x66 :field (byte 8 0) :type 'x66 :value #x66))
348 ;;; A one-byte instruction with a #x66 prefix, used to indicate an
349 ;;; operand size of :word.
350 (define-instruction-format (x66-byte 16
351 :default-printer '(:name))
352 (x66 :field (byte 8 0) :value #x66)
353 (op :field (byte 8 8)))
355 ;;; A one-byte instruction with a REX prefix, used to indicate an
356 ;;; operand size of :qword. REX.W must be 1, the other three bits are
357 ;;; ignored.
358 (define-instruction-format (rex-byte 16
359 :default-printer '(:name))
360 (rex :field (byte 5 3) :value #b01001)
361 (op :field (byte 8 8)))
363 (define-instruction-format (simple 8)
364 (op :field (byte 7 1))
365 (width :field (byte 1 0) :type 'width)
366 ;; optional fields
367 (accum :type 'accum)
368 (imm))
370 ;;; Same as simple, but with direction bit
371 (define-instruction-format (simple-dir 8 :include simple)
372 (op :field (byte 6 2))
373 (dir :field (byte 1 1)))
375 ;;; Same as simple, but with the immediate value occurring by default,
376 ;;; and with an appropiate printer.
377 (define-instruction-format (accum-imm 8
378 :include simple
379 :default-printer '(:name
380 :tab accum ", " imm))
381 (imm :type 'signed-imm-data))
383 (define-instruction-format (reg-no-width 8
384 :default-printer '(:name :tab reg))
385 (op :field (byte 5 3))
386 (reg :field (byte 3 0) :type 'reg-b)
387 ;; optional fields
388 (accum :type 'accum)
389 (imm))
391 ;;; This is reg-no-width with a mandatory REX prefix and accum field,
392 ;;; with the ability to match against REX.W and REX.B individually.
393 ;;; REX.R and REX.X are ignored.
394 (define-instruction-format (rex-accum-reg 16
395 :default-printer
396 '(:name :tab accum ", " reg))
397 (rex :field (byte 4 4) :value #b0100)
398 (rex-w :field (byte 1 3) :type 'rex-w)
399 (rex-b :field (byte 1 0) :type 'rex-b)
400 (op :field (byte 5 11))
401 (reg :field (byte 3 8) :type 'reg-b)
402 (accum :type 'accum))
404 ;;; Same as reg-no-width, but with a default operand size of :qword.
405 (define-instruction-format (reg-no-width-default-qword 8
406 :include reg-no-width
407 :default-printer '(:name :tab reg))
408 (reg :type 'reg-b-default-qword))
410 ;;; Adds a width field to reg-no-width. Note that we can't use
411 ;;; :INCLUDE REG-NO-WIDTH here to save typing because that would put
412 ;;; the WIDTH field last, but the prefilter for WIDTH must run before
413 ;;; the one for IMM to be able to determine the correct size of IMM.
414 (define-instruction-format (reg 8
415 :default-printer '(:name :tab reg))
416 (op :field (byte 4 4))
417 (width :field (byte 1 3) :type 'width)
418 (reg :field (byte 3 0) :type 'reg-b)
419 ;; optional fields
420 (accum :type 'accum)
421 (imm))
423 (define-instruction-format (reg-reg/mem 16
424 :default-printer
425 `(:name :tab reg ", " reg/mem))
426 (op :field (byte 7 1))
427 (width :field (byte 1 0) :type 'width)
428 (reg/mem :fields (list (byte 2 14) (byte 3 8))
429 :type 'reg/mem :reader reg-r/m-inst-r/m-arg)
430 (reg :field (byte 3 11) :type 'reg)
431 ;; optional fields
432 (imm))
434 ;;; same as reg-reg/mem, but with direction bit
435 (define-instruction-format (reg-reg/mem-dir 16
436 :include reg-reg/mem
437 :default-printer
438 `(:name
439 :tab
440 ,(swap-if 'dir 'reg/mem ", " 'reg)))
441 (op :field (byte 6 2))
442 (dir :field (byte 1 1)))
444 ;;; Same as reg-reg/mem, but uses the reg field as a second op code.
445 (define-instruction-format (reg/mem 16
446 :default-printer '(:name :tab reg/mem))
447 (op :fields (list (byte 7 1) (byte 3 11)))
448 (width :field (byte 1 0) :type 'width)
449 (reg/mem :fields (list (byte 2 14) (byte 3 8))
450 :type 'sized-reg/mem)
451 ;; optional fields
452 (imm))
454 ;;; Same as reg/mem, but without a width field and with a default
455 ;;; operand size of :qword.
456 (define-instruction-format (reg/mem-default-qword 16
457 :default-printer '(:name :tab reg/mem))
458 (op :fields (list (byte 8 0) (byte 3 11)))
459 (reg/mem :fields (list (byte 2 14) (byte 3 8))
460 :type 'sized-reg/mem-default-qword))
462 ;;; Same as reg/mem, but with the immediate value occurring by default,
463 ;;; and with an appropiate printer.
464 (define-instruction-format (reg/mem-imm 16
465 :include reg/mem
466 :default-printer
467 '(:name :tab reg/mem ", " imm))
468 (reg/mem :type 'sized-reg/mem)
469 (imm :type 'signed-imm-data))
471 (define-instruction-format (reg/mem-imm/asm-routine 16
472 :include reg/mem-imm
473 :default-printer
474 '(:name :tab reg/mem ", " imm))
475 (reg/mem :type 'sized-reg/mem)
476 (imm :type 'signed-imm-data/asm-routine))
478 ;;; Same as reg/mem, but with using the accumulator in the default printer
479 (define-instruction-format
480 (accum-reg/mem 16
481 :include reg/mem :default-printer '(:name :tab accum ", " reg/mem))
482 (reg/mem :type 'reg/mem) ; don't need a size
483 (accum :type 'accum))
485 ;;; Same as reg-reg/mem, but with a prefix of #b00001111
486 (define-instruction-format (ext-reg-reg/mem 24
487 :default-printer
488 `(:name :tab reg ", " reg/mem))
489 (prefix :field (byte 8 0) :value #b00001111)
490 (op :field (byte 7 9))
491 (width :field (byte 1 8) :type 'width)
492 (reg/mem :fields (list (byte 2 22) (byte 3 16))
493 :type 'reg/mem)
494 (reg :field (byte 3 19) :type 'reg)
495 ;; optional fields
496 (imm))
498 (define-instruction-format (ext-reg-reg/mem-no-width 24
499 :default-printer
500 `(:name :tab reg ", " reg/mem))
501 (prefix :field (byte 8 0) :value #b00001111)
502 (op :field (byte 8 8))
503 (reg/mem :fields (list (byte 2 22) (byte 3 16))
504 :type 'reg/mem)
505 (reg :field (byte 3 19) :type 'reg)
506 ;; optional fields
507 (imm))
509 (define-instruction-format (ext-reg/mem-no-width 24
510 :default-printer
511 `(:name :tab reg/mem))
512 (prefix :field (byte 8 0) :value #b00001111)
513 (op :fields (list (byte 8 8) (byte 3 19)))
514 (reg/mem :fields (list (byte 2 22) (byte 3 16))
515 :type 'reg/mem))
517 ;;; reg-no-width with #x0f prefix
518 (define-instruction-format (ext-reg-no-width 16
519 :default-printer '(:name :tab reg))
520 (prefix :field (byte 8 0) :value #b00001111)
521 (op :field (byte 5 11))
522 (reg :field (byte 3 8) :type 'reg-b))
524 ;;; Same as reg/mem, but with a prefix of #b00001111
525 (define-instruction-format (ext-reg/mem 24
526 :default-printer '(:name :tab reg/mem))
527 (prefix :field (byte 8 0) :value #b00001111)
528 (op :fields (list (byte 7 9) (byte 3 19)))
529 (width :field (byte 1 8) :type 'width)
530 (reg/mem :fields (list (byte 2 22) (byte 3 16))
531 :type 'sized-reg/mem)
532 ;; optional fields
533 (imm))
535 (define-instruction-format (ext-reg/mem-imm 24
536 :include ext-reg/mem
537 :default-printer
538 '(:name :tab reg/mem ", " imm))
539 (imm :type 'signed-imm-data))
541 (define-instruction-format (ext-reg/mem-no-width+imm8 24
542 :include ext-reg/mem-no-width
543 :default-printer
544 '(:name :tab reg/mem ", " imm))
545 (imm :type 'imm-byte))
547 ;;;; XMM instructions
549 ;;; All XMM instructions use an extended opcode (#x0F as the first
550 ;;; opcode byte). Therefore in the following "EXT" in the name of the
551 ;;; instruction formats refers to the formats that have an additional
552 ;;; prefix (#x66, #xF2 or #xF3).
554 ;;; Instructions having an XMM register as the destination operand
555 ;;; and an XMM register or a memory location as the source operand.
556 ;;; The size of the operands is implicitly given by the instruction.
557 (define-instruction-format (xmm-xmm/mem 24
558 :default-printer
559 '(:name :tab reg ", " reg/mem))
560 (x0f :field (byte 8 0) :value #x0f)
561 (op :field (byte 8 8))
562 (reg/mem :fields (list (byte 2 22) (byte 3 16))
563 :type 'xmmreg/mem)
564 (reg :field (byte 3 19) :type 'xmmreg)
565 ;; optional fields
566 (imm))
568 (define-instruction-format (ext-xmm-xmm/mem 32
569 :default-printer
570 '(:name :tab reg ", " reg/mem))
571 (prefix :field (byte 8 0))
572 (x0f :field (byte 8 8) :value #x0f)
573 (op :field (byte 8 16))
574 (reg/mem :fields (list (byte 2 30) (byte 3 24))
575 :type 'xmmreg/mem)
576 (reg :field (byte 3 27) :type 'xmmreg)
577 (imm))
579 (define-instruction-format (ext-rex-xmm-xmm/mem 40
580 :default-printer
581 '(:name :tab reg ", " reg/mem))
582 (prefix :field (byte 8 0))
583 (rex :field (byte 4 12) :value #b0100)
584 (wrxb :field (byte 4 8) :type 'wrxb)
585 (x0f :field (byte 8 16) :value #x0f)
586 (op :field (byte 8 24))
587 (reg/mem :fields (list (byte 2 38) (byte 3 32))
588 :type 'xmmreg/mem)
589 (reg :field (byte 3 35) :type 'xmmreg)
590 (imm))
592 (define-instruction-format (ext-2byte-xmm-xmm/mem 40
593 :default-printer
594 '(:name :tab reg ", " reg/mem))
595 (prefix :field (byte 8 0))
596 (x0f :field (byte 8 8) :value #x0f)
597 (op1 :field (byte 8 16)) ; #x38 or #x3a
598 (op2 :field (byte 8 24))
599 (reg/mem :fields (list (byte 2 38) (byte 3 32))
600 :type 'xmmreg/mem)
601 (reg :field (byte 3 35) :type 'xmmreg))
603 (define-instruction-format (ext-rex-2byte-xmm-xmm/mem 48
604 :default-printer
605 '(:name :tab reg ", " reg/mem))
606 (prefix :field (byte 8 0))
607 (rex :field (byte 4 12) :value #b0100)
608 (wrxb :field (byte 4 8) :type 'wrxb)
609 (x0f :field (byte 8 16) :value #x0f)
610 (op1 :field (byte 8 24)) ; #x38 or #x3a
611 (op2 :field (byte 8 32))
612 (reg/mem :fields (list (byte 2 46) (byte 3 40))
613 :type 'xmmreg/mem)
614 (reg :field (byte 3 43) :type 'xmmreg))
616 ;;; Same as xmm-xmm/mem etc., but with direction bit.
618 (define-instruction-format (ext-xmm-xmm/mem-dir 32
619 :include ext-xmm-xmm/mem
620 :default-printer
621 `(:name
622 :tab
623 ,(swap-if 'dir 'reg ", " 'reg/mem)))
624 (op :field (byte 7 17))
625 (dir :field (byte 1 16)))
627 (define-instruction-format (ext-rex-xmm-xmm/mem-dir 40
628 :include ext-rex-xmm-xmm/mem
629 :default-printer
630 `(:name
631 :tab
632 ,(swap-if 'dir 'reg ", " 'reg/mem)))
633 (op :field (byte 7 25))
634 (dir :field (byte 1 24)))
636 ;;; Instructions having an XMM register as one operand
637 ;;; and a constant (unsigned) byte as the other.
639 (define-instruction-format (ext-xmm-imm 32
640 :default-printer
641 '(:name :tab reg/mem ", " imm))
642 (prefix :field (byte 8 0))
643 (x0f :field (byte 8 8) :value #x0f)
644 (op :field (byte 8 16))
645 (/i :field (byte 3 27))
646 (b11 :field (byte 2 30) :value #b11)
647 (reg/mem :field (byte 3 24)
648 :type 'xmmreg-b)
649 (imm :type 'imm-byte))
651 (define-instruction-format (ext-rex-xmm-imm 40
652 :default-printer
653 '(:name :tab reg/mem ", " imm))
654 (prefix :field (byte 8 0))
655 (rex :field (byte 4 12) :value #b0100)
656 (wrxb :field (byte 4 8) :type 'wrxb)
657 (x0f :field (byte 8 16) :value #x0f)
658 (op :field (byte 8 24))
659 (/i :field (byte 3 35))
660 (b11 :field (byte 2 38) :value #b11)
661 (reg/mem :field (byte 3 32)
662 :type 'xmmreg-b)
663 (imm :type 'imm-byte))
665 ;;; Instructions having an XMM register as one operand and a general-
666 ;;; -purpose register or a memory location as the other operand.
668 (define-instruction-format (xmm-reg/mem 24
669 :default-printer
670 '(:name :tab reg ", " reg/mem))
671 (x0f :field (byte 8 0) :value #x0f)
672 (op :field (byte 8 8))
673 (reg/mem :fields (list (byte 2 22) (byte 3 16))
674 :type 'sized-reg/mem)
675 (reg :field (byte 3 19) :type 'xmmreg)
676 (imm))
678 (define-instruction-format (ext-xmm-reg/mem 32
679 :default-printer
680 '(:name :tab reg ", " reg/mem))
681 (prefix :field (byte 8 0))
682 (x0f :field (byte 8 8) :value #x0f)
683 (op :field (byte 8 16))
684 (reg/mem :fields (list (byte 2 30) (byte 3 24))
685 :type 'sized-reg/mem)
686 (reg :field (byte 3 27) :type 'xmmreg)
687 (imm))
689 (define-instruction-format (ext-rex-xmm-reg/mem 40
690 :default-printer
691 '(:name :tab reg ", " reg/mem))
692 (prefix :field (byte 8 0))
693 (rex :field (byte 4 12) :value #b0100)
694 (wrxb :field (byte 4 8) :type 'wrxb)
695 (x0f :field (byte 8 16) :value #x0f)
696 (op :field (byte 8 24))
697 (reg/mem :fields (list (byte 2 38) (byte 3 32))
698 :type 'sized-reg/mem)
699 (reg :field (byte 3 35) :type 'xmmreg)
700 (imm))
702 (define-instruction-format (ext-2byte-xmm-reg/mem 40
703 :default-printer
704 '(:name :tab reg ", " reg/mem))
705 (prefix :field (byte 8 0))
706 (x0f :field (byte 8 8) :value #x0f)
707 (op1 :field (byte 8 16))
708 (op2 :field (byte 8 24))
709 (reg/mem :fields (list (byte 2 38) (byte 3 32)) :type 'sized-reg/mem)
710 (reg :field (byte 3 35) :type 'xmmreg)
711 (imm))
713 ;;; Instructions having a general-purpose register as one operand and an
714 ;;; XMM register or a memory location as the other operand.
716 (define-instruction-format (reg-xmm/mem 24
717 :default-printer
718 '(:name :tab reg ", " reg/mem))
719 (x0f :field (byte 8 0) :value #x0f)
720 (op :field (byte 8 8))
721 (reg/mem :fields (list (byte 2 22) (byte 3 16))
722 :type 'xmmreg/mem)
723 (reg :field (byte 3 19) :type 'reg))
725 (define-instruction-format (ext-reg-xmm/mem 32
726 :default-printer
727 '(:name :tab reg ", " reg/mem))
728 (prefix :field (byte 8 0))
729 (x0f :field (byte 8 8) :value #x0f)
730 (op :field (byte 8 16))
731 (reg/mem :fields (list (byte 2 30) (byte 3 24))
732 :type 'xmmreg/mem)
733 (reg :field (byte 3 27) :type 'reg))
735 (define-instruction-format (ext-rex-reg-xmm/mem 40
736 :default-printer
737 '(:name :tab reg ", " reg/mem))
738 (prefix :field (byte 8 0))
739 (rex :field (byte 4 12) :value #b0100)
740 (wrxb :field (byte 4 8) :type 'wrxb)
741 (x0f :field (byte 8 16) :value #x0f)
742 (op :field (byte 8 24))
743 (reg/mem :fields (list (byte 2 38) (byte 3 32))
744 :type 'xmmreg/mem)
745 (reg :field (byte 3 35) :type 'reg))
747 ;;; Instructions having a general-purpose register or a memory location
748 ;;; as one operand and an a XMM register as the other operand.
750 (define-instruction-format (ext-reg/mem-xmm 32
751 :default-printer
752 '(:name :tab reg/mem ", " reg))
753 (prefix :field (byte 8 0))
754 (x0f :field (byte 8 8) :value #x0f)
755 (op :field (byte 8 16))
756 (reg/mem :fields (list (byte 2 30) (byte 3 24))
757 :type 'reg/mem)
758 (reg :field (byte 3 27) :type 'xmmreg)
759 (imm))
761 (define-instruction-format (ext-rex-reg/mem-xmm 40
762 :default-printer
763 '(:name :tab reg/mem ", " reg))
764 (prefix :field (byte 8 0))
765 (rex :field (byte 4 12) :value #b0100)
766 (wrxb :field (byte 4 8) :type 'wrxb)
767 (x0f :field (byte 8 16) :value #x0f)
768 (op :field (byte 8 24))
769 (reg/mem :fields (list (byte 2 38) (byte 3 32))
770 :type 'reg/mem)
771 (reg :field (byte 3 35) :type 'xmmreg)
772 (imm))
774 (define-instruction-format (ext-2byte-reg/mem-xmm 40
775 :default-printer
776 '(:name :tab reg/mem ", " reg))
777 (prefix :field (byte 8 0))
778 (x0f :field (byte 8 8) :value #x0f)
779 (op1 :field (byte 8 16))
780 (op2 :field (byte 8 24))
781 (reg/mem :fields (list (byte 2 38) (byte 3 32)) :type 'reg/mem)
782 (reg :field (byte 3 35) :type 'xmmreg)
783 (imm))
785 (define-instruction-format (ext-rex-2byte-reg/mem-xmm 48
786 :default-printer
787 '(:name :tab reg/mem ", " reg))
788 (prefix :field (byte 8 0))
789 (rex :field (byte 4 12) :value #b0100)
790 (wrxb :field (byte 4 8) :type 'wrxb)
791 (x0f :field (byte 8 16) :value #x0f)
792 (op1 :field (byte 8 24))
793 (op2 :field (byte 8 32))
794 (reg/mem :fields (list (byte 2 46) (byte 3 40)) :type 'reg/mem)
795 (reg :field (byte 3 43) :type 'xmmreg)
796 (imm))
798 ;;; Instructions having a general-purpose register as one operand and an a
799 ;;; general-purpose register or a memory location as the other operand,
800 ;;; and using a prefix byte.
802 (define-instruction-format (ext-prefix-reg-reg/mem 32
803 :default-printer
804 '(:name :tab reg ", " reg/mem))
805 (prefix :field (byte 8 0))
806 (x0f :field (byte 8 8) :value #x0f)
807 (op :field (byte 8 16))
808 (reg/mem :fields (list (byte 2 30) (byte 3 24))
809 :type 'sized-reg/mem)
810 (reg :field (byte 3 27) :type 'reg))
812 (define-instruction-format (ext-rex-prefix-reg-reg/mem 40
813 :default-printer
814 '(:name :tab reg ", " reg/mem))
815 (prefix :field (byte 8 0))
816 (rex :field (byte 4 12) :value #b0100)
817 (wrxb :field (byte 4 8) :type 'wrxb)
818 (x0f :field (byte 8 16) :value #x0f)
819 (op :field (byte 8 24))
820 (reg/mem :fields (list (byte 2 38) (byte 3 32))
821 :type 'sized-reg/mem)
822 (reg :field (byte 3 35) :type 'reg))
824 (define-instruction-format (ext-2byte-prefix-reg-reg/mem 40
825 :default-printer
826 '(:name :tab reg ", " reg/mem))
827 (prefix :field (byte 8 0))
828 (x0f :field (byte 8 8) :value #x0f)
829 (op1 :field (byte 8 16)) ; #x38 or #x3a
830 (op2 :field (byte 8 24))
831 (reg/mem :fields (list (byte 2 38) (byte 3 32))
832 :type 'sized-reg/mem)
833 (reg :field (byte 3 35) :type 'reg))
835 (define-instruction-format (ext-rex-2byte-prefix-reg-reg/mem 48
836 :default-printer
837 '(:name :tab reg ", " reg/mem))
838 (prefix :field (byte 8 0))
839 (rex :field (byte 4 12) :value #b0100)
840 (wrxb :field (byte 4 8) :type 'wrxb)
841 (x0f :field (byte 8 16) :value #x0f)
842 (op1 :field (byte 8 24)) ; #x38 or #x3a
843 (op2 :field (byte 8 32))
844 (reg/mem :fields (list (byte 2 46) (byte 3 40))
845 :type 'sized-reg/mem)
846 (reg :field (byte 3 43) :type 'reg))
848 ;; XMM comparison instruction
850 (defconstant-eqx +sse-conditions+
851 #(:eq :lt :le :unord :neq :nlt :nle :ord)
852 #'equalp)
854 (define-arg-type sse-condition-code
855 ;; Inherit the prefilter from IMM-BYTE to READ-SUFFIX the byte.
856 :type 'imm-byte
857 :printer +sse-conditions+)
859 (define-instruction-format (string-op 8
860 :include simple
861 :default-printer '(:name width)))
863 (define-instruction-format (short-cond-jump 16)
864 (op :field (byte 4 4))
865 (cc :field (byte 4 0) :type 'condition-code)
866 (label :field (byte 8 8) :type 'displacement))
868 (define-instruction-format (short-jump 16 :default-printer '(:name :tab label))
869 (const :field (byte 4 4) :value #b1110)
870 (op :field (byte 4 0))
871 (label :field (byte 8 8) :type 'displacement))
873 (define-instruction-format (near-cond-jump 48)
874 (op :fields (list (byte 8 0) (byte 4 12)) :value '(#b00001111 #b1000))
875 (cc :field (byte 4 8) :type 'condition-code)
876 (label :field (byte 32 16) :type 'displacement))
878 (define-instruction-format (near-jump 40 :default-printer '(:name :tab label))
879 (op :field (byte 8 0))
880 (label :field (byte 32 8) :type 'displacement :reader near-jump-displacement))
882 (define-instruction-format (cond-set 24 :default-printer '('set cc :tab reg/mem))
883 (prefix :field (byte 8 0) :value #b00001111)
884 (op :field (byte 4 12) :value #b1001)
885 (cc :field (byte 4 8) :type 'condition-code)
886 (reg/mem :fields (list (byte 2 22) (byte 3 16))
887 :type 'sized-byte-reg/mem)
888 (reg :field (byte 3 19) :value #b000))
890 (define-instruction-format (cond-move 24
891 :default-printer
892 '('cmov cc :tab reg ", " reg/mem))
893 (prefix :field (byte 8 0) :value #b00001111)
894 (op :field (byte 4 12) :value #b0100)
895 (cc :field (byte 4 8) :type 'condition-code)
896 (reg/mem :fields (list (byte 2 22) (byte 3 16))
897 :type 'reg/mem)
898 (reg :field (byte 3 19) :type 'reg))
900 (define-instruction-format (enter-format 32
901 :default-printer '(:name
902 :tab disp
903 (:unless (:constant 0)
904 ", " level)))
905 (op :field (byte 8 0))
906 (disp :field (byte 16 8))
907 (level :field (byte 8 24)))
909 ;;; Single byte instruction with an immediate byte argument.
910 (define-instruction-format (byte-imm 16 :default-printer '(:name :tab code))
911 (op :field (byte 8 0))
912 (code :field (byte 8 8) :reader byte-imm-code))
914 ;;; Two byte instruction with an immediate byte argument.
916 (define-instruction-format (word-imm 24 :default-printer '(:name :tab code))
917 (op :field (byte 16 0))
918 (code :field (byte 8 16) :reader word-imm-code))
920 ;;; F3 escape map - Needs a ton more work.
922 (define-instruction-format (F3-escape 24)
923 (prefix1 :field (byte 8 0) :value #xF3)
924 (prefix2 :field (byte 8 8) :value #x0F)
925 (op :field (byte 8 16)))
927 (define-instruction-format (rex-F3-escape 32)
928 ;; F3 is a legacy prefix which was generalized to select an alternate opcode
929 ;; map. Legacy prefixes are encoded in the instruction before a REX prefix.
930 (prefix1 :field (byte 8 0) :value #xF3)
931 (rex :field (byte 4 12) :value 4) ; "prefix2"
932 (wrxb :field (byte 4 8) :type 'wrxb)
933 (prefix3 :field (byte 8 16) :value #x0F)
934 (op :field (byte 8 24)))
936 (define-instruction-format (F3-escape-reg-reg/mem 32
937 :include F3-escape
938 :default-printer
939 '(:name :tab reg ", " reg/mem))
940 (reg/mem :fields (list (byte 2 30) (byte 3 24)) :type 'sized-reg/mem)
941 (reg :field (byte 3 27) :type 'reg))
943 (define-instruction-format (rex-F3-escape-reg-reg/mem 40
944 :include rex-F3-escape
945 :default-printer
946 '(:name :tab reg ", " reg/mem))
947 (reg/mem :fields (list (byte 2 38) (byte 3 32)) :type 'sized-reg/mem)
948 (reg :field (byte 3 35) :type 'reg))
951 ;;;; primitive emitters
953 (define-bitfield-emitter emit-word 16
954 (byte 16 0))
956 ;; FIXME: a nice enhancement would be to save all sexprs of small functions
957 ;; within the same file, and drop them at the end.
958 ;; Expressly declaimed inline definitions would be saved as usual though.
959 (declaim (inline emit-dword))
960 (define-bitfield-emitter emit-dword 32
961 (byte 32 0))
962 (declaim (notinline emit-dword))
964 ;;; Most uses of dwords are as displacements or as immediate values in
965 ;;; 64-bit operations. In these cases they are sign-extended to 64 bits.
966 ;;; EMIT-DWORD is unsuitable there because it accepts values of type
967 ;;; (OR (SIGNED-BYTE 32) (UNSIGNED-BYTE 32)), so we provide a more
968 ;;; restricted emitter here.
969 (defun emit-signed-dword (segment value)
970 (declare (type sb!assem:segment segment)
971 (type (signed-byte 32) value))
972 (declare (inline emit-dword))
973 (emit-dword segment value))
975 (define-bitfield-emitter emit-qword 64
976 (byte 64 0))
978 (define-bitfield-emitter emit-mod-reg-r/m-byte 8
979 (byte 2 6) (byte 3 3) (byte 3 0))
981 (define-bitfield-emitter emit-sib-byte 8
982 (byte 2 6) (byte 3 3) (byte 3 0))
984 (define-bitfield-emitter emit-rex-byte 8
985 (byte 4 4) (byte 1 3) (byte 1 2) (byte 1 1) (byte 1 0))
989 ;;;; fixup emitters
991 (defun emit-absolute-fixup (segment fixup &optional quad-p)
992 (note-fixup segment (if quad-p :absolute64 :absolute) fixup)
993 (let ((offset (fixup-offset fixup)))
994 (if (label-p offset)
995 (emit-back-patch segment
996 (if quad-p 8 4)
997 (lambda (segment posn)
998 (declare (ignore posn))
999 (let ((val (- (+ (component-header-length)
1000 (or (label-position offset)
1002 other-pointer-lowtag)))
1003 (if quad-p
1004 (emit-qword segment val)
1005 (emit-signed-dword segment val)))))
1006 (if quad-p
1007 (emit-qword segment (or offset 0))
1008 (emit-signed-dword segment (or offset 0))))))
1010 (defun emit-relative-fixup (segment fixup)
1011 (note-fixup segment :relative fixup)
1012 (emit-signed-dword segment (or (fixup-offset fixup) 0)))
1015 ;;;; the effective-address (ea) structure
1017 (declaim (ftype (sfunction (tn) (mod 8)) reg-tn-encoding))
1018 (defun reg-tn-encoding (tn)
1019 (declare (type tn tn))
1020 ;; ea only has space for three bits of register number: regs r8
1021 ;; and up are selected by a REX prefix byte which caller is responsible
1022 ;; for having emitted where necessary already
1023 (ecase (sb-name (sc-sb (tn-sc tn)))
1024 (registers
1025 (let ((offset (mod (tn-offset tn) 16)))
1026 (logior (ash (logand offset 1) 2)
1027 (ash offset -1))))
1028 (float-registers
1029 (mod (tn-offset tn) 8))))
1031 (defun emit-byte+reg (seg byte reg)
1032 (emit-byte seg (+ byte (reg-tn-encoding reg))))
1034 (defstruct (ea (:constructor make-ea (size &key base index scale disp))
1035 (:copier nil))
1036 ;; note that we can represent an EA with a QWORD size, but EMIT-EA
1037 ;; can't actually emit it on its own: caller also needs to emit REX
1038 ;; prefix
1039 (size nil :type (member :byte :word :dword :qword))
1040 (base nil :type (or tn null))
1041 (index nil :type (or tn null))
1042 (scale 1 :type (member 1 2 4 8))
1043 (disp 0 :type (or (unsigned-byte 32) (signed-byte 32) fixup)))
1044 (defmethod print-object ((ea ea) stream)
1045 (cond ((or *print-escape* *print-readably*)
1046 (print-unreadable-object (ea stream :type t)
1047 (format stream
1048 "~S~@[ base=~S~]~@[ index=~S~]~@[ scale=~S~]~@[ disp=~S~]"
1049 (ea-size ea)
1050 (ea-base ea)
1051 (ea-index ea)
1052 (let ((scale (ea-scale ea)))
1053 (if (= scale 1) nil scale))
1054 (ea-disp ea))))
1056 (format stream "~A PTR [" (symbol-name (ea-size ea)))
1057 (when (ea-base ea)
1058 (write-string (sb!c:location-print-name (ea-base ea)) stream)
1059 (when (ea-index ea)
1060 (write-string "+" stream)))
1061 (when (ea-index ea)
1062 (write-string (sb!c:location-print-name (ea-index ea)) stream))
1063 (unless (= (ea-scale ea) 1)
1064 (format stream "*~A" (ea-scale ea)))
1065 (typecase (ea-disp ea)
1066 (null)
1067 (integer
1068 (format stream "~@D" (ea-disp ea)))
1070 (format stream "+~A" (ea-disp ea))))
1071 (write-char #\] stream))))
1073 (defun sized-ea (ea new-size)
1074 (make-ea new-size
1075 :base (ea-base ea) :index (ea-index ea) :scale (ea-scale ea)
1076 :disp (ea-disp ea)))
1078 (defun emit-constant-tn-rip (segment constant-tn reg remaining-bytes)
1079 ;; AMD64 doesn't currently have a code object register to use as a
1080 ;; base register for constant access. Instead we use RIP-relative
1081 ;; addressing. The offset from the SIMPLE-FUN-HEADER to the instruction
1082 ;; is passed to the backpatch callback. In addition we need the offset
1083 ;; from the start of the function header to the slot in the CODE-HEADER
1084 ;; that stores the constant. Since we don't know where the code header
1085 ;; starts, instead count backwards from the function header.
1086 (let* ((2comp (component-info *component-being-compiled*))
1087 (constants (ir2-component-constants 2comp))
1088 (len (length constants))
1089 ;; Both CODE-HEADER and SIMPLE-FUN-HEADER are 16-byte aligned.
1090 ;; If there are an even amount of constants, there will be
1091 ;; an extra qword of padding before the function header, which
1092 ;; needs to be adjusted for. XXX: This will break if new slots
1093 ;; are added to the code header.
1094 (offset (* (- (+ len (if (evenp len)
1097 (tn-offset constant-tn))
1098 n-word-bytes)))
1099 ;; RIP-relative addressing
1100 (emit-mod-reg-r/m-byte segment #b00 reg #b101)
1101 (emit-back-patch segment
1103 (lambda (segment posn)
1104 ;; The addressing is relative to end of instruction,
1105 ;; i.e. the end of this dword. Hence the + 4.
1106 (emit-signed-dword segment
1107 (+ 4 remaining-bytes
1108 (- (+ offset posn)))))))
1109 (values))
1111 (defun emit-byte-displacement-backpatch (segment target)
1112 (emit-back-patch segment 1
1113 (lambda (segment posn)
1114 (emit-byte segment
1115 (the (signed-byte 8)
1116 (- (label-position target) (1+ posn)))))))
1118 (defun emit-dword-displacement-backpatch (segment target &optional (n-extra 0))
1119 ;; N-EXTRA is how many more instruction bytes will follow, to properly compute
1120 ;; the displacement from the beginning of the next instruction to TARGET.
1121 (emit-back-patch segment 4
1122 (lambda (segment posn)
1123 (emit-signed-dword segment (- (label-position target)
1124 (+ 4 posn n-extra))))))
1126 (defun emit-label-rip (segment fixup reg remaining-bytes)
1127 ;; RIP-relative addressing
1128 (emit-mod-reg-r/m-byte segment #b00 reg #b101)
1129 (emit-dword-displacement-backpatch segment (fixup-offset fixup) remaining-bytes)
1130 (values))
1132 (defun emit-ea (segment thing reg &key allow-constants (remaining-bytes 0))
1133 (etypecase thing
1135 ;; this would be eleganter if we had a function that would create
1136 ;; an ea given a tn
1137 (ecase (sb-name (sc-sb (tn-sc thing)))
1138 ((registers float-registers)
1139 (emit-mod-reg-r/m-byte segment #b11 reg (reg-tn-encoding thing)))
1140 (stack
1141 ;; Convert stack tns into an index off RBP.
1142 (let ((disp (frame-byte-offset (tn-offset thing))))
1143 (cond ((<= -128 disp 127)
1144 (emit-mod-reg-r/m-byte segment #b01 reg #b101)
1145 (emit-byte segment disp))
1147 (emit-mod-reg-r/m-byte segment #b10 reg #b101)
1148 (emit-signed-dword segment disp)))))
1149 (constant
1150 (unless allow-constants
1151 ;; Why?
1152 (error
1153 "Constant TNs can only be directly used in MOV, PUSH, and CMP."))
1154 (emit-constant-tn-rip segment thing reg remaining-bytes))))
1156 (let* ((base (ea-base thing))
1157 (index (ea-index thing))
1158 (scale (ea-scale thing))
1159 (disp (ea-disp thing))
1160 (mod (cond ((or (null base)
1161 (and (eql disp 0)
1162 (not (= (reg-tn-encoding base) #b101))))
1163 #b00)
1164 ((and (fixnump disp) (<= -128 disp 127))
1165 #b01)
1167 #b10)))
1168 (r/m (cond (index #b100)
1169 ((null base) #b101)
1170 (t (reg-tn-encoding base)))))
1171 (when (and (fixup-p disp)
1172 (label-p (fixup-offset disp)))
1173 (aver (null base))
1174 (aver (null index))
1175 (return-from emit-ea (emit-ea segment disp reg
1176 :allow-constants allow-constants
1177 :remaining-bytes remaining-bytes)))
1178 (when (and (= mod 0) (= r/m #b101))
1179 ;; this is rip-relative in amd64, so we'll use a sib instead
1180 (setf r/m #b100 scale 1))
1181 (emit-mod-reg-r/m-byte segment mod reg r/m)
1182 (when (= r/m #b100)
1183 (let ((ss (1- (integer-length scale)))
1184 (index (if (null index)
1185 #b100
1186 (if (location= index sb!vm::rsp-tn)
1187 (error "can't index off of RSP")
1188 (reg-tn-encoding index))))
1189 (base (if (null base)
1190 #b101
1191 (reg-tn-encoding base))))
1192 (emit-sib-byte segment ss index base)))
1193 (cond ((= mod #b01)
1194 (emit-byte segment disp))
1195 ((or (= mod #b10) (null base))
1196 (if (fixup-p disp)
1197 (emit-absolute-fixup segment disp)
1198 (emit-signed-dword segment disp))))))
1199 (fixup
1200 (typecase (fixup-offset thing)
1201 (label
1202 (when (eq (fixup-flavor thing) :closure)
1203 ;; A closure entry label points to a simple-fun header word, and not
1204 ;; the first executable instruction. To get the proper entry address,
1205 ;; make 'remaining-bytes' negative so that the origin of the offset
1206 ;; calculation appears as if earlier in the instruction stream by
1207 ;; exactly 6 words. The computed EA will come out right.
1208 (decf remaining-bytes (* n-word-bytes simple-fun-code-offset)))
1209 (emit-label-rip segment thing reg remaining-bytes))
1211 (emit-mod-reg-r/m-byte segment #b00 reg #b100)
1212 (emit-sib-byte segment 0 #b100 #b101)
1213 (emit-absolute-fixup segment thing))))))
1215 (defun byte-reg-p (thing)
1216 (and (tn-p thing)
1217 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)
1218 (member (sc-name (tn-sc thing)) *byte-sc-names*)
1221 (defun byte-ea-p (thing)
1222 (typecase thing
1223 (ea (eq (ea-size thing) :byte))
1225 (and (member (sc-name (tn-sc thing)) *byte-sc-names*) t))
1226 (t nil)))
1228 (defun word-reg-p (thing)
1229 (and (tn-p thing)
1230 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)
1231 (member (sc-name (tn-sc thing)) *word-sc-names*)
1234 (defun word-ea-p (thing)
1235 (typecase thing
1236 (ea (eq (ea-size thing) :word))
1237 (tn (and (member (sc-name (tn-sc thing)) *word-sc-names*) t))
1238 (t nil)))
1240 (defun dword-reg-p (thing)
1241 (and (tn-p thing)
1242 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)
1243 (member (sc-name (tn-sc thing)) *dword-sc-names*)
1246 (defun dword-ea-p (thing)
1247 (typecase thing
1248 (ea (eq (ea-size thing) :dword))
1250 (and (member (sc-name (tn-sc thing)) *dword-sc-names*) t))
1251 (t nil)))
1253 (defun qword-reg-p (thing)
1254 (and (tn-p thing)
1255 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)
1256 (member (sc-name (tn-sc thing)) *qword-sc-names*)
1259 (defun qword-ea-p (thing)
1260 (typecase thing
1261 (ea (eq (ea-size thing) :qword))
1263 (and (member (sc-name (tn-sc thing)) *qword-sc-names*) t))
1264 (t nil)))
1266 ;;; Return true if THING is a general-purpose register TN.
1267 (defun register-p (thing)
1268 (and (tn-p thing)
1269 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)))
1271 (defun accumulator-p (thing)
1272 (and (register-p thing)
1273 (= (tn-offset thing) 0)))
1275 ;;; Return true if THING is an XMM register TN.
1276 (defun xmm-register-p (thing)
1277 (and (tn-p thing)
1278 (eq (sb-name (sc-sb (tn-sc thing))) 'float-registers)))
1281 ;;;; utilities
1283 (defconstant +operand-size-prefix-byte+ #b01100110)
1285 (defun maybe-emit-operand-size-prefix (segment size)
1286 (unless (or (eq size :byte)
1287 (eq size :qword) ; REX prefix handles this
1288 (eq size +default-operand-size+))
1289 (emit-byte segment +operand-size-prefix-byte+)))
1291 ;;; A REX prefix must be emitted if at least one of the following
1292 ;;; conditions is true:
1293 ;; 1. The operand size is :QWORD and the default operand size of the
1294 ;; instruction is not :QWORD.
1295 ;;; 2. The instruction references an extended register.
1296 ;;; 3. The instruction references one of the byte registers SIL, DIL,
1297 ;;; SPL or BPL.
1299 ;;; Emit a REX prefix if necessary. OPERAND-SIZE is used to determine
1300 ;;; whether to set REX.W. Callers pass it explicitly as :DO-NOT-SET if
1301 ;;; this should not happen, for example because the instruction's
1302 ;;; default operand size is qword. R, X and B are NIL or TNs specifying
1303 ;;; registers the encodings of which are extended with the REX.R, REX.X
1304 ;;; and REX.B bit, respectively. To determine whether one of the byte
1305 ;;; registers is used that can only be accessed using a REX prefix, we
1306 ;;; need only to test R and B, because X is only used for the index
1307 ;;; register of an effective address and therefore never byte-sized.
1308 ;;; For R we can avoid to calculate the size of the TN because it is
1309 ;;; always OPERAND-SIZE. The size of B must be calculated here because
1310 ;;; B can be address-sized (if it is the base register of an effective
1311 ;;; address), of OPERAND-SIZE (if the instruction operates on two
1312 ;;; registers) or of some different size (in the instructions that
1313 ;;; combine arguments of different sizes: MOVZX, MOVSX, MOVSXD and
1314 ;;; several SSE instructions, e.g. CVTSD2SI). We don't distinguish
1315 ;;; between general-purpose and floating point registers for this cause
1316 ;;; because only general-purpose registers can be byte-sized at all.
1317 (defun maybe-emit-rex-prefix (segment operand-size r x b)
1318 (declare (type (member nil :byte :word :dword :qword :do-not-set)
1319 operand-size)
1320 (type (or null tn) r x b))
1321 (labels ((if-hi (r)
1322 (if (and r (> (tn-offset r)
1323 ;; offset of r8 is 16, offset of xmm8 is 8
1324 (if (eq (sb-name (sc-sb (tn-sc r)))
1325 'float-registers)
1327 15)))
1330 (reg-4-7-p (r)
1331 ;; Assuming R is a TN describing a general-purpose
1332 ;; register, return true if it references register
1333 ;; 4 upto 7.
1334 (<= 8 (tn-offset r) 15)))
1335 (let ((rex-w (if (eq operand-size :qword) 1 0))
1336 (rex-r (if-hi r))
1337 (rex-x (if-hi x))
1338 (rex-b (if-hi b)))
1339 (when (or (not (zerop (logior rex-w rex-r rex-x rex-b)))
1340 (and r
1341 (eq operand-size :byte)
1342 (reg-4-7-p r))
1343 (and b
1344 (eq (operand-size b) :byte)
1345 (reg-4-7-p b)))
1346 (emit-rex-byte segment #b0100 rex-w rex-r rex-x rex-b)))))
1348 ;;; Emit a REX prefix if necessary. The operand size is determined from
1349 ;;; THING or can be overwritten by OPERAND-SIZE. This and REG are always
1350 ;;; passed to MAYBE-EMIT-REX-PREFIX. Additionally, if THING is an EA we
1351 ;;; pass its index and base registers, if it is a register TN, we pass
1352 ;;; only itself.
1353 ;;; In contrast to EMIT-EA above, neither stack TNs nor fixups need to
1354 ;;; be treated specially here: If THING is a stack TN, neither it nor
1355 ;;; any of its components are passed to MAYBE-EMIT-REX-PREFIX which
1356 ;;; works correctly because stack references always use RBP as the base
1357 ;;; register and never use an index register so no extended registers
1358 ;;; need to be accessed. Fixups are assembled using an addressing mode
1359 ;;; of displacement-only or RIP-plus-displacement (see EMIT-EA), so may
1360 ;;; not reference an extended register. The displacement-only addressing
1361 ;;; mode requires that REX.X is 0, which is ensured here.
1362 (defun maybe-emit-rex-for-ea (segment thing reg &key operand-size)
1363 (declare (type (or ea tn fixup) thing)
1364 (type (or null tn) reg)
1365 (type (member nil :byte :word :dword :qword :do-not-set)
1366 operand-size))
1367 (let ((ea-p (ea-p thing)))
1368 (maybe-emit-rex-prefix segment
1369 (or operand-size (operand-size thing))
1371 (and ea-p (ea-index thing))
1372 (cond (ea-p (ea-base thing))
1373 ((and (tn-p thing)
1374 (member (sb-name (sc-sb (tn-sc thing)))
1375 '(float-registers registers)))
1376 thing)
1377 (t nil)))))
1379 (defun operand-size (thing)
1380 (typecase thing
1382 ;; FIXME: might as well be COND instead of having to use #. readmacro
1383 ;; to hack up the code
1384 (case (sc-name (tn-sc thing))
1385 #!+sb-simd-pack
1386 (#.sb!vm::*oword-sc-names*
1387 :oword)
1388 (#.*qword-sc-names*
1389 :qword)
1390 (#.*dword-sc-names*
1391 :dword)
1392 (#.*word-sc-names*
1393 :word)
1394 (#.*byte-sc-names*
1395 :byte)
1396 ;; added by jrd: float-registers is a separate size (?)
1397 ;; The only place in the code where we are called with THING
1398 ;; being a float-register is in MAYBE-EMIT-REX-PREFIX when it
1399 ;; checks whether THING is a byte register. Thus our result in
1400 ;; these cases could as well be :dword and :qword. I leave it as
1401 ;; :float and :double which is more likely to trigger an aver
1402 ;; instead of silently doing the wrong thing in case this
1403 ;; situation should change. Lutz Euler, 2005-10-23.
1404 (#.sb!vm::*float-sc-names*
1405 :float)
1406 (#.sb!vm::*double-sc-names*
1407 :double)
1408 (#.sb!vm::*complex-sc-names*
1409 :complex)
1411 (error "can't tell the size of ~S ~S" thing (sc-name (tn-sc thing))))))
1413 (ea-size thing))
1414 (fixup
1415 ;; GNA. Guess who spelt "flavor" correctly first time round?
1416 ;; There's a strong argument in my mind to change all uses of
1417 ;; "flavor" to "kind": and similarly with some misguided uses of
1418 ;; "type" here and there. -- CSR, 2005-01-06.
1419 (case (fixup-flavor thing)
1420 ((:foreign-dataref) :qword)))
1422 nil)))
1424 (defun matching-operand-size (dst src)
1425 (let ((dst-size (operand-size dst))
1426 (src-size (operand-size src)))
1427 (if dst-size
1428 (if src-size
1429 (if (eq dst-size src-size)
1430 dst-size
1431 (error "size mismatch: ~S is a ~S and ~S is a ~S."
1432 dst dst-size src src-size))
1433 dst-size)
1434 (if src-size
1435 src-size
1436 (error "can't tell the size of either ~S or ~S" dst src)))))
1438 ;;; Except in a very few cases (MOV instructions A1, A3 and B8 - BF)
1439 ;;; we expect dword data bytes even when 64 bit work is being done.
1440 ;;; But A1 and A3 are currently unused and B8 - BF use EMIT-QWORD
1441 ;;; directly, so we emit all quad constants as dwords, additionally
1442 ;;; making sure that they survive the sign-extension to 64 bits
1443 ;;; unchanged.
1444 (defun emit-sized-immediate (segment size value)
1445 (ecase size
1446 (:byte
1447 (emit-byte segment value))
1448 (:word
1449 (emit-word segment value))
1450 (:dword
1451 (emit-dword segment value))
1452 (:qword
1453 (emit-signed-dword segment value))))
1455 ;;;; prefixes
1457 (define-instruction rex (segment)
1458 (:printer rex () nil :print-name nil))
1460 (define-instruction x66 (segment)
1461 (:printer x66 () nil :print-name nil))
1463 (defun emit-prefix (segment name)
1464 (declare (ignorable segment))
1465 (ecase name
1466 ((nil))
1467 (:lock
1468 #!+sb-thread
1469 (emit-byte segment #xf0))))
1471 (define-instruction fs (segment)
1472 (:printer byte ((op #x64)) nil))
1474 (define-instruction lock (segment)
1475 (:printer byte ((op #b11110000)) nil))
1477 (define-instruction rep (segment)
1478 (:emitter
1479 (emit-byte segment #b11110011)))
1481 (define-instruction repe (segment)
1482 (:printer byte ((op #b11110011)) nil)
1483 (:emitter
1484 (emit-byte segment #b11110011)))
1486 (define-instruction repne (segment)
1487 (:printer byte ((op #b11110010)) nil)
1488 (:emitter
1489 (emit-byte segment #b11110010)))
1491 ;;;; general data transfer
1493 (define-instruction mov (segment dst src)
1494 ;; immediate to register
1495 (:printer reg ((op #b1011 :prefilter (lambda (dstate value)
1496 (dstate-put-inst-prop dstate +allow-qword-imm+)
1497 value))
1498 (imm nil :type 'signed-imm-data/asm-routine))
1499 '(:name :tab reg ", " imm))
1500 ;; absolute mem to/from accumulator
1501 (:printer simple-dir ((op #b101000) (imm nil :type 'imm-addr))
1502 `(:name :tab ,(swap-if 'dir 'accum ", " '("[" imm "]"))))
1503 ;; register to/from register/memory
1504 (:printer reg-reg/mem-dir ((op #b100010)))
1505 ;; immediate to register/memory
1506 (:printer reg/mem-imm/asm-routine ((op '(#b1100011 #b000))))
1508 (:emitter
1509 (let ((size (matching-operand-size dst src)))
1510 (maybe-emit-operand-size-prefix segment size)
1511 (cond ((register-p dst)
1512 (cond ((integerp src)
1513 ;; We want to encode the immediate using the fewest bytes possible.
1514 (let ((immediate-size
1515 ;; If it's a :qword constant that fits in an unsigned
1516 ;; :dword, then use a zero-extended :dword immediate.
1517 (if (and (eq size :qword) (typep src '(unsigned-byte 32)))
1518 :dword
1519 size)))
1520 (maybe-emit-rex-prefix segment immediate-size nil nil dst))
1521 (acond ((neq size :qword) ; :dword or smaller dst is straightforward
1522 (emit-byte+reg segment (if (eq size :byte) #xB0 #xB8) dst)
1523 (emit-sized-immediate segment size src))
1524 ;; This must be move to a :qword register.
1525 ((typep src '(unsigned-byte 32))
1526 ;; Encode as B8+dst using operand size of 32 bits
1527 ;; and implicit zero-extension.
1528 ;; Instruction size: 5 if no REX prefix, or 6 with.
1529 (emit-byte+reg segment #xB8 dst)
1530 (emit-dword segment src))
1531 ((sb!vm::immediate32-p src)
1532 ;; It's either a signed-byte-32, or a large unsigned
1533 ;; value whose 33 high bits are all 1.
1534 ;; Encode as C7 which sign-extends a 32-bit imm to 64 bits.
1535 ;; Instruction size: 7 bytes.
1536 (emit-byte segment #xC7)
1537 (emit-mod-reg-r/m-byte segment #b11 #b000 (reg-tn-encoding dst))
1538 (emit-signed-dword segment it))
1540 ;; 64-bit immediate. Instruction size: 10 bytes.
1541 (emit-byte+reg segment #xB8 dst)
1542 (emit-qword segment src))))
1543 ((and (fixup-p src)
1544 (member (fixup-flavor src)
1545 '(:named-call :static-call :assembly-routine
1546 :layout :immobile-object :foreign)))
1547 (maybe-emit-rex-prefix segment :dword nil nil dst)
1548 (emit-byte+reg segment #xB8 dst)
1549 (emit-absolute-fixup segment src))
1551 (maybe-emit-rex-for-ea segment src dst)
1552 (emit-byte segment (if (eq size :byte) #x8A #x8B))
1553 (emit-ea segment src (reg-tn-encoding dst)
1554 :allow-constants t))))
1555 ((integerp src) ; imm to memory
1556 ;; C7 only deals with 32 bit immediates even if the
1557 ;; destination is a 64-bit location. The value is
1558 ;; sign-extended in this case.
1559 (maybe-emit-rex-for-ea segment dst nil)
1560 (emit-byte segment (if (eq size :byte) #xC6 #xC7))
1561 (emit-ea segment dst #b000)
1562 (emit-sized-immediate segment size src))
1563 ((register-p src) ; reg to mem
1564 (maybe-emit-rex-for-ea segment dst src)
1565 (emit-byte segment (if (eq size :byte) #x88 #x89))
1566 (emit-ea segment dst (reg-tn-encoding src)))
1567 ((fixup-p src)
1568 ;; Generally we can't MOV a fixupped value into an EA, since
1569 ;; MOV on non-registers can only take a 32-bit immediate arg.
1570 ;; Make an exception for :FOREIGN fixups (pretty much just
1571 ;; the runtime asm, since other foreign calls go through the
1572 ;; the linkage table) and for linkage table references, since
1573 ;; these should always end up in low memory.
1574 (aver (or (member (fixup-flavor src)
1575 '(:foreign :foreign-dataref :symbol-tls-index
1576 :assembly-routine :layout :immobile-object))
1577 (eq (ea-size dst) :dword)))
1578 (maybe-emit-rex-for-ea segment dst nil)
1579 (emit-byte segment #xC7)
1580 (emit-ea segment dst #b000)
1581 (emit-absolute-fixup segment src))
1583 (error "bogus arguments to MOV: ~S ~S" dst src))))))
1585 ;;; Emit a sign-extending (if SIGNED-P is true) or zero-extending move.
1586 ;;; To achieve the shortest possible encoding zero extensions into a
1587 ;;; 64-bit destination are assembled as a straight 32-bit MOV (if the
1588 ;;; source size is 32 bits) or as MOVZX with a 32-bit destination (if
1589 ;;; the source size is 8 or 16 bits). Due to the implicit zero extension
1590 ;;; to 64 bits this has the same effect as a MOVZX with 64-bit
1591 ;;; destination but often needs no REX prefix.
1592 (defun emit-move-with-extension (segment dst src signed-p)
1593 (aver (register-p dst))
1594 (let ((dst-size (operand-size dst))
1595 (src-size (operand-size src))
1596 (opcode (if signed-p #b10111110 #b10110110)))
1597 (macrolet ((emitter (operand-size &rest bytes)
1598 `(progn
1599 (maybe-emit-rex-for-ea segment src dst
1600 :operand-size ,operand-size)
1601 ,@(mapcar (lambda (byte)
1602 `(emit-byte segment ,byte))
1603 bytes)
1604 (emit-ea segment src (reg-tn-encoding dst)))))
1605 (ecase dst-size
1606 (:word
1607 (aver (eq src-size :byte))
1608 (maybe-emit-operand-size-prefix segment :word)
1609 (emitter :word #b00001111 opcode))
1610 ((:dword :qword)
1611 (unless signed-p
1612 (setf dst-size :dword))
1613 (ecase src-size
1614 (:byte
1615 (emitter dst-size #b00001111 opcode))
1616 (:word
1617 (emitter dst-size #b00001111 (logior opcode 1)))
1618 (:dword
1619 (aver (or (not signed-p) (eq dst-size :qword)))
1620 (emitter dst-size
1621 (if signed-p #x63 #x8b))))))))) ; movsxd or straight mov
1623 ;; MOV[SZ]X - #x66 or REX selects the destination REG size, wherein :byte isn't
1624 ;; a possibility. The 'width' bit selects a source r/m size of :byte or :word.
1625 (define-instruction-format
1626 (move-with-extension 24 :include ext-reg-reg/mem
1627 :default-printer
1628 '(:name :tab reg ", "
1629 (:cond ((width :constant 0) (:using #'print-sized-byte-reg/mem reg/mem))
1630 (t (:using #'print-sized-word-reg/mem reg/mem)))))
1631 (width :prefilter nil)) ; doesn't affect DSTATE
1633 (define-instruction movsx (segment dst src)
1634 (:printer move-with-extension ((op #b1011111)))
1635 (:emitter (emit-move-with-extension segment dst src :signed)))
1637 (define-instruction movzx (segment dst src)
1638 (:printer move-with-extension ((op #b1011011)))
1639 (:emitter (emit-move-with-extension segment dst src nil)))
1641 ;;; The regular use of MOVSXD is with an operand size of :qword. This
1642 ;;; sign-extends the dword source into the qword destination register.
1643 ;;; If the operand size is :dword the instruction zero-extends the dword
1644 ;;; source into the qword destination register, i.e. it does the same as
1645 ;;; a dword MOV into a register.
1646 (define-instruction movsxd (segment dst src)
1647 (:printer reg-reg/mem ((op #b0110001) (width 1)
1648 (reg/mem nil :type 'sized-dword-reg/mem)))
1649 (:emitter (emit-move-with-extension segment dst src :signed)))
1651 ;;; this is not a real amd64 instruction, of course
1652 (define-instruction movzxd (segment dst src)
1653 ; (:printer reg-reg/mem ((op #x63) (reg nil :type 'reg)))
1654 (:emitter (emit-move-with-extension segment dst src nil)))
1656 (define-instruction push (segment src)
1657 ;; register
1658 (:printer reg-no-width-default-qword ((op #b01010)))
1659 ;; register/memory
1660 (:printer reg/mem-default-qword ((op '(#b11111111 #b110))))
1661 ;; immediate
1662 (:printer byte ((op #b01101010) (imm nil :type 'signed-imm-byte))
1663 '(:name :tab imm))
1664 (:printer byte ((op #b01101000)
1665 (imm nil :type 'signed-imm-data-default-qword))
1666 '(:name :tab imm))
1667 ;; ### segment registers?
1669 (:emitter
1670 (cond ((integerp src)
1671 (cond ((<= -128 src 127)
1672 (emit-byte segment #b01101010)
1673 (emit-byte segment src))
1675 ;; A REX-prefix is not needed because the operand size
1676 ;; defaults to 64 bits. The size of the immediate is 32
1677 ;; bits and it is sign-extended.
1678 (emit-byte segment #b01101000)
1679 (emit-signed-dword segment src))))
1681 (let ((size (operand-size src)))
1682 (aver (or (eq size :qword) (eq size :word)))
1683 (maybe-emit-operand-size-prefix segment size)
1684 (maybe-emit-rex-for-ea segment src nil :operand-size :do-not-set)
1685 (cond ((register-p src)
1686 (emit-byte+reg segment #x50 src))
1688 (emit-byte segment #b11111111)
1689 (emit-ea segment src #b110 :allow-constants t))))))))
1691 (define-instruction pop (segment dst)
1692 (:printer reg-no-width-default-qword ((op #b01011)))
1693 (:printer reg/mem-default-qword ((op '(#b10001111 #b000))))
1694 (:emitter
1695 (let ((size (operand-size dst)))
1696 (aver (or (eq size :qword) (eq size :word)))
1697 (maybe-emit-operand-size-prefix segment size)
1698 (maybe-emit-rex-for-ea segment dst nil :operand-size :do-not-set)
1699 (cond ((register-p dst)
1700 (emit-byte+reg segment #x58 dst))
1702 (emit-byte segment #b10001111)
1703 (emit-ea segment dst #b000))))))
1705 ;;; Compared to x86 we need to take two particularities into account
1706 ;;; here:
1707 ;;; * XCHG EAX, EAX can't be encoded as #x90 as the processor interprets
1708 ;;; that opcode as NOP while XCHG EAX, EAX is specified to clear the
1709 ;;; upper half of RAX. We need to use the long form #x87 #xC0 instead.
1710 ;;; * The opcode #x90 is not only used for NOP and XCHG RAX, RAX and
1711 ;;; XCHG AX, AX, but also for XCHG RAX, R8 (and the corresponding 32-
1712 ;;; and 16-bit versions). The printer for the NOP instruction (further
1713 ;;; below) matches all these encodings so needs to be overridden here
1714 ;;; for the cases that need to print as XCHG.
1715 ;;; Assembler and disassembler chained then map these special cases as
1716 ;;; follows:
1717 ;;; (INST NOP) -> 90 -> NOP
1718 ;;; (INST XCHG RAX-TN RAX-TN) -> 4890 -> NOP
1719 ;;; (INST XCHG EAX-TN EAX-TN) -> 87C0 -> XCHG EAX, EAX
1720 ;;; (INST XCHG AX-TN AX-TN) -> 6690 -> NOP
1721 ;;; (INST XCHG RAX-TN R8-TN) -> 4990 -> XCHG RAX, R8
1722 ;;; (INST XCHG EAX-TN R8D-TN) -> 4190 -> XCHG EAX, R8D
1723 ;;; (INST XCHG AX-TN R8W-TN) -> 664190 -> XCHG AX, R8W
1724 ;;; The disassembler additionally correctly matches encoding variants
1725 ;;; that the assembler doesn't generate, for example 4E90 prints as NOP
1726 ;;; and 4F90 as XCHG RAX, R8 (both because REX.R and REX.X are ignored).
1727 (define-instruction xchg (segment operand1 operand2)
1728 ;; This printer matches all patterns that encode exchanging RAX with
1729 ;; R8, EAX with R8D, or AX with R8W. These consist of the opcode #x90
1730 ;; with a REX prefix with REX.B = 1, and possibly the #x66 prefix.
1731 ;; We rely on the prefix automatism for the #x66 prefix, but
1732 ;; explicitly match the REX prefix as we need to provide a value for
1733 ;; REX.B, and to override the NOP printer by virtue of a longer match.
1734 (:printer rex-accum-reg ((rex-b 1) (op #b10010) (reg #b000)))
1735 ;; Register with accumulator.
1736 (:printer reg-no-width ((op #b10010)) '(:name :tab accum ", " reg))
1737 ;; Register/Memory with Register.
1738 (:printer reg-reg/mem ((op #b1000011)))
1739 (:emitter
1740 (let ((size (matching-operand-size operand1 operand2)))
1741 (maybe-emit-operand-size-prefix segment size)
1742 (labels ((xchg-acc-with-something (acc something)
1743 (if (and (not (eq size :byte))
1744 (register-p something)
1745 ;; Don't use the short encoding for XCHG EAX, EAX:
1746 (not (and (= (tn-offset something) sb!vm::eax-offset)
1747 (eq size :dword))))
1748 (progn
1749 (maybe-emit-rex-for-ea segment something acc)
1750 (emit-byte+reg segment #x90 something))
1751 (xchg-reg-with-something acc something)))
1752 (xchg-reg-with-something (reg something)
1753 (maybe-emit-rex-for-ea segment something reg)
1754 (emit-byte segment (if (eq size :byte) #b10000110 #b10000111))
1755 (emit-ea segment something (reg-tn-encoding reg))))
1756 (cond ((accumulator-p operand1)
1757 (xchg-acc-with-something operand1 operand2))
1758 ((accumulator-p operand2)
1759 (xchg-acc-with-something operand2 operand1))
1760 ((register-p operand1)
1761 (xchg-reg-with-something operand1 operand2))
1762 ((register-p operand2)
1763 (xchg-reg-with-something operand2 operand1))
1765 (error "bogus args to XCHG: ~S ~S" operand1 operand2)))))))
1767 (define-instruction lea (segment dst src)
1768 (:printer
1769 reg-reg/mem
1770 ((op #b1000110) (width 1)
1771 (reg/mem nil :use-label #'lea-compute-label :printer #'lea-print-ea)))
1772 (:emitter
1773 (aver (or (dword-reg-p dst) (qword-reg-p dst)))
1774 (maybe-emit-rex-for-ea segment src dst
1775 :operand-size (if (dword-reg-p dst) :dword :qword))
1776 (emit-byte segment #b10001101)
1777 (emit-ea segment src (reg-tn-encoding dst))))
1779 (define-instruction cmpxchg (segment dst src &optional prefix)
1780 ;; Register/Memory with Register.
1781 (:printer ext-reg-reg/mem ((op #b1011000)) '(:name :tab reg/mem ", " reg))
1782 (:emitter
1783 (aver (register-p src))
1784 (emit-prefix segment prefix)
1785 (let ((size (matching-operand-size src dst)))
1786 (maybe-emit-operand-size-prefix segment size)
1787 (maybe-emit-rex-for-ea segment dst src)
1788 (emit-byte segment #b00001111)
1789 (emit-byte segment (if (eq size :byte) #b10110000 #b10110001))
1790 (emit-ea segment dst (reg-tn-encoding src)))))
1792 (define-instruction cmpxchg16b (segment mem &optional prefix)
1793 (:printer ext-reg/mem-no-width
1794 ((op '(#xC7 1))))
1795 (:emitter
1796 (aver (not (register-p mem)))
1797 (emit-prefix segment prefix)
1798 (maybe-emit-rex-for-ea segment mem nil :operand-size :qword)
1799 (emit-byte segment #x0F)
1800 (emit-byte segment #xC7)
1801 (emit-ea segment mem 1))) ; operand extension
1803 (define-instruction rdrand (segment dst)
1804 (:printer ext-reg/mem-no-width
1805 ((op '(#xC7 6))))
1806 (:emitter
1807 (aver (register-p dst))
1808 (maybe-emit-operand-size-prefix segment (operand-size dst))
1809 (maybe-emit-rex-for-ea segment dst nil)
1810 (emit-byte segment #x0F)
1811 (emit-byte segment #xC7)
1812 (emit-ea segment dst 6)))
1814 ;;;; flag control instructions
1816 ;;; CLC -- Clear Carry Flag.
1817 (define-instruction clc (segment)
1818 (:printer byte ((op #b11111000)))
1819 (:emitter
1820 (emit-byte segment #b11111000)))
1822 ;;; CLD -- Clear Direction Flag.
1823 (define-instruction cld (segment)
1824 (:printer byte ((op #b11111100)))
1825 (:emitter
1826 (emit-byte segment #b11111100)))
1828 ;;; CLI -- Clear Iterrupt Enable Flag.
1829 (define-instruction cli (segment)
1830 (:printer byte ((op #b11111010)))
1831 (:emitter
1832 (emit-byte segment #b11111010)))
1834 ;;; CMC -- Complement Carry Flag.
1835 (define-instruction cmc (segment)
1836 (:printer byte ((op #b11110101)))
1837 (:emitter
1838 (emit-byte segment #b11110101)))
1840 ;;; LAHF -- Load AH into flags.
1841 (define-instruction lahf (segment)
1842 (:printer byte ((op #b10011111)))
1843 (:emitter
1844 (emit-byte segment #b10011111)))
1846 ;;; POPF -- Pop flags.
1847 (define-instruction popf (segment)
1848 (:printer byte ((op #b10011101)))
1849 (:emitter
1850 (emit-byte segment #b10011101)))
1852 ;;; PUSHF -- push flags.
1853 (define-instruction pushf (segment)
1854 (:printer byte ((op #b10011100)))
1855 (:emitter
1856 (emit-byte segment #b10011100)))
1858 ;;; SAHF -- Store AH into flags.
1859 (define-instruction sahf (segment)
1860 (:printer byte ((op #b10011110)))
1861 (:emitter
1862 (emit-byte segment #b10011110)))
1864 ;;; STC -- Set Carry Flag.
1865 (define-instruction stc (segment)
1866 (:printer byte ((op #b11111001)))
1867 (:emitter
1868 (emit-byte segment #b11111001)))
1870 ;;; STD -- Set Direction Flag.
1871 (define-instruction std (segment)
1872 (:printer byte ((op #b11111101)))
1873 (:emitter
1874 (emit-byte segment #b11111101)))
1876 ;;; STI -- Set Interrupt Enable Flag.
1877 (define-instruction sti (segment)
1878 (:printer byte ((op #b11111011)))
1879 (:emitter
1880 (emit-byte segment #b11111011)))
1882 ;;;; arithmetic
1884 (defun emit-random-arith-inst (name segment dst src opcode
1885 &optional allow-constants)
1886 (let ((size (matching-operand-size dst src)))
1887 (maybe-emit-operand-size-prefix segment size)
1888 (cond
1889 ((and (neq size :byte) (typep src '(signed-byte 8)))
1890 (maybe-emit-rex-for-ea segment dst nil)
1891 (emit-byte segment #b10000011)
1892 (emit-ea segment dst opcode :allow-constants allow-constants)
1893 (emit-byte segment src))
1894 ((or (integerp src)
1895 (and (fixup-p src)
1896 (memq (fixup-flavor src) '(:layout :immobile-object))))
1897 (maybe-emit-rex-for-ea segment dst nil)
1898 (cond ((accumulator-p dst)
1899 (emit-byte segment
1900 (dpb opcode
1901 (byte 3 3)
1902 (if (eq size :byte) #b00000100 #b00000101))))
1904 (emit-byte segment (if (eq size :byte) #b10000000 #b10000001))
1905 (emit-ea segment dst opcode :allow-constants allow-constants)))
1906 (if (fixup-p src)
1907 (emit-absolute-fixup segment src)
1908 (emit-sized-immediate segment size src)))
1909 ((register-p src)
1910 (maybe-emit-rex-for-ea segment dst src)
1911 (emit-byte segment
1912 (dpb opcode
1913 (byte 3 3)
1914 (if (eq size :byte) #b00000000 #b00000001)))
1915 (emit-ea segment dst (reg-tn-encoding src)
1916 :allow-constants allow-constants))
1917 ((register-p dst)
1918 (maybe-emit-rex-for-ea segment src dst)
1919 (emit-byte segment
1920 (dpb opcode
1921 (byte 3 3)
1922 (if (eq size :byte) #b00000010 #b00000011)))
1923 (emit-ea segment src (reg-tn-encoding dst)
1924 :allow-constants allow-constants))
1926 (error "bogus operands to ~A" name)))))
1928 (macrolet ((define (name subop &optional allow-constants)
1929 `(define-instruction ,name (segment dst src &optional prefix)
1930 (:printer accum-imm ((op ,(dpb subop (byte 3 2) #b0000010))))
1931 (:printer reg/mem-imm ((op '(#b1000000 ,subop))))
1932 ;; The redundant encoding #x82 is invalid in 64-bit mode,
1933 ;; therefore we force WIDTH to 1.
1934 (:printer reg/mem-imm ((op '(#b1000001 ,subop)) (width 1)
1935 (imm nil :type 'signed-imm-byte)))
1936 (:printer reg-reg/mem-dir ((op ,(dpb subop (byte 3 1) #b000000))))
1937 (:emitter
1938 (emit-prefix segment prefix)
1939 (emit-random-arith-inst ,(string name) segment dst src ,subop
1940 ,allow-constants)))))
1941 (define add #b000)
1942 (define adc #b010)
1943 (define sub #b101)
1944 (define sbb #b011)
1945 (define cmp #b111 t)
1946 (define and #b100)
1947 (define or #b001)
1948 (define xor #b110))
1950 ;;; The one-byte encodings for INC and DEC are used as REX prefixes
1951 ;;; in 64-bit mode so we always use the two-byte form.
1952 (define-instruction inc (segment dst &optional prefix)
1953 (:printer reg/mem ((op '(#b1111111 #b000))))
1954 (:emitter
1955 (emit-prefix segment prefix)
1956 (let ((size (operand-size dst)))
1957 (maybe-emit-operand-size-prefix segment size)
1958 (maybe-emit-rex-for-ea segment dst nil)
1959 (emit-byte segment (if (eq size :byte) #b11111110 #b11111111))
1960 (emit-ea segment dst #b000))))
1962 (define-instruction dec (segment dst &optional prefix)
1963 (:printer reg/mem ((op '(#b1111111 #b001))))
1964 (:emitter
1965 (emit-prefix segment prefix)
1966 (let ((size (operand-size dst)))
1967 (maybe-emit-operand-size-prefix segment size)
1968 (maybe-emit-rex-for-ea segment dst nil)
1969 (emit-byte segment (if (eq size :byte) #b11111110 #b11111111))
1970 (emit-ea segment dst #b001))))
1972 (define-instruction neg (segment dst)
1973 (:printer reg/mem ((op '(#b1111011 #b011))))
1974 (:emitter
1975 (let ((size (operand-size dst)))
1976 (maybe-emit-operand-size-prefix segment size)
1977 (maybe-emit-rex-for-ea segment dst nil)
1978 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
1979 (emit-ea segment dst #b011))))
1981 (define-instruction mul (segment dst src)
1982 (:printer accum-reg/mem ((op '(#b1111011 #b100))))
1983 (:emitter
1984 (let ((size (matching-operand-size dst src)))
1985 (aver (accumulator-p dst))
1986 (maybe-emit-operand-size-prefix segment size)
1987 (maybe-emit-rex-for-ea segment src nil)
1988 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
1989 (emit-ea segment src #b100))))
1991 (define-instruction imul (segment dst &optional src1 src2)
1992 (:printer accum-reg/mem ((op '(#b1111011 #b101))))
1993 (:printer ext-reg-reg/mem-no-width ((op #b10101111)))
1994 ;; These next two are like a single format where one bit in the opcode byte
1995 ;; determines the size of the immediate datum. A REG-REG/MEM-IMM format
1996 ;; would save one entry in the decoding table, since that bit would become
1997 ;; "don't care" from a decoding perspective, but we don't have (many) other
1998 ;; 3-operand opcodes in the general purpose (non-SSE) opcode space.
1999 (:printer reg-reg/mem ((op #b0110100) (width 1)
2000 (imm nil :type 'signed-imm-data))
2001 '(:name :tab reg ", " reg/mem ", " imm))
2002 (:printer reg-reg/mem ((op #b0110101) (width 1)
2003 (imm nil :type 'signed-imm-byte))
2004 '(:name :tab reg ", " reg/mem ", " imm))
2005 (:emitter
2006 (flet ((r/m-with-immed-to-reg (reg r/m immed)
2007 (let* ((size (matching-operand-size reg r/m))
2008 (sx (and (not (eq size :byte)) (<= -128 immed 127))))
2009 (maybe-emit-operand-size-prefix segment size)
2010 (maybe-emit-rex-for-ea segment r/m reg)
2011 (emit-byte segment (if sx #b01101011 #b01101001))
2012 (emit-ea segment r/m (reg-tn-encoding reg))
2013 (if sx
2014 (emit-byte segment immed)
2015 (emit-sized-immediate segment size immed)))))
2016 (cond (src2
2017 (r/m-with-immed-to-reg dst src1 src2))
2018 (src1
2019 (if (integerp src1)
2020 (r/m-with-immed-to-reg dst dst src1)
2021 (let ((size (matching-operand-size dst src1)))
2022 (maybe-emit-operand-size-prefix segment size)
2023 (maybe-emit-rex-for-ea segment src1 dst)
2024 (emit-byte segment #b00001111)
2025 (emit-byte segment #b10101111)
2026 (emit-ea segment src1 (reg-tn-encoding dst)))))
2028 (let ((size (operand-size dst)))
2029 (maybe-emit-operand-size-prefix segment size)
2030 (maybe-emit-rex-for-ea segment dst nil)
2031 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2032 (emit-ea segment dst #b101)))))))
2034 (define-instruction div (segment dst src)
2035 (:printer accum-reg/mem ((op '(#b1111011 #b110))))
2036 (:emitter
2037 (let ((size (matching-operand-size dst src)))
2038 (aver (accumulator-p dst))
2039 (maybe-emit-operand-size-prefix segment size)
2040 (maybe-emit-rex-for-ea segment src nil)
2041 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2042 (emit-ea segment src #b110))))
2044 (define-instruction idiv (segment dst src)
2045 (:printer accum-reg/mem ((op '(#b1111011 #b111))))
2046 (:emitter
2047 (let ((size (matching-operand-size dst src)))
2048 (aver (accumulator-p dst))
2049 (maybe-emit-operand-size-prefix segment size)
2050 (maybe-emit-rex-for-ea segment src nil)
2051 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2052 (emit-ea segment src #b111))))
2054 (define-instruction bswap (segment dst)
2055 (:printer ext-reg-no-width ((op #b11001)))
2056 (:emitter
2057 (let ((size (operand-size dst)))
2058 (maybe-emit-rex-prefix segment size nil nil dst)
2059 (emit-byte segment #x0f)
2060 (emit-byte+reg segment #xC8 dst))))
2062 ;;; CBW -- Convert Byte to Word. AX <- sign_xtnd(AL)
2063 (define-instruction cbw (segment)
2064 (:printer x66-byte ((op #b10011000)))
2065 (:emitter
2066 (maybe-emit-operand-size-prefix segment :word)
2067 (emit-byte segment #b10011000)))
2069 ;;; CWDE -- Convert Word To Double Word Extended. EAX <- sign_xtnd(AX)
2070 (define-instruction cwde (segment)
2071 (:printer byte ((op #b10011000)))
2072 (:emitter
2073 (maybe-emit-operand-size-prefix segment :dword)
2074 (emit-byte segment #b10011000)))
2076 ;;; CDQE -- Convert Double Word To Quad Word Extended. RAX <- sign_xtnd(EAX)
2077 (define-instruction cdqe (segment)
2078 (:printer rex-byte ((op #b10011000)))
2079 (:emitter
2080 (maybe-emit-rex-prefix segment :qword nil nil nil)
2081 (emit-byte segment #b10011000)))
2083 ;;; CWD -- Convert Word to Double Word. DX:AX <- sign_xtnd(AX)
2084 (define-instruction cwd (segment)
2085 (:printer x66-byte ((op #b10011001)))
2086 (:emitter
2087 (maybe-emit-operand-size-prefix segment :word)
2088 (emit-byte segment #b10011001)))
2090 ;;; CDQ -- Convert Double Word to Quad Word. EDX:EAX <- sign_xtnd(EAX)
2091 (define-instruction cdq (segment)
2092 (:printer byte ((op #b10011001)))
2093 (:emitter
2094 (maybe-emit-operand-size-prefix segment :dword)
2095 (emit-byte segment #b10011001)))
2097 ;;; CQO -- Convert Quad Word to Octaword. RDX:RAX <- sign_xtnd(RAX)
2098 (define-instruction cqo (segment)
2099 (:printer rex-byte ((op #b10011001)))
2100 (:emitter
2101 (maybe-emit-rex-prefix segment :qword nil nil nil)
2102 (emit-byte segment #b10011001)))
2104 (define-instruction xadd (segment dst src &optional prefix)
2105 ;; Register/Memory with Register.
2106 (:printer ext-reg-reg/mem ((op #b1100000)) '(:name :tab reg/mem ", " reg))
2107 (:emitter
2108 (aver (register-p src))
2109 (emit-prefix segment prefix)
2110 (let ((size (matching-operand-size src dst)))
2111 (maybe-emit-operand-size-prefix segment size)
2112 (maybe-emit-rex-for-ea segment dst src)
2113 (emit-byte segment #b00001111)
2114 (emit-byte segment (if (eq size :byte) #b11000000 #b11000001))
2115 (emit-ea segment dst (reg-tn-encoding src)))))
2118 ;;;; logic
2120 (defun emit-shift-inst (segment dst amount opcode)
2121 (let ((size (operand-size dst)))
2122 (maybe-emit-operand-size-prefix segment size)
2123 (multiple-value-bind (major-opcode immed)
2124 (case amount
2125 (:cl (values #b11010010 nil))
2126 (1 (values #b11010000 nil))
2127 (t (values #b11000000 t)))
2128 (maybe-emit-rex-for-ea segment dst nil)
2129 (emit-byte segment
2130 (if (eq size :byte) major-opcode (logior major-opcode 1)))
2131 (emit-ea segment dst opcode)
2132 (when immed
2133 (emit-byte segment amount)))))
2135 (define-instruction-format
2136 (shift-inst 16 :include reg/mem
2137 :default-printer '(:name :tab reg/mem ", " (:if (varying :positive) 'cl 1)))
2138 (op :fields (list (byte 6 2) (byte 3 11)))
2139 (varying :field (byte 1 1)))
2141 (macrolet ((define (name subop)
2142 `(define-instruction ,name (segment dst amount)
2143 (:printer shift-inst ((op '(#b110100 ,subop)))) ; shift by CL or 1
2144 (:printer reg/mem-imm ((op '(#b1100000 ,subop))
2145 (imm nil :type 'imm-byte)))
2146 (:emitter (emit-shift-inst segment dst amount ,subop)))))
2147 (define rol #b000)
2148 (define ror #b001)
2149 (define rcl #b010)
2150 (define rcr #b011)
2151 (define shl #b100)
2152 (define shr #b101)
2153 (define sar #b111))
2155 (defun emit-double-shift (segment opcode dst src amt)
2156 (let ((size (matching-operand-size dst src)))
2157 (when (eq size :byte)
2158 (error "Double shifts can only be used with words."))
2159 (maybe-emit-operand-size-prefix segment size)
2160 (maybe-emit-rex-for-ea segment dst src)
2161 (emit-byte segment #b00001111)
2162 (emit-byte segment (dpb opcode (byte 1 3)
2163 (if (eq amt :cl) #b10100101 #b10100100)))
2164 (emit-ea segment dst (reg-tn-encoding src))
2165 (unless (eq amt :cl)
2166 (emit-byte segment amt))))
2168 (macrolet ((define (name direction-bit op)
2169 `(define-instruction ,name (segment dst src amt)
2170 (:declare (type (or (member :cl) (mod 32)) amt))
2171 (:printer ext-reg-reg/mem-no-width ((op ,(logior op #b100))
2172 (imm nil :type 'imm-byte))
2173 '(:name :tab reg/mem ", " reg ", " imm))
2174 (:printer ext-reg-reg/mem-no-width ((op ,(logior op #b101)))
2175 '(:name :tab reg/mem ", " reg ", " 'cl))
2176 (:emitter
2177 (emit-double-shift segment ,direction-bit dst src amt)))))
2178 (define shld 0 #b10100000)
2179 (define shrd 1 #b10101000))
2181 (define-instruction test (segment this that)
2182 (:printer accum-imm ((op #b1010100)))
2183 (:printer reg/mem-imm ((op '(#b1111011 #b000))))
2184 (:printer reg-reg/mem ((op #b1000010)))
2185 (:emitter
2186 (let ((size (matching-operand-size this that)))
2187 (maybe-emit-operand-size-prefix segment size)
2188 (flet ((test-immed-and-something (immed something)
2189 (cond ((accumulator-p something)
2190 (maybe-emit-rex-for-ea segment something nil)
2191 (emit-byte segment
2192 (if (eq size :byte) #b10101000 #b10101001))
2193 (emit-sized-immediate segment size immed))
2195 (maybe-emit-rex-for-ea segment something nil)
2196 (emit-byte segment
2197 (if (eq size :byte) #b11110110 #b11110111))
2198 (emit-ea segment something #b000)
2199 (emit-sized-immediate segment size immed))))
2200 (test-reg-and-something (reg something)
2201 (maybe-emit-rex-for-ea segment something reg)
2202 (emit-byte segment (if (eq size :byte) #b10000100 #b10000101))
2203 (emit-ea segment something (reg-tn-encoding reg))))
2204 (cond ((integerp that)
2205 (test-immed-and-something that this))
2206 ((integerp this)
2207 (test-immed-and-something this that))
2208 ((register-p this)
2209 (test-reg-and-something this that))
2210 ((register-p that)
2211 (test-reg-and-something that this))
2213 (error "bogus operands for TEST: ~S and ~S" this that)))))))
2215 (define-instruction not (segment dst)
2216 (:printer reg/mem ((op '(#b1111011 #b010))))
2217 (:emitter
2218 (let ((size (operand-size dst)))
2219 (maybe-emit-operand-size-prefix segment size)
2220 (maybe-emit-rex-for-ea segment dst nil)
2221 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
2222 (emit-ea segment dst #b010))))
2224 ;;;; string manipulation
2226 (define-instruction cmps (segment size)
2227 (:printer string-op ((op #b1010011)))
2228 (:emitter
2229 (maybe-emit-operand-size-prefix segment size)
2230 (maybe-emit-rex-prefix segment size nil nil nil)
2231 (emit-byte segment (if (eq size :byte) #b10100110 #b10100111))))
2233 (define-instruction ins (segment acc)
2234 (:printer string-op ((op #b0110110)))
2235 (:emitter
2236 (let ((size (operand-size acc)))
2237 (aver (accumulator-p acc))
2238 (maybe-emit-operand-size-prefix segment size)
2239 (maybe-emit-rex-prefix segment size nil nil nil)
2240 (emit-byte segment (if (eq size :byte) #b01101100 #b01101101)))))
2242 (define-instruction lods (segment acc)
2243 (:printer string-op ((op #b1010110)))
2244 (:emitter
2245 (let ((size (operand-size acc)))
2246 (aver (accumulator-p acc))
2247 (maybe-emit-operand-size-prefix segment size)
2248 (maybe-emit-rex-prefix segment size nil nil nil)
2249 (emit-byte segment (if (eq size :byte) #b10101100 #b10101101)))))
2251 (define-instruction movs (segment size)
2252 (:printer string-op ((op #b1010010)))
2253 (:emitter
2254 (maybe-emit-operand-size-prefix segment size)
2255 (maybe-emit-rex-prefix segment size nil nil nil)
2256 (emit-byte segment (if (eq size :byte) #b10100100 #b10100101))))
2258 (define-instruction outs (segment acc)
2259 (:printer string-op ((op #b0110111)))
2260 (:emitter
2261 (let ((size (operand-size acc)))
2262 (aver (accumulator-p acc))
2263 (maybe-emit-operand-size-prefix segment size)
2264 (maybe-emit-rex-prefix segment size nil nil nil)
2265 (emit-byte segment (if (eq size :byte) #b01101110 #b01101111)))))
2267 (define-instruction scas (segment acc)
2268 (:printer string-op ((op #b1010111)))
2269 (:emitter
2270 (let ((size (operand-size acc)))
2271 (aver (accumulator-p acc))
2272 (maybe-emit-operand-size-prefix segment size)
2273 (maybe-emit-rex-prefix segment size nil nil nil)
2274 (emit-byte segment (if (eq size :byte) #b10101110 #b10101111)))))
2276 (define-instruction stos (segment acc)
2277 (:printer string-op ((op #b1010101)))
2278 (:emitter
2279 (let ((size (operand-size acc)))
2280 (aver (accumulator-p acc))
2281 (maybe-emit-operand-size-prefix segment size)
2282 (maybe-emit-rex-prefix segment size nil nil nil)
2283 (emit-byte segment (if (eq size :byte) #b10101010 #b10101011)))))
2285 (define-instruction xlat (segment)
2286 (:printer byte ((op #b11010111)))
2287 (:emitter
2288 (emit-byte segment #b11010111)))
2291 ;;;; bit manipulation
2293 (define-instruction bsf (segment dst src)
2294 (:printer ext-reg-reg/mem-no-width ((op #b10111100)))
2295 (:emitter
2296 (let ((size (matching-operand-size dst src)))
2297 (when (eq size :byte)
2298 (error "can't scan bytes: ~S" src))
2299 (maybe-emit-operand-size-prefix segment size)
2300 (maybe-emit-rex-for-ea segment src dst)
2301 (emit-byte segment #b00001111)
2302 (emit-byte segment #b10111100)
2303 (emit-ea segment src (reg-tn-encoding dst)))))
2305 (define-instruction bsr (segment dst src)
2306 (:printer ext-reg-reg/mem-no-width ((op #b10111101)))
2307 (:emitter
2308 (let ((size (matching-operand-size dst src)))
2309 (when (eq size :byte)
2310 (error "can't scan bytes: ~S" src))
2311 (maybe-emit-operand-size-prefix segment size)
2312 (maybe-emit-rex-for-ea segment src dst)
2313 (emit-byte segment #b00001111)
2314 (emit-byte segment #b10111101)
2315 (emit-ea segment src (reg-tn-encoding dst)))))
2317 (defun emit-bit-test-and-mumble (segment src index opcode)
2318 (let ((size (operand-size src)))
2319 (when (eq size :byte)
2320 (error "can't scan bytes: ~S" src))
2321 (maybe-emit-operand-size-prefix segment size)
2322 (cond ((integerp index)
2323 (maybe-emit-rex-for-ea segment src nil)
2324 (emit-byte segment #b00001111)
2325 (emit-byte segment #b10111010)
2326 (emit-ea segment src opcode)
2327 (emit-byte segment index))
2329 (maybe-emit-rex-for-ea segment src index)
2330 (emit-byte segment #b00001111)
2331 (emit-byte segment (dpb opcode (byte 3 3) #b10000011))
2332 (emit-ea segment src (reg-tn-encoding index))))))
2334 (macrolet ((define (inst opcode-extension)
2335 `(define-instruction ,inst (segment src index &optional prefix)
2336 (:printer ext-reg/mem-no-width+imm8
2337 ((op '(#xBA ,opcode-extension))
2338 (reg/mem nil :type 'sized-reg/mem)))
2339 (:printer ext-reg-reg/mem-no-width
2340 ((op ,(dpb opcode-extension (byte 3 3) #b10000011))
2341 (reg/mem nil :type 'sized-reg/mem))
2342 '(:name :tab reg/mem ", " reg))
2343 (:emitter
2344 (emit-prefix segment prefix)
2345 (emit-bit-test-and-mumble segment src index
2346 ,opcode-extension)))))
2347 (define bt 4)
2348 (define bts 5)
2349 (define btr 6)
2350 (define btc 7))
2353 ;;;; control transfer
2355 (define-instruction call (segment where)
2356 (:printer near-jump ((op #xE8)))
2357 (:printer reg/mem-default-qword ((op '(#b11111111 #b010))))
2358 (:emitter
2359 (typecase where
2360 (label
2361 (emit-byte segment #b11101000) ; 32 bit relative
2362 (emit-dword-displacement-backpatch segment where))
2363 (fixup
2364 (emit-byte segment #b11101000)
2365 (emit-relative-fixup segment where))
2367 (maybe-emit-rex-for-ea segment where nil :operand-size :do-not-set)
2368 (emit-byte segment #b11111111)
2369 (emit-ea segment where #b010)))))
2371 (define-instruction jmp (segment cond &optional where)
2372 ;; conditional jumps
2373 (:printer short-cond-jump ((op #b0111)) '('j cc :tab label))
2374 (:printer near-cond-jump () '('j cc :tab label))
2375 ;; unconditional jumps
2376 (:printer short-jump ((op #b1011)))
2377 (:printer near-jump ((op #xE9)))
2378 (:printer reg/mem-default-qword ((op '(#b11111111 #b100))))
2379 (:emitter
2380 (cond (where
2381 (emit-chooser
2382 segment 6 2
2383 (lambda (segment posn delta-if-after)
2384 (let ((disp (- (label-position where posn delta-if-after)
2385 (+ posn 2))))
2386 (when (<= -128 disp 127)
2387 (emit-byte segment
2388 (dpb (conditional-opcode cond)
2389 (byte 4 0)
2390 #b01110000))
2391 (emit-byte-displacement-backpatch segment where)
2392 t)))
2393 (lambda (segment posn)
2394 (let ((disp (- (label-position where) (+ posn 6))))
2395 (emit-byte segment #b00001111)
2396 (emit-byte segment
2397 (dpb (conditional-opcode cond)
2398 (byte 4 0)
2399 #b10000000))
2400 (emit-signed-dword segment disp)))))
2401 ((label-p (setq where cond))
2402 (emit-chooser
2403 segment 5 0
2404 (lambda (segment posn delta-if-after)
2405 (let ((disp (- (label-position where posn delta-if-after)
2406 (+ posn 2))))
2407 (when (<= -128 disp 127)
2408 (emit-byte segment #b11101011)
2409 (emit-byte-displacement-backpatch segment where)
2410 t)))
2411 (lambda (segment posn)
2412 (let ((disp (- (label-position where) (+ posn 5))))
2413 (emit-byte segment #b11101001)
2414 (emit-signed-dword segment disp)))))
2415 ((fixup-p where)
2416 (emit-byte segment #b11101001)
2417 (emit-relative-fixup segment where))
2419 (unless (or (ea-p where) (tn-p where))
2420 (error "don't know what to do with ~A" where))
2421 ;; near jump defaults to 64 bit
2422 ;; w-bit in rex prefix is unnecessary
2423 (maybe-emit-rex-for-ea segment where nil :operand-size :do-not-set)
2424 (emit-byte segment #b11111111)
2425 (emit-ea segment where #b100)))))
2427 (define-instruction ret (segment &optional stack-delta)
2428 (:printer byte ((op #b11000011)))
2429 (:printer byte ((op #b11000010) (imm nil :type 'imm-word-16))
2430 '(:name :tab imm))
2431 (:emitter
2432 (cond ((and stack-delta (not (zerop stack-delta)))
2433 (emit-byte segment #b11000010)
2434 (emit-word segment stack-delta))
2436 (emit-byte segment #b11000011)))))
2438 (define-instruction jrcxz (segment target)
2439 (:printer short-jump ((op #b0011)))
2440 (:emitter
2441 (emit-byte segment #b11100011)
2442 (emit-byte-displacement-backpatch segment target)))
2444 (define-instruction loop (segment target)
2445 (:printer short-jump ((op #b0010)))
2446 (:emitter
2447 (emit-byte segment #b11100010) ; pfw this was 11100011, or jecxz!!!!
2448 (emit-byte-displacement-backpatch segment target)))
2450 (define-instruction loopz (segment target)
2451 (:printer short-jump ((op #b0001)))
2452 (:emitter
2453 (emit-byte segment #b11100001)
2454 (emit-byte-displacement-backpatch segment target)))
2456 (define-instruction loopnz (segment target)
2457 (:printer short-jump ((op #b0000)))
2458 (:emitter
2459 (emit-byte segment #b11100000)
2460 (emit-byte-displacement-backpatch segment target)))
2462 ;;;; conditional move
2463 (define-instruction cmov (segment cond dst src)
2464 (:printer cond-move ())
2465 (:emitter
2466 (aver (register-p dst))
2467 (let ((size (matching-operand-size dst src)))
2468 (aver (or (eq size :word) (eq size :dword) (eq size :qword)))
2469 (maybe-emit-operand-size-prefix segment size))
2470 (maybe-emit-rex-for-ea segment src dst)
2471 (emit-byte segment #b00001111)
2472 (emit-byte segment (dpb (conditional-opcode cond) (byte 4 0) #b01000000))
2473 (emit-ea segment src (reg-tn-encoding dst) :allow-constants t)))
2475 ;;;; conditional byte set
2477 (define-instruction set (segment dst cond)
2478 (:printer cond-set ())
2479 (:emitter
2480 (maybe-emit-rex-for-ea segment dst nil :operand-size :byte)
2481 (emit-byte segment #b00001111)
2482 (emit-byte segment (dpb (conditional-opcode cond) (byte 4 0) #b10010000))
2483 (emit-ea segment dst #b000)))
2485 ;;;; enter/leave
2487 (define-instruction enter (segment disp &optional (level 0))
2488 (:declare (type (unsigned-byte 16) disp)
2489 (type (unsigned-byte 8) level))
2490 (:printer enter-format ((op #b11001000)))
2491 (:emitter
2492 (emit-byte segment #b11001000)
2493 (emit-word segment disp)
2494 (emit-byte segment level)))
2496 (define-instruction leave (segment)
2497 (:printer byte ((op #b11001001)))
2498 (:emitter
2499 (emit-byte segment #b11001001)))
2501 ;;;; interrupt instructions
2503 (define-instruction break (segment code)
2504 (:declare (type (unsigned-byte 8) code))
2505 #!-ud2-breakpoints (:printer byte-imm ((op (or #!+int4-breakpoints #xCE #xCC)))
2506 '(:name :tab code) :control #'break-control)
2507 #!+ud2-breakpoints (:printer word-imm ((op #b0000101100001111))
2508 '(:name :tab code) :control #'break-control)
2509 (:emitter
2510 #!-ud2-breakpoints (emit-byte segment (or #!+int4-breakpoints #xCE #xCC))
2511 ;; On darwin, trap handling via SIGTRAP is unreliable, therefore we
2512 ;; throw a sigill with 0x0b0f instead and check for this in the
2513 ;; SIGILL handler and pass it on to the sigtrap handler if
2514 ;; appropriate
2515 #!+ud2-breakpoints (emit-word segment #b0000101100001111)
2516 (emit-byte segment code)))
2518 (define-instruction int (segment number)
2519 (:declare (type (unsigned-byte 8) number))
2520 (:printer byte-imm ((op #b11001101)))
2521 (:emitter
2522 (etypecase number
2523 ((member 3 4)
2524 (emit-byte segment (if (eql number 4) #xCE #xCC)))
2525 ((unsigned-byte 8)
2526 (emit-byte segment #b11001101)
2527 (emit-byte segment number)))))
2529 (define-instruction iret (segment)
2530 (:printer byte ((op #b11001111)))
2531 (:emitter
2532 (emit-byte segment #b11001111)))
2534 ;;;; processor control
2536 (define-instruction hlt (segment)
2537 (:printer byte ((op #b11110100)))
2538 (:emitter
2539 (emit-byte segment #b11110100)))
2541 (define-instruction nop (segment)
2542 (:printer byte ((op #b10010000)))
2543 ;; multi-byte NOP
2544 (:printer ext-reg/mem-no-width ((op '(#x1f 0))) '(:name))
2545 (:emitter
2546 (emit-byte segment #b10010000)))
2548 ;;; Emit a sequence of single- or multi-byte NOPs to fill AMOUNT many
2549 ;;; bytes with the smallest possible number of such instructions.
2550 (defun emit-long-nop (segment amount)
2551 (declare (type sb!assem:segment segment)
2552 (type index amount))
2553 ;; Pack all instructions into one byte vector to save space.
2554 (let* ((bytes #.(!coerce-to-specialized
2555 #(#x90
2556 #x66 #x90
2557 #x0f #x1f #x00
2558 #x0f #x1f #x40 #x00
2559 #x0f #x1f #x44 #x00 #x00
2560 #x66 #x0f #x1f #x44 #x00 #x00
2561 #x0f #x1f #x80 #x00 #x00 #x00 #x00
2562 #x0f #x1f #x84 #x00 #x00 #x00 #x00 #x00
2563 #x66 #x0f #x1f #x84 #x00 #x00 #x00 #x00 #x00)
2564 '(unsigned-byte 8)))
2565 (max-length (isqrt (* 2 (length bytes)))))
2566 (loop
2567 (let* ((count (min amount max-length))
2568 (start (ash (* count (1- count)) -1)))
2569 (dotimes (i count)
2570 (emit-byte segment (aref bytes (+ start i)))))
2571 (if (> amount max-length)
2572 (decf amount max-length)
2573 (return)))))
2575 (define-instruction wait (segment)
2576 (:printer byte ((op #b10011011)))
2577 (:emitter
2578 (emit-byte segment #b10011011)))
2581 ;;;; miscellaneous hackery
2583 (define-instruction byte (segment byte)
2584 (:emitter
2585 (emit-byte segment byte)))
2587 (define-instruction word (segment word)
2588 (:emitter
2589 (emit-word segment word)))
2591 (define-instruction dword (segment dword)
2592 (:emitter
2593 (emit-dword segment dword)))
2595 (defun emit-header-data (segment type)
2596 (emit-back-patch segment
2597 n-word-bytes
2598 (lambda (segment posn)
2599 (emit-qword segment
2600 (logior type
2601 (ash (+ posn
2602 (component-header-length))
2603 (- n-widetag-bits
2604 word-shift)))))))
2606 (define-instruction simple-fun-header-word (segment)
2607 (:emitter
2608 (emit-header-data segment
2609 (logior simple-fun-widetag
2610 #!+(and compact-instance-header (host-feature sb-xc-host))
2611 (ash function-layout 32)))))
2614 ;;;; Instructions required to do floating point operations using SSE
2616 ;; Return a one- or two-element list of printers for SSE instructions.
2617 ;; The one-element list is used in the cases where the REX prefix is
2618 ;; really a prefix and thus automatically supported, the two-element
2619 ;; list is used when the REX prefix is used in an infix position.
2620 (eval-when (:compile-toplevel :execute)
2621 (defun sse-inst-printer-list (inst-format-stem prefix opcode
2622 &key more-fields printer)
2623 (let ((fields `(,@(when prefix
2624 `((prefix ,prefix)))
2625 (op ,opcode)
2626 ,@more-fields))
2627 (inst-formats (if prefix
2628 (list (symbolicate "EXT-" inst-format-stem)
2629 (symbolicate "EXT-REX-" inst-format-stem))
2630 (list inst-format-stem))))
2631 (mapcar (lambda (inst-format)
2632 `(:printer ,inst-format ,fields ,@(if printer `(',printer))))
2633 inst-formats)))
2634 (defun 2byte-sse-inst-printer-list (inst-format-stem prefix op1 op2
2635 &key more-fields printer)
2636 (let ((fields `(,@(when prefix
2637 `((prefix, prefix)))
2638 (op1 ,op1)
2639 (op2 ,op2)
2640 ,@more-fields))
2641 (inst-formats (if prefix
2642 (list (symbolicate "EXT-" inst-format-stem)
2643 (symbolicate "EXT-REX-" inst-format-stem))
2644 (list inst-format-stem))))
2645 (mapcar (lambda (inst-format)
2646 `(:printer ,inst-format ,fields ,@(if printer `(',printer))))
2647 inst-formats))))
2649 (defun emit-sse-inst (segment dst src prefix opcode
2650 &key operand-size (remaining-bytes 0))
2651 (when prefix
2652 (emit-byte segment prefix))
2653 (if operand-size
2654 (maybe-emit-rex-for-ea segment src dst :operand-size operand-size)
2655 (maybe-emit-rex-for-ea segment src dst))
2656 (emit-byte segment #x0f)
2657 (emit-byte segment opcode)
2658 (emit-ea segment src (reg-tn-encoding dst) :remaining-bytes remaining-bytes))
2660 ;; 0110 0110:0000 1111:0111 00gg: 11 010 xmmreg:imm8
2662 (defun emit-sse-inst-with-imm (segment dst/src imm
2663 prefix opcode /i
2664 &key operand-size)
2665 (aver (<= 0 /i 7))
2666 (when prefix
2667 (emit-byte segment prefix))
2668 ;; dst/src is encoded in the r/m field, not r; REX.B must be
2669 ;; set to use extended XMM registers
2670 (maybe-emit-rex-prefix segment operand-size nil nil dst/src)
2671 (emit-byte segment #x0F)
2672 (emit-byte segment opcode)
2673 (emit-byte segment (logior (ash (logior #b11000 /i) 3)
2674 (reg-tn-encoding dst/src)))
2675 (emit-byte segment imm))
2677 (defun emit-sse-inst-2byte (segment dst src prefix op1 op2
2678 &key operand-size (remaining-bytes 0))
2679 (when prefix
2680 (emit-byte segment prefix))
2681 (if operand-size
2682 (maybe-emit-rex-for-ea segment src dst :operand-size operand-size)
2683 (maybe-emit-rex-for-ea segment src dst))
2684 (emit-byte segment #x0f)
2685 (emit-byte segment op1)
2686 (emit-byte segment op2)
2687 (emit-ea segment src (reg-tn-encoding dst) :remaining-bytes remaining-bytes))
2689 (macrolet
2690 ((define-imm-sse-instruction (name opcode /i)
2691 `(define-instruction ,name (segment dst/src imm)
2692 ,@(sse-inst-printer-list 'xmm-imm #x66 opcode
2693 :more-fields `((/i ,/i)))
2694 (:emitter
2695 (emit-sse-inst-with-imm segment dst/src imm
2696 #x66 ,opcode ,/i
2697 :operand-size :do-not-set)))))
2698 (define-imm-sse-instruction pslldq #x73 7)
2699 (define-imm-sse-instruction psllw-imm #x71 6)
2700 (define-imm-sse-instruction pslld-imm #x72 6)
2701 (define-imm-sse-instruction psllq-imm #x73 6)
2703 (define-imm-sse-instruction psraw-imm #x71 4)
2704 (define-imm-sse-instruction psrad-imm #x72 4)
2706 (define-imm-sse-instruction psrldq #x73 3)
2707 (define-imm-sse-instruction psrlw-imm #x71 2)
2708 (define-imm-sse-instruction psrld-imm #x72 2)
2709 (define-imm-sse-instruction psrlq-imm #x73 2))
2711 ;;; Emit an SSE instruction that has an XMM register as the destination
2712 ;;; operand and for which the size of the operands is implicitly given
2713 ;;; by the instruction.
2714 (defun emit-regular-sse-inst (segment dst src prefix opcode
2715 &key (remaining-bytes 0))
2716 (aver (xmm-register-p dst))
2717 (emit-sse-inst segment dst src prefix opcode
2718 :operand-size :do-not-set
2719 :remaining-bytes remaining-bytes))
2721 (defun emit-regular-2byte-sse-inst (segment dst src prefix op1 op2
2722 &key (remaining-bytes 0))
2723 (aver (xmm-register-p dst))
2724 (emit-sse-inst-2byte segment dst src prefix op1 op2
2725 :operand-size :do-not-set
2726 :remaining-bytes remaining-bytes))
2728 ;;; Instructions having an XMM register as the destination operand
2729 ;;; and an XMM register or a memory location as the source operand.
2730 ;;; The operand size is implicitly given by the instruction.
2732 (macrolet ((define-regular-sse-inst (name prefix opcode)
2733 `(define-instruction ,name (segment dst src)
2734 ,@(sse-inst-printer-list 'xmm-xmm/mem prefix opcode)
2735 (:emitter
2736 (emit-regular-sse-inst segment dst src ,prefix ,opcode)))))
2737 ;; moves
2738 (define-regular-sse-inst movshdup #xf3 #x16)
2739 (define-regular-sse-inst movsldup #xf3 #x12)
2740 (define-regular-sse-inst movddup #xf2 #x12)
2741 ;; logical
2742 (define-regular-sse-inst andpd #x66 #x54)
2743 (define-regular-sse-inst andps nil #x54)
2744 (define-regular-sse-inst andnpd #x66 #x55)
2745 (define-regular-sse-inst andnps nil #x55)
2746 (define-regular-sse-inst orpd #x66 #x56)
2747 (define-regular-sse-inst orps nil #x56)
2748 (define-regular-sse-inst pand #x66 #xdb)
2749 (define-regular-sse-inst pandn #x66 #xdf)
2750 (define-regular-sse-inst por #x66 #xeb)
2751 (define-regular-sse-inst pxor #x66 #xef)
2752 (define-regular-sse-inst xorpd #x66 #x57)
2753 (define-regular-sse-inst xorps nil #x57)
2754 ;; comparison
2755 (define-regular-sse-inst comisd #x66 #x2f)
2756 (define-regular-sse-inst comiss nil #x2f)
2757 (define-regular-sse-inst ucomisd #x66 #x2e)
2758 (define-regular-sse-inst ucomiss nil #x2e)
2759 ;; integer comparison
2760 (define-regular-sse-inst pcmpeqb #x66 #x74)
2761 (define-regular-sse-inst pcmpeqw #x66 #x75)
2762 (define-regular-sse-inst pcmpeqd #x66 #x76)
2763 (define-regular-sse-inst pcmpgtb #x66 #x64)
2764 (define-regular-sse-inst pcmpgtw #x66 #x65)
2765 (define-regular-sse-inst pcmpgtd #x66 #x66)
2766 ;; max/min
2767 (define-regular-sse-inst maxpd #x66 #x5f)
2768 (define-regular-sse-inst maxps nil #x5f)
2769 (define-regular-sse-inst maxsd #xf2 #x5f)
2770 (define-regular-sse-inst maxss #xf3 #x5f)
2771 (define-regular-sse-inst minpd #x66 #x5d)
2772 (define-regular-sse-inst minps nil #x5d)
2773 (define-regular-sse-inst minsd #xf2 #x5d)
2774 (define-regular-sse-inst minss #xf3 #x5d)
2775 ;; integer max/min
2776 (define-regular-sse-inst pmaxsw #x66 #xee)
2777 (define-regular-sse-inst pmaxub #x66 #xde)
2778 (define-regular-sse-inst pminsw #x66 #xea)
2779 (define-regular-sse-inst pminub #x66 #xda)
2780 ;; arithmetic
2781 (define-regular-sse-inst addpd #x66 #x58)
2782 (define-regular-sse-inst addps nil #x58)
2783 (define-regular-sse-inst addsd #xf2 #x58)
2784 (define-regular-sse-inst addss #xf3 #x58)
2785 (define-regular-sse-inst addsubpd #x66 #xd0)
2786 (define-regular-sse-inst addsubps #xf2 #xd0)
2787 (define-regular-sse-inst divpd #x66 #x5e)
2788 (define-regular-sse-inst divps nil #x5e)
2789 (define-regular-sse-inst divsd #xf2 #x5e)
2790 (define-regular-sse-inst divss #xf3 #x5e)
2791 (define-regular-sse-inst haddpd #x66 #x7c)
2792 (define-regular-sse-inst haddps #xf2 #x7c)
2793 (define-regular-sse-inst hsubpd #x66 #x7d)
2794 (define-regular-sse-inst hsubps #xf2 #x7d)
2795 (define-regular-sse-inst mulpd #x66 #x59)
2796 (define-regular-sse-inst mulps nil #x59)
2797 (define-regular-sse-inst mulsd #xf2 #x59)
2798 (define-regular-sse-inst mulss #xf3 #x59)
2799 (define-regular-sse-inst rcpps nil #x53)
2800 (define-regular-sse-inst rcpss #xf3 #x53)
2801 (define-regular-sse-inst rsqrtps nil #x52)
2802 (define-regular-sse-inst rsqrtss #xf3 #x52)
2803 (define-regular-sse-inst sqrtpd #x66 #x51)
2804 (define-regular-sse-inst sqrtps nil #x51)
2805 (define-regular-sse-inst sqrtsd #xf2 #x51)
2806 (define-regular-sse-inst sqrtss #xf3 #x51)
2807 (define-regular-sse-inst subpd #x66 #x5c)
2808 (define-regular-sse-inst subps nil #x5c)
2809 (define-regular-sse-inst subsd #xf2 #x5c)
2810 (define-regular-sse-inst subss #xf3 #x5c)
2811 (define-regular-sse-inst unpckhpd #x66 #x15)
2812 (define-regular-sse-inst unpckhps nil #x15)
2813 (define-regular-sse-inst unpcklpd #x66 #x14)
2814 (define-regular-sse-inst unpcklps nil #x14)
2815 ;; integer arithmetic
2816 (define-regular-sse-inst paddb #x66 #xfc)
2817 (define-regular-sse-inst paddw #x66 #xfd)
2818 (define-regular-sse-inst paddd #x66 #xfe)
2819 (define-regular-sse-inst paddq #x66 #xd4)
2820 (define-regular-sse-inst paddsb #x66 #xec)
2821 (define-regular-sse-inst paddsw #x66 #xed)
2822 (define-regular-sse-inst paddusb #x66 #xdc)
2823 (define-regular-sse-inst paddusw #x66 #xdd)
2824 (define-regular-sse-inst pavgb #x66 #xe0)
2825 (define-regular-sse-inst pavgw #x66 #xe3)
2826 (define-regular-sse-inst pmaddwd #x66 #xf5)
2827 (define-regular-sse-inst pmulhuw #x66 #xe4)
2828 (define-regular-sse-inst pmulhw #x66 #xe5)
2829 (define-regular-sse-inst pmullw #x66 #xd5)
2830 (define-regular-sse-inst pmuludq #x66 #xf4)
2831 (define-regular-sse-inst psadbw #x66 #xf6)
2832 (define-regular-sse-inst psllw #x66 #xf1)
2833 (define-regular-sse-inst pslld #x66 #xf2)
2834 (define-regular-sse-inst psllq #x66 #xf3)
2835 (define-regular-sse-inst psraw #x66 #xe1)
2836 (define-regular-sse-inst psrad #x66 #xe2)
2837 (define-regular-sse-inst psrlw #x66 #xd1)
2838 (define-regular-sse-inst psrld #x66 #xd2)
2839 (define-regular-sse-inst psrlq #x66 #xd3)
2840 (define-regular-sse-inst psubb #x66 #xf8)
2841 (define-regular-sse-inst psubw #x66 #xf9)
2842 (define-regular-sse-inst psubd #x66 #xfa)
2843 (define-regular-sse-inst psubq #x66 #xfb)
2844 (define-regular-sse-inst psubsb #x66 #xe8)
2845 (define-regular-sse-inst psubsw #x66 #xe9)
2846 (define-regular-sse-inst psubusb #x66 #xd8)
2847 (define-regular-sse-inst psubusw #x66 #xd9)
2848 ;; conversion
2849 (define-regular-sse-inst cvtdq2pd #xf3 #xe6)
2850 (define-regular-sse-inst cvtdq2ps nil #x5b)
2851 (define-regular-sse-inst cvtpd2dq #xf2 #xe6)
2852 (define-regular-sse-inst cvtpd2ps #x66 #x5a)
2853 (define-regular-sse-inst cvtps2dq #x66 #x5b)
2854 (define-regular-sse-inst cvtps2pd nil #x5a)
2855 (define-regular-sse-inst cvtsd2ss #xf2 #x5a)
2856 (define-regular-sse-inst cvtss2sd #xf3 #x5a)
2857 (define-regular-sse-inst cvttpd2dq #x66 #xe6)
2858 (define-regular-sse-inst cvttps2dq #xf3 #x5b)
2859 ;; integer
2860 (define-regular-sse-inst packsswb #x66 #x63)
2861 (define-regular-sse-inst packssdw #x66 #x6b)
2862 (define-regular-sse-inst packuswb #x66 #x67)
2863 (define-regular-sse-inst punpckhbw #x66 #x68)
2864 (define-regular-sse-inst punpckhwd #x66 #x69)
2865 (define-regular-sse-inst punpckhdq #x66 #x6a)
2866 (define-regular-sse-inst punpckhqdq #x66 #x6d)
2867 (define-regular-sse-inst punpcklbw #x66 #x60)
2868 (define-regular-sse-inst punpcklwd #x66 #x61)
2869 (define-regular-sse-inst punpckldq #x66 #x62)
2870 (define-regular-sse-inst punpcklqdq #x66 #x6c))
2872 (macrolet ((define-xmm-shuffle-sse-inst (name prefix opcode n-bits radix)
2873 (let ((shuffle-pattern
2874 (intern (format nil "SSE-SHUFFLE-PATTERN-~D-~D"
2875 n-bits radix))))
2876 `(define-instruction ,name (segment dst src pattern)
2877 ,@(sse-inst-printer-list
2878 'xmm-xmm/mem prefix opcode
2879 :more-fields `((imm nil :type ',shuffle-pattern))
2880 :printer '(:name :tab reg ", " reg/mem ", " imm))
2882 (:emitter
2883 (aver (typep pattern '(unsigned-byte ,n-bits)))
2884 (emit-regular-sse-inst segment dst src ,prefix ,opcode
2885 :remaining-bytes 1)
2886 (emit-byte segment pattern))))))
2887 (define-xmm-shuffle-sse-inst pshufd #x66 #x70 8 4)
2888 (define-xmm-shuffle-sse-inst pshufhw #xf3 #x70 8 4)
2889 (define-xmm-shuffle-sse-inst pshuflw #xf2 #x70 8 4)
2890 (define-xmm-shuffle-sse-inst shufpd #x66 #xc6 2 2)
2891 (define-xmm-shuffle-sse-inst shufps nil #xc6 8 4))
2893 ;; MASKMOVDQU (dst is DS:RDI)
2894 (define-instruction maskmovdqu (segment src mask)
2895 (:emitter
2896 (aver (xmm-register-p src))
2897 (aver (xmm-register-p mask))
2898 (emit-regular-sse-inst segment src mask #x66 #xf7))
2899 . #.(sse-inst-printer-list 'xmm-xmm/mem #x66 #xf7))
2901 (macrolet ((define-comparison-sse-inst (name prefix opcode
2902 name-prefix name-suffix)
2903 `(define-instruction ,name (segment op x y)
2904 ,@(sse-inst-printer-list
2905 'xmm-xmm/mem prefix opcode
2906 :more-fields '((imm nil :type 'sse-condition-code))
2907 :printer `(,name-prefix imm ,name-suffix
2908 :tab reg ", " reg/mem))
2909 (:emitter
2910 (let ((code (position op +sse-conditions+)))
2911 (aver code)
2912 (emit-regular-sse-inst segment x y ,prefix ,opcode
2913 :remaining-bytes 1)
2914 (emit-byte segment code))))))
2915 (define-comparison-sse-inst cmppd #x66 #xc2 "CMP" "PD")
2916 (define-comparison-sse-inst cmpps nil #xc2 "CMP" "PS")
2917 (define-comparison-sse-inst cmpsd #xf2 #xc2 "CMP" "SD")
2918 (define-comparison-sse-inst cmpss #xf3 #xc2 "CMP" "SS"))
2920 ;;; MOVSD, MOVSS
2921 (macrolet ((define-movsd/ss-sse-inst (name prefix)
2922 `(define-instruction ,name (segment dst src)
2923 ,@(sse-inst-printer-list 'xmm-xmm/mem-dir prefix #b0001000)
2924 (:emitter
2925 (cond ((xmm-register-p dst)
2926 (emit-sse-inst segment dst src ,prefix #x10
2927 :operand-size :do-not-set))
2929 (aver (xmm-register-p src))
2930 (emit-sse-inst segment src dst ,prefix #x11
2931 :operand-size :do-not-set)))))))
2932 (define-movsd/ss-sse-inst movsd #xf2)
2933 (define-movsd/ss-sse-inst movss #xf3))
2935 ;;; Packed MOVs
2936 (macrolet ((define-mov-sse-inst (name prefix opcode-from opcode-to
2937 &key force-to-mem reg-reg-name)
2938 `(progn
2939 ,(when reg-reg-name
2940 `(define-instruction ,reg-reg-name (segment dst src)
2941 (:emitter
2942 (aver (xmm-register-p dst))
2943 (aver (xmm-register-p src))
2944 (emit-regular-sse-inst segment dst src
2945 ,prefix ,opcode-from))))
2946 (define-instruction ,name (segment dst src)
2947 ,@(when opcode-from
2948 (sse-inst-printer-list 'xmm-xmm/mem prefix opcode-from))
2949 ,@(sse-inst-printer-list
2950 'xmm-xmm/mem prefix opcode-to
2951 :printer '(:name :tab reg/mem ", " reg))
2952 (:emitter
2953 (cond ,@(when opcode-from
2954 `(((xmm-register-p dst)
2955 ,(when force-to-mem
2956 `(aver (not (or (register-p src)
2957 (xmm-register-p src)))))
2958 (emit-regular-sse-inst
2959 segment dst src ,prefix ,opcode-from))))
2961 (aver (xmm-register-p src))
2962 ,(when force-to-mem
2963 `(aver (not (or (register-p dst)
2964 (xmm-register-p dst)))))
2965 (emit-regular-sse-inst segment src dst
2966 ,prefix ,opcode-to))))))))
2967 ;; direction bit?
2968 (define-mov-sse-inst movapd #x66 #x28 #x29)
2969 (define-mov-sse-inst movaps nil #x28 #x29)
2970 (define-mov-sse-inst movdqa #x66 #x6f #x7f)
2971 (define-mov-sse-inst movdqu #xf3 #x6f #x7f)
2973 ;; streaming
2974 (define-mov-sse-inst movntdq #x66 nil #xe7 :force-to-mem t)
2975 (define-mov-sse-inst movntpd #x66 nil #x2b :force-to-mem t)
2976 (define-mov-sse-inst movntps nil nil #x2b :force-to-mem t)
2978 ;; use movhps for movlhps and movlps for movhlps
2979 (define-mov-sse-inst movhpd #x66 #x16 #x17 :force-to-mem t)
2980 (define-mov-sse-inst movhps nil #x16 #x17 :reg-reg-name movlhps)
2981 (define-mov-sse-inst movlpd #x66 #x12 #x13 :force-to-mem t)
2982 (define-mov-sse-inst movlps nil #x12 #x13 :reg-reg-name movhlps)
2983 (define-mov-sse-inst movupd #x66 #x10 #x11)
2984 (define-mov-sse-inst movups nil #x10 #x11))
2986 ;;; MOVNTDQA
2987 (define-instruction movntdqa (segment dst src)
2988 (:emitter
2989 (aver (and (xmm-register-p dst)
2990 (not (xmm-register-p src))))
2991 (emit-regular-2byte-sse-inst segment dst src #x66 #x38 #x2a))
2992 . #.(2byte-sse-inst-printer-list '2byte-xmm-xmm/mem #x66 #x38 #x2a))
2994 ;;; MOVQ
2995 (define-instruction movq (segment dst src)
2996 (:emitter
2997 (cond ((xmm-register-p dst)
2998 (emit-sse-inst segment dst src #xf3 #x7e
2999 :operand-size :do-not-set))
3001 (aver (xmm-register-p src))
3002 (emit-sse-inst segment src dst #x66 #xd6
3003 :operand-size :do-not-set))))
3004 . #.(append (sse-inst-printer-list 'xmm-xmm/mem #xf3 #x7e)
3005 (sse-inst-printer-list 'xmm-xmm/mem #x66 #xd6
3006 :printer '(:name :tab reg/mem ", " reg))))
3008 ;;; Instructions having an XMM register as the destination operand
3009 ;;; and a general-purpose register or a memory location as the source
3010 ;;; operand. The operand size is calculated from the source operand.
3012 ;;; MOVD - Move a 32- or 64-bit value from a general-purpose register or
3013 ;;; a memory location to the low order 32 or 64 bits of an XMM register
3014 ;;; with zero extension or vice versa.
3015 ;;; We do not support the MMX version of this instruction.
3016 (define-instruction movd (segment dst src)
3017 (:emitter
3018 (cond ((xmm-register-p dst)
3019 (emit-sse-inst segment dst src #x66 #x6e))
3021 (aver (xmm-register-p src))
3022 (emit-sse-inst segment src dst #x66 #x7e))))
3023 . #.(append (sse-inst-printer-list 'xmm-reg/mem #x66 #x6e)
3024 (sse-inst-printer-list 'xmm-reg/mem #x66 #x7e
3025 :printer '(:name :tab reg/mem ", " reg))))
3027 (macrolet ((define-extract-sse-instruction (name prefix op1 op2
3028 &key explicit-qword)
3029 `(define-instruction ,name (segment dst src imm)
3030 (:printer
3031 ,(if op2 (if explicit-qword
3032 'ext-rex-2byte-reg/mem-xmm
3033 'ext-2byte-reg/mem-xmm)
3034 'ext-reg/mem-xmm)
3035 ((prefix '(,prefix))
3036 ,@(if op2
3037 `((op1 '(,op1)) (op2 '(,op2)))
3038 `((op '(,op1))))
3039 (imm nil :type 'imm-byte))
3040 '(:name :tab reg/mem ", " reg ", " imm))
3041 (:emitter
3042 (aver (and (xmm-register-p src) (not (xmm-register-p dst))))
3043 ,(if op2
3044 `(emit-sse-inst-2byte segment dst src ,prefix ,op1 ,op2
3045 :operand-size ,(if explicit-qword
3046 :qword
3047 :do-not-set)
3048 :remaining-bytes 1)
3049 `(emit-sse-inst segment dst src ,prefix ,op1
3050 :operand-size ,(if explicit-qword
3051 :qword
3052 :do-not-set)
3053 :remaining-bytes 1))
3054 (emit-byte segment imm))))
3056 (define-insert-sse-instruction (name prefix op1 op2)
3057 `(define-instruction ,name (segment dst src imm)
3058 (:printer
3059 ,(if op2 'ext-2byte-xmm-reg/mem 'ext-xmm-reg/mem)
3060 ((prefix '(,prefix))
3061 ,@(if op2
3062 `((op1 '(,op1)) (op2 '(,op2)))
3063 `((op '(,op1))))
3064 (imm nil :type 'imm-byte))
3065 '(:name :tab reg ", " reg/mem ", " imm))
3066 (:emitter
3067 (aver (and (xmm-register-p dst) (not (xmm-register-p src))))
3068 ,(if op2
3069 `(emit-sse-inst-2byte segment dst src ,prefix ,op1 ,op2
3070 :operand-size :do-not-set
3071 :remaining-bytes 1)
3072 `(emit-sse-inst segment dst src ,prefix ,op1
3073 :operand-size :do-not-set
3074 :remaining-bytes 1))
3075 (emit-byte segment imm)))))
3078 ;; pinsrq not encodable in 64-bit mode
3079 (define-insert-sse-instruction pinsrb #x66 #x3a #x20)
3080 (define-insert-sse-instruction pinsrw #x66 #xc4 nil)
3081 (define-insert-sse-instruction pinsrd #x66 #x3a #x22)
3082 (define-insert-sse-instruction insertps #x66 #x3a #x21)
3084 (define-extract-sse-instruction pextrb #x66 #x3a #x14)
3085 (define-extract-sse-instruction pextrd #x66 #x3a #x16)
3086 (define-extract-sse-instruction pextrq #x66 #x3a #x16 :explicit-qword t)
3087 (define-extract-sse-instruction extractps #x66 #x3a #x17))
3089 ;; PEXTRW has a new 2-byte encoding in SSE4.1 to allow dst to be
3090 ;; a memory address.
3091 (define-instruction pextrw (segment dst src imm)
3092 (:emitter
3093 (aver (xmm-register-p src))
3094 (if (not (register-p dst))
3095 (emit-sse-inst-2byte segment dst src #x66 #x3a #x15
3096 :operand-size :do-not-set :remaining-bytes 1)
3097 (emit-sse-inst segment dst src #x66 #xc5
3098 :operand-size :do-not-set :remaining-bytes 1))
3099 (emit-byte segment imm))
3100 . #.(append
3101 (2byte-sse-inst-printer-list '2byte-reg/mem-xmm #x66 #x3a #x15
3102 :more-fields '((imm nil :type 'imm-byte))
3103 :printer '(:name :tab reg/mem ", " reg ", " imm))
3104 (sse-inst-printer-list 'reg/mem-xmm #x66 #xc5
3105 :more-fields '((imm nil :type 'imm-byte))
3106 :printer '(:name :tab reg/mem ", " reg ", " imm))))
3108 (macrolet ((define-integer-source-sse-inst (name prefix opcode &key mem-only)
3109 `(define-instruction ,name (segment dst src)
3110 ,@(sse-inst-printer-list 'xmm-reg/mem prefix opcode)
3111 (:emitter
3112 (aver (xmm-register-p dst))
3113 ,(when mem-only
3114 `(aver (not (or (register-p src)
3115 (xmm-register-p src)))))
3116 (let ((src-size (operand-size src)))
3117 (aver (or (eq src-size :qword) (eq src-size :dword))))
3118 (emit-sse-inst segment dst src ,prefix ,opcode)))))
3119 (define-integer-source-sse-inst cvtsi2sd #xf2 #x2a)
3120 (define-integer-source-sse-inst cvtsi2ss #xf3 #x2a)
3121 ;; FIXME: memory operand is always a QWORD
3122 (define-integer-source-sse-inst cvtpi2pd #x66 #x2a :mem-only t)
3123 (define-integer-source-sse-inst cvtpi2ps nil #x2a :mem-only t))
3125 ;;; Instructions having a general-purpose register as the destination
3126 ;;; operand and an XMM register or a memory location as the source
3127 ;;; operand. The operand size is calculated from the destination
3128 ;;; operand.
3130 (macrolet ((define-gpr-destination-sse-inst (name prefix opcode &key reg-only)
3131 `(define-instruction ,name (segment dst src)
3132 ,@(sse-inst-printer-list 'reg-xmm/mem prefix opcode)
3133 (:emitter
3134 (aver (register-p dst))
3135 ,(when reg-only
3136 `(aver (xmm-register-p src)))
3137 (let ((dst-size (operand-size dst)))
3138 (aver (or (eq dst-size :qword) (eq dst-size :dword)))
3139 (emit-sse-inst segment dst src ,prefix ,opcode
3140 :operand-size dst-size))))))
3141 (define-gpr-destination-sse-inst cvtsd2si #xf2 #x2d)
3142 (define-gpr-destination-sse-inst cvtss2si #xf3 #x2d)
3143 (define-gpr-destination-sse-inst cvttsd2si #xf2 #x2c)
3144 (define-gpr-destination-sse-inst cvttss2si #xf3 #x2c)
3145 (define-gpr-destination-sse-inst movmskpd #x66 #x50 :reg-only t)
3146 (define-gpr-destination-sse-inst movmskps nil #x50 :reg-only t)
3147 (define-gpr-destination-sse-inst pmovmskb #x66 #xd7 :reg-only t))
3149 ;;;; We call these "2byte" instructions due to their two opcode bytes.
3150 ;;;; Intel and AMD call them three-byte instructions, as they count the
3151 ;;;; 0x0f byte for determining the number of opcode bytes.
3153 ;;; Instructions that take XMM-XMM/MEM and XMM-XMM/MEM-IMM arguments.
3155 (macrolet ((regular-2byte-sse-inst (name prefix op1 op2)
3156 `(define-instruction ,name (segment dst src)
3157 ,@(2byte-sse-inst-printer-list '2byte-xmm-xmm/mem prefix
3158 op1 op2)
3159 (:emitter
3160 (emit-regular-2byte-sse-inst segment dst src ,prefix
3161 ,op1 ,op2))))
3162 (regular-2byte-sse-inst-imm (name prefix op1 op2)
3163 `(define-instruction ,name (segment dst src imm)
3164 ,@(2byte-sse-inst-printer-list
3165 '2byte-xmm-xmm/mem prefix op1 op2
3166 :more-fields '((imm nil :type 'imm-byte))
3167 :printer `(:name :tab reg ", " reg/mem ", " imm))
3168 (:emitter
3169 (aver (typep imm '(unsigned-byte 8)))
3170 (emit-regular-2byte-sse-inst segment dst src ,prefix ,op1 ,op2
3171 :remaining-bytes 1)
3172 (emit-byte segment imm)))))
3173 (regular-2byte-sse-inst pshufb #x66 #x38 #x00)
3174 (regular-2byte-sse-inst phaddw #x66 #x38 #x01)
3175 (regular-2byte-sse-inst phaddd #x66 #x38 #x02)
3176 (regular-2byte-sse-inst phaddsw #x66 #x38 #x03)
3177 (regular-2byte-sse-inst pmaddubsw #x66 #x38 #x04)
3178 (regular-2byte-sse-inst phsubw #x66 #x38 #x05)
3179 (regular-2byte-sse-inst phsubd #x66 #x38 #x06)
3180 (regular-2byte-sse-inst phsubsw #x66 #x38 #x07)
3181 (regular-2byte-sse-inst psignb #x66 #x38 #x08)
3182 (regular-2byte-sse-inst psignw #x66 #x38 #x09)
3183 (regular-2byte-sse-inst psignd #x66 #x38 #x0a)
3184 (regular-2byte-sse-inst pmulhrsw #x66 #x38 #x0b)
3186 (regular-2byte-sse-inst ptest #x66 #x38 #x17)
3187 (regular-2byte-sse-inst pabsb #x66 #x38 #x1c)
3188 (regular-2byte-sse-inst pabsw #x66 #x38 #x1d)
3189 (regular-2byte-sse-inst pabsd #x66 #x38 #x1e)
3191 (regular-2byte-sse-inst pmuldq #x66 #x38 #x28)
3192 (regular-2byte-sse-inst pcmpeqq #x66 #x38 #x29)
3193 (regular-2byte-sse-inst packusdw #x66 #x38 #x2b)
3195 (regular-2byte-sse-inst pcmpgtq #x66 #x38 #x37)
3196 (regular-2byte-sse-inst pminsb #x66 #x38 #x38)
3197 (regular-2byte-sse-inst pminsd #x66 #x38 #x39)
3198 (regular-2byte-sse-inst pminuw #x66 #x38 #x3a)
3199 (regular-2byte-sse-inst pminud #x66 #x38 #x3b)
3200 (regular-2byte-sse-inst pmaxsb #x66 #x38 #x3c)
3201 (regular-2byte-sse-inst pmaxsd #x66 #x38 #x3d)
3202 (regular-2byte-sse-inst pmaxuw #x66 #x38 #x3e)
3203 (regular-2byte-sse-inst pmaxud #x66 #x38 #x3f)
3205 (regular-2byte-sse-inst pmulld #x66 #x38 #x40)
3206 (regular-2byte-sse-inst phminposuw #x66 #x38 #x41)
3208 (regular-2byte-sse-inst aesimc #x66 #x38 #xdb)
3209 (regular-2byte-sse-inst aesenc #x66 #x38 #xdc)
3210 (regular-2byte-sse-inst aesenclast #x66 #x38 #xdd)
3211 (regular-2byte-sse-inst aesdec #x66 #x38 #xde)
3212 (regular-2byte-sse-inst aesdeclast #x66 #x38 #xdf)
3214 (regular-2byte-sse-inst pmovsxbw #x66 #x38 #x20)
3215 (regular-2byte-sse-inst pmovsxbd #x66 #x38 #x21)
3216 (regular-2byte-sse-inst pmovsxbq #x66 #x38 #x22)
3217 (regular-2byte-sse-inst pmovsxwd #x66 #x38 #x23)
3218 (regular-2byte-sse-inst pmovsxwq #x66 #x38 #x24)
3219 (regular-2byte-sse-inst pmovsxdq #x66 #x38 #x25)
3221 (regular-2byte-sse-inst pmovzxbw #x66 #x38 #x30)
3222 (regular-2byte-sse-inst pmovzxbd #x66 #x38 #x31)
3223 (regular-2byte-sse-inst pmovzxbq #x66 #x38 #x32)
3224 (regular-2byte-sse-inst pmovzxwd #x66 #x38 #x33)
3225 (regular-2byte-sse-inst pmovzxwq #x66 #x38 #x34)
3226 (regular-2byte-sse-inst pmovzxdq #x66 #x38 #x35)
3228 (regular-2byte-sse-inst-imm roundps #x66 #x3a #x08)
3229 (regular-2byte-sse-inst-imm roundpd #x66 #x3a #x09)
3230 (regular-2byte-sse-inst-imm roundss #x66 #x3a #x0a)
3231 (regular-2byte-sse-inst-imm roundsd #x66 #x3a #x0b)
3232 (regular-2byte-sse-inst-imm blendps #x66 #x3a #x0c)
3233 (regular-2byte-sse-inst-imm blendpd #x66 #x3a #x0d)
3234 (regular-2byte-sse-inst-imm pblendw #x66 #x3a #x0e)
3235 (regular-2byte-sse-inst-imm palignr #x66 #x3a #x0f)
3236 (regular-2byte-sse-inst-imm dpps #x66 #x3a #x40)
3237 (regular-2byte-sse-inst-imm dppd #x66 #x3a #x41)
3239 (regular-2byte-sse-inst-imm mpsadbw #x66 #x3a #x42)
3240 (regular-2byte-sse-inst-imm pclmulqdq #x66 #x3a #x44)
3242 (regular-2byte-sse-inst-imm pcmpestrm #x66 #x3a #x60)
3243 (regular-2byte-sse-inst-imm pcmpestri #x66 #x3a #x61)
3244 (regular-2byte-sse-inst-imm pcmpistrm #x66 #x3a #x62)
3245 (regular-2byte-sse-inst-imm pcmpistri #x66 #x3a #x63)
3247 (regular-2byte-sse-inst-imm aeskeygenassist #x66 #x3a #xdf))
3249 ;;; Other SSE instructions
3251 ;; Instructions implicitly using XMM0 as a mask
3252 (macrolet ((define-sse-inst-implicit-mask (name prefix op1 op2)
3253 `(define-instruction ,name (segment dst src mask)
3254 ,@(2byte-sse-inst-printer-list
3255 '2byte-xmm-xmm/mem prefix op1 op2
3256 :printer '(:name :tab reg ", " reg/mem ", XMM0"))
3257 (:emitter
3258 (aver (xmm-register-p dst))
3259 (aver (and (xmm-register-p mask) (= (tn-offset mask) 0)))
3260 (emit-regular-2byte-sse-inst segment dst src ,prefix
3261 ,op1 ,op2)))))
3263 (define-sse-inst-implicit-mask pblendvb #x66 #x38 #x10)
3264 (define-sse-inst-implicit-mask blendvps #x66 #x38 #x14)
3265 (define-sse-inst-implicit-mask blendvpd #x66 #x38 #x15))
3267 (define-instruction movnti (segment dst src)
3268 (:printer ext-reg-reg/mem-no-width ((op #xc3)) '(:name :tab reg/mem ", " reg))
3269 (:emitter
3270 (aver (not (or (register-p dst)
3271 (xmm-register-p dst))))
3272 (aver (register-p src))
3273 (maybe-emit-rex-for-ea segment dst src)
3274 (emit-byte segment #x0f)
3275 (emit-byte segment #xc3)
3276 (emit-ea segment dst (reg-tn-encoding src))))
3278 (define-instruction prefetch (segment type src)
3279 (:printer ext-reg/mem-no-width ((op '(#x18 0)))
3280 '("PREFETCHNTA" :tab reg/mem))
3281 (:printer ext-reg/mem-no-width ((op '(#x18 1)))
3282 '("PREFETCHT0" :tab reg/mem))
3283 (:printer ext-reg/mem-no-width ((op '(#x18 2)))
3284 '("PREFETCHT1" :tab reg/mem))
3285 (:printer ext-reg/mem-no-width ((op '(#x18 3)))
3286 '("PREFETCHT2" :tab reg/mem))
3287 (:emitter
3288 (aver (not (or (register-p src)
3289 (xmm-register-p src))))
3290 (aver (eq (operand-size src) :byte))
3291 (let ((type (position type #(:nta :t0 :t1 :t2))))
3292 (aver type)
3293 (maybe-emit-rex-for-ea segment src nil)
3294 (emit-byte segment #x0f)
3295 (emit-byte segment #x18)
3296 (emit-ea segment src type))))
3298 (define-instruction clflush (segment src)
3299 (:printer ext-reg/mem-no-width ((op '(#xae 7))))
3300 (:emitter
3301 (aver (not (or (register-p src)
3302 (xmm-register-p src))))
3303 (aver (eq (operand-size src) :byte))
3304 (maybe-emit-rex-for-ea segment src nil)
3305 (emit-byte segment #x0f)
3306 (emit-byte segment #xae)
3307 (emit-ea segment src 7)))
3309 (macrolet ((define-fence-instruction (name last-byte)
3310 `(define-instruction ,name (segment)
3311 (:printer three-bytes ((op '(#x0f #xae ,last-byte))))
3312 (:emitter
3313 (emit-byte segment #x0f)
3314 (emit-byte segment #xae)
3315 (emit-byte segment ,last-byte)))))
3316 (define-fence-instruction lfence #b11101000)
3317 (define-fence-instruction mfence #b11110000)
3318 (define-fence-instruction sfence #b11111000))
3320 (define-instruction pause (segment)
3321 (:printer two-bytes ((op '(#xf3 #x90))))
3322 (:emitter
3323 (emit-byte segment #xf3)
3324 (emit-byte segment #x90)))
3326 (define-instruction ldmxcsr (segment src)
3327 (:printer ext-reg/mem-no-width ((op '(#xae 2))))
3328 (:emitter
3329 (aver (not (or (register-p src)
3330 (xmm-register-p src))))
3331 (aver (eq (operand-size src) :dword))
3332 (maybe-emit-rex-for-ea segment src nil)
3333 (emit-byte segment #x0f)
3334 (emit-byte segment #xae)
3335 (emit-ea segment src 2)))
3337 (define-instruction stmxcsr (segment dst)
3338 (:printer ext-reg/mem-no-width ((op '(#xae 3))))
3339 (:emitter
3340 (aver (not (or (register-p dst)
3341 (xmm-register-p dst))))
3342 (aver (eq (operand-size dst) :dword))
3343 (maybe-emit-rex-for-ea segment dst nil)
3344 (emit-byte segment #x0f)
3345 (emit-byte segment #xae)
3346 (emit-ea segment dst 3)))
3348 (define-instruction popcnt (segment dst src)
3349 (:printer f3-escape-reg-reg/mem ((op #xB8)))
3350 (:printer rex-f3-escape-reg-reg/mem ((op #xB8)))
3351 (:emitter
3352 (aver (register-p dst))
3353 (aver (and (register-p dst) (not (eq (operand-size dst) :byte))))
3354 (aver (not (eq (operand-size src) :byte)))
3355 (emit-sse-inst segment dst src #xf3 #xb8)))
3357 (define-instruction crc32 (segment dst src)
3358 ;; The low bit of the final opcode byte sets the source size.
3359 ;; REX.W bit sets the destination size. can't have #x66 prefix and REX.W = 1.
3360 (:printer ext-2byte-prefix-reg-reg/mem
3361 ((prefix #xf2) (op1 #x38)
3362 (op2 #b1111000 :field (byte 7 25)) ; #xF0 ignoring the low bit
3363 (src-width nil :field (byte 1 24) :prefilter #'prefilter-width)
3364 (reg nil :printer #'print-d/q-word-reg)))
3365 (:printer ext-rex-2byte-prefix-reg-reg/mem
3366 ((prefix #xf2) (op1 #x38)
3367 (op2 #b1111000 :field (byte 7 33)) ; ditto
3368 (src-width nil :field (byte 1 32) :prefilter #'prefilter-width)
3369 (reg nil :printer #'print-d/q-word-reg)))
3370 (:emitter
3371 (let ((dst-size (operand-size dst))
3372 (src-size (operand-size src)))
3373 ;; The following operand size combinations are possible:
3374 ;; dst = r32, src = r/m{8, 16, 32}
3375 ;; dst = r64, src = r/m{8, 64}
3376 (aver (and (register-p dst)
3377 (memq src-size (case dst-size
3378 (:dword '(:byte :word :dword))
3379 (:qword '(:byte :qword))))))
3380 (maybe-emit-operand-size-prefix segment src-size)
3381 (emit-sse-inst-2byte segment dst src #xf2 #x38
3382 (if (eq src-size :byte) #xf0 #xf1)
3383 ;; :OPERAND-SIZE is ordinarily determined
3384 ;; from 'src', so override it to use 'dst'.
3385 :operand-size dst-size))))
3387 ;;;; Miscellany
3389 (define-instruction cpuid (segment)
3390 (:printer two-bytes ((op '(#b00001111 #b10100010))))
3391 (:emitter
3392 (emit-byte segment #b00001111)
3393 (emit-byte segment #b10100010)))
3395 (define-instruction rdtsc (segment)
3396 (:printer two-bytes ((op '(#b00001111 #b00110001))))
3397 (:emitter
3398 (emit-byte segment #b00001111)
3399 (emit-byte segment #b00110001)))
3401 ;;;; Intel TSX - some user library (STMX) used to define these,
3402 ;;;; but it's not really supported and they actually belong here.
3404 (define-instruction-format
3405 (xbegin 48 :default-printer '(:name :tab label))
3406 (op :fields (list (byte 8 0) (byte 8 8)) :value '(#xc7 #xf8))
3407 (label :field (byte 32 16) :type 'displacement))
3409 (define-instruction-format
3410 (xabort 24 :default-printer '(:name :tab imm))
3411 (op :fields (list (byte 8 0) (byte 8 8)) :value '(#xc6 #xf8))
3412 (imm :field (byte 8 16)))
3414 (define-instruction xbegin (segment &optional where)
3415 (:printer xbegin ())
3416 (:emitter
3417 (emit-byte segment #xc7)
3418 (emit-byte segment #xf8)
3419 (if where
3420 ;; emit 32-bit, signed relative offset for where
3421 (emit-dword-displacement-backpatch segment where)
3422 ;; nowhere to jump: simply jump to the next instruction
3423 (emit-skip segment 4 0))))
3425 (define-instruction xend (segment)
3426 (:printer three-bytes ((op '(#x0f #x01 #xd5))))
3427 (:emitter
3428 (emit-byte segment #x0f)
3429 (emit-byte segment #x01)
3430 (emit-byte segment #xd5)))
3432 (define-instruction xabort (segment reason)
3433 (:printer xabort ())
3434 (:emitter
3435 (aver (<= 0 reason #xff))
3436 (emit-byte segment #xc6)
3437 (emit-byte segment #xf8)
3438 (emit-byte segment reason)))
3440 (define-instruction xtest (segment)
3441 (:printer three-bytes ((op '(#x0f #x01 #xd6))))
3442 (:emitter
3443 (emit-byte segment #x0f)
3444 (emit-byte segment #x01)
3445 (emit-byte segment #xd6)))
3447 (define-instruction xacquire (segment) ;; same prefix byte as repne/repnz
3448 (:emitter
3449 (emit-byte segment #xf2)))
3451 (define-instruction xrelease (segment) ;; same prefix byte as rep/repe/repz
3452 (:emitter
3453 (emit-byte segment #xf3)))
3455 ;;;; Late VM definitions
3457 (defun canonicalize-inline-constant (constant &aux (alignedp nil))
3458 (let ((first (car constant)))
3459 (when (eql first :aligned)
3460 (setf alignedp t)
3461 (pop constant)
3462 (setf first (car constant)))
3463 (typecase first
3464 (single-float (setf constant (list :single-float first)))
3465 (double-float (setf constant (list :double-float first)))
3467 #+sb-xc-host
3468 ((complex
3469 ;; It's an error (perhaps) on the host to use simd-pack type.
3470 ;; [and btw it's disconcerting that this isn't an ETYPECASE.]
3471 (error "xc-host can't reference complex float")))
3472 #-sb-xc-host
3473 (((complex single-float)
3474 (setf constant (list :complex-single-float first)))
3475 ((complex double-float)
3476 (setf constant (list :complex-double-float first)))
3477 #!+sb-simd-pack
3478 (simd-pack
3479 (setq constant
3480 (list :sse (logior (%simd-pack-low first)
3481 (ash (%simd-pack-high first) 64))))))))
3482 (destructuring-bind (type value) constant
3483 (ecase type
3484 ((:byte :word :dword :qword)
3485 (aver (integerp value))
3486 (cons type value))
3487 ((:base-char)
3488 #!+sb-unicode (aver (typep value 'base-char))
3489 (cons :byte (char-code value)))
3490 ((:character)
3491 (aver (characterp value))
3492 (cons :dword (char-code value)))
3493 ((:single-float)
3494 (aver (typep value 'single-float))
3495 (cons (if alignedp :oword :dword)
3496 (ldb (byte 32 0) (single-float-bits value))))
3497 ((:double-float)
3498 (aver (typep value 'double-float))
3499 (cons (if alignedp :oword :qword)
3500 (ldb (byte 64 0) (logior (ash (double-float-high-bits value) 32)
3501 (double-float-low-bits value)))))
3502 ((:complex-single-float)
3503 (aver (typep value '(complex single-float)))
3504 (cons (if alignedp :oword :qword)
3505 (ldb (byte 64 0)
3506 (logior (ash (single-float-bits (imagpart value)) 32)
3507 (ldb (byte 32 0)
3508 (single-float-bits (realpart value)))))))
3509 ((:oword :sse)
3510 (aver (integerp value))
3511 (cons :oword value))
3512 ((:complex-double-float)
3513 (aver (typep value '(complex double-float)))
3514 (cons :oword
3515 (logior (ash (double-float-high-bits (imagpart value)) 96)
3516 (ash (double-float-low-bits (imagpart value)) 64)
3517 (ash (ldb (byte 32 0)
3518 (double-float-high-bits (realpart value)))
3520 (double-float-low-bits (realpart value))))))))
3522 (defun inline-constant-value (constant)
3523 (let ((label (gen-label))
3524 (size (ecase (car constant)
3525 ((:byte :word :dword :qword) (car constant))
3526 ((:oword) :qword))))
3527 (values label (make-ea size
3528 :disp (make-fixup nil :code-object label)))))
3530 (defun emit-constant-segment-header (segment constants optimize)
3531 (declare (ignore constants))
3532 (emit-long-nop segment (if optimize 64 16)))
3534 (defun size-nbyte (size)
3535 (ecase size
3536 (:byte 1)
3537 (:word 2)
3538 (:dword 4)
3539 (:qword 8)
3540 (:oword 16)))
3542 (defun sort-inline-constants (constants)
3543 (stable-sort constants #'> :key (lambda (constant)
3544 (size-nbyte (caar constant)))))
3546 (defun emit-inline-constant (constant label)
3547 (let ((size (size-nbyte (car constant))))
3548 (emit-alignment (integer-length (1- size)))
3549 (emit-label label)
3550 (let ((val (cdr constant)))
3551 (loop repeat size
3552 do (inst byte (ldb (byte 8 0) val))
3553 (setf val (ash val -8))))))