| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
import sys |
| 7 |
|
| 8 |
if len(sys.argv) == 1: |
| 9 |
print("Syntax: %s <GenDisassemblerTables.inc> <arch>" %sys.argv[0]) |
| 10 |
sys.exit(1) |
| 11 |
|
| 12 |
f = open(sys.argv[1]) |
| 13 |
lines = f.readlines() |
| 14 |
f.close() |
| 15 |
|
| 16 |
print("/* Capstone Disassembly Engine, http://www.capstone-engine.org */") |
| 17 |
print("/* By Nguyen Anh Quynh <[email protected]>, 2013-2019 */") |
| 18 |
print("/* Automatically generated file, do not edit! */\n") |
| 19 |
print('#include "../../MCInst.h"') |
| 20 |
print('#include "../../LEB128.h"') |
| 21 |
print("") |
| 22 |
|
| 23 |
print(""" |
| 24 |
// Helper function for extracting fields from encoded instructions. |
| 25 |
|
| 26 |
//#if defined(_MSC_VER) && !defined(__clang__) |
| 27 |
//__declspec(noinline) |
| 28 |
//#endif |
| 29 |
|
| 30 |
#define FieldFromInstruction(fname, InsnType) \\ |
| 31 |
static InsnType fname(InsnType insn, unsigned startBit, unsigned numBits) \\ |
| 32 |
{ \\ |
| 33 |
InsnType fieldMask; \\ |
| 34 |
if (numBits == sizeof(InsnType) * 8) \\ |
| 35 |
fieldMask = (InsnType)(-1LL); \\ |
| 36 |
else \\ |
| 37 |
fieldMask = (((InsnType)1 << numBits) - 1) << startBit; \\ |
| 38 |
return (insn & fieldMask) >> startBit; \\ |
| 39 |
} |
| 40 |
""") |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
def extract_brackets(line): |
| 46 |
return line[line.index('<')+1 : line.index('>')] |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
def del_brackets(line): |
| 51 |
return line[:line.index('<')] + line[line.index('>') + 1:] |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
skip_print = True |
| 56 |
|
| 57 |
adding_slash = False |
| 58 |
|
| 59 |
llvm_debug = False |
| 60 |
|
| 61 |
def print_line(line): |
| 62 |
if skip_print is True: |
| 63 |
return |
| 64 |
if adding_slash: |
| 65 |
|
| 66 |
if (len(line.strip()) == 0): |
| 67 |
return |
| 68 |
|
| 69 |
if '//' in line: |
| 70 |
line = line.replace('//', '/*') |
| 71 |
line += ' */' |
| 72 |
print(line + ' \\') |
| 73 |
else: |
| 74 |
print(line) |
| 75 |
|
| 76 |
|
| 77 |
for line in lines: |
| 78 |
line2 = line.rstrip() |
| 79 |
|
| 80 |
if '#include ' in line2: |
| 81 |
continue |
| 82 |
|
| 83 |
|
| 84 |
elif skip_print and 'static const uint8_t DecoderTable' in line2: |
| 85 |
skip_print = False |
| 86 |
|
| 87 |
elif 'end namespace llvm' in line2: |
| 88 |
|
| 89 |
break |
| 90 |
|
| 91 |
elif 'llvm_unreachable' in line2: |
| 92 |
line2 = line2.replace('llvm_unreachable', '/* llvm_unreachable') |
| 93 |
line2 += '*/ ' |
| 94 |
if '"Invalid index!"' in line2: |
| 95 |
pass |
| 96 |
|
| 97 |
|
| 98 |
elif 'Bits[' in line2: |
| 99 |
if sys.argv[2] == 'ARM': |
| 100 |
line2 = line2.replace('Bits[', 'ARM_getFeatureBits(MI->csh->mode, ') |
| 101 |
line2 = line2.replace(']', ')') |
| 102 |
elif sys.argv[2] == 'AArch64': |
| 103 |
line2 = line2.replace('Bits[', 'AArch64_getFeatureBits(') |
| 104 |
line2 = line2.replace(']', ')') |
| 105 |
|
| 106 |
elif 'static bool checkDecoderPredicate(unsigned Idx, const FeatureBitset &Bits) {' in line2: |
| 107 |
line2 = 'static bool checkDecoderPredicate(unsigned Idx, MCInst *MI)\n{' |
| 108 |
|
| 109 |
elif 'checkDecoderPredicate(PIdx, ' in line2: |
| 110 |
line2 = line2.replace(', Bits)', ', MI)') |
| 111 |
|
| 112 |
elif 'template <typename InsnType>' in line2: |
| 113 |
continue |
| 114 |
|
| 115 |
elif 'static DecodeStatus decodeToMCInst' in line2: |
| 116 |
line2 = '#define DecodeToMCInst(fname, fieldname, InsnType) \\\n' + \ |
| 117 |
'static DecodeStatus fname(DecodeStatus S, unsigned Idx, InsnType insn, MCInst *MI, \\\n' + \ |
| 118 |
'\t\tuint64_t Address, bool *Decoder) \\\n{' |
| 119 |
adding_slash = True |
| 120 |
|
| 121 |
elif 'fieldFromInstruction' in line2: |
| 122 |
line2 = line2.replace('fieldFromInstruction', 'fieldname') |
| 123 |
if 'InsnType FieldValue' in line2: |
| 124 |
line2 = line2.replace('InsnType ', '') |
| 125 |
if 'insertBits(tmp,' in line2: |
| 126 |
line2 = line2.replace('insertBits(', '') |
| 127 |
tmpLn = line2.split(',') |
| 128 |
line2 = tmpLn[0] + ' |=' + tmpLn[1] + ',' + tmpLn[2] + ',' + tmpLn[3] + ' <<' + tmpLn[4] + ';' |
| 129 |
|
| 130 |
elif 'DecodeComplete = true;' in line2: |
| 131 |
|
| 132 |
continue |
| 133 |
|
| 134 |
elif 'bool &DecodeComplete) {' in line2: |
| 135 |
continue |
| 136 |
|
| 137 |
elif line2 == '}': |
| 138 |
if adding_slash: |
| 139 |
adding_slash = False |
| 140 |
|
| 141 |
elif 'static DecodeStatus decodeInstruction' in line2: |
| 142 |
line2 = '#define DecodeInstruction(fname, fieldname, decoder, InsnType) \\\n' + \ |
| 143 |
'static DecodeStatus fname(const uint8_t DecodeTable[], MCInst *MI, \\\n' + \ |
| 144 |
'\t\tInsnType insn, uint64_t Address) \\\n{ \\\n' + \ |
| 145 |
' unsigned Start, Len, NumToSkip, PIdx, Opc, DecodeIdx; \\\n' + \ |
| 146 |
' InsnType Val, FieldValue, PositiveMask, NegativeMask; \\\n' + \ |
| 147 |
' bool Pred, Fail, DecodeComplete = true; \\\n' + \ |
| 148 |
' uint32_t ExpectedValue;' |
| 149 |
|
| 150 |
adding_slash = True |
| 151 |
print_line(line2) |
| 152 |
|
| 153 |
skip_print = True |
| 154 |
elif 'const MCSubtargetInfo &STI' in line2: |
| 155 |
skip_print = False |
| 156 |
|
| 157 |
continue |
| 158 |
elif 'Bits = STI.getFeatureBits()' in line2: |
| 159 |
|
| 160 |
continue |
| 161 |
elif 'errs() << ' in line2: |
| 162 |
continue |
| 163 |
elif 'unsigned Start =' in line2: |
| 164 |
line2 = line2.replace('unsigned ', '') |
| 165 |
elif 'unsigned Len =' in line2: |
| 166 |
line2 = line2.replace('unsigned ', '') |
| 167 |
elif 'unsigned Len;' in line2: |
| 168 |
continue |
| 169 |
elif 'MCInst TmpMI;' in line2: |
| 170 |
continue |
| 171 |
elif 'bool Pred;' in line2: |
| 172 |
continue |
| 173 |
elif 'bool DecodeComplete;' in line2: |
| 174 |
continue |
| 175 |
elif 'unsigned NumToSkip =' in line2: |
| 176 |
line2 = line2.replace('unsigned ', '') |
| 177 |
elif 'unsigned PIdx =' in line2: |
| 178 |
line2 = line2.replace('unsigned ', '') |
| 179 |
elif 'unsigned Opc =' in line2: |
| 180 |
line2 = line2.replace('unsigned ', '') |
| 181 |
elif 'unsigned DecodeIdx =' in line2: |
| 182 |
line2 = line2.replace('unsigned ', '') |
| 183 |
elif 'InsnType Val =' in line2: |
| 184 |
line2 = line2.replace('InsnType ', '') |
| 185 |
elif 'bool Fail' in line2: |
| 186 |
line2 = line2.replace('bool ', '') |
| 187 |
elif 'InsnType PositiveMask =' in line2: |
| 188 |
line2 = line2.replace('InsnType ', '') |
| 189 |
elif 'InsnType NegativeMask =' in line2: |
| 190 |
line2 = line2.replace('InsnType ', '') |
| 191 |
elif 'InsnType ExpectedValue' in line2: |
| 192 |
line2 = line2.replace('InsnType ', '') |
| 193 |
elif 'ptrdiff_t Loc = ' in line2: |
| 194 |
continue |
| 195 |
elif 'LLVM_DEBUG(' in line2: |
| 196 |
|
| 197 |
if ');' in line2: |
| 198 |
continue |
| 199 |
skip_print = True |
| 200 |
llvm_debug = True |
| 201 |
continue |
| 202 |
elif skip_print and llvm_debug and ');' in line2: |
| 203 |
llvm_debug = False |
| 204 |
skip_print = False |
| 205 |
continue |
| 206 |
elif 'decodeToMCInst(' in line2: |
| 207 |
line2 = line2.replace('decodeToMCInst', 'decoder') |
| 208 |
line2 = line2.replace('DecodeComplete);', '&DecodeComplete);') |
| 209 |
line2 = line2.replace(', DisAsm', '') |
| 210 |
line2 = line2.replace(', TmpMI', ', MI') |
| 211 |
elif 'TmpMI.setOpcode(Opc);' in line2: |
| 212 |
line2 = ' MCInst_setOpcode(MI, Opc);' |
| 213 |
elif 'MI.setOpcode(Opc);' in line2: |
| 214 |
line2 = ' MCInst_setOpcode(MI, Opc);' |
| 215 |
elif 'MI.clear();' in line2: |
| 216 |
line2 = ' MCInst_clear(MI);' |
| 217 |
elif 'assert(' in line2: |
| 218 |
line2 = line2.replace('assert(', '/* assert(') |
| 219 |
line2 += ' */' |
| 220 |
elif 'Check(S, ' in line2: |
| 221 |
line2 = line2.replace('Check(S, ', 'Check(&S, ') |
| 222 |
if 'DecodeImm8OptLsl<' in line2: |
| 223 |
param = extract_brackets(line2) |
| 224 |
line2 = del_brackets(line2) |
| 225 |
line2 = line2.replace(', Decoder)', ', Decoder, %s)' %param) |
| 226 |
elif 'DecodeSImm<' in line2: |
| 227 |
param = extract_brackets(line2) |
| 228 |
line2 = del_brackets(line2) |
| 229 |
line2 = line2.replace(', Decoder)', ', Decoder, %s)' %param) |
| 230 |
elif 'DecodeMatrixTile<' in line2: |
| 231 |
param = extract_brackets(line2) |
| 232 |
line2 = del_brackets(line2) |
| 233 |
line2 = line2.replace(', Decoder)', ', Decoder, %s)' %param) |
| 234 |
if 'DecodeComplete = false; ' in line2: |
| 235 |
line2 = line2.replace('DecodeComplete = false; ', '') |
| 236 |
elif 'decodeUImmOperand<' in line2 or 'decodeSImmOperand<' in line2 : |
| 237 |
|
| 238 |
param = extract_brackets(line2) |
| 239 |
line2 = del_brackets(line2) |
| 240 |
line2 = line2.replace(', Decoder)', ', Decoder, %s)' %param) |
| 241 |
elif 'MI.addOperand(MCOperand::createImm(tmp));' in line2: |
| 242 |
line2 = ' MCOperand_CreateImm0(MI, tmp);' |
| 243 |
elif 'MI = TmpMI;' in line2: |
| 244 |
line2 = '' |
| 245 |
|
| 246 |
elif 'using TmpType = std::conditional' in line2: |
| 247 |
continue |
| 248 |
elif 'TmpType tmp;' in line2: |
| 249 |
line2 = line2.replace('TmpType', 'InsnType') |
| 250 |
|
| 251 |
line2 = line2.replace('::', '_') |
| 252 |
print_line(line2) |
| 253 |
|
| 254 |
if sys.argv[2] == 'ARM': |
| 255 |
print(""" |
| 256 |
FieldFromInstruction(fieldFromInstruction_2, uint16_t) |
| 257 |
DecodeToMCInst(decodeToMCInst_2, fieldFromInstruction_2, uint16_t) |
| 258 |
DecodeInstruction(decodeInstruction_2, fieldFromInstruction_2, decodeToMCInst_2, uint16_t) |
| 259 |
|
| 260 |
FieldFromInstruction(fieldFromInstruction_4, uint32_t) |
| 261 |
DecodeToMCInst(decodeToMCInst_4, fieldFromInstruction_4, uint32_t) |
| 262 |
DecodeInstruction(decodeInstruction_4, fieldFromInstruction_4, decodeToMCInst_4, uint32_t) |
| 263 |
""") |
| 264 |
|
| 265 |
if sys.argv[2] in ('AArch64', 'PPC'): |
| 266 |
print(""" |
| 267 |
FieldFromInstruction(fieldFromInstruction_4, uint32_t) |
| 268 |
DecodeToMCInst(decodeToMCInst_4, fieldFromInstruction_4, uint32_t) |
| 269 |
DecodeInstruction(decodeInstruction_4, fieldFromInstruction_4, decodeToMCInst_4, uint32_t) |
| 270 |
""") |