-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathopcodes.py
executable file
·140 lines (124 loc) · 3.7 KB
/
opcodes.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python3
'parse opcodes from risc-v opcodes'
import os
import io
def fileparse(fs, default_class=''):
comment = default_class
if isinstance(fs, str):
fs = fs.split("\n")
elif isinstance(fs, io.IOBase):
comment = default_class or os.path.basename(fs.name)
fs = fs.readlines()
codelist = []
for line in [x.strip() for x in fs]:
if line == '':
continue
elif line[0] == '#':
comment = line[1:].strip()
else:
i = line.find('#')
linecomment = ''
if i != -1:
linecomment = line[i+1:].strip()
line = line[:i].strip()
codelist.append((line, comment, linecomment))
return codelist
class UndefinedParameterError(NotImplementedError):
'code type undefined'
class InvaildInstructionError(ValueError):
'code invaild length'
DefinedParameter = {
'rd': (11, 7),
'rs1': (19, 15),
'rs2': (24, 20),
'imm12': (31, 20),
'imm12hi': (31, 25),
'imm12lo': (11, 7),
'imm20': (31, 12),
'jimm20': (31, 12),
'bimm12hi': (31, 25),
'bimm12lo': (11, 7),
'shamt': (25, 20),
'shamtw': (24, 20),
'pred': (27, 24),
'succ': (23, 20),
}
def T(s):
a = list(s)
a.sort()
return tuple(a)
InstructionType = {
T({'rd', 'imm20'}): 'U',
T({'rd', 'jimm20'}): 'J',
T({'rd', 'rs1', 'imm12'}): 'I',
T({'rd', 'rs1', 'shamt'}): 'I',
T({'rd', 'rs1', 'rs2'}): 'R',
T({'rs1', 'rs2', 'bimm12hi', 'bimm12lo'}): 'B',
T({'rs1', 'rs2', 'imm12hi', 'imm12lo'}): 'S',
}
def codeparse(code):
if isinstance(code, tuple):
code = code[0]
sig, *args = code.split()
args_list = []
arg_set = set()
for arg in args:
pos, *val = arg.split('=')
if not val:
if not pos in DefinedParameter:
raise UndefinedParameterError(pos)
arg_set.add(pos)
args_list.append((pos, DefinedParameter[pos], None))
else:
hi, *loa = pos.split('..')
r = (int(hi), int(loa[0])) if loa else (int(hi), int(hi))
if val[0] == 'ignore':
args_list.append(('var', r, None))
else:
if val[0].startswith('0x'):
valnum = int(val[0], 16)
elif val[0].startswith('0b'):
valnum = int(val[0], 2)
else:
valnum = int(val[0])
args_list.append(('const', r, valnum))
# Check Instruction Type
index = T(arg_set)
if index in InstructionType:
codetype = InstructionType[index]
else:
codetype = 'O'
# Check Instruction Vaild
mask = 0
constmask = 0
const = 0
for part in args_list:
mask ^= (lambda x:(1<<(x[0]+1)) - (1<<x[1]))(part[1])
if part[0] == 'const':
constmask ^= (lambda x:(1<<(x[0]+1)) - (1<<x[1]))(part[1])
const ^= (lambda x, y:y<<x[1])(part[1], part[2])
length = None
for bit_length in range(4,8):
if mask ^ ((1<<(1<<bit_length))-1) == 0:
length = (1<<bit_length)
break
if not length:
raise InvaildInstructionError(sig, bin(mask))
return {
'name': sig,
'args': args_list,
'type': codetype,
'len': length,
'const': const,
'mask': constmask,
}
filename = 'opcodes/opcodes'
with open(filename) as fp:
content = fp.readlines()
content.insert(9, '# RV32I')
content = fileparse(content)
codes = [codeparse(code) for code in filter(lambda x:x[1] == 'RV32I' or x[1] == 'SYSTEM', content)]
if __name__ == '__main__':
import pprint
pp = pprint.PrettyPrinter(indent=2).pprint
pp(codes)