asm.py 13 KB

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