Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Lisp to C compiler

Name: Anonymous 2014-04-19 17:42

Hi! It is me again!

I've found that continuations in C/C++ doesn't require tail-call optimization on the part of the compiler. You can just do setjmp and longjmp when stack gets close to full. This means you can't do stack allocation, but you should allocate everything on heap anyway.

Name: Anonymous 2014-04-21 2:50

>>4
neyaaa, it's actually moar difficult. It depends on the working definition of tail call optimization. This becomes less clear when exotic function call stacks are used. But consider the following le c program.


struct bar {
int x;
int y;
};

int foo(struct bar* m) {
struct bar n;
if (m->x == 0) {
return m->y;
} else if (m->y % 2 == 0) {
m->y += m->x;
m->x--;
return foo(m);
} else {
n.x = m->x - m->y - 1;
n.y = m->x + m->y + 1;
return foo(&n);
}
}


In each of these tail calls, when can n be deallocated from the stack and when can it not? In order to perform a proper tail call, no pointers to stack allocated items in the scope can escape. In this example some analysis can rule out the cases, but with more complex code it becomes harder to trace. If the object can't be deallocated from the stack in a tail call, it can't be considered tail call optimization because with enough iteration the stack will le overflow.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List