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