noreturn function attribute and ret asm instruction

Zack Weinberg zack@codesourcery.com
Tue Aug 26 07:05:00 GMT 2003


Kristis Makris <kristis.makris@asu.edu> writes:

> Thanks for the reply and your insight Jim. I have some questions though.
>
> On Mon, 2003-08-25 at 13:30, Jim Wilson wrote:
>> An extended asm isn't allowed to change the flow of control.  So this is 
>> an invalid asm.
>
> Why is it not allowed to change the flow of control ? Define "invalid".
> I tried using this asm syntax in a function and I was able to change the
> flow of control.

The optimizer does not know what you are doing, and *will*
mis-optimize the function.

It is currently impossible to do what you want.  (I once proposed that
clobbering "pc" in an asm() should indicate that control did not pass
beyond that asm(), but it got shot down.)

>> noreturn does not suppress the ret instruction at the end of a function. 
>
> Why not ?

Because attribute noreturn has no effect on code generation of the
function it is applied to.  Its effect is to allow better optimization
of the places where that function is called, and that's all it does.

> The question I have is what happens to the stack when function A makes a
> call to function B (which is flagged noreturn) ? Is a return address
> pushed on the stack ? I would assume one should not, since the function
> called (B) will not return, thus this would not be a "call" but a "jmp".

Depending on the platform ABI and the skill of the optimizer, the
instruction generated may be either "jmp" or "call"; you cannot count
on either.

> In effect, what I am trying to accomplish is write a C function that can
> be called without pushing a return address on the stack. And that can be
> accomplished if the function is either jmp'ed to, or flagged to be
> inlined. Inlined won't work for me as I need to be able to memcpy during
> runtime the code generated for this function, and that may be impossible
> to do if the function is inlined and further optimized differently per
> case where it is inlined.

It begins to sound like you should be writing raw assembly language in
an .s file.

> Perhaps I'm looking at the wrong direction. How do I generate "a chunk
> of code" with no ret instruction at the end if I want to write that code
> in C ?

There is no way to do this for arbitrary code.  The optimizer *may*
notice that the epilogue is unreachable and not bother generating code
for it but you cannot rely on that.

> Regardless, gcc should be able to see that a function flagged as
> noreturn does not return if there's a jmp instruction in it that
> branches somewhere else.

gcc does not know what your asm() statement means, other than what you
tell it with input and output constraints and clobbers.

zw



More information about the Gcc-bugs mailing list