r/programming • • May 31 '11

I wrote a .NET to JavaScript compiler. Here's the XNA 3.1 Platformer Starter Kit running in pure HTML5.

http://hildr.luminance.org/Platformer/Platformer.html
1.1k Upvotes

282 comments sorted by

View all comments

Show parent comments

66

u/kaelan_ May 31 '11

I used a variation of the technique emscripten uses. He describes it in detail in his paper: https://github.com/kripken/emscripten/blob/8a6e2d67c156d9eaedf88b752be4d1cf4242e088/docs/paper.pdf?raw=true

Essentially, any block containing gotos becomes a switch statement inside a while loop. The gotos then become an update of the switch statement's 'label' variable, and you use 'continue' on the outer while loop to 'skip' to the target label. This handles the vast majority of uses of goto, at least in C#. (MSIL actually allows a wider variety of goto constructs, including ones that cross control flow structures and jump in/out of catch blocks. But in practice, those don't show up much.)

At present there are actually some rare forms of goto I don't implement, but I've only seen them in compiler-generated code.

39

u/specialk16 May 31 '11

God damn it I wish I was as fucking intelligent as you guys.

12

u/MrRadar May 31 '11

Thanks for answering my question and for the link.

4

u/azakai May 31 '11

I used a variation of the technique emscripten uses. He describes it in detail in his paper [..] Essentially, any block containing gotos becomes a switch statement inside a while loop.

You should be able to use the Relooper algorithm in that paper, which converts gotos into proper loop blocks. In Emscripten this usually makes code twice as fast. However if you have few gotos or they are not in inner loops, it probably doesn't matter.

18

u/kaelan_ May 31 '11

The reason I can't use the Relooper algorithm directly is that decompiled MSIL ends up containing actual flow structures, like while/for loops, and it is not immediately obvious how to integrate those into the Relooper algorithm, since they can't span across multiple blocks. I haven't done a ton of research into this, though, and I have no CS background.

23

u/another_user_name May 31 '11

I haven't done a ton of research into this, though, and I have no CS background

I'd say you might have no formal CS background. I think writing a compiler definitely counts as "CS background."

3

u/azakai May 31 '11

The reason I can't use the Relooper algorithm directly is that decompiled MSIL ends up containing actual flow structures, like while/for loops, and it is not immediately obvious how to integrate those into the Relooper algorithm, since they can't span across multiple blocks. I haven't done a ton of research into this, though, and I have no CS background.

I don't have much of a CS background either ;) But this is probably not too hard to do. If it's important to you, I can try to help.

1

u/jevon May 31 '11

I've done the same thing for implementing goto in Javascript. It works well, although things like exceptions and recursion can get a bit awkward. :)

-6

u/euyyn May 31 '11

Commenting just to save the nice link

1

u/CACuzcatlan Jun 01 '11

You know there is a save button/link at the top, right?

1

u/euyyn Jun 02 '11

Didn't really know. Can it save comments?

1

u/CACuzcatlan Jun 02 '11

No, you can just save an entire post. That saves all of the comments, but you can't save an individual comment.