GCC Explorer - an interactive take on compilation
Posted May 24, 2012 21:20 UTC (Thu) by
gutschke (subscriber, #27910)
In reply to:
GCC Explorer - an interactive take on compilation by Richard_J_Neill
Parent article:
GCC Explorer - an interactive take on compilation
It is relatively easy for GCC and glibc to come up with a new extension. This extension would allow the compiler to cleanly detect whenever it is compiling code that eventually executes in a standard run-time environment.
All we need is a new function attribute to annotate the declaration of the printf() function. For example:
extern int printf(const char * restrict format, ...)
__attribute__((__format__(__printf__, 1, 2),
__runtime_function__(__printf__)));
I do not believe, this is how it is done today. But it would be easy to add to both GCC and glibc. It would allow the compiler to do optimizations, whenever it finds a suitable function declaration in <stdio.h>. But if it builds for a non-standard run-time environment, it would automatically skip doing these optimizations.
Of course, that raises the question whether it should ever try to do these optimizations in the first place. And arguably, the answer is more difficult than a simple "yes" or "no".
For example, it is quite common to have code that uses the same format string multiple times just with different arguments. If GCC always expanded the arguments, it would lose the ability to coalesce the identical format strings. For some programs, the extra space requirements in the .data segment could very well be more painful than the minute cost of computing the output at run-time. In fact, I have a hard time envisioning any program where the cost of printf() is noticeable at all.
(
Log in to post comments)