Skip to content

Commit 4088940

Browse files
committed
[thumb2] Updated thumb2 disassembler generator to use Tatsu-generated parser instead of deprecated Grako
1 parent d6e13c7 commit 4088940

File tree

2 files changed

+39
-34
lines changed

2 files changed

+39
-34
lines changed

arch/armv7/thumb2_disasm/generator.py

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,12 @@ def genCheckMatch(self, varName='instr'):
448448
maskNeg += 'x'*int(m.group(1))
449449
elif re.match(r'^[\(\)01]+$', field):
450450
# unpredictable fields (eg: "(0)(0)(0)(0)") are do-not-cares
451-
maskPos += 'x'*(len(field)/3)
452-
maskNeg += 'x'*(len(field)/3)
451+
try:
452+
maskPos += 'x'*(len(field)//3)
453+
maskNeg += 'x'*(len(field)//3)
454+
except:
455+
print(f'stupid math in {field!r} in {self.text!r}')
456+
raise
453457
else:
454458
parseError('genCheckMatch(): unknown bit extract field %s' % field)
455459

@@ -459,7 +463,7 @@ def genCheckMatch(self, varName='instr'):
459463

460464
# finally convert to a check
461465
tmp = bitMaskGenCheckMatch(maskPos, varName)
462-
if filter(lambda a: a!='x', maskNeg):
466+
if list(filter(lambda a: a!='x', maskNeg)):
463467
tmp += ' && !'+bitMaskGenCheckMatch(maskNeg, varName)
464468
return tmp
465469

@@ -532,7 +536,7 @@ def genExtractGeneral(self, varName='instr'):
532536
else:
533537
m = re.match(r'^[\(\)01]+$', field)
534538
if m:
535-
nBits = len(field)/3
539+
nBits = len(field)//3
536540
else:
537541
parseError('genExtractGeneral(): unknown bit extract field %s' % field)
538542

@@ -600,7 +604,7 @@ def genExtractToDrawing(self, varName='instr'):
600604

601605
# get the width of a variable from within the pattern
602606
def getVarWidth(self, varName):
603-
regex = varName + '\.(\\d)'
607+
regex = varName + r'\.(\\d)'
604608

605609
#print("trying to get var: %s" % varName)
606610
#print("using regex: %s" % regex)
@@ -673,7 +677,7 @@ def genEncodingBlock(mgr, encName, arches, fmts, pattern, pcode):
673677
# generate architecture check
674678
checks = []
675679
for arch in arches.split(', '):
676-
checks.append('!(req->arch & ARCH_%s)' % string.replace(arch, '*', ''))
680+
checks.append('!(req->arch & ARCH_%s)' % arch.replace('*', ''))
677681
mgr.add("if(%s) {" % ' && '.join(checks))
678682
mgr.tab()
679683
mgr.add('res->status |= STATUS_ARCH_UNSUPPORTED;')
@@ -1323,9 +1327,9 @@ def gen_node(mgr, nodeName, lines):
13231327
checks.append(bitMaskGenCheckMatch(mask, varName))
13241328

13251329
# collapse all '1's into a single 1
1326-
trues = filter(lambda x: x=='1', checks)
1330+
trues = list(filter(lambda x: x=='1', checks))
13271331
if trues:
1328-
others = filter(lambda x: x!='1', checks)
1332+
others = list(filter(lambda x: x!='1', checks))
13291333
checks = others + ['1']
13301334

13311335
# and generate the code
@@ -1389,30 +1393,32 @@ def gen_node(mgr, nodeName, lines):
13891393
for node in node2lines.keys():
13901394
node2crc[node] = binascii.crc32((''.join(node2lines[node])).encode('utf-8')) & 0xFFFFFFFF
13911395

1392-
# open spec.cpp, read the crc's of generated functions (detecting if they need regen)
1393-
print('collecting functions from spec.cpp')
1394-
fp = open('spec.cpp', 'r')
1395-
lines = fp.readlines()
1396-
fp.close()
1397-
1398-
lines = list(map(lambda x: x.rstrip(), lines))
1396+
forceGen = 'force' in sys.argv
13991397
funcInfo = {}
1400-
i = 0
1401-
while i < len(lines):
1402-
m = re.match(r'^// gen_crc: (........).*', lines[i])
1403-
if not m:
1404-
i += 1
1405-
continue
1406-
crc = int(m.group(1), 16)
1407-
m = re.match(r'^int ([\w\d]+)\(.*$', lines[i+1])
1408-
if not m:
1409-
raise Exception('did not find function after crc line %d: %s' % (i+1,lines[i]))
1410-
name = m.group(1)
1411-
start = i
1412-
while lines[i] != '}':
1413-
i += 1
1414-
funcInfo[name] = {'crc':crc, 'lines':'\n'.join(lines[start:i+1])}
1415-
#print('found that %s has crc %08X' % (name, crc))
1398+
if not forceGen and os.path.exists('spec.cpp'):
1399+
# open spec.cpp, read the crc's of generated functions (detecting if they need regen)
1400+
print('collecting functions from spec.cpp')
1401+
fp = open('spec.cpp', 'r')
1402+
lines = fp.readlines()
1403+
fp.close()
1404+
1405+
lines = list(map(lambda x: x.rstrip(), lines))
1406+
i = 0
1407+
while i < len(lines):
1408+
m = re.match(r'^// gen_crc: (........).*', lines[i])
1409+
if not m:
1410+
i += 1
1411+
continue
1412+
crc = int(m.group(1), 16)
1413+
m = re.match(r'^int ([\w\d]+)\(.*$', lines[i+1])
1414+
if not m:
1415+
raise Exception('did not find function after crc line %d: %s' % (i+1,lines[i]))
1416+
name = m.group(1)
1417+
start = i
1418+
while lines[i] != '}':
1419+
i += 1
1420+
funcInfo[name] = {'crc':crc, 'lines':'\n'.join(lines[start:i+1])}
1421+
#print('found that %s has crc %08X' % (name, crc))
14161422

14171423
# construct the new file
14181424
mgr = CodeManager()
@@ -1423,7 +1429,6 @@ def gen_node(mgr, nodeName, lines):
14231429
mgr.add(support)
14241430

14251431
count = 0
1426-
forceGen = 'force' in sys.argv
14271432
# for every node that doesn't have a matching function, generate!
14281433
for node in sorted(node2lines.keys()):
14291434
nodeCrc = node2crc[node]

arch/armv7/thumb2_disasm/spec.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34286,7 +34286,7 @@ int vcvt_float_fixed(struct decomp_request *req, struct decomp_result *res)
3428634286
return undefined(req, res);
3428734287
}
3428834288

34289-
// gen_crc: 3CB799A8
34289+
// gen_crc: EBC7DA24
3429034290
int vcvt_float_int(struct decomp_request *req, struct decomp_result *res)
3429134291
{
3429234292
int rc = -1;
@@ -34486,7 +34486,7 @@ int vcvt_float_int(struct decomp_request *req, struct decomp_result *res)
3448634486
res->fields[FIELD_fmt_idx] = (((((((res->fields[FIELD_op]) == (0x0))) * (4))) + (((res->fields[FIELD_opc3]) * (2)))) + (res->fields[FIELD_sz])) + (2);
3448734487
res->fields_mask[FIELD_fmt_idx >> 6] |= 1LL << (FIELD_fmt_idx & 63);
3448834488
/* pcode: if (opc1 == '0') then fmt_idx = sz */
34489-
if((res->fields[FIELD_opc1]) == 0x0) {
34489+
if(((res->fields[FIELD_opc1]) == (0x0))) {
3449034490
res->fields[FIELD_fmt_idx] = res->fields[FIELD_sz];
3449134491
res->fields_mask[FIELD_fmt_idx >> 6] |= 1LL << (FIELD_fmt_idx & 63);
3449234492
}

0 commit comments

Comments
 (0)