GCC Explorer - an interactive take on compilation
Posted May 30, 2012 5:04 UTC (Wed) by jzbiciak
(✭ supporter ✭
In reply to: GCC Explorer - an interactive take on compilation
Parent article: GCC Explorer - an interactive take on compilation
GCC (and C compilers in general) can safely assume the existence of the standard library, since it forms part of the environment that strictly conforming programs must adhere to. See page 19:
5 A strictly conforming program shall use only those features of the language and library specified in this International Standard. It shall not produce output dependent on any unspecified, undefined, or implementation-defined behavior, and shall not exceed any minimum implementation limit.
6 The two forms of conforming implementation are hosted and freestanding. A conforming hosted implementation shall accept any strictly conforming program. A conforming freestanding implementation shall accept any strictly conforming program that does not use complex types and in which the use of the features specified in the library clause (clause 7) is confined to the contents of the standard headers <float.h>, <iso646.h>, <limits.h>, <stdarg.h>, <stdbool.h>, <stddef.h>, and <stdint.h>. A conforming implementation may have extensions (including additional library functions), provided they do not alter the behavior of any strictly conforming program.
So, even in a freestanding implementation, there is some minimal amount of library implied (to the extent that the headers listed need under-the-hood support), although the vast majority of it disappears. In the more common hosted implementation (which nearly all GCC targets are, including the one that GCC Explorer compiles for), you've got the full standard C library there.
Nothing stops a conforming hosted compiler implementation from replacing calls to printf with something else (including inlined code), if the behavior of the result is indistinguishable to a correct program. Now, if you were expecting your own implementation of printf() to get called, that's non-standard.
Now GCC does offer the -ffreestanding flag, which would disable the sort of optimization shown up-thread. But in a hosted environment, I see absolutely nothing wrong with said optimization.
On a different note: As I recall, these sorts of printf() optimizations are among the optimizations that NullStone tests for.
to post comments)