[PATCH][MSP430] Implement reduced code size printf and puts functions

Jozef Lawrynowicz jozef.l@mittosystems.com
Mon Apr 15 11:33:00 GMT 2019

For simple, "bare-metal" applications for MSP430, it is not necessary to have
reentrant printf and puts functions. The supporting functions for printf
and puts also support reading/writing from streams.
The code size penalty of including these features for a memory-constrained
device like the MSP430 is significant.

The attached patches implement reduced code size printf and puts functions, by
removing functionality from the nano versions of these functions:
- The functions are not reentrant, and the application is assumed to be
  running in a single thread
- The built-in support for streams in the supporting functions have been
  removed and the functions will always send the string to be printed to
  stdout via <<write>>, with no considerations for buffering.

The first patch only copies newlib/libc/stdio/nano-vfprintf.c to the msp430
libc subdirectory, so that it is clear in patch 2 which modifications have
been made.

The second patch makes the modifications to the printf and puts functions.

The reduced code size implementation of printf and puts can be enabled
in an application by passing "--wrap printf" and "--wrap puts" to the
GNU linker. This will replace references to "printf" and "puts" in user
code with "__wrap_printf" and "__wrap_puts" respectively.
If there is no implementation of these __wrap* functions in user code,
these "tiny" printf and puts implementations will be linked into the
final executable.

The wrapping mechanism is supposed to be invisible to the user:
- A GCC wrapper option such as "-mtiny-printf" will be added to alias
  these wrap commands.
- If the user is unaware of the "tiny" implementation, and chooses to
  implement their own __wrap_printf and __wrap_puts, their own
  implementation will be automatically chosen over the "tiny" printf and
  puts from the library.

Newlib must be configured with --enable-newlib-nano-formatted-io for 
the "tiny" printf and puts functions to be built into the library.

Size reduction examples:
printf("Hello World\n")
  baseline - msp430-elf-gcc gcc-8_3_0-release
     text    data     bss
   5638     214      26
  "tiny" puts enabled
    text    data     bss
     714      90      20

printf("Hello %d\n", a)
  baseline - msp430-elf-gcc gcc-8_3_0-release
    text    data     bss
   10916     614      28

  "tiny" printf enabled
    text    data     bss
    4632     280      20

I regtested the changes in the GCC DejaGNU testsiute with the GCC 8.3.0
release. A significant number of tests use printf, and there were no
regressions when using the tiny versions.

If the patches are acceptable, I would appreciate if someone could commit them
for me, as I do not have write access.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-MSP430-1-2-Copy-prerequisite-file-for-tiny-printf-im.patch
Type: text/x-patch
Size: 18913 bytes
Desc: not available
URL: <http://sourceware.org/pipermail/newlib/attachments/20190415/9ac0a1ea/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0002-MSP430-2-2-Implement-reduced-code-size-tiny-printf-a.patch
Type: text/x-patch
Size: 23833 bytes
Desc: not available
URL: <http://sourceware.org/pipermail/newlib/attachments/20190415/9ac0a1ea/attachment-0001.bin>

More information about the Newlib mailing list