• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
GCC inline assembly question (operand to 'call' instruction)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Prev by Date: no cocoa unit test
  • Next by Date: gdb/kernel crash: unable to read unknown load command 0x58
  • Previous by thread: no cocoa unit test
  • Next by thread: gdb/kernel crash: unable to read unknown load command 0x58
  • Index(es):
    • Date
    • Thread