1 ;;;; that part of the description of the x86 instruction set (for
2 ;;;; 80386 and above) which can live on the cross-compilation host
4 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
14 ;;; FIXME: SB!DISASSEM: prefixes are used so widely in this file that
15 ;;; I wonder whether the separation of the disassembler from the
16 ;;; virtual machine is valid or adds value.
18 ;;; Note: In CMU CL, this used to be a call to SET-DISASSEM-PARAMS.
19 (setf sb!disassem:*disassem-inst-alignment-bytes* 1)
21 (deftype reg () '(unsigned-byte 3))
23 (def!constant +default-operand-size+ :dword)
25 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
27 (defun offset-next (value dstate)
28 (declare (type integer value)
29 (type sb!disassem:disassem-state dstate))
30 (+ (sb!disassem:dstate-next-addr dstate) value))
32 (defparameter *default-address-size*
33 ;; Actually, :DWORD is the only one really supported.
36 (defparameter *byte-reg-names*
37 #(al cl dl bl ah ch dh bh))
38 (defparameter *word-reg-names*
39 #(ax cx dx bx sp bp si di))
40 (defparameter *dword-reg-names*
41 #(eax ecx edx ebx esp ebp esi edi))
43 (defun print-reg-with-width (value width stream dstate)
44 (declare (ignore dstate))
45 (princ (aref (ecase width
46 (:byte *byte-reg-names*)
47 (:word *word-reg-names*)
48 (:dword *dword-reg-names*))
51 ;; XXX plus should do some source-var notes
54 (defun print-reg (value stream dstate)
55 (declare (type reg value)
57 (type sb!disassem:disassem-state dstate))
58 (print-reg-with-width value
59 (sb!disassem:dstate-get-prop dstate 'width)
63 (defun print-word-reg (value stream dstate)
64 (declare (type reg value)
66 (type sb!disassem:disassem-state dstate))
67 (print-reg-with-width value
68 (or (sb!disassem:dstate-get-prop dstate 'word-width)
69 +default-operand-size+)
73 (defun print-byte-reg (value stream dstate)
74 (declare (type reg value)
76 (type sb!disassem:disassem-state dstate))
77 (print-reg-with-width value :byte stream dstate))
79 (defun print-addr-reg (value stream dstate)
80 (declare (type reg value)
82 (type sb!disassem:disassem-state dstate))
83 (print-reg-with-width value *default-address-size* stream dstate))
85 (defun print-reg/mem (value stream dstate)
86 (declare (type (or list reg) value)
88 (type sb!disassem:disassem-state dstate))
89 (if (typep value 'reg)
90 (print-reg value stream dstate)
91 (print-mem-access value stream nil dstate)))
93 ;; Same as print-reg/mem, but prints an explicit size indicator for
95 (defun print-sized-reg/mem (value stream dstate)
96 (declare (type (or list reg) value)
98 (type sb!disassem:disassem-state dstate))
99 (if (typep value 'reg)
100 (print-reg value stream dstate)
101 (print-mem-access value stream t dstate)))
103 (defun print-byte-reg/mem (value stream dstate)
104 (declare (type (or list reg) value)
106 (type sb!disassem:disassem-state dstate))
107 (if (typep value 'reg)
108 (print-byte-reg value stream dstate)
109 (print-mem-access value stream t dstate)))
111 (defun print-word-reg/mem (value stream dstate)
112 (declare (type (or list reg) value)
114 (type sb!disassem:disassem-state dstate))
115 (if (typep value 'reg)
116 (print-word-reg value stream dstate)
117 (print-mem-access value stream nil dstate)))
119 (defun print-label (value stream dstate)
120 (declare (ignore dstate))
121 (sb!disassem:princ16 value stream))
123 ;;; Returns either an integer, meaning a register, or a list of
124 ;;; (BASE-REG OFFSET INDEX-REG INDEX-SCALE), where any component
125 ;;; may be missing or nil to indicate that it's not used or has the
126 ;;; obvious default value (e.g., 1 for the index-scale).
127 (defun prefilter-reg/mem (value dstate)
128 (declare (type list value)
129 (type sb!disassem:disassem-state dstate))
130 (let ((mod (car value))
132 (declare (type (unsigned-byte 2) mod)
133 (type (unsigned-byte 3) r/m))
139 (let ((sib (sb!disassem:read-suffix 8 dstate)))
140 (declare (type (unsigned-byte 8) sib))
141 (let ((base-reg (ldb (byte 3 0) sib))
142 (index-reg (ldb (byte 3 3) sib))
143 (index-scale (ldb (byte 2 6) sib)))
144 (declare (type (unsigned-byte 3) base-reg index-reg)
145 (type (unsigned-byte 2) index-scale))
149 (if (= base-reg #b101)
150 (sb!disassem:read-signed-suffix 32 dstate)
153 (sb!disassem:read-signed-suffix 8 dstate))
155 (sb!disassem:read-signed-suffix 32 dstate)))))
156 (list (if (and (= mod #b00) (= base-reg #b101)) nil base-reg)
158 (if (= index-reg #b100) nil index-reg)
159 (ash 1 index-scale))))))
160 ((and (= mod #b00) (= r/m #b101))
161 (list nil (sb!disassem:read-signed-suffix 32 dstate)) )
165 (list r/m (sb!disassem:read-signed-suffix 8 dstate)))
167 (list r/m (sb!disassem:read-signed-suffix 32 dstate))))))
170 ;;; This is a sort of bogus prefilter that just stores the info globally for
171 ;;; other people to use; it probably never gets printed.
172 (defun prefilter-width (value dstate)
173 (setf (sb!disassem:dstate-get-prop dstate 'width)
177 ;; set by a prefix instruction
178 (or (sb!disassem:dstate-get-prop dstate 'word-width)
179 +default-operand-size+)))
180 (when (not (eql word-width +default-operand-size+))
182 (setf (sb!disassem:dstate-get-prop dstate 'word-width)
183 +default-operand-size+))
186 (defun read-address (value dstate)
187 (declare (ignore value)) ; always nil anyway
188 (sb!disassem:read-suffix (width-bits *default-address-size*) dstate))
190 (defun width-bits (width)
200 ;;;; disassembler argument types
202 (sb!disassem:define-arg-type displacement
204 :use-label #'offset-next
205 :printer (lambda (value stream dstate)
206 (sb!disassem:maybe-note-assembler-routine value nil dstate)
207 (print-label value stream dstate)))
209 (sb!disassem:define-arg-type accum
210 :printer (lambda (value stream dstate)
211 (declare (ignore value)
213 (type sb!disassem:disassem-state dstate))
214 (print-reg 0 stream dstate)))
216 (sb!disassem:define-arg-type word-accum
217 :printer (lambda (value stream dstate)
218 (declare (ignore value)
220 (type sb!disassem:disassem-state dstate))
221 (print-word-reg 0 stream dstate)))
223 (sb!disassem:define-arg-type reg
224 :printer #'print-reg)
226 (sb!disassem:define-arg-type addr-reg
227 :printer #'print-addr-reg)
229 (sb!disassem:define-arg-type word-reg
230 :printer #'print-word-reg)
232 (sb!disassem:define-arg-type imm-addr
233 :prefilter #'read-address
234 :printer #'print-label)
236 (sb!disassem:define-arg-type imm-data
237 :prefilter (lambda (value dstate)
238 (declare (ignore value)) ; always nil anyway
239 (sb!disassem:read-suffix
240 (width-bits (sb!disassem:dstate-get-prop dstate 'width))
243 (sb!disassem:define-arg-type signed-imm-data
244 :prefilter (lambda (value dstate)
245 (declare (ignore value)) ; always nil anyway
246 (let ((width (sb!disassem:dstate-get-prop dstate 'width)))
247 (sb!disassem:read-signed-suffix (width-bits width) dstate))))
249 (sb!disassem:define-arg-type signed-imm-byte
250 :prefilter (lambda (value dstate)
251 (declare (ignore value)) ; always nil anyway
252 (sb!disassem:read-signed-suffix 8 dstate)))
254 (sb!disassem:define-arg-type signed-imm-dword
255 :prefilter (lambda (value dstate)
256 (declare (ignore value)) ; always nil anyway
257 (sb!disassem:read-signed-suffix 32 dstate)))
259 (sb!disassem:define-arg-type imm-word
260 :prefilter (lambda (value dstate)
261 (declare (ignore value)) ; always nil anyway
263 (or (sb!disassem:dstate-get-prop dstate 'word-width)
264 +default-operand-size+)))
265 (sb!disassem:read-suffix (width-bits width) dstate))))
267 (sb!disassem:define-arg-type signed-imm-word
268 :prefilter (lambda (value dstate)
269 (declare (ignore value)) ; always nil anyway
271 (or (sb!disassem:dstate-get-prop dstate 'word-width)
272 +default-operand-size+)))
273 (sb!disassem:read-signed-suffix (width-bits width) dstate))))
275 ;;; needed for the ret imm16 instruction
276 (sb!disassem:define-arg-type imm-word-16
277 :prefilter (lambda (value dstate)
278 (declare (ignore value)) ; always nil anyway
279 (sb!disassem:read-suffix 16 dstate)))
281 (sb!disassem:define-arg-type reg/mem
282 :prefilter #'prefilter-reg/mem
283 :printer #'print-reg/mem)
284 (sb!disassem:define-arg-type sized-reg/mem
285 ;; Same as reg/mem, but prints an explicit size indicator for
286 ;; memory references.
287 :prefilter #'prefilter-reg/mem
288 :printer #'print-sized-reg/mem)
289 (sb!disassem:define-arg-type byte-reg/mem
290 :prefilter #'prefilter-reg/mem
291 :printer #'print-byte-reg/mem)
292 (sb!disassem:define-arg-type word-reg/mem
293 :prefilter #'prefilter-reg/mem
294 :printer #'print-word-reg/mem)
297 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
298 (defun print-fp-reg (value stream dstate)
299 (declare (ignore dstate))
300 (format stream "FR~D" value))
301 (defun prefilter-fp-reg (value dstate)
303 (declare (ignore dstate))
306 (sb!disassem:define-arg-type fp-reg
307 :prefilter #'prefilter-fp-reg
308 :printer #'print-fp-reg)
310 (sb!disassem:define-arg-type width
311 :prefilter #'prefilter-width
312 :printer (lambda (value stream dstate)
315 (and (numberp value) (zerop value))) ; zzz jrd
318 ;; set by a prefix instruction
319 (or (sb!disassem:dstate-get-prop dstate 'word-width)
320 +default-operand-size+)))
321 (princ (schar (symbol-name word-width) 0) stream)))))
323 (eval-when (:compile-toplevel :load-toplevel :execute)
324 (defparameter *conditions*
327 (:b . 2) (:nae . 2) (:c . 2)
328 (:nb . 3) (:ae . 3) (:nc . 3)
329 (:eq . 4) (:e . 4) (:z . 4)
336 (:np . 11) (:po . 11)
337 (:l . 12) (:nge . 12)
338 (:nl . 13) (:ge . 13)
339 (:le . 14) (:ng . 14)
340 (:nle . 15) (:g . 15)))
341 (defparameter *condition-name-vec*
342 (let ((vec (make-array 16 :initial-element nil)))
343 (dolist (cond *conditions*)
344 (when (null (aref vec (cdr cond)))
345 (setf (aref vec (cdr cond)) (car cond))))
349 ;;; Set assembler parameters. (In CMU CL, this was done with
350 ;;; a call to a macro DEF-ASSEMBLER-PARAMS.)
351 (eval-when (:compile-toplevel :load-toplevel :execute)
352 (setf sb!assem:*assem-scheduler-p* nil))
354 (sb!disassem:define-arg-type condition-code
355 :printer *condition-name-vec*)
357 (defun conditional-opcode (condition)
358 (cdr (assoc condition *conditions* :test #'eq)))
360 ;;;; disassembler instruction formats
362 (eval-when (:compile-toplevel :execute)
363 (defun swap-if (direction field1 separator field2)
364 `(:if (,direction :constant 0)
365 (,field1 ,separator ,field2)
366 (,field2 ,separator ,field1))))
368 (sb!disassem:define-instruction-format (byte 8 :default-printer '(:name))
369 (op :field (byte 8 0))
374 (sb!disassem:define-instruction-format (simple 8)
375 (op :field (byte 7 1))
376 (width :field (byte 1 0) :type 'width)
381 ;;; Same as simple, but with direction bit
382 (sb!disassem:define-instruction-format (simple-dir 8 :include 'simple)
383 (op :field (byte 6 2))
384 (dir :field (byte 1 1)))
386 ;;; Same as simple, but with the immediate value occurring by default,
387 ;;; and with an appropiate printer.
388 (sb!disassem:define-instruction-format (accum-imm 8
390 :default-printer '(:name
391 :tab accum ", " imm))
392 (imm :type 'imm-data))
394 (sb!disassem:define-instruction-format (reg-no-width 8
395 :default-printer '(:name :tab reg))
396 (op :field (byte 5 3))
397 (reg :field (byte 3 0) :type 'word-reg)
399 (accum :type 'word-accum)
402 ;;; adds a width field to reg-no-width
403 (sb!disassem:define-instruction-format (reg 8
404 :default-printer '(:name :tab reg))
405 (op :field (byte 4 4))
406 (width :field (byte 1 3) :type 'width)
407 (reg :field (byte 3 0) :type 'reg)
413 ;;; Same as reg, but with direction bit
414 (sb!disassem:define-instruction-format (reg-dir 8 :include 'reg)
415 (op :field (byte 3 5))
416 (dir :field (byte 1 4)))
418 (sb!disassem:define-instruction-format (two-bytes 16
419 :default-printer '(:name))
420 (op :fields (list (byte 8 0) (byte 8 8))))
422 (sb!disassem:define-instruction-format (reg-reg/mem 16
424 `(:name :tab reg ", " reg/mem))
425 (op :field (byte 7 1))
426 (width :field (byte 1 0) :type 'width)
427 (reg/mem :fields (list (byte 2 14) (byte 3 8))
429 (reg :field (byte 3 11) :type 'reg)
433 ;;; same as reg-reg/mem, but with direction bit
434 (sb!disassem:define-instruction-format (reg-reg/mem-dir 16
435 :include 'reg-reg/mem
439 ,(swap-if 'dir 'reg/mem ", " 'reg)))
440 (op :field (byte 6 2))
441 (dir :field (byte 1 1)))
443 ;;; Same as reg-rem/mem, but uses the reg field as a second op code.
444 (sb!disassem:define-instruction-format (reg/mem 16
445 :default-printer '(:name :tab reg/mem))
446 (op :fields (list (byte 7 1) (byte 3 11)))
447 (width :field (byte 1 0) :type 'width)
448 (reg/mem :fields (list (byte 2 14) (byte 3 8))
449 :type 'sized-reg/mem)
453 ;;; Same as reg/mem, but with the immediate value occurring by default,
454 ;;; and with an appropiate printer.
455 (sb!disassem:define-instruction-format (reg/mem-imm 16
458 '(:name :tab reg/mem ", " imm))
459 (reg/mem :type 'sized-reg/mem)
460 (imm :type 'imm-data))
462 ;;; Same as reg/mem, but with using the accumulator in the default printer
463 (sb!disassem:define-instruction-format
465 :include 'reg/mem :default-printer '(:name :tab accum ", " reg/mem))
466 (reg/mem :type 'reg/mem) ; don't need a size
467 (accum :type 'accum))
469 ;;; Same as reg-reg/mem, but with a prefix of #b00001111
470 (sb!disassem:define-instruction-format (ext-reg-reg/mem 24
472 `(:name :tab reg ", " reg/mem))
473 (prefix :field (byte 8 0) :value #b00001111)
474 (op :field (byte 7 9))
475 (width :field (byte 1 8) :type 'width)
476 (reg/mem :fields (list (byte 2 22) (byte 3 16))
478 (reg :field (byte 3 19) :type 'reg)
482 ;;; Same as reg/mem, but with a prefix of #b00001111
483 (sb!disassem:define-instruction-format (ext-reg/mem 24
484 :default-printer '(:name :tab reg/mem))
485 (prefix :field (byte 8 0) :value #b00001111)
486 (op :fields (list (byte 7 9) (byte 3 19)))
487 (width :field (byte 1 8) :type 'width)
488 (reg/mem :fields (list (byte 2 22) (byte 3 16))
489 :type 'sized-reg/mem)
493 (sb!disassem:define-instruction-format (ext-reg/mem-imm 24
494 :include 'ext-reg/mem
496 '(:name :tab reg/mem ", " imm))
497 (imm :type 'imm-data))
499 ;;;; This section was added by jrd, for fp instructions.
501 ;;; regular fp inst to/from registers/memory
502 (sb!disassem:define-instruction-format (floating-point 16
504 `(:name :tab reg/mem))
505 (prefix :field (byte 5 3) :value #b11011)
506 (op :fields (list (byte 3 0) (byte 3 11)))
507 (reg/mem :fields (list (byte 2 14) (byte 3 8)) :type 'reg/mem))
509 ;;; fp insn to/from fp reg
510 (sb!disassem:define-instruction-format (floating-point-fp 16
511 :default-printer `(:name :tab fp-reg))
512 (prefix :field (byte 5 3) :value #b11011)
513 (suffix :field (byte 2 14) :value #b11)
514 (op :fields (list (byte 3 0) (byte 3 11)))
515 (fp-reg :field (byte 3 8) :type 'fp-reg))
517 ;;; fp insn to/from fp reg, with the reversed source/destination flag.
518 (sb!disassem:define-instruction-format
519 (floating-point-fp-d 16
520 :default-printer `(:name :tab ,(swap-if 'd "ST0" ", " 'fp-reg)))
521 (prefix :field (byte 5 3) :value #b11011)
522 (suffix :field (byte 2 14) :value #b11)
523 (op :fields (list (byte 2 0) (byte 3 11)))
524 (d :field (byte 1 2))
525 (fp-reg :field (byte 3 8) :type 'fp-reg))
528 ;;; (added by (?) pfw)
529 ;;; fp no operand isns
530 (sb!disassem:define-instruction-format (floating-point-no 16
531 :default-printer '(:name))
532 (prefix :field (byte 8 0) :value #b11011001)
533 (suffix :field (byte 3 13) :value #b111)
534 (op :field (byte 5 8)))
536 (sb!disassem:define-instruction-format (floating-point-3 16
537 :default-printer '(:name))
538 (prefix :field (byte 5 3) :value #b11011)
539 (suffix :field (byte 2 14) :value #b11)
540 (op :fields (list (byte 3 0) (byte 6 8))))
542 (sb!disassem:define-instruction-format (floating-point-5 16
543 :default-printer '(:name))
544 (prefix :field (byte 8 0) :value #b11011011)
545 (suffix :field (byte 3 13) :value #b111)
546 (op :field (byte 5 8)))
548 (sb!disassem:define-instruction-format (floating-point-st 16
549 :default-printer '(:name))
550 (prefix :field (byte 8 0) :value #b11011111)
551 (suffix :field (byte 3 13) :value #b111)
552 (op :field (byte 5 8)))
554 (sb!disassem:define-instruction-format (string-op 8
556 :default-printer '(:name width)))
558 (sb!disassem:define-instruction-format (short-cond-jump 16)
559 (op :field (byte 4 4))
560 (cc :field (byte 4 0) :type 'condition-code)
561 (label :field (byte 8 8) :type 'displacement))
563 (sb!disassem:define-instruction-format (short-jump 16
564 :default-printer '(:name :tab label))
565 (const :field (byte 4 4) :value #b1110)
566 (op :field (byte 4 0))
567 (label :field (byte 8 8) :type 'displacement))
569 (sb!disassem:define-instruction-format (near-cond-jump 16)
570 (op :fields (list (byte 8 0) (byte 4 12)) :value '(#b00001111 #b1000))
571 (cc :field (byte 4 8) :type 'condition-code)
572 ;; The disassembler currently doesn't let you have an instruction > 32 bits
573 ;; long, so we fake it by using a prefilter to read the offset.
574 (label :type 'displacement
575 :prefilter (lambda (value dstate)
576 (declare (ignore value)) ; always nil anyway
577 (sb!disassem:read-signed-suffix 32 dstate))))
579 (sb!disassem:define-instruction-format (near-jump 8
580 :default-printer '(:name :tab label))
581 (op :field (byte 8 0))
582 ;; The disassembler currently doesn't let you have an instruction > 32 bits
583 ;; long, so we fake it by using a prefilter to read the address.
584 (label :type 'displacement
585 :prefilter (lambda (value dstate)
586 (declare (ignore value)) ; always nil anyway
587 (sb!disassem:read-signed-suffix 32 dstate))))
590 (sb!disassem:define-instruction-format (cond-set 24
591 :default-printer '('set cc :tab reg/mem))
592 (prefix :field (byte 8 0) :value #b00001111)
593 (op :field (byte 4 12) :value #b1001)
594 (cc :field (byte 4 8) :type 'condition-code)
595 (reg/mem :fields (list (byte 2 22) (byte 3 16))
597 (reg :field (byte 3 19) :value #b000))
599 (sb!disassem:define-instruction-format (cond-move 24
601 '('cmov cc :tab reg ", " reg/mem))
602 (prefix :field (byte 8 0) :value #b00001111)
603 (op :field (byte 4 12) :value #b0100)
604 (cc :field (byte 4 8) :type 'condition-code)
605 (reg/mem :fields (list (byte 2 22) (byte 3 16))
607 (reg :field (byte 3 19) :type 'reg))
609 (sb!disassem:define-instruction-format (enter-format 32
610 :default-printer '(:name
612 (:unless (:constant 0)
614 (op :field (byte 8 0))
615 (disp :field (byte 16 8))
616 (level :field (byte 8 24)))
618 (sb!disassem:define-instruction-format (prefetch 24
620 '(:name ", " reg/mem))
621 (prefix :field (byte 8 0) :value #b00001111)
622 (op :field (byte 8 8) :value #b00011000)
623 (reg/mem :fields (list (byte 2 22) (byte 3 16)) :type 'byte-reg/mem)
624 (reg :field (byte 3 19) :type 'reg))
626 ;;; Single byte instruction with an immediate byte argument.
627 (sb!disassem:define-instruction-format (byte-imm 16
628 :default-printer '(:name :tab code))
629 (op :field (byte 8 0))
630 (code :field (byte 8 8)))
632 ;;;; primitive emitters
634 (define-bitfield-emitter emit-word 16
637 (define-bitfield-emitter emit-dword 32
640 (define-bitfield-emitter emit-byte-with-reg 8
641 (byte 5 3) (byte 3 0))
643 (define-bitfield-emitter emit-mod-reg-r/m-byte 8
644 (byte 2 6) (byte 3 3) (byte 3 0))
646 (define-bitfield-emitter emit-sib-byte 8
647 (byte 2 6) (byte 3 3) (byte 3 0))
651 (defun emit-absolute-fixup (segment fixup)
652 (note-fixup segment :absolute fixup)
653 (let ((offset (fixup-offset fixup)))
655 (emit-back-patch segment
656 4 ; FIXME: n-word-bytes
657 (lambda (segment posn)
658 (declare (ignore posn))
660 (- (+ (component-header-length)
661 (or (label-position offset)
663 other-pointer-lowtag))))
664 (emit-dword segment (or offset 0)))))
666 (defun emit-relative-fixup (segment fixup)
667 (note-fixup segment :relative fixup)
668 (emit-dword segment (or (fixup-offset fixup) 0)))
670 ;;;; the effective-address (ea) structure
672 (defun reg-tn-encoding (tn)
673 (declare (type tn tn))
674 (aver (eq (sb-name (sc-sb (tn-sc tn))) 'registers))
675 (let ((offset (tn-offset tn)))
676 (logior (ash (logand offset 1) 2)
679 (defstruct (ea (:constructor make-ea (size &key base index scale disp))
681 (size nil :type (member :byte :word :dword))
682 (base nil :type (or tn null))
683 (index nil :type (or tn null))
684 (scale 1 :type (member 1 2 4 8))
685 (disp 0 :type (or (unsigned-byte 32) (signed-byte 32) fixup)))
686 (def!method print-object ((ea ea) stream)
687 (cond ((or *print-escape* *print-readably*)
688 (print-unreadable-object (ea stream :type t)
690 "~S~@[ base=~S~]~@[ index=~S~]~@[ scale=~S~]~@[ disp=~S~]"
694 (let ((scale (ea-scale ea)))
695 (if (= scale 1) nil scale))
698 (format stream "~A PTR [" (symbol-name (ea-size ea)))
700 (write-string (sb!c::location-print-name (ea-base ea)) stream)
702 (write-string "+" stream)))
704 (write-string (sb!c::location-print-name (ea-index ea)) stream))
705 (unless (= (ea-scale ea) 1)
706 (format stream "*~A" (ea-scale ea)))
707 (typecase (ea-disp ea)
710 (format stream "~@D" (ea-disp ea)))
712 (format stream "+~A" (ea-disp ea))))
713 (write-char #\] stream))))
715 (defun emit-ea (segment thing reg &optional allow-constants)
718 (ecase (sb-name (sc-sb (tn-sc thing)))
720 (emit-mod-reg-r/m-byte segment #b11 reg (reg-tn-encoding thing)))
722 ;; Convert stack tns into an index off of EBP.
723 (let ((disp (- (* (1+ (tn-offset thing)) n-word-bytes))))
724 (cond ((< -128 disp 127)
725 (emit-mod-reg-r/m-byte segment #b01 reg #b101)
726 (emit-byte segment disp))
728 (emit-mod-reg-r/m-byte segment #b10 reg #b101)
729 (emit-dword segment disp)))))
731 (unless allow-constants
733 "Constant TNs can only be directly used in MOV, PUSH, and CMP."))
734 (emit-mod-reg-r/m-byte segment #b00 reg #b101)
735 (emit-absolute-fixup segment
738 (- (* (tn-offset thing) n-word-bytes)
739 other-pointer-lowtag))))))
741 (let* ((base (ea-base thing))
742 (index (ea-index thing))
743 (scale (ea-scale thing))
744 (disp (ea-disp thing))
745 (mod (cond ((or (null base)
747 (not (= (reg-tn-encoding base) #b101))))
749 ((and (fixnump disp) (<= -128 disp 127))
753 (r/m (cond (index #b100)
755 (t (reg-tn-encoding base)))))
756 (emit-mod-reg-r/m-byte segment mod reg r/m)
758 (let ((ss (1- (integer-length scale)))
759 (index (if (null index)
761 (let ((index (reg-tn-encoding index)))
763 (error "can't index off of ESP")
765 (base (if (null base)
767 (reg-tn-encoding base))))
768 (emit-sib-byte segment ss index base)))
770 (emit-byte segment disp))
771 ((or (= mod #b10) (null base))
773 (emit-absolute-fixup segment disp)
774 (emit-dword segment disp))))))
776 (emit-mod-reg-r/m-byte segment #b00 reg #b101)
777 (emit-absolute-fixup segment thing))))
779 (defun fp-reg-tn-p (thing)
781 (eq (sb-name (sc-sb (tn-sc thing))) 'float-registers)))
783 ;;; like the above, but for fp-instructions--jrd
784 (defun emit-fp-op (segment thing op)
785 (if (fp-reg-tn-p thing)
786 (emit-byte segment (dpb op (byte 3 3) (dpb (tn-offset thing)
789 (emit-ea segment thing op)))
791 (defun byte-reg-p (thing)
793 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)
794 (member (sc-name (tn-sc thing)) *byte-sc-names*)
797 (defun byte-ea-p (thing)
799 (ea (eq (ea-size thing) :byte))
801 (and (member (sc-name (tn-sc thing)) *byte-sc-names*) t))
804 (defun word-reg-p (thing)
806 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)
807 (member (sc-name (tn-sc thing)) *word-sc-names*)
810 (defun word-ea-p (thing)
812 (ea (eq (ea-size thing) :word))
813 (tn (and (member (sc-name (tn-sc thing)) *word-sc-names*) t))
816 (defun dword-reg-p (thing)
818 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)
819 (member (sc-name (tn-sc thing)) *dword-sc-names*)
822 (defun dword-ea-p (thing)
824 (ea (eq (ea-size thing) :dword))
826 (and (member (sc-name (tn-sc thing)) *dword-sc-names*) t))
829 (defun register-p (thing)
831 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)))
833 (defun accumulator-p (thing)
834 (and (register-p thing)
835 (= (tn-offset thing) 0)))
839 (def!constant +operand-size-prefix-byte+ #b01100110)
841 (defun maybe-emit-operand-size-prefix (segment size)
842 (unless (or (eq size :byte) (eq size +default-operand-size+))
843 (emit-byte segment +operand-size-prefix-byte+)))
845 (defun operand-size (thing)
848 ;; FIXME: might as well be COND instead of having to use #. readmacro
849 ;; to hack up the code
850 (case (sc-name (tn-sc thing))
857 ;; added by jrd: float-registers is a separate size (?)
863 (error "can't tell the size of ~S ~S" thing (sc-name (tn-sc thing))))))
869 (defun matching-operand-size (dst src)
870 (let ((dst-size (operand-size dst))
871 (src-size (operand-size src)))
874 (if (eq dst-size src-size)
876 (error "size mismatch: ~S is a ~S and ~S is a ~S."
877 dst dst-size src src-size))
881 (error "can't tell the size of either ~S or ~S" dst src)))))
883 (defun emit-sized-immediate (segment size value)
886 (emit-byte segment value))
888 (emit-word segment value))
890 (emit-dword segment value))))
892 ;;;; general data transfer
894 (define-instruction mov (segment dst src)
895 ;; immediate to register
896 (:printer reg ((op #b1011) (imm nil :type 'imm-data))
897 '(:name :tab reg ", " imm))
898 ;; absolute mem to/from accumulator
899 (:printer simple-dir ((op #b101000) (imm nil :type 'imm-addr))
900 `(:name :tab ,(swap-if 'dir 'accum ", " '("[" imm "]"))))
901 ;; register to/from register/memory
902 (:printer reg-reg/mem-dir ((op #b100010)))
903 ;; immediate to register/memory
904 (:printer reg/mem-imm ((op '(#b1100011 #b000))))
907 (let ((size (matching-operand-size dst src)))
908 (maybe-emit-operand-size-prefix segment size)
909 (cond ((register-p dst)
910 (cond ((integerp src)
911 (emit-byte-with-reg segment
915 (reg-tn-encoding dst))
916 (emit-sized-immediate segment size src))
917 ((and (fixup-p src) (accumulator-p dst))
922 (emit-absolute-fixup segment src))
928 (emit-ea segment src (reg-tn-encoding dst) t))))
929 ((and (fixup-p dst) (accumulator-p src))
930 (emit-byte segment (if (eq size :byte) #b10100010 #b10100011))
931 (emit-absolute-fixup segment dst))
933 (emit-byte segment (if (eq size :byte) #b11000110 #b11000111))
934 (emit-ea segment dst #b000)
935 (emit-sized-immediate segment size src))
937 (emit-byte segment (if (eq size :byte) #b10001000 #b10001001))
938 (emit-ea segment dst (reg-tn-encoding src)))
940 (aver (eq size :dword))
941 (emit-byte segment #b11000111)
942 (emit-ea segment dst #b000)
943 (emit-absolute-fixup segment src))
945 (error "bogus arguments to MOV: ~S ~S" dst src))))))
947 (defun emit-move-with-extension (segment dst src opcode)
948 (aver (register-p dst))
949 (let ((dst-size (operand-size dst))
950 (src-size (operand-size src)))
953 (aver (eq src-size :byte))
954 (maybe-emit-operand-size-prefix segment :word)
955 (emit-byte segment #b00001111)
956 (emit-byte segment opcode)
957 (emit-ea segment src (reg-tn-encoding dst)))
961 (maybe-emit-operand-size-prefix segment :dword)
962 (emit-byte segment #b00001111)
963 (emit-byte segment opcode)
964 (emit-ea segment src (reg-tn-encoding dst)))
966 (emit-byte segment #b00001111)
967 (emit-byte segment (logior opcode 1))
968 (emit-ea segment src (reg-tn-encoding dst))))))))
970 (define-instruction movsx (segment dst src)
971 (:printer ext-reg-reg/mem ((op #b1011111) (reg nil :type 'word-reg)))
972 (:emitter (emit-move-with-extension segment dst src #b10111110)))
974 (define-instruction movzx (segment dst src)
975 (:printer ext-reg-reg/mem ((op #b1011011) (reg nil :type 'word-reg)))
976 (:emitter (emit-move-with-extension segment dst src #b10110110)))
978 (define-instruction push (segment src)
980 (:printer reg-no-width ((op #b01010)))
982 (:printer reg/mem ((op '(#b1111111 #b110)) (width 1)))
984 (:printer byte ((op #b01101010) (imm nil :type 'signed-imm-byte))
986 (:printer byte ((op #b01101000) (imm nil :type 'imm-word))
988 ;; ### segment registers?
991 (cond ((integerp src)
992 (cond ((<= -128 src 127)
993 (emit-byte segment #b01101010)
994 (emit-byte segment src))
996 (emit-byte segment #b01101000)
997 (emit-dword segment src))))
999 ;; Interpret the fixup as an immediate dword to push.
1000 (emit-byte segment #b01101000)
1001 (emit-absolute-fixup segment src))
1003 (let ((size (operand-size src)))
1004 (aver (not (eq size :byte)))
1005 (maybe-emit-operand-size-prefix segment size)
1006 (cond ((register-p src)
1007 (emit-byte-with-reg segment #b01010 (reg-tn-encoding src)))
1009 (emit-byte segment #b11111111)
1010 (emit-ea segment src #b110 t))))))))
1012 (define-instruction pusha (segment)
1013 (:printer byte ((op #b01100000)))
1015 (emit-byte segment #b01100000)))
1017 (define-instruction pop (segment dst)
1018 (:printer reg-no-width ((op #b01011)))
1019 (:printer reg/mem ((op '(#b1000111 #b000)) (width 1)))
1021 (let ((size (operand-size dst)))
1022 (aver (not (eq size :byte)))
1023 (maybe-emit-operand-size-prefix segment size)
1024 (cond ((register-p dst)
1025 (emit-byte-with-reg segment #b01011 (reg-tn-encoding dst)))
1027 (emit-byte segment #b10001111)
1028 (emit-ea segment dst #b000))))))
1030 (define-instruction popa (segment)
1031 (:printer byte ((op #b01100001)))
1033 (emit-byte segment #b01100001)))
1035 (define-instruction xchg (segment operand1 operand2)
1036 ;; Register with accumulator.
1037 (:printer reg-no-width ((op #b10010)) '(:name :tab accum ", " reg))
1038 ;; Register/Memory with Register.
1039 (:printer reg-reg/mem ((op #b1000011)))
1041 (let ((size (matching-operand-size operand1 operand2)))
1042 (maybe-emit-operand-size-prefix segment size)
1043 (labels ((xchg-acc-with-something (acc something)
1044 (if (and (not (eq size :byte)) (register-p something))
1045 (emit-byte-with-reg segment
1047 (reg-tn-encoding something))
1048 (xchg-reg-with-something acc something)))
1049 (xchg-reg-with-something (reg something)
1050 (emit-byte segment (if (eq size :byte) #b10000110 #b10000111))
1051 (emit-ea segment something (reg-tn-encoding reg))))
1052 (cond ((accumulator-p operand1)
1053 (xchg-acc-with-something operand1 operand2))
1054 ((accumulator-p operand2)
1055 (xchg-acc-with-something operand2 operand1))
1056 ((register-p operand1)
1057 (xchg-reg-with-something operand1 operand2))
1058 ((register-p operand2)
1059 (xchg-reg-with-something operand2 operand1))
1061 (error "bogus args to XCHG: ~S ~S" operand1 operand2)))))))
1063 (define-instruction lea (segment dst src)
1064 (:printer reg-reg/mem ((op #b1000110) (width 1)))
1066 (aver (dword-reg-p dst))
1067 (emit-byte segment #b10001101)
1068 (emit-ea segment src (reg-tn-encoding dst))))
1070 (define-instruction cmpxchg (segment dst src)
1071 ;; Register/Memory with Register.
1072 (:printer ext-reg-reg/mem ((op #b1011000)) '(:name :tab reg/mem ", " reg))
1074 (aver (register-p src))
1075 (let ((size (matching-operand-size src dst)))
1076 (maybe-emit-operand-size-prefix segment size)
1077 (emit-byte segment #b00001111)
1078 (emit-byte segment (if (eq size :byte) #b10110000 #b10110001))
1079 (emit-ea segment dst (reg-tn-encoding src)))))
1083 (define-instruction fs-segment-prefix (segment)
1085 (emit-byte segment #x64)))
1087 ;;;; flag control instructions
1089 ;;; CLC -- Clear Carry Flag.
1090 (define-instruction clc (segment)
1091 (:printer byte ((op #b11111000)))
1093 (emit-byte segment #b11111000)))
1095 ;;; CLD -- Clear Direction Flag.
1096 (define-instruction cld (segment)
1097 (:printer byte ((op #b11111100)))
1099 (emit-byte segment #b11111100)))
1101 ;;; CLI -- Clear Iterrupt Enable Flag.
1102 (define-instruction cli (segment)
1103 (:printer byte ((op #b11111010)))
1105 (emit-byte segment #b11111010)))
1107 ;;; CMC -- Complement Carry Flag.
1108 (define-instruction cmc (segment)
1109 (:printer byte ((op #b11110101)))
1111 (emit-byte segment #b11110101)))
1113 ;;; LAHF -- Load AH into flags.
1114 (define-instruction lahf (segment)
1115 (:printer byte ((op #b10011111)))
1117 (emit-byte segment #b10011111)))
1119 ;;; POPF -- Pop flags.
1120 (define-instruction popf (segment)
1121 (:printer byte ((op #b10011101)))
1123 (emit-byte segment #b10011101)))
1125 ;;; PUSHF -- push flags.
1126 (define-instruction pushf (segment)
1127 (:printer byte ((op #b10011100)))
1129 (emit-byte segment #b10011100)))
1131 ;;; SAHF -- Store AH into flags.
1132 (define-instruction sahf (segment)
1133 (:printer byte ((op #b10011110)))
1135 (emit-byte segment #b10011110)))
1137 ;;; STC -- Set Carry Flag.
1138 (define-instruction stc (segment)
1139 (:printer byte ((op #b11111001)))
1141 (emit-byte segment #b11111001)))
1143 ;;; STD -- Set Direction Flag.
1144 (define-instruction std (segment)
1145 (:printer byte ((op #b11111101)))
1147 (emit-byte segment #b11111101)))
1149 ;;; STI -- Set Interrupt Enable Flag.
1150 (define-instruction sti (segment)
1151 (:printer byte ((op #b11111011)))
1153 (emit-byte segment #b11111011)))
1157 (defun emit-random-arith-inst (name segment dst src opcode
1158 &optional allow-constants)
1159 (let ((size (matching-operand-size dst src)))
1160 (maybe-emit-operand-size-prefix segment size)
1163 (cond ((and (not (eq size :byte)) (<= -128 src 127))
1164 (emit-byte segment #b10000011)
1165 (emit-ea segment dst opcode allow-constants)
1166 (emit-byte segment src))
1167 ((accumulator-p dst)
1174 (emit-sized-immediate segment size src))
1176 (emit-byte segment (if (eq size :byte) #b10000000 #b10000001))
1177 (emit-ea segment dst opcode allow-constants)
1178 (emit-sized-immediate segment size src))))
1183 (if (eq size :byte) #b00000000 #b00000001)))
1184 (emit-ea segment dst (reg-tn-encoding src) allow-constants))
1189 (if (eq size :byte) #b00000010 #b00000011)))
1190 (emit-ea segment src (reg-tn-encoding dst) allow-constants))
1192 (error "bogus operands to ~A" name)))))
1194 (eval-when (:compile-toplevel :execute)
1195 (defun arith-inst-printer-list (subop)
1196 `((accum-imm ((op ,(dpb subop (byte 3 2) #b0000010))))
1197 (reg/mem-imm ((op (#b1000000 ,subop))))
1198 (reg/mem-imm ((op (#b1000001 ,subop))
1199 (imm nil :type signed-imm-byte)))
1200 (reg-reg/mem-dir ((op ,(dpb subop (byte 3 1) #b000000))))))
1203 (define-instruction add (segment dst src)
1204 (:printer-list (arith-inst-printer-list #b000))
1205 (:emitter (emit-random-arith-inst "ADD" segment dst src #b000)))
1207 (define-instruction adc (segment dst src)
1208 (:printer-list (arith-inst-printer-list #b010))
1209 (:emitter (emit-random-arith-inst "ADC" segment dst src #b010)))
1211 (define-instruction sub (segment dst src)
1212 (:printer-list (arith-inst-printer-list #b101))
1213 (:emitter (emit-random-arith-inst "SUB" segment dst src #b101)))
1215 (define-instruction sbb (segment dst src)
1216 (:printer-list (arith-inst-printer-list #b011))
1217 (:emitter (emit-random-arith-inst "SBB" segment dst src #b011)))
1219 (define-instruction cmp (segment dst src)
1220 (:printer-list (arith-inst-printer-list #b111))
1221 (:emitter (emit-random-arith-inst "CMP" segment dst src #b111 t)))
1223 (define-instruction inc (segment dst)
1225 (:printer reg-no-width ((op #b01000)))
1227 (:printer reg/mem ((op '(#b1111111 #b000))))
1229 (let ((size (operand-size dst)))
1230 (maybe-emit-operand-size-prefix segment size)
1231 (cond ((and (not (eq size :byte)) (register-p dst))
1232 (emit-byte-with-reg segment #b01000 (reg-tn-encoding dst)))
1234 (emit-byte segment (if (eq size :byte) #b11111110 #b11111111))
1235 (emit-ea segment dst #b000))))))
1237 (define-instruction dec (segment dst)
1239 (:printer reg-no-width ((op #b01001)))
1241 (:printer reg/mem ((op '(#b1111111 #b001))))
1243 (let ((size (operand-size dst)))
1244 (maybe-emit-operand-size-prefix segment size)
1245 (cond ((and (not (eq size :byte)) (register-p dst))
1246 (emit-byte-with-reg segment #b01001 (reg-tn-encoding dst)))
1248 (emit-byte segment (if (eq size :byte) #b11111110 #b11111111))
1249 (emit-ea segment dst #b001))))))
1251 (define-instruction neg (segment dst)
1252 (:printer reg/mem ((op '(#b1111011 #b011))))
1254 (let ((size (operand-size dst)))
1255 (maybe-emit-operand-size-prefix segment size)
1256 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
1257 (emit-ea segment dst #b011))))
1259 (define-instruction aaa (segment)
1260 (:printer byte ((op #b00110111)))
1262 (emit-byte segment #b00110111)))
1264 (define-instruction aas (segment)
1265 (:printer byte ((op #b00111111)))
1267 (emit-byte segment #b00111111)))
1269 (define-instruction daa (segment)
1270 (:printer byte ((op #b00100111)))
1272 (emit-byte segment #b00100111)))
1274 (define-instruction das (segment)
1275 (:printer byte ((op #b00101111)))
1277 (emit-byte segment #b00101111)))
1279 (define-instruction mul (segment dst src)
1280 (:printer accum-reg/mem ((op '(#b1111011 #b100))))
1282 (let ((size (matching-operand-size dst src)))
1283 (aver (accumulator-p dst))
1284 (maybe-emit-operand-size-prefix segment size)
1285 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
1286 (emit-ea segment src #b100))))
1288 (define-instruction imul (segment dst &optional src1 src2)
1289 (:printer accum-reg/mem ((op '(#b1111011 #b101))))
1290 (:printer ext-reg-reg/mem ((op #b1010111)))
1291 (:printer reg-reg/mem ((op #b0110100) (width 1)
1292 (imm nil :type 'signed-imm-word))
1293 '(:name :tab reg ", " reg/mem ", " imm))
1294 (:printer reg-reg/mem ((op #b0110101) (width 1)
1295 (imm nil :type 'signed-imm-byte))
1296 '(:name :tab reg ", " reg/mem ", " imm))
1298 (flet ((r/m-with-immed-to-reg (reg r/m immed)
1299 (let* ((size (matching-operand-size reg r/m))
1300 (sx (and (not (eq size :byte)) (<= -128 immed 127))))
1301 (maybe-emit-operand-size-prefix segment size)
1302 (emit-byte segment (if sx #b01101011 #b01101001))
1303 (emit-ea segment r/m (reg-tn-encoding reg))
1305 (emit-byte segment immed)
1306 (emit-sized-immediate segment size immed)))))
1308 (r/m-with-immed-to-reg dst src1 src2))
1311 (r/m-with-immed-to-reg dst dst src1)
1312 (let ((size (matching-operand-size dst src1)))
1313 (maybe-emit-operand-size-prefix segment size)
1314 (emit-byte segment #b00001111)
1315 (emit-byte segment #b10101111)
1316 (emit-ea segment src1 (reg-tn-encoding dst)))))
1318 (let ((size (operand-size dst)))
1319 (maybe-emit-operand-size-prefix segment size)
1320 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
1321 (emit-ea segment dst #b101)))))))
1323 (define-instruction div (segment dst src)
1324 (:printer accum-reg/mem ((op '(#b1111011 #b110))))
1326 (let ((size (matching-operand-size dst src)))
1327 (aver (accumulator-p dst))
1328 (maybe-emit-operand-size-prefix segment size)
1329 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
1330 (emit-ea segment src #b110))))
1332 (define-instruction idiv (segment dst src)
1333 (:printer accum-reg/mem ((op '(#b1111011 #b111))))
1335 (let ((size (matching-operand-size dst src)))
1336 (aver (accumulator-p dst))
1337 (maybe-emit-operand-size-prefix segment size)
1338 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
1339 (emit-ea segment src #b111))))
1341 (define-instruction aad (segment)
1342 (:printer two-bytes ((op '(#b11010101 #b00001010))))
1344 (emit-byte segment #b11010101)
1345 (emit-byte segment #b00001010)))
1347 (define-instruction aam (segment)
1348 (:printer two-bytes ((op '(#b11010100 #b00001010))))
1350 (emit-byte segment #b11010100)
1351 (emit-byte segment #b00001010)))
1353 ;;; CBW -- Convert Byte to Word. AX <- sign_xtnd(AL)
1354 (define-instruction cbw (segment)
1356 (maybe-emit-operand-size-prefix segment :word)
1357 (emit-byte segment #b10011000)))
1359 ;;; CWDE -- Convert Word To Double Word Extened. EAX <- sign_xtnd(AX)
1360 (define-instruction cwde (segment)
1362 (maybe-emit-operand-size-prefix segment :dword)
1363 (emit-byte segment #b10011000)))
1365 ;;; CWD -- Convert Word to Double Word. DX:AX <- sign_xtnd(AX)
1366 (define-instruction cwd (segment)
1368 (maybe-emit-operand-size-prefix segment :word)
1369 (emit-byte segment #b10011001)))
1371 ;;; CDQ -- Convert Double Word to Quad Word. EDX:EAX <- sign_xtnd(EAX)
1372 (define-instruction cdq (segment)
1373 (:printer byte ((op #b10011001)))
1375 (maybe-emit-operand-size-prefix segment :dword)
1376 (emit-byte segment #b10011001)))
1378 (define-instruction xadd (segment dst src)
1379 ;; Register/Memory with Register.
1380 (:printer ext-reg-reg/mem ((op #b1100000)) '(:name :tab reg/mem ", " reg))
1382 (aver (register-p src))
1383 (let ((size (matching-operand-size src dst)))
1384 (maybe-emit-operand-size-prefix segment size)
1385 (emit-byte segment #b00001111)
1386 (emit-byte segment (if (eq size :byte) #b11000000 #b11000001))
1387 (emit-ea segment dst (reg-tn-encoding src)))))
1392 (defun emit-shift-inst (segment dst amount opcode)
1393 (let ((size (operand-size dst)))
1394 (maybe-emit-operand-size-prefix segment size)
1395 (multiple-value-bind (major-opcode immed)
1397 (:cl (values #b11010010 nil))
1398 (1 (values #b11010000 nil))
1399 (t (values #b11000000 t)))
1401 (if (eq size :byte) major-opcode (logior major-opcode 1)))
1402 (emit-ea segment dst opcode)
1404 (emit-byte segment amount)))))
1406 (eval-when (:compile-toplevel :execute)
1407 (defun shift-inst-printer-list (subop)
1408 `((reg/mem ((op (#b1101000 ,subop)))
1409 (:name :tab reg/mem ", 1"))
1410 (reg/mem ((op (#b1101001 ,subop)))
1411 (:name :tab reg/mem ", " 'cl))
1412 (reg/mem-imm ((op (#b1100000 ,subop))
1413 (imm nil :type signed-imm-byte))))))
1415 (define-instruction rol (segment dst amount)
1417 (shift-inst-printer-list #b000))
1419 (emit-shift-inst segment dst amount #b000)))
1421 (define-instruction ror (segment dst amount)
1423 (shift-inst-printer-list #b001))
1425 (emit-shift-inst segment dst amount #b001)))
1427 (define-instruction rcl (segment dst amount)
1429 (shift-inst-printer-list #b010))
1431 (emit-shift-inst segment dst amount #b010)))
1433 (define-instruction rcr (segment dst amount)
1435 (shift-inst-printer-list #b011))
1437 (emit-shift-inst segment dst amount #b011)))
1439 (define-instruction shl (segment dst amount)
1441 (shift-inst-printer-list #b100))
1443 (emit-shift-inst segment dst amount #b100)))
1445 (define-instruction shr (segment dst amount)
1447 (shift-inst-printer-list #b101))
1449 (emit-shift-inst segment dst amount #b101)))
1451 (define-instruction sar (segment dst amount)
1453 (shift-inst-printer-list #b111))
1455 (emit-shift-inst segment dst amount #b111)))
1457 (defun emit-double-shift (segment opcode dst src amt)
1458 (let ((size (matching-operand-size dst src)))
1459 (when (eq size :byte)
1460 (error "Double shifts can only be used with words."))
1461 (maybe-emit-operand-size-prefix segment size)
1462 (emit-byte segment #b00001111)
1463 (emit-byte segment (dpb opcode (byte 1 3)
1464 (if (eq amt :cl) #b10100101 #b10100100)))
1466 (emit-ea segment dst src)
1467 (emit-ea segment dst (reg-tn-encoding src)) ; pw tries this
1468 (unless (eq amt :cl)
1469 (emit-byte segment amt))))
1471 (eval-when (:compile-toplevel :execute)
1472 (defun double-shift-inst-printer-list (op)
1474 (ext-reg-reg/mem-imm ((op ,(logior op #b100))
1475 (imm nil :type signed-imm-byte)))
1476 (ext-reg-reg/mem ((op ,(logior op #b101)))
1477 (:name :tab reg/mem ", " 'cl)))))
1479 (define-instruction shld (segment dst src amt)
1480 (:declare (type (or (member :cl) (mod 32)) amt))
1481 (:printer-list (double-shift-inst-printer-list #b10100000))
1483 (emit-double-shift segment #b0 dst src amt)))
1485 (define-instruction shrd (segment dst src amt)
1486 (:declare (type (or (member :cl) (mod 32)) amt))
1487 (:printer-list (double-shift-inst-printer-list #b10101000))
1489 (emit-double-shift segment #b1 dst src amt)))
1491 (define-instruction and (segment dst src)
1493 (arith-inst-printer-list #b100))
1495 (emit-random-arith-inst "AND" segment dst src #b100)))
1497 (define-instruction test (segment this that)
1498 (:printer accum-imm ((op #b1010100)))
1499 (:printer reg/mem-imm ((op '(#b1111011 #b000))))
1500 (:printer reg-reg/mem ((op #b1000010)))
1502 (let ((size (matching-operand-size this that)))
1503 (maybe-emit-operand-size-prefix segment size)
1504 (flet ((test-immed-and-something (immed something)
1505 (cond ((accumulator-p something)
1507 (if (eq size :byte) #b10101000 #b10101001))
1508 (emit-sized-immediate segment size immed))
1511 (if (eq size :byte) #b11110110 #b11110111))
1512 (emit-ea segment something #b000)
1513 (emit-sized-immediate segment size immed))))
1514 (test-reg-and-something (reg something)
1515 (emit-byte segment (if (eq size :byte) #b10000100 #b10000101))
1516 (emit-ea segment something (reg-tn-encoding reg))))
1517 (cond ((integerp that)
1518 (test-immed-and-something that this))
1520 (test-immed-and-something this that))
1522 (test-reg-and-something this that))
1524 (test-reg-and-something that this))
1526 (error "bogus operands for TEST: ~S and ~S" this that)))))))
1528 (define-instruction or (segment dst src)
1530 (arith-inst-printer-list #b001))
1532 (emit-random-arith-inst "OR" segment dst src #b001)))
1534 (define-instruction xor (segment dst src)
1536 (arith-inst-printer-list #b110))
1538 (emit-random-arith-inst "XOR" segment dst src #b110)))
1540 (define-instruction not (segment dst)
1541 (:printer reg/mem ((op '(#b1111011 #b010))))
1543 (let ((size (operand-size dst)))
1544 (maybe-emit-operand-size-prefix segment size)
1545 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
1546 (emit-ea segment dst #b010))))
1548 ;;;; string manipulation
1550 (define-instruction cmps (segment size)
1551 (:printer string-op ((op #b1010011)))
1553 (maybe-emit-operand-size-prefix segment size)
1554 (emit-byte segment (if (eq size :byte) #b10100110 #b10100111))))
1556 (define-instruction ins (segment acc)
1557 (:printer string-op ((op #b0110110)))
1559 (let ((size (operand-size acc)))
1560 (aver (accumulator-p acc))
1561 (maybe-emit-operand-size-prefix segment size)
1562 (emit-byte segment (if (eq size :byte) #b01101100 #b01101101)))))
1564 (define-instruction lods (segment acc)
1565 (:printer string-op ((op #b1010110)))
1567 (let ((size (operand-size acc)))
1568 (aver (accumulator-p acc))
1569 (maybe-emit-operand-size-prefix segment size)
1570 (emit-byte segment (if (eq size :byte) #b10101100 #b10101101)))))
1572 (define-instruction movs (segment size)
1573 (:printer string-op ((op #b1010010)))
1575 (maybe-emit-operand-size-prefix segment size)
1576 (emit-byte segment (if (eq size :byte) #b10100100 #b10100101))))
1578 (define-instruction outs (segment acc)
1579 (:printer string-op ((op #b0110111)))
1581 (let ((size (operand-size acc)))
1582 (aver (accumulator-p acc))
1583 (maybe-emit-operand-size-prefix segment size)
1584 (emit-byte segment (if (eq size :byte) #b01101110 #b01101111)))))
1586 (define-instruction scas (segment acc)
1587 (:printer string-op ((op #b1010111)))
1589 (let ((size (operand-size acc)))
1590 (aver (accumulator-p acc))
1591 (maybe-emit-operand-size-prefix segment size)
1592 (emit-byte segment (if (eq size :byte) #b10101110 #b10101111)))))
1594 (define-instruction stos (segment acc)
1595 (:printer string-op ((op #b1010101)))
1597 (let ((size (operand-size acc)))
1598 (aver (accumulator-p acc))
1599 (maybe-emit-operand-size-prefix segment size)
1600 (emit-byte segment (if (eq size :byte) #b10101010 #b10101011)))))
1602 (define-instruction xlat (segment)
1603 (:printer byte ((op #b11010111)))
1605 (emit-byte segment #b11010111)))
1607 (define-instruction rep (segment)
1609 (emit-byte segment #b11110010)))
1611 (define-instruction repe (segment)
1612 (:printer byte ((op #b11110011)))
1614 (emit-byte segment #b11110011)))
1616 (define-instruction repne (segment)
1617 (:printer byte ((op #b11110010)))
1619 (emit-byte segment #b11110010)))
1622 ;;;; bit manipulation
1624 (define-instruction bsf (segment dst src)
1625 (:printer ext-reg-reg/mem ((op #b1011110) (width 0)))
1627 (let ((size (matching-operand-size dst src)))
1628 (when (eq size :byte)
1629 (error "can't scan bytes: ~S" src))
1630 (maybe-emit-operand-size-prefix segment size)
1631 (emit-byte segment #b00001111)
1632 (emit-byte segment #b10111100)
1633 (emit-ea segment src (reg-tn-encoding dst)))))
1635 (define-instruction bsr (segment dst src)
1636 (:printer ext-reg-reg/mem ((op #b1011110) (width 1)))
1638 (let ((size (matching-operand-size dst src)))
1639 (when (eq size :byte)
1640 (error "can't scan bytes: ~S" src))
1641 (maybe-emit-operand-size-prefix segment size)
1642 (emit-byte segment #b00001111)
1643 (emit-byte segment #b10111101)
1644 (emit-ea segment src (reg-tn-encoding dst)))))
1646 (defun emit-bit-test-and-mumble (segment src index opcode)
1647 (let ((size (operand-size src)))
1648 (when (eq size :byte)
1649 (error "can't scan bytes: ~S" src))
1650 (maybe-emit-operand-size-prefix segment size)
1651 (emit-byte segment #b00001111)
1652 (cond ((integerp index)
1653 (emit-byte segment #b10111010)
1654 (emit-ea segment src opcode)
1655 (emit-byte segment index))
1657 (emit-byte segment (dpb opcode (byte 3 3) #b10000011))
1658 (emit-ea segment src (reg-tn-encoding index))))))
1660 (eval-when (:compile-toplevel :execute)
1661 (defun bit-test-inst-printer-list (subop)
1662 `((ext-reg/mem-imm ((op (#b1011101 ,subop))
1663 (reg/mem nil :type word-reg/mem)
1664 (imm nil :type imm-data)
1666 (ext-reg-reg/mem ((op ,(dpb subop (byte 3 2) #b1000001))
1668 (:name :tab reg/mem ", " reg)))))
1670 (define-instruction bt (segment src index)
1671 (:printer-list (bit-test-inst-printer-list #b100))
1673 (emit-bit-test-and-mumble segment src index #b100)))
1675 (define-instruction btc (segment src index)
1676 (:printer-list (bit-test-inst-printer-list #b111))
1678 (emit-bit-test-and-mumble segment src index #b111)))
1680 (define-instruction btr (segment src index)
1681 (:printer-list (bit-test-inst-printer-list #b110))
1683 (emit-bit-test-and-mumble segment src index #b110)))
1685 (define-instruction bts (segment src index)
1686 (:printer-list (bit-test-inst-printer-list #b101))
1688 (emit-bit-test-and-mumble segment src index #b101)))
1691 ;;;; control transfer
1693 (define-instruction call (segment where)
1694 (:printer near-jump ((op #b11101000)))
1695 (:printer reg/mem ((op '(#b1111111 #b010)) (width 1)))
1699 (emit-byte segment #b11101000)
1700 (emit-back-patch segment
1702 (lambda (segment posn)
1704 (- (label-position where)
1707 (emit-byte segment #b11101000)
1708 (emit-relative-fixup segment where))
1710 (emit-byte segment #b11111111)
1711 (emit-ea segment where #b010)))))
1713 (defun emit-byte-displacement-backpatch (segment target)
1714 (emit-back-patch segment
1716 (lambda (segment posn)
1717 (let ((disp (- (label-position target) (1+ posn))))
1718 (aver (<= -128 disp 127))
1719 (emit-byte segment disp)))))
1721 (define-instruction jmp (segment cond &optional where)
1722 ;; conditional jumps
1723 (:printer short-cond-jump ((op #b0111)) '('j cc :tab label))
1724 (:printer near-cond-jump () '('j cc :tab label))
1725 ;; unconditional jumps
1726 (:printer short-jump ((op #b1011)))
1727 (:printer near-jump ((op #b11101001)) )
1728 (:printer reg/mem ((op '(#b1111111 #b100)) (width 1)))
1733 (lambda (segment posn delta-if-after)
1734 (let ((disp (- (label-position where posn delta-if-after)
1736 (when (<= -128 disp 127)
1738 (dpb (conditional-opcode cond)
1741 (emit-byte-displacement-backpatch segment where)
1743 (lambda (segment posn)
1744 (let ((disp (- (label-position where) (+ posn 6))))
1745 (emit-byte segment #b00001111)
1747 (dpb (conditional-opcode cond)
1750 (emit-dword segment disp)))))
1751 ((label-p (setq where cond))
1754 (lambda (segment posn delta-if-after)
1755 (let ((disp (- (label-position where posn delta-if-after)
1757 (when (<= -128 disp 127)
1758 (emit-byte segment #b11101011)
1759 (emit-byte-displacement-backpatch segment where)
1761 (lambda (segment posn)
1762 (let ((disp (- (label-position where) (+ posn 5))))
1763 (emit-byte segment #b11101001)
1764 (emit-dword segment disp)))))
1766 (emit-byte segment #b11101001)
1767 (emit-relative-fixup segment where))
1769 (unless (or (ea-p where) (tn-p where))
1770 (error "don't know what to do with ~A" where))
1771 (emit-byte segment #b11111111)
1772 (emit-ea segment where #b100)))))
1774 (define-instruction jmp-short (segment label)
1776 (emit-byte segment #b11101011)
1777 (emit-byte-displacement-backpatch segment label)))
1779 (define-instruction ret (segment &optional stack-delta)
1780 (:printer byte ((op #b11000011)))
1781 (:printer byte ((op #b11000010) (imm nil :type 'imm-word-16))
1785 (emit-byte segment #b11000010)
1786 (emit-word segment stack-delta))
1788 (emit-byte segment #b11000011)))))
1790 (define-instruction jecxz (segment target)
1791 (:printer short-jump ((op #b0011)))
1793 (emit-byte segment #b11100011)
1794 (emit-byte-displacement-backpatch segment target)))
1796 (define-instruction loop (segment target)
1797 (:printer short-jump ((op #b0010)))
1799 (emit-byte segment #b11100010) ; pfw this was 11100011, or jecxz!!!!
1800 (emit-byte-displacement-backpatch segment target)))
1802 (define-instruction loopz (segment target)
1803 (:printer short-jump ((op #b0001)))
1805 (emit-byte segment #b11100001)
1806 (emit-byte-displacement-backpatch segment target)))
1808 (define-instruction loopnz (segment target)
1809 (:printer short-jump ((op #b0000)))
1811 (emit-byte segment #b11100000)
1812 (emit-byte-displacement-backpatch segment target)))
1814 ;;;; conditional move
1815 (define-instruction cmov (segment cond dst src)
1816 (:printer cond-move ())
1818 (aver (register-p dst))
1819 (let ((size (matching-operand-size dst src)))
1820 (aver (or (eq size :word) (eq size :dword)))
1821 (maybe-emit-operand-size-prefix segment size))
1822 (emit-byte segment #b00001111)
1823 (emit-byte segment (dpb (conditional-opcode cond) (byte 4 0) #b01000000))
1824 (emit-ea segment src (reg-tn-encoding dst))))
1826 ;;;; conditional byte set
1828 (define-instruction set (segment dst cond)
1829 (:printer cond-set ())
1831 (emit-byte segment #b00001111)
1832 (emit-byte segment (dpb (conditional-opcode cond) (byte 4 0) #b10010000))
1833 (emit-ea segment dst #b000)))
1837 (define-instruction enter (segment disp &optional (level 0))
1838 (:declare (type (unsigned-byte 16) disp)
1839 (type (unsigned-byte 8) level))
1840 (:printer enter-format ((op #b11001000)))
1842 (emit-byte segment #b11001000)
1843 (emit-word segment disp)
1844 (emit-byte segment level)))
1846 (define-instruction leave (segment)
1847 (:printer byte ((op #b11001001)))
1849 (emit-byte segment #b11001001)))
1852 (define-instruction prefetchnta (segment ea)
1853 (:printer prefetch ((op #b00011000) (reg #b000)))
1855 (aver (typep ea 'ea))
1856 (aver (eq :byte (ea-size ea)))
1857 (emit-byte segment #b00001111)
1858 (emit-byte segment #b00011000)
1859 (emit-ea segment ea #b000)))
1861 (define-instruction prefetcht0 (segment ea)
1862 (:printer prefetch ((op #b00011000) (reg #b001)))
1864 (aver (typep ea 'ea))
1865 (aver (eq :byte (ea-size ea)))
1866 (emit-byte segment #b00001111)
1867 (emit-byte segment #b00011000)
1868 (emit-ea segment ea #b001)))
1870 (define-instruction prefetcht1 (segment ea)
1871 (:printer prefetch ((op #b00011000) (reg #b010)))
1873 (aver (typep ea 'ea))
1874 (aver (eq :byte (ea-size ea)))
1875 (emit-byte segment #b00001111)
1876 (emit-byte segment #b00011000)
1877 (emit-ea segment ea #b010)))
1879 (define-instruction prefetcht2 (segment ea)
1880 (:printer prefetch ((op #b00011000) (reg #b011)))
1882 (aver (typep ea 'ea))
1883 (aver (eq :byte (ea-size ea)))
1884 (emit-byte segment #b00001111)
1885 (emit-byte segment #b00011000)
1886 (emit-ea segment ea #b011)))
1888 ;;;; interrupt instructions
1890 (defun snarf-error-junk (sap offset &optional length-only)
1891 (let* ((length (sb!sys:sap-ref-8 sap offset))
1892 (vector (make-array length :element-type '(unsigned-byte 8))))
1893 (declare (type sb!sys:system-area-pointer sap)
1894 (type (unsigned-byte 8) length)
1895 (type (simple-array (unsigned-byte 8) (*)) vector))
1897 (values 0 (1+ length) nil nil))
1899 (sb!kernel:copy-from-system-area sap (* n-byte-bits (1+ offset))
1900 vector (* n-word-bits
1902 (* length n-byte-bits))
1903 (collect ((sc-offsets)
1905 (lengths 1) ; the length byte
1907 (error-number (sb!c:read-var-integer vector index)))
1910 (when (>= index length)
1912 (let ((old-index index))
1913 (sc-offsets (sb!c:read-var-integer vector index))
1914 (lengths (- index old-index))))
1915 (values error-number
1921 (defmacro break-cases (breaknum &body cases)
1922 (let ((bn-temp (gensym)))
1923 (collect ((clauses))
1924 (dolist (case cases)
1925 (clauses `((= ,bn-temp ,(car case)) ,@(cdr case))))
1926 `(let ((,bn-temp ,breaknum))
1927 (cond ,@(clauses))))))
1930 (defun break-control (chunk inst stream dstate)
1931 (declare (ignore inst))
1932 (flet ((nt (x) (if stream (sb!disassem:note x dstate))))
1933 ;; FIXME: Make sure that BYTE-IMM-CODE is defined. The genesis
1934 ;; map has it undefined; and it should be easier to look in the target
1935 ;; Lisp (with (DESCRIBE 'BYTE-IMM-CODE)) than to definitively deduce
1936 ;; from first principles whether it's defined in some way that genesis
1938 (case (byte-imm-code chunk dstate)
1941 (sb!disassem:handle-break-args #'snarf-error-junk stream dstate))
1944 (sb!disassem:handle-break-args #'snarf-error-junk stream dstate))
1946 (nt "breakpoint trap"))
1947 (#.pending-interrupt-trap
1948 (nt "pending interrupt trap"))
1951 (#.fun-end-breakpoint-trap
1952 (nt "function end breakpoint trap")))))
1954 (define-instruction break (segment code)
1955 (:declare (type (unsigned-byte 8) code))
1956 (:printer byte-imm ((op #b11001100)) '(:name :tab code)
1957 :control #'break-control)
1959 (emit-byte segment #b11001100)
1960 (emit-byte segment code)))
1962 (define-instruction int (segment number)
1963 (:declare (type (unsigned-byte 8) number))
1964 (:printer byte-imm ((op #b11001101)))
1968 (emit-byte segment #b11001100))
1970 (emit-byte segment #b11001101)
1971 (emit-byte segment number)))))
1973 (define-instruction into (segment)
1974 (:printer byte ((op #b11001110)))
1976 (emit-byte segment #b11001110)))
1978 (define-instruction bound (segment reg bounds)
1980 (let ((size (matching-operand-size reg bounds)))
1981 (when (eq size :byte)
1982 (error "can't bounds-test bytes: ~S" reg))
1983 (maybe-emit-operand-size-prefix segment size)
1984 (emit-byte segment #b01100010)
1985 (emit-ea segment bounds (reg-tn-encoding reg)))))
1987 (define-instruction iret (segment)
1988 (:printer byte ((op #b11001111)))
1990 (emit-byte segment #b11001111)))
1992 ;;;; processor control
1994 (define-instruction hlt (segment)
1995 (:printer byte ((op #b11110100)))
1997 (emit-byte segment #b11110100)))
1999 (define-instruction nop (segment)
2000 (:printer byte ((op #b10010000)))
2002 (emit-byte segment #b10010000)))
2004 (define-instruction wait (segment)
2005 (:printer byte ((op #b10011011)))
2007 (emit-byte segment #b10011011)))
2009 (define-instruction lock (segment)
2010 (:printer byte ((op #b11110000)))
2012 (emit-byte segment #b11110000)))
2014 ;;;; miscellaneous hackery
2016 (define-instruction byte (segment byte)
2018 (emit-byte segment byte)))
2020 (define-instruction word (segment word)
2022 (emit-word segment word)))
2024 (define-instruction dword (segment dword)
2026 (emit-dword segment dword)))
2028 (defun emit-header-data (segment type)
2029 (emit-back-patch segment
2031 (lambda (segment posn)
2035 (component-header-length))
2039 (define-instruction simple-fun-header-word (segment)
2041 (emit-header-data segment simple-fun-header-widetag)))
2043 (define-instruction lra-header-word (segment)
2045 (emit-header-data segment return-pc-header-widetag)))
2047 ;;;; fp instructions
2049 ;;;; FIXME: This section said "added by jrd", which should end up in CREDITS.
2051 ;;;; Note: We treat the single-precision and double-precision variants
2052 ;;;; as separate instructions.
2054 ;;; Load single to st(0).
2055 (define-instruction fld (segment source)
2056 (:printer floating-point ((op '(#b001 #b000))))
2058 (emit-byte segment #b11011001)
2059 (emit-fp-op segment source #b000)))
2061 ;;; Load double to st(0).
2062 (define-instruction fldd (segment source)
2063 (:printer floating-point ((op '(#b101 #b000))))
2064 (:printer floating-point-fp ((op '(#b001 #b000))))
2066 (if (fp-reg-tn-p source)
2067 (emit-byte segment #b11011001)
2068 (emit-byte segment #b11011101))
2069 (emit-fp-op segment source #b000)))
2071 ;;; Load long to st(0).
2072 (define-instruction fldl (segment source)
2073 (:printer floating-point ((op '(#b011 #b101))))
2075 (emit-byte segment #b11011011)
2076 (emit-fp-op segment source #b101)))
2078 ;;; Store single from st(0).
2079 (define-instruction fst (segment dest)
2080 (:printer floating-point ((op '(#b001 #b010))))
2082 (cond ((fp-reg-tn-p dest)
2083 (emit-byte segment #b11011101)
2084 (emit-fp-op segment dest #b010))
2086 (emit-byte segment #b11011001)
2087 (emit-fp-op segment dest #b010)))))
2089 ;;; Store double from st(0).
2090 (define-instruction fstd (segment dest)
2091 (:printer floating-point ((op '(#b101 #b010))))
2092 (:printer floating-point-fp ((op '(#b101 #b010))))
2094 (cond ((fp-reg-tn-p dest)
2095 (emit-byte segment #b11011101)
2096 (emit-fp-op segment dest #b010))
2098 (emit-byte segment #b11011101)
2099 (emit-fp-op segment dest #b010)))))
2101 ;;; Arithmetic ops are all done with at least one operand at top of
2102 ;;; stack. The other operand is is another register or a 32/64 bit
2105 ;;; dtc: I've tried to follow the Intel ASM386 conventions, but note
2106 ;;; that these conflict with the Gdb conventions for binops. To reduce
2107 ;;; the confusion I've added comments showing the mathamatical
2108 ;;; operation and the two syntaxes. By the ASM386 convention the
2109 ;;; instruction syntax is:
2112 ;;; or Fop Destination, Source
2114 ;;; If only one operand is given then it is the source and the
2115 ;;; destination is ST(0). There are reversed forms of the fsub and
2116 ;;; fdiv instructions inducated by an 'R' suffix.
2118 ;;; The mathematical operation for the non-reverse form is always:
2119 ;;; destination = destination op source
2121 ;;; For the reversed form it is:
2122 ;;; destination = source op destination
2124 ;;; The instructions below only accept one operand at present which is
2125 ;;; usually the source. I've hack in extra instructions to implement
2126 ;;; the fops with a ST(i) destination, these have a -sti suffix and
2127 ;;; the operand is the destination with the source being ST(0).
2130 ;;; st(0) = st(0) + memory or st(i).
2131 (define-instruction fadd (segment source)
2132 (:printer floating-point ((op '(#b000 #b000))))
2134 (emit-byte segment #b11011000)
2135 (emit-fp-op segment source #b000)))
2138 ;;; st(0) = st(0) + memory or st(i).
2139 (define-instruction faddd (segment source)
2140 (:printer floating-point ((op '(#b100 #b000))))
2141 (:printer floating-point-fp ((op '(#b000 #b000))))
2143 (if (fp-reg-tn-p source)
2144 (emit-byte segment #b11011000)
2145 (emit-byte segment #b11011100))
2146 (emit-fp-op segment source #b000)))
2148 ;;; Add double destination st(i):
2149 ;;; st(i) = st(0) + st(i).
2150 (define-instruction fadd-sti (segment destination)
2151 (:printer floating-point-fp ((op '(#b100 #b000))))
2153 (aver (fp-reg-tn-p destination))
2154 (emit-byte segment #b11011100)
2155 (emit-fp-op segment destination #b000)))
2157 (define-instruction faddp-sti (segment destination)
2158 (:printer floating-point-fp ((op '(#b110 #b000))))
2160 (aver (fp-reg-tn-p destination))
2161 (emit-byte segment #b11011110)
2162 (emit-fp-op segment destination #b000)))
2164 ;;; Subtract single:
2165 ;;; st(0) = st(0) - memory or st(i).
2166 (define-instruction fsub (segment source)
2167 (:printer floating-point ((op '(#b000 #b100))))
2169 (emit-byte segment #b11011000)
2170 (emit-fp-op segment source #b100)))
2172 ;;; Subtract single, reverse:
2173 ;;; st(0) = memory or st(i) - st(0).
2174 (define-instruction fsubr (segment source)
2175 (:printer floating-point ((op '(#b000 #b101))))
2177 (emit-byte segment #b11011000)
2178 (emit-fp-op segment source #b101)))
2180 ;;; Subtract double:
2181 ;;; st(0) = st(0) - memory or st(i).
2182 (define-instruction fsubd (segment source)
2183 (:printer floating-point ((op '(#b100 #b100))))
2184 (:printer floating-point-fp ((op '(#b000 #b100))))
2186 (if (fp-reg-tn-p source)
2187 (emit-byte segment #b11011000)
2188 (emit-byte segment #b11011100))
2189 (emit-fp-op segment source #b100)))
2191 ;;; Subtract double, reverse:
2192 ;;; st(0) = memory or st(i) - st(0).
2193 (define-instruction fsubrd (segment source)
2194 (:printer floating-point ((op '(#b100 #b101))))
2195 (:printer floating-point-fp ((op '(#b000 #b101))))
2197 (if (fp-reg-tn-p source)
2198 (emit-byte segment #b11011000)
2199 (emit-byte segment #b11011100))
2200 (emit-fp-op segment source #b101)))
2202 ;;; Subtract double, destination st(i):
2203 ;;; st(i) = st(i) - st(0).
2205 ;;; ASM386 syntax: FSUB ST(i), ST
2206 ;;; Gdb syntax: fsubr %st,%st(i)
2207 (define-instruction fsub-sti (segment destination)
2208 (:printer floating-point-fp ((op '(#b100 #b101))))
2210 (aver (fp-reg-tn-p destination))
2211 (emit-byte segment #b11011100)
2212 (emit-fp-op segment destination #b101)))
2214 (define-instruction fsubp-sti (segment destination)
2215 (:printer floating-point-fp ((op '(#b110 #b101))))
2217 (aver (fp-reg-tn-p destination))
2218 (emit-byte segment #b11011110)
2219 (emit-fp-op segment destination #b101)))
2221 ;;; Subtract double, reverse, destination st(i):
2222 ;;; st(i) = st(0) - st(i).
2224 ;;; ASM386 syntax: FSUBR ST(i), ST
2225 ;;; Gdb syntax: fsub %st,%st(i)
2226 (define-instruction fsubr-sti (segment destination)
2227 (:printer floating-point-fp ((op '(#b100 #b100))))
2229 (aver (fp-reg-tn-p destination))
2230 (emit-byte segment #b11011100)
2231 (emit-fp-op segment destination #b100)))
2233 (define-instruction fsubrp-sti (segment destination)
2234 (:printer floating-point-fp ((op '(#b110 #b100))))
2236 (aver (fp-reg-tn-p destination))
2237 (emit-byte segment #b11011110)
2238 (emit-fp-op segment destination #b100)))
2240 ;;; Multiply single:
2241 ;;; st(0) = st(0) * memory or st(i).
2242 (define-instruction fmul (segment source)
2243 (:printer floating-point ((op '(#b000 #b001))))
2245 (emit-byte segment #b11011000)
2246 (emit-fp-op segment source #b001)))
2248 ;;; Multiply double:
2249 ;;; st(0) = st(0) * memory or st(i).
2250 (define-instruction fmuld (segment source)
2251 (:printer floating-point ((op '(#b100 #b001))))
2252 (:printer floating-point-fp ((op '(#b000 #b001))))
2254 (if (fp-reg-tn-p source)
2255 (emit-byte segment #b11011000)
2256 (emit-byte segment #b11011100))
2257 (emit-fp-op segment source #b001)))
2259 ;;; Multiply double, destination st(i):
2260 ;;; st(i) = st(i) * st(0).
2261 (define-instruction fmul-sti (segment destination)
2262 (:printer floating-point-fp ((op '(#b100 #b001))))
2264 (aver (fp-reg-tn-p destination))
2265 (emit-byte segment #b11011100)
2266 (emit-fp-op segment destination #b001)))
2269 ;;; st(0) = st(0) / memory or st(i).
2270 (define-instruction fdiv (segment source)
2271 (:printer floating-point ((op '(#b000 #b110))))
2273 (emit-byte segment #b11011000)
2274 (emit-fp-op segment source #b110)))
2276 ;;; Divide single, reverse:
2277 ;;; st(0) = memory or st(i) / st(0).
2278 (define-instruction fdivr (segment source)
2279 (:printer floating-point ((op '(#b000 #b111))))
2281 (emit-byte segment #b11011000)
2282 (emit-fp-op segment source #b111)))
2285 ;;; st(0) = st(0) / memory or st(i).
2286 (define-instruction fdivd (segment source)
2287 (:printer floating-point ((op '(#b100 #b110))))
2288 (:printer floating-point-fp ((op '(#b000 #b110))))
2290 (if (fp-reg-tn-p source)
2291 (emit-byte segment #b11011000)
2292 (emit-byte segment #b11011100))
2293 (emit-fp-op segment source #b110)))
2295 ;;; Divide double, reverse:
2296 ;;; st(0) = memory or st(i) / st(0).
2297 (define-instruction fdivrd (segment source)
2298 (:printer floating-point ((op '(#b100 #b111))))
2299 (:printer floating-point-fp ((op '(#b000 #b111))))
2301 (if (fp-reg-tn-p source)
2302 (emit-byte segment #b11011000)
2303 (emit-byte segment #b11011100))
2304 (emit-fp-op segment source #b111)))
2306 ;;; Divide double, destination st(i):
2307 ;;; st(i) = st(i) / st(0).
2309 ;;; ASM386 syntax: FDIV ST(i), ST
2310 ;;; Gdb syntax: fdivr %st,%st(i)
2311 (define-instruction fdiv-sti (segment destination)
2312 (:printer floating-point-fp ((op '(#b100 #b111))))
2314 (aver (fp-reg-tn-p destination))
2315 (emit-byte segment #b11011100)
2316 (emit-fp-op segment destination #b111)))
2318 ;;; Divide double, reverse, destination st(i):
2319 ;;; st(i) = st(0) / st(i).
2321 ;;; ASM386 syntax: FDIVR ST(i), ST
2322 ;;; Gdb syntax: fdiv %st,%st(i)
2323 (define-instruction fdivr-sti (segment destination)
2324 (:printer floating-point-fp ((op '(#b100 #b110))))
2326 (aver (fp-reg-tn-p destination))
2327 (emit-byte segment #b11011100)
2328 (emit-fp-op segment destination #b110)))
2330 ;;; Exchange fr0 with fr(n). (There is no double precision variant.)
2331 (define-instruction fxch (segment source)
2332 (:printer floating-point-fp ((op '(#b001 #b001))))
2334 (unless (and (tn-p source)
2335 (eq (sb-name (sc-sb (tn-sc source))) 'float-registers))
2337 (emit-byte segment #b11011001)
2338 (emit-fp-op segment source #b001)))
2340 ;;; Push 32-bit integer to st0.
2341 (define-instruction fild (segment source)
2342 (:printer floating-point ((op '(#b011 #b000))))
2344 (emit-byte segment #b11011011)
2345 (emit-fp-op segment source #b000)))
2347 ;;; Push 64-bit integer to st0.
2348 (define-instruction fildl (segment source)
2349 (:printer floating-point ((op '(#b111 #b101))))
2351 (emit-byte segment #b11011111)
2352 (emit-fp-op segment source #b101)))
2354 ;;; Store 32-bit integer.
2355 (define-instruction fist (segment dest)
2356 (:printer floating-point ((op '(#b011 #b010))))
2358 (emit-byte segment #b11011011)
2359 (emit-fp-op segment dest #b010)))
2361 ;;; Store and pop 32-bit integer.
2362 (define-instruction fistp (segment dest)
2363 (:printer floating-point ((op '(#b011 #b011))))
2365 (emit-byte segment #b11011011)
2366 (emit-fp-op segment dest #b011)))
2368 ;;; Store and pop 64-bit integer.
2369 (define-instruction fistpl (segment dest)
2370 (:printer floating-point ((op '(#b111 #b111))))
2372 (emit-byte segment #b11011111)
2373 (emit-fp-op segment dest #b111)))
2375 ;;; Store single from st(0) and pop.
2376 (define-instruction fstp (segment dest)
2377 (:printer floating-point ((op '(#b001 #b011))))
2379 (cond ((fp-reg-tn-p dest)
2380 (emit-byte segment #b11011101)
2381 (emit-fp-op segment dest #b011))
2383 (emit-byte segment #b11011001)
2384 (emit-fp-op segment dest #b011)))))
2386 ;;; Store double from st(0) and pop.
2387 (define-instruction fstpd (segment dest)
2388 (:printer floating-point ((op '(#b101 #b011))))
2389 (:printer floating-point-fp ((op '(#b101 #b011))))
2391 (cond ((fp-reg-tn-p dest)
2392 (emit-byte segment #b11011101)
2393 (emit-fp-op segment dest #b011))
2395 (emit-byte segment #b11011101)
2396 (emit-fp-op segment dest #b011)))))
2398 ;;; Store long from st(0) and pop.
2399 (define-instruction fstpl (segment dest)
2400 (:printer floating-point ((op '(#b011 #b111))))
2402 (emit-byte segment #b11011011)
2403 (emit-fp-op segment dest #b111)))
2405 ;;; Decrement stack-top pointer.
2406 (define-instruction fdecstp (segment)
2407 (:printer floating-point-no ((op #b10110)))
2409 (emit-byte segment #b11011001)
2410 (emit-byte segment #b11110110)))
2412 ;;; Increment stack-top pointer.
2413 (define-instruction fincstp (segment)
2414 (:printer floating-point-no ((op #b10111)))
2416 (emit-byte segment #b11011001)
2417 (emit-byte segment #b11110111)))
2419 ;;; Free fp register.
2420 (define-instruction ffree (segment dest)
2421 (:printer floating-point-fp ((op '(#b101 #b000))))
2423 (emit-byte segment #b11011101)
2424 (emit-fp-op segment dest #b000)))
2426 (define-instruction fabs (segment)
2427 (:printer floating-point-no ((op #b00001)))
2429 (emit-byte segment #b11011001)
2430 (emit-byte segment #b11100001)))
2432 (define-instruction fchs (segment)
2433 (:printer floating-point-no ((op #b00000)))
2435 (emit-byte segment #b11011001)
2436 (emit-byte segment #b11100000)))
2438 (define-instruction frndint(segment)
2439 (:printer floating-point-no ((op #b11100)))
2441 (emit-byte segment #b11011001)
2442 (emit-byte segment #b11111100)))
2445 (define-instruction fninit(segment)
2446 (:printer floating-point-5 ((op #b00011)))
2448 (emit-byte segment #b11011011)
2449 (emit-byte segment #b11100011)))
2451 ;;; Store Status Word to AX.
2452 (define-instruction fnstsw(segment)
2453 (:printer floating-point-st ((op #b00000)))
2455 (emit-byte segment #b11011111)
2456 (emit-byte segment #b11100000)))
2458 ;;; Load Control Word.
2460 ;;; src must be a memory location
2461 (define-instruction fldcw(segment src)
2462 (:printer floating-point ((op '(#b001 #b101))))
2464 (emit-byte segment #b11011001)
2465 (emit-fp-op segment src #b101)))
2467 ;;; Store Control Word.
2468 (define-instruction fnstcw(segment dst)
2469 (:printer floating-point ((op '(#b001 #b111))))
2471 (emit-byte segment #b11011001)
2472 (emit-fp-op segment dst #b111)))
2474 ;;; Store FP Environment.
2475 (define-instruction fstenv(segment dst)
2476 (:printer floating-point ((op '(#b001 #b110))))
2478 (emit-byte segment #b11011001)
2479 (emit-fp-op segment dst #b110)))
2481 ;;; Restore FP Environment.
2482 (define-instruction fldenv(segment src)
2483 (:printer floating-point ((op '(#b001 #b100))))
2485 (emit-byte segment #b11011001)
2486 (emit-fp-op segment src #b100)))
2489 (define-instruction fsave(segment dst)
2490 (:printer floating-point ((op '(#b101 #b110))))
2492 (emit-byte segment #b11011101)
2493 (emit-fp-op segment dst #b110)))
2495 ;;; Restore FP State.
2496 (define-instruction frstor(segment src)
2497 (:printer floating-point ((op '(#b101 #b100))))
2499 (emit-byte segment #b11011101)
2500 (emit-fp-op segment src #b100)))
2502 ;;; Clear exceptions.
2503 (define-instruction fnclex(segment)
2504 (:printer floating-point-5 ((op #b00010)))
2506 (emit-byte segment #b11011011)
2507 (emit-byte segment #b11100010)))
2510 (define-instruction fcom (segment src)
2511 (:printer floating-point ((op '(#b000 #b010))))
2513 (emit-byte segment #b11011000)
2514 (emit-fp-op segment src #b010)))
2516 (define-instruction fcomd (segment src)
2517 (:printer floating-point ((op '(#b100 #b010))))
2518 (:printer floating-point-fp ((op '(#b000 #b010))))
2520 (if (fp-reg-tn-p src)
2521 (emit-byte segment #b11011000)
2522 (emit-byte segment #b11011100))
2523 (emit-fp-op segment src #b010)))
2525 ;;; Compare ST1 to ST0, popping the stack twice.
2526 (define-instruction fcompp (segment)
2527 (:printer floating-point-3 ((op '(#b110 #b011001))))
2529 (emit-byte segment #b11011110)
2530 (emit-byte segment #b11011001)))
2532 ;;; unordered comparison
2533 (define-instruction fucom (segment src)
2534 (:printer floating-point-fp ((op '(#b101 #b100))))
2536 (aver (fp-reg-tn-p src))
2537 (emit-byte segment #b11011101)
2538 (emit-fp-op segment src #b100)))
2540 (define-instruction ftst (segment)
2541 (:printer floating-point-no ((op #b00100)))
2543 (emit-byte segment #b11011001)
2544 (emit-byte segment #b11100100)))
2548 (define-instruction fsqrt(segment)
2549 (:printer floating-point-no ((op #b11010)))
2551 (emit-byte segment #b11011001)
2552 (emit-byte segment #b11111010)))
2554 (define-instruction fscale(segment)
2555 (:printer floating-point-no ((op #b11101)))
2557 (emit-byte segment #b11011001)
2558 (emit-byte segment #b11111101)))
2560 (define-instruction fxtract(segment)
2561 (:printer floating-point-no ((op #b10100)))
2563 (emit-byte segment #b11011001)
2564 (emit-byte segment #b11110100)))
2566 (define-instruction fsin(segment)
2567 (:printer floating-point-no ((op #b11110)))
2569 (emit-byte segment #b11011001)
2570 (emit-byte segment #b11111110)))
2572 (define-instruction fcos(segment)
2573 (:printer floating-point-no ((op #b11111)))
2575 (emit-byte segment #b11011001)
2576 (emit-byte segment #b11111111)))
2578 (define-instruction fprem1(segment)
2579 (:printer floating-point-no ((op #b10101)))
2581 (emit-byte segment #b11011001)
2582 (emit-byte segment #b11110101)))
2584 (define-instruction fprem(segment)
2585 (:printer floating-point-no ((op #b11000)))
2587 (emit-byte segment #b11011001)
2588 (emit-byte segment #b11111000)))
2590 (define-instruction fxam (segment)
2591 (:printer floating-point-no ((op #b00101)))
2593 (emit-byte segment #b11011001)
2594 (emit-byte segment #b11100101)))
2596 ;;; These do push/pop to stack and need special handling
2597 ;;; in any VOPs that use them. See the book.
2599 ;;; st0 <- st1*log2(st0)
2600 (define-instruction fyl2x(segment) ; pops stack
2601 (:printer floating-point-no ((op #b10001)))
2603 (emit-byte segment #b11011001)
2604 (emit-byte segment #b11110001)))
2606 (define-instruction fyl2xp1(segment)
2607 (:printer floating-point-no ((op #b11001)))
2609 (emit-byte segment #b11011001)
2610 (emit-byte segment #b11111001)))
2612 (define-instruction f2xm1(segment)
2613 (:printer floating-point-no ((op #b10000)))
2615 (emit-byte segment #b11011001)
2616 (emit-byte segment #b11110000)))
2618 (define-instruction fptan(segment) ; st(0) <- 1; st(1) <- tan
2619 (:printer floating-point-no ((op #b10010)))
2621 (emit-byte segment #b11011001)
2622 (emit-byte segment #b11110010)))
2624 (define-instruction fpatan(segment) ; POPS STACK
2625 (:printer floating-point-no ((op #b10011)))
2627 (emit-byte segment #b11011001)
2628 (emit-byte segment #b11110011)))
2630 ;;;; loading constants
2632 (define-instruction fldz(segment)
2633 (:printer floating-point-no ((op #b01110)))
2635 (emit-byte segment #b11011001)
2636 (emit-byte segment #b11101110)))
2638 (define-instruction fld1(segment)
2639 (:printer floating-point-no ((op #b01000)))
2641 (emit-byte segment #b11011001)
2642 (emit-byte segment #b11101000)))
2644 (define-instruction fldpi(segment)
2645 (:printer floating-point-no ((op #b01011)))
2647 (emit-byte segment #b11011001)
2648 (emit-byte segment #b11101011)))
2650 (define-instruction fldl2t(segment)
2651 (:printer floating-point-no ((op #b01001)))
2653 (emit-byte segment #b11011001)
2654 (emit-byte segment #b11101001)))
2656 (define-instruction fldl2e(segment)
2657 (:printer floating-point-no ((op #b01010)))
2659 (emit-byte segment #b11011001)
2660 (emit-byte segment #b11101010)))
2662 (define-instruction fldlg2(segment)
2663 (:printer floating-point-no ((op #b01100)))
2665 (emit-byte segment #b11011001)
2666 (emit-byte segment #b11101100)))
2668 (define-instruction fldln2(segment)
2669 (:printer floating-point-no ((op #b01101)))
2671 (emit-byte segment #b11011001)
2672 (emit-byte segment #b11101101)))