operands.lisp 61.9 KB
Newer Older
1 2
;;;;------------------------------------------------------------------
;;;; 
3
;;;;    Copyright (C) 20012000, 2002-2004,
4
;;;;    Department of Computer Science, University of Tromso, Norway
5 6 7 8 9 10 11
;;;; 
;;;; Filename:      operands.lisp
;;;; Description:   Operand representation.
;;;; Author:        Frode Vatvedt Fjeld <frodef@acm.org>
;;;; Created at:    Wed Feb 16 14:02:57 2000
;;;; Distribution:  See the accompanying file COPYING.
;;;;                
12
;;;; $Id: operands.lisp,v 1.4 2004/02/10 00:03:42 ffjeld Exp $
13 14 15
;;;;                
;;;;------------------------------------------------------------------

16
(in-package #:ia-x86)
17 18 19 20 21 22 23 24 25 26 27 28 29 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 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 136 137 138 139

;;; ----------------------------------------------------------------
;;;                         Operand types
;;; ----------------------------------------------------------------

;;; Operand types are identified by symbols

(defmacro def-operand-types (&rest ot-specs)
  (list* 'cl:eval-when '(:load-toplevel)
	 (loop for (sym . properties) in ot-specs
	     append
	       `((setf ,@(loop for (p v) in properties
			     appending `((cl:get ',sym ',p) ,v)))
		 (import ',sym :ia-x86-instr)))))

(defun operand-type-property (sym p)
  (get sym p))

(def-operand-types
    (immediate (immediate t))
    (displacement (immediate nil))
    (imm32 (immediate t)
	   (bit-size 32)
	   (signed nil))
    (imm16 (immediate t)
	   (bit-size 16)
	   (signed nil))
  (imm8  (immediate t)
	 (bit-size 8)
	 (signed nil))
  (simm32 (immediate t)
	  (bit-size 32)
	  (signed t))
  (simm16 (immediate t)
	  (bit-size 16)
	  (signed t))
  (simm8  (immediate t)
	  (bit-size 8)
	  (signed t))
  (imm16-8 (immediate t)
	   (bit-size 16))
  (imm8-0 (immediate t)
	  (bit-size 8))
  (r32 (bit-size 32))
  (r16 (bit-size 16))
  (r8  (bit-size 8))
  (+r32 (bit-size 32))
  (+r16 (bit-size 16))
  (+r8  (bit-size 8))
  (m (bit-size 32))			; memory poiner
  (m64 (bit-size 64))
  (mm (bit-size 64))			; mmx register
  (mm/m64 (immediate nil))
  (mm/m32 (immediate nil))
  (xmm (bit-size 128))			; simd register
  (xmm/m128) (xmm/m64) (xmm/m32)
  (moffs8  (immediate nil)
	   (bit-size 8)
	   (signed nil))
  (moffs16 (immediate nil)
	   (bit-size 16)
	   (signed nil))
  (moffs32 (immediate nil)
	   (bit-size 32)
	   (signed nil))
  (al      (immediate nil)
	   (bit-size 8))
  (ah      (immediate nil)
	   (bit-size 8))
  (ax      (immediate nil)
	   (bit-size 16))
  (eax     (immediate nil)
	   (bit-size 32))
  (ebx     (immediate nil)
	   (bit-size 32))
  (dx      (immediate nil)
	   (bit-size 16))
  (edx     (immediate nil)
	   (bit-size 32))
  (cr0) (cr2) (cr3) (cr4)
  (dr0) (dr1) (dr2) (dr3)
  (dr4) (dr5) (dr6) (dr7)
  (ptr16-16 (immediate nil))
  (ptr16-32 (immediate nil))
  (m16-16 (immediate nil))
  (m16-32 (immediate nil))
  (m32real (immediate nil))
  (m64real (immediate nil))
  (m80real (immediate nil))
  (m16int (immediate nil))
  (m32int (immediate nil))
  (m64int (immediate nil)))

;;; ----------------------------------------------------------------
;;;                     Operand Class
;;; ----------------------------------------------------------------

(defclass operand () ())

(defclass concrete-operand (operand) ()
	  (:documentation "Operands that correspond directly
to one of the x86 operand adressing modes."))

(defclass abstract-operand (operand) ()
	  (:documentation "Operands that are not concrete, for example
symbolic references. Abstract operands need to be resolved to concrete
operands at encoding-time."))

(defmethod print-object ((obj concrete-operand) stream)
  (format stream "~A" (operand-listform obj))
  obj)

(defmethod print-object ((obj abstract-operand) stream)
  (format stream "~A" (operand-listform obj))
  obj)

;;; ----------------------------------------------------------------
;;;                        Abstract operands
;;; ----------------------------------------------------------------

(defun abstract-operand-to-offset (operand template instr env)
  (sign-extend (mod (- (operand-resolve-to-number operand env)
		       (assemble-env-current-pc env)
140
		       (template-instr-and-prefix-length template instr env))
141 142 143 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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
		    #x100000000)
	       4))

(defclass operand-label (abstract-operand)
  ((label
    :type symbol
    :initarg label
    :accessor operand-label)
   (user-size
    :initarg user-size
    :reader operand-user-size
    :initform nil)))

(defmethod operand-user-size ((operand t)) nil)

(defmethod operand-listform ((operand operand-label))
  (list* 'quote
	 (operand-label operand)
	 (operand-user-size operand)))

(defmethod operand-resolve-to-number ((operand operand-label) env)
  (assert (not (null env)) ()
    "Resolving ~A requires an assemble-environment." operand)
  (symtab-lookup-label (assemble-env-symtab env)
		       (operand-label operand)))

(defclass calculated-operand (abstract-operand)
  ((sub-operands
    :initarg :sub-operands
    :accessor sub-operands)
   (calculation
    :initarg :calculation
    :reader operand-calculation)
   (user-size
    :initarg user-size
    :reader operand-user-size
    :initform nil)))

(defmethod operand-resolve-to-number ((operand calculated-operand) env)
  (assert (not (null env)) ()
    "Resolving ~A requires an assemble-environment." operand)
  (apply (operand-calculation operand)
	 (mapcar #'operand-resolve-to-number
		 (sub-operands operand)
		 (let ((x (cons env nil)))
		   (setf (cdr x) x)))))	; make circular one-list.

(defclass operand-number (abstract-operand)
189 190 191 192
  ((number
    :type integer
    :initarg number
    :reader operand-number)))
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

(defmethod operand-listform ((operand operand-number))
  (list* 'quote
	 (operand-number operand)))

(defmethod operand-resolve-to-number ((operand operand-number) env)
  (declare (ignore env))
  (operand-number operand))

;;; ----------------------------------------------------------------
;;;     Concrete operands (modelling the "real world" x86 CPU)
;;; ----------------------------------------------------------------

;;; ----------------------------------------------------------------
;;;                          Immediate
;;; ----------------------------------------------------------------

(defclass operand-immediate (concrete-operand)
211 212 213
  ((value
    :initarg value
    :accessor operand-value)))
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 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 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 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 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 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 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 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 991 992 993 994 995 996 997 998 999 1000 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 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 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 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 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 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 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 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868

(defmethod operand-listform ((obj operand-immediate))
  (operand-value obj))

(defmethod print-object ((obj operand-immediate) stream)
  (if (and (not *print-readably*)
	   *print-pretty*)
      (progn
	(format stream "~A" (slot-value obj 'value))
	obj)
    (call-next-method obj stream)))

;;; ----------------------------------------------------------------
;;;                      Register
;;; ----------------------------------------------------------------

(defclass operand-register (concrete-operand)
  ((register
    :initarg register
    :accessor operand-register)))

(defmethod operand-listform ((obj operand-register))
  (operand-register obj))

(defmethod print-object ((obj operand-register) stream)
  (if (and (not *print-readably*)
	   *print-pretty*)
      (progn (format stream "%~A" (slot-value obj 'register))
	     obj)
    (call-next-method obj stream)))

;;; ----------------------------------------------------------------
;;;                        Memory operands
;;; ----------------------------------------------------------------

(defclass operand-memory (concrete-operand)
  (referenced-size))

;;; ----------------------------------------------------------------
;;;                        Absolute Pointer
;;; ----------------------------------------------------------------

(defclass operand-direct (operand-memory)
  ((address :accessor operand-address
	    :initarg address)
   (segment :accessor operand-segment
	    :initform nil
	    :initarg segment)))

(defmethod operand-listform ((obj operand-direct))
  (if (null (operand-segment obj))
      (list (operand-address obj))
    (list (operand-segment obj)
	  (operand-address obj))))

(defmethod print-object ((obj operand-direct) stream)
  (if (not *print-readably*)
      (progn
	(format stream "[~@[~A:~]~A]"
		(operand-segment obj)
		(operand-address obj))
	obj)
    (call-next-method obj stream)))

;;; ----------------------------------------------------------------
;;;                     PC-Relative Pointer
;;; ----------------------------------------------------------------

(defclass operand-rel-pointer (operand-memory)
  ((offset
    :accessor operand-offset
    :initarg offset)))

(defmethod operand-listform ((obj operand-rel-pointer))
  (list :pc+ (operand-offset obj)))

(defmethod print-object ((obj operand-rel-pointer) stream)
  (if (not *print-readably*)
      (progn
	(format stream "%PC+~A" (slot-value obj 'offset))
	obj)
    (call-next-method obj stream)))

;;; ----------------------------------------------------------------
;;;                  Register-Relative pointer
;;; ----------------------------------------------------------------

(defclass operand-indirect-register (operand-memory)
  ((register
    :accessor operand-register
    :initarg register)
   (register2
    :initform nil
    :accessor operand-register2
    :initarg register2)
   (offset
    :accessor operand-offset
    :initarg offset
    :initform 0)
   (scale
    :type (integer 0 8)			; scale for register (not register2)
    :initarg scale
    :accessor operand-scale
    :initform 1)))

(defmethod operand-listform ((obj operand-indirect-register))
  (with-slots (offset register scale register2)
      obj
    (append (unless (and (integerp offset) (zerop offset))
	      (list offset))
	    (if (= 1 scale)
		(list register)
	      (list (list register scale)))
	    (when register2
	      (list register2)))))

(defmethod print-object ((obj operand-indirect-register) stream)
  (if (not *print-readably*)
      (with-slots (offset register2 register scale) obj
	(format stream "[~@[~A+~]~@[%~A+~]%~A~@[*~D~]]"
		(unless (and (integerp offset) (zerop offset))
		  offset)
		register2
		register
		(when (> scale 1)
		  scale))
	obj)
    (call-next-method obj stream)))

(defun resolve-indirect-register (operand env)
  (with-slots (register register2 offset scale) operand
    (etypecase offset
      (integer
       operand)
      (symbol
       (make-instance 'operand-indirect-register
	 'offset (symtab-lookup-label (assemble-env-symtab env) offset)
	 'register register
	 'register2 register2
	 'scale scale))
      (list
       (make-instance 'operand-indirect-register
	 'offset (apply (car offset)
			(mapcar #'(lambda (o)
				    (etypecase o
				      (integer o)
				      (symbol
				       (symtab-lookup-label (assemble-env-symtab env) o))))
				(cdr offset)))
	 'register register
	 'register2 register2
	 'scale scale)))))

(defun resolve-direct (operand env)
  (with-slots (address segment) operand
    (if (not (symbolp address))
	operand
      (make-instance 'operand-direct
	'address (symtab-lookup-label (assemble-env-symtab env) address)
	'segment segment))))

;;; ----------------------------------------------------------------
;;;             Definition of specialized operand classes
;;; ----------------------------------------------------------------

(defvar *operand-classes* (make-hash-table :test #'equal))
(defvar *operand-encoding-by-type* (make-hash-table :test #'eq))

(defmacro def-operand-class ((operand-encoding operand-types
			      &optional (reg-set (first operand-types)))
			     (base-operand-class) slots)
  (let ((name (intern (format nil "~A~S~{-~S~}" ; the name isn't really important
			      (symbol-name '#:operand-)
			      operand-encoding operand-types))))
    `(progn
       (assert (subtypep (find-class ',base-operand-class)
			 (find-class 'operand))
	   ()
	 "Base operand-class ~A is not an OPERAND class." ',base-operand-class)
       (defclass ,name (,base-operand-class) ,slots)
       (defmethod operand-class-register-set ((operand-encoding (eql (find-class ',name))))
	 (values ',reg-set))
       (defmethod operand-class-encoding ((operand-encoding (eql (find-class ',name))))
	 (values ',operand-encoding))
       (defmethod operand-class-base-class ((operand-class (eql (find-class ',name))))
	 (values (find-class ',base-operand-class)))
       ,@(loop for ot in operand-types
	     appending
	       `((setf (gethash (cons ',operand-encoding ',ot) *operand-classes*) 
		   (find-class ',name))
		 (pushnew ',operand-encoding
			  (gethash ',ot *operand-encoding-by-type*)))))))

(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter +operand-types-indirect-modrm+
      '(r/m8 r/m16 r/m32 m m64
	mm/m64 mm/m32
	xmm/m128 xmm/m64 xmm/m32
	m32real m64real m80real
	m16int m32int m64int
	m16-16 m16-32)
    "This set of operand-types are pointers which are encoded the same way,
but differ in only in what they point to."))

(defmacro def-operand-class-imodrm ((operand-encoding
				     &optional (reg-set nil))
				    (base-operand-class) slots)
  `(def-operand-class (,operand-encoding ,+operand-types-indirect-modrm+ ,reg-set)
       (,base-operand-class) ,slots))
     

(defun find-operand-class (operand-encoding operand-type &key (errorp t))
  "Locate the operand class that encodes <operand-type> into <operand-encoding>."
  (let ((oc (gethash (cons operand-encoding operand-type) *operand-classes*)))
    (unless (or oc (not errorp))
      (error "couldn't find operand-class for (~A ~A)." operand-encoding operand-type))
    (values oc)))

(defun find-operand-type-encodings (operand-type)
  (gethash operand-type *operand-encoding-by-type*))

(defmethod operand-decode (operand (encoding (eql nil)) instr-symbolic)
  "Fallback when no operand-encoding was specified."
  (if (operand-class-encoding (class-of operand))
      (operand-decode operand (operand-class-encoding (class-of operand)) instr-symbolic)
    (call-next-method operand encoding instr-symbolic)))


;;; ----------------------------------------------------------------
;;;                     Operand unification
;;; ----------------------------------------------------------------

(defgeneric operand-and-encoding-unifies-p (operand encoding operand-type)
  (:documentation "This predicate determines if an operand instance may
be encoded in a particular encoding and operand-type."))

(defmethod operand-and-encoding-unifies-p (operand encoding operand-type)
  "If no specialized method exists, the operand and encoding don't unify."
  (declare (ignore operand encoding operand-type))
  (values nil))

(defun operand-unifies-with (operand operand-type)
  "Return a list of all encodings this operand unifies with."
  (loop for encoding in (find-operand-type-encodings operand-type)
      when (operand-and-encoding-unifies-p operand encoding operand-type)
      collect encoding))



(defgeneric operand-and-encoding-unify (operand encoding operand-type template instr env)
  (:documentation "If OPERAND cannot be encoded in ENCODING and
OPERAND-TYPE, NIL is returned. Otherwise, a concretized OPERAND
is returned (if OPERAND is concrete, the same operand is typically
returned."))

(defmethod operand-and-encoding-unify (operand encoding operand-type template instr env)
  "If no specialized method exists, the operand and encoding don't unify."
  (declare (ignore operand encoding operand-type template instr env))
  (values nil))

;;; ----------------------------------------------------------------
;;;                  General, plain operand classes
;;; ----------------------------------------------------------------

;;; Displacement

(def-operand-class (plain-displacement (displacement)) (operand-direct) ())

(defmethod operand-and-encoding-unify ((operand operand-direct)
				       (encoding (eql 'plain-displacement))
				       operand-type
				       template instr
				       env)
  (declare (ignore operand-type instr))
  (let ((resolved-operand (resolve-direct operand env)))
    (with-slots (address segment)
	resolved-operand
      (and (null segment)
	   (<= 0 address (expt 2 (* 8 (template-instr-displacement-numo template))))
	   resolved-operand))))

(defmethod operand-decode ((operand operand-direct)
			   (encoding (eql 'plain-displacement))
			   instr-symbolic)
  (setf (operand-address operand)
    (slot-value instr-symbolic 'displacement))
  (values operand))

(defmethod operand-encode ((operand operand-direct)
			   (encoding (eql 'plain-displacement))
			   operand-type
			   instr-symbolic)
  (declare (ignore operand-type))
  (setf (slot-value instr-symbolic 'displacement)
    (operand-address operand))
  (values instr-symbolic '(displacement)))

;;; Immediate

;;;(def-operand-class (plain-immediate (immediate)) (operand-immediate) ())
;;;
;;;(defmethod operand-and-encoding-unify ((operand operand-immediate)
;;;				       (encoding (eql 'plain-immediate))
;;;				       operand-type
;;;				       template instr
;;;				       env)
;;;  (declare (ignore operand-type instr env))
;;;  (with-slots (value)
;;;      operand
;;;    (and (<= 0 value (expt 2 (* 8 (template-instr-immediate-numo template))))
;;;	 operand)))
;;;
;;;(defmethod operand-decode ((operand operand-immediate)
;;;			   (encoding (eql 'plain-immediate))
;;;			   instr-symbolic)
;;;  (setf (operand-value operand)
;;;    (slot-value instr-symbolic 'immediate))
;;;  (values operand))
;;;
;;;(defmethod operand-encode ((operand operand-immediate)
;;;			   (encoding (eql 'plain-immediate))
;;;			   operand-type
;;;			   instr-symbolic)
;;;  (declare (ignore operand-type))
;;;  (setf (slot-value instr-symbolic 'immediate)
;;;    (operand-value operand))
;;;  (values instr-symbolic '(immediate)))


;;; ----------------------------------------------------------------
;;;                  Specialized operand classes
;;; ----------------------------------------------------------------


;;; Direct register operands encoded in the REG of MODR/M.

(def-operand-class (register-reg (r8)) (operand-register) ())
(def-operand-class (register-reg (r16)) (operand-register) ())
(def-operand-class (register-reg (r32)) (operand-register) ())
(def-operand-class (register-reg (sreg)) (operand-register) ())
(def-operand-class (register-reg (mm)) (operand-register) ()) ; MMX
(def-operand-class (register-reg (xmm)) (operand-register) ()) ; SIMD

(defmethod operand-and-encoding-unify ((operand operand-register)
				       (encoding (eql 'register-reg))
				       operand-type
				       template instr
				       env)
  (declare (ignore template instr env))
  (and (decode-set (find-register-encode-set operand-type)
		   (slot-value operand 'register)
		   :errorp nil)
       operand))

(defmethod operand-decode ((operand operand-register)
			   (encoding (eql 'register-reg))
			   instr-symbolic)
  (setf (operand-register operand)
    (decode-set (find-register-decode-set (operand-class-register-set (class-of operand)))
		(slot-value instr-symbolic 'reg)))
  (assert (not (null (operand-register operand)))
      ((operand-register operand))
    "Unable to decode operand value ~A from set ~A"
    (slot-value instr-symbolic 'reg)
    (find-register-decode-set (operand-class-register-set (class-of operand))))
  (values operand))

(defmethod operand-encode ((operand operand-register)
			   (encoding (eql 'register-reg))
			   operand-type
			   instr-symbolic)
  (setf (slot-value instr-symbolic 'reg)
    (decode-set (find-register-encode-set operand-type)
		(operand-register operand)))
  (values instr-symbolic '(reg)))


;;; Direct register operands encoded in the R/M of of MODR/M.

(def-operand-class (register-r/m (r/m8)) (operand-register) ())
(def-operand-class (register-r/m (r/m16)) (operand-register) ())
(def-operand-class (register-r/m (r/m32)) (operand-register) ())
(def-operand-class (register-r/m (mm/m64)) (operand-register) ()) ; MMX
(def-operand-class (register-r/m (xmm/m128 xmm/m64 xmm/m32)) (operand-register) ()) ; SIMD

(defmethod operand-and-encoding-unify ((operand operand-register)
				       (encoding (eql 'register-r/m))
				       operand-type
				       template instr
				       env)
  (declare (ignore template instr env))
  (and (decode-set (find-register-encode-set operand-type)
		   (slot-value operand 'register)
		   :errorp nil)
       operand))

(defmethod operand-decode ((operand operand-register)
			   (encoding (eql 'register-r/m))
			   instr-symbolic)
  (assert (= #b11 (slot-value instr-symbolic 'mod)))
  (setf (operand-register operand)
    (decode-set (find-register-decode-set (operand-class-register-set (class-of operand)))
		(slot-value instr-symbolic 'r/m)))
  (values operand))

(defmethod operand-encode ((operand operand-register)
			   (encoding (eql 'register-r/m))
			   operand-type
			   instr-symbolic)
  (with-slots (mod r/m)
      instr-symbolic
    (setf mod #b11
	  r/m (decode-set (find-register-encode-set operand-type)
			  (slot-value operand 'register))))
  (values instr-symbolic '(mod r/m)))


;;; Indirect register operand encoded in R/M,
;;; with Mod=00 and R/M /= {#b100, #b101}

(def-operand-class-imodrm (indirect-register-mod00) (operand-indirect-register) ())

(defmethod operand-and-encoding-unify ((operand operand-indirect-register)
				       (encoding (eql 'indirect-register-mod00))
				       operand-type
				       template instr
				       env)
  (declare (ignore template instr))
  (let ((resolved-operand (resolve-indirect-register operand env)))
    (with-slots (offset register register2 scale)
	resolved-operand
      (and (member operand-type +operand-types-indirect-modrm+)
	   (zerop offset)
	   (not register2)
	   (= 1 scale)
	   (member register '(eax ecx edx ebx esi edi))
	   resolved-operand))))

(defmethod operand-decode ((operand operand-indirect-register)
			   (encoding (eql 'indirect-register-mod00))
			   instr-symbolic)
  (assert (= #b00 (slot-value instr-symbolic 'mod)))
  (assert (/= #b100 #b101 (slot-value instr-symbolic 'r/m)))
  (with-slots (register register2 offset scale)
      operand
    (setf register (decode-set (find-register-decode-set 'r/m32-00)
			       (slot-value instr-symbolic 'r/m))
	  register2 nil
	  offset 0
	  scale 1))
  (values operand))

(defmethod operand-encode ((operand operand-indirect-register)
			   (encoding (eql 'indirect-register-mod00))
			   operand-type
			   instr-symbolic)
  (declare (ignore operand-type))
  (with-slots (mod r/m)
      instr-symbolic
    (setf mod #b00
	  r/m (decode-set (find-register-encode-set 'r/m32-00)
			  (slot-value operand 'register))))
  (values instr-symbolic '(mod r/m)))
	  

;;; Indirect register with MOD=#b00, R/M=#b100 in ModR/M
;;; and neither index=#b100 nor base=#b101 in SIB.

(def-operand-class-imodrm (indirect-register-00-sib) (operand-indirect-register) ())

(defmethod operand-and-encoding-unify ((operand operand-indirect-register)
				       (encoding (eql 'indirect-register-00-sib))
				       operand-type
				       template instr
				       env)
  (declare (ignore template instr))
  (let ((resolved-operand (resolve-indirect-register operand env)))
    (with-slots (register register2 offset scale)
	resolved-operand
      (and (member operand-type +operand-types-indirect-modrm+)
	   (zerop offset)
	   (member register '(eax ecx edx ebx ebp esi edi))
	   (member register2 '(eax ecx edx ebx esp esi edi))
	   (member scale '(1 2 4 8))
	   resolved-operand))))

(defmethod operand-decode ((operand operand-indirect-register)
			   (encoding (eql 'indirect-register-00-sib))
			   instr-symbolic)
  (assert (= #b00 (slot-value instr-symbolic 'mod)))
  (assert (= #b100 (slot-value instr-symbolic 'r/m)))
  (assert (/= #b100 (slot-value instr-symbolic 'index)))
  (assert (/= #b101 (slot-value instr-symbolic 'base)))
  (with-slots (register register2 offset scale)
      operand
    (setf register2 (decode-set (find-register-decode-set 'sib-base-00)
			       (slot-value instr-symbolic 'base))
	  register (decode-set (find-register-decode-set 'sib-index)
				(slot-value instr-symbolic 'index))
	  scale (expt 2 (slot-value instr-symbolic 'scale))
	  offset 0))
  (values operand))
(defmethod operand-encode ((operand operand-indirect-register)
			   (encoding (eql 'indirect-register-00-sib))
			   operand-type
			   instr-symbolic)
  (declare (ignore operand-type))
  (with-slots (mod r/m base index scale)
      instr-symbolic
    (setf mod #b00
	  r/m #b100
	  base (decode-set (find-register-encode-set 'sib-base-00)
			   (slot-value operand 'register2))
	  index (decode-set (find-register-encode-set 'sib-index)
			    (slot-value operand 'register))
	  scale (cdr (assoc (slot-value operand 'scale)
			    '((0 . 0) (1 . 0) (2 . 1) (4 . 2) (8 . 3))))))
  (values instr-symbolic '(base index scale)))

;;; Indirect register with MOD=#b00, R/M=#b100 in ModR/M
;;; and base=#b101 and index/=#b100 in SIB.

(def-operand-class-imodrm (indirect-register-00-sib-base5) (operand-indirect-register) ())

(defmethod operand-and-encoding-unify ((operand operand-indirect-register)
				       (encoding (eql 'indirect-register-00-sib-base5))
				       operand-type
				       template instr
				       env)
  (declare (ignore template instr))
  (assert (member operand-type +operand-types-indirect-modrm+))
  (let ((resolved-operand (resolve-indirect-register operand env)))
    (with-slots (register register2 scale)
	resolved-operand
      (and (not register2)
	   (member register '(eax ecx edx ebx ebp esi edi))
	   (member scale '(1 2 4 8))
	   resolved-operand))))

(defmethod operand-decode ((operand operand-indirect-register)
			   (encoding (eql 'indirect-register-00-sib-base5))
			   instr-symbolic)
  (assert (= #b00 (slot-value instr-symbolic 'mod)))
  (assert (= #b100 (slot-value instr-symbolic 'r/m)))
  (assert (= #b101 (slot-value instr-symbolic 'base)))
  (assert (/= #b100 (slot-value instr-symbolic 'index)))
  (with-slots (register register2 offset scale)
      operand
    (setf register (decode-set (find-register-decode-set 'sib-index)
			       (slot-value instr-symbolic 'index))
	  register2 nil
	  offset (realpart (slot-value instr-symbolic 'displacement))
	  scale (expt 2 (slot-value instr-symbolic 'scale))))
  (values operand))
(defmethod operand-encode ((operand operand-indirect-register)
			   (encoding (eql 'indirect-register-00-sib-base5))
			   operand-type
			   instr-symbolic)
  (declare (ignore operand-type))
  (with-slots (mod r/m base index scale displacement)
      instr-symbolic
    (setf mod #b00
	  r/m #b100
	  base #b101
	  index (decode-set (find-register-encode-set 'sib-index)
			    (slot-value operand 'register))
	  scale (1- (integer-length (slot-value operand 'scale)))
	  displacement (realpart (slot-value operand 'offset))))
  (values instr-symbolic '(mod r/m base index scale)))


;;; Indirect register with MOD=#b00, R/M=#b100 in ModR/M
;;; and base/=#b101 and index=#b100 in SIB.

(def-operand-class-imodrm (indirect-register-00-sib-index4) (operand-indirect-register) ())

(defmethod operand-and-encoding-unify ((operand operand-indirect-register)
				       (encoding (eql 'indirect-register-00-sib-index4))
				       operand-type
				       template instr
				       env)
  (declare (ignore template instr))
  (assert (member operand-type +operand-types-indirect-modrm+))
  (let ((resolved-operand (resolve-indirect-register operand env)))
    (with-slots (register register2 offset scale)
	resolved-operand
      (and register
	   (zerop offset)
	   (null register2)
	   (= 1 scale)
	   (member register '(eax ecx edx ebx esp esi edi))
	   resolved-operand))))
					    
(defmethod operand-decode ((operand operand-indirect-register)
			   (encoding (eql 'indirect-register-00-sib-index4))
			   instr-symbolic)
  (assert (= #b00 (slot-value instr-symbolic 'mod)))
  (assert (= #b100 (slot-value instr-symbolic 'r/m)))
  (assert (= #b100 (slot-value instr-symbolic 'index)))
  (assert (/= #b101 (slot-value instr-symbolic 'base)))
  (with-slots (register offset scale)
      operand
    (setf register (decode-set (find-register-decode-set 'sib-base-00)
			       (slot-value instr-symbolic 'base))
	  offset 0
	  scale 1))
  (values operand))

(defmethod operand-encode ((operand operand-indirect-register)
			   (encoding (eql 'indirect-register-00-sib-index4))
			   operand-type
			   instr-symbolic)
  (declare (ignore operand-type))
  (with-slots (mod r/m index base scale)
      instr-symbolic
    (setf mod #b00
	  r/m #b100
	  index #b100
	  base (decode-set (find-register-encode-set 'sib-base-00)
			   (slot-value operand 'register))
	  scale 0))
  (values instr-symbolic '(mod r/m index base scale)))
	  

;;; Indirect pointer with MOD=#b00, R/M=#b100 in ModR/M
;;; and base=#b101 and index=#b100 in SIB.

(def-operand-class-imodrm (indirect-pointer-00-sib-index4-base5) (operand-direct) ())

(defmethod operand-and-encoding-unify ((operand operand-direct)
				       (encoding (eql 'indirect-pointer-00-sib-index4-base5))
				       operand-type
				       template instr
				       env)
  (declare (ignore template instr))
  (let ((resolved-operand (resolve-direct operand env)))
    (assert (member operand-type +operand-types-indirect-modrm+))
    (and (null (slot-value resolved-operand 'segment))
	 resolved-operand)))

(defmethod operand-decode ((operand operand-direct)
			   (encoding (eql 'indirect-pointer-00-sib-index4-base5))
			   instr-symbolic)
  (assert (= #b00 (slot-value instr-symbolic 'mod)))
  (assert (= #b100 (slot-value instr-symbolic 'r/m)))
  (assert (= #b101 (slot-value instr-symbolic 'base)))
  (assert (= #b100 (slot-value instr-symbolic 'index)))
  (setf (slot-value operand 'address)
    (realpart (slot-value instr-symbolic 'displacement)))
  (values operand))

(defmethod operand-encode ((operand operand-direct)
			   (encoding (eql 'indirect-pointer-00-sib-index4-base5))
			   operand-type
			   instr-symbolic)
  (declare (ignore operand-type))
  (with-slots (mod r/m base index scale displacement)
      instr-symbolic
    (setf mod #b00
	  r/m #b100
	  base #b101
	  index #b100
	  scale 0 ; don't care
	  displacement (slot-value operand 'address)))
  (values instr-symbolic '(mod r/m base index displacement)))

;;; Indirect pointer with MOD=#b00, R/M=#b101 in ModR/M

(def-operand-class-imodrm (indirect-pointer-00) (operand-direct) ())

(defmethod operand-and-encoding-unify ((operand operand-direct)
				       (encoding (eql 'indirect-pointer-00))
				       operand-type
				       template instr
				       env)
  (declare (ignore template instr))
  (assert (member operand-type +operand-types-indirect-modrm+))
  (let ((resolved-operand (resolve-direct operand env)))
    (and (null (slot-value resolved-operand 'segment))
	 resolved-operand)))

(defmethod operand-decode ((operand operand-direct)
			   (encoding (eql 'indirect-pointer-00))
			   instr-symbolic)
  (assert (= #b00 (slot-value instr-symbolic 'mod)))
  (assert (= #b101 (slot-value instr-symbolic 'r/m)))
  (setf (slot-value operand 'address)
    (realpart (slot-value instr-symbolic 'displacement)))
  (values operand))

(defmethod operand-encode ((operand operand-direct)
			   (encoding (eql 'indirect-pointer-00))
			   operand-type
			   instr-symbolic)
  (declare (ignore operand-type))
  (with-slots (mod r/m displacement)
      instr-symbolic
    (setf mod #b00
	  r/m #b101
	  displacement (realpart (slot-value operand 'address))))
  (values instr-symbolic '(mod r/m displacement)))


;;; Indirect register with MOD=#b01, R/M/=#b100 in ModR/M.

(def-operand-class-imodrm (indirect-register-01) (operand-indirect-register) ())

(defmethod operand-and-encoding-unify ((operand operand-indirect-register)
				       (encoding (eql 'indirect-register-01))
				       operand-type
				       template instr
				       env)
  (declare (ignore template instr))
  (assert (member operand-type +operand-types-indirect-modrm+))
  (let ((resolved-operand (resolve-indirect-register operand env)))
    (with-slots (register register2 offset scale)
	resolved-operand
      (and (= 1 scale)
	   (member register '(eax ecx edx ebx ebp esi edi))
	   (not register2)
	   (<= -128 offset 127)
	   resolved-operand))))
  
(defmethod operand-decode ((operand operand-indirect-register)
			   (encoding (eql 'indirect-register-01))
			   instr-symbolic)
  (assert (= #b01 (slot-value instr-symbolic 'mod)))
  (assert (/= #b100 (slot-value instr-symbolic 'r/m)))
  (with-slots (mod r/m displacement)
      instr-symbolic
    (with-slots (register offset)
	operand
      (setf register (decode-set (find-register-decode-set 'r/m32-01)
				 r/m)
	    offset (realpart (sign-extend-complex displacement)))))
  (values operand))

(defmethod operand-encode ((operand operand-indirect-register)
			   (encoding (eql 'indirect-register-01))
			   operand-type
			   instr-symbolic)
  (declare (ignore operand-type))
  (with-slots (mod r/m displacement)
      instr-symbolic
    (setf mod #b01
	  r/m (decode-set (find-register-encode-set 'r/m32-01)
			  (slot-value operand 'register))
	  displacement (realpart (slot-value operand 'offset))))
  (values instr-symbolic '(mod r/m displacement)))

;;; Indirect register with MOD=#b01, R/M=#b100 in ModR/M,
;;; index/=#b100 in SIB.

(def-operand-class-imodrm (indirect-register-01-sib) (operand-indirect-register) ())

(defmethod operand-and-encoding-unify ((operand operand-indirect-register)
				       (encoding (eql 'indirect-register-01-sib))
				       operand-type
				       template instr
				       env)
  (declare (ignore template instr))
  (assert (member operand-type +operand-types-indirect-modrm+))
  (let ((resolved-operand (resolve-indirect-register operand env)))
    (with-slots (register register2 offset scale)
	resolved-operand
      (cond
       ((and (member register '(eax ecx edx ebx ebp esi edi))
	     (member register2 '(eax ecx edx ebx esp ebp esi edi))
	     (member scale '(1 2 4 8) :test #'=)
	     (<= -128 offset 127))
	resolved-operand)
       ((and (member register2 '(eax ecx edx ebx ebp esi edi))
	     (member register '(eax ecx edx ebx esp ebp esi edi))
	     (= scale 1)
	     (<= -128 offset 127))
	;; exchange register and register2
	(make-instance 'operand-indirect-register
	  'offset offset
	  'register register2
	  'register2 register
	  'scale 1))
       (t nil)))))
	  
(defmethod operand-decode ((operand operand-indirect-register)
			   (encoding (eql 'indirect-register-01-sib))
			   instr-symbolic)
  (assert (= #b01 (slot-value instr-symbolic 'mod)))
  (assert (= #b100 (slot-value instr-symbolic 'r/m)))
  (assert (/= #b100 (slot-value instr-symbolic 'index)))
  (with-slots (register register2 scale offset)
      operand
    (setf register (decode-set (find-register-decode-set 'sib-index)
			       (slot-value instr-symbolic 'index))
	  scale (expt 2 (slot-value instr-symbolic 'scale))
	  register2 (decode-set (find-register-decode-set 'sib-base)
				(slot-value instr-symbolic 'base))
	  offset (realpart (slot-value instr-symbolic 'displacement)))) ; disp8
  (values operand))


(defmethod operand-encode ((operand operand-indirect-register)
			   (encoding (eql 'indirect-register-01-sib))
			   operand-type
			   instr-symbolic)
  (declare (ignore operand-type))
  (with-slots (mod r/m scale index base displacement)
      instr-symbolic
    (setf mod #b01
	  r/m #b100
	  index (decode-set (find-register-encode-set 'sib-index)
			    (slot-value operand 'register))
	  scale (1- (integer-length (slot-value operand 'scale)))
	  base (decode-set (find-register-encode-set 'sib-base)
			   (slot-value operand 'register2))
	  displacement (slot-value operand 'offset)))
  (values instr-symbolic '(mod r/m scale index base displacement)))


;;; Indirect register with MOD=#b01, R/M=#b100 in ModR/M,
;;; index=#b100 in SIB.

(def-operand-class-imodrm (indirect-register-01-sib-index4) (operand-indirect-register) ())

(defmethod operand-and-encoding-unify ((operand operand-indirect-register)
				       (encoding (eql 'indirect-register-01-sib-index4))
				       operand-type
				       template instr
				       env)
  (declare (ignore template instr))
  (assert (member operand-type +operand-types-indirect-modrm+))
  (let ((resolved-operand (resolve-indirect-register operand env)))
    (with-slots (register register2 offset scale)
	resolved-operand
      (and (member register '(eax ecx edx ebx esp ebp esi edi))
	   (not register2)
	   (<= -128 offset 127)
	   (= 1 scale)
	   resolved-operand))))

(defmethod operand-decode ((operand operand-indirect-register)
			   (encoding (eql 'indirect-register-01-sib-index4))
			   instr-symbolic)
  (assert (= #b01 (slot-value instr-symbolic 'mod)))
  (assert (= #b100 (slot-value instr-symbolic 'r/m)))
  (assert (= #b100 (slot-value instr-symbolic 'index)))
  (with-slots (register offset scale)
      operand
    (setf register (decode-set (find-register-decode-set 'sib-base)
			       (slot-value instr-symbolic 'base))
	  offset (realpart (slot-value instr-symbolic 'displacement))
	  scale 1))
  (values operand))

(defmethod operand-encode ((operand operand-indirect-register)
			   (encoding (eql 'indirect-register-01-sib-index4))
			   operand-type
			   instr-symbolic)
  (declare (ignore operand-type))
  (with-slots (mod r/m index base scale displacement)
      instr-symbolic
    (setf mod #b01
	  r/m #b100
	  index #b100
	  base (decode-set (find-register-encode-set 'sib-base)
			   (slot-value operand 'register))
	  scale 0			; don't care
	  displacement (slot-value operand 'offset)))
  (values instr-symbolic '(mod r/m index base scale displacement)))


;;; Indirect register with MOD=#b10, R/M/=#b100 in ModR/M.

(def-operand-class-imodrm (indirect-register-10) (operand-indirect-register) ())

(defmethod operand-and-encoding-unify ((operand operand-indirect-register)
				       (encoding (eql 'indirect-register-10))
				       operand-type
				       template instr
				       env)
  (declare (ignore template instr))
  (assert (member operand-type +operand-types-indirect-modrm+))
  (let ((resolved-operand (resolve-indirect-register operand env)))
    (with-slots (register register2 offset scale)
	resolved-operand
      (and (= 1 scale)
	   (member register '(eax ecx edx ebx ebp esi edi))
	   (not register2)
	   (<= #x-80000000 offset #xffffffff)
	   resolved-operand))))

(defmethod operand-decode ((operand operand-indirect-register)
			   (encoding (eql 'indirect-register-10))
			   instr-symbolic)
  (assert (= #b10 (slot-value instr-symbolic 'mod)))
  (assert (/= #b100 (slot-value instr-symbolic 'r/m)))
  (with-slots (mod r/m displacement)
      instr-symbolic
    (with-slots (register offset)
	operand
      (setf register (decode-set (find-register-decode-set 'r/m32-01)
				 r/m)
	    offset (realpart displacement))))
  (values operand))
(defmethod operand-encode ((operand operand-indirect-register)
			   (encoding (eql 'indirect-register-10))
			   operand-type
			   instr-symbolic)
  (declare (ignore operand-type))
  (with-slots (mod r/m displacement)
      instr-symbolic
    (setf mod #b10
	  r/m (decode-set (find-register-encode-set 'r/m32-01)
			  (slot-value operand 'register))
	  displacement (realpart (slot-value operand 'offset))))
  (values instr-symbolic '(mod r/m displacement)))

;;; Indirect register with MOD=#b10, R/M=#b100 in ModR/M,
;;; index/=#b100 in SIB.

(def-operand-class-imodrm (indirect-register-10-sib) (operand-indirect-register) ())

(defmethod operand-and-encoding-unify ((operand operand-indirect-register)
				       (encoding (eql 'indirect-register-10-sib))
				       operand-type
				       template instr
				       env)
  (declare (ignore template instr))
  (assert (member operand-type +operand-types-indirect-modrm+))
  (let ((resolved-operand (resolve-indirect-register operand env)))
    (with-slots (register register2 offset scale)
	resolved-operand
      (and (member register '(eax ecx edx ebx ebp esi edi))
	   (member register2 '(eax ecx edx ebx esp ebp esi edi))
	   (member scale '(1 2 4 8))
	   (<= #x-80000000 offset #xffffffff)
	   resolved-operand))))

(defmethod operand-decode ((operand operand-indirect-register)
			   (encoding (eql 'indirect-register-10-sib))
			   instr-symbolic)
  (assert (= #b10 (slot-value instr-symbolic 'mod)))
  (assert (= #b100 (slot-value instr-symbolic 'r/m)))
  (assert (/= #b100 (slot-value instr-symbolic 'index)))
  (with-slots (register register2 scale offset)
      operand
    (setf register (decode-set (find-register-decode-set 'sib-index)
			       (slot-value instr-symbolic 'index))
	  scale (expt 2 (slot-value instr-symbolic 'scale))
	  register2 (decode-set (find-register-decode-set 'sib-base)
				(slot-value instr-symbolic 'base))
	  offset (realpart (slot-value instr-symbolic 'displacement)))) ; disp8
  (values operand))


(defmethod operand-encode ((operand operand-indirect-register)
			   (encoding (eql 'indirect-register-10-sib))
			   operand-type
			   instr-symbolic)
  (declare (ignore operand-type))
  (with-slots (mod r/m scale index base displacement)
      instr-symbolic
    (setf mod #b10
	  r/m #b100
	  index (decode-set (find-register-encode-set 'sib-index)
			    (slot-value operand 'register))
	  scale (1- (integer-length (slot-value operand 'scale)))
	  base (decode-set (find-register-encode-set 'sib-base)
			   (slot-value operand 'register2))
	  displacement (slot-value operand 'offset)))
  (values instr-symbolic '(mod r/m scale index base displacement)))

;;; Indirect register with MOD=#b10, R/M=#b100 in ModR/M,
;;; index=#b100 in SIB.

(def-operand-class-imodrm (indirect-register-10-sib-index4) (operand-indirect-register) ())

(defmethod operand-and-encoding-unify ((operand operand-indirect-register)
				       (encoding (eql 'indirect-register-10-sib-index4))
				       operand-type
				       template instr
				       env)
  (declare (ignore template instr))
  (assert (member operand-type +operand-types-indirect-modrm+))
  (let ((resolved-operand (resolve-indirect-register operand env)))
    (with-slots (register register2 offset scale)
	resolved-operand
      (and (member register '(eax ecx edx ebx esp ebp esi edi))
	   (not register2)
	   (<= #x-80000000 offset #xffffffff)
	   (= 1 scale)
	   resolved-operand))))

(defmethod operand-decode ((operand operand-indirect-register)
			   (encoding (eql 'indirect-register-10-sib-index4))
			   instr-symbolic)
  (assert (= #b10 (slot-value instr-symbolic 'mod)))
  (assert (= #b100 (slot-value instr-symbolic 'r/m)))
  (assert (= #b100 (slot-value instr-symbolic 'index)))
  (with-slots (register offset scale)
      operand
    (setf register (decode-set (find-register-decode-set 'sib-base)
			       (slot-value instr-symbolic 'base))
	  offset (realpart (slot-value instr-symbolic 'displacement))
	  scale 1))
  (values operand))

(defmethod operand-encode ((operand operand-indirect-register)
			   (encoding (eql 'indirect-register-10-sib-index4))
			   operand-type
			   instr-symbolic)
  (declare (ignore operand-type))
  (with-slots (mod r/m index base scale displacement)
      instr-symbolic
    (setf mod #b10
	  r/m #b100
	  index #b100
	  base (decode-set (find-register-encode-set 'sib-base)
			   (slot-value operand 'register))
	  scale 0			; don't care
	  displacement (slot-value operand 'offset)))
  (values instr-symbolic '(mod r/m index base scale displacement)))

;;; Indirect 16-bit register with MOD=#b00, R/M/=#b110 in ModR/M,

(def-operand-class-imodrm (16bit-indirect-register-mod00) (operand-indirect-register) ())

(defmethod operand-and-encoding-unify ((operand operand-indirect-register)
				       (encoding (eql '16bit-indirect-register-mod00))
				       operand-type
				       template instr
				       env)
  (declare (ignore template instr))
  (assert (member operand-type +operand-types-indirect-modrm+))
  (let ((resolved-operand (resolve-indirect-register operand env)))
    (with-slots (register register2 offset scale)
	resolved-operand
      (and (= offset 0)
	   (= scale 1)
	   (if register2
	       (and (member register '(bx bp))
		    (member register2 '(si di)))
	     (member register '(si di bx)))
	   resolved-operand))))

(defmethod operand-decode ((operand operand-indirect-register)
			   (encoding (eql '16bit-indirect-register-mod00))
			   instr-symbolic)
  (assert (= #b00 (slot-value instr-symbolic 'mod)))
  (assert (/= #b110 (slot-value instr-symbolic 'r/m)))
  (with-slots (register register2 offset scale)
      operand
    (destructuring-bind (r1 . r2)
	(decode-set (find-register-decode-set 'r/m-16bit)
		    (slot-value instr-symbolic 'r/m))
      (setf register r1
	    register2 r2
	    offset 0
	    scale 1)))
  (values operand))

(defmethod operand-encode ((operand operand-indirect-register)
			   (encoding (eql '16bit-indirect-register-mod00))
			   operand-type
			   instr-symbolic)
  (declare (ignore operand-type))
  (with-slots (mod r/m)
      instr-symbolic
    (setf mod #b00
	  r/m (decode-set (find-register-encode-set 'r/m-16bit)
			  (cons (slot-value operand 'register)
				(slot-value operand 'register2)))))
  (values instr-symbolic '(mod r/m)))

;;; Indirect 16bit pointer with MOD=#b00, R/M=#b110 in ModR/M.

(def-operand-class-imodrm (16bit-indirect-pointer) (operand-direct) ())

(defmethod operand-and-encoding-unify ((operand operand-direct)
				       (encoding (eql '16bit-indirect-pointer))
				       operand-type
				       template instr
				       env)
  (declare (ignore template instr))
  (assert (member operand-type +operand-types-indirect-modrm+))
  (let ((resolved-operand (resolve-direct operand env)))
    (with-slots (address segment)
	resolved-operand
      (and (null segment)
	   (<= 0 address #xffff)
	   resolved-operand))))

(defmethod operand-decode ((operand operand-direct)
			   (encoding (eql '16bit-indirect-pointer))
			   instr-symbolic)
  (assert (= #b00 (slot-value instr-symbolic 'mod)))
  (assert (= #b110 (slot-value instr-symbolic 'r/m)))
  (with-slots (address)
      operand
    (setf address (realpart (slot-value instr-symbolic 'displacement))))
  (values operand))

(defmethod operand-encode ((operand operand-direct)
			   (encoding (eql '16bit-indirect-pointer))
			   operand-type
			   instr-symbolic)
  (declare (ignore operand-type))
  (with-slots (mod r/m displacement)
      instr-symbolic
    (setf mod #b00
	  r/m #b110
	  displacement (slot-value operand 'address)))
  (values instr-symbolic '(mod r/m displacement)))

;;; Indirect 16-bit register with MOD=#b01 in ModR/M.

(def-operand-class-imodrm (16bit-indirect-register-mod01) (operand-indirect-register) ())

(defmethod operand-and-encoding-unify ((operand operand-indirect-register)
				       (encoding (eql '16bit-indirect-register-mod01))
				       operand-type
				       template instr
				       env)
  (declare (ignore template instr))
  (assert (member operand-type +operand-types-indirect-modrm+))
  (let ((resolved-operand (resolve-indirect-register operand env)))
    (with-slots (register register2 offset scale)
	resolved-operand
      (and (= scale 1)
	   (<= -128 offset 127)
	   (if register2
	       (and (member register '(bx bp))
		    (member register2 '(si di)))
	     (member register '(si di bp bx)))
	   resolved-operand))))

(defmethod operand-decode ((operand operand-indirect-register)
			   (encoding (eql '16bit-indirect-register-mod01))
			   instr-symbolic)
  (assert (= #b01 (slot-value instr-symbolic 'mod)))
  (with-slots (register register2 offset scale)
      operand
    (destructuring-bind (r1 . r2)
	(decode-set (find-register-decode-set 'r/m-16bit)
		    (slot-value instr-symbolic 'r/m))
      (setf register r1
	    register2 r2
	    offset (sign-extend (realpart (slot-value instr-symbolic 'displacement)) 1)
	    scale 1)))
  (values operand))

(defmethod operand-encode ((operand operand-indirect-register)
			   (encoding (eql '16bit-indirect-register-mod01))
			   operand-type
			   instr-symbolic)
  (declare (ignore operand-type))
  (with-slots (mod r/m displacement)
      instr-symbolic
    (setf mod #b01
	  r/m (decode-set (find-register-encode-set 'r/m-16bit)
			  (cons (slot-value operand 'register)
				(slot-value operand 'register2)))
	  displacement (slot-value operand 'offset)))
  (values instr-symbolic '(mod r/m displacement)))

;;; Indirect 16-bit register with MOD=#b10 in ModR/M.

(def-operand-class-imodrm (16bit-indirect-register-mod10) (operand-indirect-register) ())

(defmethod operand-and-encoding-unify ((operand operand-indirect-register)
				       (encoding (eql '16bit-indirect-register-mod10))
				       operand-type
				       template instr
				       env)
  (declare (ignore template instr))
  (assert (member operand-type +operand-types-indirect-modrm+))
  (let ((resolved-operand (resolve-indirect-register operand env)))
    (with-slots (register register2 offset scale)
	resolved-operand
      (and (= scale 1)
	   (<= 0 offset #xffff)
	   (if register2
	       (and (member register '(bx bp))
		    (member register2 '(si di)))
	     (member register '(si di bp bx)))
	   resolved-operand))))

(defmethod operand-decode ((operand operand-indirect-register)
			   (encoding (eql '16bit-indirect-register-mod10))
			   instr-symbolic)
  (assert (= #b10 (slot-value instr-symbolic 'mod)))
  (with-slots (register register2 offset scale)
      operand
    (destructuring-bind (r1 . r2)
	(decode-set (find-register-decode-set 'r/m-16bit)
		    (slot-value instr-symbolic 'r/m))
      (setf register r1
	    register2 r2
	    offset (realpart (slot-value instr-symbolic 'displacement))
	    scale 1)))
  (values operand))

(defmethod operand-encode ((operand operand-indirect-register)
			   (encoding (eql '16bit-indirect-register-mod10))
			   operand-type
			   instr-symbolic)
  (declare (ignore operand-type))
  (assert (<= 0 (slot-value operand 'offset) #xffff))
  (with-slots (mod r/m displacement)
      instr-symbolic
    (setf mod #b10
	  r/m (decode-set (find-register-encode-set 'r/m-16bit)
			  (cons (slot-value operand 'register)
				(slot-value operand 'register2)))
	  displacement (slot-value operand 'offset)))
  (values instr-symbolic '(mod r/m displacement)))

;;; Absolute pointer encoded in the moffs operand-type

(def-operand-class (abs-pointer-moffs (moffs8 moffs16 moffs32))
    (operand-direct) ())

(defmethod operand-and-encoding-unify ((operand operand-direct)
				       (encoding (eql 'abs-pointer-moffs))
				       operand-type
				       template instr
				       env)
  (declare (ignore template instr))
  (let ((resolved-operand (resolve-direct operand env)))
    (with-slots (address)
	resolved-operand
      (and
       (ecase operand-type
	 (moffs8  (<= 0 address #xff))
	 (moffs16 (<= 0 address #xffff))
	 (moffs32 (<= 0 address #xffffffff)))
       resolved-operand))))
  
(defmethod operand-decode ((operand operand-direct)
			   (encoding (eql 'abs-pointer-moffs))
			   instr-symbolic)
  (with-slots (address)
      operand
    (setf address (realpart (slot-value instr-symbolic 'displacement))))
  (values operand))

(defmethod operand-encode ((operand operand-direct)
			   (encoding (eql 'abs-pointer-moffs))
			   operand-type
			   instr-symbolic)
  (declare (ignore operand-type))
  (with-slots (displacement)
      instr-symbolic
    (setf displacement (slot-value operand 'address)))
  (values instr-symbolic '(displacement)))

;;; Register constants (no encoding)

(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter +constant-register-operands+
      '(al ah ax eax
	bl bh bx ebx
	dl dh dx edx
	cl ch cx ecx
	cs ds es fs gs ss
	cr0 cr2 cr3 cr4
	dr0 dr1 dr2 dr3 dr4 dr5 dr6 dr7)))

(defmacro def-many-constant-registers (cr-list)
  (cons 'cl:progn
	(loop for cr in (symbol-value cr-list)
	    collect `(def-operand-class (register-constant (,cr)) (operand-register) ()))))

(def-many-constant-registers +constant-register-operands+)

(defmethod operand-and-encoding-unify ((operand operand-register)
				       (encoding (eql 'register-constant))
				       operand-type
				       template instr
				       env)
  (declare (ignore template instr env))
  (assert (member operand-type +constant-register-operands+))
  (and (eq operand-type
	   (slot-value operand 'register))
       operand))

(defmethod operand-decode ((operand operand-register)
			   (encoding (eql 'register-constant))
			   instr-symbolic)
  (declare (ignore instr-symbolic))
  (with-slots (register)
      operand
    (setf register (operand-class-register-set (class-of operand))))
  (values operand))

(defmethod operand-encode ((operand operand-register)
			   (encoding (eql 'register-constant))
			   operand-type
			   instr-symbolic)
  (declare (ignore operand-type))
  (values instr-symbolic '()))


;;; Immediate constants (no encoding)

(def-operand-class (register-constant (1)) (operand-immediate) ())

(defmethod operand-and-encoding-unify ((operand operand-immediate)
				       (encoding (eql 'register-constant))
				       operand-type
				       template instr
				       env)
  (declare (ignore template instr env))
  (and (= operand-type
	  (slot-value operand 'value))
   operand))

(defmethod operand-decode ((operand operand-immediate)
			   (encoding (eql 'register-constant))
			   instr-symbolic)
  (declare (ignore instr-symbolic))
  (with-slots (value)
      operand
    (setf value (operand-class-register-set (class-of operand))))
  (values operand))

(defmethod operand-encode ((operand operand-immediate)
			   (encoding (eql 'register-constant))
			   operand-type
			   instr-symbolic)
  (declare (ignore operand-type))
  (values instr-symbolic '()))


;;; Register encoded in the opcode (plus-format).

(def-operand-class (register-plus (+r8)) (operand-register) ())
(def-operand-class (register-plus (+r16)) (operand-register) ())
(def-operand-class (register-plus (+r32)) (operand-register) ())

(defmethod operand-and-encoding-unify ((operand operand-register)
				       (encoding (eql 'register-plus))
				       operand-type
				       template instr
				       env)
  (declare (ignore template instr env))
  (with-slots (register)
      operand
    (and (ecase operand-type
	   ((+r8) (member register '(al cl dl bl ah ch dh bh)))
	   ((+r16) (member register '(ax cx dx bx sp bp si di)))
	   ((+r32) (member register '(eax ecx edx ebx esp ebp esi edi))))
	 operand)))

(defmethod operand-decode ((operand operand-register)
			   (encoding (eql 'register-plus))
			   instr-symbolic)
  (with-slots (register)
      operand
    (setf register
      (decode-set (find-register-decode-set (operand-class-register-set (class-of operand)))
		  (ldb (byte 3 0)
		       (slot-value instr-symbolic 'opcode)))))
  (values operand))

(defmethod operand-encode ((operand operand-register)
			   (encoding (eql 'register-plus))
			   operand-type
			   instr-symbolic)
  (with-slots (opcode)
      instr-symbolic
    (setf (ldb (byte 3 0) opcode)
      (decode-set (find-register-encode-set operand-type)
		  (slot-value operand 'register))))
  (values instr-symbolic '(opcode)))


;;; Immediate values

(def-operand-class (immediate (imm8 simm8 imm16 simm32 imm32)) (operand-immediate) ())

(defmethod operand-and-encoding-unify ((operand operand-immediate)
				       (encoding (eql 'immediate))
				       operand-type
				       template instr
				       env)
  (declare (ignore template instr env))
  (with-slots (value)
      operand
    (and (ecase operand-type
	   (simm8 (<= #x-80 value #x7f))
	   (imm8  (<= 0 value #xff))
	   (imm16 (<= 0 value #xffff))
	   (simm32 (<= #x-80000000 value #xffffffff))
	   (imm32 (<= 0 value #xffffffff)))
	 operand)))

(defmethod operand-and-encoding-unify ((operand abstract-operand)
				       (encoding (eql 'immediate))
				       operand-type
				       template instr
				       env)
  (operand-and-encoding-unify (make-instance 'operand-immediate
				'value (operand-resolve-to-number operand env))
			      encoding operand-type
			      template instr env))

(defmethod operand-decode ((operand operand-immediate)
			   (encoding (eql 'immediate))
			   instr-symbolic)
  (with-slots (value)
      operand
    (setf value (sign-extend (realpart (slot-value instr-symbolic 'immediate))
			     (imagpart (slot-value instr-symbolic 'immediate)))))
  (values operand))

(defmethod operand-encode ((operand operand-immediate)
			   (encoding (eql 'immediate))
			   operand-type
			   instr-symbolic)
  (declare (ignore operand-type))
  (unless (instr-symbolic-reg instr-symbolic)
    (setf (instr-symbolic-reg instr-symbolic) 0)) ; don't care)
  (with-slots (immediate)
      instr-symbolic
    (setf immediate (slot-value operand 'value)))
  (values instr-symbolic '(immediate)))

;;; PC-relative addresses

(def-operand-class (pc-relative (rel8 rel16 rel32)) (operand-rel-pointer) ())

(defmethod operand-and-encoding-unify ((operand operand-rel-pointer)
				       (encoding (eql 'pc-relative))
				       operand-type
				       template instr
				       env)
  (declare (ignore template instr env))
  (with-slots (offset)
      operand
    (and (ecase operand-type
	   ((rel8)  (<= #x-80 offset #x7f))
	   ((rel16) (<= #x-8000 offset #x7fff))
	   ((rel32) (<= #x-80000000 offset #x7fffffff)))
	 operand)))

(defmethod operand-and-encoding-unify ((operand abstract-operand)
				       (encoding (eql 'pc-relative))
				       operand-type
				       template instr
				       env)
  (ecase operand-type
    ((rel8 rel32)
     (operand-and-encoding-unify (make-instance 'operand-rel-pointer
				   'offset (abstract-operand-to-offset operand
								       template
								       instr
								       env))
				 encoding operand-type
				 template instr env))
    ((rel16)
     ;; rel16 operands cause EIP to be masked with #x0000ffff
     (and (<= 0 (operand-resolve-to-number operand env) #x0000ffff)
	  (operand-and-encoding-unify (make-instance 'operand-rel-pointer
					'offset (abstract-operand-to-offset operand
									    template
									    instr
									    env))
				      encoding operand-type
				      template instr env)))))
	  
(defmethod operand-decode ((operand operand-rel-pointer)
			   (encoding (eql 'pc-relative))
			   instr-symbolic)
  (with-slots (offset)
      operand
    (setf offset
      (realpart (sign-extend-complex (slot-value instr-symbolic 'displacement)))))
  (values operand))

(defmethod operand-encode ((operand operand-rel-pointer)
			   (encoding (eql 'pc-relative))
			   operand-type
			   instr-symbolic)
  (declare (ignore operand-type))
  (with-slots (displacement)
      instr-symbolic
    (setf displacement
      (slot-value operand 'offset)))
  (values instr-symbolic '(displacement)))

;;; 32-bit Segmented addresses

(def-operand-class (ptr16-32 (ptr16-32)) (operand-direct) ())

(defmethod operand-and-encoding-unify ((operand operand-direct)
				       (encoding (eql 'ptr16-32))
				       operand-type
				       template instr
				       env)
  (declare (ignore template instr))
  (assert (eq operand-type 'ptr16-32))
  (let ((resolved-operand (resolve-direct operand env)))
    (with-slots (address segment)
	resolved-operand
      (and address
	   (<= 0 address #xffffffff)
	   segment
	   (<= 0 segment #xffff)
	   resolved-operand))))

(defmethod operand-decode ((operand operand-direct)
			   (encoding (eql 'ptr16-32))
			   instr-symbolic)
  (with-slots (address segment)
      operand
    (setf
	address (ldb (byte 32 0) (realpart (instr-symbolic-displacement instr-symbolic)))
	segment (ldb (byte 16 32) (realpart (instr-symbolic-displacement instr-symbolic)))))
  (values operand))

(defmethod operand-encode ((operand operand-direct)
			   (encoding (eql 'ptr16-32))
			   operand-type
			   instr-symbolic)
  (declare (ignore operand-type))
  (with-slots (displacement)
      instr-symbolic
    (setf
	(ldb (byte 32 0) displacement) (slot-value operand 'address)
	(ldb (byte 16 32) displacement) (slot-value operand 'segment)))
  (values instr-symbolic '(displacement)))

;;; 16-bit Segmented addresses

(def-operand-class (ptr16-16 (ptr16-16)) (operand-direct) ())

(defmethod operand-and-encoding-unify ((operand operand-direct)
				       (encoding (eql 'ptr16-16))
				       operand-type
				       template instr
				       env)
  (declare (ignore template instr))
  (assert (eq operand-type 'ptr16-16))
  (let ((resolved-operand (resolve-direct operand env)))
    (with-slots (address segment)
	resolved-operand
      (and address
	   (<= 0 address #xffff)
	   segment
	   (<= 0 segment #xffff)
	   resolved-operand))))

(defmethod operand-decode ((operand operand-direct)
			   (encoding (eql 'ptr16-16))
			   instr-symbolic)
  (with-slots (address segment)
      operand
    (setf
	address (ldb (byte 16 0) (realpart (instr-symbolic-displacement instr-symbolic)))
	segment (ldb (byte 16 16) (realpart (instr-symbolic-displacement instr-symbolic)))))
  (values operand))

(defmethod operand-encode ((operand operand-direct)
			   (encoding (eql 'ptr16-16))
			   operand-type
			   instr-symbolic)
  (declare (ignore operand-type))
  (with-slots (displacement)
      instr-symbolic
    (setf displacement 0
	  (ldb (byte 16 0) displacement) (slot-value operand 'address)
	  (ldb (byte 16 16) displacement) (slot-value operand 'segment)))
  (values instr-symbolic '(displacement)))

;;; Two immediate operands (for ENTER)

(def-operand-class (imm16-8 (imm16-8)) (operand-immediate) ())

(defmethod operand-and-encoding-unify ((operand operand-immediate)
				       (encoding (eql 'imm16-8))
				       operand-type
				       template instr
				       env)
  (declare (ignore template instr env))
  (assert (eq operand-type 'imm16-8))
  (with-slots (value)
      operand
    (and (<= 0 value #xffff)
	 operand)))

(defmethod operand-decode ((operand operand-immediate)
			   (encoding (eql 'imm16-8))
			   instr-symbolic)
  (with-slots (value)
      operand
    (setf value (ldb (byte 16 0)
		     (realpart (slot-value instr-symbolic 'immediate)))))
  (values operand))
					   
(defmethod operand-encode ((operand operand-immediate)
			   (encoding (eql 'imm16-8))
			   operand-type
			   instr-symbolic)
  (assert (eq operand-type 'imm16-8)
      (operand-type))
  (unless (instr-symbolic-immediate instr-symbolic)
    (setf (slot-value instr-symbolic 'immediate) 0))
  (with-slots (immediate)
      instr-symbolic
    (setf (ldb (byte 16 0) immediate)
      (slot-value operand 'value)))
  (values instr-symbolic '(immediate)))
					   

;;; Two immediate operands (for ENTER)

(def-operand-class (imm8-0 (imm8-0)) (operand-immediate) ())

(defmethod operand-and-encoding-unify ((operand operand-immediate)
				       (encoding (eql 'imm8-0))
				       operand-type
				       template instr
				       env)
  (declare (ignore template instr env))
  (assert (eq operand-type 'imm8-0))
  (with-slots (value)
      operand
    (and (<= 0 value #x7f)
	 operand)))

(defmethod operand-decode ((operand operand-immediate)
			   (encoding (eql 'imm8-0))
			   instr-symbolic)
  (with-slots (value)
      operand
    (setf value (ldb (byte 8 16)
		     (realpart (slot-value instr-symbolic 'immediate)))))
  (values operand))
					   
(defmethod operand-encode ((operand operand-immediate)
			   (encoding (eql 'imm8-0))
			   operand-type
			   instr-symbolic)
  (assert (eq operand-type 'imm8-0)
      (operand-type))
  (unless (instr-symbolic-immediate instr-symbolic)
    (setf (slot-value instr-symbolic 'immediate) 0))
  (with-slots (immediate)
      instr-symbolic
    (setf (ldb (byte 8 16) immediate)
      (slot-value operand 'value)))
  (values instr-symbolic '(immediate)))