Rafael Dazcal

Strange Compiler behaviour

Discussion created by Rafael Dazcal on Sep 23, 2010
Latest reply on Sep 25, 2010 by CompilerGuru

Greetings,

 

I wanted to report a strange compiler behavior that I noticed more than once, and that is really concerning. Please let me know if anyone has noticed it as well, or why this is hapening.

 

I'm using CW6.2, build 080625 and I'm compiling for MC9S08LC36.

 

Once in a while, I notice that when entering a function, the compiler "forgets" to place the function parameter in the stack. Take this function as an example:

 

 

void perif_LC60_configureSerial (ConfigureSerial *ConfigSerial){        SCIC2 = 0x00;          (void)(SCIS1 == 0);    (void)(SCID == 0);         SCIBDH = ConfigSerial->scibdh;    SCIBDL = ConfigSerial->scibdl;        SCIC1 = ConfigSerial->scic1.registrador;    SCIC3 = ConfigSerial->scic3.registrador;    SCIC2 = ConfigSerial->scic2.registrador; }

 

 

In this function, the compiler only stores the return address in the stack, and not the parameter. So I get e freekish behaviour: (*ConfigSerial) actually points to the return address of the function!! Of course it will have random values.

 

To correct this behaviour, I added the following line before SCIC2 = 0x00:

"ConfigureSerial dummy = (*ConfigSerial);"

 

That is enough for me to force the compiler to recognize the parameter.

 

It looks like a serious compiler bug, and I have seen it in other functions as well. It actually appeared in a function I was not working on, which makes me verry suspicious on the whole program... I might get completly unexpected behaviors!

 

Does anyone has a clue on what the problem is? Is it a compiler optimization issue?

 

I apreciate the help

Outcomes