I have a legacy firmware application that requires new functionality. The size of the application was already near the limited flash capacity of the device and the few new funct
If you still need more space than with compile with optimizations
turned on, then look at the generated assembly versus unoptimized code. Then re-write the code where the biggest changes took place so that the compiler generates the same optimizations based on tricky C re-writes with optimization turned off.
For instance, you may have several 'if' statements that make similar comparisons:
if(A && B && (C || D)){}
if(A && !B && (C || D)){}
if(!A && B && (C || D)){}
Then creating anew variable and making some comparisons in advance will save the compiler from duplicating code:
E = (C || D);
if(A && B && E){}
if(A && !B && E){}
if(!A && B && E){}
This is one of the optimizations the compiler does for you automatically if you turn it on. There are many, many others, and you might consider reading a bit of compiler theory if you want to learn how to do this by hand in the C code.