Skip to content

Commit

Permalink
removed walrus operator
Browse files Browse the repository at this point in the history
  • Loading branch information
IIITM-Jay committed Oct 9, 2024
1 parent deac634 commit 41fc44b
Show file tree
Hide file tree
Showing 8 changed files with 564 additions and 367 deletions.
57 changes: 34 additions & 23 deletions c_utils.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,60 @@
import re
import collections
import glob
import logging
import os
import pprint
import logging
import collections
import yaml
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')
logging.basicConfig(level=logging.INFO, format="%(levelname)s:: %(message)s")


def make_c(instr_dict):
mask_match_str = ''
declare_insn_str = ''
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'
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'
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 = ''
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"
declare_cause_str += (
f"DECLARE_CAUSE(\"{name}\", CAUSE_{name.upper().replace(' ','_')})\n"
)

arg_str = ''
arg_str = ""
for name, rng in arg_lut.items():
begin = rng[1]
end = rng[0]
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:
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 */
enc_file = open("encoding.out.h", "w")
enc_file.write(
f"""/* SPDX-License-Identifier: BSD-3-Clause */
/* Copyright (c) 2023 RISC-V International */
Expand All @@ -67,5 +77,6 @@ def make_c(instr_dict):
{declare_csr_str}#endif
#ifdef DECLARE_CAUSE
{declare_cause_str}#endif
''')
"""
)
enc_file.close()
66 changes: 36 additions & 30 deletions chisel_utils.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
from constants import *
import collections
import copy
import re
import glob
import logging
import os
import pprint
import logging
import collections
import yaml
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')
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 = ''
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'
Expand All @@ -27,7 +31,7 @@ def make_chisel(instr_dict, spinal_hdl=False):
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)
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:
Expand All @@ -38,42 +42,43 @@ def make_chisel(instr_dict, spinal_hdl=False):
e_format = e.upper
chisel_names += f' val {e_format+"Type"} = Map(\n'
for instr in e_instrs:
tmp_instr_name = '"'+instr.upper().replace(".","_")+'"'
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'
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 = {
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
}'''
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 = {
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
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
}'''
csr_names_str += f""" res += {name}\n"""
csr_names_str += """ res.toArray
}"""

if spinal_hdl:
chisel_file = open('inst.spinalhdl','w')
chisel_file = open("inst.spinalhdl", "w")
else:
chisel_file = open('inst.chisel','w')
chisel_file.write(f'''
chisel_file = open("inst.chisel", "w")
chisel_file.write(
f"""
/* Automatically generated by parse_opcodes */
object Instructions {{
{chisel_names}
Expand All @@ -84,5 +89,6 @@ def make_chisel(instr_dict, spinal_hdl=False):
object CSRs {{
{csr_names_str}
}}
''')
"""
)
chisel_file.close()
50 changes: 27 additions & 23 deletions go_utils.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import re
import collections
import glob
import logging
import os
import pprint
import logging
import collections
import yaml
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')
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 = f"""// Code generated by {args}; DO NOT EDIT."""

prelude += '''
prelude += """
package riscv
import "cmd/internal/obj"
Expand All @@ -33,33 +36,34 @@ def make_go(instr_dict):
func encode(a obj.As) *inst {
switch a {
'''
"""

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

instr_str = ''
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("_","")}:
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:
"""

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
pass
Loading

0 comments on commit 41fc44b

Please sign in to comment.