[yalo commit] r129 - trunk/cc wiki

0 views
Skip to first unread message

codesite...@google.com

unread,
Feb 25, 2009, 10:21:13 AM2/25/09
to yalo-...@googlegroups.com
Author: yujian.zhang
Date: Wed Feb 25 06:15:29 2009
New Revision: 129

Modified:
trunk/cc/lap.lisp
trunk/cc/test-cc.lisp
trunk/cc/x86-64-syntax.lisp
wiki/AssemblyX64J.wiki

Log:
Add instruction jcc.

Modified: trunk/cc/lap.lisp
==============================================================================
--- trunk/cc/lap.lisp (original)
+++ trunk/cc/lap.lisp Wed Feb 25 06:15:29 2009
@@ -142,14 +142,12 @@
(member (car (last x)) '(1 cl)))))
;; Special case for (shl/shr r/m8/16 1/cl).
(encode-complex (butlast e) (butlast (instruction-type e)) it cursor
bits))
- ((and (>= (length (str (car e))) 5) (string= (subseq (str (car e)) 0
4) "CMOV")
- (>= (cc->int (symb (subseq (str (car e)) 4))) 0))
- ;; CMOVcc.
+ ((cc-instruction? e 'cmov) ; CMOVcc.
(declare (ignore it))
- (let* ((cc (symb (subseq (str (car e)) 4)))
- (cc-code (cc->int cc))
- (e* (cons 'cmovcc (cdr e))))
- (match-n-encode e* cursor bits cc-code)))
+ (cc-encode e 'cmov cursor bits))
+ ((cc-instruction? e 'j) ; Jcc.
+ (declare (ignore it))
+ (cc-encode e 'j cursor bits))
(t
(declare (ignore it))
(case (car e)
@@ -174,53 +172,58 @@
(match-instruction e (instruction-type e) bits)
(encode-complex e type opcode cursor bits cc-code)))

-(defun encode-complex (instruction type opcode cursor bits &optional
(cc-code 0))
+(defun encode-complex (instruction type opcode cursor bits
+ &optional (cc-code 0))
"Encode instruction (with optional rex prefix). Other prefixes like
lock are directly handled in encode()."
(let* (rex-set ; Possibly containing a subset of {w r x b}.
(dummy (when (member* '(r/m64 r64 rax qword) type)
(push 'w rex-set)))
+ (encoded-len 0) ; Tracking for (R)IP relative encoding.
(remaining
(mapcan
#'(lambda (on)
- (cond
- ((numberp on) (list on))
- ((listp on)
- (ecase (car on)
- (+ (ecase (caddr on)
- (r (list (+ (cadr on)
- (reg->int (second instruction)))))
- (cc (list (+ (cadr on) cc-code)))))))
- (t
- (ecase on
- ((o16 o32 a16 a32) (size-prefix on bits))
- ((ib iw id io)
- (try-encode-bytes (instruction-value instruction type
- (on->in on))
- (on-length on)))
- ((rb rw rd ro)
- (try-encode-bytes
- `(- ,(instruction-value instruction type (on->in on))
- ,(+ cursor 1 (on-length on)))
- (on-length on)))
- ((/0 /1 /2 /3 /4 /5 /6 /7)
- (multiple-value-bind (mod-sib-disp rex-set*)
- (encode-r/m-sib-disp
- (instruction-value instruction type
- (find-r/m instruction type))
- on bits)
- (setf rex-set (append rex-set rex-set*))
- mod-sib-disp))
- (/r
- (multiple-value-bind (mod-sib-disp rex-set*)
- (encode-r/m-sib-disp
- (instruction-value instruction type
- (find-r/m instruction type))
- (instruction-value instruction type
- (find-reg instruction type))
- bits)
- (setf rex-set (append rex-set rex-set*))
- mod-sib-disp))))))
+ (let ((x
+ (cond
+ ((numberp on) (list on))
+ ((listp on)
+ (ecase (car on)
+ (+ (ecase (caddr on)
+ (r (list (+ (cadr on)
+ (reg->int (second
instruction)))))
+ (cc (list (+ (cadr on) cc-code)))))))
+ (t
+ (ecase on
+ ((o16 o32 a16 a32) (size-prefix on bits))
+ ((ib iw id io)
+ (try-encode-bytes (instruction-value
instruction type
+ (on->in
on))
+ (on-length on)))
+ ((rb rw rd ro)
+ (try-encode-bytes
+ `(- ,(instruction-value instruction type
(on->in on))
+ ,(+ cursor encoded-len (on-length on)))
+ (on-length on)))
+ ((/0 /1 /2 /3 /4 /5 /6 /7)
+ (multiple-value-bind (mod-sib-disp rex-set*)
+ (encode-r/m-sib-disp
+ (instruction-value instruction type
+ (find-r/m instruction
type))
+ on bits)
+ (setf rex-set (append rex-set rex-set*))
+ mod-sib-disp))
+ (/r
+ (multiple-value-bind (mod-sib-disp rex-set*)
+ (encode-r/m-sib-disp
+ (instruction-value instruction type
+ (find-r/m instruction
type))
+ (instruction-value instruction type
+ (find-reg instruction
type))
+ bits)
+ (setf rex-set (append rex-set rex-set*))
+ mod-sib-disp)))))))
+ (incf encoded-len (length x))
+ x))
opcode)))
(declare (ignore dummy))
(when (and rex-set (/= bits 64))
@@ -554,7 +557,7 @@
((rax rcx rdx rbx rsp rbp rsi rdi
r8 r9 r10 r11 r12 r13 r14 r15) 'r64)
((cs ds es ss fs gs) 'sreg)
- ((short byte word dword qword) operand)
+ ((near short byte word dword qword) operand)
(t 'label)))))

(defun r32? (op)
@@ -611,6 +614,22 @@
(s 8) (ns 9) ((p pe) 10) ((np po) 11)
((l nge) 12) ((ge nl) 13) ((le ng) 14) ((g nle) 15)
(t -1)))
+
+(defun cc-instruction? (e prefix)
+ "Returns T if instruction e contains instruction code with prefix."
+ (let* ((mnemonic (str (car e)))
+ (prefix-s (str prefix))
+ (prefix-len (length prefix-s)))
+ (and (>= (length mnemonic) (1+ prefix-len))
+ (string= (subseq mnemonic 0 prefix-len) prefix-s)
+ (>= (cc->int (symb (subseq mnemonic prefix-len))) 0))))
+
+(defun cc-encode (e prefix cursor bits)
+ "Encode instructions with conditional codes."
+ (let* ((cc (symb (subseq (str (car e)) (length (str prefix)))))
+ (cc-code (cc->int cc))
+ (e* (cons (symb prefix 'cc) (cdr e))))
+ (match-n-encode e* cursor bits cc-code)))

(defun string->bytes (s)
(map 'list #'char-code s))

Modified: trunk/cc/test-cc.lisp
==============================================================================
--- trunk/cc/test-cc.lisp (original)
+++ trunk/cc/test-cc.lisp Wed Feb 25 06:15:29 2009
@@ -168,6 +168,7 @@
(int 3)
(int #x10)
.loop
+ (je short .loop)
(jmp short .loop)
(lgdt (msg))
(lidt (msg))
@@ -213,6 +214,7 @@
(cmova ax bx)
(cmovc eax edx)
(cmove rdx r10)
+ (jb near msg)
(syscall)
(sysret)

@@ -227,14 +229,14 @@
"Miscellaneous instructions.")

(defparameter *misc-code*
- '(232 108 0 248 252 250 244 228 3 229 4 236 237 204 205 16 235 254
- 15 1 22 111 124 15 1 30 111 124 15 0 210 15 0 22 111 124 172 173
- 226 232 180 9 187 13 0 137 200 137 30 111 124 139 14 123 28 199 6
- 111 124 123 0 142 195 140 200 144 230 3 231 4 238 239 81 14 22 30
- 6 90 23 31 7 243 164 243 165 243 102 165 195 249 253 251 170 171
- 102 15 71 195 15 66 194 73 15 68 210 15 5 15 7 111 124 72 101 108
- 108 111 32 87 111 114 108 100 33 32 0 0 0 85 170 64 226 1 0 67 104
- 120 0 0 0 230 130 217 250 11 0))
+ '(232 116 0 248 252 250 244 228 3 229 4 236 237 204 205 16 116 254
+ 235 252 15 1 22 119 124 15 1 30 119 124 15 0 210 15 0 22 119 124
+ 172 173 226 230 180 9 187 13 0 137 200 137 30 119 124 139 14 123
+ 28 199 6 119 124 123 0 142 195 140 200 144 230 3 231 4 238 239 81
+ 14 22 30 6 90 23 31 7 243 164 243 165 243 102 165 195 249 253 251
+ 170 171 102 15 71 195 15 66 194 73 15 68 210 15 130 6 0 0 0 15 5
+ 15 7 119 124 72 101 108 108 111 32 87 111 114 108 100 33 32 0 0 0
+ 85 170 64 226 1 0 67 104 120 0 0 0 230 130 217 250 11 0))

(defparameter *address-asm*
'((org #x7c00)

Modified: trunk/cc/x86-64-syntax.lisp
==============================================================================
--- trunk/cc/x86-64-syntax.lisp (original)
+++ trunk/cc/x86-64-syntax.lisp Wed Feb 25 06:15:29 2009
@@ -116,6 +116,8 @@
((inc dword m) . (o32 #xff /0))
((int 3) . (#xcc))
((int imm8) . (#xcd ib))
+ ((jcc short (imm8 label imm16)) . ((+ #x70 cc) rb))
+ ((jcc near (imm32 label imm8 imm16)) . (#x0f (+ #x80 cc) rd))
((jmp short (imm8 label imm16)) . (#xeb rb))
((lldt (r/m16 r16 m)) . (#x0f #x00 /2))
((lodsb) . (#xac))

Modified: wiki/AssemblyX64J.wiki
==============================================================================
--- wiki/AssemblyX64J.wiki (original)
+++ wiki/AssemblyX64J.wiki Wed Feb 25 06:15:29 2009
@@ -4,6 +4,14 @@

<wiki:toc max_depth="1" />

+= jcc: Conditional Branch =
+
+|| Instruction || Opcode || 64-Bit Mode || 16/32-Bit Mode ||
Description ||
+|| jcc short imm8 || (+ 70 cc) rb || Valid || Valid ||
Short conditional jump ||
+|| jcc near imm32 || 0F (+ 80 cc) rd || Valid || Valid ||
Near conditional jump ||
+
+Please refer [AssemblyX64Overview#Conditional_Codes conditional codes] for
details.
+
= jmp: Jump =

|| Instruction || Opcode || 64-Bit Mode || 16/32-Bit Mode ||
Description ||

Reply all
Reply to author
Forward
0 new messages