Re:Regex Parser in C Using Continuation Passing
by tinkersleep on Wednesday December 20, 2017 @05:13PM
(#15974335)
gcc generates 'jmp *%eax' (and other computed branch instructions) for this code, so yes, it uses tail-calls for function pointers.
(How does that work if the target function has a separate entry point for accepting a tail call?)
If you pile up stack frames while matching regex, you will blow up on long matches. Eg. a+ matching string with thousands or millions of a's.
This could be made not to rely on tail call support by using a:
which is returned from each function. A dispatch loop invokes the tail call by calling fn in the returned structure, passing it data. Needless to say, data can't be the address of some local variable in the function which returns this structure. Obviously, non-tail invocations of the continuations sometimes occur, and those have to be handled differently.