forked from riscv/riscv-opcodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rust_utils.py
28 lines (24 loc) · 963 Bytes
/
rust_utils.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
import logging
import pprint
from constants import causes, csrs, csrs32
from shared_utils import InstrDict
pp = pprint.PrettyPrinter(indent=2)
logging.basicConfig(level=logging.INFO, format="%(levelname)s:: %(message)s")
def make_rust(instr_dict: InstrDict):
mask_match_str = ""
for i in instr_dict:
mask_match_str += f'const MATCH_{i.upper().replace(".","_")}: u32 = {(instr_dict[i]["match"])};\n'
mask_match_str += f'const MASK_{i.upper().replace(".","_")}: u32 = {(instr_dict[i]["mask"])};\n'
for num, name in csrs + csrs32:
mask_match_str += f"const CSR_{name.upper()}: u16 = {hex(num)};\n"
for num, name in causes:
mask_match_str += (
f'const CAUSE_{name.upper().replace(" ","_")}: u8 = {hex(num)};\n'
)
with open("inst.rs", "w", encoding="utf-8") as rust_file:
rust_file.write(
f"""
/* Automatically generated by parse_opcodes */
{mask_match_str}
"""
)