1from __future__ import print_function
2import re
3import sys
4
5from . import common
6
7if sys.version_info[0] > 2:
8  class string:
9    expandtabs = str.expandtabs
10else:
11  import string
12
13# RegEx: this is where the magic happens.
14
15##### Assembly parser
16
17ASM_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)',
23    flags=(re.M | re.S))
24
25ASM_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
30    flags=(re.M | re.S))
31
32ASM_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
35     r'(?P<body>.*?)\n'
36     # This list is incomplete
37     r'^\s*(\.Lfunc_end[0-9]+|// -- End function)',
38     flags=(re.M | re.S))
39
40ASM_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)',
45    flags=(re.M | re.S))
46
47ASM_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',
52    flags=(re.M | re.S))
53
54ASM_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',
58    flags=(re.M | re.S))
59
60ASM_FUNCTION_MIPS_RE = re.compile(
61    r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@"?(?P=func)"?\n[^:]*?' # f: (name of func)
62    r'(?:\s*\.?Ltmp[^:\n]*:\n)?[^:]*?'        # optional .Ltmp<N> for EH
63    r'(?:^[ \t]+\.(frame|f?mask|set).*?\n)+'  # Mips+LLVM standard asm prologue
64    r'(?P<body>.*?)\n'                        # (body of the function)
65    # Mips+LLVM standard asm epilogue
66    r'(?:(^[ \t]+\.set[^\n]*?\n)*^[ \t]+\.end.*?\n)'
67    r'(\$|\.L)func_end[0-9]+:\n',             # $func_end0: (mips32 - O32) or
68                                              # .Lfunc_end0: (mips64 - NewABI)
69    flags=(re.M | re.S))
70
71ASM_FUNCTION_MSP430_RE = re.compile(
72    r'^_?(?P<func>[^:]+):[ \t]*;+[ \t]*@"?(?P=func)"?\n[^:]*?'
73    r'(?P<body>.*?)\n'
74    r'(\$|\.L)func_end[0-9]+:\n',             # $func_end0:
75    flags=(re.M | re.S))
76
77ASM_FUNCTION_AVR_RE = re.compile(
78    r'^_?(?P<func>[^:]+):[ \t]*;+[ \t]*@"?(?P=func)"?\n[^:]*?'
79    r'(?P<body>.*?)\n'
80    r'.Lfunc_end[0-9]+:\n',
81    flags=(re.M | re.S))
82
83ASM_FUNCTION_PPC_RE = re.compile(
84    r'#[ \-\t]*Begin function (?P<func>[^.:]+)\n'
85    r'.*?'
86    r'^[_.]?(?P=func):(?:[ \t]*#+[ \t]*@"?(?P=func)"?)?\n'
87    r'(?:^[^#]*\n)*'
88    r'(?P<body>.*?)\n'
89    # This list is incomplete
90    r'(?:^[ \t]*(?:\.(?:long|quad|v?byte)[ \t]+[^\n]+)\n)*'
91    r'(?:\.Lfunc_end|L\.\.(?P=func))[0-9]+:\n',
92    flags=(re.M | re.S))
93
94ASM_FUNCTION_RISCV_RE = re.compile(
95    r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@"?(?P=func)"?\n'
96    r'(?:\s*\.?L(?P=func)\$local:\n)?'  # optional .L<func>$local: due to -fno-semantic-interposition
97    r'(?:\s*\.?Lfunc_begin[^:\n]*:\n)?[^:]*?'
98    r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*'
99    r'.Lfunc_end[0-9]+:\n',
100    flags=(re.M | re.S))
101
102ASM_FUNCTION_LANAI_RE = re.compile(
103    r'^_?(?P<func>[^:]+):[ \t]*!+[ \t]*@"?(?P=func)"?\n'
104    r'(?:[ \t]+.cfi_startproc\n)?'  # drop optional cfi noise
105    r'(?P<body>.*?)\s*'
106    r'.Lfunc_end[0-9]+:\n',
107    flags=(re.M | re.S))
108
109ASM_FUNCTION_SPARC_RE = re.compile(
110    r'^_?(?P<func>[^:]+):[ \t]*!+[ \t]*@"?(?P=func)"?\n'
111    r'(?P<body>.*?)\s*'
112    r'.Lfunc_end[0-9]+:\n',
113    flags=(re.M | re.S))
114
115ASM_FUNCTION_SYSTEMZ_RE = re.compile(
116    r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@"?(?P=func)"?\n'
117    r'[ \t]+.cfi_startproc\n'
118    r'(?P<body>.*?)\n'
119    r'.Lfunc_end[0-9]+:\n',
120    flags=(re.M | re.S))
121
122ASM_FUNCTION_AARCH64_DARWIN_RE = re.compile(
123    r'^_(?P<func>[^:]+):[ \t]*;[ \t]@"?(?P=func)"?\n'
124    r'([ \t]*.cfi_startproc\n[\s]*)?'
125    r'(?P<body>.*?)'
126    r'([ \t]*.cfi_endproc\n[\s]*)?'
127    r'^[ \t]*;[ \t]--[ \t]End[ \t]function',
128    flags=(re.M | re.S))
129
130ASM_FUNCTION_ARM_DARWIN_RE = re.compile(
131    r'^[ \t]*\.globl[ \t]*_(?P<func>[^ \t])[ \t]*@[ \t]--[ \t]Begin[ \t]function[ \t]"?(?P=func)"?'
132    r'(?P<directives>.*?)'
133    r'^_(?P=func):\n[ \t]*'
134    r'(?P<body>.*?)'
135    r'^[ \t]*@[ \t]--[ \t]End[ \t]function',
136    flags=(re.M | re.S ))
137
138ASM_FUNCTION_ARM_MACHO_RE = re.compile(
139    r'^_(?P<func>[^:]+):[ \t]*\n'
140    r'([ \t]*.cfi_startproc\n[ \t]*)?'
141    r'(?P<body>.*?)\n'
142    r'[ \t]*\.cfi_endproc\n',
143    flags=(re.M | re.S))
144
145ASM_FUNCTION_THUMBS_DARWIN_RE = re.compile(
146    r'^_(?P<func>[^:]+):\n'
147    r'(?P<body>.*?)\n'
148    r'[ \t]*\.data_region\n',
149    flags=(re.M | re.S))
150
151ASM_FUNCTION_THUMB_DARWIN_RE = re.compile(
152    r'^_(?P<func>[^:]+):\n'
153    r'(?P<body>.*?)\n'
154    r'^[ \t]*@[ \t]--[ \t]End[ \t]function',
155    flags=(re.M | re.S))
156
157ASM_FUNCTION_ARM_IOS_RE = re.compile(
158    r'^_(?P<func>[^:]+):\n'
159    r'(?P<body>.*?)'
160    r'^[ \t]*@[ \t]--[ \t]End[ \t]function',
161    flags=(re.M | re.S))
162
163ASM_FUNCTION_WASM32_RE = re.compile(
164    r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@"?(?P=func)"?\n'
165    r'(?P<body>.*?)\n'
166    r'^\s*(\.Lfunc_end[0-9]+:\n|end_function)',
167    flags=(re.M | re.S))
168
169ASM_FUNCTION_VE_RE = re.compile(
170    r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n'
171    r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*'
172    r'.Lfunc_end[0-9]+:\n',
173    flags=(re.M | re.S))
174
175ASM_FUNCTION_CSKY_RE = re.compile(
176    r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n(?:\s*\.?Lfunc_begin[^:\n]*:\n)?[^:]*?'
177    r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*'
178    r'.Lfunc_end[0-9]+:\n',
179    flags=(re.M | re.S))
180
181ASM_FUNCTION_NVPTX_RE = re.compile(
182    # function attributes and retval
183    # .visible .func (.param .align 16 .b8 func_retval0[32])
184    #r'^(\.visible\s+)?\.func\s+(\([^\)]*\)\s*)?'
185    r'^(\.(func|visible|weak|entry|noreturn|extern)\s+)+(\([^\)]*\)\s*)?'
186
187    # function name
188    r'(?P<func>[^\(\n]+)'
189
190    # function name separator (opening brace)
191    r'(?P<func_name_separator>\()'
192
193    # function parameters
194    # (
195    #   .param .align 16 .b8 callee_St8x4_param_0[32]
196    # ) // -- Begin function callee_St8x4
197    r'[^\)]*\)(\s*//[^\n]*)?\n'
198
199    # function body
200    r'(?P<body>.*?)\n'
201
202    # function body end marker
203    r'\s*// -- End function',
204    flags=(re.M | re.S))
205
206ASM_FUNCTION_LOONGARCH_RE = re.compile(
207    r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@"?(?P=func)"?\n'
208    r'(?:\s*\.?Lfunc_begin[^:\n]*:\n)?[^:]*?'
209    r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*'
210    r'.Lfunc_end[0-9]+:\n',
211    flags=(re.M | re.S))
212
213SCRUB_X86_SHUFFLES_RE = (
214    re.compile(
215        r'^(\s*\w+) [^#\n]+#+ ((?:[xyz]mm\d+|mem)( \{%k\d+\}( \{z\})?)? = .*)$',
216        flags=re.M))
217
218SCRUB_X86_SHUFFLES_NO_MEM_RE = (
219    re.compile(
220        r'^(\s*\w+) [^#\n]+#+ ((?:[xyz]mm\d+|mem)( \{%k\d+\}( \{z\})?)? = (?!.*(?:mem)).*)$',
221        flags=re.M))
222
223SCRUB_X86_SPILL_RELOAD_RE = (
224    re.compile(
225        r'-?\d+\(%([er])[sb]p\)(.*(?:Spill|Reload))$',
226        flags=re.M))
227SCRUB_X86_SP_RE = re.compile(r'\d+\(%(esp|rsp)\)')
228SCRUB_X86_RIP_RE = re.compile(r'[.\w]+\(%rip\)')
229SCRUB_X86_LCP_RE = re.compile(r'\.?LCPI[0-9]+_[0-9]+')
230SCRUB_X86_RET_RE = re.compile(r'ret[l|q]')
231
232def scrub_asm_x86(asm, args):
233  # Scrub runs of whitespace out of the assembly, but leave the leading
234  # whitespace in place.
235  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
236  # Expand the tabs used for indentation.
237  asm = string.expandtabs(asm, 2)
238
239  # Detect shuffle asm comments and hide the operands in favor of the comments.
240  if getattr(args, 'no_x86_scrub_mem_shuffle', True):
241    asm = SCRUB_X86_SHUFFLES_NO_MEM_RE.sub(r'\1 {{.*#+}} \2', asm)
242  else:
243    asm = SCRUB_X86_SHUFFLES_RE.sub(r'\1 {{.*#+}} \2', asm)
244
245  # Detect stack spills and reloads and hide their exact offset and whether
246  # they used the stack pointer or frame pointer.
247  asm = SCRUB_X86_SPILL_RELOAD_RE.sub(r'{{[-0-9]+}}(%\1{{[sb]}}p)\2', asm)
248  if getattr(args, 'x86_scrub_sp', True):
249    # Generically match the stack offset of a memory operand.
250    asm = SCRUB_X86_SP_RE.sub(r'{{[0-9]+}}(%\1)', asm)
251  if getattr(args, 'x86_scrub_rip', False):
252    # Generically match a RIP-relative memory operand.
253    asm = SCRUB_X86_RIP_RE.sub(r'{{.*}}(%rip)', asm)
254  # Generically match a LCP symbol.
255  asm = SCRUB_X86_LCP_RE.sub(r'{{\.?LCPI[0-9]+_[0-9]+}}', asm)
256  if getattr(args, 'extra_scrub', False):
257    # Avoid generating different checks for 32- and 64-bit because of 'retl' vs 'retq'.
258    asm = SCRUB_X86_RET_RE.sub(r'ret{{[l|q]}}', asm)
259  # Strip kill operands inserted into the asm.
260  asm = common.SCRUB_KILL_COMMENT_RE.sub('', asm)
261  # Strip trailing whitespace.
262  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
263  return asm
264
265def scrub_asm_amdgpu(asm, args):
266  # Scrub runs of whitespace out of the assembly, but leave the leading
267  # whitespace in place.
268  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
269  # Expand the tabs used for indentation.
270  asm = string.expandtabs(asm, 2)
271  # Strip trailing whitespace.
272  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
273  return asm
274
275def scrub_asm_arm_eabi(asm, args):
276  # Scrub runs of whitespace out of the assembly, but leave the leading
277  # whitespace in place.
278  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
279  # Expand the tabs used for indentation.
280  asm = string.expandtabs(asm, 2)
281  # Strip kill operands inserted into the asm.
282  asm = common.SCRUB_KILL_COMMENT_RE.sub('', asm)
283  # Strip trailing whitespace.
284  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
285  return asm
286
287def scrub_asm_hexagon(asm, args):
288  # Scrub runs of whitespace out of the assembly, but leave the leading
289  # whitespace in place.
290  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
291  # Expand the tabs used for indentation.
292  asm = string.expandtabs(asm, 2)
293  # Strip trailing whitespace.
294  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
295  return asm
296
297def scrub_asm_powerpc(asm, args):
298  # Scrub runs of whitespace out of the assembly, but leave the leading
299  # whitespace in place.
300  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
301  # Expand the tabs used for indentation.
302  asm = string.expandtabs(asm, 2)
303  # Strip unimportant comments, but leave the token '#' in place.
304  asm = common.SCRUB_LOOP_COMMENT_RE.sub(r'#', asm)
305  # Strip trailing whitespace.
306  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
307  # Strip the tailing token '#', except the line only has token '#'.
308  asm = common.SCRUB_TAILING_COMMENT_TOKEN_RE.sub(r'', asm)
309  return asm
310
311def scrub_asm_m68k(asm, args):
312  # Scrub runs of whitespace out of the assembly, but leave the leading
313  # whitespace in place.
314  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
315  # Expand the tabs used for indentation.
316  asm = string.expandtabs(asm, 2)
317  # Strip trailing whitespace.
318  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
319  return asm
320
321def scrub_asm_mips(asm, args):
322  # Scrub runs of whitespace out of the assembly, but leave the leading
323  # whitespace in place.
324  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
325  # Expand the tabs used for indentation.
326  asm = string.expandtabs(asm, 2)
327  # Strip trailing whitespace.
328  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
329  return asm
330
331def scrub_asm_msp430(asm, args):
332  # Scrub runs of whitespace out of the assembly, but leave the leading
333  # whitespace in place.
334  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
335  # Expand the tabs used for indentation.
336  asm = string.expandtabs(asm, 2)
337  # Strip trailing whitespace.
338  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
339  return asm
340
341def scrub_asm_avr(asm, args):
342  # Scrub runs of whitespace out of the assembly, but leave the leading
343  # whitespace in place.
344  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
345  # Expand the tabs used for indentation.
346  asm = string.expandtabs(asm, 2)
347  # Strip trailing whitespace.
348  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
349  return asm
350
351def scrub_asm_riscv(asm, args):
352  # Scrub runs of whitespace out of the assembly, but leave the leading
353  # whitespace in place.
354  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
355  # Expand the tabs used for indentation.
356  asm = string.expandtabs(asm, 2)
357  # Strip trailing whitespace.
358  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
359  return asm
360
361def scrub_asm_lanai(asm, args):
362  # Scrub runs of whitespace out of the assembly, but leave the leading
363  # whitespace in place.
364  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
365  # Expand the tabs used for indentation.
366  asm = string.expandtabs(asm, 2)
367  # Strip trailing whitespace.
368  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
369  return asm
370
371def scrub_asm_sparc(asm, args):
372  # Scrub runs of whitespace out of the assembly, but leave the leading
373  # whitespace in place.
374  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
375  # Expand the tabs used for indentation.
376  asm = string.expandtabs(asm, 2)
377  # Strip trailing whitespace.
378  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
379  return asm
380
381def scrub_asm_systemz(asm, args):
382  # Scrub runs of whitespace out of the assembly, but leave the leading
383  # whitespace in place.
384  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
385  # Expand the tabs used for indentation.
386  asm = string.expandtabs(asm, 2)
387  # Strip trailing whitespace.
388  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
389  return asm
390
391def scrub_asm_wasm32(asm, args):
392  # Scrub runs of whitespace out of the assembly, but leave the leading
393  # whitespace in place.
394  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
395  # Expand the tabs used for indentation.
396  asm = string.expandtabs(asm, 2)
397  # Strip trailing whitespace.
398  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
399  return asm
400
401def scrub_asm_ve(asm, args):
402  # Scrub runs of whitespace out of the assembly, but leave the leading
403  # whitespace in place.
404  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
405  # Expand the tabs used for indentation.
406  asm = string.expandtabs(asm, 2)
407  # Strip trailing whitespace.
408  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
409  return asm
410
411def scrub_asm_csky(asm, args):
412  # Scrub runs of whitespace out of the assembly, but leave the leading
413  # whitespace in place.
414  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
415  # Expand the tabs used for indentation.
416  asm = string.expandtabs(asm, 2)
417  # Strip kill operands inserted into the asm.
418  asm = common.SCRUB_KILL_COMMENT_RE.sub('', asm)
419  # Strip trailing whitespace.
420  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
421  return asm
422
423def scrub_asm_nvptx(asm, args):
424  # Scrub runs of whitespace out of the assembly, but leave the leading
425  # whitespace in place.
426  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
427  # Expand the tabs used for indentation.
428  asm = string.expandtabs(asm, 2)
429  # Strip trailing whitespace.
430  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
431  return asm
432
433def scrub_asm_loongarch(asm, args):
434  # Scrub runs of whitespace out of the assembly, but leave the leading
435  # whitespace in place.
436  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
437  # Expand the tabs used for indentation.
438  asm = string.expandtabs(asm, 2)
439  # Strip trailing whitespace.
440  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
441  return asm
442
443# Returns a tuple of a scrub function and a function regex. Scrub function is
444# used to alter function body in some way, for example, remove trailing spaces.
445# Function regex is used to match function name, body, etc. in raw llc output.
446def get_run_handler(triple):
447  target_handlers = {
448      'i686': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
449      'x86': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
450      'i386': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
451      'arm64_32-apple-ios': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_DARWIN_RE),
452      'arm64_32-apple-watchos2.0.0': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_DARWIN_RE),
453      'aarch64': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_RE),
454      'aarch64-apple-darwin': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_DARWIN_RE),
455      'aarch64-apple-ios': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_DARWIN_RE),
456      'hexagon': (scrub_asm_hexagon, ASM_FUNCTION_HEXAGON_RE),
457      'r600': (scrub_asm_amdgpu, ASM_FUNCTION_AMDGPU_RE),
458      'amdgcn': (scrub_asm_amdgpu, ASM_FUNCTION_AMDGPU_RE),
459      'arm': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
460      'arm64': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_RE),
461      'arm64e': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_DARWIN_RE),
462      'arm64-apple-ios': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_DARWIN_RE),
463      'armv7-apple-ios' : (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_IOS_RE),
464      'armv7-apple-darwin': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_DARWIN_RE),
465      'thumb': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
466      'thumb-macho': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_MACHO_RE),
467      'thumbv5-macho': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_MACHO_RE),
468      'thumbv7s-apple-darwin' : (scrub_asm_arm_eabi, ASM_FUNCTION_THUMBS_DARWIN_RE),
469      'thumbv7-apple-darwin' : (scrub_asm_arm_eabi, ASM_FUNCTION_THUMB_DARWIN_RE),
470      'thumbv7-apple-ios' : (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_IOS_RE),
471      'm68k': (scrub_asm_m68k, ASM_FUNCTION_M68K_RE),
472      'mips': (scrub_asm_mips, ASM_FUNCTION_MIPS_RE),
473      'msp430': (scrub_asm_msp430, ASM_FUNCTION_MSP430_RE),
474      'avr': (scrub_asm_avr, ASM_FUNCTION_AVR_RE),
475      'ppc32': (scrub_asm_powerpc, ASM_FUNCTION_PPC_RE),
476      'powerpc': (scrub_asm_powerpc, ASM_FUNCTION_PPC_RE),
477      'riscv32': (scrub_asm_riscv, ASM_FUNCTION_RISCV_RE),
478      'riscv64': (scrub_asm_riscv, ASM_FUNCTION_RISCV_RE),
479      'lanai': (scrub_asm_lanai, ASM_FUNCTION_LANAI_RE),
480      'sparc': (scrub_asm_sparc, ASM_FUNCTION_SPARC_RE),
481      's390x': (scrub_asm_systemz, ASM_FUNCTION_SYSTEMZ_RE),
482      'wasm32': (scrub_asm_wasm32, ASM_FUNCTION_WASM32_RE),
483      've': (scrub_asm_ve, ASM_FUNCTION_VE_RE),
484      'csky': (scrub_asm_csky, ASM_FUNCTION_CSKY_RE),
485      'nvptx': (scrub_asm_nvptx, ASM_FUNCTION_NVPTX_RE),
486      'loongarch32': (scrub_asm_loongarch, ASM_FUNCTION_LOONGARCH_RE),
487      'loongarch64': (scrub_asm_loongarch, ASM_FUNCTION_LOONGARCH_RE)
488  }
489  handler = None
490  best_prefix = ''
491  for prefix, s in target_handlers.items():
492    if triple.startswith(prefix) and len(prefix) > len(best_prefix):
493      handler = s
494      best_prefix = prefix
495
496  if handler is None:
497    raise KeyError('Triple %r is not supported' % (triple))
498
499  return handler
500
501##### Generator of assembly CHECK lines
502
503def add_checks(output_lines, comment_marker, prefix_list, func_dict,
504               func_name, global_vars_seen_dict, is_filtered):
505  # Label format is based on ASM string.
506  check_label_format = '{} %s-LABEL: %s%s%s'.format(comment_marker)
507  return common.add_checks(output_lines, comment_marker, prefix_list, func_dict,
508                           func_name, check_label_format, True, False,
509                           global_vars_seen_dict, is_filtered=is_filtered)
510