Preface
The previous article covered four kinds of Node
inherited from ScopeNode: ProgramNode,
EvalNode, FunctionNode, and
ModuleProgramNode. Their bytecode generation eventually
reaches emitStatementsBytecode.
Bytecode Instruction Set
Before continuing the analysis, let’s first get a rough sense of what bytecode looks like:
1 | { |
You can see the instruction name and length. The final bytecode is a
collection of instructions like these. The complete instruction set can
be found in JavaScriptCore/bytecode/BytecodeList.json.
emitStatementsBytecode
Now let’s look at the long-awaited
emitStatementsBytecode. It is a method on
ScopeNode.
1 | inline void ScopeNode::emitStatementsBytecode(BytecodeGenerator& generator, RegisterID* dst) |
The type of m_statements is SourceElements.
Its definition is shown below. It is a singly linked list whose node
type is StatementNode. This means the stored nodes can be
StatementNode itself or subclasses of
StatementNode. It also tells us that a
ScopeNode is composed of linked StatementNode
instances.
1 | class SourceElements final : public ParserArenaFreeable { |
The list is traversed, and bytecode is generated for every node:
1 | inline void SourceElements::emitBytecode(BytecodeGenerator& generator, RegisterID* dst) |
The actual type of statementNode can be any of the
following Node types, all inherited from
StatementNode:
1 | IfElseNode |
StatementNode
StatementNode mainly describes language structures:
- Sequential structures:
ClassDeclNode,DeclarationStateMentNode, and so on. - Conditional structures:
IFElseNode,SwitchNode, and so on. - Loop structures:
WhileNode,ForNode,DoWhileNode, and so on.
Next, I will pick several representative nodes and analyze how their bytecode is generated.
Backpatching
Unlike sequential structures, conditional and loop structures do not directly produce concrete logical instructions. They mainly generate jump instructions to control program flow.
When a jump instruction is generated, its destination may not be known immediately. So the target label is left unspecified for the moment. Once the correct target label can be determined, the instruction is patched with that target label. This technique is called backpatching.
Conditional Structures
1 | // Returns the next available temporary register. Registers returned by |
IfElseNode
As mentioned in the parsing article, the parser normalizes
IfStatement inside parseIfStatement. Now let’s
see how it generates bytecode.
1 | void IfElseNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst) |
Here is the SDT for IfElseNode:
SwitchNode
SwitchNode is familiar enough. Let’s look at how it
generates bytecode.
1 | void SwitchNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst) |
switch {} contains multiple case clauses
and possibly a default clause.
1 | enum SwitchType |
1 | switch E |
In the ordinary case, SwitchNode might be translated
sequentially like this, which is relatively inefficient:
JavaScriptCore uses another translation strategy for
SwitchNode. During code generation, if the number of
branches falls within a relatively small range, the conditional jump
sequence can be translated into a more efficient n-way branch.
You can see that the jump table for SwitchNone differs
from the other three modes. Its jump table appears earlier, but there is
no essential difference.
Loop Structures
WhileNode
1 | void WhileNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst) |
Here is the SDT for WhileNode:
break and continue appear often in loops.
Let’s see how their bytecode is generated.
BreakNode
1 | void BreakNode::emitBytecode(BytecodeGenerator& generator, RegisterID*) |
ContinueNode
1 | void ContinueNode::emitBytecode(BytecodeGenerator& generator, RegisterID*) |
Both eventually add a jump instruction, jumping to the
breakTarget or continueTarget of their owning
scope.
You can also see that scopes are popped. This is easy to understand:
break and continue are only valid in specific
contexts, such as loop structures and switch structures.
Once either is triggered, execution may leave the current
switch or loop scope directly.
Sequential Structures
Unlike the previous two categories, sequential structures produce more concrete and detailed bytecode.
ClassDeclNode
The bytecode-generation statement for class declaration node
ClassDeclNode is only one familiar line:
1 | void ClassDeclNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst) |
The type of m_classDeclaration is
ClassExprNode, a subclass of ExpressionNode.
The bytecode generation of this expression will be analyzed in detail
later.
ReturnNode
1 | void ReturnNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst) |
1 | RegisterID* BytecodeGenerator::emitReturn(RegisterID* src) |
You can see that an op_ret instruction is inserted into
instructions, and the following
src->index() is the offset of src in the
virtual register VirtualRegister.
LabelNode
The bytecode for LabelNode is simple. It mainly
generates a jump label.
1 | void LabelNode::emitBytecode(BytecodeGenerator& generator, RegisterID* dst) |
FuncDeclNode
A function declaration is essentially function registration.
1 | void FuncDeclNode::emitBytecode(BytecodeGenerator& generator, RegisterID*) |