From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 32577 invoked by alias); 20 Sep 2004 05:44:20 -0000 Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org Received: (qmail 32559 invoked from network); 20 Sep 2004 05:44:17 -0000 Received: from unknown (HELO mail.kloo.net) (63.192.214.25) by sourceware.org with SMTP; 20 Sep 2004 05:44:17 -0000 Received: by mail.kloo.net (Postfix, from userid 504) id 447863B0229; Sun, 19 Sep 2004 22:30:08 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by mail.kloo.net (Postfix) with ESMTP id 449843B4545; Sun, 19 Sep 2004 22:30:08 -0700 (PDT) Date: Mon, 20 Sep 2004 07:37:00 -0000 From: To: Nathan Sidwell Cc: Jamie Lokier , gcc@gcc.gnu.org Subject: Re: A question about "memory" clobbers in asm In-Reply-To: <414B0528.1040504@codesourcery.com> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-SW-Source: 2004-09/txt/msg01154.txt.bz2 On Fri, 17 Sep 2004, Nathan Sidwell wrote: > 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'. This definition of memory clobbers is incomplete. A memory clobber is required when the inline assembly either reads from or writes to arbitrary memory or both. Not just "writes to". I've seen cases like this: *ptr = 5; /* basically result = (*ptr == 5) */ __asm__ volatile ("mov.l @%1,%0; mov #5,%2; cmp/eq %1,%2; movt %0" : "r" (result), "r" (temp) : "r" (ptr)); if (!result) { ... } If the memory clobber is omitted, then the scheduler can move *ptr = 5 AFTER the inline assembly fragment, which will result in the incorrect result. So if the inline assembly reads arbitrary memory, then it needs a memory clobber regardless of whether it writes to memory. Toshi