This post is also available in: cs
This post is part of a series of articles about STM8 development with OpenSource tools. You can find an overview of the whole toolchain in the post STM8 Development with OpenSource Tools.
Historical solutions
The procedures described in this article are historical solutions. They work, but the projects are unmaintained and in the vast majority of cases you do not need them today — using SPLSPL is enough. I kept the article for completeness and to broaden your horizons, so you can see that the problem can be solved in other ways too.
The SDCC compiler cannot remove
unused (dead) code, so when you use a library such as SPL the resulting
program is needlessly large. The basic solution (the split library SPLSPL)
is described in the main article.
Here are two other ways to remove dead code.
SDCC-gas¶
https://github.com/hainguyen8y/sdcc-gas
The project is most likely dead
The original XaviDCR92/sdcc-gas repository has disappeared from GitHub (404),
so I link to a copy by another user (same history, last commit from 10 Nov 2019).
The original page is also preserved in the
Wayback Machine.
Development stopped long ago and the future of this solution is uncertain. The GNU
Assembler for STM8 that sdcc-gas builds on lives on (as an experimental project) in the
vdudouyt/stm8-gas repository.
This is probably the best way to remove dead code, but at the cost of complications.
The solution was created by adding support for
GNU Assembler
— gas — into SDCC 3.9.0. gas is
both the advantage and disadvantage of this solution. On one hand it means we can
use classic tools from GNU
binutils, on the other hand it
means that those parts of sdcc-libraries written in STM8
assembly cannot be used and you need to use less optimal code written in C or rewrite
the STM8 assembly into GNU assembler. …and occasionally it happens that some internal
functionality (for example multiplication of 64-bit integers) is written only in STM8
assembler and you will have no choice but to work around it or rewrite that function in GNU assembler.
If you want to know more you can read the discussion in issue #2 (archive) or have a look at the stm8-dce-example project (my copy of the original repository). The second related discussion (issue #4) unfortunately has not been preserved.
This is the reason why in the Makefile-sdcc-gas there are these lines:
1 2 3 4 | |
Any problems with missing functions can be fixed by adding the file name
to the SDCC_LIB_SOURCE variable.
Installation¶
You have to build SDCC-gas from source code. It also needs stm8-binutils-gdb. The procedure is described here.
sdccrm¶
sdccrm is a dead code optimization tool for the STM8 SDCC port, which
removes unused functions.
https://github.com/sealj553/sdccrm
The original repository has disappeared
The XaviDCR92/sdccrm repository has disappeared from GitHub (404) and
development is most likely dead. I therefore link to a working copy by another
user. The original state is also preserved in the
Wayback Machine.
How does it work?: The code is first compiled to assembler by classic
SDCC, then the unused code is removed using sdccrm,
the whole process is completed and the code is converted from assembler to machine code.
It is a halfway solution: It works, the machine code is really smaller, but this option excludes the use of a debugger. Sometimes this may bother someone, other times it may not matter.
Also, you need to manually specify the functions you don’t want to “optimize” — meaning remove.
So you need to watch error messages and enter the name of the missing function into the exclude_reference file inside the project directory.
Installation¶
You have to build sdccrm from source code. A Windows binary is part of the
toolchain in the file .make/sdccrm.exe. The procedure is described
here.