Emscripten: An LLVM-to-JavaScript Compiler
Alon Zakai
Mozilla
azakai@mozilla.com
Abstract
We present Emscripten, a compiler from LLVM (Low Level
Virtual Machine) assembly to JavaScript. This opens up
two avenues for running code written in languages other
than JavaScript on the web: (1) Compile code directly into
LLVM assembly, and then compile that into JavaScript using
Emscripten, or (2) Compile a language’s entire runtime into
LLVM and then JavaScript, as in the previous approach, and
then use the compiled runtime to run code written in that
language. For example, the former approach can work for
C and C++, while the latter can work for Python; all three
examples open up new opportunities for running code on the
web.
Emscripten itself is written in JavaScript and is avail-
able under the MIT license (a permissive open source li-
cense), at http://www.emscripten.org. As a compiler
from LLVM to JavaScript, the challenges in designing Em-
scripten are somewhat the reverse of the norm – one must go
from a low-level assembly into a high-level language, and
recreate parts of the original high-level structure of the code
that were lost in the compilation to low-level LLVM. We
detail the methods used in Emscripten to deal with those
challenges, and in particular present and prove the validity
of Emscripten’s Relooper algorithm, which recreates high-
level loop structures from low-level branching data.
1. Introduction
Since the mid 1990’s, JavaScript [5] has been present in
most web browsers (sometimes with minor variations and
under slightly different names, e.g., JScript in Internet Ex-
plorer), and today it is well-supported on essentially all
web browsers, from desktop browsers like Internet Ex-
plorer, Firefox, Chrome and Safari, to mobile browsers on
[Copyright notice will appear here once ’preprint’ option is removed.]
smartphones and tablets. Together with HTML and CSS,
JavaScript forms the standards-based foundation of the web.
Running other programming languages on the web has
been suggested many times, and browser plugins have al-
lowed doing so, e.g., via the Java and Flash plugins. How-
ever, plugins must be manually installed and do not integrate
in a perfect way with the outside HTML. Perhaps more prob-
lematic is that they cannot run at all on some platforms, for
example, Java and Flash cannot run on iOS devices such as
the iPhone and iPad. For those reasons, JavaScript remains
the primary programming language of the web.
There are, however, reasonable motivations for running
code from other programming languages on the web, for ex-
ample, if one has a large amount of existing code already
written in another language, or if one simply has a strong
preference for another language and perhaps is more pro-
ductive in it. As a consequence, there has been work on
tools to compile languages into JavaScript. Since JavaScript
is present in essentially all web browsers, by compiling one’s
language of choice into JavaScript, one can still generate
content that will run practically everywhere.
Examples of the approach of compiling into JavaScript
include the Google Web Toolkit [8], which compiles Java
into JavaScript; Pyjamas
1
, which compiles Python into
JavaScript; SCM2JS [6], which compiles Scheme to JavaScript,
Links [3], which compiles an ML-like language into JavaScript;
and AFAX [7], which compiles F# to JavaScript; see also
[1] for additional examples. While useful, such tools usually
only allow a subset of the original language to be compiled.
For example, multithreaded code (with shared memory) is
not possible on the web, so compiling code of that sort is
not directly possible. There are also often limitations of the
conversion process, for example, Pyjamas compiles Python
to JavaScript in a nearly 1-to-1 manner, and as a conse-
quence the underlying semantics are those of JavaScript, not
Python, so for example division of integers can yield unex-
pected results (it should yield an integer in Python 2.x, but
in JavaScript and in Pyjamas a floating-point number can be
generated).
In this paper we present another project along those lines:
Emscripten, which compiles LLVM (Low Level Virtual
1
http://pyjs.org/
1 2013/5/14
评论0