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