[libc][regex] Make derivative iterative (#246333)
derivative() recursed once per nested node, so a deep expression could
overflow the call stack. Commits d159e4f06b81 and 4c0d3e841c0c removed
the same recursion from ExprPool.
Walk the expression with an explicit stack instead:
* DerivFrame: one pending parent node
* DerivStack: 8 frames inline, then a doubling heap array that match()
reuses across bytes
* derivative_impl: makes the same ExprPool calls in the same order as
the recursive code
derivative() can now also return REG_ESPACE when the stack cannot grow.
Optimised x86-64 frames in regex_matcher.cpp stay at or under 232 bytes.
Also move ExprBuilder into a shared test header.
Compliance: Henry Spencer spencer_tests.txt 268/456 Bool/Err (+0) and
84/456 Full Pos (+0); AT&T testregex 399/400 ERE Bool/Err (+0), 401/473
All Bool/Err (+0), and 18/465 Full Pos (+0); zero regressions.
Assisted-by: Automated tooling, human reviewed.