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

torque in V8

torque in V8

V8 javascript eingineのdslであるtorqueの話

More Decks by Taketoshi Aono(青野健利 a.k.a brn)

Other Decks in Programming

Transcript

  1. Name
    @brn (ꫬꅿ⨳ⵃ)
    Occupation
    ؿٗٝزؒٝسؒٝآص،٥ط؎ذ؍ـؒٝآص،
    Company
    Cyberagent ،سذؙأةآؔ AI Messenger
    OSS
    Contributor of V8
    About
    http://info.b6n.ch

    View full-size slide

  2. What is torque?
    V8ⰻ鿇דⵃ欽ׅ׷׋׭ח⡲׵׸׋DSL
    TypeScript괏㄂ז俑岀ד葿ղ邌植ׅ׷

    View full-size slide

  3. How does it works?
    torqueכ؝ٝػ؎ׁٕ׸׷הCSA(CodeStubAssembler)ח㢌䳔ׁ׸
    ׷կ
    CSAכفٓحزؿؓ٦يꬊ⣛㶷זIRד剑穄涸ח،إٝـٓח؝ٝػ
    ؎ׁٕ׸׷կ

    View full-size slide

  4. Why V8 need torque?
    CSAכ剅ְג׫׷ה׻ַ׷ָ穠圓הְֲַַז׶׃׿וְ
    -  ✲佦׶װְׅ
    -  僓כ㘗ָⰋגNode㘗׌׏׋ח׮ꟼ׻׵׆ⰻ鿇涸חכSmiהַ
    IntPtrהַ㘗ָאְגְגծ♶侭さؒٓ٦ָ⳿װַׅ׏׋կ
    -  ⵖ䖴圓俑ָ铣׫חְֻ

    View full-size slide

  5. function add100() {!
    let value = 0;!
    for (let i = 0; i < 100; i++) {!
    value += 1;!
    }!
    return value;!
    }!

    View full-size slide

  6. TNode Add100(TNode context) {!
    TVARIABLE(var_value, MachineType::PointerRepresentation(),
    IntPtrConstant(0));!
    TVARIABLE(var_index, MachineType::PointerRepresentation(),
    IntPtrConstant(0));!
    !
    Label loop(this), after_loop(this);!
    !
    Branch(IntPtrGreaterThan(IntPtrConstant(100), var_index), &loop,
    &after_loop);!
    BIND(&loop);!
    {!
    var_a.Bind(IntPtrAdd(var_a, IntPtrConstant(1)));!
    Increment(&var_index, 1);!
    Branch(IntPtrGreaterThan(IntPtrConstant(100), var_index),!
    &loop, &after_loop);!
    }!
    BIND(&after_loop);!
    return var_value.value;!
    }!

    View full-size slide

  7. macro Add100(): Smi {!
    let value: Smi = 0;!
    let len: Smi = 100;!
    !
    for (let i: Smi = 0; k < len; i = i + 1) {!
    value = value + 1;!
    }!
    return value;!
    }!

    View full-size slide

  8. torque grammars
    㛇劤涸חכ♧菙涸ז䩛竲ֹ㘗鎉铂הずׄ״ֲז俑岀
    TypeScriptך㘗،ظذ٦ءّٝ׾⿫罋ח׃גְ׷
    ָծDSLזךד׍׳׏ה暴婊ז俑岀׮㢳ְ

    View full-size slide

  9. Type Definition
    TypeScriptך䕎ؒ؎ٔ،أה⡂גְ׷ָծֿ׍׵כC++ך㘗ה
    torqueⰻ鿇ך䕎׾֮׻ׇ׷湡涸

    View full-size slide

  10. type Object generates 'TNode';!
    type int32 generates 'TNode';!

    View full-size slide

  11. External functions
    V8ⰻ鿇חכׅדח㣐ꆀךِ٦ذ؍ٔذ؍ꟼ侧ָ㹀纏ׁ׸גֶ׶ծ
    ׉׸׾ⵃ欽ׅ׷׋׭חextern䭷㹀㶨ָ㹀纏ׁ׸גְ׷
    -  macro CSAⰻ鿇ד㹀纏ׁ׸גְ׷ِ٦ذ؍ٔذ؍وؙٗ
    -  builtin V8ךbuiltins⟃♴ח֮׷ꟼ侧纇
    -  runtime C++ד鎸鶢ׁ׸׋ًاحس

    View full-size slide

  12. extern macro Print(Object);!
    !
    extern builtin ToObject(Context, Number): Object;!
    !
    extern runtime CreateDataProperty(!
    Context, Object, String, Object);!

    View full-size slide

  13. Operators
    torqueכoperator׮ꟼ侧ה׃ג㹀纏ׅ׷ֿהָדֹ׷

    View full-size slide

  14. extern operator "+" macro SmiAdd(Smi, Smi): Smi;!
    extern operator "-" macro SmiSub(Smi, Smi): Smi;!

    View full-size slide

  15. Cast
    CastכCSAךCast׾ⵃ欽ׅ׷ָծ㣟侁׃׋㜥さחכGotoדٓكٕפ
    굲ע

    View full-size slide

  16. cast(a) otherwise CastError!

    View full-size slide

  17. Labels
    CSAכ׮ה׮הַז׶⡚ٖكٕז邌植זךדծ
    if俑װfor俑瘝ךⵖ䖴圓俑׾׮׏גְזְկ
    ֿ׸׵׾邌植ׅ׷ךכׅץגLabelהGotoד֮׷կ

    View full-size slide

  18. Branch(var_a, label1, label2)!
    !
    BIND(&label1);!
    Print("A");!
    !
    BIND(&label2);!
    Print("B");!

    View full-size slide

  19. macro Foo(context: Context): Smi labels
    Bailout, CastError {!
    Goto Bailout;!
    }!

    View full-size slide

  20. compiler::TNode FooFromDSLAssembler::Foo(!
    TNode p_context,!
    Label* label_Bailout_1,!
    Label* label_CastError_1) {!
    Goto(label_Bailout_1);!
    }!

    View full-size slide

  21. WriteBarrier
    V8כ؝ٖٝؕٝز٥؎ًؙٝٔٝةٕGC׾琎׿דְ׷ךדծ
    ؔـآؙؑزפך،ؙإأךꥷחכWriteBarrierָ䗳갭׌ָ
    ֿ׸׮CSAך㜥さחכ荈ⴓדWriteBarrier׾⢪ֲַNoBarrierד遤ֻ
    ַ鼅䫛׃זֽ׸לז׵זְկ
    torqueדכ㼛勻涸חֿֿ׮荈⹛⻉ׅ׷✮㹀׵׃ְ

    View full-size slide

  22. Compilation
    植㖈ךהֿ׹ػ٦؟٦כAntlrד剅ַ׸גְ׷
    俑岀涸חכLL(1)דػ٦أ〳腉
    㼛勻涸חכ䩛剅ֹךػ٦؟ח׃׋ְ׵׃ְ

    View full-size slide

  23. Compilation
    V8ךؽٕسفٗإأ⚥ח.tqؿ؋؎ٕךؽٕسָ饥׶ծ
    ׉ך穠卓
    <ر؍ؙٖزٔせ>-<ؿ؋؎ٕせ>-from-dsl-gen.h/cc
    הְֲC++ؿ؋؎ָٕ欰䧭ׁ׸
    ׁ׵ח׉ךؿ؋؎ָٕؽٕسׁ׸غ؎شٔך♧鿇חז׷

    View full-size slide

  24. .tq ~-from-dsl-gen.h/cc V8

    View full-size slide

  25. SourceMaps
    植㖈ךהֿ׹فٗزة؎ف׌ָծ--gdbjit-fullؔفءّٝדا٦أ⡘
    縧ךوحؾؚٝ׾׃גְ׷׵׃ְ

    View full-size slide

  26. Summary
    ؝ٝػ؎ٓך⚥חDSL׾אֻ׷ךכ״ֻ֮׷鑧׌ָծ
    ֿֿתדֹ׏׍׶׃׋鎉铂כ穠圓棆׃ְ

    View full-size slide

  27. Reference
    -  https://docs.google.com/presentation/d/
    1PlQEjS5xyJA-0GY8mjB4wr-pLyzFtdMRfhXczJJ2wic/
    edit#slide=id.g388aa7220e_2_348
    -  https://github.com/v8/v8/blob/master/src/builtins/typed-
    array.tq

    View full-size slide