Monday, July 7th, 2008

LLVM and running C as well as Python in the browser

Category: JavaScript, Python

Dan Morrill doesn’t like JavaScript 2. His reasoning is a little like the folks who don’t want Java.Next to try to copy features from every other language, but to just be the best static language, and let other languages like Scala, Groovy, JRuby (and the hundreds of others like Fan) go in different directions on the same Java platform.

You could argue the same for the browser platform. Why push JavaScript 2 further than cleaning it up, and instead allow other languages to augment it.

This is where technology such as IronMonkey come in, as well as the work that Scott Peterson is doing, written up here:

Scott Petersen from Adobe gave a talk at Mozilla on a toolchain he’s been creating—soon to be open-sourced—that allows C code to be targeted to the Tamarin virtual machine. Aside from being a really interesting piece of technology, I thought its implications for the web were pretty impressive.

If I followed his presentation right, Petersen’s toolchain works something like this:

  1. A special version of the GNU C Compiler—possibly llvm-gcc—compiles C code into instructions for the Low Level Virtual Machine.
  2. The LLVM instructions are converted into opcodes for a custom Virtual Machine that runs in ActionScript, a variant of ECMAScript and sibling of JavaScript.
  3. The ActionScript is automatically compiled into Tamarin bytecode by Adobe Flash, which may be further compiled into native machine language by Tamarin’s Just-in-Time (JIT) compiler.

The toolchain includes lots of other details, such as a custom POSIX system call API and a C multimedia library that provides access to Flash. And there’s some things that Petersen had to add to Tamarin, such as a native byte array that maps directly to RAM, thereby allowing the VM’s “emulation” of memory to have only a minor overhead over the real thing.

The end result is the ability to run a wide variety of existing C code in Flash at acceptable speeds. Petersen demonstrated a version of Quake running in a Flash app, as well as a C-based Nintendo emulator running Zelda; both were eminently playable, and included sound effects and music.

Posted by Dion Almaer at 8:50 am

4 rating from 23 votes


Comments feed TrackBack URI

It doesn’t really work for the browser though, does it? Unless all browsers end up supporting the same tokenized files. And if they do, that’s pretty much the end of the open web.
This seems more like a technology that stays in Flash forever than something likely to enter the browser world.
Really cool, though. It opens up a ton of possibilities for Flash and AIR apps. Like all those nifty emulated HP calculators that I’d been thinking of porting to Flash and JavaScript after I got rich.
And MAME, of course.

Comment by Nosredna — July 7, 2008

If that ever gets out of Flash it could become interesting but otherwise, Silverlight already runs a variety of languages, including Python and Ruby.

Comment by BertrandLeRoy — July 7, 2008

Yeah, but almost everyone has Flash. I can’t imagine Silverlight getting that kind of installed base even in the next 5 years.

Comment by Nosredna — July 7, 2008

i think there’s a lot of great potential here… Adobe doesn’t have to spread its arms open wide to all the .NET languages… but allowing better programming and cross-compilation from well respected languages like C and C++ is an excellent start, especially since the near-native performance seems to be acceptable (if quake is playable!). And I’m sure more cross-compiler options will follow for other languages, just like the .NET intermediate runtime language spread.
And the point is definitely well made (IMHO) that Flash is at 99% and silverlight won’t be there for a *long* time.

Comment by shadedecho — July 7, 2008

I keep saying it, but I don’t think Silverlight can even try to be a player until there are free development environments for it.
Microsoft says it’ll eventually work in VS Express, but the last time I checked it’s only in Visual Studio proper. Which I’m sure seems fine if you work INSIDE Microsoft’s walls.

Comment by Nosredna — July 7, 2008

grrrr8 post.
i realy like it… :)

Comment by Ajaxester — July 9, 2008

I am waiting for the point where you really can use native java (instead of javascript) in the browser – not in the form of GWT and not in the form of Applets – but a replacement for javascript. Then you could use java, scala, jruby, jython, groovy, or any other jvm language.

Comment by matanlurey — July 9, 2008

Leave a comment

You must be logged in to post a comment.