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 if getattr(args, 'x86_scrub_sp', True): 181 # Generically match the stack offset of a memory operand. 182 asm = SCRUB_X86_SP_RE.sub(r'{{[0-9]+}}(%\1)', asm) 183 if getattr(args, 'x86_scrub_rip', False): 184 # Generically match a RIP-relative memory operand. 185 asm = SCRUB_X86_RIP_RE.sub(r'{{.*}}(%rip)', asm) 186 # Generically match a LCP symbol. 187 asm = SCRUB_X86_LCP_RE.sub(r'{{\.LCPI.*}}', asm) 188 if getattr(args, 'extra_scrub', False): 189 # Avoid generating different checks for 32- and 64-bit because of 'retl' vs 'retq'. 190 asm = SCRUB_X86_RET_RE.sub(r'ret{{[l|q]}}', asm) 191 # Strip kill operands inserted into the asm. 192 asm = common.SCRUB_KILL_COMMENT_RE.sub('', asm) 193 # Strip trailing whitespace. 194 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) 195 return asm 196 197def scrub_asm_amdgpu(asm, args): 198 # Scrub runs of whitespace out of the assembly, but leave the leading 199 # whitespace in place. 200 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) 201 # Expand the tabs used for indentation. 202 asm = string.expandtabs(asm, 2) 203 # Strip trailing whitespace. 204 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) 205 return asm 206 207def scrub_asm_arm_eabi(asm, args): 208 # Scrub runs of whitespace out of the assembly, but leave the leading 209 # whitespace in place. 210 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) 211 # Expand the tabs used for indentation. 212 asm = string.expandtabs(asm, 2) 213 # Strip kill operands inserted into the asm. 214 asm = common.SCRUB_KILL_COMMENT_RE.sub('', asm) 215 # Strip trailing whitespace. 216 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) 217 return asm 218 219def scrub_asm_hexagon(asm, args): 220 # Scrub runs of whitespace out of the assembly, but leave the leading 221 # whitespace in place. 222 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) 223 # Expand the tabs used for indentation. 224 asm = string.expandtabs(asm, 2) 225 # Strip trailing whitespace. 226 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) 227 return asm 228 229def scrub_asm_powerpc(asm, args): 230 # Scrub runs of whitespace out of the assembly, but leave the leading 231 # whitespace in place. 232 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) 233 # Expand the tabs used for indentation. 234 asm = string.expandtabs(asm, 2) 235 # Strip unimportant comments, but leave the token '#' in place. 236 asm = common.SCRUB_LOOP_COMMENT_RE.sub(r'#', asm) 237 # Strip trailing whitespace. 238 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) 239 # Strip the tailing token '#', except the line only has token '#'. 240 asm = common.SCRUB_TAILING_COMMENT_TOKEN_RE.sub(r'', asm) 241 return asm 242 243def scrub_asm_mips(asm, args): 244 # Scrub runs of whitespace out of the assembly, but leave the leading 245 # whitespace in place. 246 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) 247 # Expand the tabs used for indentation. 248 asm = string.expandtabs(asm, 2) 249 # Strip trailing whitespace. 250 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) 251 return asm 252 253def scrub_asm_msp430(asm, args): 254 # Scrub runs of whitespace out of the assembly, but leave the leading 255 # whitespace in place. 256 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) 257 # Expand the tabs used for indentation. 258 asm = string.expandtabs(asm, 2) 259 # Strip trailing whitespace. 260 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) 261 return asm 262 263def scrub_asm_riscv(asm, args): 264 # Scrub runs of whitespace out of the assembly, but leave the leading 265 # whitespace in place. 266 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) 267 # Expand the tabs used for indentation. 268 asm = string.expandtabs(asm, 2) 269 # Strip trailing whitespace. 270 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) 271 return asm 272 273def scrub_asm_lanai(asm, args): 274 # Scrub runs of whitespace out of the assembly, but leave the leading 275 # whitespace in place. 276 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) 277 # Expand the tabs used for indentation. 278 asm = string.expandtabs(asm, 2) 279 # Strip trailing whitespace. 280 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) 281 return asm 282 283def scrub_asm_sparc(asm, args): 284 # Scrub runs of whitespace out of the assembly, but leave the leading 285 # whitespace in place. 286 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) 287 # Expand the tabs used for indentation. 288 asm = string.expandtabs(asm, 2) 289 # Strip trailing whitespace. 290 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) 291 return asm 292 293def scrub_asm_systemz(asm, args): 294 # Scrub runs of whitespace out of the assembly, but leave the leading 295 # whitespace in place. 296 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) 297 # Expand the tabs used for indentation. 298 asm = string.expandtabs(asm, 2) 299 # Strip trailing whitespace. 300 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) 301 return asm 302 303def scrub_asm_wasm32(asm, args): 304 # Scrub runs of whitespace out of the assembly, but leave the leading 305 # whitespace in place. 306 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) 307 # Expand the tabs used for indentation. 308 asm = string.expandtabs(asm, 2) 309 # Strip trailing whitespace. 310 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) 311 return asm 312 313def get_triple_from_march(march): 314 triples = { 315 'amdgcn': 'amdgcn', 316 'r600': 'r600', 317 'mips': 'mips', 318 'sparc': 'sparc', 319 'hexagon': 'hexagon', 320 } 321 for prefix, triple in triples.items(): 322 if march.startswith(prefix): 323 return triple 324 print("Cannot find a triple. Assume 'x86'", file=sys.stderr) 325 return 'x86' 326 327def get_run_handler(triple): 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 return handler 369 370##### Generator of assembly CHECK lines 371 372def add_asm_checks(output_lines, comment_marker, prefix_list, func_dict, func_name): 373 # Label format is based on ASM string. 374 check_label_format = '{} %s-LABEL: %s%s:'.format(comment_marker) 375 global_vars_seen_dict = {} 376 common.add_checks(output_lines, comment_marker, prefix_list, func_dict, func_name, check_label_format, True, False, global_vars_seen_dict) 377