MCU/DSP F2808 (to be ported to F28335 soon).
CCSv6 Version: 6.0.1.00040 most recent version (last week all downloaded and updated)
OPT=2 in complier setting
Issue: if statement can run code statement even false or true, very strange! ,has to use if else statement with __NOP to make it work correctly
Question: Why if statement run true statement code even condition is false (or true)?, what really happening,
Question : Is there fault in compiler optimization or setting
This is very fundamental bug, never seen anything like this before for 15 year programming (at mid level optimization).
R.
----------------------------------------------------------------------------------------------------------------------------------------
writedata= zTI_ComAdd.All.zWord & 0x0018;
if (writedata == 0x0010)
{
UARTA_dsPIC33_Send_Status();
}
else
{
UARTA_dsPIC33_Protocol_Manager_Set_Status_NACK(); // Set NACK flag.
UARTA_dsPIC33_Send_Status();
return;
}
The above code work, when writedata==0x0010 then it goes to true statement, otherwise false statement.
But….
Below code will not work correctly, in case of true or false condition it both executes false statement!!. I think this may be related to optimization.
writedata= zTI_ComAdd.All.zWord & 0x0018;
if (writedata == 0x0010)
{
}
else
{
UARTA_dsPIC33_Protocol_Manager_Set_Status_NACK(); // Set NACK flag.
UARTA_dsPIC33_Send_Status();
return;
}
The fix is to add Nop statement in true statement
if (writedata == 0x0010)
{
_NOP;
}
else
{
UARTA_dsPIC33_Protocol_Manager_Set_Status_NACK(); // Set NACK flag.
UARTA_dsPIC33_Send_Status();
return;
}
---------------------------------------------------------------------------------------------------------
Especially
if (writedata != 0x0010)
{
UARTA_dsPIC33_Protocol_Manager_Set_Status_NACK(); // Set NACK flag.
UARTA_dsPIC33_Send_Status();
return;
}
Which will not work correctly, the NACK/Status statement executes under both true/false, the below is a correct fix.
if (writedata != 0x0010)
{
UARTA_dsPIC33_Protocol_Manager_Set_Status_NACK(); // Set NACK flag.
UARTA_dsPIC33_Send_Status();
return;
}
else
{
_NOP;
}
Take care!, compiler optimization can do strange thing using OPT=2 which is not that high level setting.
if ((zTI_ComAdd.All.zWord & 0x0018) != 0x0010)
{
UARTA_dsPIC33_Protocol_Manager_Set_Status_NACK(); // Set NACK flag.
UARTA_dsPIC33_Send_Status();
return;
}
else { _NOP;} // Needed to fix bug, for proper operation.
Comment welcome…..