Skip to content

Commit f7b561f

Browse files
committed
Simplify go's LUT
I am working on C support and it started being very ackward 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
1 parent 34e1d81 commit f7b561f

1 file changed

Lines changed: 6 additions & 26 deletions

File tree

go_utils.py

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,43 +19,23 @@ def make_go(instr_dict: InstrDict):
1919
2020
import "cmd/internal/obj"
2121
22-
type inst struct {
23-
opcode uint32
24-
funct3 uint32
25-
rs1 uint32
26-
rs2 uint32
27-
csr int64
28-
funct7 uint32
29-
}
30-
31-
func encode(a obj.As) *inst {
22+
// matchFor returns the fixed bits for the given As.
23+
func matchFor(a obj.As) (_ uint32, found bool) {
3224
switch a {
3325
"""
3426

35-
endoffile = """ }
36-
return nil
27+
endoffile = """ }
28+
return 0, false
3729
}
3830
"""
3931

4032
instr_str = ""
4133
for i in instr_dict:
42-
enc_match = int(instr_dict[i]["match"], 0)
43-
opcode = (enc_match >> 0) & ((1 << 7) - 1)
44-
funct3 = (enc_match >> 12) & ((1 << 3) - 1)
45-
rs1 = (enc_match >> 15) & ((1 << 5) - 1)
46-
rs2 = (enc_match >> 20) & ((1 << 5) - 1)
47-
csr = (enc_match >> 20) & ((1 << 12) - 1)
48-
funct7 = (enc_match >> 25) & ((1 << 7) - 1)
49-
instr_str += f""" case A{i.upper().replace("_","")}:
50-
return &inst{{ {hex(opcode)}, {hex(funct3)}, {hex(rs1)}, {hex(rs2)}, {signed(csr,12)}, {hex(funct7)} }}
34+
instr_str += f""" case A{i.upper().replace("_","")}:
35+
return {hex(int(instr_dict[i]["match"], 0))}, true
5136
"""
5237

5338
with open("inst.go", "w", encoding="utf-8") as file:
5439
file.write(prelude)
5540
file.write(instr_str)
5641
file.write(endoffile)
57-
58-
try:
59-
subprocess.run(["go", "fmt", "inst.go"], check=True)
60-
except: # pylint: disable=bare-except
61-
pass

0 commit comments

Comments
 (0)