Search code examples
optimizationembeddeddisassemblystm8

How to disable all optimization when using COSMIC compiler?


I am using the COSMIC compiler in the STVD ide and even though optimization is turned of with -no (documentation says "-no: do not use optimizer") some lines of code get removed and cannot have a breakpoint placed upon them, nor are they to be found in the disassembly.

I tried to set -oc (leave removed instructions as comments) which resulted in not even showing the removed lines as comment.

bool foo(void)
{
   uint8_t val;

   if (globalvar > 5)
      val = 0;

   for (val = 0; val < 8; val++)
   {
      some code...
   }

   return true;
}

I do know it seems idiotic to set val to 0 prior to the for loop but lets just assume it is for some reason necessary. When I set no optimization I expect it to be not optimized but insted the val = 0; gets removed without any traces.

I am not looking for a workaround like declaring val volatile whitch solves the problem. I am rather looking for a way to prevent the optimization or at least understand/know what changes are made to my code when compiling.


Solution

  • It is not clear from the manual, but it seems that the -no option prevents assembly level optimisation. It seems possible that the code generator stage that runs before assembly optimisation may perform higher level optimisation such as redundant code removal.

    From the manual:

    -cp

    disable the constant propagation optimization. By default, when a variable is assigned with a constant, any subsequent access to that variable is replaced by the constant itself until the variable is modified or a flow break is encountered (function call, loop, label ...).

    It seems that it is this constant propagation feature that you must explicitly disable.

    It is unusual perhaps, but it appears that this compiler optimises by default, and distinguishes between compiler optimisations and assembler optimisations (performed as the compilation stage), and them makes you switch off each individual optimisation separately.

    To avoid this in the code, rather than switching it off globally, you could initialise val to a non-zero value in this case:

    int val = -1 ;
    

    Then the later assignment to zero will require explicit code. This has the advantage over volatile perhaps in that it will not block optimisations when you do enable them.