Clang] Fix expansion of response files in -Wp after integrated-cc1 change
[llvm-project.git] / llvm / utils / UpdateTestChecks / asm.py
blob06f8a22ad4554e728ce987fdebd9877247968341
1 from __future__ import print_function
2 import re
3 import sys
5 from . import common
7 if sys.version_info[0] > 2:
8 class string:
9 expandtabs = str.expandtabs
10 else:
11 import string
13 # RegEx: this is where the magic happens.
15 ##### Assembly parser
17 ASM_FUNCTION_X86_RE = re.compile(
18 r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n(?:\s*\.?Lfunc_begin[^:\n]*:\n)?[^:]*?'
19 r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*'
20 r'^\s*(?:[^:\n]+?:\s*\n\s*\.size|\.cfi_endproc|\.globl|\.comm|\.(?:sub)?section|#+ -- End function)',
21 flags=(re.M | re.S))
23 ASM_FUNCTION_ARM_RE = re.compile(
24 r'^(?P<func>[0-9a-zA-Z_]+):\n' # f: (name of function)
25 r'\s+\.fnstart\n' # .fnstart
26 r'(?P<body>.*?)\n' # (body of the function)
27 r'.Lfunc_end[0-9]+:', # .Lfunc_end0: or # -- End function
28 flags=(re.M | re.S))
30 ASM_FUNCTION_AARCH64_RE = re.compile(
31 r'^_?(?P<func>[^:]+):[ \t]*\/\/[ \t]*@(?P=func)\n'
32 r'(?:[ \t]+.cfi_startproc\n)?' # drop optional cfi noise
33 r'(?P<body>.*?)\n'
34 # This list is incomplete
35 r'.Lfunc_end[0-9]+:\n',
36 flags=(re.M | re.S))
38 ASM_FUNCTION_AMDGPU_RE = re.compile(
39 r'^_?(?P<func>[^:]+):[ \t]*;+[ \t]*@(?P=func)\n[^:]*?'
40 r'(?P<body>.*?)\n' # (body of the function)
41 # This list is incomplete
42 r'^\s*(\.Lfunc_end[0-9]+:\n|\.section)',
43 flags=(re.M | re.S))
45 ASM_FUNCTION_HEXAGON_RE = re.compile(
46 r'^_?(?P<func>[^:]+):[ \t]*//[ \t]*@(?P=func)\n[^:]*?'
47 r'(?P<body>.*?)\n' # (body of the function)
48 # This list is incomplete
49 r'.Lfunc_end[0-9]+:\n',
50 flags=(re.M | re.S))
52 ASM_FUNCTION_MIPS_RE = re.compile(
53 r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n[^:]*?' # f: (name of func)
54 r'(?:^[ \t]+\.(frame|f?mask|set).*?\n)+' # Mips+LLVM standard asm prologue
55 r'(?P<body>.*?)\n' # (body of the function)
56 # Mips+LLVM standard asm epilogue
57 r'(?:(^[ \t]+\.set[^\n]*?\n)*^[ \t]+\.end.*?\n)'
58 r'(\$|\.L)func_end[0-9]+:\n', # $func_end0: (mips32 - O32) or
59 # .Lfunc_end0: (mips64 - NewABI)
60 flags=(re.M | re.S))
62 ASM_FUNCTION_MSP430_RE = re.compile(
63 r'^_?(?P<func>[^:]+):[ \t]*;+[ \t]*@(?P=func)\n[^:]*?'
64 r'(?P<body>.*?)\n'
65 r'(\$|\.L)func_end[0-9]+:\n', # $func_end0:
66 flags=(re.M | re.S))
68 ASM_FUNCTION_PPC_RE = re.compile(
69 r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n'
70 r'.*?'
71 r'\.Lfunc_begin[0-9]+:\n'
72 r'(?:[ \t]+.cfi_startproc\n)?'
73 r'(?:\.Lfunc_[gl]ep[0-9]+:\n(?:[ \t]+.*?\n)*)*'
74 r'(?P<body>.*?)\n'
75 # This list is incomplete
76 r'(?:^[ \t]*(?:\.long[ \t]+[^\n]+|\.quad[ \t]+[^\n]+)\n)*'
77 r'.Lfunc_end[0-9]+:\n',
78 flags=(re.M | re.S))
80 ASM_FUNCTION_RISCV_RE = re.compile(
81 r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n(?:\s*\.?Lfunc_begin[^:\n]*:\n)?[^:]*?'
82 r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*'
83 r'.Lfunc_end[0-9]+:\n',
84 flags=(re.M | re.S))
86 ASM_FUNCTION_LANAI_RE = re.compile(
87 r'^_?(?P<func>[^:]+):[ \t]*!+[ \t]*@(?P=func)\n'
88 r'(?:[ \t]+.cfi_startproc\n)?' # drop optional cfi noise
89 r'(?P<body>.*?)\s*'
90 r'.Lfunc_end[0-9]+:\n',
91 flags=(re.M | re.S))
93 ASM_FUNCTION_SPARC_RE = re.compile(
94 r'^_?(?P<func>[^:]+):[ \t]*!+[ \t]*@(?P=func)\n'
95 r'(?P<body>.*?)\s*'
96 r'.Lfunc_end[0-9]+:\n',
97 flags=(re.M | re.S))
99 ASM_FUNCTION_SYSTEMZ_RE = re.compile(
100 r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n'
101 r'[ \t]+.cfi_startproc\n'
102 r'(?P<body>.*?)\n'
103 r'.Lfunc_end[0-9]+:\n',
104 flags=(re.M | re.S))
106 ASM_FUNCTION_AARCH64_DARWIN_RE = re.compile(
107 r'^_(?P<func>[^:]+):[ \t]*;[ \t]@(?P=func)\n'
108 r'([ \t]*.cfi_startproc\n[\s]*)?'
109 r'(?P<body>.*?)'
110 r'([ \t]*.cfi_endproc\n[\s]*)?'
111 r'^[ \t]*;[ \t]--[ \t]End[ \t]function',
112 flags=(re.M | re.S))
114 ASM_FUNCTION_ARM_DARWIN_RE = re.compile(
115 r'^[ \t]*\.globl[ \t]*_(?P<func>[^ \t])[ \t]*@[ \t]--[ \t]Begin[ \t]function[ \t](?P=func)'
116 r'(?P<directives>.*?)'
117 r'^_(?P=func):\n[ \t]*'
118 r'(?P<body>.*?)'
119 r'^[ \t]*@[ \t]--[ \t]End[ \t]function',
120 flags=(re.M | re.S ))
122 ASM_FUNCTION_ARM_MACHO_RE = re.compile(
123 r'^_(?P<func>[^:]+):[ \t]*\n'
124 r'([ \t]*.cfi_startproc\n[ \t]*)?'
125 r'(?P<body>.*?)\n'
126 r'[ \t]*\.cfi_endproc\n',
127 flags=(re.M | re.S))
129 ASM_FUNCTION_ARM_IOS_RE = re.compile(
130 r'^_(?P<func>[^:]+):[ \t]*\n'
131 r'^Lfunc_begin(?P<id>[0-9][1-9]*):\n'
132 r'(?P<body>.*?)'
133 r'^Lfunc_end(?P=id):\n'
134 r'^[ \t]*@[ \t]--[ \t]End[ \t]function',
135 flags=(re.M | re.S))
137 ASM_FUNCTION_WASM32_RE = re.compile(
138 r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n'
139 r'(?P<body>.*?)\n'
140 r'^\s*(\.Lfunc_end[0-9]+:\n|end_function)',
141 flags=(re.M | re.S))
144 SCRUB_LOOP_COMMENT_RE = re.compile(
145 r'# =>This Inner Loop Header:.*|# in Loop:.*', flags=re.M)
147 SCRUB_X86_SHUFFLES_RE = (
148 re.compile(
149 r'^(\s*\w+) [^#\n]+#+ ((?:[xyz]mm\d+|mem)( \{%k\d+\}( \{z\})?)? = .*)$',
150 flags=re.M))
151 SCRUB_X86_SPILL_RELOAD_RE = (
152 re.compile(
153 r'-?\d+\(%([er])[sb]p\)(.*(?:Spill|Reload))$',
154 flags=re.M))
155 SCRUB_X86_SP_RE = re.compile(r'\d+\(%(esp|rsp)\)')
156 SCRUB_X86_RIP_RE = re.compile(r'[.\w]+\(%rip\)')
157 SCRUB_X86_LCP_RE = re.compile(r'\.LCPI[0-9]+_[0-9]+')
158 SCRUB_X86_RET_RE = re.compile(r'ret[l|q]')
160 def scrub_asm_x86(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 # Detect shuffle asm comments and hide the operands in favor of the comments.
167 asm = SCRUB_X86_SHUFFLES_RE.sub(r'\1 {{.*#+}} \2', asm)
168 # Detect stack spills and reloads and hide their exact offset and whether
169 # they used the stack pointer or frame pointer.
170 asm = SCRUB_X86_SPILL_RELOAD_RE.sub(r'{{[-0-9]+}}(%\1{{[sb]}}p)\2', asm)
171 # Generically match the stack offset of a memory operand.
172 asm = SCRUB_X86_SP_RE.sub(r'{{[0-9]+}}(%\1)', asm)
173 if getattr(args, 'x86_scrub_rip', False):
174 # Generically match a RIP-relative memory operand.
175 asm = SCRUB_X86_RIP_RE.sub(r'{{.*}}(%rip)', asm)
176 # Generically match a LCP symbol.
177 asm = SCRUB_X86_LCP_RE.sub(r'{{\.LCPI.*}}', asm)
178 if getattr(args, 'extra_scrub', False):
179 # Avoid generating different checks for 32- and 64-bit because of 'retl' vs 'retq'.
180 asm = SCRUB_X86_RET_RE.sub(r'ret{{[l|q]}}', asm)
181 # Strip kill operands inserted into the asm.
182 asm = common.SCRUB_KILL_COMMENT_RE.sub('', asm)
183 # Strip trailing whitespace.
184 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
185 return asm
187 def scrub_asm_amdgpu(asm, args):
188 # Scrub runs of whitespace out of the assembly, but leave the leading
189 # whitespace in place.
190 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
191 # Expand the tabs used for indentation.
192 asm = string.expandtabs(asm, 2)
193 # Strip trailing whitespace.
194 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
195 return asm
197 def scrub_asm_arm_eabi(asm, args):
198 # Scrub runs of whitespace out of the assembly, but leave the leading
199 # whitespace in place.
200 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
201 # Expand the tabs used for indentation.
202 asm = string.expandtabs(asm, 2)
203 # Strip kill operands inserted into the asm.
204 asm = common.SCRUB_KILL_COMMENT_RE.sub('', asm)
205 # Strip trailing whitespace.
206 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
207 return asm
209 def scrub_asm_hexagon(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
219 def scrub_asm_powerpc(asm, args):
220 # Scrub runs of whitespace out of the assembly, but leave the leading
221 # whitespace in place.
222 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
223 # Expand the tabs used for indentation.
224 asm = string.expandtabs(asm, 2)
225 # Stripe unimportant comments, but leave the token '#' in place.
226 asm = SCRUB_LOOP_COMMENT_RE.sub(r'#', asm)
227 # Strip trailing whitespace.
228 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
229 return asm
231 def scrub_asm_mips(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
241 def scrub_asm_msp430(asm, args):
242 # Scrub runs of whitespace out of the assembly, but leave the leading
243 # whitespace in place.
244 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
245 # Expand the tabs used for indentation.
246 asm = string.expandtabs(asm, 2)
247 # Strip trailing whitespace.
248 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
249 return asm
251 def scrub_asm_riscv(asm, args):
252 # Scrub runs of whitespace out of the assembly, but leave the leading
253 # whitespace in place.
254 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
255 # Expand the tabs used for indentation.
256 asm = string.expandtabs(asm, 2)
257 # Strip trailing whitespace.
258 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
259 return asm
261 def scrub_asm_lanai(asm, args):
262 # Scrub runs of whitespace out of the assembly, but leave the leading
263 # whitespace in place.
264 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
265 # Expand the tabs used for indentation.
266 asm = string.expandtabs(asm, 2)
267 # Strip trailing whitespace.
268 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
269 return asm
271 def scrub_asm_sparc(asm, args):
272 # Scrub runs of whitespace out of the assembly, but leave the leading
273 # whitespace in place.
274 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
275 # Expand the tabs used for indentation.
276 asm = string.expandtabs(asm, 2)
277 # Strip trailing whitespace.
278 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
279 return asm
281 def scrub_asm_systemz(asm, args):
282 # Scrub runs of whitespace out of the assembly, but leave the leading
283 # whitespace in place.
284 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
285 # Expand the tabs used for indentation.
286 asm = string.expandtabs(asm, 2)
287 # Strip trailing whitespace.
288 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
289 return asm
291 def scrub_asm_wasm32(asm, args):
292 # Scrub runs of whitespace out of the assembly, but leave the leading
293 # whitespace in place.
294 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
295 # Expand the tabs used for indentation.
296 asm = string.expandtabs(asm, 2)
297 # Strip trailing whitespace.
298 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
299 return asm
301 def get_triple_from_march(march):
302 triples = {
303 'amdgcn': 'amdgcn',
304 'r600': 'r600',
305 'mips': 'mips',
306 'sparc': 'sparc',
307 'hexagon': 'hexagon',
309 for prefix, triple in triples.items():
310 if march.startswith(prefix):
311 return triple
312 print("Cannot find a triple. Assume 'x86'", file=sys.stderr)
313 return 'x86'
315 def build_function_body_dictionary_for_triple(args, raw_tool_output, triple, prefixes, func_dict):
316 target_handlers = {
317 'i686': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
318 'x86': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
319 'i386': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
320 'aarch64': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_RE),
321 'aarch64-apple-darwin': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_DARWIN_RE),
322 'hexagon': (scrub_asm_hexagon, ASM_FUNCTION_HEXAGON_RE),
323 'r600': (scrub_asm_amdgpu, ASM_FUNCTION_AMDGPU_RE),
324 'amdgcn': (scrub_asm_amdgpu, ASM_FUNCTION_AMDGPU_RE),
325 'arm': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
326 'arm64': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_RE),
327 'arm64-apple-ios': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_DARWIN_RE),
328 'armv7-apple-ios' : (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_IOS_RE),
329 'armv7-apple-darwin': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_DARWIN_RE),
330 'thumb': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
331 'thumb-macho': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_MACHO_RE),
332 'thumbv5-macho': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_MACHO_RE),
333 'thumbv7-apple-ios' : (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_IOS_RE),
334 'mips': (scrub_asm_mips, ASM_FUNCTION_MIPS_RE),
335 'msp430': (scrub_asm_msp430, ASM_FUNCTION_MSP430_RE),
336 'ppc32': (scrub_asm_powerpc, ASM_FUNCTION_PPC_RE),
337 'powerpc': (scrub_asm_powerpc, ASM_FUNCTION_PPC_RE),
338 'riscv32': (scrub_asm_riscv, ASM_FUNCTION_RISCV_RE),
339 'riscv64': (scrub_asm_riscv, ASM_FUNCTION_RISCV_RE),
340 'lanai': (scrub_asm_lanai, ASM_FUNCTION_LANAI_RE),
341 'sparc': (scrub_asm_sparc, ASM_FUNCTION_SPARC_RE),
342 's390x': (scrub_asm_systemz, ASM_FUNCTION_SYSTEMZ_RE),
343 'wasm32': (scrub_asm_wasm32, ASM_FUNCTION_WASM32_RE),
345 handler = None
346 best_prefix = ''
347 for prefix, s in target_handlers.items():
348 if triple.startswith(prefix) and len(prefix) > len(best_prefix):
349 handler = s
350 best_prefix = prefix
352 if handler is None:
353 raise KeyError('Triple %r is not supported' % (triple))
355 scrubber, function_re = handler
356 common.build_function_body_dictionary(
357 function_re, scrubber, [args], raw_tool_output, prefixes,
358 func_dict, args.verbose, False)
360 ##### Generator of assembly CHECK lines
362 def add_asm_checks(output_lines, comment_marker, prefix_list, func_dict, func_name):
363 # Label format is based on ASM string.
364 check_label_format = '{} %s-LABEL: %s%s:'.format(comment_marker)
365 common.add_checks(output_lines, comment_marker, prefix_list, func_dict, func_name, check_label_format, True, False)