Preface
Without realizing it, it is once again time to dig a new hole. As a
beginner, I want to give myself a chance to push my own learning and
summaries forward, while also giving myself a challenge: analyze the
implementation principles of JavaScriptCore from its
open-source code.
This series will try to be as substantial as possible, and the content may continue to be updated.
Introduction to JavaScriptCore
JavaScriptCore is the default JavaScript
engine in Apple’s open-source WebKit project. It is used to interpret
and execute JavaScript code.
At the beginning, it was developed based on KJS and was an
abstract-syntax-tree-based interpreter, with relatively poor
performance. In 2008, Apple rewrote the compiler and bytecode
interpreter and named it SquirrelFish:
SquirrelFish is a register-based, direct-threaded(bigger code and data, but fast), high-level bytecode engine, with a sliding register window calling convention. It lazily generates bytecodes from a syntax tree, using a simple one-pass compiler with built-in copy propagation.
Source Directory Structure
API: public interface classes exposed by JavaScriptCore.assembler: assembler, used to generate assembly code for different platforms.b3: the backend used by FTL.bytecode: bytecode-related content, such as types and execution process.bytecompiler: compiles bytecode.Configurations: Xcode-related configuration.Debugger: program used for testing scripts.dfg: DFG JIT compiler.disassembler: disassembler.heap: runtime heap and garbage-collection mechanism.ftl: fourth-tier compilation.interpreter: interpreter, responsible for parsing and executing bytecode.jit: converts bytecode into machine code at runtime, doing dynamic just-in-time compilation.llint: Low Level Interpreter, the first tier of the four compilation tiers, responsible for interpreting lower-efficiency bytecode.parser: lexical and syntax analysis, used to build the syntax tree.profiler: information collection, including function-call frequency and time cost.runtime: the full set of runtime operations for JavaScript.wasm: WebAssembly implementation.yarr: Yet Another Regex Runtime, runtime regular-expression parsing.
Core Modules
This section is mainly translated from the official documentation.
JavaScriptCore is a highly optimized virtual machine. It
mainly includes the following modules:
- Lexer: lexical analyzer. It decomposes JavaScript source code into tokens. Source file:
parser/Lexer.cpp.- Parser: syntax analyzer. It generates a parse tree based on tokens from the lexer. It is a handwritten recursive-descent parser. Source file:
parser/Parser.cpp.- LLInt:
Low Level Interpreter, which executes bytecode generated byParser. Its code is underllint/. It uses a portable assembly implementation, also called offlineasm, whose code lives underofflineasm/. It can compile into x86 assembly, ARMv7 assembly, and C code. Apart from lexical and syntax parsing, calls, stack transitions, and register transitions executed by the JIT compiler have almost no startup cost. For example, calling an LLInt function is similar to calling a function compiled to native code, unless the machine-code entry happens to be a shared LLInt prologue. LLInt also includes optimizations such as inline caching to speed up property access.- Baseline JIT: triggered after a function has been called around 6 times, or a section of code has looped around 100 times. It can also be triggered by combinations, such as 3 calls with 50 iterations. These numbers are approximate; the actual heuristics depend on function size and memory state. When JIT is stuck in a loop, it uses
On-Stack Replacement, or OSR, to redirect all callers of the function to newly compiled code.Baseline JITis also the fallback for further optimization. If code cannot be optimized, it can OSR back toBaseline JIT. Baseline JIT code is underjit/. It also performs almost all heap access for inline caching.- DFG JIT: triggered after a function has been called at least around 60 times, or code has looped around 1000 times. Again, these are approximate and heuristic. DFG aggressively uses information collected earlier by the baseline JIT and interpreter to make type speculation, allowing type information to be forward-propagated earlier and reducing many type checks. DFG can also speculate by itself. For example, to enable inlining, it may identify a value loaded from the heap as a known function object. If speculation fails, DFG deoptimizes, also called an OSR exit. Deoptimization may be synchronous, when a type-check branch is executing, or asynchronous, when runtime observes that a value has changed and conflicts with DFG’s assumptions. The latter is also called watchpointing.
Baseline JITandDFG JITshare a bidirectional OSR mechanism: Baseline can OSR into DFG when a function is called frequently, while DFG can OSR back to Baseline during deoptimization. Repeated OSR exits also provide statistics. DFG OSR exits record both frequency and exit reasons, such as failed value-type speculation. After enough exits, reoptimization may be triggered. Callers are redirected back to Baseline JIT, more statistics are collected, and DFG may be invoked again as needed. Reoptimization uses exponential backoff to deal with difficult code. DFG code is underdfg/.- FTL: high-throughput optimizing JIT, short for Faster Than Light. It combines high-level DFG optimization with low-level B3 optimization. It previously meant Fourth Tier LLVM and used LLVM underneath. B3 trims LLVM and adapts it for JavaScriptCore. B3 IR has an interface similar to LLVM IR. The replacement of LLVM with B3 was mainly about reducing memory cost. LLVM is primarily designed for compilers, and ordinary compilers do not have the same memory-pressure incentives as a JIT. B3 IR replaces pointers with more compact integers to represent references. Immutable common information uses fixed-size structures, while other data lives elsewhere. Compact data is stored in arrays, using more arrays and fewer linked lists. The resulting IR uses less memory.
At any moment, functions, eval code blocks, and global code may all
be running simultaneously under LLInt,
Baseline JIT, and DFG. An extreme example is a
recursive function: because it has multiple stack frames, one frame may
be running under LLInt, another under
Baseline JIT, and others under DFG.
An even more extreme case occurs when reoptimization is triggered during execution: one stack frame may still be executing old DFG-compiled code, while another is executing new DFG-compiled code. For this reason, the three tiers are designed to maintain similar execution semantics. Their combined use brings significant performance improvements.
Workflow Diagram
Basic Execution Environment
Code parsed and generated by JSC runs on a virtual machine. Broadly speaking, JSC itself is a virtual machine. JSC uses a register-based VM. Another implementation style is a stack-based VM. The difference can be understood simply as how the instruction set passes parameters: through registers or through the stack.
Compared with a stack-based VM, a register-based VM is more efficient because it avoids frequent push/pop operations and supports ternary operations. Its portability is relatively weaker.
The so-called ternary operation can be seen in add:
add dst, src1, src2. It adds src1 and
src2, then stores the result in dst.
dst, src1, and src2 are all
registers.
Referencing the JSC bytecode specification, here are two examples:
1 | new_array |
1 | add |
Bytecode generated by a stack-based virtual machine looks like this:
1 | 0: iconst_1 |
You can feel the difference between the two approaches.
From JS Script to ByteCode
First, Lexer, Parser, and
ByteCode generation are all completed during
ProgramExecutable initialization. In JSC’s API
evaluate(), a ProgramExecutable is created and
the script code is specified. Then, when passed into
Interpreter, the UnlinkedProgramCodeBlock
obtained through CodeCache is already a
CodeBlock with generated ByteCode.
The following diagram shows the sequence in which
CodeCache calls Parser and
BytecodeGenerator:
Lexer is called during the parsing process:
More specific details will be analyzed in the corresponding follow-up articles.