www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript

c++ - Optimizer bug (+exception handling)

#include <stdio.h>

struct B
{
  ~B()
  { }

  void h(const B &);
};

inline B operator +(const B &a, const B &b)
{
  B res;
  res.h(a);
  res.h(b);
  return res;
}

struct A
{
  void f();
  B s1;
  B s2;
};

int main()
{
  A a;

  a.f();
  printf("OK\n");
}

void A::f()
{
  if (&(s1 + s2))
  {
    for (int i = 0; i < 1; i++)
    {
      printf("%08x\n", &s2);
    }

    s1 + s2;
  }
}

void B::h(const B &)
{ }


Compile it with "-o+all -Ae" and the output of the program is:

00000001
OK

Of course, 1 isn't the correct address of s2...


Looking at the code:

                xor     EBX,EBX
                lea     ESI,1[EBX]
LC8:            push    ESI
                push    offset FLAT:_DATA[029h]
                call    near ptr _printf

Hmm, seems that the compiler assumes that EBX still contains the this
pointer (but it's also used for the loop counter)...


BTW, two of these instructions could probably be optimised away (just a few
lines before the printf):

                mov     dword ptr -4[EBP],0FFFFFFFFh
                mov     dword ptr -4[EBP],1
                mov     dword ptr -4[EBP],0FFFFFFFFh


bye, Christof

-- 
http://cmeerw.org                                 JID: cmeerw jabber.at
mailto cmeerw at web.de

...and what have you contributed to the Net?
Jan 05 2003