This is the mail archive of the
mailing list for the GCC project.
Re: AW: basic asm and memory clobbers - Proposed solution
- From: Bernd Edlinger <bernd dot edlinger at hotmail dot de>
- To: David Brown <david at westcontrol dot com>
- Cc: "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>
- Date: Wed, 2 Dec 2015 11:34:32 +0000
- Subject: Re: AW: basic asm and memory clobbers - Proposed solution
- Authentication-results: sourceware.org; auth=none
- Authentication-results: gcc.gnu.org; dkim=none (message not signed) header.d=none;gcc.gnu.org; dmarc=none action=none header.from=hotmail.de;
- Spamdiagnosticmetadata: NSPM
- Spamdiagnosticoutput: 1:23
> Surely in code like that, you would make "x" volatile? Memory clobbers
> are not a substitute for correct use of volatile accesses.
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;
x = 1;
x = 0;
gcc -S -O2 test.c gives:
movl $1, x(%rip)
movl $0, x(%rip)
# 6 "test.c" 1
# 0 "" 2
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.