Code is getting optimized,

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

Code is getting optimized,

487 Views
pratikpatil
Contributor I

Hi,

My code is getting optimized, optimization is of value of global variable.

Example given below:

uint8 a = 0;  // Global variable

Function()

{

   a = 5;

   Modify(a);

}

modify(uint8 var)

{

   switch(var)

   {

      case 1:

      case 5: do

      default:

   }

}

We are passing global variable a, while passing value is 5.

But when it enters into called function modify then its value changes and automatically.

Adding a NOP instruction after variable function call resolves this issue, written below:

Function()

{

   a = 5;

   Modify(a);

   NOP

}

Optimization level is 2 and for space.

Can one explain how adding NOP changes the situation and why passed value changes?

0 Kudos
Reply
1 Reply

396 Views
RadekS
NXP Employee
NXP Employee

Hi Pratik,

Unfortunately, your code is too simplified and I cannot simulate it on my side. You also didn’t mention your MCU family.

But I guess that this is just a problem with debugging due to fact that content of modify() (or Modify()) function was directly inlined into Function() code. This may happen when two functions are written consequently and one of them is used only inside the second function. The compiler/linker saves CALL/RTC instructions and space on stack this way. However, it makes problem in C-code debugging since inlined function will not be debugable due to missing CALL/RTC instructions.

 

 

When we add somewhere between first and second function asm NOP; command, the inlining cannot be performed because compiler/linker do not optimize assembler codes. So, there will be still CALL/RTC instructions for calling modify() function.

I hope it helps you.

Have a great day,
Radek

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
Reply