xref: /netbsd-src/external/apache2/llvm/dist/llvm/utils/UpdateTestChecks/asm.py (revision 82d56013d7b633d116a93943de88e08335357a7c)
17330f729Sjoergfrom __future__ import print_function
27330f729Sjoergimport re
37330f729Sjoergimport sys
47330f729Sjoerg
57330f729Sjoergfrom . import common
67330f729Sjoerg
77330f729Sjoergif sys.version_info[0] > 2:
87330f729Sjoerg  class string:
97330f729Sjoerg    expandtabs = str.expandtabs
107330f729Sjoergelse:
117330f729Sjoerg  import string
127330f729Sjoerg
137330f729Sjoerg# RegEx: this is where the magic happens.
147330f729Sjoerg
157330f729Sjoerg##### Assembly parser
167330f729Sjoerg
177330f729SjoergASM_FUNCTION_X86_RE = re.compile(
18*82d56013Sjoerg    r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*(@"?(?P=func)"?| -- Begin function (?P=func))\n(?:\s*\.?Lfunc_begin[^:\n]*:\n)?'
19*82d56013Sjoerg    r'(?:\.L[^$]+\$local:\n)?'      # drop .L<func>$local:
20*82d56013Sjoerg    r'(?:[ \t]+.cfi_startproc\n|.seh_proc[^\n]+\n)?'  # drop optional cfi
217330f729Sjoerg    r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*'
227330f729Sjoerg    r'^\s*(?:[^:\n]+?:\s*\n\s*\.size|\.cfi_endproc|\.globl|\.comm|\.(?:sub)?section|#+ -- End function)',
237330f729Sjoerg    flags=(re.M | re.S))
247330f729Sjoerg
257330f729SjoergASM_FUNCTION_ARM_RE = re.compile(
267330f729Sjoerg        r'^(?P<func>[0-9a-zA-Z_]+):\n' # f: (name of function)
277330f729Sjoerg        r'\s+\.fnstart\n' # .fnstart
287330f729Sjoerg        r'(?P<body>.*?)\n' # (body of the function)
297330f729Sjoerg        r'.Lfunc_end[0-9]+:', # .Lfunc_end0: or # -- End function
307330f729Sjoerg        flags=(re.M | re.S))
317330f729Sjoerg
327330f729SjoergASM_FUNCTION_AARCH64_RE = re.compile(
33*82d56013Sjoerg     r'^_?(?P<func>[^:]+):[ \t]*\/\/[ \t]*@"?(?P=func)"?( (Function|Tail Call))?\n'
347330f729Sjoerg     r'(?:[ \t]+.cfi_startproc\n)?'  # drop optional cfi noise
357330f729Sjoerg     r'(?P<body>.*?)\n'
367330f729Sjoerg     # This list is incomplete
377330f729Sjoerg     r'.Lfunc_end[0-9]+:\n',
387330f729Sjoerg     flags=(re.M | re.S))
397330f729Sjoerg
407330f729SjoergASM_FUNCTION_AMDGPU_RE = re.compile(
41*82d56013Sjoerg    r'^_?(?P<func>[^:]+):[ \t]*;+[ \t]*@"?(?P=func)"?\n[^:]*?'
427330f729Sjoerg    r'(?P<body>.*?)\n' # (body of the function)
437330f729Sjoerg    # This list is incomplete
447330f729Sjoerg    r'^\s*(\.Lfunc_end[0-9]+:\n|\.section)',
457330f729Sjoerg    flags=(re.M | re.S))
467330f729Sjoerg
477330f729SjoergASM_FUNCTION_HEXAGON_RE = re.compile(
48*82d56013Sjoerg    r'^_?(?P<func>[^:]+):[ \t]*//[ \t]*@"?(?P=func)"?\n[^:]*?'
497330f729Sjoerg    r'(?P<body>.*?)\n' # (body of the function)
507330f729Sjoerg    # This list is incomplete
517330f729Sjoerg    r'.Lfunc_end[0-9]+:\n',
527330f729Sjoerg    flags=(re.M | re.S))
537330f729Sjoerg
54*82d56013SjoergASM_FUNCTION_M68K_RE = re.compile(
55*82d56013Sjoerg    r'^_?(?P<func>[^:]+):[ \t]*;[ \t]*@"?(?P=func)"?\n'
56*82d56013Sjoerg    r'(?P<body>.*?)\s*' # (body of the function)
57*82d56013Sjoerg    r'.Lfunc_end[0-9]+:\n',
58*82d56013Sjoerg    flags=(re.M | re.S))
59*82d56013Sjoerg
607330f729SjoergASM_FUNCTION_MIPS_RE = re.compile(
61*82d56013Sjoerg    r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@"?(?P=func)"?\n[^:]*?' # f: (name of func)
627330f729Sjoerg    r'(?:^[ \t]+\.(frame|f?mask|set).*?\n)+'  # Mips+LLVM standard asm prologue
637330f729Sjoerg    r'(?P<body>.*?)\n'                        # (body of the function)
64*82d56013Sjoerg    # Mips+LLVM standard asm epilogue
65*82d56013Sjoerg    r'(?:(^[ \t]+\.set[^\n]*?\n)*^[ \t]+\.end.*?\n)'
667330f729Sjoerg    r'(\$|\.L)func_end[0-9]+:\n',             # $func_end0: (mips32 - O32) or
677330f729Sjoerg                                              # .Lfunc_end0: (mips64 - NewABI)
687330f729Sjoerg    flags=(re.M | re.S))
697330f729Sjoerg
707330f729SjoergASM_FUNCTION_MSP430_RE = re.compile(
71*82d56013Sjoerg    r'^_?(?P<func>[^:]+):[ \t]*;+[ \t]*@"?(?P=func)"?\n[^:]*?'
727330f729Sjoerg    r'(?P<body>.*?)\n'
737330f729Sjoerg    r'(\$|\.L)func_end[0-9]+:\n',             # $func_end0:
747330f729Sjoerg    flags=(re.M | re.S))
757330f729Sjoerg
76*82d56013SjoergASM_FUNCTION_AVR_RE = re.compile(
77*82d56013Sjoerg    r'^_?(?P<func>[^:]+):[ \t]*;+[ \t]*@"?(?P=func)"?\n[^:]*?'
787330f729Sjoerg    r'(?P<body>.*?)\n'
797330f729Sjoerg    r'.Lfunc_end[0-9]+:\n',
807330f729Sjoerg    flags=(re.M | re.S))
817330f729Sjoerg
82*82d56013SjoergASM_FUNCTION_PPC_RE = re.compile(
83*82d56013Sjoerg    r'#[ \-\t]*Begin function (?P<func>[^.:]+)\n'
84*82d56013Sjoerg    r'.*?'
85*82d56013Sjoerg    r'^[_.]?(?P=func):(?:[ \t]*#+[ \t]*@"?(?P=func)"?)?\n'
86*82d56013Sjoerg    r'(?:^[^#]*\n)*'
87*82d56013Sjoerg    r'(?P<body>.*?)\n'
88*82d56013Sjoerg    # This list is incomplete
89*82d56013Sjoerg    r'(?:^[ \t]*(?:\.(?:long|quad|v?byte)[ \t]+[^\n]+)\n)*'
90*82d56013Sjoerg    r'(?:\.Lfunc_end|L\.\.(?P=func))[0-9]+:\n',
91*82d56013Sjoerg    flags=(re.M | re.S))
92*82d56013Sjoerg
937330f729SjoergASM_FUNCTION_RISCV_RE = re.compile(
94*82d56013Sjoerg    r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@"?(?P=func)"?\n'
95*82d56013Sjoerg    r'(?:\s*\.?L(?P=func)\$local:\n)?'  # optional .L<func>$local: due to -fno-semantic-interposition
96*82d56013Sjoerg    r'(?:\s*\.?Lfunc_begin[^:\n]*:\n)?[^:]*?'
977330f729Sjoerg    r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*'
987330f729Sjoerg    r'.Lfunc_end[0-9]+:\n',
997330f729Sjoerg    flags=(re.M | re.S))
1007330f729Sjoerg
1017330f729SjoergASM_FUNCTION_LANAI_RE = re.compile(
102*82d56013Sjoerg    r'^_?(?P<func>[^:]+):[ \t]*!+[ \t]*@"?(?P=func)"?\n'
1037330f729Sjoerg    r'(?:[ \t]+.cfi_startproc\n)?'  # drop optional cfi noise
1047330f729Sjoerg    r'(?P<body>.*?)\s*'
1057330f729Sjoerg    r'.Lfunc_end[0-9]+:\n',
1067330f729Sjoerg    flags=(re.M | re.S))
1077330f729Sjoerg
1087330f729SjoergASM_FUNCTION_SPARC_RE = re.compile(
109*82d56013Sjoerg    r'^_?(?P<func>[^:]+):[ \t]*!+[ \t]*@"?(?P=func)"?\n'
1107330f729Sjoerg    r'(?P<body>.*?)\s*'
1117330f729Sjoerg    r'.Lfunc_end[0-9]+:\n',
1127330f729Sjoerg    flags=(re.M | re.S))
1137330f729Sjoerg
1147330f729SjoergASM_FUNCTION_SYSTEMZ_RE = re.compile(
115*82d56013Sjoerg    r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@"?(?P=func)"?\n'
1167330f729Sjoerg    r'[ \t]+.cfi_startproc\n'
1177330f729Sjoerg    r'(?P<body>.*?)\n'
1187330f729Sjoerg    r'.Lfunc_end[0-9]+:\n',
1197330f729Sjoerg    flags=(re.M | re.S))
1207330f729Sjoerg
1217330f729SjoergASM_FUNCTION_AARCH64_DARWIN_RE = re.compile(
122*82d56013Sjoerg     r'^_(?P<func>[^:]+):[ \t]*;[ \t]@"?(?P=func)"?\n'
1237330f729Sjoerg     r'([ \t]*.cfi_startproc\n[\s]*)?'
1247330f729Sjoerg     r'(?P<body>.*?)'
1257330f729Sjoerg     r'([ \t]*.cfi_endproc\n[\s]*)?'
1267330f729Sjoerg     r'^[ \t]*;[ \t]--[ \t]End[ \t]function',
1277330f729Sjoerg     flags=(re.M | re.S))
1287330f729Sjoerg
1297330f729SjoergASM_FUNCTION_ARM_DARWIN_RE = re.compile(
130*82d56013Sjoerg     r'^[ \t]*\.globl[ \t]*_(?P<func>[^ \t])[ \t]*@[ \t]--[ \t]Begin[ \t]function[ \t]"?(?P=func)"?'
1317330f729Sjoerg     r'(?P<directives>.*?)'
1327330f729Sjoerg     r'^_(?P=func):\n[ \t]*'
1337330f729Sjoerg     r'(?P<body>.*?)'
1347330f729Sjoerg     r'^[ \t]*@[ \t]--[ \t]End[ \t]function',
1357330f729Sjoerg     flags=(re.M | re.S ))
1367330f729Sjoerg
1377330f729SjoergASM_FUNCTION_ARM_MACHO_RE = re.compile(
1387330f729Sjoerg     r'^_(?P<func>[^:]+):[ \t]*\n'
1397330f729Sjoerg     r'([ \t]*.cfi_startproc\n[ \t]*)?'
1407330f729Sjoerg     r'(?P<body>.*?)\n'
1417330f729Sjoerg     r'[ \t]*\.cfi_endproc\n',
1427330f729Sjoerg     flags=(re.M | re.S))
1437330f729Sjoerg
1447330f729SjoergASM_FUNCTION_ARM_IOS_RE = re.compile(
1457330f729Sjoerg     r'^_(?P<func>[^:]+):[ \t]*\n'
1467330f729Sjoerg     r'^Lfunc_begin(?P<id>[0-9][1-9]*):\n'
1477330f729Sjoerg     r'(?P<body>.*?)'
1487330f729Sjoerg     r'^Lfunc_end(?P=id):\n'
1497330f729Sjoerg     r'^[ \t]*@[ \t]--[ \t]End[ \t]function',
1507330f729Sjoerg     flags=(re.M | re.S))
1517330f729Sjoerg
1527330f729SjoergASM_FUNCTION_WASM32_RE = re.compile(
153*82d56013Sjoerg    r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@"?(?P=func)"?\n'
1547330f729Sjoerg    r'(?P<body>.*?)\n'
1557330f729Sjoerg    r'^\s*(\.Lfunc_end[0-9]+:\n|end_function)',
1567330f729Sjoerg    flags=(re.M | re.S))
1577330f729Sjoerg
1587330f729SjoergSCRUB_X86_SHUFFLES_RE = (
1597330f729Sjoerg    re.compile(
1607330f729Sjoerg        r'^(\s*\w+) [^#\n]+#+ ((?:[xyz]mm\d+|mem)( \{%k\d+\}( \{z\})?)? = .*)$',
1617330f729Sjoerg        flags=re.M))
162*82d56013Sjoerg
163*82d56013SjoergSCRUB_X86_SHUFFLES_NO_MEM_RE = (
164*82d56013Sjoerg    re.compile(
165*82d56013Sjoerg        r'^(\s*\w+) [^#\n]+#+ ((?:[xyz]mm\d+|mem)( \{%k\d+\}( \{z\})?)? = (?!.*(?:mem)).*)$',
166*82d56013Sjoerg        flags=re.M))
167*82d56013Sjoerg
1687330f729SjoergSCRUB_X86_SPILL_RELOAD_RE = (
1697330f729Sjoerg    re.compile(
1707330f729Sjoerg        r'-?\d+\(%([er])[sb]p\)(.*(?:Spill|Reload))$',
1717330f729Sjoerg        flags=re.M))
1727330f729SjoergSCRUB_X86_SP_RE = re.compile(r'\d+\(%(esp|rsp)\)')
1737330f729SjoergSCRUB_X86_RIP_RE = re.compile(r'[.\w]+\(%rip\)')
174*82d56013SjoergSCRUB_X86_LCP_RE = re.compile(r'\.?LCPI[0-9]+_[0-9]+')
1757330f729SjoergSCRUB_X86_RET_RE = re.compile(r'ret[l|q]')
1767330f729Sjoerg
1777330f729Sjoergdef scrub_asm_x86(asm, args):
1787330f729Sjoerg  # Scrub runs of whitespace out of the assembly, but leave the leading
1797330f729Sjoerg  # whitespace in place.
1807330f729Sjoerg  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
1817330f729Sjoerg  # Expand the tabs used for indentation.
1827330f729Sjoerg  asm = string.expandtabs(asm, 2)
183*82d56013Sjoerg
1847330f729Sjoerg  # Detect shuffle asm comments and hide the operands in favor of the comments.
185*82d56013Sjoerg  if getattr(args, 'no_x86_scrub_mem_shuffle', True):
186*82d56013Sjoerg    asm = SCRUB_X86_SHUFFLES_NO_MEM_RE.sub(r'\1 {{.*#+}} \2', asm)
187*82d56013Sjoerg  else:
1887330f729Sjoerg    asm = SCRUB_X86_SHUFFLES_RE.sub(r'\1 {{.*#+}} \2', asm)
189*82d56013Sjoerg
1907330f729Sjoerg  # Detect stack spills and reloads and hide their exact offset and whether
1917330f729Sjoerg  # they used the stack pointer or frame pointer.
1927330f729Sjoerg  asm = SCRUB_X86_SPILL_RELOAD_RE.sub(r'{{[-0-9]+}}(%\1{{[sb]}}p)\2', asm)
193*82d56013Sjoerg  if getattr(args, 'x86_scrub_sp', True):
1947330f729Sjoerg    # Generically match the stack offset of a memory operand.
1957330f729Sjoerg    asm = SCRUB_X86_SP_RE.sub(r'{{[0-9]+}}(%\1)', asm)
1967330f729Sjoerg  if getattr(args, 'x86_scrub_rip', False):
1977330f729Sjoerg    # Generically match a RIP-relative memory operand.
1987330f729Sjoerg    asm = SCRUB_X86_RIP_RE.sub(r'{{.*}}(%rip)', asm)
1997330f729Sjoerg  # Generically match a LCP symbol.
200*82d56013Sjoerg  asm = SCRUB_X86_LCP_RE.sub(r'{{\.?LCPI[0-9]+_[0-9]+}}', asm)
2017330f729Sjoerg  if getattr(args, 'extra_scrub', False):
2027330f729Sjoerg    # Avoid generating different checks for 32- and 64-bit because of 'retl' vs 'retq'.
2037330f729Sjoerg    asm = SCRUB_X86_RET_RE.sub(r'ret{{[l|q]}}', asm)
2047330f729Sjoerg  # Strip kill operands inserted into the asm.
2057330f729Sjoerg  asm = common.SCRUB_KILL_COMMENT_RE.sub('', asm)
2067330f729Sjoerg  # Strip trailing whitespace.
2077330f729Sjoerg  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
2087330f729Sjoerg  return asm
2097330f729Sjoerg
2107330f729Sjoergdef scrub_asm_amdgpu(asm, args):
2117330f729Sjoerg  # Scrub runs of whitespace out of the assembly, but leave the leading
2127330f729Sjoerg  # whitespace in place.
2137330f729Sjoerg  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
2147330f729Sjoerg  # Expand the tabs used for indentation.
2157330f729Sjoerg  asm = string.expandtabs(asm, 2)
2167330f729Sjoerg  # Strip trailing whitespace.
2177330f729Sjoerg  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
2187330f729Sjoerg  return asm
2197330f729Sjoerg
2207330f729Sjoergdef scrub_asm_arm_eabi(asm, args):
2217330f729Sjoerg  # Scrub runs of whitespace out of the assembly, but leave the leading
2227330f729Sjoerg  # whitespace in place.
2237330f729Sjoerg  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
2247330f729Sjoerg  # Expand the tabs used for indentation.
2257330f729Sjoerg  asm = string.expandtabs(asm, 2)
2267330f729Sjoerg  # Strip kill operands inserted into the asm.
2277330f729Sjoerg  asm = common.SCRUB_KILL_COMMENT_RE.sub('', asm)
2287330f729Sjoerg  # Strip trailing whitespace.
2297330f729Sjoerg  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
2307330f729Sjoerg  return asm
2317330f729Sjoerg
2327330f729Sjoergdef scrub_asm_hexagon(asm, args):
2337330f729Sjoerg  # Scrub runs of whitespace out of the assembly, but leave the leading
2347330f729Sjoerg  # whitespace in place.
2357330f729Sjoerg  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
2367330f729Sjoerg  # Expand the tabs used for indentation.
2377330f729Sjoerg  asm = string.expandtabs(asm, 2)
2387330f729Sjoerg  # Strip trailing whitespace.
2397330f729Sjoerg  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
2407330f729Sjoerg  return asm
2417330f729Sjoerg
2427330f729Sjoergdef scrub_asm_powerpc(asm, args):
2437330f729Sjoerg  # Scrub runs of whitespace out of the assembly, but leave the leading
2447330f729Sjoerg  # whitespace in place.
2457330f729Sjoerg  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
2467330f729Sjoerg  # Expand the tabs used for indentation.
2477330f729Sjoerg  asm = string.expandtabs(asm, 2)
248*82d56013Sjoerg  # Strip unimportant comments, but leave the token '#' in place.
249*82d56013Sjoerg  asm = common.SCRUB_LOOP_COMMENT_RE.sub(r'#', asm)
250*82d56013Sjoerg  # Strip trailing whitespace.
251*82d56013Sjoerg  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
252*82d56013Sjoerg  # Strip the tailing token '#', except the line only has token '#'.
253*82d56013Sjoerg  asm = common.SCRUB_TAILING_COMMENT_TOKEN_RE.sub(r'', asm)
254*82d56013Sjoerg  return asm
255*82d56013Sjoerg
256*82d56013Sjoergdef scrub_asm_m68k(asm, args):
257*82d56013Sjoerg  # Scrub runs of whitespace out of the assembly, but leave the leading
258*82d56013Sjoerg  # whitespace in place.
259*82d56013Sjoerg  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
260*82d56013Sjoerg  # Expand the tabs used for indentation.
261*82d56013Sjoerg  asm = string.expandtabs(asm, 2)
2627330f729Sjoerg  # Strip trailing whitespace.
2637330f729Sjoerg  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
2647330f729Sjoerg  return asm
2657330f729Sjoerg
2667330f729Sjoergdef scrub_asm_mips(asm, args):
2677330f729Sjoerg  # Scrub runs of whitespace out of the assembly, but leave the leading
2687330f729Sjoerg  # whitespace in place.
2697330f729Sjoerg  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
2707330f729Sjoerg  # Expand the tabs used for indentation.
2717330f729Sjoerg  asm = string.expandtabs(asm, 2)
2727330f729Sjoerg  # Strip trailing whitespace.
2737330f729Sjoerg  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
2747330f729Sjoerg  return asm
2757330f729Sjoerg
2767330f729Sjoergdef scrub_asm_msp430(asm, args):
2777330f729Sjoerg  # Scrub runs of whitespace out of the assembly, but leave the leading
2787330f729Sjoerg  # whitespace in place.
2797330f729Sjoerg  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
2807330f729Sjoerg  # Expand the tabs used for indentation.
2817330f729Sjoerg  asm = string.expandtabs(asm, 2)
2827330f729Sjoerg  # Strip trailing whitespace.
2837330f729Sjoerg  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
2847330f729Sjoerg  return asm
2857330f729Sjoerg
286*82d56013Sjoergdef scrub_asm_avr(asm, args):
287*82d56013Sjoerg  # Scrub runs of whitespace out of the assembly, but leave the leading
288*82d56013Sjoerg  # whitespace in place.
289*82d56013Sjoerg  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
290*82d56013Sjoerg  # Expand the tabs used for indentation.
291*82d56013Sjoerg  asm = string.expandtabs(asm, 2)
292*82d56013Sjoerg  # Strip trailing whitespace.
293*82d56013Sjoerg  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
294*82d56013Sjoerg  return asm
295*82d56013Sjoerg
2967330f729Sjoergdef scrub_asm_riscv(asm, args):
2977330f729Sjoerg  # Scrub runs of whitespace out of the assembly, but leave the leading
2987330f729Sjoerg  # whitespace in place.
2997330f729Sjoerg  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
3007330f729Sjoerg  # Expand the tabs used for indentation.
3017330f729Sjoerg  asm = string.expandtabs(asm, 2)
3027330f729Sjoerg  # Strip trailing whitespace.
3037330f729Sjoerg  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
3047330f729Sjoerg  return asm
3057330f729Sjoerg
3067330f729Sjoergdef scrub_asm_lanai(asm, args):
3077330f729Sjoerg  # Scrub runs of whitespace out of the assembly, but leave the leading
3087330f729Sjoerg  # whitespace in place.
3097330f729Sjoerg  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
3107330f729Sjoerg  # Expand the tabs used for indentation.
3117330f729Sjoerg  asm = string.expandtabs(asm, 2)
3127330f729Sjoerg  # Strip trailing whitespace.
3137330f729Sjoerg  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
3147330f729Sjoerg  return asm
3157330f729Sjoerg
3167330f729Sjoergdef scrub_asm_sparc(asm, args):
3177330f729Sjoerg  # Scrub runs of whitespace out of the assembly, but leave the leading
3187330f729Sjoerg  # whitespace in place.
3197330f729Sjoerg  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
3207330f729Sjoerg  # Expand the tabs used for indentation.
3217330f729Sjoerg  asm = string.expandtabs(asm, 2)
3227330f729Sjoerg  # Strip trailing whitespace.
3237330f729Sjoerg  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
3247330f729Sjoerg  return asm
3257330f729Sjoerg
3267330f729Sjoergdef scrub_asm_systemz(asm, args):
3277330f729Sjoerg  # Scrub runs of whitespace out of the assembly, but leave the leading
3287330f729Sjoerg  # whitespace in place.
3297330f729Sjoerg  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
3307330f729Sjoerg  # Expand the tabs used for indentation.
3317330f729Sjoerg  asm = string.expandtabs(asm, 2)
3327330f729Sjoerg  # Strip trailing whitespace.
3337330f729Sjoerg  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
3347330f729Sjoerg  return asm
3357330f729Sjoerg
3367330f729Sjoergdef scrub_asm_wasm32(asm, args):
3377330f729Sjoerg  # Scrub runs of whitespace out of the assembly, but leave the leading
3387330f729Sjoerg  # whitespace in place.
3397330f729Sjoerg  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
3407330f729Sjoerg  # Expand the tabs used for indentation.
3417330f729Sjoerg  asm = string.expandtabs(asm, 2)
3427330f729Sjoerg  # Strip trailing whitespace.
3437330f729Sjoerg  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
3447330f729Sjoerg  return asm
3457330f729Sjoerg
3467330f729Sjoergdef get_triple_from_march(march):
3477330f729Sjoerg  triples = {
3487330f729Sjoerg      'amdgcn': 'amdgcn',
3497330f729Sjoerg      'r600': 'r600',
3507330f729Sjoerg      'mips': 'mips',
3517330f729Sjoerg      'sparc': 'sparc',
3527330f729Sjoerg      'hexagon': 'hexagon',
3537330f729Sjoerg  }
3547330f729Sjoerg  for prefix, triple in triples.items():
3557330f729Sjoerg    if march.startswith(prefix):
3567330f729Sjoerg      return triple
3577330f729Sjoerg  print("Cannot find a triple. Assume 'x86'", file=sys.stderr)
3587330f729Sjoerg  return 'x86'
3597330f729Sjoerg
360*82d56013Sjoergdef get_run_handler(triple):
3617330f729Sjoerg  target_handlers = {
3627330f729Sjoerg      'i686': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
3637330f729Sjoerg      'x86': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
3647330f729Sjoerg      'i386': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
3657330f729Sjoerg      'aarch64': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_RE),
3667330f729Sjoerg      'aarch64-apple-darwin': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_DARWIN_RE),
3677330f729Sjoerg      'hexagon': (scrub_asm_hexagon, ASM_FUNCTION_HEXAGON_RE),
3687330f729Sjoerg      'r600': (scrub_asm_amdgpu, ASM_FUNCTION_AMDGPU_RE),
3697330f729Sjoerg      'amdgcn': (scrub_asm_amdgpu, ASM_FUNCTION_AMDGPU_RE),
3707330f729Sjoerg      'arm': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
3717330f729Sjoerg      'arm64': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_RE),
372*82d56013Sjoerg      'arm64e': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_DARWIN_RE),
3737330f729Sjoerg      'arm64-apple-ios': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_DARWIN_RE),
3747330f729Sjoerg      'armv7-apple-ios' : (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_IOS_RE),
3757330f729Sjoerg      'armv7-apple-darwin': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_DARWIN_RE),
3767330f729Sjoerg      'thumb': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
3777330f729Sjoerg      'thumb-macho': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_MACHO_RE),
3787330f729Sjoerg      'thumbv5-macho': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_MACHO_RE),
3797330f729Sjoerg      'thumbv7-apple-ios' : (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_IOS_RE),
380*82d56013Sjoerg      'm68k': (scrub_asm_m68k, ASM_FUNCTION_M68K_RE),
3817330f729Sjoerg      'mips': (scrub_asm_mips, ASM_FUNCTION_MIPS_RE),
3827330f729Sjoerg      'msp430': (scrub_asm_msp430, ASM_FUNCTION_MSP430_RE),
383*82d56013Sjoerg      'avr': (scrub_asm_avr, ASM_FUNCTION_AVR_RE),
3847330f729Sjoerg      'ppc32': (scrub_asm_powerpc, ASM_FUNCTION_PPC_RE),
3857330f729Sjoerg      'powerpc': (scrub_asm_powerpc, ASM_FUNCTION_PPC_RE),
3867330f729Sjoerg      'riscv32': (scrub_asm_riscv, ASM_FUNCTION_RISCV_RE),
3877330f729Sjoerg      'riscv64': (scrub_asm_riscv, ASM_FUNCTION_RISCV_RE),
3887330f729Sjoerg      'lanai': (scrub_asm_lanai, ASM_FUNCTION_LANAI_RE),
3897330f729Sjoerg      'sparc': (scrub_asm_sparc, ASM_FUNCTION_SPARC_RE),
3907330f729Sjoerg      's390x': (scrub_asm_systemz, ASM_FUNCTION_SYSTEMZ_RE),
3917330f729Sjoerg      'wasm32': (scrub_asm_wasm32, ASM_FUNCTION_WASM32_RE),
3927330f729Sjoerg  }
3937330f729Sjoerg  handler = None
3947330f729Sjoerg  best_prefix = ''
3957330f729Sjoerg  for prefix, s in target_handlers.items():
3967330f729Sjoerg    if triple.startswith(prefix) and len(prefix) > len(best_prefix):
3977330f729Sjoerg      handler = s
3987330f729Sjoerg      best_prefix = prefix
3997330f729Sjoerg
4007330f729Sjoerg  if handler is None:
4017330f729Sjoerg    raise KeyError('Triple %r is not supported' % (triple))
4027330f729Sjoerg
403*82d56013Sjoerg  return handler
4047330f729Sjoerg
4057330f729Sjoerg##### Generator of assembly CHECK lines
4067330f729Sjoerg
4077330f729Sjoergdef add_asm_checks(output_lines, comment_marker, prefix_list, func_dict, func_name):
4087330f729Sjoerg  # Label format is based on ASM string.
409*82d56013Sjoerg  check_label_format = '{} %s-LABEL: %s%s:'.format(comment_marker)
410*82d56013Sjoerg  global_vars_seen_dict = {}
411*82d56013Sjoerg  common.add_checks(output_lines, comment_marker, prefix_list, func_dict, func_name, check_label_format, True, False, global_vars_seen_dict)
412