Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 1 | import re |
Fangrui Song | 4f0f426 | 2018-02-10 05:01:33 +0000 | [diff] [blame] | 2 | import sys |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 3 | |
| 4 | from . import common |
| 5 | |
Fangrui Song | 4f0f426 | 2018-02-10 05:01:33 +0000 | [diff] [blame] | 6 | if sys.version_info[0] > 2: |
| 7 | class string: |
| 8 | expandtabs = str.expandtabs |
| 9 | else: |
| 10 | import string |
| 11 | |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 12 | # RegEx: this is where the magic happens. |
| 13 | |
Fangrui Song | 4f0f426 | 2018-02-10 05:01:33 +0000 | [diff] [blame] | 14 | ##### Assembly parser |
| 15 | |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 16 | ASM_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 | |
| 22 | ASM_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 | |
| 29 | ASM_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 | |
| 37 | ASM_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 | |
| 46 | ASM_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 | |
| 57 | ASM_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 | |
| 63 | ASM_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 | |
| 71 | SCRUB_LOOP_COMMENT_RE = re.compile( |
| 72 | r'# =>This Inner Loop Header:.*|# in Loop:.*', flags=re.M) |
| 73 | |
| 74 | SCRUB_X86_SHUFFLES_RE = ( |
| 75 | re.compile( |
| 76 | r'^(\s*\w+) [^#\n]+#+ ((?:[xyz]mm\d+|mem)( \{%k\d+\}( \{z\})?)? = .*)$', |
| 77 | flags=re.M)) |
Chandler Carruth | 6646bec | 2018-04-03 09:57:05 +0000 | [diff] [blame] | 78 | SCRUB_X86_SPILL_RELOAD_RE = ( |
| 79 | re.compile( |
| 80 | r'-?\d+\(%([er])[sb]p\)(.*(?:Spill|Reload))$', |
| 81 | flags=re.M)) |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 82 | SCRUB_X86_SP_RE = re.compile(r'\d+\(%(esp|rsp)\)') |
| 83 | SCRUB_X86_RIP_RE = re.compile(r'[.\w]+\(%rip\)') |
| 84 | SCRUB_X86_LCP_RE = re.compile(r'\.LCPI[0-9]+_[0-9]+') |
| 85 | SCRUB_X86_RET_RE = re.compile(r'ret[l|q]') |
| 86 | |
| 87 | def scrub_asm_x86(asm, args): |
| 88 | # Scrub runs of whitespace out of the assembly, but leave the leading |
| 89 | # whitespace in place. |
| 90 | asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) |
| 91 | # Expand the tabs used for indentation. |
| 92 | asm = string.expandtabs(asm, 2) |
| 93 | # Detect shuffle asm comments and hide the operands in favor of the comments. |
| 94 | asm = SCRUB_X86_SHUFFLES_RE.sub(r'\1 {{.*#+}} \2', asm) |
Chandler Carruth | 6646bec | 2018-04-03 09:57:05 +0000 | [diff] [blame] | 95 | # Detect stack spills and reloads and hide their exact offset and whether |
| 96 | # they used the stack pointer or frame pointer. |
Chandler Carruth | ff2f4fc | 2018-04-03 10:28:56 +0000 | [diff] [blame] | 97 | asm = SCRUB_X86_SPILL_RELOAD_RE.sub(r'{{[-0-9]+}}(%\1{{[sb]}}p)\2', asm) |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 98 | # Generically match the stack offset of a memory operand. |
| 99 | asm = SCRUB_X86_SP_RE.sub(r'{{[0-9]+}}(%\1)', asm) |
| 100 | # Generically match a RIP-relative memory operand. |
| 101 | asm = SCRUB_X86_RIP_RE.sub(r'{{.*}}(%rip)', asm) |
| 102 | # Generically match a LCP symbol. |
| 103 | asm = SCRUB_X86_LCP_RE.sub(r'{{\.LCPI.*}}', asm) |
Fangrui Song | 0a301a1 | 2018-03-02 17:37:04 +0000 | [diff] [blame] | 104 | if getattr(args, 'x86_extra_scrub', False): |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 105 | # Avoid generating different checks for 32- and 64-bit because of 'retl' vs 'retq'. |
| 106 | asm = SCRUB_X86_RET_RE.sub(r'ret{{[l|q]}}', asm) |
| 107 | # Strip kill operands inserted into the asm. |
| 108 | asm = common.SCRUB_KILL_COMMENT_RE.sub('', asm) |
| 109 | # Strip trailing whitespace. |
| 110 | asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) |
| 111 | return asm |
| 112 | |
| 113 | def scrub_asm_arm_eabi(asm, args): |
| 114 | # Scrub runs of whitespace out of the assembly, but leave the leading |
| 115 | # whitespace in place. |
| 116 | asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) |
| 117 | # Expand the tabs used for indentation. |
| 118 | asm = string.expandtabs(asm, 2) |
| 119 | # Strip kill operands inserted into the asm. |
| 120 | asm = common.SCRUB_KILL_COMMENT_RE.sub('', asm) |
| 121 | # Strip trailing whitespace. |
| 122 | asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) |
| 123 | return asm |
| 124 | |
| 125 | def scrub_asm_powerpc64(asm, args): |
| 126 | # Scrub runs of whitespace out of the assembly, but leave the leading |
| 127 | # whitespace in place. |
| 128 | asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) |
| 129 | # Expand the tabs used for indentation. |
| 130 | asm = string.expandtabs(asm, 2) |
| 131 | # Stripe unimportant comments |
| 132 | asm = SCRUB_LOOP_COMMENT_RE.sub(r'', asm) |
| 133 | # Strip trailing whitespace. |
| 134 | asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) |
| 135 | return asm |
| 136 | |
| 137 | def scrub_asm_mips(asm, args): |
| 138 | # Scrub runs of whitespace out of the assembly, but leave the leading |
| 139 | # whitespace in place. |
| 140 | asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) |
| 141 | # Expand the tabs used for indentation. |
| 142 | asm = string.expandtabs(asm, 2) |
| 143 | # Strip trailing whitespace. |
| 144 | asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) |
| 145 | return asm |
| 146 | |
| 147 | def scrub_asm_riscv(asm, args): |
| 148 | # Scrub runs of whitespace out of the assembly, but leave the leading |
| 149 | # whitespace in place. |
| 150 | asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) |
| 151 | # Expand the tabs used for indentation. |
| 152 | asm = string.expandtabs(asm, 2) |
| 153 | # Strip trailing whitespace. |
| 154 | asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) |
| 155 | return asm |
| 156 | |
| 157 | def scrub_asm_systemz(asm, args): |
| 158 | # Scrub runs of whitespace out of the assembly, but leave the leading |
| 159 | # whitespace in place. |
| 160 | asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) |
| 161 | # Expand the tabs used for indentation. |
| 162 | asm = string.expandtabs(asm, 2) |
| 163 | # Strip trailing whitespace. |
| 164 | asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) |
| 165 | return asm |
| 166 | |
| 167 | |
| 168 | def build_function_body_dictionary_for_triple(args, raw_tool_output, triple, prefixes, func_dict): |
| 169 | target_handlers = { |
| 170 | 'x86_64': (scrub_asm_x86, ASM_FUNCTION_X86_RE), |
| 171 | 'i686': (scrub_asm_x86, ASM_FUNCTION_X86_RE), |
| 172 | 'x86': (scrub_asm_x86, ASM_FUNCTION_X86_RE), |
| 173 | 'i386': (scrub_asm_x86, ASM_FUNCTION_X86_RE), |
| 174 | 'aarch64': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_RE), |
| 175 | 'arm-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 176 | 'thumb-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 177 | 'thumbv6': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 178 | 'thumbv6-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 179 | 'thumbv6t2': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 180 | 'thumbv6t2-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 181 | 'thumbv6m': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 182 | 'thumbv6m-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 183 | 'thumbv7': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 184 | 'thumbv7-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 185 | 'thumbv7m': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 186 | 'thumbv7m-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 187 | 'thumbv8-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 188 | 'thumbv8m.base': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 189 | 'thumbv8m.main': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 190 | 'armv6': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 191 | 'armv7': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 192 | 'armv7-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 193 | 'armeb-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 194 | 'armv7eb-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 195 | 'armv7eb': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 196 | 'mips': (scrub_asm_mips, ASM_FUNCTION_MIPS_RE), |
| 197 | 'powerpc64': (scrub_asm_powerpc64, ASM_FUNCTION_PPC_RE), |
| 198 | 'powerpc64le': (scrub_asm_powerpc64, ASM_FUNCTION_PPC_RE), |
| 199 | 'riscv32': (scrub_asm_riscv, ASM_FUNCTION_RISCV_RE), |
| 200 | 'riscv64': (scrub_asm_riscv, ASM_FUNCTION_RISCV_RE), |
| 201 | 's390x': (scrub_asm_systemz, ASM_FUNCTION_SYSTEMZ_RE), |
| 202 | } |
| 203 | handlers = None |
| 204 | for prefix, s in target_handlers.items(): |
| 205 | if triple.startswith(prefix): |
| 206 | handlers = s |
| 207 | break |
| 208 | else: |
| 209 | raise KeyError('Triple %r is not supported' % (triple)) |
| 210 | |
| 211 | scrubber, function_re = handlers |
| 212 | common.build_function_body_dictionary( |
| 213 | function_re, scrubber, [args], raw_tool_output, prefixes, |
| 214 | func_dict, args.verbose) |
Fangrui Song | 4f0f426 | 2018-02-10 05:01:33 +0000 | [diff] [blame] | 215 | |
| 216 | ##### Generator of assembly CHECK lines |
| 217 | |
Simon Pilgrim | 702ec04 | 2018-04-05 09:50:58 +0000 | [diff] [blame] | 218 | def add_asm_checks(output_lines, comment_marker, prefix_list, func_dict, func_name): |
| 219 | # Label format is based on ASM string. |
| 220 | check_label_format = '{} %s-LABEL: %s:'.format(comment_marker) |
Simon Pilgrim | 8296229 | 2018-04-05 10:48:38 +0000 | [diff] [blame] | 221 | common.add_checks(output_lines, comment_marker, prefix_list, func_dict, func_name, check_label_format, True) |