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