This is the mail archive of the gcc@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]
Other format: [Raw text]

Re: A question about "memory" clobbers in asm


Jamie Lokier wrote:
I'm having trouble understand when it's appropriate to use a "memory"
clobber, when to use volatile, and when to use both.  The manual is
unclear to me.  Although I see that someone has tried to clarify it
since I last read it, it's still not obvious how "memory" is different
from `volatile'.

my understanding is


* volatile asm says something to the effect 'this changes state that you
(the compiler) don't know about' -- such as writing to an IO port.

* memory clobber says 'this asm will change memory, so don't cache anything
across this asm'.

So,

  asm volatile ("pokeIOport");
  v = *ptr;

could hoist the load before the asm, but the asm will never be deleted
from the code sequence.  Whereas

  asm ("readsomething %0" :"=r" (v2) :: "memory");
  v = *ptr;

would do no such hoisting, but would delete the asm, if V2 was never
subsequently used.

nathan

--
Nathan Sidwell    ::   http://www.codesourcery.com   ::     CodeSourcery LLC
nathan@codesourcery.com    ::     http://www.planetfall.pwp.blueyonder.co.uk



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