Slide 1

Slide 1 text

ࣗ࡞ݴޠΛ wasmʹίϯύΠϧ͢Δ karamaru

Slide 2

Slide 2 text

݁࿦ Rust੡ͷࣗ࡞ݴޠΛࣗྗͰwasmόΠφϦʹม׵ͯ͠ ϒϥ΢βͰ࣮ߦͯ͠ΈͨΑʂ (࢛ଇԋࢉͱؔ਺exportͷΈ)

Slide 3

Slide 3 text

͜Μͳײ͡ ࣗ࡞ݴޠ wasmόΠτίʔυ ϒϥ΢βͰ࣮ߦ ͜͜Λ࡞Δ!

Slide 4

Slide 4 text

ɾWebAssembly֓ཁ ɾHello! Wasm! ɾast·Ͱ࡞Δ ɾࣈ۟ղੳ ɾߏจղੳ ɾwasmม׵Λ࡞Δ ɹɾόΠφϦ༷ࣜ ໨࣍

Slide 5

Slide 5 text

Wasm֓ཁ ɾϒϥ΢βͰಈ͘όΠτίʔυ ɾjsҎ֎ͷߴ଎ͳݴޠͰॲཧΛ࣮ߦͤ͞Δ͜ͱ͕໨తͩͬͨ -> લ਎͸asm.jsɻjsαϒηοτͱͯ͠ಈ࡞ͯ͠AOTίϯύΠϧ͕ߦΘΕΔɻ ɾԾ૝తͳϋʔυ΢ΣΞͰಈ͘௿ਫ४ͳ໋ྩͷू·Γͱͯ͠ϞδϡʔϧΛఆٛͰ͖Δ -> runtimeଆͰ໌ࣔతʹڐՄ͞Εͨ΋ͷ͚࣮ͩߦՄೳ (sandbox) ɾݴޠෆ໰ͰίϯύΠϧͰ͖Δ ɾ͜ͷن໿ΛԠ༻ͯ͠ϒϥ΢β֎Ͱ࣮ߦ͠Α͏ͱ͢Δಈ͖΋࠷ۙଟ͍Ͱ͢ΑͶ

Slide 6

Slide 6 text

ࣗ࡞ݴޠͱwasm ࣗ࡞ݴޠ͔ΒwasmΛు͚ΔΑ͏ʹͳΕ͹ ଞͷݴޠͷruntimeʹཔΒͣಈ͔͢͜ͱ͕Ͱ͖Δʂ LLVM IRม׵Ͱ΋ྑ͍͚Ͳ௚઀wasmు͖͍ͨʂ

Slide 7

Slide 7 text

׬੒Πϝʔδ Chorome console ಠࣗݴޠ(text)͔Β ͜ΕܥͷόΠτίʔυΛు͚Ε͹͍͍

Slide 8

Slide 8 text

͜Μͳײ͡ͰਐΊ·͢ 1. ࣗ࡞ݴޠͷίʔυ४උ 2. τʔΫϯԽ lexser 3. ߏจղੳ parser 4. wasmԽ emitter

Slide 9

Slide 9 text

ࣗ࡞ݴޠͷίʔυ ܕͳ͠ͷؔ਺ͬΆ͍΍ͭʂ ͜ΕΛwasmԽͯ͠ ϒϥ΢βͰ࣮ߦ͢Δ·ͰؤுΔ🔥

Slide 10

Slide 10 text

