Sunday, April 6, 2014

Optimizing MSP430 Code Size with GCC

I’ve started using the prerelease version of Code Composer Studio 6.0, which comes with a GCC 4.8 compiler support. For now, I’m just working on getting used to how it differs from TI’s compiler.
One of the problems I’ve been having is that adding in unused source files causes the compiled code to balloon in size. For instance, adding in my commonly used libraries caused the binary to take up 3172 bytes for a simple UART loopback program. Removing some files I knew never got included shrunk it to 1344 bytes.
I eventually realized this has to do with how GCC, and possibly all compilers, work. It compiles each source file individually, then links them together. By default, all unused functions in each source file are still included, because it can’t tell what is and isn’t used when compiling each file. This seems somewhat obvious in retrospect, but isn’t it always?
To get around this, you can tell GCC to place each function and data item into its own section using the flags -ffunction-sections and -fdata-sections. This makes it possible for the linker to identify function code and data items after compilation. To actually have the linker optimize unused sections out, you’ll want to add the flag --gc-sections. See the images below for where these options can be checked off in Code Composer Studio.
With these options on, my code size is now 1288 bytes. I’m certain there are other optimizations I could do here to make the code even smaller, but this at least allows me to include source code libraries.



No comments:

Post a Comment