summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Benjamin Barenblat <bbaren@google.com>2016-01-21 15:21:36 -0500
committerGravatar Benjamin Barenblat <bbaren@google.com>2016-01-21 15:21:36 -0500
commitb307dca0dcf4fd9be9ddc5c80ead64f12d37c393 (patch)
tree678e94edd4ef2875a4493320bb20351e02a58e64
parentcd97d2f441715dd31bb3fd079ab86dd957b5b4b0 (diff)
Support generating Go code
Generate Go code for the RISC-V Go port <https://github.com/riscv/riscv-go>;.
-rw-r--r--.gitignore1
-rw-r--r--Makefile3
-rwxr-xr-xparse-opcodes52
3 files changed, 56 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index 5b578f1..9512eca 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
.*.swp
inst.chisel
+inst.go
instr-table.tex
priv-instr-table.tex
diff --git a/Makefile b/Makefile
index 0fa58a0..cce8512 100644
--- a/Makefile
+++ b/Makefile
@@ -20,6 +20,9 @@ $(GAS_H) $(XCC_H): $(ALL_OPCODES) parse-opcodes
inst.chisel: $(ALL_OPCODES) parse-opcodes
cat opcodes opcodes-custom opcodes-pseudo | ./parse-opcodes -chisel > $@
+inst.go: opcodes opcodes-pseudo parse-opcodes
+ cat opcodes opcodes-pseudo | ./parse-opcodes -go > $@
+
instr-table.tex: $(ALL_OPCODES) parse-opcodes
cat opcodes opcodes-pseudo | ./parse-opcodes -tex > $@
diff --git a/parse-opcodes b/parse-opcodes
index ffc1f07..c5427d2 100755
--- a/parse-opcodes
+++ b/parse-opcodes
@@ -4,6 +4,35 @@ import math
import sys
import tokenize
+# Instructions not used by the RISC-V Go port (and which should therefore not be
+# emitted).
+GO_UNUSED_INSTRUCTIONS = set((
+ 'addiw',
+ 'csrrc',
+ 'csrrci',
+ 'csrrs',
+ 'csrrsi',
+ 'csrrw',
+ 'csrrwi',
+ 'ebreak',
+ 'ecall',
+ 'eret',
+ 'fcvt.d.s',
+ 'fcvt.s.d',
+ 'fsgnjx.d',
+ 'fsgnjx.s',
+ 'hrts',
+ 'lw',
+ 'mrth',
+ 'mrts',
+ 'sfence.vm',
+ 'slli.rv32',
+ 'srai.rv32',
+ 'sret',
+ 'srli.rv32',
+ 'wfi',
+))
+
namelist = []
match = {}
mask = {}
@@ -767,6 +796,27 @@ def make_chisel():
print ' }'
print '}'
+def print_go_insn(name):
+ print '\tcase A%s:' % name.upper().replace('.', '')
+ print '\t\treturn 0x%x' % yank(match[name], 0, 7)
+
+def make_go():
+ print '// Automatically generated by parse-opcodes'
+ print
+ print 'package riscv'
+ print
+ print 'import "log"'
+ print
+ print 'func opcode(a int) uint32 {'
+ print '\tswitch a {'
+ for name in namelist:
+ if name not in GO_UNUSED_INSTRUCTIONS:
+ print_go_insn(name)
+ print '\t}'
+ print '\tlog.Fatalf("opcode: missing %v (bug in parse-opcodes)", a)'
+ print '\treturn 0'
+ print '}'
+
for line in sys.stdin:
line = line.partition('#')
tokens = line[0].split()
@@ -840,5 +890,7 @@ elif sys.argv[1] == '-chisel':
make_chisel()
elif sys.argv[1] == '-c':
make_c(match,mask)
+elif sys.argv[1] == '-go':
+ make_go()
else:
assert 0