August 28th, 2024

Compilation of JavaScript to WASM, Part 3: Partial Evaluation

The article concludes a series on compiling JavaScript to WebAssembly, discussing challenges, proposing a portable interpreter for debugging, and introducing the Futamura projection for program compilation and analysis.

Read original articleLink Icon
Compilation of JavaScript to WASM, Part 3: Partial Evaluation

This article is the final installment of a three-part series by Chris Fallin, focusing on the compilation of JavaScript to WebAssembly (Wasm) with an emphasis on partial evaluation. The previous posts introduced a portable interpreter and discussed ahead-of-time (AOT) compilation. This post delves into the construction of AOT compiler backends, specifically addressing the challenges of compiling JavaScript bytecode to Wasm. The author critiques the direct porting of SpiderMonkey's JIT to Wasm due to operational and security constraints, such as the prohibition of runtime code generation in Wasm environments. Instead, Fallin proposes leveraging a portable interpreter (PBL) that maintains the semantics of JavaScript execution, allowing for easier debugging and development. The article introduces the concept of the first Futamura projection, which combines an interpreter with its input to derive a compiled program. This technique involves constant propagation and specialization, where the interpreter's behavior is simplified based on known constants. However, the complexity increases with the introduction of loops and multiple opcodes, necessitating advanced dataflow analysis techniques to ensure accurate compilation. The author emphasizes the importance of developing robust tooling to facilitate this process and improve debugging capabilities for Wasm bytecode.

- The article concludes a series on compiling JavaScript to WebAssembly, focusing on partial evaluation.

- It critiques direct JIT porting to Wasm due to operational and security limitations.

- The author proposes using a portable interpreter to maintain execution semantics for easier debugging.

- The first Futamura projection is introduced as a method to derive compiled programs from interpreters.

- The complexity of constant propagation and dataflow analysis is highlighted in the context of loops and multiple opcodes.

Related

CPS in Hoot

CPS in Hoot

The Hoot Scheme-to-Wasm compiler uses CPS transformation to handle push calls in WebAssembly. Strategies like generic slicing and CPS transformation are explored, enabling features like Fibers and promise integration. Performance impact is under evaluation.

Dynamic Translation of Smalltalk to WebAssembly

Dynamic Translation of Smalltalk to WebAssembly

The article explores Smalltalk code translation to WebAssembly (WASM) within the Catalyst project. It details levels of Smalltalk, JavaScript, and WASM, focusing on direct Smalltalk to WASM optimization. A new translator, WATCompiledMethodTranslator, aids in this process, exemplified by a Smalltalk expression conversion. Creation of a WASM module for Smalltalk methods is explained, highlighting performance advantages and potential future comparisons with dynamic JS translations. Collaboration in the Smalltalk community is encouraged for further advancement.

Beating the Compiler

Beating the Compiler

The blog post discusses optimizing interpreters in assembly to outperform compilers. By enhancing the Uxn CPU interpreter, a 10-20% speedup was achieved through efficient assembly implementations and techniques inspired by LuaJIT.

Porffor: A from-scratch ahead-of-time JS engine

Porffor: A from-scratch ahead-of-time JS engine

Porffor is an experimental JavaScript engine that compiles code to WebAssembly or native binaries, achieving significant size and performance improvements, but is currently intended for research purposes only.

Compilation of JavaScript to WASM, Part 2: Ahead-of-Time vs. JIT

Compilation of JavaScript to WASM, Part 2: Ahead-of-Time vs. JIT

Chris Fallin's blog post discusses his AOT compiler for JavaScript to WebAssembly, achieving 3-5x performance improvements while addressing challenges like dynamic typing and Wasm platform constraints.

Link Icon 1 comments
By @Klasiaster - about 2 months
I love that his exists without even being in the need of it!