From e1d35673a4fd0d7cfe25f8459b8a2a06275a50cb Mon Sep 17 00:00:00 2001 From: Jorropo Date: Fri, 3 Jan 2025 02:20:52 +0100 Subject: [PATCH] Simplify go's LUT I am working on C support and it started being very awkward to have to add things like funct4 and funct6 when the bits overlap and the some of the fields are used differently or not used at all by different encodings. By passing the match the go assembler can do a simple bitwise or. This was also changed to emit go fmt-ed code from the start as go fmt now refuse to run due to the internal import. Here is the matching CL on go's side https://go-review.googlesource.com/c/go/+/637940 --- go_utils.py | 32 ++++++-------------------------- 1 file changed, 6 insertions(+), 26 deletions(-) diff --git a/go_utils.py b/go_utils.py index 5c1fcbf3..8049f369 100644 --- a/go_utils.py +++ b/go_utils.py @@ -19,43 +19,23 @@ def make_go(instr_dict: InstrDict): 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 { +// matchFor returns the fixed bits for the given As. +func matchFor(a obj.As) (_ uint32, found bool) { switch a { """ - endoffile = """ } - return nil + endoffile = """ } + return 0, false } """ 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)} }} + instr_str += f""" case A{i.upper().replace("_","")}: + return {hex(int(instr_dict[i]["match"], 0))}, true """ with open("inst.go", "w", encoding="utf-8") as file: file.write(prelude) file.write(instr_str) file.write(endoffile) - - try: - subprocess.run(["go", "fmt", "inst.go"], check=True) - except: # pylint: disable=bare-except - pass