-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgen_decoder.py
More file actions
136 lines (115 loc) · 4.83 KB
/
gen_decoder.py
File metadata and controls
136 lines (115 loc) · 4.83 KB
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
# MIT License
#
# Copyright (c) 2023 Can Joshua Lehmann
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Script for generating the instruction decoder
from dataclasses import dataclass
import os, os.path
def read_file(path):
with open(path, "r") as f:
return f.read()
def write_file(path, content):
with open(path, "w") as f:
f.write(content)
@dataclass
class Inst:
name: str
mask: int
pattern: int
args: list[str]
def load(path):
insts = []
for line in read_file(path).split("\n"):
if len(line) == 0 or line.startswith("#") or line.startswith("$"):
continue
parts = [part for part in line.split(" ") if len(part) > 0]
name = parts[0]
pattern = 0
mask = 0
args = []
for part in parts[1:]:
if "=" in part:
lhs, rhs = part.split("=")
if ".." in lhs:
msb, lsb = lhs.split("..")
offset = int(lsb)
size = int(msb) - offset + 1
else:
offset = int(lhs)
size = 1
local_mask = 2 ** size - 1
mask |= local_mask << offset
if rhs.startswith("0x"):
val = int(rhs[2:], 16)
elif rhs.startswith("0b"):
val = int(rhs[2:], 2)
else:
val = int(rhs)
assert (val & local_mask) == val
pattern |= val << offset
else:
args.append(part)
insts.append(Inst(
name=name,
mask=mask,
pattern=pattern,
args=args
))
return insts
def load_dir(path, prefix="rv"):
insts = []
for base, dirs, files in os.walk(path):
for file_name in files:
if file_name.startswith(prefix):
insts += Inst.load(os.path.join(base, file_name))
return insts
def gen_case(inst):
fields = {"opcode": inst.name.upper()}
for arg in inst.args:
match arg:
case "imm20": fields["imm"] = "decode_imm20(inst)"
case "rd": fields["rd"] = "(inst >> 7) & 31"
case "rs1": fields["rs1"] = "(inst >> 15) & 31"
case "rs2": fields["rs2"] = "(inst >> 20) & 31"
case "jimm20": fields["imm"] = "decode_jimm20(inst)"
case "imm12": fields["imm"] = "decode_imm12(inst)"
case "bimm12hi":
assert "bimm12lo" in inst.args
fields["imm"] = "decode_bimm12_lo_hi(inst)"
case "imm12hi":
assert "imm12lo" in inst.args
fields["imm"] = "decode_imm12_lo_hi(inst)"
case "bimm12lo": assert "bimm12hi" in inst.args
case "imm12lo": assert "imm12hi" in inst.args
case "fm" | "pred" | "succ": pass
case _:
raise Exception(f"Unknown instruction argument: {arg}")
fields = ", ".join([f".{name} = {value}" for name, value in fields.items()])
return f"CASE({inst.mask}, {inst.pattern}, ((inst_t){{ {fields} }}));"
insts = Inst.load_dir("tools/opcodes")
template = read_file("tools/decode.tmpl.c")
opcode_names = [inst.name.upper() for inst in insts]
opcode_names.sort()
opcode_cases = [gen_case(inst) for inst in insts]
code = template \
.replace("<<opcode>>", ", ".join(opcode_names)) \
.replace("<<names>>", ", ".join([f"\"{name}\"" for name in opcode_names])) \
.replace("<<decode>>", "\n ".join(opcode_cases))
write_file("src/decode.c", code)