diff options
author | Jay Dev Jha <jaydev.neuroscitech@gmail.com> | 2024-10-27 20:38:10 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-27 20:38:10 +0530 |
commit | 25c09e69c72371d1f72ff22c3359e83a4b3247a7 (patch) | |
tree | 4c9b578a359e371cd4c49175b4abd57e595ca5e6 /c_utils.py | |
parent | 020470605b661fbffaa0e946cf279fee7c7e6fda (diff) | |
parent | 6900b2aba2ab6e895cf21bcf37c9cea4e97409bf (diff) | |
download | riscv-opcodes-25c09e69c72371d1f72ff22c3359e83a4b3247a7.zip riscv-opcodes-25c09e69c72371d1f72ff22c3359e83a4b3247a7.tar.gz riscv-opcodes-25c09e69c72371d1f72ff22c3359e83a4b3247a7.tar.bz2 |
Merge pull request #283 from riscv/latex-based-output-refactor
Refactored and Optimized Logic:: Parser Logic & Shared Modules
Diffstat (limited to 'c_utils.py')
-rw-r--r-- | c_utils.py | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/c_utils.py b/c_utils.py new file mode 100644 index 0000000..cff33dd --- /dev/null +++ b/c_utils.py @@ -0,0 +1,85 @@ +import collections +import glob +import logging +import os +import pprint +import re +import sys + +import yaml + +# 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_c(instr_dict): + mask_match_str = "" + declare_insn_str = "" + for i in instr_dict: + mask_match_str += ( + f'#define MATCH_{i.upper().replace(".","_")} {instr_dict[i]["match"]}\n' + ) + mask_match_str += ( + f'#define MASK_{i.upper().replace(".","_")} {instr_dict[i]["mask"]}\n' + ) + declare_insn_str += f'DECLARE_INSN({i.replace(".","_")}, MATCH_{i.upper().replace(".","_")}, MASK_{i.upper().replace(".","_")})\n' + + csr_names_str = "" + declare_csr_str = "" + for num, name in csrs + csrs32: + csr_names_str += f"#define CSR_{name.upper()} {hex(num)}\n" + declare_csr_str += f"DECLARE_CSR({name}, CSR_{name.upper()})\n" + + causes_str = "" + declare_cause_str = "" + for num, name in causes: + causes_str += f"#define CAUSE_{name.upper().replace(' ', '_')} {hex(num)}\n" + declare_cause_str += ( + f"DECLARE_CAUSE(\"{name}\", CAUSE_{name.upper().replace(' ','_')})\n" + ) + + arg_str = "" + for name, rng in arg_lut.items(): + sanitized_name = name.replace(" ", "_").replace("=", "_eq_") + begin = rng[1] + end = rng[0] + mask = ((1 << (end - begin + 1)) - 1) << begin + arg_str += f"#define INSN_FIELD_{sanitized_name.upper()} {hex(mask)}\n" + + with open(f"{os.path.dirname(__file__)}/encoding.h", "r") as file: + enc_header = file.read() + + commit = os.popen('git log -1 --format="format:%h"').read() + + # Generate the output as a string + output_str = f"""/* SPDX-License-Identifier: BSD-3-Clause */ + +/* Copyright (c) 2023 RISC-V International */ + +/* + * This file is auto-generated by running 'make' in + * https://github.com/riscv/riscv-opcodes ({commit}) + */ + +{enc_header} +/* Automatically generated by parse_opcodes. */ +#ifndef RISCV_ENCODING_H +#define RISCV_ENCODING_H +{mask_match_str} +{csr_names_str} +{causes_str} +{arg_str}#endif +#ifdef DECLARE_INSN +{declare_insn_str}#endif +#ifdef DECLARE_CSR +{declare_csr_str}#endif +#ifdef DECLARE_CAUSE +{declare_cause_str}#endif +""" + + # Write the modified output to the file + with open("encoding.out.h", "w") as enc_file: + enc_file.write(output_str) |