Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Node.jsでV8バイトコードを観察する - 3つのJavaScript実験

Node.jsでV8バイトコードを観察する - 3つのJavaScript実験

JSConf JP 2026の発表資料です
https://jsconf.jp/2026/ja/talks/roland-richard

Richard Rowland

November 23, 2026

Transcript

41 slides
  1. 登壇歴 コンパイラーに興味があります Pythonコンパイラーの解説 PyCon Mini Kumamoto / Pycon Portugal /

    Pycon Ireland PyCascades (Canada) Goコンパイラーの解説 GoLab (Italy) / Go Conference mini KYOTO 人工知能における「知能」の定義について AI博覧会 “Attention Is All You Need” から理解するTransformer PyCon JP Twitter/GitHub/Instagram: @coolandsmartrr | Email: [email protected]
  2. アジェンダ • V8エンジンの全体像 • フロントエンド処理 • 3つの実験 1. 2つの値を足す 2.

    関数のifを処理する 3. オブジェクトのプロパティを読む Twitter/GitHub/Instagram: @coolandsmartrr | Email: [email protected]
  3. V8エンジンの全体像 JavaScript ソース 字句解析器 / 構文解析器 フロントエンド 解析・バイトコード生成 AST バイトコード生成器

    バックエンド Ignition バイトコード 実行・最適化 Ignition インタープリター Sparkplug Maglev マシンコード Twitter/GitHub/Instagram: @coolandsmartrr | Email: [email protected] TurboFan / Turboshaft
  4. パーサー(構文解析器) トークンからASTを作る { type: "FunctionDeclaration", name: "add", params: [ {

    type: "Identifier", name: "a" }, { type: "Identifier", name: "b" } ], // ... } Twitter/GitHub/Instagram: @coolandsmartrr | Email: [email protected]
  5. AST トークンを構造化する Program FunctionDeclaration Identifier: add Parameter: a Parameter: b

    BlockStatement ReturnStatement BinaryExpression: + Identifier: a Twitter/GitHub/Instagram: @coolandsmartrr | Email: [email protected] Identifier: b
  6. バイトコード 217種類もある(調査時) Wide ExtraWide DebugBreakWide DebugBreakExtraWide DebugBreak0 DebugBreak1 DebugBreak2 DebugBreak3

    DebugBreak4 DebugBreak5 DebugBreak6 Ldar LdaZero LdaSmi LdaUnde ned LdaNull LdaTheHole LdaTdzHole LdaTrue LdaFalse LdaConstant LdaContextSlotNoCell LdaContextSlot LdaImmutableContextSlot LdaCurrentContextSlotNoCell LdaCurrentContextSlot LdaImmutableCurrentContextSlot Star Mov PushContext PopContext TestReferenceEqual TestUndetectable TestNull TestUnde ned TestTypeOf LdaGlobal LdaGlobalInsideTypeof StaGlobal StaContextSlotNoCell StaCurrentContextSlotNoCell StaContextSlot StaCurrentContextSlot LdaLookupSlot LdaLookupContextSlotNoCell LdaLookupContextSlot LdaLookupGlobalSlot LdaLookupSlotInsideTypeof LdaLookupContextSlotNoCellInsideTypeof LdaLookupContextSlotInsideTypeof LdaLookupGlobalSlotInsideTypeof StaLookupSlot GetNamedProperty GetNamedPropertyFromSuper GetKeyedProperty GetEnumeratedKeyedProperty GetPrivateField LdaModuleVariable StaModuleVariable SetNamedProperty De neNamedOwnProperty De neNamedOwnPropertyInLiteral SetKeyedProperty De neKeyedOwnProperty StaInArrayLiteral De neKeyedOwnPropertyInLiteral SetPrototypeProperties SetPrivateField Add Sub Mul Div Mod Exp BitwiseOr BitwiseXor BitwiseAnd ShiftLeft ShiftRight ShiftRightLogical Add_StringConstant_Internalize AddSmi SubSmi MulSmi DivSmi ModSmi ExpSmi BitwiseOrSmi BitwiseXorSmi BitwiseAndSmi ShiftLeftSmi ShiftRightSmi ShiftRightLogicalSmi Inc Dec Negate BitwiseNot ToBooleanLogicalNot LogicalNot TypeOf DeletePropertyStrict DeletePropertySloppy GetSuperConstructor FindNonDefaultConstructorOrConstruct CallAnyReceiver CallProperty CallProperty0 CallProperty1 CallProperty2 CallUnde nedReceiver CallUnde nedReceiver0 CallUnde nedReceiver1 CallUnde nedReceiver2 CallWithSpread CallRuntime CallRuntimeForPair CallJSRuntime InvokeIntrinsic Construct ConstructWithSpread ConstructForwardAllArgs TestEqual TestEqualStrict TestLessThan TestGreaterThan TestLessThanOrEqual TestGreaterThanOrEqual TestInstanceOf TestIn ToName ToNumber ToNumeric ToObject ToString ToBoolean CreateRegExpLiteral CreateArrayLiteral CreateArrayFromIterable CreateEmptyArrayLiteral CreateObjectLiteral CreateEmptyObjectLiteral CloneObject GetTemplateObject CreateClosure CreateBlockContext CreateCatchContext CreateFunctionContext CreateFunctionContextWithCells CreateEvalContext CreateWithContext CreateMappedArguments CreateUnmappedArguments CreateRestParameter JumpLoop Jump JumpConstant JumpIfNullConstant JumpIfNotNullConstant JumpIfUnde nedConstant JumpIfNotUnde nedConstant JumpIfUnde nedOrNullConstant JumpIfTrueConstant JumpIfFalseConstant JumpIfJSReceiverConstant JumpIfForInDoneConstant JumpIfToBooleanTrueConstant JumpIfToBooleanFalseConstant JumpIfToBooleanTrue JumpIfToBooleanFalse JumpIfTrue JumpIfFalse JumpIfNull JumpIfNotNull JumpIfUnde ned JumpIfNotUnde ned JumpIfUnde nedOrNull JumpIfJSReceiver JumpIfForInDone SwitchOnSmiNoFeedback ForInEnumerate ForInPrepare ForInNext ForInStep ForOfNext SetPendingMessage Throw ReThrow Return ThrowReferenceErrorIfTdzHole ThrowSuperNotCalledIfTdzHole ThrowSuperAlreadyCalledIfNotTdzHole ThrowIfNotSuperConstructor SwitchOnGeneratorState SuspendGenerator ResumeGenerator GetIterator ArrayDestructure Debugger IncBlockCounter Abort Star15 Star14 Star13 Star12 Star11 Star10 Star9 Star8 Star7 Star6 Star5 Star4 Star3 Star2 Star1 Star0 Illegal src/interpreter/bytecodes.h より fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi Twitter/GitHub/Instagram: @coolandsmartrr | Email: [email protected]
  7. 実験1: 2つの値を足す 13,866行のバイトコード [generated bytecode for function: (0x33ed6b5550d1 <SharedFunctionInfo>)] Bytecode

    length: 5 Parameter count 1 Register count 0 Frame size 0 0 E> 0x38b29df41198 @ 0 : 85 00 00 00 CreateClosure [0], [0], #0 1367 S> 0x38b29df4119c @ 4 : ae Return Constant pool (size = 1) 0x38b29df41141: [TrustedFixedArray] - map: 0x39172ca009a9 <Map(TRUSTED_FIXED_ARRAY_TYPE)> - length: 1 0: 0x33ed6b555199 <SharedFunctionInfo> Handler Table (size = 0) Source Position Table (size = 7) 0x38b29df411a1 <Other heap object (TRUSTED_BYTE_ARRAY_TYPE)> [generated bytecode for function: (0x33ed6b555199 <SharedFunctionInfo>)] Bytecode length: 154 Parameter count 5 Register count 10 Frame size 80 25 S> 0x38b29df41298 @ 0 : 2f 06 00 00 GetNamedProperty a3, [0], [0] 0x38b29df4129c @ 4 : c9 Star0 131 S> 0x38b29df4129d @ 5 : 13 01 LdaConstant [1] 0x38b29df4129f @ 7 : c1 Star8 131 E> 0x38b29df412a0 @ 8 : 65 04 f1 02 CallUndefinedReceiver1 a1, r8, [2] 0x38b29df412a4 @ 12 : c2 Star7 74 E> 0x38b29df412a5 @ 13 : 2f f2 02 04 GetNamedProperty r7, [2], [4] 0x38b29df412a9 @ 17 : c8 Star1 104 E> 0x38b29df412aa @ 18 : 2f f2 03 06 GetNamedProperty r7, [3], [6] 0x38b29df412ae @ 22 : c7 Star2 201 S> 0x38b29df412af @ 23 : 13 04 LdaConstant [4] 0x38b29df412b1 @ 25 : c1 Star8 201 E> 0x38b29df412b2 @ 26 : 65 04 f1 08 CallUndefinedReceiver1 a1, r8, [8] 0x38b29df412b6 @ 30 : c2 Star7 182 E> 0x38b29df412b7 @ 31 : 2f f2 05 0a GetNamedProperty r7, [5], [10] 0x38b29df412bb @ 35 : c6 Star3 266 S> 0x38b29df412bc @ 36 : 13 06 LdaConstant [6] 0x38b29df412be @ 38 : c1 Star8 266 E> 0x38b29df412bf @ 39 : 65 04 f1 0c CallUndefinedReceiver1 a1, r8, [12] 0x38b29df412c3 @ 43 : c2 Star7 238 E> 0x38b29df412c4 @ 44 : 2f f2 07 0e GetNamedProperty r7, [7], [14] 0x38b29df412c8 @ 48 : c5 Star4 312 S> 0x38b29df412c9 @ 49 : 13 08 LdaConstant [8] 0x38b29df412cb @ 51 : c1 Star8 312 E> 0x38b29df412cc @ 52 : 65 f6 f1 10 CallUndefinedReceiver1 r3, r8, [16] 0x38b29df412d0 @ 56 : c4 Star5 361 S> 0x38b29df412d1 @ 57 : 0b f4 Ldar r5 389 E> 0x38b29df412d3 @ 59 : 57 ToBooleanLogicalNot 0x38b29df412d4 @ 60 : c1 Star8 361 E> 0x38b29df412d5 @ 61 : 65 f8 f1 12 CallUndefinedReceiver1 r1, r8, [18] … Twitter/GitHub/Instagram: @coolandsmartrr | Email: [email protected]
  8. 実験1: 2つの値を足す バイトコードが13行で分かりやすい [generated bytecode for function: add (0x35870a9daed1 <SharedFunctionInfo

    add>)] Bytecode length: 6 Parameter count 3 Register count 0 Frame size 0 23 S> 0x4be2f181ea8 @ 0 : 0b 04 Ldar a1 32 E> 0x4be2f181eaa @ 2 : 3b 03 00 Add a0, [0] 36 S> 0x4be2f181ead @ 5 : ae Return Constant pool (size = 0) Handler Table (size = 0) Source Position Table (size = 8) 0x04be2f181eb1 <Other heap object (TRUSTED_BYTE_ARRAY_TYPE)> 3 Twitter/GitHub/Instagram: @coolandsmartrr | Email: [email protected]
  9. 実験1: 2つの値を足す 出力の内容を理解する 関数名 バイトコード パラメーター数 使用する仮想レジスタ数 フレームサイズ [generated bytecode

    for function: add (0x35870a9daed1 <SharedFunctionInfo add>)] Bytecode length: 6 Parameter count 3 命令名とオペランド Register count 0 バイト位置 実際のバイト Frame size 0 23 S> 0x4be2f181ea8 @ 0 : 0b 04 Ldar a1 32 E> 0x4be2f181eaa @ 2 : 3b 03 00 Add a0, [0] 36 S> 0x4be2f181ead @ 5 : ae Return Constant pool (size = 0) Handler Table (size = 0) Source Position Table (size = 8) 0x04be2f181eb1 <Other heap object (TRUSTED_BYTE_ARRAY_TYPE)> 3 Twitter/GitHub/Instagram: @coolandsmartrr | Email: [email protected]
  10. 実験1: 2つの値を足す パラメーター数には`this`を含める パラメーター数は3 [generated bytecode for function: add (0x35870a9daed1

    <SharedFunctionInfo add>)] Bytecode length: 6 Parameter count 3 Register count 0 Frame size 0 23 S> 0x4be2f181ea8 @ 0 : 0b 04 Ldar a1 ソースコードの引数は2つ 32 E> 0x4be2f181eaa @ 2 : 3b 03 00 Add a0, [0] 36 S> 0x4be2f181ead @ 5 : ae Return Constant pool (size = 0) Handler Table (size = 0) Source Position Table (size = 8) 0x04be2f181eb1 <Other heap object (TRUSTED_BYTE_ARRAY_TYPE)> 3 Twitter/GitHub/Instagram: @coolandsmartrr | Email: [email protected]
  11. 実験1: 2つの値を足す 3つの命令を読む Ldar = Load accumulator from register Ldar

    a1 // bをアキュムレータへ Add a0, [0] // aとアキュミュレータを足す Return // アキュムレータの値を返す Twitter/GitHub/Instagram: @coolandsmartrr | Email: [email protected]
  12. 実験1: 2つの値を足す 関数の最適化 • Ignition(インタプリター) • バイトコードを解釈して実行するのみ • 最適化された機械語を生成しない •

    関数が実行されると、V8は値についてのフィードバックを集める • 例: `add`について数値が繰り返し渡されている、と観測する • 観測した情報を元に、MaglevやTurboFanのような最適化コンパイラが、 `+`をより具体的な数値加算として扱えるか検討する Twitter/GitHub/Instagram: @coolandsmartrr | Email: [email protected]
  13. 実験1: 2つの値を足す 最適化の例: `Add` —allow-natives-syntax V8内部の関数を呼ぶための %… 構文を有効にする —no-maglev V8の中間段階の最適化コンパイラ

    Maglev を無効にする —print-opt-code 最適化済みの機械語をアセンブリ表記で出力する --print-opt-code- lter=add `--print-opt-code` の出力を add という名前の関数に絞る %PrepareFunctionForOptimization 実行時フィードバックを記録する準備をする %OptimizeFunctionOnNextCall 次に呼び出したときに最適化するように指示する fi Twitter/GitHub/Instagram: @coolandsmartrr | Email: [email protected]
  14. 実験1: 2つの値を足す 最適化された`Add`は75行(Apple ARM64命令) 0x12800c6e0 0 f85e8050 0x12800c6e4 4 b842f210

    0x12800c6e8 8 36e80070 0x12800c6ec c 58000631 0x12800c6f0 10 d61f0220 0x12800c6f4 14 a9bf7bfd 0x12800c6f8 18 910003fd 0x12800c6fc 1c a9be03ff 0x12800c700 20 a9016fe1 0x12800c704 24 f8520342 0x12800c708 28 f90003fb 0x12800c70c 2c eb2263ff 0x12800c710 30 540002c9 0x12800c714 34 f9401fe2 0x12800c718 38 f240005f 0x12800c71c 3c 540005a1 0x12800c720 40 9360fc43 0x12800c724 44 f94023e4 0x12800c728 48 f240009f 0x12800c72c 4c 54000541 0x12800c730 50 9360fc85 0x12800c734 54 2b050063 0x12800c738 58 54000506 0x12800c73c 5c d3607c60 0x12800c740 60 f85e83a3 0x12800c744 64 910003bf 0x12800c748 68 a8c17bfd 0x12800c74c 6c f1000c7f 0x12800c750 70 5400004a 0x12800c754 74 d2800063 0x12800c758 78 91000470 0x12800c75c 7c 927ffa10 0x12800c760 80 8b306fff 0x12800c764 84 d65f03c0 0x12800c768 88 d2c00502 0x12800c76c 8c d10043ff 0x12800c770 90 f90007ff 0x12800c774 94 f90003e2 0x12800c778 98 d2855201 0x12800c77c 9c f2a056c1 0x12800c780 a0 f2c00021 0x12800c784 a4 d2800020 0x12800c788 a8 5800011b <NativeContext[287]> 0x12800c78c ac 58000170 ldur x16, [x2, #-24] ldur w16, [x16, #47] tbz w16, #29, #+0xc (addr 0x12800c6f4) ldr x17, pc+196 (addr 0x000000012800c7b0) br x17 stp fp, lr, [sp, #-16]! mov fp, sp stp xzr, x0, [sp, #-32]! stp x1, cp, [sp, #16] ldur x2, [x26, #-224] str cp, [sp] cmp sp, x2 b.ls #+0x58 (addr 0x12800c768) ldr x2, [sp, #56] tst x2, #0x1 b.ne #+0xb4 (addr 0x12800c7d0) asr x3, x2, #32 ldr x4, [sp, #64] tst x4, #0x1 b.ne #+0xa8 (addr 0x12800c7d4) asr x5, x4, #32 adds w3, w3, w5 b.vs #+0xa0 (addr 0x12800c7d8) lsl x0, x3, #32 ldur x3, [fp, #-24] mov sp, fp ldp fp, lr, [sp], #16 cmp x3, #0x3 (3) b.ge #+0x8 (addr 0x12800c758) movz x3, #0x3 add x16, x3, #0x1 (1) and x16, x16, #0xfffffffffffffffe add sp, sp, x16, lsl #3 ret movz x2, #0x2800000000 sub sp, sp, #0x10 (16) str xzr, [sp, #8] str x2, [sp] movz x1, #0x2a90 movk x1, #0x2b6, lsl #16 movk x1, #0x1, lsl #32 movz x0, #0x1 ldr cp, pc+32 (addr 0x000000012800c7a8) 0x12800c790 0x12800c794 0x12800c798 0x12800c79c 0x12800c7a0 0x12800c7a4 0x12800c7a8 0x12800c7ac 0x12800c7b0 0x12800c7b4 0x12800c7b8 0x12800c7bc 0x12800c7c0 0x12800c7c4 0x12800c7c8 0x12800c7cc 0x12800c7d0 offset '20' b0 b4 b8 bc c0 c4 c8 cc d0 d4 d8 dc e0 e4 e8 ec f0 d63f0200 17ffffe0 d503201f 5800011f d63f03e0 d503201f 7b181231 00000c1c 02f92b80 00000001 03025da0 00000001 f8550350 d61f0200 f8558350 d61f0200 97fffffc ;; debug: deopt position, inlining id 'ffffffff' 0x12800c7d4 offset '20' f4 97fffffb f8 97fffffa 'ffffffff' ;; object: 0x0c1c7b181231 ldr x16, pc+44 (addr 0x000000012800c7b8) Twitter/GitHub/Instagram: @coolandsmartrr | Email: [email protected] ;; debug: deopt reason 'not a Smi' ;; debug: deopt index 1 bl #-0x18 (addr 0x12800c7c0) ;; debug: deopt position, script ;; debug: deopt position, inlining id 'ffffffff' 0x12800c7dc offset 'c' ;; debug: deopt reason 'not a Smi' ;; debug: deopt index 0 bl #-0x14 (addr 0x12800c7c0) ;; debug: deopt position, script ;; debug: deopt position, inlining id 'ffffffff' 0x12800c7d8 offset '20' blr x16 b #-0x80 (addr 0x12800c714) nop constant pool begin (num_const = 8) ;; constant pool constant constant constant constant constant constant constant constant ldur x16, [x26, #-176] br x16 ldur x16, [x26, #-168] br x16 bl #-0x10 (addr 0x12800c7c0) ;; debug: deopt position, script fc 97fffffb ;; debug: deopt reason 'overflow' ;; debug: deopt index 2 bl #-0x14 (addr 0x12800c7c8) ;; debug: deopt position, script ;; debug: deopt position, inlining id ;; debug: deopt reason '(unknown)' ;; debug: deopt index 3
  15. 実験1: 2つの値を足す 機械語の中身:ガード、分岐、加算 38 3c tst x2, #0x1 b.ne #+0xb4

    (addr 0x12800c7d0) // a がSmiでなければ分岐 48 4c tst x4, #0x1 b.ne #+0xa8 (addr 0x12800c7d4) // b がSmiでなければ分岐 54 58 adds w3, w3, w5 b.vs #+0xa0 (addr 0x12800c7d8) // 加算がオーバーフローしたら分岐 deoptへの入口 分岐先アドレス 0x12800c7d0 命令 deoptの理由 deopt index bl #-0x10 not a Smi(第1引数 a) 0 0x12800c7d4 bl #-0x14 not a Smi(第2引数 b) 1 0x12800c7d8 bl #-0x18 over ow(加算のオーバーフロー) 2 3つの bl は共通の 0x12800c7c0(deopt処理への中継地点) を呼び出す fl Twitter/GitHub/Instagram: @coolandsmartrr | Email: [email protected]
  16. 実験2: `if`を追加する バイトコード [generated bytecode for function: positiveOrZero (0x34aea771b2d9 <SharedFunctionInfo

    positiveOrZero>)] Bytecode length: 11 Parameter count 2 Register count 0 Frame size 0 31 S> 0x60fba881f88 @ 0 : 0c LdaZero 37 E> 0x60fba881f89 @ 1 : 72 03 00 TestGreaterThan a0, [0] 0x60fba881f8c @ 4 : 9e 05 JumpIfFalse [5] (0x60fba881f91 @ 9) 48 S> 0x60fba881f8e @ 6 : 0b 03 Ldar a0 57 S> 0x60fba881f90 @ 8 : ae Return 65 S> 0x60fba881f91 @ 9 : 0c LdaZero 74 S> 0x60fba881f92 @ 10 : ae Return Constant pool (size = 0) Handler Table (size = 0) Source Position Table (size = 14) 0x060fba881f99 <Other heap object (TRUSTED_BYTE_ARRAY_TYPE)> 0 Twitter/GitHub/Instagram: @coolandsmartrr | Email: [email protected]
  17. 実験2: `if`を追加する バイトコード解説 0 LdaZero // アキュムレータに0を入れる 1 TestGreaterThan a0,

    [0] // 引数xと0を比較する 4 JumpIfFalse [5] // 分岐点: falseなら5バイト先のLdaZeroへ 6 Ldar a0 // アキュムレータにxを入れる 8 Return // アキュムレータの値を返す 9 LdaZero // アキュムレータに0を入れる 10 Return // アキュムレータの値を返す Twitter/GitHub/Instagram: @coolandsmartrr | Email: [email protected]
  18. 実験3: プロパティを読む バイトコード [generated bytecode for function: getName (0x397e0b19b221 <SharedFunctionInfo

    getName>)] Bytecode length: 5 Parameter count 2 Register count 0 Frame size 0 39 S> 0x3cdc45901fc0 @ 0 : 2f 03 00 00 GetNamedProperty a0, [0], [0] 44 S> 0x3cdc45901fc4 @ 4 : ae Return Constant pool (size = 1) 0x3cdc45901f69: [TrustedFixedArray] - map: 0x102eedf409a9 <Map(TRUSTED_FIXED_ARRAY_TYPE)> - length: 1 0: 0x102eedf41831 <String[4]: #name> Handler Table (size = 0) Source Position Table (size = 6) 0x3cdc45901fc9 <Other heap object (TRUSTED_BYTE_ARRAY_TYPE)> JavaScript Twitter/GitHub/Instagram: @coolandsmartrr | Email: [email protected]
  19. 実験3: プロパティを読む バイトコード解説 GetNamedProperty a0, [0], [0] // a0はuser //

    1つ目の[0]は定数プールのインデックス // プロパティ名’name’を指す // 2つ目の[0]はプロパティーアクセスに使う // フィードバックスロットの番号 Return Constant pool (size = 1) Twitter/GitHub/Instagram: @coolandsmartrr | Email: [email protected]
  20. 3つの実験で見たOpCode 足し算: Ldar Add Return 条件分岐: LdaZero TestGreaterThan JumpIfFalse ...

    プロパティアクセス: GetNamedProperty Return Twitter/GitHub/Instagram: @coolandsmartrr | Email: [email protected]

Other Decks in Programming