Pelles C forum

Pelles C => Bug reports => Topic started by: skirby on May 04, 2007, 10:25:47 AM

Title: [OK] Maximize speed optimization problem
Post by: skirby on May 04, 2007, 10:25:47 AM
Hello Pelle,

I don't know if it can be considered as a bug but here is what I have found:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
  int i;
  char s[10];
  strcpy(s, "TEST");

  for (i = 0; i < 2; i++) {
    char c1, c2, r1, r2;

    c1 = s[0]; c2 = s[1];
    r1 = 0; r2 = 0;

    _asm {
      PUSHAD
      MOV AL, BYTE PTR [c1]
      MOV CL, BYTE PTR [c2]
      MOV BYTE PTR [r1], AL
      MOV BYTE PTR [r2], CL
      POPAD
    }
    printf("r1 : %c   ---   r2 : %c\n", r1, r2);
  }


  system("PAUSE");
  return 0;
}


In this small example, the content of r1 and r2 variables is incorrect if optimizations compiler option is Maximize speed.

Build warning :

Building main.obj.
D:\Test\main.c(11): warning #2115: Local 'c1' is initialized but never used.
D:\Test\main.c(11): warning #2115: Local 'c2' is initialized but never used.
Building Test.exe.
Done.

There is no problem if I add c1 and c2 variables in the printf function or if I remove compiler optimization.

Is it normal?

For information, I have done the same test with Visual C++ 2005 and there is no problem.
Title: Re: Maximize speed optimization problem
Post by: frankie on May 04, 2007, 10:59:07 AM
The problem is already known, it is due to the fact that the compiler doesn't check variables use in assembler code.
There should be a precedent answer of mine on this issue, anyway to workaround define the variables c1 and c2 with the qualifier 'volatile' as:
  for (i = 0; i < 2; i++) {
    volatile char c1, c2;
    char r1, r2;

Maybe in next versions Pelle will check for variables use in asm blocks.
Title: Re: Maximize speed optimization problem
Post by: skirby on May 04, 2007, 01:26:45 PM
Thanks a lot for the information :)

Quote from: frankie on May 04, 2007, 10:59:07 AM
Maybe in next versions Pelle will check for variables use in asm blocks.

I hope too.