After a heated discussion, I decided to fix this problem of VLAs
It took me almost the whole day to realize that
1) I am storing the size of ALL VLAs in a hidden local variable
in case somebody calls sizeof(vla), and I need to return that
size. This means most of this problem was solved already.
2) I am executing code anyway when I leave a scope.
THEN:
When leaving a scope with level bigger than function, i.e. an inner
scope, go through all local variables of that scope and see if it
is a VLA.
If it is, look where its size is stored. Read that size and add it to
the stack pointer.
----------------------------------------------------------------
Open questions:
--------------
What happens with:
for (int i = 0; i<100; i++) {
int tab[i*1024];
int *pint = alloca(42);
}
With the current scheme of freeing the VLAs this
will crash.
:-(
This was NOT crashing before. I do not know what to do with
this stuff.
gcc dedicates a register to save the stack position
in such blocks. When the block exits, the whole stack
is restored, what means that the alloca's done are
automatically taken care of correctly.
This is a hell of expensive, since I have only 3 free registers in
the x86 32 bit architecture. This means that I would lose 33% of the
machine dedicated to holding the stack value.
gcc uses esi, and it can (with its bloated "optimize it all"
machinery) remark that this is only used in this block and
can be used elsewhere. I do not do global register allocation,
since it just would bring me a few percent speed gain at enormous
cost.
I would have to dedicate the register within the WHOLE function.
For the time being I will leave it like this. Note that using
alloca within a loop is quite suicidal anyway, since all the storage
can't be reclaimed until the function exit in principle in many
implementations.
I can "cheat out" by writing this in the docs...
"Do not use alloca in blocks using VLAs"
:-)
So, I see the regulars come and start arguing:
"Jacob recommended alloca and it crashes. "
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32