As a workaround for https://e2e.ti.com/support/development_tools/compiler/f/343/t/487754 I wrote this inline assembly:
inline bool BOOL(unsigned int v) {
bool b;
asm("clr %0\n"
"cmp #0, %1\n"
"jz $+2\n"
"mov #1, %0" : "=r" (b) : "r" (v));
return b;
}
int main() {
volatile unsigned int v = 100;
volatile bool i = BOOL(v);
}
/opt/msp430/bin/msp430-elf-gcc -mmcu=msp430g2553 -O3 main.cc -o main.elf
However, this doesn't work as expected:
0000c150 <main>:
c150: 04 12 push r4 ;
c152: 04 41 mov r1, r4 ;
c154: 21 82 sub #4, r1 ;r2 As==10
c156: b4 40 64 00 mov #100, -2(r4) ;#0x0064, 0xfffe
c15a: fe ff
c15c: 1c 44 fe ff mov -2(r4), r12 ;
c160: 0c 43 clr r12 ;
c162: 0c 93 cmp #0, r12 ;r3 As==00
c164: 00 24 jz $+2 ;abs 0xc166
c166: 1c 43 mov #1, r12 ;r3 As==01
c168: c4 4c fd ff mov.b r12, -3(r4) ; 0xfffd
c16c: 0c 43 clr r12 ;
c16e: 21 52 add #4, r1 ;r2 As==10
c170: 34 41 pop r4 ;
c172: 30 41 ret
gcc reuses the same register (r12) as both the input and the output of my BOOL function. Is this a bug in gcc or am I doing something wrong?