Emulating continuations in C++?

Started by
12 comments, last by flangazor 19 years, 4 months ago
Is it possible to emulate continuations in C++? If so, how might I go about doing this? I'm not sure how I'd save the contents of the stack to begin with, and this sounds like it'll need to be relegated to assembly.
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
Advertisement
How about some setjmp/longjmp contortions? setjmp will save the local stack, and longjmp will execute a nonlocal goto.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Pardon my ignorance as I don't actually know what a continuation is, although I'm interested now. It sounds like your trying to program using other language idioms in C++.
There are cleaner and more effective ways to program in C++ than resorting to assembly just to emmulate other language features. Trust me, it's better in the long run.

In other words:
"When in Rome... "

"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Even if you could duplicate the stack effects, which would be a non-trivial task in and of itself, I don't think you'd be able to capture effects on heap objects properly. The best you could probably hope for is a clean stack unwind, but that's only a pale shadow of a true continuation.

So no, probably not.
Original post by iMalc
Quote:Pardon my ignorance as I don't actually know what a continuation is, although I'm interested now. It sounds like your trying to program using other language idioms in C++.

A continuation is just a fancy term for a means of representing the executable state of a program at a given point. In a language with stack frames like C++, this means the state of stack. The basic idea is that, instead of having a function return a value, functions take an argument that is another function called the continuation. The result of that function is then given to the continuation as an argument. It's a little bit of a kooky concept, but continuations are basically gotos with parameters.

Here's a more in-depth explanation if you're confuzzled. You can also Google for "continuation language feature", "continuation-passing style", and similar terms.

Quote:There are cleaner and more effective ways to program in C++ than resorting to assembly just to emmulate other language features. Trust me, it's better in the long run.

Rest assured, this is purely an academic exercise. I'm not actually planning to use them in a serious program. I'd just prefer to experiment and practice with them a bit using a language I already know. Similarly, when a professional basketball player wants to practice a new jump shot, he doesn't head for the tennis court or go bowling. ;)
Bleh. The AP is me. The link I neglected to mention is here.
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
GPG2 had a few articles on micro-threading AI that saved off the current state of the stack and could then restore it later. The code in GPG only works on x86, but I got it to work on MIPS also... there's just a whole bunch more registers to save (don't forget saved FPU registers!!! that was a nasty bug.....), and you need to do some extra fancy footwork with the function headers to make sure specify your own preamble (different call/ret convention) and not let the compiler optimize it away (i.e. you need the ret to finally happen at the right time so all descructors get called correctly).

A pretty big pain to get it all working, but it is possible :)
You can edit the source files for GCC and introduce your own keyword which does this.
Simple.
CoV
I know that you can simulate the same (sementicaly and syntaxicaly speaking) exception handling than Java in pure C with setjump, longjump and several macro definitions, so I suppose that you could simulate the continuation in the same way. The issue is that the documentation about the implementation is at home and I'll back home in two month, but I'm sure that it should be possible to googling that, sorry :).
-uto-
- a human beyond the bug -

This topic is closed to new replies.

Advertisement