xref: /llvm-project/llvm/utils/UpdateTestChecks/asm.py (revision ff2f4fcd518b7526dc4c0f1b374e7a34e6cf137e)
1import re
2import sys
3
4from . import common
5
6if sys.version_info[0] > 2:
7  class string:
8    expandtabs = str.expandtabs
9else:
10  import string
11
12# RegEx: this is where the magic happens.
13
14##### Assembly parser
15
16ASM_FUNCTION_X86_RE = re.compile(
17    r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n[^:]*?'
18    r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*'
19    r'^\s*(?:[^:\n]+?:\s*\n\s*\.size|\.cfi_endproc|\.globl|\.comm|\.(?:sub)?section|#+ -- End function)',
20    flags=(re.M | re.S))
21
22ASM_FUNCTION_ARM_RE = re.compile(
23        r'^(?P<func>[0-9a-zA-Z_]+):\n' # f: (name of function)
24        r'\s+\.fnstart\n' # .fnstart
25        r'(?P<body>.*?)\n' # (body of the function)
26        r'.Lfunc_end[0-9]+:', # .Lfunc_end0: or # -- End function
27        flags=(re.M | re.S))
28
29ASM_FUNCTION_AARCH64_RE = re.compile(
30     r'^_?(?P<func>[^:]+):[ \t]*\/\/[ \t]*@(?P=func)\n'
31     r'[ \t]+.cfi_startproc\n'
32     r'(?P<body>.*?)\n'
33     # This list is incomplete
34     r'.Lfunc_end[0-9]+:\n',
35     flags=(re.M | re.S))
36
37ASM_FUNCTION_MIPS_RE = re.compile(
38    r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n[^:]*?' # f: (name of func)
39    r'(?:^[ \t]+\.(frame|f?mask|set).*?\n)+'  # Mips+LLVM standard asm prologue
40    r'(?P<body>.*?)\n'                        # (body of the function)
41    r'(?:^[ \t]+\.(set|end).*?\n)+'           # Mips+LLVM standard asm epilogue
42    r'(\$|\.L)func_end[0-9]+:\n',             # $func_end0: (mips32 - O32) or
43                                              # .Lfunc_end0: (mips64 - NewABI)
44    flags=(re.M | re.S))
45
46ASM_FUNCTION_PPC_RE = re.compile(
47    r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n'
48    r'\.Lfunc_begin[0-9]+:\n'
49    r'(?:[ \t]+.cfi_startproc\n)?'
50    r'(?:\.Lfunc_[gl]ep[0-9]+:\n(?:[ \t]+.*?\n)*)*'
51    r'(?P<body>.*?)\n'
52    # This list is incomplete
53    r'(?:^[ \t]*(?:\.long[ \t]+[^\n]+|\.quad[ \t]+[^\n]+)\n)*'
54    r'.Lfunc_end[0-9]+:\n',
55    flags=(re.M | re.S))
56
57ASM_FUNCTION_RISCV_RE = re.compile(
58    r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n[^:]*?'
59    r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*'
60    r'.Lfunc_end[0-9]+:\n',
61    flags=(re.M | re.S))
62
63ASM_FUNCTION_SYSTEMZ_RE = re.compile(
64    r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n'
65    r'[ \t]+.cfi_startproc\n'
66    r'(?P<body>.*?)\n'
67    r'.Lfunc_end[0-9]+:\n',
68    flags=(re.M | re.S))
69
70
71SCRUB_LOOP_COMMENT_RE = re.compile(
72    r'# =>This Inner Loop Header:.*|# in Loop:.*', flags=re.M)
73
74SCRUB_X86_SHUFFLES_RE = (
75    re.compile(
76        r'^(\s*\w+) [^#\n]+#+ ((?:[xyz]mm\d+|mem)( \{%k\d+\}( \{z\})?)? = .*)$',
77        flags=re.M))
78SCRUB_X86_SPILL_RELOAD_RE = (
79    re.compile(
80        r'-?\d+\(%([er])[sb]p\)(.*(?:Spill|Reload))$',
81        flags=re.M))
82SCRUB_X86_SP_RE = re.compile(r'\d+\(%(esp|rsp)\)')
83SCRUB_X86_RIP_RE = re.compile(r'[.\w]+\(%rip\)')
84SCRUB_X86_LCP_RE = re.compile(r'\.LCPI[0-9]+_[0-9]+')
85SCRUB_X86_RET_RE = re.compile(r'ret[l|q]')
86
87def scrub_asm_x86(asm, args):
88  # Scrub runs of whitespace out of the assembly, but leave the leading
89  # whitespace in place.
90  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
91  # Expand the tabs used for indentation.
92  asm = string.expandtabs(asm, 2)
93  # Detect shuffle asm comments and hide the operands in favor of the comments.
94  asm = SCRUB_X86_SHUFFLES_RE.sub(r'\1 {{.*#+}} \2', asm)
95  # Detect stack spills and reloads and hide their exact offset and whether
96  # they used the stack pointer or frame pointer.
97  asm = SCRUB_X86_SPILL_RELOAD_RE.sub(r'{{[-0-9]+}}(%\1{{[sb]}}p)\2', asm)
98  # Generically match the stack offset of a memory operand.
99  asm = SCRUB_X86_SP_RE.sub(r'{{[0-9]+}}(%\1)', asm)
100  # Generically match a RIP-relative memory operand.
101  asm = SCRUB_X86_RIP_RE.sub(r'{{.*}}(%rip)', asm)
102  # Generically match a LCP symbol.
103  asm = SCRUB_X86_LCP_RE.sub(r'{{\.LCPI.*}}', asm)
104  if getattr(args, 'x86_extra_scrub', False):
105    # Avoid generating different checks for 32- and 64-bit because of 'retl' vs 'retq'.
106    asm = SCRUB_X86_RET_RE.sub(r'ret{{[l|q]}}', asm)
107  # Strip kill operands inserted into the asm.
108  asm = common.SCRUB_KILL_COMMENT_RE.sub('', asm)
109  # Strip trailing whitespace.
110  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
111  return asm
112
113def scrub_asm_arm_eabi(asm, args):
114  # Scrub runs of whitespace out of the assembly, but leave the leading
115  # whitespace in place.
116  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
117  # Expand the tabs used for indentation.
118  asm = string.expandtabs(asm, 2)
119  # Strip kill operands inserted into the asm.
120  asm = common.SCRUB_KILL_COMMENT_RE.sub('', asm)
121  # Strip trailing whitespace.
122  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
123  return asm
124
125def scrub_asm_powerpc64(asm, args):
126  # Scrub runs of whitespace out of the assembly, but leave the leading
127  # whitespace in place.
128  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
129  # Expand the tabs used for indentation.
130  asm = string.expandtabs(asm, 2)
131  # Stripe unimportant comments
132  asm = SCRUB_LOOP_COMMENT_RE.sub(r'', asm)
133  # Strip trailing whitespace.
134  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
135  return asm
136
137def scrub_asm_mips(asm, args):
138  # Scrub runs of whitespace out of the assembly, but leave the leading
139  # whitespace in place.
140  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
141  # Expand the tabs used for indentation.
142  asm = string.expandtabs(asm, 2)
143  # Strip trailing whitespace.
144  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
145  return asm
146
147def scrub_asm_riscv(asm, args):
148  # Scrub runs of whitespace out of the assembly, but leave the leading
149  # whitespace in place.
150  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
151  # Expand the tabs used for indentation.
152  asm = string.expandtabs(asm, 2)
153  # Strip trailing whitespace.
154  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
155  return asm
156
157def scrub_asm_systemz(asm, args):
158  # Scrub runs of whitespace out of the assembly, but leave the leading
159  # whitespace in place.
160  asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm)
161  # Expand the tabs used for indentation.
162  asm = string.expandtabs(asm, 2)
163  # Strip trailing whitespace.
164  asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
165  return asm
166
167
168def build_function_body_dictionary_for_triple(args, raw_tool_output, triple, prefixes, func_dict):
169  target_handlers = {
170      'x86_64': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
171      'i686': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
172      'x86': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
173      'i386': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
174      'aarch64': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_RE),
175      'arm-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
176      'thumb-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
177      'thumbv6': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
178      'thumbv6-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
179      'thumbv6t2': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
180      'thumbv6t2-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
181      'thumbv6m': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
182      'thumbv6m-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
183      'thumbv7': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
184      'thumbv7-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
185      'thumbv7m': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
186      'thumbv7m-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
187      'thumbv8-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
188      'thumbv8m.base': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
189      'thumbv8m.main': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
190      'armv6': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
191      'armv7': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
192      'armv7-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
193      'armeb-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
194      'armv7eb-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
195      'armv7eb': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
196      'mips': (scrub_asm_mips, ASM_FUNCTION_MIPS_RE),
197      'powerpc64': (scrub_asm_powerpc64, ASM_FUNCTION_PPC_RE),
198      'powerpc64le': (scrub_asm_powerpc64, ASM_FUNCTION_PPC_RE),
199      'riscv32': (scrub_asm_riscv, ASM_FUNCTION_RISCV_RE),
200      'riscv64': (scrub_asm_riscv, ASM_FUNCTION_RISCV_RE),
201      's390x': (scrub_asm_systemz, ASM_FUNCTION_SYSTEMZ_RE),
202  }
203  handlers = None
204  for prefix, s in target_handlers.items():
205    if triple.startswith(prefix):
206      handlers = s
207      break
208  else:
209    raise KeyError('Triple %r is not supported' % (triple))
210
211  scrubber, function_re = handlers
212  common.build_function_body_dictionary(
213          function_re, scrubber, [args], raw_tool_output, prefixes,
214          func_dict, args.verbose)
215
216##### Generator of assembly CHECK lines
217
218def add_asm_checks(output_lines, comment_marker, run_list, func_dict, func_name):
219  printed_prefixes = []
220  for p in run_list:
221    checkprefixes = p[0]
222    for checkprefix in checkprefixes:
223      if checkprefix in printed_prefixes:
224        break
225      # TODO func_dict[checkprefix] may be None, '' or not exist.
226      # Fix the call sites.
227      if func_name not in func_dict[checkprefix] or not func_dict[checkprefix][func_name]:
228        continue
229      # Add some space between different check prefixes.
230      if len(printed_prefixes) != 0:
231        output_lines.append(comment_marker)
232      printed_prefixes.append(checkprefix)
233      output_lines.append('%s %s-LABEL: %s:' % (comment_marker, checkprefix, func_name))
234      func_body = func_dict[checkprefix][func_name].splitlines()
235      output_lines.append('%s %s:       %s' % (comment_marker, checkprefix, func_body[0]))
236      for func_line in func_body[1:]:
237        output_lines.append('%s %s-NEXT:  %s' % (comment_marker, checkprefix, func_line))
238      # Add space between different check prefixes and the first line of code.
239      # output_lines.append(';')
240      break
241