This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: gcc using clobbered register as input with inline assembly


On Mon, Mar 26, 2001 at 01:08:32PM -0600, Timur Tabi wrote:
> ** Reply to message from "Zack Weinberg" <zackw@stanford.edu> on Mon, 26 Mar
> 2001 10:20:21 -0800
> 
> 
> > > According to the documentation, because I specified eax and edx as
> > > clobbered registers, gcc should not be using them as inputs or outputs. 
> > > But that's exactly what's happening in my case.
> > 
> > Clobbers happen *after* the asm completes.  You have to do the moves
> > separately, or let the compiler do them for you, e.g:
> 
> I don't understand this.  What good is it to specify clobbers if they happen
> after the assembly?  I have no control over what happens outside the assembly
> code!  I thought the point behind clobbers was to inform the compiler of
> specific registers that my assembly code modifies.

Mm, let me rephrase.  You wrote

>     __asm__ __volatile__ (
>         "rdtsc \n\t"
>         "mov %%eax, (%1) \n\t"
>         "mov %%edx, 4(%1) \n\t"
>         : "=m" (*pTime)
>         : "r" (pTime)                        
>         : "eax", "edx");

As far as GCC is concerned, that is *one* machine instruction which:
	- takes one input, the value of pTime, in a register
	- places a value into memory at *pTime
	- destroys the values in eax and edx
in that order.  It has no way of knowing that there's three machine
instructions embedded in that text string, and that the destruction of
eax and edx happens before the input is used.

There is a thing called an early clobber which could help here, but
when you can explain to the compiler what rdtsc *really* does,
i.e. "write 64-bit value into eax and edx considered as a low/high
pair", why not do that instead?  With inline assembly, you're almost
always better off writing just one machine instruction per asm() and
describing as accurately as possible what that instruction does with
the constraints.

zw


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]