Adding Custom Instructions to RISC-V GCC Toolchain

First, clone these repos:

git clone git@github.com:riscv-collab/riscv-gnu-toolchain.git
cd ./riscv-gnu-toolchain/
git submodule update --init --recursive
git clone git@github.com:riscv/riscv-opcodes.git

First, we are going to use riscv-opcodes to generate the encoding of our custom instruction.

Under extensions/unratified/, create a new file for the new instruction. We will use rv_testinst as an example

Inside the file, we have our custom instruction:

testinst    rd rs1 rs2 31..25=1  14..12=0 6..2=0x1A 1..0=3

Then, run the following command to generate C header for this instriction:

uv run riscv_opcodes -c "unratified/rv_testinst"

In the resulting encoding.out.h file, we can see the custom instruction:

encoding.out.h
/* Automatically generated by parse_opcodes. */
#ifndef RISCV_ENCODING_H
#define RISCV_ENCODING_H
#define MATCH_TESTINST 0x200006b
#define MASK_TESTINST 0xfe00707f

Then, we go to riscv-gnu-toolchain repo.

We need to edit the following files:

./binutils/bfd/elfxx-riscv.c

./binutils/include/opcode/riscv-opc.h

./binutils/include/opcode/riscv.h

./gcc/gcc/common/config/riscv/riscv-common.cc

and also the files in ./gdb that is similar to binutils.

Build

Optionally, enable multilib

Example program

Last updated