According to https://learn.microsoft.com/en-us/cpp/build/stack-usage?view=msvc-170, the caller must always allocate sufficient space for the 4 register parameters, even if the callee doesn’t have that many parameters. Space is always allocated for the register parameters, even if the parameters themselves are never homed to the stack.
What's the usage of this shadow space for the 4 register parameters?
I disassembled some programs compiled by VS and G++, and found that the callee saves the register parameters in the shadow space at the beginning. For example, WinMain(HINSTANCE *hInstance, HINSTANCE *hPrevInstance, char *lpCmdLine, int nCmdShow)
function does the following pushing at its beginning:
mov [rsp+arg_18], r9d
mov [rsp+arg_10], r8
mov [rsp+arg_8], rdx
mov [rsp+arg_0], rcx
Why the callee saves the register parameters in the shadow space?
If the callee has to save the register parameters in the stack, why they use registers to pass the parameters instead of passing all the parameters by stack directly?