Skip to content

Commit

Permalink
Merge pull request #283 from riscv/latex-based-output-refactor
Browse files Browse the repository at this point in the history
Refactored and Optimized Logic:: Parser Logic & Shared Modules
  • Loading branch information
IIITM-Jay authored Oct 27, 2024
2 parents 0204706 + 6900b2a commit 25c09e6
Show file tree
Hide file tree
Showing 10 changed files with 1,358 additions and 1,191 deletions.
85 changes: 85 additions & 0 deletions c_utils.py
Original file line number Diff line number Diff line change
@@ -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)
94 changes: 94 additions & 0 deletions chisel_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import collections
import copy
import glob
import logging
import os
import pprint
import re
import sys

import yaml

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_chisel(instr_dict, spinal_hdl=False):

chisel_names = ""
cause_names_str = ""
csr_names_str = ""
for i in instr_dict:
if spinal_hdl:
chisel_names += f' def {i.upper().replace(".","_"):<18s} = M"b{instr_dict[i]["encoding"].replace("-","-")}"\n'
# else:
# chisel_names += f' def {i.upper().replace(".","_"):<18s} = BitPat("b{instr_dict[i]["encoding"].replace("-","?")}")\n'
if not spinal_hdl:
extensions = instr_dict_2_extensions(instr_dict)
for e in extensions:
e_instrs = filter(lambda i: instr_dict[i]["extension"][0] == e, instr_dict)
if "rv64_" in e:
e_format = e.replace("rv64_", "").upper() + "64"
elif "rv32_" in e:
e_format = e.replace("rv32_", "").upper() + "32"
elif "rv_" in e:
e_format = e.replace("rv_", "").upper()
else:
e_format = e.upper
chisel_names += f' val {e_format+"Type"} = Map(\n'
for instr in e_instrs:
tmp_instr_name = '"' + instr.upper().replace(".", "_") + '"'
chisel_names += f' {tmp_instr_name:<18s} -> BitPat("b{instr_dict[instr]["encoding"].replace("-","?")}"),\n'
chisel_names += f" )\n"

for num, name in causes:
cause_names_str += f' val {name.lower().replace(" ","_")} = {hex(num)}\n'
cause_names_str += """ val all = {
val res = collection.mutable.ArrayBuffer[Int]()
"""
for num, name in causes:
cause_names_str += f' res += {name.lower().replace(" ","_")}\n'
cause_names_str += """ res.toArray
}"""

for num, name in csrs + csrs32:
csr_names_str += f" val {name} = {hex(num)}\n"
csr_names_str += """ val all = {
val res = collection.mutable.ArrayBuffer[Int]()
"""
for num, name in csrs:
csr_names_str += f""" res += {name}\n"""
csr_names_str += """ res.toArray
}
val all32 = {
val res = collection.mutable.ArrayBuffer(all:_*)
"""
for num, name in csrs32:
csr_names_str += f""" res += {name}\n"""
csr_names_str += """ res.toArray
}"""

if spinal_hdl:
chisel_file = open("inst.spinalhdl", "w")
else:
chisel_file = open("inst.chisel", "w")
chisel_file.write(
f"""
/* Automatically generated by parse_opcodes */
object Instructions {{
{chisel_names}
}}
object Causes {{
{cause_names_str}
}}
object CSRs {{
{csr_names_str}
}}
"""
)
chisel_file.close()
69 changes: 69 additions & 0 deletions go_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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_go(instr_dict):

args = " ".join(sys.argv)
prelude = f"""// Code generated by {args}; DO NOT EDIT."""

prelude += """
package riscv
import "cmd/internal/obj"
type inst struct {
opcode uint32
funct3 uint32
rs1 uint32
rs2 uint32
csr int64
funct7 uint32
}
func encode(a obj.As) *inst {
switch a {
"""

endoffile = """ }
return nil
}
"""

instr_str = ""
for i in instr_dict:
enc_match = int(instr_dict[i]["match"], 0)
opcode = (enc_match >> 0) & ((1 << 7) - 1)
funct3 = (enc_match >> 12) & ((1 << 3) - 1)
rs1 = (enc_match >> 15) & ((1 << 5) - 1)
rs2 = (enc_match >> 20) & ((1 << 5) - 1)
csr = (enc_match >> 20) & ((1 << 12) - 1)
funct7 = (enc_match >> 25) & ((1 << 7) - 1)
instr_str += f""" case A{i.upper().replace("_","")}:
return &inst{{ {hex(opcode)}, {hex(funct3)}, {hex(rs1)}, {hex(rs2)}, {signed(csr,12)}, {hex(funct7)} }}
"""

with open("inst.go", "w") as file:
file.write(prelude)
file.write(instr_str)
file.write(endoffile)

try:
import subprocess

subprocess.run(["go", "fmt", "inst.go"])
except:
pass
Loading

0 comments on commit 25c09e6

Please sign in to comment.