I am trying to port an historical functional language interpreter (KRC for EMAS) to modern systems (C for Unix) and it has a garbage collector that expects to be able to scan the stack for pointers into the heap to know which pointers it must relocate when objects in the heap are moved during a GC. For this to work, all function arguments and local variables that point into the heap must be found in the stack.
Now, there was a time when the "register" keyword meant "you can put this variable in a register if you like" and otherwise it was on the stack, but nowadays all (GCC, Clang, Tinyc/tcc) C compilers seem to put local variables into registers regardless, with no way to disable this behaviour and the result is that that the GC is missing out on some values belonging to in-progress functions, failing to preserve them and corrupting the heap.
Is there a way to tell any of these compilers to use the original C semantics, whereby all local variables are on the stack unless you say "register"?
I have a few warty "solutions":
- adding extra code everywhere to take the address of each heap-oriented local variable and passing it to a dummy function, as a way of forcing it to be in a memory location;
- making all static functions global so as to avoid function inlining and the resultant optimising-out of the inlined function's parameters;
- bracketing the GC() function with a stub that pushes all the machine registers onto the stack, calls the real GC() function and then pops them;
which all seem to improve matters, but are awfully hacky and unreliable.
Is there a better way to achieve the required result, of ensuring that all function parameters and local variables will be on the stack?