CW removes dead code that shouldn't be dead.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

CW removes dead code that shouldn't be dead.

Jump to solution
2,260 Views
Smiffytech
Contributor III

Help, help, I'm being repressed! Er, optimised   This is CodeWarrior 10.2, target is HCS08.

 

For some reason, when I follow a loop with another loop, subsequent loops are getting optimised out, with a message "...ProcessorExpert.c:70:warning:C5660 Removed dead code"


Here is a sample of my code:


/* Write your code here */

/* For example: for(;;) { } */

for(;;)

{

  for (i=1; i<=255; i++)

  {

    LED1R_SetRatio8(i); 

    LED2G_SetRatio8(255-i);

    WAIT1_Waitms(5);

  }

  

  for (i=1; i<=255; i++)

  {

    LED2B_SetRatio8(i); 

    LED1R_SetRatio8(255-i);

    WAIT1_Waitms(5);

  }

}


Why am I not able to have subsequent loops?

 

Labels (1)
Tags (2)
0 Kudos
1 Solution
824 Views
BlackNight
NXP Employee
NXP Employee

Hi Matthew,

Hint: pay close attention to compiler warning and information messages ;-).

I assume your variable 'i' is of type unsigned char?

That means that the variable has a value range of 0..255 (0..0xff).

And if you do a comparison of 'i<=255' in the for() loop, this means that this condition is always TRUE.

As such, you should see a message C4000 Condition always is TRUE (unless you have switched off such messages).

Which means that your first loop never will finish.

:-).

Hope this helps,

Erich

View solution in original post

0 Kudos
2 Replies
825 Views
BlackNight
NXP Employee
NXP Employee

Hi Matthew,

Hint: pay close attention to compiler warning and information messages ;-).

I assume your variable 'i' is of type unsigned char?

That means that the variable has a value range of 0..255 (0..0xff).

And if you do a comparison of 'i<=255' in the for() loop, this means that this condition is always TRUE.

As such, you should see a message C4000 Condition always is TRUE (unless you have switched off such messages).

Which means that your first loop never will finish.

:-).

Hope this helps,

Erich

0 Kudos
824 Views
Smiffytech
Contributor III

Thanks, Erich! I didn't notice any other warnings, but I guess they may have been scrolled off the page.

Now that you point out the error, yes, I can well see what I've done! That's the problem with not being used to typed languages - I've written tens of thousands of lines of Perl over the last few years, but very, very, little C - so it would have taken me a LONG time to spot that, if you hadn't pointed it out!

Whilst I was waiting to see if anyone came up with an answer, I started testing something else - driving the LEDs by setting the PWM values and the delays between changing them from the output of an LFSR, which I generated the code for using lfsr-generator - makes for some very groovy effects! (This project is actually wearable jewellery - LED effects triggered by an MMA7660 accelerometer, for which I am now working on the I2C code.)

Thanks again for your continued support!

0 Kudos