Preface
After JavaScript source code goes through lexical analysis and syntax analysis, it produces a syntax tree. The next task is to convert that syntax tree into bytecode, which is the subject of this article.
This whole process is essentially a step every programming language must go through: compilation. But unlike many languages such as C, C++, and Objective-C, after JavaScript compilation finishes, it does not generate object code or an executable file stored on disk. The generated bytecode may be interpreted line by line by the virtual machine immediately, or it may be cached and later converted into native code through JIT.
CodeCache
In the opening article, we mentioned the following diagram:
After syntax analysis finishes, it is time for CodeCache
to show up. As the name suggests, it is responsible for caching
CodeBlock.
1 | class CodeCache { |
m_sourceCodeis a container used to storeCodeBlock.CodeCachehas fourgetXXXCodeBlockmethods for retrieving different kinds ofCodeBlock. The first three eventually callgetGlobalCodeBlock.
1 | template <class UnlinkedCodeBlockType, class ExecutableType> |
getFunctionExecutableFromGlobalCode is special:
1 | UnlinkedFunctionExecutable* CodeCache::getFunctionExecutableFromGlobalCode(VM& vm, const Identifier& name, const SourceCode& source, ParserError& error) |
It does not return a CodeBlock; it returns an
UnlinkedFunctionExecutable. However, inside
UnlinkedFunctionExecutable, the method
generateUnlinkedFunctionCodeBlock also calls
BytecodeGenerator::generate:
1 | static UnlinkedFunctionCodeBlock* generateUnlinkedFunctionCodeBlock( |
After following this path, it becomes clear that exploring
BytecodeGenerator is especially important.
BytecodeGenerator
BytecodeGenerator means exactly what its name says: it
generates bytecode. It exposes a class method named
generate. Inside, it calls the constructor to create a
temporary BytecodeGenerator, then calls its parameterless
generate method. The return value indicates whether a parse
error occurred, while the actual bytecode result is stored in the third
parameter, CodeBlock.
1 | template<typename... Args> |
Next, let’s look at several of its private constructors:
1 | class BytecodeGenerator |
BytecodeGenerator needs the following parameters. Except
for DebuggerMode, any one of them could easily take an
entire article to explain, so I will keep this brief.
VM: the virtual machine.Node: one ofProgramNode,FunctionNode,EvalNode, orModuleProgramNode.UnlinkedCodeBlock: corresponding to the node, one ofUnlinkedProgramCodeBlock,UnlinkedFunctionCodeBlock,UnlinkedEvalCodeBlock, orUnlinkedModuleProgramCodeBlock.DebuggerMode: whether it is in debug mode.VariableEnvironment: the variable environment.
Parameter Analysis
VM
As shown by the VMType enum, there are three types of
VM:
1 | enum VMType { Default, APIContextGroup, APIShared }; |
1 | // WebCore has a one-to-one mapping of threads to VMs; |
The definition of VM is shown below. Its constructor is
private and cannot be accessed externally. I also list several members
that I consider important:
1 | class VM : public ThreadSafeRefCounted<VM> { |
ExecState is the object that executes scripts. It is
managed by GlobalObject and records the script execution
context.
Node
In the syntax-analysis article, we mentioned the inheritance graph of
Node. The four kinds of Node discussed here
all inherit from ScopeNode, and ScopeNode
inherits from StatementNode. Obviously, these four node
types are basic statements and are related to scope.
UnlinkedCodeBlock
UnlinkedCodeBlock is a code-management class. It mainly
has four variants: UnlinkedProgramCodeBlock,
UnlinkedFunctionCodeBlock,
UnlinkedEvalCodeBlock, and
UnlinkedModuleProgramCodeBlock. It stores compiled
bytecode. Its inheritance hierarchy is shown below:
There is also a corresponding CodeBlock, mainly used by
LLInt and JIT. Its inheritance hierarchy is
extremely similar to UnlinkedCodeBlock; later articles in
the series will cover it.
VariableEnvironment
VariableEnvironment has a very important member
variable, m_map, which stores marked variables:
1 | Map m_map; |
Its key is of type UniquedStringImpl*, and its value is
the enum type Traits:
1 | enum Traits : uint16_t |
It also has several important methods for capturing and marking variables:
1 | bool hasCapturedVariables() const; |
From these method names and enum values, we can roughly infer that it marks variable context: whether a variable is captured by a closure, imported, exported, and so on.
generate
Now it is time to analyze the core generate
function.
1 | ParserError BytecodeGenerator::generate() |
emitBytecode
The four node types mentioned above all inherit from
ScopeNode, so we can analyze this through polymorphism.
ProgramNode
1 | void ProgramNode::emitBytecode(BytecodeGenerator& generator, RegisterID*) |
1 | static void emitProgramNodeBytecode(BytecodeGenerator& generator, ScopeNode& scopeNode) |
Call stack:
1 | ProgramNode::emitBytecode |
ModuleProgramNode
1 | void ModuleProgramNode::emitBytecode(BytecodeGenerator& generator, RegisterID*) |
ModuleProgramNode and ProgramNode therefore
share the same emitBytecode call stack:
1 | ModuleProgramNode::emitBytecode |
EvalNode
1 | void EvalNode::emitBytecode(BytecodeGenerator& generator, RegisterID*) |
Call stack:
1 | EvalNode::emitBytecode |
FunctionNode
Bytecode generation for FunctionNode is more
complicated. It distinguishes several parse modes:
1 | enum class SourceParseMode : uint8_t { |
The first seven modes all fall under the function category.
GeneratorWrapperFunctionMode and
GeneratorBodyMode are handled separately, while the others
take the default path.
1 | void FunctionNode::emitBytecode(BytecodeGenerator& generator, RegisterID*) |
Both GeneratorWrapperFunctionMode and
GeneratorBodyMode contain Generator, so they
are probably related to the ES6 Generator function.
Briefly:
In form, a Generator function is still an ordinary function, but it
has two characteristics. First, there is an asterisk between the
function keyword and the function name. Second, the
function body uses yield expressions to define different
internal states.
GeneratorBodyMode looks like this:
1 | function* helloWorldGenerator() { |
GeneratorWrapperFunctionMode looks like this:
1 | function *gen(a, b = hello()) |
This is only a brief introduction. I will not go deeper here, because the real focus is bytecode generation.
No matter which of the above nodes calls emitBytecode,
the flow eventually reaches emitStatementsBytecode. After
all, whether it is ProgramNode, EvalNode, or
FunctionNode, it is composed of basic statements.
Therefore, bytecode generation eventually becomes related to the various
Node types discussed in the syntax-analysis article.
It is also reasonable to infer that
emitStatementsBytecode is only a starting point, not the
end. Inside it, many recursive and looping calls may still be waiting
for us.