Understanding Adobe Alchemy

Adobe recently announced the first public release of a research project code-named Alchemy. Alchemy is a C/C++ to ActionScript compiler that opens up a huge new world for the Flash platform.

Automata Studios has worked closely with engineers over at Adobe for the past few months testing Alchemy and preparing some public samples. We were very happy to help present the public release at the day 2 keynote of Adobe MAX 2008 earlier this week. You can also see some video of me talking about our work porting Ogg Vorbis using Alchemy on Adobe Labs.

There is currently quite a bit of confusion about Alchemy and this article is a quick attempt to answer the most common questions about the technology. First, let’s look at what Alchemy really is and what it is doing.

One of the main pieces of Alchemy is a new backend to the LLVM compiler infrastructure. LLVM is short for Low Level Virtual Machine. The big idea behind LLVM is that the core LLVM compiler will take code in many different languages and compile it into simple RISC-like instructions that are platform neutral. Then, anyone can write a backend to LLVM that can take LLVMs simple instructions and turn them into actual executables for a given platform. Platforms include x64, ARM, PowerPC, and now ActionScript.

You may notice how much ActionScript stands out in that list. All of the rest of the members are CPUs - not programming languages. In fact, if you manage to stop Alchemy mid-compile (when it has created the ActionScript, but not yet compiled it into a SWF) and look at the ActionScript that it creates it will look like some odd hybrid of ActionScript 3 and assembly language. In fact you’ll even see sections that looks suspiciously like inline assembler. This is because the ActionScript compiler that ships with Alchemy does in fact allow for inline AVM2 byte codes.

Essentially when you compile some C or C++ code with Alchemy the end result is a class that is basically a virtual machine written in ActionScript and AVM2 bytecode. This is also why Alchemy generated SWCs are on the large side - they include everything that C or C++ may need such as the C standard library and POSIX support. For example the stringcho example that ships with Alchemy takes 54 lines of C and spits out 27415 lines of ActionScript. This is also why Alchemy can do things like its built-in “green threads” that let’s you run synchronous C asynchronously inside of the Flash Player.

In addition to the LLVM backend, Alchemy includes a set of scripts that are drop-in replacements for standard development tools like make and autoconf. This allows you to use the standard UNIX-style tool chain to create SWFs and SWCs (and thus easily recompile most existing source with Alchemy). The alc-on and alc-off scripts that ship with Alchemy are in fact swapping the standard tools for the Alchemy tools and vice versa.

The output you get from Alchemy is generally a SWC (you can get just a SWF if you want, but usually it won’t be terribly useful on its own). SWCs created from Alchemy can be used in Flex 3 (as long as the build can target FP10), Flex 4 (”Gumbo” - in public preview) and Flash CS4. While I believe there are some switches to make Alchemy compiled code target Flash Player 9, this should be avoided as there were specific changes made to Flash Player 10 that greatly increase runtime speed.

As you may have guessed by now, once C and C++ code is compiled with Alchemy into a SWF it is subject to all of the rules and limitations of a normal SWF. That means you’re still in the security sandbox, all visual output goes through the display list, and all network and file access goes through the existing classes. Under the surface, when you instantiate your Alchemy compiled library, the code is allocating a nice chunk of memory into a ByteArray. This is how Alchemy takes pointers, malloc, free, etc and makes those concepts work in ActionScript.

Knowing that Alchemy is just spitting out the same AVM2 bytecode that Flash and Flex spit out it is pretty confusing how Alchemy code could be faster than standard ActionScript. In fact, it is not faster across the board - just in specific types of operations and when the length of a task can be used to overcome Alchemy’s intrinsic overhead. Now, that’s not to say that Alchemy compiled code is slouchy by default. In fact, because LLVM does a lot of optimizations, whereas Flash and Flex have no optimization step built into their compilers, Alchemy code manages to overcome a lot of it is overhead just on its own.

Now, what are these operations that Alchemy does so well? Memory access and function calls. Alchemy compiled code utilizes new bytecodes added to FP10 for working with ByteArrays - which as you’ll remember are what make up the “RAM” in Alchemy. Function calls are faster because in ActionScript function calls require that their parameters be “boxed” and “unboxed” into and out of objects for each call. Alchemy code doesn’t have to do this.

All of this speediness can be undone though by jumping back and forth between Alchemy code and regular ActionScript. This process is called marshaling and you’ll want to keep it to a minimum. Marshaling is expensive so you’ll want to try to create calls that stay in the C code as long as possible. You’ll also want to try to limit the number of parameters you’re passing back and forth (it’s actually best to work directly with Alchemy’s “RAM” if possible).

