riscv-software-src / riscv-tools

RISC-V Tools (ISA Simulator and Tests)
1.13k stars 446 forks source link

QUERY REGARDING ADDING CUSTOM INSTRUCTIONS #355

Open BhattSoham opened 2 years ago

BhattSoham commented 2 years ago

Hi all, Good day. I tried to add custom instructions to RISCV ISA using the following link: https://nitish2112.github.io/post/adding-instruction-riscv/ But, I have a question, that while running the code using asm volatile, why can't we use immediate operand in it? Don't know if it's a basic question or not. But thought to ask it.

Suppose, I define a unused instruction: mod rd rs1 rs2 31..25=1 14..12=0 6..2=0x1A 1..0=3. So, while running the C code, do we have to write the code like this: '

include

int main(){ int a,b,c; a = 5; b = 2; asm volatile ( "mod %[z], %[x], %[y]\n\t" : [z] "=r" (c) : [x] "r" (a), [y] "r" (b) );

if ( c != 1 ){ printf("\n[[FAILED]]\n"); return -1; }

printf("\n[[PASSED]]\n");

return 0; } ' Can we use the mod function like this in the code: asm volatile ( "mod %[z], %[x], %[y]\n\t" : [z] "=r" (c) : [x] "r" (a), [y] "m" (b) );

RATHER

asm volatile ( "mod %[z], %[x], %[y]\n\t" : [z] "=r" (c) : [x] "r" (a), [y] "r" (b) );

Actually, my question is that, if the instruction is defined as addi rd rs1 imm12 14..12=0 6..2=0x04 1..0=3 So, in case of 'imm12', do we need to use immediate operand only in this case, not register one?

And, if the instruction is assigned like this: mod rd rs1 rs2 31..25=1 14..12=0 6..2=0x1A 1..0=3. So, in this case of 'rs2', we have to use register operand only in the code, not immediate operand?

Your time and consideration is appreciated.

Thanks in advance!

Sincerely, Soham.

jim-wilson commented 2 years ago

This a gcc question not a riscv-tools question. Maybe you should try the gcc-help mailing list.

If hardware requires a register, then you must give the assembler a register. You can't give it an immediate, that makes no sense.

The compiler has a reload pass that will try to fix instruction arguments if they are wrong, but there are limits on what it can do. If an instruction needs a register, and an asm gives it an immediate, then the compiler can just emit a load-immediate instruction to put the immediate in a register and pass the register to the instruction. If the instruction needs a register, and an asm gives it a memory location, then the compiler can just emit a load-from-memory instruction to put the value in a register. That is about all that the compiler can do.

Note that it is easy to experiment with asms. Just try changing them and compiling them to see what the compiler does.

The instructions you are pointing at are very old, but will probably mostly work. Note that the GNU toolchain doesn't use riscv-opcodes. If you have riscv-opcodes, then you probably should ask in the riscv-opcodes repo. And note that there is no GNU tolchain in riscv-tools. If you have gcc questions you probably should ask in the riscv-gnu-toolchain repo or in an FSF GCC mailing list like gcc-help.