Inline assembler question
David Betz
Posts: 14,516
I'm working on some pin functions for Propeller 2 and am having trouble with inline assembly. I've written the following inline function for setting the value of a pin:
My problem is that the following generated code is wrong. It reuses r5 even though it is clobbered by the rcr instruction. Does anyone know how to fix this?
Source code fragment: (prop_setpin is a macro that calls p2_setpin with the same arguments)
Generated code:
static __inline__ void p2_setpin(uint32_t pin, uint32_t value) { __asm__ volatile ( "rcr %1, #1 wc\n\t" "setpc %0" : /* no outputs */ : /* inputs */ "r" (pin), "r" (value) : /* no clobbered registers */ ); }
My problem is that the following generated code is wrong. It reuses r5 even though it is clobbered by the rcr instruction. Does anyone know how to fix this?
Source code fragment: (prop_setpin is a macro that calls p2_setpin with the same arguments)
_NATIVE void main(volatile struct toggle_mailbox *m) { for (;;){ prop_setpin(32, 1); prop_setpin(33, 1); prop_waitcnt(prop_getcnt() + *CLKFREQ_P); prop_waitcnt(prop_getcnt() + (*CLKFREQ_P >> 2)); } ...
Generated code:
_main rdlong r4, .LC0 mov r1, r4 shr r1, #2 mov r2, #32 mov r5, #1 mov r3, #33 mov r6, #0 .L2 ' 94 "propstuff.h" 1 rcr r5, #1 wc setpc r2 ' 0 "" 2 ' 94 "propstuff.h" 1 rcr r5, #1 wc setpc r3
Comments
I have to say I prefer the following notation for readability.
Also I think instead of using value as input and output parameter, it's probably better to use it as output parameter with "+" meaning "input and output".
Note, I didn't test if this made a difference and my experience with inline assembly on GCC is still limited...
===Jac
That's because I haven't written it yet :-)
===Jac
That's because you haven't written it yet :-)
(Sorry, I couldn't resist)
But seriously: It's probably difficult to write a web page about inline assembly that doesn't focus on just one processor without giving a lot of information that's going to be useless to the reader. And because so many people use the x86, the x86-biased pages are obviously going to come out on top in Google results.
Another problem is that many pages are apparently outdated so they don't mention the named parameters syntax that I used in the code fragment above. Actually I can't even find the page where I found out what the '+' modifier does, right now.
Interestingly, googling for "Propeller gcc inline assembler" (without the quotes), does yield some pages about other processors such as ARM.
===Jac
If you find a reference to "+" please let me know. I haven't ever seen that.
Here you go: http://gcc.gnu.org/onlinedocs/gcc/Modifiers.html#Modifiers