gcc forgets to decrease esp after function call

Vladimir Simonov Vladimir.Simonov@acronis.com
Fri Mar 9 12:55:00 GMT 2007


Hi all,

Compiling some complex templated & inlined C++
code by gcc 4.1.1/4.1.2 I've met a problem with -O2
optimization. Objdump'ed text is below.

In line 30 the code calls method which returns
object via shadow pointer(in %esp). Calling
function has "ret 4" at the end as it should be.
But the problem code does not have
"sub 0x4,%esp" to fix esp after the call.

Obviously, codegenerator knows about calling
function prototype(it creates shadow parameter).
But by some reason it does not emit "sub 0x4,%esp"
and uses esp in line 41 as if esp's value is not
changed by called in line 30 function.
If I "sub 0x4,%esp" in line 33 in gdb all is working
fine.

Any hints about workarround the problem will be very
appritiated.

Best regards
Vladimir Simonov


-----------------------------------------------
00000000 <some_function>:
    0:   55                      push   %ebp
    1:   89 e5                   mov    %esp,%ebp
    3:   57                      push   %edi
    4:   56                      push   %esi
    5:   53                      push   %ebx
    6:   83 ec 3c                sub    $0x3c,%esp
    9:   8b 45 08                mov    0x8(%ebp),%eax
    c:   e8 fc ff ff ff          call   d
                         d: R_386_PC32   __i686.get_pc_thunk.bx
   11:   81 c3 02 00 00 00       add    $0x2,%ebx
                         13: R_386_GOTPC _GLOBAL_OFFSET_TABLE_
#include "pragmas.h"
           FORCE_INLINE static typename PrototypeAnalyser<Func>::ReturnType
           Call(ObjectType* object, Func function, const ArgumentsType&)
         {
           return (object->*function)();
   17:   8b 10                   mov    (%eax),%edx
   19:   8b 72 cc                mov    0xffffffcc(%edx),%esi
   1c:   8d 55 d4                lea    0xffffffd4(%ebp),%edx
   1f:   89 55 d0                mov    %edx,0xffffffd0(%ebp)
   22:   01 f0                   add    %esi,%eax
   24:   8b 10                   mov    (%eax),%edx
   26:   89 44 24 04             mov    %eax,0x4(%esp)
   2a:   8b 45 d0                mov    0xffffffd0(%ebp),%eax
   2d:   89 04 24                mov    %eax,(%esp)
   30:   ff 52 08                call   *0x8(%edx)

LARGE AMOUNT OF INLINED TEMPLATED CODE

         }
       };

   33:   8b 4d 14                mov    0x14(%ebp),%ecx
   36:   85 c9                   test   %ecx,%ecx
   38:   74 7c                   je     b6 <>
       }

       void* operator new(size_t itemSize)
       {
         char* mem = reinterpret_cast<char*>(FomatikAlloc(itemSize));
   3a:   8b 45 d8                mov    0xffffffd8(%ebp),%eax
   3d:   8d 7c 00 18             lea    0x18(%eax,%eax,1),%edi
   41:   89 3c 24                mov    %edi,(%esp)
   44:   e8 fc ff ff ff          call
   45: R_386_PLT32       MyAlloc



More information about the Gcc-help mailing list