asm.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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'^\s*(\.Lfunc_end[0-9]+:\n|\.section)',
  35. flags=(re.M | re.S))
  36. ASM_FUNCTION_HEXAGON_RE = re.compile(
  37. r'^_?(?P<func>[^:]+):[ \t]*//[ \t]*@(?P=func)\n[^:]*?'
  38. r'(?P<body>.*?)\n' # (body of the function)
  39. # This list is incomplete
  40. r'.Lfunc_end[0-9]+:\n',
  41. flags=(re.M | re.S))
  42. ASM_FUNCTION_MIPS_RE = re.compile(
  43. r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n[^:]*?' # f: (name of func)
  44. r'(?:^[ \t]+\.(frame|f?mask|set).*?\n)+' # Mips+LLVM standard asm prologue
  45. r'(?P<body>.*?)\n' # (body of the function)
  46. r'(?:^[ \t]+\.(set|end).*?\n)+' # Mips+LLVM standard asm epilogue
  47. r'(\$|\.L)func_end[0-9]+:\n', # $func_end0: (mips32 - O32) or
  48. # .Lfunc_end0: (mips64 - NewABI)
  49. flags=(re.M | re.S))
  50. ASM_FUNCTION_PPC_RE = re.compile(
  51. r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n'
  52. r'.*?'
  53. r'\.Lfunc_begin[0-9]+:\n'
  54. r'(?:[ \t]+.cfi_startproc\n)?'
  55. r'(?:\.Lfunc_[gl]ep[0-9]+:\n(?:[ \t]+.*?\n)*)*'
  56. r'(?P<body>.*?)\n'
  57. # This list is incomplete
  58. r'(?:^[ \t]*(?:\.long[ \t]+[^\n]+|\.quad[ \t]+[^\n]+)\n)*'
  59. r'.Lfunc_end[0-9]+:\n',
  60. flags=(re.M | re.S))
  61. ASM_FUNCTION_RISCV_RE = re.compile(
  62. r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n(?:\s*\.?Lfunc_begin[^:\n]*:\n)?[^:]*?'
  63. r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*'
  64. r'.Lfunc_end[0-9]+:\n',
  65. flags=(re.M | re.S))
  66. ASM_FUNCTION_LANAI_RE = re.compile(
  67. r'^_?(?P<func>[^:]+):[ \t]*!+[ \t]*@(?P=func)\n'
  68. r'(?:[ \t]+.cfi_startproc\n)?' # drop optional cfi noise
  69. r'(?P<body>.*?)\s*'
  70. r'.Lfunc_end[0-9]+:\n',
  71. flags=(re.M | re.S))
  72. ASM_FUNCTION_SPARC_RE = re.compile(
  73. r'^_?(?P<func>[^:]+):[ \t]*!+[ \t]*@(?P=func)\n'
  74. r'(?P<body>.*?)\s*'
  75. r'.Lfunc_end[0-9]+:\n',
  76. flags=(re.M | re.S))
  77. ASM_FUNCTION_SYSTEMZ_RE = re.compile(
  78. r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n'
  79. r'[ \t]+.cfi_startproc\n'
  80. r'(?P<body>.*?)\n'
  81. r'.Lfunc_end[0-9]+:\n',
  82. flags=(re.M | re.S))
  83. ASM_FUNCTION_AARCH64_DARWIN_RE = re.compile(
  84. r'^_(?P<func>[^:]+):[ \t]*;[ \t]@(?P=func)\n'
  85. r'([ \t]*.cfi_startproc\n[\s]*)?'
  86. r'(?P<body>.*?)'
  87. r'([ \t]*.cfi_endproc\n[\s]*)?'
  88. r'^[ \t]*;[ \t]--[ \t]End[ \t]function',
  89. flags=(re.M | re.S))
  90. ASM_FUNCTION_ARM_DARWIN_RE = re.compile(
  91. r'^[ \t]*\.globl[ \t]*_(?P<func>[^ \t])[ \t]*@[ \t]--[ \t]Begin[ \t]function[ \t](?P=func)'
  92. r'(?P<directives>.*?)'
  93. r'^_(?P=func):\n[ \t]*'
  94. r'(?P<body>.*?)'
  95. r'^[ \t]*@[ \t]--[ \t]End[ \t]function',
  96. flags=(re.M | re.S ))
  97. ASM_FUNCTION_ARM_MACHO_RE = re.compile(
  98. r'^_(?P<func>[^:]+):[ \t]*\n'
  99. r'([ \t]*.cfi_startproc\n[ \t]*)?'
  100. r'(?P<body>.*?)\n'
  101. r'[ \t]*\.cfi_endproc\n',
  102. flags=(re.M | re.S))
  103. ASM_FUNCTION_ARM_IOS_RE = re.compile(
  104. r'^_(?P<func>[^:]+):[ \t]*\n'
  105. r'^Lfunc_begin(?P<id>[0-9][1-9]*):\n'
  106. r'(?P<body>.*?)'
  107. r'^Lfunc_end(?P=id):\n'
  108. r'^[ \t]*@[ \t]--[ \t]End[ \t]function',
  109. flags=(re.M | re.S))
  110. ASM_FUNCTION_WASM32_RE = re.compile(
  111. r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n'
  112. r'(?P<body>.*?)\n'
  113. r'^\s*(\.Lfunc_end[0-9]+:\n|end_function)',
  114. flags=(re.M | re.S))
  115. SCRUB_LOOP_COMMENT_RE = re.compile(
  116. r'# =>This Inner Loop Header:.*|# in Loop:.*', flags=re.M)
  117. SCRUB_X86_SHUFFLES_RE = (
  118. re.compile(
  119. r'^(\s*\w+) [^#\n]+#+ ((?:[xyz]mm\d+|mem)( \{%k\d+\}( \{z\})?)? = .*)$',
  120. flags=re.M))
  121. SCRUB_X86_SPILL_RELOAD_RE = (
  122. re.compile(
  123. r'-?\d+\(%([er])[sb]p\)(.*(?:Spill|Reload))$',
  124. flags=re.M))
  125. SCRUB_X86_SP_RE = re.compile(r'\d+\(%(esp|rsp)\)')
  126. SCRUB_X86_RIP_RE = re.compile(r'[.\w]+\(%rip\)')
  127. SCRUB_X86_LCP_RE = re.compile(r'\.LCPI[0-9]+_[0-9]+')
  128. SCRUB_X86_RET_RE = re.compile(r'ret[l|q]')
  129. def scrub_asm_x86(asm, args):
  130. # Scrub runs of whitespace out of the assembly, but leave the leading
  131. # whitespace in place.
  132. asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
  133. # Expand the tabs used for indentation.
  134. asm = string.expandtabs(asm, 2)
  135. # Detect shuffle asm comments and hide the operands in favor of the comments.
  136. asm = SCRUB_X86_SHUFFLES_RE.sub(r'\1 {{.*#+}} \2', asm)
  137. # Detect stack spills and reloads and hide their exact offset and whether
  138. # they used the stack pointer or frame pointer.
  139. asm = SCRUB_X86_SPILL_RELOAD_RE.sub(r'{{[-0-9]+}}(%\1{{[sb]}}p)\2', asm)
  140. # Generically match the stack offset of a memory operand.
  141. asm = SCRUB_X86_SP_RE.sub(r'{{[0-9]+}}(%\1)', asm)
  142. if getattr(args, 'x86_scrub_rip', False):
  143. # Generically match a RIP-relative memory operand.
  144. asm = SCRUB_X86_RIP_RE.sub(r'{{.*}}(%rip)', asm)
  145. # Generically match a LCP symbol.
  146. asm = SCRUB_X86_LCP_RE.sub(r'{{\.LCPI.*}}', asm)
  147. if getattr(args, 'extra_scrub', False):
  148. # Avoid generating different checks for 32- and 64-bit because of 'retl' vs 'retq'.
  149. asm = SCRUB_X86_RET_RE.sub(r'ret{{[l|q]}}', asm)
  150. # Strip kill operands inserted into the asm.
  151. asm = common.SCRUB_KILL_COMMENT_RE.sub('', asm)
  152. # Strip trailing whitespace.
  153. asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
  154. return asm
  155. def scrub_asm_amdgpu(asm, args):
  156. # Scrub runs of whitespace out of the assembly, but leave the leading
  157. # whitespace in place.
  158. asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
  159. # Expand the tabs used for indentation.
  160. asm = string.expandtabs(asm, 2)
  161. # Strip trailing whitespace.
  162. asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
  163. return asm
  164. def scrub_asm_arm_eabi(asm, args):
  165. # Scrub runs of whitespace out of the assembly, but leave the leading
  166. # whitespace in place.
  167. asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
  168. # Expand the tabs used for indentation.
  169. asm = string.expandtabs(asm, 2)
  170. # Strip kill operands inserted into the asm.
  171. asm = common.SCRUB_KILL_COMMENT_RE.sub('', asm)
  172. # Strip trailing whitespace.
  173. asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
  174. return asm
  175. def scrub_asm_hexagon(asm, args):
  176. # Scrub runs of whitespace out of the assembly, but leave the leading
  177. # whitespace in place.
  178. asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
  179. # Expand the tabs used for indentation.
  180. asm = string.expandtabs(asm, 2)
  181. # Strip trailing whitespace.
  182. asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
  183. return asm
  184. def scrub_asm_powerpc(asm, args):
  185. # Scrub runs of whitespace out of the assembly, but leave the leading
  186. # whitespace in place.
  187. asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
  188. # Expand the tabs used for indentation.
  189. asm = string.expandtabs(asm, 2)
  190. # Stripe unimportant comments, but leave the token '#' in place.
  191. asm = SCRUB_LOOP_COMMENT_RE.sub(r'#', asm)
  192. # Strip trailing whitespace.
  193. asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
  194. return asm
  195. def scrub_asm_mips(asm, args):
  196. # Scrub runs of whitespace out of the assembly, but leave the leading
  197. # whitespace in place.
  198. asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
  199. # Expand the tabs used for indentation.
  200. asm = string.expandtabs(asm, 2)
  201. # Strip trailing whitespace.
  202. asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
  203. return asm
  204. def scrub_asm_riscv(asm, args):
  205. # Scrub runs of whitespace out of the assembly, but leave the leading
  206. # whitespace in place.
  207. asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
  208. # Expand the tabs used for indentation.
  209. asm = string.expandtabs(asm, 2)
  210. # Strip trailing whitespace.
  211. asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
  212. return asm
  213. def scrub_asm_lanai(asm, args):
  214. # Scrub runs of whitespace out of the assembly, but leave the leading
  215. # whitespace in place.
  216. asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
  217. # Expand the tabs used for indentation.
  218. asm = string.expandtabs(asm, 2)
  219. # Strip trailing whitespace.
  220. asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
  221. return asm
  222. def scrub_asm_sparc(asm, args):
  223. # Scrub runs of whitespace out of the assembly, but leave the leading
  224. # whitespace in place.
  225. asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
  226. # Expand the tabs used for indentation.
  227. asm = string.expandtabs(asm, 2)
  228. # Strip trailing whitespace.
  229. asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
  230. return asm
  231. def scrub_asm_systemz(asm, args):
  232. # Scrub runs of whitespace out of the assembly, but leave the leading
  233. # whitespace in place.
  234. asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
  235. # Expand the tabs used for indentation.
  236. asm = string.expandtabs(asm, 2)
  237. # Strip trailing whitespace.
  238. asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
  239. return asm
  240. def scrub_asm_wasm32(asm, args):
  241. # Scrub runs of whitespace out of the assembly, but leave the leading
  242. # whitespace in place.
  243. asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
  244. # Expand the tabs used for indentation.
  245. asm = string.expandtabs(asm, 2)
  246. # Strip trailing whitespace.
  247. asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
  248. return asm
  249. def get_triple_from_march(march):
  250. triples = {
  251. 'amdgcn': 'amdgcn',
  252. 'r600': 'r600',
  253. 'mips': 'mips',
  254. 'sparc': 'sparc',
  255. 'hexagon': 'hexagon',
  256. }
  257. for prefix, triple in triples.items():
  258. if march.startswith(prefix):
  259. return triple
  260. print("Cannot find a triple. Assume 'x86'", file=sys.stderr)
  261. return 'x86'
  262. def build_function_body_dictionary_for_triple(args, raw_tool_output, triple, prefixes, func_dict):
  263. target_handlers = {
  264. 'i686': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
  265. 'x86': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
  266. 'i386': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
  267. 'aarch64': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_RE),
  268. 'aarch64-apple-darwin': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_DARWIN_RE),
  269. 'hexagon': (scrub_asm_hexagon, ASM_FUNCTION_HEXAGON_RE),
  270. 'r600': (scrub_asm_amdgpu, ASM_FUNCTION_AMDGPU_RE),
  271. 'amdgcn': (scrub_asm_amdgpu, ASM_FUNCTION_AMDGPU_RE),
  272. 'arm': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  273. 'arm64': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_RE),
  274. 'arm64-apple-ios': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_DARWIN_RE),
  275. 'armv7-apple-ios' : (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_IOS_RE),
  276. 'armv7-apple-darwin': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_DARWIN_RE),
  277. 'thumb': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
  278. 'thumb-macho': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_MACHO_RE),
  279. 'thumbv5-macho': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_MACHO_RE),
  280. 'thumbv7-apple-ios' : (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_IOS_RE),
  281. 'mips': (scrub_asm_mips, ASM_FUNCTION_MIPS_RE),
  282. 'ppc32': (scrub_asm_powerpc, ASM_FUNCTION_PPC_RE),
  283. 'powerpc': (scrub_asm_powerpc, ASM_FUNCTION_PPC_RE),
  284. 'riscv32': (scrub_asm_riscv, ASM_FUNCTION_RISCV_RE),
  285. 'riscv64': (scrub_asm_riscv, ASM_FUNCTION_RISCV_RE),
  286. 'lanai': (scrub_asm_lanai, ASM_FUNCTION_LANAI_RE),
  287. 'sparc': (scrub_asm_sparc, ASM_FUNCTION_SPARC_RE),
  288. 's390x': (scrub_asm_systemz, ASM_FUNCTION_SYSTEMZ_RE),
  289. 'wasm32': (scrub_asm_wasm32, ASM_FUNCTION_WASM32_RE),
  290. }
  291. handler = None
  292. best_prefix = ''
  293. for prefix, s in target_handlers.items():
  294. if triple.startswith(prefix) and len(prefix) > len(best_prefix):
  295. handler = s
  296. best_prefix = prefix
  297. if handler is None:
  298. raise KeyError('Triple %r is not supported' % (triple))
  299. scrubber, function_re = handler
  300. common.build_function_body_dictionary(
  301. function_re, scrubber, [args], raw_tool_output, prefixes,
  302. func_dict, args.verbose)
  303. ##### Generator of assembly CHECK lines
  304. def add_asm_checks(output_lines, comment_marker, prefix_list, func_dict, func_name):
  305. # Label format is based on ASM string.
  306. check_label_format = '{} %s-LABEL: %s:'.format(comment_marker)
  307. common.add_checks(output_lines, comment_marker, prefix_list, func_dict, func_name, check_label_format, True, False)