I have the following piece of code:
template<int maxRegNum> void f(MuliInstr instr);
template<> void f<0>(MuliInstr instr)
{
if (instr.regA == 0) asm volatile ("mov r4, r0");
}
template<int maxRegNum> void f(MuliInstr instr)
{
if (instr.regA == maxRegNum) asm volatile ("mov r4, r" ???);
f<maxRegNum - 1>(instr);
}
The ??? is a place holder in the code where I want to stringify maxRegNum, is this somehow possible using C++11? A C-Preprocessor solution would be fine too :-)
I want to achieve to use this as a code generator avoiding to repeat writing this assembler move instruction 30 times if I need to make a choice out of 30 registers e.g.
Thanks for any hint on this question.
Kind Regards, Steve
EDIT: I want to achieve the following:
When I write f<31>(instr); somewhere in the code, I want the lines
if (instr.regA == 31) asm volatile ("mov r4, r31");
if (instr.regA == 30) asm volatile ("mov r4, r30");
if (instr.regA == 29) asm volatile ("mov r4, r29");
...
if (instr.regA == 0) asm volatile ("mov r4, r0");
being placed into thhe compilation result
Unfortunately, you probably require a string literal to be given
asm, which you can't build with templates.You can use
BOOST_PP_REPEAT_FROM_TOto do it easily with macros though:Without boost, you can still avoid some repetition by using macros. This should be equivalent code if your
regAis an integer member: