www.digitalmars.com         C & C++   DMDScript  

c++ - code generation/optimizer bug

#include <stdio.h>

char *f(char *c)
{
  return c + 1;
}

void g(char *ptr)
{
  char *p;
  char c;

  c = *ptr;
  *ptr = '\0';
  if (f(ptr))
  {
    *ptr = c;
    p = ptr;
    if (c == '(')
    {
      printf("OK\n");
    }

    *p++ = '\0';
    if (*p != '\0')
    {
      f(p);
    }
  }
}

int main()
{
  g("(abc)");

  return 0;
}


This test-case should print "OK", but it doesn't if compiled with -o+speed

Here ist the generated assembler code:

?g  YAXPAD Z:
		push	EAX
		push	EAX
		push	EBX
		mov	EBX,010h[ESP]
		mov	AL,[EBX]                ; c = *ptr
		push	ESI
		mov	0Ch[ESP],AL
		mov	byte ptr [EBX],0        ; *ptr = '\0';
		push	EBX
		call	near ptr ?f  YAPADPAD Z ; f(ptr);
		add	ESP,4
		test	EAX,EAX
		je	L50
		mov	AL,0Ch[ESP]
		mov	ESI,EBX                 ; p = ptr
		mov	[EBX],AL                ; *ptr = c
		cmp	BL,028h  ; But this one should be "cmp AL, 028h"

		jne	L3E
		push	offset FLAT:_DATA
		call	near ptr _printf
		add	ESP,4


BTW, I have tried it with DMC 8.37.7 and 8.38.2.


bye, Christof

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

...and what have you contributed to the Net?
Oct 26 2003