This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
FW: noreturn atribute and its optimization
- From: boris smidt <borissmidt at hotmail dot com>
- To: "gcc-help at gcc dot gnu dot org" <gcc-help at gcc dot gnu dot org>
- Date: Sun, 1 Nov 2015 11:11:25 +0000
- Subject: FW: noreturn atribute and its optimization
- Authentication-results: sourceware.org; auth=none
- Authentication-results: gcc.gnu.org; dkim=none (message not signed) header.d=none;gcc.gnu.org; dmarc=none action=none header.from=hotmail.com;
- References: <AM3PR07MB066319D9F11993E6A79A0E7BCD2D0 at AM3PR07MB0663 dot eurprd07 dot prod dot outlook dot com>,<AM3PR07MB0663CBA381695F5921BBE0D9CD2D0 at AM3PR07MB0663 dot eurprd07 dot prod dot outlook dot com>
- Spamdiagnosticmetadata: NSPM
- Spamdiagnosticoutput: 1:23
Dear,
I have been playing around with the noreturn atribute and created the following program (i know this is not thr right way to write C):
#include <stdio.h>
static void loop()__attribute__((noreturn,nothrow));
//int main()__attribute__((noreturn,nothrow));
static void loop(int a)
{
int b= a+1;
printf("Hello World %u!\n",b);
loop(b);
}
int main()
{
int b= 1;
printf("Hello World %u!\n",b);
loop(b);
}
I would have expected this program to run forever. But when i looked at the generated assembly code with -O3 it calls the function loop and allocates stack in the loop function and thus crashes.
But i wonder why this function doesn't clear/reset the stack, since it is a "no return function" and thus can never return to older stack references.
When i compiled the program it with -O3, without the atributes, it generates the expected result. i.e. a jump to the function label and no allocating on the stack.
Here is the generated assembly:
with atrributes
.file "main.c"
.section .rdata,"dr"
.LC0:
.ascii "Hello World %u!\12\0"
.text
.p2align 4,,15
.def loop; .scl 3; .type 32; .endef
.seh_proc loop
loop:
pushq %rbx
.seh_pushreg %rbx
subq $32, %rsp
.seh_stackalloc 32
.seh_endprologue
leal 1(%rcx), %ebx
leaq .LC0(%rip), %rcx
movl %ebx, %edx
call printf
movl %ebx, %ecx
call loop
nop
.seh_endproc
.def __main; .scl 2; .type 32; .endef
.section .text.startup,"x"
.p2align 4,,15
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
subq $40, %rsp
.seh_stackalloc 40
.seh_endprologue
call __main
leaq .LC0(%rip), %rcx
movl $1, %edx
call printf
movl $1, %ecx
call loop
nop
.seh_endproc
.def printf; .scl 2; .type 32; .endef
without attributs
.file "main.c"
.def __main; .scl 2; .type 32; .endef
.section .rdata,"dr"
.LC0:
.ascii "Hello World %u!\12\0"
.section .text.startup,"x"
.p2align 4,,15
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
pushq %rbx
.seh_pushreg %rbx
subq $32, %rsp
.seh_stackalloc 32
.seh_endprologue
movl $1, %ebx
call __main
leaq .LC0(%rip), %rcx
movl $1, %edx
call printf
.p2align 4,,10
.L2:
addl $1, %ebx
leaq .LC0(%rip), %rcx
movl %ebx, %edx
call printf
jmp .L2
.seh_endproc
.def printf; .scl 2; .type 32; .endef