aboutsummaryrefslogtreecommitdiff
path: root/go_utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'go_utils.py')
-rw-r--r--go_utils.py69
1 files changed, 69 insertions, 0 deletions
diff --git a/go_utils.py b/go_utils.py
new file mode 100644
index 0000000..1f4c94b
--- /dev/null
+++ b/go_utils.py
@@ -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