aboutsummaryrefslogtreecommitdiff
path: root/parse.py
diff options
context:
space:
mode:
authorNeel Gala <neelgala@incoresemi.com>2022-05-03 11:27:13 +0530
committerNeel Gala <neelgala@incoresemi.com>2022-05-03 11:27:13 +0530
commit89ee805a0c61e4c16c01f11bd80d4304c761ab59 (patch)
tree526880e03b82648775f7cba13457f773fa15acc0 /parse.py
parent6176fb4ce8899084901d6ff80420d8895cd5690a (diff)
downloadriscv-opcodes-89ee805a0c61e4c16c01f11bd80d4304c761ab59.zip
riscv-opcodes-89ee805a0c61e4c16c01f11bd80d4304c761ab59.tar.gz
riscv-opcodes-89ee805a0c61e4c16c01f11bd80d4304c761ab59.tar.bz2
adding support for Go code generation
Diffstat (limited to 'parse.py')
-rwxr-xr-xparse.py52
1 files changed, 51 insertions, 1 deletions
diff --git a/parse.py b/parse.py
index 11fd1ba..4903bed 100755
--- a/parse.py
+++ b/parse.py
@@ -839,11 +839,57 @@ def make_c(instr_dict):
''')
enc_file.close()
+def make_go(instr_dict):
+ prelude = '''// Code generated by parse_opcodes -go; DO NOT EDIT.
+
+package riscv
+
+import "cmd/internal/obj"
+
+type inst struct {
+ opcode uint32
+ funct3 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)
+ 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(rs2)}, {signed(csr,12)}, {hex(funct7)} }}
+'''
+
+ with open('inst.go','w') as file:
+ file.write(prelude)
+ file.write(instr_str)
+ file.write(endoffile)
+
+def signed(value, width):
+ if 0 <= value < (1<<(width-1)):
+ return value
+ else:
+ return value - (1<<width)
+
+
if __name__ == "__main__":
print(f'Running with args : {sys.argv}')
extensions = sys.argv[1:]
- for i in ['-c','-latex','-chisel','-sverilog','-rust']:
+ for i in ['-c','-latex','-chisel','-sverilog','-rust', '-go', '-spinalhdl']:
if i in extensions:
extensions.remove(i)
print(f'Extensions selected : {extensions}')
@@ -872,6 +918,10 @@ if __name__ == "__main__":
make_rust(instr_dict)
logging.info('inst.rs generated successfully')
+ if '-go' in sys.argv[1:]:
+ make_go(instr_dict)
+ logging.info('inst.go generated successfully')
+
if '-latex' in sys.argv[1:]:
make_latex_table()
logging.info('instr-table.tex generated successfully')