Modified:
trunk/cc/lap.lisp
trunk/cc/test-cc.lisp
Log:
Add arith-syntax-2 to unify syntax tables for div/mul/neg/not.
Modified: trunk/cc/lap.lisp
==============================================================================
--- trunk/cc/lap.lisp (original)
+++ trunk/cc/lap.lisp Tue Feb 10 06:50:36 2009
@@ -75,9 +75,8 @@
"Processing the list with asm, and pretty print the bytes with pp-hex."
(pp-hex (asm listing)))
-(defun arith-syntax (mnemonic)
- "Return syntax table for arithmetic operations: add, and, cmp, or,
-sub, and xor."
+(defun arith-syntax-1 (mnemonic)
+ "Return syntax table for arithmetic operations: add/and/cmp/or/sub/xor."
(let ((base ; Base opcode for operation on r/m8 r8.
(ecase mnemonic
(add #x00) (and #x20) (cmp #x38)
@@ -86,23 +85,32 @@
(ecase mnemonic
(add '/0) (and '/4) (cmp '/7)
(or '/1) (sub '/5) (xor '/6))))
- `(((,mnemonic al imm8) . (,(+ base #x04) ib))
- ((,mnemonic ax (imm16 imm8)) . (o16 ,(+ base #x05) iw))
- ((,mnemonic eax (imm32 imm16 imm8)) . (o32 ,(+ base #x05) id))
- ((,mnemonic (r/m8 r8) imm8) . (#x80 ,opcode ib))
- ((,mnemonic byte m imm8) . (#x80 ,opcode ib))
- ((,mnemonic (r/m16 r16 m) imm16) . (o16 #x81 ,opcode iw))
- ((,mnemonic (r/m32 r32 m) imm32) . (o32 #x81 ,opcode id))
- ((,mnemonic (r/m16 r16) imm8) . (o16 #x83 ,opcode ib))
- ((,mnemonic (r/m32 r32) imm8) . (o32 #x83 ,opcode ib))
- ((,mnemonic word m imm8) . (o16 #x83 ,opcode ib))
- ((,mnemonic dword m imm8) . (o32 #x83 ,opcode ib))
- ((,mnemonic (r/m8 r8 m) r8) . (,base /r))
- ((,mnemonic (r/m16 r16 m) r16) . (o16 ,(+ base #x01) /r))
- ((,mnemonic (r/m32 r32 m) r32) . (o32 ,(+ base #x01) /r))
- ((,mnemonic r8 (r/m8 r8 m)) . (,(+ base #x02) /r))
- ((,mnemonic r16 (r/m16 r16 m)) . (o16 ,(+ base #x03) /r))
- ((,mnemonic r32 (r/m32 r32 m)) . (o32 ,(+ base #x03) /r)))))
+ `(((,mnemonic al imm8) . (,(+ base #x04) ib))
+ ((,mnemonic ax (imm16 imm8)) . (o16 ,(+ base #x05) iw))
+ ((,mnemonic eax (imm32 imm16 imm8)) . (o32 ,(+ base #x05) id))
+ ((,mnemonic (r/m8 r8) imm8) . (#x80 ,opcode ib))
+ ((,mnemonic byte m imm8) . (#x80 ,opcode ib))
+ ((,mnemonic (r/m16 r16 m) imm16) . (o16 #x81 ,opcode iw))
+ ((,mnemonic (r/m32 r32 m) imm32) . (o32 #x81 ,opcode id))
+ ((,mnemonic (r/m16 r16) imm8) . (o16 #x83 ,opcode ib))
+ ((,mnemonic (r/m32 r32) imm8) . (o32 #x83 ,opcode ib))
+ ((,mnemonic word m imm8) . (o16 #x83 ,opcode ib))
+ ((,mnemonic dword m imm8) . (o32 #x83 ,opcode ib))
+ ((,mnemonic (r/m8 r8 m) r8) . (,base /r))
+ ((,mnemonic (r/m16 r16 m) r16) . (o16 ,(+ base #x01) /r))
+ ((,mnemonic (r/m32 r32 m) r32) . (o32 ,(+ base #x01) /r))
+ ((,mnemonic r8 (r/m8 r8 m)) . (,(+ base #x02) /r))
+ ((,mnemonic r16 (r/m16 r16 m)) . (o16 ,(+ base #x03) /r))
+ ((,mnemonic r32 (r/m32 r32 m)) . (o32 ,(+ base #x03) /r)))))
+
+(defun arith-syntax-2 (mnemonic)
+ "Return syntax table for arithmetic operations: div/mul/neg/not."
+ (let ((opcode (ecase mnemonic
+ (div '/6) (mul '/4) (neg '/3) (not '/2))))
+ `(((,mnemonic (r/m8 r8)) . (#xf6 ,opcode))
+ ((,mnemonic byte m) . (#xf6 ,opcode))
+ ((,mnemonic (r/m16 r16)) . (#xf7 ,opcode))
+ ((,mnemonic word m) . (#xf7 ,opcode)))))
;;; Following are syntax tables for x86-64. For each entry, 1st part
;;; is the instruction type, 2nd part is the corresponding opcode.
@@ -115,16 +123,13 @@
;;; refer to http://code.google.com/p/yalo/wiki/AssemblyX64Overview")
(defparameter *x86-64-syntax-common*
- `(,@(arith-syntax 'add)
- ,@(arith-syntax 'and)
+ `(,@(arith-syntax-1 'add)
+ ,@(arith-syntax-1 'and)
((clc) . (#xf8))
((cld) . (#xfc))
((cli) . (#xfa))
- ,@(arith-syntax 'cmp)
- ((div (r/m8 r8)) . (#xf6 /6))
- ((div byte m) . (#xf6 /6))
- ((div (r/m16 r16)) . (#xf7 /6))
- ((div word m) . (#xf7 /6))
+ ,@(arith-syntax-1 'cmp)
+ ,@(arith-syntax-2 'div)
((hlt) . (#xf4))
((in al imm8) . (#xe4 ib))
((in ax imm8) . (#xe5 ib))
@@ -151,20 +156,11 @@
((movsb) . (#xa4))
((movsw) . (o16 #xa5))
((movsd) . (o32 #xa5))
- ((mul (r/m8 r8)) . (#xf6 /4))
- ((mul byte m) . (#xf6 /4))
- ((mul (r/m16 r16)) . (#xf7 /4))
- ((mul word m) . (#xf7 /4))
- ((neg (r/m8 r8)) . (#xf6 /3))
- ((neg byte m) . (#xf6 /3))
- ((neg (r/m16 r16)) . (#xf7 /3))
- ((neg word m) . (#xf7 /3))
+ ,@(arith-syntax-2 'mul)
+ ,@(arith-syntax-2 'neg)
((nop) . (#x90))
- ((not (r/m8 r8)) . (#xf6 /2))
- ((not byte m) . (#xf6 /2))
- ((not (r/m16 r16)) . (#xf7 /2))
- ((not word m) . (#xf7 /2))
- ,@(arith-syntax 'or)
+ ,@(arith-syntax-2 'not)
+ ,@(arith-syntax-1 'or)
((out imm8 r8) . (#xe6 ib)) ; (out imm8 al)
((out imm8 r16) . (#xe7 ib)) ; (out imm8 ax)
((out dx al) . (#xee))
@@ -201,7 +197,7 @@
((sti) . (#xfb))
((stosb) . (#xaa))
((stosw) . (#xab))
- ,@(arith-syntax 'sub)
+ ,@(arith-syntax-1 'sub)
((test al imm8) . (#xa8 ib))
((test ax (imm16 imm8)) . (#xa9 iw))
((test (r/m8 r8) imm8) . (#xf6 /0 ib))
@@ -210,7 +206,7 @@
((test word m (imm16 imm8)) . (#xf7 /0 iw))
((test (r/m8 r8 m) r8) . (#x84 /r))
((test (r/m16 r16 m) r16) . (#x85 /r))
- ,@(arith-syntax 'xor))
+ ,@(arith-syntax-1 'xor))
"Valid for both 16-bit and 64-bit modes.")
(defparameter *x86-64-syntax-16/32-bit-only*
@@ -733,7 +729,7 @@
(return o))))
(defun scaled-index? (v)
- "Returns T is v is a scaled index (e.g. eax*2)."
+ "Returns T if v is a scaled index (e.g. eax*2)."
;; TODO: using regular expression when available.
(let ((s (str v)))
(and (= (length s) 5)
Modified: trunk/cc/test-cc.lisp
==============================================================================
--- trunk/cc/test-cc.lisp (original)
+++ trunk/cc/test-cc.lisp Tue Feb 10 06:50:36 2009
@@ -9,48 +9,42 @@
(in-package :cc)
-(defun arith-test (mnemonic)
- "Return test codes for arithmetic operations: add, and, cmp, or,
-sub, and xor."
- `((,mnemonic al 8)
- (,mnemonic ax 1000)
- (,mnemonic bl 3)
- (,mnemonic byte (msg) 4)
- (,mnemonic bx 1234)
- (,mnemonic (msg) 5678)
- (,mnemonic cx 9)
- (,mnemonic word (msg) 12)
- (,mnemonic al bl)
- (,mnemonic (msg) ch)
- (,mnemonic cx bx)
- (,mnemonic (msg) dx)
- (,mnemonic ch (msg))
- (,mnemonic dx (msg))))
+(defun arith-test-1 (mnemonic)
+ "Return test codes for arithmetic operations: add/and/cmp/or/sub/xor."
+ `((,mnemonic al 8)
+ (,mnemonic ax 1000)
+ (,mnemonic bl 3)
+ (,mnemonic byte (msg) 4)
+ (,mnemonic bx 1234)
+ (,mnemonic (msg) 5678)
+ (,mnemonic cx 9)
+ (,mnemonic word (msg) 12)
+ (,mnemonic al bl)
+ (,mnemonic (msg) ch)
+ (,mnemonic cx bx)
+ (,mnemonic (msg) dx)
+ (,mnemonic ch (msg))
+ (,mnemonic dx (msg))))
+
+(defun arith-test-2 (mnemonic)
+ "Return test codes for arithmetic operations: div/mul/neg/not"
+ `((,mnemonic ch)
+ (,mnemonic byte (msg))
+ (,mnemonic di)
+ (,mnemonic word (bp si 3))))
(defparameter *arith-asm*
`((bits 16)
(org #x7c00)
- ,@(arith-test 'add)
- ,@(arith-test 'and)
- ,@(arith-test 'cmp)
- (div ch)
- (div byte (msg))
- (div di)
- (div word (bp si 3))
- (mul ch)
- (mul byte (msg))
- (mul di)
- (mul word (bp si 3))
- (neg al)
- (neg byte (bx 3))
- (neg bx)
- (neg word (bp si 4))
- (not al)
- (not byte (bx 3))
- (not bx)
- (not word (bp si 4))
- ,@(arith-test 'or)
+ ,@(arith-test-1 'add)
+ ,@(arith-test-1 'and)
+ ,@(arith-test-1 'cmp)
+ ,@(arith-test-2 'div)
+ ,@(arith-test-2 'mul)
+ ,@(arith-test-2 'neg)
+ ,@(arith-test-2 'not)
+ ,@(arith-test-1 'or)
(shl dh 1)
(shl byte (msg) 1)
(shl dh cl)
@@ -75,7 +69,7 @@
(shr word (msg) cl)
(shr dx 5)
(shr word (msg) 5)
- ,@(arith-test 'sub)
+ ,@(arith-test-1 'sub)
(test al 8)
(test ax 1000)
(test bl 3)
@@ -86,7 +80,7 @@
(test (msg) ch)
(test cx bx)
(test (msg) dx)
- ,@(arith-test 'xor)
+ ,@(arith-test-1 'xor)
(bits 32)
(add ax 1000)
@@ -99,32 +93,32 @@
"Arithmetic instructions are tested separately.")
(defparameter *arith-code*
- '(4 8 5 232 3 128 195 3 128 6 217 125 4 129 195 210 4 129 6 217 125
- 46 22 131 193 9 131 6 217 125 12 0 216 0 46 217 125 1 217 1 22 217
- 125 2 46 217 125 3 22 217 125 36 8 37 232 3 128 227 3 128 38 217
- 125 4 129 227 210 4 129 38 217 125 46 22 131 225 9 131 38 217 125
- 12 32 216 32 46 217 125 33 217 33 22 217 125 34 46 217 125 35 22
- 217 125 60 8 61 232 3 128 251 3 128 62 217 125 4 129 251 210 4 129
- 62 217 125 46 22 131 249 9 131 62 217 125 12 56 216 56 46 217 125
- 57 217 57 22 217 125 58 46 217 125 59 22 217 125 246 245 246 54
- 217 125 247 247 247 114 3 246 229 246 38 217 125 247 231 247 98 3
- 246 216 246 95 3 247 219 247 90 4 246 208 246 87 3 247 211 247 82
- 4 12 8 13 232 3 128 203 3 128 14 217 125 4 129 203 210 4 129 14
- 217 125 46 22 131 201 9 131 14 217 125 12 8 216 8 46 217 125 9 217
- 9 22 217 125 10 46 217 125 11 22 217 125 208 230 208 38 217 125
- 210 230 210 38 217 125 192 230 5 192 38 217 125 5 209 226 209 38
- 217 125 211 226 211 38 217 125 193 226 5 193 38 217 125 5 208 238
- 208 46 217 125 210 238 210 46 217 125 192 238 5 192 46 217 125 5
- 209 234 209 46 217 125 211 234 211 46 217 125 193 234 5 193 46 217
- 125 5 44 8 45 232 3 128 235 3 128 46 217 125 4 129 235 210 4 129
- 46 217 125 46 22 131 233 9 131 46 217 125 12 40 216 40 46 217 125
- 41 217 41 22 217 125 42 46 217 125 43 22 217 125 168 8 169 232 3
- 246 195 3 246 6 217 125 4 247 195 210 4 247 6 217 125 46 22 132
- 216 132 46 217 125 133 217 133 22 217 125 52 8 53 232 3 128 243 3
- 128 54 217 125 4 129 243 210 4 129 54 217 125 46 22 131 241 9 131
- 54 217 125 12 48 216 48 46 217 125 49 217 49 22 217 125 50 46 217
- 125 51 22 217 125 102 5 232 3 72 5 3 2 1 16 72 101 108 108 111 32
- 87 111 114 108 100 33 32))
+ '(4 8 5 232 3 128 195 3 128 6 219 125 4 129 195 210 4 129 6 219 125
+ 46 22 131 193 9 131 6 219 125 12 0 216 0 46 219 125 1 217 1 22 219
+ 125 2 46 219 125 3 22 219 125 36 8 37 232 3 128 227 3 128 38 219
+ 125 4 129 227 210 4 129 38 219 125 46 22 131 225 9 131 38 219 125
+ 12 32 216 32 46 219 125 33 217 33 22 219 125 34 46 219 125 35 22
+ 219 125 60 8 61 232 3 128 251 3 128 62 219 125 4 129 251 210 4 129
+ 62 219 125 46 22 131 249 9 131 62 219 125 12 56 216 56 46 219 125
+ 57 217 57 22 219 125 58 46 219 125 59 22 219 125 246 245 246 54
+ 219 125 247 247 247 114 3 246 229 246 38 219 125 247 231 247 98 3
+ 246 221 246 30 219 125 247 223 247 90 3 246 213 246 22 219 125 247
+ 215 247 82 3 12 8 13 232 3 128 203 3 128 14 219 125 4 129 203 210
+ 4 129 14 219 125 46 22 131 201 9 131 14 219 125 12 8 216 8 46 219
+ 125 9 217 9 22 219 125 10 46 219 125 11 22 219 125 208 230 208 38
+ 219 125 210 230 210 38 219 125 192 230 5 192 38 219 125 5 209 226
+ 209 38 219 125 211 226 211 38 219 125 193 226 5 193 38 219 125 5
+ 208 238 208 46 219 125 210 238 210 46 219 125 192 238 5 192 46 219
+ 125 5 209 234 209 46 219 125 211 234 211 46 219 125 193 234 5 193
+ 46 219 125 5 44 8 45 232 3 128 235 3 128 46 219 125 4 129 235 210
+ 4 129 46 219 125 46 22 131 233 9 131 46 219 125 12 40 216 40 46
+ 219 125 41 217 41 22 219 125 42 46 219 125 43 22 219 125 168 8 169
+ 232 3 246 195 3 246 6 219 125 4 247 195 210 4 247 6 219 125 46 22
+ 132 216 132 46 219 125 133 217 133 22 219 125 52 8 53 232 3 128
+ 243 3 128 54 219 125 4 129 243 210 4 129 54 219 125 46 22 131 241
+ 9 131 54 219 125 12 48 216 48 46 219 125 49 217 49 22 219 125 50
+ 46 219 125 51 22 219 125 102 5 232 3 72 5 3 2 1 16 72 101 108 108
+ 111 32 87 111 114 108 100 33 32))
(defparameter *misc-asm*
'((bits 16)