Looking at the assembly output by GCC with -O2 I see that if I use printf
GCC will create a function named, for example, _Z6printfPKcz
, which then calls __mingw_vprintf
. What is the purpose for this indirection? Why doesn't printf
translate directly to a call to __mingw_printf
? This is the same for the other related functions, such as sprintf.
Edit:
For the record, I know what name mangling is and how it works. My question is why is the compiler generating the function in the first place, a function which does nothing but forward the arguments to __mingw_vprintf, when it could simply call __mingw_printf directly and save the unnecessary indirection.
In other words, why should this:
printf("%d\n", var);
compile to this:
_Z6printfPKcz:
sub rsp, 56
mov QWORD PTR 72[rsp], rdx
lea rdx, 72[rsp]
mov QWORD PTR 80[rsp], r8
mov QWORD PTR 88[rsp], r9
mov QWORD PTR 40[rsp], rdx
call __mingw_vprintf
add rsp, 56
ret
main:
...
call _Z6printfPKcz
...
when this would suffice
main:
...
call __mingw_printf
...
_Z6printfPKcz
is just the mangled name of printf
- search "name mangling" for more information on that. It's the actual printf function.
__mingw_vprintf
is an internal function which printf
calls - there's no requirement that printf
doesn't call another function to do its work. Maybe __mingw_vprintf
handles all the formatting and printing, and printf
is written like this:
int printf(const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
int result = __mingw_vprintf(fmt, va);
va_end(va);
return result;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With