Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Ruby in Ruby - Building an AOT Compiler with Ruby
Search
MATSUMOTO Katsuyoshi
September 25, 2024
Programming
410
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Ruby in Ruby - Building an AOT Compiler with Ruby
MATSUMOTO Katsuyoshi
September 25, 2024
More Decks by MATSUMOTO Katsuyoshi
See All by MATSUMOTO Katsuyoshi
A Journey of Box bugs
katsyoshi
0
94
POI: Proof of Identity
katsyoshi
0
210
(私的)RubyKaigi の歩き方
katsyoshi
0
1.8k
Building Ruby Native Extension using Ruby
katsyoshi
0
2.3k
ちいさなクラウド業者やってみた
katsyoshi
0
230
fluentdとxlsx
katsyoshi
0
200
mikutterとJubatusとfluentd
katsyoshi
0
11k
ZFSでNASやってはまったこと
katsyoshi
0
2.1k
Asakusa.rbに一年間通ったらこうなった
katsyoshi
1
2.8k
Other Decks in Programming
See All in Programming
Herb in Rails 8.2: Your ERB views, now HTML-aware @ Rails World 2026, Austin, Texas
marcoroth
0
130
MVNOの申込からeSIM開通までをiOSアプリでつなぐ- 本人確認・MNP・通信事業者基盤をまたぐ実装
satotakeshi
0
510
[Rails World 2026] Durable orchestration on Rails: from continuation to workflow
palkan
1
400
大喜利で理解するLLM as a Judge / Understanding LLM-as-a-Judge through Ogiri
rockname
0
180
Androidだけじゃない、DroidKaigiで広がった私の技術スタック
akkeylab
0
100
Embedded Swiftで作る自作USBデバイス によるiOSデバイスの自動テスト / iOSDC Japan 2026 glassfiber
glassfiber
0
200
標準パッケージに uuid が追加された 背景から見る Go らしい意思決定 / go_127_uuid_decision
convto
5
9.2k
動作中のプログラムの中身をリアルタイムに覗く / Realtime Debugger for CSharp with Roslyn
prota
1
1.6k
wkhtmltopdfの次どうするか問題2026
willnet
2
1.8k
iOSDC2026登壇資料.pdf
riofujimon
0
200
Intent as Code
shoppingjaws
6
1.1k
ソニーのクラウド共通基盤の変遷とAI時代の開発スタイルに合わせた進化 / The Journey of Sony’s Common Cloud Platform and Its Evolution for AI-Native Development
kenjiyoneyama
0
200
Featured
See All Featured
The Art of Programming - Codeland 2020
erikaheidi
57
14k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
390
Side Projects
sachag
456
43k
Ethics towards AI in product and experience design
skipperchong
2
400
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.8k
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
290
Rebuilding a faster, lazier Slack
samanthasiow
85
9.7k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
550
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
300
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
530
Utilizing Notion as your number one productivity tool
mfonobong
4
600
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
560
Transcript
Ruby in Ruby Building an AOT Compiler with Ruby katsyoshi
Repro Inc.
self
self 開発者募集中!!! Rails や Kafka をやりたい/できるひと 上記以外にも Go での配信基盤開発や Java/Objective-C
などでAndroid/iOS SDK開発も
self Twitter, GitHub, bluesky…: katsyoshi Hobbies: keyboard, fishing, watching football…
Work: Rails Application Programmer
My Gems fluent-plugins fluent-plugin-serialport… itamae-plugins itamae-plugin-resource-pip… irb-theme-dracula vaporware-compiler considering rename…
Dragon Book Club: Member フロントエンドエンジニアの集まり コンパイラーとしての話です そろそろ読みたいと思い20年が経ち… @spikeolaf「#lr-parser にいる人は全員参 加するのはあたりまえだよなあ???
しゃーない行くかー よみたかったしな!
Introduction AOT コンパイラーつくるぞ Matz も言ってたしそろそろ欲しいよね とりあえずうごくもの作ろう
Self Motivation コンパイラーつくったことないし一度作って みるのもいいよね 直接バイナリー作たい!! アセンブラー書いて機械語書いてみたい! 幸い資料も実装もたくさんあるしな!
Simmilar Implementations LLVM Backends: RubyMotion うーん… Rust Backends: monoruby もうやってるひといるしな
Go Backends: goruby もうやってるひといるしな Ruby でやるぞ!!!
C Language Compile Flow C言語でコンパイルすると上図のようになる これらの流れを Ruby でもできるようにするぞ! 全部一旦実装する!
Create Components Compiler Assembler Linker
Compiler 整数演算と簡単な制御構文が使えるようにす る 四則演算 変数 条件分岐 if, while
Compiler Ruby が AST(Abstruct Syntax Tree) を持っ ている Ruby からアクセス可能
Compiler RubyVM::AbstructSyntaxTree.parse 木構造なので辿るだけで大丈夫 x86_64 Assembly 言語へ変換
Assembler 中間言語表現からオブジェクトファイルを生 成 今回は x86_64 Assembly を選択 決まった中間言語表現から Binary Format
へ変換するだけ ということで Format を決めましょう
Binary Format Linux ELF
ELF Executable and Linking Format Linux などの Unix 系 OS
で動作するバイナ リフォーマット
Structure of ELF ELF Header Sections Section Headers
Structure of ELF
ELF Header ELF オブジェクトファイルのヘッダー ファイルサイズなどが書かれている 0x40 bytes
ELF Header
Sections Text Section: 本体 Data Section, BSS Section, Note Section
今回はサポートしないHeaderと必要最低限の実装 Symtab Section Strtab Section Shstrtab Section
Sections: Text Section プログラムの本体
Section Headers 各 Section の開始点など情報を持つ部分
Structure of ELF 完全理解した あとは実装するだけ!
Assembler Assembly 言語から機械語へ変換する 今回はx86_64のELFへ変換
Learn x86_64 Machine Language 次に機械語を覗いてみよう 基本的に Assembly に書いてある命令をその まま変換すればいい
Learn x86_64 Machine Language
Learn x86_64 Machine Language やりたいこと: 64bit で加算したい 加算命令: ADD REX.W
01 と書いてあるやつを選べばよさそう
Linker オブジェクトファイルと実行に必要なライブ ラリを繋げてくれて実行ファイルを作ってく れるやつ 今回はここはできあいの mold, gold, lld で
Current ((1 + 2) * 3) / (5-4) 上記のコードをx86_64 Assembly
言語にコ ンパイル コンパイルされた Assembly 言語から ELF のオブジェクトファイルへアセンブル ELF のオブジェクトファイルと必要なライブ ラリをリンクし実行ファイルを生成
Current まだまだ実装たりない 簡単なコードを AST からネイティブに変換 しただけ Linker もない
Futures Linker を作る 構文サポート 制御構文、クラス、メソッド…
Conclusion Compiler、Assembler を作ってみた 現状としてはまだまだ ベンチマークとれるレベルまで持っていく