1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
|
%\documentclass[11pt]{proc}
\documentclass[preprint,10pt]{sigplanconf}
\usepackage{amsmath}
\usepackage{url}
\usepackage{graphicx}
\begin{document}
\title{Emscripten: An LLVM-to-JavaScript Compiler}
\conferenceinfo{Splash '11}{??-2011, Portland.}
\copyrightyear{2011}
\copyrightdata{[to be supplied]}
\titlebanner{} % These are ignored unless
\preprintfooter{} % 'preprint' option specified.
\authorinfo{Alon Zakai}
{Mozilla}
{azakai@mozilla.com}
\maketitle
%\title{Emscripten: An LLVM-to-JavaScript Compiler}
%\subtitle{}
%\authorinfo{Alon Zakai}
% {Mozilla}
% {azakai@mozilla.com}
%\author{Alon Zakai \\ Mozilla \\ \url{azakai@mozilla.com}}
%\maketitle
\begin{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 available under the MIT
license (a permissive open source license), at \url{http://www.emscripten.org}.
As a compiler from LLVM to JavaScript, the challenges in designing
Emscripten 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.
\end{abstract}
%\category{CR-number}{subcategory}{third-level}
%\terms
%term1, term2
%\keywords
%keyword1, keyword2
\bigskip
%\copyright 2011 Alon Zakai. License: Creative Commons Attribution-ShareAlike (CC BY-SA), \url{http://creativecommons.org/licenses/by-sa/3.0/}
\section{Introduction}
Since the mid 1990's, JavaScript~\cite{js} has been present in most web browsers (sometimes
with minor variations and under slightly different names, e.g., JScript in Internet
Explorer), and today it is
well-supported on essentially all web browsers, from desktop browsers like
Internet Explorer, Firefox, Chrome and Safari, to mobile browsers on 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 allowed doing so, e.g., via the Java
and Flash plugins. However, plugins must be manually installed and do not integrate in
a perfect way with the outside HTML. Perhaps more problematic 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 example, 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 productive in it. As a consequence, there has been work on tools to compile languages
\textbf{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~\cite{gwt}, which compiles Java into JavaScript;
Pyjamas\footnote{\url{http://pyjs.org/}}, which compiles Python into JavaScript;
SCM2JS \cite{hop}, which compiles Scheme to JavaScript,
Links \cite{links}, which compiles an ML-like language into JavaScript;
and AFAX \cite{afax}, which compiles F\# to JavaScript;
see also \cite{ashkenas} 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 consequence the underlying semantics are those of JavaScript,
not Python, so for example division of integers can yield unexpected 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: \textbf{Emscripten},
which compiles LLVM (Low Level Virtual Machine\footnote{\url{http://llvm.org/}}) assembly into JavaScript.
LLVM is a compiler project primarily focused on C, C++ and
Objective-C. It compiles those languages through a \emph{frontend} (the
main ones of which are Clang and LLVM-GCC) into the
LLVM intermediary representation (which can be machine-readable
bitcode, or human-readable assembly), and then passes it
through a \emph{backend} which generates actual machine code for a particular
architecure. Emscripten plays the role of a backend which targets JavaScript.
By using Emscripten, potentially many languages can be
run on the web, using one of the following methods:
\begin{itemize}
\item Compile \textbf{code} in a language recognized by one of the existing LLVM frontends
into LLVM, and then compile that
into JavaScript using Emscripten. Frontends for various languages
exist, including many of the most popular programming languages such as C and
C++, and also various new and emerging languages (e.g., Rust\footnote{\url{https://github.com/graydon/rust/}}).
\item Compile the \textbf{runtime} used to parse and execute code in
a particular language into LLVM, then compile that into JavaScript using
Emscripten. It is then possible to run code in that runtime on the web.
This is a useful approach if
a language's runtime is written in a language for which an LLVM
frontend exists, but the language itself has no such frontend. For
example, there is currently no frontend for Python, however
it is possible to compile CPython -- the standard implementation of
Python, written in C -- into JavaScript, and run Python code on that
(see Section~\ref{sec:examples}).
\end{itemize}
From a technical standpoint, one challenge in designing and implementing
Emscripten is that it compiles a low-level language -- LLVM assembly -- into
a high-level one -- JavaScript. This is somethat the reverse of the usual
situation one is in when building a compiler, and leads to some unique
difficulties. For example, to get good performance in JavaScript one must
use natural JavaScript code flow structures, like loops and ifs, but
those structures do not exist in LLVM assembly (instead, what is present
there is a `soup of code fragments': blocks of code with branching information
but no high-level structure).
Emscripten must therefore reconstruct a high-level
representation from the low-level data it receives.
In theory that issue could have been avoided by compiling a higher-level
language into JavaScript. For example, if compiling Java into JavaScript
(as the Google Web Toolkit does), then one can benefit from the fact
that Java's loops, ifs and so forth generally have a very direct parallel
in JavaScript. But of course the downside in that approach is it yields a
compiler only for Java. In Section~\ref{sec:relooper}
we present the `Relooper' algorithm, which generates high-level loop structures from the low-level
branching data present in LLVM assembly. It is similar to loop recovery algorithms used in decompilation
(see, for example, \cite{Cifuentes98assemblyto}, \cite{pro97}).
The main difference between the Relooper and standard loop recovery algorithms
is that the Relooper generates loops in a different language than that which was compiled originally, whereas
decompilers generally assume they are returning to the original language. The Relooper's
goal is not to accurately recreate the original source code, but rather to generate
native JavaScript control flow structures, which can then be implemented
efficiently in modern JavaScript engines.
Another challenge in Emscripten is to maintain accuracy (that is, to
keep the results of the compiled code the same as the original)
while not sacrificing performance. LLVM assembly
is an abstraction of how modern CPUs are programmed for, and its basic
operations are not all directly possible in JavaScript. For example, if in
LLVM we are to add two unsigned 8-bit numbers $x$ and $y$, with overflowing (e.g., 255
plus 1 should give 0), then there is no single operation in JavaScript which
can do this -- we cannot just write $x+y$, as that would use the normal JavaScript
semantics. It is possible to emulate a CPU in JavaScript, however doing so
is very slow. Emscripten's approach is to allow such emulation, but to try to
use it as little as possible, and to provide tools that help one find out
which parts of the compiled code actually need such full emulation.
We conclude this introduction with a list of this paper's main contributions:
\begin{itemize}
\item We describe Emscripten itself, during
which we detail its approach in compiling LLVM into JavaScript.
\item We give details of Emscripten's Relooper algorithm, mentioned earlier, which generates
high-level loop structures from low-level branching data, and prove
its validity.
\end{itemize}
In addition, the following are the main contributions of Emscripten
itself, that to our knowledge were not previously possible:
\begin{itemize}
\item It allows compiling a very large subset of C and C++ code into
JavaScript, which can then be run on the web.
\item By compiling their runtimes, it allows running languages such as Python
on the web (with their normal semantics).
\end{itemize}
The remainder of this paper is structured as follows. In Section~\ref{sec:compapp} we
describe the approach Emscripten takes to compiling LLVM assembly into JavaScript,
and show some benchmark data.
In Section~\ref{sec:emarch} we describe Emscripten's internal design and in
particular elaborate on the Relooper algorithm.
In Section~\ref{sec:examples} we give several example uses of
Emscripten. In Section~\ref{sec:summary} we summarize and give directions for future
work.
\section{Compilation Approach}
\label{sec:compapp}
Let us begin by considering what the challenge is, when we want to compile LLVM assembly
into JavaScript. Assume we are given the
following simple example of a C program:
\begin{verbatim}
#include <stdio.h>
int main()
{
int sum = 0;
for (int i = 1; i < 100; i++)
sum += i;
printf("1+...+100=%d\n", sum);
return 0;
}
\end{verbatim}
This program calculates the sum of the integers from 1 to 100. When
compiled by Clang, the generated LLVM
assembly code includes the following:
\label{code:examplellvm}
\begin{verbatim}
@.str = private constant [14 x i8]
c"1+...+100=%d\0A\00"
define i32 @main() {
%1 = alloca i32, align 4
%sum = alloca i32, align 4
%i = alloca i32, align 4
store i32 0, i32* %1
store i32 0, i32* %sum, align 4
store i32 1, i32* %i, align 4
br label %2
; <label>:2
%3 = load i32* %i, align 4
%4 = icmp slt i32 %3, 100
br i1 %4, label %5, label %12
; <label>:5
%6 = load i32* %i, align 4
%7 = load i32* %sum, align 4
%8 = add nsw i32 %7, %6
store i32 %8, i32* %sum, align 4
br label %9
; <la
|