Jump to content
  • Advertisement
Sign in to follow this  
kSquared

Emulating continuations in C++?

This topic is 5053 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
How about some setjmp/longjmp contortions? setjmp will save the local stack, and longjmp will execute a nonlocal goto.

Share this post


Link to post
Share on other sites
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... "

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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. ;)

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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-

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!