www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/10/25/12:34:22

Sender: nate AT cartsys DOT com
Message-ID: <36337B38.A191F5FD@cartsys.com>
Date: Sun, 25 Oct 1998 11:25:44 -0800
From: Nate Eldredge <nate AT cartsys DOT com>
X-Mailer: Mozilla 4.05 [en] (X11; I; Linux 2.0.35 i486)
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: Re: -O3 breaks my code: inline assembler & optimization
References: <36237ef5 DOT 671209 AT crispin> <70suri$g3m$1 AT latinum DOT dresearch DOT de>
Reply-To: djgpp AT delorie DOT com

Michael Beck wrote:
> 
> Ignacio García Pérez wrote in message <36237ef5 DOT 671209 AT crispin>...
> [..]
> >I've traced down the problem to the following function:
 
> At least with 2.7.2 it works for me in all combinations, however the problem
> are the asm instructions.
> asm did NOT work like inline assembly in typical x86 compilers. GCC did not
> recognize any instruction
> in the asm statement, it simply add the string you have specified into the
> assembly it generates. That's why
> you have to tell gcc which registers/memory your inline asm uses/changes.
> The way you have specified your
> instructions means: Neither changes registers NOR memory. So, the optimizer
> is free to remove your code!
> The right way is the following:
> 
> __asm __volatile__("pushf"
>     "cli");
> 
> The __volatile__ here tells the compiler, that this code cannot be removed
> and further that it cannot be moved to another
> place. If you specify two asm and both change no register, gcc is free to
> change the execution order of it!

Actually, I think GCC now assumes `volatile' for an asm with no outputs,
which is probably the Right Thing.  But it's certainly safer to use it.

Another thing: Manipulating the stack without the compiler's knowledge
is not such a good thing, especially if you use `-fomit-frame-pointer'. 
Also, there's no good way to tell the compiler that the stack pointer is
not where it thinks.  I would use code more like this (though it's
somewhat less efficient):

unsigned long flags;
asm("pushfl; popl %0" : "=r" (flags));  /* "g" or "m" will also be bad
*/
/* Now disable interrupts or whatever */
asm("pushl %0; popfl" : : "r" (flags) : "cc");

This way the stack is correct everywhere GCC wants to access it.
-- 

Nate Eldredge
nate AT cartsys DOT com

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019