asm.py 10 KB

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