asmi386.sh
上传用户:shenzhenrh
上传日期:2013-05-12
资源大小:2904k
文件大小:4k
源码类别:

信息检索与抽取

开发平台:

Unix_Linux

  1. #!/bin/sh
  2. # Translate the assembler syntax of i386 assembler programs
  3. # Usage: asmsyntax [-no-C] < gas-asm-file > all-asm-file
  4. # Warning! All comments are stripped.
  5. tmpscript01=sed$$tmp01
  6. tmpscript02=sed$$tmp02
  7. tmpscript03=sed$$tmp03
  8. tmpscript04=sed$$tmp04
  9. tmpscript05=sed$$tmp05
  10. tmpscript06=sed$$tmp06
  11. tmpscript07=sed$$tmp07
  12. tmpscript08=sed$$tmp08
  13. tmpscript09=sed$$tmp09
  14. tmpscript10=sed$$tmp10
  15. tmpscript11=sed$$tmp11
  16. tmpremove='rm -f $tmpscript01 $tmpscript02 $tmpscript03 $tmpscript04 $tmpscript05 $tmpscript06 $tmpscript07 $tmpscript08 $tmpscript09 $tmpscript10 $tmpscript11'
  17. trap "$tmpremove" 1 2 15
  18. cat > $tmpscript01 << EOF
  19. # ----------- Strip comments
  20. s,# .*,,
  21. s,[  ][  ]*$,,
  22. EOF
  23. cat > $tmpscript02 << EOF
  24. # ----------- Remove #APP/#NO_APP lines, add a blank line at the end
  25. /^#APP$/d
  26. /^#NO_APP$/d
  27. /gcc2_compiled/d
  28. /gnu_compiled_c/d
  29. EOF
  30. cat > $tmpscript03 << EOF
  31. # ----------- Global symbols depends on ASM_UNDERSCORE
  32. s/_([A-Za-z0-9_:]*)/C(1)/
  33. EOF
  34. cat > $tmpscript04 << EOF
  35. # ----------- Introduce macro syntax for operands
  36. s/([-+0-9A-Z_]+)[(]%(e..)[)]/MEM_DISP(2,1)/g
  37. s/[(]%(e..)[)]/MEM(1)/g
  38. s/([-+0-9A-Z_]+)[(],%(e..),([0-9]*)[)]/MEM_DISP_SHINDEX0(1,2,3)/g
  39. s/([-+0-9A-Z_]+)[(]%(e..),%(e..),([0-9]*)[)]/MEM_DISP_SHINDEX(2,1,3,4)/g
  40. s/[(]%(e..),%(e..),([0-9]*)[)]/MEM_SHINDEX(1,2,3)/g
  41. s/[(]%(e..),%(e..)[)]/MEM_INDEX(1,2)/g
  42. EOF
  43. cat > $tmpscript05 << EOF
  44. # ----------- Introduce macro syntax for instructions
  45. s/(push|pop|mul|div|not|neg|inc|dec|fld|fstp)(.)( +)(.*)$/INSN1(1,2 ,4)/
  46. s/(call|jmp|jc|jnc|je|jne|jz|jnz|ja|jae|jb|jbe|jl|jge|js|jns)( +)(.*)$/INSN1(1,_ ,3)/
  47. s/(movs|movz)(.)l( +)(.*)$/INSN2MOVX(1,2 ,4)/
  48. s/(mov|add|sub|adc|sbb|xor|test|cmp|rcl|rcr|and|or|sar|shr|shl|lea)(.)( +)(.*)$/INSN2(1,2 ,4)/
  49. s/(shld|shrd)(.)( +)shcl( +)(.*)$/INSN2SHCL(1,2 ,5)/
  50. s/rep ;/REP/
  51. s/repz ;/REPZ/
  52. EOF
  53. cat > $tmpscript06 << EOF
  54. # ----------- Add size prefixes to memory references' 
  55. s/([(]f[^(,]*,s.*),MEM/1,X4 MEM/g
  56. s/([(]f[^(,]*,l.*),MEM/1,X8 MEM/g
  57. s/([(][^(,]*,b.*),MEM/1,X1 MEM/g
  58. s/([(][^(,]*,w.*),MEM/1,X2 MEM/g
  59. s/([(][^(,]*,l.*),MEM/1,X4 MEM/g
  60. EOF
  61. cat > $tmpscript07 << EOF
  62. # ----------- Introduce macro syntax for register names
  63. s/%(e..)/R(1)/g
  64. s/%(..)/R(1)/g
  65. s/$([-0-9]*)/NUM(1)/g
  66. EOF
  67. cat > $tmpscript08 << EOF
  68. # ----------- Treat table jumps (hairy)
  69. # (Needed because the MSVC inline assembler does not have pseudo-ops.
  70. # Note that we transform a table of absolute addresses with 4 bytes
  71. # per entry into a table of absolute addresses with 8 bytes per entry.)
  72. s/^ .long (.*)$/#ifdef _MSC_VER
  73. nop
  74. nop
  75. push 1
  76. #else
  77. .long 1
  78. #endif/
  79. s/^ (INSN1[(]jmp,_[^,]*,)*MEM_DISP_SHINDEX0[(]([^,)]*),([^,)]*),4[)][)]$/#ifdef _MSC_VER
  80. INSN2(lea,l ,MEM_DISP_SHINDEX0(2+8,3,8),R(3))
  81. INSN2(mov,l ,X4 MEM_DISP(3,-4),R(3))
  82. INSN1(jmp,_ ,INDIR(R(3)))
  83. #else
  84. 1INDIR(MEM_DISP_SHINDEX0(2,3,4)))
  85. #endif/
  86. EOF
  87. cat > $tmpscript09 << EOF
  88. # ----------- Treat indirect calls
  89. s/(INSN1[(](call|jmp),_[^,]*,)*(R[(][^)]*[)])[)]$/1INDIR(3))/
  90. EOF
  91. cat > $tmpscript10 << EOF
  92. # ----------- Introduce macro syntax for assembler pseudo-ops
  93. /.file([  ]+)/d
  94. s/.text/TEXT()/
  95. s/^([^#]*).align (.*)/1ALIGN(2)/
  96. s/.globl( +)(.*)$/GLOBL(2)/
  97. s/^C(([A-Za-z0-9_]*):)/FUNBEGIN(1)/
  98. # The next 5 lines add FUNEND() after each ret followed by an empty line
  99. /[  ]ret *$/{
  100. n
  101. /^$/s/^$/FUNEND()
  102. /
  103. }
  104. EOF
  105. cat > $tmpscript11 << EOF
  106. # ----------- Declare global symbols as functions (we have no variables)
  107. s/GLOBL(C(([A-Za-z0-9_]*)))$/GLOBL(C(1))
  108. DECLARE_FUNCTION(1)/
  109. EOF
  110. sed -f $tmpscript01 | 
  111. sed -f $tmpscript02 | 
  112. (cat - ; echo) | 
  113. (if [ $# = 1 -a "x$1" = "x-no-C" ] ; then cat - ; else sed -f $tmpscript03 ; fi) | 
  114. sed -f $tmpscript04 | 
  115. sed -f $tmpscript05 | 
  116. sed -f $tmpscript06 | 
  117. sed -f $tmpscript07 | 
  118. sed -f $tmpscript08 | 
  119. sed -f $tmpscript09 | 
  120. sed -f $tmpscript10 | 
  121. (if [ $# = 1 -a "x$1" = "x-no-C" ] ; then cat - ; else sed -f $tmpscript11 ; fi)
  122. eval "$tmpremove"