Interview with Scott Petersen on the C/C++ in Flash Player Sneak
Scott Petersen presented a project he’s been working on that takes C/C++ code and turns it into ActionScript that is runnable by the Flash Player. A lot of people wanted more info so I sent him some questions. You can take a look at the demo over on Peter Elst’s site.
Ryan Stewart: So who are you? What do you do at Adobe?
Scott Petersen: I’ve been at Adobe almost 10 years. I worked all but the last 5 months or so in the Acrobat Engineering Organization. My current role involves doing research for the Document and Application Technology Group (DATG). I’m doing research into development tools and methodologies for Adobe’s platforms (Flash, Flex, AIR.)
RS: And you blew people away during Sneaks at MAX right? What exactly did you show?
SP: At MAX I showed a demonstration of some experimental tools I’ve been developing that allow cross compiling C and C++ code into ActionScript that can then be used in Flash, Flex, or AIR apps.
RS: Now before we get started this is just a side project, right? Do we have plans to release this?
SP: It’s currently a side project. However, the considerable excitement around the demos will hopefully fuel my own efforts to eventually have a releasable piece of technology. No promises, but I personally would love to see what cool things the community could do with this kind of tooling.
RS: You showed Quake running in the Flash Player right? So how exactly does it work?
SP: I showed Quake running as an AIR application, though the AIR application was only made up of the application descriptor, a single SWF and Quake’s data file. While the SWF would run in shipping Flash Player 9, I cheated a little and optimized access to ByteArray objects in my AIR runtime. This did not entail any new APIs, though. The Quake SWF will happily run in your Flash 9-enabled browser though the performance won’t be quite what you saw at MAX.
The technology itself is essentially a backend for the LLVM compiler infrastructure that emits ActionScript instead of platform assembly code.
RS: Can you explain a little bit what the LLVM compiler is for those that might not know?
SP: The LLVM compiler infrastructure is an amazing set of programs that provides all of the common command line C and C++ development tools like a compiler, assembler, linker, archiver, etc. However, instead of generating and manipulating platform specific assembly code and object code, these tools operate on a platform neutral “assembly language” and byte code.
RS: So you’ve added/tweaked the LLVM compiler so that your C or C++ code compiles down to ActionScript?
SP: Right. I’ve created an LLVM backend that uses the same underlying mechanism as the platform specific assembly language generators for x86, ARM, PowerPC, etc. But instead of generating “real” assembly language, it generates low level ActionScript.
RS: You mentioned in sneaks that you can get some “faux-multithreading” using asynchronous code in ActionScript? Are there other performance or functionality enhancements that you get by converting C/C++ to ActionScript?
SP: One of the big differences between typical ActionScript and typical C and C++ is asynchronous vs. synchronous code. Most ActionScript code is asynchronous meaning that it responds to events. Most C and C++ code is synchronous meaning that there’s a “main” function that runs for the duration of the program. Naive conversion of C or C++ programs to ActionScript would result in a “main” type function that would execute continuously and not give the Flash Player a chance to draw, respond to keyboard or mouse input, etc. To get around that problem, the backend generates an ActionScript class for every C or C++ function. An instantiation of one of these classes more or less corresponds to a C or C++ function invocation. However, unlike the function invocation in C or C++, the corresponding object instance doesn’t do its processing all at once. It has a “work” method that, when called, does a little bit of work and returns. This allows the generated ActionScript to be effectively suspended and resumed to bubble all the way back out to the Flash player so it can draw, etc. If you imagine several instances of these types of objects, one could call the “work” method on each object in turn simulating multiple threads of execution.
RS: So can you take any C/C++ code and compile it to ActionScript right now using your compiler?
SP: Yes, but there are caveats. Any C or C++ code can be converted to ActionScript, but library calls used by any given piece of C or C++ code may or may not be supported by the tools. For many library calls, if a given call isn’t currently supported, it could be by either writing equivalent calls in pure ActionScript or by porting C or C++ implementations of the call. However, some library calls do things the Flash player / AIR runtime can’t do or won’t do (say, for security reasons). In those cases, the Flash player or AIR runtime would need to be extended.
RS: So will you be able to support other languages besides C/C++?
SP: I mentioned at MAX that C and C++ support effectively means some minimal support for any language that has a C or C++ based interpreter. Ruby, Python, PHP, Squeak and others fall into this category. A non-JITting JVM could go this way as well. As a first pass, just port the interpreter/VM. This doesn’t magically expose the Flash Player / AIR runtime’s object model, but it’s a start. A pretty obvious second step is to map that object model into the scripting environment/VM. There is probably a clever way to do this pretty transparently in many cases. Longer term, direct conversion could be developed as well for optimal performance.
LLVM uses the GCC frontend for C and C++ so pretty much any C or C++ GCC can deal with can be dealt with by the tool. One might imagine that other languages supported by GCC such as Java, Objective-C, etc. could make their way into LLVM as well. The way LLVM is architected, it should require very little work (or none at all) to have ActionScript support for those languages after LLVM brings support.
RS: Since people are excited, how can the community help? Are you looking for feedback on anything?
SP: Primarily, I’m interested in what people see using this technology for. That could be C/C++ libraries (Adobe code or open source), desiring to write Flash/AIR apps from scratch with C/C++ or interest in other languages this tech could enable (like Ruby, Java, etc.)
RS: Is there anything else you’re working on?
SP: I spend a fair bit of my time these days trying to decide which of the various uses of this technology to pursue first. Looking beyond enabling various languages on the Flash/AIR platforms, one could imagine something even crazier like conversion of, say, x86 machine code to ActionScript. Perhaps even dynamically. This is by no means right around the corner, but I’d love to some day be able to “free” code trapped in native libraries that have no corresponding source for use with Flash and AIR.
RS: Awesome! Thanks a lot for taking the time to do this Scott. The community was stoked about the demo, so thanks for giving us some insight into how it works.
[tags]Flash Player, C++, C, ActionScript, Scott Petersen[/tags]
Posted in Flash Player, Rich Internet Applications







