asm.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. from __future__ import print_function
  2. import re
  3. import sys
  4. from . import common
  5. if sys.version_info[0] > 2:
  6. class string:
  7. expandtabs = str.expandtabs
  8. else:
  9. import string
  10. # RegEx: this is where the magic happens.
  11. ##### Assembly parser
  12. ASM_FUNCTION_X86_RE = re.compile(
  13. r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n(?:\s*\.?Lfunc_begin[^:\n]*:\n)?[^:]*?'
  14. r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*'
  15. r'^\s*(?:[^:\n]+?:\s*\n\s*\.size|\.cfi_endproc|\.globl|\.comm|\.(?:sub)?section|#+ -- End function)',
  16. flags=(re.M | re.S))
  17. ASM_FUNCTION_ARM_RE = re.compile(
  18. r'^(?P<func>[0-9a-zA-Z_]+):\n' # f: (name of function)
  19. r'\s+\.fnstart\n' # .fnstart
  20. r'(?P<body>.*?)\n' # (body of the function)
  21. r'.Lfunc_end[0-9]+:', # .Lfunc_end0: or # -- End function
  22. flags=(re.M | re.S))
  23. ASM_FUNCTION_AARCH64_RE = re.compile(
  24. r'^_?(?P<func>[^:]+):[ \t]*\/\/[ \t]*@(?P=func)\n'
  25. r'(?:[ \t]+.cfi_startproc\n)?' # drop optional cfi noise
  26. r'(?P<body>.*?)\n'
  27. # This list is incomplete
  28. r'.Lfunc_end[0-9]+:\n',
  29. flags=(re.M | re.S))
  30. ASM_FUNCTION_AMDGPU_RE = re.compile(
  31. r'^_?(?P<func>[^:]+):[ \t]*;+[ \t]*@(?P=func)\n[^:]*?'
  32. r'(?P<body>.*?)\n' # (body of the function)
  33. # This list is incomplete
  34. r'.Lfunc_end[0-9]+:\n',
  35. flags=(re.M | re.S))
  36. ASM_FUNCTION_MIPS_RE = re.compile(
  37. r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n[^:]*?' # f: (name of func)
  38. r'(?:^[ \t]+\.(frame|f?mask|set).*?\n)+' # Mips+LLVM standard asm prologue
  39. r'(?P<body>.*?)\n' # (body of the function)
  40. r'(?:^[ \t]+\.(set|end).*?\n)+' # Mips+LLVM standard asm epilogue
  41. r'(\$|\.L)func_end[0-9]+:\n', # $func_end0: (mips32 - O32) or
  42. # .Lfunc_end0: (mips64 - NewABI)
  43. flags=(re.M | re.S))
  44. ASM_FUNCTION_PPC_RE = re.compile(
  45. r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n'
  46. r'.*?'
  47. r'\.Lfunc_begin[0-9]+:\n'
  48. r'(?:[ \t]+.cfi_startproc\n)?'
  49. r'(?:\.Lfunc_[gl]ep[0-9]+:\n(?:[ \t]+.*?\n)*)*'
  50. r'(?P<body>.*?)\n'
  51. # This list is incomplete
  52. r'(?:^[ \t]*(?:\.long[ \t]+[^\n]+|\.quad[ \t]+[^\n]+)\n)*'
  53. r'.Lfunc_end[0-9]+:\n',
  54. flags=(re.M | re.S))
  55. ASM_FUNCTION_RISCV_RE = re.compile(
  56. r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n[^:]*?'
  57. r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*'
  58. r'.Lfunc_end[0-9]+:\n',
  59. flags=(re.M | re.S))
  60. ASM_FUNCTION_SPARC_RE = re.compile(
  61. r'^_?(?P<func>[^:]+):[ \t]*!+[ \t]*@(?P=func)\n'
  62. r'(?P<body>.*?)\s*'
  63. r'.Lfunc_end[0-9]+:\n',
  64. flags=(re.M | re.S))
  65. ASM_FUNCTION_SYSTEMZ_RE = re.compile(
  66. r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n'
  67. r'[ \t]+.cfi_startproc\n'
  68. r'(?P<body>.*?)\n'
  69. r'.Lfunc_end[0-9]+:\n',
  70. flags=(re.M | re.S))
  71. ASM_FUNCTION_WASM32_RE = re.compile(
  72. r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n'
  73. r'(?P<body>.*?)\n'
  74. r'.Lfunc_end[0-9]+:\n',
  75. flags=(re.M | re.S))
  76. SCRUB_LOOP_COMMENT_RE = re.compile(
  77. r'# =>This Inner Loop Header:.*|# in Loop:.*', flags=re.M)
  78. SCRUB_X86_SHUFFLES_RE = (
  79. re.compile(
  80. r'^(\s*\w+) [^#\n]+#+ ((?:[xyz]mm\d+|mem)( \{%k\d+\}( \{z\})?)? = .*)$',
  81. flags=re.M))
  82. SCRUB_X86_SPILL_RELOAD_RE = (
  83. re.compile(
  84. r'-?\d+\(%([er])[sb]p\)(.*(?:Spill|Reload))$',
  85. flags=re.M))
  86. SCRUB_X86_SP_RE = re.compile(r'\d+\(%(esp|rsp)\)')
  87. SCRUB_X86_RIP_RE = re.compile(r'[.\w]+\(%rip\)')
  88. SCRUB_X86_LCP_RE = re.compile(r'\.LCPI[0-9]+_[0-9]+')
  89. SCRUB_X86_RET_RE = re.compile(r'ret[l|q]')
  90. def scrub_asm_x86(asm, args):
  91. # Scrub runs of whitespace out of the assembly, but leave the leading
  92. # whitespace in place.
  93. asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
  94. # Expand the tabs used for indentation.
  95. asm = string.expandtabs(asm, 2)
  96. # Detect shuffle asm comments and hide the operands in favor of the comments.
  97. asm = SCRUB_X86_SHUFFLES_RE.sub(r'\1 {{.*#+}} \2', asm)
  98. # Detect stack spills and reloads and hide their exact offset and whether
  99. # they used the stack pointer or frame pointer.
  100. asm = SCRUB_X86_SPILL_RELOAD_RE.sub(r'{{[-0-9]+}}(%\1{{[sb]}}p)\2', asm)
  101. # Generically match the stack offset of a memory operand.
  102. asm = SCRUB_X86_SP_RE.sub(r'{{[0-9]+}}(%\1)', asm)
  103. if getattr(args, 'x86_scrub_rip', False):
  104. # Generically match a RIP-relative memory operand.
  105. asm = SCRUB_X86_RIP_RE.sub(r'{{.*}}(%rip)', asm)
  106. # Generically match a LCP symbol.
  107. asm = SCRUB_X86_LCP_RE.sub(r'{{\.LCPI.*}}', asm)
  108. if getattr(args, 'extra_scrub', False):
  109. # Avoid generating different checks for 32- and 64-bit because of 'retl' vs 'retq'.
  110. asm = SCRUB_X86_RET_RE.sub(r'ret{{[l|q]}}', asm)
  111. # Strip kill operands inserted into the asm.
  112. asm = common.SCRUB_KILL_COMMENT_RE.sub('', asm)
  113. # Strip trailing whitespace.
  114. asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
  115. return asm
  116. def scrub_asm_amdgpu(asm, args):
  117. # Scrub runs of whitespace out of the assembly, but leave the leading
  118. # whitespace in place.
  119. asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
  120. # Expand the tabs used for indentation.
  121. asm = string.expandtabs(asm, 2)
  122. # Strip trailing whitespace.
  123. asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
  124. return asm
  125. def scrub_asm_arm_eabi(asm, args):
  126. # Scrub runs of whitespace out of the assembly, but leave the leading
  127. # whitespace in place.
  128. asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
  129. # Expand the tabs used for indentation.
  130. asm = string.expandtabs(asm, 2)
  131. # Strip kill operands inserted into the asm.
  132. asm = common.SCRUB_KILL_COMMENT_RE.sub('', asm)
  133. # Strip trailing whitespace.
  134. asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
  135. return asm
  136. def scrub_asm_powerpc(asm, args):
  137. # Scrub runs of whitespace out of the assembly, but leave the leading
  138. # whitespace in place.
  139. asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
  140. # Expand the tabs used for indentation.
  141. asm = string.expandtabs(asm, 2)
  142. # Stripe unimportant comments
  143. asm = SCRUB_LOOP_COMMENT_RE.sub(r'', asm)
  144. # Strip trailing whitespace.
  145. asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
  146. return asm
  147. def scrub_asm_mips(asm, args):
  148. # Scrub runs of whitespace out of the assembly, but leave the leading
  149. # whitespace in place.
  150. asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
  151. # Expand the tabs used for indentation.
  152. asm = string.expandtabs(asm, 2)
  153. # Strip trailing whitespace.
  154. asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
  155. return asm
  156. def scrub_asm_riscv(asm, args):
  157. # Scrub runs of whitespace out of the assembly, but leave the leading
  158. # whitespace in place.
  159. asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
  160. # Expand the tabs used for indentation.
  161. asm = string.expandtabs(asm, 2)
  162. # Strip trailing whitespace.
  163. asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
  164. return asm
  165. def scrub_asm_sparc(asm, args):
  166. # Scrub runs of whitespace out of the assembly, but leave the leading
  167. # whitespace in place.
  168. asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
  169. # Expand the tabs used for indentation.
  170. asm = string.expandtabs(asm, 2)
  171. # Strip trailing whitespace.
  172. asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
  173. return asm
  174. def scrub_asm_systemz(asm, args):
  175. # Scrub runs of whitespace out of the assembly, but leave the leading
  176. # whitespace in place.
  177. asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
  178. # Expand the tabs used for indentation.
  179. asm = string.expandtabs(asm, 2)
  180. # Strip trailing whitespace.
  181. asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
  182. return asm
  183. def scrub_asm_wasm32(asm, args):
  184. # Scrub runs of whitespace out of the assembly, but leave the leading
  185. # whitespace in place.
  186. asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
  187. # Expand the tabs used for indentation.
  188. asm = string.expandtabs(asm, 2)
  189. # Strip trailing whitespace.
  190. asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
  191. return asm
  192. def get_triple_from_march(march):
  193. triples = {
  194. 'amdgcn': 'amdgcn',
  195. 'mips': 'mips',
  196. 'sparc': 'sparc',
  197. }
  198. for prefix, triple in triples.items():
  199. if march.startswith(prefix):
  200. return triple
  201. print("Cannot find a triple. Assume 'x86'", file=sys.stderr)
  202. return 'x86'
  203. def build_function_body_dictionary_for_triple(args, raw_tool_output, triple, prefixes, func_dict):
  204. target_handlers = {
  205. 'x86_64': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
  206. 'i686': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
  207. 'x86': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
  208. 'i386': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
  209. 'arm64-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_RE),
  210. 'aarch64': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_RE),
  211. 'r600': (scrub_asm_amdgpu, ASM_FUNCTION_AMDGPU_RE),
  212. 'amdgcn': (scrub_asm_amdgpu, ASM_FUNCTION_AMDGPU_RE),
  213. 'arm-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  214. 'thumb-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  215. 'thumbv6': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  216. 'thumbv6-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  217. 'thumbv6t2': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  218. 'thumbv6t2-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  219. 'thumbv6m': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  220. 'thumbv6m-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  221. 'thumbv7': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  222. 'thumbv7-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  223. 'thumbv7m': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  224. 'thumbv7m-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  225. 'thumbv8-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  226. 'thumbv8m.base': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  227. 'thumbv8m.main': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  228. 'armv6': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  229. 'armv7': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  230. 'armv7-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  231. 'armeb-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  232. 'armv7eb-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  233. 'armv7eb': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  234. 'armv8a': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  235. 'mips': (scrub_asm_mips, ASM_FUNCTION_MIPS_RE),
  236. 'ppc32': (scrub_asm_powerpc, ASM_FUNCTION_PPC_RE),
  237. 'powerpc64': (scrub_asm_powerpc, ASM_FUNCTION_PPC_RE),
  238. 'powerpc64le': (scrub_asm_powerpc, ASM_FUNCTION_PPC_RE),
  239. 'riscv32': (scrub_asm_riscv, ASM_FUNCTION_RISCV_RE),
  240. 'riscv64': (scrub_asm_riscv, ASM_FUNCTION_RISCV_RE),
  241. 'sparc': (scrub_asm_sparc, ASM_FUNCTION_SPARC_RE),
  242. 'sparcv9': (scrub_asm_sparc, ASM_FUNCTION_SPARC_RE),
  243. 's390x': (scrub_asm_systemz, ASM_FUNCTION_SYSTEMZ_RE),
  244. 'wasm32': (scrub_asm_wasm32, ASM_FUNCTION_WASM32_RE),
  245. }
  246. handlers = None
  247. for prefix, s in target_handlers.items():
  248. if triple.startswith(prefix):
  249. handlers = s
  250. break
  251. else:
  252. raise KeyError('Triple %r is not supported' % (triple))
  253. scrubber, function_re = handlers
  254. common.build_function_body_dictionary(
  255. function_re, scrubber, [args], raw_tool_output, prefixes,
  256. func_dict, args.verbose)
  257. ##### Generator of assembly CHECK lines
  258. def add_asm_checks(output_lines, comment_marker, prefix_list, func_dict, func_name):
  259. # Label format is based on ASM string.
  260. check_label_format = '{} %s-LABEL: %s:'.format(comment_marker)
  261. common.add_checks(output_lines, comment_marker, prefix_list, func_dict, func_name, check_label_format, True, False)