初めに
Node.js には、V8エンジンが生成した JavaScript のバイトコードを表示するオプションがあります。
本記事では、--print-bytecode を使って関数 add(a, b) の実際の命令列を確認します。
変換
script.js
function add(a, b) {
return a + b;
}
add(3, 5);
test@test-ThinkPad-X280:~/kaihatsu/js$ node --print-bytecode --print-bytecode-filter="add" script.js
[generated bytecode for function: add (0x2069ee61d219 <SharedFunctionInfo add>)]
Bytecode length: 6
Parameter count 3
Register count 0
Frame size 0
OSR urgency: 0
Bytecode age: 0
25 S> 0x2069ee61e000 @ 0 : 0b 04 Ldar a1
34 E> 0x2069ee61e002 @ 2 : 39 03 00 Add a0, [0]
38 S> 0x2069ee61e005 @ 5 : a9 Return
Constant pool (size = 0)
Handler Table (size = 0)
Source Position Table (size = 8)
0x2069ee61e009 <ByteArray[8]>
a + bは以下のバイトコードに変換されました。
Ldar a1 # 第二引数`a1`をスタックへ積む。
Add a0, [0] # 第一引数`a0`にスタックから取り出した`a1`の値を加算する。