News for hackers. Stuff that matters.
Re:Creating a Common Lisp implementation, part 1 [video] by eatonphil on Tuesday August 25, 2020 @08:03PM (#24277692)
I don't understand the point about tail call optimizations not being a part of C... Sure, but you can just rewrite the tail call into a loop and get the exact effect, no?
Re:Creating a Common Lisp implementation, part 1 [video] by betterunix2 on Tuesday August 25, 2020 @08:32PM (#24277847)
No, because TCO only results in a loop when you have recursion, but there are other situations in which TCO can be applied to create an unconditional jump. TCO can also be applied when there is a pointer to a function (jump to a pointer target), and so in theory can be used for the C++ vtable.

In some sense the reverse of what you said would be a better strategy: rewrite loops as tail calls. Doing that as part of the CPS conversion, and combining with TCO and a garbage collector, gives you a simple non-optimizing compiler for functional languages (but theoretically can be used for imperative languages like C, hence the conversion of a loop to a tail call).

Re:Creating a Common Lisp implementation, part 1 [video] by Joker_vD on Wednesday August 26, 2020 @06:28AM (#24281101)
Not unless it's the loop is made out several function calls. For example:

    def even(x):
        return x == 0 ? True : odd(x - 1)

    def odd(x):
        return x == 0 ? False : even(x - 1)

    print(even(17))
Properly implemented TCO means that the call to 'even(17)' should use only one stack frame.
Re:Creating a Common Lisp implementation, part 1 [video] by phoe-krk on Wednesday August 26, 2020 @06:57AM (#24281253)
> Properly implemented TCO

Such is the case e.g. in Erlang/BEAM.

Re:Creating a Common Lisp implementation, part 1 [video] by moonchild on Tuesday August 25, 2020 @08:14PM (#24277761)
I'm not sure what he was talking about, but I can make a guess.

According to the sysv abi, every function has to have a stack frame; so for strict conformance (e.g. for unwinding the stack), you should have a stack frame for every function. In practice, this is kinda dumb. Gcc/clang will do TCO (and won't even generate a stack frame if you pass -fomit-frame-pointer). There's no particular reason why the generated code should abide internally by the sysv abi, only that it be able to call external code written according to said abi (and pass callbacks to said external code, but you'd need thunks for that anyway to account for closures).

Re:Creating a Common Lisp implementation, part 1 [video] by hyperion2010 on Wednesday August 26, 2020 @02:46AM (#24279830)
I just wanted to record for posterity that after 14 years of reading it as fomit frame pointer this is the post where I finally realized that it is -f omit frame pointer. What are are fomit pointers and what do they do!? Well now I know.
Re:Creating a Common Lisp implementation, part 1 [video] by agumonkey on Wednesday August 26, 2020 @08:41AM (#24281861)
And now I wonder what -f orce are
Re:Creating a Common Lisp implementation, part 1 [video] by yvdriess on Wednesday August 26, 2020 @08:56AM (#24281970)
(for context: -fomit-frame-pointer causes the FP to not be saved on the stack, stack unwinding then relies on DWARF or compiler-internal information to unwind the stack.)

I don't see how that would have an effect on TCO, as the storing or not of the FP (beyond in a register) is not part of the calling convention AFAIK. - Only externally linked functions have to conform, so I can understand if TCO does not apply to mutually recursive external functions. Perhaps with LTO it can? - Even an external function that calls itself in tail-position should be TCO-able under -fno-omit-frame-pointer. The compiler reuses the same frame and generates a jump instead of a full function call.

Re:Creating a Common Lisp implementation, part 1 [video] by moonchild on Wednesday August 26, 2020 @10:01PM (#24289456)
> I don't see how that would have an effect on TCO, as the storing or not of the FP (beyond in a register) is not part of the calling convention AFAIK

Sysv abi[1], section 3.2.2:

> In addition to registers, each function has a frame on the run-time stack

So it is part of the calling convention. But again, this is IMO pedantry that doesn't really matter.

> Even an external function that calls itself in tail-position should be TCO-able under -fno-omit-frame-pointer. The compiler reuses the same frame and generates a jump instead of a full function call

Yes, I agree. Just interpreting what I think he meant.

1. https://raw.githubusercontent.com/wiki/hjl-tools/x86-psABI/x...