Skip to content

Commit 3c73392

Browse files
authored
Add CSR encodings for Go and remove call to Go fmt (#329)
* Don't run go fmt error in go_utils.py go_utils.py currently runs go fmt to pretty print the generated inst.go file. Unfortunately, this call to go fmt always fails as inst.go contains an internal import and go fmt is being run out of tree. Here we remove the code that attempts to format inst.go resulting in a clean run of go_utils.py without any errors. The generated file, inst.go, can be formatted when it's copied into the Go source tree, prior to its submission to the Golang project. * Add CSR support to go_utils.py go_utils.py now generates a Go map that maps CSR numbers to CSR names. The resulting map is written into inst.go for use by the Go assembler.
1 parent 34e1d81 commit 3c73392

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

go_utils.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import logging
22
import pprint
3-
import subprocess
43
import sys
54

5+
from constants import csrs
66
from shared_utils import InstrDict, signed
77

88
pp = pprint.PrettyPrinter(indent=2)
@@ -32,9 +32,14 @@ def make_go(instr_dict: InstrDict):
3232
switch a {
3333
"""
3434

35-
endoffile = """ }
35+
csrs_map_str = """ }
3636
return nil
3737
}
38+
39+
var csrs = map[uint16]string {
40+
"""
41+
42+
endoffile = """}
3843
"""
3944

4045
instr_str = ""
@@ -49,13 +54,11 @@ def make_go(instr_dict: InstrDict):
4954
instr_str += f""" case A{i.upper().replace("_","")}:
5055
return &inst{{ {hex(opcode)}, {hex(funct3)}, {hex(rs1)}, {hex(rs2)}, {signed(csr,12)}, {hex(funct7)} }}
5156
"""
57+
for num, name in sorted(csrs, key=lambda row: row[0]):
58+
csrs_map_str += f'{hex(num)} : "{name.upper()}",\n'
5259

5360
with open("inst.go", "w", encoding="utf-8") as file:
5461
file.write(prelude)
5562
file.write(instr_str)
63+
file.write(csrs_map_str)
5664
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)