As you can see, Alchemy is very powerful, but it is not some magic bullet. More than anything else Alchemy has the potential to drastically speed up the process of bringing existing C/C++ code to the Flash Platform - something that had to be done line-by-line by hand previously. If the ActionScript wrapper around an Alchemy compiled library is well written to take advantage of Alchemy’s strengths it is even possible to get significant speed improvements (up to an order of magnitude or so) over hand-written ActionScript.

That’s it for now, but I plan on writing more about compiling with Alchemy, using GlueGen (a special language you can use with Alchemy to somewhat automate the task of writing C-to-ActionScript glue code), and utlizing Alchemy-compiled libraries within Flash and Flex.

Finally, let me just give a public “THANK YOU!” and “OMFG U RULEZ!” to Scott Petersen(the father of alchemy), Joe Steele, and all of the other employees at Adobe that made Alchemy happen - it’s simply an awesome tool!

20 comments

  • Alchemy is really amazing! But I think that not only the “speed-question” is important but also the fact that Alchemy offers such a huge playground for developers.

    Thanks for sharing those information, I am looking forward to your next article.

  • Alchemy is indeed quite smart at wrapping LLVM bytecode to AVM2. The nice thing is that the new FP10 opcodes can be used in different ways that also benefit other applications :

    Check out http://ncannasse.fr/blog/fast_inverse_square_root for an example.

  • Ed McManus

    Great post, Brandon!

    Please keep sharing your insight; I’m sure the community would appreciate a more thorough technical discussion of the product. (If you have the inclination, of course!)

  • [...] Hall did a nice writeup on Alchemy explaining above better then [...]

  • [...] out the B. Hall’s post Understanding Adobe Alchemy if you wanna know more about it. Flash, [...]

  • Thx for the interesting article.

  • [...] the Doom 1 port on NewGrounds. Alchemy is essentially a C-to-ActionScript bridge, making ports of C/C++ code incredibly simple. This is big news to Flash [...]

  • [...] Oldies, back to the past - 1993 - : souvenez-vous de Doom, le célèbre FPS. Si si, souvenez-vous ! Porté directement à partir du source, il est à présent jouable au travers de flash grâce à Alchemy, un toolkit permettant de compiler du C ou C++ afin de l’utiliser dans du flash. (ca me rappelle le WPF avec Silverlight mais là s’arrête la comparaison….). Bon, cela devient un peu compliqué mais c’est expliqué là. [...]

  • Thanks Branden for your explanation :)

  • [...] Hall has great article on Alchemy, a new technology which compiles C/C++ into Actionscript. This will allow Flash and Flex [...]

  • MyNameIsEarl

    Hi, I’m really curious about this. I just want to ask, if i do a fork() inside the C program, would this compile? If it doesn’t then is it safe to say that there a certain limitations that you can put in your programs and if there are, is there a list of these limitations?

  • Brendan

    Port sqLite to as3 ?

  • Branden Hall

    No, fork() will not work.

    Essentially if it works in the Flash Player it will work with Alchemy, but that’s it. Alchemy doesn’t add any new features to Flash or allow it to escape it’s sandbox.

  • [...] Understanding Adobe Alchemy "Alchemy is a C/C++ to ActionScript compiler that opens up a huge new world for the Flash platform." [...]

  • Branden,

    Great work. Good to hear back from you.

    -abdul

  • [...] Voici un nouveau portage de jeu en C, C++ vers notre plateforme favorite, le Flash Player. Je vous avais déjà parlé d’un portage de Quake 1 de AS3. Il s’agit donc de Doom qui est à présent jouable au travers de flash grâce à Alchemy, un toolkit permettant de compiler du C ou C++ afin de l’utiliser dans le Flash Player. Le tout est assez complexe et est expliqué ici. [...]

  • [...] Now we come to the yes part, of my earlier “yes and no”. Next to the virtual memory opcodes that are currently unsupported from within the AS3 language, Branden Hall states something very disturbing on his blog about Alchemy, “Understanding Adobe Alchemy“. [...]

  • Wow! This is quite exciting. It seems to me that using C/C++ within Flash could really be an amazing thing. However it does bring back the question of machine/OS dependence –which is one big reason we have VMs in the first place…

  • Branden Hall

    Actually, in most cases it shouldn’t - the C/C++ is getting compiled into ActionScript, which is platform neutral!