home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2431 / macros < prev   
Encoding:
Text File  |  1991-01-01  |  12.3 KB  |  375 lines

  1. ;;
  2. ;; Command file for post-processing output from disasm
  3. ;;
  4.  
  5. (progn
  6. (goto-char (point-min))
  7. (message "remove trailing tabs")
  8. (replace-regexp "\t$" "" nil)
  9.  
  10. (goto-char (point-min))
  11. (message "remove nop instructions")
  12. (replace-regexp "    nop            ; unreachable$" "" nil)
  13.  
  14. (goto-char (point-min))
  15. (message "remove all 'Hidden label' messages")
  16. (replace-regexp "^Warning: Hidden label: L[0-9]+\n" "" nil)
  17.  
  18. (goto-char (point-min))
  19. (message "Convert data references")
  20. (replace-regexp "\\(0x18[89][0-9a-f][0-9a-f][0-9a-f][0-9a-f]\\)" "D\\1" nil)
  21.  
  22. (goto-char (point-min))
  23. (message "Make -1's from 0xffffffff's")
  24. (replace-string "0xffffffff" "-1" nil)
  25.  
  26. ;; Make some simplifications
  27.  
  28. ;; replace all $_... and $D... to &... to simplify later processing
  29. (goto-char (point-min))
  30. (message "Convert 'address of' to &")
  31. (replace-regexp "\$\\([_D]\\)" "&\\1" nil)
  32.  
  33. (goto-char (point-min))
  34. (message "Convert 'movl     &...' to assignment statement")
  35. (replace-regexp "movl    \\(&.*\\),\\(.*\\)$" "\\2 = \\1;" nil)
  36.  
  37. ;; move with converts
  38. (goto-char (point-min))
  39. (message "Convert movswl to assignment")
  40. (replace-regexp "movswl    \\(.*\\),\\(%e..\\)$" "\\2 = (short)\\1;" nil)
  41. (goto-char (point-min))
  42. (message "Convert movsbl to assignment")
  43. (replace-regexp "movsbl    \\(.*\\),\\(%e..\\)$" "\\2 = (char)\\1;" nil)
  44. (goto-char (point-min))
  45. (message "Convert movsbw to assignment")
  46. (replace-regexp "movsbw    \\(.*\\),\\(%[a-z][a-z]\\)$" "\\2 = (char)\\1;" nil)
  47.  
  48. (goto-char (point-min))
  49. (message "Convert movzwl to assignment")
  50. (replace-regexp "movzwl    \\(.*\\),\\(%e..\\)$" "\\2 = (ushort)\\1;" nil)
  51. (goto-char (point-min))
  52. (message "Convert movzbl to assignment")
  53. (replace-regexp "movzbl    \\(.*\\),\\(%e..\\)$" "\\2 = (uchar)\\1;" nil)
  54. (goto-char (point-min))
  55. (message "Convert movzbw to assignment")
  56. (replace-regexp "movzbw    \\(.*\\),\\(%[a-z][a-z]\\)$" "\\2 = (uchar)\\1;" nil)
  57.  
  58. ;; Convert register moves to assignment statements
  59. (goto-char (point-min))
  60. (message "Convert register moves to assignments")
  61. (replace-regexp "movl    \\(.*\\),\\(%e..\\)$" "\\2 = \\1;" nil)
  62. (goto-char (point-min))
  63. (replace-regexp "movl    \\(%e..\\),\\(.*\\)$" "\\2 = \\1;" nil)
  64. (goto-char (point-min))
  65. (replace-regexp "movw    \\(%..\\),\\(.*\\)$" "(word)\\2 = \\1;" nil)
  66. (goto-char (point-min))
  67. (replace-regexp "movw    \\(.*\\),\\(%..\\)$" "\\2 = (word)\\1;" nil)
  68. (goto-char (point-min))
  69. (replace-regexp "movb    \\(%..\\),\\(.*\\)$" "(byte)\\2 = \\1;" nil)
  70. (goto-char (point-min))
  71. (replace-regexp "movb    \\(.*\\),\\(%..\\)$" "\\2 = (byte)\\1;" nil)
  72.  
  73. (goto-char (point-min))
  74. (message "Convert 'leal' to assignment")
  75. (replace-regexp "leal    \\(.*\\),\\(%e..\\)$" "\\2 = &\\1;" nil)
  76.  
  77.  
  78. (goto-char (point-min))
  79. (message "Convert immediate moves to assignments")
  80. (replace-regexp "movb    $\\([0-9]*\\|0x[0-9a-f]*\\),\\(.*\\)$" "(byte)\\2 = \\1;" nil)
  81. (goto-char (point-min))
  82. (replace-regexp "movw    $\\([0-9]*\\|0x[0-9a-f]*\\),\\(.*\\)$" "(word)\\2 = \\1;" nil)
  83. (goto-char (point-min))
  84. (replace-regexp "movl    $\\([0-9]*\\|0x[0-9a-f]*\\),\\(.*\\)$" "\\2 = \\1;" nil)
  85.  
  86. (goto-char (point-min))
  87. (message "Fix return's")
  88. (replace-string "leave
  89.     ret" "return" nil)
  90.  
  91. (goto-char (point-min))
  92. (replace-string "popl    %esi
  93.     popl    %edi
  94.     popl    %ebx
  95.     return" "return" nil)
  96.  
  97. (goto-char (point-min))
  98. (replace-string "popl    %esi
  99.     popl    %edi
  100.     return" "return" nil)
  101.  
  102. (goto-char (point-min))
  103. (replace-string "popl    %esi
  104.     popl    %ebx
  105.     return" "return" nil)
  106.  
  107. (goto-char (point-min))
  108. (replace-string "popl    %esi
  109.     return" "return" nil)
  110.  
  111. (goto-char (point-min))
  112. (message "Make return 0's")
  113. (replace-string "%eax = 0
  114.     return" "return 0;
  115. " nil)
  116.  
  117. (goto-char (point-min))
  118. (message "Convert jump statement conditionals")
  119. (replace-string "    je    " "    j==    " nil)
  120. (goto-char (point-min))
  121. (replace-string "    jne    " "    j!=    " nil)
  122. (goto-char (point-min))
  123. (replace-string "    jl    " "    j<    " nil)
  124. (goto-char (point-min))
  125. (replace-string "    jnl    " "    j>=    " nil)
  126. (goto-char (point-min))
  127. (replace-string "    jg    " "    j>    " nil)
  128. (goto-char (point-min))
  129. (replace-string "    jle    " "    j<=    " nil)
  130.  
  131. (goto-char (point-min))
  132. (message "Convert simple compare and branch to if-statement")
  133. (replace-regexp "cmpl    \\($.*\\),\\(.*\\)
  134.     j\\([!=<>]*\\)    \\(.*$\\)" "if (\\2 \\3 \\1) goto \\4;" nil)
  135. (goto-char (point-min))
  136. (replace-regexp "cmpl    \\(.*\\),\\(%[a-z]*\\)
  137.     j\\([!=<>]*\\)    \\(.*$\\)" "if (\\2 \\3 \\1) goto \\4;" nil)
  138. (goto-char (point-min))
  139. (replace-regexp "cmpl    \\(%[a-z]*\\),\\(.*\\)
  140.     j\\([!=<>]*\\)    \\(.*$\\)" "if (\\2 \\3 \\1) goto \\4;" nil)
  141.  
  142. (goto-char (point-min))
  143. (replace-regexp "cmpw    \\($.*\\),\\(.*\\)
  144.     j\\([!=<>]*\\)    \\(.*$\\)" "if ((word)\\2 \\3 \\1) goto \\4;" nil)
  145. (goto-char (point-min))
  146. (replace-regexp "cmpw    \\(.*\\),\\(%[a-z]*\\)
  147.     j\\([!=<>]*\\)    \\(.*$\\)" "if (\\2 \\3 (word)\\1) goto \\4;" nil)
  148. (goto-char (point-min))
  149. (replace-regexp "cmpw    \\(%[a-z]*\\),\\(.*\\)
  150.     j\\([!=<>]*\\)    \\(.*$\\)" "if ((word)\\2 \\3 \\1) goto \\4;" nil)
  151.  
  152. (goto-char (point-min))
  153. (replace-regexp "cmpb    \\($.*\\),\\(.*\\)
  154.     j\\([!=<>]*\\)    \\(.*$\\)" "if ((byte)\\2 \\3 \\1) goto \\4;" nil)
  155. (goto-char (point-min))
  156. (replace-regexp "cmpb    \\(.*\\),\\(%[a-z]*\\)
  157.     j\\([!=<>]*\\)    \\(.*$\\)" "if (\\2 \\3 (byte)\\1) goto \\4;" nil)
  158. (goto-char (point-min))
  159. (replace-regexp "cmpb    \\(%[a-z]*\\),\\(.*\\)
  160.     j\\([!=<>]*\\)    \\(.*$\\)" "if ((byte)\\2 \\3 \\1) goto \\4;" nil)
  161.  
  162. (goto-char (point-min))
  163. (message "Convert bit test statements to if's")
  164. (replace-regexp "testl    $\\([0-9]+\\|0x[0-9a-f]+\\),\\(.*\\)
  165.     j!=    \\(.*\\)$" "if (\\2 & $\\1) goto \\3;" nil)
  166. (goto-char (point-min))
  167. (replace-regexp "testl    \\(%e..\\),\\(.*\\)
  168.     j!=    \\(.*\\)$" "if (\\1 & \\2) goto \\3;" nil)
  169. (goto-char (point-min))
  170. (replace-regexp "testl    \\(%e..\\),\\(.*\\)
  171.     j==    \\(.*\\)$" "if (!(\\1 & \\2)) goto \\3;" nil)
  172. (goto-char (point-min))
  173. (replace-regexp "testw    $\\([0-9]+\\|0x[0-9a-f]+\\),\\(.*\\)
  174.     j!=    \\(.*\\)$" "if ((word)\\2 & $\\1) goto \\3;" nil)
  175. (goto-char (point-min))
  176. (replace-regexp "testb    $\\([0-9]+\\|0x[0-9a-f]+\\),\\(.*\\)
  177.     j!=    \\(.*\\)$" "if ((byte)\\2 & $\\1) goto \\3;" nil)
  178.  
  179. (goto-char (point-min))
  180. (replace-regexp "testl    $\\([0-9]+\\|0x[0-9a-f]+\\),\\(.*\\)
  181.     j==    \\(.*\\)$" "if (!(\\2 & $\\1)) goto \\3;" nil)
  182. (goto-char (point-min))
  183. (replace-regexp "testw    $\\([0-9]+\\|0x[0-9a-f]+\\),\\(.*\\)
  184.     j==    \\(.*\\)$" "if (!((word)\\2 & $\\1)) goto \\3;" nil)
  185. (goto-char (point-min))
  186. (replace-regexp "testb    $\\([0-9]+\\|0x[0-9a-f]+\\),\\(.*\\)
  187.     j==    \\(.*\\)$" "if (!((byte)\\2 & $\\1)) goto \\3;" nil)
  188.  
  189. (goto-char (point-min))
  190. (message "Fix a bug in the disasm program")
  191. (replace-regexp "\\(orb    .*\\)
  192. Warning: Unreachable code" "\\1" nil)
  193.  
  194. (goto-char (point-min))
  195. (message "Convert or to statement")
  196. (replace-regexp "\<orl    \\(%[a-z]+\\),\\1
  197.     j\\([!=<>]*\\)    \\(.*$\\)" "if (\\1 \\2 0) goto \\3;" nil)
  198. (goto-char (point-min))
  199. (replace-regexp "\<orw    \\(%[a-z]+\\),\\1
  200.     j\\([!=<>]*\\)    \\(.*$\\)" "if ((word)\\1 \\2 0) goto \\3;" nil)
  201. (goto-char (point-min))
  202. (replace-regexp "\<orb    \\(%[a-z]+\\),\\1
  203.     j\\([!=<>]*\\)    \\(.*$\\)" "if ((word)\\1 \\2 0) goto \\3;" nil)
  204.  
  205.  
  206. (goto-char (point-min))
  207. (message "Convert jmp's to goto")
  208. (replace-string "    jmp    " "    goto " nil)
  209.  
  210. (goto-char (point-min))
  211. (message "Convert dec's and inc's")
  212. (replace-regexp "    decl    \\(.*\\)$" "    \\1--;" nil)
  213. (goto-char (point-min))
  214. (replace-regexp "    incl    \\(.*\\)$" "    \\1++;" nil)
  215.  
  216. (goto-char (point-min))
  217. (replace-regexp "    decw    \\(.*\\)$" "    (word)\\1--;" nil)
  218. (goto-char (point-min))
  219. (replace-regexp "    incw    \\(.*\\)$" "    (word)\\1++;" nil)
  220.  
  221. (goto-char (point-min))
  222. (message "Converting shl's and shr's")
  223. (replace-regexp "    shl[lwb]    $\\([0-9]+\\|0x[0-9a-f]+\\),\\(.*\\)$" "    \\2 <<= $\\1;" nil)
  224. (goto-char (point-min))
  225. (replace-regexp "    shl[lwb]    \\(%[a-z]+\\)$" "    \\1 <<= 1;" nil)
  226. (goto-char (point-min))
  227. (replace-regexp "    shl[lwb]    \\(%[a-z]+\\),\\(%[a-z]+\\)$" "    \\1 <<= \\2;" nil)
  228. (goto-char (point-min))
  229. (replace-regexp "    shr[lwb]    $\\([0-9]+\\|0x[0-9a-f]+\\),\\(.*\\)$" "    (unsigned)\\2 >>= $\\1;" nil)
  230. (goto-char (point-min))
  231. (replace-regexp "    sar[lwb]    $\\([0-9]+\\|0x[0-9a-f]+\\),\\(.*\\)$" "    (int)\\2 >>= $\\1;" nil)
  232.  
  233. (goto-char (point-min))
  234. (message "Subroutine entry")
  235. (replace-regexp "pushl    %ebp
  236.     %ebp = %esp;
  237.     subl    $\\(.*\\),%esp" "enter    \\1" nil)
  238.  
  239. (goto-char (point-min))
  240. (message "Converting and's, or's and xor's")
  241. (replace-regexp "andl    \\($[0-9]*\\|$0x[0-9a-f]*\\|%[a-z]+\\),\\(.*\\)$" "\\2 &= \\1;" nil)
  242. (goto-char (point-min))
  243. (replace-regexp "andw    \\($[0-9]*\\|$0x[0-9a-f]*\\|%[a-z]+\\),\\(.*\\)$" "(word)\\2 &= \\1;" nil)
  244. (goto-char (point-min))
  245. (replace-regexp "andb    \\($[0-9]*\\|$0x[0-9a-f]*\\|%[a-z]+\\),\\(.*\\)$" "(byte)\\2 &= \\1;" nil)
  246. (goto-char (point-min))
  247. (replace-regexp "xorl    \\($[0-9]*\\|$0x[0-9a-f]*\\|%[a-z]+\\),\\(.*\\)$" "\\2 ^= \\1;" nil)
  248. (goto-char (point-min))
  249. (replace-regexp "xorw    \\($[0-9]*\\|$0x[0-9a-f]*\\|%[a-z]+\\),\\(.*\\)$" "(word)\\2 ^= \\1;" nil)
  250. (goto-char (point-min))
  251. (replace-regexp "xorb    \\($[0-9]*\\|$0x[0-9a-f]*\\|%[a-z]+\\),\\(.*\\)$" "(byte)\\2 ^= \\1;" nil)
  252. (goto-char (point-min))
  253. (replace-regexp "orl    \\($[0-9]*\\|$0x[0-9a-f]*\\|%[a-z]+\\),\\(.*\\)$" "\\2 |= \\1;" nil)
  254. (goto-char (point-min))
  255. (replace-regexp "orw    \\($[0-9]*\\|$0x[0-9a-f]*\\|%[a-z]+\\),\\(.*\\)$" "(word)\\2 |= \\1;" nil)
  256. (goto-char (point-min))
  257. (replace-regexp "orb    \\($[0-9]*\\|$0x[0-9a-f]*\\|%[a-z]+\\),\\(.*\\)$" "(byte)\\2 |= \\1;" nil)
  258.  
  259. (goto-char (point-min))
  260. (message "Converting not's and neg's")
  261. (replace-regexp "    %eax = \\(.*\\);
  262.     notl    %eax$" "    %eax = ~( \\1 );" nil)
  263. (goto-char (point-min))
  264. (replace-regexp "    notl    \\(.*\\)$" "    \\1 = ~( \\1 );" nil)
  265. (goto-char (point-min))
  266. (replace-regexp "    %eax = \\(.*\\);
  267.     negl    %eax$" "    %eax = -( \\1 );" nil)
  268. (goto-char (point-min))
  269. (replace-regexp "    negl    \\(.*\\)$" "    \\1 = -( \\1 );" nil)
  270.     
  271.  
  272. (goto-char (point-min))
  273. (message "Make arg names")
  274. (replace-regexp "\\([^-_0-9a-z]\\)8(%ebp)" "\\1ARG_1" nil)
  275. (goto-char (point-min))
  276. (replace-regexp "\\([^-_0-9a-z]\\)12(%ebp)" "\\1ARG_2" nil)
  277. (goto-char (point-min))
  278. (replace-regexp "\\([^-_0-9a-z]\\)16(%ebp)" "\\1ARG_3" nil)
  279. (goto-char (point-min))
  280. (replace-regexp "\\([^-_0-9a-z]\\)20(%ebp)" "\\1ARG_4" nil)
  281. (goto-char (point-min))
  282. (replace-regexp "\\([^-_0-9a-z]\\)24(%ebp)" "\\1ARG_5" nil)
  283. (goto-char (point-min))
  284. (replace-regexp "\\([^-_0-9a-z]\\)28(%ebp)" "\\1ARG_6" nil)
  285. (goto-char (point-min))
  286. (replace-regexp "\\([^-_0-9a-z]\\)32(%ebp)" "\\1ARG_7" nil)
  287. (goto-char (point-min))
  288. (replace-regexp "\\([^-_0-9a-z]\\)36(%ebp)" "\\1ARG_8" nil)
  289. (goto-char (point-min))
  290. (replace-regexp "\\([^-_0-9a-z]\\)40(%ebp)" "\\1ARG_9" nil)
  291. (goto-char (point-min))
  292. (replace-regexp "-\\([0-9]+\\)(%ebp)" "L_\\1" nil)
  293.  
  294. (goto-char (point-min))
  295. (message "Remove leading '_' from names")
  296. (replace-string "\n_" "\n#\n_" nil)
  297. (goto-char (point-min))
  298. (replace-regexp "\\([^_0-9a-z]\\)_\\([0-9a-z_]+\\)" "\\1\\2" nil)
  299.  
  300. (goto-char (point-min))
  301. (message "pushl's")
  302. (replace-regexp "\\(%e..\\) = \\(.*\\);
  303.     pushl    \\1" "pushl    \\1 = \\2" nil)
  304.  
  305. (goto-char (point-min))
  306. (message "Return result from function calls")
  307. (replace-regexp "    \\([_a-z][_a-z0-9]*(.*)\\);
  308.     \\(.*\\) = %eax;$" "    \\2 = %eax = \\1" nil)
  309.  
  310. (goto-char (point-min))
  311. (message "Convert simple single-parameter procedure calls to c-sequence")
  312. (replace-regexp "pushl    \\(.*\\)
  313.     call    \\(.*\\)
  314.     addl    $4,%esp" "\\2(\\1);" nil)
  315.  
  316. (goto-char (point-min))
  317. (message "Convert simple double-parameter procedure calls to c-sequence")
  318. (replace-regexp "pushl    \\(.*\\)
  319.     pushl    \\(.*\\)
  320.     call    \\(.*\\)
  321.     addl    $8,%esp" "\\3(\\2,\\1);" nil)
  322.  
  323. (goto-char (point-min))
  324. (message "Convert simple triple-parameter procedure calls to c-sequence")
  325. (replace-regexp "pushl    \\(.*\\)
  326.     pushl    \\(.*\\)
  327.     pushl    \\(.*\\)
  328.     call    \\(.*\\)
  329.     addl    $0xc,%esp" "\\4(\\3,\\2,\\1);" nil)
  330.  
  331. (goto-char (point-min))
  332. (message "Convert simple quadruple-parameter procedure calls to c-sequence")
  333. (replace-regexp "pushl    \\(.*\\)
  334.     pushl    \\(.*\\)
  335.     pushl    \\(.*\\)
  336.     pushl    \\(.*\\)
  337.     call    \\(.*\\)
  338.     addl    $0x10,%esp" "\\5(\\4,\\3,\\2,\\1);" nil)
  339.  
  340. (goto-char (point-min))
  341. (message "Convert simple 5-parameter procedure calls to c-sequence")
  342. (replace-regexp "pushl    \\(.*\\)
  343.     pushl    \\(.*\\)
  344.     pushl    \\(.*\\)
  345.     pushl    \\(.*\\)
  346.     pushl    \\(.*\\)
  347.     call    \\(.*\\)
  348.     addl    $0x14,%esp" "\\6(\\5,\\4,\\3,\\2,\\1);" nil)
  349.  
  350. (goto-char (point-min))
  351. (message "Convert simple 6-parameter procedure calls to c-sequence")
  352. (replace-regexp "pushl    \\(.*\\)
  353.     pushl    \\(.*\\)
  354.     pushl    \\(.*\\)
  355.     pushl    \\(.*\\)
  356.     pushl    \\(.*\\)
  357.     pushl    \\(.*\\)
  358.     call    \\(.*\\)
  359.     addl    $0x18,%esp" "\\7(\\6,\\5,\\4,\\3,\\2,\\1);" nil)
  360.  
  361. (goto-char (point-min))
  362. (message "Zero-parameters procedure calls")
  363. (replace-regexp "    call    \\(.*\\)
  364.     \\([^ag]\\)" "    \\1();
  365.     \\2" nil)
  366.  
  367. (message "End of file")
  368. (goto-char (point-min))
  369. (replace-regexp "    \\(%[a-z]+\\) ^= \\1;" "    \\1 = 0;" nil)
  370.  
  371. ;; new code
  372. (replace-regexp "    %eax = \\(.*\\);
  373.     return" "    return \\1;" nil)
  374. )
  375.