JavaScriptCore Deep Dive 1 - Introduction

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 by Parser. Its code is under llint/. It uses a portable assembly implementation, also called offlineasm, whose code lives under offlineasm/. 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 JIT is also the fallback for further optimization. If code cannot be optimized, it can OSR back to Baseline JIT. Baseline JIT code is under jit/. 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 JIT and DFG JIT share 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 under dfg/.
  • 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

JavaScriptCore Deep Dive 1 - Introduction: Workflow Diagram (figure 1)

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
2
3
4
new_array
Format: new_array dst(r) firstArg(r) argCount(n)

Constructs a new Array instance using the original constructor, and puts the result in register dst. The array will contain argCount elements with values taken from registers starting at register firstArg.
1
2
3
4
add
Format: add dst(r) src1(r) src2(r)

Adds register src1 and register src2, and puts the result in register dst. (JS add may be string concatenation or numeric add, depending on the types of the operands.)

Bytecode generated by a stack-based virtual machine looks like this:

1
2
3
4
5
6
7
8
9
10
11
0:  iconst_1  
1: istore_0
2: iconst_2
3: istore_1
4: iload_0
5: iload_1
6: iadd
7: iconst_5
8: imul
9: istore_2
10: return

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.

JavaScriptCore Deep Dive 1 - Introduction: From JS Script to ByteCode (figure 2)

The following diagram shows the sequence in which CodeCache calls Parser and BytecodeGenerator:

JavaScriptCore Deep Dive 1 - Introduction: From JS Script to ByteCode (figure 3)

Lexer is called during the parsing process:

JavaScriptCore Deep Dive 1 - Introduction: From JS Script to ByteCode (figure 4)

More specific details will be analyzed in the corresponding follow-up articles.

Updating…

-------------本文结束 感谢您的阅读-------------
If this article helped you, your support encourages me to keep writing.

Title:JavaScriptCore Deep Dive 1 - Introduction

Author:lingyun

Published:2018-07-19 00:22

Updated:2026-10-07 00:00

Original link:https://tsuijunxi.github.io/en/2018/07/19/javascriptcore-deep-dive-1-introduction/

License: Attribution-NonCommercial-NoDerivatives 4.0 International Please keep the original link and author when sharing.