GCC inline assembly question (operand to 'call' instruction)
GCC inline assembly question (operand to 'call' instruction)
- Subject: GCC inline assembly question (operand to 'call' instruction)
- From: Jens Alfke <email@hidden>
- Date: Fri, 20 Nov 2009 09:34:44 -0800
I'm trying to use the 'asm' directive to get GCC 4.2 to emit efficient x86 code for the 'deref' method in WebKit's RefCounted class, but I can't quite get it to do what I want. I'm having trouble with (a) how to use the condition codes set by an instruction, and (b) how to pass the address of a function to a 'call' instruction.
The C++ code I'm trying to optimize is basically this:
void RefCounted::deref() {
if (--m_refCount == 0)
this->destroy();
}
The optimal x86 code (assuming 'this' is in ëx) would be:
decl (ëx)
jne L1
movl ëx, (%esp)
call destroy
L1:
Instead what GCC 4.2 with -O3 produces is:
movl (ëx), êx
decl êx
movl êx, (ëx)
testl êx, êx
jne L1
movl ëx, (%esp)
call destroy
L1:
I've asked earlier why it wants to do the redundant test (and therefore won't use decl). Now I'm trying to work around it using inline assembly:
void RefCounted::deref() {
asm volatile ( "decl (%0) \n\t jne 0f \n\t movl %1, (%%esp) \n\t call %2 \n0:"
: // no output
: "r" (&m_refCount), "r" (this), "i" (destroy)
: "cc" // clobbers condition code register
);
}
The problem is that this generates illegal assembly code: the last instruction comes out "call $destroy" instead of "call destroy", a syntax error. I can't find a good way around this. I can change the constraint on 'destroy' from "i" to "r", but that causes the compiler to prefix an instruction that loads the address of 'destroy' into a register, and then at the end it calls indirectly through the register.
Does anyone know a way to get rid of the pesky "$", or to get the assembler to ignore it, or how to otherwise accomplish what I want with inline assembly?
—Jens
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden