diff options
author | Andrew Waterman <andrew@sifive.com> | 2022-05-02 23:11:26 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-02 23:11:26 -0700 |
commit | 833ba82b5df000ad6bc2a2cbca367d889272f88a (patch) | |
tree | 3325155de24726a4d0a07e4f75c14e5fe84b5071 /parse.py | |
parent | 6176fb4ce8899084901d6ff80420d8895cd5690a (diff) | |
parent | 4aba5dd473577015281484e876f0c922b30696ac (diff) | |
download | riscv-opcodes-833ba82b5df000ad6bc2a2cbca367d889272f88a.zip riscv-opcodes-833ba82b5df000ad6bc2a2cbca367d889272f88a.tar.gz riscv-opcodes-833ba82b5df000ad6bc2a2cbca367d889272f88a.tar.bz2 |
Merge pull request #110 from incoresemi/support-for-go
Support for Go language code generation
Diffstat (limited to 'parse.py')
-rwxr-xr-x | parse.py | 52 |
1 files changed, 51 insertions, 1 deletions
@@ -839,11 +839,57 @@ def make_c(instr_dict): ''') enc_file.close() +def make_go(instr_dict): + prelude = '''// Code generated by parse_opcodes -go; DO NOT EDIT. + +package riscv + +import "cmd/internal/obj" + +type inst struct { + opcode uint32 + funct3 uint32 + rs2 uint32 + csr int64 + funct7 uint32 +} + +func encode(a obj.As) *inst { + switch a { +''' + endoffile = ''' } + return nil +} +''' + 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) + 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(rs2)}, {signed(csr,12)}, {hex(funct7)} }} +''' + + with open('inst.go','w') as file: + file.write(prelude) + file.write(instr_str) + file.write(endoffile) + +def signed(value, width): + if 0 <= value < (1<<(width-1)): + return value + else: + return value - (1<<width) + + if __name__ == "__main__": print(f'Running with args : {sys.argv}') extensions = sys.argv[1:] - for i in ['-c','-latex','-chisel','-sverilog','-rust']: + for i in ['-c','-latex','-chisel','-sverilog','-rust', '-go', '-spinalhdl']: if i in extensions: extensions.remove(i) print(f'Extensions selected : {extensions}') @@ -872,6 +918,10 @@ if __name__ == "__main__": make_rust(instr_dict) logging.info('inst.rs generated successfully') + if '-go' in sys.argv[1:]: + make_go(instr_dict) + logging.info('inst.go generated successfully') + if '-latex' in sys.argv[1:]: make_latex_table() logging.info('instr-table.tex generated successfully') |