asm.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import re
  2. import string
  3. from . import common
  4. # RegEx: this is where the magic happens.
  5. ASM_FUNCTION_X86_RE = re.compile(
  6. r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n[^:]*?'
  7. r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*'
  8. r'^\s*(?:[^:\n]+?:\s*\n\s*\.size|\.cfi_endproc|\.globl|\.comm|\.(?:sub)?section|#+ -- End function)',
  9. flags=(re.M | re.S))
  10. ASM_FUNCTION_ARM_RE = re.compile(
  11. r'^(?P<func>[0-9a-zA-Z_]+):\n' # f: (name of function)
  12. r'\s+\.fnstart\n' # .fnstart
  13. r'(?P<body>.*?)\n' # (body of the function)
  14. r'.Lfunc_end[0-9]+:', # .Lfunc_end0: or # -- End function
  15. flags=(re.M | re.S))
  16. ASM_FUNCTION_AARCH64_RE = re.compile(
  17. r'^_?(?P<func>[^:]+):[ \t]*\/\/[ \t]*@(?P=func)\n'
  18. r'[ \t]+.cfi_startproc\n'
  19. r'(?P<body>.*?)\n'
  20. # This list is incomplete
  21. r'.Lfunc_end[0-9]+:\n',
  22. flags=(re.M | re.S))
  23. ASM_FUNCTION_MIPS_RE = re.compile(
  24. r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n[^:]*?' # f: (name of func)
  25. r'(?:^[ \t]+\.(frame|f?mask|set).*?\n)+' # Mips+LLVM standard asm prologue
  26. r'(?P<body>.*?)\n' # (body of the function)
  27. r'(?:^[ \t]+\.(set|end).*?\n)+' # Mips+LLVM standard asm epilogue
  28. r'(\$|\.L)func_end[0-9]+:\n', # $func_end0: (mips32 - O32) or
  29. # .Lfunc_end0: (mips64 - NewABI)
  30. flags=(re.M | re.S))
  31. ASM_FUNCTION_PPC_RE = re.compile(
  32. r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n'
  33. r'\.Lfunc_begin[0-9]+:\n'
  34. r'(?:[ \t]+.cfi_startproc\n)?'
  35. r'(?:\.Lfunc_[gl]ep[0-9]+:\n(?:[ \t]+.*?\n)*)*'
  36. r'(?P<body>.*?)\n'
  37. # This list is incomplete
  38. r'(?:^[ \t]*(?:\.long[ \t]+[^\n]+|\.quad[ \t]+[^\n]+)\n)*'
  39. r'.Lfunc_end[0-9]+:\n',
  40. flags=(re.M | re.S))
  41. ASM_FUNCTION_RISCV_RE = re.compile(
  42. r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n[^:]*?'
  43. r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*'
  44. r'.Lfunc_end[0-9]+:\n',
  45. flags=(re.M | re.S))
  46. ASM_FUNCTION_SYSTEMZ_RE = re.compile(
  47. r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n'
  48. r'[ \t]+.cfi_startproc\n'
  49. r'(?P<body>.*?)\n'
  50. r'.Lfunc_end[0-9]+:\n',
  51. flags=(re.M | re.S))
  52. SCRUB_LOOP_COMMENT_RE = re.compile(
  53. r'# =>This Inner Loop Header:.*|# in Loop:.*', flags=re.M)
  54. SCRUB_X86_SHUFFLES_RE = (
  55. re.compile(
  56. r'^(\s*\w+) [^#\n]+#+ ((?:[xyz]mm\d+|mem)( \{%k\d+\}( \{z\})?)? = .*)$',
  57. flags=re.M))
  58. SCRUB_X86_SP_RE = re.compile(r'\d+\(%(esp|rsp)\)')
  59. SCRUB_X86_RIP_RE = re.compile(r'[.\w]+\(%rip\)')
  60. SCRUB_X86_LCP_RE = re.compile(r'\.LCPI[0-9]+_[0-9]+')
  61. SCRUB_X86_RET_RE = re.compile(r'ret[l|q]')
  62. def scrub_asm_x86(asm, args):
  63. # Scrub runs of whitespace out of the assembly, but leave the leading
  64. # whitespace in place.
  65. asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
  66. # Expand the tabs used for indentation.
  67. asm = string.expandtabs(asm, 2)
  68. # Detect shuffle asm comments and hide the operands in favor of the comments.
  69. asm = SCRUB_X86_SHUFFLES_RE.sub(r'\1 {{.*#+}} \2', asm)
  70. # Generically match the stack offset of a memory operand.
  71. asm = SCRUB_X86_SP_RE.sub(r'{{[0-9]+}}(%\1)', asm)
  72. # Generically match a RIP-relative memory operand.
  73. asm = SCRUB_X86_RIP_RE.sub(r'{{.*}}(%rip)', asm)
  74. # Generically match a LCP symbol.
  75. asm = SCRUB_X86_LCP_RE.sub(r'{{\.LCPI.*}}', asm)
  76. if args.x86_extra_scrub:
  77. # Avoid generating different checks for 32- and 64-bit because of 'retl' vs 'retq'.
  78. asm = SCRUB_X86_RET_RE.sub(r'ret{{[l|q]}}', asm)
  79. # Strip kill operands inserted into the asm.
  80. asm = common.SCRUB_KILL_COMMENT_RE.sub('', asm)
  81. # Strip trailing whitespace.
  82. asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
  83. return asm
  84. def scrub_asm_arm_eabi(asm, args):
  85. # Scrub runs of whitespace out of the assembly, but leave the leading
  86. # whitespace in place.
  87. asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
  88. # Expand the tabs used for indentation.
  89. asm = string.expandtabs(asm, 2)
  90. # Strip kill operands inserted into the asm.
  91. asm = common.SCRUB_KILL_COMMENT_RE.sub('', asm)
  92. # Strip trailing whitespace.
  93. asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
  94. return asm
  95. def scrub_asm_powerpc64(asm, args):
  96. # Scrub runs of whitespace out of the assembly, but leave the leading
  97. # whitespace in place.
  98. asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
  99. # Expand the tabs used for indentation.
  100. asm = string.expandtabs(asm, 2)
  101. # Stripe unimportant comments
  102. asm = SCRUB_LOOP_COMMENT_RE.sub(r'', asm)
  103. # Strip trailing whitespace.
  104. asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
  105. return asm
  106. def scrub_asm_mips(asm, args):
  107. # Scrub runs of whitespace out of the assembly, but leave the leading
  108. # whitespace in place.
  109. asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
  110. # Expand the tabs used for indentation.
  111. asm = string.expandtabs(asm, 2)
  112. # Strip trailing whitespace.
  113. asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
  114. return asm
  115. def scrub_asm_riscv(asm, args):
  116. # Scrub runs of whitespace out of the assembly, but leave the leading
  117. # whitespace in place.
  118. asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
  119. # Expand the tabs used for indentation.
  120. asm = string.expandtabs(asm, 2)
  121. # Strip trailing whitespace.
  122. asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
  123. return asm
  124. def scrub_asm_systemz(asm, args):
  125. # Scrub runs of whitespace out of the assembly, but leave the leading
  126. # whitespace in place.
  127. asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
  128. # Expand the tabs used for indentation.
  129. asm = string.expandtabs(asm, 2)
  130. # Strip trailing whitespace.
  131. asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
  132. return asm
  133. def build_function_body_dictionary_for_triple(args, raw_tool_output, triple, prefixes, func_dict):
  134. target_handlers = {
  135. 'x86_64': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
  136. 'i686': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
  137. 'x86': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
  138. 'i386': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
  139. 'aarch64': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_RE),
  140. 'arm-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  141. 'thumb-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  142. 'thumbv6': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  143. 'thumbv6-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  144. 'thumbv6t2': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  145. 'thumbv6t2-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  146. 'thumbv6m': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  147. 'thumbv6m-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  148. 'thumbv7': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  149. 'thumbv7-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  150. 'thumbv7m': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  151. 'thumbv7m-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  152. 'thumbv8-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  153. 'thumbv8m.base': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  154. 'thumbv8m.main': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  155. 'armv6': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  156. 'armv7': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  157. 'armv7-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  158. 'armeb-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  159. 'armv7eb-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  160. 'armv7eb': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  161. 'mips': (scrub_asm_mips, ASM_FUNCTION_MIPS_RE),
  162. 'powerpc64': (scrub_asm_powerpc64, ASM_FUNCTION_PPC_RE),
  163. 'powerpc64le': (scrub_asm_powerpc64, ASM_FUNCTION_PPC_RE),
  164. 'riscv32': (scrub_asm_riscv, ASM_FUNCTION_RISCV_RE),
  165. 'riscv64': (scrub_asm_riscv, ASM_FUNCTION_RISCV_RE),
  166. 's390x': (scrub_asm_systemz, ASM_FUNCTION_SYSTEMZ_RE),
  167. }
  168. handlers = None
  169. for prefix, s in target_handlers.items():
  170. if triple.startswith(prefix):
  171. handlers = s
  172. break
  173. else:
  174. raise KeyError('Triple %r is not supported' % (triple))
  175. scrubber, function_re = handlers
  176. common.build_function_body_dictionary(
  177. function_re, scrubber, [args], raw_tool_output, prefixes,
  178. func_dict, args.verbose)