New feature: asms that do not return

Geoff Keating geoffk@envy.cygnus.com
Thu Jan 6 14:19:00 GMT 2000


Zack Weinberg <zack@rabi.columbia.edu> writes:

> Suppose you have an inline function that looks something like this:
> 
> extern inline void __attribute__ ((noreturn)) crash(void)
> {
> 	asm volatile ("illegal");
> }
> 
> The asm, when executed, will cause a fatal signal, so control will never get
> past it, but the compiler doesn't know that.  You get obnoxious warnings (in
> the real-life example I'm working with, the function is in a deeply-nested
> header) and no optimization from the noreturn attribute.

Isn't this exactly what __builtin_trap does?

You can simply write

extern inline void __attribute__ ((noreturn)) crash(void)
{
	__builtin_trap();
}

and the advantage of this is that gcc understands about conditional traps,
so if you write

#define my_assert(expr) ({ if (!(expr)) __builtin_trap(); })

you will get the 'conditional trap' instruction of the architecture
used, assuming it has one.

-- 
- Geoffrey Keating <geoffk@cygnus.com>


More information about the Gcc-patches mailing list