From 89ee805a0c61e4c16c01f11bd80d4304c761ab59 Mon Sep 17 00:00:00 2001 From: Neel Gala Date: Tue, 3 May 2022 11:27:13 +0530 Subject: [PATCH 1/3] adding support for Go code generation --- parse.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/parse.py b/parse.py index 11fd1ba2..4903bedd 100755 --- a/parse.py +++ b/parse.py @@ -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< Date: Tue, 3 May 2022 11:28:31 +0530 Subject: [PATCH 2/3] readme update for Go support --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d65b0c77..5d346f62 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,7 @@ The following artifacts can be generated using parse.py: - inst.sverilog : system verilog code to decode instructions - inst.rs : rust code containing mask and match variables for all instructions - inst.spinalhdl : spinalhdl code to decode instructions +- inst.go : go code to decode instructions Make sure you install the required python pre-requisites are installed by executing the following command: @@ -137,12 +138,14 @@ pip3 install -r requirements.txt To generate all the above artifacts for all instructions currently checked in, simply run `make` from the root-directory. This should print the following log on the command-line: ``` -Running with args : ['./parse.py', '-c', '-chisel', '-sverilog', '-rust', '-latex', 'rv*', 'unratified/rv*'] +Running with args : ['./parse.py', '-c', '-go', '-chisel', '-sverilog', '-rust', '-latex', '-spinalhdl', 'rv*', 'unratified/rv*'] Extensions selected : ['rv*', 'unratified/rv*'] INFO:: encoding.out.h generated successfully INFO:: inst.chisel generated successfully +INFO:: inst.spinalhdl generated successfully INFO:: inst.sverilog generated successfully INFO:: inst.rs generated successfully +INFO:: inst.go generated successfully INFO:: instr-table.tex generated successfully INFO:: priv-instr-table.tex generated successfully ``` From 4aba5dd473577015281484e876f0c922b30696ac Mon Sep 17 00:00:00 2001 From: Neel Gala Date: Tue, 3 May 2022 11:28:57 +0530 Subject: [PATCH 3/3] actions update to generate Go in CI --- .github/workflows/python-app.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml index 72a81281..2781ac55 100644 --- a/.github/workflows/python-app.yml +++ b/.github/workflows/python-app.yml @@ -18,6 +18,6 @@ jobs: - name: Install PyYAML run: pip3 install -r requirements.txt - name: Generation C code - run: ./parse.py -c -chisel -sverilog -rust -latex "rv*" "unratified/rv*" + run: ./parse.py -c -chisel -sverilog -rust -latex -spinalhdl -go "rv*" "unratified/rv*" - name: Check C output run: cat encoding.out.h | cpp