Comment # 1
on bug 154
from art.08.09@gmail.com
> asm {"
> mov %2, %0;
> lock;
> xadd %0, (%1);
>
> "
> : "=r"(retVal)
> : "r"(counter), "r"(addition)
> : "memory"; }
> when compiling with -O2 the exchangeAndAdd function assembly is the same but
> it is also inlined into main like so (invalid assembly):
>
> 406614: ba 02 00 00 00 mov $0x2,%edx
> 406619: 48 c7 44 24 08 0a 00 movq $0xa,0x8(%rsp)
> 406620: 00 00
> 406622: 48 8d 44 24 08 lea 0x8(%rsp),%rax
> 406627: 48 89 d0 mov %rdx,%rax
> 40662a: f0 48 0f c1 00 lock xadd %rax,(%rax)
>
> This obviously segfaults as rax is garbaged.
That happens because you're modifying the output before using the inputs.
Not a bug; you need to mark the output as an earlyclobber:
ulong /*RAX*/ exchangeAndAdd(ulong * counter /*RSI*/, ulong addition /*RDI*/)
{
ulong retVal = void;
asm {"
mov %2, %0;
lock;
xadd %0, (%1);
"
: "=&r"(retVal)
: "r"(counter), "r"(addition)
: "memory"; }
return retVal;
}