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  # Stripe 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  return asm
237
238def scrub_asm_mips(asm, args):
239  # Scrub runs of whitespace out of the assembly, but leave the leading
240  # whitespace in place.
241  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
242  # Expand the tabs used for indentation.
243  asm = string.expandtabs(asm, 2)
244  # Strip trailing whitespace.
245  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
246  return asm
247
248def scrub_asm_msp430(asm, args):
249  # Scrub runs of whitespace out of the assembly, but leave the leading
250  # whitespace in place.
251  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
252  # Expand the tabs used for indentation.
253  asm = string.expandtabs(asm, 2)
254  # Strip trailing whitespace.
255  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
256  return asm
257
258def scrub_asm_riscv(asm, args):
259  # Scrub runs of whitespace out of the assembly, but leave the leading
260  # whitespace in place.
261  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
262  # Expand the tabs used for indentation.
263  asm = string.expandtabs(asm, 2)
264  # Strip trailing whitespace.
265  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
266  return asm
267
268def scrub_asm_lanai(asm, args):
269  # Scrub runs of whitespace out of the assembly, but leave the leading
270  # whitespace in place.
271  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
272  # Expand the tabs used for indentation.
273  asm = string.expandtabs(asm, 2)
274  # Strip trailing whitespace.
275  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
276  return asm
277
278def scrub_asm_sparc(asm, args):
279  # Scrub runs of whitespace out of the assembly, but leave the leading
280  # whitespace in place.
281  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
282  # Expand the tabs used for indentation.
283  asm = string.expandtabs(asm, 2)
284  # Strip trailing whitespace.
285  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
286  return asm
287
288def scrub_asm_systemz(asm, args):
289  # Scrub runs of whitespace out of the assembly, but leave the leading
290  # whitespace in place.
291  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
292  # Expand the tabs used for indentation.
293  asm = string.expandtabs(asm, 2)
294  # Strip trailing whitespace.
295  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
296  return asm
297
298def scrub_asm_wasm32(asm, args):
299  # Scrub runs of whitespace out of the assembly, but leave the leading
300  # whitespace in place.
301  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
302  # Expand the tabs used for indentation.
303  asm = string.expandtabs(asm, 2)
304  # Strip trailing whitespace.
305  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
306  return asm
307
308def get_triple_from_march(march):
309  triples = {
310      'amdgcn': 'amdgcn',
311      'r600': 'r600',
312      'mips': 'mips',
313      'sparc': 'sparc',
314      'hexagon': 'hexagon',
315  }
316  for prefix, triple in triples.items():
317    if march.startswith(prefix):
318      return triple
319  print("Cannot find a triple. Assume 'x86'", file=sys.stderr)
320  return 'x86'
321
322def build_function_body_dictionary_for_triple(args, raw_tool_output, triple, prefixes, func_dict):
323  target_handlers = {
324      'i686': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
325      'x86': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
326      'i386': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
327      'aarch64': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_RE),
328      'aarch64-apple-darwin': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_DARWIN_RE),
329      'hexagon': (scrub_asm_hexagon, ASM_FUNCTION_HEXAGON_RE),
330      'r600': (scrub_asm_amdgpu, ASM_FUNCTION_AMDGPU_RE),
331      'amdgcn': (scrub_asm_amdgpu, ASM_FUNCTION_AMDGPU_RE),
332      'arm': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
333      'arm64': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_RE),
334      'arm64-apple-ios': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_DARWIN_RE),
335      'armv7-apple-ios' : (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_IOS_RE),
336      'armv7-apple-darwin': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_DARWIN_RE),
337      'thumb': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
338      'thumb-macho': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_MACHO_RE),
339      'thumbv5-macho': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_MACHO_RE),
340      'thumbv7-apple-ios' : (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_IOS_RE),
341      'mips': (scrub_asm_mips, ASM_FUNCTION_MIPS_RE),
342      'msp430': (scrub_asm_msp430, ASM_FUNCTION_MSP430_RE),
343      'ppc32': (scrub_asm_powerpc, ASM_FUNCTION_PPC_RE),
344      'powerpc': (scrub_asm_powerpc, ASM_FUNCTION_PPC_RE),
345      'riscv32': (scrub_asm_riscv, ASM_FUNCTION_RISCV_RE),
346      'riscv64': (scrub_asm_riscv, ASM_FUNCTION_RISCV_RE),
347      'lanai': (scrub_asm_lanai, ASM_FUNCTION_LANAI_RE),
348      'sparc': (scrub_asm_sparc, ASM_FUNCTION_SPARC_RE),
349      's390x': (scrub_asm_systemz, ASM_FUNCTION_SYSTEMZ_RE),
350      'wasm32': (scrub_asm_wasm32, ASM_FUNCTION_WASM32_RE),
351  }
352  handler = None
353  best_prefix = ''
354  for prefix, s in target_handlers.items():
355    if triple.startswith(prefix) and len(prefix) > len(best_prefix):
356      handler = s
357      best_prefix = prefix
358
359  if handler is None:
360    raise KeyError('Triple %r is not supported' % (triple))
361
362  scrubber, function_re = handler
363  common.build_function_body_dictionary(
364          function_re, scrubber, [args], raw_tool_output, prefixes,
365          func_dict, args.verbose, False)
366
367##### Generator of assembly CHECK lines
368
369def add_asm_checks(output_lines, comment_marker, prefix_list, func_dict, func_name):
370  # Label format is based on ASM string.
371  check_label_format = '{} %s-LABEL: %s%s:'.format(comment_marker)
372  common.add_checks(output_lines, comment_marker, prefix_list, func_dict, func_name, check_label_format, True, False)
373