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


Ian Lance Taylor wrote:
> Should this be on gcc-help?

No, because I think it's a documentation bug.

> > 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'.
> 
> Volatile means that the assembler instruction can not be deleted, even
> if it appears to be useless.
> 
> Marking the instruction as clobbering memory means that if the
> instruction is executed, then memory is clobbered in some undescribed
> way.  However, that clobbering is not important to proper execution of
> the program, and the instruction can be deleted if the specified
> side-effects turn out to be unnecessary.

If correct, I find your explanation very well phrased - much clearer
than the manual.

Take a look at this sentence from the manual:

   You will also want to add the `volatile' keyword if the memory affected
   is not listed in the inputs or outputs of the `asm', as the `memory'
   clobber does not count as a side-effect of the `asm'.

Because "memory" isn't needed when the affected memory is listed in
the inputs or outputs, as it goes on to show in the example, the above
sentence implies that you should always use `volatile' any time that
you would use "memory".

That is the source of confusion, as that is found in the only
paragraph which describes what "memory" means.  The paragraph starts
by saying when to use "memory", then goes on to say when you would use
`volatile' in addition to "memory", but gives a condition which is
always true any time you would use "memory" according to the next bit.
("You _will_ also... if..."  <condition which is always true when you
would use "memory">).

Breaking down that paragraph, remember it's the only paragraph which
mentions "memory" so it is really the definition of "memory", it says:

   1. Use "memory" when memory is accessed unpredictably.
   2. Use `volatile' when access is to memory not mention in operands.
   3. Don't use "memory" when access is to memory mentioned in operands.

The only logical result which fits is "Use `volatile' whenever you
have reason to use "memory"".

Yet, if you take the fact that the sentence "You will also..." is
conditional as implying that sometimes you don't want to add the
`volatile' keyword, then the paragraph is in contradiction with itself.

In practice it means I don't know when it's appropriate to use
"memory" by itself.  I don't know what code doing that would mean or
if it's correct - at least, not by reading the GCC manual.

A quick check of Linux kernel's x86 code shows that it always uses
`volatile' whenever it uses "memory" - so no ambiguity there.

If your understand is correct, then I think it is quite well phrased
and something like it should go in the manual.

-- Jamie


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