asm-x86.lisp 68 KB
Newer Older
1 2 3 4
;;;;------------------------------------------------------------------
;;;; 
;;;;    Copyright (C) 2007 Frode V. Fjeld
;;;; 
5
;;;; Description:   x86 assembler for 16, 32, and 64-bit modes.
6 7 8
;;;; Author:        Frode Vatvedt Fjeld <frodef@acm.org>
;;;; Distribution:  See the accompanying file COPYING.
;;;;                
9
;;;; $Id: asm-x86.lisp,v 1.38 2008/04/12 16:46:48 ffjeld Exp $
10 11 12 13
;;;;                
;;;;------------------------------------------------------------------

(defpackage asm-x86
14
  (:use :common-lisp :asm)
15 16
  (:export #:assemble-instruction
	   #:disassemble-instruction
17 18
	   #:*cpu-mode*
	   #:*position-independent-p*))
19 20 21 22 23 24 25 26

(in-package asm-x86)

(defvar *cpu-mode* :32-bit)

(defvar *instruction-encoders*
  (make-hash-table :test 'eq))

27 28 29
(defvar *use-jcc-16-bit-p* nil
  "Whether to use 16-bit JCC instructions in 32-bit mode.")

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
(defun prefix-lookup (prefix-name)
  (cdr (or (assoc prefix-name
		  '((:operand-size-override . #x66)
		    (:address-size-override . #x67)
		    (:16-bit-operand . #x66)
		    (:16-bit-address . #x67)
		    (:lock . #xf0)
		    (:repne . #xf2)
		    (:repz . #xf3)
		    (:cs-override . #x2e)
		    (:ss-override . #x36)
		    (:ds-override . #x3e)
		    (:es-override . #x26)
		    (:fs-override . #x64)
		    (:gs-override . #x65)))
	   (error "There is no instruction prefix named ~S." prefix-name))))

(defun rex-encode (rexes &key rm)
  (let ((rex (logior (if (null rexes)
			 0
			 (+ #x40 (loop for rex in rexes
				    sum (ecase rex
					  (:rex.w #b1000)
					  (:rex.r #b0100)
					  (:rex.x #b0010)
					  (:rex.b #b0001)))))
		     (if (not rm)
			 0
			 (ldb (byte 1 3) rm)))))
    (unless (zerop rex)
      (list rex))))



(deftype octet ()
  '(unsigned-byte 8))

(deftype xint (size)
  `(or (unsigned-byte ,size)
       (signed-byte ,size)))

(deftype uint (size)
  `(unsigned-byte ,size))

(deftype sint (size)
  `(signed-byte ,size))

(defun integer-to-octets (i n)
  "Return list of n octets, encoding i in signed little endian."
  (loop for b from 0 below (* 8 n) by 8
     collect (ldb (byte 8 b) i)))

82
(defun encode-values-fun (operator legacy-prefixes prefixes rexes opcode mod reg rm scale index base displacement immediate operand-size address-size)
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  (assert opcode)
  (when (or (and (eq address-size :32-bit)
		 (eq *cpu-mode* :64-bit))
	    (and (eq address-size :16-bit)
		 (eq *cpu-mode* :32-bit))
	    (and (eq address-size :64-bit)
		 (eq *cpu-mode* :32-bit))
	    (and (eq address-size :32-bit)
		 (eq *cpu-mode* :16-bit)))
    (pushnew :address-size-override
	     prefixes))
  (when (or (and (eq operand-size :16-bit)
		 (eq *cpu-mode* :64-bit))
	    (and (eq operand-size :16-bit)
		 (eq *cpu-mode* :32-bit))
	    (and (eq operand-size :32-bit)
		 (eq *cpu-mode* :16-bit)))
    (pushnew :operand-size-override
	     prefixes))
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
  (let ((code (append legacy-prefixes
		      (mapcar #'prefix-lookup (reverse prefixes))
		      (rex-encode rexes :rm rm)
		      (when (< 16 (integer-length opcode))
			(list (ldb (byte 8 16) opcode)))
		      (when (< 8(integer-length opcode))
			(list (ldb (byte 8 8) opcode)))
		      (list (ldb (byte 8 0) opcode))
		      (when (or mod reg rm)
			(assert (and mod reg rm) (mod reg rm)
				"Either all or none of mod, reg, and rm must be defined. mod=~S, reg=~S, rm=~S." mod reg rm)
			(check-type mod (unsigned-byte 2))
			(list (logior (ash (ldb (byte 2 0) mod)
					   6)
				      (ash (ldb (byte 3 0) reg)
					   3)
				      (ash (ldb (byte 3 0) rm)
					   0))))
		      (when (or scale index base)
			(assert (and scale index base) (scale index base)
				"Either all or none of scale, index, and base must be defined. scale=~S, index=~S, base=~S." scale index base)
			(check-type scale (unsigned-byte 2))
			(check-type index (unsigned-byte 4))
			(check-type base (unsigned-byte 4))
			(list (logior (ash (ldb (byte 2 0) scale)
					   6)
				      (ash (ldb (byte 3 0) index)
					   3)
				      (ash (ldb (byte 3 0) base)
					   0))))
		      displacement
		      immediate)))
    (append (compute-extra-prefixes operator *pc* (length code))
	    code)))
136 137

(defmacro encode (values-form)
138
  `(multiple-value-call #'encode-values-fun operator legacy-prefixes ,values-form))
139

140
(defmacro merge-encodings (form1 form2)
141
  `(multiple-value-bind (prefixes1 rexes1 opcode1 mod1 reg1 rm1 scale1 index1 base1 displacement1 immediate1 operand-size1 address-size1)
142
       ,form1
143
     (multiple-value-bind (prefixes2 rexes2 opcode2 mod2 reg2 rm2 scale2 index2 base2 displacement2 immediate2 operand-size2 address-size2)
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
	 ,form2
       (macrolet ((getone (a b name)
		    `(cond
		       ((and ,a ,b)
			(error "~A doubly defined: ~S and ~S." ',name ,a ,b))
		       (,a)
		       (,b))))
	 (encoded-values :prefixes (append prefixes1 prefixes2)
			 :rex (append (if (listp rexes1)
					  rexes1
					  (list rexes1))
				      (if (listp rexes2)
					  rexes2
					  (list rexes2)))
			 :opcode (getone opcode1 opcode2 opcode)
			 :mod (getone mod1 mod2 mod)
			 :reg (getone reg1 reg2 reg)
			 :rm (getone rm1 rm2 rm)
			 :scale (getone scale1 scale2 scale)
			 :index (getone index1 index2 index)
			 :base (getone base1 base2 base)
			 :displacement (getone displacement1 displacement2 displacement)
			 :immediate (getone immediate1 immediate2 immediate)
			 :operand-size (getone operand-size1 operand-size2 operand-size)
			 :address-size (getone address-size1 address-size2 address-size))))))


171
(defun encoded-values (&key prefixes prefix rex opcode mod reg rm scale index base displacement immediate operand-size address-size)
172 173 174 175 176 177 178 179 180 181 182 183 184 185
  (values (append (when prefix
		    (list prefix))
		  prefixes)
	  (if (keywordp rex)
	      (list rex)
	      rex)
	  opcode
	  mod reg rm
	  scale index base
	  displacement
	  immediate
	  operand-size
	  address-size))
 
186 187
(defun assemble-instruction (instruction)
  "Assemble a single instruction to a list of octets of x86 machine code, according to *cpu-mode* etc."
188
  (multiple-value-bind (instruction legacy-prefixes options)
189
      (if (listp (car instruction))
190 191 192 193 194 195
	  (values (cdr instruction)
		  (remove-if #'listp (car instruction))
		  (remove-if #'keywordp (car instruction)))
	  (values instruction
		  nil
		  nil))
196 197
    (destructuring-bind (operator &rest operands)
	instruction
198 199 200 201 202 203
      (multiple-value-bind (code failp)
	  (apply (or (gethash operator *instruction-encoders*)
		     (error "Unknown instruction operator ~S in ~S." operator instruction))
		 operator
		 (mapcar #'prefix-lookup legacy-prefixes)
		 operands)
204
	(cond
205 206
	  (failp
	   (error "Unable to encode ~S." instruction))
207 208 209 210 211 212
	  ((null options)
	   code)
	  ((assoc :size options)
	   (assert (= (second (assoc :size options))
		      (length code)))
	   code))))))
213

214
(defmacro define-operator (operator operator-mode lambda-list &body body)
215
  (check-type operator keyword)
216 217 218 219
  (labels ((find-forms (body)
	     (cond
	       ((atom body)
		nil)
220
	       ((member (car body) '(reg-modrm modrm opcode imm-modrm imm opcode-reg
221 222
				     opcode-reg-imm pc-rel moffset sreg-modrm reg-cr
				     far-pointer))
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
		(list body))
	       (t (mapcan #'find-forms body)))))
    (let ((defun-name (intern (format nil "~A-~A" 'instruction-encoder operator))))
      `(progn
	 (defun ,defun-name (operator legacy-prefixes ,@lambda-list)
	   (declare (ignorable operator legacy-prefixes))
	   (let ((operator-mode ',operator-mode)
		 (default-rex nil))
	     (declare (ignorable operator-mode default-rex))
	     (macrolet ((disassembler (&body body)
			  (declare (ignore body)))
			(assembler (&body body)
			  `(progn ,@body)))
	       (block operator
		 ,@body
		 (values nil 'fail)))))
	 (setf (gethash ',operator *instruction-encoders*)
	       ',defun-name)
	 (macrolet ((disassembler (&body body)
		      `(progn ,@body))
		    (assembler (&body body)
		      (declare (ignore body))))
	   (let ((operator ',operator)
246 247
		 (operator-mode ',operator-mode)
		 (operand-formals ',lambda-list))
248
	     (declare (ignorable operator operand-formals operator-mode))
249 250 251 252 253 254 255
	     ,@(find-forms body)))
	 ',operator))))

(defmacro define-operator/none (name lambda-list &body body)
  `(define-operator ,name nil ,lambda-list ,@body))

(deftype list-of (&rest elements)
256
  "A list with elements of specified type(s)."
257 258 259 260 261 262 263
  (labels ((make-list-of (elements)
	     (if (null elements)
		 'null
		 `(cons ,(car elements)
			,(make-list-of (cdr elements))))))
    (make-list-of elements)))

264 265 266 267 268 269 270 271 272
(deftype list-of* (&rest elements)
  "A list starting with elements of specified type(s)."
  (labels ((make-list-of (elements)
	     (if (null elements)
		 'list
		 `(cons ,(car elements)
			,(make-list-of (cdr elements))))))
    (make-list-of elements)))

273 274 275 276 277 278 279 280 281 282
(defparameter *opcode-disassemblers-16*
  (make-array 256 :initial-element nil))

(defparameter *opcode-disassemblers-32*
  (make-array 256 :initial-element nil))

(defparameter *opcode-disassemblers-64*
  (make-array 256 :initial-element nil))

(deftype disassembly-decoder ()
283
  '(list-of* boolean keyword (or keyword null) symbol))
284 285 286 287 288 289 290 291 292

(defun (setf opcode-disassembler) (decoder opcode operator-mode)
  (check-type decoder disassembly-decoder)
  (labels ((set-it (table pos)
	     (check-type pos (integer 0 *))
	     (check-type table (simple-vector 256))
	     (let ((bit-pos (* 8 (1- (ceiling (integer-length pos) 8)))))
	       (if (not (plusp bit-pos))
		   (progn
Frode Vatvedt Fjeld's avatar
Frode Vatvedt Fjeld committed
293 294 295 296 297
		     #+(or) (unless (or (eq nil decoder)
					(eq nil (svref table pos))
					(equal decoder (svref table pos)))
			      (warn "Redefining disassembler for ~@[~(~A~) ~]opcode #x~X from ~{~S ~}to ~{~S~^ ~}."
				    operator-mode opcode (svref table pos) decoder))
298 299 300 301 302 303 304 305 306 307 308 309
		     (setf (svref table pos) decoder))
		   (set-it (or (svref table (ldb (byte 8 bit-pos) pos))
			       (setf (svref table (ldb (byte 8 bit-pos) pos))
				     (make-array 256 :initial-element nil)))
			   (ldb (byte bit-pos 0) pos))))))
    (ecase operator-mode
      (:16-bit
       (set-it *opcode-disassemblers-16* opcode))
      (:32-bit
       (set-it *opcode-disassemblers-32* opcode))
      (:64-bit
       (set-it *opcode-disassemblers-64* opcode))
310
      ((:8-bit nil)
311 312 313 314
       (set-it *opcode-disassemblers-16* opcode)
       (set-it *opcode-disassemblers-32* opcode)
       (set-it *opcode-disassemblers-64* opcode)))))

315

316 317 318 319 320 321 322 323 324 325 326 327 328
(defmacro pop-code (code-place &optional context)
  `(progn
     (unless ,code-place
       (error "End of byte-stream in the middle of an instruction."))
     (let ((x (pop ,code-place)))
     (check-type x (unsigned-byte 8) ,(format nil "an octet (context: ~A)" context))
     x)))

(defmacro code-call (form &optional (code-place (case (car form) ((funcall apply) (third form)) (t (second form)))))
  "Execute form, then 'magically' update the code binding with the secondary return value from form."
  `(let (tmp)
     (declare (ignorable tmp))
     (setf (values tmp ,code-place) ,form)))
329

330
(defmacro define-disassembler ((operator opcode &optional cpu-mode digit backup-p operand-size) lambda-list &body body)
331
  (cond
332
    (digit
333 334 335 336 337 338
     `(loop for mod from #b00 to #b11
	 do (loop for r/m from #b000 to #b111
	       as ext-opcode = (logior (ash ,opcode 8)
				       (ash ,digit 3)
				       (ash mod 6)
				       r/m)
339
	       do (define-disassembler (,operator ext-opcode ,cpu-mode nil t ,operand-size) ,lambda-list ,@body))))
340
    ((symbolp lambda-list)
341
     `(setf (opcode-disassembler ,opcode ,cpu-mode) (list ,backup-p ,operator ,(or operand-size cpu-mode) ',lambda-list ,@body)))
342 343 344
    (t (let ((defun-name (intern (format nil "~A-~A-~X~@[-~A~]" 'disassembler operator opcode cpu-mode))))
	 `(progn
	    (defun ,defun-name ,lambda-list ,@body)
345
	    (setf (opcode-disassembler ,opcode ',cpu-mode) (list ,backup-p ,operator ',(or operand-size cpu-mode) ',defun-name))
346
	    ',defun-name)))))
347 348 349

(defun disassemble-simple-prefix (code operator opcode operand-size address-size rex)
  (declare (ignore opcode rex))
350
  (let ((instruction (code-call (disassemble-instruction code operand-size address-size nil))))
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
    (values (if (consp (car instruction))
		(list* (list* operator (car instruction))
		       (cdr instruction))
		(list* (list operator)
		       instruction))
	    code)))

(define-disassembler (:lock #xf0) disassemble-simple-prefix)
(define-disassembler (:repne #xf2) disassemble-simple-prefix)
(define-disassembler (:repz #xf3) disassemble-simple-prefix)
(define-disassembler (:cs-override #x2e) disassemble-simple-prefix)
(define-disassembler (:ss-override #x36) disassemble-simple-prefix)
(define-disassembler (:ds-override #x3e) disassemble-simple-prefix)
(define-disassembler (:es-override #x26) disassemble-simple-prefix)
(define-disassembler (:fs-override #x64) disassemble-simple-prefix)
(define-disassembler (:gs-override #x65) disassemble-simple-prefix)

(define-disassembler (:operand-size-override #x66 :32-bit) (code operator opcode operand-size address-size rex)
  (declare (ignore operator opcode operand-size rex))
370
  (disassemble-instruction code :16-bit address-size nil))
371 372

(define-disassembler (:address-size-override #x67 :32-bit) (code operator opcode operand-size address-size rex)
373
  (declare (ignore operator opcode address-size rex))
374
  (disassemble-instruction code operand-size :16-bit nil))
375

376 377
(define-disassembler (:operand-size-override #x66 :16-bit) (code operator opcode operand-size address-size rex)
  (declare (ignore operator opcode operand-size rex))
378
  (disassemble-instruction code :32-bit address-size nil))
379 380

(define-disassembler (:address-size-override #x67 :16-bit) (code operator opcode operand-size address-size rex)
381
  (declare (ignore operator opcode address-size rex))
382
  (disassemble-instruction code operand-size :32-bit nil))
383 384

(defmacro define-operator/8 (operator lambda-list &body body)
385 386 387
  `(define-operator ,operator :8-bit ,lambda-list
     (let ((default-rex nil))
       (declare (ignorable default-rex))
388
       ,@body)))
389 390

(defmacro define-operator/16 (operator lambda-list &body body)
391 392 393
  `(define-operator ,operator :16-bit ,lambda-list
     (let ((default-rex nil))
       (declare (ignorable default-rex))
394
       ,@body)))
395 396

(defmacro define-operator/32 (operator lambda-list &body body)
397 398 399
  `(define-operator ,operator :32-bit ,lambda-list
     (let ((default-rex nil))
       (declare (ignorable default-rex))
400
       ,@body)))
401 402

(defmacro define-operator/64 (operator lambda-list &body body)
403 404 405
  `(define-operator ,operator :64-bit ,lambda-list
     (let ((default-rex '(:rex.w)))
       (declare (ignorable default-rex))
406
       ,@body)))
407 408

(defmacro define-operator/64* (operator lambda-list &body body)
409 410
  `(define-operator ,operator :64-bit ,lambda-list
     (let ((default-rex (case *cpu-mode*
411 412
			  (:64-bit nil)
			  (t '(:rex.w)))))
413
       (declare (ignorable default-rex))
414 415
       ,@body)))

416
(defmacro define-operator* ((&key |16| |32| |64| dispatch) args &body body)
417
  (let ((body16 (subst '(xint 16) :int-16-32-64
418 419
                       (subst :dx :dx-edx-rdx
                              (subst :ax :ax-eax-rax body))))
420
        (body32 (subst '(xint 32) :int-16-32-64
421 422
                       (subst :edx :dx-edx-rdx
                              (subst :eax :ax-eax-rax body))))
423
        (body64 (subst '(sint 32) :int-16-32-64
424 425
                       (subst :rdx :dx-edx-rdx
                              (subst :rax :ax-eax-rax body)))))
426 427 428 429 430 431
    `(progn
       ,(when |16|
              `(define-operator/16 ,|16| ,args ,@body16))
       ,(when |32|
              `(define-operator/32 ,|32| ,args ,@body32))
       ,(when |64|
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
              `(define-operator/64 ,|64| ,args ,@body64))
       ,(when dispatch
	      (let ((dispatch-name (intern (format nil "~A-~A" 'instruction-dispatcher dispatch))))
		`(progn
		   (defun ,dispatch-name (&rest args)
		     (declare (dynamic-extent args))
		     (loop for encoder in (ecase *cpu-mode*
					    (:32-bit ',(remove nil (list |32| |16| |64|)))
					    (:64-bit ',(remove nil (list |64| |32| |16|)))
					    (:16-bit ',(remove nil (list |16| |32| |64|))))
			thereis (apply (gethash encoder *instruction-encoders*) args)
			finally (return (values nil 'fail))))
		   (setf (gethash ',dispatch *instruction-encoders*)
			 ',dispatch-name))))
       nil)))
447 448 449 450 451 452 453 454 455 456 457 458 459 460

(defun resolve-and-encode (x type &key size)
  (encode-integer (cond
		    ((typep x type)
		     x)
		    ((integerp x)
		     (error "Immediate value #x~X out of range for ~S." x type))
		    ((assoc x *symtab*)
		     (let ((value (cdr (assoc x *symtab*))))
		       (assert (typep value type))
		       value))
		    (t (error "Unresolved symbol ~S (size ~S)." x size)))
		  type))

461
(defun resolve-pc-relative (operand)
462
  (etypecase operand
463
    (pc-relative-operand
464
     (reduce #'+ (cdr operand)
465
	     :key #'resolve-operand))
466
    (symbol-reference
467
     (assert *pc* (*pc*) "Cannot encode a pc-relative operand without a value for ~S." '*pc*)
468
     (- (resolve-operand operand)
469
	*pc*))))
470

471 472 473 474 475 476
(defun encode-integer (i type)
  (assert (typep i type))
  (let ((bit-size (cadr type)))
    (loop for b upfrom 0 below bit-size by 8
       collect (ldb (byte 8 b) i))))	 

477 478 479 480 481 482 483 484 485 486 487
(defun type-octet-size (type)
  (assert (member (car type)
		  '(sint uint xint))
	  (type))
  (values (ceiling (cadr type) 8)))

(defun opcode-octet-size (opcode)
  (loop do (setf opcode (ash opcode -8))
     count t
     while (plusp opcode)))

488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
(defun parse-indirect-operand (operand)
  (assert (indirect-operand-p operand))
  (let (reg offsets reg2 reg-scale)
    (dolist (expr operand)
      (etypecase expr
	(register-operand
	 (if reg
	     (setf reg2 expr)
	     (setf reg expr)))
	((cons register-operand
	       (cons (member 1 2 4 8) null))
	 (when reg
	   (assert (not reg-scale))
	   (setf reg2 reg))
	 (setf reg (first expr)
	       reg-scale (second expr)))
	(immediate-operand
	 (push expr offsets))
	((cons (eql :+))
	 (dolist (term (cdr expr))
	   (push term offsets)))))
509 510 511 512 513
    (when (and (eq reg2 :esp)
	       (or (not reg-scale)
		   (eql 1 reg-scale)))
      (psetf reg reg2
	     reg2 reg))
514 515 516 517
    (values reg offsets reg2 (if (not reg)
				 nil
				 (or reg-scale 1)))))

518 519 520 521 522 523 524
(defun register-set-by-mode (mode)
  (ecase mode
    (:8-bit  '(:al :cl :dl :bl :ah :ch :dh :bh))
    (:16-bit '(:ax :cx :dx :bx :sp :bp :si :di))
    (:32-bit '(:eax :ecx :edx :ebx :esp :ebp :esi :edi))
    (:64-bit '(:rax :rcx :rdx :rbx :rsp :rbp :rsi :rdi :r8 :r9 :r10 :r11 :r12 :13 :r14 :r15))
    (:mm '(:mm0 :mm1 :mm2 :mm3 :mm4 :mm5 :mm6 :mm7))
525 526
    (:xmm '(:xmm0 :xmm1 :xmm2 :xmm3 :xmm4 :xmm5 :xmm6 :xmm7))
    (:segment '(:es :cs :ss :ds :fs :gs))))
527 528

(defun encode-reg/mem (operand mode)
529 530
  (check-type mode (member nil :8-bit :16-bit :32-bit :64-bit :mm :xmm))
  (if (and mode (keywordp operand))
531
      (encoded-values :mod #b11
532
		      :rm (or (position operand (register-set-by-mode mode))
533
			      (error "Unknown ~(~D~) register ~S." mode operand)))
534 535 536 537 538 539 540 541
      (multiple-value-bind (reg offsets reg2 reg-scale)
	  (parse-indirect-operand operand)
	(check-type reg-scale (member nil 1 2 4 8))
	(assert (or (not reg2)
		    (and reg reg2)))
	(assert (or (not reg-scale)
		    (and reg reg-scale)))
	(let ((offset (reduce #'+ offsets
542
			      :key #'resolve-operand)))
543
	  (cond
544 545 546 547 548 549 550
            ((and (not reg)
                  (eq mode :16-bit)
                  (typep offset '(xint 16)))
             (encoded-values :mod #b00
                             :rm #b110
                             :address-size :16-bit
                             :displacement (encode-integer offset '(xint 16))))
551 552 553 554
	    ((and (not reg)
		  (typep offset '(xint 32)))
	     (encoded-values :mod #b00
			     :rm #b101
555
                             :address-size :32-bit
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
			     :displacement (encode-integer offset '(xint 32))))
	    ((and (eq reg :sp)
		  (not reg2)
		  (= 1 reg-scale))
	     (etypecase offset
	       ((eql 0)
		(encoded-values :mod #b00
				:rm #b100
				:scale 0
				:index #b100
				:base #b100
				:address-size :16-bit))
	       ((sint 8)
		(encoded-values :mod #b01
				:rm #b100
				:displacement (encode-integer offset '(sint 8))
				:scale 0
				:index #b100
				:base #b100
				:address-size :16-bit))
	       ((xint 32)
		(encoded-values :mod #b10
				:rm #b100
				:displacement (encode-integer offset '(xint 32))
				:scale 0
				:index #b100
				:base #b100
				:address-size :16-bit))))
	    ((and (eq reg :esp)
		  (= 1 reg-scale))
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
	     (let ((reg2-index (or (position reg2 '(:eax :ecx :edx :ebx nil :ebp :esi :edi))
				   (error "Unknown reg2 [F] ~S." reg2))))
	       (etypecase offset
		 ((eql 0)
		  (encoded-values :mod #b00
				  :rm #b100
				  :scale 0
				  :index reg2-index
				  :base #b100
				  :address-size :32-bit))
		 ((sint 8)
		  (encoded-values :mod #b01
				  :rm #b100
				  :displacement (encode-integer offset '(sint 8))
				  :scale 0
				  :index reg2-index
				  :base #b100
				  :address-size :32-bit))
		 ((xint 32)
		  (encoded-values :mod #b10
				  :rm #b100
				  :displacement (encode-integer offset '(xint 32))
				  :scale 0
				  :index reg2-index
				  :base #b100
				  :address-size :32-bit)))))
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
	    ((and (eq reg :rsp)
		  (not reg2)
		  (= 1 reg-scale))
	     (etypecase offset
	       ((eql 0)
		(encoded-values :mod #b00
				:rm #b100
				:scale 0
				:index #b100
				:base #b100
				:address-size :64-bit))
	       ((sint 8)
		(encoded-values :mod #b01
				:rm #b100
				:displacement (encode-integer offset '(sint 8))
				:scale 0
				:index #b100
				:base #b100
				:address-size :64-bit))
	       ((sint 32)
		(encoded-values :mod #b10
				:rm #b100
				:displacement (encode-integer offset '(sint 32))
				:scale 0
				:index #b100
				:base #b100
				:address-size :64-bit))))
	    (t (multiple-value-bind (register-index map address-size)
640
		   (let* ((map32 '(:eax :ecx :edx :ebx :esp :ebp :esi :edi))
641
			  (index32 (position reg map32))
642
			  (map64 '(:rax :rcx :rdx :rbx :rsp :rbp :rsi :rdi :r8 :r9 :r10 :r11 :r12 :r13 :r14 :r15))
643 644 645 646 647 648 649 650 651
			  (index64 (unless index32
				     (position reg map64))))
		     (if index32
			 (values index32 map32 :32-bit)
			 (values index64 map64 :64-bit)))
		 (cond
		   ((and (not reg2)
			 register-index
			 (= 1 reg-scale)
Frode Vatvedt Fjeld's avatar
Frode Vatvedt Fjeld committed
652 653
			 (and (zerop offset)
			      (not (= register-index #b101))))
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
		    (encoded-values :mod #b00
				    :rm register-index
				    :address-size address-size))
		   ((and (not reg2)
			 register-index
			 (= 1 reg-scale)
			 (typep offset '(sint 8)))
		    (encoded-values :mod #b01
				    :rm register-index
				    :displacement (encode-integer offset '(sint 8))
				    :address-size address-size))
		   ((and (not reg2)
			 register-index
			 (= 1 reg-scale)
			 (or (typep offset '(sint 32))
			     (and (eq :32-bit address-size)
				  (typep offset '(xint 32)))))
		    (encoded-values :mod #b10
				    :rm register-index
				    :displacement (encode-integer offset '(sint 32))
				    :address-size address-size))
675 676 677 678 679 680 681 682 683 684 685 686 687
                   ((and (not reg2)
                         register-index
                         (if (eq :64-bit *cpu-mode*)
                             (typep offset '(sint 32))
                             (typep offset '(xint 32)))
                         (not (= #b100 register-index)))
                    (encoded-values :rm #b100
                                    :mod #b00
                                    :index register-index
                                    :base #b101
                                    :scale (or (position reg-scale '(1 2 4 8))
                                               (error "Unknown register scale ~S." reg-scale))
                                    :displacement (encode-integer offset '(xint 32))))
688 689 690 691 692 693
		   ((and reg2
			 register-index
			 (zerop offset)
			 (not (= register-index #b100)))
		    (encoded-values :mod #b00
				    :rm #b100
694 695
				    :scale (or (position reg-scale '(1 2 4 8))
                                               (error "Unknown register scale ~S." reg-scale))
696 697
				    :index register-index
				    :base (or (position reg2 map)
698
					      (error "unknown reg2 [A] ~S" reg2))
699 700 701 702 703 704 705 706 707 708
				    :address-size address-size))
		   ((and reg2
			 register-index
			 (typep offset '(sint 8))
			 (not (= register-index #b100)))
		    (encoded-values :mod #b01
				    :rm #b100
				    :scale (position reg-scale '(1 2 4 8))
				    :index register-index
				    :base (or (position reg2 map)
709
					      (error "unknown reg2 [B] ~S" reg2))
710 711 712 713 714
				    :address-size address-size
				    :displacement (encode-integer offset '(sint 8))))
		   ((and reg2
			 register-index
			 (eq :32-bit address-size)
715
			 (typep offset '(sint 8))
716 717 718 719 720
			 (not (= register-index #b100)))
		    (encoded-values :mod #b01
				    :rm #b100
				    :scale (position reg-scale '(1 2 4 8))
				    :index register-index
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
				    :base (or (position reg2 map)
					      (error "unknown reg2 [C] ~S." reg2))
				    :address-size address-size
				    :displacement (encode-integer offset '(sint 8))))
		   ((and reg2
			 register-index
			 (eq :32-bit address-size)
			 (typep offset '(xint 32))
			 (not (= register-index #b100)))
		    (encoded-values :mod #b10
				    :rm #b100
				    :scale (position reg-scale '(1 2 4 8))
				    :index register-index
				    :base (or (position reg2 map)
					      (error "unknown reg2 [D] ~S." reg2))
				    :address-size address-size
				    :displacement (encode-integer offset '(xint 32))))
738 739 740 741 742 743 744 745 746 747
		   ((and reg2
			 register-index
			 (eq :64-bit address-size)
			 (typep offset '(sint 32))
			 (not (= register-index #b100)))
		    (encoded-values :mod #b01
				    :rm #b100
				    :scale (position reg-scale '(1 2 4 8))
				    :index register-index
				    :base (or (position reg2 map)
748
					      (error "unknown reg2 [E] ~S" reg2))
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
				    :address-size address-size
				    :displacement (encode-integer offset '(sint 32))))
		   (t (let ((rm16 (position-if (lambda (x)
						 (or (and (eq (car x) reg)
							  (eq (cdr x) reg2))
						     (and (eq (car x) reg2)
							  (eq (cdr x) reg))))
					       '((:bx . :si) (:bx . :di) (:bp . :si) (:bp . :di)
						 (:si) (:di) (:bp) (:bx)))))
			(cond
			  ((and rm16
				(zerop offset)
				(not (= #b110 rm16)))
			   (encoded-values :mod #b00
					   :rm rm16
					   :address-size :16-bit))
			  ((and rm16
				(typep offset '(sint 8)))
			   (encoded-values :mod #b01
					   :rm rm16
					   :address-size :16-bit
					   :displacement (encode-integer offset '(sint 8))))
			  ((and rm16
				(typep offset '(xint 16)))
			   (encoded-values :mod #b10
					   :rm rm16
					   :address-size :16-bit
					   :displacement (encode-integer offset '(xint 16))))
777 778
			  (t (error "Huh? reg: ~S, reg2: ~S, scale: ~S, offset: ~S" reg reg2 reg-scale offset)))))))))))))

779 780 781 782 783 784 785 786 787 788 789
(defun operand-ordering (formals &rest arrangement)
  (loop with rarrangement = (reverse arrangement)
     for formal in formals
     when (getf rarrangement formal)
     collect it))

(defun order-operands (ordering &rest operands)
  (loop for key in ordering
     collect (or (getf operands key)
		 (error "No operand ~S in ~S." key operands))))

790 791 792 793 794 795 796 797 798 799 800 801
(defun decode-integer (code type)
  "Decode an integer of specified type."
  (let* ((bit-size (cadr type))
	 (unsigned-integer (loop for b from 0 below bit-size by 8
			      sum (ash (pop-code code integer) b))))
    (values (if (or (not (member (car type) '(sint signed-byte)))
		    (not (logbitp (1- bit-size) unsigned-integer)))
		unsigned-integer
		(- (ldb (byte bit-size 0)
			(1+ (lognot unsigned-integer)))))
	    code)))

802 803
(defun disassemble-instruction (code &optional override-operand-size override-address-size rex)
  (labels ((lookup-decoder (table opcode)
804 805
	     (let* ((backup-code code)
		    (datum (pop-code code))
806 807 808 809
		    (opcode (logior (ash opcode 8)
				    datum))
		    (decoder (svref table datum)))
	       (typecase decoder
810
		 (vector
811 812
		  (lookup-decoder decoder opcode))
		 (disassembly-decoder
813 814 815
		  (when (car decoder)
		    (setf code backup-code))
		  (values (cdr decoder)
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
			  opcode))
		 (t (error "No disassembler registered for opcode #x~X." opcode))))))
    (multiple-value-bind (decoder opcode)
	(lookup-decoder (ecase (or override-operand-size *cpu-mode*)
			  (:16-bit *opcode-disassemblers-16*)
			  (:32-bit *opcode-disassemblers-32*)
			  (:64-bit *opcode-disassemblers-64*))
			0)
      (destructuring-bind (operator operand-size decoder-function &rest extra-args)
	  decoder
	(values (code-call (apply decoder-function
				  code
				  operator
				  opcode
				  (or operand-size override-operand-size)
				  (or override-address-size *cpu-mode*)
				  rex
				  extra-args))
		code)))))

836
(defun decode-no-operands (code operator opcode operand-size address-size rex &rest fixed-operands)
837
  (declare (ignore opcode operand-size address-size rex))
838 839
  (values (list* operator
		 (remove nil fixed-operands))
840
	  code))
841

842 843 844 845 846 847 848 849 850 851 852
(defun decode-reg-cr (code operator opcode operand-size address-size rex operand-ordering)
  (declare (ignore opcode operand-size address-size))
  (let ((modrm (pop-code code)))
    (values (list* operator
		   (order-operands operand-ordering
				   :reg (nth (ldb (byte 3 0) modrm)
					     (register-set-by-mode (if rex :64-bit :32-bit)))
				   :cr (nth (ldb (byte 3 3) modrm)
					    '(:cr0 :cr1 :cr2 :cr3 :cr4 :cr5 :cr6 :cr7))))
	    code)))

853
(defun decode-reg-modrm (code operator opcode operand-size address-size rex operand-ordering &optional (reg-mode operand-size))
854 855 856 857
  (declare (ignore opcode rex))
  (values (list* operator
		 (order-operands operand-ordering
				 :reg (nth (ldb (byte 3 3) (car code))
858
					   (register-set-by-mode reg-mode))
859 860 861 862 863 864 865 866 867
				 :modrm (ecase address-size
					  (:32-bit
					   (code-call (decode-reg-modrm-32 code operand-size)))
					  (:16-bit
					   (code-call (decode-reg-modrm-16 code operand-size))))))
	  code))
	      

(defun decode-modrm (code operator opcode operand-size address-size rex)
868
  (declare (ignore opcode rex))
869 870 871 872 873 874 875 876 877
  (values (list operator
		(ecase address-size
		  (:32-bit
		   (code-call (decode-reg-modrm-32 code operand-size)))
		  (:16-bit
		   (code-call (decode-reg-modrm-16 code operand-size)))))
	  code))

(defun decode-imm-modrm (code operator opcode operand-size address-size rex imm-type operand-ordering &key fixed-modrm)
878
  (declare (ignore opcode rex))
879 880 881 882 883 884 885 886 887 888 889 890
  (values (list* operator
		 (order-operands operand-ordering
				 :modrm (or fixed-modrm
					    (when (member :modrm operand-ordering)
					      (ecase address-size
						(:32-bit
						 (code-call (decode-reg-modrm-32 code operand-size)))
						(:16-bit
						 (code-call (decode-reg-modrm-16 code operand-size))))))
				 :imm (code-call (decode-integer code imm-type))))
	  code))

891 892 893 894 895 896
(defun decode-far-pointer (code operator opcode operand-size address-size rex type)
  (declare (ignore opcode operand-size address-size rex))
  (let ((offset (code-call (decode-integer code type)))
	(segment (code-call (decode-integer code '(uint 16)))))
    (values (list operator
		  segment
897
		  (list offset))
898 899
	    code)))

900
(defun decode-pc-rel (code operator opcode operand-size address-size rex type)
901
  (declare (ignore opcode operand-size address-size rex))
902 903 904 905
  (values (list operator
		`(:pc+ ,(code-call (decode-integer code type))))
	  code))

906 907 908 909 910 911 912 913
(defun decode-moffset (code operator opcode operand-size address-size rex type operand-ordering fixed-operand)
  (declare (ignore opcode operand-size address-size rex))
  (values (list* operator
		 (order-operands operand-ordering
				 :moffset (list (code-call (decode-integer code type)))
				 :fixed fixed-operand))
	  code))

914
(defun decode-opcode-reg (code operator opcode operand-size address-size rex operand-ordering extra-operand)
915
  (declare (ignore address-size rex))
916 917 918 919 920 921 922
  (values (list* operator
		 (order-operands operand-ordering
				 :reg (nth (ldb (byte 3 0) opcode)
					   (register-set-by-mode operand-size))
				 :extra extra-operand))
	  code))

923 924 925 926 927 928 929 930 931
(defun decode-opcode-reg-imm (code operator opcode operand-size address-size rex operand-ordering imm-type)
  (declare (ignore address-size rex))
  (values (list* operator
		 (order-operands operand-ordering
				 :reg (nth (ldb (byte 3 0) opcode)
					   (register-set-by-mode operand-size))
				 :imm (code-call (decode-integer code imm-type))))
	  code))

932
(defun decode-reg-modrm-16 (code operand-size)
933 934 935 936
  (let* ((modrm (pop-code code mod/rm))
	 (mod (ldb (byte 2 6) modrm))
	 (reg (ldb (byte 3 3) modrm))
	 (r/m (ldb (byte 3 0) modrm)))
937 938 939 940 941 942 943
    (values (if (= mod #b11)
		(nth reg (register-set-by-mode operand-size))
		(flet ((operands (i)
			 (nth i '((:bx :si) (:bx :di) (:bp :si) (:bp :di) (:si) (:di) (:bp) (:bx)))))
		  (ecase mod
		    (#b00
		     (case r/m
944
		       (#b110 (list (code-call (decode-integer code '(uint 16)))))
945 946 947
		       (t (operands r/m))))
		    (#b01
		     (append (operands r/m)
948
			     (list (code-call (decode-integer code '(sint 8))))))
949 950
		    (#b10
		     (append (operands r/m)
951
			     (list (code-call (decode-integer code '(uint 16)))))))))
952
	    code)))
953

954
(defun decode-reg-modrm-32 (code operand-size)
955 956 957 958
  "Return a list of the REG, and the MOD/RM operands."
  (let* ((modrm (pop-code code mod/rm))
	 (mod (ldb (byte 2 6) modrm))
	 (r/m (ldb (byte 3 0) modrm)))
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
    (values (if (= mod #b11)
		(nth r/m (register-set-by-mode operand-size))
		(flet ((decode-sib ()
			 (let* ((sib (pop-code code sib))
				(ss (ldb (byte 2 6) sib))
				(index (ldb (byte 3 3) sib))
				(base (ldb (byte 3 0) sib)))
			   (nconc (unless (= index #b100)
				    (let ((index-reg (nth index (register-set-by-mode :32-bit))))
				      (if (= ss #b00)
					  (list index-reg)
					  (list (list index-reg (ash 2 ss))))))
				  (if (/= base #b101)
				      (list (nth base (register-set-by-mode :32-bit)))
				      (ecase mod
					(#b00 nil)
					((#b01 #b10) (list :ebp))))))))
		  (ecase mod
		    (#b00 (case r/m
			    (#b100 (decode-sib))
			    (#b101 (code-call (decode-integer code '(uint 32))))
			    (t (list (nth r/m (register-set-by-mode :32-bit))))))
		    (#b01 (case r/m
			    (#b100 (nconc(decode-sib)
					 (list (code-call (decode-integer code '(sint 8))))))
			    (t (list (nth r/m (register-set-by-mode :32-bit))
				     (code-call (decode-integer code '(sint 8)))))))
		    (#b10 (case r/m
			    (#b100 (nconc (decode-sib)
					  (list (code-call (decode-integer code '(uint 32))))))
			    (t (list (nth r/m (register-set-by-mode :32-bit))
				     (code-call (decode-integer code '(uint 32))))))))))
991 992
	    code)))

993

994 995 996 997 998 999 1000
(defmacro return-when (form)
  `(let ((x ,form))
     (when x (return-from operator x))))

(defmacro return-values-when (form)
  `(let ((x (encode ,form)))
     (when x (return-from operator x))))
1001

1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
(defmacro imm (imm-operand opcode imm-type &optional extra-operand &rest extras)
  `(progn
     (assembler
      (when (and ,@(when extra-operand
			 (list (list* 'eql extra-operand)))
		 (immediate-p ,imm-operand))
	(let ((immediate (resolve-operand ,imm-operand)))
	  (when (typep immediate ',imm-type)
	    (return-values-when
	     (encoded-values :opcode ,opcode
			     :immediate (encode-integer immediate ',imm-type)
			     :operand-size operator-mode
			     :rex default-rex
			     ,@extras))))))
     (disassembler
      ,(if extra-operand
	   `(define-disassembler (operator ,opcode operator-mode)
		decode-imm-modrm
	      ',imm-type
	      (operand-ordering operand-formals
				:imm ',imm-operand
				:modrm ',(first extra-operand))
	      :fixed-modrm ',(second extra-operand))
	   `(define-disassembler (operator ,opcode operator-mode)
		decode-imm-modrm
	      ',imm-type
	      '(:imm))))))
1029

1030
(defmacro imm-modrm (op-imm op-modrm opcode digit type)
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
  `(progn
     (assembler
      (when (immediate-p ,op-imm)
	(let ((immediate (resolve-operand ,op-imm)))
	  (when (typep immediate ',type)
	    (return-values-when
	     (merge-encodings (encoded-values :opcode ,opcode
					      :reg ,digit
					      :operand-size operator-mode
					      :rex default-rex
					      :immediate (encode-integer immediate ',type))
			      (encode-reg/mem ,op-modrm operator-mode)))))))
     (disassembler
      (define-disassembler (operator ,opcode operator-mode ,digit)
	  decode-imm-modrm
	',type
	(operand-ordering operand-formals
			  :imm ',op-imm
			  :modrm ',op-modrm)))))
1050

1051 1052

(defun compute-extra-prefixes (operator pc size)
1053 1054 1055 1056 1057
  (loop for (pattern . function) in *instruction-compute-extra-prefix-map*
     when (or (eq pattern t)
	      (eq pattern operator))
     return (funcall function pc size)))
	      
1058 1059

(defun encode-pc-rel (operator legacy-prefixes opcode operand type &rest extras)
1060
  (when (typep operand '(or pc-relative-operand symbol-reference))
1061 1062 1063 1064 1065 1066
    (let* ((estimated-code-size-no-extras (+ (length legacy-prefixes)
					     (type-octet-size type)
					     (opcode-octet-size opcode)))
	   (estimated-extra-prefixes (compute-extra-prefixes operator *pc* estimated-code-size-no-extras))
	   (estimated-code-size (+ estimated-code-size-no-extras
				   (length estimated-extra-prefixes)))
1067 1068
	   (offset (let ((*pc* (when *pc*
				 (+ *pc* estimated-code-size))))
1069 1070
		     (resolve-pc-relative operand))))
      (when (typep offset type)
1071 1072 1073 1074 1075
	(let ((code (let ((*instruction-compute-extra-prefix-map* nil))
		      (encode (apply #'encoded-values
				     :opcode opcode
				     :displacement (encode-integer offset type)
				     extras)))))
1076
	  (if (= (length code)
1077 1078
		 estimated-code-size-no-extras)
	      (append estimated-extra-prefixes code)
1079
	      (let* ((code-size (length code))
1080
		     (extra-prefixes (compute-extra-prefixes operator *pc* code-size))
1081 1082
		     (offset (let ((*pc* (when *pc*
					   (+ *pc* code-size (length extra-prefixes)))))
1083 1084
			       (resolve-pc-relative operand))))
		(when (typep offset type)
1085 1086 1087 1088 1089
		  (let ((code (let ((*instruction-compute-extra-prefix-map* nil))
				(encode (apply #'encoded-values
					       :opcode opcode
					       :displacement (encode-integer offset type)
					       extras)))))
1090
		    (assert (= code-size (length code)))
1091
		    (append extra-prefixes code))))))))))
1092

1093
(defmacro pc-rel (opcode operand type &optional (mode 'operator-mode) &rest extras)
1094 1095 1096 1097
  `(progn
     (assembler
      (return-when (encode-pc-rel operator legacy-prefixes ,opcode ,operand ',type ,@extras)))
     (disassembler
1098
      (define-disassembler (operator ,opcode ,mode)
1099 1100
	  decode-pc-rel
	',type))))
1101

1102
(defmacro modrm (operand opcode digit)
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
  `(progn
     (assembler
      (when (typep ,operand '(or register-operand indirect-operand))
	(return-values-when
	 (merge-encodings (encoded-values :opcode ,opcode
					  :reg ,digit
					  :operand-size operator-mode
					  :rex default-rex)
			  (encode-reg/mem ,operand operator-mode)))))
     (disassembler
      (define-disassembler (operator ,opcode operator-mode ,digit) decode-modrm))))
1114

1115
(defun encode-reg-modrm (operator legacy-prefixes op-reg op-modrm opcode operator-mode default-rex &optional reg/mem-mode &rest extras)
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
  (let* ((reg-map (ecase operator-mode
		    (:8-bit '(:al :cl :dl :bl :ah :ch :dh :bh))
		    (:16-bit '(:ax :cx :dx :bx :sp :bp :si :di))
		    (:32-bit '(:eax :ecx :edx :ebx :esp :ebp :esi :edi))
		    (:64-bit '(:rax :rcx :rdx :rbx :rsp :rbp :rsi :rdi :r8 :r9 :r10 :r11 :r12 :r13 :r14 :r15))
		    (:mm '(:mm0 :mm1 :mm2 :mm3 :mm4 :mm5 :mm6 :mm7 :mm8))
		    (:xmm '(:xmm0 :xmm1 :xmm2 :xmm3 :xmm4 :xmm5 :xmm6 :xmm7))))
	 (reg-index (position op-reg reg-map)))
    (when reg-index
      (encode (merge-encodings (apply #'encoded-values
				      :opcode opcode
				      :reg reg-index
				      :operand-size operator-mode
				      :rex default-rex
				      extras)
1131
			       (encode-reg/mem op-modrm (or reg/mem-mode operator-mode)))))))
1132

1133
(defmacro reg-modrm (op-reg op-modrm opcode &optional reg/mem-mode &rest extras)
1134 1135 1136 1137 1138
  `(progn
     (assembler
      (return-when (encode-reg-modrm operator legacy-prefixes ,op-reg ,op-modrm ,opcode
				     operator-mode default-rex ,reg/mem-mode ,@extras)))
     (disassembler
1139 1140 1141 1142 1143
      (define-disassembler (operator ,opcode operator-mode)
	  decode-reg-modrm
	(operand-ordering operand-formals
			  :reg ',op-reg
			  :modrm ',op-modrm)))))
1144

1145
(defun encode-reg-cr (operator legacy-prefixes op-reg op-cr opcode operator-mode default-rex &rest extras)
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
  (let* ((reg-map (ecase operator-mode
		    (:32-bit '(:eax :ecx :edx :ebx :esp :ebp :esi :edi))
		    (:64-bit '(:rax :rcx :rdx :rbx :rsp :rbp :rsi :rdi :r8 :r9 :r10 :r11 :r12 :r13 :r14 :r15))))
	 (reg-index (position op-reg reg-map))
	 (cr-index (position op-cr '(:cr0 :cr1 :cr2 :cr3 :cr4 :cr5 :cr6 :cr7))))
    (when (and reg-index
	       cr-index)
      (encode (apply #'encoded-values
		     :opcode opcode
		     :mod #b11
		     :rm reg-index
		     :reg cr-index
1158 1159 1160
		     :operand-size (if (not (eq *cpu-mode* :64-bit))
				       nil
				       operator-mode)
1161 1162
		     :rex default-rex
		     extras)))))
1163

1164
(defmacro reg-cr (op-reg op-cr opcode &rest extras)
1165 1166 1167 1168 1169 1170 1171 1172 1173
  `(progn
     (assembler
      (return-when (encode-reg-cr operator legacy-prefixes ,op-reg ,op-cr ,opcode operator-mode default-rex ,@extras)))
     (disassembler
      (define-disassembler (operator ,opcode nil nil nil :32-bit)
	  decode-reg-cr
	(operand-ordering operand-formals
			  :reg ',op-reg
			  :cr ',op-cr)))))
1174

1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
(defmacro sreg-modrm (op-sreg op-modrm opcode &rest extras)
  `(progn
     (assembler
      (let* ((reg-map '(:es :cs :ss :ds :fs :gs))
	     (reg-index (position ,op-sreg reg-map)))
	(when reg-index
	  (return-values-when
	   (merge-encodings (encoded-values :opcode ,opcode
					    :reg reg-index
					    :rex default-rex
					    ,@extras)
			    (encode-reg/mem ,op-modrm operator-mode))))))
     (disassembler
      (define-disassembler (operator ,opcode nil nil nil :16-bit)
	  decode-reg-modrm
	(operand-ordering operand-formals
			  :reg ',op-sreg
			  :modrm ',op-modrm)
	:segment))))
1194

1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
(defmacro moffset (opcode op-offset type fixed-operand)
  `(progn
     (assembler
      (when (and ,@(when fixed-operand
			 `((eql ,@fixed-operand)))
		 (indirect-operand-p ,op-offset))
	(multiple-value-bind (reg offsets reg2)
	    (parse-indirect-operand ,op-offset)
	  (when (and (not reg)
		     (not reg2))
	    (return-values-when
	     (encoded-values :opcode ,opcode
			     :displacement (encode-integer (reduce #'+ offsets
								   :key #'resolve-operand)
							   ',type)))))))
     (disassembler
      (define-disassembler (operator ,opcode operator-mode)
	  decode-moffset
	',type
	(operand-ordering operand-formals
			  :moffset ',op-offset
			  :fixed ',(first fixed-operand))
	',(second fixed-operand)))))


1220

1221
(defmacro opcode (opcode &optional fixed-operand fixed-operand2 &rest extras)
1222 1223
  `(progn
     (assembler
1224
      (when (and ,@(when fixed-operand
1225 1226 1227
			 `((eql ,@fixed-operand)))
		 ,@(when fixed-operand2
			 `((eql ,@fixed-operand2))))
1228 1229 1230 1231
	(return-values-when
	 (encoded-values :opcode ,opcode
			 ,@extras
			 :operand-size operator-mode))))
1232
     (disassembler
1233 1234
      (define-disassembler (operator ,opcode)
	  decode-no-operands
1235 1236
	,(second fixed-operand)
	,(second fixed-operand2)))))
1237 1238

(defmacro opcode* (opcode &rest extras)
1239 1240
  `(return-values-when
    (encoded-values :opcode ,opcode
1241 1242
                     ,@extras)))

1243
(defun encode-opcode-reg (operator legacy-prefixes opcode op-reg operator-mode default-rex)
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
  (let* ((reg-map (ecase operator-mode
		    (:8-bit '(:al :cl :dl :bl :ah :ch :dh :bh))
		    (:16-bit '(:ax :cx :dx :bx :sp :bp :si :di))
		    (:32-bit '(:eax :ecx :edx :ebx :esp :ebp :esi :edi))
		    (:64-bit '(:rax :rcx :rdx :rbx :rsp :rbp :rsi :rdi :r8 :r9 :r10 :r11 :r12 :r13 :r14 :r15))
		    (:mm '(:mm0 :mm1 :mm2 :mm3 :mm4 :mm5 :mm6 :mm7 :mm8))
		    (:xmm '(:xmm0 :xmm1 :xmm2 :xmm3 :xmm4 :xmm5 :xmm6 :xmm7))))
	 (reg-index (position op-reg reg-map)))
    (when reg-index
      (encode (encoded-values :opcode (+ opcode (ldb (byte 3 0) reg-index))
			      :operand-size operator-mode
			      :rex (cond
				     ((>= reg-index 8)
				      (assert (eq :64-bit operator-mode))
				      '(:rex.w :rex.r))
				     (t default-rex)))))))

1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
(defmacro opcode-reg (opcode op-reg &optional extra-operand)
  `(progn
     (assembler
      (when (and ,@(when extra-operand
			 `((eql ,@extra-operand))))
	(return-when
	 (encode-opcode-reg operator legacy-prefixes ,opcode ,op-reg operator-mode default-rex))))
     (disassembler
      (loop for reg from #b000 to #b111
	 do ,(if (not extra-operand)
		 `(define-disassembler (operator (logior ,opcode reg) operator-mode)
		      decode-opcode-reg
		    '(:reg)
		    nil)
		 `(define-disassembler (operator (logior ,opcode reg) operator-mode)
		      decode-opcode-reg
		    (operand-ordering operand-formals
				      :reg ',op-reg
				      :extra ',(first extra-operand))
		    ',(second extra-operand)))))))
1281

1282
(defun encode-opcode-reg-imm (operator legacy-prefixes opcode op-reg op-imm type operator-mode default-rex)
1283
  (when (immediate-p op-imm)
1284
    (let ((immediate (resolve-operand op-imm)))
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
      (when (typep immediate type)
	(let* ((reg-map (ecase operator-mode
			  (:8-bit '(:al :cl :dl :bl :ah :ch :dh :bh))
			  (:16-bit '(:ax :cx :dx :bx :sp :bp :si :di))
			  (:32-bit '(:eax :ecx :edx :ebx :esp :ebp :esi :edi))
			  (:64-bit '(:rax :rcx :rdx :rbx :rsp :rbp :rsi :rdi :r8 :r9 :r10 :r11 :r12 :r13 :r14 :r15))
			  (:mm '(:mm0 :mm1 :mm2 :mm3 :mm4 :mm5 :mm6 :mm7 :mm8))
			  (:xmm '(:xmm0 :xmm1 :xmm2 :xmm3 :xmm4 :xmm5 :xmm6 :xmm7))))
	       (reg-index (position op-reg reg-map)))
	  (when reg-index
	    (encode (encoded-values :opcode (+ opcode (ldb (byte 3 0) reg-index))
				    :operand-size operator-mode
1297
				    :immediate (encode-integer immediate type)
1298 1299 1300 1301 1302
				    :rex (cond
					   ((>= reg-index 8)
					    (assert (eq :64-bit operator-mode))
					    '(:rex.w :rex.r))
					   (t default-rex))))))))))
1303 1304

(defmacro opcode-reg-imm (opcode op-reg op-imm type)
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
  `(progn
     (assembler
      (return-when
       (encode-opcode-reg-imm operator legacy-prefixes ,opcode ,op-reg ,op-imm ',type operator-mode default-rex)))
     (disassembler
      (loop for reg from #b000 to #b111
	 do (define-disassembler (operator (logior ,opcode reg) operator-mode)
		decode-opcode-reg-imm
	      (operand-ordering operand-formals
				:reg ',op-reg
				:imm ',op-imm)
	      ',type)))))
1317

1318
(defmacro far-pointer (opcode segment offset offset-type &optional mode &rest extra)
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
  `(progn
     (assembler
      (when (and (immediate-p ,segment)
		 (indirect-operand-p ,offset)) ; FIXME: should be immediate-p, change in bootblock.lisp.
	(let ((segment (resolve-operand ,segment))
	      (offset (resolve-operand (car ,offset))))
	  (when (and (typep segment '(uint 16))
		     (typep offset ',offset-type))
	    (return-when (encode (encoded-values :opcode ,opcode
						 :immediate (append (encode-integer offset ',offset-type)
								    (encode-integer segment '(uint 16)))
						 ,@extra)))))))
     (disassembler
1332
      (define-disassembler (operator ,opcode ,(or mode 'operator-mode))
1333 1334
	  decode-far-pointer
	',offset-type))))
1335

1336

1337
;;;;;;;;;;; Pseudo-instructions
1338

1339
(define-operator/none :% (op &rest form)
1340 1341
  (case op
    (:bytes
1342 1343 1344
     (return-from operator
       (destructuring-bind (byte-size &rest data)
	   form
1345 1346 1347
	 (loop for datum in data
	    append (loop for b from 0 below byte-size by 8
		      collect (ldb (byte 8 b)
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
				   (resolve-operand datum)))))))
    (:funcall
     (return-from operator
       (destructuring-bind (function &rest args)
	   form
	 (apply function (mapcar #'resolve-operand args)))))
    (:fun
     (return-from operator
       (destructuring-bind (function &rest args)
	   (car form)
	 (loop for cbyte in (apply function (mapcar #'resolve-operand args))
	    append (loop for octet from 0 below (imagpart cbyte)
		      collect (ldb (byte 8 (* 8 octet))
				   (realpart cbyte)))))))
    (:format
     (return-from operator
       (destructuring-bind (byte-size format-control &rest format-args)
	   form
	 (ecase byte-size
	   (8 (let ((data (map 'list #'char-code
			       (apply #'format nil format-control
				      (mapcar #'resolve-operand format-args)))))
		(cons (length data)
		      data)))))))
    (:align
     (return-from operator
       (destructuring-bind (alignment)
	   form
	 (let* ((offset (mod *pc* alignment)))
	   (when (plusp offset)
	     (make-list (- alignment offset)
			:initial-element 0))))))))
1380 1381 1382 1383

;;;;;;;;;;; ADC

(define-operator/8 :adcb (src dst)
1384
  (imm src #x14 (xint 8) (dst :al))
1385 1386 1387 1388 1389 1390
  (imm-modrm src dst #x80 2 (xint 8))
  (reg-modrm dst src #x12)
  (reg-modrm src dst #x10))

(define-operator* (:16 :adcw :32 :adcl :64 :adcr) (src dst)
  (imm-modrm src dst #x83 2 (sint 8))
1391
  (imm src #x15 :int-16-32-64 (dst :ax-eax-rax))
1392 1393 1394 1395 1396
  (imm-modrm src dst #x81 2 :int-16-32-64)
  (reg-modrm dst src #x13)
  (reg-modrm src dst #x11))

;;;;;;;;;;; ADD
1397 1398

(define-operator/8 :addb (src dst)
1399
  (imm src #x04 (xint 8) (dst :al))
1400 1401 1402 1403
  (imm-modrm src dst #x80 0 (xint 8))
  (reg-modrm dst src #x02)
  (reg-modrm src dst #x00))

1404
(define-operator* (:16 :addw :32 :addl :64 :addr) (src dst)
1405
  (imm-modrm src dst #x83 0 (sint 8))
1406
  (imm src #x05 :int-16-32-64 (dst :ax-eax-rax))
1407
  (imm-modrm src dst #x81 0 :int-16-32-64)
1408 1409 1410
  (reg-modrm dst src #x03)
  (reg-modrm src dst #x01))

1411
;;;;;;;;;;; AND
1412

1413
(define-operator/8 :andb (mask dst)
1414
  (imm mask #x24 (xint 8) (dst :al))
1415 1416 1417 1418 1419 1420
  (imm-modrm mask dst #x80 4 (xint 8))
  (reg-modrm dst mask #x22)
  (reg-modrm mask dst #x20))

(define-operator* (:16 :andw :32 :andl :64 :andr) (mask dst)
  (imm-modrm mask dst #x83 4 (sint 8))
1421
  (imm mask #x25 :int-16-32-64 (dst :ax-eax-rax))
1422 1423 1424 1425 1426 1427
  (imm-modrm mask dst #x81 4 :int-16-32-64)
  (reg-modrm dst mask #x23)
  (reg-modrm mask dst #x21))

;;;;;;;;;;; BOUND, BSF, BSR, BSWAP

Frode Vatvedt Fjeld's avatar
Frode Vatvedt Fjeld committed
1428
(define-operator* (:16 :boundw :32 :bound) (bounds reg)
1429 1430 1431 1432 1433 1434 1435 1436
  (reg-modrm reg bounds #x62))

(define-operator* (:16 :bsfw :32 :bsfl :64 :bsfr) (src dst)
  (reg-modrm dst src #x0fbc))

(define-operator* (:16 :bsrw :32 :bsrl :64 :bsrr) (src dst)
  (reg-modrm dst src #x0fbd))

1437
(define-operator* (:32 :bswap :64 :bswapr) (dst)
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
  (opcode-reg #x0fc8 dst))

;;;;;;;;;;; BT, BTC, BTR, BTS

(define-operator* (:16 :btw :32 :btl :64 :btr) (bit src)
  (imm-modrm bit src #x0fba 4 (uint 8))
  (reg-modrm bit src #x0fa3))

(define-operator* (:16 :btcw :32 :btcl :64 :btcr) (bit src)
  (imm-modrm bit src #x0fba 7 (uint 8))
  (reg-modrm bit src #x0fbb))

(define-operator* (:16 :btrw :32 :btrl :64 :btrr) (bit src)
  (imm-modrm bit src #x0fba 6 (uint 8))
  (reg-modrm bit src #x0fb3))

(define-operator* (:16 :btsw :32 :btsl :64 :btsr) (bit src)
  (imm-modrm bit src #x0fba 5 (uint 8))
  (reg-modrm bit src #x0fab))

;;;;;;;;;;; CALL

1460 1461 1462 1463 1464 1465 1466
(define-operator/16 :callw (dest)
  (pc-rel #xe8 dest (sint 16))
  (modrm dest #xff 2))

(define-operator/32 :call (dest)
  (pc-rel #xe8 dest (sint 32))
  (modrm dest #xff 2))
1467

1468
(define-operator/none :call-segment (dest)
1469 1470
  (modrm dest #xff 3))

1471 1472
;;;;;;;;;;; CLC, CLD, CLI, CLTS, CMC

1473 1474 1475 1476 1477
(define-operator/none :clc () (opcode #xf8))
(define-operator/none :cld () (opcode #xfc))
(define-operator/none :cli () (opcode #xfa))
(define-operator/none :clts () (opcode #x0f06))
(define-operator/none :cmc () (opcode #xf5))
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564

;;;;;;;;;;; CMOVcc

(define-operator* (:16 :cmovaw :32 :cmova :64 :cmovar) (src dst)
  (reg-modrm dst src #x0f47)) ; Move if above, CF=0 and ZF=0.

(define-operator* (:16 :cmovaew :32 :cmovae :64 :cmovaer) (src dst)
  (reg-modrm dst src #x0f43)) ; Move if above or equal, CF=0.

(define-operator* (:16 :cmovbw :32 :cmovb :64 :cmovbr) (src dst)
  (reg-modrm dst src #x0f42)) ; Move if below, CF=1.

(define-operator* (:16 :cmovbew :32 :cmovbe :64 :cmovber) (src dst)
  (reg-modrm dst src #x0f46)) ; Move if below or  equal, CF=1 or ZF=1.

(define-operator* (:16 :cmovcw :32 :cmovc :64 :cmovcr) (src dst)
  (reg-modrm dst src #x0f42)) ; Move if carry, CF=1.

(define-operator* (:16 :cmovew :32 :cmove :64 :cmover) (src dst)
  (reg-modrm dst src #x0f44)) ; Move if equal, ZF=1.

(define-operator* (:16 :cmovgw :32 :cmovg :64 :cmovgr) (src dst)
  (reg-modrm dst src #x0f4f)) ; Move if greater, ZF=0 and SF=OF.

(define-operator* (:16 :cmovgew :32 :cmovge :64 :cmovger) (src dst)
  (reg-modrm dst src #x0f4d)) ; Move if greater or equal, SF=OF.

(define-operator* (:16 :cmovlw :32 :cmovl :64 :cmovlr) (src dst)
  (reg-modrm dst src #x0f4c))

(define-operator* (:16 :cmovlew :32 :cmovle :64 :cmovler) (src dst)
  (reg-modrm dst src #x0f4e)) ; Move if less or equal, ZF=1 or SF/=OF.

(define-operator* (:16 :cmovnaw :32 :cmovna :64 :cmovnar) (src dst)
  (reg-modrm dst src #x0f46)) ; Move if not above, CF=1 or ZF=1.

(define-operator* (:16 :cmovnaew :32 :cmovnae :64 :cmovnaer) (src dst)
  (reg-modrm dst src #x0f42)) ; Move if not above or equal, CF=1.

(define-operator* (:16 :cmovnbw :32 :cmovnb :64 :cmovnbr) (src dst)
  (reg-modrm dst src #x0f43)) ; Move if not below, CF=0.

(define-operator* (:16 :cmovnbew :32 :cmovnbe :64 :cmovnber) (src dst)
  (reg-modrm dst src #x0f47)) ; Move if not below or equal, CF=0 and ZF=0.

(define-operator* (:16 :cmovncw :32 :cmovnc :64 :cmovncr) (src dst)
  (reg-modrm dst src #x0f43)) ; Move if not carry, CF=0.

(define-operator* (:16 :cmovnew :32 :cmovne :64 :cmovner) (src dst)
  (reg-modrm dst src #x0f45)) ; Move if not equal, ZF=0.

(define-operator* (:16 :cmovngew :32 :cmovnge :64 :cmovnger) (src dst)
  (reg-modrm dst src #x0f4c)) ; Move if not greater or equal, SF/=OF.

(define-operator* (:16 :cmovnlw :32 :cmovnl :64 :cmovnlr) (src dst)
  (reg-modrm dst src #x0f4d)) ; Move if not less SF=OF.

(define-operator* (:16 :cmovnlew :32 :cmovnle :64 :cmovnler) (src dst)
  (reg-modrm dst src #x0f4f)) ; Move if not less or equal, ZF=0 and SF=OF.

(define-operator* (:16 :cmovnow :32 :cmovno :64 :cmovnor) (src dst)
  (reg-modrm dst src #x0f41)) ; Move if not overflow, OF=0.

(define-operator* (:16 :cmovnpw :32 :cmovnp :64 :cmovnpr) (src dst)
  (reg-modrm dst src #x0f4b)) ; Move if not parity, PF=0.

(define-operator* (:16 :cmovnsw :32 :cmovns :64 :cmovnsr) (src dst)
  (reg-modrm dst src #x0f49)) ; Move if not sign, SF=0.

(define-operator* (:16 :cmovnzw :32 :cmovnz :64 :cmovnzr) (src dst)
  (reg-modrm dst src #x0f45)) ; Move if not zero, ZF=0.

(define-operator* (:16 :cmovow :32 :cmovo :64 :cmovor) (src dst)
  (reg-modrm dst src #x0f40)) ; Move if overflow, OF=1.

(define-operator* (:16 :cmovpw :32 :cmovp :64 :cmovpr) (src dst)
  (reg-modrm dst src #x0f4a)) ; Move if parity, PF=1.

(define-operator* (:16 :cmovsw :32 :cmovs :64 :cmovsr) (src dst)
  (reg-modrm dst src #x0f48)) ; Move if sign, SF=1

(define-operator* (:16 :cmovzw :32 :cmovz :64 :cmovzr) (src dst)
  (reg-modrm dst src #x0f44)) ; Move if zero, ZF=1

;;;;;;;;;;; CMP

(define-operator/8 :cmpb (src dst)
1565
  (imm src #x3c (xint 8) (dst :al))
1566 1567 1568 1569 1570 1571
  (imm-modrm src dst #x80 7 (xint 8))
  (reg-modrm dst src #x3a)
  (reg-modrm src dst #x38))

(define-operator* (:16 :cmpw :32 :cmpl :64 :cmpr) (src dst)
  (imm-modrm src dst #x83 7 (sint 8))
1572
  (imm src #x3d :int-16-32-64 (dst :ax-eax-rax))
1573 1574 1575 1576 1577
  (imm-modrm src dst #x81 7 :int-16-32-64)
  (reg-modrm dst src #x3b)
  (reg-modrm src dst #x39))

;;;;;;;;;;; CMPXCHG
1578

1579 1580 1581 1582 1583 1584 1585 1586
(define-operator/8 :cmpxchgb (cmp-reg cmp-modrm al-dst)
  (when (eq al-dst :al)
    (reg-modrm cmp-reg cmp-modrm #x0fb0)))

(define-operator* (:16 :cmpxchgw :32 :cmpxchgl :64 :cmpxchgr) (cmp-reg cmp-modrm al-dst)
  (when (eq al-dst :ax-eax-rax)
    (reg-modrm cmp-reg cmp-modrm #x0fb1)))

1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
;;;;;;;;;;; CMPXCHG8B, CMPXCHG16B

(define-operator/32 :cmpxchg8b (address)
  (modrm address #x0fc7 1))

(define-operator/64 :cmpxchg16b (address)
  (modrm address #x0fc7 1))

;;;;;;;;;;; CPUID

1597
(define-operator/none :cpuid ()
1598 1599 1600 1601 1602 1603
  (opcode* #x0fa2))

;;;;;;;;;;; CWD, CDQ

(define-operator/16 :cwd (reg1 reg2)
  (when (and (eq reg1 :ax)
1604
	     (eq reg2 :dx))
1605 1606 1607 1608
    (opcode #x99)))

(define-operator/32 :cdq (reg1 reg2)
  (when (and (eq reg1 :eax)
1609
		 (eq reg2 :edx))
1610 1611 1612 1613
    (opcode #x99)))

(define-operator/64 :cqo (reg1 reg2)
  (when (and (eq reg1 :rax)
1614
		 (eq reg2 :rdx))
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
    (opcode #x99)))

;;;;;;;;;;; DEC

(define-operator/8 :decb (dst)
  (modrm dst #xfe 1))

(define-operator* (:16 :decw :32 :decl) (dst)
  (unless (eq *cpu-mode* :64-bit)
    (opcode-reg #x48 dst))
  (modrm dst #xff 1))

(define-operator* (:64 :decr) (dst)
  (modrm dst #xff 1))
    
;;;;;;;;;;; DIV

(define-operator/8 :divb (divisor dividend)
  (when (eq dividend :ax)
    (modrm divisor #xf6 6)))

(define-operator* (:16 :divw :32 :divl :64 :divr) (divisor dividend1 dividend2)
  (when (and (eq dividend1 :ax-eax-rax)
1638
	     (eq dividend2 :dx-edx-rdx))
1639 1640 1641 1642
    (modrm divisor #xf7 6)))

;;;;;;;;;;; HLT

1643
(define-operator/none :halt ()
1644 1645 1646 1647 1648 1649
  (opcode #xf4))

;;;;;;;;;;; IDIV

(define-operator/8 :idivb (divisor dividend1 dividend2)
  (when (and (eq dividend1 :al)
1650
	     (eq dividend2 :ah))
1651 1652 1653 1654
    (modrm divisor #xf6 7)))

(define-operator* (:16 :idivw :32 :idivl :64 :idivr) (divisor dividend1 dividend2)
  (when (and (eq dividend1 :ax-eax-rax)
1655
		 (eq dividend2 :dx-edx-rdx))
1656 1657 1658 1659 1660 1661 1662 1663
    (modrm divisor #xf7 7)))

;;;;;;;;;;; IMUL

(define-operator/32 :imull (factor product1 &optional product2)
  (when (not product2)
    (reg-modrm product1 factor #x0faf))
  (when (and (eq product1 :eax)
1664
	     (eq product2 :edx))
1665 1666