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: AW: basic asm and memory clobbers - Proposed solution


Hi,

> Surely in code like that, you would make "x" volatile?  Memory clobbers
> are not a substitute for correct use of volatile accesses.

No,

It is as I wrote, a memory clobber is the only way to guarantee that
the asm statement is not move somewhere else.

I changed the example to use volatile and compiled it
with gcc-Version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) 

volatile int x;
void
test()
{
   x = 1;
   asm volatile("nop");
   x = 0;
}

gcc -S -O2 test.c gives:


test:
.LFB0:
	.cfi_startproc
	movl	$1, x(%rip)
	movl	$0, x(%rip)
#APP
# 6 "test.c" 1
	nop
# 0 "" 2
#NO_APP
	ret
	.cfi_endproc


While it works with asm volatile("nop" ::: "memory").
Likewise for "cli" and "sti" if you try to implement critical sections.
Although, these instructions do not touch any memory, we
need the memory clobber to prevent code motion.



Bernd.

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