/
SortedOpcodeTblGen.py
74 lines (59 loc) · 2.28 KB
/
SortedOpcodeTblGen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
'''
This program sorts the opcode table into two dictionaries. The first sorts
on the base opcode. The second sorts on the mnemonic instruction name.
'''
with open('OpcodeTbl.txt', 'rt') as finp:
lines = finp.readlines()
instrByOpcodeTbl = {}
for line in lines:
flds = line.split()
instr, addrMd = flds[0].split('_')
opLen = int(flds[1])
dtLen = int(flds[2])
opcode = flds[3][-2:]
if opcode in instrByOpcodeTbl:
instrByOpcodeTbl[opcode].append((instr, addrMd, opLen, dtLen, flds[3]))
else:
instrByOpcodeTbl[opcode] = [(instr, addrMd, opLen, dtLen, flds[3])]
with open('InstrByOpcodeTbl.txt', 'wt') as fout:
for instr in instrByOpcodeTbl:
print(instr, ' ', end='')
print(instr, ' ', file=fout, end='')
for i in range(len(instrByOpcodeTbl[instr])):
if i == 0:
print(instrByOpcodeTbl[instr][i])
print(instrByOpcodeTbl[instr][i], file=fout)
else:
print(' ', instrByOpcodeTbl[instr][i])
print(' ', instrByOpcodeTbl[instr][i], file=fout)
instrByNameTbl = {}
maxInstrLen = 0
for line in lines:
flds = line.split()
instr, addrMd = flds[0].split('_')
base = instr.split('.')[0]
opLen = int(flds[1])
dtLen = int(flds[2])
if base[-1].isdigit():
base = base[:-1]
if maxInstrLen < len(base):
maxInstrLen = len(base)
if base in instrByNameTbl:
instrByNameTbl[base].append((instr, addrMd, opLen, dtLen, flds[3]))
else:
instrByNameTbl[base] = [(instr, addrMd, opLen, dtLen, flds[3])]
with open('InstrByNameTbl.txt', 'wt') as fout:
maxInstrLen += 1
for instr in instrByNameTbl:
padLen = maxInstrLen - len(instr)
print(instr, ' '*padLen, end='')
print(instr, ' '*padLen, file=fout, end='')
for i in range(len(instrByNameTbl[instr])):
if i == 0:
print(instrByNameTbl[instr][i])
print(instrByNameTbl[instr][i], file=fout)
else:
print(' '*maxInstrLen, instrByNameTbl[instr][i])
print(' '*maxInstrLen, instrByNameTbl[instr][i], file=fout)
numUniqueMnemonics = len(instrByNameTbl.keys())
print('Number of unique instruction mnemonics: ', numUniqueMnemonics)