Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
自作言語をrustでwasmにコンパイルする
Search
karamaru
PRO
February 01, 2023
0
33
自作言語をrustでwasmにコンパイルする
自作言語をparseして、wasmの四則演算と関数exportにマッピングするまでを行いました。
karamaru
PRO
February 01, 2023
Tweet
Share
More Decks by karamaru
See All by karamaru
unique パッケージから学ぶ interning と weak reference @ Asakusa.go#3
karamaru
PRO
3
1k
ぼくのかんがえたさいきょうのもっくらいぶらり! @ GoConnect#1
karamaru
PRO
0
62
詳解 "Fixing For Loops in Go 1.22" 自作linterをgolangci-lintへコントリビュートした話 @ GoConference2024
karamaru
PRO
0
23
zsh*単一デスクトップ環境最速を目指して
karamaru
PRO
0
140
対isuconメトリクスツール isumaru作った
karamaru
PRO
0
36
Featured
See All Featured
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
Practical Orchestrator
shlominoach
186
10k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
720
Statistics for Hackers
jakevdp
796
220k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
Speed Design
sergeychernyshev
24
610
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
Keith and Marios Guide to Fast Websites
keithpitt
409
22k
Code Review Best Practice
trishagee
64
17k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
31
2.7k
Transcript
ࣗ࡞ݴޠΛ wasmʹίϯύΠϧ͢Δ karamaru
݁ Rustͷࣗ࡞ݴޠΛࣗྗͰwasmόΠφϦʹมͯ͠ ϒϥβͰ࣮ߦͯ͠ΈͨΑʂ (࢛ଇԋࢉͱؔexportͷΈ)
͜Μͳײ͡ ࣗ࡞ݴޠ wasmόΠτίʔυ ϒϥβͰ࣮ߦ ͜͜Λ࡞Δ!
ɾWebAssembly֓ཁ ɾHello! Wasm! ɾast·Ͱ࡞Δ ɾࣈ۟ղੳ ɾߏจղੳ ɾwasmมΛ࡞Δ ɹɾόΠφϦ༷ࣜ ࣍
Wasm֓ཁ ɾϒϥβͰಈ͘όΠτίʔυ ɾjsҎ֎ͷߴͳݴޠͰॲཧΛ࣮ߦͤ͞Δ͜ͱ͕తͩͬͨ -> લasm.jsɻjsαϒηοτͱͯ͠ಈ࡞ͯ͠AOTίϯύΠϧ͕ߦΘΕΔɻ ɾԾతͳϋʔυΣΞͰಈ͘ਫ४ͳ໋ྩͷू·Γͱͯ͠ϞδϡʔϧΛఆٛͰ͖Δ -> runtimeଆͰ໌ࣔతʹڐՄ͞Εͨͷ͚࣮ͩߦՄೳ (sandbox) ɾݴޠෆͰίϯύΠϧͰ͖Δ
ɾ͜ͷنΛԠ༻ͯ͠ϒϥβ֎Ͱ࣮ߦ͠Α͏ͱ͢Δಈ͖࠷ۙଟ͍Ͱ͢ΑͶ
ࣗ࡞ݴޠͱwasm ࣗ࡞ݴޠ͔ΒwasmΛు͚ΔΑ͏ʹͳΕ ଞͷݴޠͷruntimeʹཔΒͣಈ͔͢͜ͱ͕Ͱ͖Δʂ LLVM IRมͰྑ͍͚Ͳwasmు͖͍ͨʂ
Πϝʔδ Chorome console ಠࣗݴޠ(text)͔Β ͜ΕܥͷόΠτίʔυΛు͚Ε͍͍
͜Μͳײ͡ͰਐΊ·͢ 1. ࣗ࡞ݴޠͷίʔυ४උ 2. τʔΫϯԽ lexser 3. ߏจղੳ parser 4.
wasmԽ emitter
ࣗ࡞ݴޠͷίʔυ ܕͳ͠ͷؔͬΆ͍ͭʂ ͜ΕΛwasmԽͯ͠ ϒϥβͰ࣮ߦ͢Δ·ͰؤுΔ🔥
ࣈ۟ղੳ Lexser ·ͣࣈ۟ղੳʂ ϓϩάϥϜΛτʔΫφΠζͯ͠ɺ ಠࣗݴޠΛಡΈਐΊΔࡍͷ࠷খ୯Ґ(τʔΫϯ)ͷࣝผ͢Δɻ fn … ؔએݴ add …
ࣝผࢠ ( … ࠨׅހ a … ࣝผࢠ , … ΧϯϚ ͱ͔ɺ ه߸,ࣝผࢠͳͲͷίʔυͷ࠷খ୯ҐΛཧղ͠·͢
ࣈ۟ղੳ ࣮ جຊ1จࣈͣͭ ύλʔϯϚονͰผ enumʹΛ࣋ͨͤΒΕΔ
ࣈ۟ղੳث ʂ ࣮ࡍߏจղੳͰιʔείʔυΛಡΈਐΊΔࡍʹ ࣈ۟ղੳ͢ΔΜ͚ͩͲݟ͢͞తʹશ෦ग़ͯ͠Δ τʔΫϯԽʹޭ͠·ͨ͠Ͷ
ߏจղੳ ࣍ߏจղੳʂ ࿈ଓ͢ΔτʔΫϯྻ͔Βҙຯͷ͋Δ·ͱ·Γ(ߏจ)Λࣝผ͢Δɻ Lexser ͜ͷτʔΫϯྻ… ɾࣝผࢠʮaddʯͰఆٛ͞ΕΔؔఆٛͩʂ ɾࣝผࢠʮaʯͱʮbʯͰఆٛ͞ΕΔҾ͕͋Δʂ ɾ෦Ͱʮa + b
* 2ʯΛߦͳ͍ͬͯΔʂ తͳ
ߏจղੳ ܕఆٛ ϓϩάϥϜจ(statement)ͷू߹ମ ܕؔΛࣜ(expression)ͱͯ͠ѻ͏ If/for/letͱ͔࣮ͯ͠ͳ͍ͷͰจࣜͷΈ
ߏจղੳ ࣮ Expression::Infix( Infix::Plus, Expression::Integer(1), Expression::Integer(2), ) ྫ͑ʮɹ1 + 2ɹʯͩͬͨΒ
precedence: ༏ઌॱҐ
ߏจղੳث AST͕Ͱ͖·ͨ͠Ͷ ֻ͚ࢉΛઌʹͬͯΔͷ͑Β͍ʂ
͜Ε͔Β͍ͨ͜͠ͱ wasmόΠτίʔυ ͜͜Λ࡞Δʂ WasmόΠφϦܗࣜΛݟΔඞཁ͕͋Δ Emitter
WasmόΠφϦΛͭͬͯ͘ΈΑ͏ʂ όΠφϦܗࣜΛֶͿͨΊʹaddؔͷwasmΛͭ͘Δʂ ɾwasmςΩετܗࣜ(Sࣜ)Ͱهड़Ͱ͖Δ = wat ɾwat2wasmΛ༻͍ͯมͰ͖Δ wasmελοΫϚγϯͳΜͩͶʂ
WasmόΠφϦΛݟͯΈΑ͏ʂ ɾγϯϓϧͳ͠ࢉؔͷexport41byteͰදݱͰ͖Δʂ ɾಠࣗݴޠͷιʔείʔυ(ast)͔Β͜ΕΛग़ͤΕউͪ AddؔͷwasmΛݩʹɺܗࣜΛਅࣅ͍ͯ͜͏ʂ
WasmͷηΫγϣϯ: ϓϦΞϯϒϧ ɾ4όΠτͷϚδοΫφϯόʔʢasmͷASCIIίʔυʣɻwasmϞδϡʔϧͩͱೝࣝͤ͞Δɻ ɾ4όΠτόʔδϣϯ൪߸ (ݱࡏ1) https://webassembly.github.io/spec/core/binary/modules.html#binary-magic
WasmͷηΫγϣϯ: ϓϦΞϯϒϧ ࣮ ɾ8όΠτ٧ΊΔ͚ͩ ɾ͜Ε͔ΒemitؔͰast͔ΒbytecodeมΛߦ͏
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
WasmͷηΫγϣϯ: Type ࣮ ϓϩάϥϜ͔ΒؔΛ୳ͯ͠ γάωνϟຖʹu8ͷvectorΛ࡞͢Δ
WasmͷηΫγϣϯ: Function ɾWasmϞδϡʔϧͰ༻͞ΕΔ֤ؔͷએݴ ɾؔͱγάωνϟΛඥ͚ͮΔ 03: ηΫγϣϯ൪߸ 02: ηΫγϣϯbodyͷbyte 01: ؔͷ૯
00: index0ͷؔɺindex0ͷγάωνϟʹରԠ͢Δ https://webassembly.github.io/spec/core/binary/modules.html#function-section
WasmͷηΫγϣϯ: Function ࣮ ؔͱ TypeηΫγϣϯͰొͨ͠γάωνϟͷ IndexΛඥ͚ͮΔ
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
WasmͷηΫγϣϯ: Export ࣮ Export͢Δؔͷ໊લใ ͱindexΛ٧ΊΔʂ
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
WasmͷηΫγϣϯ: Code ࣮ ΘͪΌΘͪΌ
WasmͷηΫγϣϯ: Code ݁Ռʂ ଞʹsection͋Δ͚Ͳɺࠓճ͜ΕͰे
WasmόΠφϦͰ͖ͨʂ ࠓ·ͰͷsectionΛܨ͗߹Θͤͯ… ʂ
݁Ռ: ಠࣗιʔεͷ༻ҙͱίϯύΠϧ ؔͷ༰Λఆٛ͢Δ
݁Ռ: wasmಡΈࠐΈ&ϒϥβͰ࣮ߦ python3 -m http.server Index.html ॕʂʂ🎉
·ͱΊ ɾࣗ࡞ݴޠ͕ಈ͘ͷͨͷ͍͠ ɾWasiରԠͱ͔͍ͨ͠ ɾࠓޙ͍͖͍ٕͬͯͨज़
ࢀߟจݙ 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
fin.