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_SP_RE = re.compile(r'\d+\(%(esp|rsp)\)') 79SCRUB_X86_RIP_RE = re.compile(r'[.\w]+\(%rip\)') 80SCRUB_X86_LCP_RE = re.compile(r'\.LCPI[0-9]+_[0-9]+') 81SCRUB_X86_RET_RE = re.compile(r'ret[l|q]') 82 83def scrub_asm_x86(asm, args): 84 # Scrub runs of whitespace out of the assembly, but leave the leading 85 # whitespace in place. 86 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) 87 # Expand the tabs used for indentation. 88 asm = string.expandtabs(asm, 2) 89 # Detect shuffle asm comments and hide the operands in favor of the comments. 90 asm = SCRUB_X86_SHUFFLES_RE.sub(r'\1 {{.*#+}} \2', asm) 91 # Generically match the stack offset of a memory operand. 92 asm = SCRUB_X86_SP_RE.sub(r'{{[0-9]+}}(%\1)', asm) 93 # Generically match a RIP-relative memory operand. 94 asm = SCRUB_X86_RIP_RE.sub(r'{{.*}}(%rip)', asm) 95 # Generically match a LCP symbol. 96 asm = SCRUB_X86_LCP_RE.sub(r'{{\.LCPI.*}}', asm) 97 if args.x86_extra_scrub: 98 # Avoid generating different checks for 32- and 64-bit because of 'retl' vs 'retq'. 99 asm = SCRUB_X86_RET_RE.sub(r'ret{{[l|q]}}', asm) 100 # Strip kill operands inserted into the asm. 101 asm = common.SCRUB_KILL_COMMENT_RE.sub('', asm) 102 # Strip trailing whitespace. 103 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) 104 return asm 105 106def scrub_asm_arm_eabi(asm, args): 107 # Scrub runs of whitespace out of the assembly, but leave the leading 108 # whitespace in place. 109 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) 110 # Expand the tabs used for indentation. 111 asm = string.expandtabs(asm, 2) 112 # Strip kill operands inserted into the asm. 113 asm = common.SCRUB_KILL_COMMENT_RE.sub('', asm) 114 # Strip trailing whitespace. 115 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) 116 return asm 117 118def scrub_asm_powerpc64(asm, args): 119 # Scrub runs of whitespace out of the assembly, but leave the leading 120 # whitespace in place. 121 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) 122 # Expand the tabs used for indentation. 123 asm = string.expandtabs(asm, 2) 124 # Stripe unimportant comments 125 asm = SCRUB_LOOP_COMMENT_RE.sub(r'', asm) 126 # Strip trailing whitespace. 127 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) 128 return asm 129 130def scrub_asm_mips(asm, args): 131 # Scrub runs of whitespace out of the assembly, but leave the leading 132 # whitespace in place. 133 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) 134 # Expand the tabs used for indentation. 135 asm = string.expandtabs(asm, 2) 136 # Strip trailing whitespace. 137 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) 138 return asm 139 140def scrub_asm_riscv(asm, args): 141 # Scrub runs of whitespace out of the assembly, but leave the leading 142 # whitespace in place. 143 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) 144 # Expand the tabs used for indentation. 145 asm = string.expandtabs(asm, 2) 146 # Strip trailing whitespace. 147 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) 148 return asm 149 150def scrub_asm_systemz(asm, args): 151 # Scrub runs of whitespace out of the assembly, but leave the leading 152 # whitespace in place. 153 asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) 154 # Expand the tabs used for indentation. 155 asm = string.expandtabs(asm, 2) 156 # Strip trailing whitespace. 157 asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) 158 return asm 159 160 161def build_function_body_dictionary_for_triple(args, raw_tool_output, triple, prefixes, func_dict): 162 target_handlers = { 163 'x86_64': (scrub_asm_x86, ASM_FUNCTION_X86_RE), 164 'i686': (scrub_asm_x86, ASM_FUNCTION_X86_RE), 165 'x86': (scrub_asm_x86, ASM_FUNCTION_X86_RE), 166 'i386': (scrub_asm_x86, ASM_FUNCTION_X86_RE), 167 'aarch64': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_RE), 168 'arm-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), 169 'thumb-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), 170 'thumbv6': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), 171 'thumbv6-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), 172 'thumbv6t2': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), 173 'thumbv6t2-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), 174 'thumbv6m': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), 175 'thumbv6m-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), 176 'thumbv7': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), 177 'thumbv7-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), 178 'thumbv7m': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), 179 'thumbv7m-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), 180 'thumbv8-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), 181 'thumbv8m.base': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), 182 'thumbv8m.main': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), 183 'armv6': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), 184 'armv7': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), 185 'armv7-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), 186 'armeb-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), 187 'armv7eb-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), 188 'armv7eb': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), 189 'mips': (scrub_asm_mips, ASM_FUNCTION_MIPS_RE), 190 'powerpc64': (scrub_asm_powerpc64, ASM_FUNCTION_PPC_RE), 191 'powerpc64le': (scrub_asm_powerpc64, ASM_FUNCTION_PPC_RE), 192 'riscv32': (scrub_asm_riscv, ASM_FUNCTION_RISCV_RE), 193 'riscv64': (scrub_asm_riscv, ASM_FUNCTION_RISCV_RE), 194 's390x': (scrub_asm_systemz, ASM_FUNCTION_SYSTEMZ_RE), 195 } 196 handlers = None 197 for prefix, s in target_handlers.items(): 198 if triple.startswith(prefix): 199 handlers = s 200 break 201 else: 202 raise KeyError('Triple %r is not supported' % (triple)) 203 204 scrubber, function_re = handlers 205 common.build_function_body_dictionary( 206 function_re, scrubber, [args], raw_tool_output, prefixes, 207 func_dict, args.verbose) 208 209##### Generator of assembly CHECK lines 210 211def add_asm_checks(output_lines, comment_marker, run_list, func_dict, func_name): 212 printed_prefixes = [] 213 for p in run_list: 214 checkprefixes = p[0] 215 for checkprefix in checkprefixes: 216 if checkprefix in printed_prefixes: 217 break 218 # TODO func_dict[checkprefix] may be None, '' or not exist. 219 # Fix the call sites. 220 if func_name not in func_dict[checkprefix] or not func_dict[checkprefix][func_name]: 221 continue 222 # Add some space between different check prefixes. 223 if len(printed_prefixes) != 0: 224 output_lines.append(comment_marker) 225 printed_prefixes.append(checkprefix) 226 output_lines.append('%s %s-LABEL: %s:' % (comment_marker, checkprefix, func_name)) 227 func_body = func_dict[checkprefix][func_name].splitlines() 228 output_lines.append('%s %s: %s' % (comment_marker, checkprefix, func_body[0])) 229 for func_line in func_body[1:]: 230 output_lines.append('%s %s-NEXT: %s' % (comment_marker, checkprefix, func_line)) 231 # Add space between different check prefixes and the first line of code. 232 # output_lines.append(';') 233 break 234