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'.Lfunc_end[0-9]+:\n', 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_MIPS_RE = re.compile( 55 r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@"?(?P=func)"?\n[^:]*?' # f: (name of func) 56 r'(?:^[ \t]+\.(frame|f?mask|set).*?\n)+' # Mips+LLVM standard asm prologue 57 r'(?P<body>.*?)\n' # (body of the function) 58 # Mips+LLVM standard asm epilogue 59 r'(?:(^[ \t]+\.set[^\n]*?\n)*^[ \t]+\.end.*?\n)' 60 r'(\$|\.L)func_end[0-9]+:\n', # $func_end0: (mips32 - O32) or 61 # .Lfunc_end0: (mips64 - NewABI) 62 flags=(re.M | re.S)) 63 64ASM_FUNCTION_MSP430_RE = re.compile( 65 r'^_?(?P<func>[^:]+):[ \t]*;+[ \t]*@"?(?P=func)"?\n[^:]*?' 66 r'(?P<body>.*?)\n' 67 r'(\$|\.L)func_end[0-9]+:\n', # $func_end0: 68 flags=(re.M | re.S)) 69 70ASM_FUNCTION_AVR_RE = re.compile( 71 r'^_?(?P<func>[^:]+):[ \t]*;+[ \t]*@"?(?P=func)"?\n[^:]*?' 72 r'(?P<body>.*?)\n' 73 r'.Lfunc_end[0-9]+:\n', 74 flags=(re.M | re.S)) 75 76ASM_FUNCTION_PPC_RE = re.compile( 77 r'#[ \-\t]*Begin function (?P<func>[^.:]+)\n' 78 r'.*?' 79 r'^[_.]?(?P=func):(?:[ \t]*#+[ \t]*@"?(?P=func)"?)?\n' 80 r'(?:^[^#]*\n)*' 81 r'(?P<body>.*?)\n' 82 # This list is incomplete 83 r'(?:^[ \t]*(?:\.(?:long|quad|v?byte)[ \t]+[^\n]+)\n)*' 84 r'(?:\.Lfunc_end|L\.\.(?P=func))[0-9]+:\n', 85 flags=(re.M | re.S)) 86 87ASM_FUNCTION_RISCV_RE = re.compile( 88 r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@"?(?P=func)"?\n' 89 r'(?:\s*\.?L(?P=func)\$local:\n)?' # optional .L<func>$local: due to -fno-semantic-interposition 90 r'(?:\s*\.?Lfunc_begin[^:\n]*:\n)?[^:]*?' 91 r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*' 92 r'.Lfunc_end[0-9]+:\n', 93 flags=(re.M | re.S)) 94 95ASM_FUNCTION_LANAI_RE = re.compile( 96 r'^_?(?P<func>[^:]+):[ \t]*!+[ \t]*@"?(?P=func)"?\n' 97 r'(?:[ \t]+.cfi_startproc\n)?' # drop optional cfi noise 98 r'(?P<body>.*?)\s*' 99 r'.Lfunc_end[0-9]+:\n', 100 flags=(re.M | re.S)) 101 102ASM_FUNCTION_SPARC_RE = re.compile( 103 r'^_?(?P<func>[^:]+):[ \t]*!+[ \t]*@"?(?P=func)"?\n' 104 r'(?P<body>.*?)\s*' 105 r'.Lfunc_end[0-9]+:\n', 106 flags=(re.M | re.S)) 107 108ASM_FUNCTION_SYSTEMZ_RE = re.compile( 109 r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@"?(?P=func)"?\n' 110 r'[ \t]+.cfi_startproc\n' 111 r'(?P<body>.*?)\n' 112 r'.Lfunc_end[0-9]+:\n', 113 flags=(re.M | re.S)) 114 115ASM_FUNCTION_AARCH64_DARWIN_RE = re.compile( 116 r'^_(?P<func>[^:]+):[ \t]*;[ \t]@"?(?P=func)"?\n' 117 r'([ \t]*.cfi_startproc\n[\s]*)?' 118 r'(?P<body>.*?)' 119 r'([ \t]*.cfi_endproc\n[\s]*)?' 120 r'^[ \t]*;[ \t]--[ \t]End[ \t]function', 121 flags=(re.M | re.S)) 122 123ASM_FUNCTION_ARM_DARWIN_RE = re.compile( 124 r'^[ \t]*\.globl[ \t]*_(?P<func>[^ \t])[ \t]*@[ \t]--[ \t]Begin[ \t]function[ \t]"?(?P=func)"?' 125 r'(?P<directives>.*?)' 126 r'^_(?P=func):\n[ \t]*' 127 r'(?P<body>.*?)' 128 r'^[ \t]*@[ \t]--[ \t]End[ \t]function', 129 flags=(re.M | re.S )) 130 131ASM_FUNCTION_ARM_MACHO_RE = re.compile( 132 r'^_(?P<func>[^:]+):[ \t]*\n' 133 r'([ \t]*.cfi_startproc\n[ \t]*)?' 134 r'(?P<body>.*?)\n' 135 r'[ \t]*\.cfi_endproc\n', 136 flags=(re.M | re.S)) 137 138ASM_FUNCTION_ARM_IOS_RE = re.compile( 139 r'^_(?P<func>[^:]+):[ \t]*\n' 140 r'^Lfunc_begin(?P<id>[0-9][1-9]*):\n' 141 r'(?P<body>.*?)' 142 r'^Lfunc_end(?P=id):\n' 143 r'^[ \t]*@[ \t]--[ \t]End[ \t]function', 144 flags=(re.M | re.S)) 145 146ASM_FUNCTION_WASM32_RE = re.compile( 147 r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@"?(?P=func)"?\n' 148 r'(?P<body>.*?)\n' 149 r'^\s*(\.Lfunc_end[0-9]+:\n|end_function)', 150 flags=(re.M | re.S)) 151 152SCRUB_X86_SHUFFLES_RE = ( 153 re.compile( 154 r'^(\s*\w+) [^#\n]+#+ ((?:[xyz]mm\d+|mem)( \{%k\d+\}( \{z\})?)? = .*)$', 155 flags=re.M)) 156 157SCRUB_X86_SHUFFLES_NO_MEM_RE = ( 158 re.compile( 159 r'^(\s*\w+) [^#\n]+#+ ((?:[xyz]mm\d+|mem)( \{%k\d+\}( \{z\})?)? = (?!.*(?:mem)).*)$', 160 flags=re.M)) 161 162SCRUB_X86_SPILL_RELOAD_RE = ( 163 re.compile( 164 r'-?\d+\(%([er])[sb]p\)(.*(?:Spill|Reload))$', 165 flags=re.M)) 166SCRUB_X86_SP_RE = re.compile(r'\d+\(%(esp|rsp)\)') 167SCRUB_X86_RIP_RE = re.compile(r'[.\w]+\(%rip\)') 168SCRUB_X86_LCP_RE = re.compile(r'\.LCPI[0-9]+_[0-9]+') 169SCRUB_X86_RET_RE = re.compile(r'ret[l|q]') 170 171def scrub_asm_x86(asm, args): 172 # Scrub runs of whitespace out of the assembly, but leave the leading 173 # whitespace in place. 174 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) 175 # Expand the tabs used for indentation. 176 asm = string.expandtabs(asm, 2) 177 178 # Detect shuffle asm comments and hide the operands in favor of the comments. 179 if getattr(args, 'no_x86_scrub_mem_shuffle', True): 180 asm = SCRUB_X86_SHUFFLES_NO_MEM_RE.sub(r'\1 {{.*#+}} \2', asm) 181 else: 182 asm = SCRUB_X86_SHUFFLES_RE.sub(r'\1 {{.*#+}} \2', asm) 183 184 # Detect stack spills and reloads and hide their exact offset and whether 185 # they used the stack pointer or frame pointer. 186 asm = SCRUB_X86_SPILL_RELOAD_RE.sub(r'{{[-0-9]+}}(%\1{{[sb]}}p)\2', asm) 187 if getattr(args, 'x86_scrub_sp', True): 188 # Generically match the stack offset of a memory operand. 189 asm = SCRUB_X86_SP_RE.sub(r'{{[0-9]+}}(%\1)', asm) 190 if getattr(args, 'x86_scrub_rip', False): 191 # Generically match a RIP-relative memory operand. 192 asm = SCRUB_X86_RIP_RE.sub(r'{{.*}}(%rip)', asm) 193 # Generically match a LCP symbol. 194 asm = SCRUB_X86_LCP_RE.sub(r'{{\.LCPI.*}}', asm) 195 if getattr(args, 'extra_scrub', False): 196 # Avoid generating different checks for 32- and 64-bit because of 'retl' vs 'retq'. 197 asm = SCRUB_X86_RET_RE.sub(r'ret{{[l|q]}}', asm) 198 # Strip kill operands inserted into the asm. 199 asm = common.SCRUB_KILL_COMMENT_RE.sub('', asm) 200 # Strip trailing whitespace. 201 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) 202 return asm 203 204def scrub_asm_amdgpu(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 trailing whitespace. 211 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) 212 return asm 213 214def scrub_asm_arm_eabi(asm, args): 215 # Scrub runs of whitespace out of the assembly, but leave the leading 216 # whitespace in place. 217 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) 218 # Expand the tabs used for indentation. 219 asm = string.expandtabs(asm, 2) 220 # Strip kill operands inserted into the asm. 221 asm = common.SCRUB_KILL_COMMENT_RE.sub('', asm) 222 # Strip trailing whitespace. 223 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) 224 return asm 225 226def scrub_asm_hexagon(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 trailing whitespace. 233 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) 234 return asm 235 236def scrub_asm_powerpc(asm, args): 237 # Scrub runs of whitespace out of the assembly, but leave the leading 238 # whitespace in place. 239 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) 240 # Expand the tabs used for indentation. 241 asm = string.expandtabs(asm, 2) 242 # Strip unimportant comments, but leave the token '#' in place. 243 asm = common.SCRUB_LOOP_COMMENT_RE.sub(r'#', asm) 244 # Strip trailing whitespace. 245 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) 246 # Strip the tailing token '#', except the line only has token '#'. 247 asm = common.SCRUB_TAILING_COMMENT_TOKEN_RE.sub(r'', asm) 248 return asm 249 250def scrub_asm_mips(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_msp430(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_avr(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_riscv(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_lanai(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_sparc(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 scrub_asm_systemz(asm, args): 311 # Scrub runs of whitespace out of the assembly, but leave the leading 312 # whitespace in place. 313 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) 314 # Expand the tabs used for indentation. 315 asm = string.expandtabs(asm, 2) 316 # Strip trailing whitespace. 317 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) 318 return asm 319 320def scrub_asm_wasm32(asm, args): 321 # Scrub runs of whitespace out of the assembly, but leave the leading 322 # whitespace in place. 323 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) 324 # Expand the tabs used for indentation. 325 asm = string.expandtabs(asm, 2) 326 # Strip trailing whitespace. 327 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) 328 return asm 329 330def get_triple_from_march(march): 331 triples = { 332 'amdgcn': 'amdgcn', 333 'r600': 'r600', 334 'mips': 'mips', 335 'sparc': 'sparc', 336 'hexagon': 'hexagon', 337 } 338 for prefix, triple in triples.items(): 339 if march.startswith(prefix): 340 return triple 341 print("Cannot find a triple. Assume 'x86'", file=sys.stderr) 342 return 'x86' 343 344def get_run_handler(triple): 345 target_handlers = { 346 'i686': (scrub_asm_x86, ASM_FUNCTION_X86_RE), 347 'x86': (scrub_asm_x86, ASM_FUNCTION_X86_RE), 348 'i386': (scrub_asm_x86, ASM_FUNCTION_X86_RE), 349 'aarch64': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_RE), 350 'aarch64-apple-darwin': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_DARWIN_RE), 351 'hexagon': (scrub_asm_hexagon, ASM_FUNCTION_HEXAGON_RE), 352 'r600': (scrub_asm_amdgpu, ASM_FUNCTION_AMDGPU_RE), 353 'amdgcn': (scrub_asm_amdgpu, ASM_FUNCTION_AMDGPU_RE), 354 'arm': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), 355 'arm64': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_RE), 356 'arm64e': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_DARWIN_RE), 357 'arm64-apple-ios': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_DARWIN_RE), 358 'armv7-apple-ios' : (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_IOS_RE), 359 'armv7-apple-darwin': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_DARWIN_RE), 360 'thumb': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), 361 'thumb-macho': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_MACHO_RE), 362 'thumbv5-macho': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_MACHO_RE), 363 'thumbv7-apple-ios' : (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_IOS_RE), 364 'mips': (scrub_asm_mips, ASM_FUNCTION_MIPS_RE), 365 'msp430': (scrub_asm_msp430, ASM_FUNCTION_MSP430_RE), 366 'avr': (scrub_asm_avr, ASM_FUNCTION_AVR_RE), 367 'ppc32': (scrub_asm_powerpc, ASM_FUNCTION_PPC_RE), 368 'powerpc': (scrub_asm_powerpc, ASM_FUNCTION_PPC_RE), 369 'riscv32': (scrub_asm_riscv, ASM_FUNCTION_RISCV_RE), 370 'riscv64': (scrub_asm_riscv, ASM_FUNCTION_RISCV_RE), 371 'lanai': (scrub_asm_lanai, ASM_FUNCTION_LANAI_RE), 372 'sparc': (scrub_asm_sparc, ASM_FUNCTION_SPARC_RE), 373 's390x': (scrub_asm_systemz, ASM_FUNCTION_SYSTEMZ_RE), 374 'wasm32': (scrub_asm_wasm32, ASM_FUNCTION_WASM32_RE), 375 } 376 handler = None 377 best_prefix = '' 378 for prefix, s in target_handlers.items(): 379 if triple.startswith(prefix) and len(prefix) > len(best_prefix): 380 handler = s 381 best_prefix = prefix 382 383 if handler is None: 384 raise KeyError('Triple %r is not supported' % (triple)) 385 386 return handler 387 388##### Generator of assembly CHECK lines 389 390def add_asm_checks(output_lines, comment_marker, prefix_list, func_dict, func_name): 391 # Label format is based on ASM string. 392 check_label_format = '{} %s-LABEL: %s%s:'.format(comment_marker) 393 global_vars_seen_dict = {} 394 common.add_checks(output_lines, comment_marker, prefix_list, func_dict, func_name, check_label_format, True, False, global_vars_seen_dict) 395