aboutsummaryrefslogtreecommitdiff
path: root/src/riscv_opcodes/rust_utils.py
diff options
context:
space:
mode:
authorAndrew Waterman <andrew@sifive.com>2025-10-13 16:24:22 -0700
committerGitHub <noreply@github.com>2025-10-13 16:24:22 -0700
commit26e2c04c913a67ac51bf0a354f21f2d7d5c07c40 (patch)
tree7db4f4637f71f1777f86702a2228ccf3b9217a03 /src/riscv_opcodes/rust_utils.py
parent433081c02368eb72e6dea5413e404a8ef231658e (diff)
downloadriscv-opcodes-master.zip
riscv-opcodes-master.tar.gz
riscv-opcodes-master.tar.bz2
Reorganise Python code using pyproject.toml (#378)HEADmaster
Add pyproject.toml (the modern alternative to requirements.txt), making this a proper Python package that can be installed via pip and potentially uploaded to PyPI. This also loads the files using `importlib.resources` and installs them into the wheel. This means that when you create a wheel using `uv build` it will still be able to load all the opcodes and CSV files. To avoid moving those resource files in the source repo, the Python build backend (hatchling) is instructed to move them to the right place when building a wheel, and the `resource_root()` function checks in both places so it always works. This is a little hacky but it works. CI builds source and binary wheels (not actually binary) that can be uploaded to PyPI. If we do upload them then using this project is as simple as ``` uvx riscv_opcodes -c 'rv*' ``` Co-authored-by: Tim Hutt <timothy.hutt@codasip.com>
Diffstat (limited to 'src/riscv_opcodes/rust_utils.py')
-rw-r--r--src/riscv_opcodes/rust_utils.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/riscv_opcodes/rust_utils.py b/src/riscv_opcodes/rust_utils.py
new file mode 100644
index 0000000..74e17eb
--- /dev/null
+++ b/src/riscv_opcodes/rust_utils.py
@@ -0,0 +1,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}
+"""
+ )