www.digitalmars.com         C & C++   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