Tracing JavaScript

Chris Double, Mozilla contributor and excellent programmer, posted a great overview of what the new JavaScript/ActionScript runtime is going to be like in upcoming versions of Mozilla Firefox and Adobe Flash Player. Specifically, how the new tracing portions of the compiler work. I got permission from him to re-post it here – enjoy!

I attended the Tamarin Tech summit at Adobe on Friday. My main interest for attending was to learn more about the tamarin-tracing project. The goal of Tamarin is to produce a high performance ECMAScript 4 implementation.

‘Tamarin Tracing’ is an implementation that uses a ‘tracing jit’. This type of ‘just in time compiler’ traces code executing during hotspots and compiles it so when those hotspots are entered again the compiled code is run instead. It traces each statement executed, including within other function calls, and this entire execution path is compiled. This is different from compiling individual functions. You can gain more information for the optimizer to operate on, and remove some of the overhead of the calls. Anytime the compiled code makes a call to code that has not been jitted, the interpreter is called to continue.

Apparently the JIT for Lua is also being written using a tracing jit method and a post by Mike Pall describes the approach they are taking in some detail and lists references. A followup post provides more information and mentions Tamarin Tracing.

‘Tamarin Tracing’ is open source and can be obtained from the mercurial repository:

$ hg clone

To build the source you create a directory to hold the build files, change to it, and run the configure script:

$ $ mkdir mybuild
$ cd mybuild
$ ../tamarin-tracing/configure --enable-shell
$ make

The ‘enable-shell’ option is required to produce the ‘avmshell’ binary that executes the bytecode. At the end of the build you’ll see the avmshell binary in the shell subdirectory:

$ shell/avmshell
avmplus shell 1.0 build cyclone

‘avmshell’ operates on files containing bytecode not JavaScript. To use it you’ll need to have a front end that compiles JavaScript to the ‘abc’ bytecode format it uses. The bytecode is the ActionScript bytecode. You’ll need a compiler that generates this. This can be obtained from the Flex SDK. This is a free download from Adobe. You can also use any other tool that generates the correct bytecode.

Included with Tamarin Tracing is the source for ‘esc’. This is a work-in-progress implementation of an ECMAScript 4 compiler written in ECMAScript. It generates the ‘abc’ bytecode but is (I think) not quite ready for prime time. In this post I’m using the ‘asc’ compiler from the Flex 2 SDK on Linux. This compiler is written in Java and is in the ‘lib/asc.jar’ file in the SDK.

A quick test that the avmshell program works:

$ echo 'print(\'hello world!\');' >>
$ java -jar asc.jar, 86 bytes written
$ shell/avmshell
hello world!

‘avmshell’ has a number of debugging options that are only available when configuring the build with ‘–enable-debugger’. This allows you to get some information about the trace jit. Here’s the build process with a debug enabled build and the available options:

$ mkdir mybuild
$ cd mybuild
$ ../tamarin-tracing/configure --enable-shell --enable-debugger
$ make
$ shell/avmshell
avmplus shell 1.0 build cyclone

To demonstrate some of the output I’ll use a simple fibonacci benchmark. This is the contents of

function fib(n) {
 if(n <= 1)
  return 1;
  return fib(n-1) + fib(n-2);

print('fib 30 = ' + fib(30));

A comparison of times with and without the tracing jit enabled:

$ shell/avmshell -lifespan -interp
fib 30 = 1346269
Run time was 26249 msec = 26.25 sec
$ shell/avmshell -lifespan
fib 30 = 1346269
Run time was 1967 msec = 1.97 sec

There's a lot of other interesting stuff in the Tamarin Tracing source that I hope to dive into. For example:

  • the interpreter is written in Forth. There are .fs files in the 'core' subdirectory that contains the Forth source code. Each 'abc' bytecode is implemented in lower level instructions which are implemented in Forth. The tracing jit operates on these lower level instructions. The system can be extended with Forth code to call native C functions. The compiler from Forth to C++ is written in Python and is in 'utils/'
  • The jit has two backends. One for Intel x86 32 bit, and the other for ARM. See the 'nanojit' subdirectory.
  • The complete interpreter source can be rebuilt from the Forth using 'core/'. This requires 'asc.jar' to be placed in the 'utils' subdirectory of Tamarin Tracing.

At the summit there was an in-depth session of the internals of the Forth code and how to extend it. I'll write more about that later when/if I get a chance to dig into it.

Posted: February 7th, 2008

Subscribe for email updates

3 Comments (Show Comments)

Comments are closed.
Comments are automatically turned off two weeks after the original post. If you have a question concerning the content of this post, please feel free to contact me.

Secrets of the JavaScript Ninja

Secrets of the JS Ninja

Secret techniques of top JavaScript programmers. Published by Manning.

Ukiyo-e Database and Search

Japanese woodblock print database and search engine.

John Resig Twitter Updates


Infrequent, short, updates and links.