This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Useless assembly



When I run GCC 3.4.3 on this code:
[...]
it generates the assembly code (this is i686 assembly)

When doing such test, you shouldn't test it on the function main(), as it is obviously treated specially. For example, take this example:

int hello()
{
        return(0);
}

int main()
{
        return(0);
}

And compile it with GCC 3.4.3 on x86. It gives (simplified):

hello:  pushl   %ebp
        movl    %esp, %ebp
        movl    $0, %eax
        popl    %ebp
        ret

main:   pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        andl    $-16, %esp
        movl    $0, %eax
        addl    $15, %eax
        addl    $15, %eax
        shrl    $4, %eax
        sall    $4, %eax
        subl    %eax, %esp
        movl    $0, %eax
        leave
        ret

You see the difference.  Note that this was done without any optimization
enabled, the command line was "gcc -c -S a.c"

> Also, when I just compiled a "return 0;", I would expect that to just
> generate main and the instruction "ret" (or is it "retn?").

No, because frame pointers aren't omitted by default.  Compile the
above example with the option "-fomit-frame-pointer" and you get what
you want, but only for hello(), not for main().  And that still without
optimization turned on.  To make main() a bit shorter, the option "-O"
is enough.

There probably are many reasons to not omit those additional instructions
from main.  And it's not necessary either, because main() only gets
executed once.

jlh

Attachment: signature.asc
Description: OpenPGP digital signature


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]