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 ;;; Disassembling x86 code needs to take into account little things
44 ;;; like instructions that have a byte/word length bit in their
45 ;;; encoding, prefixes to change the default word length for a single
46 ;;; instruction, and so on. Unfortunately, there is no easy way with
47 ;;; this disassembler framework to handle prefixes that will work
48 ;;; correctly in all cases, so we copy the x86-64 version which at
49 ;;; least can handle the code output by the compiler.
51 ;;; Width information for an instruction and whether a segment
52 ;;; override prefix was seen is stored as an inst-prop on the dstate.
53 ;;; The inst-props are cleared automatically after each non-prefix
54 ;;; instruction, must be set by prefilters, and contain a single bit of
55 ;;; data each (presence/absence).
57 ;;; Return the operand size based on the prefixes and width bit from
59 (defun inst-operand-size (dstate)
60 (declare (type sb!disassem:disassem-state dstate))
61 (cond ((sb!disassem:dstate-get-inst-prop dstate 'operand-size-8)
63 ((sb!disassem:dstate-get-inst-prop dstate 'operand-size-16)
66 +default-operand-size+)))
68 ;;; Return the operand size for a "word-sized" operand based on the
69 ;;; prefixes from the dstate.
70 (defun inst-word-operand-size (dstate)
71 (declare (type sb!disassem:disassem-state dstate))
72 (if (sb!disassem:dstate-get-inst-prop dstate 'operand-size-16)
76 (defun print-reg-with-width (value width stream dstate)
77 (declare (ignore dstate))
78 (princ (aref (ecase width
79 (:byte *byte-reg-names*)
80 (:word *word-reg-names*)
81 (:dword *dword-reg-names*))
84 ;; XXX plus should do some source-var notes
87 (defun print-reg (value stream dstate)
88 (declare (type reg value)
90 (type sb!disassem:disassem-state dstate))
91 (print-reg-with-width value
92 (inst-operand-size dstate)
96 (defun print-word-reg (value stream dstate)
97 (declare (type reg value)
99 (type sb!disassem:disassem-state dstate))
100 (print-reg-with-width value
101 (inst-word-operand-size dstate)
105 (defun print-byte-reg (value stream dstate)
106 (declare (type reg value)
108 (type sb!disassem:disassem-state dstate))
109 (print-reg-with-width value :byte stream dstate))
111 (defun print-addr-reg (value stream dstate)
112 (declare (type reg value)
114 (type sb!disassem:disassem-state dstate))
115 (print-reg-with-width value *default-address-size* stream dstate))
117 (defun print-reg/mem (value stream dstate)
118 (declare (type (or list reg) value)
120 (type sb!disassem:disassem-state dstate))
121 (if (typep value 'reg)
122 (print-reg value stream dstate)
123 (print-mem-access value stream nil dstate)))
125 ;; Same as print-reg/mem, but prints an explicit size indicator for
126 ;; memory references.
127 (defun print-sized-reg/mem (value stream dstate)
128 (declare (type (or list reg) value)
130 (type sb!disassem:disassem-state dstate))
131 (if (typep value 'reg)
132 (print-reg value stream dstate)
133 (print-mem-access value stream t dstate)))
135 (defun print-byte-reg/mem (value stream dstate)
136 (declare (type (or list reg) value)
138 (type sb!disassem:disassem-state dstate))
139 (if (typep value 'reg)
140 (print-byte-reg value stream dstate)
141 (print-mem-access value stream t dstate)))
143 (defun print-word-reg/mem (value stream dstate)
144 (declare (type (or list reg) value)
146 (type sb!disassem:disassem-state dstate))
147 (if (typep value 'reg)
148 (print-word-reg value stream dstate)
149 (print-mem-access value stream nil dstate)))
151 (defun print-label (value stream dstate)
152 (declare (ignore dstate))
153 (sb!disassem:princ16 value stream))
155 (defun maybe-print-segment-override (stream dstate)
156 (cond ((sb!disassem:dstate-get-inst-prop dstate 'fs-segment-prefix)
157 (princ "FS:" stream))
158 ((sb!disassem:dstate-get-inst-prop dstate 'gs-segment-prefix)
159 (princ "GS:" stream))))
161 ;;; Returns either an integer, meaning a register, or a list of
162 ;;; (BASE-REG OFFSET INDEX-REG INDEX-SCALE), where any component
163 ;;; may be missing or nil to indicate that it's not used or has the
164 ;;; obvious default value (e.g., 1 for the index-scale).
165 (defun prefilter-reg/mem (value dstate)
166 (declare (type list value)
167 (type sb!disassem:disassem-state dstate))
168 (let ((mod (car value))
170 (declare (type (unsigned-byte 2) mod)
171 (type (unsigned-byte 3) r/m))
177 (let ((sib (sb!disassem:read-suffix 8 dstate)))
178 (declare (type (unsigned-byte 8) sib))
179 (let ((base-reg (ldb (byte 3 0) sib))
180 (index-reg (ldb (byte 3 3) sib))
181 (index-scale (ldb (byte 2 6) sib)))
182 (declare (type (unsigned-byte 3) base-reg index-reg)
183 (type (unsigned-byte 2) index-scale))
187 (if (= base-reg #b101)
188 (sb!disassem:read-signed-suffix 32 dstate)
191 (sb!disassem:read-signed-suffix 8 dstate))
193 (sb!disassem:read-signed-suffix 32 dstate)))))
194 (list (if (and (= mod #b00) (= base-reg #b101)) nil base-reg)
196 (if (= index-reg #b100) nil index-reg)
197 (ash 1 index-scale))))))
198 ((and (= mod #b00) (= r/m #b101))
199 (list nil (sb!disassem:read-signed-suffix 32 dstate)) )
203 (list r/m (sb!disassem:read-signed-suffix 8 dstate)))
205 (list r/m (sb!disassem:read-signed-suffix 32 dstate))))))
208 ;;; This is a sort of bogus prefilter that just stores the info globally for
209 ;;; other people to use; it probably never gets printed.
210 (defun prefilter-width (value dstate)
211 (declare (type bit value)
212 (type sb!disassem:disassem-state dstate))
214 (sb!disassem:dstate-put-inst-prop dstate 'operand-size-8))
217 ;;; This prefilter is used solely for its side effect, namely to put
218 ;;; the property OPERAND-SIZE-16 into the DSTATE.
219 (defun prefilter-x66 (value dstate)
220 (declare (type (eql #x66) value)
222 (type sb!disassem:disassem-state dstate))
223 (sb!disassem:dstate-put-inst-prop dstate 'operand-size-16))
225 ;;; This prefilter is used solely for its side effect, namely to put
226 ;;; one of the properties [FG]S-SEGMENT-PREFIX into the DSTATE.
227 ;;; Unlike PREFILTER-X66, this prefilter only catches the low bit of
229 (defun prefilter-seg (value dstate)
230 (declare (type bit value)
231 (type sb!disassem:disassem-state dstate))
232 (sb!disassem:dstate-put-inst-prop
233 dstate (elt '(fs-segment-prefix gs-segment-prefix) value)))
235 (defun read-address (value dstate)
236 (declare (ignore value)) ; always nil anyway
237 (sb!disassem:read-suffix (width-bits *default-address-size*) dstate))
239 (defun width-bits (width)
249 ;;;; disassembler argument types
251 (sb!disassem:define-arg-type displacement
253 :use-label #'offset-next
254 :printer (lambda (value stream dstate)
255 (sb!disassem:maybe-note-assembler-routine value nil dstate)
256 (print-label value stream dstate)))
258 (sb!disassem:define-arg-type accum
259 :printer (lambda (value stream dstate)
260 (declare (ignore value)
262 (type sb!disassem:disassem-state dstate))
263 (print-reg 0 stream dstate)))
265 (sb!disassem:define-arg-type word-accum
266 :printer (lambda (value stream dstate)
267 (declare (ignore value)
269 (type sb!disassem:disassem-state dstate))
270 (print-word-reg 0 stream dstate)))
272 (sb!disassem:define-arg-type reg
273 :printer #'print-reg)
275 (sb!disassem:define-arg-type addr-reg
276 :printer #'print-addr-reg)
278 (sb!disassem:define-arg-type word-reg
279 :printer #'print-word-reg)
281 (sb!disassem:define-arg-type imm-addr
282 :prefilter #'read-address
283 :printer #'print-label)
285 (sb!disassem:define-arg-type imm-data
286 :prefilter (lambda (value dstate)
287 (declare (ignore value)) ; always nil anyway
288 (sb!disassem:read-suffix
289 (width-bits (inst-operand-size dstate))
292 (sb!disassem:define-arg-type signed-imm-data
293 :prefilter (lambda (value dstate)
294 (declare (ignore value)) ; always nil anyway
295 (let ((width (inst-operand-size dstate)))
296 (sb!disassem:read-signed-suffix (width-bits width) dstate))))
298 (sb!disassem:define-arg-type signed-imm-byte
299 :prefilter (lambda (value dstate)
300 (declare (ignore value)) ; always nil anyway
301 (sb!disassem:read-signed-suffix 8 dstate)))
303 (sb!disassem:define-arg-type signed-imm-dword
304 :prefilter (lambda (value dstate)
305 (declare (ignore value)) ; always nil anyway
306 (sb!disassem:read-signed-suffix 32 dstate)))
308 (sb!disassem:define-arg-type imm-word
309 :prefilter (lambda (value dstate)
310 (declare (ignore value)) ; always nil anyway
311 (let ((width (inst-word-operand-size dstate)))
312 (sb!disassem:read-suffix (width-bits width) dstate))))
314 (sb!disassem:define-arg-type signed-imm-word
315 :prefilter (lambda (value dstate)
316 (declare (ignore value)) ; always nil anyway
317 (let ((width (inst-word-operand-size dstate)))
318 (sb!disassem:read-signed-suffix (width-bits width) dstate))))
320 ;;; needed for the ret imm16 instruction
321 (sb!disassem:define-arg-type imm-word-16
322 :prefilter (lambda (value dstate)
323 (declare (ignore value)) ; always nil anyway
324 (sb!disassem:read-suffix 16 dstate)))
326 (sb!disassem:define-arg-type reg/mem
327 :prefilter #'prefilter-reg/mem
328 :printer #'print-reg/mem)
329 (sb!disassem:define-arg-type sized-reg/mem
330 ;; Same as reg/mem, but prints an explicit size indicator for
331 ;; memory references.
332 :prefilter #'prefilter-reg/mem
333 :printer #'print-sized-reg/mem)
334 (sb!disassem:define-arg-type byte-reg/mem
335 :prefilter #'prefilter-reg/mem
336 :printer #'print-byte-reg/mem)
337 (sb!disassem:define-arg-type word-reg/mem
338 :prefilter #'prefilter-reg/mem
339 :printer #'print-word-reg/mem)
342 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
343 (defun print-fp-reg (value stream dstate)
344 (declare (ignore dstate))
345 (format stream "FR~D" value))
346 (defun prefilter-fp-reg (value dstate)
348 (declare (ignore dstate))
351 (sb!disassem:define-arg-type fp-reg
352 :prefilter #'prefilter-fp-reg
353 :printer #'print-fp-reg)
355 (sb!disassem:define-arg-type width
356 :prefilter #'prefilter-width
357 :printer (lambda (value stream dstate)
358 (declare (ignore value))
359 (princ (schar (symbol-name (inst-operand-size dstate)) 0)
362 ;;; Used to capture the effect of the #x66 operand size override prefix.
363 (sb!disassem:define-arg-type x66
364 :prefilter #'prefilter-x66)
366 ;;; Used to capture the effect of the #x64 and #x65 segment override
368 (sb!disassem:define-arg-type seg
369 :prefilter #'prefilter-seg)
371 (eval-when (:compile-toplevel :load-toplevel :execute)
372 (defparameter *conditions*
375 (:b . 2) (:nae . 2) (:c . 2)
376 (:nb . 3) (:ae . 3) (:nc . 3)
377 (:eq . 4) (:e . 4) (:z . 4)
384 (:np . 11) (:po . 11)
385 (:l . 12) (:nge . 12)
386 (:nl . 13) (:ge . 13)
387 (:le . 14) (:ng . 14)
388 (:nle . 15) (:g . 15)))
389 (defparameter *condition-name-vec*
390 (let ((vec (make-array 16 :initial-element nil)))
391 (dolist (cond *conditions*)
392 (when (null (aref vec (cdr cond)))
393 (setf (aref vec (cdr cond)) (car cond))))
397 ;;; Set assembler parameters. (In CMU CL, this was done with
398 ;;; a call to a macro DEF-ASSEMBLER-PARAMS.)
399 (eval-when (:compile-toplevel :load-toplevel :execute)
400 (setf sb!assem:*assem-scheduler-p* nil))
402 (sb!disassem:define-arg-type condition-code
403 :printer *condition-name-vec*)
405 (defun conditional-opcode (condition)
406 (cdr (assoc condition *conditions* :test #'eq)))
408 ;;;; disassembler instruction formats
410 (eval-when (:compile-toplevel :execute)
411 (defun swap-if (direction field1 separator field2)
412 `(:if (,direction :constant 0)
413 (,field1 ,separator ,field2)
414 (,field2 ,separator ,field1))))
416 (sb!disassem:define-instruction-format (byte 8 :default-printer '(:name))
417 (op :field (byte 8 0))
422 ;;; Prefix instructions
424 (sb!disassem:define-instruction-format (x66 8)
425 (x66 :field (byte 8 0) :type 'x66 :value #x66))
427 (sb!disassem:define-instruction-format (seg 8)
428 (seg :field (byte 7 1) :value #x32)
429 (fsgs :field (byte 1 0) :type 'seg))
431 (sb!disassem:define-instruction-format (simple 8)
432 (op :field (byte 7 1))
433 (width :field (byte 1 0) :type 'width)
438 (sb!disassem:define-instruction-format (two-bytes 16
439 :default-printer '(:name))
440 (op :fields (list (byte 8 0) (byte 8 8))))
442 ;;; Same as simple, but with direction bit
443 (sb!disassem:define-instruction-format (simple-dir 8 :include 'simple)
444 (op :field (byte 6 2))
445 (dir :field (byte 1 1)))
447 ;;; Same as simple, but with the immediate value occurring by default,
448 ;;; and with an appropiate printer.
449 (sb!disassem:define-instruction-format (accum-imm 8
451 :default-printer '(:name
452 :tab accum ", " imm))
453 (imm :type 'imm-data))
455 (sb!disassem:define-instruction-format (reg-no-width 8
456 :default-printer '(:name :tab reg))
457 (op :field (byte 5 3))
458 (reg :field (byte 3 0) :type 'word-reg)
460 (accum :type 'word-accum)
463 ;;; adds a width field to reg-no-width
464 (sb!disassem:define-instruction-format (reg 8
465 :default-printer '(:name :tab reg))
466 (op :field (byte 4 4))
467 (width :field (byte 1 3) :type 'width)
468 (reg :field (byte 3 0) :type 'reg)
474 ;;; Same as reg, but with direction bit
475 (sb!disassem:define-instruction-format (reg-dir 8 :include 'reg)
476 (op :field (byte 3 5))
477 (dir :field (byte 1 4)))
479 (sb!disassem:define-instruction-format (two-bytes 16
480 :default-printer '(:name))
481 (op :fields (list (byte 8 0) (byte 8 8))))
483 (sb!disassem:define-instruction-format (reg-reg/mem 16
485 `(:name :tab reg ", " reg/mem))
486 (op :field (byte 7 1))
487 (width :field (byte 1 0) :type 'width)
488 (reg/mem :fields (list (byte 2 14) (byte 3 8))
490 (reg :field (byte 3 11) :type 'reg)
494 ;;; same as reg-reg/mem, but with direction bit
495 (sb!disassem:define-instruction-format (reg-reg/mem-dir 16
496 :include 'reg-reg/mem
500 ,(swap-if 'dir 'reg/mem ", " 'reg)))
501 (op :field (byte 6 2))
502 (dir :field (byte 1 1)))
504 ;;; Same as reg-rem/mem, but uses the reg field as a second op code.
505 (sb!disassem:define-instruction-format (reg/mem 16
506 :default-printer '(:name :tab reg/mem))
507 (op :fields (list (byte 7 1) (byte 3 11)))
508 (width :field (byte 1 0) :type 'width)
509 (reg/mem :fields (list (byte 2 14) (byte 3 8))
510 :type 'sized-reg/mem)
514 ;;; Same as reg/mem, but with the immediate value occurring by default,
515 ;;; and with an appropiate printer.
516 (sb!disassem:define-instruction-format (reg/mem-imm 16
519 '(:name :tab reg/mem ", " imm))
520 (reg/mem :type 'sized-reg/mem)
521 (imm :type 'imm-data))
523 ;;; Same as reg/mem, but with using the accumulator in the default printer
524 (sb!disassem:define-instruction-format
526 :include 'reg/mem :default-printer '(:name :tab accum ", " reg/mem))
527 (reg/mem :type 'reg/mem) ; don't need a size
528 (accum :type 'accum))
530 ;;; Same as reg-reg/mem, but with a prefix of #b00001111
531 (sb!disassem:define-instruction-format (ext-reg-reg/mem 24
533 `(:name :tab reg ", " reg/mem))
534 (prefix :field (byte 8 0) :value #b00001111)
535 (op :field (byte 7 9))
536 (width :field (byte 1 8) :type 'width)
537 (reg/mem :fields (list (byte 2 22) (byte 3 16))
539 (reg :field (byte 3 19) :type 'reg)
543 ;;; reg-no-width with #x0f prefix
544 (sb!disassem:define-instruction-format (ext-reg-no-width 16
545 :default-printer '(:name :tab reg))
546 (prefix :field (byte 8 0) :value #b00001111)
547 (op :field (byte 5 11))
548 (reg :field (byte 3 8) :type 'reg))
550 ;;; Same as reg/mem, but with a prefix of #b00001111
551 (sb!disassem:define-instruction-format (ext-reg/mem 24
552 :default-printer '(:name :tab reg/mem))
553 (prefix :field (byte 8 0) :value #b00001111)
554 (op :fields (list (byte 7 9) (byte 3 19)))
555 (width :field (byte 1 8) :type 'width)
556 (reg/mem :fields (list (byte 2 22) (byte 3 16))
557 :type 'sized-reg/mem)
561 (sb!disassem:define-instruction-format (ext-reg/mem-imm 24
562 :include 'ext-reg/mem
564 '(:name :tab reg/mem ", " imm))
565 (imm :type 'imm-data))
567 ;;;; This section was added by jrd, for fp instructions.
569 ;;; regular fp inst to/from registers/memory
570 (sb!disassem:define-instruction-format (floating-point 16
572 `(:name :tab reg/mem))
573 (prefix :field (byte 5 3) :value #b11011)
574 (op :fields (list (byte 3 0) (byte 3 11)))
575 (reg/mem :fields (list (byte 2 14) (byte 3 8)) :type 'reg/mem))
577 ;;; fp insn to/from fp reg
578 (sb!disassem:define-instruction-format (floating-point-fp 16
579 :default-printer `(:name :tab fp-reg))
580 (prefix :field (byte 5 3) :value #b11011)
581 (suffix :field (byte 2 14) :value #b11)
582 (op :fields (list (byte 3 0) (byte 3 11)))
583 (fp-reg :field (byte 3 8) :type 'fp-reg))
585 ;;; fp insn to/from fp reg, with the reversed source/destination flag.
586 (sb!disassem:define-instruction-format
587 (floating-point-fp-d 16
588 :default-printer `(:name :tab ,(swap-if 'd "ST0" ", " 'fp-reg)))
589 (prefix :field (byte 5 3) :value #b11011)
590 (suffix :field (byte 2 14) :value #b11)
591 (op :fields (list (byte 2 0) (byte 3 11)))
592 (d :field (byte 1 2))
593 (fp-reg :field (byte 3 8) :type 'fp-reg))
596 ;;; (added by (?) pfw)
597 ;;; fp no operand isns
598 (sb!disassem:define-instruction-format (floating-point-no 16
599 :default-printer '(:name))
600 (prefix :field (byte 8 0) :value #b11011001)
601 (suffix :field (byte 3 13) :value #b111)
602 (op :field (byte 5 8)))
604 (sb!disassem:define-instruction-format (floating-point-3 16
605 :default-printer '(:name))
606 (prefix :field (byte 5 3) :value #b11011)
607 (suffix :field (byte 2 14) :value #b11)
608 (op :fields (list (byte 3 0) (byte 6 8))))
610 (sb!disassem:define-instruction-format (floating-point-5 16
611 :default-printer '(:name))
612 (prefix :field (byte 8 0) :value #b11011011)
613 (suffix :field (byte 3 13) :value #b111)
614 (op :field (byte 5 8)))
616 (sb!disassem:define-instruction-format (floating-point-st 16
617 :default-printer '(:name))
618 (prefix :field (byte 8 0) :value #b11011111)
619 (suffix :field (byte 3 13) :value #b111)
620 (op :field (byte 5 8)))
622 (sb!disassem:define-instruction-format (string-op 8
624 :default-printer '(:name width)))
626 (sb!disassem:define-instruction-format (short-cond-jump 16)
627 (op :field (byte 4 4))
628 (cc :field (byte 4 0) :type 'condition-code)
629 (label :field (byte 8 8) :type 'displacement))
631 (sb!disassem:define-instruction-format (short-jump 16
632 :default-printer '(:name :tab label))
633 (const :field (byte 4 4) :value #b1110)
634 (op :field (byte 4 0))
635 (label :field (byte 8 8) :type 'displacement))
637 (sb!disassem:define-instruction-format (near-cond-jump 16)
638 (op :fields (list (byte 8 0) (byte 4 12)) :value '(#b00001111 #b1000))
639 (cc :field (byte 4 8) :type 'condition-code)
640 ;; The disassembler currently doesn't let you have an instruction > 32 bits
641 ;; long, so we fake it by using a prefilter to read the offset.
642 (label :type 'displacement
643 :prefilter (lambda (value dstate)
644 (declare (ignore value)) ; always nil anyway
645 (sb!disassem:read-signed-suffix 32 dstate))))
647 (sb!disassem:define-instruction-format (near-jump 8
648 :default-printer '(:name :tab label))
649 (op :field (byte 8 0))
650 ;; The disassembler currently doesn't let you have an instruction > 32 bits
651 ;; long, so we fake it by using a prefilter to read the address.
652 (label :type 'displacement
653 :prefilter (lambda (value dstate)
654 (declare (ignore value)) ; always nil anyway
655 (sb!disassem:read-signed-suffix 32 dstate))))
658 (sb!disassem:define-instruction-format (cond-set 24
659 :default-printer '('set cc :tab reg/mem))
660 (prefix :field (byte 8 0) :value #b00001111)
661 (op :field (byte 4 12) :value #b1001)
662 (cc :field (byte 4 8) :type 'condition-code)
663 (reg/mem :fields (list (byte 2 22) (byte 3 16))
665 (reg :field (byte 3 19) :value #b000))
667 (sb!disassem:define-instruction-format (cond-move 24
669 '('cmov cc :tab reg ", " reg/mem))
670 (prefix :field (byte 8 0) :value #b00001111)
671 (op :field (byte 4 12) :value #b0100)
672 (cc :field (byte 4 8) :type 'condition-code)
673 (reg/mem :fields (list (byte 2 22) (byte 3 16))
675 (reg :field (byte 3 19) :type 'reg))
677 (sb!disassem:define-instruction-format (enter-format 32
678 :default-printer '(:name
680 (:unless (:constant 0)
682 (op :field (byte 8 0))
683 (disp :field (byte 16 8))
684 (level :field (byte 8 24)))
686 (sb!disassem:define-instruction-format (prefetch 24
688 '(:name ", " reg/mem))
689 (prefix :field (byte 8 0) :value #b00001111)
690 (op :field (byte 8 8) :value #b00011000)
691 (reg/mem :fields (list (byte 2 22) (byte 3 16)) :type 'byte-reg/mem)
692 (reg :field (byte 3 19) :type 'reg))
694 ;;; Single byte instruction with an immediate byte argument.
695 (sb!disassem:define-instruction-format (byte-imm 16
696 :default-printer '(:name :tab code))
697 (op :field (byte 8 0))
698 (code :field (byte 8 8)))
700 ;;; Two byte instruction with an immediate byte argument.
702 (sb!disassem:define-instruction-format (word-imm 24
703 :default-printer '(:name :tab code))
704 (op :field (byte 16 0))
705 (code :field (byte 8 16)))
708 ;;;; primitive emitters
710 (define-bitfield-emitter emit-word 16
713 (define-bitfield-emitter emit-dword 32
716 (define-bitfield-emitter emit-byte-with-reg 8
717 (byte 5 3) (byte 3 0))
719 (define-bitfield-emitter emit-mod-reg-r/m-byte 8
720 (byte 2 6) (byte 3 3) (byte 3 0))
722 (define-bitfield-emitter emit-sib-byte 8
723 (byte 2 6) (byte 3 3) (byte 3 0))
727 (defun emit-absolute-fixup (segment fixup)
728 (note-fixup segment :absolute fixup)
729 (let ((offset (fixup-offset fixup)))
731 (emit-back-patch segment
732 4 ; FIXME: n-word-bytes
733 (lambda (segment posn)
734 (declare (ignore posn))
736 (- (+ (component-header-length)
737 (or (label-position offset)
739 other-pointer-lowtag))))
740 (emit-dword segment (or offset 0)))))
742 (defun emit-relative-fixup (segment fixup)
743 (note-fixup segment :relative fixup)
744 (emit-dword segment (or (fixup-offset fixup) 0)))
746 ;;;; the effective-address (ea) structure
748 (defun reg-tn-encoding (tn)
749 (declare (type tn tn))
750 (aver (eq (sb-name (sc-sb (tn-sc tn))) 'registers))
751 (let ((offset (tn-offset tn)))
752 (logior (ash (logand offset 1) 2)
755 (defstruct (ea (:constructor make-ea (size &key base index scale disp))
757 (size nil :type (member :byte :word :dword))
758 (base nil :type (or tn null))
759 (index nil :type (or tn null))
760 (scale 1 :type (member 1 2 4 8))
761 (disp 0 :type (or (unsigned-byte 32) (signed-byte 32) fixup)))
762 (def!method print-object ((ea ea) stream)
763 (cond ((or *print-escape* *print-readably*)
764 (print-unreadable-object (ea stream :type t)
766 "~S~@[ base=~S~]~@[ index=~S~]~@[ scale=~S~]~@[ disp=~S~]"
770 (let ((scale (ea-scale ea)))
771 (if (= scale 1) nil scale))
774 (format stream "~A PTR [" (symbol-name (ea-size ea)))
776 (write-string (sb!c::location-print-name (ea-base ea)) stream)
778 (write-string "+" stream)))
780 (write-string (sb!c::location-print-name (ea-index ea)) stream))
781 (unless (= (ea-scale ea) 1)
782 (format stream "*~A" (ea-scale ea)))
783 (typecase (ea-disp ea)
786 (format stream "~@D" (ea-disp ea)))
788 (format stream "+~A" (ea-disp ea))))
789 (write-char #\] stream))))
791 (defun emit-ea (segment thing reg &optional allow-constants)
794 (ecase (sb-name (sc-sb (tn-sc thing)))
796 (emit-mod-reg-r/m-byte segment #b11 reg (reg-tn-encoding thing)))
798 ;; Convert stack tns into an index off of EBP.
799 (let ((disp (frame-byte-offset (tn-offset thing))))
800 (cond ((<= -128 disp 127)
801 (emit-mod-reg-r/m-byte segment #b01 reg #b101)
802 (emit-byte segment disp))
804 (emit-mod-reg-r/m-byte segment #b10 reg #b101)
805 (emit-dword segment disp)))))
807 (unless allow-constants
809 "Constant TNs can only be directly used in MOV, PUSH, and CMP."))
810 (emit-mod-reg-r/m-byte segment #b00 reg #b101)
811 (emit-absolute-fixup segment
814 (- (* (tn-offset thing) n-word-bytes)
815 other-pointer-lowtag))))))
817 (let* ((base (ea-base thing))
818 (index (ea-index thing))
819 (scale (ea-scale thing))
820 (disp (ea-disp thing))
821 (mod (cond ((or (null base)
823 (not (= (reg-tn-encoding base) #b101))))
825 ((and (fixnump disp) (<= -128 disp 127))
829 (r/m (cond (index #b100)
831 (t (reg-tn-encoding base)))))
832 (when (and (fixup-p disp)
833 (label-p (fixup-offset disp)))
836 (return-from emit-ea (emit-ea segment disp reg allow-constants)))
837 (emit-mod-reg-r/m-byte segment mod reg r/m)
839 (let ((ss (1- (integer-length scale)))
840 (index (if (null index)
842 (let ((index (reg-tn-encoding index)))
844 (error "can't index off of ESP")
846 (base (if (null base)
848 (reg-tn-encoding base))))
849 (emit-sib-byte segment ss index base)))
851 (emit-byte segment disp))
852 ((or (= mod #b10) (null base))
854 (emit-absolute-fixup segment disp)
855 (emit-dword segment disp))))))
857 (emit-mod-reg-r/m-byte segment #b00 reg #b101)
858 (emit-absolute-fixup segment thing))))
860 (defun fp-reg-tn-p (thing)
862 (eq (sb-name (sc-sb (tn-sc thing))) 'float-registers)))
864 ;;; like the above, but for fp-instructions--jrd
865 (defun emit-fp-op (segment thing op)
866 (if (fp-reg-tn-p thing)
867 (emit-byte segment (dpb op (byte 3 3) (dpb (tn-offset thing)
870 (emit-ea segment thing op)))
872 (defun byte-reg-p (thing)
874 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)
875 (member (sc-name (tn-sc thing)) *byte-sc-names*)
878 (defun byte-ea-p (thing)
880 (ea (eq (ea-size thing) :byte))
882 (and (member (sc-name (tn-sc thing)) *byte-sc-names*) t))
885 (defun word-reg-p (thing)
887 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)
888 (member (sc-name (tn-sc thing)) *word-sc-names*)
891 (defun word-ea-p (thing)
893 (ea (eq (ea-size thing) :word))
894 (tn (and (member (sc-name (tn-sc thing)) *word-sc-names*) t))
897 (defun dword-reg-p (thing)
899 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)
900 (member (sc-name (tn-sc thing)) *dword-sc-names*)
903 (defun dword-ea-p (thing)
905 (ea (eq (ea-size thing) :dword))
907 (and (member (sc-name (tn-sc thing)) *dword-sc-names*) t))
910 (defun register-p (thing)
912 (eq (sb-name (sc-sb (tn-sc thing))) 'registers)))
914 (defun accumulator-p (thing)
915 (and (register-p thing)
916 (= (tn-offset thing) 0)))
920 (def!constant +operand-size-prefix-byte+ #b01100110)
922 (defun maybe-emit-operand-size-prefix (segment size)
923 (unless (or (eq size :byte) (eq size +default-operand-size+))
924 (emit-byte segment +operand-size-prefix-byte+)))
926 (defun operand-size (thing)
929 ;; FIXME: might as well be COND instead of having to use #. readmacro
930 ;; to hack up the code
931 (case (sc-name (tn-sc thing))
938 ;; added by jrd: float-registers is a separate size (?)
944 (error "can't tell the size of ~S ~S" thing (sc-name (tn-sc thing))))))
950 (defun matching-operand-size (dst src)
951 (let ((dst-size (operand-size dst))
952 (src-size (operand-size src)))
955 (if (eq dst-size src-size)
957 (error "size mismatch: ~S is a ~S and ~S is a ~S."
958 dst dst-size src src-size))
962 (error "can't tell the size of either ~S or ~S" dst src)))))
964 (defun emit-sized-immediate (segment size value)
967 (emit-byte segment value))
969 (emit-word segment value))
971 (emit-dword segment value))))
975 (define-instruction x66 (segment)
976 (:printer x66 () nil :print-name nil)
978 (bug "#X66 prefix used as a standalone instruction")))
980 (defun emit-prefix (segment name)
985 (emit-byte segment #xf0))
987 (emit-byte segment #x64))
989 (emit-byte segment #x65))))
991 (define-instruction fs (segment)
992 (:printer seg ((fsgs #b0)) nil :print-name nil)
994 (bug "FS prefix used as a standalone instruction")))
996 (define-instruction gs (segment)
997 (:printer seg ((fsgs #b1)) nil :print-name nil)
999 (bug "GS prefix used as a standalone instruction")))
1001 (define-instruction lock (segment)
1002 (:printer byte ((op #b11110000)) nil)
1004 (bug "LOCK prefix used as a standalone instruction")))
1006 (define-instruction rep (segment)
1008 (emit-byte segment #b11110011)))
1010 (define-instruction repe (segment)
1011 (:printer byte ((op #b11110011)) nil)
1013 (emit-byte segment #b11110011)))
1015 (define-instruction repne (segment)
1016 (:printer byte ((op #b11110010)) nil)
1018 (emit-byte segment #b11110010)))
1020 ;;;; general data transfer
1022 (define-instruction mov (segment dst src &optional prefix)
1023 ;; immediate to register
1024 (:printer reg ((op #b1011) (imm nil :type 'imm-data))
1025 '(:name :tab reg ", " imm))
1026 ;; absolute mem to/from accumulator
1027 (:printer simple-dir ((op #b101000) (imm nil :type 'imm-addr))
1028 `(:name :tab ,(swap-if 'dir 'accum ", " '("[" imm "]"))))
1029 ;; register to/from register/memory
1030 (:printer reg-reg/mem-dir ((op #b100010)))
1031 ;; immediate to register/memory
1032 (:printer reg/mem-imm ((op '(#b1100011 #b000))))
1035 (emit-prefix segment prefix)
1036 (let ((size (matching-operand-size dst src)))
1037 (maybe-emit-operand-size-prefix segment size)
1038 (cond ((register-p dst)
1039 (cond ((integerp src)
1040 (emit-byte-with-reg segment
1044 (reg-tn-encoding dst))
1045 (emit-sized-immediate segment size src))
1046 ((and (fixup-p src) (accumulator-p dst))
1051 (emit-absolute-fixup segment src))
1057 (emit-ea segment src (reg-tn-encoding dst) t))))
1058 ((and (fixup-p dst) (accumulator-p src))
1059 (emit-byte segment (if (eq size :byte) #b10100010 #b10100011))
1060 (emit-absolute-fixup segment dst))
1062 (emit-byte segment (if (eq size :byte) #b11000110 #b11000111))
1063 (emit-ea segment dst #b000)
1064 (emit-sized-immediate segment size src))
1066 (emit-byte segment (if (eq size :byte) #b10001000 #b10001001))
1067 (emit-ea segment dst (reg-tn-encoding src)))
1069 (aver (eq size :dword))
1070 (emit-byte segment #b11000111)
1071 (emit-ea segment dst #b000)
1072 (emit-absolute-fixup segment src))
1074 (error "bogus arguments to MOV: ~S ~S" dst src))))))
1076 (defun emit-move-with-extension (segment dst src opcode)
1077 (aver (register-p dst))
1078 (let ((dst-size (operand-size dst))
1079 (src-size (operand-size src)))
1082 (aver (eq src-size :byte))
1083 (maybe-emit-operand-size-prefix segment :word)
1084 (emit-byte segment #b00001111)
1085 (emit-byte segment opcode)
1086 (emit-ea segment src (reg-tn-encoding dst)))
1090 (maybe-emit-operand-size-prefix segment :dword)
1091 (emit-byte segment #b00001111)
1092 (emit-byte segment opcode)
1093 (emit-ea segment src (reg-tn-encoding dst)))
1095 (emit-byte segment #b00001111)
1096 (emit-byte segment (logior opcode 1))
1097 (emit-ea segment src (reg-tn-encoding dst))))))))
1099 (define-instruction movsx (segment dst src)
1100 (:printer ext-reg-reg/mem ((op #b1011111)
1101 (reg nil :type 'word-reg)
1102 (reg/mem nil :type 'sized-reg/mem)))
1103 (:emitter (emit-move-with-extension segment dst src #b10111110)))
1105 (define-instruction movzx (segment dst src)
1106 (:printer ext-reg-reg/mem ((op #b1011011)
1107 (reg nil :type 'word-reg)
1108 (reg/mem nil :type 'sized-reg/mem)))
1109 (:emitter (emit-move-with-extension segment dst src #b10110110)))
1111 (define-instruction push (segment src &optional prefix)
1113 (:printer reg-no-width ((op #b01010)))
1115 (:printer reg/mem ((op '(#b1111111 #b110)) (width 1)))
1117 (:printer byte ((op #b01101010) (imm nil :type 'signed-imm-byte))
1119 (:printer byte ((op #b01101000) (imm nil :type 'imm-word))
1121 ;; ### segment registers?
1124 (emit-prefix segment prefix)
1125 (cond ((integerp src)
1126 (cond ((<= -128 src 127)
1127 (emit-byte segment #b01101010)
1128 (emit-byte segment src))
1130 (emit-byte segment #b01101000)
1131 (emit-dword segment src))))
1133 ;; Interpret the fixup as an immediate dword to push.
1134 (emit-byte segment #b01101000)
1135 (emit-absolute-fixup segment src))
1137 (let ((size (operand-size src)))
1138 (aver (not (eq size :byte)))
1139 (maybe-emit-operand-size-prefix segment size)
1140 (cond ((register-p src)
1141 (emit-byte-with-reg segment #b01010 (reg-tn-encoding src)))
1143 (emit-byte segment #b11111111)
1144 (emit-ea segment src #b110 t))))))))
1146 (define-instruction pusha (segment)
1147 (:printer byte ((op #b01100000)))
1149 (emit-byte segment #b01100000)))
1151 (define-instruction pop (segment dst)
1152 (:printer reg-no-width ((op #b01011)))
1153 (:printer reg/mem ((op '(#b1000111 #b000)) (width 1)))
1155 (let ((size (operand-size dst)))
1156 (aver (not (eq size :byte)))
1157 (maybe-emit-operand-size-prefix segment size)
1158 (cond ((register-p dst)
1159 (emit-byte-with-reg segment #b01011 (reg-tn-encoding dst)))
1161 (emit-byte segment #b10001111)
1162 (emit-ea segment dst #b000))))))
1164 (define-instruction popa (segment)
1165 (:printer byte ((op #b01100001)))
1167 (emit-byte segment #b01100001)))
1169 (define-instruction xchg (segment operand1 operand2)
1170 ;; Register with accumulator.
1171 (:printer reg-no-width ((op #b10010)) '(:name :tab accum ", " reg))
1172 ;; Register/Memory with Register.
1173 (:printer reg-reg/mem ((op #b1000011)))
1175 (let ((size (matching-operand-size operand1 operand2)))
1176 (maybe-emit-operand-size-prefix segment size)
1177 (labels ((xchg-acc-with-something (acc something)
1178 (if (and (not (eq size :byte)) (register-p something))
1179 (emit-byte-with-reg segment
1181 (reg-tn-encoding something))
1182 (xchg-reg-with-something acc something)))
1183 (xchg-reg-with-something (reg something)
1184 (emit-byte segment (if (eq size :byte) #b10000110 #b10000111))
1185 (emit-ea segment something (reg-tn-encoding reg))))
1186 (cond ((accumulator-p operand1)
1187 (xchg-acc-with-something operand1 operand2))
1188 ((accumulator-p operand2)
1189 (xchg-acc-with-something operand2 operand1))
1190 ((register-p operand1)
1191 (xchg-reg-with-something operand1 operand2))
1192 ((register-p operand2)
1193 (xchg-reg-with-something operand2 operand1))
1195 (error "bogus args to XCHG: ~S ~S" operand1 operand2)))))))
1197 (define-instruction lea (segment dst src)
1198 (:printer reg-reg/mem ((op #b1000110) (width 1)))
1200 (aver (dword-reg-p dst))
1201 (emit-byte segment #b10001101)
1202 (emit-ea segment src (reg-tn-encoding dst))))
1204 (define-instruction cmpxchg (segment dst src &optional prefix)
1205 ;; Register/Memory with Register.
1206 (:printer ext-reg-reg/mem ((op #b1011000)) '(:name :tab reg/mem ", " reg))
1208 (aver (register-p src))
1209 (emit-prefix segment prefix)
1210 (let ((size (matching-operand-size src dst)))
1211 (maybe-emit-operand-size-prefix segment size)
1212 (emit-byte segment #b00001111)
1213 (emit-byte segment (if (eq size :byte) #b10110000 #b10110001))
1214 (emit-ea segment dst (reg-tn-encoding src)))))
1216 (define-instruction pause (segment)
1217 (:printer two-bytes ((op '(#xf3 #x90))))
1219 (emit-byte segment #xf3)
1220 (emit-byte segment #x90)))
1222 ;;;; flag control instructions
1224 ;;; CLC -- Clear Carry Flag.
1225 (define-instruction clc (segment)
1226 (:printer byte ((op #b11111000)))
1228 (emit-byte segment #b11111000)))
1230 ;;; CLD -- Clear Direction Flag.
1231 (define-instruction cld (segment)
1232 (:printer byte ((op #b11111100)))
1234 (emit-byte segment #b11111100)))
1236 ;;; CLI -- Clear Iterrupt Enable Flag.
1237 (define-instruction cli (segment)
1238 (:printer byte ((op #b11111010)))
1240 (emit-byte segment #b11111010)))
1242 ;;; CMC -- Complement Carry Flag.
1243 (define-instruction cmc (segment)
1244 (:printer byte ((op #b11110101)))
1246 (emit-byte segment #b11110101)))
1248 ;;; LAHF -- Load AH into flags.
1249 (define-instruction lahf (segment)
1250 (:printer byte ((op #b10011111)))
1252 (emit-byte segment #b10011111)))
1254 ;;; POPF -- Pop flags.
1255 (define-instruction popf (segment)
1256 (:printer byte ((op #b10011101)))
1258 (emit-byte segment #b10011101)))
1260 ;;; PUSHF -- push flags.
1261 (define-instruction pushf (segment)
1262 (:printer byte ((op #b10011100)))
1264 (emit-byte segment #b10011100)))
1266 ;;; SAHF -- Store AH into flags.
1267 (define-instruction sahf (segment)
1268 (:printer byte ((op #b10011110)))
1270 (emit-byte segment #b10011110)))
1272 ;;; STC -- Set Carry Flag.
1273 (define-instruction stc (segment)
1274 (:printer byte ((op #b11111001)))
1276 (emit-byte segment #b11111001)))
1278 ;;; STD -- Set Direction Flag.
1279 (define-instruction std (segment)
1280 (:printer byte ((op #b11111101)))
1282 (emit-byte segment #b11111101)))
1284 ;;; STI -- Set Interrupt Enable Flag.
1285 (define-instruction sti (segment)
1286 (:printer byte ((op #b11111011)))
1288 (emit-byte segment #b11111011)))
1292 (defun emit-random-arith-inst (name segment dst src opcode
1293 &optional allow-constants)
1294 (let ((size (matching-operand-size dst src)))
1295 (maybe-emit-operand-size-prefix segment size)
1298 (cond ((and (not (eq size :byte)) (<= -128 src 127))
1299 (emit-byte segment #b10000011)
1300 (emit-ea segment dst opcode allow-constants)
1301 (emit-byte segment src))
1302 ((accumulator-p dst)
1309 (emit-sized-immediate segment size src))
1311 (emit-byte segment (if (eq size :byte) #b10000000 #b10000001))
1312 (emit-ea segment dst opcode allow-constants)
1313 (emit-sized-immediate segment size src))))
1318 (if (eq size :byte) #b00000000 #b00000001)))
1319 (emit-ea segment dst (reg-tn-encoding src) allow-constants))
1324 (if (eq size :byte) #b00000010 #b00000011)))
1325 (emit-ea segment src (reg-tn-encoding dst) allow-constants))
1327 (error "bogus operands to ~A" name)))))
1329 (eval-when (:compile-toplevel :execute)
1330 (defun arith-inst-printer-list (subop)
1331 `((accum-imm ((op ,(dpb subop (byte 3 2) #b0000010))))
1332 (reg/mem-imm ((op (#b1000000 ,subop))))
1333 (reg/mem-imm ((op (#b1000001 ,subop))
1334 (imm nil :type signed-imm-byte)))
1335 (reg-reg/mem-dir ((op ,(dpb subop (byte 3 1) #b000000)))))))
1337 (define-instruction add (segment dst src &optional prefix)
1338 (:printer-list (arith-inst-printer-list #b000))
1340 (emit-prefix segment prefix)
1341 (emit-random-arith-inst "ADD" segment dst src #b000)))
1343 (define-instruction adc (segment dst src)
1344 (:printer-list (arith-inst-printer-list #b010))
1345 (:emitter (emit-random-arith-inst "ADC" segment dst src #b010)))
1347 (define-instruction sub (segment dst src &optional prefix)
1348 (:printer-list (arith-inst-printer-list #b101))
1350 (emit-prefix segment prefix)
1351 (emit-random-arith-inst "SUB" segment dst src #b101)))
1353 (define-instruction sbb (segment dst src)
1354 (:printer-list (arith-inst-printer-list #b011))
1355 (:emitter (emit-random-arith-inst "SBB" segment dst src #b011)))
1357 (define-instruction cmp (segment dst src &optional prefix)
1358 (:printer-list (arith-inst-printer-list #b111))
1360 (emit-prefix segment prefix)
1361 (emit-random-arith-inst "CMP" segment dst src #b111 t)))
1363 (define-instruction inc (segment dst)
1365 (:printer reg-no-width ((op #b01000)))
1367 (:printer reg/mem ((op '(#b1111111 #b000))))
1369 (let ((size (operand-size dst)))
1370 (maybe-emit-operand-size-prefix segment size)
1371 (cond ((and (not (eq size :byte)) (register-p dst))
1372 (emit-byte-with-reg segment #b01000 (reg-tn-encoding dst)))
1374 (emit-byte segment (if (eq size :byte) #b11111110 #b11111111))
1375 (emit-ea segment dst #b000))))))
1377 (define-instruction dec (segment dst)
1379 (:printer reg-no-width ((op #b01001)))
1381 (:printer reg/mem ((op '(#b1111111 #b001))))
1383 (let ((size (operand-size dst)))
1384 (maybe-emit-operand-size-prefix segment size)
1385 (cond ((and (not (eq size :byte)) (register-p dst))
1386 (emit-byte-with-reg segment #b01001 (reg-tn-encoding dst)))
1388 (emit-byte segment (if (eq size :byte) #b11111110 #b11111111))
1389 (emit-ea segment dst #b001))))))
1391 (define-instruction neg (segment dst)
1392 (:printer reg/mem ((op '(#b1111011 #b011))))
1394 (let ((size (operand-size dst)))
1395 (maybe-emit-operand-size-prefix segment size)
1396 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
1397 (emit-ea segment dst #b011))))
1399 (define-instruction aaa (segment)
1400 (:printer byte ((op #b00110111)))
1402 (emit-byte segment #b00110111)))
1404 (define-instruction aas (segment)
1405 (:printer byte ((op #b00111111)))
1407 (emit-byte segment #b00111111)))
1409 (define-instruction daa (segment)
1410 (:printer byte ((op #b00100111)))
1412 (emit-byte segment #b00100111)))
1414 (define-instruction das (segment)
1415 (:printer byte ((op #b00101111)))
1417 (emit-byte segment #b00101111)))
1419 (define-instruction mul (segment dst src)
1420 (:printer accum-reg/mem ((op '(#b1111011 #b100))))
1422 (let ((size (matching-operand-size dst src)))
1423 (aver (accumulator-p dst))
1424 (maybe-emit-operand-size-prefix segment size)
1425 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
1426 (emit-ea segment src #b100))))
1428 (define-instruction imul (segment dst &optional src1 src2)
1429 (:printer accum-reg/mem ((op '(#b1111011 #b101))))
1430 (:printer ext-reg-reg/mem ((op #b1010111)))
1431 (:printer reg-reg/mem ((op #b0110100) (width 1)
1432 (imm nil :type 'signed-imm-word))
1433 '(:name :tab reg ", " reg/mem ", " imm))
1434 (:printer reg-reg/mem ((op #b0110101) (width 1)
1435 (imm nil :type 'signed-imm-byte))
1436 '(:name :tab reg ", " reg/mem ", " imm))
1438 (flet ((r/m-with-immed-to-reg (reg r/m immed)
1439 (let* ((size (matching-operand-size reg r/m))
1440 (sx (and (not (eq size :byte)) (<= -128 immed 127))))
1441 (maybe-emit-operand-size-prefix segment size)
1442 (emit-byte segment (if sx #b01101011 #b01101001))
1443 (emit-ea segment r/m (reg-tn-encoding reg))
1445 (emit-byte segment immed)
1446 (emit-sized-immediate segment size immed)))))
1448 (r/m-with-immed-to-reg dst src1 src2))
1451 (r/m-with-immed-to-reg dst dst src1)
1452 (let ((size (matching-operand-size dst src1)))
1453 (maybe-emit-operand-size-prefix segment size)
1454 (emit-byte segment #b00001111)
1455 (emit-byte segment #b10101111)
1456 (emit-ea segment src1 (reg-tn-encoding dst)))))
1458 (let ((size (operand-size dst)))
1459 (maybe-emit-operand-size-prefix segment size)
1460 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
1461 (emit-ea segment dst #b101)))))))
1463 (define-instruction div (segment dst src)
1464 (:printer accum-reg/mem ((op '(#b1111011 #b110))))
1466 (let ((size (matching-operand-size dst src)))
1467 (aver (accumulator-p dst))
1468 (maybe-emit-operand-size-prefix segment size)
1469 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
1470 (emit-ea segment src #b110))))
1472 (define-instruction idiv (segment dst src)
1473 (:printer accum-reg/mem ((op '(#b1111011 #b111))))
1475 (let ((size (matching-operand-size dst src)))
1476 (aver (accumulator-p dst))
1477 (maybe-emit-operand-size-prefix segment size)
1478 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
1479 (emit-ea segment src #b111))))
1481 (define-instruction aad (segment)
1482 (:printer two-bytes ((op '(#b11010101 #b00001010))))
1484 (emit-byte segment #b11010101)
1485 (emit-byte segment #b00001010)))
1487 (define-instruction aam (segment)
1488 (:printer two-bytes ((op '(#b11010100 #b00001010))))
1490 (emit-byte segment #b11010100)
1491 (emit-byte segment #b00001010)))
1493 (define-instruction bswap (segment dst)
1494 (:printer ext-reg-no-width ((op #b11001)))
1496 (emit-byte segment #x0f)
1497 (emit-byte-with-reg segment #b11001 (reg-tn-encoding dst))))
1499 ;;; CBW -- Convert Byte to Word. AX <- sign_xtnd(AL)
1500 (define-instruction cbw (segment)
1501 (:printer two-bytes ((op '(#b01100110 #b10011000))))
1503 (maybe-emit-operand-size-prefix segment :word)
1504 (emit-byte segment #b10011000)))
1506 ;;; CWDE -- Convert Word To Double Word Extened. EAX <- sign_xtnd(AX)
1507 (define-instruction cwde (segment)
1508 (:printer byte ((op #b10011000)))
1510 (maybe-emit-operand-size-prefix segment :dword)
1511 (emit-byte segment #b10011000)))
1513 ;;; CWD -- Convert Word to Double Word. DX:AX <- sign_xtnd(AX)
1514 (define-instruction cwd (segment)
1515 (:printer two-bytes ((op '(#b01100110 #b10011001))))
1517 (maybe-emit-operand-size-prefix segment :word)
1518 (emit-byte segment #b10011001)))
1520 ;;; CDQ -- Convert Double Word to Quad Word. EDX:EAX <- sign_xtnd(EAX)
1521 (define-instruction cdq (segment)
1522 (:printer byte ((op #b10011001)))
1524 (maybe-emit-operand-size-prefix segment :dword)
1525 (emit-byte segment #b10011001)))
1527 (define-instruction xadd (segment dst src &optional prefix)
1528 ;; Register/Memory with Register.
1529 (:printer ext-reg-reg/mem ((op #b1100000)) '(:name :tab reg/mem ", " reg))
1531 (aver (register-p src))
1532 (emit-prefix segment prefix)
1533 (let ((size (matching-operand-size src dst)))
1534 (maybe-emit-operand-size-prefix segment size)
1535 (emit-byte segment #b00001111)
1536 (emit-byte segment (if (eq size :byte) #b11000000 #b11000001))
1537 (emit-ea segment dst (reg-tn-encoding src)))))
1542 (defun emit-shift-inst (segment dst amount opcode)
1543 (let ((size (operand-size dst)))
1544 (maybe-emit-operand-size-prefix segment size)
1545 (multiple-value-bind (major-opcode immed)
1547 (:cl (values #b11010010 nil))
1548 (1 (values #b11010000 nil))
1549 (t (values #b11000000 t)))
1551 (if (eq size :byte) major-opcode (logior major-opcode 1)))
1552 (emit-ea segment dst opcode)
1554 (emit-byte segment amount)))))
1556 (eval-when (:compile-toplevel :execute)
1557 (defun shift-inst-printer-list (subop)
1558 `((reg/mem ((op (#b1101000 ,subop)))
1559 (:name :tab reg/mem ", 1"))
1560 (reg/mem ((op (#b1101001 ,subop)))
1561 (:name :tab reg/mem ", " 'cl))
1562 (reg/mem-imm ((op (#b1100000 ,subop))
1563 (imm nil :type signed-imm-byte))))))
1565 (define-instruction rol (segment dst amount)
1567 (shift-inst-printer-list #b000))
1569 (emit-shift-inst segment dst amount #b000)))
1571 (define-instruction ror (segment dst amount)
1573 (shift-inst-printer-list #b001))
1575 (emit-shift-inst segment dst amount #b001)))
1577 (define-instruction rcl (segment dst amount)
1579 (shift-inst-printer-list #b010))
1581 (emit-shift-inst segment dst amount #b010)))
1583 (define-instruction rcr (segment dst amount)
1585 (shift-inst-printer-list #b011))
1587 (emit-shift-inst segment dst amount #b011)))
1589 (define-instruction shl (segment dst amount)
1591 (shift-inst-printer-list #b100))
1593 (emit-shift-inst segment dst amount #b100)))
1595 (define-instruction shr (segment dst amount)
1597 (shift-inst-printer-list #b101))
1599 (emit-shift-inst segment dst amount #b101)))
1601 (define-instruction sar (segment dst amount)
1603 (shift-inst-printer-list #b111))
1605 (emit-shift-inst segment dst amount #b111)))
1607 (defun emit-double-shift (segment opcode dst src amt)
1608 (let ((size (matching-operand-size dst src)))
1609 (when (eq size :byte)
1610 (error "Double shifts can only be used with words."))
1611 (maybe-emit-operand-size-prefix segment size)
1612 (emit-byte segment #b00001111)
1613 (emit-byte segment (dpb opcode (byte 1 3)
1614 (if (eq amt :cl) #b10100101 #b10100100)))
1616 (emit-ea segment dst src)
1617 (emit-ea segment dst (reg-tn-encoding src)) ; pw tries this
1618 (unless (eq amt :cl)
1619 (emit-byte segment amt))))
1621 (eval-when (:compile-toplevel :execute)
1622 (defun double-shift-inst-printer-list (op)
1623 `((ext-reg-reg/mem ((op ,(logior op #b10)) (width 0)
1624 (imm nil :type signed-imm-byte)))
1625 (ext-reg-reg/mem ((op ,(logior op #b10)) (width 1))
1626 (:name :tab reg/mem ", " reg ", " 'cl)))))
1628 (define-instruction shld (segment dst src amt)
1629 (:declare (type (or (member :cl) (mod 32)) amt))
1630 (:printer-list (double-shift-inst-printer-list #b1010000))
1632 (emit-double-shift segment #b0 dst src amt)))
1634 (define-instruction shrd (segment dst src amt)
1635 (:declare (type (or (member :cl) (mod 32)) amt))
1636 (:printer-list (double-shift-inst-printer-list #b1010100))
1638 (emit-double-shift segment #b1 dst src amt)))
1640 (define-instruction and (segment dst src)
1642 (arith-inst-printer-list #b100))
1644 (emit-random-arith-inst "AND" segment dst src #b100)))
1646 (define-instruction test (segment this that)
1647 (:printer accum-imm ((op #b1010100)))
1648 (:printer reg/mem-imm ((op '(#b1111011 #b000))))
1649 (:printer reg-reg/mem ((op #b1000010)))
1651 (let ((size (matching-operand-size this that)))
1652 (maybe-emit-operand-size-prefix segment size)
1653 (flet ((test-immed-and-something (immed something)
1654 (cond ((accumulator-p something)
1656 (if (eq size :byte) #b10101000 #b10101001))
1657 (emit-sized-immediate segment size immed))
1660 (if (eq size :byte) #b11110110 #b11110111))
1661 (emit-ea segment something #b000)
1662 (emit-sized-immediate segment size immed))))
1663 (test-reg-and-something (reg something)
1664 (emit-byte segment (if (eq size :byte) #b10000100 #b10000101))
1665 (emit-ea segment something (reg-tn-encoding reg))))
1666 (cond ((integerp that)
1667 (test-immed-and-something that this))
1669 (test-immed-and-something this that))
1671 (test-reg-and-something this that))
1673 (test-reg-and-something that this))
1675 (error "bogus operands for TEST: ~S and ~S" this that)))))))
1677 ;;; Emit the most compact form of the test immediate instruction,
1678 ;;; using an 8 bit test when the immediate is only 8 bits and the
1679 ;;; value is one of the four low registers (eax, ebx, ecx, edx) or the
1681 (defun emit-optimized-test-inst (x y)
1684 (let ((offset (tn-offset x)))
1685 (cond ((and (sc-is x any-reg descriptor-reg)
1686 (or (= offset eax-offset) (= offset ebx-offset)
1687 (= offset ecx-offset) (= offset edx-offset)))
1688 (inst test (make-random-tn :kind :normal
1689 :sc (sc-or-lose 'byte-reg)
1692 ((sc-is x control-stack)
1693 (inst test (make-ea :byte :base ebp-tn
1694 :disp (frame-byte-offset offset))
1701 (define-instruction or (segment dst src &optional prefix)
1703 (arith-inst-printer-list #b001))
1705 (emit-prefix segment prefix)
1706 (emit-random-arith-inst "OR" segment dst src #b001)))
1708 (define-instruction xor (segment dst src &optional prefix)
1710 (arith-inst-printer-list #b110))
1712 (emit-prefix segment prefix)
1713 (emit-random-arith-inst "XOR" segment dst src #b110)))
1715 (define-instruction not (segment dst)
1716 (:printer reg/mem ((op '(#b1111011 #b010))))
1718 (let ((size (operand-size dst)))
1719 (maybe-emit-operand-size-prefix segment size)
1720 (emit-byte segment (if (eq size :byte) #b11110110 #b11110111))
1721 (emit-ea segment dst #b010))))
1723 ;;;; string manipulation
1725 (define-instruction cmps (segment size)
1726 (:printer string-op ((op #b1010011)))
1728 (maybe-emit-operand-size-prefix segment size)
1729 (emit-byte segment (if (eq size :byte) #b10100110 #b10100111))))
1731 (define-instruction ins (segment acc)
1732 (:printer string-op ((op #b0110110)))
1734 (let ((size (operand-size acc)))
1735 (aver (accumulator-p acc))
1736 (maybe-emit-operand-size-prefix segment size)
1737 (emit-byte segment (if (eq size :byte) #b01101100 #b01101101)))))
1739 (define-instruction lods (segment acc)
1740 (:printer string-op ((op #b1010110)))
1742 (let ((size (operand-size acc)))
1743 (aver (accumulator-p acc))
1744 (maybe-emit-operand-size-prefix segment size)
1745 (emit-byte segment (if (eq size :byte) #b10101100 #b10101101)))))
1747 (define-instruction movs (segment size)
1748 (:printer string-op ((op #b1010010)))
1750 (maybe-emit-operand-size-prefix segment size)
1751 (emit-byte segment (if (eq size :byte) #b10100100 #b10100101))))
1753 (define-instruction outs (segment acc)
1754 (:printer string-op ((op #b0110111)))
1756 (let ((size (operand-size acc)))
1757 (aver (accumulator-p acc))
1758 (maybe-emit-operand-size-prefix segment size)
1759 (emit-byte segment (if (eq size :byte) #b01101110 #b01101111)))))
1761 (define-instruction scas (segment acc)
1762 (:printer string-op ((op #b1010111)))
1764 (let ((size (operand-size acc)))
1765 (aver (accumulator-p acc))
1766 (maybe-emit-operand-size-prefix segment size)
1767 (emit-byte segment (if (eq size :byte) #b10101110 #b10101111)))))
1769 (define-instruction stos (segment acc)
1770 (:printer string-op ((op #b1010101)))
1772 (let ((size (operand-size acc)))
1773 (aver (accumulator-p acc))
1774 (maybe-emit-operand-size-prefix segment size)
1775 (emit-byte segment (if (eq size :byte) #b10101010 #b10101011)))))
1777 (define-instruction xlat (segment)
1778 (:printer byte ((op #b11010111)))
1780 (emit-byte segment #b11010111)))
1783 ;;;; bit manipulation
1785 (define-instruction bsf (segment dst src)
1786 (:printer ext-reg-reg/mem ((op #b1011110) (width 0)))
1788 (let ((size (matching-operand-size dst src)))
1789 (when (eq size :byte)
1790 (error "can't scan bytes: ~S" src))
1791 (maybe-emit-operand-size-prefix segment size)
1792 (emit-byte segment #b00001111)
1793 (emit-byte segment #b10111100)
1794 (emit-ea segment src (reg-tn-encoding dst)))))
1796 (define-instruction bsr (segment dst src)
1797 (:printer ext-reg-reg/mem ((op #b1011110) (width 1)))
1799 (let ((size (matching-operand-size dst src)))
1800 (when (eq size :byte)
1801 (error "can't scan bytes: ~S" src))
1802 (maybe-emit-operand-size-prefix segment size)
1803 (emit-byte segment #b00001111)
1804 (emit-byte segment #b10111101)
1805 (emit-ea segment src (reg-tn-encoding dst)))))
1807 (defun emit-bit-test-and-mumble (segment src index opcode)
1808 (let ((size (operand-size src)))
1809 (when (eq size :byte)
1810 (error "can't scan bytes: ~S" src))
1811 (maybe-emit-operand-size-prefix segment size)
1812 (emit-byte segment #b00001111)
1813 (cond ((integerp index)
1814 (emit-byte segment #b10111010)
1815 (emit-ea segment src opcode)
1816 (emit-byte segment index))
1818 (emit-byte segment (dpb opcode (byte 3 3) #b10000011))
1819 (emit-ea segment src (reg-tn-encoding index))))))
1821 (eval-when (:compile-toplevel :execute)
1822 (defun bit-test-inst-printer-list (subop)
1823 `((ext-reg/mem-imm ((op (#b1011101 ,subop))
1824 (reg/mem nil :type word-reg/mem)
1825 (imm nil :type imm-data)
1827 (ext-reg-reg/mem ((op ,(dpb subop (byte 3 2) #b1000001))
1829 (:name :tab reg/mem ", " reg)))))
1831 (define-instruction bt (segment src index)
1832 (:printer-list (bit-test-inst-printer-list #b100))
1834 (emit-bit-test-and-mumble segment src index #b100)))
1836 (define-instruction btc (segment src index)
1837 (:printer-list (bit-test-inst-printer-list #b111))
1839 (emit-bit-test-and-mumble segment src index #b111)))
1841 (define-instruction btr (segment src index)
1842 (:printer-list (bit-test-inst-printer-list #b110))
1844 (emit-bit-test-and-mumble segment src index #b110)))
1846 (define-instruction bts (segment src index)
1847 (:printer-list (bit-test-inst-printer-list #b101))
1849 (emit-bit-test-and-mumble segment src index #b101)))
1852 ;;;; control transfer
1854 (define-instruction call (segment where)
1855 (:printer near-jump ((op #b11101000)))
1856 (:printer reg/mem ((op '(#b1111111 #b010)) (width 1)))
1860 (emit-byte segment #b11101000)
1861 (emit-back-patch segment
1863 (lambda (segment posn)
1865 (- (label-position where)
1868 (emit-byte segment #b11101000)
1869 (emit-relative-fixup segment where))
1871 (emit-byte segment #b11111111)
1872 (emit-ea segment where #b010)))))
1874 (defun emit-byte-displacement-backpatch (segment target)
1875 (emit-back-patch segment
1877 (lambda (segment posn)
1878 (let ((disp (- (label-position target) (1+ posn))))
1879 (aver (<= -128 disp 127))
1880 (emit-byte segment disp)))))
1882 (define-instruction jmp (segment cond &optional where)
1883 ;; conditional jumps
1884 (:printer short-cond-jump ((op #b0111)) '('j cc :tab label))
1885 (:printer near-cond-jump () '('j cc :tab label))
1886 ;; unconditional jumps
1887 (:printer short-jump ((op #b1011)))
1888 (:printer near-jump ((op #b11101001)) )
1889 (:printer reg/mem ((op '(#b1111111 #b100)) (width 1)))
1894 (lambda (segment posn delta-if-after)
1895 (let ((disp (- (label-position where posn delta-if-after)
1897 (when (<= -128 disp 127)
1899 (dpb (conditional-opcode cond)
1902 (emit-byte-displacement-backpatch segment where)
1904 (lambda (segment posn)
1905 (let ((disp (- (label-position where) (+ posn 6))))
1906 (emit-byte segment #b00001111)
1908 (dpb (conditional-opcode cond)
1911 (emit-dword segment disp)))))
1912 ((label-p (setq where cond))
1915 (lambda (segment posn delta-if-after)
1916 (let ((disp (- (label-position where posn delta-if-after)
1918 (when (<= -128 disp 127)
1919 (emit-byte segment #b11101011)
1920 (emit-byte-displacement-backpatch segment where)
1922 (lambda (segment posn)
1923 (let ((disp (- (label-position where) (+ posn 5))))
1924 (emit-byte segment #b11101001)
1925 (emit-dword segment disp)))))
1927 (emit-byte segment #b11101001)
1928 (emit-relative-fixup segment where))
1930 (unless (or (ea-p where) (tn-p where))
1931 (error "don't know what to do with ~A" where))
1932 (emit-byte segment #b11111111)
1933 (emit-ea segment where #b100)))))
1935 (define-instruction jmp-short (segment label)
1937 (emit-byte segment #b11101011)
1938 (emit-byte-displacement-backpatch segment label)))
1940 (define-instruction ret (segment &optional stack-delta)
1941 (:printer byte ((op #b11000011)))
1942 (:printer byte ((op #b11000010) (imm nil :type 'imm-word-16))
1945 (cond ((and stack-delta (not (zerop stack-delta)))
1946 (emit-byte segment #b11000010)
1947 (emit-word segment stack-delta))
1949 (emit-byte segment #b11000011)))))
1951 (define-instruction jecxz (segment target)
1952 (:printer short-jump ((op #b0011)))
1954 (emit-byte segment #b11100011)
1955 (emit-byte-displacement-backpatch segment target)))
1957 (define-instruction loop (segment target)
1958 (:printer short-jump ((op #b0010)))
1960 (emit-byte segment #b11100010) ; pfw this was 11100011, or jecxz!!!!
1961 (emit-byte-displacement-backpatch segment target)))
1963 (define-instruction loopz (segment target)
1964 (:printer short-jump ((op #b0001)))
1966 (emit-byte segment #b11100001)
1967 (emit-byte-displacement-backpatch segment target)))
1969 (define-instruction loopnz (segment target)
1970 (:printer short-jump ((op #b0000)))
1972 (emit-byte segment #b11100000)
1973 (emit-byte-displacement-backpatch segment target)))
1975 ;;;; conditional move
1976 (define-instruction cmov (segment cond dst src)
1977 (:printer cond-move ())
1979 (aver (register-p dst))
1980 (let ((size (matching-operand-size dst src)))
1981 (aver (or (eq size :word) (eq size :dword)))
1982 (maybe-emit-operand-size-prefix segment size))
1983 (emit-byte segment #b00001111)
1984 (emit-byte segment (dpb (conditional-opcode cond) (byte 4 0) #b01000000))
1985 (emit-ea segment src (reg-tn-encoding dst))))
1987 ;;;; conditional byte set
1989 (define-instruction set (segment dst cond)
1990 (:printer cond-set ())
1992 (emit-byte segment #b00001111)
1993 (emit-byte segment (dpb (conditional-opcode cond) (byte 4 0) #b10010000))
1994 (emit-ea segment dst #b000)))
1998 (define-instruction enter (segment disp &optional (level 0))
1999 (:declare (type (unsigned-byte 16) disp)
2000 (type (unsigned-byte 8) level))
2001 (:printer enter-format ((op #b11001000)))
2003 (emit-byte segment #b11001000)
2004 (emit-word segment disp)
2005 (emit-byte segment level)))
2007 (define-instruction leave (segment)
2008 (:printer byte ((op #b11001001)))
2010 (emit-byte segment #b11001001)))
2013 (define-instruction prefetchnta (segment ea)
2014 (:printer prefetch ((op #b00011000) (reg #b000)))
2016 (aver (typep ea 'ea))
2017 (aver (eq :byte (ea-size ea)))
2018 (emit-byte segment #b00001111)
2019 (emit-byte segment #b00011000)
2020 (emit-ea segment ea #b000)))
2022 (define-instruction prefetcht0 (segment ea)
2023 (:printer prefetch ((op #b00011000) (reg #b001)))
2025 (aver (typep ea 'ea))
2026 (aver (eq :byte (ea-size ea)))
2027 (emit-byte segment #b00001111)
2028 (emit-byte segment #b00011000)
2029 (emit-ea segment ea #b001)))
2031 (define-instruction prefetcht1 (segment ea)
2032 (:printer prefetch ((op #b00011000) (reg #b010)))
2034 (aver (typep ea 'ea))
2035 (aver (eq :byte (ea-size ea)))
2036 (emit-byte segment #b00001111)
2037 (emit-byte segment #b00011000)
2038 (emit-ea segment ea #b010)))
2040 (define-instruction prefetcht2 (segment ea)
2041 (:printer prefetch ((op #b00011000) (reg #b011)))
2043 (aver (typep ea 'ea))
2044 (aver (eq :byte (ea-size ea)))
2045 (emit-byte segment #b00001111)
2046 (emit-byte segment #b00011000)
2047 (emit-ea segment ea #b011)))
2049 ;;;; interrupt instructions
2051 (defun snarf-error-junk (sap offset &optional length-only)
2052 (let* ((length (sb!sys:sap-ref-8 sap offset))
2053 (vector (make-array length :element-type '(unsigned-byte 8))))
2054 (declare (type sb!sys:system-area-pointer sap)
2055 (type (unsigned-byte 8) length)
2056 (type (simple-array (unsigned-byte 8) (*)) vector))
2058 (values 0 (1+ length) nil nil))
2060 (sb!kernel:copy-ub8-from-system-area sap (1+ offset)
2062 (collect ((sc-offsets)
2064 (lengths 1) ; the length byte
2066 (error-number (sb!c:read-var-integer vector index)))
2069 (when (>= index length)
2071 (let ((old-index index))
2072 (sc-offsets (sb!c:read-var-integer vector index))
2073 (lengths (- index old-index))))
2074 (values error-number
2080 (defmacro break-cases (breaknum &body cases)
2081 (let ((bn-temp (gensym)))
2082 (collect ((clauses))
2083 (dolist (case cases)
2084 (clauses `((= ,bn-temp ,(car case)) ,@(cdr case))))
2085 `(let ((,bn-temp ,breaknum))
2086 (cond ,@(clauses))))))
2089 (defun break-control (chunk inst stream dstate)
2090 (declare (ignore inst))
2091 (flet ((nt (x) (if stream (sb!disassem:note x dstate))))
2092 ;; FIXME: Make sure that BYTE-IMM-CODE is defined. The genesis
2093 ;; map has it undefined; and it should be easier to look in the target
2094 ;; Lisp (with (DESCRIBE 'BYTE-IMM-CODE)) than to definitively deduce
2095 ;; from first principles whether it's defined in some way that genesis
2097 (case #!-ud2-breakpoints (byte-imm-code chunk dstate)
2098 #!+ud2-breakpoints (word-imm-code chunk dstate)
2101 (sb!disassem:handle-break-args #'snarf-error-junk stream dstate))
2104 (sb!disassem:handle-break-args #'snarf-error-junk stream dstate))
2106 (nt "breakpoint trap"))
2107 (#.pending-interrupt-trap
2108 (nt "pending interrupt trap"))
2111 (#.fun-end-breakpoint-trap
2112 (nt "function end breakpoint trap")))))
2114 (define-instruction break (segment code)
2115 (:declare (type (unsigned-byte 8) code))
2116 #!-ud2-breakpoints (:printer byte-imm ((op #b11001100)) '(:name :tab code)
2117 :control #'break-control)
2118 #!+ud2-breakpoints (:printer word-imm ((op #b0000101100001111)) '(:name :tab code)
2119 :control #'break-control)
2121 #!-ud2-breakpoints (emit-byte segment #b11001100)
2122 ;; On darwin, trap handling via SIGTRAP is unreliable, therefore we
2123 ;; throw a sigill with 0x0b0f instead and check for this in the
2124 ;; SIGILL handler and pass it on to the sigtrap handler if
2126 #!+ud2-breakpoints (emit-word segment #b0000101100001111)
2127 (emit-byte segment code)))
2129 (define-instruction int (segment number)
2130 (:declare (type (unsigned-byte 8) number))
2131 (:printer byte-imm ((op #b11001101)))
2135 (emit-byte segment #b11001100))
2137 (emit-byte segment #b11001101)
2138 (emit-byte segment number)))))
2140 (define-instruction into (segment)
2141 (:printer byte ((op #b11001110)))
2143 (emit-byte segment #b11001110)))
2145 (define-instruction bound (segment reg bounds)
2147 (let ((size (matching-operand-size reg bounds)))
2148 (when (eq size :byte)
2149 (error "can't bounds-test bytes: ~S" reg))
2150 (maybe-emit-operand-size-prefix segment size)
2151 (emit-byte segment #b01100010)
2152 (emit-ea segment bounds (reg-tn-encoding reg)))))
2154 (define-instruction iret (segment)
2155 (:printer byte ((op #b11001111)))
2157 (emit-byte segment #b11001111)))
2159 ;;;; processor control
2161 (define-instruction hlt (segment)
2162 (:printer byte ((op #b11110100)))
2164 (emit-byte segment #b11110100)))
2166 (define-instruction nop (segment)
2167 (:printer byte ((op #b10010000)))
2169 (emit-byte segment #b10010000)))
2171 (define-instruction wait (segment)
2172 (:printer byte ((op #b10011011)))
2174 (emit-byte segment #b10011011)))
2176 ;;;; miscellaneous hackery
2178 (define-instruction byte (segment byte)
2180 (emit-byte segment byte)))
2182 (define-instruction word (segment word)
2184 (emit-word segment word)))
2186 (define-instruction dword (segment dword)
2188 (emit-dword segment dword)))
2190 (defun emit-header-data (segment type)
2191 (emit-back-patch segment
2193 (lambda (segment posn)
2197 (component-header-length))
2201 (define-instruction simple-fun-header-word (segment)
2203 (emit-header-data segment simple-fun-header-widetag)))
2205 (define-instruction lra-header-word (segment)
2207 (emit-header-data segment return-pc-header-widetag)))
2209 ;;;; fp instructions
2211 ;;;; FIXME: This section said "added by jrd", which should end up in CREDITS.
2213 ;;;; Note: We treat the single-precision and double-precision variants
2214 ;;;; as separate instructions.
2216 ;;; Load single to st(0).
2217 (define-instruction fld (segment source)
2218 (:printer floating-point ((op '(#b001 #b000))))
2220 (emit-byte segment #b11011001)
2221 (emit-fp-op segment source #b000)))
2223 ;;; Load double to st(0).
2224 (define-instruction fldd (segment source)
2225 (:printer floating-point ((op '(#b101 #b000))))
2226 (:printer floating-point-fp ((op '(#b001 #b000))))
2228 (if (fp-reg-tn-p source)
2229 (emit-byte segment #b11011001)
2230 (emit-byte segment #b11011101))
2231 (emit-fp-op segment source #b000)))
2233 ;;; Load long to st(0).
2234 (define-instruction fldl (segment source)
2235 (:printer floating-point ((op '(#b011 #b101))))
2237 (emit-byte segment #b11011011)
2238 (emit-fp-op segment source #b101)))
2240 ;;; Store single from st(0).
2241 (define-instruction fst (segment dest)
2242 (:printer floating-point ((op '(#b001 #b010))))
2244 (cond ((fp-reg-tn-p dest)
2245 (emit-byte segment #b11011101)
2246 (emit-fp-op segment dest #b010))
2248 (emit-byte segment #b11011001)
2249 (emit-fp-op segment dest #b010)))))
2251 ;;; Store double from st(0).
2252 (define-instruction fstd (segment dest)
2253 (:printer floating-point ((op '(#b101 #b010))))
2254 (:printer floating-point-fp ((op '(#b101 #b010))))
2256 (cond ((fp-reg-tn-p dest)
2257 (emit-byte segment #b11011101)
2258 (emit-fp-op segment dest #b010))
2260 (emit-byte segment #b11011101)
2261 (emit-fp-op segment dest #b010)))))
2263 ;;; Arithmetic ops are all done with at least one operand at top of
2264 ;;; stack. The other operand is is another register or a 32/64 bit
2267 ;;; dtc: I've tried to follow the Intel ASM386 conventions, but note
2268 ;;; that these conflict with the Gdb conventions for binops. To reduce
2269 ;;; the confusion I've added comments showing the mathamatical
2270 ;;; operation and the two syntaxes. By the ASM386 convention the
2271 ;;; instruction syntax is:
2274 ;;; or Fop Destination, Source
2276 ;;; If only one operand is given then it is the source and the
2277 ;;; destination is ST(0). There are reversed forms of the fsub and
2278 ;;; fdiv instructions inducated by an 'R' suffix.
2280 ;;; The mathematical operation for the non-reverse form is always:
2281 ;;; destination = destination op source
2283 ;;; For the reversed form it is:
2284 ;;; destination = source op destination
2286 ;;; The instructions below only accept one operand at present which is
2287 ;;; usually the source. I've hack in extra instructions to implement
2288 ;;; the fops with a ST(i) destination, these have a -sti suffix and
2289 ;;; the operand is the destination with the source being ST(0).
2292 ;;; st(0) = st(0) + memory or st(i).
2293 (define-instruction fadd (segment source)
2294 (:printer floating-point ((op '(#b000 #b000))))
2296 (emit-byte segment #b11011000)
2297 (emit-fp-op segment source #b000)))
2300 ;;; st(0) = st(0) + memory or st(i).
2301 (define-instruction faddd (segment source)
2302 (:printer floating-point ((op '(#b100 #b000))))
2303 (:printer floating-point-fp ((op '(#b000 #b000))))
2305 (if (fp-reg-tn-p source)
2306 (emit-byte segment #b11011000)
2307 (emit-byte segment #b11011100))
2308 (emit-fp-op segment source #b000)))
2310 ;;; Add double destination st(i):
2311 ;;; st(i) = st(0) + st(i).
2312 (define-instruction fadd-sti (segment destination)
2313 (:printer floating-point-fp ((op '(#b100 #b000))))
2315 (aver (fp-reg-tn-p destination))
2316 (emit-byte segment #b11011100)
2317 (emit-fp-op segment destination #b000)))
2319 (define-instruction faddp-sti (segment destination)
2320 (:printer floating-point-fp ((op '(#b110 #b000))))
2322 (aver (fp-reg-tn-p destination))
2323 (emit-byte segment #b11011110)
2324 (emit-fp-op segment destination #b000)))
2326 ;;; Subtract single:
2327 ;;; st(0) = st(0) - memory or st(i).
2328 (define-instruction fsub (segment source)
2329 (:printer floating-point ((op '(#b000 #b100))))
2331 (emit-byte segment #b11011000)
2332 (emit-fp-op segment source #b100)))
2334 ;;; Subtract single, reverse:
2335 ;;; st(0) = memory or st(i) - st(0).
2336 (define-instruction fsubr (segment source)
2337 (:printer floating-point ((op '(#b000 #b101))))
2339 (emit-byte segment #b11011000)
2340 (emit-fp-op segment source #b101)))
2342 ;;; Subtract double:
2343 ;;; st(0) = st(0) - memory or st(i).
2344 (define-instruction fsubd (segment source)
2345 (:printer floating-point ((op '(#b100 #b100))))
2346 (:printer floating-point-fp ((op '(#b000 #b100))))
2348 (if (fp-reg-tn-p source)
2349 (emit-byte segment #b11011000)
2350 (emit-byte segment #b11011100))
2351 (emit-fp-op segment source #b100)))
2353 ;;; Subtract double, reverse:
2354 ;;; st(0) = memory or st(i) - st(0).
2355 (define-instruction fsubrd (segment source)
2356 (:printer floating-point ((op '(#b100 #b101))))
2357 (:printer floating-point-fp ((op '(#b000 #b101))))
2359 (if (fp-reg-tn-p source)
2360 (emit-byte segment #b11011000)
2361 (emit-byte segment #b11011100))
2362 (emit-fp-op segment source #b101)))
2364 ;;; Subtract double, destination st(i):
2365 ;;; st(i) = st(i) - st(0).
2367 ;;; ASM386 syntax: FSUB ST(i), ST
2368 ;;; Gdb syntax: fsubr %st,%st(i)
2369 (define-instruction fsub-sti (segment destination)
2370 (:printer floating-point-fp ((op '(#b100 #b101))))
2372 (aver (fp-reg-tn-p destination))
2373 (emit-byte segment #b11011100)
2374 (emit-fp-op segment destination #b101)))
2376 (define-instruction fsubp-sti (segment destination)
2377 (:printer floating-point-fp ((op '(#b110 #b101))))
2379 (aver (fp-reg-tn-p destination))
2380 (emit-byte segment #b11011110)
2381 (emit-fp-op segment destination #b101)))
2383 ;;; Subtract double, reverse, destination st(i):
2384 ;;; st(i) = st(0) - st(i).
2386 ;;; ASM386 syntax: FSUBR ST(i), ST
2387 ;;; Gdb syntax: fsub %st,%st(i)
2388 (define-instruction fsubr-sti (segment destination)
2389 (:printer floating-point-fp ((op '(#b100 #b100))))
2391 (aver (fp-reg-tn-p destination))
2392 (emit-byte segment #b11011100)
2393 (emit-fp-op segment destination #b100)))
2395 (define-instruction fsubrp-sti (segment destination)
2396 (:printer floating-point-fp ((op '(#b110 #b100))))
2398 (aver (fp-reg-tn-p destination))
2399 (emit-byte segment #b11011110)
2400 (emit-fp-op segment destination #b100)))
2402 ;;; Multiply single:
2403 ;;; st(0) = st(0) * memory or st(i).
2404 (define-instruction fmul (segment source)
2405 (:printer floating-point ((op '(#b000 #b001))))
2407 (emit-byte segment #b11011000)
2408 (emit-fp-op segment source #b001)))
2410 ;;; Multiply double:
2411 ;;; st(0) = st(0) * memory or st(i).
2412 (define-instruction fmuld (segment source)
2413 (:printer floating-point ((op '(#b100 #b001))))
2414 (:printer floating-point-fp ((op '(#b000 #b001))))
2416 (if (fp-reg-tn-p source)
2417 (emit-byte segment #b11011000)
2418 (emit-byte segment #b11011100))
2419 (emit-fp-op segment source #b001)))
2421 ;;; Multiply double, destination st(i):
2422 ;;; st(i) = st(i) * st(0).
2423 (define-instruction fmul-sti (segment destination)
2424 (:printer floating-point-fp ((op '(#b100 #b001))))
2426 (aver (fp-reg-tn-p destination))
2427 (emit-byte segment #b11011100)
2428 (emit-fp-op segment destination #b001)))
2431 ;;; st(0) = st(0) / memory or st(i).
2432 (define-instruction fdiv (segment source)
2433 (:printer floating-point ((op '(#b000 #b110))))
2435 (emit-byte segment #b11011000)
2436 (emit-fp-op segment source #b110)))
2438 ;;; Divide single, reverse:
2439 ;;; st(0) = memory or st(i) / st(0).
2440 (define-instruction fdivr (segment source)
2441 (:printer floating-point ((op '(#b000 #b111))))
2443 (emit-byte segment #b11011000)
2444 (emit-fp-op segment source #b111)))
2447 ;;; st(0) = st(0) / memory or st(i).
2448 (define-instruction fdivd (segment source)
2449 (:printer floating-point ((op '(#b100 #b110))))
2450 (:printer floating-point-fp ((op '(#b000 #b110))))
2452 (if (fp-reg-tn-p source)
2453 (emit-byte segment #b11011000)
2454 (emit-byte segment #b11011100))
2455 (emit-fp-op segment source #b110)))
2457 ;;; Divide double, reverse:
2458 ;;; st(0) = memory or st(i) / st(0).
2459 (define-instruction fdivrd (segment source)
2460 (:printer floating-point ((op '(#b100 #b111))))
2461 (:printer floating-point-fp ((op '(#b000 #b111))))
2463 (if (fp-reg-tn-p source)
2464 (emit-byte segment #b11011000)
2465 (emit-byte segment #b11011100))
2466 (emit-fp-op segment source #b111)))
2468 ;;; Divide double, destination st(i):
2469 ;;; st(i) = st(i) / st(0).
2471 ;;; ASM386 syntax: FDIV ST(i), ST
2472 ;;; Gdb syntax: fdivr %st,%st(i)
2473 (define-instruction fdiv-sti (segment destination)
2474 (:printer floating-point-fp ((op '(#b100 #b111))))
2476 (aver (fp-reg-tn-p destination))
2477 (emit-byte segment #b11011100)
2478 (emit-fp-op segment destination #b111)))
2480 ;;; Divide double, reverse, destination st(i):
2481 ;;; st(i) = st(0) / st(i).
2483 ;;; ASM386 syntax: FDIVR ST(i), ST
2484 ;;; Gdb syntax: fdiv %st,%st(i)
2485 (define-instruction fdivr-sti (segment destination)
2486 (:printer floating-point-fp ((op '(#b100 #b110))))
2488 (aver (fp-reg-tn-p destination))
2489 (emit-byte segment #b11011100)
2490 (emit-fp-op segment destination #b110)))
2492 ;;; Exchange fr0 with fr(n). (There is no double precision variant.)
2493 (define-instruction fxch (segment source)
2494 (:printer floating-point-fp ((op '(#b001 #b001))))
2496 (aver (and (tn-p source)
2497 (eq (sb-name (sc-sb (tn-sc source))) 'float-registers)))
2498 (emit-byte segment #b11011001)
2499 (emit-fp-op segment source #b001)))
2501 ;;; Push 32-bit integer to st0.
2502 (define-instruction fild (segment source)
2503 (:printer floating-point ((op '(#b011 #b000))))
2505 (emit-byte segment #b11011011)
2506 (emit-fp-op segment source #b000)))
2508 ;;; Push 64-bit integer to st0.
2509 (define-instruction fildl (segment source)
2510 (:printer floating-point ((op '(#b111 #b101))))
2512 (emit-byte segment #b11011111)
2513 (emit-fp-op segment source #b101)))
2515 ;;; Store 32-bit integer.
2516 (define-instruction fist (segment dest)
2517 (:printer floating-point ((op '(#b011 #b010))))
2519 (emit-byte segment #b11011011)
2520 (emit-fp-op segment dest #b010)))
2522 ;;; Store and pop 32-bit integer.
2523 (define-instruction fistp (segment dest)
2524 (:printer floating-point ((op '(#b011 #b011))))
2526 (emit-byte segment #b11011011)
2527 (emit-fp-op segment dest #b011)))
2529 ;;; Store and pop 64-bit integer.
2530 (define-instruction fistpl (segment dest)
2531 (:printer floating-point ((op '(#b111 #b111))))
2533 (emit-byte segment #b11011111)
2534 (emit-fp-op segment dest #b111)))
2536 ;;; Store single from st(0) and pop.
2537 (define-instruction fstp (segment dest)
2538 (:printer floating-point ((op '(#b001 #b011))))
2540 (cond ((fp-reg-tn-p dest)
2541 (emit-byte segment #b11011101)
2542 (emit-fp-op segment dest #b011))
2544 (emit-byte segment #b11011001)
2545 (emit-fp-op segment dest #b011)))))
2547 ;;; Store double from st(0) and pop.
2548 (define-instruction fstpd (segment dest)
2549 (:printer floating-point ((op '(#b101 #b011))))
2550 (:printer floating-point-fp ((op '(#b101 #b011))))
2552 (cond ((fp-reg-tn-p dest)
2553 (emit-byte segment #b11011101)
2554 (emit-fp-op segment dest #b011))
2556 (emit-byte segment #b11011101)
2557 (emit-fp-op segment dest #b011)))))
2559 ;;; Store long from st(0) and pop.
2560 (define-instruction fstpl (segment dest)
2561 (:printer floating-point ((op '(#b011 #b111))))
2563 (emit-byte segment #b11011011)
2564 (emit-fp-op segment dest #b111)))
2566 ;;; Decrement stack-top pointer.
2567 (define-instruction fdecstp (segment)
2568 (:printer floating-point-no ((op #b10110)))
2570 (emit-byte segment #b11011001)
2571 (emit-byte segment #b11110110)))
2573 ;;; Increment stack-top pointer.
2574 (define-instruction fincstp (segment)
2575 (:printer floating-point-no ((op #b10111)))
2577 (emit-byte segment #b11011001)
2578 (emit-byte segment #b11110111)))
2580 ;;; Free fp register.
2581 (define-instruction ffree (segment dest)
2582 (:printer floating-point-fp ((op '(#b101 #b000))))
2584 (emit-byte segment #b11011101)
2585 (emit-fp-op segment dest #b000)))
2587 (define-instruction fabs (segment)
2588 (:printer floating-point-no ((op #b00001)))
2590 (emit-byte segment #b11011001)
2591 (emit-byte segment #b11100001)))
2593 (define-instruction fchs (segment)
2594 (:printer floating-point-no ((op #b00000)))
2596 (emit-byte segment #b11011001)
2597 (emit-byte segment #b11100000)))
2599 (define-instruction frndint(segment)
2600 (:printer floating-point-no ((op #b11100)))
2602 (emit-byte segment #b11011001)
2603 (emit-byte segment #b11111100)))
2606 (define-instruction fninit(segment)
2607 (:printer floating-point-5 ((op #b00011)))
2609 (emit-byte segment #b11011011)
2610 (emit-byte segment #b11100011)))
2612 ;;; Store Status Word to AX.
2613 (define-instruction fnstsw(segment)
2614 (:printer floating-point-st ((op #b00000)))
2616 (emit-byte segment #b11011111)
2617 (emit-byte segment #b11100000)))
2619 ;;; Load Control Word.
2621 ;;; src must be a memory location
2622 (define-instruction fldcw(segment src)
2623 (:printer floating-point ((op '(#b001 #b101))))
2625 (emit-byte segment #b11011001)
2626 (emit-fp-op segment src #b101)))
2628 ;;; Store Control Word.
2629 (define-instruction fnstcw(segment dst)
2630 (:printer floating-point ((op '(#b001 #b111))))
2632 (emit-byte segment #b11011001)
2633 (emit-fp-op segment dst #b111)))
2635 ;;; Store FP Environment.
2636 (define-instruction fstenv(segment dst)
2637 (:printer floating-point ((op '(#b001 #b110))))
2639 (emit-byte segment #b11011001)
2640 (emit-fp-op segment dst #b110)))
2642 ;;; Restore FP Environment.
2643 (define-instruction fldenv(segment src)
2644 (:printer floating-point ((op '(#b001 #b100))))
2646 (emit-byte segment #b11011001)
2647 (emit-fp-op segment src #b100)))
2650 (define-instruction fsave(segment dst)
2651 (:printer floating-point ((op '(#b101 #b110))))
2653 (emit-byte segment #b11011101)
2654 (emit-fp-op segment dst #b110)))
2656 ;;; Restore FP State.
2657 (define-instruction frstor(segment src)
2658 (:printer floating-point ((op '(#b101 #b100))))
2660 (emit-byte segment #b11011101)
2661 (emit-fp-op segment src #b100)))
2663 ;;; Clear exceptions.
2664 (define-instruction fnclex(segment)
2665 (:printer floating-point-5 ((op #b00010)))
2667 (emit-byte segment #b11011011)
2668 (emit-byte segment #b11100010)))
2671 (define-instruction fcom (segment src)
2672 (:printer floating-point ((op '(#b000 #b010))))
2674 (emit-byte segment #b11011000)
2675 (emit-fp-op segment src #b010)))
2677 (define-instruction fcomd (segment src)
2678 (:printer floating-point ((op '(#b100 #b010))))
2679 (:printer floating-point-fp ((op '(#b000 #b010))))
2681 (if (fp-reg-tn-p src)
2682 (emit-byte segment #b11011000)
2683 (emit-byte segment #b11011100))
2684 (emit-fp-op segment src #b010)))
2686 ;;; Compare ST1 to ST0, popping the stack twice.
2687 (define-instruction fcompp (segment)
2688 (:printer floating-point-3 ((op '(#b110 #b011001))))
2690 (emit-byte segment #b11011110)
2691 (emit-byte segment #b11011001)))
2693 ;;; unordered comparison
2694 (define-instruction fucom (segment src)
2695 (:printer floating-point-fp ((op '(#b101 #b100))))
2697 (aver (fp-reg-tn-p src))
2698 (emit-byte segment #b11011101)
2699 (emit-fp-op segment src #b100)))
2701 (define-instruction ftst (segment)
2702 (:printer floating-point-no ((op #b00100)))
2704 (emit-byte segment #b11011001)
2705 (emit-byte segment #b11100100)))
2709 (define-instruction fsqrt(segment)
2710 (:printer floating-point-no ((op #b11010)))
2712 (emit-byte segment #b11011001)
2713 (emit-byte segment #b11111010)))
2715 (define-instruction fscale(segment)
2716 (:printer floating-point-no ((op #b11101)))
2718 (emit-byte segment #b11011001)
2719 (emit-byte segment #b11111101)))
2721 (define-instruction fxtract(segment)
2722 (:printer floating-point-no ((op #b10100)))
2724 (emit-byte segment #b11011001)
2725 (emit-byte segment #b11110100)))
2727 (define-instruction fsin(segment)
2728 (:printer floating-point-no ((op #b11110)))
2730 (emit-byte segment #b11011001)
2731 (emit-byte segment #b11111110)))
2733 (define-instruction fcos(segment)
2734 (:printer floating-point-no ((op #b11111)))
2736 (emit-byte segment #b11011001)
2737 (emit-byte segment #b11111111)))
2739 (define-instruction fprem1(segment)
2740 (:printer floating-point-no ((op #b10101)))
2742 (emit-byte segment #b11011001)
2743 (emit-byte segment #b11110101)))
2745 (define-instruction fprem(segment)
2746 (:printer floating-point-no ((op #b11000)))
2748 (emit-byte segment #b11011001)
2749 (emit-byte segment #b11111000)))
2751 (define-instruction fxam (segment)
2752 (:printer floating-point-no ((op #b00101)))
2754 (emit-byte segment #b11011001)
2755 (emit-byte segment #b11100101)))
2757 ;;; These do push/pop to stack and need special handling
2758 ;;; in any VOPs that use them. See the book.
2760 ;;; st0 <- st1*log2(st0)
2761 (define-instruction fyl2x(segment) ; pops stack
2762 (:printer floating-point-no ((op #b10001)))
2764 (emit-byte segment #b11011001)
2765 (emit-byte segment #b11110001)))
2767 (define-instruction fyl2xp1(segment)
2768 (:printer floating-point-no ((op #b11001)))
2770 (emit-byte segment #b11011001)
2771 (emit-byte segment #b11111001)))
2773 (define-instruction f2xm1(segment)
2774 (:printer floating-point-no ((op #b10000)))
2776 (emit-byte segment #b11011001)
2777 (emit-byte segment #b11110000)))
2779 (define-instruction fptan(segment) ; st(0) <- 1; st(1) <- tan
2780 (:printer floating-point-no ((op #b10010)))
2782 (emit-byte segment #b11011001)
2783 (emit-byte segment #b11110010)))
2785 (define-instruction fpatan(segment) ; POPS STACK
2786 (:printer floating-point-no ((op #b10011)))
2788 (emit-byte segment #b11011001)
2789 (emit-byte segment #b11110011)))
2791 ;;;; loading constants
2793 (define-instruction fldz(segment)
2794 (:printer floating-point-no ((op #b01110)))
2796 (emit-byte segment #b11011001)
2797 (emit-byte segment #b11101110)))
2799 (define-instruction fld1(segment)
2800 (:printer floating-point-no ((op #b01000)))
2802 (emit-byte segment #b11011001)
2803 (emit-byte segment #b11101000)))
2805 (define-instruction fldpi(segment)
2806 (:printer floating-point-no ((op #b01011)))
2808 (emit-byte segment #b11011001)
2809 (emit-byte segment #b11101011)))
2811 (define-instruction fldl2t(segment)
2812 (:printer floating-point-no ((op #b01001)))
2814 (emit-byte segment #b11011001)
2815 (emit-byte segment #b11101001)))
2817 (define-instruction fldl2e(segment)
2818 (:printer floating-point-no ((op #b01010)))
2820 (emit-byte segment #b11011001)
2821 (emit-byte segment #b11101010)))
2823 (define-instruction fldlg2(segment)
2824 (:printer floating-point-no ((op #b01100)))
2826 (emit-byte segment #b11011001)
2827 (emit-byte segment #b11101100)))
2829 (define-instruction fldln2(segment)
2830 (:printer floating-point-no ((op #b01101)))
2832 (emit-byte segment #b11011001)
2833 (emit-byte segment #b11101101)))
2837 (define-instruction cpuid (segment)
2838 (:printer two-bytes ((op '(#b00001111 #b10100010))))
2840 (emit-byte segment #b00001111)
2841 (emit-byte segment #b10100010)))
2843 (define-instruction rdtsc (segment)
2844 (:printer two-bytes ((op '(#b00001111 #b00110001))))
2846 (emit-byte segment #b00001111)
2847 (emit-byte segment #b00110001)))
2849 ;;;; Late VM definitions
2850 (defun canonicalize-inline-constant (constant)
2851 (let ((first (car constant)))
2853 (single-float (setf constant (list :single-float first)))
2854 (double-float (setf constant (list :double-float first)))))
2855 (destructuring-bind (type value) constant
2857 ((:byte :word :dword)
2858 (aver (integerp value))
2861 (aver (base-char-p value))
2862 (cons :byte (char-code value)))
2864 (aver (characterp value))
2865 (cons :dword (char-code value)))
2867 (aver (typep value 'single-float))
2868 (cons :dword (ldb (byte 32 0) (single-float-bits value))))
2869 ((:double-float-bits)
2870 (aver (integerp value))
2871 (cons :double-float (ldb (byte 64 0) value)))
2873 (aver (typep value 'double-float))
2875 (ldb (byte 64 0) (logior (ash (double-float-high-bits value) 32)
2876 (double-float-low-bits value))))))))
2878 (defun inline-constant-value (constant)
2879 (let ((label (gen-label))
2880 (size (ecase (car constant)
2881 ((:byte :word :dword) (car constant))
2882 (:double-float :dword))))
2883 (values label (make-ea size
2884 :disp (make-fixup nil :code-object label)))))
2886 (defun emit-constant-segment-header (segment constants optimize)
2887 (declare (ignore segment constants))
2888 (loop repeat (if optimize 64 16) do (inst byte #x90)))
2890 (defun size-nbyte (size)
2897 (defun sort-inline-constants (constants)
2898 (stable-sort constants #'> :key (lambda (constant)
2899 (size-nbyte (caar constant)))))
2901 (defun emit-inline-constant (constant label)
2902 (let ((size (size-nbyte (car constant))))
2903 (emit-alignment (integer-length (1- size)))
2905 (let ((val (cdr constant)))
2907 do (inst byte (ldb (byte 8 0) val))
2908 (setf val (ash val -8))))))