ࣈ۟ղੳ Lexser ·ͣ͸ࣈ۟ղੳʂ ϓϩάϥϜΛτʔΫφΠζͯ͠ɺ ಠࣗݴޠΛಡΈਐΊΔࡍͷ࠷খ୯Ґ(τʔΫϯ)ͷࣝผ͢Δɻ fn … ؔ਺એݴ add … ࣝผࢠ ( … ࠨׅހ a … ࣝผࢠ , … ΧϯϚ ͱ͔ɺ ه߸΍਺஋,ࣝผࢠͳͲͷίʔυͷ࠷খ୯ҐΛཧղ͠·͢

Slide 11

Slide 11 text

ࣈ۟ղੳ ࣮૷ جຊ1จࣈͣͭ ύλʔϯϚονͰ൑ผ enumʹ஋Λ࣋ͨͤΒΕΔ

Slide 12

Slide 12 text

ࣈ۟ղੳث ׬੒ʂ ࣮ࡍ͸ߏจղੳͰιʔείʔυΛಡΈਐΊΔࡍʹ ౎౓ࣈ۟ղੳ͢ΔΜ͚ͩͲݟ΍͢͞తʹશ෦ग़ͯ͠Δ τʔΫϯԽʹ੒ޭ͠·ͨ͠Ͷ

Slide 13

Slide 13 text

ߏจղੳ ࣍͸ߏจղੳʂ ࿈ଓ͢ΔτʔΫϯྻ͔Βҙຯͷ͋Δ·ͱ·Γ(ߏจ)Λࣝผ͢Δɻ Lexser ͜ͷτʔΫϯྻ͸… ɾࣝผࢠʮaddʯͰఆٛ͞ΕΔؔ਺ఆٛͩʂ ɾࣝผࢠʮaʯͱʮbʯͰఆٛ͞ΕΔҾ਺͕͋Δʂ ɾ಺෦Ͱ͸ʮa + b * 2ʯΛߦͳ͍ͬͯΔʂ తͳ

Slide 14

Slide 14 text

ߏจղੳ ܕఆٛ ϓϩάϥϜ͸จ(statement)ͷू߹ମ ਺஋ܕ΍ؔ਺Λࣜ(expression)ͱͯ͠ѻ͏ If/for/letͱ͔͸࣮૷ͯ͠ͳ͍ͷͰจ͸ࣜͷΈ

Slide 15

Slide 15 text

ߏจղੳ ࣮૷ Expression::Infix( Infix::Plus, Expression::Integer(1), Expression::Integer(2), ) ྫ͑͹ʮɹ1 + 2ɹʯͩͬͨΒ precedence: ༏ઌॱҐ

Slide 16

Slide 16 text

ߏจղੳث ׬੒ AST͕Ͱ͖·ͨ͠Ͷ ֻ͚ࢉΛઌʹ΍ͬͯΔͷ΋͑Β͍ʂ

Slide 17

Slide 17 text

͜Ε͔Β͍ͨ͜͠ͱ wasmόΠτίʔυ ͜͜Λ࡞Δʂ WasmόΠφϦܗࣜΛݟΔඞཁ͕͋Δ Emitter

Slide 18

Slide 18 text

WasmόΠφϦΛͭͬͯ͘ΈΑ͏ʂ όΠφϦܗࣜΛֶͿͨΊʹaddؔ਺ͷwasmΛͭ͘Δʂ ɾwasm͸ςΩετܗࣜ(Sࣜ)Ͱهड़Ͱ͖Δ = wat ɾwat2wasmΛ༻͍ͯม׵Ͱ͖Δ wasm͸ελοΫϚγϯͳΜͩͶʂ

Slide 19

Slide 19 text

WasmόΠφϦΛݟͯΈΑ͏ʂ ɾγϯϓϧͳ଍͠ࢉؔ਺ͷexport͸41byteͰදݱͰ͖Δʂ ɾಠࣗݴޠͷιʔείʔυ(ast)͔Β͜ΕΛग़ͤΕ͹উͪ Addؔ਺ͷwasmΛݩʹɺܗࣜΛਅࣅ͍ͯ͜͏ʂ

Slide 20

Slide 20 text

WasmͷηΫγϣϯ: ϓϦΞϯϒϧ ɾ4όΠτͷϚδοΫφϯόʔʢasmͷASCIIίʔυʣɻwasmϞδϡʔϧͩͱೝࣝͤ͞Δɻ ɾ4όΠτόʔδϣϯ൪߸ (ݱࡏ͸1) https://webassembly.github.io/spec/core/binary/modules.html#binary-magic

Slide 21

Slide 21 text

WasmͷηΫγϣϯ: ϓϦΞϯϒϧ ࣮૷ ɾ8όΠτ٧ΊΔ͚ͩ ɾ͜Ε͔Β͸emitؔ਺Ͱast͔Βbytecodeม׵Λߦ͏

Slide 22

Slide 22 text

WasmͷηΫγϣϯ: Type module಺Ͱ࢖༻͞ΕΔؔ਺ͷγάωνϟΛએݴ͢Δ https://webassembly.github.io/spec/core/binary/types.html http://webassembly.github.io/spec/core/binary/types.html#function-types https://webassembly.github.io/spec/core/binary/modules.html#type-section 01: ηΫγϣϯ൪߸ 07: ηΫγϣϯbodyͷbyte਺ 01: ؔ਺ͷγάωνϟͷݸ਺ 60 02 7f 7f 01 7f: fn (i32, i32) -> i32

Slide 23

Slide 23 text

WasmͷηΫγϣϯ: Type ࣮૷ ϓϩάϥϜ͔Βؔ਺Λ୳ͯ͠ γάωνϟຖʹu8ͷvectorΛ࡞੒͢Δ

Slide 24

Slide 24 text

WasmͷηΫγϣϯ: Function ɾWasmϞδϡʔϧ಺Ͱ࢖༻͞ΕΔ֤ؔ਺ͷએݴ ɾؔ਺ͱγάωνϟΛඥ͚ͮΔ 03: ηΫγϣϯ൪߸ 02: ηΫγϣϯbodyͷbyte਺ 01: ؔ਺ͷ૯਺ 00: index0ͷؔ਺͸ɺindex0ͷγάωνϟʹରԠ͢Δ https://webassembly.github.io/spec/core/binary/modules.html#function-section

Slide 25

Slide 25 text

WasmͷηΫγϣϯ: Function ࣮૷ ؔ਺ͱ TypeηΫγϣϯͰొ࿥ͨ͠γάωνϟͷ IndexΛඥ͚ͮΔ

Slide 26

Slide 26 text

WasmͷηΫγϣϯ: Export ϗετ؀ڥʹఏڙ͢ΔશͯͷΦϒδΣΫτͷએݴ 07: ηΫγϣϯ൪߸ 07: ηΫγϣϯbodyͷbyte਺ 01: export͢Δݸ਺ 03: ࣝผࢠͷbyte਺ 61 64 64: ࣝผ໊add 00: exportछผ(ؔ਺) 00: exportͷindex http://webassembly.github.io/spec/core/binary/modules.html#export-section

Slide 27

Slide 27 text

WasmͷηΫγϣϯ: Export ࣮૷ Export͢Δؔ਺ͷ໊લ৘ใ ͱindexΛ٧ΊΔʂ

Slide 28

Slide 28 text

WasmͷηΫγϣϯ: Code ؔ਺ͷ಺༰Λఆٛ͢Δ (΍ͬͱຊ୊ײ) 0a: ηΫγϣϯ൪߸ 09: ηΫγϣϯbodyͷbyte਺ 01: ؔ਺ͷݸ਺ 07: ؔ਺(index:0)ͷbyte਺ 00: ؔ਺಺ͷม਺એݴ਺ 20: GetLocal 00: Ҿ਺1൪໨ 20: GetLocal 01: Ҿ਺2൪໨ 6a: plus 0b: end https://webassembly.github.io/spec/core/binary/modules.html#binary-codesec https://webassembly.github.io/spec/core/binary/instructions.html

Slide 29

Slide 29 text

WasmͷηΫγϣϯ: Code ࣮૷ ΘͪΌΘͪΌ

Slide 30

Slide 30 text

WasmͷηΫγϣϯ: Code ݁Ռʂ ଞʹ΋section͋Δ͚Ͳɺࠓճ͸͜ΕͰे෼

Slide 31

Slide 31 text

WasmόΠφϦͰ͖ͨʂ ࠓ·ͰͷsectionΛܨ͗߹Θͤͯ… ׬੒ʂ

Slide 32

Slide 32 text

݁Ռ: ಠࣗιʔεͷ༻ҙͱίϯύΠϧ ؔ਺ͷ಺༰Λఆٛ͢Δ

Slide 33

Slide 33 text

݁Ռ: wasmಡΈࠐΈ&ϒϥ΢βͰ࣮ߦ python3 -m http.server Index.html ॕʂʂ🎉

Slide 34

Slide 34 text

·ͱΊ ɾࣗ࡞ݴޠ͕ಈ͘ͷ͸ͨͷ͍͠ ɾWasiରԠͱ͔͍ͨ͠ ɾࠓޙ΋௥͍͖͍ٕͬͯͨज़

Slide 35

Slide 35 text

ࢀߟจݙ wasm-compiler-in-rust https://github.com/karamaru-alpha/wasm-compiler-in-rust WebAssembly binary format. https://webassembly.github.io/spec/core/binary/ ϋϯζΦϯWebAssembly https://www.oreilly.co.jp/books/9784814400102/ WebAssemblyͷྺ࢙ʹ͍ͭͯ https://zenn.dev/hodagi/articles/4925afbeb3c4dc#fn-9e18-10 Linuxίϯςφͷ࣍ͱͯ͠ͷWebAssembly https://zenn.dev/koduki/articles/9f86d03cd703c4

Slide 36

Slide 36 text

fin.