This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
volatile asms are optimized away in gcc-2.96
- To: egcs-bugs at egcs dot cygnus dot com
- Subject: volatile asms are optimized away in gcc-2.96
- From: Mark Kettenis <kettenis at wins dot uva dot nl>
- Date: Thu, 10 Jun 1999 00:21:13 +0200 (CEST)
- CC: roland at frob dot com
In the piece of code at the end of this message, the
asm volatile ("movl %eax, %esp; jmp *%ecx");
is optimized away when -O or -O2 is used (and probably at other
optimization levels too). Compile with `gcc -O -S' and note the
absence of the `jmp' instruction in the generated assembler. This
type of code is used in several places in the i386-specific code for
the Hurd, to switch stacks. Note that the the GCC manual says:
You can prevent an `asm' instruction from being deleted, moved
significantly, or combined, by writing the keyword `volatile' after the
`asm'.
Previous versions of gcc have never optimized away these statements
(I've verified this for gcc 2.8.1, but the same code worked with 2.7 too).
Egcs-1.1.1 also compiles this right.
I'm using a gcc compiled from CVS sources checked out on June 7th. It
reports itself as gcc-2.96 19990530 (experimental).
Mark
void *(*_cthread_init_routine) (void); /* Returns new SP to use. */
static void
init1 (int argc, char *arg0, ...)
{
}
void
init (int *data)
{
void *newsp = (*_cthread_init_routine) ();
*--(int *) newsp = data[-1];
((void **) data)[-1] = &&switch_stacks;
/* Force NEWSP into %ecx and &init1 into %eax, which are not restored
by function return. */
asm volatile ("# a %0 c %1" : : "a" (newsp), "c" (&init1));
return;
switch_stacks:
/* Our return address was redirected to here, so at this point our stack
is unwound and callers' registers restored. Only %ecx and %eax are
call-clobbered and thus still have the values we set just above.
Fetch from there the new stack pointer we will run on, and jmp to the
run-time address of `init1'; when it returns, it will run the user
code with the argument data at the top of the stack. */
asm volatile ("movl %eax, %esp; jmp *%ecx");
/* NOTREACHED */
}