Handmade Network»Forums
Mārtiņš Možeiko
2559 posts / 2 projects
Question about calling convetions and registers
Replying to longtran2904 (#27010)

Yes, caller allocates space. But the part of "waste of time" was about dumping registers to that space. Allocating space does not cost anything. It's moving registers to it is what wastes time (memory accesses, cache pollution).

No idea why they talk about address taking in varargs section.

217 posts
Question about calling convetions and registers
Replying to mmozeiko (#27019)

So the shadow space is before any arguments that are on the stack? And because callers already need to allocate space for some arguments, it doesn't cost them much to push a bigger size on the stack.

183 posts / 1 project
Question about calling convetions and registers
Edited by Dawoodoz on
Replying to longtran2904 (#27020)

Allocating a constant amount of memory (no variable length array) on the stack is just an addition to the stack pointer (using negative values on Intel when the stack space is up-side-down), which will later be used to set the frame/base pointer when making calls. The frame/base pointer is the base address for a local stack space from which fixed memory locations can be found using fixed structure offsets (until you have variable length arrays). The stack pointer tells how much is allocated on the stack to allow allocating variable length arrays from runtime values.

217 posts
Question about calling convetions and registers
Replying to Dawoodoz (#27028)

The stack pointer tells how much is allocated on the stack to allow allocating variable length arrays from runtime values.

Do you mean some things like printf and varargs?

Mārtiņš Možeiko
2559 posts / 2 projects
Question about calling convetions and registers
Replying to longtran2904 (#27029)

Variable length array (VLA) is when you don't know length at compile time. This is C99 feature:

void fun(int n)
{
    char arr[n]; // array for n bytes
    ...
}

Same thing can be accomplished with alloca even in C++:

void fun(int n)
{
    char* arr = (char*)alloca(n); // allocate n bytes on stack
    ...
}