|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import json |
| 5 | +import logging |
| 6 | +import pprint |
| 7 | +import os |
| 8 | +import sys |
| 9 | +import shutil |
| 10 | +from contextlib import contextmanager |
| 11 | +from pathlib import Path |
| 12 | +from typing import Dict, List, Any |
| 13 | + |
| 14 | +# Add riscv-opcodes directory to Python path |
| 15 | +SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 16 | +RISCV_OPCODES_DIR = os.path.join(SCRIPT_DIR, "..", "riscv-opcodes") |
| 17 | +sys.path.insert(0, RISCV_OPCODES_DIR) |
| 18 | + |
| 19 | + |
| 20 | +@contextmanager |
| 21 | +def working_directory(path): |
| 22 | + """Context manager for changing the current working directory""" |
| 23 | + prev_cwd = os.getcwd() |
| 24 | + os.chdir(path) |
| 25 | + try: |
| 26 | + yield prev_cwd |
| 27 | + finally: |
| 28 | + os.chdir(prev_cwd) |
| 29 | + |
| 30 | + |
| 31 | +# Change to riscv-opcodes directory when importing to ensure relative paths work |
| 32 | +with working_directory(RISCV_OPCODES_DIR): |
| 33 | + from c_utils import make_c |
| 34 | + from chisel_utils import make_chisel |
| 35 | + from constants import emitted_pseudo_ops |
| 36 | + from go_utils import make_go |
| 37 | + from latex_utils import make_latex_table, make_priv_latex_table |
| 38 | + from rust_utils import make_rust |
| 39 | + from sverilog_utils import make_sverilog |
| 40 | + |
| 41 | +LOG_FORMAT = "%(levelname)s:: %(message)s" |
| 42 | +LOG_LEVEL = logging.INFO |
| 43 | + |
| 44 | +pretty_printer = pprint.PrettyPrinter(indent=2) |
| 45 | +logging.basicConfig(level=LOG_LEVEL, format=LOG_FORMAT) |
| 46 | + |
| 47 | + |
| 48 | +def load_instruction_dict(json_path: str) -> Dict[str, Any]: |
| 49 | + """ |
| 50 | + Load instruction dictionary from a JSON file. |
| 51 | + """ |
| 52 | + try: |
| 53 | + with open(json_path, encoding="utf-8") as f: |
| 54 | + return json.load(f) |
| 55 | + except FileNotFoundError: |
| 56 | + logging.error(f"Input JSON file not found: {json_path}") |
| 57 | + raise |
| 58 | + except json.JSONDecodeError: |
| 59 | + logging.error(f"Invalid JSON format in file: {json_path}") |
| 60 | + raise |
| 61 | + |
| 62 | + |
| 63 | +def move_file(src: str, dest_dir: str): |
| 64 | + """ |
| 65 | + Move a file to the destination directory if it exists. |
| 66 | + """ |
| 67 | + if os.path.exists(src): |
| 68 | + dest = os.path.join(dest_dir, os.path.basename(src)) |
| 69 | + shutil.move(src, dest) |
| 70 | + |
| 71 | + |
| 72 | +def generate_outputs( |
| 73 | + instr_dict: Dict[str, Any], |
| 74 | + include_pseudo: bool, |
| 75 | + c: bool, |
| 76 | + chisel: bool, |
| 77 | + spinalhdl: bool, |
| 78 | + sverilog: bool, |
| 79 | + rust: bool, |
| 80 | + go: bool, |
| 81 | + latex: bool, |
| 82 | +): |
| 83 | + """ |
| 84 | + Generate output files based on the instruction dictionary. |
| 85 | + """ |
| 86 | + # Sort the dictionary for consistent output |
| 87 | + instr_dict = dict(sorted(instr_dict.items())) |
| 88 | + |
| 89 | + # Save the processed dictionary in current directory |
| 90 | + with open("processed_instr_dict.json", "w", encoding="utf-8") as outfile: |
| 91 | + json.dump(instr_dict, outfile, indent=2) |
| 92 | + |
| 93 | + # Generate files in riscv-opcodes directory and move them to current directory |
| 94 | + with working_directory(RISCV_OPCODES_DIR) as orig_dir: |
| 95 | + if c: |
| 96 | + # For C output, filter pseudo-ops if needed |
| 97 | + if not include_pseudo: |
| 98 | + c_dict = { |
| 99 | + k: v for k, v in instr_dict.items() if k not in emitted_pseudo_ops |
| 100 | + } |
| 101 | + else: |
| 102 | + c_dict = instr_dict |
| 103 | + make_c(c_dict) |
| 104 | + move_file("encoding.out.h", orig_dir) |
| 105 | + logging.info("encoding.out.h generated successfully") |
| 106 | + |
| 107 | + if chisel: |
| 108 | + make_chisel(instr_dict) |
| 109 | + move_file("inst.chisel", orig_dir) |
| 110 | + logging.info("inst.chisel generated successfully") |
| 111 | + |
| 112 | + if spinalhdl: |
| 113 | + make_chisel(instr_dict, True) |
| 114 | + move_file("inst.spinalhdl", orig_dir) |
| 115 | + logging.info("inst.spinalhdl generated successfully") |
| 116 | + |
| 117 | + if sverilog: |
| 118 | + make_sverilog(instr_dict) |
| 119 | + move_file("inst.sverilog", orig_dir) |
| 120 | + logging.info("inst.sverilog generated successfully") |
| 121 | + |
| 122 | + if rust: |
| 123 | + make_rust(instr_dict) |
| 124 | + move_file("inst.rs", orig_dir) |
| 125 | + logging.info("inst.rs generated successfully") |
| 126 | + |
| 127 | + if go: |
| 128 | + make_go(instr_dict) |
| 129 | + move_file("inst.go", orig_dir) |
| 130 | + logging.info("inst.go generated successfully") |
| 131 | + |
| 132 | + if latex: |
| 133 | + make_latex_table() |
| 134 | + make_priv_latex_table() |
| 135 | + move_file("instr-table.tex", orig_dir) |
| 136 | + move_file("priv-instr-table.tex", orig_dir) |
| 137 | + logging.info("LaTeX files generated successfully") |
| 138 | + |
| 139 | + |
| 140 | +def main(): |
| 141 | + parser = argparse.ArgumentParser( |
| 142 | + description="Generate RISC-V constants from JSON input" |
| 143 | + ) |
| 144 | + parser.add_argument( |
| 145 | + "input_json", help="Path to JSON file containing instruction definitions" |
| 146 | + ) |
| 147 | + parser.add_argument( |
| 148 | + "-pseudo", action="store_true", help="Include pseudo-instructions" |
| 149 | + ) |
| 150 | + parser.add_argument("-c", action="store_true", help="Generate output for C") |
| 151 | + parser.add_argument( |
| 152 | + "-chisel", action="store_true", help="Generate output for Chisel" |
| 153 | + ) |
| 154 | + parser.add_argument( |
| 155 | + "-spinalhdl", action="store_true", help="Generate output for SpinalHDL" |
| 156 | + ) |
| 157 | + parser.add_argument( |
| 158 | + "-sverilog", action="store_true", help="Generate output for SystemVerilog" |
| 159 | + ) |
| 160 | + parser.add_argument("-rust", action="store_true", help="Generate output for Rust") |
| 161 | + parser.add_argument("-go", action="store_true", help="Generate output for Go") |
| 162 | + parser.add_argument("-latex", action="store_true", help="Generate output for Latex") |
| 163 | + |
| 164 | + args = parser.parse_args() |
| 165 | + |
| 166 | + # Load instruction dictionary from JSON |
| 167 | + instr_dict = load_instruction_dict(args.input_json) |
| 168 | + |
| 169 | + print(f"Loaded instruction dictionary from: {args.input_json}") |
| 170 | + |
| 171 | + # Generate outputs based on the loaded dictionary |
| 172 | + generate_outputs( |
| 173 | + instr_dict, |
| 174 | + args.pseudo, |
| 175 | + args.c, |
| 176 | + args.chisel, |
| 177 | + args.spinalhdl, |
| 178 | + args.sverilog, |
| 179 | + args.rust, |
| 180 | + args.go, |
| 181 | + args.latex, |
| 182 | + ) |
| 183 | + |
| 184 | + |
| 185 | +if __name__ == "__main__": |
| 186 | + main() |
0 commit comments