8000 Simplify go's LUT by Jorropo · Pull Request #332 · riscv/riscv-opcodes · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Simplify go's LUT #332

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
32 changes: 6 additions & 26 deletions go_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
0