1 from __future__
import print_function
7 if sys
.version_info
[0] > 2:
9 expandtabs
= str.expandtabs
13 # RegEx: this is where the magic happens.
17 ASM_FUNCTION_X86_RE
= re
.compile(
18 r
'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*(@"?(?P=func)"?| -- Begin function (?P=func))\n(?:\s*\.?Lfunc_begin[^:\n]*:\n)?'
19 r
'(?:\.L[^$]+\$local:\n)?' # drop .L<func>$local:
20 r
'(?:[ \t]+.cfi_startproc\n|.seh_proc[^\n]+\n)?' # drop optional cfi
21 r
'(?P<body>^##?[ \t]+[^:]+:.*?)\s*'
22 r
'^\s*(?:[^:\n]+?:\s*\n\s*\.size|\.cfi_endproc|\.globl|\.comm|\.(?:sub)?section|#+ -- End function)',
25 ASM_FUNCTION_ARM_RE
= re
.compile(
26 r
'^(?P<func>[0-9a-zA-Z_]+):\n' # f: (name of function)
27 r
'\s+\.fnstart\n' # .fnstart
28 r
'(?P<body>.*?)\n' # (body of the function)
29 r
'.Lfunc_end[0-9]+:', # .Lfunc_end0: or # -- End function
32 ASM_FUNCTION_AARCH64_RE
= re
.compile(
33 r
'^_?(?P<func>[^:]+):[ \t]*\/\/[ \t]*@"?(?P=func)"?( (Function|Tail Call))?\n'
34 r
'(?:[ \t]+.cfi_startproc\n)?' # drop optional cfi noise
36 # This list is incomplete
37 r
'.Lfunc_end[0-9]+:\n',
40 ASM_FUNCTION_AMDGPU_RE
= re
.compile(
41 r
'^_?(?P<func>[^:]+):[ \t]*;+[ \t]*@"?(?P=func)"?\n[^:]*?'
42 r
'(?P<body>.*?)\n' # (body of the function)
43 # This list is incomplete
44 r
'^\s*(\.Lfunc_end[0-9]+:\n|\.section)',
47 ASM_FUNCTION_HEXAGON_RE
= re
.compile(
48 r
'^_?(?P<func>[^:]+):[ \t]*//[ \t]*@"?(?P=func)"?\n[^:]*?'
49 r
'(?P<body>.*?)\n' # (body of the function)
50 # This list is incomplete
51 r
'.Lfunc_end[0-9]+:\n',
54 ASM_FUNCTION_M68K_RE
= re
.compile(
55 r
'^_?(?P<func>[^:]+):[ \t]*;[ \t]*@"?(?P=func)"?\n'
56 r
'(?P<body>.*?)\s*' # (body of the function)
57 r
'.Lfunc_end[0-9]+:\n',
60 ASM_FUNCTION_MIPS_RE
= re
.compile(
61 r
'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@"?(?P=func)"?\n[^:]*?' # f: (name of func)
62 r
'(?:^[ \t]+\.(frame|f?mask|set).*?\n)+' # Mips+LLVM standard asm prologue
63 r
'(?P<body>.*?)\n' # (body of the function)
64 # Mips+LLVM standard asm epilogue
65 r
'(?:(^[ \t]+\.set[^\n]*?\n)*^[ \t]+\.end.*?\n)'
66 r
'(\$|\.L)func_end[0-9]+:\n', # $func_end0: (mips32 - O32) or
67 # .Lfunc_end0: (mips64 - NewABI)
70 ASM_FUNCTION_MSP430_RE
= re
.compile(
71 r
'^_?(?P<func>[^:]+):[ \t]*;+[ \t]*@"?(?P=func)"?\n[^:]*?'
73 r
'(\$|\.L)func_end[0-9]+:\n', # $func_end0:
76 ASM_FUNCTION_AVR_RE
= re
.compile(
77 r
'^_?(?P<func>[^:]+):[ \t]*;+[ \t]*@"?(?P=func)"?\n[^:]*?'
79 r
'.Lfunc_end[0-9]+:\n',
82 ASM_FUNCTION_PPC_RE
= re
.compile(
83 r
'#[ \-\t]*Begin function (?P<func>[^.:]+)\n'
85 r
'^[_.]?(?P=func):(?:[ \t]*#+[ \t]*@"?(?P=func)"?)?\n'
88 # This list is incomplete
89 r
'(?:^[ \t]*(?:\.(?:long|quad|v?byte)[ \t]+[^\n]+)\n)*'
90 r
'(?:\.Lfunc_end|L\.\.(?P=func))[0-9]+:\n',
93 ASM_FUNCTION_RISCV_RE
= re
.compile(
94 r
'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@"?(?P=func)"?\n'
95 r
'(?:\s*\.?L(?P=func)\$local:\n)?' # optional .L<func>$local: due to -fno-semantic-interposition
96 r
'(?:\s*\.?Lfunc_begin[^:\n]*:\n)?[^:]*?'
97 r
'(?P<body>^##?[ \t]+[^:]+:.*?)\s*'
98 r
'.Lfunc_end[0-9]+:\n',
101 ASM_FUNCTION_LANAI_RE
= re
.compile(
102 r
'^_?(?P<func>[^:]+):[ \t]*!+[ \t]*@"?(?P=func)"?\n'
103 r
'(?:[ \t]+.cfi_startproc\n)?' # drop optional cfi noise
105 r
'.Lfunc_end[0-9]+:\n',
108 ASM_FUNCTION_SPARC_RE
= re
.compile(
109 r
'^_?(?P<func>[^:]+):[ \t]*!+[ \t]*@"?(?P=func)"?\n'
111 r
'.Lfunc_end[0-9]+:\n',
114 ASM_FUNCTION_SYSTEMZ_RE
= re
.compile(
115 r
'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@"?(?P=func)"?\n'
116 r
'[ \t]+.cfi_startproc\n'
118 r
'.Lfunc_end[0-9]+:\n',
121 ASM_FUNCTION_AARCH64_DARWIN_RE
= re
.compile(
122 r
'^_(?P<func>[^:]+):[ \t]*;[ \t]@"?(?P=func)"?\n'
123 r
'([ \t]*.cfi_startproc\n[\s]*)?'
125 r
'([ \t]*.cfi_endproc\n[\s]*)?'
126 r
'^[ \t]*;[ \t]--[ \t]End[ \t]function',
129 ASM_FUNCTION_ARM_DARWIN_RE
= re
.compile(
130 r
'^[ \t]*\.globl[ \t]*_(?P<func>[^ \t])[ \t]*@[ \t]--[ \t]Begin[ \t]function[ \t]"?(?P=func)"?'
131 r
'(?P<directives>.*?)'
132 r
'^_(?P=func):\n[ \t]*'
134 r
'^[ \t]*@[ \t]--[ \t]End[ \t]function',
135 flags
=(re
.M | re
.S
))
137 ASM_FUNCTION_ARM_MACHO_RE
= re
.compile(
138 r
'^_(?P<func>[^:]+):[ \t]*\n'
139 r
'([ \t]*.cfi_startproc\n[ \t]*)?'
141 r
'[ \t]*\.cfi_endproc\n',
144 ASM_FUNCTION_THUMBS_DARWIN_RE
= re
.compile(
145 r
'^_(?P<func>[^:]+):\n'
147 r
'[ \t]*\.data_region\n',
150 ASM_FUNCTION_THUMB_DARWIN_RE
= re
.compile(
151 r
'^_(?P<func>[^:]+):\n'
153 r
'^[ \t]*@[ \t]--[ \t]End[ \t]function',
156 ASM_FUNCTION_ARM_IOS_RE
= re
.compile(
157 r
'^_(?P<func>[^:]+):\n'
159 r
'^[ \t]*@[ \t]--[ \t]End[ \t]function',
162 ASM_FUNCTION_WASM32_RE
= re
.compile(
163 r
'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@"?(?P=func)"?\n'
165 r
'^\s*(\.Lfunc_end[0-9]+:\n|end_function)',
168 SCRUB_X86_SHUFFLES_RE
= (
170 r
'^(\s*\w+) [^#\n]+#+ ((?:[xyz]mm\d+|mem)( \{%k\d+\}( \{z\})?)? = .*)$',
173 SCRUB_X86_SHUFFLES_NO_MEM_RE
= (
175 r
'^(\s*\w+) [^#\n]+#+ ((?:[xyz]mm\d+|mem)( \{%k\d+\}( \{z\})?)? = (?!.*(?:mem)).*)$',
178 SCRUB_X86_SPILL_RELOAD_RE
= (
180 r
'-?\d+\(%([er])[sb]p\)(.*(?:Spill|Reload))$',
182 SCRUB_X86_SP_RE
= re
.compile(r
'\d+\(%(esp|rsp)\)')
183 SCRUB_X86_RIP_RE
= re
.compile(r
'[.\w]+\(%rip\)')
184 SCRUB_X86_LCP_RE
= re
.compile(r
'\.?LCPI[0-9]+_[0-9]+')
185 SCRUB_X86_RET_RE
= re
.compile(r
'ret[l|q]')
187 def scrub_asm_x86(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)
194 # Detect shuffle asm comments and hide the operands in favor of the comments.
195 if getattr(args
, 'no_x86_scrub_mem_shuffle', True):
196 asm
= SCRUB_X86_SHUFFLES_NO_MEM_RE
.sub(r
'\1 {{.*#+}} \2', asm
)
198 asm
= SCRUB_X86_SHUFFLES_RE
.sub(r
'\1 {{.*#+}} \2', asm
)
200 # Detect stack spills and reloads and hide their exact offset and whether
201 # they used the stack pointer or frame pointer.
202 asm
= SCRUB_X86_SPILL_RELOAD_RE
.sub(r
'{{[-0-9]+}}(%\1{{[sb]}}p)\2', asm
)
203 if getattr(args
, 'x86_scrub_sp', True):
204 # Generically match the stack offset of a memory operand.
205 asm
= SCRUB_X86_SP_RE
.sub(r
'{{[0-9]+}}(%\1)', asm
)
206 if getattr(args
, 'x86_scrub_rip', False):
207 # Generically match a RIP-relative memory operand.
208 asm
= SCRUB_X86_RIP_RE
.sub(r
'{{.*}}(%rip)', asm
)
209 # Generically match a LCP symbol.
210 asm
= SCRUB_X86_LCP_RE
.sub(r
'{{\.?LCPI[0-9]+_[0-9]+}}', asm
)
211 if getattr(args
, 'extra_scrub', False):
212 # Avoid generating different checks for 32- and 64-bit because of 'retl' vs 'retq'.
213 asm
= SCRUB_X86_RET_RE
.sub(r
'ret{{[l|q]}}', asm
)
214 # Strip kill operands inserted into the asm.
215 asm
= common
.SCRUB_KILL_COMMENT_RE
.sub('', asm
)
216 # Strip trailing whitespace.
217 asm
= common
.SCRUB_TRAILING_WHITESPACE_RE
.sub(r
'', asm
)
220 def scrub_asm_amdgpu(asm
, args
):
221 # Scrub runs of whitespace out of the assembly, but leave the leading
222 # whitespace in place.
223 asm
= common
.SCRUB_WHITESPACE_RE
.sub(r
' ', asm
)
224 # Expand the tabs used for indentation.
225 asm
= string
.expandtabs(asm
, 2)
226 # Strip trailing whitespace.
227 asm
= common
.SCRUB_TRAILING_WHITESPACE_RE
.sub(r
'', asm
)
230 def scrub_asm_arm_eabi(asm
, args
):
231 # Scrub runs of whitespace out of the assembly, but leave the leading
232 # whitespace in place.
233 asm
= common
.SCRUB_WHITESPACE_RE
.sub(r
' ', asm
)
234 # Expand the tabs used for indentation.
235 asm
= string
.expandtabs(asm
, 2)
236 # Strip kill operands inserted into the asm.
237 asm
= common
.SCRUB_KILL_COMMENT_RE
.sub('', asm
)
238 # Strip trailing whitespace.
239 asm
= common
.SCRUB_TRAILING_WHITESPACE_RE
.sub(r
'', asm
)
242 def scrub_asm_hexagon(asm
, args
):
243 # Scrub runs of whitespace out of the assembly, but leave the leading
244 # whitespace in place.
245 asm
= common
.SCRUB_WHITESPACE_RE
.sub(r
' ', asm
)
246 # Expand the tabs used for indentation.
247 asm
= string
.expandtabs(asm
, 2)
248 # Strip trailing whitespace.
249 asm
= common
.SCRUB_TRAILING_WHITESPACE_RE
.sub(r
'', asm
)
252 def scrub_asm_powerpc(asm
, args
):
253 # Scrub runs of whitespace out of the assembly, but leave the leading
254 # whitespace in place.
255 asm
= common
.SCRUB_WHITESPACE_RE
.sub(r
' ', asm
)
256 # Expand the tabs used for indentation.
257 asm
= string
.expandtabs(asm
, 2)
258 # Strip unimportant comments, but leave the token '#' in place.
259 asm
= common
.SCRUB_LOOP_COMMENT_RE
.sub(r
'#', asm
)
260 # Strip trailing whitespace.
261 asm
= common
.SCRUB_TRAILING_WHITESPACE_RE
.sub(r
'', asm
)
262 # Strip the tailing token '#', except the line only has token '#'.
263 asm
= common
.SCRUB_TAILING_COMMENT_TOKEN_RE
.sub(r
'', asm
)
266 def scrub_asm_m68k(asm
, args
):
267 # Scrub runs of whitespace out of the assembly, but leave the leading
268 # whitespace in place.
269 asm
= common
.SCRUB_WHITESPACE_RE
.sub(r
' ', asm
)
270 # Expand the tabs used for indentation.
271 asm
= string
.expandtabs(asm
, 2)
272 # Strip trailing whitespace.
273 asm
= common
.SCRUB_TRAILING_WHITESPACE_RE
.sub(r
'', asm
)
276 def scrub_asm_mips(asm
, args
):
277 # Scrub runs of whitespace out of the assembly, but leave the leading
278 # whitespace in place.
279 asm
= common
.SCRUB_WHITESPACE_RE
.sub(r
' ', asm
)
280 # Expand the tabs used for indentation.
281 asm
= string
.expandtabs(asm
, 2)
282 # Strip trailing whitespace.
283 asm
= common
.SCRUB_TRAILING_WHITESPACE_RE
.sub(r
'', asm
)
286 def scrub_asm_msp430(asm
, args
):
287 # Scrub runs of whitespace out of the assembly, but leave the leading
288 # whitespace in place.
289 asm
= common
.SCRUB_WHITESPACE_RE
.sub(r
' ', asm
)
290 # Expand the tabs used for indentation.
291 asm
= string
.expandtabs(asm
, 2)
292 # Strip trailing whitespace.
293 asm
= common
.SCRUB_TRAILING_WHITESPACE_RE
.sub(r
'', asm
)
296 def scrub_asm_avr(asm
, args
):
297 # Scrub runs of whitespace out of the assembly, but leave the leading
298 # whitespace in place.
299 asm
= common
.SCRUB_WHITESPACE_RE
.sub(r
' ', asm
)
300 # Expand the tabs used for indentation.
301 asm
= string
.expandtabs(asm
, 2)
302 # Strip trailing whitespace.
303 asm
= common
.SCRUB_TRAILING_WHITESPACE_RE
.sub(r
'', asm
)
306 def scrub_asm_riscv(asm
, args
):
307 # Scrub runs of whitespace out of the assembly, but leave the leading
308 # whitespace in place.
309 asm
= common
.SCRUB_WHITESPACE_RE
.sub(r
' ', asm
)
310 # Expand the tabs used for indentation.
311 asm
= string
.expandtabs(asm
, 2)
312 # Strip trailing whitespace.
313 asm
= common
.SCRUB_TRAILING_WHITESPACE_RE
.sub(r
'', asm
)
316 def scrub_asm_lanai(asm
, args
):
317 # Scrub runs of whitespace out of the assembly, but leave the leading
318 # whitespace in place.
319 asm
= common
.SCRUB_WHITESPACE_RE
.sub(r
' ', asm
)
320 # Expand the tabs used for indentation.
321 asm
= string
.expandtabs(asm
, 2)
322 # Strip trailing whitespace.
323 asm
= common
.SCRUB_TRAILING_WHITESPACE_RE
.sub(r
'', asm
)
326 def scrub_asm_sparc(asm
, args
):
327 # Scrub runs of whitespace out of the assembly, but leave the leading
328 # whitespace in place.
329 asm
= common
.SCRUB_WHITESPACE_RE
.sub(r
' ', asm
)
330 # Expand the tabs used for indentation.
331 asm
= string
.expandtabs(asm
, 2)
332 # Strip trailing whitespace.
333 asm
= common
.SCRUB_TRAILING_WHITESPACE_RE
.sub(r
'', asm
)
336 def scrub_asm_systemz(asm
, args
):
337 # Scrub runs of whitespace out of the assembly, but leave the leading
338 # whitespace in place.
339 asm
= common
.SCRUB_WHITESPACE_RE
.sub(r
' ', asm
)
340 # Expand the tabs used for indentation.
341 asm
= string
.expandtabs(asm
, 2)
342 # Strip trailing whitespace.
343 asm
= common
.SCRUB_TRAILING_WHITESPACE_RE
.sub(r
'', asm
)
346 def scrub_asm_wasm32(asm
, args
):
347 # Scrub runs of whitespace out of the assembly, but leave the leading
348 # whitespace in place.
349 asm
= common
.SCRUB_WHITESPACE_RE
.sub(r
' ', asm
)
350 # Expand the tabs used for indentation.
351 asm
= string
.expandtabs(asm
, 2)
352 # Strip trailing whitespace.
353 asm
= common
.SCRUB_TRAILING_WHITESPACE_RE
.sub(r
'', asm
)
356 def get_triple_from_march(march
):
362 'hexagon': 'hexagon',
364 for prefix
, triple
in triples
.items():
365 if march
.startswith(prefix
):
367 print("Cannot find a triple. Assume 'x86'", file=sys
.stderr
)
370 def get_run_handler(triple
):
372 'i686': (scrub_asm_x86
, ASM_FUNCTION_X86_RE
),
373 'x86': (scrub_asm_x86
, ASM_FUNCTION_X86_RE
),
374 'i386': (scrub_asm_x86
, ASM_FUNCTION_X86_RE
),
375 'arm64_32-apple-ios': (scrub_asm_arm_eabi
, ASM_FUNCTION_AARCH64_DARWIN_RE
),
376 'aarch64': (scrub_asm_arm_eabi
, ASM_FUNCTION_AARCH64_RE
),
377 'aarch64-apple-darwin': (scrub_asm_arm_eabi
, ASM_FUNCTION_AARCH64_DARWIN_RE
),
378 'aarch64-apple-ios': (scrub_asm_arm_eabi
, ASM_FUNCTION_AARCH64_DARWIN_RE
),
379 'hexagon': (scrub_asm_hexagon
, ASM_FUNCTION_HEXAGON_RE
),
380 'r600': (scrub_asm_amdgpu
, ASM_FUNCTION_AMDGPU_RE
),
381 'amdgcn': (scrub_asm_amdgpu
, ASM_FUNCTION_AMDGPU_RE
),
382 'arm': (scrub_asm_arm_eabi
, ASM_FUNCTION_ARM_RE
),
383 'arm64': (scrub_asm_arm_eabi
, ASM_FUNCTION_AARCH64_RE
),
384 'arm64e': (scrub_asm_arm_eabi
, ASM_FUNCTION_AARCH64_DARWIN_RE
),
385 'arm64-apple-ios': (scrub_asm_arm_eabi
, ASM_FUNCTION_AARCH64_DARWIN_RE
),
386 'armv7-apple-ios' : (scrub_asm_arm_eabi
, ASM_FUNCTION_ARM_IOS_RE
),
387 'armv7-apple-darwin': (scrub_asm_arm_eabi
, ASM_FUNCTION_ARM_DARWIN_RE
),
388 'thumb': (scrub_asm_arm_eabi
, ASM_FUNCTION_ARM_RE
),
389 'thumb-macho': (scrub_asm_arm_eabi
, ASM_FUNCTION_ARM_MACHO_RE
),
390 'thumbv5-macho': (scrub_asm_arm_eabi
, ASM_FUNCTION_ARM_MACHO_RE
),
391 'thumbv7s-apple-darwin' : (scrub_asm_arm_eabi
, ASM_FUNCTION_THUMBS_DARWIN_RE
),
392 'thumbv7-apple-darwin' : (scrub_asm_arm_eabi
, ASM_FUNCTION_THUMB_DARWIN_RE
),
393 'thumbv7-apple-ios' : (scrub_asm_arm_eabi
, ASM_FUNCTION_ARM_IOS_RE
),
394 'm68k': (scrub_asm_m68k
, ASM_FUNCTION_M68K_RE
),
395 'mips': (scrub_asm_mips
, ASM_FUNCTION_MIPS_RE
),
396 'msp430': (scrub_asm_msp430
, ASM_FUNCTION_MSP430_RE
),
397 'avr': (scrub_asm_avr
, ASM_FUNCTION_AVR_RE
),
398 'ppc32': (scrub_asm_powerpc
, ASM_FUNCTION_PPC_RE
),
399 'powerpc': (scrub_asm_powerpc
, ASM_FUNCTION_PPC_RE
),
400 'riscv32': (scrub_asm_riscv
, ASM_FUNCTION_RISCV_RE
),
401 'riscv64': (scrub_asm_riscv
, ASM_FUNCTION_RISCV_RE
),
402 'lanai': (scrub_asm_lanai
, ASM_FUNCTION_LANAI_RE
),
403 'sparc': (scrub_asm_sparc
, ASM_FUNCTION_SPARC_RE
),
404 's390x': (scrub_asm_systemz
, ASM_FUNCTION_SYSTEMZ_RE
),
405 'wasm32': (scrub_asm_wasm32
, ASM_FUNCTION_WASM32_RE
),
409 for prefix
, s
in target_handlers
.items():
410 if triple
.startswith(prefix
) and len(prefix
) > len(best_prefix
):
415 raise KeyError('Triple %r is not supported' % (triple
))
419 ##### Generator of assembly CHECK lines
421 def add_asm_checks(output_lines
, comment_marker
, prefix_list
, func_dict
, func_name
):
422 # Label format is based on ASM string.
423 check_label_format
= '{} %s-LABEL: %s%s:'.format(comment_marker
)
424 global_vars_seen_dict
= {}
425 common
.add_checks(output_lines
, comment_marker
, prefix_list
, func_dict
, func_name
, check_label_format
, True, False, global_vars_seen_dict
)