Understanding V8's Bytecode. V8 Is Google's Open Source JavaScript - by Franziska Hinkelmann - DailyJS - Medium
Understanding V8's Bytecode. V8 Is Google's Open Source JavaScript - by Franziska Hinkelmann - DailyJS - Medium
V8 is Google’s open source JavaScript engine. Chrome, Node.js, and many other
applications use V8. This article explains V8’s bytecode format — which is actually easy
to read once you understand some basic concepts.
https://medium.com/dailyjs/understanding-v8s-bytecode-317d46c94775 1/7
10/29/21, 11:00 AM Understanding V8’s Bytecode. V8 is Google’s open source JavaScript… | by Franziska Hinkelmann | DailyJS | Medium
Ignition! We have lift-off! Interpreter Ignition is part of our compiler pipeline since 2016.
When V8 compiles JavaScript code, the parser generates an abstract syntax tree. A
syntax tree is a tree representation of the syntactic structure of the JavaScript code.
Ignition, the interpreter, generates bytecode from this syntax tree. TurboFan, the
optimizing compiler, eventually takes the bytecode and generates optimized machine
code from it.
If you want to know why we have two execution modes, you can check out my video
from JSConfEU:
https://medium.com/dailyjs/understanding-v8s-bytecode-317d46c94775 2/7
10/29/21, 11:00 AM Understanding V8’s Bytecode. V8 is Google’s open source JavaScript… | by Franziska Hinkelmann | DailyJS | Medium
You can think of V8's bytecodes as small building blocks that make up any JavaScript
functionality when composed together. V8 has several hundred bytecodes. There are
bytecodes for operators like Add or TypeOf , or for property loads like LdaNamedProperty .
https://medium.com/dailyjs/understanding-v8s-bytecode-317d46c94775 3/7
10/29/21, 11:00 AM Understanding V8’s Bytecode. V8 is Google’s open source JavaScript… | by Franziska Hinkelmann | DailyJS | Medium
SuspendGenerator . The header file bytecodes.h defines the complete list of V8’s
bytecodes.
Each bytecode specifies its inputs and outputs as register operands. Ignition uses
registers r0, r1, r2, ... and an accumulator register. Almost all bytecodes use the
accumulator register. It is like a regular register, except that the bytecodes don’t specify
it. For example, Add r1 adds the value in register r1 to the value in the accumulator.
This keeps bytecodes shorter and saves memory.
Many of the bytecodes begin with Lda or Sta . The a in Lda and Sta stands for
accumulator. For example, LdaSmi [42] loads the Small Integer (Smi) 42 into the
accumulator register. Star r0 stores the value currently in the accumulator in register
r0 .
So far the basics, time to look at the bytecode for an actual function.
function incrementX(obj) {
return 1 + obj.x;
If you want to see V8's bytecode of JavaScript code, you can print it by calling D8 or
Node.js (8.3 or higher) with the flag --print-bytecode . For Chrome, start Chrome from the
...
Parameter count 2
Frame size 8
0x2ddf8802cf71 @ Star r0
https://medium.com/dailyjs/understanding-v8s-bytecode-317d46c94775 4/7
10/29/21, 11:00 AM Understanding V8’s Bytecode. V8 is Google’s open source JavaScript… | by Franziska Hinkelmann | DailyJS | Medium
- length: 1
We can ignore most of the output and focus on the actual bytecodes. Here is what each
bytecode means, line by line.
LdaSmi [1]
LdaSmi [1] loads the constant value 1 in the accumulator.
Star r0
Next, Star r0 stores the value that is currently in the accumulator, 1, in the register
r0 .
- length: 1
https://medium.com/dailyjs/understanding-v8s-bytecode-317d46c94775 5/7
10/29/21, 11:00 AM Understanding V8’s Bytecode. V8 is Google’s open source JavaScript… | by Franziska Hinkelmann | DailyJS | Medium
What is the operand with value 4 used for? It is an index of the so-called feedback vector
of the function incrementX() . The feedback vector contains runtime information that is
Return
Return returns the value in the accumulator. That is the end of the function
incrementX() . The caller of incrementX() starts off with 43 in the accumulator and can
further work with this value.
At a first glance, V8’s bytecode might look rather cryptic, especially with all the extra
information printed. But once you know that Ignition is a register machine with an
accumulator register, you can figure out what most bytecodes do.
Learned something? Clap your 👏 to say “thanks!” and help others find this
article.
Note: The bytecode described here is from V8 version 6.2, Chrome 62, and a (not yet
released) version of Node 9. We always work on V8 to improve performance and memory
consumption. In other V8 versions, the details might be different.
https://medium.com/dailyjs/understanding-v8s-bytecode-317d46c94775 6/7
10/29/21, 11:00 AM Understanding V8’s Bytecode. V8 is Google’s open source JavaScript… | by Franziska Hinkelmann | DailyJS | Medium
https://medium.com/dailyjs/understanding-v8s-bytecode-317d46c94775 7/7