SourceForge has been redesigned. Learn more.
Close

#35 Optimize 2 causes loops to not work

compiler
closed-fixed
NBC (24)
5
2009-01-26
2007-09-21
Andy Shaw
No

If you set the optimization level in Brixcc to be 2 and using the NXC compiler (both internal and external) included with version 3.3.7.17 (NXC version 1.0.1.33), then loops no longer exit correctly. The following program shows the problem:

int line=0;
#define LINE_MAX 8
int lcd[]={LCD_LINE1, LCD_LINE2, LCD_LINE3, LCD_LINE4, LCD_LINE5, LCD_LINE6, LCD_LINE7, LCD_LINE8};

void Log(string str, int num)
{
if(line == 0) ClearScreen();
TextOut(0, lcd[line], str);
NumOut(60, lcd[line], num);
if (++line >= LINE_MAX) line = 0;
}

void LogClear()
{
ClearScreen();
line = 0;
}

task main()
{
int i;

i = 0;
while (i < 10)
{
if (i >=10)
{
Log("Error!", i);

break;
}
i++;
}
Log("Final", i);
for(i=0; i < 10; i++)
{
if (i >= 10)
{
Log("Error!", i);
break;
}
}
Log("Final", i);
Wait(10000);
}

With optimize set to 0 or 1 then the (as expected) the display is two lines each showing "Final 10". With optimize set to 2 then the error display is shown.

This may sound a little odd but if you do not see the problem try switching to the external compiler and try again. For some reason sometimes I do not get the above error when using the internal compiler, but switching to the external compiler seems to make it appear, after that switching back to the internal compiler I again get the error (and changing the optimization level makes it come and go!). Have no idea what may be going on here!

Thanks for the tools. Let me know if you need any more info.

Discussion

  • John Hansen

    John Hansen - 2009-01-26
    • assigned_to: nobody --> afanofosc_99
     
  • John Hansen

    John Hansen - 2009-01-26

    Level 2 optimizations have been disabled in the latest release.

     
  • John Hansen

    John Hansen - 2009-01-26
    • labels: --> NBC
    • milestone: --> compiler
    • status: open --> closed-fixed