Files
riscv-opcodes/rust_utils.py
Tim Hutt 284a5fa0f7 Add static type hints
This makes the code easier to understand and navigate, and also detected a few of bugs:

1. Missing brackets on e.upper. (Fixed)
2. Not strictly related to types, but a lot of the regexes were not raw strings and therefore contained invalid escape sequences. Python prints a warning about these in recent versions. (Fixed)
3. Expression in `process_pseudo_instructions()` that is always false. (Not fixed)
4. Missing definition of `log_and_exit()`. (Fixed)

This is validated via pre-commit in CI.
2024-10-31 11:42:41 +00:00

32 lines
1.1 KiB
Python

import logging
import pprint
from constants import *
# from shared_utils import overlaps, overlap_allowed, extension_overlap_allowed, instruction_overlap_allowed, process_enc_line, same_base_isa, add_segmented_vls_insn, expand_nf_field
from shared_utils import *
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'
)
rust_file = open("inst.rs", "w")
rust_file.write(
f"""
/* Automatically generated by parse_opcodes */
{mask_match_str}
"""
)
rust_file.close()