home *** CD-ROM | disk | FTP | other *** search
- ;;
- ;; Command file for post-processing output from disasm
- ;;
-
- (progn
- (goto-char (point-min))
- (message "remove trailing tabs")
- (replace-regexp "\t$" "" nil)
-
- (goto-char (point-min))
- (message "remove nop instructions")
- (replace-regexp " nop ; unreachable$" "" nil)
-
- (goto-char (point-min))
- (message "remove all 'Hidden label' messages")
- (replace-regexp "^Warning: Hidden label: L[0-9]+\n" "" nil)
-
- (goto-char (point-min))
- (message "Convert data references")
- (replace-regexp "\\(0x18[89][0-9a-f][0-9a-f][0-9a-f][0-9a-f]\\)" "D\\1" nil)
-
- (goto-char (point-min))
- (message "Make -1's from 0xffffffff's")
- (replace-string "0xffffffff" "-1" nil)
-
- ;; Make some simplifications
-
- ;; replace all $_... and $D... to &... to simplify later processing
- (goto-char (point-min))
- (message "Convert 'address of' to &")
- (replace-regexp "\$\\([_D]\\)" "&\\1" nil)
-
- (goto-char (point-min))
- (message "Convert 'movl &...' to assignment statement")
- (replace-regexp "movl \\(&.*\\),\\(.*\\)$" "\\2 = \\1;" nil)
-
- ;; move with converts
- (goto-char (point-min))
- (message "Convert movswl to assignment")
- (replace-regexp "movswl \\(.*\\),\\(%e..\\)$" "\\2 = (short)\\1;" nil)
- (goto-char (point-min))
- (message "Convert movsbl to assignment")
- (replace-regexp "movsbl \\(.*\\),\\(%e..\\)$" "\\2 = (char)\\1;" nil)
- (goto-char (point-min))
- (message "Convert movsbw to assignment")
- (replace-regexp "movsbw \\(.*\\),\\(%[a-z][a-z]\\)$" "\\2 = (char)\\1;" nil)
-
- (goto-char (point-min))
- (message "Convert movzwl to assignment")
- (replace-regexp "movzwl \\(.*\\),\\(%e..\\)$" "\\2 = (ushort)\\1;" nil)
- (goto-char (point-min))
- (message "Convert movzbl to assignment")
- (replace-regexp "movzbl \\(.*\\),\\(%e..\\)$" "\\2 = (uchar)\\1;" nil)
- (goto-char (point-min))
- (message "Convert movzbw to assignment")
- (replace-regexp "movzbw \\(.*\\),\\(%[a-z][a-z]\\)$" "\\2 = (uchar)\\1;" nil)
-
- ;; Convert register moves to assignment statements
- (goto-char (point-min))
- (message "Convert register moves to assignments")
- (replace-regexp "movl \\(.*\\),\\(%e..\\)$" "\\2 = \\1;" nil)
- (goto-char (point-min))
- (replace-regexp "movl \\(%e..\\),\\(.*\\)$" "\\2 = \\1;" nil)
- (goto-char (point-min))
- (replace-regexp "movw \\(%..\\),\\(.*\\)$" "(word)\\2 = \\1;" nil)
- (goto-char (point-min))
- (replace-regexp "movw \\(.*\\),\\(%..\\)$" "\\2 = (word)\\1;" nil)
- (goto-char (point-min))
- (replace-regexp "movb \\(%..\\),\\(.*\\)$" "(byte)\\2 = \\1;" nil)
- (goto-char (point-min))
- (replace-regexp "movb \\(.*\\),\\(%..\\)$" "\\2 = (byte)\\1;" nil)
-
- (goto-char (point-min))
- (message "Convert 'leal' to assignment")
- (replace-regexp "leal \\(.*\\),\\(%e..\\)$" "\\2 = &\\1;" nil)
-
-
- (goto-char (point-min))
- (message "Convert immediate moves to assignments")
- (replace-regexp "movb $\\([0-9]*\\|0x[0-9a-f]*\\),\\(.*\\)$" "(byte)\\2 = \\1;" nil)
- (goto-char (point-min))
- (replace-regexp "movw $\\([0-9]*\\|0x[0-9a-f]*\\),\\(.*\\)$" "(word)\\2 = \\1;" nil)
- (goto-char (point-min))
- (replace-regexp "movl $\\([0-9]*\\|0x[0-9a-f]*\\),\\(.*\\)$" "\\2 = \\1;" nil)
-
- (goto-char (point-min))
- (message "Fix return's")
- (replace-string "leave
- ret" "return" nil)
-
- (goto-char (point-min))
- (replace-string "popl %esi
- popl %edi
- popl %ebx
- return" "return" nil)
-
- (goto-char (point-min))
- (replace-string "popl %esi
- popl %edi
- return" "return" nil)
-
- (goto-char (point-min))
- (replace-string "popl %esi
- popl %ebx
- return" "return" nil)
-
- (goto-char (point-min))
- (replace-string "popl %esi
- return" "return" nil)
-
- (goto-char (point-min))
- (message "Make return 0's")
- (replace-string "%eax = 0
- return" "return 0;
- " nil)
-
- (goto-char (point-min))
- (message "Convert jump statement conditionals")
- (replace-string " je " " j== " nil)
- (goto-char (point-min))
- (replace-string " jne " " j!= " nil)
- (goto-char (point-min))
- (replace-string " jl " " j< " nil)
- (goto-char (point-min))
- (replace-string " jnl " " j>= " nil)
- (goto-char (point-min))
- (replace-string " jg " " j> " nil)
- (goto-char (point-min))
- (replace-string " jle " " j<= " nil)
-
- (goto-char (point-min))
- (message "Convert simple compare and branch to if-statement")
- (replace-regexp "cmpl \\($.*\\),\\(.*\\)
- j\\([!=<>]*\\) \\(.*$\\)" "if (\\2 \\3 \\1) goto \\4;" nil)
- (goto-char (point-min))
- (replace-regexp "cmpl \\(.*\\),\\(%[a-z]*\\)
- j\\([!=<>]*\\) \\(.*$\\)" "if (\\2 \\3 \\1) goto \\4;" nil)
- (goto-char (point-min))
- (replace-regexp "cmpl \\(%[a-z]*\\),\\(.*\\)
- j\\([!=<>]*\\) \\(.*$\\)" "if (\\2 \\3 \\1) goto \\4;" nil)
-
- (goto-char (point-min))
- (replace-regexp "cmpw \\($.*\\),\\(.*\\)
- j\\([!=<>]*\\) \\(.*$\\)" "if ((word)\\2 \\3 \\1) goto \\4;" nil)
- (goto-char (point-min))
- (replace-regexp "cmpw \\(.*\\),\\(%[a-z]*\\)
- j\\([!=<>]*\\) \\(.*$\\)" "if (\\2 \\3 (word)\\1) goto \\4;" nil)
- (goto-char (point-min))
- (replace-regexp "cmpw \\(%[a-z]*\\),\\(.*\\)
- j\\([!=<>]*\\) \\(.*$\\)" "if ((word)\\2 \\3 \\1) goto \\4;" nil)
-
- (goto-char (point-min))
- (replace-regexp "cmpb \\($.*\\),\\(.*\\)
- j\\([!=<>]*\\) \\(.*$\\)" "if ((byte)\\2 \\3 \\1) goto \\4;" nil)
- (goto-char (point-min))
- (replace-regexp "cmpb \\(.*\\),\\(%[a-z]*\\)
- j\\([!=<>]*\\) \\(.*$\\)" "if (\\2 \\3 (byte)\\1) goto \\4;" nil)
- (goto-char (point-min))
- (replace-regexp "cmpb \\(%[a-z]*\\),\\(.*\\)
- j\\([!=<>]*\\) \\(.*$\\)" "if ((byte)\\2 \\3 \\1) goto \\4;" nil)
-
- (goto-char (point-min))
- (message "Convert bit test statements to if's")
- (replace-regexp "testl $\\([0-9]+\\|0x[0-9a-f]+\\),\\(.*\\)
- j!= \\(.*\\)$" "if (\\2 & $\\1) goto \\3;" nil)
- (goto-char (point-min))
- (replace-regexp "testl \\(%e..\\),\\(.*\\)
- j!= \\(.*\\)$" "if (\\1 & \\2) goto \\3;" nil)
- (goto-char (point-min))
- (replace-regexp "testl \\(%e..\\),\\(.*\\)
- j== \\(.*\\)$" "if (!(\\1 & \\2)) goto \\3;" nil)
- (goto-char (point-min))
- (replace-regexp "testw $\\([0-9]+\\|0x[0-9a-f]+\\),\\(.*\\)
- j!= \\(.*\\)$" "if ((word)\\2 & $\\1) goto \\3;" nil)
- (goto-char (point-min))
- (replace-regexp "testb $\\([0-9]+\\|0x[0-9a-f]+\\),\\(.*\\)
- j!= \\(.*\\)$" "if ((byte)\\2 & $\\1) goto \\3;" nil)
-
- (goto-char (point-min))
- (replace-regexp "testl $\\([0-9]+\\|0x[0-9a-f]+\\),\\(.*\\)
- j== \\(.*\\)$" "if (!(\\2 & $\\1)) goto \\3;" nil)
- (goto-char (point-min))
- (replace-regexp "testw $\\([0-9]+\\|0x[0-9a-f]+\\),\\(.*\\)
- j== \\(.*\\)$" "if (!((word)\\2 & $\\1)) goto \\3;" nil)
- (goto-char (point-min))
- (replace-regexp "testb $\\([0-9]+\\|0x[0-9a-f]+\\),\\(.*\\)
- j== \\(.*\\)$" "if (!((byte)\\2 & $\\1)) goto \\3;" nil)
-
- (goto-char (point-min))
- (message "Fix a bug in the disasm program")
- (replace-regexp "\\(orb .*\\)
- Warning: Unreachable code" "\\1" nil)
-
- (goto-char (point-min))
- (message "Convert or to statement")
- (replace-regexp "\<orl \\(%[a-z]+\\),\\1
- j\\([!=<>]*\\) \\(.*$\\)" "if (\\1 \\2 0) goto \\3;" nil)
- (goto-char (point-min))
- (replace-regexp "\<orw \\(%[a-z]+\\),\\1
- j\\([!=<>]*\\) \\(.*$\\)" "if ((word)\\1 \\2 0) goto \\3;" nil)
- (goto-char (point-min))
- (replace-regexp "\<orb \\(%[a-z]+\\),\\1
- j\\([!=<>]*\\) \\(.*$\\)" "if ((word)\\1 \\2 0) goto \\3;" nil)
-
-
- (goto-char (point-min))
- (message "Convert jmp's to goto")
- (replace-string " jmp " " goto " nil)
-
- (goto-char (point-min))
- (message "Convert dec's and inc's")
- (replace-regexp " decl \\(.*\\)$" " \\1--;" nil)
- (goto-char (point-min))
- (replace-regexp " incl \\(.*\\)$" " \\1++;" nil)
-
- (goto-char (point-min))
- (replace-regexp " decw \\(.*\\)$" " (word)\\1--;" nil)
- (goto-char (point-min))
- (replace-regexp " incw \\(.*\\)$" " (word)\\1++;" nil)
-
- (goto-char (point-min))
- (message "Converting shl's and shr's")
- (replace-regexp " shl[lwb] $\\([0-9]+\\|0x[0-9a-f]+\\),\\(.*\\)$" " \\2 <<= $\\1;" nil)
- (goto-char (point-min))
- (replace-regexp " shl[lwb] \\(%[a-z]+\\)$" " \\1 <<= 1;" nil)
- (goto-char (point-min))
- (replace-regexp " shl[lwb] \\(%[a-z]+\\),\\(%[a-z]+\\)$" " \\1 <<= \\2;" nil)
- (goto-char (point-min))
- (replace-regexp " shr[lwb] $\\([0-9]+\\|0x[0-9a-f]+\\),\\(.*\\)$" " (unsigned)\\2 >>= $\\1;" nil)
- (goto-char (point-min))
- (replace-regexp " sar[lwb] $\\([0-9]+\\|0x[0-9a-f]+\\),\\(.*\\)$" " (int)\\2 >>= $\\1;" nil)
-
- (goto-char (point-min))
- (message "Subroutine entry")
- (replace-regexp "pushl %ebp
- %ebp = %esp;
- subl $\\(.*\\),%esp" "enter \\1" nil)
-
- (goto-char (point-min))
- (message "Converting and's, or's and xor's")
- (replace-regexp "andl \\($[0-9]*\\|$0x[0-9a-f]*\\|%[a-z]+\\),\\(.*\\)$" "\\2 &= \\1;" nil)
- (goto-char (point-min))
- (replace-regexp "andw \\($[0-9]*\\|$0x[0-9a-f]*\\|%[a-z]+\\),\\(.*\\)$" "(word)\\2 &= \\1;" nil)
- (goto-char (point-min))
- (replace-regexp "andb \\($[0-9]*\\|$0x[0-9a-f]*\\|%[a-z]+\\),\\(.*\\)$" "(byte)\\2 &= \\1;" nil)
- (goto-char (point-min))
- (replace-regexp "xorl \\($[0-9]*\\|$0x[0-9a-f]*\\|%[a-z]+\\),\\(.*\\)$" "\\2 ^= \\1;" nil)
- (goto-char (point-min))
- (replace-regexp "xorw \\($[0-9]*\\|$0x[0-9a-f]*\\|%[a-z]+\\),\\(.*\\)$" "(word)\\2 ^= \\1;" nil)
- (goto-char (point-min))
- (replace-regexp "xorb \\($[0-9]*\\|$0x[0-9a-f]*\\|%[a-z]+\\),\\(.*\\)$" "(byte)\\2 ^= \\1;" nil)
- (goto-char (point-min))
- (replace-regexp "orl \\($[0-9]*\\|$0x[0-9a-f]*\\|%[a-z]+\\),\\(.*\\)$" "\\2 |= \\1;" nil)
- (goto-char (point-min))
- (replace-regexp "orw \\($[0-9]*\\|$0x[0-9a-f]*\\|%[a-z]+\\),\\(.*\\)$" "(word)\\2 |= \\1;" nil)
- (goto-char (point-min))
- (replace-regexp "orb \\($[0-9]*\\|$0x[0-9a-f]*\\|%[a-z]+\\),\\(.*\\)$" "(byte)\\2 |= \\1;" nil)
-
- (goto-char (point-min))
- (message "Converting not's and neg's")
- (replace-regexp " %eax = \\(.*\\);
- notl %eax$" " %eax = ~( \\1 );" nil)
- (goto-char (point-min))
- (replace-regexp " notl \\(.*\\)$" " \\1 = ~( \\1 );" nil)
- (goto-char (point-min))
- (replace-regexp " %eax = \\(.*\\);
- negl %eax$" " %eax = -( \\1 );" nil)
- (goto-char (point-min))
- (replace-regexp " negl \\(.*\\)$" " \\1 = -( \\1 );" nil)
-
-
- (goto-char (point-min))
- (message "Make arg names")
- (replace-regexp "\\([^-_0-9a-z]\\)8(%ebp)" "\\1ARG_1" nil)
- (goto-char (point-min))
- (replace-regexp "\\([^-_0-9a-z]\\)12(%ebp)" "\\1ARG_2" nil)
- (goto-char (point-min))
- (replace-regexp "\\([^-_0-9a-z]\\)16(%ebp)" "\\1ARG_3" nil)
- (goto-char (point-min))
- (replace-regexp "\\([^-_0-9a-z]\\)20(%ebp)" "\\1ARG_4" nil)
- (goto-char (point-min))
- (replace-regexp "\\([^-_0-9a-z]\\)24(%ebp)" "\\1ARG_5" nil)
- (goto-char (point-min))
- (replace-regexp "\\([^-_0-9a-z]\\)28(%ebp)" "\\1ARG_6" nil)
- (goto-char (point-min))
- (replace-regexp "\\([^-_0-9a-z]\\)32(%ebp)" "\\1ARG_7" nil)
- (goto-char (point-min))
- (replace-regexp "\\([^-_0-9a-z]\\)36(%ebp)" "\\1ARG_8" nil)
- (goto-char (point-min))
- (replace-regexp "\\([^-_0-9a-z]\\)40(%ebp)" "\\1ARG_9" nil)
- (goto-char (point-min))
- (replace-regexp "-\\([0-9]+\\)(%ebp)" "L_\\1" nil)
-
- (goto-char (point-min))
- (message "Remove leading '_' from names")
- (replace-string "\n_" "\n#\n_" nil)
- (goto-char (point-min))
- (replace-regexp "\\([^_0-9a-z]\\)_\\([0-9a-z_]+\\)" "\\1\\2" nil)
-
- (goto-char (point-min))
- (message "pushl's")
- (replace-regexp "\\(%e..\\) = \\(.*\\);
- pushl \\1" "pushl \\1 = \\2" nil)
-
- (goto-char (point-min))
- (message "Return result from function calls")
- (replace-regexp " \\([_a-z][_a-z0-9]*(.*)\\);
- \\(.*\\) = %eax;$" " \\2 = %eax = \\1" nil)
-
- (goto-char (point-min))
- (message "Convert simple single-parameter procedure calls to c-sequence")
- (replace-regexp "pushl \\(.*\\)
- call \\(.*\\)
- addl $4,%esp" "\\2(\\1);" nil)
-
- (goto-char (point-min))
- (message "Convert simple double-parameter procedure calls to c-sequence")
- (replace-regexp "pushl \\(.*\\)
- pushl \\(.*\\)
- call \\(.*\\)
- addl $8,%esp" "\\3(\\2,\\1);" nil)
-
- (goto-char (point-min))
- (message "Convert simple triple-parameter procedure calls to c-sequence")
- (replace-regexp "pushl \\(.*\\)
- pushl \\(.*\\)
- pushl \\(.*\\)
- call \\(.*\\)
- addl $0xc,%esp" "\\4(\\3,\\2,\\1);" nil)
-
- (goto-char (point-min))
- (message "Convert simple quadruple-parameter procedure calls to c-sequence")
- (replace-regexp "pushl \\(.*\\)
- pushl \\(.*\\)
- pushl \\(.*\\)
- pushl \\(.*\\)
- call \\(.*\\)
- addl $0x10,%esp" "\\5(\\4,\\3,\\2,\\1);" nil)
-
- (goto-char (point-min))
- (message "Convert simple 5-parameter procedure calls to c-sequence")
- (replace-regexp "pushl \\(.*\\)
- pushl \\(.*\\)
- pushl \\(.*\\)
- pushl \\(.*\\)
- pushl \\(.*\\)
- call \\(.*\\)
- addl $0x14,%esp" "\\6(\\5,\\4,\\3,\\2,\\1);" nil)
-
- (goto-char (point-min))
- (message "Convert simple 6-parameter procedure calls to c-sequence")
- (replace-regexp "pushl \\(.*\\)
- pushl \\(.*\\)
- pushl \\(.*\\)
- pushl \\(.*\\)
- pushl \\(.*\\)
- pushl \\(.*\\)
- call \\(.*\\)
- addl $0x18,%esp" "\\7(\\6,\\5,\\4,\\3,\\2,\\1);" nil)
-
- (goto-char (point-min))
- (message "Zero-parameters procedure calls")
- (replace-regexp " call \\(.*\\)
- \\([^ag]\\)" " \\1();
- \\2" nil)
-
- (message "End of file")
- (goto-char (point-min))
- (replace-regexp " \\(%[a-z]+\\) ^= \\1;" " \\1 = 0;" nil)
-
- ;; new code
- (replace-regexp " %eax = \\(.*\\);
- return" " return \\1;" nil)
- )
-