Sub topic 1
From Source Text to Bytecode: The Engine Pipeline
Before a JavaScript engine can optimize anything, it has to turn your source text into something it can execute. This chapter follows that path in V8, the engine in Chrome and Node.js, from raw characters to bytecode. Later chapters cover what happens after bytecode exists.
Prerequisites
This chapter assumes you can:
- Write JavaScript functions, objects, and classes.
- Explain the basic difference between compiling and interpreting a program.
- Run a script with Node.js from the command line.
The Stages at a Glance
Scanning
The scanner reads a stream of characters and groups them into tokens such as function, add, (, and +. It also drops whitespace and comments.
Parsing
The parser checks tokens against the language grammar and builds an abstract syntax tree (AST). An AST is a tree of nodes like "function declaration", "return statement", and "binary + expression". During this step V8 also does scope analysis. It decides which variable each name refers to and whether a variable must live in a heap-allocated context because a closure captures it.
Generating bytecode
The bytecode generator walks the AST and emits bytecode. Bytecode is a compact list of instructions for V8's interpreter, Ignition. Once a function has bytecode, its AST is no longer needed and is thrown away.
Lazy Parsing
Parsing every function in a large bundle up front would slow down startup, so V8 parses lazily. A function that does not run immediately gets only a fast preparse. The preparse checks syntax and records which variables the function captures, but it builds no AST and no bytecode. The full parse and bytecode generation happen the first time the function is called.
The catch is that a function preparsed and then called right away gets parsed twice. V8 uses heuristics to spot this. One example is a function wrapped in parentheses, as in an immediately invoked function expression, which V8 parses eagerly.
Seeing Real Bytecode
Save this as add.js:
function add(a, b) {
return a + b;
}
console.log(add(2, 3));Run it with V8's bytecode printer, filtered to one function:
node --print-bytecode --print-bytecode-filter=add add.jsAmong the output you will see instructions roughly like these (exact offsets and formatting vary between V8 versions):
Ldar a1
Add a0, [0]
ReturnLdar a1 loads argument b into the accumulator, a special register that most instructions read from and write to implicitly. Add a0, [0] adds argument a to it. The [0] is a slot index into the function's feedback vector, where V8 records the types it sees. Later chapters show how the optimizing compilers use that feedback.
| Stage | Input | Output |
|---|---|---|
| Scanner | Characters | Tokens |
| Parser | Tokens | AST and scope information |
| Bytecode generator | AST | Ignition bytecode |
Exercise
A script defines 200 functions, but only 10 of them are called during page load. Under lazy parsing, which functions get an AST and bytecode during load, and what happens to the other 190?
Show answer
Only the 10 called functions are fully parsed into an AST and compiled to bytecode. The other 190 are only preparsed, which checks their syntax and records the variables they capture without building an AST or bytecode. If one of them is called later, it gets fully parsed and compiled at that point.
Exercise
Why does the Add instruction in the printed bytecode carry an operand like [0] that is not part of the JavaScript source?
Show answer
The [0] is the index of a feedback vector slot. The interpreter records the operand types it sees at that addition, and the optimizing compilers later read that record to decide how to specialize the code.