Jump to content
Sign in to follow this  
jbremnant

Optimizing for flash space

Recommended Posts

Wanted to start a general discussion on what tricks (if any) you guys use to reduce code size?

On the Launchpad and its family of valueline chips, you get 2Kbytes with 128byte ram.

 

To start, some of my "tricks":

 

- I am working in Linux environment and already building my object files with -Os flag, which optimizes for space rather than performance.

- For frequently called functions, I try to reduce the number of arguments passed into those functions. This reduces the overall number of push/pop ops for each argument on the stack and reduces the amount of .text.

 

I've inspected the assembly code that msp430-gcc compiler produced for some of my functions. I only have cursory experience with assembly on msp430, but it seems you can further optimize for space by writing your functions in assembly by hand? Does CCS or IAR produce more compact code that msp430-gcc?

 

Let me know your thoughts!

Share this post


Link to post
Share on other sites

A few:

 

- Missed out most of the time: Package code in Release mode. Strip debugging symbols out by taking out the -g flag

- Convert frequently used macros to functions.

- Try to use printf for debugging only with an #ifdef flag. Or if you have to use it, make your own by trowing individual characters out the serial port.

- Use right sized variables.

 

I am working in Linux environment and already building my object files with -Os flag, which optimizes for space rather than performance.

Just a thought, sometimes compilers act too smart and may strip out needed code, like critical delay functions, as an example. So use it as long as you test it out.

Share this post


Link to post
Share on other sites

I think most c-compilers are pretty efficient in generating code. Very often, the algorithm used plays a bigger role in code size and speed.

Share this post


Link to post
Share on other sites
I think most c-compilers are pretty efficient in generating code. Very often, the algorithm used plays a bigger role in code size and speed.

 

I agree. Choice of algorithms can make a world of difference when it comes to performance, and they often reduce code complexity and size. Given the constraints and the framework of embedded world, however, I wonder if there are ways to achieve tighter/smaller assembled instructions when going from C -> assembly.

Share this post


Link to post
Share on other sites

happy new year everyone.

 

@jbremnant

 

replace your CFLAGS

 

# original
#CFLAGS=-Os -Wall -mmcu=msp430x2012
# try this one, could further save but need u to put all your 'c' files into one big c file.
#CFLAGS=-mmcu=msp430x2012 -Os -std=gnu99 -W -Wall -pedantic -Wstrict-prototypes -Wundef -funsigned-char -funsigned-bitfields -ffunction-sections -fpack-struct -fshort-enums -ffreestanding --combine -fwhole-program -fno-inline-small-functions -fno-split-wide-types -fno-tree-scev-cprop -Wl,--relax,--gc-sections
# i tried this one, see result below
CFLAGS=-mmcu=msp430x2012 -Os -std=gnu99 -W -Wall -pedantic -Wstrict-prototypes -Wundef -funsigned-char -funsigned-bitfields -ffunction-sections -fpack-struct -fshort-enums -ffreestanding --combine -fno-inline-small-functions -fno-split-wide-types -fno-tree-scev-cprop -Wl,--relax,--gc-sections

 

make clean and make

....

msp430-size -d --format=berkeley noklcd.elf

text data bss dec hex filename

1520 0 20 1540 604 noklcd.elf

 

it may break things if u had strange timing related tweaks in your code. may want to load this and try.

 

after that, it's like OCY said, u will need to play around w/ your logic to trade between data and code, i can see that inside

mintetris.c u have a table that can be reduced, but if it takes more instructions bytes to reduce 28 bytes to 14 bytes, it's not worth it.

Share this post


Link to post
Share on other sites

@simpleavr

 

You the man. You took the trouble of looking at my code from the other thread to see where my code was taking space.

But the code I posted is not the current version I am playing with...

While this post intended to look for optimizations ppl do, I wanted to get your collective knowledge to help finish my project. 8-)

 

I'll surely try your suggestions and let you know.

Yeah, I considered reducing that table, and I didn't think the tradeoff was worth it.

Afterall, my bss and data segments fit under 128 so far.

 

Thanks!

Share this post


Link to post
Share on other sites

@jbremnant

i always does that when i see code, and it's easy under linux, get the source package and try compiler switches via a script, all in command line, no ide.

i didn't spend time to analyze your code, just looking at high-level flow. the CFLAG is just something i used for tight projects and so i gave it a try. it reduces the executable from 1938 to 1520. hope it can also help in your current project w/o breaking anything.

Share this post


Link to post
Share on other sites

@simpleavr,

 

Worked! I was able to reduce my latest code from 2014 bytes all the way down to 1230 bytes.

So far, everything looks like it's working! This is exciting.

Thanks for all your help. :D

 

I also work almost exclusively on Linux - total terminal junkie. All my dev work is done in screen,vim,quick shell/perl scripts.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...