asm volatile (
"ldr %0, [%1]\n\t"
"add %0, %0, #1\n\t"
"str %0, [%1]\n\t"
: "=r" (tmp)
: "r" (p)
:
);
Guess what this gets compiled to?
30: f9400000 ldr x0, [x0] 34: 91000400 add x0, x0, #0x1 38: f9000000 str x0, [x0]
...equivalent to, of course,
asm volatile (
"ldr %0, [%0]\n\t"
"add %0, %0, #1\n\t"
"str %0, [%0]\n\t"
: "+r" (p)
:
:
);
The sort of aggressive and non-obvious optimization is crazy because if I really wanted the generated code, I'd have written the inline asm the second way with a read and write modifier. Maybe for architectures with specialized and very few registers this is a reasonable approach, but for RISCish instruction sets with large instruction files this is nuts. There should be a warning option for this nonsense.
This "correct way" is to use an earlyclobber modifier.
asm volatile (
"ldr %0, [%1]\n\t"
"add %0, %0, #1\n\t"
"str %0, [%1]\n\t"
: "=&r" (tmp)
: "r" (p)
:
);
IMO anything that needs a separate paragraph in third-party documents as "a caveat" needs to be fixed.
Speaking of which... Given that C really is a high-level assembly, why not finally standardize on inline asm?
No comments:
Post a Comment