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)\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))
22
23ASM_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))
29
30ASM_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))
37
38ASM_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))
44
45ASM_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))
51
52ASM_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))
61
62ASM_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))
67
68ASM_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))
79
80ASM_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))
85
86ASM_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))
92
93ASM_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))
98
99ASM_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))
105
106ASM_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))
113
114ASM_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 ))
121
122ASM_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))
128
129ASM_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))
136
137ASM_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))
142
143SCRUB_X86_SHUFFLES_RE = (
144    re.compile(
145        r'^(\s*\w+) [^#\n]+#+ ((?:[xyz]mm\d+|mem)( \{%k\d+\}( \{z\})?)? = .*)$',
146        flags=re.M))
147
148SCRUB_X86_SHUFFLES_NO_MEM_RE = (
149    re.compile(
150        r'^(\s*\w+) [^#\n]+#+ ((?:[xyz]mm\d+|mem)( \{%k\d+\}( \{z\})?)? = (?!.*(?:mem)).*)$',
151        flags=re.M))
152
153SCRUB_X86_SPILL_RELOAD_RE = (
154    re.compile(
155        r'-?\d+\(%([er])[sb]p\)(.*(?:Spill|Reload))$',
156        flags=re.M))
157SCRUB_X86_SP_RE = re.compile(r'\d+\(%(esp|rsp)\)')
158SCRUB_X86_RIP_RE = re.compile(r'[.\w]+\(%rip\)')
159SCRUB_X86_LCP_RE = re.compile(r'\.LCPI[0-9]+_[0-9]+')
160SCRUB_X86_RET_RE = re.compile(r'ret[l|q]')
161
162def scrub_asm_x86(asm, args):
163  # Scrub runs of whitespace out of the assembly, but leave the leading
164  # whitespace in place.
165  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
166  # Expand the tabs used for indentation.
167  asm = string.expandtabs(asm, 2)
168
169  # Detect shuffle asm comments and hide the operands in favor of the comments.
170  if getattr(args, 'no_x86_scrub_mem_shuffle', True):
171    asm = SCRUB_X86_SHUFFLES_NO_MEM_RE.sub(r'\1 {{.*#+}} \2', asm)
172  else:
173    asm = SCRUB_X86_SHUFFLES_RE.sub(r'\1 {{.*#+}} \2', asm)
174
175  # Detect stack spills and reloads and hide their exact offset and whether
176  # they used the stack pointer or frame pointer.
177  asm = SCRUB_X86_SPILL_RELOAD_RE.sub(r'{{[-0-9]+}}(%\1{{[sb]}}p)\2', asm)
178  # Generically match the stack offset of a memory operand.
179  asm = SCRUB_X86_SP_RE.sub(r'{{[0-9]+}}(%\1)', asm)
180  if getattr(args, 'x86_scrub_rip', False):
181    # Generically match a RIP-relative memory operand.
182    asm = SCRUB_X86_RIP_RE.sub(r'{{.*}}(%rip)', asm)
183  # Generically match a LCP symbol.
184  asm = SCRUB_X86_LCP_RE.sub(r'{{\.LCPI.*}}', asm)
185  if getattr(args, 'extra_scrub', False):
186    # Avoid generating different checks for 32- and 64-bit because of 'retl' vs 'retq'.
187    asm = SCRUB_X86_RET_RE.sub(r'ret{{[l|q]}}', asm)
188  # Strip kill operands inserted into the asm.
189  asm = common.SCRUB_KILL_COMMENT_RE.sub('', asm)
190  # Strip trailing whitespace.
191  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
192  return asm
193
194def scrub_asm_amdgpu(asm, args):
195  # Scrub runs of whitespace out of the assembly, but leave the leading
196  # whitespace in place.
197  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
198  # Expand the tabs used for indentation.
199  asm = string.expandtabs(asm, 2)
200  # Strip trailing whitespace.
201  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
202  return asm
203
204def scrub_asm_arm_eabi(asm, args):
205  # Scrub runs of whitespace out of the assembly, but leave the leading
206  # whitespace in place.
207  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
208  # Expand the tabs used for indentation.
209  asm = string.expandtabs(asm, 2)
210  # Strip kill operands inserted into the asm.
211  asm = common.SCRUB_KILL_COMMENT_RE.sub('', asm)
212  # Strip trailing whitespace.
213  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
214  return asm
215
216def scrub_asm_hexagon(asm, args):
217  # Scrub runs of whitespace out of the assembly, but leave the leading
218  # whitespace in place.
219  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
220  # Expand the tabs used for indentation.
221  asm = string.expandtabs(asm, 2)
222  # Strip trailing whitespace.
223  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
224  return asm
225
226def scrub_asm_powerpc(asm, args):
227  # Scrub runs of whitespace out of the assembly, but leave the leading
228  # whitespace in place.
229  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
230  # Expand the tabs used for indentation.
231  asm = string.expandtabs(asm, 2)
232  # Strip unimportant comments, but leave the token '#' in place.
233  asm = common.SCRUB_LOOP_COMMENT_RE.sub(r'#', asm)
234  # Strip trailing whitespace.
235  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
236  # Strip the tailing token '#', except the line only has token '#'.
237  asm = common.SCRUB_TAILING_COMMENT_TOKEN_RE.sub(r'', asm)
238  return asm
239
240def scrub_asm_mips(asm, args):
241  # Scrub runs of whitespace out of the assembly, but leave the leading
242  # whitespace in place.
243  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
244  # Expand the tabs used for indentation.
245  asm = string.expandtabs(asm, 2)
246  # Strip trailing whitespace.
247  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
248  return asm
249
250def scrub_asm_msp430(asm, args):
251  # Scrub runs of whitespace out of the assembly, but leave the leading
252  # whitespace in place.
253  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
254  # Expand the tabs used for indentation.
255  asm = string.expandtabs(asm, 2)
256  # Strip trailing whitespace.
257  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
258  return asm
259
260def scrub_asm_riscv(asm, args):
261  # Scrub runs of whitespace out of the assembly, but leave the leading
262  # whitespace in place.
263  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
264  # Expand the tabs used for indentation.
265  asm = string.expandtabs(asm, 2)
266  # Strip trailing whitespace.
267  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
268  return asm
269
270def scrub_asm_lanai(asm, args):
271  # Scrub runs of whitespace out of the assembly, but leave the leading
272  # whitespace in place.
273  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
274  # Expand the tabs used for indentation.
275  asm = string.expandtabs(asm, 2)
276  # Strip trailing whitespace.
277  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
278  return asm
279
280def scrub_asm_sparc(asm, args):
281  # Scrub runs of whitespace out of the assembly, but leave the leading
282  # whitespace in place.
283  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
284  # Expand the tabs used for indentation.
285  asm = string.expandtabs(asm, 2)
286  # Strip trailing whitespace.
287  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
288  return asm
289
290def scrub_asm_systemz(asm, args):
291  # Scrub runs of whitespace out of the assembly, but leave the leading
292  # whitespace in place.
293  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
294  # Expand the tabs used for indentation.
295  asm = string.expandtabs(asm, 2)
296  # Strip trailing whitespace.
297  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
298  return asm
299
300def scrub_asm_wasm32(asm, args):
301  # Scrub runs of whitespace out of the assembly, but leave the leading
302  # whitespace in place.
303  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
304  # Expand the tabs used for indentation.
305  asm = string.expandtabs(asm, 2)
306  # Strip trailing whitespace.
307  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
308  return asm
309
310def get_triple_from_march(march):
311  triples = {
312      'amdgcn': 'amdgcn',
313      'r600': 'r600',
314      'mips': 'mips',
315      'sparc': 'sparc',
316      'hexagon': 'hexagon',
317  }
318  for prefix, triple in triples.items():
319    if march.startswith(prefix):
320      return triple
321  print("Cannot find a triple. Assume 'x86'", file=sys.stderr)
322  return 'x86'
323
324def build_function_body_dictionary_for_triple(args, raw_tool_output, triple, prefixes, func_dict):
325  target_handlers = {
326      'i686': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
327      'x86': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
328      'i386': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
329      'aarch64': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_RE),
330      'aarch64-apple-darwin': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_DARWIN_RE),
331      'hexagon': (scrub_asm_hexagon, ASM_FUNCTION_HEXAGON_RE),
332      'r600': (scrub_asm_amdgpu, ASM_FUNCTION_AMDGPU_RE),
333      'amdgcn': (scrub_asm_amdgpu, ASM_FUNCTION_AMDGPU_RE),
334      'arm': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
335      'arm64': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_RE),
336      'arm64-apple-ios': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_DARWIN_RE),
337      'armv7-apple-ios' : (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_IOS_RE),
338      'armv7-apple-darwin': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_DARWIN_RE),
339      'thumb': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
340      'thumb-macho': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_MACHO_RE),
341      'thumbv5-macho': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_MACHO_RE),
342      'thumbv7-apple-ios' : (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_IOS_RE),
343      'mips': (scrub_asm_mips, ASM_FUNCTION_MIPS_RE),
344      'msp430': (scrub_asm_msp430, ASM_FUNCTION_MSP430_RE),
345      'ppc32': (scrub_asm_powerpc, ASM_FUNCTION_PPC_RE),
346      'powerpc': (scrub_asm_powerpc, ASM_FUNCTION_PPC_RE),
347      'riscv32': (scrub_asm_riscv, ASM_FUNCTION_RISCV_RE),
348      'riscv64': (scrub_asm_riscv, ASM_FUNCTION_RISCV_RE),
349      'lanai': (scrub_asm_lanai, ASM_FUNCTION_LANAI_RE),
350      'sparc': (scrub_asm_sparc, ASM_FUNCTION_SPARC_RE),
351      's390x': (scrub_asm_systemz, ASM_FUNCTION_SYSTEMZ_RE),
352      'wasm32': (scrub_asm_wasm32, ASM_FUNCTION_WASM32_RE),
353  }
354  handler = None
355  best_prefix = ''
356  for prefix, s in target_handlers.items():
357    if triple.startswith(prefix) and len(prefix) > len(best_prefix):
358      handler = s
359      best_prefix = prefix
360
361  if handler is None:
362    raise KeyError('Triple %r is not supported' % (triple))
363
364  scrubber, function_re = handler
365  common.build_function_body_dictionary(
366          function_re, scrubber, [args], raw_tool_output, prefixes,
367          func_dict, args.verbose, False, False)
368
369##### Generator of assembly CHECK lines
370
371def add_asm_checks(output_lines, comment_marker, prefix_list, func_dict, func_name):
372  # Label format is based on ASM string.
373  check_label_format = '{} %s-LABEL: %s%s:'.format(comment_marker)
374  common.add_checks(output_lines, comment_marker, prefix_list, func_dict, func_name, check_label_format, True, False)
375