Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored and Optimized Logic:: Parser Logic & Shared Modules #283

Merged
merged 14 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions c_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
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():
begin = rng[1]
end = rng[0]
mask = ((1 << (end - begin + 1)) - 1) << begin
arg_str += f"#define INSN_FIELD_{name.upper().replace(' ', '_')} {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()
enc_file = open("encoding.out.h", "w")
enc_file.write(
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
"""
)
enc_file.close()
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
Loading