October 17th, 2007 at 7:32 pm
Thanks Ryan for this very informative interview.
October 17th, 2007 at 11:27 pm
I’d just like to put a shout out that, as an LLVM developer, we’d love contributions from people to make frontends for other languages!
October 18th, 2007 at 2:40 am
[...] Interview with Scott Petersen on the C/C++ in Flash Player Sneak by Ryan Stewart Published October 18th, 2007 AIR – ex Apollo , Actionscript 3.0 , Flash , Flex , Interview , People Ryan Stewart made really interesting interview with Scott Pettersen from Adobe. Topic is one of the most interesting new stuff that popped out in Adobe MAX in Chicago, C/C++ code able to run in Flash Player. Scott answers many interesting questions giving us picture about current state of project, ideas and technology behind and so on… Great stuff read it at Ryan Stewart’s blog Digital Backcountry [...]
October 18th, 2007 at 7:43 am
Yes, great interview. Clears up most of the confusion. It’s indeed an awesome feature/compiler/converter/etc.
October 18th, 2007 at 10:53 am
[...] Then I read a crazy post about a guy at Adobe that can cross compile C/C++ to Flex. He has used this to make a version of Quake that runs in a browser entirely in Flex! [...]
October 18th, 2007 at 10:19 pm
[...] Οι εφαÏμογÎÏ‚ αυτÎÏ‚ μποÏοÏν να Ï„ÏÎχουν χωÏίς Ï€Ïόβλημα στον νÎο Flash Player 9 που Ï€Ïόκειται να κυκλοφοÏήσει σÏντομα και αν κάποιος μεταφÏάσει ακÏιβώς τα λόγια του δημιουÏÎ³Î¿Ï (που παÏουσίασε Îνα demo της όλης Ï€Ïοσπάθειας, στα πλαίσια του Adobe Max, με τον κώδικα του Quake να Ï„ÏÎχει σε Flash και το πλήθος να παÏαληÏεί) οποιοσδήποτε κώδικας γÏαμμÎνος σε C / C++ μποÏεί να μετατÏαπεί, εκτός και αν κάνει κλήσεις σε εξωτεÏικÎÏ‚ βιβλιοθήκες που μποÏεί να μην υποστηÏίζονται από τα εÏγαλεία. Εξηγώντας πεÏαιτÎÏω την τεχνική κατασκευής αυτών των εÏγαλείων, ο Ryan ανÎφεÏε πως απλά Îκανε κάποιες αλλαγÎÏ‚ στον LLVM (Low Level Virtual Machine) Compiler ώστε αντί να παÏάγει κανονική assembly, παÏάγει low-level Actionscript κώδικα. [...]
October 19th, 2007 at 4:01 am
WHREIO DSKHD kdieh iaufhi sdj jd shdh d sudh hdj dhjj.
October 19th, 2007 at 4:07 am
great interview and very informativ. thanks
October 19th, 2007 at 11:00 am
[...] Interview with, Scott Petersen, the creator of the C/C++ translator into ActionScript 3.0. The sneak peak playing Quake game after converting it from C/C++ into Actionscript made alot of arguments on how powerful AS3.0 is. The MAX sneak peak of playing the game shows Quake running in Flash Player. [...]
October 22nd, 2007 at 9:30 am
[...] You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your ownsite. [...]
October 22nd, 2007 at 4:18 pm
[...] Sounds like crazy Adobe marketing speak doesn’t it? Well this was proposed by a group including Brad Neuberg, Glen Lipka and Alex Russell. None of whom are Adobe fanboys, and open-source advocates to the end. This is totally feasible especially with the advances they’re making in the Flash player and converting C code to Action Script. So there you go our just barely “good enough” technology, Ajax, leveraging the far superior Flash for it’s ubiquity and uniformity to run everywhere. [...]
October 31st, 2007 at 3:51 am
You Tube Flash on C/C++ Sneak Peek http://www.youtube.com/watch?v=0hX-Uh3oTcE
December 3rd, 2007 at 2:54 pm
[...] Referente ao post anterior, o palestrante Scott Petersen deu uma entrevista à um blog em inglês referente ao sneak-peek “C/C++ on Flash”, explicando mais ou menos o que ele havia feito. Você pode ler a entrevista aqui [...]
January 10th, 2008 at 6:30 am
It’d be interesting to know how exactly the access to bytearray was optimized with Air – anyone know what he’s talking about?
I’m working on my own 3d engine for Flash and I’d like to see some optimization tips for this.
February 29th, 2008 at 9:19 am
Stumbled, reviewed and tagged. Great post, very fun and enjoyable to read.
Sounds interesting, I better brush up on my C++.
Is there an exact date or rough time when his “experimental tools” will be released?