SDCC 3.4.0 does not optimize the unused functions, variables and constants. As a result, the hex file size is 18,8kb, and the IAR generates a hex file size 2,5kb.
-------------- Build: Release in test (compiler: Small Device C Compiler)---------------
sdcc.exe -mstm8 --opt-code-size -I"C:\Program Files\SDCC\include" -c main.c -o obj\Release\main.rel
sdcc.exe -L"C:\Program Files\SDCC\lib" -o bin\Release\test.ihx -mstm8 -DSTM8S003 --opt-code-size -lstm8 --out-fmt-ihx obj\Release\main.rel
Output file is bin\Release\test.ihx with size 18.82 KB
Running project post-build steps
cmd /c "packihx <bin\\Release\\test.ihx>bin\Release\test.hex"
packihx: read 253 lines, wrote 503: OK.
This main.c - used only function LCD_init ()
| #define SYSCLK 1000000UL
|
| #include <stm8 STM8L151K4.h="">
| #include <stm8 STM8_SDCC.h="">
| #include "STM8_KS0108_CFG.h"
| #include "STM8_KS0108.h"
|
|
| void main(void)
| {
| LCD_init();
| while(1);
| }
ASM file size is 115,217kb
Why don't work optimization with STM8 CPU?
Thanks in advance!
P.S. I tried all possible COMMAND LINE OPTIONS and SDCC version with STM8 support. Flash size of my CPU is only 16kb
When i compile with --noinvariant --nolospre i see next:
-------------- Build: Release in test (compiler: Small Device C Compiler)---------------
sdcc.exe -mstm8 -DSTM8S003 --opt-code-size --noinvariant --nolospre -I"C:\Program Files\SDCC\include" -c main.c -o obj\Release\main.rel
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp1. Please contact sdcc authors with source code to reproduce.
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp7. Please contact sdcc authors with source code to reproduce.
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp6. Please contact sdcc authors with source code to reproduce.
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp16. Please contact sdcc authors with source code to reproduce.
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp16. Please contact sdcc authors with source code to reproduce.
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp16. Please contact sdcc authors with source code to reproduce.
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp16. Please contact sdcc authors with source code to reproduce.
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp2. Please contact sdcc authors with source code to reproduce.
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp2. Please contact sdcc authors with source code to reproduce.
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp16. Please contact sdcc authors with source code to reproduce.
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp16. Please contact sdcc authors with source code to reproduce.
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp16. Please contact sdcc authors with source code to reproduce.
Warning: Non-connected liverange found and extended to connected component of the CFG:iTemp16. Please contact sdcc authors with source code to reproduce.
sdcc.exe -L"C:\Program Files\SDCC\lib" -o bin\Release\test.ihx -mstm8 -DSTM8S003 --opt-code-size --noinvariant --nolospre -lstm8 --out-fmt-ihx obj\Release\main.rel
Output file is bin\Release\test.ihx with size 18.72 KB
Running project post-build steps
cmd /c "packihx <bin\\Release\\test.ihx>bin\Release\test.hex"
packihx: read 252 lines, wrote 500: OK.
Process terminated with status 0 (0 minute(s), 4 second(s))
0 error(s), 0 warning(s) (0 minute(s), 4 second(s))
I thought you have encountered two different problems:
For the first problem, I have no idea. For the second, the lack of removal of unused functions & variables is not specific to stm8, but exists on other ports.
I tried the attached a.c with stm8, mcs51 and z80. The unused function sqs() remains on all of them.
There are 2 aspects to removinf unused fuctios:
1) static functions. The compiler could kow if these are called, and could remove them. Currently not implemented.
2) Other functions. The compiler cannot know if they are unused, and thus cannot remove them. The linker can know if they are unused, and can remove them. For that to happen, the unused functions need to be in a library (preferably one function per module, as each module will be pulled in as soon as there is a used function in the module).
Philipp
Do you mean that put each function into an isolated .c file, and then build the lib ? Is it easy for sdld to do the unused functions removal without such a special process?
Yes. One file per function, then build a lib.
Otherwise sdld can't be sure what can be removed. Consider e.g. a module containing two functions, the first called from the program. sdld cannot know if the second can be safely removed, since the first function could reference the second one in ways not visible to sdld (such as relative jumps).
Philipp
Last edit: Philipp Klaus Krause 2014-09-08
I tried gcc with the attached a.c (-O2 level opimization), that the unused function ab1234() was not removed else. So there might be other reasons that code size of IAR is quite smaller than SDCC ? (I have no available IAR to test)
Unused functions eliminated by GCC with `LDOPT := ,--gc-sections' option(stm32 project), full Makefile attached FYI.
Have you tried with LTO (Link Time Optimization) in gcc/ld? Nevertheless, this is no easy feature.
I confirm on pic14 unused static functions are not removed.
For a compiler, this should be be easy feature. I really miss this: adding #if 0 everywhere to test implementations, and updating them coherently, is a pain.
Compilation just fails because device size is exceeded. So it's not just futile feature.
Suggest creating a separate ticket so this issue can be seen under appropriate title.
Opened RFE #452 for this.
Philipp