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