Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 1 | import re |
| 2 | import string |
| 3 | |
| 4 | from . import common |
| 5 | |
| 6 | # RegEx: this is where the magic happens. |
| 7 | |
| 8 | ASM_FUNCTION_X86_RE = re.compile( |
| 9 | r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n[^:]*?' |
| 10 | r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*' |
| 11 | r'^\s*(?:[^:\n]+?:\s*\n\s*\.size|\.cfi_endproc|\.globl|\.comm|\.(?:sub)?section|#+ -- End function)', |
| 12 | flags=(re.M | re.S)) |
| 13 | |
| 14 | ASM_FUNCTION_ARM_RE = re.compile( |
| 15 | r'^(?P<func>[0-9a-zA-Z_]+):\n' # f: (name of function) |
| 16 | r'\s+\.fnstart\n' # .fnstart |
| 17 | r'(?P<body>.*?)\n' # (body of the function) |
| 18 | r'.Lfunc_end[0-9]+:', # .Lfunc_end0: or # -- End function |
| 19 | flags=(re.M | re.S)) |
| 20 | |
| 21 | ASM_FUNCTION_AARCH64_RE = re.compile( |
| 22 | r'^_?(?P<func>[^:]+):[ \t]*\/\/[ \t]*@(?P=func)\n' |
| 23 | r'[ \t]+.cfi_startproc\n' |
| 24 | r'(?P<body>.*?)\n' |
| 25 | # This list is incomplete |
| 26 | r'.Lfunc_end[0-9]+:\n', |
| 27 | flags=(re.M | re.S)) |
| 28 | |
| 29 | ASM_FUNCTION_MIPS_RE = re.compile( |
| 30 | r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n[^:]*?' # f: (name of func) |
| 31 | r'(?:^[ \t]+\.(frame|f?mask|set).*?\n)+' # Mips+LLVM standard asm prologue |
| 32 | r'(?P<body>.*?)\n' # (body of the function) |
| 33 | r'(?:^[ \t]+\.(set|end).*?\n)+' # Mips+LLVM standard asm epilogue |
| 34 | r'(\$|\.L)func_end[0-9]+:\n', # $func_end0: (mips32 - O32) or |
| 35 | # .Lfunc_end0: (mips64 - NewABI) |
| 36 | flags=(re.M | re.S)) |
| 37 | |
| 38 | ASM_FUNCTION_PPC_RE = re.compile( |
| 39 | r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n' |
| 40 | r'\.Lfunc_begin[0-9]+:\n' |
| 41 | r'(?:[ \t]+.cfi_startproc\n)?' |
| 42 | r'(?:\.Lfunc_[gl]ep[0-9]+:\n(?:[ \t]+.*?\n)*)*' |
| 43 | r'(?P<body>.*?)\n' |
| 44 | # This list is incomplete |
| 45 | r'(?:^[ \t]*(?:\.long[ \t]+[^\n]+|\.quad[ \t]+[^\n]+)\n)*' |
| 46 | r'.Lfunc_end[0-9]+:\n', |
| 47 | flags=(re.M | re.S)) |
| 48 | |
| 49 | ASM_FUNCTION_RISCV_RE = re.compile( |
| 50 | r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n[^:]*?' |
| 51 | r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*' |
| 52 | r'.Lfunc_end[0-9]+:\n', |
| 53 | flags=(re.M | re.S)) |
| 54 | |
| 55 | ASM_FUNCTION_SYSTEMZ_RE = re.compile( |
| 56 | r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n' |
| 57 | r'[ \t]+.cfi_startproc\n' |
| 58 | r'(?P<body>.*?)\n' |
| 59 | r'.Lfunc_end[0-9]+:\n', |
| 60 | flags=(re.M | re.S)) |
| 61 | |
| 62 | |
| 63 | SCRUB_LOOP_COMMENT_RE = re.compile( |
| 64 | r'# =>This Inner Loop Header:.*|# in Loop:.*', flags=re.M) |
| 65 | |
| 66 | SCRUB_X86_SHUFFLES_RE = ( |
| 67 | re.compile( |
| 68 | r'^(\s*\w+) [^#\n]+#+ ((?:[xyz]mm\d+|mem)( \{%k\d+\}( \{z\})?)? = .*)$', |
| 69 | flags=re.M)) |
| 70 | SCRUB_X86_SP_RE = re.compile(r'\d+\(%(esp|rsp)\)') |
| 71 | SCRUB_X86_RIP_RE = re.compile(r'[.\w]+\(%rip\)') |
| 72 | SCRUB_X86_LCP_RE = re.compile(r'\.LCPI[0-9]+_[0-9]+') |
| 73 | SCRUB_X86_RET_RE = re.compile(r'ret[l|q]') |
| 74 | |
| 75 | def scrub_asm_x86(asm, args): |
| 76 | # Scrub runs of whitespace out of the assembly, but leave the leading |
| 77 | # whitespace in place. |
| 78 | asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) |
| 79 | # Expand the tabs used for indentation. |
| 80 | asm = string.expandtabs(asm, 2) |
| 81 | # Detect shuffle asm comments and hide the operands in favor of the comments. |
| 82 | asm = SCRUB_X86_SHUFFLES_RE.sub(r'\1 {{.*#+}} \2', asm) |
| 83 | # Generically match the stack offset of a memory operand. |
| 84 | asm = SCRUB_X86_SP_RE.sub(r'{{[0-9]+}}(%\1)', asm) |
| 85 | # Generically match a RIP-relative memory operand. |
| 86 | asm = SCRUB_X86_RIP_RE.sub(r'{{.*}}(%rip)', asm) |
| 87 | # Generically match a LCP symbol. |
| 88 | asm = SCRUB_X86_LCP_RE.sub(r'{{\.LCPI.*}}', asm) |
| 89 | if args.x86_extra_scrub: |
| 90 | # Avoid generating different checks for 32- and 64-bit because of 'retl' vs 'retq'. |
| 91 | asm = SCRUB_X86_RET_RE.sub(r'ret{{[l|q]}}', asm) |
| 92 | # Strip kill operands inserted into the asm. |
| 93 | asm = common.SCRUB_KILL_COMMENT_RE.sub('', asm) |
| 94 | # Strip trailing whitespace. |
| 95 | asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) |
| 96 | return asm |
| 97 | |
| 98 | def scrub_asm_arm_eabi(asm, args): |
| 99 | # Scrub runs of whitespace out of the assembly, but leave the leading |
| 100 | # whitespace in place. |
| 101 | asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) |
| 102 | # Expand the tabs used for indentation. |
| 103 | asm = string.expandtabs(asm, 2) |
| 104 | # Strip kill operands inserted into the asm. |
| 105 | asm = common.SCRUB_KILL_COMMENT_RE.sub('', asm) |
| 106 | # Strip trailing whitespace. |
| 107 | asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) |
| 108 | return asm |
| 109 | |
| 110 | def scrub_asm_powerpc64(asm, args): |
| 111 | # Scrub runs of whitespace out of the assembly, but leave the leading |
| 112 | # whitespace in place. |
| 113 | asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) |
| 114 | # Expand the tabs used for indentation. |
| 115 | asm = string.expandtabs(asm, 2) |
| 116 | # Stripe unimportant comments |
| 117 | asm = SCRUB_LOOP_COMMENT_RE.sub(r'', asm) |
| 118 | # Strip trailing whitespace. |
| 119 | asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) |
| 120 | return asm |
| 121 | |
| 122 | def scrub_asm_mips(asm, args): |
| 123 | # Scrub runs of whitespace out of the assembly, but leave the leading |
| 124 | # whitespace in place. |
| 125 | asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) |
| 126 | # Expand the tabs used for indentation. |
| 127 | asm = string.expandtabs(asm, 2) |
| 128 | # Strip trailing whitespace. |
| 129 | asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) |
| 130 | return asm |
| 131 | |
| 132 | def scrub_asm_riscv(asm, args): |
| 133 | # Scrub runs of whitespace out of the assembly, but leave the leading |
| 134 | # whitespace in place. |
| 135 | asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) |
| 136 | # Expand the tabs used for indentation. |
| 137 | asm = string.expandtabs(asm, 2) |
| 138 | # Strip trailing whitespace. |
| 139 | asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) |
| 140 | return asm |
| 141 | |
| 142 | def scrub_asm_systemz(asm, args): |
| 143 | # Scrub runs of whitespace out of the assembly, but leave the leading |
| 144 | # whitespace in place. |
| 145 | asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) |
| 146 | # Expand the tabs used for indentation. |
| 147 | asm = string.expandtabs(asm, 2) |
| 148 | # Strip trailing whitespace. |
| 149 | asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) |
| 150 | return asm |
| 151 | |
| 152 | |
| 153 | def build_function_body_dictionary_for_triple(args, raw_tool_output, triple, prefixes, func_dict): |
| 154 | target_handlers = { |
| 155 | 'x86_64': (scrub_asm_x86, ASM_FUNCTION_X86_RE), |
| 156 | 'i686': (scrub_asm_x86, ASM_FUNCTION_X86_RE), |
| 157 | 'x86': (scrub_asm_x86, ASM_FUNCTION_X86_RE), |
| 158 | 'i386': (scrub_asm_x86, ASM_FUNCTION_X86_RE), |
| 159 | 'aarch64': (scrub_asm_arm_eabi, ASM_FUNCTION_AARCH64_RE), |
| 160 | 'arm-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 161 | 'thumb-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 162 | 'thumbv6': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 163 | 'thumbv6-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 164 | 'thumbv6t2': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 165 | 'thumbv6t2-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 166 | 'thumbv6m': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 167 | 'thumbv6m-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 168 | 'thumbv7': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 169 | 'thumbv7-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 170 | 'thumbv7m': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 171 | 'thumbv7m-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 172 | 'thumbv8-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 173 | 'thumbv8m.base': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 174 | 'thumbv8m.main': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 175 | 'armv6': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 176 | 'armv7': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 177 | 'armv7-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 178 | 'armeb-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 179 | 'armv7eb-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 180 | 'armv7eb': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 181 | 'mips': (scrub_asm_mips, ASM_FUNCTION_MIPS_RE), |
| 182 | 'powerpc64': (scrub_asm_powerpc64, ASM_FUNCTION_PPC_RE), |
| 183 | 'powerpc64le': (scrub_asm_powerpc64, ASM_FUNCTION_PPC_RE), |
| 184 | 'riscv32': (scrub_asm_riscv, ASM_FUNCTION_RISCV_RE), |
| 185 | 'riscv64': (scrub_asm_riscv, ASM_FUNCTION_RISCV_RE), |
| 186 | 's390x': (scrub_asm_systemz, ASM_FUNCTION_SYSTEMZ_RE), |
| 187 | } |
| 188 | handlers = None |
| 189 | for prefix, s in target_handlers.items(): |
| 190 | if triple.startswith(prefix): |
| 191 | handlers = s |
| 192 | break |
| 193 | else: |
| 194 | raise KeyError('Triple %r is not supported' % (triple)) |
| 195 | |
| 196 | scrubber, function_re = handlers |
| 197 | common.build_function_body_dictionary( |
| 198 | function_re, scrubber, [args], raw_tool_output, prefixes, |
| 199 | func_dict, args.verbose) |