There have been requests [1] for libgccjit to better support functional programming by supporting the contination-passing style, in which every function "returns" by calling a "continuation" function pointer.
These calls must be guaranteed to be implemented as a jump, otherwise the program could consume an arbitrary amount of stack space as it executed. This patch kit implements this. Patch 1 is a preliminary tweak to calls.c Patch 2 implements a new flag in tree.h: CALL_EXPR_MUST_TAIL_CALL, which makes calls.c try harder to implement a flagged call as a tail-call/sibling call, and makes it issue an error if the optimization is impossible. It doesn't implement any frontend support for setting the flag (instead using a plugin to test it). We had some discussion on the jit list about possibly introducing a new builtin for this, but the patch punts on this issue. Patch 3 implements the libgccjit.h API support to allow client code to set the flag. [1] https://gcc.gnu.org/ml/jit/2016-q2/msg00010.html David Malcolm (3): Introduce can_implement_as_sibling_call_p Implement CALL_EXPR_MUST_TAIL_CALL jit: implement gcc_jit_rvalue_set_bool_require_tail_call gcc/calls.c | 211 +++++++++++++++++---- gcc/cfgexpand.c | 1 + gcc/gimple-pretty-print.c | 2 + gcc/gimple.c | 1 + gcc/gimple.h | 20 ++ gcc/jit/docs/topics/compatibility.rst | 7 + gcc/jit/docs/topics/expressions.rst | 24 +++ gcc/jit/jit-common.h | 1 + gcc/jit/jit-playback.c | 23 ++- gcc/jit/jit-playback.h | 9 +- gcc/jit/jit-recording.c | 60 ++++-- gcc/jit/jit-recording.h | 46 +++-- gcc/jit/libgccjit.c | 20 ++ gcc/jit/libgccjit.h | 13 ++ gcc/jit/libgccjit.map | 5 + gcc/print-tree.c | 2 +- gcc/testsuite/gcc.dg/plugin/must-tail-call-1.c | 22 +++ gcc/testsuite/gcc.dg/plugin/must-tail-call-2.c | 58 ++++++ .../gcc.dg/plugin/must_tail_call_plugin.c | 76 ++++++++ gcc/testsuite/gcc.dg/plugin/plugin.exp | 3 + gcc/testsuite/jit.dg/all-non-failing-tests.h | 10 + .../jit.dg/test-error-impossible-must-tail-call.c | 93 +++++++++ .../jit.dg/test-factorial-must-tail-call.c | 109 +++++++++++ gcc/tree-core.h | 3 + gcc/tree.h | 5 + 25 files changed, 744 insertions(+), 80 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/plugin/must-tail-call-1.c create mode 100644 gcc/testsuite/gcc.dg/plugin/must-tail-call-2.c create mode 100644 gcc/testsuite/gcc.dg/plugin/must_tail_call_plugin.c create mode 100644 gcc/testsuite/jit.dg/test-error-impossible-must-tail-call.c create mode 100644 gcc/testsuite/jit.dg/test-factorial-must-tail-call.c -- 1.8.5.3