diff options
author | Benjamin Barenblat <benjamin@barenblat.name> | 2017-03-31 14:43:45 -0400 |
---|---|---|
committer | Andrew Waterman <aswaterman@gmail.com> | 2017-03-31 11:43:45 -0700 |
commit | f86f5cb4589e5fcc15b09b5a59be9d63c51dcc6f (patch) | |
tree | 7f2c97fd0386dab777bf037f29d669d712ef9201 | |
parent | 3c1a9110b71658f6e3249186e8b44e7474a4ee90 (diff) | |
download | riscv-opcodes-f86f5cb4589e5fcc15b09b5a59be9d63c51dcc6f.zip riscv-opcodes-f86f5cb4589e5fcc15b09b5a59be9d63c51dcc6f.tar.gz riscv-opcodes-f86f5cb4589e5fcc15b09b5a59be9d63c51dcc6f.tar.bz2 |
Support generating Go code (#3)
* Support generating Go code
Generate Go code for the RISC-V Go port
<https://github.com/riscv/riscv-go>.
* Clarify use of yank in Go backend
* Go: Also generate funct3, csr, and funct7 encodings
* Go: Emit all instructions
Changes to the RISC-V Go implementation obviate the need for
GO_UNUSED_INSTRUCTIONS.
* Go: Print CSRs as signed values
* Go: Update parse-opcodes to use obj.As
See https://github.com/golang/go/commit/0d9258a830c585.
* Go: Return errors out of band
* Go: Return 'ok' status instead of 'err' status
Also clean up imports.
* Go: Make gofmt-clean
* Go: Return rs2 value for each instructions
Some binary floating-point instructions (ab)use the rs2 value to hold
additional instruction data, so we need that data in the Go assembler.
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile | 3 | ||||
-rwxr-xr-x | parse-opcodes | 41 |
3 files changed, 45 insertions, 0 deletions
@@ -1,4 +1,5 @@ .*.swp inst.chisel +inst.go instr-table.tex priv-instr-table.tex @@ -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 0437480..2a5de5b 100755 --- a/parse-opcodes +++ b/parse-opcodes @@ -824,6 +824,45 @@ def make_chisel(): print ' }' print '}' +def signed(value, width): + if 0 <= value < (1<<(width-1)): + return value + else: + return value - (1<<width) + +def print_go_insn(name): + print '\tcase A%s:' % name.upper().replace('.', '') + m = match[name] + opcode = yank(m, 0, 7) + funct3 = yank(m, 12, 3) + rs2 = yank(m, 20, 5) + csr = yank(m, 20, 12) + funct7 = yank(m, 25, 7) + print '\t\treturn &inst{0x%x, 0x%x, 0x%x, %d, 0x%x}, true' % (opcode, funct3, rs2, signed(csr, 12), funct7) + +def make_go(): + print '// Automatically generated by parse-opcodes' + print + print 'package riscv' + print + print 'import "cmd/internal/obj"' + print + print 'type inst struct {' + print '\topcode uint32' + print '\tfunct3 uint32' + print '\trs2 uint32' + print '\tcsr int64' + print '\tfunct7 uint32' + print '}' + print + print 'func encode(a obj.As) (i *inst, ok bool) {' + print '\tswitch a {' + for name in namelist: + print_go_insn(name) + print '\t}' + print '\treturn nil, false' + print '}' + for line in sys.stdin: line = line.partition('#') tokens = line[0].split() @@ -897,5 +936,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 |