G++ bug?

Toshiyasu Morita tm@netcom.com
Mon Aug 3 14:45:00 GMT 1998

Compiler: egcs-1998-07-27
    Host: i386-linux
  Target: i386-linux

G++ seems to generate very bizarre and incorrect code for this code sample:

class MyClass;

typedef char (MyClass::*MyClassFuncPtr)( void );

typedef char (*AnyFuncPtr)( MyClass* );

class MyClass
        char A( void ) { return 'a'; }
        char B( void ) { return 'b'; }

void main()
        MyClass Instance;

        MyClassFuncPtr FuncPtr1 = &MyClass::A;

When compiled as "gcc -O2 -S test.cpp" it generates a *huge* amount of
code for some reason, and uses %edx without initializing it:

        .align 4
.globl main
        .type    main,@function
        pushl %ebp
        movl %esp,%ebp
        subl $20,%esp
        xorw %dx,%dx            <- %edx used here without initialization
        orl $-65536,%edx        <-
        pushl %edi
        movl $A__7MyClass,%eax
        pushl %esi
        movl %eax,%ecx
        pushl %ebx
        movl %edx,%ebx
        sarl $16,%ebx
        testl %ebx,%ebx
        jle .L6
        movswl %cx,%eax
        leal -1(%ebp),%esi
        movl (%eax,%esi),%eax
        movl %esi,-16(%ebp)
        movl -8(%eax,%ebx,8),%esi
        movl -4(%eax,%ebx,8),%edi
        movl %esi,-12(%ebp)
        movl %edi,-8(%ebp)
        movl -8(%ebp),%edi
        movl %edi,-20(%ebp)
        jmp .L7
        .align 4
        movl %eax,-20(%ebp)
        leal -1(%ebp),%esi
        movl %esi,-16(%ebp)
        movswl %dx,%edx
        testl %ebx,%ebx
        jle .L8
        movswl -12(%ebp),%eax
        addl %edx,%eax
        jmp .L9
        .align 4
        movl %edx,%eax
        addl -16(%ebp),%eax
        pushl %eax
        movl -20(%ebp),%edi
        call *%edi
        leal -32(%ebp),%esp
        xorl %eax,%eax
        popl %ebx
        popl %esi
        popl %edi
        movl %ebp,%esp
        popl %ebp

Anyone know why this is happening and/or can offer a patch?

Also, sizeof(MyClassFuncPtr) is inexplicably eight bytes?


More information about the Gcc-bugs mailing list